blitz-pdf 0.0.0-stage → 0.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 (370) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +355 -2
  3. package/THIRD_PARTY_NOTICES.md +25 -0
  4. package/assets/icon.svg +4 -0
  5. package/dist/api.d.ts +7 -0
  6. package/dist/api.d.ts.map +1 -0
  7. package/dist/api.js +46 -0
  8. package/dist/api.js.map +1 -0
  9. package/dist/assets.d.ts +7 -0
  10. package/dist/assets.d.ts.map +1 -0
  11. package/dist/assets.js +52 -0
  12. package/dist/assets.js.map +1 -0
  13. package/dist/browser.d.ts +3 -0
  14. package/dist/browser.d.ts.map +1 -0
  15. package/dist/browser.js +3 -0
  16. package/dist/browser.js.map +1 -0
  17. package/dist/node.d.ts +3 -0
  18. package/dist/node.d.ts.map +1 -0
  19. package/dist/node.js +10 -0
  20. package/dist/node.js.map +1 -0
  21. package/dist/options.d.ts +24 -0
  22. package/dist/options.d.ts.map +1 -0
  23. package/dist/options.js +135 -0
  24. package/dist/options.js.map +1 -0
  25. package/dist/types.d.ts +51 -0
  26. package/dist/types.d.ts.map +1 -0
  27. package/dist/types.js +2 -0
  28. package/dist/types.js.map +1 -0
  29. package/examples/bun.ts +8 -0
  30. package/examples/german-invoice/README.md +70 -0
  31. package/examples/german-invoice/data.mjs +83 -0
  32. package/examples/german-invoice/invoice.css +59 -0
  33. package/examples/german-invoice/template.mjs +49 -0
  34. package/examples/german-invoice.mjs +20 -0
  35. package/examples/invoice.html +29 -0
  36. package/examples/node.mjs +8 -0
  37. package/examples/pagination.html +40 -0
  38. package/examples/pagination.mjs +8 -0
  39. package/licenses/DejaVu.txt +187 -0
  40. package/licenses/RUST_DEPENDENCIES.txt +11577 -0
  41. package/package.json +89 -4
  42. package/src/api.ts +44 -0
  43. package/src/assets.ts +43 -0
  44. package/src/browser.ts +6 -0
  45. package/src/node.ts +12 -0
  46. package/src/options.ts +101 -0
  47. package/src/types.ts +49 -0
  48. package/vendor/README.md +17 -0
  49. package/vendor/stylo/.cargo-ok +1 -0
  50. package/vendor/stylo/.cargo_vcs_info.json +6 -0
  51. package/vendor/stylo/Cargo.lock +1485 -0
  52. package/vendor/stylo/Cargo.toml +274 -0
  53. package/vendor/stylo/LICENSE +373 -0
  54. package/vendor/stylo/README.md +6 -0
  55. package/vendor/stylo/applicable_declarations.rs +328 -0
  56. package/vendor/stylo/author_styles.rs +79 -0
  57. package/vendor/stylo/bezier.rs +176 -0
  58. package/vendor/stylo/bloom.rs +414 -0
  59. package/vendor/stylo/build.rs +91 -0
  60. package/vendor/stylo/build_gecko.rs +393 -0
  61. package/vendor/stylo/color/color_function.rs +601 -0
  62. package/vendor/stylo/color/component.rs +207 -0
  63. package/vendor/stylo/color/convert.rs +935 -0
  64. package/vendor/stylo/color/gamut/raytrace.rs +265 -0
  65. package/vendor/stylo/color/gamut.rs +227 -0
  66. package/vendor/stylo/color/mix.rs +645 -0
  67. package/vendor/stylo/color/mod.rs +750 -0
  68. package/vendor/stylo/color/parsing.rs +770 -0
  69. package/vendor/stylo/color/to_css.rs +297 -0
  70. package/vendor/stylo/context.rs +716 -0
  71. package/vendor/stylo/counter_style/mod.rs +754 -0
  72. package/vendor/stylo/counter_style/predefined.rs +61 -0
  73. package/vendor/stylo/counter_style/update_predefined.py +35 -0
  74. package/vendor/stylo/custom_properties.rs +2023 -0
  75. package/vendor/stylo/custom_properties_map.rs +220 -0
  76. package/vendor/stylo/data.rs +693 -0
  77. package/vendor/stylo/device/gecko.rs +486 -0
  78. package/vendor/stylo/device/mod.rs +304 -0
  79. package/vendor/stylo/device/servo.rs +507 -0
  80. package/vendor/stylo/dom.rs +1164 -0
  81. package/vendor/stylo/dom_apis.rs +937 -0
  82. package/vendor/stylo/driver.rs +172 -0
  83. package/vendor/stylo/error_reporting.rs +484 -0
  84. package/vendor/stylo/font_face.rs +558 -0
  85. package/vendor/stylo/font_metrics.rs +124 -0
  86. package/vendor/stylo/gecko/anon_boxes.toml +113 -0
  87. package/vendor/stylo/gecko/arc_types.rs +211 -0
  88. package/vendor/stylo/gecko/conversions.rs +59 -0
  89. package/vendor/stylo/gecko/data.rs +222 -0
  90. package/vendor/stylo/gecko/media_features.rs +971 -0
  91. package/vendor/stylo/gecko/mod.rs +20 -0
  92. package/vendor/stylo/gecko/non_ts_pseudo_class_list.rs +119 -0
  93. package/vendor/stylo/gecko/pseudo_element.rs +640 -0
  94. package/vendor/stylo/gecko/pseudo_element_definition.mako.rs +298 -0
  95. package/vendor/stylo/gecko/pseudo_elements.py +137 -0
  96. package/vendor/stylo/gecko/pseudo_elements.toml +197 -0
  97. package/vendor/stylo/gecko/regen_pseudo_elements.py +33 -0
  98. package/vendor/stylo/gecko/restyle_damage.rs +127 -0
  99. package/vendor/stylo/gecko/selector_parser.rs +627 -0
  100. package/vendor/stylo/gecko/snapshot.rs +202 -0
  101. package/vendor/stylo/gecko/snapshot_helpers.rs +334 -0
  102. package/vendor/stylo/gecko/traversal.rs +52 -0
  103. package/vendor/stylo/gecko/wrapper.rs +2258 -0
  104. package/vendor/stylo/gecko_bindings/mod.rs +34 -0
  105. package/vendor/stylo/gecko_bindings/sugar/mod.rs +12 -0
  106. package/vendor/stylo/gecko_bindings/sugar/ns_com_ptr.rs +15 -0
  107. package/vendor/stylo/gecko_bindings/sugar/ns_compatibility.rs +19 -0
  108. package/vendor/stylo/gecko_bindings/sugar/ns_style_auto_array.rs +111 -0
  109. package/vendor/stylo/gecko_bindings/sugar/origin_flags.rs +31 -0
  110. package/vendor/stylo/gecko_bindings/sugar/ownership.rs +70 -0
  111. package/vendor/stylo/gecko_bindings/sugar/refptr.rs +299 -0
  112. package/vendor/stylo/gecko_string_cache/mod.rs +543 -0
  113. package/vendor/stylo/gecko_string_cache/namespace.rs +106 -0
  114. package/vendor/stylo/global_style_data.rs +224 -0
  115. package/vendor/stylo/invalidation/element/document_state.rs +156 -0
  116. package/vendor/stylo/invalidation/element/element_wrapper.rs +432 -0
  117. package/vendor/stylo/invalidation/element/invalidation_map.rs +1631 -0
  118. package/vendor/stylo/invalidation/element/invalidator.rs +1470 -0
  119. package/vendor/stylo/invalidation/element/mod.rs +13 -0
  120. package/vendor/stylo/invalidation/element/relative_selector.rs +1399 -0
  121. package/vendor/stylo/invalidation/element/restyle_hints.rs +226 -0
  122. package/vendor/stylo/invalidation/element/state_and_attributes.rs +756 -0
  123. package/vendor/stylo/invalidation/media_queries.rs +144 -0
  124. package/vendor/stylo/invalidation/mod.rs +10 -0
  125. package/vendor/stylo/invalidation/stylesheets.rs +745 -0
  126. package/vendor/stylo/invalidation/viewport_units.rs +71 -0
  127. package/vendor/stylo/lib.rs +336 -0
  128. package/vendor/stylo/logical_geometry.rs +1684 -0
  129. package/vendor/stylo/macros.rs +70 -0
  130. package/vendor/stylo/matching.rs +1226 -0
  131. package/vendor/stylo/media_queries/media_list.rs +168 -0
  132. package/vendor/stylo/media_queries/media_query.rs +190 -0
  133. package/vendor/stylo/media_queries/mod.rs +13 -0
  134. package/vendor/stylo/parallel.rs +205 -0
  135. package/vendor/stylo/parser.rs +276 -0
  136. package/vendor/stylo/piecewise_linear.rs +282 -0
  137. package/vendor/stylo/prefs.rs +33 -0
  138. package/vendor/stylo/properties/build.py +119 -0
  139. package/vendor/stylo/properties/cascade.rs +2704 -0
  140. package/vendor/stylo/properties/computed_value_flags.rs +206 -0
  141. package/vendor/stylo/properties/counted_unknown_properties.py +104 -0
  142. package/vendor/stylo/properties/counter_style_descriptors.toml +45 -0
  143. package/vendor/stylo/properties/data.py +1375 -0
  144. package/vendor/stylo/properties/declaration_block.rs +1788 -0
  145. package/vendor/stylo/properties/font_face_descriptors.toml +50 -0
  146. package/vendor/stylo/properties/gecko.mako.rs +1555 -0
  147. package/vendor/stylo/properties/helpers/animated_properties.mako.rs +800 -0
  148. package/vendor/stylo/properties/helpers.mako.rs +812 -0
  149. package/vendor/stylo/properties/longhands.toml +4305 -0
  150. package/vendor/stylo/properties/mod.rs +1857 -0
  151. package/vendor/stylo/properties/properties.html.mako +31 -0
  152. package/vendor/stylo/properties/properties.mako.rs +3103 -0
  153. package/vendor/stylo/properties/property_descriptors.toml +18 -0
  154. package/vendor/stylo/properties/shorthands.rs +4241 -0
  155. package/vendor/stylo/properties/shorthands.toml +575 -0
  156. package/vendor/stylo/properties/vendored_python/mako-1.3.10-py3-none-any.whl +0 -0
  157. package/vendor/stylo/properties/vendored_python/markupsafe/LICENSE.txt +28 -0
  158. package/vendor/stylo/properties/vendored_python/markupsafe/__init__.py +384 -0
  159. package/vendor/stylo/properties/vendored_python/toml-0.10.2-py2.py3-none-any.whl +0 -0
  160. package/vendor/stylo/properties/view_transition_descriptors.toml +11 -0
  161. package/vendor/stylo/properties_and_values/mod.rs +12 -0
  162. package/vendor/stylo/properties_and_values/registry.rs +65 -0
  163. package/vendor/stylo/properties_and_values/rule.rs +331 -0
  164. package/vendor/stylo/properties_and_values/syntax/ascii.rs +60 -0
  165. package/vendor/stylo/properties_and_values/syntax/data_type.rs +146 -0
  166. package/vendor/stylo/properties_and_values/syntax/mod.rs +437 -0
  167. package/vendor/stylo/properties_and_values/value.rs +758 -0
  168. package/vendor/stylo/queries/condition.rs +996 -0
  169. package/vendor/stylo/queries/feature.rs +199 -0
  170. package/vendor/stylo/queries/feature_expression.rs +1170 -0
  171. package/vendor/stylo/queries/mod.rs +19 -0
  172. package/vendor/stylo/queries/values.rs +46 -0
  173. package/vendor/stylo/rule_cache.rs +267 -0
  174. package/vendor/stylo/rule_collector.rs +554 -0
  175. package/vendor/stylo/rule_tree/core.rs +787 -0
  176. package/vendor/stylo/rule_tree/level.rs +396 -0
  177. package/vendor/stylo/rule_tree/map.rs +201 -0
  178. package/vendor/stylo/rule_tree/mod.rs +427 -0
  179. package/vendor/stylo/rule_tree/source.rs +80 -0
  180. package/vendor/stylo/rule_tree/unsafe_box.rs +78 -0
  181. package/vendor/stylo/scoped_tls.rs +80 -0
  182. package/vendor/stylo/selector_map.rs +1050 -0
  183. package/vendor/stylo/selector_parser.rs +305 -0
  184. package/vendor/stylo/servo/animation.rs +1922 -0
  185. package/vendor/stylo/servo/attr.rs +714 -0
  186. package/vendor/stylo/servo/encoding_support.rs +81 -0
  187. package/vendor/stylo/servo/media_features.rs +254 -0
  188. package/vendor/stylo/servo/mod.rs +16 -0
  189. package/vendor/stylo/servo/restyle_damage.rs +215 -0
  190. package/vendor/stylo/servo/selector_parser.rs +1036 -0
  191. package/vendor/stylo/servo/shadow_parts.rs +207 -0
  192. package/vendor/stylo/shared_lock.rs +271 -0
  193. package/vendor/stylo/sharing/checks.rs +281 -0
  194. package/vendor/stylo/sharing/mod.rs +1000 -0
  195. package/vendor/stylo/simple_buckets_map.rs +52 -0
  196. package/vendor/stylo/str.rs +168 -0
  197. package/vendor/stylo/style_adjuster.rs +1086 -0
  198. package/vendor/stylo/style_resolver.rs +658 -0
  199. package/vendor/stylo/stylesheet_set.rs +658 -0
  200. package/vendor/stylo/stylesheets/appearance_base_rule.rs +51 -0
  201. package/vendor/stylo/stylesheets/container_rule.rs +769 -0
  202. package/vendor/stylo/stylesheets/counter_style_rule.rs +7 -0
  203. package/vendor/stylo/stylesheets/document_rule.rs +272 -0
  204. package/vendor/stylo/stylesheets/font_face_rule.rs +7 -0
  205. package/vendor/stylo/stylesheets/font_feature_values_rule.rs +481 -0
  206. package/vendor/stylo/stylesheets/font_palette_values_rule.rs +304 -0
  207. package/vendor/stylo/stylesheets/import_rule.rs +259 -0
  208. package/vendor/stylo/stylesheets/keyframes_rule.rs +733 -0
  209. package/vendor/stylo/stylesheets/layer_rule.rs +221 -0
  210. package/vendor/stylo/stylesheets/loader.rs +29 -0
  211. package/vendor/stylo/stylesheets/margin_rule.rs +190 -0
  212. package/vendor/stylo/stylesheets/media_rule.rs +191 -0
  213. package/vendor/stylo/stylesheets/mod.rs +918 -0
  214. package/vendor/stylo/stylesheets/namespace_rule.rs +43 -0
  215. package/vendor/stylo/stylesheets/nested_declarations_rule.rs +51 -0
  216. package/vendor/stylo/stylesheets/origin.rs +249 -0
  217. package/vendor/stylo/stylesheets/page_rule.rs +354 -0
  218. package/vendor/stylo/stylesheets/position_try_rule.rs +68 -0
  219. package/vendor/stylo/stylesheets/property_rule.rs +5 -0
  220. package/vendor/stylo/stylesheets/rule_list.rs +167 -0
  221. package/vendor/stylo/stylesheets/rule_parser.rs +1189 -0
  222. package/vendor/stylo/stylesheets/rules_iterator.rs +397 -0
  223. package/vendor/stylo/stylesheets/scope_rule.rs +511 -0
  224. package/vendor/stylo/stylesheets/starting_style_rule.rs +54 -0
  225. package/vendor/stylo/stylesheets/style_rule.rs +80 -0
  226. package/vendor/stylo/stylesheets/stylesheet.rs +570 -0
  227. package/vendor/stylo/stylesheets/supports_rule.rs +486 -0
  228. package/vendor/stylo/stylesheets/view_transition_rule.rs +81 -0
  229. package/vendor/stylo/stylist.rs +5076 -0
  230. package/vendor/stylo/thread_state.rs +87 -0
  231. package/vendor/stylo/traversal.rs +765 -0
  232. package/vendor/stylo/traversal_flags.rs +68 -0
  233. package/vendor/stylo/typed_om/mod.rs +981 -0
  234. package/vendor/stylo/typed_om/numeric.rs +128 -0
  235. package/vendor/stylo/typed_om/numeric_declaration.rs +93 -0
  236. package/vendor/stylo/typed_om/numeric_type.rs +640 -0
  237. package/vendor/stylo/typed_om/sum_value.rs +426 -0
  238. package/vendor/stylo/url/gecko.rs +367 -0
  239. package/vendor/stylo/url/mod.rs +71 -0
  240. package/vendor/stylo/url/servo.rs +239 -0
  241. package/vendor/stylo/use_counters/mod.rs +137 -0
  242. package/vendor/stylo/values/animated/color.rs +103 -0
  243. package/vendor/stylo/values/animated/effects.rs +26 -0
  244. package/vendor/stylo/values/animated/font.rs +37 -0
  245. package/vendor/stylo/values/animated/grid.rs +161 -0
  246. package/vendor/stylo/values/animated/lists.rs +141 -0
  247. package/vendor/stylo/values/animated/mod.rs +550 -0
  248. package/vendor/stylo/values/animated/svg.rs +46 -0
  249. package/vendor/stylo/values/animated/text.rs +58 -0
  250. package/vendor/stylo/values/animated/transform.rs +1592 -0
  251. package/vendor/stylo/values/computed/align.rs +90 -0
  252. package/vendor/stylo/values/computed/angle.rs +130 -0
  253. package/vendor/stylo/values/computed/animation.rs +124 -0
  254. package/vendor/stylo/values/computed/background.rs +13 -0
  255. package/vendor/stylo/values/computed/basic_shape.rs +267 -0
  256. package/vendor/stylo/values/computed/border.rs +163 -0
  257. package/vendor/stylo/values/computed/box.rs +267 -0
  258. package/vendor/stylo/values/computed/calc.rs +334 -0
  259. package/vendor/stylo/values/computed/color.rs +161 -0
  260. package/vendor/stylo/values/computed/column.rs +11 -0
  261. package/vendor/stylo/values/computed/corner_shape.rs +155 -0
  262. package/vendor/stylo/values/computed/counters.rs +26 -0
  263. package/vendor/stylo/values/computed/easing.rs +109 -0
  264. package/vendor/stylo/values/computed/effects.rs +30 -0
  265. package/vendor/stylo/values/computed/flex.rs +19 -0
  266. package/vendor/stylo/values/computed/font.rs +1517 -0
  267. package/vendor/stylo/values/computed/image.rs +273 -0
  268. package/vendor/stylo/values/computed/length.rs +576 -0
  269. package/vendor/stylo/values/computed/length_percentage.rs +1055 -0
  270. package/vendor/stylo/values/computed/list.rs +16 -0
  271. package/vendor/stylo/values/computed/mod.rs +1141 -0
  272. package/vendor/stylo/values/computed/motion.rs +73 -0
  273. package/vendor/stylo/values/computed/outline.rs +7 -0
  274. package/vendor/stylo/values/computed/page.rs +77 -0
  275. package/vendor/stylo/values/computed/param.rs +9 -0
  276. package/vendor/stylo/values/computed/percentage.rs +143 -0
  277. package/vendor/stylo/values/computed/position.rs +273 -0
  278. package/vendor/stylo/values/computed/ratio.rs +88 -0
  279. package/vendor/stylo/values/computed/rect.rs +19 -0
  280. package/vendor/stylo/values/computed/resolution.rs +76 -0
  281. package/vendor/stylo/values/computed/svg.rs +66 -0
  282. package/vendor/stylo/values/computed/table.rs +7 -0
  283. package/vendor/stylo/values/computed/text.rs +199 -0
  284. package/vendor/stylo/values/computed/time.rs +83 -0
  285. package/vendor/stylo/values/computed/transform.rs +559 -0
  286. package/vendor/stylo/values/computed/tree_counting.rs +30 -0
  287. package/vendor/stylo/values/computed/ui.rs +23 -0
  288. package/vendor/stylo/values/computed/url.rs +12 -0
  289. package/vendor/stylo/values/distance.rs +139 -0
  290. package/vendor/stylo/values/generics/animation.rs +363 -0
  291. package/vendor/stylo/values/generics/background.rs +65 -0
  292. package/vendor/stylo/values/generics/basic_shape.rs +1389 -0
  293. package/vendor/stylo/values/generics/border.rs +303 -0
  294. package/vendor/stylo/values/generics/box.rs +552 -0
  295. package/vendor/stylo/values/generics/calc.rs +2986 -0
  296. package/vendor/stylo/values/generics/color.rs +318 -0
  297. package/vendor/stylo/values/generics/column.rs +44 -0
  298. package/vendor/stylo/values/generics/counters.rs +320 -0
  299. package/vendor/stylo/values/generics/easing.rs +132 -0
  300. package/vendor/stylo/values/generics/effects.rs +131 -0
  301. package/vendor/stylo/values/generics/flex.rs +34 -0
  302. package/vendor/stylo/values/generics/font.rs +376 -0
  303. package/vendor/stylo/values/generics/grid.rs +962 -0
  304. package/vendor/stylo/values/generics/image.rs +653 -0
  305. package/vendor/stylo/values/generics/length.rs +656 -0
  306. package/vendor/stylo/values/generics/mod.rs +303 -0
  307. package/vendor/stylo/values/generics/motion.rs +274 -0
  308. package/vendor/stylo/values/generics/page.rs +167 -0
  309. package/vendor/stylo/values/generics/position.rs +669 -0
  310. package/vendor/stylo/values/generics/ratio.rs +88 -0
  311. package/vendor/stylo/values/generics/rect.rs +147 -0
  312. package/vendor/stylo/values/generics/size.rs +102 -0
  313. package/vendor/stylo/values/generics/svg.rs +226 -0
  314. package/vendor/stylo/values/generics/text.rs +268 -0
  315. package/vendor/stylo/values/generics/transform.rs +1157 -0
  316. package/vendor/stylo/values/generics/ui.rs +147 -0
  317. package/vendor/stylo/values/generics/url.rs +51 -0
  318. package/vendor/stylo/values/mod.rs +820 -0
  319. package/vendor/stylo/values/resolved/animation.rs +33 -0
  320. package/vendor/stylo/values/resolved/color.rs +53 -0
  321. package/vendor/stylo/values/resolved/counters.rs +45 -0
  322. package/vendor/stylo/values/resolved/mod.rs +316 -0
  323. package/vendor/stylo/values/specified/align.rs +711 -0
  324. package/vendor/stylo/values/specified/angle.rs +407 -0
  325. package/vendor/stylo/values/specified/animation.rs +921 -0
  326. package/vendor/stylo/values/specified/background.rs +270 -0
  327. package/vendor/stylo/values/specified/basic_shape.rs +986 -0
  328. package/vendor/stylo/values/specified/border.rs +430 -0
  329. package/vendor/stylo/values/specified/box.rs +2231 -0
  330. package/vendor/stylo/values/specified/calc.rs +1657 -0
  331. package/vendor/stylo/values/specified/color.rs +1210 -0
  332. package/vendor/stylo/values/specified/column.rs +11 -0
  333. package/vendor/stylo/values/specified/corner_shape.rs +152 -0
  334. package/vendor/stylo/values/specified/counters.rs +242 -0
  335. package/vendor/stylo/values/specified/easing.rs +225 -0
  336. package/vendor/stylo/values/specified/effects.rs +421 -0
  337. package/vendor/stylo/values/specified/flex.rs +42 -0
  338. package/vendor/stylo/values/specified/font.rs +2103 -0
  339. package/vendor/stylo/values/specified/frequency.rs +39 -0
  340. package/vendor/stylo/values/specified/grid.rs +407 -0
  341. package/vendor/stylo/values/specified/image.rs +1385 -0
  342. package/vendor/stylo/values/specified/intersection_observer.rs +80 -0
  343. package/vendor/stylo/values/specified/length.rs +2024 -0
  344. package/vendor/stylo/values/specified/list.rs +190 -0
  345. package/vendor/stylo/values/specified/mod.rs +531 -0
  346. package/vendor/stylo/values/specified/motion.rs +316 -0
  347. package/vendor/stylo/values/specified/number.rs +603 -0
  348. package/vendor/stylo/values/specified/outline.rs +69 -0
  349. package/vendor/stylo/values/specified/page.rs +94 -0
  350. package/vendor/stylo/values/specified/param.rs +121 -0
  351. package/vendor/stylo/values/specified/percentage.rs +364 -0
  352. package/vendor/stylo/values/specified/position.rs +2247 -0
  353. package/vendor/stylo/values/specified/ratio.rs +29 -0
  354. package/vendor/stylo/values/specified/rect.rs +28 -0
  355. package/vendor/stylo/values/specified/resolution.rs +237 -0
  356. package/vendor/stylo/values/specified/source_size_list.rs +135 -0
  357. package/vendor/stylo/values/specified/svg.rs +427 -0
  358. package/vendor/stylo/values/specified/svg_path.rs +1115 -0
  359. package/vendor/stylo/values/specified/table.rs +38 -0
  360. package/vendor/stylo/values/specified/text.rs +1561 -0
  361. package/vendor/stylo/values/specified/time.rs +313 -0
  362. package/vendor/stylo/values/specified/transform.rs +599 -0
  363. package/vendor/stylo/values/specified/tree_counting.rs +32 -0
  364. package/vendor/stylo/values/specified/ui.rs +344 -0
  365. package/vendor/stylo/values/specified/url.rs +12 -0
  366. package/vendor/stylo/values/tagged_numeric.rs +253 -0
  367. package/wasm/blitz_pdf.d.ts +67 -0
  368. package/wasm/blitz_pdf.js +699 -0
  369. package/wasm/blitz_pdf_bg.wasm +0 -0
  370. package/wasm/blitz_pdf_bg.wasm.d.ts +20 -0
