html2canvas-pro 2.0.3 → 2.1.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 (631) hide show
  1. package/README.md +8 -6
  2. package/dist/html2canvas-pro.esm.js +6754 -6599
  3. package/dist/html2canvas-pro.esm.js.map +1 -1
  4. package/dist/html2canvas-pro.js +6753 -6599
  5. package/dist/html2canvas-pro.js.map +1 -1
  6. package/dist/html2canvas-pro.min.js +5 -5
  7. package/dist/lib/config.js +3 -4
  8. package/dist/lib/core/bitwise.js +0 -1
  9. package/dist/lib/core/cache-storage.js +34 -48
  10. package/dist/lib/core/context.js +0 -1
  11. package/dist/lib/core/debugger.js +0 -1
  12. package/dist/lib/core/features.js +0 -1
  13. package/dist/lib/core/logger.js +0 -1
  14. package/dist/lib/core/origin-checker.js +0 -1
  15. package/dist/lib/core/performance-monitor.js +1 -24
  16. package/dist/lib/core/render-element.js +210 -0
  17. package/dist/lib/core/util.js +0 -1
  18. package/dist/lib/core/validator.js +0 -1
  19. package/dist/lib/css/index.js +150 -89
  20. package/dist/lib/css/layout/bounds.js +0 -1
  21. package/dist/lib/css/layout/text.js +4 -1
  22. package/dist/lib/css/{ITypeDescriptor.js → property-descriptor.js} +0 -1
  23. package/dist/lib/css/property-descriptors/background-clip.js +0 -1
  24. package/dist/lib/css/property-descriptors/background-color.js +0 -1
  25. package/dist/lib/css/property-descriptors/background-image.js +0 -1
  26. package/dist/lib/css/property-descriptors/background-origin.js +0 -1
  27. package/dist/lib/css/property-descriptors/background-position.js +0 -1
  28. package/dist/lib/css/property-descriptors/background-repeat.js +0 -1
  29. package/dist/lib/css/property-descriptors/background-size.js +0 -1
  30. package/dist/lib/css/property-descriptors/border-color.js +0 -1
  31. package/dist/lib/css/property-descriptors/border-radius.js +0 -1
  32. package/dist/lib/css/property-descriptors/border-style.js +0 -1
  33. package/dist/lib/css/property-descriptors/border-width.js +0 -1
  34. package/dist/lib/css/property-descriptors/box-shadow.js +0 -1
  35. package/dist/lib/css/property-descriptors/clip-path.js +0 -1
  36. package/dist/lib/css/property-descriptors/color.js +0 -1
  37. package/dist/lib/css/property-descriptors/content.js +0 -1
  38. package/dist/lib/css/property-descriptors/counter-increment.js +0 -1
  39. package/dist/lib/css/property-descriptors/counter-reset.js +0 -1
  40. package/dist/lib/css/property-descriptors/direction.js +0 -1
  41. package/dist/lib/css/property-descriptors/display.js +0 -1
  42. package/dist/lib/css/property-descriptors/duration.js +0 -1
  43. package/dist/lib/css/property-descriptors/float.js +0 -1
  44. package/dist/lib/css/property-descriptors/font-family.js +0 -1
  45. package/dist/lib/css/property-descriptors/font-size.js +0 -1
  46. package/dist/lib/css/property-descriptors/font-style.js +0 -1
  47. package/dist/lib/css/property-descriptors/font-variant.js +0 -1
  48. package/dist/lib/css/property-descriptors/font-weight.js +0 -1
  49. package/dist/lib/css/property-descriptors/image-rendering.js +0 -1
  50. package/dist/lib/css/property-descriptors/letter-spacing.js +0 -1
  51. package/dist/lib/css/property-descriptors/line-break.js +0 -1
  52. package/dist/lib/css/property-descriptors/line-height.js +0 -1
  53. package/dist/lib/css/property-descriptors/list-style-image.js +0 -1
  54. package/dist/lib/css/property-descriptors/list-style-position.js +0 -1
  55. package/dist/lib/css/property-descriptors/list-style-type.js +0 -1
  56. package/dist/lib/css/property-descriptors/margin.js +0 -1
  57. package/dist/lib/css/property-descriptors/mix-blend-mode.js +31 -0
  58. package/dist/lib/css/property-descriptors/object-fit.js +0 -1
  59. package/dist/lib/css/property-descriptors/opacity.js +0 -1
  60. package/dist/lib/css/property-descriptors/overflow-wrap.js +0 -1
  61. package/dist/lib/css/property-descriptors/overflow.js +0 -1
  62. package/dist/lib/css/property-descriptors/padding.js +0 -1
  63. package/dist/lib/css/property-descriptors/paint-order.js +0 -1
  64. package/dist/lib/css/property-descriptors/position.js +0 -1
  65. package/dist/lib/css/property-descriptors/quotes.js +0 -1
  66. package/dist/lib/css/property-descriptors/rotate.js +0 -1
  67. package/dist/lib/css/property-descriptors/text-align.js +0 -1
  68. package/dist/lib/css/property-descriptors/text-decoration-color.js +0 -1
  69. package/dist/lib/css/property-descriptors/text-decoration-line.js +0 -1
  70. package/dist/lib/css/property-descriptors/text-decoration-style.js +0 -1
  71. package/dist/lib/css/property-descriptors/text-decoration-thickness.js +0 -1
  72. package/dist/lib/css/property-descriptors/text-overflow.js +0 -1
  73. package/dist/lib/css/property-descriptors/text-shadow.js +0 -1
  74. package/dist/lib/css/property-descriptors/text-transform.js +0 -1
  75. package/dist/lib/css/property-descriptors/text-underline-offset.js +0 -1
  76. package/dist/lib/css/property-descriptors/transform-origin.js +0 -1
  77. package/dist/lib/css/property-descriptors/transform.js +0 -1
  78. package/dist/lib/css/property-descriptors/visibility.js +0 -1
  79. package/dist/lib/css/property-descriptors/webkit-line-clamp.js +0 -1
  80. package/dist/lib/css/property-descriptors/webkit-text-stroke-color.js +0 -1
  81. package/dist/lib/css/property-descriptors/webkit-text-stroke-width.js +0 -1
  82. package/dist/lib/css/property-descriptors/word-break.js +0 -1
  83. package/dist/lib/css/property-descriptors/writing-mode.js +28 -0
  84. package/dist/lib/css/property-descriptors/z-index.js +0 -1
  85. package/dist/lib/css/syntax/parser.js +0 -1
  86. package/dist/lib/css/syntax/tokenizer.js +14 -1
  87. package/dist/lib/css/{IPropertyDescriptor.js → type-descriptor.js} +0 -1
  88. package/dist/lib/css/types/angle.js +0 -1
  89. package/dist/lib/css/types/color-math.js +0 -1
  90. package/dist/lib/css/types/color-spaces/a98.js +0 -1
  91. package/dist/lib/css/types/color-spaces/p3.js +0 -1
  92. package/dist/lib/css/types/color-spaces/pro-photo.js +0 -1
  93. package/dist/lib/css/types/color-spaces/rec2020.js +0 -1
  94. package/dist/lib/css/types/color-spaces/srgb.js +0 -1
  95. package/dist/lib/css/types/color-utilities.js +0 -1
  96. package/dist/lib/css/types/color.js +0 -1
  97. package/dist/lib/css/types/functions/-prefix-linear-gradient.js +0 -1
  98. package/dist/lib/css/types/functions/-prefix-radial-gradient.js +0 -1
  99. package/dist/lib/css/types/functions/-webkit-gradient.js +0 -1
  100. package/dist/lib/css/types/functions/counter.js +0 -1
  101. package/dist/lib/css/types/functions/gradient.js +0 -1
  102. package/dist/lib/css/types/functions/linear-gradient.js +0 -1
  103. package/dist/lib/css/types/functions/radial-gradient.js +0 -1
  104. package/dist/lib/css/types/image.js +0 -1
  105. package/dist/lib/css/types/index.js +0 -1
  106. package/dist/lib/css/types/length-percentage.js +0 -1
  107. package/dist/lib/css/types/length.js +0 -1
  108. package/dist/lib/css/types/time.js +0 -1
  109. package/dist/lib/dom/document-cloner.js +27 -22
  110. package/dist/lib/dom/dom-normalizer.js +0 -1
  111. package/dist/lib/dom/element-container.js +7 -6
  112. package/dist/lib/dom/elements/li-element-container.js +0 -1
  113. package/dist/lib/dom/elements/ol-element-container.js +0 -1
  114. package/dist/lib/dom/elements/select-element-container.js +0 -1
  115. package/dist/lib/dom/elements/textarea-element-container.js +0 -1
  116. package/dist/lib/dom/node-parser.js +37 -53
  117. package/dist/lib/dom/node-type-guards.js +62 -25
  118. package/dist/lib/dom/replaced-elements/canvas-element-container.js +0 -1
  119. package/dist/lib/dom/replaced-elements/iframe-element-container.js +0 -1
  120. package/dist/lib/dom/replaced-elements/image-element-container.js +0 -1
  121. package/dist/lib/dom/replaced-elements/index.js +0 -1
  122. package/dist/lib/dom/replaced-elements/input-element-container.js +0 -1
  123. package/dist/lib/dom/replaced-elements/pseudo-elements.js +0 -1
  124. package/dist/lib/dom/replaced-elements/svg-element-container.js +0 -1
  125. package/dist/lib/dom/text-container.js +5 -1
  126. package/dist/lib/index.js +32 -210
  127. package/dist/lib/invariant.js +0 -1
  128. package/dist/lib/options.js +2 -0
  129. package/dist/lib/render/background.js +0 -1
  130. package/dist/lib/render/bezier-curve.js +0 -1
  131. package/dist/lib/render/border.js +0 -1
  132. package/dist/lib/render/bound-curves.js +0 -1
  133. package/dist/lib/render/box-sizing.js +0 -1
  134. package/dist/lib/render/canvas/background-renderer.js +2 -24
  135. package/dist/lib/render/canvas/border-renderer.js +0 -1
  136. package/dist/lib/render/canvas/canvas-path.js +25 -0
  137. package/dist/lib/render/canvas/canvas-renderer.js +25 -98
  138. package/dist/lib/render/canvas/effects-renderer.js +3 -1
  139. package/dist/lib/render/canvas/foreignobject-renderer.js +8 -16
  140. package/dist/lib/render/canvas/text-renderer.js +92 -121
  141. package/dist/lib/render/effects.js +11 -2
  142. package/dist/lib/render/font-metrics.js +0 -1
  143. package/dist/lib/render/object-fit.js +87 -0
  144. package/dist/lib/render/path.js +0 -1
  145. package/dist/lib/render/renderer-interface.js +0 -1
  146. package/dist/lib/render/stacking-context.js +16 -8
  147. package/dist/lib/render/vector.js +0 -1
  148. package/dist/types/core/cache-storage.d.ts +5 -6
  149. package/dist/types/core/performance-monitor.d.ts +0 -11
  150. package/dist/types/core/render-element.d.ts +3 -0
  151. package/dist/types/css/index.d.ts +5 -0
  152. package/dist/types/css/property-descriptors/background-clip.d.ts +1 -1
  153. package/dist/types/css/property-descriptors/background-color.d.ts +1 -1
  154. package/dist/types/css/property-descriptors/background-image.d.ts +1 -1
  155. package/dist/types/css/property-descriptors/background-origin.d.ts +1 -1
  156. package/dist/types/css/property-descriptors/background-position.d.ts +1 -1
  157. package/dist/types/css/property-descriptors/background-repeat.d.ts +1 -1
  158. package/dist/types/css/property-descriptors/background-size.d.ts +1 -1
  159. package/dist/types/css/property-descriptors/border-color.d.ts +1 -1
  160. package/dist/types/css/property-descriptors/border-radius.d.ts +1 -1
  161. package/dist/types/css/property-descriptors/border-style.d.ts +1 -1
  162. package/dist/types/css/property-descriptors/border-width.d.ts +1 -1
  163. package/dist/types/css/property-descriptors/box-shadow.d.ts +1 -1
  164. package/dist/types/css/property-descriptors/clip-path.d.ts +1 -1
  165. package/dist/types/css/property-descriptors/color.d.ts +1 -1
  166. package/dist/types/css/property-descriptors/content.d.ts +1 -1
  167. package/dist/types/css/property-descriptors/counter-increment.d.ts +1 -1
  168. package/dist/types/css/property-descriptors/counter-reset.d.ts +1 -1
  169. package/dist/types/css/property-descriptors/direction.d.ts +1 -1
  170. package/dist/types/css/property-descriptors/display.d.ts +1 -1
  171. package/dist/types/css/property-descriptors/duration.d.ts +1 -1
  172. package/dist/types/css/property-descriptors/float.d.ts +1 -1
  173. package/dist/types/css/property-descriptors/font-family.d.ts +1 -1
  174. package/dist/types/css/property-descriptors/font-size.d.ts +1 -1
  175. package/dist/types/css/property-descriptors/font-style.d.ts +1 -1
  176. package/dist/types/css/property-descriptors/font-variant.d.ts +1 -1
  177. package/dist/types/css/property-descriptors/font-weight.d.ts +1 -1
  178. package/dist/types/css/property-descriptors/image-rendering.d.ts +1 -1
  179. package/dist/types/css/property-descriptors/letter-spacing.d.ts +1 -1
  180. package/dist/types/css/property-descriptors/line-break.d.ts +1 -1
  181. package/dist/types/css/property-descriptors/line-height.d.ts +1 -1
  182. package/dist/types/css/property-descriptors/list-style-image.d.ts +1 -1
  183. package/dist/types/css/property-descriptors/list-style-position.d.ts +1 -1
  184. package/dist/types/css/property-descriptors/list-style-type.d.ts +1 -1
  185. package/dist/types/css/property-descriptors/margin.d.ts +1 -1
  186. package/dist/types/css/property-descriptors/mix-blend-mode.d.ts +21 -0
  187. package/dist/types/css/property-descriptors/object-fit.d.ts +1 -1
  188. package/dist/types/css/property-descriptors/opacity.d.ts +1 -1
  189. package/dist/types/css/property-descriptors/overflow-wrap.d.ts +1 -1
  190. package/dist/types/css/property-descriptors/overflow.d.ts +1 -1
  191. package/dist/types/css/property-descriptors/padding.d.ts +1 -1
  192. package/dist/types/css/property-descriptors/paint-order.d.ts +1 -1
  193. package/dist/types/css/property-descriptors/position.d.ts +1 -1
  194. package/dist/types/css/property-descriptors/quotes.d.ts +1 -1
  195. package/dist/types/css/property-descriptors/rotate.d.ts +1 -1
  196. package/dist/types/css/property-descriptors/text-align.d.ts +1 -1
  197. package/dist/types/css/property-descriptors/text-decoration-color.d.ts +1 -1
  198. package/dist/types/css/property-descriptors/text-decoration-line.d.ts +1 -1
  199. package/dist/types/css/property-descriptors/text-decoration-style.d.ts +1 -1
  200. package/dist/types/css/property-descriptors/text-decoration-thickness.d.ts +1 -1
  201. package/dist/types/css/property-descriptors/text-overflow.d.ts +1 -1
  202. package/dist/types/css/property-descriptors/text-shadow.d.ts +1 -1
  203. package/dist/types/css/property-descriptors/text-transform.d.ts +1 -1
  204. package/dist/types/css/property-descriptors/text-underline-offset.d.ts +1 -1
  205. package/dist/types/css/property-descriptors/transform-origin.d.ts +1 -1
  206. package/dist/types/css/property-descriptors/transform.d.ts +1 -1
  207. package/dist/types/css/property-descriptors/visibility.d.ts +1 -1
  208. package/dist/types/css/property-descriptors/webkit-line-clamp.d.ts +1 -1
  209. package/dist/types/css/property-descriptors/webkit-text-stroke-color.d.ts +1 -1
  210. package/dist/types/css/property-descriptors/webkit-text-stroke-width.d.ts +1 -1
  211. package/dist/types/css/property-descriptors/word-break.d.ts +1 -1
  212. package/dist/types/css/property-descriptors/writing-mode.d.ts +11 -0
  213. package/dist/types/css/property-descriptors/z-index.d.ts +1 -1
  214. package/dist/types/css/syntax/tokenizer.d.ts +5 -0
  215. package/dist/types/css/types/angle.d.ts +1 -1
  216. package/dist/types/css/types/color.d.ts +1 -1
  217. package/dist/types/css/types/image.d.ts +1 -1
  218. package/dist/types/css/types/time.d.ts +1 -1
  219. package/dist/types/dom/element-container.d.ts +4 -11
  220. package/dist/types/dom/node-parser.d.ts +2 -15
  221. package/dist/types/dom/node-type-guards.d.ts +21 -22
  222. package/dist/types/index.d.ts +28 -48
  223. package/dist/types/options.d.ts +51 -0
  224. package/dist/types/render/canvas/background-renderer.d.ts +0 -6
  225. package/dist/types/render/canvas/canvas-path.d.ts +3 -0
  226. package/dist/types/render/canvas/canvas-renderer.d.ts +4 -2
  227. package/dist/types/render/canvas/foreignobject-renderer.d.ts +2 -3
  228. package/dist/types/render/canvas/text-renderer.d.ts +8 -1
  229. package/dist/types/render/effects.d.ts +9 -1
  230. package/dist/types/render/object-fit.d.ts +18 -0
  231. package/package.json +43 -35
  232. package/src/__tests__/index.ts +99 -0
  233. package/src/config.ts +107 -0
  234. package/src/core/__mocks__/cache-storage.ts +1 -0
  235. package/src/core/__mocks__/context.ts +19 -0
  236. package/{dist/lib/core/__mocks__/features.js → src/core/__mocks__/features.ts} +1 -5
  237. package/src/core/__mocks__/logger.ts +17 -0
  238. package/{dist/lib/core/__tests__/cache-storage.test.js → src/core/__tests__/cache-storage.test.ts} +95 -48
  239. package/{dist/lib/core/__tests__/cache-storage.js → src/core/__tests__/cache-storage.ts} +117 -64
  240. package/{dist/lib/core/__tests__/logger.js → src/core/__tests__/logger.ts} +10 -8
  241. package/{dist/lib/core/__tests__/validator.js → src/core/__tests__/validator.ts} +161 -98
  242. package/src/core/bitwise.ts +1 -0
  243. package/src/core/cache-storage.ts +315 -0
  244. package/src/core/context.ts +31 -0
  245. package/src/core/debugger.ts +32 -0
  246. package/src/core/features.ts +222 -0
  247. package/src/core/logger.ts +64 -0
  248. package/src/core/origin-checker.ts +57 -0
  249. package/src/core/performance-monitor.ts +241 -0
  250. package/src/core/render-element.ts +272 -0
  251. package/src/core/util.ts +1 -0
  252. package/src/core/validator.ts +593 -0
  253. package/src/css/index.ts +427 -0
  254. package/src/css/layout/__mocks__/bounds.ts +6 -0
  255. package/src/css/layout/bounds.ts +79 -0
  256. package/src/css/layout/text.ts +161 -0
  257. package/src/css/property-descriptor.ts +49 -0
  258. package/src/css/property-descriptors/__tests__/background-tests.ts +65 -0
  259. package/src/css/property-descriptors/__tests__/clip-path.test.ts +280 -0
  260. package/src/css/property-descriptors/__tests__/font-family.ts +25 -0
  261. package/src/css/property-descriptors/__tests__/image-rendering-integration.test.ts +153 -0
  262. package/{dist/lib/css/property-descriptors/__tests__/image-rendering-performance.test.js → src/css/property-descriptors/__tests__/image-rendering-performance.test.ts} +74 -66
  263. package/src/css/property-descriptors/__tests__/image-rendering.test.ts +72 -0
  264. package/src/css/property-descriptors/__tests__/paint-order.ts +87 -0
  265. package/src/css/property-descriptors/__tests__/text-shadow.ts +94 -0
  266. package/src/css/property-descriptors/__tests__/transform-tests.ts +18 -0
  267. package/src/css/property-descriptors/background-clip.ts +30 -0
  268. package/src/css/property-descriptors/background-color.ts +9 -0
  269. package/src/css/property-descriptors/background-image.ts +27 -0
  270. package/src/css/property-descriptors/background-origin.ts +31 -0
  271. package/src/css/property-descriptors/background-position.ts +38 -0
  272. package/src/css/property-descriptors/background-repeat.ts +44 -0
  273. package/src/css/property-descriptors/background-size.ts +27 -0
  274. package/src/css/property-descriptors/border-color.ts +13 -0
  275. package/src/css/property-descriptors/border-radius.ts +19 -0
  276. package/src/css/property-descriptors/border-style.ts +34 -0
  277. package/src/css/property-descriptors/border-width.ts +20 -0
  278. package/src/css/property-descriptors/box-shadow.ts +60 -0
  279. package/src/css/property-descriptors/clip-path.ts +271 -0
  280. package/src/css/property-descriptors/color.ts +9 -0
  281. package/src/css/property-descriptors/content.ts +26 -0
  282. package/src/css/property-descriptors/counter-increment.ts +43 -0
  283. package/src/css/property-descriptors/counter-reset.ts +36 -0
  284. package/src/css/property-descriptors/direction.ts +23 -0
  285. package/src/css/property-descriptors/display.ts +117 -0
  286. package/src/css/property-descriptors/duration.ts +14 -0
  287. package/src/css/property-descriptors/float.ts +29 -0
  288. package/src/css/property-descriptors/font-family.ts +38 -0
  289. package/src/css/property-descriptors/font-size.ts +9 -0
  290. package/src/css/property-descriptors/font-style.ts +25 -0
  291. package/src/css/property-descriptors/font-variant.ts +12 -0
  292. package/src/css/property-descriptors/font-weight.ts +26 -0
  293. package/src/css/property-descriptors/image-rendering.ts +33 -0
  294. package/src/css/property-descriptors/letter-spacing.ts +25 -0
  295. package/src/css/property-descriptors/line-break.ts +22 -0
  296. package/src/css/property-descriptors/line-height.ts +22 -0
  297. package/src/css/property-descriptors/list-style-image.ts +19 -0
  298. package/src/css/property-descriptors/list-style-position.ts +22 -0
  299. package/src/css/property-descriptors/list-style-type.ts +179 -0
  300. package/src/css/property-descriptors/margin.ts +13 -0
  301. package/src/css/property-descriptors/mix-blend-mode.ts +35 -0
  302. package/src/css/property-descriptors/object-fit.ts +39 -0
  303. package/src/css/property-descriptors/opacity.ts +15 -0
  304. package/src/css/property-descriptors/overflow-wrap.ts +22 -0
  305. package/src/css/property-descriptors/overflow.ts +34 -0
  306. package/src/css/property-descriptors/padding.ts +14 -0
  307. package/src/css/property-descriptors/paint-order.ts +42 -0
  308. package/src/css/property-descriptors/position.ts +30 -0
  309. package/src/css/property-descriptors/quotes.ts +57 -0
  310. package/src/css/property-descriptors/rotate.ts +34 -0
  311. package/src/css/property-descriptors/text-align.ts +26 -0
  312. package/src/css/property-descriptors/text-decoration-color.ts +9 -0
  313. package/src/css/property-descriptors/text-decoration-line.ts +38 -0
  314. package/src/css/property-descriptors/text-decoration-style.ts +32 -0
  315. package/src/css/property-descriptors/text-decoration-thickness.ts +30 -0
  316. package/src/css/property-descriptors/text-overflow.ts +23 -0
  317. package/src/css/property-descriptors/text-shadow.ts +52 -0
  318. package/src/css/property-descriptors/text-transform.ts +27 -0
  319. package/src/css/property-descriptors/text-underline-offset.ts +27 -0
  320. package/src/css/property-descriptors/transform-origin.ts +29 -0
  321. package/src/css/property-descriptors/transform.ts +74 -0
  322. package/src/css/property-descriptors/visibility.ts +25 -0
  323. package/src/css/property-descriptors/webkit-line-clamp.ts +30 -0
  324. package/src/css/property-descriptors/webkit-text-stroke-color.ts +8 -0
  325. package/src/css/property-descriptors/webkit-text-stroke-width.ts +15 -0
  326. package/src/css/property-descriptors/word-break.ts +25 -0
  327. package/src/css/property-descriptors/writing-mode.ts +37 -0
  328. package/src/css/property-descriptors/z-index.ts +27 -0
  329. package/src/css/syntax/__tests__/tokernizer-tests.ts +29 -0
  330. package/src/css/syntax/parser.ts +188 -0
  331. package/src/css/syntax/tokenizer.ts +822 -0
  332. package/src/css/type-descriptor.ts +7 -0
  333. package/src/css/types/__tests__/color-tests.ts +147 -0
  334. package/src/css/types/__tests__/image-tests.ts +239 -0
  335. package/src/css/types/angle.ts +86 -0
  336. package/src/css/types/color-math.ts +22 -0
  337. package/src/css/types/color-spaces/a98.ts +86 -0
  338. package/src/css/types/color-spaces/p3.ts +92 -0
  339. package/src/css/types/color-spaces/pro-photo.ts +87 -0
  340. package/src/css/types/color-spaces/rec2020.ts +90 -0
  341. package/src/css/types/color-spaces/srgb.ts +87 -0
  342. package/src/css/types/color-utilities.ts +452 -0
  343. package/src/css/types/color.ts +485 -0
  344. package/src/css/types/functions/-prefix-linear-gradient.ts +35 -0
  345. package/src/css/types/functions/-prefix-radial-gradient.ts +106 -0
  346. package/src/css/types/functions/-webkit-gradient.ts +69 -0
  347. package/src/css/types/functions/__tests__/radial-gradient.ts +69 -0
  348. package/src/css/types/functions/counter.ts +511 -0
  349. package/src/css/types/functions/gradient.ts +206 -0
  350. package/src/css/types/functions/linear-gradient.ts +28 -0
  351. package/src/css/types/functions/radial-gradient.ts +101 -0
  352. package/src/css/types/image.ts +120 -0
  353. package/src/css/types/index.ts +1 -0
  354. package/src/css/types/length-percentage.ts +137 -0
  355. package/src/css/types/length.ts +7 -0
  356. package/src/css/types/time.ts +20 -0
  357. package/src/dom/__mocks__/document-cloner.ts +22 -0
  358. package/{dist/lib/dom/__tests__/dom-normalizer.test.js → src/dom/__tests__/dom-normalizer.test.ts} +64 -44
  359. package/src/dom/__tests__/element-container.test.ts +129 -0
  360. package/src/dom/document-cloner.ts +929 -0
  361. package/src/dom/dom-normalizer.ts +133 -0
  362. package/src/dom/element-container.ts +75 -0
  363. package/src/dom/elements/li-element-container.ts +10 -0
  364. package/src/dom/elements/ol-element-container.ts +12 -0
  365. package/src/dom/elements/select-element-container.ts +10 -0
  366. package/src/dom/elements/textarea-element-container.ts +9 -0
  367. package/src/dom/node-parser.ts +177 -0
  368. package/src/dom/node-type-guards.ts +70 -0
  369. package/src/dom/replaced-elements/canvas-element-container.ts +15 -0
  370. package/src/dom/replaced-elements/iframe-element-container.ts +55 -0
  371. package/src/dom/replaced-elements/image-element-container.ts +16 -0
  372. package/src/dom/replaced-elements/index.ts +5 -0
  373. package/src/dom/replaced-elements/input-element-container.ts +105 -0
  374. package/src/dom/replaced-elements/pseudo-elements.ts +0 -0
  375. package/src/dom/replaced-elements/svg-element-container.ts +23 -0
  376. package/src/dom/text-container.ts +42 -0
  377. package/src/global.d.ts +19 -0
  378. package/src/index.ts +82 -0
  379. package/src/invariant.ts +5 -0
  380. package/src/options.ts +55 -0
  381. package/src/render/__tests__/object-fit.test.ts +85 -0
  382. package/src/render/background.ts +298 -0
  383. package/src/render/bezier-curve.ts +47 -0
  384. package/src/render/border.ts +165 -0
  385. package/src/render/bound-curves.ts +388 -0
  386. package/src/render/box-sizing.ts +31 -0
  387. package/{dist/lib/render/canvas/__tests__/background-renderer.test.js → src/render/canvas/__tests__/background-renderer.test.ts} +32 -25
  388. package/src/render/canvas/__tests__/border-renderer.test.ts +24 -0
  389. package/src/render/canvas/__tests__/effects-renderer.test.ts +32 -0
  390. package/src/render/canvas/__tests__/text-renderer.test.ts +471 -0
  391. package/src/render/canvas/background-renderer.ts +271 -0
  392. package/src/render/canvas/border-renderer.ts +224 -0
  393. package/src/render/canvas/canvas-path.ts +31 -0
  394. package/src/render/canvas/canvas-renderer.ts +641 -0
  395. package/src/render/canvas/effects-renderer.ts +130 -0
  396. package/src/render/canvas/foreignobject-renderer.ts +53 -0
  397. package/src/render/canvas/text-renderer.ts +700 -0
  398. package/src/render/effects.ts +75 -0
  399. package/src/render/font-metrics.ts +72 -0
  400. package/src/render/object-fit.ts +100 -0
  401. package/src/render/path.ts +37 -0
  402. package/src/render/renderer-interface.ts +28 -0
  403. package/src/render/stacking-context.ts +386 -0
  404. package/src/render/vector.ts +19 -0
  405. package/demo/image-smoothing-demo.html +0 -256
  406. package/demo/refactoring-test.html +0 -602
  407. package/dist/lib/__tests__/index.js +0 -85
  408. package/dist/lib/__tests__/index.js.map +0 -1
  409. package/dist/lib/config.js.map +0 -1
  410. package/dist/lib/core/__mocks__/cache-storage.js +0 -7
  411. package/dist/lib/core/__mocks__/cache-storage.js.map +0 -1
  412. package/dist/lib/core/__mocks__/context.js +0 -19
  413. package/dist/lib/core/__mocks__/context.js.map +0 -1
  414. package/dist/lib/core/__mocks__/features.js.map +0 -1
  415. package/dist/lib/core/__mocks__/logger.js +0 -16
  416. package/dist/lib/core/__mocks__/logger.js.map +0 -1
  417. package/dist/lib/core/__tests__/cache-storage.js.map +0 -1
  418. package/dist/lib/core/__tests__/cache-storage.test.js.map +0 -1
  419. package/dist/lib/core/__tests__/logger.js.map +0 -1
  420. package/dist/lib/core/__tests__/validator.js.map +0 -1
  421. package/dist/lib/core/bitwise.js.map +0 -1
  422. package/dist/lib/core/cache-storage.js.map +0 -1
  423. package/dist/lib/core/context.js.map +0 -1
  424. package/dist/lib/core/debugger.js.map +0 -1
  425. package/dist/lib/core/features.js.map +0 -1
  426. package/dist/lib/core/logger.js.map +0 -1
  427. package/dist/lib/core/origin-checker.js.map +0 -1
  428. package/dist/lib/core/performance-monitor.js.map +0 -1
  429. package/dist/lib/core/util.js.map +0 -1
  430. package/dist/lib/core/validator.js.map +0 -1
  431. package/dist/lib/css/IPropertyDescriptor.js.map +0 -1
  432. package/dist/lib/css/ITypeDescriptor.js.map +0 -1
  433. package/dist/lib/css/index.js.map +0 -1
  434. package/dist/lib/css/layout/__mocks__/bounds.js +0 -9
  435. package/dist/lib/css/layout/__mocks__/bounds.js.map +0 -1
  436. package/dist/lib/css/layout/bounds.js.map +0 -1
  437. package/dist/lib/css/layout/text.js.map +0 -1
  438. package/dist/lib/css/property-descriptors/__tests__/background-tests.js +0 -49
  439. package/dist/lib/css/property-descriptors/__tests__/background-tests.js.map +0 -1
  440. package/dist/lib/css/property-descriptors/__tests__/clip-path.test.js +0 -273
  441. package/dist/lib/css/property-descriptors/__tests__/clip-path.test.js.map +0 -1
  442. package/dist/lib/css/property-descriptors/__tests__/font-family.js +0 -19
  443. package/dist/lib/css/property-descriptors/__tests__/font-family.js.map +0 -1
  444. package/dist/lib/css/property-descriptors/__tests__/image-rendering-integration.test.js +0 -143
  445. package/dist/lib/css/property-descriptors/__tests__/image-rendering-integration.test.js.map +0 -1
  446. package/dist/lib/css/property-descriptors/__tests__/image-rendering-performance.test.js.map +0 -1
  447. package/dist/lib/css/property-descriptors/__tests__/image-rendering.test.js +0 -61
  448. package/dist/lib/css/property-descriptors/__tests__/image-rendering.test.js.map +0 -1
  449. package/dist/lib/css/property-descriptors/__tests__/paint-order.js +0 -66
  450. package/dist/lib/css/property-descriptors/__tests__/paint-order.js.map +0 -1
  451. package/dist/lib/css/property-descriptors/__tests__/text-shadow.js +0 -82
  452. package/dist/lib/css/property-descriptors/__tests__/text-shadow.js.map +0 -1
  453. package/dist/lib/css/property-descriptors/__tests__/transform-tests.js +0 -14
  454. package/dist/lib/css/property-descriptors/__tests__/transform-tests.js.map +0 -1
  455. package/dist/lib/css/property-descriptors/background-clip.js.map +0 -1
  456. package/dist/lib/css/property-descriptors/background-color.js.map +0 -1
  457. package/dist/lib/css/property-descriptors/background-image.js.map +0 -1
  458. package/dist/lib/css/property-descriptors/background-origin.js.map +0 -1
  459. package/dist/lib/css/property-descriptors/background-position.js.map +0 -1
  460. package/dist/lib/css/property-descriptors/background-repeat.js.map +0 -1
  461. package/dist/lib/css/property-descriptors/background-size.js.map +0 -1
  462. package/dist/lib/css/property-descriptors/border-color.js.map +0 -1
  463. package/dist/lib/css/property-descriptors/border-radius.js.map +0 -1
  464. package/dist/lib/css/property-descriptors/border-style.js.map +0 -1
  465. package/dist/lib/css/property-descriptors/border-width.js.map +0 -1
  466. package/dist/lib/css/property-descriptors/box-shadow.js.map +0 -1
  467. package/dist/lib/css/property-descriptors/clip-path.js.map +0 -1
  468. package/dist/lib/css/property-descriptors/color.js.map +0 -1
  469. package/dist/lib/css/property-descriptors/content.js.map +0 -1
  470. package/dist/lib/css/property-descriptors/counter-increment.js.map +0 -1
  471. package/dist/lib/css/property-descriptors/counter-reset.js.map +0 -1
  472. package/dist/lib/css/property-descriptors/direction.js.map +0 -1
  473. package/dist/lib/css/property-descriptors/display.js.map +0 -1
  474. package/dist/lib/css/property-descriptors/duration.js.map +0 -1
  475. package/dist/lib/css/property-descriptors/float.js.map +0 -1
  476. package/dist/lib/css/property-descriptors/font-family.js.map +0 -1
  477. package/dist/lib/css/property-descriptors/font-size.js.map +0 -1
  478. package/dist/lib/css/property-descriptors/font-style.js.map +0 -1
  479. package/dist/lib/css/property-descriptors/font-variant.js.map +0 -1
  480. package/dist/lib/css/property-descriptors/font-weight.js.map +0 -1
  481. package/dist/lib/css/property-descriptors/image-rendering.js.map +0 -1
  482. package/dist/lib/css/property-descriptors/letter-spacing.js.map +0 -1
  483. package/dist/lib/css/property-descriptors/line-break.js.map +0 -1
  484. package/dist/lib/css/property-descriptors/line-height.js.map +0 -1
  485. package/dist/lib/css/property-descriptors/list-style-image.js.map +0 -1
  486. package/dist/lib/css/property-descriptors/list-style-position.js.map +0 -1
  487. package/dist/lib/css/property-descriptors/list-style-type.js.map +0 -1
  488. package/dist/lib/css/property-descriptors/margin.js.map +0 -1
  489. package/dist/lib/css/property-descriptors/object-fit.js.map +0 -1
  490. package/dist/lib/css/property-descriptors/opacity.js.map +0 -1
  491. package/dist/lib/css/property-descriptors/overflow-wrap.js.map +0 -1
  492. package/dist/lib/css/property-descriptors/overflow.js.map +0 -1
  493. package/dist/lib/css/property-descriptors/padding.js.map +0 -1
  494. package/dist/lib/css/property-descriptors/paint-order.js.map +0 -1
  495. package/dist/lib/css/property-descriptors/position.js.map +0 -1
  496. package/dist/lib/css/property-descriptors/quotes.js.map +0 -1
  497. package/dist/lib/css/property-descriptors/rotate.js.map +0 -1
  498. package/dist/lib/css/property-descriptors/text-align.js.map +0 -1
  499. package/dist/lib/css/property-descriptors/text-decoration-color.js.map +0 -1
  500. package/dist/lib/css/property-descriptors/text-decoration-line.js.map +0 -1
  501. package/dist/lib/css/property-descriptors/text-decoration-style.js.map +0 -1
  502. package/dist/lib/css/property-descriptors/text-decoration-thickness.js.map +0 -1
  503. package/dist/lib/css/property-descriptors/text-overflow.js.map +0 -1
  504. package/dist/lib/css/property-descriptors/text-shadow.js.map +0 -1
  505. package/dist/lib/css/property-descriptors/text-transform.js.map +0 -1
  506. package/dist/lib/css/property-descriptors/text-underline-offset.js.map +0 -1
  507. package/dist/lib/css/property-descriptors/transform-origin.js.map +0 -1
  508. package/dist/lib/css/property-descriptors/transform.js.map +0 -1
  509. package/dist/lib/css/property-descriptors/visibility.js.map +0 -1
  510. package/dist/lib/css/property-descriptors/webkit-line-clamp.js.map +0 -1
  511. package/dist/lib/css/property-descriptors/webkit-text-stroke-color.js.map +0 -1
  512. package/dist/lib/css/property-descriptors/webkit-text-stroke-width.js.map +0 -1
  513. package/dist/lib/css/property-descriptors/word-break.js.map +0 -1
  514. package/dist/lib/css/property-descriptors/z-index.js.map +0 -1
  515. package/dist/lib/css/syntax/__tests__/tokernizer-tests.js +0 -26
  516. package/dist/lib/css/syntax/__tests__/tokernizer-tests.js.map +0 -1
  517. package/dist/lib/css/syntax/parser.js.map +0 -1
  518. package/dist/lib/css/syntax/tokenizer.js.map +0 -1
  519. package/dist/lib/css/types/__tests__/color-tests.js +0 -111
  520. package/dist/lib/css/types/__tests__/color-tests.js.map +0 -1
  521. package/dist/lib/css/types/__tests__/image-tests.js +0 -215
  522. package/dist/lib/css/types/__tests__/image-tests.js.map +0 -1
  523. package/dist/lib/css/types/angle.js.map +0 -1
  524. package/dist/lib/css/types/color-math.js.map +0 -1
  525. package/dist/lib/css/types/color-spaces/a98.js.map +0 -1
  526. package/dist/lib/css/types/color-spaces/p3.js.map +0 -1
  527. package/dist/lib/css/types/color-spaces/pro-photo.js.map +0 -1
  528. package/dist/lib/css/types/color-spaces/rec2020.js.map +0 -1
  529. package/dist/lib/css/types/color-spaces/srgb.js.map +0 -1
  530. package/dist/lib/css/types/color-utilities.js.map +0 -1
  531. package/dist/lib/css/types/color.js.map +0 -1
  532. package/dist/lib/css/types/functions/-prefix-linear-gradient.js.map +0 -1
  533. package/dist/lib/css/types/functions/-prefix-radial-gradient.js.map +0 -1
  534. package/dist/lib/css/types/functions/-webkit-gradient.js.map +0 -1
  535. package/dist/lib/css/types/functions/__tests__/radial-gradient.js +0 -63
  536. package/dist/lib/css/types/functions/__tests__/radial-gradient.js.map +0 -1
  537. package/dist/lib/css/types/functions/counter.js.map +0 -1
  538. package/dist/lib/css/types/functions/gradient.js.map +0 -1
  539. package/dist/lib/css/types/functions/linear-gradient.js.map +0 -1
  540. package/dist/lib/css/types/functions/radial-gradient.js.map +0 -1
  541. package/dist/lib/css/types/image.js.map +0 -1
  542. package/dist/lib/css/types/index.js.map +0 -1
  543. package/dist/lib/css/types/length-percentage.js.map +0 -1
  544. package/dist/lib/css/types/length.js.map +0 -1
  545. package/dist/lib/css/types/time.js.map +0 -1
  546. package/dist/lib/dom/__mocks__/document-cloner.js +0 -23
  547. package/dist/lib/dom/__mocks__/document-cloner.js.map +0 -1
  548. package/dist/lib/dom/__tests__/dom-normalizer.test.js.map +0 -1
  549. package/dist/lib/dom/__tests__/element-container.test.js +0 -109
  550. package/dist/lib/dom/__tests__/element-container.test.js.map +0 -1
  551. package/dist/lib/dom/document-cloner.js.map +0 -1
  552. package/dist/lib/dom/dom-normalizer.js.map +0 -1
  553. package/dist/lib/dom/element-container.js.map +0 -1
  554. package/dist/lib/dom/elements/li-element-container.js.map +0 -1
  555. package/dist/lib/dom/elements/ol-element-container.js.map +0 -1
  556. package/dist/lib/dom/elements/select-element-container.js.map +0 -1
  557. package/dist/lib/dom/elements/textarea-element-container.js.map +0 -1
  558. package/dist/lib/dom/node-parser.js.map +0 -1
  559. package/dist/lib/dom/node-type-guards.js.map +0 -1
  560. package/dist/lib/dom/replaced-elements/canvas-element-container.js.map +0 -1
  561. package/dist/lib/dom/replaced-elements/iframe-element-container.js.map +0 -1
  562. package/dist/lib/dom/replaced-elements/image-element-container.js.map +0 -1
  563. package/dist/lib/dom/replaced-elements/index.js.map +0 -1
  564. package/dist/lib/dom/replaced-elements/input-element-container.js.map +0 -1
  565. package/dist/lib/dom/replaced-elements/pseudo-elements.js.map +0 -1
  566. package/dist/lib/dom/replaced-elements/svg-element-container.js.map +0 -1
  567. package/dist/lib/dom/text-container.js.map +0 -1
  568. package/dist/lib/index.js.map +0 -1
  569. package/dist/lib/invariant.js.map +0 -1
  570. package/dist/lib/render/background.js.map +0 -1
  571. package/dist/lib/render/bezier-curve.js.map +0 -1
  572. package/dist/lib/render/border.js.map +0 -1
  573. package/dist/lib/render/bound-curves.js.map +0 -1
  574. package/dist/lib/render/box-sizing.js.map +0 -1
  575. package/dist/lib/render/canvas/__tests__/background-renderer.test.js.map +0 -1
  576. package/dist/lib/render/canvas/__tests__/border-renderer.test.js +0 -23
  577. package/dist/lib/render/canvas/__tests__/border-renderer.test.js.map +0 -1
  578. package/dist/lib/render/canvas/__tests__/effects-renderer.test.js +0 -30
  579. package/dist/lib/render/canvas/__tests__/effects-renderer.test.js.map +0 -1
  580. package/dist/lib/render/canvas/__tests__/text-renderer.test.js +0 -310
  581. package/dist/lib/render/canvas/__tests__/text-renderer.test.js.map +0 -1
  582. package/dist/lib/render/canvas/background-renderer.js.map +0 -1
  583. package/dist/lib/render/canvas/border-renderer.js.map +0 -1
  584. package/dist/lib/render/canvas/canvas-renderer.js.map +0 -1
  585. package/dist/lib/render/canvas/effects-renderer.js.map +0 -1
  586. package/dist/lib/render/canvas/foreignobject-renderer.js.map +0 -1
  587. package/dist/lib/render/canvas/text-renderer.js.map +0 -1
  588. package/dist/lib/render/effects.js.map +0 -1
  589. package/dist/lib/render/font-metrics.js.map +0 -1
  590. package/dist/lib/render/path.js.map +0 -1
  591. package/dist/lib/render/renderer-interface.js.map +0 -1
  592. package/dist/lib/render/renderer.js +0 -11
  593. package/dist/lib/render/renderer.js.map +0 -1
  594. package/dist/lib/render/stacking-context.js.map +0 -1
  595. package/dist/lib/render/vector.js.map +0 -1
  596. package/dist/types/__tests__/index.d.ts +0 -1
  597. package/dist/types/core/__mocks__/cache-storage.d.ts +0 -2
  598. package/dist/types/core/__mocks__/context.d.ts +0 -9
  599. package/dist/types/core/__mocks__/features.d.ts +0 -8
  600. package/dist/types/core/__mocks__/logger.d.ts +0 -9
  601. package/dist/types/core/__tests__/cache-storage.d.ts +0 -1
  602. package/dist/types/core/__tests__/cache-storage.test.d.ts +0 -1
  603. package/dist/types/core/__tests__/logger.d.ts +0 -1
  604. package/dist/types/core/__tests__/validator.d.ts +0 -1
  605. package/dist/types/css/layout/__mocks__/bounds.d.ts +0 -2
  606. package/dist/types/css/property-descriptors/__tests__/background-tests.d.ts +0 -1
  607. package/dist/types/css/property-descriptors/__tests__/clip-path.test.d.ts +0 -1
  608. package/dist/types/css/property-descriptors/__tests__/font-family.d.ts +0 -1
  609. package/dist/types/css/property-descriptors/__tests__/image-rendering-integration.test.d.ts +0 -1
  610. package/dist/types/css/property-descriptors/__tests__/image-rendering-performance.test.d.ts +0 -1
  611. package/dist/types/css/property-descriptors/__tests__/image-rendering.test.d.ts +0 -1
  612. package/dist/types/css/property-descriptors/__tests__/paint-order.d.ts +0 -1
  613. package/dist/types/css/property-descriptors/__tests__/text-shadow.d.ts +0 -1
  614. package/dist/types/css/property-descriptors/__tests__/transform-tests.d.ts +0 -1
  615. package/dist/types/css/syntax/__tests__/tokernizer-tests.d.ts +0 -1
  616. package/dist/types/css/types/__tests__/color-tests.d.ts +0 -1
  617. package/dist/types/css/types/__tests__/image-tests.d.ts +0 -1
  618. package/dist/types/css/types/functions/__tests__/radial-gradient.d.ts +0 -1
  619. package/dist/types/dom/__mocks__/document-cloner.d.ts +0 -6
  620. package/dist/types/dom/__tests__/dom-normalizer.test.d.ts +0 -1
  621. package/dist/types/dom/__tests__/element-container.test.d.ts +0 -1
  622. package/dist/types/render/canvas/__tests__/background-renderer.test.d.ts +0 -1
  623. package/dist/types/render/canvas/__tests__/border-renderer.test.d.ts +0 -1
  624. package/dist/types/render/canvas/__tests__/effects-renderer.test.d.ts +0 -1
  625. package/dist/types/render/canvas/__tests__/text-renderer.test.d.ts +0 -1
  626. package/dist/types/render/renderer.d.ts +0 -7
  627. package/eslint.config.js +0 -35
  628. package/jest.config.cjs +0 -5
  629. package/karma.conf.cjs +0 -300
  630. /package/dist/types/css/{IPropertyDescriptor.d.ts → property-descriptor.d.ts} +0 -0
  631. /package/dist/types/css/{ITypeDescriptor.d.ts → type-descriptor.d.ts} +0 -0
