Skip to content

Commit 1c97647

Browse files
authored
Merge pull request #23 from xweiba/main
Fix the issue where TextNode does not render when it is an empty string and the line-height issue in non-Firefox browsers; also, fix the misalignment of textDecorationLine at high resolutions.
2 parents d3cf8b0 + ecc0df2 commit 1c97647

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

src/dom/node-parser.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const parseNodeTree = (context: Context, node: Node, parent: ElementContainer, r
1818
for (let childNode = node.firstChild, nextNode; childNode; childNode = nextNode) {
1919
nextNode = childNode.nextSibling;
2020

21-
if (isTextNode(childNode) && childNode.data.trim().length > 0) {
21+
// Fixes #2238 #1624 - Fix the issue of TextNode content being overlooked in rendering due to being perceived as blank by trim().
22+
if (isTextNode(childNode) && childNode.data.length > 0) {
2223
parent.textNodes.push(new TextContainer(context, childNode, parent.styles));
2324
} else if (isElementNode(childNode)) {
2425
if (isSlotElement(childNode) && childNode.assignedNodes) {

src/render/canvas/canvas-renderer.ts

+14-25
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,14 @@ export class CanvasRenderer extends Renderer {
153153

154154
renderTextWithLetterSpacing(text: TextBounds, letterSpacing: number, baseline: number): void {
155155
if (letterSpacing === 0) {
156-
this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + baseline);
156+
// Fixed an issue with characters moving up in non-Firefox.
157+
// https://github.com/niklasvh/html2canvas/issues/2107#issuecomment-692462900
158+
if (navigator.userAgent.indexOf('Firefox') === -1){
159+
this.ctx.textBaseline = 'ideographic';
160+
this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + text.bounds.height);
161+
} else {
162+
this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + baseline);
163+
}
157164
} else {
158165
const letters = segmentGraphemes(text.text);
159166
letters.reduce((left, letter) => {
@@ -188,7 +195,7 @@ export class CanvasRenderer extends Renderer {
188195
this.ctx.direction = styles.direction === DIRECTION.RTL ? 'rtl' : 'ltr';
189196
this.ctx.textAlign = 'left';
190197
this.ctx.textBaseline = 'alphabetic';
191-
const { baseline, middle } = this.fontMetrics.getMetrics(fontFamily, fontSize);
198+
const { baseline } = this.fontMetrics.getMetrics(fontFamily, fontSize);
192199
const paintOrder = styles.paintOrder;
193200

194201
text.textBounds.forEach((text) => {
@@ -224,36 +231,18 @@ export class CanvasRenderer extends Renderer {
224231

225232
if (styles.textDecorationLine.length) {
226233
this.ctx.fillStyle = asString(styles.textDecorationColor || styles.color);
234+
var decorationLineHeight = 1;
227235
styles.textDecorationLine.forEach((textDecorationLine) => {
236+
// Fix the issue where textDecorationLine exhibits x-axis positioning errors on high-resolution devices due to varying devicePixelRatio, corrected by using relative values of element heights.
228237
switch (textDecorationLine) {
229238
case TEXT_DECORATION_LINE.UNDERLINE:
230-
// Draws a line at the baseline of the font
231-
// TODO As some browsers display the line as more than 1px if the font-size is big,
232-
// need to take that into account both in position and size
233-
this.ctx.fillRect(
234-
text.bounds.left,
235-
Math.round(text.bounds.top + baseline),
236-
text.bounds.width,
237-
1
238-
);
239-
239+
this.ctx.fillRect(text.bounds.left, text.bounds.top + text.bounds.height - decorationLineHeight, text.bounds.width, decorationLineHeight);
240240
break;
241241
case TEXT_DECORATION_LINE.OVERLINE:
242-
this.ctx.fillRect(
243-
text.bounds.left,
244-
Math.round(text.bounds.top),
245-
text.bounds.width,
246-
1
247-
);
242+
this.ctx.fillRect(text.bounds.left, text.bounds.top , text.bounds.width, decorationLineHeight);
248243
break;
249244
case TEXT_DECORATION_LINE.LINE_THROUGH:
250-
// TODO try and find exact position for line-through
251-
this.ctx.fillRect(
252-
text.bounds.left,
253-
Math.ceil(text.bounds.top + middle),
254-
text.bounds.width,
255-
1
256-
);
245+
this.ctx.fillRect(text.bounds.left, text.bounds.top + (text.bounds.height / 2 - decorationLineHeight / 2), text.bounds.width, decorationLineHeight);
257246
break;
258247
}
259248
});

0 commit comments

Comments
 (0)