Files
Reactive-Resume/patches/@react-pdf__textkit.patch
T
Amruth Pillai 8354c39c45 fix(pdf): respect requested font metrics when positioning text (#3430)
* fix(pdf): respect requested font metrics when positioning text

* fix(pdf): preserve Noto Sans HK line metrics
2026-09-05 07:32:04 -07:00

73 lines
2.8 KiB
Diff

diff --git a/lib/textkit.js b/lib/textkit.js
index 2c0ec95..3683493 100644
--- a/lib/textkit.js
+++ b/lib/textkit.js
@@ -709,6 +709,28 @@ const omit = (value, run) => {
return Object.assign({}, run, { attributes });
};
+/**
+ * Honor the font's USE_TYPO_METRICS flag instead of substituting OS/2
+ * metrics for every font. Roboto and IBM Plex Sans Condensed deliberately
+ * use hhea metrics; their unused typo ascenders move text above icons.
+ * Keep the compact metrics used by our Noto CJK / Source Han fallbacks
+ * to avoid restoring inflated line boxes and CJK clipping (#2986).
+ * Standard PDF fonts have no OS/2 table and retain their own metrics.
+ */
+const resolveTypoMetrics = (font) => {
+ const os2 = font?.['OS/2'];
+ const isCjkFont = /^(?:Noto (?:Sans|Serif) (?:SC|TC|HK|JP|KR)|Source Han (?:Sans|Serif)(?: (?:SC|TC|HK|JP|KR))?)$/.test(font?.familyName || '');
+ const useTypoMetrics = os2?.fsSelection?.useTypoMetrics || isCjkFont;
+ if (!useTypoMetrics || typeof os2?.typoAscender !== 'number' || typeof os2?.typoDescender !== 'number') {
+ return { ascent: font?.ascent || 0, descent: font?.descent || 0, lineGap: font?.lineGap || 0 };
+ }
+ return {
+ ascent: os2.typoAscender,
+ descent: os2.typoDescender,
+ lineGap: typeof os2.typoLineGap === 'number' ? os2.typoLineGap : (font.lineGap || 0),
+ };
+};
+
/**
* Get run ascent
*
@@ -718,7 +743,7 @@ const omit = (value, run) => {
const ascent$1 = (run) => {
const { font, attachment } = run.attributes;
const attachmentHeight = attachment?.height || 0;
- const fontAscent = typeof font === 'string' ? 0 : font?.[0]?.ascent || 0;
+ const fontAscent = typeof font === 'string' ? 0 : resolveTypoMetrics(font?.[0]).ascent;
return Math.max(attachmentHeight, fontAscent * scale(run));
};
@@ -730,7 +755,7 @@ const ascent$1 = (run) => {
*/
const descent = (run) => {
const font = run.attributes?.font;
- const fontDescent = typeof font === 'string' ? 0 : font?.[0]?.descent || 0;
+ const fontDescent = typeof font === 'string' ? 0 : resolveTypoMetrics(font?.[0]).descent;
return scale(run) * fontDescent;
};
@@ -742,8 +767,8 @@ const descent = (run) => {
*/
const lineGap = (run) => {
const font = run.attributes?.font;
- const lineGap = typeof font === 'string' ? 0 : font?.[0]?.lineGap || 0;
- return lineGap * scale(run);
+ const fontLineGap = typeof font === 'string' ? 0 : resolveTypoMetrics(font?.[0]).lineGap;
+ return fontLineGap * scale(run);
};
/**
@@ -754,7 +779,8 @@ const lineGap = (run) => {
*/
const height$1 = (run) => {
const lineHeight = run.attributes?.lineHeight;
- return lineHeight || lineGap(run) + ascent$1(run) - descent(run);
+ const intrinsic = lineGap(run) + ascent$1(run) - descent(run);
+ return Math.max(lineHeight || 0, intrinsic);
};
/**