Skip to content

Commit

Permalink
fix: ios 15 font rendering crash (#2645)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvh authored Dec 29, 2021
1 parent d922207 commit ba2b1cd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/render/canvas/canvas-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class CanvasRenderer extends Renderer {
const fontVariant = styles.fontVariant
.filter((variant) => variant === 'normal' || variant === 'small-caps')
.join('');
const fontFamily = styles.fontFamily.join(', ');
const fontFamily = fixIOSSystemFonts(styles.fontFamily).join(', ');
const fontSize = isDimensionToken(styles.fontSize)
? `${styles.fontSize.number}${styles.fontSize.unit}`
: `${styles.fontSize.number}px`;
Expand Down Expand Up @@ -947,3 +947,12 @@ const canvasTextAlign = (textAlign: TEXT_ALIGN): CanvasTextAlign => {
return 'left';
}
};

// see https://github.com/niklasvh/html2canvas/pull/2645
const iOSBrokenFonts = ['-apple-system', 'system-ui'];

const fixIOSSystemFonts = (fontFamilies: string[]): string[] => {
return /iPhone OS 15_(0|1)/.test(window.navigator.userAgent)
? fontFamilies.filter((fontFamily) => iOSBrokenFonts.indexOf(fontFamily) === -1)
: fontFamilies;
};
9 changes: 9 additions & 0 deletions tests/reftests/text/lang/chinese.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
float: left;
}

.apple-system {
font-family: -apple-system, Arial;
}

.system-ui {
font-family: system-ui, Arial;
}
</style>
</head>
<body>
Expand All @@ -32,5 +39,7 @@
</p><p>&nbsp;&nbsp;&nbsp;&nbsp;〔13〕 法捷耶夫(一九○一——一九五六),苏联名作家。他所作的小说《毁灭》于一九二七年出版,内容是描写苏联国内战争时期由苏联远东滨海边区工人、农民和革命知识分子所组成的一支游击队同国内反革命白卫军以及日本武装干涉军进行斗争的故事。这部小说曾由鲁迅译为汉文。
</p><p>&nbsp;&nbsp;&nbsp;&nbsp;〔14〕 见鲁迅《集外集·自嘲》(《鲁迅全集》第7卷,人民文学出版社1981年版,第147页)。</p>
</div>
<div class="apple-system">中文</div>
<div class="system-ui">中文</div>
</body>
</html>
2 changes: 1 addition & 1 deletion tests/reftests/text/text.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ <h3>&lt;h3&gt; misc text alignments</h3>
<span>[AB / CD]</span>
</div>
<div>Emojis 🤷🏾‍♂️👨‍👩‍👧‍👦 :)</div>
<div style="letter-spacing: 2px">Emojis with letter-spacing 🤷🏾‍♂️👨‍👩‍👧‍👦 :)</div>
<div style="letter-spacing: 2px">Emojis with letter-spacing 🤷🏾‍♂️👨‍👩‍👧‍👦 :) ❤️❤️❤️👨‍❤️‍💋‍👨👨‍❤️‍👨</div>
</body>
</html>

0 comments on commit ba2b1cd

Please sign in to comment.