blitz-pdf 0.1.5 → 0.1.6

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.
package/README.md CHANGED
@@ -396,6 +396,38 @@ Input limits: 16 MiB HTML, 64 MiB supplied assets, 64 MiB attachments, 32 MiB su
396
396
  resource requests. These are practical limits, not a complete sandbox; isolate
397
397
  untrusted documents in a terminable worker with application limits.
398
398
 
399
+ ### Unicode text
400
+
401
+ HTML strings and UTF-8 assets preserve the text produced by layout, including
402
+ non-Latin scripts, supplementary-plane characters, combining marks, and
403
+ ligatures. Visually identical sequences such as `é` / `é`, `fi` / `fi`, and
404
+ composed / decomposed Hangul can coexist in one document. Their source mappings
405
+ remain separate in body text, form values, repeated table headers, margin
406
+ templates, and page numbers. CSS whitespace handling and `text-transform`
407
+ still apply normally.
408
+
409
+ PDF Unicode maps and `ActualText` spans preserve the original sequences when
410
+ multiple characters share a glyph or a cluster uses multiple glyphs. Accurate
411
+ copy/paste requires a reader that supports `ActualText`; extractors that ignore
412
+ it, including PDF.js, can normalize ligatures or repeat combining clusters.
413
+ The Unicode regression suite verifies extraction with Poppler's `pdftotext`.
414
+
415
+ Unicode support does not imply that one font covers every character. There is
416
+ no system-font discovery or automatic font download. For example, provide a
417
+ static CJK font and select its family to render Chinese, Japanese, or Korean:
418
+
419
+ ```js
420
+ import { readFile } from "node:fs/promises";
421
+
422
+ const pdf = await renderPdf(
423
+ '<p style="font-family: \'Noto Sans CJK SC\'">中文 日本語 한국어</p>',
424
+ { fonts: [await readFile("NotoSansCJKsc-Regular.otf")] },
425
+ );
426
+ ```
427
+
428
+ Use `@font-face` and `assets` for a CSS family alias. Fonts must contain the
429
+ required glyphs and permit embedding; missing glyphs still reject PDF/A output.
430
+
399
431
  ## PDF/A-3 conformance
400
432
 
401
433
  Every successful render targets **PDF/A-3u (ISO 19005-3, level U)**. The PDF 1.7
