Skip to content

Commit

Permalink
Merge pull request #43 from KonghaYao/feature/analyze
Browse files Browse the repository at this point in the history
Feature/analyze
  • Loading branch information
KonghaYao authored Jan 7, 2024
2 parents 507baf6 + 71e1a82 commit 1cbf4c3
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/font-analyze/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "font-analyze",
"version": "1.3.1",
"version": "1.3.4",
"description": "",
"main": "dist/index.js",
"type": "module",
Expand All @@ -17,7 +17,7 @@
"license": "ISC",
"dependencies": {
"@types/opentype.js": "^1.3.4",
"@konghayao/opentype.js": "^2023.10.5"
"@konghayao/opentype.js": "^2023.11.20"
},
"devDependencies": {
"compressing": "^1.9.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/font-analyze/src/Charset/FontSetMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export const calcCharset = (charset: Charset) => {
}, 0);
};

/** 字符集判断 */
/** 判断字体中是否包含 json 文件的 unicode 字符 */
export const FontSetMatch = (
font: Font,
items: Set<number>,
charset: Charset,
name: string
name: string,
): CharsetReporter => {
let support_count = 0;
let area_count = 0;
Expand Down
3 changes: 3 additions & 0 deletions packages/font-analyze/src/Charset/defaultCharsetLoader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Charset } from './FontSetMatch.js';
import { UnicodeCharset } from './UnicodeMatch.js';

/** 字符集支持报告的表格 */
export interface CharsetReporter {
name: string;
cn?: string;
Expand All @@ -11,6 +12,8 @@ export interface CharsetReporter {
coverage: string;
in_set_rate: string;
}

/** 从远程加载数据 json 文件 */
export type CharsetLoader = (path: string) => Promise<Charset | UnicodeCharset>;
export const defaultCharsetLoader: CharsetLoader = async (path) => {
const { default: D } = await import('../data/' + path);
Expand Down
13 changes: 7 additions & 6 deletions packages/font-analyze/src/Charset/getCharsetReport.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { Font } from '@konghayao/opentype.js';
import { Charset, FontSetMatch } from './FontSetMatch.js';
import { UnicodeCharset, UnicodeMatch } from './UnicodeMatch.js';
import { CharsetLoader } from './defaultCharsetLoader.js';

/** 获取 unicode 字符集检测报告 */
export async function getCharsetReport(
charsetLoader: CharsetLoader,
font: any,
unicodeSet: Set<number>
font: Font,
unicodeSet: Set<number>,
) {
const standard = await getCharsetStandard(charsetLoader, font, unicodeSet);
// console.table(standard);
const Unicode = await charsetLoader('unicodes.json');
const unicodeReport = UnicodeMatch(
font,
unicodeSet,
Unicode as UnicodeCharset
Unicode as UnicodeCharset,
);
return { unicodeReport, standard };
}
/** 获取标准字符集数据 */
async function getCharsetStandard(
charsetLoader: CharsetLoader,
font: any,
unicodeSet: Set<number>
font: Font,
unicodeSet: Set<number>,
) {
return await Promise.all(
[
Expand All @@ -45,6 +46,6 @@ async function getCharsetStandard(
].map(async ([_path, name]) => {
const set = await charsetLoader(_path);
return FontSetMatch(font, unicodeSet, set as Charset, name);
})
}),
);
}
147 changes: 147 additions & 0 deletions packages/font-analyze/src/Feature/FeatureList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
export const FeatureList = [
'aalt',
'abvf',
'abvm',
'abvs',
'afrc',
'akhn',
'blwf',
'blwm',
'blws',
'calt',
'case',
'ccmp',
'cfar',
'chws',
'cjct',
'clig',
'cpct',
'cpsp',
'cswh',
'curs',
...[...Array(99).keys()].map(
(i) => 'cv' + (i + 1).toString().padStart(2, '0'),
),
'c2pc',
'c2sc',
'dist',
'dlig',
'dnom',
'dtls',
'expt',
'falt',
'fin2',
'fin3',
'fina',
'flac',
'frac',
'fwid',
'half',
'haln',
'halt',
'hist',
'hkna',
'hlig',
'hngl',
'hojo',
'hwid',
'init',
'isol',
'ital',
'jalt',
'jp78',
'jp83',
'jp90',
'jp04',
'kern',
'lfbd',
'liga',
'ljmo',
'lnum',
'locl',
'ltra',
'ltrm',
'mark',
'med2',
'medi',
'mgrk',
'mkmk',
'mset',
'nalt',
'nlck',
'nukt',
'numr',
'onum',
'opbd',
'ordn',
'ornm',
'palt',
'pcap',
'pkna',
'pnum',
'pref',
'pres',
'pstf',
'psts',
'pwid',
'qwid',
'rand',
'rclt',
'rkrf',
'rlig',
'rphf',
'rtbd',
'rtla',
'rtlm',
'ruby',
'rvrn',
'salt',
'sinf',
'size',
'smcp',
'smpl',
'ss01',
'ss02',
'ss03',
'ss04',
'ss05',
'ss06',
'ss07',
'ss08',
'ss09',
'ss10',
'ss11',
'ss12',
'ss13',
'ss14',
'ss15',
'ss16',
'ss17',
'ss18',
'ss19',
'ss20',
'ssty',
'stch',
'subs',
'sups',
'swsh',
'titl',
'tjmo',
'tnam',
'tnum',
'trad',
'twid',
'unic',
'valt',
'vatu',
'vchw',
'vert',
'vhal',
'vjmo',
'vkna',
'vkrn',
'vpal',
'vrt2',
'vrtr',
'zero',
];
27 changes: 27 additions & 0 deletions packages/font-analyze/src/Feature/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Font } from '@konghayao/opentype.js';
import { FeatureList } from './FeatureList';

