Skip to content

Commit

Permalink
feat(config): adds addLigatures option to config file
Browse files Browse the repository at this point in the history
  • Loading branch information
garciamo committed Mar 4, 2022
1 parent 2514400 commit e7cf2aa
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const DEFAULT_OPTIONS: Omit<RunnerOptions, 'inputDir' | 'outputDir'> = {
tag: 'i',
prefix: 'icon',
fontsUrl: undefined,
getIconId: getIconId
getIconId: getIconId,
addLigatures: false
};

export const DEFAULT_START_CODEPOINT = 0xf101;
3 changes: 2 additions & 1 deletion src/core/__tests__/config-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const mockConfig = {
scss: 'scss',
html: 'html'
},
getIconId: DEFAULT_OPTIONS.getIconId
getIconId: DEFAULT_OPTIONS.getIconId,
addLigatures: false
};

const testError = async (options: object, key: string, message: string) =>
Expand Down
3 changes: 2 additions & 1 deletion src/core/config-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const CONFIG_VALIDATORS: {
tag: [parseString],
prefix: [parseString],
fontsUrl: [optional(parseString)],
getIconId: [optional(parseFunction)]
getIconId: [optional(parseFunction)],
addLigatures: [optional(parseBoolean)]
};

export const parseConfig = async (input: object = {}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`\`SVG\` font generator passes correctly format options to \`SVGIcons2SVGFontStream\` 1`] = `"processed->content->/root/foo.svg|{\\"name\\":\\"foo\\",\\"unicode\\":[\\"\\\\u0001\\",\\"foo\\"]}$processed->content->/root/bar.svg|{\\"name\\":\\"bar\\",\\"unicode\\":[\\"\\\\u0001\\",\\"bar\\"]}$"`;
exports[`\`SVG\` font generator passes correctly format options to \`SVGIcons2SVGFontStream\` 1`] = `"processed->content->/root/foo.svg|{\\"name\\":\\"foo\\",\\"unicode\\":[\\"\\\\u0001\\"]}$processed->content->/root/bar.svg|{\\"name\\":\\"bar\\",\\"unicode\\":[\\"\\\\u0001\\"]}$"`;

exports[`\`SVG\` font generator resolves with the result of the completed \`SVGIcons2SVGFontStream\` 1`] = `"processed->content->/root/foo.svg|{\\"name\\":\\"foo\\",\\"unicode\\":[\\"\\\\u0001\\",\\"foo\\"]}$processed->content->/root/bar.svg|{\\"name\\":\\"bar\\",\\"unicode\\":[\\"\\\\u0001\\",\\"bar\\"]}$"`;
exports[`\`SVG\` font generator passes correctly format options to \`SVGIcons2SVGFontStream\` with ligatures 1`] = `"processed->content->/root/foo.svg|{\\"name\\":\\"foo\\",\\"unicode\\":[\\"\\\\u0001\\",\\"foo\\"]}$processed->content->/root/bar.svg|{\\"name\\":\\"bar\\",\\"unicode\\":[\\"\\\\u0001\\",\\"bar\\"]}$"`;

exports[`\`SVG\` font generator resolves with the result of the completed \`SVGIcons2SVGFontStream\` 1`] = `"processed->content->/root/foo.svg|{\\"name\\":\\"foo\\",\\"unicode\\":[\\"\\\\u0001\\"]}$processed->content->/root/bar.svg|{\\"name\\":\\"bar\\",\\"unicode\\":[\\"\\\\u0001\\"]}$"`;

exports[`\`SVG\` font generator resolves with the result of the completed \`SVGIcons2SVGFontStream\` with ligatures 1`] = `"processed->content->/root/foo.svg|{\\"name\\":\\"foo\\",\\"unicode\\":[\\"\\\\u0001\\",\\"foo\\"]}$processed->content->/root/bar.svg|{\\"name\\":\\"bar\\",\\"unicode\\":[\\"\\\\u0001\\",\\"bar\\"]}$"`;
53 changes: 53 additions & 0 deletions src/generators/asset-types/__tests__/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ const mockOptions = (svgOptions = { __mock: 'options__' } as any) =>
}
} as unknown as FontGeneratorOptions);

const mockOptionsWithLigatures = (
svgOptions = { __mock: 'options__' } as any
) =>
({
name: 'foo',
fontHeight: 1,
descent: 2,
normalize: false,
formatOptions: { [FontAssetType.SVG]: svgOptions },
codepoints: { foo: 1, bar: 1 },
assets: {
foo: { id: 'foo', absolutePath: '/root/foo.svg' },
bar: { id: 'bar', absolutePath: '/root/bar.svg' }
},
addLigatures: true
} as unknown as FontGeneratorOptions);

describe('`SVG` font generator', () => {
beforeEach(() => {
SVGIcons2SVGFontStream.mockClear();
Expand Down Expand Up @@ -95,4 +112,40 @@ describe('`SVG` font generator', () => {
normalize: false
});
});

test('resolves with the result of the completed `SVGIcons2SVGFontStream` with ligatures', async () => {
const result = await svgGen.generate(mockOptionsWithLigatures(), null);

expect(SVGIcons2SVGFontStream).toHaveBeenCalledTimes(1);
expect(SVGIcons2SVGFontStream).toHaveBeenCalledWith({
descent: 2,
fontHeight: 1,
fontName: 'foo',
log: expect.any(Function),
normalize: false,
__mock: 'options__'
});

expect(result).toMatchSnapshot();
});

test('passes correctly format options to `SVGIcons2SVGFontStream` with ligatures', async () => {
const log = () => null;
const formatOptions = { descent: 5, fontHeight: 6, log };
const result = await svgGen.generate(
mockOptionsWithLigatures(formatOptions),
null
);

expect(result).toMatchSnapshot();

expect(SVGIcons2SVGFontStream).toHaveBeenCalledTimes(1);
expect(SVGIcons2SVGFontStream).toHaveBeenCalledWith({
descent: 5,
fontHeight: 6,
fontName: 'foo',
log,
normalize: false
});
});
});
15 changes: 10 additions & 5 deletions src/generators/asset-types/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const generator: FontGenerator<void> = {
normalize,
assets,
codepoints,
formatOptions: { svg } = {}
formatOptions: { svg } = {},
addLigatures
}) =>
new Promise(resolve => {
let font = Buffer.alloc(0);
Expand All @@ -31,11 +32,15 @@ const generator: FontGenerator<void> = {
for (const { id, absolutePath } of Object.values(assets)) {
const glyph: GglyphStream = createReadStream(absolutePath);
const unicode = String.fromCharCode(codepoints[id]);
let ligature = '';
for (var i = 0; i < id.length; i++) {
ligature += String.fromCharCode(id.charCodeAt(i));
if (addLigatures) {
let ligature = '';
for (var i = 0; i < id.length; i++) {
ligature += String.fromCharCode(id.charCodeAt(i));
}
glyph.metadata = { name: id, unicode: [unicode, ligature] };
} else {
glyph.metadata = { name: id, unicode: [unicode] };
}
glyph.metadata = { name: id, unicode: [unicode, ligature] };

fontStream.write(glyph);
}
Expand Down
1 change: 1 addition & 0 deletions src/types/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type RunnerOptionalOptions = {
prefix: string;
fontsUrl: string;
getIconId: GetIconIdFn;
addLigatures: boolean;
};

export type RunnerOptionsInput = RunnerMandatoryOptions &
Expand Down

0 comments on commit e7cf2aa

Please sign in to comment.