Skip to content

Commit 86565cd

Browse files
committed
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.
1 parent 7305b74 commit 86565cd

14 files changed

+310
-161
lines changed

src/core/calibri_factors.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const CalibriBoldFactors = [
8181
0.95794, 0.95794, 0.82616, 0.86513, 0.85162, 0.85162, 0.85162, 0.85162,
8282
0.91133, 0.85162, 0.79492, 0.79492, 0.79492, 0.79492, 0.91133, 0.79109,
8383
];
84-
const CalibriBoldLineHeight = 1.2207;
84+
const CalibriBoldMetrics = { lineHeight: 1.2207, lineGap: 0.2207 };
8585

8686
// Factors to rescale LiberationSans-BoldItalic.ttf to have the same
8787
// metrics as calibriz.ttf.
@@ -153,7 +153,7 @@ const CalibriBoldItalicFactors = [
153153
0.84548, 0.84548, 0.91133, 0.84548, 0.79492, 0.79492, 0.79492, 0.79492,
154154
0.91133, 0.74081,
155155
];
156-
const CalibriBoldItalicLineHeight = 1.2207;
156+
const CalibriBoldItalicMetrics = { lineHeight: 1.2207, lineGap: 0.2207 };
157157

158158
// Factors to rescale LiberationSans-Italic.ttf to have the same
159159
// metrics as calibrii.ttf.
@@ -223,7 +223,7 @@ const CalibriItalicFactors = [
223223
0.84153, 0.89453, 0.89453, 0.89453, 0.89453, 0.91133, 0.89453, 0.79004,
224224
0.79004, 0.79004, 0.79004, 0.91133, 0.75026,
225225
];
226-
const CalibriItalicLineHeight = 1.2207;
226+
const CalibriItalicMetrics = { lineHeight: 1.2207, lineGap: 0.2207 };
227227

228228
// Factors to rescale LiberationSans-Regular.ttf to have the same
229229
// metrics as calibri.ttf.
@@ -294,15 +294,15 @@ const CalibriRegularFactors = [
294294
0.83969, 0.90527, 0.90527, 0.90527, 0.90527, 0.91133, 0.90527, 0.79004,
295295
0.79004, 0.79004, 0.79004, 0.91133, 0.78848,
296296
];
297-
const CalibriRegularLineHeight = 1.2207;
297+
const CalibriRegularMetrics = { lineHeight: 1.2207, lineGap: 0.2207 };
298298

299299
export {
300300
CalibriBoldFactors,
301301
CalibriBoldItalicFactors,
302-
CalibriBoldItalicLineHeight,
303-
CalibriBoldLineHeight,
302+
CalibriBoldItalicMetrics,
303+
CalibriBoldMetrics,
304304
CalibriItalicFactors,
305-
CalibriItalicLineHeight,
305+
CalibriItalicMetrics,
306306
CalibriRegularFactors,
307-
CalibriRegularLineHeight,
307+
CalibriRegularMetrics,
308308
};

