mirror of
https://github.com/PR0M3TH3AN/Artstr-Studio.git
synced 2026-10-31 02:30:39 +00:00
The plain-text fillText path (drawTextLayerPlainToCanvas) strips inline
marks via layerHtmlToPlainText. Add a rich-text path that preserves
inline bold / italic / underline / lists / links in the JPEG output.
First attempt was an SVG <foreignObject> wrapping the layer's HTML,
loaded via a Blob URL and drawImage'd onto the canvas — clean and
short. Chromium taints the canvas on every <foreignObject> SVG
regardless of origin, so toBlob('image/jpeg') threw SecurityError.
Confirmed via CDP: `kind:'taint', err:"Tainted canvases may not be
exported."`
Switched to a pure-canvas rich-text engine:
- _parseTextRunsFromHtml walks the layer's HTML into a flat list of
styled runs ({text, bold, italic, underline} or {br}). Handles
<b> / <strong>, <i> / <em>, <u>, <a> (visual underline only,
links don't survive into the canvas image), <br>, <p> / <div> /
<h1>-<h3> (block break before+after), <ul>/<ol>/<li> (bullets
for unordered, "1.", "2.", … prefixes for ordered, properly
nested via a list stack).
- _layoutTextRuns walks the runs with ctx.measureText, builds a line
list with word-wrap at the layer's inner width. Trailing whitespace
at a wrap point is discarded.
- drawTextLayerRichToCanvas applies the layer's clip, walks the lines
with proper left / center / right alignment using each line's total
width, fillText per item, and a tiny fillRect under each underlined
run (sat just below the baseline at ~95% line-height, ~6% thick).
The dispatcher drawTextLayerToCanvas calls the rich path when
_textHasInlineMarks(layer.html) — i.e. layer.html contains any of
b/strong/i/em/u/a/br/ul/ol/li/span/font — and the plain path
otherwise. drawLayerToCanvas now awaits drawTextLayerToCanvas (it
became async).
End-to-end verified via CDP on a Custom Art canvas:
layer 1: "<b>Bold</b>, <i>italic</i>, <u>underlined</u>,
<a href=…>link</a>."
layer 2: "<ul><li>Item one</li>… </ul>"
Resulting JPEG renders Bold as bold, italic as italic, both
underlined and link as underlined, and the list as bulleted lines.
Co-Authored-By: Claude Opus 4.7 <[email protected]>