html2canvas-pro 2.4.4 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (228) hide show
  1. package/README.md +115 -13
  2. package/dist/html2canvas-pro.cjs +3246 -1601
  3. package/dist/html2canvas-pro.cjs.map +1 -1
  4. package/dist/html2canvas-pro.esm.js +3246 -1601
  5. package/dist/html2canvas-pro.esm.js.map +1 -1
  6. package/dist/html2canvas-pro.js +3242 -1597
  7. package/dist/html2canvas-pro.js.map +1 -1
  8. package/dist/html2canvas-pro.min.js +5 -5
  9. package/dist/types/core/cache-storage.d.ts +4 -1
  10. package/dist/types/core/context.d.ts +10 -1
  11. package/dist/types/core/render-element.d.ts +11 -0
  12. package/dist/types/core/util.d.ts +6 -0
  13. package/dist/types/css/index.d.ts +77 -46
  14. package/dist/types/css/parse.d.ts +15 -0
  15. package/dist/types/css/property-descriptors/accent-color.d.ts +5 -0
  16. package/dist/types/css/property-descriptors/backdrop-filter.d.ts +9 -0
  17. package/dist/types/css/property-descriptors/background-blend-mode.d.ts +16 -16
  18. package/dist/types/css/property-descriptors/border-image-width.d.ts +36 -0
  19. package/dist/types/css/property-descriptors/clip-path.d.ts +16 -2
  20. package/dist/types/css/property-descriptors/isolation.d.ts +6 -0
  21. package/dist/types/css/property-descriptors/mask.d.ts +25 -0
  22. package/dist/types/css/property-descriptors/mix-blend-mode.d.ts +16 -16
  23. package/dist/types/css/property-descriptors/outline.d.ts +15 -0
  24. package/dist/types/css/property-descriptors/text-emphasis.d.ts +22 -0
  25. package/dist/types/css/property-descriptors/text-shadow.d.ts +1 -1
  26. package/dist/types/css/property-descriptors/webkit-box-reflect.d.ts +19 -0
  27. package/dist/types/css/property-descriptors/webkit-text-fill-color.d.ts +9 -0
  28. package/dist/types/css/syntax/token-constants.d.ts +1 -1
  29. package/dist/types/css/types/color-math.d.ts +7 -1
  30. package/dist/types/css/types/color-spaces/a98.d.ts +2 -1
  31. package/dist/types/css/types/color-spaces/p3.d.ts +5 -4
  32. package/dist/types/css/types/color-spaces/pro-photo.d.ts +5 -4
  33. package/dist/types/css/types/color-spaces/rec2020.d.ts +5 -4
  34. package/dist/types/css/types/color-spaces/srgb.d.ts +5 -4
  35. package/dist/types/css/types/color-utilities.d.ts +11 -11
  36. package/dist/types/css/types/color.d.ts +1 -0
  37. package/dist/types/css/types/functions/conic-gradient.d.ts +11 -0
  38. package/dist/types/css/types/functions/image-set.d.ts +11 -0
  39. package/dist/types/css/types/functions/repeating-radial-gradient.d.ts +8 -0
  40. package/dist/types/css/types/image.d.ts +15 -2
  41. package/dist/types/css/types/safe-eval.d.ts +0 -6
  42. package/dist/types/dom/copy-css-styles.d.ts +8 -0
  43. package/dist/types/dom/document-cloner.d.ts +3 -13
  44. package/dist/types/dom/iframe-mount.d.ts +37 -0
  45. package/dist/types/dom/pseudo-content.d.ts +33 -0
  46. package/dist/types/dom/replaced-elements/iframe-element-container.d.ts +2 -2
  47. package/dist/types/options.d.ts +8 -2
  48. package/dist/types/render/canvas/background-renderer.d.ts +19 -0
  49. package/dist/types/render/canvas/border-image-renderer.d.ts +50 -1
  50. package/dist/types/render/canvas/box-shadow-geometry.d.ts +3 -0
  51. package/dist/types/render/canvas/box-shadow-painter.d.ts +13 -0
  52. package/dist/types/render/canvas/box-shadow-transform.d.ts +16 -0
  53. package/dist/types/render/canvas/canvas-renderer.d.ts +21 -15
  54. package/dist/types/render/canvas/mask-renderer.d.ts +36 -0
  55. package/dist/types/render/canvas/text-clip-renderer.d.ts +36 -0
  56. package/dist/types/render/canvas/text-renderer.d.ts +22 -0
  57. package/dist/types/render/stacking-context.d.ts +4 -1
  58. package/package.json +39 -40
  59. package/dist/lib/config.js +0 -49
  60. package/dist/lib/core/abort-helper.js +0 -20
  61. package/dist/lib/core/background-parser.js +0 -41
  62. package/dist/lib/core/bitwise.js +0 -5
  63. package/dist/lib/core/cache-storage.js +0 -283
  64. package/dist/lib/core/config-assembler.js +0 -88
  65. package/dist/lib/core/constants.js +0 -25
  66. package/dist/lib/core/context.js +0 -19
  67. package/dist/lib/core/debugger.js +0 -27
  68. package/dist/lib/core/features.js +0 -190
  69. package/dist/lib/core/logger.js +0 -56
  70. package/dist/lib/core/lru-map.js +0 -66
  71. package/dist/lib/core/origin-checker.js +0 -53
  72. package/dist/lib/core/performance-monitor.js +0 -185
  73. package/dist/lib/core/render-element.js +0 -131
  74. package/dist/lib/core/util.js +0 -4
  75. package/dist/lib/core/validator.js +0 -500
  76. package/dist/lib/css/grouped/background-styles.js +0 -36
  77. package/dist/lib/css/grouped/border-styles.js +0 -75
  78. package/dist/lib/css/grouped/font-styles.js +0 -93
  79. package/dist/lib/css/grouped/layout-styles.js +0 -127
  80. package/dist/lib/css/index.js +0 -339
  81. package/dist/lib/css/layout/bounds.js +0 -52
  82. package/dist/lib/css/layout/text.js +0 -133
  83. package/dist/lib/css/property-descriptor.js +0 -2
  84. package/dist/lib/css/property-descriptors/background-blend-mode.js +0 -41
  85. package/dist/lib/css/property-descriptors/background-clip.js +0 -25
  86. package/dist/lib/css/property-descriptors/background-color.js +0 -10
  87. package/dist/lib/css/property-descriptors/background-image.js +0 -24
  88. package/dist/lib/css/property-descriptors/background-origin.js +0 -23
  89. package/dist/lib/css/property-descriptors/background-position.js +0 -19
  90. package/dist/lib/css/property-descriptors/background-repeat.js +0 -33
  91. package/dist/lib/css/property-descriptors/background-size.js +0 -23
  92. package/dist/lib/css/property-descriptors/border-color.js +0 -14
  93. package/dist/lib/css/property-descriptors/border-image-repeat.js +0 -42
  94. package/dist/lib/css/property-descriptors/border-image-slice.js +0 -45
  95. package/dist/lib/css/property-descriptors/border-image-source.js +0 -22
  96. package/dist/lib/css/property-descriptors/border-radius.js +0 -15
  97. package/dist/lib/css/property-descriptors/border-style.js +0 -26
  98. package/dist/lib/css/property-descriptors/border-width.js +0 -20
  99. package/dist/lib/css/property-descriptors/box-decoration-break.js +0 -18
  100. package/dist/lib/css/property-descriptors/box-shadow.js +0 -54
  101. package/dist/lib/css/property-descriptors/clip-path.js +0 -189
  102. package/dist/lib/css/property-descriptors/color.js +0 -10
  103. package/dist/lib/css/property-descriptors/content.js +0 -19
  104. package/dist/lib/css/property-descriptors/counter-increment.js +0 -35
  105. package/dist/lib/css/property-descriptors/counter-reset.js +0 -18
  106. package/dist/lib/css/property-descriptors/direction.js +0 -18
  107. package/dist/lib/css/property-descriptors/display.js +0 -81
  108. package/dist/lib/css/property-descriptors/duration.js +0 -14
  109. package/dist/lib/css/property-descriptors/filter.js +0 -86
  110. package/dist/lib/css/property-descriptors/float.js +0 -22
  111. package/dist/lib/css/property-descriptors/font-family.js +0 -47
  112. package/dist/lib/css/property-descriptors/font-size.js +0 -10
  113. package/dist/lib/css/property-descriptors/font-style.js +0 -20
  114. package/dist/lib/css/property-descriptors/font-variant-ligatures.js +0 -34
  115. package/dist/lib/css/property-descriptors/font-variant.js +0 -13
  116. package/dist/lib/css/property-descriptors/font-weight.js +0 -25
  117. package/dist/lib/css/property-descriptors/image-rendering.js +0 -33
  118. package/dist/lib/css/property-descriptors/letter-spacing.js +0 -21
  119. package/dist/lib/css/property-descriptors/line-break.js +0 -23
  120. package/dist/lib/css/property-descriptors/line-height.js +0 -24
  121. package/dist/lib/css/property-descriptors/list-style-image.js +0 -17
  122. package/dist/lib/css/property-descriptors/list-style-position.js +0 -18
  123. package/dist/lib/css/property-descriptors/list-style-type.js +0 -122
  124. package/dist/lib/css/property-descriptors/margin.js +0 -13
  125. package/dist/lib/css/property-descriptors/mix-blend-mode.js +0 -31
  126. package/dist/lib/css/property-descriptors/object-fit.js +0 -28
  127. package/dist/lib/css/property-descriptors/object-position.js +0 -42
  128. package/dist/lib/css/property-descriptors/opacity.js +0 -16
  129. package/dist/lib/css/property-descriptors/overflow-wrap.js +0 -18
  130. package/dist/lib/css/property-descriptors/overflow.js +0 -27
  131. package/dist/lib/css/property-descriptors/padding.js +0 -14
  132. package/dist/lib/css/property-descriptors/paint-order.js +0 -33
  133. package/dist/lib/css/property-descriptors/position.js +0 -22
  134. package/dist/lib/css/property-descriptors/quotes.js +0 -41
  135. package/dist/lib/css/property-descriptors/rotate.js +0 -27
  136. package/dist/lib/css/property-descriptors/text-align.js +0 -21
  137. package/dist/lib/css/property-descriptors/text-decoration-color.js +0 -10
  138. package/dist/lib/css/property-descriptors/text-decoration-line.js +0 -28
  139. package/dist/lib/css/property-descriptors/text-decoration-style.js +0 -24
  140. package/dist/lib/css/property-descriptors/text-decoration-thickness.js +0 -26
  141. package/dist/lib/css/property-descriptors/text-overflow.js +0 -18
  142. package/dist/lib/css/property-descriptors/text-shadow.js +0 -46
  143. package/dist/lib/css/property-descriptors/text-transform.js +0 -20
  144. package/dist/lib/css/property-descriptors/text-underline-offset.js +0 -23
  145. package/dist/lib/css/property-descriptors/transform-origin.js +0 -24
  146. package/dist/lib/css/property-descriptors/transform.js +0 -57
  147. package/dist/lib/css/property-descriptors/visibility.js +0 -20
  148. package/dist/lib/css/property-descriptors/webkit-line-clamp.js +0 -26
  149. package/dist/lib/css/property-descriptors/webkit-text-stroke-color.js +0 -10
  150. package/dist/lib/css/property-descriptors/webkit-text-stroke-width.js +0 -16
  151. package/dist/lib/css/property-descriptors/white-space.js +0 -32
  152. package/dist/lib/css/property-descriptors/word-break.js +0 -26
  153. package/dist/lib/css/property-descriptors/writing-mode.js +0 -28
  154. package/dist/lib/css/property-descriptors/z-index.js +0 -19
  155. package/dist/lib/css/property-descriptors/zoom.js +0 -18
  156. package/dist/lib/css/syntax/parser.js +0 -141
  157. package/dist/lib/css/syntax/token-constants.js +0 -214
  158. package/dist/lib/css/syntax/token-singletons.js +0 -36
  159. package/dist/lib/css/syntax/token-types.js +0 -10
  160. package/dist/lib/css/syntax/tokenizer.js +0 -502
  161. package/dist/lib/css/type-descriptor.js +0 -2
  162. package/dist/lib/css/types/angle.js +0 -80
  163. package/dist/lib/css/types/color-math.js +0 -25
  164. package/dist/lib/css/types/color-spaces/a98.js +0 -78
  165. package/dist/lib/css/types/color-spaces/p3.js +0 -86
  166. package/dist/lib/css/types/color-spaces/pro-photo.js +0 -83
  167. package/dist/lib/css/types/color-spaces/rec2020.js +0 -85
  168. package/dist/lib/css/types/color-spaces/srgb.js +0 -79
  169. package/dist/lib/css/types/color-utilities.js +0 -342
  170. package/dist/lib/css/types/color.js +0 -400
  171. package/dist/lib/css/types/functions/-prefix-linear-gradient.js +0 -32
  172. package/dist/lib/css/types/functions/-prefix-radial-gradient.js +0 -83
  173. package/dist/lib/css/types/functions/-webkit-gradient.js +0 -59
  174. package/dist/lib/css/types/functions/counter.js +0 -367
  175. package/dist/lib/css/types/functions/gradient.js +0 -163
  176. package/dist/lib/css/types/functions/linear-gradient.js +0 -27
  177. package/dist/lib/css/types/functions/radial-gradient.js +0 -90
  178. package/dist/lib/css/types/functions/repeating-linear-gradient.js +0 -9
  179. package/dist/lib/css/types/image.js +0 -62
  180. package/dist/lib/css/types/index.js +0 -2
  181. package/dist/lib/css/types/length-percentage.js +0 -197
  182. package/dist/lib/css/types/length.js +0 -5
  183. package/dist/lib/css/types/safe-eval.js +0 -80
  184. package/dist/lib/css/types/time.js +0 -17
  185. package/dist/lib/dom/document-cloner.js +0 -618
  186. package/dist/lib/dom/dom-normalizer.js +0 -115
  187. package/dist/lib/dom/element-container.js +0 -71
  188. package/dist/lib/dom/elements/li-element-container.js +0 -11
  189. package/dist/lib/dom/elements/ol-element-container.js +0 -12
  190. package/dist/lib/dom/elements/select-element-container.js +0 -12
  191. package/dist/lib/dom/elements/textarea-element-container.js +0 -11
  192. package/dist/lib/dom/node-parser.js +0 -125
  193. package/dist/lib/dom/node-type-guards.js +0 -81
  194. package/dist/lib/dom/replaced-elements/canvas-element-container.js +0 -13
  195. package/dist/lib/dom/replaced-elements/iframe-element-container.js +0 -38
  196. package/dist/lib/dom/replaced-elements/image-element-container.js +0 -14
  197. package/dist/lib/dom/replaced-elements/index.js +0 -2
  198. package/dist/lib/dom/replaced-elements/input-element-container.js +0 -89
  199. package/dist/lib/dom/replaced-elements/svg-element-container.js +0 -57
  200. package/dist/lib/dom/slot-cloner.js +0 -176
  201. package/dist/lib/dom/text-container.js +0 -35
  202. package/dist/lib/index.js +0 -49
  203. package/dist/lib/options.js +0 -2
  204. package/dist/lib/render/background.js +0 -188
  205. package/dist/lib/render/bezier-curve.js +0 -34
  206. package/dist/lib/render/border.js +0 -104
  207. package/dist/lib/render/bound-curves.js +0 -188
  208. package/dist/lib/render/box-sizing.js +0 -20
  209. package/dist/lib/render/canvas/background-renderer.js +0 -338
  210. package/dist/lib/render/canvas/border-image-renderer.js +0 -153
  211. package/dist/lib/render/canvas/border-renderer.js +0 -184
  212. package/dist/lib/render/canvas/canvas-path.js +0 -25
  213. package/dist/lib/render/canvas/canvas-renderer.js +0 -633
  214. package/dist/lib/render/canvas/content-renderer.js +0 -270
  215. package/dist/lib/render/canvas/effects-renderer.js +0 -171
  216. package/dist/lib/render/canvas/filter-surface.js +0 -270
  217. package/dist/lib/render/canvas/font-utils.js +0 -46
  218. package/dist/lib/render/canvas/foreignobject-renderer.js +0 -38
  219. package/dist/lib/render/canvas/surface-bounds.js +0 -64
  220. package/dist/lib/render/canvas/text/text-decoration-renderer.js +0 -99
  221. package/dist/lib/render/canvas/text-renderer.js +0 -486
  222. package/dist/lib/render/effects.js +0 -231
  223. package/dist/lib/render/font-metrics.js +0 -53
  224. package/dist/lib/render/object-fit.js +0 -91
  225. package/dist/lib/render/path.js +0 -26
  226. package/dist/lib/render/renderer-interface.js +0 -2
  227. package/dist/lib/render/stacking-context.js +0 -341
  228. package/dist/lib/render/vector.js +0 -16