src/core/evaluator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3924,7 +3924,7 @@ class PartialEvaluator {
39243924
const standardFontName = getXfaFontName(fontName.name);
39253925
if (standardFontName) {
39263926
cssFontInfo.fontFamily = `${cssFontInfo.fontFamily}-PdfJS-XFA`;
3927-
cssFontInfo.lineHeight = standardFontName.lineHeight || null;
3927+
cssFontInfo.metrics = standardFontName.metrics || null;
39283928
glyphScaleFactors = standardFontName.factors || null;
39293929
fontFile = await this.fetchStandardFontData(standardFontName.name);
39303930
isInternalFont = !!fontFile;

src/core/fonts.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2550,7 +2550,8 @@ class Font {
25502550
this.lineGap = metricsOverride.lineGap / metricsOverride.unitsPerEm;
25512551

25522552
if (this.cssFontInfo && this.cssFontInfo.lineHeight) {
2553-
this.lineHeight = this.cssFontInfo.lineHeight;
2553+
this.lineHeight = this.cssFontInfo.metrics.lineHeight;
2554+
this.lineGap = this.cssFontInfo.metrics.lineGap;
25542555
} else {
25552556
this.lineHeight = this.ascent - this.descent + this.lineGap;
25562557
}

src/core/helvetica_factors.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const HelveticaBoldFactors = [
9494
1.00022, 1.00022, 0.99973, 0.9993, 0.99973, 0.99973, 0.99973, 0.99973,
9595
0.99973, 0.99973, 1, 1, 1, 1, 0.99973, 0.99902,
9696
];
97-
const HelveticaBoldLineHeight = 1.2;
97+
const HelveticaBoldMetrics = { lineHeight: 1.2, lineGap: 0.2 };
9898

9999
// Factors to rescale LiberationSans-BoldItalic.ttf to have the same
100100
// metrics as NimbusSans-BoldItalic.otf.
@@ -177,7 +177,7 @@ const HelveticaBoldItalicFactors = [
177177
0.99973, 1.00065, 0.99973, 0.99973, 0.99973, 0.99973, 0.99973, 0.99973, 1, 1,
178178
1, 1, 0.99973, 1.00061,
179179
];
180-
const HelveticaBoldItalicLineHeight = 1.35;
180+
const HelveticaBoldItalicMetrics = { lineHeight: 1.35, lineGap: 0.2 };
181181

182182
// Factors to rescale LiberationSans-Italic.ttf to have the same
183183
// metrics as NimbusSans-Italic.otf.
@@ -258,7 +258,7 @@ const HelveticaItalicFactors = [
258258
0.99973, 0.99973, 1, 0.99977, 0.99977, 0.99977, 0.99977, 0.99977, 1, 1.0005,
259259
1, 1, 1, 1, 0.99973, 1, 1, 1, 1, 1, 0.99973, 0.99918,
260260
];
261-
const HelveticaItalicLineHeight = 1.35;
261+
const HelveticaItalicMetrics = { lineHeight: 1.35, lineGap: 0.2 };
262262

263263
// Factors to rescale LiberationSans-Regular.ttf to have the same
264264
// metrics as NimbusSans-Regular.otf.
@@ -339,15 +339,15 @@ const HelveticaRegularFactors = [
339339
0.99977, 0.99977, 0.99977, 1, 1.00055, 1, 1, 1, 1, 0.99973, 1, 1, 1, 1, 1,
340340
0.99973, 1.00019,
341341
];
342-
const HelveticaRegularLineHeight = 1.2;
342+
const HelveticaRegularMetrics = { lineHeight: 1.2, lineGap: 0.2 };
343343

344344
export {
345345
HelveticaBoldFactors,
346346
HelveticaBoldItalicFactors,
347-
HelveticaBoldItalicLineHeight,
348-
HelveticaBoldLineHeight,
347+
HelveticaBoldItalicMetrics,
348+
HelveticaBoldMetrics,
349349
HelveticaItalicFactors,
350-
HelveticaItalicLineHeight,
350+
HelveticaItalicMetrics,
351351
HelveticaRegularFactors,
352-
HelveticaRegularLineHeight,
352+
HelveticaRegularMetrics,
353353
};

src/core/myriadpro_factors.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const MyriadProBoldFactors = [
7777
0.97579, 0.97579, 0.97579, 0.9332, 1.05993, 0.94039, 0.94039, 0.94039,
7878
0.94039, 0.99793, 0.94039, 0.938, 0.938, 0.938, 0.938, 0.99793, 0.95776,
7979
];
80-
const MyriadProBoldLineHeight = 1.2;
80+
const MyriadProBoldMetrics = { lineHeight: 1.2, lineGap: 0.2 };
8181

8282
// Factors to rescale LiberationSans-BoldItalic.ttf to have the same
8383
// metrics as MyriadPro-BoldIt.otf.
@@ -144,7 +144,7 @@ const MyriadProBoldItalicFactors = [
144144
0.89544, 1.0051, 0.89364, 0.89364, 0.89364, 0.89364, 0.97276, 0.89364, 0.9,
145145
0.9, 0.9, 0.9, 0.97276, 0.86842,
146146
];
147-
const MyriadProBoldItalicLineHeight = 1.2;
147+
const MyriadProBoldItalicMetrics = { lineHeight: 1.2, lineGap: 0.2 };
148148

149149
// Factors to rescale LiberationSans-Italic.ttf to have the same
150150
// metrics as MyriadPro-It.otf.
@@ -210,7 +210,7 @@ const MyriadProItalicFactors = [
210210
0.979, 0.979, 0.979, 0.979, 0.882, 0.93559, 0.882, 0.882, 0.882, 0.882,
211211
0.88465, 0.882, 0.83, 0.83, 0.83, 0.83, 0.88465, 0.84596,
212212
];
213-
const MyriadProItalicLineHeight = 1.2;
213+
const MyriadProItalicMetrics = { lineHeight: 1.2, lineGap: 0.2 };
214214

215215
// Factors to rescale LiberationSans-Regular.ttf to have the same
216216
// metrics as MyriadPro-Regular.otf.
@@ -276,15 +276,15 @@ const MyriadProRegularFactors = [
276276
1.01915, 0.926, 0.96705, 0.942, 0.942, 0.942, 0.942, 0.92241, 0.942, 0.856,
277277
0.856, 0.856, 0.856, 0.92241, 0.92761,
278278
];
279-
const MyriadProRegularLineHeight = 1.2;
279+
const MyriadProRegularMetrics = { lineHeight: 1.2, lineGap: 0.2 };
280280

281281
export {
282282
MyriadProBoldFactors,
283283
MyriadProBoldItalicFactors,
284-
MyriadProBoldItalicLineHeight,
285-
MyriadProBoldLineHeight,
284+
MyriadProBoldItalicMetrics,
285+
MyriadProBoldMetrics,
286286
MyriadProItalicFactors,
287-
MyriadProItalicLineHeight,
287+
MyriadProItalicMetrics,
288288
MyriadProRegularFactors,
289-
MyriadProRegularLineHeight,
289+
MyriadProRegularMetrics,
290290
};

src/core/segoeui_factors.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const SegoeuiBoldFactors = [
8181
1.02511, 0.99298, 1.07237, 0.96752, 0.96752, 0.96752, 0.96752, 1.03424,
8282
0.96752, 0.95801, 0.95801, 0.95801, 0.95801, 1.03424, 1.0106,
8383
];
84-
const SegoeuiBoldLineHeight = 1.33008;
84+
const SegoeuiBoldMetrics = { lineHeight: 1.33008, lineGap: 0 };
8585

8686
// Factors to rescale LiberationSans-BoldItalic.ttf to have the same
8787
// metrics as segoeuiz.ttf.
@@ -152,7 +152,7 @@ const SegoeuiBoldItalicFactors = [
152152
0.96752, 0.96752, 1.036, 0.96752, 0.97168, 0.97168, 0.97168, 0.97168, 1.036,
153153
0.95134,
154154
];
155-
const SegoeuiBoldItalicLineHeight = 1.33008;
155+
const SegoeuiBoldItalicMetrics = { lineHeight: 1.33008, lineGap: 0 };
156156

157157
// Factors to rescale LiberationSans-Italic.ttf to have the same
158158
// metrics as segoeuii.ttf.
@@ -223,7 +223,7 @@ const SegoeuiItalicFactors = [
223223
0.96777, 0.96777, 0.96777, 0.96927, 0.96777, 0.9043, 0.9043, 0.9043, 0.9043,
224224
0.96927, 0.95364,
225225
];
226-
const SegoeuiItalicLineHeight = 1.33008;
226+
const SegoeuiItalicMetrics = { lineHeight: 1.33008, lineGap: 0 };
227227

228228
// Factors to rescale LiberationSans-Regular.ttf to have the same
229229
// metrics as segoeui.ttf.
@@ -294,15 +294,15 @@ const SegoeuiRegularFactors = [
294294
1.00068, 0.91797, 0.99346, 0.96777, 0.96777, 0.96777, 0.96777, 0.96927,
295295
0.96777, 0.9043, 0.9043, 0.9043, 0.9043, 0.96927, 1.00221,
296296
];
297-
const SegoeuiRegularLineHeight = 1.33008;
297+
const SegoeuiRegularMetrics = { lineHeight: 1.33008, lineGap: 0 };
298298

299299
export {
300300
SegoeuiBoldFactors,
301301
SegoeuiBoldItalicFactors,
302-
SegoeuiBoldItalicLineHeight,
303-
SegoeuiBoldLineHeight,
302+
SegoeuiBoldItalicMetrics,
303+
SegoeuiBoldMetrics,
304304
SegoeuiItalicFactors,
305-
SegoeuiItalicLineHeight,
305+
SegoeuiItalicMetrics,
306306
SegoeuiRegularFactors,
307-
SegoeuiRegularLineHeight,
307+
SegoeuiRegularMetrics,
308308
};

src/core/xfa/fonts.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* limitations under the License.
1414
*/
1515

16+
import { $globalData } from "./xfa_object.js";
17+
import { stripQuotes } from "./utils.js";
1618
import { warn } from "../../shared/util.js";
1719

1820
class FontFinder {
@@ -178,4 +180,32 @@ function selectFont(xfaFont, typeface) {
178180
return typeface.regular;
179181
}
180182

181-
export { FontFinder, selectFont };
183+
function getMetrics(xfaFont, real = false) {
184+
let pdfFont = null;
185+
if (xfaFont) {
186+
const name = stripQuotes(xfaFont.typeface);
187+
const typeface = xfaFont[$globalData].fontFinder.find(name);
188+
pdfFont = selectFont(xfaFont, typeface);
189+
}
190+
191+
if (!pdfFont) {
192+
return {
193+
lineHeight: 12,
194+
lineGap: 2,
195+
lineNoGap: 10,
196+
};
197+
}
198+
199+
const size = xfaFont.size || 10;
200+
const lineHeight = pdfFont.lineHeight
201+
? Math.max(real ? 0 : 1.2, pdfFont.lineHeight)
202+
: 1.2;
203+
const lineGap = pdfFont.lineGap === undefined ? 0.2 : pdfFont.lineGap;
204+
return {
205+
lineHeight: lineHeight * size,
206+
lineGap: lineGap * size,
207+
lineNoGap: Math.max(1, lineHeight - lineGap) * size,
208+
};
209+
}
210+
211+
export { FontFinder, getMetrics, selectFont };

src/core/xfa/html_utils.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ function layoutNode(node, availableSpace) {
247247
}
248248
}
249249

250-
const maxWidth = !node.w ? availableSpace.width : node.w;
250+
const maxWidth = (!node.w ? availableSpace.width : node.w) - marginH;
251251
const fontFinder = node[$globalData].fontFinder;
252252
if (
253253
node.value.exData &&
@@ -614,10 +614,8 @@ function setFontFamily(xfaFont, fontFinder, style) {
614614
}
615615

616616
const pdfFont = selectFont(xfaFont, typeface);
617-
if (pdfFont && pdfFont.lineHeight > 0) {
617+
if (pdfFont) {
618618
style.lineHeight = Math.max(1.2, pdfFont.lineHeight);
619-
} else {
620-
style.lineHeight = 1.2;
621619
}
622620
}
623621
}

0 commit comments

Comments
 (0)