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,747 @@
|
|
|
1
|
+
use anyrender::PaintScene;
|
|
2
|
+
use blitz_dom::{BaseDocument, NodeId, node::TextBrush, util::ToColorColor};
|
|
3
|
+
use kurbo::{Affine, BezPath, Cap, Circle, Rect, Stroke};
|
|
4
|
+
use parley::{Affinity, Cursor, Layout, Line, PositionedLayoutItem, Selection};
|
|
5
|
+
use peniko::Fill;
|
|
6
|
+
use std::collections::HashMap;
|
|
7
|
+
use style::properties::generated::longhands::text_decoration_style::computed_value::T as TextDecorationStyle;
|
|
8
|
+
use style::values::computed::{
|
|
9
|
+
Length, LengthPercentage, TextDecorationLength, TextDecorationLine, TextUnderlinePosition,
|
|
10
|
+
};
|
|
11
|
+
use style::values::generics::text::{GenericTextDecorationInset, GenericTextDecorationLength};
|
|
12
|
+
|
|
13
|
+
use crate::color::{Color, ToColorColor as _};
|
|
14
|
+
use crate::{FONT_EMBOLDEN_ENABLED, SELECTION_COLOR};
|
|
15
|
+
|
|
16
|
+
/// Draw the backgrounds of inline elements (e.g. `<span style="background: ...">`).
|
|
17
|
+
///
|
|
18
|
+
/// Each glyph run carries the node id of the innermost inline element it belongs to
|
|
19
|
+
/// (via its brush). We look up that node's `background-color` and, if non-transparent,
|
|
20
|
+
/// fill a rectangle covering the run's advance and its font's ascent/descent so that the
|
|
21
|
+
/// background sits behind the text.
|
|
22
|
+
///
|
|
23
|
+
/// The inline root's own background is painted separately (as a normal block box), so
|
|
24
|
+
/// runs belonging to the root are skipped to avoid drawing it twice.
|
|
25
|
+
pub(crate) fn draw_inline_backgrounds<'a>(
|
|
26
|
+
scene: &mut impl PaintScene,
|
|
27
|
+
lines: impl Iterator<Item = Line<'a, TextBrush>>,
|
|
28
|
+
doc: &BaseDocument,
|
|
29
|
+
transform: Affine,
|
|
30
|
+
inline_root_id: NodeId,
|
|
31
|
+
) {
|
|
32
|
+
for line in lines {
|
|
33
|
+
for item in line.items() {
|
|
34
|
+
let PositionedLayoutItem::GlyphRun(glyph_run) = item else {
|
|
35
|
+
continue;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
let node_id = glyph_run.style().brush.id;
|
|
39
|
+
if node_id == inline_root_id {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let Some(styles) = doc.get_node(node_id).and_then(|node| node.primary_styles()) else {
|
|
44
|
+
continue;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
let current_color = styles.clone_color();
|
|
48
|
+
let bg_color = styles
|
|
49
|
+
.get_background()
|
|
50
|
+
.background_color
|
|
51
|
+
.resolve_to_absolute(¤t_color)
|
|
52
|
+
.as_srgb_color();
|
|
53
|
+
if bg_color == Color::TRANSPARENT {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let metrics = glyph_run.run().font_metrics();
|
|
58
|
+
let x = glyph_run.offset() as f64;
|
|
59
|
+
let w = glyph_run.advance() as f64;
|
|
60
|
+
let baseline = glyph_run.baseline() as f64;
|
|
61
|
+
let y0 = baseline - metrics.ascent as f64;
|
|
62
|
+
let y1 = baseline + metrics.descent as f64;
|
|
63
|
+
let rect = Rect::new(x, y0, x + w, y1);
|
|
64
|
+
|
|
65
|
+
scene.fill(Fill::NonZero, transform, bg_color, None, &rect);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Per-font-face cache of the OS/2 `usWinAscent / unitsPerEm` ratio, keyed by the font
|
|
71
|
+
/// blob's unique id and face index. `None` is cached for fonts without a usable OS/2 or
|
|
72
|
+
/// head table so they aren't re-parsed either.
|
|
73
|
+
type WinAscentCache = HashMap<(u64, u32), Option<f32>>;
|
|
74
|
+
|
|
75
|
+
/// The font's OS/2 `usWinAscent`, in the same (device) pixels as `font_size`.
|
|
76
|
+
///
|
|
77
|
+
/// This is the ascent browsers use as the top of the "em box" when positioning overlines. It
|
|
78
|
+
/// is typically taller than the hhea ascent Parley exposes via [`parley::layout::run::RunMetrics`].
|
|
79
|
+
/// Returns `None` when the font has no OS/2 table.
|
|
80
|
+
fn win_ascent(cache: &mut WinAscentCache, font: &parley::FontData, font_size: f32) -> Option<f32> {
|
|
81
|
+
let ratio = *cache
|
|
82
|
+
.entry((font.data.id(), font.index))
|
|
83
|
+
.or_insert_with(|| win_ascent_ratio(font));
|
|
84
|
+
Some(ratio? * font_size)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// The unitless `usWinAscent / unitsPerEm` ratio for a font face, or `None` when the font
|
|
88
|
+
/// has no usable head/OS/2 table.
|
|
89
|
+
fn win_ascent_ratio(font: &parley::FontData) -> Option<f32> {
|
|
90
|
+
use skrifa::raw::{FontRef, TableProvider as _};
|
|
91
|
+
let font_ref = FontRef::from_index(font.data.as_ref(), font.index).ok()?;
|
|
92
|
+
let units_per_em = font_ref.head().ok()?.units_per_em();
|
|
93
|
+
let win_ascent = font_ref.os2().ok()?.us_win_ascent();
|
|
94
|
+
Some(win_ascent as f32 / units_per_em as f32)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// Mirrors Blink's `SelectBestDashGap`: choose the gap length (as close as possible to
|
|
98
|
+
/// `gap_length`) that fits a whole number of `dash_length` dashes across `stroke_length`,
|
|
99
|
+
/// so dashes are evenly distributed and a dash lands at each end of the line.
|
|
100
|
+
fn select_best_dash_gap(stroke_length: f64, dash_length: f64, gap_length: f64) -> f64 {
|
|
101
|
+
let available_length = stroke_length + gap_length;
|
|
102
|
+
let min_num_dashes = (available_length / (dash_length + gap_length)).floor();
|
|
103
|
+
let max_num_dashes = min_num_dashes + 1.0;
|
|
104
|
+
let min_num_gaps = min_num_dashes - 1.0;
|
|
105
|
+
let max_num_gaps = max_num_dashes - 1.0;
|
|
106
|
+
if min_num_gaps < 1.0 {
|
|
107
|
+
return gap_length;
|
|
108
|
+
}
|
|
109
|
+
let min_gap = (stroke_length - min_num_dashes * dash_length) / min_num_gaps;
|
|
110
|
+
let max_gap = (stroke_length - max_num_dashes * dash_length) / max_num_gaps;
|
|
111
|
+
if max_gap <= 0.0 || (min_gap - gap_length).abs() < (max_gap - gap_length).abs() {
|
|
112
|
+
min_gap
|
|
113
|
+
} else {
|
|
114
|
+
max_gap
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// A text decoration resolved from a single "decorating box" (an element that
|
|
119
|
+
/// sets `text-decoration-line`), ready to be drawn over a glyph run. Because the
|
|
120
|
+
/// `text-decoration-*` properties do not inherit, decorations are propagated from
|
|
121
|
+
/// ancestors rather than inherited, so a run may be covered by several of these.
|
|
122
|
+
///
|
|
123
|
+
/// Only the run-independent (style-derived) values are stored here so an instance
|
|
124
|
+
/// can be cached on the ancestor stack and reused across every run within the same
|
|
125
|
+
/// decorating box. The `text-decoration-inset` and `text-underline-offset` values
|
|
126
|
+
/// depend on the run's advance/font-size and are resolved lazily when drawing.
|
|
127
|
+
#[derive(Clone)]
|
|
128
|
+
struct ResolvedDecoration {
|
|
129
|
+
line: TextDecorationLine,
|
|
130
|
+
style: TextDecorationStyle,
|
|
131
|
+
color: Color,
|
|
132
|
+
thickness: TextDecorationLength,
|
|
133
|
+
/// `text-underline-offset`; `None` for `auto`. Resolved per run.
|
|
134
|
+
underline_offset: Option<LengthPercentage>,
|
|
135
|
+
/// Whether `text-underline-position: under` is in effect.
|
|
136
|
+
underline_under: bool,
|
|
137
|
+
/// Whether `text-underline-position: from-font` is in effect.
|
|
138
|
+
underline_from_font: bool,
|
|
139
|
+
/// `text-decoration-inset`. Resolved per run.
|
|
140
|
+
inset: GenericTextDecorationInset<LengthPercentage>,
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/// An element on the current ancestor path (inline root -> run node), caching the
|
|
144
|
+
/// values resolved from its computed style so descending into the same node across
|
|
145
|
+
/// consecutive runs doesn't re-resolve them.
|
|
146
|
+
struct DecorationStackEntry {
|
|
147
|
+
node_id: NodeId,
|
|
148
|
+
/// This node's (inherited) text colour, used for the glyphs of runs whose
|
|
149
|
+
/// innermost node is this one.
|
|
150
|
+
text_color: Color,
|
|
151
|
+
/// The decoration this node introduces as a decorating box, if any.
|
|
152
|
+
decoration: Option<ResolvedDecoration>,
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// Resolve the cached style values for a single node into a [`DecorationStackEntry`].
|
|
156
|
+
fn resolve_decoration_entry(doc: &BaseDocument, node_id: NodeId) -> DecorationStackEntry {
|
|
157
|
+
let Some(styles) = doc.get_node(node_id).and_then(|node| node.primary_styles()) else {
|
|
158
|
+
return DecorationStackEntry {
|
|
159
|
+
node_id,
|
|
160
|
+
text_color: Color::BLACK,
|
|
161
|
+
decoration: None,
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
let itext = styles.get_inherited_text();
|
|
166
|
+
let text = styles.get_text();
|
|
167
|
+
let text_color = itext.color.as_color_color();
|
|
168
|
+
|
|
169
|
+
let drawn_lines = TextDecorationLine::UNDERLINE
|
|
170
|
+
| TextDecorationLine::OVERLINE
|
|
171
|
+
| TextDecorationLine::LINE_THROUGH;
|
|
172
|
+
let line = text.text_decoration_line;
|
|
173
|
+
// Decorations propagate through the box tree, and a `display: contents` element
|
|
174
|
+
// generates no box, so its decorations have no effect on descendants.
|
|
175
|
+
let is_contents = styles.clone_display().is_contents();
|
|
176
|
+
let decoration = (!is_contents && line.intersects(drawn_lines)).then(|| {
|
|
177
|
+
// `text-decoration-color: currentColor` (the initial value) resolves against
|
|
178
|
+
// the decorating box's own colour, not the descendant run's.
|
|
179
|
+
let color = text
|
|
180
|
+
.text_decoration_color
|
|
181
|
+
.as_absolute()
|
|
182
|
+
.map(ToColorColor::as_color_color)
|
|
183
|
+
.unwrap_or(text_color);
|
|
184
|
+
|
|
185
|
+
ResolvedDecoration {
|
|
186
|
+
line,
|
|
187
|
+
style: text.text_decoration_style,
|
|
188
|
+
color,
|
|
189
|
+
thickness: text.text_decoration_thickness.clone(),
|
|
190
|
+
underline_offset: itext.text_underline_offset.non_auto(),
|
|
191
|
+
underline_under: itext
|
|
192
|
+
.text_underline_position
|
|
193
|
+
.contains(TextUnderlinePosition::UNDER),
|
|
194
|
+
underline_from_font: itext
|
|
195
|
+
.text_underline_position
|
|
196
|
+
.contains(TextUnderlinePosition::FROM_FONT),
|
|
197
|
+
inset: text.text_decoration_inset.clone(),
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
DecorationStackEntry {
|
|
202
|
+
node_id,
|
|
203
|
+
text_color,
|
|
204
|
+
decoration,
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// The run-dependent geometry used to position and size a decoration, captured from a
|
|
209
|
+
/// single glyph run. Firefox draws a decoration once for the whole decorating box using
|
|
210
|
+
/// the box's own font, so we prefer the values from a run whose innermost node *is* the
|
|
211
|
+
/// decorating box (its own text), falling back to the first run it covers.
|
|
212
|
+
#[derive(Clone)]
|
|
213
|
+
struct DecorationRunGeometry {
|
|
214
|
+
/// The baseline the decoration is positioned from (the run's own, `vertical-align`
|
|
215
|
+
/// shifted, baseline).
|
|
216
|
+
baseline: f32,
|
|
217
|
+
ascent: f32,
|
|
218
|
+
descent: f32,
|
|
219
|
+
underline_offset: f32,
|
|
220
|
+
underline_size: f32,
|
|
221
|
+
strikethrough_size: f32,
|
|
222
|
+
font: parley::FontData,
|
|
223
|
+
font_size: f32,
|
|
224
|
+
css_font_size: f64,
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/// A decoration accumulated across the runs of a single line for one decorating box.
|
|
228
|
+
///
|
|
229
|
+
/// The `text-decoration-*` properties describe the whole box, so we gather the horizontal
|
|
230
|
+
/// extent it covers on the line (`min_x`/`max_x`) and the font geometry to draw with, then
|
|
231
|
+
/// paint a single line spanning the box rather than one segment per run.
|
|
232
|
+
struct LineDecoration {
|
|
233
|
+
node_id: NodeId,
|
|
234
|
+
deco: ResolvedDecoration,
|
|
235
|
+
min_x: f64,
|
|
236
|
+
max_x: f64,
|
|
237
|
+
/// Geometry from a run whose innermost node is the decorating box itself.
|
|
238
|
+
own: Option<DecorationRunGeometry>,
|
|
239
|
+
/// Geometry from the first run the box covers (fallback when it has no own text on
|
|
240
|
+
/// this line, e.g. it only wraps differently-sized descendants).
|
|
241
|
+
first: Option<DecorationRunGeometry>,
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/// Reusable scratch storage for drawing text across all inline formatting contexts in a
|
|
245
|
+
/// document. The vectors are cleared between uses without releasing their allocations.
|
|
246
|
+
#[derive(Default)]
|
|
247
|
+
pub(crate) struct DrawTextContext {
|
|
248
|
+
stack: Vec<DecorationStackEntry>,
|
|
249
|
+
path_scratch: Vec<NodeId>,
|
|
250
|
+
deco_boxes: Vec<LineDecoration>,
|
|
251
|
+
win_ascent_ratios: WinAscentCache,
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/// Resolve the CSS `text-decoration-thickness` to a device-pixel size.
|
|
255
|
+
///
|
|
256
|
+
/// - Percentages are resolved against the font size,
|
|
257
|
+
/// - `from-font` uses the metrics from Parley,
|
|
258
|
+
/// - `auto` uses a thickness of `font-size / 10` (minimum: 1px).
|
|
259
|
+
///
|
|
260
|
+
/// The result is floored to a whole number of device pixels (minimum 1), matching
|
|
261
|
+
/// browsers, which snap decoration thickness to whole pixels.
|
|
262
|
+
fn decoration_size(
|
|
263
|
+
thickness: &TextDecorationLength,
|
|
264
|
+
metric_size: f32,
|
|
265
|
+
css_font_size: f64,
|
|
266
|
+
scale: f64,
|
|
267
|
+
) -> f32 {
|
|
268
|
+
(match thickness {
|
|
269
|
+
GenericTextDecorationLength::LengthPercentage(lp) => {
|
|
270
|
+
lp.resolve(Length::new(css_font_size as f32)).px() as f64 * scale
|
|
271
|
+
}
|
|
272
|
+
GenericTextDecorationLength::FromFont => metric_size as f64,
|
|
273
|
+
GenericTextDecorationLength::Auto => (css_font_size / 10.0).max(1.0) * scale,
|
|
274
|
+
})
|
|
275
|
+
.floor()
|
|
276
|
+
.max(1.0) as f32
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/// Draws a single decoration line of the given `text-decoration-style` spanning
|
|
280
|
+
/// `[x0, x0 + width]`. `baseline` is the line's baseline and `offset` positions the line
|
|
281
|
+
/// relative to it (`y = baseline - offset + size / 2`, growing downward). Geometry is built
|
|
282
|
+
/// explicitly (rather than relying on backend dash support) so it renders consistently
|
|
283
|
+
/// across renderers.
|
|
284
|
+
///
|
|
285
|
+
/// `double_dir` points away from the text (`+1` = down, `-1` = up) and is used to place the
|
|
286
|
+
/// second line of a `double` decoration.
|
|
287
|
+
#[allow(clippy::too_many_arguments)]
|
|
288
|
+
fn draw_decoration_line(
|
|
289
|
+
scene: &mut impl PaintScene,
|
|
290
|
+
transform: Affine,
|
|
291
|
+
scale: f64,
|
|
292
|
+
x0: f64,
|
|
293
|
+
width: f64,
|
|
294
|
+
baseline: f32,
|
|
295
|
+
offset: f32,
|
|
296
|
+
size: f32,
|
|
297
|
+
brush: &anyrender::Paint,
|
|
298
|
+
double_dir: f64,
|
|
299
|
+
deco_style: TextDecorationStyle,
|
|
300
|
+
inset_start: f64,
|
|
301
|
+
inset_end: f64,
|
|
302
|
+
) {
|
|
303
|
+
// Inset the line from the box's start/end edges (assumes LTR: start is the left edge).
|
|
304
|
+
// Negative insets extend the line past the text.
|
|
305
|
+
let x = x0 + inset_start;
|
|
306
|
+
let w = width - inset_start - inset_end;
|
|
307
|
+
if w <= 0.0 {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
let y = (baseline - offset + size / 2.0) as f64;
|
|
311
|
+
let size = size as f64;
|
|
312
|
+
let butt_stroke = Stroke::new(size).with_caps(Cap::Butt);
|
|
313
|
+
|
|
314
|
+
match deco_style {
|
|
315
|
+
TextDecorationStyle::MozNone => {
|
|
316
|
+
// no line. Equivalent to `text-decoration-line: none`
|
|
317
|
+
}
|
|
318
|
+
// `solid`
|
|
319
|
+
TextDecorationStyle::Solid => {
|
|
320
|
+
let line = kurbo::Line::new((x, y), (x + w, y));
|
|
321
|
+
scene.stroke(&butt_stroke, transform, brush, None, &line);
|
|
322
|
+
}
|
|
323
|
+
// Two lines, each `size` thick, separated by a 1px (CSS) gap. This
|
|
324
|
+
// matches Chrome, where the second line is offset by `thickness + 1px`
|
|
325
|
+
// and sits further from the text (below for underline, above for
|
|
326
|
+
// overline).
|
|
327
|
+
TextDecorationStyle::Double => {
|
|
328
|
+
let one_css_px = scale;
|
|
329
|
+
for cy in [y, y + double_dir * (size + one_css_px)] {
|
|
330
|
+
let line = kurbo::Line::new((x, cy), (x + w, cy));
|
|
331
|
+
scene.stroke(&butt_stroke, transform, brush, None, &line);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// Round dots (diameter = line thickness) spaced one dot apart.
|
|
335
|
+
TextDecorationStyle::Dotted => {
|
|
336
|
+
let radius = size / 2.0;
|
|
337
|
+
let step = 2.0 * size;
|
|
338
|
+
let mut cx = x + radius;
|
|
339
|
+
while cx <= x + w - radius {
|
|
340
|
+
let dot = Circle::new((cx, y), radius);
|
|
341
|
+
scene.fill(Fill::NonZero, transform, brush, None, &dot);
|
|
342
|
+
cx += step;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
// Dashes as filled rectangles. Dash/gap lengths are relative to the
|
|
346
|
+
// thickness (matching Blink): thinner lines use proportionally longer
|
|
347
|
+
// dashes and gaps so they don't read as dotted or solid.
|
|
348
|
+
TextDecorationStyle::Dashed => {
|
|
349
|
+
let (dash_ratio, gap_ratio) = if size >= 3.0 { (2.0, 1.0) } else { (3.0, 2.0) };
|
|
350
|
+
let dash = size * dash_ratio;
|
|
351
|
+
let nominal_gap = size * gap_ratio;
|
|
352
|
+
// Nudge the gap so a whole number of dashes fits the run evenly.
|
|
353
|
+
let gap = if w > 2.0 * dash {
|
|
354
|
+
select_best_dash_gap(w, dash, nominal_gap)
|
|
355
|
+
} else {
|
|
356
|
+
nominal_gap
|
|
357
|
+
};
|
|
358
|
+
let mut dx = x;
|
|
359
|
+
while dx < x + w {
|
|
360
|
+
let end = (dx + dash).min(x + w);
|
|
361
|
+
let rect = Rect::new(dx, y - size / 2.0, end, y + size / 2.0);
|
|
362
|
+
scene.fill(Fill::NonZero, transform, brush, None, &rect);
|
|
363
|
+
dx += dash + gap;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
// A squiggle built from alternating quadratic Béziers.
|
|
367
|
+
TextDecorationStyle::Wavy => {
|
|
368
|
+
let amplitude = size;
|
|
369
|
+
let half_wave = (2.0 * size).max(1.0);
|
|
370
|
+
let mut path = BezPath::new();
|
|
371
|
+
path.move_to((x, y));
|
|
372
|
+
let mut px = x;
|
|
373
|
+
let mut up = true;
|
|
374
|
+
while px < x + w {
|
|
375
|
+
let nx = (px + half_wave).min(x + w);
|
|
376
|
+
// A quad Bézier reaches half the control-point offset at its
|
|
377
|
+
// midpoint, so use `2 * amplitude` to hit the target peak.
|
|
378
|
+
let ctrl_y = if up {
|
|
379
|
+
y - 2.0 * amplitude
|
|
380
|
+
} else {
|
|
381
|
+
y + 2.0 * amplitude
|
|
382
|
+
};
|
|
383
|
+
path.quad_to(((px + nx) / 2.0, ctrl_y), (nx, y));
|
|
384
|
+
px = nx;
|
|
385
|
+
up = !up;
|
|
386
|
+
}
|
|
387
|
+
let stroke = Stroke::new(size).with_caps(Cap::Round);
|
|
388
|
+
scene.stroke(&stroke, transform, brush, None, &path);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/// Paint the decorations accumulated for one line, one line per decorating box.
|
|
394
|
+
fn flush_line_decorations(
|
|
395
|
+
scene: &mut impl PaintScene,
|
|
396
|
+
transform: Affine,
|
|
397
|
+
scale: f64,
|
|
398
|
+
deco_boxes: &[LineDecoration],
|
|
399
|
+
win_ascent_ratios: &mut WinAscentCache,
|
|
400
|
+
inline_root_id: NodeId,
|
|
401
|
+
line_baseline: f32,
|
|
402
|
+
) {
|
|
403
|
+
// Draw innermost boxes first so ancestors' decorations paint on top, matching the
|
|
404
|
+
// per-run drawing order this replaced (`stack.iter().rev()`).
|
|
405
|
+
for acc in deco_boxes.iter().rev() {
|
|
406
|
+
let deco = &acc.deco;
|
|
407
|
+
// Prefer the decorating box's own font; fall back to the first run it covers.
|
|
408
|
+
let geom = match (&acc.own, &acc.first) {
|
|
409
|
+
(Some(own), _) => own.clone(),
|
|
410
|
+
(None, Some(first)) => {
|
|
411
|
+
let mut geom = first.clone();
|
|
412
|
+
// A descendant run's baseline may be shifted by `vertical-align`, but the
|
|
413
|
+
// decoration is positioned from the decorating box's own baseline. For the
|
|
414
|
+
// inline root that is the line's baseline.
|
|
415
|
+
if acc.node_id == inline_root_id {
|
|
416
|
+
geom.baseline = line_baseline;
|
|
417
|
+
}
|
|
418
|
+
geom
|
|
419
|
+
}
|
|
420
|
+
(None, None) => continue,
|
|
421
|
+
};
|
|
422
|
+
let geom = &geom;
|
|
423
|
+
let width = acc.max_x - acc.min_x;
|
|
424
|
+
if width <= 0.0 {
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
let brush = anyrender::Paint::from(deco.color);
|
|
428
|
+
|
|
429
|
+
// `text-decoration-inset` shortens (or, when negative, extends) the line from the
|
|
430
|
+
// inline-start/end edges. Percentages resolve against the decoration line length
|
|
431
|
+
// (the box's total advance on this line); `auto` is no inset.
|
|
432
|
+
let (inset_start, inset_end) = match &deco.inset {
|
|
433
|
+
GenericTextDecorationInset::LengthPercentage { start, end } => {
|
|
434
|
+
// The extent is in device pixels; convert to CSS pixels so the resolved
|
|
435
|
+
// result can be scaled back.
|
|
436
|
+
let line_length = Length::new((width / scale) as f32);
|
|
437
|
+
(
|
|
438
|
+
start.resolve(line_length).px() as f64 * scale,
|
|
439
|
+
end.resolve(line_length).px() as f64 * scale,
|
|
440
|
+
)
|
|
441
|
+
}
|
|
442
|
+
GenericTextDecorationInset::Auto => (0.0, 0.0),
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
if deco.line.contains(TextDecorationLine::UNDERLINE) {
|
|
446
|
+
let size = decoration_size(
|
|
447
|
+
&deco.thickness,
|
|
448
|
+
geom.underline_size,
|
|
449
|
+
geom.css_font_size,
|
|
450
|
+
scale,
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
// `text-underline-offset` moves the underline away from the text. `auto` keeps
|
|
454
|
+
// the font's suggested position; otherwise it resolves against the font size
|
|
455
|
+
// (percentages relative to 1em).
|
|
456
|
+
let extra_underline_offset = deco
|
|
457
|
+
.underline_offset
|
|
458
|
+
.as_ref()
|
|
459
|
+
.map(|lp| lp.resolve(Length::new(geom.css_font_size as f32)).px() as f64 * scale)
|
|
460
|
+
.unwrap_or(0.0);
|
|
461
|
+
|
|
462
|
+
// `text-underline-position: under` places the underline below the glyph box
|
|
463
|
+
// (below descenders) rather than at the font's suggested position near the
|
|
464
|
+
// alphabetic baseline. We anchor the top of the line at the descent so it clears
|
|
465
|
+
// descending glyphs like "gqy".
|
|
466
|
+
// An explicit offset under `auto` is taken from the alphabetic
|
|
467
|
+
// baseline, as CSS and Chrome have it, not from the auto gap.
|
|
468
|
+
let base_offset = if deco.underline_under {
|
|
469
|
+
-geom.descent
|
|
470
|
+
} else if deco.underline_offset.is_some() && !deco.underline_from_font {
|
|
471
|
+
0.0
|
|
472
|
+
} else {
|
|
473
|
+
geom.underline_offset
|
|
474
|
+
};
|
|
475
|
+
let offset = base_offset - extra_underline_offset as f32;
|
|
476
|
+
|
|
477
|
+
// A `double` underline extends downward, away from the text.
|
|
478
|
+
draw_decoration_line(
|
|
479
|
+
scene,
|
|
480
|
+
transform,
|
|
481
|
+
scale,
|
|
482
|
+
acc.min_x,
|
|
483
|
+
width,
|
|
484
|
+
geom.baseline,
|
|
485
|
+
offset,
|
|
486
|
+
size,
|
|
487
|
+
&brush,
|
|
488
|
+
1.0,
|
|
489
|
+
deco.style,
|
|
490
|
+
inset_start,
|
|
491
|
+
inset_end,
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
if deco.line.contains(TextDecorationLine::OVERLINE) {
|
|
495
|
+
// Fonts don't provide a dedicated overline metric, so reuse the underline
|
|
496
|
+
// thickness. The line sits at the top of the "em box": its lower edge rests on
|
|
497
|
+
// the ascent so it clears the glyphs, and it extends upward from there.
|
|
498
|
+
//
|
|
499
|
+
// Browsers use the OS/2 `usWinAscent` for this edge, which is taller than the
|
|
500
|
+
// hhea-based ascent Parley reports; using the smaller value would draw the
|
|
501
|
+
// overline too low. Fall back to Parley's ascent for fonts without a usable
|
|
502
|
+
// OS/2 table.
|
|
503
|
+
let size = decoration_size(
|
|
504
|
+
&deco.thickness,
|
|
505
|
+
geom.underline_size,
|
|
506
|
+
geom.css_font_size,
|
|
507
|
+
scale,
|
|
508
|
+
);
|
|
509
|
+
let ascent =
|
|
510
|
+
win_ascent(win_ascent_ratios, &geom.font, geom.font_size).unwrap_or(geom.ascent);
|
|
511
|
+
let offset = ascent + size;
|
|
512
|
+
|
|
513
|
+
// A `double` overline extends upward, away from the text.
|
|
514
|
+
draw_decoration_line(
|
|
515
|
+
scene,
|
|
516
|
+
transform,
|
|
517
|
+
scale,
|
|
518
|
+
acc.min_x,
|
|
519
|
+
width,
|
|
520
|
+
geom.baseline,
|
|
521
|
+
offset,
|
|
522
|
+
size,
|
|
523
|
+
&brush,
|
|
524
|
+
-1.0,
|
|
525
|
+
deco.style,
|
|
526
|
+
inset_start,
|
|
527
|
+
inset_end,
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
if deco.line.contains(TextDecorationLine::LINE_THROUGH) {
|
|
531
|
+
let size = decoration_size(
|
|
532
|
+
&deco.thickness,
|
|
533
|
+
geom.strikethrough_size,
|
|
534
|
+
geom.css_font_size,
|
|
535
|
+
scale,
|
|
536
|
+
);
|
|
537
|
+
|
|
538
|
+
// Centre the line-through a third of the ascent above the baseline, matching
|
|
539
|
+
// Chrome (which places it `2/3 * ascent` below the text-top). Parley's
|
|
540
|
+
// `strikethrough_offset` (the font's `yStrikeoutPosition`) sits lower and would
|
|
541
|
+
// draw the line too close to the baseline.
|
|
542
|
+
let ascent =
|
|
543
|
+
win_ascent(win_ascent_ratios, &geom.font, geom.font_size).unwrap_or(geom.ascent);
|
|
544
|
+
let offset = ascent / 3.0 + size / 2.0;
|
|
545
|
+
|
|
546
|
+
draw_decoration_line(
|
|
547
|
+
scene,
|
|
548
|
+
transform,
|
|
549
|
+
scale,
|
|
550
|
+
acc.min_x,
|
|
551
|
+
width,
|
|
552
|
+
geom.baseline,
|
|
553
|
+
offset,
|
|
554
|
+
size,
|
|
555
|
+
&brush,
|
|
556
|
+
1.0,
|
|
557
|
+
deco.style,
|
|
558
|
+
inset_start,
|
|
559
|
+
inset_end,
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
pub(crate) fn stroke_text<'a>(
|
|
566
|
+
scene: &mut impl PaintScene,
|
|
567
|
+
lines: impl Iterator<Item = Line<'a, TextBrush>>,
|
|
568
|
+
doc: &BaseDocument,
|
|
569
|
+
transform: Affine,
|
|
570
|
+
scale: f64,
|
|
571
|
+
inline_root_id: NodeId,
|
|
572
|
+
context: &mut DrawTextContext,
|
|
573
|
+
) {
|
|
574
|
+
let DrawTextContext {
|
|
575
|
+
stack,
|
|
576
|
+
path_scratch,
|
|
577
|
+
deco_boxes,
|
|
578
|
+
win_ascent_ratios,
|
|
579
|
+
} = context;
|
|
580
|
+
stack.clear();
|
|
581
|
+
path_scratch.clear();
|
|
582
|
+
deco_boxes.clear();
|
|
583
|
+
|
|
584
|
+
// Persistent stack mirroring the ancestor path (inline root -> current run's
|
|
585
|
+
// node) as we walk the runs. The `text-decoration-*` properties are *not*
|
|
586
|
+
// inherited; instead a decoration set on an ancestor is propagated to the
|
|
587
|
+
// descendant text it wraps. Rather than re-resolving styles for the whole
|
|
588
|
+
// ancestor chain on every run, we cache each node's resolved values here and,
|
|
589
|
+
// for each run, only resolve styles for the nodes newly descended into (popping
|
|
590
|
+
// as we ascend). `path_scratch` is a reusable buffer for the run's node path.
|
|
591
|
+
for line in lines {
|
|
592
|
+
// Decorations accumulated for this line, keyed by decorating box, so each box is
|
|
593
|
+
// painted once (spanning all its runs) using its own font — matching Firefox, which
|
|
594
|
+
// draws one decoration per box rather than one stepped segment per differently-sized
|
|
595
|
+
// run. Clearing preserves the allocation for the next line and inline context.
|
|
596
|
+
deco_boxes.clear();
|
|
597
|
+
|
|
598
|
+
for item in line.items() {
|
|
599
|
+
if let PositionedLayoutItem::GlyphRun(glyph_run) = item {
|
|
600
|
+
let run = glyph_run.run();
|
|
601
|
+
let font = run.font();
|
|
602
|
+
let font_size = run.font_size();
|
|
603
|
+
let metrics = run.font_metrics();
|
|
604
|
+
let style = glyph_run.style();
|
|
605
|
+
let synthesis = run.synthesis();
|
|
606
|
+
let glyph_xform = synthesis
|
|
607
|
+
.skew()
|
|
608
|
+
.map(|angle| Affine::skew(angle.to_radians().tan() as f64, 0.0));
|
|
609
|
+
|
|
610
|
+
let css_font_size = font_size as f64 / scale;
|
|
611
|
+
|
|
612
|
+
// Reconcile the stack with this run's ancestor path. Build the path
|
|
613
|
+
// (inline root -> run node), keep the shared prefix already on the stack,
|
|
614
|
+
// pop the rest, and resolve styles only for the newly-descended nodes.
|
|
615
|
+
path_scratch.clear();
|
|
616
|
+
let mut walk_id = Some(style.brush.id);
|
|
617
|
+
while let Some(node_id) = walk_id {
|
|
618
|
+
path_scratch.push(node_id);
|
|
619
|
+
if node_id == inline_root_id {
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
walk_id = doc.get_node(node_id).and_then(|node| node.parent);
|
|
623
|
+
}
|
|
624
|
+
path_scratch.reverse();
|
|
625
|
+
|
|
626
|
+
let shared = stack
|
|
627
|
+
.iter()
|
|
628
|
+
.zip(path_scratch.iter())
|
|
629
|
+
.take_while(|(entry, node_id)| entry.node_id == **node_id)
|
|
630
|
+
.count();
|
|
631
|
+
stack.truncate(shared);
|
|
632
|
+
for &node_id in &path_scratch[shared..] {
|
|
633
|
+
stack.push(resolve_decoration_entry(doc, node_id));
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
// The glyph colour comes from the run's own node (the stack top): `color`
|
|
637
|
+
// inherits, so the innermost inline element already carries the right value.
|
|
638
|
+
let text_color = stack.last().map(|e| e.text_color).unwrap_or(Color::BLACK);
|
|
639
|
+
|
|
640
|
+
let embolden = if FONT_EMBOLDEN_ENABLED {
|
|
641
|
+
let fs = font_size as f64 / scale;
|
|
642
|
+
kurbo::Vec2::new((0.015125 * fs).min(0.3), (0.0121 * fs).min(0.3))
|
|
643
|
+
} else {
|
|
644
|
+
kurbo::Vec2::default()
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
let normalized_coords: &[i16] = bytemuck::cast_slice(run.normalized_coords());
|
|
648
|
+
scene.draw_glyphs(
|
|
649
|
+
font,
|
|
650
|
+
font_size,
|
|
651
|
+
!FONT_EMBOLDEN_ENABLED, // hint
|
|
652
|
+
normalized_coords,
|
|
653
|
+
embolden,
|
|
654
|
+
Fill::NonZero,
|
|
655
|
+
&anyrender::Paint::from(text_color),
|
|
656
|
+
1.0, // alpha
|
|
657
|
+
transform,
|
|
658
|
+
glyph_xform,
|
|
659
|
+
glyph_run.positioned_glyphs().map(|glyph| anyrender::Glyph {
|
|
660
|
+
id: glyph.id as _,
|
|
661
|
+
x: glyph.x,
|
|
662
|
+
y: glyph.y,
|
|
663
|
+
}),
|
|
664
|
+
);
|
|
665
|
+
|
|
666
|
+
// Accumulate this run's contribution to each decorating box on its ancestor
|
|
667
|
+
// path. The decoration is drawn once per box after the whole line has been
|
|
668
|
+
// walked (see `flush_line_decorations`), so mixed font sizes within a box
|
|
669
|
+
// produce a single straight line rather than one stepped segment per run.
|
|
670
|
+
let geometry = DecorationRunGeometry {
|
|
671
|
+
baseline: glyph_run.baseline(),
|
|
672
|
+
ascent: metrics.ascent,
|
|
673
|
+
descent: metrics.descent,
|
|
674
|
+
underline_offset: metrics.underline_offset,
|
|
675
|
+
underline_size: metrics.underline_size,
|
|
676
|
+
strikethrough_size: metrics.strikethrough_size,
|
|
677
|
+
font: font.clone(),
|
|
678
|
+
font_size,
|
|
679
|
+
css_font_size,
|
|
680
|
+
};
|
|
681
|
+
let run_node_id = style.brush.id;
|
|
682
|
+
let run_x0 = glyph_run.offset() as f64;
|
|
683
|
+
let run_x1 = run_x0 + glyph_run.advance() as f64;
|
|
684
|
+
|
|
685
|
+
for entry in stack.iter() {
|
|
686
|
+
if entry.decoration.is_none() {
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
let idx = match deco_boxes.iter().position(|d| d.node_id == entry.node_id) {
|
|
690
|
+
Some(idx) => idx,
|
|
691
|
+
None => {
|
|
692
|
+
deco_boxes.push(LineDecoration {
|
|
693
|
+
node_id: entry.node_id,
|
|
694
|
+
deco: entry.decoration.clone().unwrap(),
|
|
695
|
+
min_x: f64::INFINITY,
|
|
696
|
+
max_x: f64::NEG_INFINITY,
|
|
697
|
+
own: None,
|
|
698
|
+
first: None,
|
|
699
|
+
});
|
|
700
|
+
deco_boxes.len() - 1
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
let acc = &mut deco_boxes[idx];
|
|
704
|
+
acc.min_x = acc.min_x.min(run_x0);
|
|
705
|
+
acc.max_x = acc.max_x.max(run_x1);
|
|
706
|
+
if acc.first.is_none() {
|
|
707
|
+
acc.first = Some(geometry.clone());
|
|
708
|
+
}
|
|
709
|
+
// A run whose innermost node is the box itself is the box's own text, so
|
|
710
|
+
// its font is the one Firefox positions and sizes the decoration with.
|
|
711
|
+
if entry.node_id == run_node_id {
|
|
712
|
+
acc.own = Some(geometry.clone());
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
flush_line_decorations(
|
|
719
|
+
scene,
|
|
720
|
+
transform,
|
|
721
|
+
scale,
|
|
722
|
+
deco_boxes,
|
|
723
|
+
win_ascent_ratios,
|
|
724
|
+
inline_root_id,
|
|
725
|
+
line.metrics().baseline,
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/// Draw selection highlight rectangles for the given byte range in a layout.
|
|
731
|
+
/// Uses Parley's Selection type for accurate geometry calculation.
|
|
732
|
+
pub(crate) fn draw_text_selection(
|
|
733
|
+
scene: &mut impl PaintScene,
|
|
734
|
+
layout: &Layout<TextBrush>,
|
|
735
|
+
transform: Affine,
|
|
736
|
+
selection_start: usize,
|
|
737
|
+
selection_end: usize,
|
|
738
|
+
) {
|
|
739
|
+
let anchor = Cursor::from_byte_index(layout, selection_start, Affinity::Downstream);
|
|
740
|
+
let focus = Cursor::from_byte_index(layout, selection_end, Affinity::Downstream);
|
|
741
|
+
let selection = Selection::new(anchor, focus);
|
|
742
|
+
|
|
743
|
+
selection.geometry_with(layout, |rect, _line_idx| {
|
|
744
|
+
let rect = kurbo::Rect::new(rect.x0, rect.y0, rect.x1, rect.y1);
|
|
745
|
+
scene.fill(Fill::NonZero, transform, SELECTION_COLOR, None, &rect);
|
|
746
|
+
});
|
|
747
|
+
}
|