/** Opentype 的 feature 报告 */
export const getFeatureReport = (font: Font, unicodeSet: Set<number>) => {
return FeatureList.reduce(
(col, i) => {
const arr: { sub: number | number[]; by: number | number[] }[] = (
font.substitution as any
).getFeature(i);
const idToUnicode = (id: number) => font.glyphs.get(id).unicodes;
if (arr && arr.length)
col[i] = arr.map((i) => {
return {
sub: (typeof i.sub === 'number' ? [i.sub] : i.sub).map(
(i) => idToUnicode(i),
),
by: (typeof i.by === 'number' ? [i.by] : i.by).map(
(i) => idToUnicode(i),
),
};
});
return col;
},
{} as Record<string, { sub: number[][]; by: number[][] }[]>,
);
};
24 changes: 21 additions & 3 deletions packages/font-analyze/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { parse, type Font } from '@konghayao/opentype.js';
import { RenderOptions, parse } from '@konghayao/opentype.js';
import { getCharsetReport } from './Charset/getCharsetReport.js';
import {
defaultCharsetLoader,
CharsetLoader,
} from './Charset/defaultCharsetLoader.js';
import { getFeatureReport } from './Feature/index.js';

export * from './Charset/defaultCharsetLoader.js';

/** 分析字体中的字符集和字型相关信息 */
export const FontAnalyze = async (
input: Buffer | ArrayBuffer,
{ charsetLoader = defaultCharsetLoader }: { charsetLoader: CharsetLoader }
{ charsetLoader = defaultCharsetLoader }: { charsetLoader: CharsetLoader },
) => {
const font = parse(input);

Expand All @@ -22,8 +25,9 @@ export const FontAnalyze = async (
const { unicodeReport, standard } = await getCharsetReport(
charsetLoader,
font,
unicodeSet
unicodeSet,
);
const features = getFeatureReport(font, unicodeSet);
return {
file: {
size: input.byteLength,
Expand All @@ -35,5 +39,19 @@ export const FontAnalyze = async (
unicode: unicodeReport,
/** 各大标准字符集检测 */
standard,
/** opentype feature 的替换表 */
features,
/** 将文本绘制成图片 */
drawTextToSVG(
text: string,
x: number,
y: number,
fontSize: number,
options?: RenderOptions,
) {
return font
.getPaths(text, x, y, fontSize, options)
.map((i) => i.toSVG(1));
},
};
};

0 comments on commit 1cbf4c3

Please sign in to comment.