From 0cfeb517bead1b4688e99e6395ded6e104ff925e Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 22 Feb 2024 18:49:29 +0800 Subject: [PATCH] update --- src/lib/font.js | 10 +++++++++ src/lib/utils.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ test/index.js | 7 ++++-- 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/lib/utils.js diff --git a/src/lib/font.js b/src/lib/font.js index fa0d4ed..9393147 100644 --- a/src/lib/font.js +++ b/src/lib/font.js @@ -8,7 +8,17 @@ import { Controller } from './base.js' import { DEFAULT_OPTIONS } from './config.js' export class Font extends Controller { + #fontface = new Controller(DEFAULT_OPTIONS.fontface) + constructor(opt) { super(Object.assign({}, DEFAULT_OPTIONS.font, opt)) } + + get fontFace() { + return this.#fontface + } + + set fontFace(opt = {}) { + this.#fontface = new Controller(opt) + } } diff --git a/src/lib/utils.js b/src/lib/utils.js new file mode 100644 index 0000000..e9892b2 --- /dev/null +++ b/src/lib/utils.js @@ -0,0 +1,55 @@ +/** + * {} + * @author yutent + * @date 2024/02/22 18:38:26 + */ + +/** + * 字体里面的unicode千奇百怪有很多种形式,需要进行转换成16进制的unicode + * 返回最终的16进制值组成的字符串 + */ +export function str2unicode(str) { + //没有管\\u的情况 + str = str.replace(/&#(x?[a-f\d]+)(;?)/gi, function (m, u) { + let HEX_BASE = 10 + if (u.indexOf('x') === 0) { + HEX_BASE = 16 + u = u.substr(1) + } + return String.fromCodePoint(parseInt(u, HEX_BASE)) + }) + //将ascii码转换成unicode + str = str.replace(/[\x00-\xff]/g, s => `&#${s.codePointAt(0)};`) + //将汉字转成unicode + str = str.replace(/[^\u0000-\u00FF]/g, _ => { + return escape(_).replace(/(%u)(\w{4})/gi, '&#x$2;') + }) + //将十进制的unicode转换成16进制的。 + str = str.replace(/\&\#(\d+)/g, (_, n) => `&#x${(+n).toString(16)}`) + + return str +} + +export function toUnicode(str) { + let uArray = str2unicode(str) + uArray = uArray.split(';') + uArray.pop() + return uArray.map(n => n + ';') +} + +export function normalizeUnicode(str) { + return toUnicode(str)[0] +} + +export function toLine(str) { + return str.replace(/([A-Z]{1})/g, (_, s) => '-' + s.toLowerCase()) +} + +export function key2underline(obj) { + let result = {} + for (let k in obj) { + k = toLine(k) + result[k] = obj[k] + } + return result +} diff --git a/test/index.js b/test/index.js index f1dadac..05b0c45 100644 --- a/test/index.js +++ b/test/index.js @@ -5,10 +5,13 @@ */ import { svg2ttf } from '../src/index.js' +import { toUnicode } from '../src/lib/utils.js' import fs from 'iofs' -let svg = fs.cat('test/game.svg').toString() +// let svg = fs.cat('test/game.svg').toString() -let ttf = svg2ttf(svg) +// let ttf = svg2ttf(svg) // console.log(ttf) + +console.log(toUnicode('哈哈'))