@@ -0,0 +1,1115 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ //! Specified types for SVG Path.
6
+
7
+ use crate::derives::*;
8
+ use crate::parser::{Parse, ParserContext};
9
+ use crate::values::animated::{lists, Animate, Procedure};
10
+ use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
11
+ use crate::values::generics::basic_shape::GenericShapeCommand;
12
+ use crate::values::generics::basic_shape::{
13
+ ArcRadii, ArcSize, ArcSweep, AxisEndPoint, AxisPosition, CommandEndPoint, ControlPoint,
14
+ ControlReference, CoordinatePair, RelativeControlPoint,
15
+ };
16
+ use crate::values::generics::position::GenericPosition;
17
+ use crate::values::CSSFloat;
18
+ use cssparser::Parser;
19
+ use std::fmt::{self, Write};
20
+ use std::iter::{Cloned, Peekable};
21
+ use std::ops;
22
+ use std::slice;
23
+ use style_traits::values::SequenceWriter;
24
+ use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
25
+
26
+ /// Whether to allow empty string in the parser.
27
+ #[derive(Clone, Debug, Eq, PartialEq)]
28
+ #[allow(missing_docs)]
29
+ pub enum AllowEmpty {
30
+ Yes,
31
+ No,
32
+ }
33
+
34
+ /// The SVG path data.
35
+ ///
36
+ /// https://www.w3.org/TR/SVG11/paths.html#PathData
37
+ #[derive(
38
+ Clone,
39
+ Debug,
40
+ Deserialize,
41
+ MallocSizeOf,
42
+ PartialEq,
43
+ Serialize,
44
+ SpecifiedValueInfo,
45
+ ToAnimatedZero,
46
+ ToComputedValue,
47
+ ToResolvedValue,
48
+ ToShmem,
49
+ )]
50
+ #[repr(C)]
51
+ pub struct SVGPathData(
52
+ // TODO(emilio): Should probably measure this somehow only from the
53
+ // specified values.
54
+ #[ignore_malloc_size_of = "Arc"] pub crate::ArcSlice<PathCommand>,
55
+ );
56
+
57
+ impl SVGPathData {
58
+ /// Get the array of PathCommand.
59
+ #[inline]
60
+ pub fn commands(&self) -> &[PathCommand] {
61
+ &self.0
62
+ }
63
+
64
+ /// Create a normalized copy of this path by converting each relative
65
+ /// command to an absolute command.
66
+ pub fn normalize(&self, reduce: bool) -> Self {
67
+ let mut state = PathTraversalState {
68
+ subpath_start: CoordPair::new(0.0, 0.0),
69
+ pos: CoordPair::new(0.0, 0.0),
70
+ last_command: PathCommand::Close,
71
+ last_control: CoordPair::new(0.0, 0.0),
72
+ };
73
+ let iter = self.0.iter().map(|seg| seg.normalize(&mut state, reduce));
74
+ SVGPathData(crate::ArcSlice::from_iter(iter))
75
+ }
76
+
77
+ /// Parse this SVG path string with the argument that indicates whether we should allow the
78
+ /// empty string.
79
+ // We cannot use cssparser::Parser to parse a SVG path string because the spec wants to make
80
+ // the SVG path string as compact as possible. (i.e. The whitespaces may be dropped.)
81
+ // e.g. "M100 200L100 200" is a valid SVG path string. If we use tokenizer, the first ident
82
+ // is "M100", instead of "M", and this is not correct. Therefore, we use a Peekable
83
+ // str::Char iterator to check each character.
84
+ //
85
+ // css-shapes-1 says a path data string that does conform but defines an empty path is
86
+ // invalid and causes the entire path() to be invalid, so we use allow_empty to decide
87
+ // whether we should allow it.
88
+ // https://drafts.csswg.org/css-shapes-1/#typedef-basic-shape
89
+ pub fn parse(input: &mut Parser, allow_empty: AllowEmpty) -> Result<Self, ParseError> {
90
+ let path_string = input.expect_string()?.as_ref();
91
+ let (path, ok) = Self::parse_bytes(path_string.as_bytes());
92
+ if !ok || (allow_empty == AllowEmpty::No && path.0.is_empty()) {
93
+ return Err(ParseError::custom(StyleParseErrorKind::UnspecifiedError));
94
+ }
95
+ Ok(path)
96
+ }
97
+
98
+ /// As above, but just parsing the raw byte stream.
99
+ ///
100
+ /// Returns the (potentially empty or partial) path, and whether the parsing was ok or we found
101
+ /// an error. The API is a bit weird because some SVG callers require "parse until first error"
102
+ /// behavior.
103
+ pub fn parse_bytes(input: &[u8]) -> (Self, bool) {
104
+ // Parse the svg path string as multiple sub-paths.
105
+ let mut ok = true;
106
+ let mut path_parser = PathParser::new(input);
107
+
108
+ while skip_wsp(&mut path_parser.chars) {
109
+ if path_parser.parse_subpath().is_err() {
110
+ ok = false;
111
+ break;
112
+ }
113
+ }
114
+
115
+ let path = Self(crate::ArcSlice::from_iter(path_parser.path.into_iter()));
116
+ (path, ok)
117
+ }
118
+
119
+ /// Serializes to the path string, potentially including quotes.
120
+ pub fn to_css<W>(&self, dest: &mut CssWriter<W>, quote: bool) -> fmt::Result
121
+ where
122
+ W: fmt::Write,
123
+ {
124
+ if quote {
125
+ dest.write_char('"')?;
126
+ }
127
+ let mut writer = SequenceWriter::new(dest, " ");
128
+ for command in self.commands() {
129
+ writer.write_item(|inner| command.to_css_for_svg(inner))?;
130
+ }
131
+ if quote {
132
+ dest.write_char('"')?;
133
+ }
134
+ Ok(())
135
+ }
136
+ }
137
+
138
+ impl ToCss for SVGPathData {
139
+ #[inline]
140
+ fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
141
+ where
142
+ W: fmt::Write,
143
+ {
144
+ self.to_css(dest, /* quote = */ true)
145
+ }
146
+ }
147
+
148
+ impl Parse for SVGPathData {
149
+ fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
150
+ // Note that the EBNF allows the path data string in the d property to be empty, so we
151
+ // don't reject empty SVG path data.
152
+ // https://svgwg.org/svg2-draft/single-page.html#paths-PathDataBNF
153
+ SVGPathData::parse(input, AllowEmpty::Yes)
154
+ }
155
+ }
156
+
157
+ impl Animate for SVGPathData {
158
+ fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
159
+ if self.0.len() != other.0.len() {
160
+ return Err(());
161
+ }
162
+
163
+ // FIXME(emilio): This allocates three copies of the path, that's not
164
+ // great! Specially, once we're normalized once, we don't need to
165
+ // re-normalize again.
166
+ let left = self.normalize(false);
167
+ let right = other.normalize(false);
168
+
169
+ let items: Vec<_> = lists::by_computed_value::animate(&left.0, &right.0, procedure)?;
170
+ Ok(SVGPathData(crate::ArcSlice::from_iter(items.into_iter())))
171
+ }
172
+ }
173
+
174
+ impl ComputeSquaredDistance for SVGPathData {
175
+ fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
176
+ if self.0.len() != other.0.len() {
177
+ return Err(());
178
+ }
179
+ let left = self.normalize(false);
180
+ let right = other.normalize(false);
181
+ lists::by_computed_value::squared_distance(&left.0, &right.0)
182
+ }
183
+ }
184
+
185
+ /// A position type for SVG path coordinates (just a pair of floats).
186
+ pub type SVGPathPosition = GenericPosition<CSSFloat, CSSFloat>;
187
+
188
+ /// The SVG path command.
189
+ /// The fields of these commands are self-explanatory, so we skip the documents.
190
+ /// Note: the index of the control points, e.g. control1, control2, are mapping to the control
191
+ /// points of the Bézier curve in the spec.
192
+ ///
193
+ /// https://www.w3.org/TR/SVG11/paths.html#PathData
194
+ pub type PathCommand = GenericShapeCommand<CSSFloat, SVGPathPosition, CSSFloat>;
195
+
196
+ /// For internal SVGPath normalization.
197
+ #[allow(missing_docs)]
198
+ struct PathTraversalState {
199
+ subpath_start: CoordPair,
200
+ pos: CoordPair,
201
+ last_command: PathCommand,
202
+ last_control: CoordPair,
203
+ }
204
+
205
+ impl PathCommand {
206
+ /// Create a normalized copy of this PathCommand. Absolute commands will be copied as-is while
207
+ /// for relative commands an equivalent absolute command will be returned.
208
+ ///
209
+ /// See discussion: https://github.com/w3c/svgwg/issues/321
210
+ /// If reduce is true then the path will be restricted to
211
+ /// "M", "L", "C", "A" and "Z" commands.
212
+ fn normalize(&self, state: &mut PathTraversalState, reduce: bool) -> Self {
213
+ use crate::values::generics::basic_shape::GenericShapeCommand::*;
214
+ match *self {
215
+ Close => {
216
+ state.pos = state.subpath_start;
217
+ if reduce {
218
+ state.last_command = *self;
219
+ }
220
+ Close
221
+ },
222
+ Move { mut point } => {
223
+ point = point.to_abs(state.pos);
224
+ state.pos = point.into();
225
+ state.subpath_start = point.into();
226
+ if reduce {
227
+ state.last_command = *self;
228
+ }
229
+ Move { point }
230
+ },
231
+ Line { mut point } => {
232
+ point = point.to_abs(state.pos);
233
+ state.pos = point.into();
234
+ if reduce {
235
+ state.last_command = *self;
236
+ }
237
+ Line { point }
238
+ },
239
+ HLine { mut x } => {
240
+ x = x.to_abs(state.pos.x);
241
+ state.pos.x = x.into();
242
+ if reduce {
243
+ state.last_command = *self;
244
+ PathCommand::Line {
245
+ point: CommandEndPoint::ToPosition(state.pos.into()),
246
+ }
247
+ } else {
248
+ HLine { x }
249
+ }
250
+ },
251
+ VLine { mut y } => {
252
+ y = y.to_abs(state.pos.y);
253
+ state.pos.y = y.into();
254
+ if reduce {
255
+ state.last_command = *self;
256
+ PathCommand::Line {
257
+ point: CommandEndPoint::ToPosition(state.pos.into()),
258
+ }
259
+ } else {
260
+ VLine { y }
261
+ }
262
+ },
263
+ CubicCurve {
264
+ mut point,
265
+ mut control1,
266
+ mut control2,
267
+ } => {
268
+ control1 = control1.to_abs(state.pos, point);
269
+ control2 = control2.to_abs(state.pos, point);
270
+ point = point.to_abs(state.pos);
271
+ state.pos = point.into();
272
+ if reduce {
273
+ state.last_command = *self;
274
+ state.last_control = control2.into();
275
+ }
276
+ CubicCurve {
277
+ point,
278
+ control1,
279
+ control2,
280
+ }
281
+ },
282
+ QuadCurve {
283
+ mut point,
284
+ mut control1,
285
+ } => {
286
+ control1 = control1.to_abs(state.pos, point);
287
+ point = point.to_abs(state.pos);
288
+ if reduce {
289
+ let c1 = state.pos + 2. * (CoordPair::from(control1) - state.pos) / 3.;
290
+ let control2 = CoordPair::from(point)
291
+ + 2. * (CoordPair::from(control1) - point.into()) / 3.;
292
+ state.pos = point.into();
293
+ state.last_command = *self;
294
+ state.last_control = control1.into();
295
+ CubicCurve {
296
+ point,
297
+ control1: ControlPoint::Absolute(c1.into()),
298
+ control2: ControlPoint::Absolute(control2.into()),
299
+ }
300
+ } else {
301
+ state.pos = point.into();
302
+ QuadCurve { point, control1 }
303
+ }
304
+ },
305
+ SmoothCubic {
306
+ mut point,
307
+ mut control2,
308
+ } => {
309
+ control2 = control2.to_abs(state.pos, point);
310
+ point = point.to_abs(state.pos);
311
+ if reduce {
312
+ let control1 = match state.last_command {
313
+ PathCommand::CubicCurve {
314
+ point: _,
315
+ control1: _,
316
+ control2: _,
317
+ }
318
+ | PathCommand::SmoothCubic {
319
+ point: _,
320
+ control2: _,
321
+ } => state.pos + state.pos - state.last_control,
322
+ _ => state.pos,
323
+ };
324
+ state.pos = point.into();
325
+ state.last_control = control2.into();
326
+ state.last_command = *self;
327
+ CubicCurve {
328
+ point,
329
+ control1: ControlPoint::Absolute(control1.into()),
330
+ control2,
331
+ }
332
+ } else {
333
+ state.pos = point.into();
334
+ SmoothCubic { point, control2 }
335
+ }
336
+ },
337
+ SmoothQuad { mut point } => {
338
+ point = point.to_abs(state.pos);
339
+ if reduce {
340
+ let control = match state.last_command {
341
+ PathCommand::QuadCurve {
342
+ point: _,
343
+ control1: _,
344
+ }
345
+ | PathCommand::SmoothQuad { point: _ } => {
346
+ state.pos + state.pos - state.last_control
347
+ },
348
+ _ => state.pos,
349
+ };
350
+ let control1 = state.pos + 2. * (control - state.pos) / 3.;
351
+ let control2 = CoordPair::from(point) + 2. * (control - point.into()) / 3.;
352
+ state.pos = point.into();
353
+ state.last_command = *self;
354
+ state.last_control = control;
355
+ CubicCurve {
356
+ point,
357
+ control1: ControlPoint::Absolute(control1.into()),
358
+ control2: ControlPoint::Absolute(control2.into()),
359
+ }
360
+ } else {
361
+ state.pos = point.into();
362
+ SmoothQuad { point }
363
+ }
364
+ },
365
+ Arc {
366
+ mut point,
367
+ radii,
368
+ arc_sweep,
369
+ arc_size,
370
+ rotate,
371
+ } => {
372
+ point = point.to_abs(state.pos);
373
+ state.pos = point.into();
374
+ if reduce {
375
+ state.last_command = *self;
376
+ if radii.rx == 0. && radii.ry.as_ref().is_none_or(|v| *v == 0.) {
377
+ let end_point = CoordPair::from(point);
378
+ CubicCurve {
379
+ point: CommandEndPoint::ToPosition(state.pos.into()),
380
+ control1: ControlPoint::Absolute(end_point.into()),
381
+ control2: ControlPoint::Absolute(end_point.into()),
382
+ }
383
+ } else {
384
+ Arc {
385
+ point,
386
+ radii,
387
+ arc_sweep,
388
+ arc_size,
389
+ rotate,
390
+ }
391
+ }
392
+ } else {
393
+ Arc {
394
+ point,
395
+ radii,
396
+ arc_sweep,
397
+ arc_size,
398
+ rotate,
399
+ }
400
+ }
401
+ },
402
+ }
403
+ }
404
+
405
+ /// The serialization of the svg path.
406
+ fn to_css_for_svg<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
407
+ where
408
+ W: fmt::Write,
409
+ {
410
+ use crate::values::generics::basic_shape::GenericShapeCommand::*;
411
+ match *self {
412
+ Close => dest.write_char('Z'),
413
+ Move { point } => {
414
+ dest.write_char(if point.is_abs() { 'M' } else { 'm' })?;
415
+ dest.write_char(' ')?;
416
+ CoordPair::from(point).to_css(dest)
417
+ },
418
+ Line { point } => {
419
+ dest.write_char(if point.is_abs() { 'L' } else { 'l' })?;
420
+ dest.write_char(' ')?;
421
+ CoordPair::from(point).to_css(dest)
422
+ },
423
+ CubicCurve {
424
+ point,
425
+ control1,
426
+ control2,
427
+ } => {
428
+ dest.write_char(if point.is_abs() { 'C' } else { 'c' })?;
429
+ dest.write_char(' ')?;
430
+ control1.to_css(dest, point.is_abs())?;
431
+ dest.write_char(' ')?;
432
+ control2.to_css(dest, point.is_abs())?;
433
+ dest.write_char(' ')?;
434
+ CoordPair::from(point).to_css(dest)
435
+ },
436
+ QuadCurve { point, control1 } => {
437
+ dest.write_char(if point.is_abs() { 'Q' } else { 'q' })?;
438
+ dest.write_char(' ')?;
439
+ control1.to_css(dest, point.is_abs())?;
440
+ dest.write_char(' ')?;
441
+ CoordPair::from(point).to_css(dest)
442
+ },
443
+ Arc {
444
+ point,
445
+ radii,
446
+ arc_sweep,
447
+ arc_size,
448
+ rotate,
449
+ } => {
450
+ dest.write_char(if point.is_abs() { 'A' } else { 'a' })?;
451
+ dest.write_char(' ')?;
452
+ radii.to_css(dest)?;
453
+ dest.write_char(' ')?;
454
+ rotate.to_css(dest)?;
455
+ dest.write_char(' ')?;
456
+ (arc_size as i32).to_css(dest)?;
457
+ dest.write_char(' ')?;
458
+ (arc_sweep as i32).to_css(dest)?;
459
+ dest.write_char(' ')?;
460
+ CoordPair::from(point).to_css(dest)
461
+ },
462
+ HLine { x } => {
463
+ dest.write_char(if x.is_abs() { 'H' } else { 'h' })?;
464
+ dest.write_char(' ')?;
465
+ CSSFloat::from(x).to_css(dest)
466
+ },
467
+ VLine { y } => {
468
+ dest.write_char(if y.is_abs() { 'V' } else { 'v' })?;
469
+ dest.write_char(' ')?;
470
+ CSSFloat::from(y).to_css(dest)
471
+ },
472
+ SmoothCubic { point, control2 } => {
473
+ dest.write_char(if point.is_abs() { 'S' } else { 's' })?;
474
+ dest.write_char(' ')?;
475
+ control2.to_css(dest, point.is_abs())?;
476
+ dest.write_char(' ')?;
477
+ CoordPair::from(point).to_css(dest)
478
+ },
479
+ SmoothQuad { point } => {
480
+ dest.write_char(if point.is_abs() { 'T' } else { 't' })?;
481
+ dest.write_char(' ')?;
482
+ CoordPair::from(point).to_css(dest)
483
+ },
484
+ }
485
+ }
486
+ }
487
+
488
+ /// The path coord type.
489
+ pub type CoordPair = CoordinatePair<CSSFloat>;
490
+
491
+ impl ops::Add<CoordPair> for CoordPair {
492
+ type Output = CoordPair;
493
+
494
+ fn add(self, rhs: CoordPair) -> CoordPair {
495
+ Self {
496
+ x: self.x + rhs.x,
497
+ y: self.y + rhs.y,
498
+ }
499
+ }
500
+ }
501
+
502
+ impl ops::Sub<CoordPair> for CoordPair {
503
+ type Output = CoordPair;
504
+
505
+ fn sub(self, rhs: CoordPair) -> CoordPair {
506
+ Self {
507
+ x: self.x - rhs.x,
508
+ y: self.y - rhs.y,
509
+ }
510
+ }
511
+ }
512
+
513
+ impl ops::Mul<CSSFloat> for CoordPair {
514
+ type Output = CoordPair;
515
+
516
+ fn mul(self, f: CSSFloat) -> CoordPair {
517
+ Self {
518
+ x: self.x * f,
519
+ y: self.y * f,
520
+ }
521
+ }
522
+ }
523
+
524
+ impl ops::Mul<CoordPair> for CSSFloat {
525
+ type Output = CoordPair;
526
+
527
+ fn mul(self, rhs: CoordPair) -> CoordPair {
528
+ rhs * self
529
+ }
530
+ }
531
+
532
+ impl ops::Div<CSSFloat> for CoordPair {
533
+ type Output = CoordPair;
534
+
535
+ fn div(self, f: CSSFloat) -> CoordPair {
536
+ Self {
537
+ x: self.x / f,
538
+ y: self.y / f,
539
+ }
540
+ }
541
+ }
542
+
543
+ impl CommandEndPoint<SVGPathPosition, CSSFloat> {
544
+ /// Converts <command-end-point> into absolutely positioned type.
545
+ pub fn to_abs(self, state_pos: CoordPair) -> Self {
546
+ // Consume self value.
547
+ match self {
548
+ CommandEndPoint::ToPosition(_) => self,
549
+ CommandEndPoint::ByCoordinate(coord) => {
550
+ let pos = GenericPosition {
551
+ horizontal: coord.x + state_pos.x,
552
+ vertical: coord.y + state_pos.y,
553
+ };
554
+ CommandEndPoint::ToPosition(pos)
555
+ },
556
+ }
557
+ }
558
+ }
559
+
560
+ impl AxisEndPoint<CSSFloat> {
561
+ /// Converts possibly relative end point into absolutely positioned type.
562
+ pub fn to_abs(self, base: CSSFloat) -> AxisEndPoint<CSSFloat> {
563
+ // Consume self value.
564
+ match self {
565
+ AxisEndPoint::ToPosition(_) => self,
566
+ AxisEndPoint::ByCoordinate(coord) => {
567
+ AxisEndPoint::ToPosition(AxisPosition::LengthPercent(coord + base))
568
+ },
569
+ }
570
+ }
571
+ }
572
+
573
+ impl ControlPoint<SVGPathPosition, CSSFloat> {
574
+ /// Converts <control-point> into absolutely positioned control point type.
575
+ pub fn to_abs(
576
+ self,
577
+ state_pos: CoordPair,
578
+ end_point: CommandEndPoint<SVGPathPosition, CSSFloat>,
579
+ ) -> Self {
580
+ // Consume self value.
581
+ match self {
582
+ ControlPoint::Absolute(_) => self,
583
+ ControlPoint::Relative(point) => {
584
+ let mut pos = GenericPosition {
585
+ horizontal: point.coord.x,
586
+ vertical: point.coord.y,
587
+ };
588
+
589
+ match point.reference {
590
+ ControlReference::Start => {
591
+ pos.horizontal += state_pos.x;
592
+ pos.vertical += state_pos.y;
593
+ },
594
+ ControlReference::End => {
595
+ let end = CoordPair::from(end_point);
596
+ pos.horizontal += end.x;
597
+ pos.vertical += end.y;
598
+ },
599
+ _ => (),
600
+ }
601
+ ControlPoint::Absolute(pos)
602
+ },
603
+ }
604
+ }
605
+ }
606
+
607
+ impl From<CommandEndPoint<SVGPathPosition, CSSFloat>> for CoordPair {
608
+ #[inline]
609
+ fn from(p: CommandEndPoint<SVGPathPosition, CSSFloat>) -> Self {
610
+ match p {
611
+ CommandEndPoint::ToPosition(pos) => CoordPair {
612
+ x: pos.horizontal,
613
+ y: pos.vertical,
614
+ },
615
+ CommandEndPoint::ByCoordinate(coord) => coord,
616
+ }
617
+ }
618
+ }
619
+
620
+ impl From<ControlPoint<SVGPathPosition, CSSFloat>> for CoordPair {
621
+ #[inline]
622
+ fn from(point: ControlPoint<SVGPathPosition, CSSFloat>) -> Self {
623
+ match point {
624
+ ControlPoint::Absolute(pos) => CoordPair {
625
+ x: pos.horizontal,
626
+ y: pos.vertical,
627
+ },
628
+ ControlPoint::Relative(_) => {
629
+ panic!(
630
+ "Attempted to convert a relative ControlPoint to CoordPair, which is lossy. \
631
+ Consider converting it to absolute type first using `.to_abs()`."
632
+ )
633
+ },
634
+ }
635
+ }
636
+ }
637
+
638
+ impl From<CoordPair> for CommandEndPoint<SVGPathPosition, CSSFloat> {
639
+ #[inline]
640
+ fn from(coord: CoordPair) -> Self {
641
+ CommandEndPoint::ByCoordinate(coord)
642
+ }
643
+ }
644
+
645
+ impl From<CoordPair> for SVGPathPosition {
646
+ #[inline]
647
+ fn from(coord: CoordPair) -> Self {
648
+ GenericPosition {
649
+ horizontal: coord.x,
650
+ vertical: coord.y,
651
+ }
652
+ }
653
+ }
654
+
655
+ impl From<AxisEndPoint<CSSFloat>> for CSSFloat {
656
+ #[inline]
657
+ fn from(p: AxisEndPoint<CSSFloat>) -> Self {
658
+ match p {
659
+ AxisEndPoint::ToPosition(AxisPosition::LengthPercent(a)) => a,
660
+ AxisEndPoint::ToPosition(AxisPosition::Keyword(_)) => {
661
+ unreachable!("Invalid state: SVG path commands cannot contain a keyword.")
662
+ },
663
+ AxisEndPoint::ByCoordinate(a) => a,
664
+ }
665
+ }
666
+ }
667
+
668
+ impl ToCss for SVGPathPosition {
669
+ fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
670
+ where
671
+ W: Write,
672
+ {
673
+ self.horizontal.to_css(dest)?;
674
+ dest.write_char(' ')?;
675
+ self.vertical.to_css(dest)
676
+ }
677
+ }
678
+
679
+ /// SVG Path parser.
680
+ struct PathParser<'a> {
681
+ chars: Peekable<Cloned<slice::Iter<'a, u8>>>,
682
+ path: Vec<PathCommand>,
683
+ }
684
+
685
+ macro_rules! parse_arguments {
686
+ (
687
+ $parser:ident,
688
+ $enum:ident,
689
+ $( $field:ident : $value:expr, )*
690
+ [ $para:ident => $func:ident $(, $other_para:ident => $other_func:ident)* ]
691
+ ) => {
692
+ {
693
+ loop {
694
+ let $para = $func(&mut $parser.chars)?;
695
+ $(
696
+ skip_comma_wsp(&mut $parser.chars);
697
+ let $other_para = $other_func(&mut $parser.chars)?;
698
+ )*
699
+ $parser.path.push(
700
+ PathCommand::$enum { $( $field: $value, )* $para $(, $other_para)* }
701
+ );
702
+
703
+ // End of string or the next character is a possible new command.
704
+ if !skip_wsp(&mut $parser.chars) ||
705
+ $parser.chars.peek().map_or(true, |c| c.is_ascii_alphabetic()) {
706
+ break;
707
+ }
708
+ skip_comma_wsp(&mut $parser.chars);
709
+ }
710
+ Ok(())
711
+ }
712
+ }
713
+ }
714
+
715
+ impl<'a> PathParser<'a> {
716
+ /// Return a PathParser.
717
+ #[inline]
718
+ fn new(bytes: &'a [u8]) -> Self {
719
+ PathParser {
720
+ chars: bytes.iter().cloned().peekable(),
721
+ path: Vec::new(),
722
+ }
723
+ }
724
+
725
+ /// Parse a sub-path.
726
+ fn parse_subpath(&mut self) -> Result<(), ()> {
727
+ // Handle "moveto" Command first. If there is no "moveto", this is not a valid sub-path
728
+ // (i.e. not a valid moveto-drawto-command-group).
729
+ self.parse_moveto()?;
730
+
731
+ // Handle other commands.
732
+ loop {
733
+ skip_wsp(&mut self.chars);
734
+ if self.chars.peek().is_none_or(|&m| m == b'M' || m == b'm') {
735
+ break;
736
+ }
737
+
738
+ let command = self.chars.next().unwrap();
739
+
740
+ skip_wsp(&mut self.chars);
741
+ match command {
742
+ b'Z' | b'z' => self.parse_closepath(),
743
+ b'L' => self.parse_line_abs(),
744
+ b'l' => self.parse_line_rel(),
745
+ b'H' => self.parse_h_line_abs(),
746
+ b'h' => self.parse_h_line_rel(),
747
+ b'V' => self.parse_v_line_abs(),
748
+ b'v' => self.parse_v_line_rel(),
749
+ b'C' => self.parse_curve_abs(),
750
+ b'c' => self.parse_curve_rel(),
751
+ b'S' => self.parse_smooth_curve_abs(),
752
+ b's' => self.parse_smooth_curve_rel(),
753
+ b'Q' => self.parse_quadratic_bezier_curve_abs(),
754
+ b'q' => self.parse_quadratic_bezier_curve_rel(),
755
+ b'T' => self.parse_smooth_quadratic_bezier_curve_abs(),
756
+ b't' => self.parse_smooth_quadratic_bezier_curve_rel(),
757
+ b'A' => self.parse_elliptical_arc_abs(),
758
+ b'a' => self.parse_elliptical_arc_rel(),
759
+ _ => return Err(()),
760
+ }?;
761
+ }
762
+ Ok(())
763
+ }
764
+
765
+ /// Parse "moveto" command.
766
+ fn parse_moveto(&mut self) -> Result<(), ()> {
767
+ let command = match self.chars.next() {
768
+ Some(c) if c == b'M' || c == b'm' => c,
769
+ _ => return Err(()),
770
+ };
771
+
772
+ skip_wsp(&mut self.chars);
773
+ let point = if command == b'M' {
774
+ parse_command_end_abs(&mut self.chars)
775
+ } else {
776
+ parse_command_end_rel(&mut self.chars)
777
+ }?;
778
+ self.path.push(PathCommand::Move { point });
779
+
780
+ // End of string or the next character is a possible new command.
781
+ if !skip_wsp(&mut self.chars) || self.chars.peek().is_none_or(|c| c.is_ascii_alphabetic()) {
782
+ return Ok(());
783
+ }
784
+ skip_comma_wsp(&mut self.chars);
785
+
786
+ // If a moveto is followed by multiple pairs of coordinates, the subsequent
787
+ // pairs are treated as implicit lineto commands.
788
+ if point.is_abs() {
789
+ self.parse_line_abs()
790
+ } else {
791
+ self.parse_line_rel()
792
+ }
793
+ }
794
+
795
+ /// Parse "closepath" command.
796
+ fn parse_closepath(&mut self) -> Result<(), ()> {
797
+ self.path.push(PathCommand::Close);
798
+ Ok(())
799
+ }
800
+
801
+ /// Parse an absolute "lineto" ("L") command.
802
+ fn parse_line_abs(&mut self) -> Result<(), ()> {
803
+ parse_arguments!(self, Line, [ point => parse_command_end_abs ])
804
+ }
805
+
806
+ /// Parse a relative "lineto" ("l") command.
807
+ fn parse_line_rel(&mut self) -> Result<(), ()> {
808
+ parse_arguments!(self, Line, [ point => parse_command_end_rel ])
809
+ }
810
+
811
+ /// Parse an absolute horizontal "lineto" ("H") command.
812
+ fn parse_h_line_abs(&mut self) -> Result<(), ()> {
813
+ parse_arguments!(self, HLine, [ x => parse_axis_end_abs ])
814
+ }
815
+
816
+ /// Parse a relative horizontal "lineto" ("h") command.
817
+ fn parse_h_line_rel(&mut self) -> Result<(), ()> {
818
+ parse_arguments!(self, HLine, [ x => parse_axis_end_rel ])
819
+ }
820
+
821
+ /// Parse an absolute vertical "lineto" ("V") command.
822
+ fn parse_v_line_abs(&mut self) -> Result<(), ()> {
823
+ parse_arguments!(self, VLine, [ y => parse_axis_end_abs ])
824
+ }
825
+
826
+ /// Parse a relative vertical "lineto" ("v") command.
827
+ fn parse_v_line_rel(&mut self) -> Result<(), ()> {
828
+ parse_arguments!(self, VLine, [ y => parse_axis_end_rel ])
829
+ }
830
+
831
+ /// Parse an absolute cubic Bézier curve ("C") command.
832
+ fn parse_curve_abs(&mut self) -> Result<(), ()> {
833
+ parse_arguments!(self, CubicCurve, [
834
+ control1 => parse_control_point_abs, control2 => parse_control_point_abs, point => parse_command_end_abs
835
+ ])
836
+ }
837
+
838
+ /// Parse a relative cubic Bézier curve ("c") command.
839
+ fn parse_curve_rel(&mut self) -> Result<(), ()> {
840
+ parse_arguments!(self, CubicCurve, [
841
+ control1 => parse_control_point_rel, control2 => parse_control_point_rel, point => parse_command_end_rel
842
+ ])
843
+ }
844
+
845
+ /// Parse an absolute smooth "curveto" ("S") command.
846
+ fn parse_smooth_curve_abs(&mut self) -> Result<(), ()> {
847
+ parse_arguments!(self, SmoothCubic, [
848
+ control2 => parse_control_point_abs, point => parse_command_end_abs
849
+ ])
850
+ }
851
+
852
+ /// Parse a relative smooth "curveto" ("s") command.
853
+ fn parse_smooth_curve_rel(&mut self) -> Result<(), ()> {
854
+ parse_arguments!(self, SmoothCubic, [
855
+ control2 => parse_control_point_rel, point => parse_command_end_rel
856
+ ])
857
+ }
858
+
859
+ /// Parse an absolute quadratic Bézier curve ("Q") command.
860
+ fn parse_quadratic_bezier_curve_abs(&mut self) -> Result<(), ()> {
861
+ parse_arguments!(self, QuadCurve, [
862
+ control1 => parse_control_point_abs, point => parse_command_end_abs
863
+ ])
864
+ }
865
+
866
+ /// Parse a relative quadratic Bézier curve ("q") command.
867
+ fn parse_quadratic_bezier_curve_rel(&mut self) -> Result<(), ()> {
868
+ parse_arguments!(self, QuadCurve, [
869
+ control1 => parse_control_point_rel, point => parse_command_end_rel
870
+ ])
871
+ }
872
+
873
+ /// Parse an absolute smooth quadratic Bézier curveto ("T") command.
874
+ fn parse_smooth_quadratic_bezier_curve_abs(&mut self) -> Result<(), ()> {
875
+ parse_arguments!(self, SmoothQuad, [ point => parse_command_end_abs ])
876
+ }
877
+
878
+ /// Parse a relative smooth quadratic Bézier curveto ("t") command.
879
+ fn parse_smooth_quadratic_bezier_curve_rel(&mut self) -> Result<(), ()> {
880
+ parse_arguments!(self, SmoothQuad, [ point => parse_command_end_rel ])
881
+ }
882
+
883
+ /// Parse an absolute elliptical arc curve ("A") command.
884
+ fn parse_elliptical_arc_abs(&mut self) -> Result<(), ()> {
885
+ let (parse_arc_size, parse_arc_sweep) = Self::arc_flag_parsers();
886
+ parse_arguments!(self, Arc, [
887
+ radii => parse_arc_radii,
888
+ rotate => parse_number,
889
+ arc_size => parse_arc_size,
890
+ arc_sweep => parse_arc_sweep,
891
+ point => parse_command_end_abs
892
+ ])
893
+ }
894
+
895
+ /// Parse a relative elliptical arc curve ("a") command.
896
+ fn parse_elliptical_arc_rel(&mut self) -> Result<(), ()> {
897
+ let (parse_arc_size, parse_arc_sweep) = Self::arc_flag_parsers();
898
+ parse_arguments!(self, Arc, [
899
+ radii => parse_arc_radii,
900
+ rotate => parse_number,
901
+ arc_size => parse_arc_size,
902
+ arc_sweep => parse_arc_sweep,
903
+ point => parse_command_end_rel
904
+ ])
905
+ }
906
+
907
+ /// Helper that returns parsers for the arc-size and arc-sweep flags.
908
+ fn arc_flag_parsers() -> (
909
+ impl Fn(&mut Peekable<Cloned<slice::Iter<'_, u8>>>) -> Result<ArcSize, ()>,
910
+ impl Fn(&mut Peekable<Cloned<slice::Iter<'_, u8>>>) -> Result<ArcSweep, ()>,
911
+ ) {
912
+ // Parse a flag whose value is '0' or '1'; otherwise, return Err(()).
913
+ let parse_arc_size = |iter: &mut Peekable<Cloned<slice::Iter<u8>>>| match iter.next() {
914
+ Some(c) if c == b'1' => Ok(ArcSize::Large),
915
+ Some(c) if c == b'0' => Ok(ArcSize::Small),
916
+ _ => Err(()),
917
+ };
918
+ let parse_arc_sweep = |iter: &mut Peekable<Cloned<slice::Iter<u8>>>| match iter.next() {
919
+ Some(c) if c == b'1' => Ok(ArcSweep::Cw),
920
+ Some(c) if c == b'0' => Ok(ArcSweep::Ccw),
921
+ _ => Err(()),
922
+ };
923
+ (parse_arc_size, parse_arc_sweep)
924
+ }
925
+ }
926
+
927
+ /// Parse a pair of numbers into CoordPair.
928
+ fn parse_coord(iter: &mut Peekable<Cloned<slice::Iter<u8>>>) -> Result<CoordPair, ()> {
929
+ let x = parse_number(iter)?;
930
+ skip_comma_wsp(iter);
931
+ let y = parse_number(iter)?;
932
+ Ok(CoordPair::new(x, y))
933
+ }
934
+
935
+ /// Parse a pair of numbers that describes the absolutely positioned end point.
936
+ fn parse_command_end_abs(
937
+ iter: &mut Peekable<Cloned<slice::Iter<u8>>>,
938
+ ) -> Result<CommandEndPoint<SVGPathPosition, CSSFloat>, ()> {
939
+ let coord = parse_coord(iter)?;
940
+ Ok(CommandEndPoint::ToPosition(coord.into()))
941
+ }
942
+
943
+ /// Parse a pair of numbers that describes the relatively positioned end point.
944
+ fn parse_command_end_rel(
945
+ iter: &mut Peekable<Cloned<slice::Iter<u8>>>,
946
+ ) -> Result<CommandEndPoint<SVGPathPosition, CSSFloat>, ()> {
947
+ let coord = parse_coord(iter)?;
948
+ Ok(CommandEndPoint::ByCoordinate(coord))
949
+ }
950
+
951
+ /// Parse a pair of values that describe the absolutely positioned curve control point.
952
+ fn parse_control_point_abs(
953
+ iter: &mut Peekable<Cloned<slice::Iter<u8>>>,
954
+ ) -> Result<ControlPoint<SVGPathPosition, CSSFloat>, ()> {
955
+ let coord = parse_coord(iter)?;
956
+ Ok(ControlPoint::Relative(RelativeControlPoint {
957
+ coord,
958
+ reference: ControlReference::Origin,
959
+ }))
960
+ }
961
+
962
+ /// Parse a pair of values that describe the relatively positioned curve control point.
963
+ fn parse_control_point_rel(
964
+ iter: &mut Peekable<Cloned<slice::Iter<u8>>>,
965
+ ) -> Result<ControlPoint<SVGPathPosition, CSSFloat>, ()> {
966
+ let coord = parse_coord(iter)?;
967
+ Ok(ControlPoint::Relative(RelativeControlPoint {
968
+ coord,
969
+ reference: ControlReference::Start,
970
+ }))
971
+ }
972
+
973
+ /// Parse a number that describes the absolutely positioned axis end point.
974
+ fn parse_axis_end_abs(
975
+ iter: &mut Peekable<Cloned<slice::Iter<u8>>>,
976
+ ) -> Result<AxisEndPoint<f32>, ()> {
977
+ let value = parse_number(iter)?;
978
+ Ok(AxisEndPoint::ToPosition(AxisPosition::LengthPercent(value)))
979
+ }
980
+
981
+ /// Parse a number that describes the relatively positioned axis end point.
982
+ fn parse_axis_end_rel(
983
+ iter: &mut Peekable<Cloned<slice::Iter<u8>>>,
984
+ ) -> Result<AxisEndPoint<f32>, ()> {
985
+ let value = parse_number(iter)?;
986
+ Ok(AxisEndPoint::ByCoordinate(value))
987
+ }
988
+
989
+ /// Parse a pair of numbers that describes the size of the ellipse that the arc is taken from.
990
+ fn parse_arc_radii(iter: &mut Peekable<Cloned<slice::Iter<u8>>>) -> Result<ArcRadii<CSSFloat>, ()> {
991
+ let coord = parse_coord(iter)?;
992
+ Ok(ArcRadii {
993
+ rx: coord.x,
994
+ ry: Some(coord.y).into(),
995
+ })
996
+ }
997
+
998
+ /// This is a special version which parses the number for SVG Path. e.g. "M 0.6.5" should be parsed
999
+ /// as MoveTo with a coordinate of ("0.6", ".5"), instead of treating 0.6.5 as a non-valid floating
1000
+ /// point number. In other words, the logic here is similar with that of
1001
+ /// tokenizer::consume_numeric, which also consumes the number as many as possible, but here the
1002
+ /// input is a Peekable and we only accept an integer of a floating point number.
1003
+ ///
1004
+ /// The "number" syntax in https://www.w3.org/TR/SVG/paths.html#PathDataBNF
1005
+ fn parse_number(iter: &mut Peekable<Cloned<slice::Iter<u8>>>) -> Result<CSSFloat, ()> {
1006
+ // 1. Check optional sign.
1007
+ let sign = if iter
1008
+ .peek()
1009
+ .is_some_and(|&sign| sign == b'+' || sign == b'-')
1010
+ {
1011
+ if iter.next().unwrap() == b'-' {
1012
+ -1.
1013
+ } else {
1014
+ 1.
1015
+ }
1016
+ } else {
1017
+ 1.
1018
+ };
1019
+
1020
+ // 2. Check integer part.
1021
+ let mut integral_part: f64 = 0.;
1022
+ let got_dot = if iter.peek().is_none_or(|&n| n != b'.') {
1023
+ // If the first digit in integer part is neither a dot nor a digit, this is not a number.
1024
+ if iter.peek().is_none_or(|n| !n.is_ascii_digit()) {
1025
+ return Err(());
1026
+ }
1027
+
1028
+ while iter.peek().is_some_and(|n| n.is_ascii_digit()) {
1029
+ integral_part = integral_part * 10. + (iter.next().unwrap() - b'0') as f64;
1030
+ }
1031
+
1032
+ iter.peek().is_some_and(|&n| n == b'.')
1033
+ } else {
1034
+ true
1035
+ };
1036
+
1037
+ // 3. Check fractional part.
1038
+ let mut fractional_part: f64 = 0.;
1039
+ if got_dot {
1040
+ // Consume '.'.
1041
+ iter.next();
1042
+ // If the first digit in fractional part is not a digit, this is not a number.
1043
+ if iter.peek().is_none_or(|n| !n.is_ascii_digit()) {
1044
+ return Err(());
1045
+ }
1046
+
1047
+ let mut factor = 0.1;
1048
+ while iter.peek().is_some_and(|n| n.is_ascii_digit()) {
1049
+ fractional_part += (iter.next().unwrap() - b'0') as f64 * factor;
1050
+ factor *= 0.1;
1051
+ }
1052
+ }
1053
+
1054
+ let mut value = sign * (integral_part + fractional_part);
1055
+
1056
+ // 4. Check exp part. The segment name of SVG Path doesn't include 'E' or 'e', so it's ok to
1057
+ // treat the numbers after 'E' or 'e' are in the exponential part.
1058
+ if iter.peek().is_some_and(|&exp| exp == b'E' || exp == b'e') {
1059
+ // Consume 'E' or 'e'.
1060
+ iter.next();
1061
+ let exp_sign = if iter
1062
+ .peek()
1063
+ .is_some_and(|&sign| sign == b'+' || sign == b'-')
1064
+ {
1065
+ if iter.next().unwrap() == b'-' {
1066
+ -1.
1067
+ } else {
1068
+ 1.
1069
+ }
1070
+ } else {
1071
+ 1.
1072
+ };
1073
+
1074
+ let mut exp: f64 = 0.;
1075
+ while iter.peek().is_some_and(|n| n.is_ascii_digit()) {
1076
+ exp = exp * 10. + (iter.next().unwrap() - b'0') as f64;
1077
+ }
1078
+
1079
+ value *= f64::powf(10., exp * exp_sign);
1080
+ }
1081
+
1082
+ if value.is_finite() {
1083
+ Ok(value.min(f32::MAX as f64).max(f32::MIN as f64) as CSSFloat)
1084
+ } else {
1085
+ Err(())
1086
+ }
1087
+ }
1088
+
1089
+ /// Skip all svg whitespaces, and return true if |iter| hasn't finished.
1090
+ #[inline]
1091
+ fn skip_wsp(iter: &mut Peekable<Cloned<slice::Iter<u8>>>) -> bool {
1092
+ // Note: SVG 1.1 defines the whitespaces as \u{9}, \u{20}, \u{A}, \u{D}.
1093
+ // However, SVG 2 has one extra whitespace: \u{C}.
1094
+ // Therefore, we follow the newest spec for the definition of whitespace,
1095
+ // i.e. \u{9}, \u{20}, \u{A}, \u{C}, \u{D}.
1096
+ while iter.peek().is_some_and(|c| c.is_ascii_whitespace()) {
1097
+ iter.next();
1098
+ }
1099
+ iter.peek().is_some()
1100
+ }
1101
+
1102
+ /// Skip all svg whitespaces and one comma, and return true if |iter| hasn't finished.
1103
+ #[inline]
1104
+ fn skip_comma_wsp(iter: &mut Peekable<Cloned<slice::Iter<u8>>>) -> bool {
1105
+ if !skip_wsp(iter) {
1106
+ return false;
1107
+ }
1108
+
1109
+ if *iter.peek().unwrap() != b',' {
1110
+ return true;
1111
+ }
1112
+ iter.next();
1113
+
1114
+ skip_wsp(iter)
1115
+ }