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

XFA - When no fonts in the pdf just use font size as width when measuring text #13576

Merged
merged 1 commit into from
Jun 17, 2021
Merged
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
58 changes: 40 additions & 18 deletions src/core/xfa/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,25 @@ class FontInfo {
fonts.Arial ||
fonts.ArialMT ||
Object.values(fonts)[0];
const pdfFont = font.regular;
const info = pdfFont.cssFontInfo;
if (font && font.regular) {
const pdfFont = font.regular;
const info = pdfFont.cssFontInfo;
const xfaFont = {
typeface: info.fontFamily,
posture: "normal",
weight: "normal",
size: 10,
};
return [pdfFont, xfaFont];
}

const xfaFont = {
typeface: info.fontFamily,
typeface: "Courier",
posture: "normal",
weight: "normal",
size: 10,
};
return [pdfFont, xfaFont];
return [null, xfaFont];
}
}

Expand Down Expand Up @@ -124,27 +134,39 @@ class TextMeasure {
}

const lastFont = this.fontSelector.topFont();
const pdfFont = lastFont.pdfFont;
const fontSize = lastFont.xfaFont.size;
const lineHeight = Math.round(Math.max(1, pdfFont.lineHeight) * fontSize);
const scale = fontSize / 1000;
if (lastFont.pdfFont) {
const pdfFont = lastFont.pdfFont;
const lineHeight = Math.round(Math.max(1, pdfFont.lineHeight) * fontSize);
const scale = fontSize / 1000;

for (const line of str.split(/[\u2029\n]/)) {
const encodedLine = pdfFont.encodeString(line).join("");
const glyphs = pdfFont.charsToGlyphs(encodedLine);

for (const glyph of glyphs) {
this.glyphs.push([
glyph.width * scale,
lineHeight,
glyph.unicode === " ",
false,
]);
}

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]/)) {
const encodedLine = pdfFont.encodeString(line).join("");
const glyphs = pdfFont.charsToGlyphs(encodedLine);

for (const glyph of glyphs) {
this.glyphs.push([
glyph.width * scale,
lineHeight,
glyph.unicode === " ",
false,
]);
for (const char of line.split("")) {
this.glyphs.push([fontSize, fontSize, char === " ", false]);
}

this.glyphs.push([0, 0, false, true]);
}

this.glyphs.pop();
}

Expand Down