blitz-pdf 0.1.4 → 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 +67 -3
- package/THIRD_PARTY_NOTICES.md +2 -0
- package/dist/api.d.ts +9 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +25 -27
- package/dist/api.js.map +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +13 -1
- package/dist/browser.js.map +1 -1
- package/dist/node-worker.d.ts +2 -0
- package/dist/node-worker.d.ts.map +1 -0
- package/dist/node-worker.js +10 -0
- package/dist/node-worker.js.map +1 -0
- package/dist/node.d.ts +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +18 -6
- package/dist/node.js.map +1 -1
- package/dist/render.d.ts +8 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +28 -0
- package/dist/render.js.map +1 -0
- package/dist/wasm.d.ts +3 -0
- package/dist/wasm.d.ts.map +1 -0
- package/dist/wasm.js +18 -0
- package/dist/wasm.js.map +1 -0
- package/dist/worker-host.d.ts +16 -0
- package/dist/worker-host.d.ts.map +1 -0
- package/dist/worker-host.js +31 -0
- package/dist/worker-host.js.map +1 -0
- package/dist/worker-protocol.d.ts +22 -0
- package/dist/worker-protocol.d.ts.map +1 -0
- package/dist/worker-protocol.js +2 -0
- package/dist/worker-protocol.js.map +1 -0
- package/dist/worker-runtime.d.ts +8 -0
- package/dist/worker-runtime.d.ts.map +1 -0
- package/dist/worker-runtime.js +34 -0
- package/dist/worker-runtime.js.map +1 -0
- package/dist/worker.d.ts +2 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +7 -0
- package/dist/worker.js.map +1 -0
- package/package.json +5 -2
- package/src/api.ts +33 -22
- package/src/browser.ts +13 -3
- package/src/node-worker.ts +9 -0
- package/src/node.ts +13 -4
- package/src/render.ts +28 -0
- package/src/wasm.ts +17 -0
- package/src/worker-host.ts +45 -0
- package/src/worker-protocol.ts +10 -0
- package/src/worker-runtime.ts +35 -0
- package/src/worker.ts +12 -0
- package/vendor/README.md +24 -0
- package/vendor/blitz-paint/Cargo.toml +50 -0
- package/vendor/blitz-paint/LICENSE-APACHE +176 -0
- package/vendor/blitz-paint/LICENSE-MIT +23 -0
- package/vendor/blitz-paint/src/color.rs +66 -0
- package/vendor/blitz-paint/src/debug_overlay.rs +168 -0
- package/vendor/blitz-paint/src/filters.rs +40 -0
- package/vendor/blitz-paint/src/gradient.rs +536 -0
- package/vendor/blitz-paint/src/kurbo_css/css_box.rs +847 -0
- package/vendor/blitz-paint/src/kurbo_css/mod.rs +77 -0
- package/vendor/blitz-paint/src/kurbo_css/non_uniform_radii.rs +60 -0
- package/vendor/blitz-paint/src/layers.rs +95 -0
- package/vendor/blitz-paint/src/lib.rs +190 -0
- package/vendor/blitz-paint/src/render/background.rs +968 -0
- package/vendor/blitz-paint/src/render/border.rs +631 -0
- package/vendor/blitz-paint/src/render/box_shadow.rs +157 -0
- package/vendor/blitz-paint/src/render/clip_path.rs +492 -0
- package/vendor/blitz-paint/src/render/form_controls.rs +103 -0
- package/vendor/blitz-paint/src/render/mask.rs +144 -0
- package/vendor/blitz-paint/src/render.rs +1351 -0
- package/vendor/blitz-paint/src/sizing.rs +63 -0
- package/vendor/blitz-paint/src/text.rs +747 -0
- package/wasm/blitz_pdf_bg.wasm +0 -0
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
use anyrender::PaintScene;
|
|
2
|
+
use blitz_dom::node::SpecialElementData;
|
|
3
|
+
use kurbo::{BezPath, Cap, Circle, Insets, Join, PathEl, Point, Rect, Shape as _, Stroke, Vec2};
|
|
4
|
+
use peniko::{Color, Fill};
|
|
5
|
+
use smallvec::SmallVec;
|
|
6
|
+
use style::{
|
|
7
|
+
computed_values::border_collapse::T as BorderCollapse,
|
|
8
|
+
values::computed::{BorderStyle, OutlineStyle},
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
use crate::{
|
|
12
|
+
color::{ToColorColor as _, contrast_ratio},
|
|
13
|
+
kurbo_css::Edge,
|
|
14
|
+
render::{ElementCx, PhysicalTracks},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/// A darker version of a colour (mirrors WebKit/Blink's `Color::Dark`): the
|
|
18
|
+
/// brightest channel is reduced by a fixed amount and the others scaled to match,
|
|
19
|
+
/// preserving hue.
|
|
20
|
+
fn darken(color: Color) -> Color {
|
|
21
|
+
let [r, g, b, a] = color.components;
|
|
22
|
+
let v = r.max(g).max(b);
|
|
23
|
+
if v == 0.0 {
|
|
24
|
+
return color;
|
|
25
|
+
}
|
|
26
|
+
let multiplier = ((v - 0.33) / v).max(0.0);
|
|
27
|
+
Color::new([r * multiplier, g * multiplier, b * multiplier, a])
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// A lighter version of a colour (mirrors WebKit/Blink's `Color::Light`): the
|
|
31
|
+
/// brightest channel is increased by a fixed amount and the others scaled to
|
|
32
|
+
/// match, preserving hue.
|
|
33
|
+
fn lighten(color: Color) -> Color {
|
|
34
|
+
let [r, g, b, a] = color.components;
|
|
35
|
+
let v = r.max(g).max(b);
|
|
36
|
+
if v == 0.0 {
|
|
37
|
+
// Pure black: WebKit uses a fixed dark grey (0x545454).
|
|
38
|
+
return Color::new([0.33, 0.33, 0.33, a]);
|
|
39
|
+
}
|
|
40
|
+
let multiplier = (v + 0.33).min(1.0) / v;
|
|
41
|
+
Color::new([r * multiplier, g * multiplier, b * multiplier, a])
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// The colour of a single edge of an `inset`/`outset` border.
|
|
45
|
+
///
|
|
46
|
+
/// An `outset` border is raised: the top/left edges use the "lighter" shade and
|
|
47
|
+
/// the bottom/right edges the "darker" shade (`inset` is the reverse). The exact
|
|
48
|
+
/// shading matches Chrome's `CalculateInsetOutsetColor`:
|
|
49
|
+
///
|
|
50
|
+
/// * The darker edge is always darkened.
|
|
51
|
+
/// * The lighter edge keeps the base colour unchanged, *except* for colours dark
|
|
52
|
+
/// enough that the darkened edge wouldn't have enough contrast against the base
|
|
53
|
+
/// colour, which are lightened instead so the bevel stays visible.
|
|
54
|
+
fn beveled_edge_color(color: Color, edge: Edge, inset: bool) -> Color {
|
|
55
|
+
let top_or_left = matches!(edge, Edge::Top | Edge::Left);
|
|
56
|
+
let should_darken = top_or_left == inset;
|
|
57
|
+
|
|
58
|
+
let dark_color = darken(color);
|
|
59
|
+
if should_darken {
|
|
60
|
+
return dark_color;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Lighter edge. Chrome uses the base colour as-is unless the colour is dark
|
|
64
|
+
// enough that the darkened edge lacks contrast against it. The red/green
|
|
65
|
+
// shortcut mirrors Chrome: for those colours the contrast is always
|
|
66
|
+
// sufficient, so the base colour is used without a contrast check.
|
|
67
|
+
let [r, g, _, _] = color.components;
|
|
68
|
+
if r >= 150.0 / 255.0 || g >= 92.0 / 255.0 {
|
|
69
|
+
return color;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Lighten only when the darkened edge would be too low-contrast to read.
|
|
73
|
+
const MIN_CONTRAST_RATIO: f32 = 1.75;
|
|
74
|
+
if contrast_ratio(color, dark_color) < MIN_CONTRAST_RATIO {
|
|
75
|
+
lighten(color)
|
|
76
|
+
} else {
|
|
77
|
+
color
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// The (outer half, inner half) colours of a single edge of a `groove`/`ridge`
|
|
82
|
+
/// border.
|
|
83
|
+
///
|
|
84
|
+
/// A `groove` looks carved into the page: its outer half is shaded like `inset`
|
|
85
|
+
/// and its inner half like `outset`. `ridge` is the reverse (raised).
|
|
86
|
+
fn grooved_edge_colors(color: Color, edge: Edge, ridge: bool) -> (Color, Color) {
|
|
87
|
+
let outer = beveled_edge_color(color, edge, !ridge);
|
|
88
|
+
let inner = beveled_edge_color(color, edge, ridge);
|
|
89
|
+
(outer, inner)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// The `(dash, gap)` lengths of a dashed border, as multiples of the border
|
|
93
|
+
/// thickness.
|
|
94
|
+
///
|
|
95
|
+
/// Matches Chrome: borders that are at least 3px use a 2:1 dash-to-gap ratio,
|
|
96
|
+
/// while thinner borders use longer dashes and gaps (3:2) so they still read as
|
|
97
|
+
/// dashes rather than dots or a solid line. `thickness` and `scale` are in device
|
|
98
|
+
/// pixels; the 3px threshold is applied in CSS pixels.
|
|
99
|
+
fn dashed_ratios(thickness: f64, scale: f64) -> (f64, f64) {
|
|
100
|
+
if thickness >= 3.0 * scale {
|
|
101
|
+
(2.0, 1.0)
|
|
102
|
+
} else {
|
|
103
|
+
(3.0, 2.0)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// Return `count` points spaced `spacing` apart (by arc length) along `path`,
|
|
108
|
+
/// starting at its beginning. Used to place dots along a rounded border.
|
|
109
|
+
fn sample_points_along_path(path: &BezPath, spacing: f64, count: usize) -> Vec<Point> {
|
|
110
|
+
// Flatten to a polyline so we can walk it by arc length.
|
|
111
|
+
let mut poly: Vec<Point> = Vec::new();
|
|
112
|
+
kurbo::flatten(path.iter(), 0.1, |el| match el {
|
|
113
|
+
PathEl::MoveTo(p) | PathEl::LineTo(p) => poly.push(p),
|
|
114
|
+
PathEl::ClosePath => {
|
|
115
|
+
if let Some(&first) = poly.first() {
|
|
116
|
+
poly.push(first);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
_ => {}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
let mut points = Vec::with_capacity(count);
|
|
123
|
+
if poly.len() < 2 {
|
|
124
|
+
return points;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let mut seg = 0;
|
|
128
|
+
let mut seg_start = 0.0;
|
|
129
|
+
let mut seg_len = (poly[1] - poly[0]).hypot();
|
|
130
|
+
for i in 0..count {
|
|
131
|
+
let target = i as f64 * spacing;
|
|
132
|
+
while seg + 2 < poly.len() && target > seg_start + seg_len {
|
|
133
|
+
seg_start += seg_len;
|
|
134
|
+
seg += 1;
|
|
135
|
+
seg_len = (poly[seg + 1] - poly[seg]).hypot();
|
|
136
|
+
}
|
|
137
|
+
let t = if seg_len > 0.0 {
|
|
138
|
+
((target - seg_start) / seg_len).clamp(0.0, 1.0)
|
|
139
|
+
} else {
|
|
140
|
+
0.0
|
|
141
|
+
};
|
|
142
|
+
points.push(poly[seg].lerp(poly[seg + 1], t));
|
|
143
|
+
}
|
|
144
|
+
points
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
impl ElementCx<'_, '_> {
|
|
148
|
+
/// Draw all borders for a node
|
|
149
|
+
pub(crate) fn draw_border(&self, scene: &mut impl PaintScene) {
|
|
150
|
+
let style = &*self.style;
|
|
151
|
+
let border = style.get_border();
|
|
152
|
+
let current_color = style.clone_color();
|
|
153
|
+
|
|
154
|
+
// Fast path: a uniform-width, single-color solid border whose corners are
|
|
155
|
+
// all circular arcs (equal x and y radius — sharp rectangles, ordinary
|
|
156
|
+
// rounded rectangles and circles all qualify) can be drawn as a single
|
|
157
|
+
// stroke along its center-line instead of a fill per edge. Besides being
|
|
158
|
+
// faster, this works around Vello seam artifacts at the quarter-arc joins
|
|
159
|
+
// when a thin circular/elliptical border is drawn as the
|
|
160
|
+
// border-box/padding-box annulus.
|
|
161
|
+
//
|
|
162
|
+
// A full ellipse (`border-radius: 50%` on a non-square box) has the same
|
|
163
|
+
// seam problem but no rounded-rect representation, so it is stroked as an
|
|
164
|
+
// ellipse.
|
|
165
|
+
let all_solid = border.border_top_style == BorderStyle::Solid
|
|
166
|
+
&& border.border_right_style == BorderStyle::Solid
|
|
167
|
+
&& border.border_bottom_style == BorderStyle::Solid
|
|
168
|
+
&& border.border_left_style == BorderStyle::Solid;
|
|
169
|
+
if all_solid {
|
|
170
|
+
let color = border
|
|
171
|
+
.border_top_color
|
|
172
|
+
.resolve_to_absolute(¤t_color)
|
|
173
|
+
.as_srgb_color();
|
|
174
|
+
let same_color = [
|
|
175
|
+
&border.border_right_color,
|
|
176
|
+
&border.border_bottom_color,
|
|
177
|
+
&border.border_left_color,
|
|
178
|
+
]
|
|
179
|
+
.iter()
|
|
180
|
+
.all(|c| c.resolve_to_absolute(¤t_color).as_srgb_color() == color);
|
|
181
|
+
let bw = self.frame.border_width;
|
|
182
|
+
let uniform_width = bw.x0 == bw.y0 && bw.x0 == bw.x1 && bw.x0 == bw.y1 && bw.x0 > 0.0;
|
|
183
|
+
if same_color && uniform_width && color.components[3] > 0.0 {
|
|
184
|
+
let bb = self.frame.border_box;
|
|
185
|
+
let pb = self.frame.padding_box;
|
|
186
|
+
let width = (bb.width() - pb.width()) / 2.0;
|
|
187
|
+
// Miter join keeps sharp corners square. It has no effect on rounded
|
|
188
|
+
// corners, whose arcs meet the straight edges tangentially.
|
|
189
|
+
let stroke = Stroke::new(width).with_join(Join::Miter);
|
|
190
|
+
|
|
191
|
+
if self.frame.is_uniform_corner_border() {
|
|
192
|
+
let radii = self.frame.border_radii;
|
|
193
|
+
// The stroke matches the CSS border shape only when each corner is
|
|
194
|
+
// sharp or its radius is at least the border width (outer radius
|
|
195
|
+
// `r`, inner radius `r - width`, concentric). For 0 < r < width CSS
|
|
196
|
+
// draws a rounded outer edge against a sharp inner corner, which a
|
|
197
|
+
// stroke can't represent — leave those to the per-edge fill.
|
|
198
|
+
let is_exact = |r: Vec2| r.x == 0.0 || r.x >= width;
|
|
199
|
+
let stroke_is_exact = is_exact(radii.top_left)
|
|
200
|
+
&& is_exact(radii.top_right)
|
|
201
|
+
&& is_exact(radii.bottom_right)
|
|
202
|
+
&& is_exact(radii.bottom_left);
|
|
203
|
+
if stroke_is_exact {
|
|
204
|
+
let half_width = width / 2.0;
|
|
205
|
+
// Center-line rect, and corner radii shrunk to match.
|
|
206
|
+
let centerline_box = bb - Insets::uniform(half_width);
|
|
207
|
+
let centerline_radius = |r: Vec2| (r.x - half_width).max(0.0);
|
|
208
|
+
let radii = kurbo::RoundedRectRadii::new(
|
|
209
|
+
centerline_radius(radii.top_left),
|
|
210
|
+
centerline_radius(radii.top_right),
|
|
211
|
+
centerline_radius(radii.bottom_right),
|
|
212
|
+
centerline_radius(radii.bottom_left),
|
|
213
|
+
);
|
|
214
|
+
let rr = kurbo::RoundedRect::from_rect(centerline_box, radii);
|
|
215
|
+
scene.stroke(&stroke, self.transform, color, None, &rr);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
} else if self.frame.is_elliptical_border() {
|
|
219
|
+
// Center-line radii, straight from the boxes.
|
|
220
|
+
let rx = (bb.width() + pb.width()) / 4.0;
|
|
221
|
+
let ry = (bb.height() + pb.height()) / 4.0;
|
|
222
|
+
let e = kurbo::Ellipse::new(bb.center(), (rx, ry), 0.0);
|
|
223
|
+
scene.stroke(&stroke, self.transform, color, None, &e);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// (colour, path) pairs to be filled. Several entries may share a colour;
|
|
230
|
+
// they are grouped before filling so that adjacent same-coloured regions
|
|
231
|
+
// are drawn together, avoiding anti-aliasing seams between them.
|
|
232
|
+
//
|
|
233
|
+
// At most 8 entries: 4 edges, each of which can contribute 2 (the outer
|
|
234
|
+
// and inner halves of a `groove`/`ridge`).
|
|
235
|
+
let mut borders: SmallVec<[(Color, BezPath); 8]> = SmallVec::new();
|
|
236
|
+
|
|
237
|
+
for &edge in &[Edge::Top, Edge::Right, Edge::Bottom, Edge::Left] {
|
|
238
|
+
let (color, edge_style) = match edge {
|
|
239
|
+
Edge::Top => (&border.border_top_color, border.border_top_style),
|
|
240
|
+
Edge::Right => (&border.border_right_color, border.border_right_style),
|
|
241
|
+
Edge::Bottom => (&border.border_bottom_color, border.border_bottom_style),
|
|
242
|
+
Edge::Left => (&border.border_left_color, border.border_left_style),
|
|
243
|
+
};
|
|
244
|
+
let color = color.resolve_to_absolute(¤t_color).as_srgb_color();
|
|
245
|
+
|
|
246
|
+
if color.components[3] <= 0.0 {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
match edge_style {
|
|
251
|
+
// `none`/`hidden` produce a zero-width border during layout, but
|
|
252
|
+
// guard against drawing them anyway.
|
|
253
|
+
BorderStyle::None | BorderStyle::Hidden => {}
|
|
254
|
+
|
|
255
|
+
// Dashed and dotted edges are drawn immediately as their own
|
|
256
|
+
// (clipped) shapes rather than being batched with the solid edges.
|
|
257
|
+
BorderStyle::Dotted => self.draw_dotted_border_edge(scene, edge, color),
|
|
258
|
+
BorderStyle::Dashed => self.draw_dashed_border_edge(scene, edge, color),
|
|
259
|
+
|
|
260
|
+
// A double border is two solid lines separated by a gap, splitting
|
|
261
|
+
// the border width into three equal parts (outer line / gap / inner
|
|
262
|
+
// line). Both lines share a color, so both rings are placed into a
|
|
263
|
+
// single path.
|
|
264
|
+
BorderStyle::Double => {
|
|
265
|
+
// Needs at least 3px (one device pixel per line and per gap) to
|
|
266
|
+
// render as two lines; thinner borders fall back to a solid
|
|
267
|
+
// fill, matching browser behaviour.
|
|
268
|
+
let path = if self.edge_width(edge) < 3.0 * self.scale {
|
|
269
|
+
self.frame.border_edge_shape(edge)
|
|
270
|
+
} else {
|
|
271
|
+
let mut path = self
|
|
272
|
+
.frame
|
|
273
|
+
.border_slice(0.0, 1.0 / 3.0)
|
|
274
|
+
.border_edge_shape(edge);
|
|
275
|
+
path.extend(
|
|
276
|
+
&self
|
|
277
|
+
.frame
|
|
278
|
+
.border_slice(2.0 / 3.0, 1.0)
|
|
279
|
+
.border_edge_shape(edge),
|
|
280
|
+
);
|
|
281
|
+
path
|
|
282
|
+
};
|
|
283
|
+
borders.push((color, path));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// `inset`/`outset` are solid, but each edge is shaded lighter or
|
|
287
|
+
// darker to give a bevelled (3D) appearance.
|
|
288
|
+
BorderStyle::Inset | BorderStyle::Outset => {
|
|
289
|
+
let inset = edge_style == BorderStyle::Inset;
|
|
290
|
+
let shade = beveled_edge_color(color, edge, inset);
|
|
291
|
+
borders.push((shade, self.frame.border_edge_shape(edge)));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// `groove`/`ridge` split each edge into an outer and an inner half,
|
|
295
|
+
// each shaded as if it were `inset`/`outset`, producing a carved or
|
|
296
|
+
// raised ridge.
|
|
297
|
+
BorderStyle::Groove | BorderStyle::Ridge => {
|
|
298
|
+
let ridge = edge_style == BorderStyle::Ridge;
|
|
299
|
+
let (outer, inner) = grooved_edge_colors(color, edge, ridge);
|
|
300
|
+
borders.push((
|
|
301
|
+
outer,
|
|
302
|
+
self.frame.border_slice(0.0, 0.5).border_edge_shape(edge),
|
|
303
|
+
));
|
|
304
|
+
borders.push((
|
|
305
|
+
inner,
|
|
306
|
+
self.frame.border_slice(0.5, 1.0).border_edge_shape(edge),
|
|
307
|
+
));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Solid fills the whole edge region with the border colour.
|
|
311
|
+
BorderStyle::Solid => {
|
|
312
|
+
borders.push((color, self.frame.border_edge_shape(edge)));
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if borders.is_empty() {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Group together identical colors by sorting, then fill each group as a
|
|
322
|
+
// single path.
|
|
323
|
+
borders.sort_unstable_by(|a, b| {
|
|
324
|
+
a.0.components
|
|
325
|
+
.partial_cmp(&b.0.components)
|
|
326
|
+
.unwrap_or(std::cmp::Ordering::Equal)
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
let mut start = 0;
|
|
330
|
+
while start < borders.len() {
|
|
331
|
+
let color = borders[start].0;
|
|
332
|
+
let mut path = std::mem::take(&mut borders[start].1);
|
|
333
|
+
let mut next = start + 1;
|
|
334
|
+
while next < borders.len() && borders[next].0 == color {
|
|
335
|
+
path.extend(&borders[next].1);
|
|
336
|
+
next += 1;
|
|
337
|
+
}
|
|
338
|
+
scene.fill(Fill::NonZero, self.transform, color, None, &path);
|
|
339
|
+
start = next;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/// The border width (in device pixels) of a single edge.
|
|
344
|
+
fn edge_width(&self, edge: Edge) -> f64 {
|
|
345
|
+
match edge {
|
|
346
|
+
Edge::Top => self.frame.border_width.y0,
|
|
347
|
+
Edge::Bottom => self.frame.border_width.y1,
|
|
348
|
+
Edge::Left => self.frame.border_width.x0,
|
|
349
|
+
Edge::Right => self.frame.border_width.x1,
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/// Draw a single `dashed` border edge.
|
|
354
|
+
///
|
|
355
|
+
/// The dashes are produced by stroking the centre line of the border with a
|
|
356
|
+
/// dash pattern (butt caps give them flat, square ends). Everything is clipped
|
|
357
|
+
/// to the edge's region (the same trapezoid used by the solid path) so corners
|
|
358
|
+
/// are mitred, adjacent edges of different colors don't overlap, and any
|
|
359
|
+
/// border-radius is respected.
|
|
360
|
+
fn draw_dashed_border_edge(&self, scene: &mut impl PaintScene, edge: Edge, color: Color) {
|
|
361
|
+
let thickness = self.edge_width(edge);
|
|
362
|
+
if thickness <= 0.0 {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
let (dash_ratio, gap_ratio) = dashed_ratios(thickness, self.scale);
|
|
367
|
+
|
|
368
|
+
// Work out the centre line to stroke and the dash/gap lengths along it.
|
|
369
|
+
let (centerline, dash, gap) = if self.frame.has_border_radius() {
|
|
370
|
+
// Rounded corners: stroke the rounded centre line running through the
|
|
371
|
+
// whole perimeter. Every edge uses the same centre line and pattern, so
|
|
372
|
+
// dashes stay continuous and aligned as they wrap around each corner.
|
|
373
|
+
// Dash and gap keep their ratio, sized so a whole number of periods fit
|
|
374
|
+
// exactly around the perimeter (kurbo merges the dash across the seam).
|
|
375
|
+
let mut centerline = self.frame.border_slice(0.0, 0.5).padding_box_path();
|
|
376
|
+
centerline.close_path();
|
|
377
|
+
let perimeter = centerline.perimeter(0.1);
|
|
378
|
+
if perimeter <= 0.0 {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
let period0 = (dash_ratio + gap_ratio) * thickness;
|
|
382
|
+
let count = (perimeter / period0).round().max(1.0);
|
|
383
|
+
let period = perimeter / count;
|
|
384
|
+
let dash = period * dash_ratio / (dash_ratio + gap_ratio);
|
|
385
|
+
(centerline, dash, period - dash)
|
|
386
|
+
} else {
|
|
387
|
+
// Square corners: stroke a straight line through the middle of the edge,
|
|
388
|
+
// corner to corner. Dash and gap keep their ratio but are scaled so the
|
|
389
|
+
// edge both starts and ends with a dash (covering the corners).
|
|
390
|
+
let bb = self.frame.border_box;
|
|
391
|
+
let half = thickness / 2.0;
|
|
392
|
+
let (start, end, length) = match edge {
|
|
393
|
+
Edge::Top => (
|
|
394
|
+
Point::new(bb.x0, bb.y0 + half),
|
|
395
|
+
Point::new(bb.x1, bb.y0 + half),
|
|
396
|
+
bb.width(),
|
|
397
|
+
),
|
|
398
|
+
Edge::Bottom => (
|
|
399
|
+
Point::new(bb.x0, bb.y1 - half),
|
|
400
|
+
Point::new(bb.x1, bb.y1 - half),
|
|
401
|
+
bb.width(),
|
|
402
|
+
),
|
|
403
|
+
Edge::Left => (
|
|
404
|
+
Point::new(bb.x0 + half, bb.y0),
|
|
405
|
+
Point::new(bb.x0 + half, bb.y1),
|
|
406
|
+
bb.height(),
|
|
407
|
+
),
|
|
408
|
+
Edge::Right => (
|
|
409
|
+
Point::new(bb.x1 - half, bb.y0),
|
|
410
|
+
Point::new(bb.x1 - half, bb.y1),
|
|
411
|
+
bb.height(),
|
|
412
|
+
),
|
|
413
|
+
};
|
|
414
|
+
if length <= 0.0 {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
let dash0 = dash_ratio * thickness;
|
|
418
|
+
let gap0 = gap_ratio * thickness;
|
|
419
|
+
// `count` dashes with `count - 1` gaps between them.
|
|
420
|
+
let count = ((length + gap0) / (dash0 + gap0)).round().max(1.0);
|
|
421
|
+
let r = dash_ratio / gap_ratio;
|
|
422
|
+
let gap = length / (count * r + count - 1.0);
|
|
423
|
+
|
|
424
|
+
let mut line = BezPath::new();
|
|
425
|
+
line.move_to(start);
|
|
426
|
+
line.line_to(end);
|
|
427
|
+
(line, r * gap, gap)
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
let stroke = Stroke::new(thickness)
|
|
431
|
+
.with_caps(Cap::Butt)
|
|
432
|
+
.with_dashes(0.0, [dash, gap]);
|
|
433
|
+
let clip = self.frame.border_edge_shape(edge);
|
|
434
|
+
scene.push_clip_layer(Fill::NonZero, self.transform, &clip);
|
|
435
|
+
scene.stroke(&stroke, self.transform, color, None, ¢erline);
|
|
436
|
+
scene.pop_layer();
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/// Draw a single `dotted` border edge.
|
|
440
|
+
///
|
|
441
|
+
/// Dots are filled circles (diameter == border thickness). Unlike dashes they
|
|
442
|
+
/// can't be produced by stroking, because kurbo doesn't emit zero-length dashes
|
|
443
|
+
/// (so a round-capped dash pattern would render nothing); drawing them
|
|
444
|
+
/// explicitly also lets us anchor a dot in each square corner. Everything is
|
|
445
|
+
/// clipped to the edge's region, as for the other styles.
|
|
446
|
+
fn draw_dotted_border_edge(&self, scene: &mut impl PaintScene, edge: Edge, color: Color) {
|
|
447
|
+
let thickness = self.edge_width(edge);
|
|
448
|
+
if thickness <= 0.0 {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
let radius = thickness / 2.0;
|
|
452
|
+
|
|
453
|
+
let mut path = BezPath::new();
|
|
454
|
+
if self.frame.has_border_radius() {
|
|
455
|
+
// Rounded corners: dots spaced evenly around the rounded centre line so
|
|
456
|
+
// the ring wraps seamlessly around the corners.
|
|
457
|
+
let mut centerline = self.frame.border_slice(0.0, 0.5).padding_box_path();
|
|
458
|
+
centerline.close_path();
|
|
459
|
+
let perimeter = centerline.perimeter(0.1);
|
|
460
|
+
if perimeter <= 0.0 {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
let count = (perimeter / (2.0 * thickness)).round().max(1.0);
|
|
464
|
+
let spacing = perimeter / count;
|
|
465
|
+
for center in sample_points_along_path(¢erline, spacing, count as usize) {
|
|
466
|
+
path.extend(Circle::new(center, radius).path_elements(0.1));
|
|
467
|
+
}
|
|
468
|
+
} else {
|
|
469
|
+
// Square corners: a dot is anchored in each corner (both ends of the
|
|
470
|
+
// edge, inset by the radius so it sits snugly in the corner) and the
|
|
471
|
+
// rest are spread evenly between them.
|
|
472
|
+
let bb = self.frame.border_box;
|
|
473
|
+
let length = match edge {
|
|
474
|
+
Edge::Top | Edge::Bottom => bb.width(),
|
|
475
|
+
Edge::Left | Edge::Right => bb.height(),
|
|
476
|
+
};
|
|
477
|
+
if length <= 0.0 {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
let dot_center = |along: f64| -> Point {
|
|
481
|
+
match edge {
|
|
482
|
+
Edge::Top => Point::new(bb.x0 + along, bb.y0 + radius),
|
|
483
|
+
Edge::Bottom => Point::new(bb.x0 + along, bb.y1 - radius),
|
|
484
|
+
Edge::Left => Point::new(bb.x0 + radius, bb.y0 + along),
|
|
485
|
+
Edge::Right => Point::new(bb.x1 - radius, bb.y0 + along),
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
let span = length - thickness;
|
|
490
|
+
if span <= 0.0 {
|
|
491
|
+
// Edge too short to fit two dots; place a single centred dot.
|
|
492
|
+
let center = dot_center(length / 2.0);
|
|
493
|
+
path.extend(Circle::new(center, radius).path_elements(0.1));
|
|
494
|
+
} else {
|
|
495
|
+
// Aim for a centre-to-centre spacing of about two dot diameters,
|
|
496
|
+
// then adjust it so the first and last dots land in the corners.
|
|
497
|
+
let gaps = (span / (2.0 * thickness)).round().max(1.0) as usize;
|
|
498
|
+
let spacing = span / gaps as f64;
|
|
499
|
+
for i in 0..=gaps {
|
|
500
|
+
let center = dot_center(radius + i as f64 * spacing);
|
|
501
|
+
path.extend(Circle::new(center, radius).path_elements(0.1));
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
let clip = self.frame.border_edge_shape(edge);
|
|
507
|
+
scene.push_clip_layer(Fill::NonZero, self.transform, &clip);
|
|
508
|
+
scene.fill(Fill::NonZero, self.transform, color, None, &path);
|
|
509
|
+
scene.pop_layer();
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
pub(crate) fn draw_table_borders(&self, scene: &mut impl PaintScene) {
|
|
513
|
+
let SpecialElementData::TableRoot(table) = &self.element.special_data else {
|
|
514
|
+
return;
|
|
515
|
+
};
|
|
516
|
+
// Borders are only handled at the table level when BorderCollapse::Collapse
|
|
517
|
+
if table.border_collapse != BorderCollapse::Collapse {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
let Some(grid_info) = &mut *table.computed_grid_info.borrow_mut() else {
|
|
522
|
+
return;
|
|
523
|
+
};
|
|
524
|
+
let Some(border_style) = table.border_style.as_deref() else {
|
|
525
|
+
return;
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
let outer_border_style = self.style.get_border();
|
|
529
|
+
|
|
530
|
+
let cols = PhysicalTracks::from_tracks(&grid_info.columns);
|
|
531
|
+
let rows = PhysicalTracks::from_tracks(&grid_info.rows);
|
|
532
|
+
|
|
533
|
+
// TODO: support different colors for different borders
|
|
534
|
+
let current_color = self.style.clone_color();
|
|
535
|
+
let border_color = border_style
|
|
536
|
+
.border_top_color
|
|
537
|
+
.resolve_to_absolute(¤t_color)
|
|
538
|
+
.as_srgb_color();
|
|
539
|
+
|
|
540
|
+
// No need to draw transparent borders (as they won't be visible anyway)
|
|
541
|
+
if border_color == Color::TRANSPARENT {
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// Border widths are not adjusted for border-style in computed styles,
|
|
546
|
+
// so a border with `none`/`hidden` style must be treated as zero-width.
|
|
547
|
+
if border_style.border_top_style.none_or_hidden() {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// The borders are laid out as the gutters between the tracks, and as the border
|
|
552
|
+
// of the table around them. Track positions are relative to the table's border box.
|
|
553
|
+
let border_width = table.style.gap.map(|gap| gap.into_raw().value() as f64);
|
|
554
|
+
let left = cols.origin() as f64 - border_width.width;
|
|
555
|
+
let right = (cols.origin() + cols.span()) as f64 + border_width.width;
|
|
556
|
+
let top = rows.origin() as f64 - border_width.height;
|
|
557
|
+
let bottom = (rows.origin() + rows.span()) as f64 + border_width.height;
|
|
558
|
+
|
|
559
|
+
let mut fill = |x0: f64, y0: f64, x1: f64, y1: f64| {
|
|
560
|
+
let shape = Rect::new(x0, y0, x1, y1).scale_from_origin(self.scale);
|
|
561
|
+
scene.fill(Fill::NonZero, self.transform, border_color, None, &shape);
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
// Horizontal borders: the outer edges, then the gutters between adjacent row tracks.
|
|
565
|
+
// The vertical borders are drawn between them so that no corner is painted twice.
|
|
566
|
+
if outer_border_style.border_top_style != BorderStyle::Hidden {
|
|
567
|
+
fill(left, top, right, top + border_width.height);
|
|
568
|
+
}
|
|
569
|
+
if outer_border_style.border_bottom_style != BorderStyle::Hidden {
|
|
570
|
+
fill(left, bottom - border_width.height, right, bottom);
|
|
571
|
+
}
|
|
572
|
+
for (prev, next) in rows.iter().zip(rows.iter().skip(1)) {
|
|
573
|
+
fill(left, prev.end as f64, right, next.start as f64);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// Vertical borders, one segment per row
|
|
577
|
+
for row in rows.iter() {
|
|
578
|
+
let (y0, y1) = (row.start as f64, row.end as f64);
|
|
579
|
+
if outer_border_style.border_left_style != BorderStyle::Hidden {
|
|
580
|
+
fill(left, y0, left + border_width.width, y1);
|
|
581
|
+
}
|
|
582
|
+
if outer_border_style.border_right_style != BorderStyle::Hidden {
|
|
583
|
+
fill(right - border_width.width, y0, right, y1);
|
|
584
|
+
}
|
|
585
|
+
for (prev, next) in cols.iter().zip(cols.iter().skip(1)) {
|
|
586
|
+
fill(prev.end as f64, y0, next.start as f64, y1);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/// ❌ dotted - Defines a dotted border
|
|
592
|
+
/// ❌ dashed - Defines a dashed border
|
|
593
|
+
/// ✅ solid - Defines a solid border
|
|
594
|
+
/// ❌ double - Defines a double border
|
|
595
|
+
/// ❌ groove - Defines a 3D grooved border. The effect depends on the border-color value
|
|
596
|
+
/// ❌ ridge - Defines a 3D ridged border. The effect depends on the border-color value
|
|
597
|
+
/// ❌ inset - Defines a 3D inset border. The effect depends on the border-color value
|
|
598
|
+
/// ❌ outset - Defines a 3D outset border. The effect depends on the border-color value
|
|
599
|
+
/// ✅ none - Defines no border
|
|
600
|
+
/// ✅ hidden - Defines a hidden border
|
|
601
|
+
pub(crate) fn draw_outline(&self, scene: &mut impl PaintScene) {
|
|
602
|
+
let outline = self.style.get_outline();
|
|
603
|
+
|
|
604
|
+
let current_color = self.style.clone_color();
|
|
605
|
+
let color = outline
|
|
606
|
+
.outline_color
|
|
607
|
+
.resolve_to_absolute(¤t_color)
|
|
608
|
+
.as_srgb_color();
|
|
609
|
+
|
|
610
|
+
let style = match outline.outline_style {
|
|
611
|
+
OutlineStyle::Auto => return,
|
|
612
|
+
OutlineStyle::BorderStyle(style) => style,
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
let path = match style {
|
|
616
|
+
BorderStyle::None | BorderStyle::Hidden => return,
|
|
617
|
+
BorderStyle::Solid => self.frame.outline(),
|
|
618
|
+
|
|
619
|
+
// TODO: Implement other border styles
|
|
620
|
+
BorderStyle::Inset
|
|
621
|
+
| BorderStyle::Groove
|
|
622
|
+
| BorderStyle::Outset
|
|
623
|
+
| BorderStyle::Ridge
|
|
624
|
+
| BorderStyle::Dotted
|
|
625
|
+
| BorderStyle::Dashed
|
|
626
|
+
| BorderStyle::Double => self.frame.outline(),
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
scene.fill(Fill::NonZero, self.transform, color, None, &path);
|
|
630
|
+
}
|
|
631
|
+
}
|