diff --git a/dist/esm/index.mjs b/dist/esm/index.mjs index b3af3fb..88ccf98 100644 --- a/dist/esm/index.mjs +++ b/dist/esm/index.mjs @@ -3054,7 +3054,8 @@ var getReadableImageError = (err, node) => { var drawSvg = ({ drawable, dimensions, - contextToDraw + contextToDraw, + snapCaptionPaintOrigin }) => { const fitted = fitSvgIntoItsContainer({ containerSize: dimensions, @@ -3063,7 +3064,9 @@ var drawSvg = ({ height: drawable.height } }); - contextToDraw.drawImage(drawable, fitted.left, fitted.top, fitted.width, fitted.height); + // Blink snaps inline SVG paint origins before applying ancestor transforms. + const snap = snapCaptionPaintOrigin && Number.isInteger(fitted.width) && Number.isInteger(fitted.height); + contextToDraw.drawImage(drawable, snap ? Math.round(fitted.left) : fitted.left, snap ? Math.round(fitted.top) : fitted.top, fitted.width, fitted.height); }; var drawReplacedElement = ({ drawable, @@ -3126,7 +3129,7 @@ var drawReplacedElement = ({ }); contextToDraw.drawImage(drawable, result.sourceX, result.sourceY, result.sourceWidth, result.sourceHeight, result.destX, result.destY, result.destWidth, result.destHeight); }; -var drawDomElement = (node) => { +var drawDomElement = (node, snapSvgPaintOrigin) => { const domDrawFn = async ({ dimensions, contextToDraw, @@ -3134,7 +3137,7 @@ var drawDomElement = (node) => { }) => { if (node instanceof SVGSVGElement) { const drawable = await turnSvgIntoDrawable(node); - drawSvg({ drawable, dimensions, contextToDraw }); + drawSvg({ drawable, dimensions, contextToDraw, snapCaptionPaintOrigin: snapSvgPaintOrigin && node.hasAttribute("data-cutt-subtitle-viewport") }); return; } if (node instanceof HTMLImageElement || node instanceof HTMLCanvasElement) { @@ -3673,6 +3676,10 @@ var calculateTransforms = ({ blendMode = getBlendMode(computedStyle.mixBlendMode); const needsGroup = blendMode !== null || isolated; filterForPrecompositing = needsGroup && computedStyle.filter !== "none" ? computedStyle.filter : filterRequiresPrecompositing(computedStyle.filter); + // Transform the marked card as one image, including its text and border. + if (parent !== rootElement && computedStyle.filter === "none" && parent.hasAttribute("data-cutt-comment-paint-group")) { + filterForPrecompositing = "drop-shadow(0px 0px 0px transparent)"; + } const originalMaskImage = parent.style.maskImage; const originalWebkitMaskImage = parent.style.webkitMaskImage; parent.style.maskImage = "none"; @@ -5958,52 +5965,49 @@ var drawText = ({ const transformedText = applyTextTransform(originalText, textTransform); span.textContent = transformedText; const tokens = findWords(span); - const textShadows = parseTextShadow(textShadowValue); + const textShadows = onlyBackgroundClipText ? [] : parseTextShadow(textShadowValue); const ctm = contextToDraw.getTransform(); const blurScale = Math.hypot(ctm.a, ctm.b); const { strokeFirst } = parsePaintOrder(paintOrder); const measurements = contextToDraw.measureText(originalText); const { fontBoundingBoxDescent, fontBoundingBoxAscent } = measurements; const fontHeight = fontBoundingBoxAscent + fontBoundingBoxDescent; - for (const token of tokens) { - const leading = token.rect.height - fontHeight; - const halfLeading = leading / 2; - const x = (isRTL ? token.rect.right : token.rect.left) - parentRect.x; - const y = token.rect.top + fontBoundingBoxAscent + halfLeading - parentRect.y; - for (let i = textShadows.length - 1;i >= 0; i--) { - const shadow = textShadows[i]; + const positioned = tokens.map((token) => ({ + token, + x: (isRTL ? token.rect.right : token.rect.left) - parentRect.x, + y: token.rect.top + fontBoundingBoxAscent + (token.rect.height - fontHeight) / 2 - parentRect.y, + })); + // CSS shadows are independent of the fill alpha and sit below the entire text run. + for (let i = textShadows.length - 1; i >= 0; i--) { + const shadow = textShadows[i]; + for (const { token, x, y } of positioned) { + const bounds = contextToDraw.measureText(token.text); + const right = Math.max(...[ + [x - bounds.actualBoundingBoxLeft, y - bounds.actualBoundingBoxAscent], + [x + bounds.actualBoundingBoxRight, y - bounds.actualBoundingBoxAscent], + [x - bounds.actualBoundingBoxLeft, y + bounds.actualBoundingBoxDescent], + [x + bounds.actualBoundingBoxRight, y + bounds.actualBoundingBoxDescent], + ].map(([px, py]) => ctm.a * px + ctm.c * py + ctm.e)); + const shift = Math.max(0, right) + contextToDraw.canvas.width + 1; + contextToDraw.save(); + contextToDraw.setTransform(ctm.a, ctm.b, ctm.c, ctm.d, ctm.e - shift, ctm.f); + contextToDraw.fillStyle = "black"; contextToDraw.shadowColor = shadow.color; contextToDraw.shadowBlur = shadow.blurRadius * blurScale; - contextToDraw.shadowOffsetX = shadow.offsetX * ctm.a + shadow.offsetY * ctm.c; + contextToDraw.shadowOffsetX = shadow.offsetX * ctm.a + shadow.offsetY * ctm.c + shift; contextToDraw.shadowOffsetY = shadow.offsetX * ctm.b + shadow.offsetY * ctm.d; contextToDraw.fillText(token.text, x, y); + contextToDraw.restore(); } - contextToDraw.shadowColor = "transparent"; - contextToDraw.shadowBlur = 0; - contextToDraw.shadowOffsetX = 0; - contextToDraw.shadowOffsetY = 0; + } + for (const { token, x, y } of positioned) { const drawFill = () => contextToDraw.fillText(token.text, x, y); const drawStroke = () => { - if (hasStroke) { - contextToDraw.strokeText(token.text, x, y); - } + if (hasStroke) contextToDraw.strokeText(token.text, x, y); }; - if (strokeFirst) { - drawStroke(); - drawFill(); - } else { - drawFill(); - drawStroke(); - } - drawTextDecoration({ - contextToDraw, - fontSizePx, - measurements, - parentRect, - textDecorations, - token, - y - }); + if (strokeFirst) { drawStroke(); drawFill(); } + else { drawFill(); drawStroke(); } + drawTextDecoration({ contextToDraw, fontSizePx, measurements, parentRect, textDecorations, token, y }); } span.textContent = originalText; finishFilter(); @@ -6069,7 +6073,8 @@ var walkOverNode = ({ return processNode({ element: node, context, - draw: drawDomElement(node), + // Cropped intermediate canvases have their own transform and raster origin. + draw: drawDomElement(node, scale === 1 && parentRect.x === 0 && parentRect.y === 0 && !onlyBackgroundClipText), logLevel, parentRect, internalState,