@@ -1,486 +0,0 @@
1
- "use strict";
2
- /**
3
- * Text Renderer
4
- *
5
- * Handles rendering of text content including:
6
- * - Text with letter spacing
7
- * - Text decorations (underline, overline, line-through)
8
- * - Text shadows
9
- * - Webkit line clamp
10
- * - Text overflow ellipsis
11
- * - Paint order (fill/stroke)
12
- * - Font styles
13
- */
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.TextRenderer = exports.hasCJKCharacters = void 0;
16
- const text_1 = require("../../css/layout/text");
17
- const color_utilities_1 = require("../../css/types/color-utilities");
18
- const parser_1 = require("../../css/syntax/parser");
19
- const writing_mode_1 = require("../../css/property-descriptors/writing-mode");
20
- const text_decoration_renderer_1 = require("./text/text-decoration-renderer");
21
- const font_utils_1 = require("./font-utils");
22
- // iOS font fix - see https://github.com/niklasvh/html2canvas/pull/2645
23
- const iOSBrokenFonts = ['-apple-system', 'system-ui'];
24
- // Tolerance (px) for text-overflow: ellipsis detection. Canvas measureText can
25
- // report a width a fraction of a pixel wider than the browser's layout box, so
26
- // text that fits exactly must not be truncated (issue #226).
27
- const TEXT_OVERFLOW_EPSILON = 1;
28
- /**
29
- * Detect CJK (Chinese, Japanese, Korean) characters in a string.
30
- * Vertical writing modes use it to keep CJK glyphs upright and on the
31
- * ideographic baseline while other scripts are rotated.
32
- *
33
- * Covers:
34
- * U+2E80–U+2FFF CJK Radicals Supplement, Kangxi Radicals
35
- * U+3000–U+30FF CJK Symbols & Punctuation (。、「」…), Hiragana, Katakana
36
- * U+3400–U+4DBF CJK Extension A
37
- * U+4E00–U+9FFF CJK Unified Ideographs (most common Chinese/Japanese/Korean)
38
- * U+AC00–U+D7AF Hangul Syllables
39
- * U+F900–U+FAFF CJK Compatibility Ideographs
40
- * U+FF01–U+FFEF Halfwidth and Fullwidth Forms (A B 1 2 ! ? etc.)
41
- */
42
- const CJK_CHAR_REGEX = /[\u2E80-\u2FFF\u3000-\u30FF\u3400-\u4DBF\u4E00-\u9FFF\uAC00-\uD7AF\uF900-\uFAFF\uFF01-\uFFEF]/;
43
- const hasCJKCharacters = (text) => CJK_CHAR_REGEX.test(text);
44
- exports.hasCJKCharacters = hasCJKCharacters;
45
- /**
46
- * Detect iOS version from user agent
47
- * Returns null if not iOS or version cannot be determined
48
- */
49
- const getIOSVersion = () => {
50
- if (typeof navigator === 'undefined') {
51
- return null;
52
- }
53
- const userAgent = navigator.userAgent;
54
- // Check if it's iOS or iPadOS
55
- // iPadOS 13+ may identify as Macintosh, check for touch support
56
- const isIOS = /iPhone|iPad|iPod/.test(userAgent);
57
- const isIPadOS = /Macintosh/.test(userAgent) && navigator.maxTouchPoints && navigator.maxTouchPoints > 1;
58
- if (!isIOS && !isIPadOS) {
59
- return null;
60
- }
61
- // Extract version number from various iOS user agent formats:
62
- // - "iPhone OS 15_0" or "iPhone OS 15_0_1"
63
- // - "CPU OS 15_0 like Mac OS X"
64
- // - "CPU iPhone OS 15_0 like Mac OS X"
65
- // - "Version/15.0" (for iPadOS)
66
- const patterns = [
67
- /(?:iPhone|CPU(?:\siPhone)?)\sOS\s(\d+)[\._](\d+)/, // iPhone OS, CPU OS, CPU iPhone OS
68
- /Version\/(\d+)\.(\d+)/ // Version/15.0 (iPadOS)
69
- ];
70
- for (const pattern of patterns) {
71
- const match = userAgent.match(pattern);
72
- if (match && match[1]) {
73
- return parseInt(match[1], 10);
74
- }
75
- }
76
- return null;
77
- };
78
- const fixIOSSystemFonts = (fontFamilies) => {
79
- const iosVersion = getIOSVersion();
80
- // On iOS 15.0 and 15.1, system fonts have rendering issues
81
- // Fixed in iOS 17+
82
- if (iosVersion !== null && iosVersion >= 15 && iosVersion < 17) {
83
- return fontFamilies.map((fontFamily) => iOSBrokenFonts.indexOf(fontFamily) !== -1
84
- ? `-apple-system, "Helvetica Neue", Arial, sans-serif`
85
- : fontFamily);
86
- }
87
- return fontFamilies;
88
- };
89
- const getTextStrokeLineJoin = () => {
90
- const currentWindow = typeof window !== 'undefined' ? window : undefined;
91
- return currentWindow?.chrome ? 'miter' : 'round';
92
- };
93
- /**
94
- * Text Renderer
95
- *
96
- * Specialized renderer for text content.
97
- * Extracted from CanvasRenderer to improve code organization and maintainability.
98
- */
99
- class TextRenderer {
100
- constructor(deps) {
101
- /**
102
- * Per-render glyph width cache. Keyed by glyph + font string,
103
- * avoids redundant `measureText` calls when letter-spacing is non-zero.
104
- * Cleared at the start of each renderTextNode call.
105
- */
106
- this.glyphWidthCache = null;
107
- this.ctx = deps.ctx;
108
- this.options = deps.options;
109
- this.decorationRenderer = new text_decoration_renderer_1.TextDecorationRenderer(deps.ctx);
110
- }
111
- /**
112
- * Returns the cached width for a glyph+font pair, measuring via
113
- * measureText if not yet cached. Only used when letterSpacing !== 0
114
- * (the `canRenderWholeText` fast path handles the zero-spacing case).
115
- */
116
- cachedMeasureText(glyph, fontString) {
117
- let cache = this.glyphWidthCache;
118
- if (!cache) {
119
- cache = new Map();
120
- this.glyphWidthCache = cache;
121
- }
122
- const key = glyph + fontString;
123
- let w = cache.get(key);
124
- if (w === undefined) {
125
- w = this.ctx.measureText(glyph).width;
126
- cache.set(key, w);
127
- }
128
- return w;
129
- }
130
- /**
131
- * Iterate grapheme clusters one-by-one, applying correct letter-spacing.
132
- *
133
- * Issue #73: When letter-spacing is non-zero, text must be rendered character by
134
- * character, adding `letterSpacing` to each character's advance width.
135
- *
136
- * In horizontal text every glyph keeps the alphabetic baseline, CJK included:
137
- * inline layout aligns a whole line on its alphabetic baseline regardless of
138
- * script, and `baseline` was measured for that. Drawing CJK graphemes at the
139
- * same y with `textBaseline = 'ideographic'` lifts them above the Latin text
140
- * next to them, and away from where the `letterSpacing === 0` whole-string
141
- * path draws them.
142
- *
143
- * The `renderFn` callback receives (letter, x, y) and performs the actual draw
144
- * call (fillText or strokeText), allowing fill and stroke paths to share one
145
- * implementation.
146
- */
147
- iterateLettersWithLetterSpacing(text, letterSpacing, baseline, writingMode, renderFn) {
148
- if ((0, writing_mode_1.isVerticalWritingMode)(writingMode)) {
149
- this.iterateVerticalGlyphs(text, letterSpacing, baseline, writingMode, renderFn);
150
- return;
151
- }
152
- const letters = (0, text_1.segmentGraphemes)(text.text);
153
- const y = text.bounds.top + baseline;
154
- const fontString = this.ctx.font;
155
- let left = text.bounds.left;
156
- for (const letter of letters) {
157
- renderFn(letter, left, y);
158
- left += this.cachedMeasureText(letter, fontString) + letterSpacing;
159
- }
160
- }
161
- iterateVerticalGlyphs(text, letterSpacing, baseline, writingMode, renderFn) {
162
- const letters = (0, text_1.segmentGraphemes)(text.text);
163
- const fontString = this.ctx.font;
164
- const rotationAngle = writingMode === 4 /* WRITING_MODE.SIDEWAYS_LR */ ? -Math.PI / 2 : Math.PI / 2;
165
- let top = text.bounds.top;
166
- for (let i = 0; i < letters.length;) {
167
- const letter = letters[i];
168
- const isSideways = (0, writing_mode_1.isSidewaysWritingMode)(writingMode) || (!(0, exports.hasCJKCharacters)(letter) && letter.trim().length > 0);
169
- if (!isSideways) {
170
- // CJK glyph in vertical writing mode — render upright
171
- const savedBaseline = this.ctx.textBaseline;
172
- if ((0, exports.hasCJKCharacters)(letter)) {
173
- this.ctx.textBaseline = 'ideographic';
174
- }
175
- renderFn(letter, text.bounds.left, top + baseline);
176
- this.ctx.textBaseline = savedBaseline;
177
- top += this.cachedMeasureText(letter, fontString) + letterSpacing;
178
- i++;
179
- continue;
180
- }
181
- // ── Batch consecutive sideways glyphs into one save/restore ──
182
- const runStart = i;
183
- while (i < letters.length) {
184
- const ch = letters[i];
185
- if (!(0, writing_mode_1.isSidewaysWritingMode)(writingMode) && !(0, exports.hasCJKCharacters)(ch) && ch.trim().length === 0)
186
- break;
187
- if ((0, writing_mode_1.isSidewaysWritingMode)(writingMode) || (!(0, exports.hasCJKCharacters)(ch) && ch.trim().length > 0)) {
188
- i++;
189
- }
190
- else {
191
- break;
192
- }
193
- }
194
- // Render entire sideways run in one save/restore
195
- this.ctx.save();
196
- this.ctx.translate(text.bounds.left + baseline, top);
197
- this.ctx.rotate(rotationAngle);
198
- let runOffset = 0;
199
- for (let j = runStart; j < i; j++) {
200
- renderFn(letters[j], 0, runOffset);
201
- runOffset += this.cachedMeasureText(letters[j], fontString) + letterSpacing;
202
- }
203
- this.ctx.restore();
204
- top += runOffset;
205
- }
206
- }
207
- /**
208
- * Render text with letter-spacing applied (fill pass).
209
- * When letterSpacing is 0 the whole string is drawn in one call; otherwise each
210
- * grapheme is drawn individually so spacing is applied correctly.
211
- */
212
- renderTextWithLetterSpacing(text, letterSpacing, baseline, writingMode = 0 /* WRITING_MODE.HORIZONTAL_TB */) {
213
- this.renderFillText(text, letterSpacing, baseline, writingMode);
214
- }
215
- canRenderWholeText(letterSpacing, writingMode) {
216
- return letterSpacing === 0 && !(0, writing_mode_1.isVerticalWritingMode)(writingMode);
217
- }
218
- renderFillText(text, letterSpacing, baseline, writingMode) {
219
- if (this.canRenderWholeText(letterSpacing, writingMode)) {
220
- this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + baseline);
221
- }
222
- else {
223
- this.iterateLettersWithLetterSpacing(text, letterSpacing, baseline, writingMode, (letter, x, y) => {
224
- this.ctx.fillText(letter, x, y);
225
- });
226
- }
227
- }
228
- renderStrokeText(text, letterSpacing, baseline, writingMode) {
229
- if (this.canRenderWholeText(letterSpacing, writingMode)) {
230
- this.ctx.strokeText(text.text, text.bounds.left, text.bounds.top + baseline);
231
- }
232
- else {
233
- this.iterateLettersWithLetterSpacing(text, letterSpacing, baseline, writingMode, (letter, x, y) => {
234
- this.ctx.strokeText(letter, x, y);
235
- });
236
- }
237
- }
238
- renderTextStrokeWithStyle(text, styles, baseline) {
239
- if (!styles.webkitTextStrokeWidth || !text.text.trim().length) {
240
- return;
241
- }
242
- this.ctx.strokeStyle = (0, color_utilities_1.asString)(styles.webkitTextStrokeColor);
243
- this.ctx.lineWidth = styles.webkitTextStrokeWidth;
244
- this.ctx.lineJoin = getTextStrokeLineJoin();
245
- this.renderStrokeText(text, styles.letterSpacing, baseline, styles.writingMode);
246
- this.ctx.strokeStyle = '';
247
- this.ctx.lineWidth = 0;
248
- this.ctx.lineJoin = 'miter';
249
- }
250
- renderTextFillWithShadows(text, styles, baseline) {
251
- this.ctx.fillStyle = (0, color_utilities_1.asString)(styles.color);
252
- this.renderTextWithLetterSpacing(text, styles.letterSpacing, baseline, styles.writingMode);
253
- const textShadows = styles.textShadow;
254
- if (textShadows.length && text.text.trim().length) {
255
- textShadows
256
- .slice(0)
257
- .reverse()
258
- .forEach((textShadow) => {
259
- this.ctx.shadowColor = (0, color_utilities_1.asString)(textShadow.color);
260
- this.ctx.shadowOffsetX = textShadow.offsetX.number * this.options.scale;
261
- this.ctx.shadowOffsetY = textShadow.offsetY.number * this.options.scale;
262
- this.ctx.shadowBlur = textShadow.blur.number;
263
- this.renderTextWithLetterSpacing(text, styles.letterSpacing, baseline, styles.writingMode);
264
- });
265
- this.ctx.shadowColor = '';
266
- this.ctx.shadowOffsetX = 0;
267
- this.ctx.shadowOffsetY = 0;
268
- this.ctx.shadowBlur = 0;
269
- }
270
- }
271
- /**
272
- * Helper method to render text with paint order support
273
- * Reduces code duplication in line-clamp and normal rendering
274
- */
275
- renderTextBoundWithPaintOrder(textBound, styles, paintOrderLayers, baseline) {
276
- paintOrderLayers.forEach((paintOrderLayer) => {
277
- switch (paintOrderLayer) {
278
- case 0 /* PAINT_ORDER_LAYER.FILL */:
279
- this.ctx.fillStyle = (0, color_utilities_1.asString)(styles.color);
280
- this.renderTextWithLetterSpacing(textBound, styles.letterSpacing, baseline, styles.writingMode);
281
- break;
282
- case 1 /* PAINT_ORDER_LAYER.STROKE */:
283
- this.renderTextStrokeWithStyle(textBound, styles, baseline);
284
- break;
285
- }
286
- });
287
- }
288
- renderTextDecoration(bounds, styles) {
289
- this.decorationRenderer.render(bounds, styles);
290
- }
291
- // Helper method to truncate text and add ellipsis if needed
292
- truncateTextWithEllipsis(text, maxWidth, letterSpacing) {
293
- // Use the Unicode ellipsis character (U+2026) whose width the browser measures
294
- // as a single glyph, matching native text-overflow behaviour more closely.
295
- const ellipsis = '\u2026';
296
- const ellipsisWidth = this.ctx.measureText(ellipsis).width;
297
- // Segment into grapheme clusters so multi-byte characters (emoji, composed
298
- // sequences) are never split mid-character.
299
- const graphemes = (0, text_1.segmentGraphemes)(text);
300
- if (letterSpacing === 0) {
301
- // Measure the whole candidate string for accuracy: the browser applies
302
- // kerning and ligatures when rendering multiple glyphs together, so
303
- // measuring them as one string is more precise than summing individual widths.
304
- // Binary search reduces measurements from O(n) to O(log n).
305
- // NOTE: can't use cachedMeasureText here — the binary search measures
306
- // concatenated substrings, and kerning/ligatures depend on the full string.
307
- const fits = (n) => this.ctx.measureText(graphemes.slice(0, n).join('')).width + ellipsisWidth <= maxWidth;
308
- let lo = 0;
309
- let hi = graphemes.length;
310
- while (lo < hi) {
311
- const mid = (lo + hi + 1) >> 1;
312
- if (fits(mid)) {
313
- lo = mid;
314
- }
315
- else {
316
- hi = mid - 1;
317
- }
318
- }
319
- return graphemes.slice(0, lo).join('') + ellipsis;
320
- }
321
- else {
322
- const fontString = this.ctx.font;
323
- let width = ellipsisWidth;
324
- const result = [];
325
- for (const letter of graphemes) {
326
- const glyphWidth = this.cachedMeasureText(letter, fontString);
327
- // Check against glyph width only (no trailing spacing): letter-spacing
328
- // is applied *between* characters, not after the final glyph. Using
329
- // `glyphWidth + letterSpacing` would incorrectly discard letters that
330
- // fit as the last character before the ellipsis.
331
- if (width + glyphWidth > maxWidth) {
332
- break;
333
- }
334
- result.push(letter);
335
- // Accumulate glyph + inter-character spacing for the *next* iteration.
336
- width += glyphWidth + letterSpacing;
337
- }
338
- return result.join('') + ellipsis;
339
- }
340
- }
341
- /**
342
- * Create font style array
343
- * Public method used by list rendering
344
- */
345
- createFontStyle(styles) {
346
- const fontVariant = styles.fontVariant
347
- .filter((variant) => variant === 'normal' || variant === 'small-caps')
348
- .join('');
349
- const fontFamily = fixIOSSystemFonts(styles.fontFamily).join(', ');
350
- const fontSize = (0, parser_1.isDimensionToken)(styles.fontSize)
351
- ? `${styles.fontSize.number}${styles.fontSize.unit}`
352
- : `${styles.fontSize.number}px`;
353
- return [
354
- [styles.fontStyle, fontVariant, styles.fontWeight, fontSize, fontFamily].join(' '),
355
- fontFamily,
356
- fontSize
357
- ];
358
- }
359
- /**
360
- * Render text with -webkit-line-clamp truncation.
361
- * Groups text bounds by their Y position into visual lines, then renders
362
- * only the first N-1 complete lines followed by an ellipsis on the Nth line.
363
- */
364
- renderLineClampedText(text, styles, paintOrder, baseline, containerBounds) {
365
- const lineHeight = styles.fontSize.number * 1.5;
366
- const lines = [];
367
- let currentLine = [];
368
- let currentLineTop = text.textBounds[0].bounds.top;
369
- text.textBounds.forEach((tb) => {
370
- if (Math.abs(tb.bounds.top - currentLineTop) >= lineHeight * 0.5) {
371
- if (currentLine.length > 0)
372
- lines.push(currentLine);
373
- currentLine = [tb];
374
- currentLineTop = tb.bounds.top;
375
- }
376
- else {
377
- currentLine.push(tb);
378
- }
379
- });
380
- if (currentLine.length > 0)
381
- lines.push(currentLine);
382
- const maxLines = styles.webkitLineClamp;
383
- if (lines.length <= maxLines)
384
- return false; // fall through to normal rendering
385
- // Render full lines (0..N-2)
386
- for (let i = 0; i < maxLines - 1; i++) {
387
- lines[i].forEach((tb) => this.renderTextBoundWithPaintOrder(tb, styles, paintOrder, baseline));
388
- }
389
- // Nth line: truncated with ellipsis
390
- const lastLine = lines[maxLines - 1];
391
- if (lastLine?.length && containerBounds) {
392
- const textStr = lastLine.map((tb) => tb.text).join('');
393
- const first = lastLine[0];
394
- const avail = containerBounds.width - (first.bounds.left - containerBounds.left);
395
- const truncated = this.truncateTextWithEllipsis(textStr, avail, styles.letterSpacing);
396
- const bounds = new text_1.TextBounds(truncated, first.bounds);
397
- for (const layer of paintOrder) {
398
- if (layer === 0 /* PAINT_ORDER_LAYER.FILL */) {
399
- this.ctx.fillStyle = (0, color_utilities_1.asString)(styles.color);
400
- this.renderTextWithLetterSpacing(bounds, styles.letterSpacing, baseline, styles.writingMode);
401
- }
402
- else if (layer === 1 /* PAINT_ORDER_LAYER.STROKE */) {
403
- this.renderTextStrokeWithStyle(bounds, styles, baseline);
404
- }
405
- }
406
- }
407
- return true;
408
- }
409
- /**
410
- * Render single-line text with text-overflow: ellipsis.
411
- * Returns true if ellipsis was applied (caller should skip normal rendering).
412
- */
413
- renderEllipsisText(text, styles, paintOrder, baseline, containerBounds) {
414
- const lineHeight = styles.fontSize.number * 1.5;
415
- const firstTop = text.textBounds[0].bounds.top;
416
- const isSingleLine = text.textBounds.every((tb) => Math.abs(tb.bounds.top - firstTop) < lineHeight * 0.5);
417
- if (!isSingleLine)
418
- return false;
419
- let fullText = text.textBounds
420
- .map((tb) => tb.text)
421
- .join('')
422
- .replace(/\s+/g, ' ')
423
- .trim();
424
- const fullWidth = this.ctx.measureText(fullText).width;
425
- // Small tolerance: canvas measurement can differ from the browser's text
426
- // layout by a fraction of a pixel (kerning/font rounding), so text that
427
- // exactly fits the container must not be falsely truncated (issue #226).
428
- if (fullWidth <= containerBounds.width + TEXT_OVERFLOW_EPSILON)
429
- return false;
430
- const truncated = this.truncateTextWithEllipsis(fullText, containerBounds.width, styles.letterSpacing);
431
- const bounds = new text_1.TextBounds(truncated, text.textBounds[0].bounds);
432
- for (const layer of paintOrder) {
433
- if (layer === 0 /* PAINT_ORDER_LAYER.FILL */)
434
- this.renderTextFillWithShadows(bounds, styles, baseline);
435
- else if (layer === 1 /* PAINT_ORDER_LAYER.STROKE */)
436
- this.renderTextStrokeWithStyle(bounds, styles, baseline);
437
- }
438
- return true;
439
- }
440
- async renderTextNode(text, styles, containerBounds) {
441
- // Reset glyph width cache at the start of each text node render —
442
- // the font may change between nodes.
443
- this.glyphWidthCache = null;
444
- const [fontString] = this.createFontStyle(styles);
445
- this.ctx.font = fontString;
446
- this.ctx.direction = styles.direction === 1 /* DIRECTION.RTL */ ? 'rtl' : 'ltr';
447
- this.ctx.textAlign = 'left';
448
- this.ctx.textBaseline = 'alphabetic';
449
- const paintOrder = styles.paintOrder;
450
- // Compute baseline using the actual rendered font via Canvas API.
451
- // This ensures correct positioning whether using webfonts (which may not
452
- // be loaded in the original document but are active in the Canvas) or
453
- // system fonts.
454
- const baseline = (0, font_utils_1.measureBaseline)(this.ctx, styles.fontSize.number);
455
- // -webkit-line-clamp
456
- const clamp = styles.webkitLineClamp > 0 &&
457
- (styles.display & 2 /* DISPLAY.BLOCK */) !== 0 &&
458
- styles.overflowY === 1 /* OVERFLOW.HIDDEN */ &&
459
- text.textBounds.length > 0;
460
- if (clamp) {
461
- if (this.renderLineClampedText(text, styles, paintOrder, baseline, containerBounds))
462
- return;
463
- }
464
- // text-overflow: ellipsis (single-line only)
465
- const ellipsis = styles.textOverflow === 1 /* TEXT_OVERFLOW.ELLIPSIS */ &&
466
- containerBounds &&
467
- styles.overflowX === 1 /* OVERFLOW.HIDDEN */ &&
468
- text.textBounds.length > 0;
469
- if (ellipsis && this.renderEllipsisText(text, styles, paintOrder, baseline, containerBounds))
470
- return;
471
- // Normal rendering: fill + stroke + decorations per text bound
472
- text.textBounds.forEach((tb) => {
473
- paintOrder.forEach((layer) => {
474
- if (layer === 0 /* PAINT_ORDER_LAYER.FILL */) {
475
- this.renderTextFillWithShadows(tb, styles, baseline);
476
- if (styles.textDecorationLine.length)
477
- this.renderTextDecoration(tb.bounds, styles);
478
- }
479
- else if (layer === 1 /* PAINT_ORDER_LAYER.STROKE */) {
480
- this.renderTextStrokeWithStyle(tb, styles, baseline);
481
- }
482
- });
483
- });
484
- }
485
- }
486
- exports.TextRenderer = TextRenderer;