From e65b41f891119233b81e54a61497b4aa8bdaef79 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Thu, 17 Jun 2021 16:50:56 +0200 Subject: [PATCH] XFA - When no fonts in the pdf just use font size as width when measuring text --- src/core/xfa/text.js | 58 ++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/core/xfa/text.js b/src/core/xfa/text.js index a9b7688a7..4bf789817 100644 --- a/src/core/xfa/text.js +++ b/src/core/xfa/text.js @@ -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]; } } @@ -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(); }