@@ -606,6 +638,8 @@ transition rule. [§ 14 UStG](https://www.gesetze-im-internet.de/ustg_1980/__14.
606
638
 
607
639
  Install Node.js 22.13+ for development tooling, Bun, and rustup. The repository
608
640
  pins Rust 1.99.0 and its WASM target in `rust-toolchain.toml`.
641
+ The Unicode integration tests also require Poppler's `pdftotext` (`brew install
642
+ poppler` on macOS or `apt-get install poppler-utils` on Debian/Ubuntu).
609
643
 
610
644
  ```sh
611
645
  npm ci
@@ -625,11 +659,12 @@ the tarball; it does not publish. Install locally using
625
659
 
626
660
  The pinned Stylo source in `vendor/stylo` enables paged-media properties in the
627
661
  Servo build. `vendor/krilla` adds custom XMP schema support and independent
628
- modification dates. `vendor/README.md` records both patches; the sources and
629
- licenses are included in the npm archive.
662
+ modification dates. `vendor/blitz-paint` exposes source layouts in paint order
663
+ so Unicode mappings retain each run's identity. `vendor/README.md` records the
664
+ patches; the sources and licenses are included in the npm archive.
630
665
 
631
666
  `bun test` exercises the compiled module through its public package export. Tests
632
- extract Unicode with PDF.js and inspect embedded fonts, vector paths, absence
667
+ extract Unicode with PDF.js and Poppler and inspect embedded fonts, vector paths, absence
633
668
  of page images, dimensions, pagination, assets, media queries, and error recovery.
634
669
  For browser verification, run `npm run test:browser` and open
635
670
  `http://127.0.0.1:4173`. CI builds WASM, runs Bun and Node tests, validates the
@@ -20,6 +20,8 @@ independent modification dates; its source and MIT/Apache-2.0 licenses are in
20
20
  `vendor/krilla`. Blitz's DOM crate is patched for fractional PDF geometry,
21
21
  table-cell sizing/spacing, anonymous table wrappers, and paragraph margin
22
22
  collapse; its source and MIT/Apache-2.0 licenses are in `vendor/blitz-dom`.
23
+ Blitz's paint crate exposes source-layout notifications for Unicode PDF text;
24
+ its source and MIT/Apache-2.0 licenses are in `vendor/blitz-paint`.
23
25
  `vendor/README.md` lists these patches. Other dependencies are used without
24
26
  source modifications.
25
27
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blitz-pdf",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Render HTML and CSS to PDF with Blitz and WebAssembly in Node.js, Bun, and browsers.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/vendor/README.md CHANGED
@@ -75,3 +75,27 @@ against rebuilt WASM. Layout tests check physical margins, fractional viewport
75
75
  widths, header/footer geometry, table line spacing, descendant alignment, empty
76
76
  paragraphs, explicit empty-box dimensions, exact page-boundary fits, constrained
77
77
  table columns, anonymous cells, and inheritance after CSS page-size changes.
78
+
79
+ # Blitz Unicode paint notifications
80
+
81
+ `blitz-paint/` contains `packages/blitz-paint` from Blitz revision
82
+ `e72650a6e67941852fddf025295cfee30c39ea32`, with its MIT/Apache-2.0 licenses.
83
+ Its standalone manifest expands the upstream workspace dependencies and pins
84
+ sibling crates to the same revision.
85
+
86
+ - `src/lib.rs`: add the opt-in `TextObserver` and `paint_scene_with_text` API.
87
+ The original `paint_scene` entry point keeps its existing behavior.
88
+ - `src/render.rs`: notify the observer immediately before painting inline text,
89
+ input/textarea text, and outside list markers; forward it to subdocuments.
90
+ The notifications supply the exact Parley layout and source string in paint
91
+ order, including visibility and stacking order decisions.
92
+
93
+ The PDF renderer associates the observed runs with individual scene commands
94
+ and checks that their glyphs match. It retains this immutable association when
95
+ replaying pages or repeated table fragments. It no longer uses glyph shapes as
96
+ document-wide source-text identities: `fi` and `fi`, for example, legitimately
97
+ share a shape while requiring different Unicode mappings.
98
+
99
+ When upgrading Blitz, retain these notifications at all text-paint boundaries
100
+ and run `tests/unicode.test.ts` (requires Poppler), the existing rendering,
101
+ pagination, and template suites, and `npm run test:pdfa` on the rebuilt WASM.
@@ -0,0 +1,50 @@
1
+ [package]
2
+ name = "blitz-paint"
3
+ description = "Paint a Blitz Document using anyrender"
4
+ documentation = "https://docs.rs/blitz-paint"
5
+ version = "0.3.0-beta.3"
6
+ license = "MIT OR Apache-2.0"
7
+ homepage = "https://github.com/dioxuslabs/blitz"
8
+ repository = "https://github.com/dioxuslabs/blitz"
9
+ categories = ["gui"]
10
+ edition = "2024"
11
+ rust-version = "1.91.0"
12
+
13
+ [features]
14
+ default = ["svg"]
15
+ tracing = ["dep:tracing"]
16
+ svg = ["dep:anyrender_svg", "dep:usvg", "blitz-dom/svg"]
17
+ custom-widget = ["blitz-dom/custom-widget"]
18
+ font-embolden = []
19
+ apple-font-embolden = []
20
+ # Paint overlay scrollbar thumbs on scroll containers, and enable their
21
+ # drag/hover interaction in blitz-dom (off by default while the feature
22
+ # matures).
23
+ scrollbars = ["blitz-dom/scrollbars"]
24
+
25
+ [dependencies]
26
+ # Blitz dependencies
27
+ anyrender = { version = "0.14.0" }
28
+ anyrender_svg = { version = "0.15.0", optional = true }
29
+ blitz-traits = { version = "=0.3.0-beta.3", default-features = false, git = "https://github.com/DioxusLabs/blitz", rev = "e72650a6e67941852fddf025295cfee30c39ea32" }
30
+ blitz-dom = { version = "=0.3.0-beta.3", default-features = false, git = "https://github.com/DioxusLabs/blitz", rev = "e72650a6e67941852fddf025295cfee30c39ea32" }
31
+
32
+ # Servo dependencies
33
+ style = { version = "0.22.0", package = "stylo" }
34
+ euclid = { version = "0.22" }
35
+
36
+ # DioxusLabs dependencies
37
+ taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "6c2106e3df79493d304f954dc2cd4a4a382981d3", default-features = false, features = ["std", "flexbox", "flexbox_balance", "grid", "block_layout", "content_size", "calc", "detailed_layout_info"] }
38
+
39
+ # Linebender + WGPU dependencies
40
+ parley = { version = "0.12", default-features = false, features = ["std", "bytemuck"] }
41
+ skrifa = { version = "0.44", default-features = false, features = ["std"] }
42
+ color = { version = "0.3" }
43
+ peniko = { version = "0.6.0" }
44
+ kurbo = { version = "0.13.1" }
45
+ usvg = { version = "0.48.1", optional = true }
46
+
47
+ # Other dependencies
48
+ bytemuck = { version = "1" }
49
+ smallvec = { version = "1" }
50
+ tracing = { version = "0.1.40", optional = true }
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,23 @@
1
+ Permission is hereby granted, free of charge, to any
2
+ person obtaining a copy of this software and associated
3
+ documentation files (the "Software"), to deal in the
4
+ Software without restriction, including without
5
+ limitation the rights to use, copy, modify, merge,
6
+ publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software
8
+ is furnished to do so, subject to the following
9
+ conditions:
10
+
11
+ The above copyright notice and this permission notice
12
+ shall be included in all copies or substantial portions
13
+ of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17
+ TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
18
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19
+ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
22
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
+ DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ use color::{AlphaColor, DynamicColor, Srgb};
2
+ use style::color::AbsoluteColor;
3
+
4
+ pub type Color = AlphaColor<Srgb>;
5
+
6
+ pub trait ToColorColor {
7
+ /// Converts a color into the `AlphaColor<Srgb>` type from the `color` crate
8
+ fn as_srgb_color(&self) -> Color;
9
+
10
+ /// Converts a color into the `DynamicColor` type from the `color` crate
11
+ fn as_dynamic_color(&self) -> DynamicColor;
12
+ }
13
+ impl ToColorColor for AbsoluteColor {
14
+ fn as_srgb_color(&self) -> Color {
15
+ Color::new(
16
+ *self
17
+ .to_color_space(style::color::ColorSpace::Srgb)
18
+ .raw_components(),
19
+ )
20
+ }
21
+
22
+ fn as_dynamic_color(&self) -> DynamicColor {
23
+ DynamicColor::from_alpha_color(self.as_srgb_color())
24
+ }
25
+ }
26
+
27
+ /// The WCAG contrast ratio (>= 1) between two colours, matching Chrome's
28
+ /// `color_utils::GetContrastRatio`.
29
+ pub(crate) fn contrast_ratio(a: Color, b: Color) -> f32 {
30
+ // `relative_luminance` is defined on `OpaqueColor`; discard the alpha (which
31
+ // it ignores anyway) with `split`.
32
+ let la = a.split().0.relative_luminance() + 0.05;
33
+ let lb = b.split().0.relative_luminance() + 0.05;
34
+ if la > lb { la / lb } else { lb / la }
35
+ }
36
+
37
+ /// Blend `color` towards black or white (whichever has contrast headroom)
38
+ /// just far enough to reach `target` contrast with the original, so the
39
+ /// result is visibly distinct for any input. Transparent colours pass
40
+ /// through unchanged; alpha is preserved.
41
+ #[cfg(feature = "scrollbars")]
42
+ pub(crate) fn blend_for_contrast(color: Color, target: f32) -> Color {
43
+ let alpha = color.components[3];
44
+ if alpha == 0.0 {
45
+ return color;
46
+ }
47
+ let white = Color::new([1.0, 1.0, 1.0, alpha]);
48
+ let black = Color::new([0.0, 0.0, 0.0, alpha]);
49
+ let pole = if contrast_ratio(color, white) >= contrast_ratio(color, black) {
50
+ white
51
+ } else {
52
+ black
53
+ };
54
+ // Contrast grows monotonically towards the pole: bisect the blend
55
+ // fraction to land just past the target ratio.
56
+ let (mut lo, mut hi) = (0.0_f32, 1.0_f32);
57
+ for _ in 0..8 {
58
+ let mid = (lo + hi) / 2.0;
59
+ if contrast_ratio(color, color.lerp_rect(pole, mid)) < target {
60
+ lo = mid;
61
+ } else {
62
+ hi = mid;
63
+ }
64
+ }
65
+ color.lerp_rect(pole, hi)
66
+ }
@@ -0,0 +1,168 @@
1
+ use anyrender::PaintScene;
2
+ use blitz_dom::{BaseDocument, NodeId};
3
+ use kurbo::{Affine, Rect, Vec2};
4
+
5
+ use crate::color::Color;
6
+
7
+ /// Renders a layout debugging overlay which visualises the content size, padding and border
8
+ /// of the node with a transparent overlay.
9
+ pub(crate) fn render_debug_overlay(
10
+ scene: &mut impl PaintScene,
11
+ dom: &BaseDocument,
12
+ node_id: NodeId,
13
+ scale: f64,
14
+ initial_x: f64,
15
+ initial_y: f64,
16
+ ) {
17
+ // Non-atomic inline elements have no layout box of their own: they are laid
18
+ // out as style spans within an inline root's text layout and may fragment
19
+ // across multiple line boxes. Highlight each fragment's content box.
20
+ if let Some(rects) = dom.as_ref().inline_fragment_rects(node_id) {
21
+ let fill_color = Color::from_rgba8(66, 144, 245, 128); // blue
22
+ for r in rects {
23
+ let rect = Rect::new(0.0, 0.0, r.width * scale, r.height * scale);
24
+ let translation = Vec2::new(r.x * scale + initial_x, r.y * scale + initial_y);
25
+ scene.fill(
26
+ peniko::Fill::NonZero,
27
+ Affine::translate(translation),
28
+ fill_color,
29
+ None,
30
+ &rect,
31
+ );
32
+ }
33
+ return;
34
+ }
35
+
36
+ let viewport_scroll = dom.as_ref().viewport_scroll();
37
+ let mut node = &dom.as_ref().tree()[node_id];
38
+
39
+ let taffy::Layout {
40
+ size,
41
+ border,
42
+ padding,
43
+ margin,
44
+ ..
45
+ } = *node.final_layout();
46
+ let taffy::Size { width, height } = size;
47
+
48
+ let padding_border = padding + border;
49
+ let scaled_pb = padding_border.map(|v| f64::from(v) * scale);
50
+ let scaled_padding = padding.map(|v| f64::from(v) * scale);
51
+ let scaled_border = border.map(|v| f64::from(v) * scale);
52
+ let scaled_margin = margin.map(|v| f64::from(v) * scale);
53
+
54
+ let content_width = width - padding_border.left - padding_border.right;
55
+ let content_height = height - padding_border.top - padding_border.bottom;
56
+
57
+ let taffy::Point { x, y } = node.final_layout().location;
58
+
59
+ let mut abs_x = x;
60
+ let mut abs_y = y;
61
+ // A fixed box whose containing block is the root element does not scroll
62
+ // with it (nor with the viewport).
63
+ let mut fixed_to_viewport = false;
64
+ while let Some(parent_id) = node.containing_block() {
65
+ let child_is_fixed = node.taffy_position() == taffy::Position::Fixed;
66
+ node = &dom.as_ref().tree()[parent_id];
67
+ fixed_to_viewport = child_is_fixed && node.containing_block().is_none();
68
+ let taffy::Point { x, y } = node.final_layout().location;
69
+ abs_x += x;
70
+ abs_y += y;
71
+ if !fixed_to_viewport {
72
+ abs_x -= node.scroll_offset().x as f32;
73
+ abs_y -= node.scroll_offset().y as f32;
74
+ }
75
+ }
76
+
77
+ if !fixed_to_viewport {
78
+ abs_x -= viewport_scroll.x as f32;
79
+ abs_y -= viewport_scroll.y as f32;
80
+ }
81
+
82
+ // Note: initial_x/initial_y are already in physical (scaled) pixels so they must be added after scaling
83
+ // the other values
84
+ let abs_x = f64::from(abs_x) * scale + initial_x;
85
+ let abs_y = f64::from(abs_y) * scale + initial_y;
86
+ let width = f64::from(width) * scale;
87
+ let height = f64::from(height) * scale;
88
+ let content_width = f64::from(content_width) * scale;
89
+ let content_height = f64::from(content_height) * scale;
90
+
91
+ // Fill content box blue
92
+ let base_translation = Vec2::new(abs_x, abs_y);
93
+ let transform = Affine::translate(base_translation + Vec2::new(scaled_pb.left, scaled_pb.top));
94
+ let rect = Rect::new(0.0, 0.0, content_width, content_height);
95
+ let fill_color = Color::from_rgba8(66, 144, 245, 128); // blue
96
+ scene.fill(peniko::Fill::NonZero, transform, fill_color, None, &rect);
97
+
98
+ let padding_color = Color::from_rgba8(81, 144, 66, 128); // green
99
+ draw_cutout_rect(
100
+ scene,
101
+ base_translation + Vec2::new(scaled_border.left, scaled_border.top),
102
+ Vec2::new(
103
+ content_width + scaled_padding.left + scaled_padding.right,
104
+ content_height + scaled_padding.top + scaled_padding.bottom,
105
+ ),
106
+ scaled_padding.map(f64::from),
107
+ padding_color,
108
+ );
109
+
110
+ let border_color = Color::from_rgba8(245, 66, 66, 128); // red
111
+ draw_cutout_rect(
112
+ scene,
113
+ base_translation,
114
+ Vec2::new(width, height),
115
+ scaled_border.map(f64::from),
116
+ border_color,
117
+ );
118
+
119
+ let margin_color = Color::from_rgba8(249, 204, 157, 128); // orange
120
+ draw_cutout_rect(
121
+ scene,
122
+ base_translation - Vec2::new(scaled_margin.left, scaled_margin.top),
123
+ Vec2::new(
124
+ width + scaled_margin.left + scaled_margin.right,
125
+ height + scaled_margin.top + scaled_margin.bottom,
126
+ ),
127
+ scaled_margin.map(f64::from),
128
+ margin_color,
129
+ );
130
+ }
131
+
132
+ fn draw_cutout_rect(
133
+ scene: &mut impl PaintScene,
134
+ base_translation: Vec2,
135
+ size: Vec2,
136
+ edge_widths: taffy::Rect<f64>,
137
+ color: Color,
138
+ ) {
139
+ let mut fill = |pos: Vec2, width: f64, height: f64| {
140
+ scene.fill(
141
+ peniko::Fill::NonZero,
142
+ Affine::translate(pos),
143
+ color,
144
+ None,
145
+ &Rect::new(0.0, 0.0, width, height),
146
+ );
147
+ };
148
+
149
+ let right = size.x - edge_widths.right;
150
+ let bottom = size.y - edge_widths.bottom;
151
+ let inner_h = size.y - edge_widths.top - edge_widths.bottom;
152
+ let inner_w = size.x - edge_widths.left - edge_widths.right;
153
+
154
+ let bt = base_translation;
155
+ let ew = edge_widths;
156
+
157
+ // Corners
158
+ fill(bt, ew.left, ew.top); // top-left
159
+ fill(bt + Vec2::new(0.0, bottom), ew.left, ew.bottom); // bottom-left
160
+ fill(bt + Vec2::new(right, 0.0), ew.right, ew.top); // top-right
161
+ fill(bt + Vec2::new(right, bottom), ew.right, ew.bottom); // bottom-right
162
+
163
+ // Sides
164
+ fill(bt + Vec2::new(0.0, ew.top), ew.left, inner_h); // left
165
+ fill(bt + Vec2::new(right, ew.top), ew.right, inner_h); // right
166
+ fill(bt + Vec2::new(ew.left, 0.0), inner_w, ew.top); // top
167
+ fill(bt + Vec2::new(ew.left, bottom), inner_w, ew.bottom); // bottom
168
+ }
@@ -0,0 +1,40 @@
1
+ use blitz_dom::util::ToColorColor as _;
2
+ use style::color::AbsoluteColor;
3
+ pub(crate) use style::computed_values::filter::single_value::T as StyloFilter;
4
+
5
+ use anyrender::filters::{Filter, FilterEffect};
6
+
7
+ pub(crate) fn convert_filters(filters: &[StyloFilter]) -> Option<Filter> {
8
+ if filters.is_empty() {
9
+ return None;
10
+ }
11
+
12
+ Some(Filter::linear_list(
13
+ filters.iter().filter_map(convert_single_filter),
14
+ ))
15
+ }
16
+
17
+ pub(crate) fn convert_single_filter(filter: &StyloFilter) -> Option<FilterEffect> {
18
+ Some(match filter {
19
+ StyloFilter::Blur(radius) => FilterEffect::blur(radius.px()),
20
+ StyloFilter::Brightness(amount) => FilterEffect::brightness(amount.0),
21
+ StyloFilter::Contrast(amount) => FilterEffect::contrast(amount.0),
22
+ StyloFilter::Grayscale(amount) => FilterEffect::grayscale(amount.0),
23
+ StyloFilter::HueRotate(angle) => FilterEffect::hue_rotate(angle.radians()),
24
+ StyloFilter::Invert(amount) => FilterEffect::invert(amount.0),
25
+ StyloFilter::Opacity(amount) => FilterEffect::opacity(amount.0),
26
+ StyloFilter::Saturate(amount) => FilterEffect::saturate(amount.0),
27
+ StyloFilter::Sepia(amount) => FilterEffect::sepia(amount.0),
28
+ StyloFilter::DropShadow(shadow) => FilterEffect::drop_shadow(
29
+ shadow.horizontal.px(),
30
+ shadow.vertical.px(),
31
+ shadow.blur.px(),
32
+ // TODO: pass in correct currentColor
33
+ shadow
34
+ .color
35
+ .resolve_to_absolute(&AbsoluteColor::BLACK)
36
+ .as_color_color(),
37
+ ),
38
+ StyloFilter::Url(_) => return None,
39
+ })
40
+ }