Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ligatures): adds a proposal to add ligatures #315

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12,579 changes: 28 additions & 12,551 deletions package-lock.json

Large diffs are not rendered by default.

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
Expand Up @@ -2,4 +2,8 @@

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 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
});
});
});
14 changes: 11 additions & 3 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,8 +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]);

glyph.metadata = { name: id, unicode: [unicode] };
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] };
}

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