Skip to content

Commit

Permalink
feat: add getIconUnicode option. #204
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 8, 2023
1 parent deb2408 commit c271be5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ The path of the templates, see `src/styles` or `test/templates/styles` to get re
unicode start number

### getIconUnicode

Get Icon Unicode

```ts
getIconUnicode?: (name: string, unicode: string, startUnicode: number)
=> [string, number];
```

### useNameAsUnicode

> Type: `Boolean`
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export type SvgToFontOptions = {
* @default 10000
*/
startUnicode?: number;
/** Get Icon Unicode */
getIconUnicode?: (name: string, unicode: string, startUnicode: number) => [string, number];
/**
* should the name(file name) be used as unicode? this switch allows for the support of ligatures.
* @default false
Expand Down
28 changes: 16 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,21 @@ export function createSVG(options: SvgToFontOptions = {}): Promise<Record<string
// file name
let _name = path.basename(svgPath, ".svg");
const glyph = fs.createReadStream(svgPath) as ReadStream & { metadata: { unicode: string[], name: string } };
glyph.metadata = { unicode: getIconUnicode(_name, options.useNameAsUnicode), name: _name };

const curUnicode = String.fromCharCode(startUnicode);
const [_curUnicode, _startUnicode] = options.getIconUnicode
? (options.getIconUnicode(_name, curUnicode, startUnicode) || [curUnicode]) : [curUnicode];

if (_startUnicode) startUnicode = _startUnicode;

const unicode: string[] = [_curUnicode];
startUnicode++;
UnicodeObj[_name] = unicode[0];
if (!!options.useNameAsUnicode) {
unicode[0] = _name;
UnicodeObj[_name] = _name;
}
glyph.metadata = { unicode , name: _name };
fontStream.write(glyph);
}

Expand All @@ -63,6 +77,7 @@ export function createSVG(options: SvgToFontOptions = {}): Promise<Record<string
fontStream.end();
});
}

/**
* Converts a string to pascal case.
*
Expand Down Expand Up @@ -135,17 +150,6 @@ export async function createTypescript(options: Omit<SvgToFontOptions, 'typescri
log.log(`${color.green('SUCCESS')} Created ${DIST_PATH}`);
}

/*
* Get icon unicode
* @return {Array} unicode array
*/
function getIconUnicode(name: string, useNameAsUnicode: boolean) {
let unicode = !useNameAsUnicode ? String.fromCharCode(startUnicode++) : name;
UnicodeObj[name] = unicode;
return [unicode];
}


/**
* SVG font to TTF
*/
Expand Down

2 comments on commit c271be5

@chenyulun
Copy link
Contributor

@chenyulun chenyulun commented on c271be5 Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果没有配置当前的Unicode且没有重新设置起始的Unicode,才需要加1

换句话说就是用户重设了不同Unicode或者重置了不一样的起始的Unicode都不需要加1,因为老的curUnicode没被使用

if(curUnicode === _ curUnicode && startUnicode === _startUnicode) startUnicode++;

@jaywcjlove
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chenyulun 更新 v3.25.2 已经优化

Please sign in to comment.