Skip to content

Commit

Permalink
Merge pull request #26 from omacranger/feat/woff-support
Browse files Browse the repository at this point in the history
Add Woff Support, targetFormats option
  • Loading branch information
omacranger authored Jul 21, 2022
2 parents db39d85 + 00e239f commit b3d2a6c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
28 changes: 18 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import { existsSync, readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
import { sync as makeDirSync } from "mkdirp";
import { FontAwesomeOptions, IconYAML, Subset, SubsetOption } from "./types";
import { FontAwesomeOptions, IconYAML, Subset, SubsetOption, TargetFormat } from "./types";
import subsetFont from "subset-font";
import yaml from "yaml";
import { addIconError, findIconByName } from "./utils";

const OUTPUT_FORMATS: { targetFormat: "woff2" | "sfnt"; fileExt: string }[] = [
{ targetFormat: "woff2", fileExt: "woff2" },
{ targetFormat: "sfnt", fileExt: "ttf" },
];
const OUTPUT_FORMAT_MAP: Record<TargetFormat, string> = {
sfnt: "ttf",
woff: "woff",
woff2: "woff2",
};

/**
* This function will take an object of glyph names and output a subset of the standard fonts optimized in size for
Expand All @@ -26,9 +27,9 @@ const OUTPUT_FORMATS: { targetFormat: "woff2" | "sfnt"; fileExt: string }[] = [
function fontawesomeSubset(
subset: SubsetOption,
outputDir: string,
options: FontAwesomeOptions = { package: "free" }
options: FontAwesomeOptions = {}
) {
const { package: packageType } = options;
const { package: packageType = "free", targetFormats = ["woff2", "sfnt"] } = options;
// Maps style to actual font name / file name.
const fontMap: Record<Subset, string> = {
solid: "fa-solid-900",
Expand All @@ -51,6 +52,13 @@ function fontawesomeSubset(
return Promise.resolve(false);
}

// Check that we have atleast one target format for output
if (!Array.isArray(targetFormats) || targetFormats.length === 0) {
console.error("One or more target formats are required. Exiting.");

return Promise.resolve(false);
}

const fontMeta = resolve(packageLocation, "../../metadata/icons.yml");
const fontFiles = resolve(packageLocation, "../../webfonts");

Expand Down Expand Up @@ -111,12 +119,12 @@ function fontawesomeSubset(
const outputFile = resolve(outputDir, fontFileName);

// Loop over our requested output formats, and generate our subsets
for (const oFormat of OUTPUT_FORMATS) {
for (const targetFormat of targetFormats) {
promises.push(
subsetFont(fontData, unicodeCharacters.join(" "), {
targetFormat: oFormat.targetFormat,
targetFormat: targetFormat,
}).then((data) => {
writeFileSync(`${outputFile}.${oFormat.fileExt}`, data);
writeFileSync(`${outputFile}.${OUTPUT_FORMAT_MAP[targetFormat]}`, data);
})
);
}
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export interface FontAwesomeOptions {
package: "free" | "pro";
/**
* The FontAwesome package type we should use. Defaults to 'free'.
*/
package?: "free" | "pro";
/**
* Requested font output targets.
*/
targetFormats?: TargetFormat[];
}

export type Subset = "solid" | "light" | "regular" | "thin" | "brands" | "duotone";
Expand All @@ -8,6 +15,8 @@ export type GlyphName = string;

export type SubsetOption = GlyphName[] | Partial<Record<Subset, GlyphName[]>>;

export type TargetFormat = "woff" | "woff2" | "sfnt";

/**
* Type of individual result / item inside the YAML file.
*/
Expand Down
14 changes: 11 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("fontawesomeSubset", () => {
}
});

it("should create ttf & woff2 files for each requested subset", async () => {
it("should create ttf, woff, and woff2 files for each requested subset", async () => {
const tempDir = await createTempDir();
await fontawesomeSubset(
{
Expand All @@ -89,7 +89,7 @@ describe("fontawesomeSubset", () => {
duotone: ["abacus"],
},
tempDir,
{ package: "pro" }
{ package: "pro", targetFormats: ["woff2", "woff", "sfnt"] }
);

const fontNames = [
Expand All @@ -100,7 +100,7 @@ describe("fontawesomeSubset", () => {
"fa-thin-100",
"fa-light-300",
]
.map((name) => [`${name}.ttf`, `${name}.woff2`])
.map((name) => [`${name}.ttf`, `${name}.woff`, `${name}.woff2`])
.flat();
const fontPromises = fontNames.map((name) => fs.promises.access(`${tempDir}${SEP}${name}`));
const wasSuccessful = await Promise.all(fontPromises)
Expand Down Expand Up @@ -144,4 +144,12 @@ describe("fontawesomeSubset", () => {
);
expect(response).toBeFalsy();
});

it("should error when no target formats are provided", async () => {
const errorSpy = jest.spyOn(console, "error").mockImplementationOnce(() => false);
const response = await fontawesomeSubset(["arrow-left"], "", { targetFormats: [] });

expect(errorSpy).toBeCalledWith("One or more target formats are required. Exiting.");
expect(response).toBeFalsy();
});
});

0 comments on commit b3d2a6c

Please sign in to comment.