Skip to content

Commit

Permalink
Don't emit unused Helvetica (#820)
Browse files Browse the repository at this point in the history
* Don't emit unused Helvetica

* Refactor(Apply the suggestion)

Co-authored-by: terurou <terurou@denkiyagi.jp>
  • Loading branch information
ssssota and terurou authored Oct 16, 2021
1 parent 1ff8ef4 commit 07bf247
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/api/PDFPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,22 +980,19 @@ export default class PDFPage {
assertOrUndefined(options.wordBreaks, 'options.wordBreaks', [Array]);
assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);

const [originalFont] = this.getFont();
if (options.font) this.setFont(options.font);
const [font, fontKey] = this.getFont();

const { oldFont, newFont, newFontKey } = this.setOrEmbedFont(options.font);
const fontSize = options.size || this.fontSize;

const wordBreaks = options.wordBreaks || this.doc.defaultWordBreaks;
const textWidth = (t: string) => font.widthOfTextAtSize(t, fontSize);
const textWidth = (t: string) => newFont.widthOfTextAtSize(t, fontSize);
const lines =
options.maxWidth === undefined
? lineSplit(cleanText(text))
: breakTextIntoLines(text, wordBreaks, options.maxWidth, textWidth);

const encodedLines = new Array(lines.length) as PDFHexString[];
for (let idx = 0, len = lines.length; idx < len; idx++) {
encodedLines[idx] = font.encodeText(lines[idx]);
encodedLines[idx] = newFont.encodeText(lines[idx]);
}

const graphicsStateKey = this.maybeEmbedGraphicsState({
Expand All @@ -1007,7 +1004,7 @@ export default class PDFPage {
contentStream.push(
...drawLinesOfText(encodedLines, {
color: options.color ?? this.fontColor,
font: fontKey,
font: newFontKey,
size: fontSize,
rotate: options.rotate ?? degrees(0),
xSkew: options.xSkew ?? degrees(0),
Expand All @@ -1019,7 +1016,10 @@ export default class PDFPage {
}),
);

if (options.font) this.setFont(originalFont);
if (options.font) {
if (oldFont) this.setFont(oldFont);
else this.resetFont();
}
}

/**
Expand Down Expand Up @@ -1537,6 +1537,19 @@ export default class PDFPage {
this.drawEllipse({ ...options, xScale: size, yScale: size });
}

private setOrEmbedFont(font?: PDFFont) {
const oldFont = this.font;
const oldFontKey = this.fontKey;

if (font) this.setFont(font);
else this.getFont();

const newFont = this.font!;
const newFontKey = this.fontKey!;

return { oldFont, oldFontKey, newFont, newFontKey };
}

private getFont(): [PDFFont, string] {
if (!this.font || !this.fontKey) {
const font = this.doc.embedStandardFont(StandardFonts.Helvetica);
Expand All @@ -1545,6 +1558,11 @@ export default class PDFPage {
return [this.font!, this.fontKey!];
}

private resetFont(): void {
this.font = undefined;
this.fontKey = undefined;
}

private getContentStream(useExisting = true): PDFContentStream {
if (useExisting && this.contentStream) return this.contentStream;
this.contentStream = this.createContentStream();
Expand Down

0 comments on commit 07bf247

Please sign in to comment.