Skip to content
New issue

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

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

Already on GitHub? # to your account

Font features #642

Merged
merged 6 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"@types/node-fetch": "^2.5.7",
"@types/pako": "^1.0.1",
"@zerollup/ts-transform-paths": "^1.7.18",
"downlevel-dts": "^0.5.0",
"downlevel-dts": "^0.6.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be updated as part of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I don't know, I believe this was caused by an audit fix.
I'm also not super familiar writing tests either, but I can give it a go.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I'd like to undo this change. If the dependency needs to be updated, I'd prefer that to be a separate PR. But I don't think it's necessary for the changes you're making anyways.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, with respect to testing, no worries if you're new to it! I'd recommend you read over the CONTRIBUTING.md file. All contributors should read it. There are sections explaining how to run the unit tests, integration tests (though you don't need to worry about running the React Native tests), and running the typechecker (amongst other important things). Let me know if you have any questions.

"flamebearer": "^1.1.3",
"http-server": "^0.12.3",
"jest": "^26.0.1",
Expand Down
6 changes: 3 additions & 3 deletions src/api/PDFDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ export default class PDFDocument {
font: StandardFonts | string | Uint8Array | ArrayBuffer,
options: EmbedFontOptions = {},
): Promise<PDFFont> {
const { subset = false, customName } = options;
const { subset = false, customName, features } = options;

assertIs(font, 'font', ['string', Uint8Array, ArrayBuffer]);
assertIs(subset, 'subset', ['boolean']);
Expand All @@ -841,8 +841,8 @@ export default class PDFDocument {
const bytes = toUint8Array(font);
const fontkit = this.assertFontkit();
embedder = subset
? await CustomFontSubsetEmbedder.for(fontkit, bytes, customName)
: await CustomFontEmbedder.for(fontkit, bytes, customName);
? await CustomFontSubsetEmbedder.for(fontkit, bytes, customName, features)
: await CustomFontEmbedder.for(fontkit, bytes, customName, features);
} else {
throw new TypeError(
'`font` must be one of `StandardFonts | string | Uint8Array | ArrayBuffer`',
Expand Down
1 change: 1 addition & 0 deletions src/api/PDFDocumentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export interface CreateOptions {
export interface EmbedFontOptions {
subset?: boolean;
customName?: string;
features?: any[];
}
11 changes: 7 additions & 4 deletions src/core/embedders/CustomFontEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,29 @@ class CustomFontEmbedder {
fontkit: Fontkit,
fontData: Uint8Array,
customName?: string,
fontFeatures?: any[],
) {
const font = await fontkit.create(fontData);
return new CustomFontEmbedder(font, fontData, customName);
return new CustomFontEmbedder(font, fontData, customName, fontFeatures);
}

readonly font: Font;
readonly scale: number;
readonly fontData: Uint8Array;
readonly fontName: string;
readonly customName: string | undefined;
readonly fontFeatures: any[] | undefined;

protected baseFontName: string;
protected glyphCache: Cache<Glyph[]>;

protected constructor(font: Font, fontData: Uint8Array, customName?: string) {
protected constructor(font: Font, fontData: Uint8Array, customName?: string, fontFeatures?: any[]) {
this.font = font;
this.scale = 1000 / this.font.unitsPerEm;
this.fontData = fontData;
this.fontName = this.font.postscriptName || 'Font';
this.customName = customName;
this.fontFeatures = fontFeatures;

this.baseFontName = '';
this.glyphCache = Cache.populatedBy(this.allGlyphsInFontSortedById);
Expand All @@ -54,7 +57,7 @@ class CustomFontEmbedder {
* Unicode, but embedded fonts use their own custom encodings)
*/
encodeText(text: string): PDFHexString {
const { glyphs } = this.font.layout(text);
const { glyphs } = this.font.layout(text, this.fontFeatures);
const hexCodes = new Array(glyphs.length);
for (let idx = 0, len = glyphs.length; idx < len; idx++) {
hexCodes[idx] = toHexStringOfMinLength(glyphs[idx].id, 4);
Expand All @@ -65,7 +68,7 @@ class CustomFontEmbedder {
// The advanceWidth takes into account kerning automatically, so we don't
// have to do that manually like we do for the standard fonts.
widthOfTextAtSize(text: string, size: number): number {
const { glyphs } = this.font.layout(text);
const { glyphs } = this.font.layout(text, this.fontFeatures);
let totalWidth = 0;
for (let idx = 0, len = glyphs.length; idx < len; idx++) {
totalWidth += glyphs[idx].advanceWidth * this.scale;
Expand Down
8 changes: 5 additions & 3 deletions src/core/embedders/CustomFontSubsetEmbedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class CustomFontSubsetEmbedder extends CustomFontEmbedder {
fontkit: Fontkit,
fontData: Uint8Array,
customFontName?: string,
fontFeatures?: any[],
) {
const font = await fontkit.create(fontData);
return new CustomFontSubsetEmbedder(font, fontData, customFontName);
return new CustomFontSubsetEmbedder(font, fontData, customFontName, fontFeatures);
}

private readonly subset: Subset;
Expand All @@ -27,8 +28,9 @@ class CustomFontSubsetEmbedder extends CustomFontEmbedder {
font: Font,
fontData: Uint8Array,
customFontName?: string,
fontFeatures?: any[],
) {
super(font, fontData, customFontName);
super(font, fontData, customFontName, fontFeatures);

this.subset = this.font.createSubset();
this.glyphs = [];
Expand All @@ -37,7 +39,7 @@ class CustomFontSubsetEmbedder extends CustomFontEmbedder {
}

encodeText(text: string): PDFHexString {
const { glyphs } = this.font.layout(text);
const { glyphs } = this.font.layout(text, this.fontFeatures);
const hexCodes = new Array(glyphs.length);

for (let idx = 0, len = glyphs.length; idx < len; idx++) {
Expand Down
Loading