Skip to content

Commit

Permalink
refactor: read fonts directly
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKreil committed Apr 9, 2024
1 parent fabf8a7 commit 185c491
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/lib/fonts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jest.unstable_mockModule('node:fs', () => ({
if (path === '/fonts/family1/fonts.json') {
return JSON.stringify([{ name: 'fontFamily1', sources: ['font1.ttf', 'font2.otf'] }]);
}
throw Error();
return `content of ${path}`;
}),
}));

Expand All @@ -39,7 +39,7 @@ describe('getFontSources', () => {
styleName: 'Bold',
weight: 700,
},
'sources': ['/fonts/Family 1/Family1_bold.otf'],
'sources': ['content of /fonts/Family 1/Family1_bold.otf'],
},
{
fontFace: {
Expand All @@ -51,7 +51,7 @@ describe('getFontSources', () => {
styleName: 'Italic',
weight: 400,
},
'sources': ['/fonts/Family 1/Family1_italic.ttf'],
'sources': ['content of /fonts/Family 1/Family1_italic.ttf'],
},
{
fontFace: {
Expand All @@ -63,7 +63,7 @@ describe('getFontSources', () => {
styleName: 'Regular',
weight: 400,
},
'sources': ['/fonts/Family 1/Family1.otf'],
'sources': ['content of /fonts/Family 1/Family1.otf'],
},
{
fontFace: {
Expand All @@ -75,7 +75,7 @@ describe('getFontSources', () => {
styleName: 'Bold',
weight: 700,
},
'sources': ['/fonts/Family 2/Family2_bold.ttf'],
'sources': ['content of /fonts/Family 2/Family2_bold.ttf'],
},
{
fontFace: {
Expand All @@ -87,7 +87,7 @@ describe('getFontSources', () => {
styleName: 'Italic',
weight: 400,
},
'sources': ['/fonts/Family 2/Family2_italic.otf'],
'sources': ['content of /fonts/Family 2/Family2_italic.otf'],
},
]);
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/fonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync, lstatSync, readFileSync, readdirSync } from 'node:fs';
import { basename, resolve } from 'node:path';

export interface FontSourcesWrapper {
sources: string[];
sources: Buffer[];
fontFace: FontFace;
}

Expand Down Expand Up @@ -49,7 +49,7 @@ export function getFontSources(inputDir: string): FontSourcesWrapper[] {
fonts.forEach(font => {
const sources = font.sources
.filter(filename => !filename.startsWith('#'))
.map(filename => resolve(dirInFont, filename));
.map(filename => readFileSync(resolve(dirInFont, filename)));

if (sources.length <= 0) throw Error();

Expand Down
4 changes: 2 additions & 2 deletions src/lib/glyphs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ describe('buildAllGlyphs', () => {
it('should build all glyphs from font sources', async () => {
process.chdir(new URL('../../', import.meta.url).pathname);
const fontSources = getFontSources('font-sources')
.filter(f => f.sources[0].endsWith('FiraSans-Bold.ttf'));

.filter(f => f.fontFace.fontId === 'fira_sans_bold');
const result = await buildAllGlyphs(fontSources);

expect(Array.isArray(result)).toBe(true);
Expand Down
4 changes: 1 addition & 3 deletions src/lib/glyphs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fontnik from 'fontnik';
import { readFileSync } from 'node:fs';
import type { FontFace, FontSourcesWrapper } from './fonts.ts';
import { Progress } from './progress.ts';
import { runParallel } from './async.ts';
Expand All @@ -23,8 +22,7 @@ export interface FontGlyphsWrapper {
export async function buildAllGlyphs(fonts: FontSourcesWrapper[]): Promise<FontGlyphsWrapper[]> {
const fontRanges = [];
for (const font of fonts) {
for (const filename of font.sources) {
const bufferFont = readFileSync(filename);
for (const bufferFont of font.sources) {
const ranges = await getGlyphRanges(bufferFont, font.fontFace);
for (const range of ranges) {
fontRanges.push({
Expand Down

0 comments on commit 185c491

Please sign in to comment.