@@ -0,0 +1,700 @@
1
+ /**
2
+ * Text Renderer
3
+ *
4
+ * Handles rendering of text content including:
5
+ * - Text with letter spacing
6
+ * - Text decorations (underline, overline, line-through)
7
+ * - Text shadows
8
+ * - Webkit line clamp
9
+ * - Text overflow ellipsis
10
+ * - Paint order (fill/stroke)
11
+ * - Font styles
12
+ */
13
+
14
+ import { Context } from '../../core/context';
15
+ import { TextContainer } from '../../dom/text-container';
16
+ import { CSSParsedDeclaration } from '../../css';
17
+ import { Bounds } from '../../css/layout/bounds';
18
+ import { TextBounds, segmentGraphemes } from '../../css/layout/text';
19
+ import { asString } from '../../css/types/color-utilities';
20
+ import { TEXT_DECORATION_LINE } from '../../css/property-descriptors/text-decoration-line';
21
+ import { TEXT_DECORATION_STYLE } from '../../css/property-descriptors/text-decoration-style';
22
+ import { PAINT_ORDER_LAYER } from '../../css/property-descriptors/paint-order';
23
+ import { DIRECTION } from '../../css/property-descriptors/direction';
24
+ import { DISPLAY } from '../../css/property-descriptors/display';
25
+ import { TEXT_OVERFLOW } from '../../css/property-descriptors/text-overflow';
26
+ import { OVERFLOW } from '../../css/property-descriptors/overflow';
27
+ import { isDimensionToken } from '../../css/syntax/parser';
28
+ import { TextShadow } from '../../css/property-descriptors/text-shadow';
29
+ import {
30
+ isSidewaysWritingMode,
31
+ isVerticalWritingMode,
32
+ WRITING_MODE
33
+ } from '../../css/property-descriptors/writing-mode';
34
+
35
+ /**
36
+ * Dependencies required for TextRenderer
37
+ */
38
+ export interface TextRendererDependencies {
39
+ ctx: CanvasRenderingContext2D;
40
+ context: Context;
41
+ options: {
42
+ scale: number;
43
+ };
44
+ }
45
+
46
+ // iOS font fix - see https://github.com/niklasvh/html2canvas/pull/2645
47
+ const iOSBrokenFonts = ['-apple-system', 'system-ui'];
48
+
49
+ /**
50
+ * Detect CJK (Chinese, Japanese, Korean) characters in a string.
51
+ * CJK characters use the ideographic baseline in browsers, which differs
52
+ * from the alphabetic baseline used for Latin script.
53
+ *
54
+ * Covers:
55
+ * U+2E80–U+2FFF CJK Radicals Supplement, Kangxi Radicals
56
+ * U+3000–U+30FF CJK Symbols & Punctuation (。、「」…), Hiragana, Katakana
57
+ * U+3400–U+4DBF CJK Extension A
58
+ * U+4E00–U+9FFF CJK Unified Ideographs (most common Chinese/Japanese/Korean)
59
+ * U+AC00–U+D7AF Hangul Syllables
60
+ * U+F900–U+FAFF CJK Compatibility Ideographs
61
+ * U+FF01–U+FFEF Halfwidth and Fullwidth Forms (A B 1 2 ! ? etc.)
62
+ */
63
+ const CJK_CHAR_REGEX = /[\u2E80-\u2FFF\u3000-\u30FF\u3400-\u4DBF\u4E00-\u9FFF\uAC00-\uD7AF\uF900-\uFAFF\uFF01-\uFFEF]/;
64
+
65
+ export const hasCJKCharacters = (text: string): boolean => CJK_CHAR_REGEX.test(text);
66
+
67
+ /**
68
+ * Detect iOS version from user agent
69
+ * Returns null if not iOS or version cannot be determined
70
+ */
71
+ const getIOSVersion = (): number | null => {
72
+ if (typeof navigator === 'undefined') {
73
+ return null;
74
+ }
75
+
76
+ const userAgent = navigator.userAgent;
77
+
78
+ // Check if it's iOS or iPadOS
79
+ // iPadOS 13+ may identify as Macintosh, check for touch support
80
+ const isIOS = /iPhone|iPad|iPod/.test(userAgent);
81
+ const isIPadOS = /Macintosh/.test(userAgent) && navigator.maxTouchPoints && navigator.maxTouchPoints > 1;
82
+
83
+ if (!isIOS && !isIPadOS) {
84
+ return null;
85
+ }
86
+
87
+ // Extract version number from various iOS user agent formats:
88
+ // - "iPhone OS 15_0" or "iPhone OS 15_0_1"
89
+ // - "CPU OS 15_0 like Mac OS X"
90
+ // - "CPU iPhone OS 15_0 like Mac OS X"
91
+ // - "Version/15.0" (for iPadOS)
92
+ const patterns = [
93
+ /(?:iPhone|CPU(?:\siPhone)?)\sOS\s(\d+)[\._](\d+)/, // iPhone OS, CPU OS, CPU iPhone OS
94
+ /Version\/(\d+)\.(\d+)/ // Version/15.0 (iPadOS)
95
+ ];
96
+
97
+ for (const pattern of patterns) {
98
+ const match = userAgent.match(pattern);
99
+ if (match && match[1]) {
100
+ return parseInt(match[1], 10);
101
+ }
102
+ }
103
+
104
+ return null;
105
+ };
106
+
107
+ const fixIOSSystemFonts = (fontFamilies: string[]): string[] => {
108
+ const iosVersion = getIOSVersion();
109
+
110
+ // On iOS 15.0 and 15.1, system fonts have rendering issues
111
+ // Fixed in iOS 17+
112
+ if (iosVersion !== null && iosVersion >= 15 && iosVersion < 17) {
113
+ return fontFamilies.map((fontFamily) =>
114
+ iOSBrokenFonts.indexOf(fontFamily) !== -1
115
+ ? `-apple-system, "Helvetica Neue", Arial, sans-serif`
116
+ : fontFamily
117
+ );
118
+ }
119
+
120
+ return fontFamilies;
121
+ };
122
+
123
+ const getTextStrokeLineJoin = (): CanvasLineJoin => {
124
+ const currentWindow = typeof window !== 'undefined' ? (window as Window & { chrome?: unknown }) : undefined;
125
+ return currentWindow?.chrome ? 'miter' : 'round';
126
+ };
127
+
128
+ /**
129
+ * Text Renderer
130
+ *
131
+ * Specialized renderer for text content.
132
+ * Extracted from CanvasRenderer to improve code organization and maintainability.
133
+ */
134
+ export class TextRenderer {
135
+ private readonly ctx: CanvasRenderingContext2D;
136
+ private readonly options: { scale: number };
137
+
138
+ constructor(deps: TextRendererDependencies) {
139
+ this.ctx = deps.ctx;
140
+ // context stored but not used directly in this renderer
141
+ this.options = deps.options;
142
+ }
143
+
144
+ /**
145
+ * Iterate grapheme clusters one-by-one, applying correct letter-spacing and
146
+ * per-script baseline for each character.
147
+ *
148
+ * Issue #73: When letter-spacing is non-zero, text must be rendered character by
149
+ * character. This helper centralises two fixes applied during that iteration:
150
+ * 1. Add `letterSpacing` to each character's advance width (was previously
151
+ * omitted, causing characters to render without any spacing).
152
+ * 2. Switch to the ideographic baseline for CJK glyphs so their vertical
153
+ * position matches how browsers lay them out in the DOM.
154
+ *
155
+ * The `renderFn` callback receives (letter, x, y) and performs the actual draw
156
+ * call (fillText or strokeText), allowing fill and stroke paths to share one
157
+ * implementation.
158
+ */
159
+ private iterateLettersWithLetterSpacing(
160
+ text: TextBounds,
161
+ letterSpacing: number,
162
+ baseline: number,
163
+ writingMode: WRITING_MODE,
164
+ renderFn: (letter: string, x: number, y: number) => void
165
+ ): void {
166
+ if (isVerticalWritingMode(writingMode)) {
167
+ this.iterateVerticalGlyphs(text, letterSpacing, baseline, writingMode, renderFn);
168
+ return;
169
+ }
170
+
171
+ const letters = segmentGraphemes(text.text);
172
+ const y = text.bounds.top + baseline;
173
+ let left = text.bounds.left;
174
+ for (const letter of letters) {
175
+ if (hasCJKCharacters(letter)) {
176
+ const savedBaseline = this.ctx.textBaseline;
177
+ this.ctx.textBaseline = 'ideographic';
178
+ renderFn(letter, left, y);
179
+ this.ctx.textBaseline = savedBaseline;
180
+ } else {
181
+ renderFn(letter, left, y);
182
+ }
183
+ left += this.ctx.measureText(letter).width + letterSpacing;
184
+ }
185
+ }
186
+
187
+ private iterateVerticalGlyphs(
188
+ text: TextBounds,
189
+ letterSpacing: number,
190
+ baseline: number,
191
+ writingMode: WRITING_MODE,
192
+ renderFn: (letter: string, x: number, y: number) => void
193
+ ): void {
194
+ const letters = segmentGraphemes(text.text);
195
+ let top = text.bounds.top;
196
+
197
+ for (const letter of letters) {
198
+ if (isSidewaysWritingMode(writingMode) || (!hasCJKCharacters(letter) && letter.trim().length > 0)) {
199
+ this.ctx.save();
200
+ this.ctx.translate(text.bounds.left + baseline, top);
201
+ this.ctx.rotate(writingMode === WRITING_MODE.SIDEWAYS_LR ? -Math.PI / 2 : Math.PI / 2);
202
+ renderFn(letter, 0, 0);
203
+ this.ctx.restore();
204
+ } else {
205
+ const savedBaseline = this.ctx.textBaseline;
206
+ if (hasCJKCharacters(letter)) {
207
+ this.ctx.textBaseline = 'ideographic';
208
+ }
209
+ renderFn(letter, text.bounds.left, top + baseline);
210
+ this.ctx.textBaseline = savedBaseline;
211
+ }
212
+ top += this.ctx.measureText(letter).width + letterSpacing;
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Render text with letter-spacing applied (fill pass).
218
+ * When letterSpacing is 0 the whole string is drawn in one call; otherwise each
219
+ * grapheme is drawn individually so spacing and CJK baseline are applied correctly.
220
+ */
221
+ renderTextWithLetterSpacing(
222
+ text: TextBounds,
223
+ letterSpacing: number,
224
+ baseline: number,
225
+ writingMode: WRITING_MODE = WRITING_MODE.HORIZONTAL_TB
226
+ ): void {
227
+ this.renderFillText(text, letterSpacing, baseline, writingMode);
228
+ }
229
+
230
+ private canRenderWholeText(letterSpacing: number, writingMode: WRITING_MODE): boolean {
231
+ return letterSpacing === 0 && !isVerticalWritingMode(writingMode);
232
+ }
233
+
234
+ private renderFillText(text: TextBounds, letterSpacing: number, baseline: number, writingMode: WRITING_MODE): void {
235
+ if (this.canRenderWholeText(letterSpacing, writingMode)) {
236
+ this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + baseline);
237
+ } else {
238
+ this.iterateLettersWithLetterSpacing(text, letterSpacing, baseline, writingMode, (letter, x, y) => {
239
+ this.ctx.fillText(letter, x, y);
240
+ });
241
+ }
242
+ }
243
+
244
+ private renderStrokeText(
245
+ text: TextBounds,
246
+ letterSpacing: number,
247
+ baseline: number,
248
+ writingMode: WRITING_MODE
249
+ ): void {
250
+ if (this.canRenderWholeText(letterSpacing, writingMode)) {
251
+ this.ctx.strokeText(text.text, text.bounds.left, text.bounds.top + baseline);
252
+ } else {
253
+ this.iterateLettersWithLetterSpacing(text, letterSpacing, baseline, writingMode, (letter, x, y) => {
254
+ this.ctx.strokeText(letter, x, y);
255
+ });
256
+ }
257
+ }
258
+
259
+ private renderTextStrokeWithStyle(text: TextBounds, styles: CSSParsedDeclaration): void {
260
+ if (!styles.webkitTextStrokeWidth || !text.text.trim().length) {
261
+ return;
262
+ }
263
+
264
+ this.ctx.strokeStyle = asString(styles.webkitTextStrokeColor);
265
+ this.ctx.lineWidth = styles.webkitTextStrokeWidth;
266
+ this.ctx.lineJoin = getTextStrokeLineJoin();
267
+ this.renderStrokeText(text, styles.letterSpacing, styles.fontSize.number, styles.writingMode);
268
+ this.ctx.strokeStyle = '';
269
+ this.ctx.lineWidth = 0;
270
+ this.ctx.lineJoin = 'miter';
271
+ }
272
+
273
+ private renderTextFillWithShadows(text: TextBounds, styles: CSSParsedDeclaration): void {
274
+ this.ctx.fillStyle = asString(styles.color);
275
+ this.renderTextWithLetterSpacing(text, styles.letterSpacing, styles.fontSize.number, styles.writingMode);
276
+
277
+ const textShadows: TextShadow = styles.textShadow;
278
+ if (textShadows.length && text.text.trim().length) {
279
+ textShadows
280
+ .slice(0)
281
+ .reverse()
282
+ .forEach((textShadow) => {
283
+ this.ctx.shadowColor = asString(textShadow.color);
284
+ this.ctx.shadowOffsetX = textShadow.offsetX.number * this.options.scale;
285
+ this.ctx.shadowOffsetY = textShadow.offsetY.number * this.options.scale;
286
+ this.ctx.shadowBlur = textShadow.blur.number;
287
+
288
+ this.renderTextWithLetterSpacing(
289
+ text,
290
+ styles.letterSpacing,
291
+ styles.fontSize.number,
292
+ styles.writingMode
293
+ );
294
+ });
295
+
296
+ this.ctx.shadowColor = '';
297
+ this.ctx.shadowOffsetX = 0;
298
+ this.ctx.shadowOffsetY = 0;
299
+ this.ctx.shadowBlur = 0;
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Helper method to render text with paint order support
305
+ * Reduces code duplication in line-clamp and normal rendering
306
+ */
307
+ private renderTextBoundWithPaintOrder(
308
+ textBound: TextBounds,
309
+ styles: CSSParsedDeclaration,
310
+ paintOrderLayers: number[]
311
+ ): void {
312
+ paintOrderLayers.forEach((paintOrderLayer: number) => {
313
+ switch (paintOrderLayer) {
314
+ case PAINT_ORDER_LAYER.FILL:
315
+ this.ctx.fillStyle = asString(styles.color);
316
+ this.renderTextWithLetterSpacing(
317
+ textBound,
318
+ styles.letterSpacing,
319
+ styles.fontSize.number,
320
+ styles.writingMode
321
+ );
322
+ break;
323
+ case PAINT_ORDER_LAYER.STROKE:
324
+ this.renderTextStrokeWithStyle(textBound, styles);
325
+ break;
326
+ }
327
+ });
328
+ }
329
+
330
+ private renderTextDecoration(bounds: Bounds, styles: CSSParsedDeclaration): void {
331
+ this.ctx.fillStyle = asString(styles.textDecorationColor || styles.color);
332
+
333
+ // Calculate decoration line thickness
334
+ let thickness = 1; // default
335
+ if (typeof styles.textDecorationThickness === 'number') {
336
+ thickness = styles.textDecorationThickness;
337
+ } else if (styles.textDecorationThickness === 'from-font') {
338
+ // Use a reasonable default based on font size
339
+ thickness = Math.max(1, Math.floor(styles.fontSize.number * 0.05));
340
+ }
341
+ // 'auto' uses default thickness of 1
342
+
343
+ // Calculate underline offset
344
+ let underlineOffset = 0;
345
+ if (typeof styles.textUnderlineOffset === 'number') {
346
+ // It's a pixel value
347
+ underlineOffset = styles.textUnderlineOffset;
348
+ }
349
+ // 'auto' uses default offset of 0
350
+
351
+ const decorationStyle = styles.textDecorationStyle;
352
+
353
+ styles.textDecorationLine.forEach((textDecorationLine) => {
354
+ let y = 0;
355
+
356
+ switch (textDecorationLine) {
357
+ case TEXT_DECORATION_LINE.UNDERLINE:
358
+ y = bounds.top + bounds.height - thickness + underlineOffset;
359
+ break;
360
+ case TEXT_DECORATION_LINE.OVERLINE:
361
+ y = bounds.top;
362
+ break;
363
+ case TEXT_DECORATION_LINE.LINE_THROUGH:
364
+ y = bounds.top + (bounds.height / 2 - thickness / 2);
365
+ break;
366
+ default:
367
+ return;
368
+ }
369
+
370
+ this.drawDecorationLine(bounds.left, y, bounds.width, thickness, decorationStyle);
371
+ });
372
+ }
373
+
374
+ private drawDecorationLine(x: number, y: number, width: number, thickness: number, style: number): void {
375
+ switch (style) {
376
+ case TEXT_DECORATION_STYLE.SOLID:
377
+ // Solid line (default)
378
+ this.ctx.fillRect(x, y, width, thickness);
379
+ break;
380
+
381
+ case TEXT_DECORATION_STYLE.DOUBLE:
382
+ // Double line
383
+ const gap = Math.max(1, thickness);
384
+ this.ctx.fillRect(x, y, width, thickness);
385
+ this.ctx.fillRect(x, y + thickness + gap, width, thickness);
386
+ break;
387
+
388
+ case TEXT_DECORATION_STYLE.DOTTED:
389
+ // Dotted line
390
+ this.ctx.save();
391
+ this.ctx.beginPath();
392
+ this.ctx.setLineDash([thickness, thickness * 2]);
393
+ this.ctx.lineWidth = thickness;
394
+ this.ctx.strokeStyle = this.ctx.fillStyle;
395
+ this.ctx.moveTo(x, y + thickness / 2);
396
+ this.ctx.lineTo(x + width, y + thickness / 2);
397
+ this.ctx.stroke();
398
+ this.ctx.restore();
399
+ break;
400
+
401
+ case TEXT_DECORATION_STYLE.DASHED:
402
+ // Dashed line
403
+ this.ctx.save();
404
+ this.ctx.beginPath();
405
+ this.ctx.setLineDash([thickness * 3, thickness * 2]);
406
+ this.ctx.lineWidth = thickness;
407
+ this.ctx.strokeStyle = this.ctx.fillStyle;
408
+ this.ctx.moveTo(x, y + thickness / 2);
409
+ this.ctx.lineTo(x + width, y + thickness / 2);
410
+ this.ctx.stroke();
411
+ this.ctx.restore();
412
+ break;
413
+
414
+ case TEXT_DECORATION_STYLE.WAVY:
415
+ // Wavy line (approximation using quadratic curves)
416
+ this.ctx.save();
417
+ this.ctx.beginPath();
418
+ this.ctx.lineWidth = thickness;
419
+ this.ctx.strokeStyle = this.ctx.fillStyle;
420
+
421
+ const amplitude = thickness * 2;
422
+ const wavelength = thickness * 4;
423
+ let currentX = x;
424
+
425
+ this.ctx.moveTo(currentX, y + thickness / 2);
426
+
427
+ while (currentX < x + width) {
428
+ const nextX = Math.min(currentX + wavelength / 2, x + width);
429
+ this.ctx.quadraticCurveTo(
430
+ currentX + wavelength / 4,
431
+ y + thickness / 2 - amplitude,
432
+ nextX,
433
+ y + thickness / 2
434
+ );
435
+ currentX = nextX;
436
+
437
+ if (currentX < x + width) {
438
+ const nextX2 = Math.min(currentX + wavelength / 2, x + width);
439
+ this.ctx.quadraticCurveTo(
440
+ currentX + wavelength / 4,
441
+ y + thickness / 2 + amplitude,
442
+ nextX2,
443
+ y + thickness / 2
444
+ );
445
+ currentX = nextX2;
446
+ }
447
+ }
448
+
449
+ this.ctx.stroke();
450
+ this.ctx.restore();
451
+ break;
452
+
453
+ default:
454
+ // Fallback to solid
455
+ this.ctx.fillRect(x, y, width, thickness);
456
+ }
457
+ }
458
+
459
+ // Helper method to truncate text and add ellipsis if needed
460
+ private truncateTextWithEllipsis(text: string, maxWidth: number, letterSpacing: number): string {
461
+ // Use the Unicode ellipsis character (U+2026) whose width the browser measures
462
+ // as a single glyph, matching native text-overflow behaviour more closely.
463
+ const ellipsis = '\u2026';
464
+ const ellipsisWidth = this.ctx.measureText(ellipsis).width;
465
+ // Segment into grapheme clusters so multi-byte characters (emoji, composed
466
+ // sequences) are never split mid-character.
467
+ const graphemes = segmentGraphemes(text);
468
+
469
+ if (letterSpacing === 0) {
470
+ // Measure the whole candidate string for accuracy: the browser applies
471
+ // kerning and ligatures when rendering multiple glyphs together, so
472
+ // measuring them as one string is more precise than summing individual widths.
473
+ // Binary search reduces measurements from O(n) to O(log n).
474
+ const fits = (n: number) =>
475
+ this.ctx.measureText(graphemes.slice(0, n).join('')).width + ellipsisWidth <= maxWidth;
476
+ let lo = 0;
477
+ let hi = graphemes.length;
478
+ while (lo < hi) {
479
+ const mid = (lo + hi + 1) >> 1;
480
+ if (fits(mid)) {
481
+ lo = mid;
482
+ } else {
483
+ hi = mid - 1;
484
+ }
485
+ }
486
+ return graphemes.slice(0, lo).join('') + ellipsis;
487
+ } else {
488
+ let width = ellipsisWidth;
489
+ const result: string[] = [];
490
+
491
+ for (const letter of graphemes) {
492
+ const glyphWidth = this.ctx.measureText(letter).width;
493
+ // Check against glyph width only (no trailing spacing): letter-spacing
494
+ // is applied *between* characters, not after the final glyph. Using
495
+ // `glyphWidth + letterSpacing` would incorrectly discard letters that
496
+ // fit as the last character before the ellipsis.
497
+ if (width + glyphWidth > maxWidth) {
498
+ break;
499
+ }
500
+ result.push(letter);
501
+ // Accumulate glyph + inter-character spacing for the *next* iteration.
502
+ width += glyphWidth + letterSpacing;
503
+ }
504
+
505
+ return result.join('') + ellipsis;
506
+ }
507
+ }
508
+
509
+ /**
510
+ * Create font style array
511
+ * Public method used by list rendering
512
+ */
513
+ createFontStyle(styles: CSSParsedDeclaration): string[] {
514
+ const fontVariant = styles.fontVariant
515
+ .filter((variant) => variant === 'normal' || variant === 'small-caps')
516
+ .join('');
517
+ const fontFamily = fixIOSSystemFonts(styles.fontFamily).join(', ');
518
+ const fontSize = isDimensionToken(styles.fontSize)
519
+ ? `${styles.fontSize.number}${styles.fontSize.unit}`
520
+ : `${styles.fontSize.number}px`;
521
+
522
+ return [
523
+ [styles.fontStyle, fontVariant, styles.fontWeight, fontSize, fontFamily].join(' '),
524
+ fontFamily,
525
+ fontSize
526
+ ];
527
+ }
528
+
529
+ async renderTextNode(text: TextContainer, styles: CSSParsedDeclaration, containerBounds?: Bounds): Promise<void> {
530
+ const [font] = this.createFontStyle(styles);
531
+
532
+ this.ctx.font = font;
533
+
534
+ this.ctx.direction = styles.direction === DIRECTION.RTL ? 'rtl' : 'ltr';
535
+ this.ctx.textAlign = 'left';
536
+ this.ctx.textBaseline = 'alphabetic';
537
+ const paintOrder = styles.paintOrder;
538
+
539
+ // Calculate line height for text layout detection (used by both line-clamp and ellipsis)
540
+ const lineHeight = styles.fontSize.number * 1.5;
541
+
542
+ // Check if we need to apply -webkit-line-clamp
543
+ // This limits text to a specific number of lines with ellipsis
544
+ const shouldApplyLineClamp =
545
+ styles.webkitLineClamp > 0 &&
546
+ (styles.display & DISPLAY.BLOCK) !== 0 &&
547
+ styles.overflowY === OVERFLOW.HIDDEN &&
548
+ text.textBounds.length > 0;
549
+
550
+ if (shouldApplyLineClamp) {
551
+ // Group text bounds by lines based on their Y position
552
+ const lines: TextBounds[][] = [];
553
+ let currentLine: TextBounds[] = [];
554
+ let currentLineTop = text.textBounds[0].bounds.top;
555
+
556
+ text.textBounds.forEach((tb) => {
557
+ // If this text bound is on a different line, start a new line
558
+ if (Math.abs(tb.bounds.top - currentLineTop) >= lineHeight * 0.5) {
559
+ if (currentLine.length > 0) {
560
+ lines.push(currentLine);
561
+ }
562
+ currentLine = [tb];
563
+ currentLineTop = tb.bounds.top;
564
+ } else {
565
+ currentLine.push(tb);
566
+ }
567
+ });
568
+
569
+ // Don't forget the last line
570
+ if (currentLine.length > 0) {
571
+ lines.push(currentLine);
572
+ }
573
+
574
+ // Only render up to webkitLineClamp lines
575
+ const maxLines = styles.webkitLineClamp;
576
+ if (lines.length > maxLines) {
577
+ // Render only the first (maxLines - 1) complete lines
578
+ for (let i = 0; i < maxLines - 1; i++) {
579
+ lines[i].forEach((textBound) => {
580
+ this.renderTextBoundWithPaintOrder(textBound, styles, paintOrder);
581
+ });
582
+ }
583
+
584
+ // For the last line, truncate with ellipsis
585
+ const lastLine = lines[maxLines - 1];
586
+ if (lastLine && lastLine.length > 0 && containerBounds) {
587
+ const lastLineText = lastLine.map((tb) => tb.text).join('');
588
+ const firstBound = lastLine[0];
589
+ const availableWidth = containerBounds.width - (firstBound.bounds.left - containerBounds.left);
590
+ const truncatedText = this.truncateTextWithEllipsis(
591
+ lastLineText,
592
+ availableWidth,
593
+ styles.letterSpacing
594
+ );
595
+
596
+ // Build TextBounds once; reused for fill and stroke without re-allocating.
597
+ const truncatedBounds = new TextBounds(truncatedText, firstBound.bounds);
598
+
599
+ paintOrder.forEach((paintOrderLayer) => {
600
+ switch (paintOrderLayer) {
601
+ case PAINT_ORDER_LAYER.FILL:
602
+ this.ctx.fillStyle = asString(styles.color);
603
+ this.renderTextWithLetterSpacing(
604
+ truncatedBounds,
605
+ styles.letterSpacing,
606
+ styles.fontSize.number,
607
+ styles.writingMode
608
+ );
609
+ break;
610
+ case PAINT_ORDER_LAYER.STROKE:
611
+ this.renderTextStrokeWithStyle(truncatedBounds, styles);
612
+ break;
613
+ }
614
+ });
615
+ }
616
+ return; // Don't render anything else
617
+ }
618
+ // If lines.length <= maxLines, fall through to normal rendering
619
+ }
620
+
621
+ // Check if we need to apply text-overflow: ellipsis
622
+ // Issue #203: Only apply ellipsis for single-line text overflow
623
+ // Multi-line text truncation (like -webkit-line-clamp) should not be affected
624
+ const shouldApplyEllipsis =
625
+ styles.textOverflow === TEXT_OVERFLOW.ELLIPSIS &&
626
+ containerBounds &&
627
+ styles.overflowX === OVERFLOW.HIDDEN &&
628
+ text.textBounds.length > 0;
629
+
630
+ // Calculate total text width if ellipsis might be needed
631
+ let needsEllipsis = false;
632
+ let truncatedText = '';
633
+ if (shouldApplyEllipsis) {
634
+ // Check if all text bounds are on approximately the same line (single-line scenario)
635
+ // For multi-line text (like -webkit-line-clamp), textBounds will have different Y positions
636
+ const firstTop = text.textBounds[0].bounds.top;
637
+ const isSingleLine = text.textBounds.every((tb) => Math.abs(tb.bounds.top - firstTop) < lineHeight * 0.5);
638
+
639
+ if (isSingleLine) {
640
+ // Measure the full text content
641
+ // Note: text.textBounds may contain whitespace characters from HTML formatting
642
+ // We need to collapse them like the browser does for white-space: nowrap
643
+ let fullText = text.textBounds.map((tb) => tb.text).join('');
644
+
645
+ // Collapse whitespace: replace sequences of whitespace (including newlines) with single spaces
646
+ // and trim leading/trailing whitespace
647
+ fullText = fullText.replace(/\s+/g, ' ').trim();
648
+
649
+ const fullTextWidth = this.ctx.measureText(fullText).width;
650
+ const availableWidth = containerBounds.width;
651
+
652
+ if (fullTextWidth > availableWidth) {
653
+ needsEllipsis = true;
654
+ truncatedText = this.truncateTextWithEllipsis(fullText, availableWidth, styles.letterSpacing);
655
+ }
656
+ }
657
+ }
658
+
659
+ // If ellipsis is needed, render the truncated text once
660
+ if (needsEllipsis) {
661
+ const firstBound = text.textBounds[0];
662
+ // Build TextBounds once; reused across paint layers and every shadow pass
663
+ // to avoid repeated allocation inside forEach callbacks.
664
+ const truncatedBounds = new TextBounds(truncatedText, firstBound.bounds);
665
+
666
+ paintOrder.forEach((paintOrderLayer) => {
667
+ switch (paintOrderLayer) {
668
+ case PAINT_ORDER_LAYER.FILL: {
669
+ this.renderTextFillWithShadows(truncatedBounds, styles);
670
+ break;
671
+ }
672
+ case PAINT_ORDER_LAYER.STROKE:
673
+ this.renderTextStrokeWithStyle(truncatedBounds, styles);
674
+ break;
675
+ }
676
+ });
677
+ return;
678
+ }
679
+
680
+ // Normal rendering (no ellipsis needed)
681
+ text.textBounds.forEach((text) => {
682
+ paintOrder.forEach((paintOrderLayer) => {
683
+ switch (paintOrderLayer) {
684
+ case PAINT_ORDER_LAYER.FILL: {
685
+ this.renderTextFillWithShadows(text, styles);
686
+
687
+ if (styles.textDecorationLine.length) {
688
+ this.renderTextDecoration(text.bounds, styles);
689
+ }
690
+ break;
691
+ }
692
+ case PAINT_ORDER_LAYER.STROKE: {
693
+ this.renderTextStrokeWithStyle(text, styles);
694
+ break;
695
+ }
696
+ }
697
+ });
698
+ });
699
+ }
700
+ }