master
yutent 2024-02-22 18:49:29 +08:00
parent dc87a4b287
commit 0cfeb517be
3 changed files with 70 additions and 2 deletions

View File

@ -8,7 +8,17 @@ import { Controller } from './base.js'
import { DEFAULT_OPTIONS } from './config.js' import { DEFAULT_OPTIONS } from './config.js'
export class Font extends Controller { export class Font extends Controller {
#fontface = new Controller(DEFAULT_OPTIONS.fontface)
constructor(opt) { constructor(opt) {
super(Object.assign({}, DEFAULT_OPTIONS.font, opt)) super(Object.assign({}, DEFAULT_OPTIONS.font, opt))
} }
get fontFace() {
return this.#fontface
}
set fontFace(opt = {}) {
this.#fontface = new Controller(opt)
}
} }

55
src/lib/utils.js Normal file
View File

@ -0,0 +1,55 @@
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @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
}

View File

@ -5,10 +5,13 @@
*/ */
import { svg2ttf } from '../src/index.js' import { svg2ttf } from '../src/index.js'
import { toUnicode } from '../src/lib/utils.js'
import fs from 'iofs' 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(ttf)
console.log(toUnicode('哈哈'))