Skip to content

Commit

Permalink
fix: use defsubset if there is no comment in css (#133)
Browse files Browse the repository at this point in the history
* fix: use defsubset if there is no comment in CSS

* fix: lints

* fix: handle ttf fonts without duplicating it with default subset
  • Loading branch information
ayuhito committed Dec 5, 2023
1 parent 263ef24 commit 4732178
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "google-font-metadata",
"description": "A metadata generator for Google Fonts.",
"version": "5.2.0",
"version": "5.2.1",
"author": "Ayuhito <hello@ayuhito.com>",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
3 changes: 2 additions & 1 deletion src/api-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ interface GotResponse {

const fetchURL = async (url: string): Promise<void> => {
// Have to double assert to please esbuild
const response = (await got(url).json()) as unknown as GotResponse;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const response = (await got(url).json()) as GotResponse;
// Google ships icons into the API, so we have to separate them
const stripped = await stripIconsApiGen(response.items);

Expand Down
42 changes: 26 additions & 16 deletions src/api-parser-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const processCSS = (
font: APIResponse,
) => {
const id = font.family.replaceAll(/\s/g, '-').toLowerCase();
const defSubset = font.subsets.includes('latin') ? 'latin' : font.subsets[0];

const fontObject: FontObjectV2 = {
[id]: {
Expand All @@ -92,7 +93,7 @@ export const processCSS = (
styles: [],
unicodeRange: {},
variants: {},
defSubset: font.subsets.includes('latin') ? 'latin' : font.subsets[0],
defSubset,
lastModified: font.lastModified,
version: font.version,
category: font.category,
Expand All @@ -102,7 +103,7 @@ export const processCSS = (
for (const extension of css) {
const rules = compile(extension);

let subset = '';
let subset = defSubset ?? 'latin';
let fontStyle = '';
let fontWeight = '';
for (const rule of rules) {
Expand Down Expand Up @@ -186,20 +187,6 @@ export const processCSS = (
fontObject[id].variants[fontWeight] =
fontObject[id].variants[fontWeight] || {};

if (fontStyle && subset && type === 'url') {
fontObject[id].variants[fontWeight][fontStyle] =
fontObject[id].variants[fontWeight][fontStyle] || {};

fontObject[id].variants[fontWeight][fontStyle][subset] =
fontObject[id].variants[fontWeight][fontStyle][subset] || {
url: {},
};

fontObject[id].variants[fontWeight][fontStyle][subset].url[
format
] = path;
}

// APIv2 splits woff/woff2 files by subset, but uses one combined file for other formats
// These don't have a subset
if (fontStyle && type === 'url' && !format.startsWith('woff')) {
Expand All @@ -211,13 +198,36 @@ export const processCSS = (
format
] = path;
}
// We do not want to include local fonts
} else if (type === 'url') {
fontObject[id].variants[fontWeight][fontStyle] =
fontObject[id].variants[fontWeight][fontStyle] || {};

fontObject[id].variants[fontWeight][fontStyle][subset] =
fontObject[id].variants[fontWeight][fontStyle][subset] || {
url: {},
};

fontObject[id].variants[fontWeight][fontStyle][subset].url[
format
] = path;
}
}
}
}
}
}
}

// If unicode-range is empty, but the font has a subset, add a fallback range that covers all characters
if (
Object.keys(fontObject[id].unicodeRange).length === 0 &&
fontObject[id].defSubset
) {
fontObject[id].unicodeRange[fontObject[id].defSubset] =
'U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+2074,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD';
}

return fontObject;
};

Expand Down
3 changes: 2 additions & 1 deletion src/variable-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,15 @@ export const fetchCSS = async (url: string) => {

// [key, css]
export const fetchAllCSS = async (links: Links) =>
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
await (Promise.all(
Object.keys(links).map(async (key) => [key, await fetchCSS(links[key])]),
) as Promise<string[][]>); // Additional type assertion needed for pkgroll dts plugin

export const parseCSS = (cssTuple: string[][], defSubset?: string) => {
const fontVariants: FontVariantsVariable = {};

let subset = '';
let subset = defSubset ?? 'latin';
for (const [key, cssVariant] of cssTuple) {
const [fontType, fontStyle] = key.split('.');
const rules = compile(cssVariant);
Expand Down

0 comments on commit 4732178

Please sign in to comment.