Merge pull request #13576 from calixteman/really_fix_text_stuff

XFA - When no fonts in the pdf just use font size as width when measuring text
This commit is contained in:
calixteman 2021-06-17 17:47:18 +02:00 committed by GitHub
commit 3264d409dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -59,6 +59,7 @@ class FontInfo {
fonts.Arial ||
fonts.ArialMT ||
Object.values(fonts)[0];
if (font && font.regular) {
const pdfFont = font.regular;
const info = pdfFont.cssFontInfo;
const xfaFont = {
@ -69,6 +70,15 @@ class FontInfo {
};
return [pdfFont, xfaFont];
}
const xfaFont = {
typeface: "Courier",
posture: "normal",
weight: "normal",
size: 10,
};
return [null, xfaFont];
}
}
class FontSelector {
@ -124,8 +134,9 @@ class TextMeasure {
}
const lastFont = this.fontSelector.topFont();
const pdfFont = lastFont.pdfFont;
const fontSize = lastFont.xfaFont.size;
if (lastFont.pdfFont) {
const pdfFont = lastFont.pdfFont;
const lineHeight = Math.round(Math.max(1, pdfFont.lineHeight) * fontSize);
const scale = fontSize / 1000;
@ -144,7 +155,18 @@ class TextMeasure {
this.glyphs.push([0, 0, false, true]);
}
this.glyphs.pop();
return;
}
// When we have no font in the pdf, just use the font size as default width.
for (const line of str.split(/[\u2029\n]/)) {
for (const char of line.split("")) {
this.glyphs.push([fontSize, fontSize, char === " ", false]);
}
this.glyphs.push([0, 0, false, true]);
}
this.glyphs.pop();
}