From eb4a0119e06ee8e88f2805b74c87d198f8d5d1d0 Mon Sep 17 00:00:00 2001 From: yutent Date: Sun, 4 Feb 2024 18:46:04 +0800 Subject: [PATCH] update --- src/index.js | 12 ++++++ src/lib/base.js | 25 ++++++++++++ src/lib/config.js | 81 ++++++++++++++++++++++++++++++++++++++ src/lib/font.js | 14 +++++++ src/lib/glyph.js | 14 +++++++ src/lib/svg2ttf/lib/svg.js | 1 + test/game.svg | 1 + test/home.svg | 1 + test/index.js | 7 +++- 9 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/lib/base.js create mode 100644 src/lib/config.js create mode 100644 src/lib/font.js create mode 100644 src/lib/glyph.js create mode 100644 test/game.svg create mode 100644 test/home.svg diff --git a/src/index.js b/src/index.js index ee9024e..68388f1 100644 --- a/src/index.js +++ b/src/index.js @@ -5,3 +5,15 @@ */ export { svg2ttf } from './lib/core.js' + +export function createFont(opt = {}) { + // +} + +/** + * @param input {String、Buffer, path} svg, ttf字体 + * @return {Font} 字体对象 + */ +export function transfer(input, opt = {}) { + // +} diff --git a/src/lib/base.js b/src/lib/base.js new file mode 100644 index 0000000..bed4785 --- /dev/null +++ b/src/lib/base.js @@ -0,0 +1,25 @@ +/** + * {基类} + * @author yutent + * @date 2024/02/04 18:13:31 + */ + +export class Controller { + #options = {} + + constructor(opt) { + this.setOptions(opt) + } + + setOptions(opt = {}) { + Object.assign(this.#options, opt) + } + + $get(k) { + return this.#options[k] + } + + $set(k, v) { + this.#options[k] = v + } +} diff --git a/src/lib/config.js b/src/lib/config.js new file mode 100644 index 0000000..f358b15 --- /dev/null +++ b/src/lib/config.js @@ -0,0 +1,81 @@ +/** + * {} + * @author yutent + * @date 2024/02/04 18:18:04 + */ + +//导出svg的配置 +export const DEFAULT_EXPORT_OPTIONS = { + width: '100px', + height: '100px' +} + +//path 保留小数位 +export const PATH_DECIMAL = 4 + +export const FONT_FAMILY = 'iconfont' + +//默认的配置参数 +export const DEFAULT_OPTIONS = { + font: { + id: FONT_FAMILY, + horizAdvX: 1024, + vertAdvY: 1024 + }, + fontface: { + fontFamily: FONT_FAMILY, + fontWeight: '400', + fontStretch: 'normal', + unitsPerEm: '1024', + ascent: '812', + descent: '-212' + }, + glyph: { + unicode: '', + glyphName: '', + d: '', + horizAdvX: 1024, + vertAdvY: 1024 + } +} + +export const FONT_TMPL = ` + + + + + Created by font-carrier + + + + + <% print(v + '="' + fontface[v] + '"') %> + <%} %> + /> + + + <% if(!hasX){ %> + + <% } %> + + <% for(var i in glyphs){ + var glyph = glyphs[i].options; + %> + <% if (glyph['vertAdvY']) print('vert-adv-y="'+ glyph['vertAdvY']+'"') %> /> + + <% } %> + + + + +` + +export const SVG_TMPL = ` + + + x="0" y="0" width="<%= options['width'] %>" height="<%= options['height'] %>" <% } %> viewBox="0 0 <%= glyph['horizAdvX'] %> <%= glyph['vertAdvY'] %>"> + + +` diff --git a/src/lib/font.js b/src/lib/font.js new file mode 100644 index 0000000..fa0d4ed --- /dev/null +++ b/src/lib/font.js @@ -0,0 +1,14 @@ +/** + * {} + * @author yutent + * @date 2024/02/04 18:16:10 + */ + +import { Controller } from './base.js' +import { DEFAULT_OPTIONS } from './config.js' + +export class Font extends Controller { + constructor(opt) { + super(Object.assign({}, DEFAULT_OPTIONS.font, opt)) + } +} diff --git a/src/lib/glyph.js b/src/lib/glyph.js new file mode 100644 index 0000000..43bdcd0 --- /dev/null +++ b/src/lib/glyph.js @@ -0,0 +1,14 @@ +/** + * {} + * @author yutent + * @date 2024/02/04 18:16:10 + */ + +import { Controller } from './base.js' +import { DEFAULT_OPTIONS } from './config.js' + +export class Font extends Controller { + constructor(opt) { + super(Object.assign({}, DEFAULT_OPTIONS.glyph, opt)) + } +} diff --git a/src/lib/svg2ttf/lib/svg.js b/src/lib/svg2ttf/lib/svg.js index 695d768..fce8862 100644 --- a/src/lib/svg2ttf/lib/svg.js +++ b/src/lib/svg2ttf/lib/svg.js @@ -91,6 +91,7 @@ export function load(str) { fontElem = doc.getElementsByTagName('font')[0] if (!fontElem) { + console.log(doc) throw new Error( "Can't find tag. Make sure you SVG file is font, not image." ) diff --git a/test/game.svg b/test/game.svg new file mode 100644 index 0000000..c876bb9 --- /dev/null +++ b/test/game.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/home.svg b/test/home.svg new file mode 100644 index 0000000..1d1ac8b --- /dev/null +++ b/test/home.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/index.js b/test/index.js index fa5c619..a9e51eb 100644 --- a/test/index.js +++ b/test/index.js @@ -5,5 +5,10 @@ */ import { svg2ttf } from '../src/index.js' +import fs from 'iofs' -console.log(svg2ttf) +let svg = fs.cat('test/game.svg').toString() + +let ttf = svg2ttf(svg) + +console.log(ttf)