1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-28 23:28:16 +02:00

XFA - Fix auto-sized fields (bug 1722030)

- In order to better compute text fields size, use line height with no gaps (and consequently guessed height for text are slightly better in general).
  - Fix default background color in fields.
This commit is contained in:
Calixte Denizet 2021-07-27 18:43:05 +02:00
parent 336a74a0e5
commit 76d882b560
14 changed files with 311 additions and 162 deletions

View file

@ -181,9 +181,12 @@ class TextMeasure {
if (lastFont.pdfFont) {
const letterSpacing = lastFont.xfaFont.letterSpacing;
const pdfFont = lastFont.pdfFont;
const fontLineHeight = pdfFont.lineHeight || 1.2;
const lineHeight =
lastFont.lineHeight ||
Math.ceil(Math.max(1.2, pdfFont.lineHeight) * fontSize);
lastFont.lineHeight || Math.max(1.2, fontLineHeight) * fontSize;
const lineGap = pdfFont.lineGap === undefined ? 0.2 : pdfFont.lineGap;
const noGap = fontLineHeight - lineGap;
const firstLineHeight = Math.max(1, noGap) * fontSize;
const scale = fontSize / 1000;
for (const line of str.split(/[\u2029\n]/)) {
@ -194,12 +197,13 @@ class TextMeasure {
this.glyphs.push([
glyph.width * scale + letterSpacing,
lineHeight,
firstLineHeight,
glyph.unicode === " ",
false,
]);
}
this.glyphs.push([0, 0, false, true]);
this.glyphs.push([0, 0, 0, false, true]);
}
this.glyphs.pop();
return;
@ -208,10 +212,16 @@ class TextMeasure {
// 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([
fontSize,
1.2 * fontSize,
fontSize,
char === " ",
false,
]);
}
this.glyphs.push([0, 0, false, true]);
this.glyphs.push([0, 0, 0, false, true]);
}
this.glyphs.pop();
}
@ -224,9 +234,12 @@ class TextMeasure {
currentLineWidth = 0,
currentLineHeight = 0;
let isBroken = false;
let isFirstLine = true;
for (let i = 0, ii = this.glyphs.length; i < ii; i++) {
const [glyphWidth, glyphHeight, isSpace, isEOL] = this.glyphs[i];
const [glyphWidth, lineHeight, firstLineHeight, isSpace, isEOL] =
this.glyphs[i];
const glyphHeight = isFirstLine ? firstLineHeight : lineHeight;
if (isEOL) {
width = Math.max(width, currentLineWidth);
currentLineWidth = 0;
@ -234,6 +247,7 @@ class TextMeasure {
currentLineHeight = glyphHeight;
lastSpacePos = -1;
lastSpaceWidth = 0;
isFirstLine = false;
continue;
}
@ -247,6 +261,7 @@ class TextMeasure {
lastSpacePos = -1;
lastSpaceWidth = 0;
isBroken = true;
isFirstLine = false;
} else {
currentLineHeight = Math.max(glyphHeight, currentLineHeight);
lastSpaceWidth = currentLineWidth;
@ -272,6 +287,7 @@ class TextMeasure {
currentLineWidth = glyphWidth;
}
isBroken = true;
isFirstLine = false;
continue;
}