Skip to content

Commit

Permalink
chore: babel deps
Browse files Browse the repository at this point in the history
  • Loading branch information
kekee000 committed Jan 6, 2023
1 parent 2056817 commit d52c5eb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 53 deletions.
95 changes: 55 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,34 @@
- woff2 (read and write)
- eot (read and write)
- svg (read and write)
- otf (only read)
- otf (only read and convert to ttf)
- ttf glyph adjust
- svg to glyph

## Usage

```js

```javascript
// read font file
const Font = require('fonteditor-core').Font;
const fs = require('fs');

let buffer = fs.readFileSync('font.ttf');
// read font data
let font = Font.create(buffer, {
type: 'ttf', // support ttf, woff, woff2, eot, otf, svg
subset: [65, 66], // only read `a`, `b` glyf
hinting: true, // save font hinting
compound2simple: true, // transform ttf compound glyf to simple
inflate: null, // inflate function for woff
combinePath: false, // for svg path
import {Font} from 'fonteditor-core';
import fs from 'fs';

const buffer = fs.readFileSync('font.ttf');
// read font data, support ArrayBuffer | Buffer | string
const font = Font.create(buffer, {
// support ttf, woff, woff2, eot, otf, svg
type: 'ttf',
// only read `a`, `b` glyphs
subset: [65, 66],
// save font hinting
hinting: true,
// transform ttf compound glyph to simple
compound2simple: true,
// inflate function for woff
inflate: null,
// for svg path
combinePath: false,
});
let fontObject = font.get();
const fontObject = font.get();
console.log(Object.keys(fontObject));

/* => [ 'version',
Expand All @@ -59,33 +64,39 @@ console.log(Object.keys(fontObject));
*/

// write font file
let buffer = font.write({
type: 'woff', // support ttf, woff, woff2, eot, svg
hinting: true, // save font hinting
deflate: null, // deflate function for woff
support: {head: {}, hhea: {}} // for user to overwrite head.xMin, head.xMax, head.yMin, head.yMax, hhea etc.
const buffer = font.write({
// support ttf, woff, woff2, eot, svg
type: 'woff',
// save font hinting
hinting: true,
// deflate function for woff, eg. pako.deflate
deflate: null,
// for user to overwrite head.xMin, head.xMax, head.yMin, head.yMax, hhea etc.
support: {head: {}, hhea: {}}
});
// fs.writeFileSync('font.woff', buffer);
fs.writeFileSync('font.woff', buffer);

// to base64 str
font.toBase64({
type: 'ttf' // support ttf, woff, woff2, eot, svg
// support ttf, woff, woff2, eot, svg
type: 'ttf'
});

// optimize glyf
// optimize glyphs
font.optimize()

// compound2simple
font.compound2simple()

// sort glyf
// sort glyphs
font.sort()

// find glyf
let result = font.find({
// find glyphs
const result = font.find({
unicode: [65]
});
let result = font.find({

const result = font.find({
filter: function (glyf) {
return glyf.name == 'icon'
}
Expand All @@ -95,31 +106,35 @@ let result = font.find({
font.merge(font1, {
scale: 1
});

```

### woff2

Notice: woff2 use wasm build of google woff2, before read and write `woff2`,
you should first call `woff2.init()`.
**Notice: ** woff2 use wasm build of google woff2, before read and write `woff2`, we should first call `woff2.init()`.

```javascript
const Font = require('fonteditor-core').Font;
const woff2 = require('fonteditor-core').woff2;
import {Font, woff2} from 'fonteditor-core';

// in nodejs
woff2.init().then(() => {
// read
let font = Font.create(buffer, {
type: 'woff2'
});
// write
font.write({type: 'woff2'});
// read woff2
const font = Font.create(buffer, {
type: 'woff2'
});
// write woff2
const buffer = font.write({type: 'woff2'});
});

// in browser
woff2.init('/assets/woff2.wasm').then(() => {
// read woff2
const font = Font.createEmpty();
// write woff2
const arrayBuffer = font.write({type: 'woff2'});
});
```



## Demo

```
Expand Down
17 changes: 12 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ export namespace FontEditor {
hinting?: boolean;

/**
* tranfrom compound glyph to simple, default true
* tranfrom compound glyph to simple,
* @default true
*/
compound2simple?: boolean;

Expand All @@ -211,7 +212,8 @@ export namespace FontEditor {
inflate?: (deflatedData: UInt8[]) => UInt8[];

/**
* combine svg paths to one glyph in one svg file. default true
* combine svg paths to one glyph in one svg file.
* @default true
*/
combinePath?: boolean;
}
Expand Down Expand Up @@ -269,6 +271,11 @@ export namespace FontEditor {

class Font {

/**
* create empty font object
*/
static create(): Font;

/**
* create font object with font data
*
Expand All @@ -285,7 +292,7 @@ export namespace FontEditor {
static toBase64(buffer: FontInput): string;

/**
* create empty ttf object
* read empty ttf object
*/
readEmpty(): Font;

Expand Down Expand Up @@ -322,9 +329,9 @@ export namespace FontEditor {

/**
* optimize glyphs
* @param outRef optimize results
* @param outRef optimize results, will get result field after optimize
*/
optimize(outRef: {result: any}): Font;
optimize(outRef?: {result: true | {repeatList: number[]}}): Font;

/**
* tranfrom compound glyph to simple, default true
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fonteditor-core",
"version": "2.1.10",
"version": "2.1.11",
"description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.",
"keywords": [
"sfnt",
Expand Down Expand Up @@ -66,12 +66,12 @@
"main": "./lib/main.js",
"module": "./src/main.js",
"devDependencies": {
"@babel/cli": "^7.6.4",
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/register": "^7.6.2",
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.12",
"@babel/preset-env": "^7.20.2",
"@babel/register": "^7.18.9",
"babel-loader": "^8.0.6",
"babel-plugin-module-resolver": "^3.2.0",
"babel-plugin-module-resolver": "^4.1.0",
"commander": "^3.0.2",
"css-loader": "^3.2.0",
"eslint": "^7.9.0",
Expand Down
4 changes: 2 additions & 2 deletions src/ttf/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class Font {
}

/**
* 创建一个空的ttfObject对象
* 设置一个空的 ttfObject 对象
*
* @return {Font}
*/
Expand Down Expand Up @@ -335,7 +335,7 @@ export default class Font {
}

/**
* 读取字体数据
* 读取字体数据返回字体对象
*
* @param {ArrayBuffer|Buffer|string} buffer 字体数据
* @param {Object} options 读取参数
Expand Down
1 change: 1 addition & 0 deletions test/spec/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fonteditor from 'fonteditor-core/main';
describe('main', function () {
it('entries', function () {
assert.ok(fonteditor.Font, 'exports');
assert.ok(fonteditor.Font.create, 'exports');
assert.ok(fonteditor.TTF, 'exports');
assert.ok(fonteditor.TTFReader, 'exports');
assert.ok(fonteditor.TTFWriter, 'exports');
Expand Down

0 comments on commit d52c5eb

Please sign in to comment.