UNPKG

@ginstone/nga-api

Version:

81 lines (80 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bin2UInt = exports.parseTitleFont = exports.TitleColor = exports.TitleFont = void 0; const StrUtils_1 = require("../../utils/StrUtils"); class TitleFont { constructor(s) { if (!s || "" === s) { this.color = dic[5]; this.bold = false; this.lineThrough = false; this.italic = false; return; } const { stid, titleFont } = (0, exports.parseTitleFont)(s); const colorData = titleFont.substring(0, 5); const index = colorData.indexOf("1"); this.color = index > -1 ? dic[index] : dic[5]; const fontData = titleFont.substring(5); this.bold = fontData.charAt(0) == '1'; this.italic = fontData.charAt(1) == '1'; this.lineThrough = fontData.charAt(2) == '1'; } } exports.TitleFont = TitleFont; /** * 标题颜色 */ class TitleColor { constructor(name, rgb) { this.name = name; this.rgb = rgb; } } exports.TitleColor = TitleColor; const dic = [ new TitleColor("红色", "D00"), new TitleColor("蓝色", "06B"), new TitleColor("绿色", "3D9F0E"), new TitleColor("棕/橙色", "A06700"), new TitleColor("银/灰色", "C0C0C0"), new TitleColor("默认", "1A3959"), ]; const parseTitleFont = (data) => { //将字串使用base64解码,并切割为单字节数组 const s = window.atob(data).split(""); //将各字节转换为8位二进制数组(补齐位数) const array = s .map(i => (0, exports.bin2UInt)(i).toString(2)) .map(i => ('00000000' + i).slice(-8)); let stid; let titleFont = ""; //以5为步长循环该数组(如果为合集主题array长度为10,否则为5) for (let i = 0; i < array.length - 1; i += 5) { //首字节表示数据类型 const type = parseInt(array[i], 2) === 2 ? "stid" : 'bit'; //将后续4个字节数据拼接并转换为十进制数 let bit = parseInt(array.slice(i + 1, i + 5).join(''), 2); if (type === 'stid') { //如果数据类型为1 , 表示bit为集合id stid = bit; } if (type === 'bit') { //如果是字体数据,把数据转换为2进制字符串,并反向方便后续处理 titleFont = (0, StrUtils_1.parseBit)(bit); } } return { stid, titleFont }; }; exports.parseTitleFont = parseTitleFont; //二进制字符串转为多字节整数(big-endian) const bin2UInt = (x) => { let z = 0, y = 0; for (let i = 0; i < x.length; i++) { y = x.charCodeAt(i); //如果输入字符串中有utf16字符则一次移动两字节 z = (z << (y > 255 ? 16 : 8)) + y; } return z; }; exports.bin2UInt = bin2UInt;