-
-
Notifications
You must be signed in to change notification settings - Fork 633
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
Don't emit unused Helvetica #820
Conversation
Hello @ssssota! Thanks so much for working on this! Apologies for my late response. I've been very busy these past few months taking care of some personal stuff and starting my new job at GitHub 🙂 I'm hoping to get around to reviewing this PR in the next few weeks now that I've got some more free time. Please note that you'll need to either update this PR (via a force push) or open a new PR with your changes. The tl;dr is that I recently had to do a fairly extensive git history rewrite that caused a bunch of conflicts. Please read https://github.com/Hopding/pdf-lib#git-history-rewrite for details. Apologies for the hassle! |
12c63f2
to
e0a4521
Compare
Hi @Hopding! |
You're right that embedding the helvetica font when a custom one is supplied isn't ideal, so I like the idea of fixing that. However, this implementation pushes a lot of confusing conditional logic and non-null assertions into diff --git a/src/api/PDFPage.ts b/src/api/PDFPage.ts
index 51215c89..ce2116b1 100644
--- a/src/api/PDFPage.ts
+++ b/src/api/PDFPage.ts
@@ -893,14 +893,12 @@ 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))
@@ -908,7 +906,7 @@ export default class PDFPage {
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({
@@ -920,7 +918,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),
@@ -932,7 +930,10 @@ export default class PDFPage {
}),
);
- if (options.font) this.setFont(originalFont);
+ if (options.font) {
+ if (oldFont) this.setFont(oldFont);
+ else this.resetFont();
+ }
}
/**
@@ -1450,6 +1451,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);
@@ -1458,6 +1472,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(); |
This PR is stale because it has been open 3 weeks with no activity. It will be be closed in 2 days unless there is new activity. See MAINTAINERSHIP.md#pull-requests for details. |
f43707f
to
e0a4521
Compare
@Hopding I applied your suggestion, please check it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Thanks @ssssota!
Helvetica font is referenced when I don't use it.
Like this:
sample.pdf
font properties(with Acrobat Reader DC)