1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) 2023-present, Raphael Amorim.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
//
// Compositor with vertex capture for text run caching
use crate::layout::{SpanStyleDecoration, UnderlineShape};
use crate::renderer::batch::{BatchManager, RunUnderline};
pub use crate::renderer::batch::{Rect, Vertex};
use crate::renderer::image_cache::glyph::GlyphCacheSession;
use crate::renderer::text::*;
pub struct Compositor {
pub batches: BatchManager,
}
impl Compositor {
pub fn new() -> Self {
Self {
batches: BatchManager::new(),
}
}
/// Creates an underline decoration based on the style and rect
pub fn create_underline_from_decoration(
&self,
style: &TextRunStyle,
) -> Option<RunUnderline> {
match style.decoration {
Some(SpanStyleDecoration::Underline(info)) => {
// Use font metrics for thickness when available, otherwise fall back to shape-based defaults
let underline_thickness = if style.underline_thickness > 0.0 {
style.underline_thickness
} else {
// Fallback thickness based on font size
// Use approximately 8% of font size for all underline types
let base_thickness = style.font_size * 0.08;
base_thickness.max(1.0)
};
// Use real font metrics for proper underline positioning
let underline_offset =
self.calculate_underline_offset(style, underline_thickness);
Some(RunUnderline {
enabled: true,
offset: underline_offset,
size: underline_thickness,
color: style.decoration_color.unwrap_or(style.color),
is_doubled: info.is_doubled,
shape: info.shape,
})
}
Some(SpanStyleDecoration::Strikethrough) => {
// Strikethrough should be positioned through the middle of the text
// Use font-provided strikeout_offset if available, otherwise x_height/2
let strikethrough_offset = if style.strikeout_offset != 0.0 {
// Font provides strikeout_offset as distance from baseline
-style.strikeout_offset
} else if style.x_height > 0.0 {
// x_height is the height of lowercase letters, strike through middle
-(style.x_height / 2.0)
} else {
// Fallback: 25% of ascent above baseline
-(style.ascent * 0.25)
};
// Use font metrics for thickness when available
let strikethrough_thickness = if style.underline_thickness > 0.0 {
style.underline_thickness
} else {
1.5
};
Some(RunUnderline {
enabled: true,
offset: strikethrough_offset,
size: strikethrough_thickness,
color: style.decoration_color.unwrap_or(style.color),
is_doubled: false,
shape: UnderlineShape::Regular,
})
}
_ => None,
}
}
/// Calculate underline offset using font metrics, with fallback
fn calculate_underline_offset(
&self,
style: &TextRunStyle,
underline_thickness: f32,
) -> f32 {
// Use font's built-in underline position when available
if style.underline_offset != 0.0 {
// Font provides underline_offset as distance from baseline to underline top
// Negative values mean below baseline, which is what we want
// But Rio's renderer expects positive offset for below baseline
-style.underline_offset
} else {
// Fallback: place underline 1 thickness below baseline
underline_thickness
}
}
#[inline]
pub fn finish(&mut self, vertices: &mut Vec<Vertex>) {
self.batches.build_display_list(vertices);
self.batches.reset();
}
/// Standard draw_run method (for compatibility)
#[inline]
pub fn draw_run(
&mut self,
session: &mut GlyphCacheSession,
rect: impl Into<Rect>,
depth: f32,
style: &TextRunStyle,
glyphs: &[Glyph],
order: u8,
) {
self.draw_run_internal(session, rect, depth, style, glyphs, order);
}
/// Internal rendering implementation
#[inline]
fn draw_run_internal(
&mut self,
session: &mut GlyphCacheSession,
rect: impl Into<Rect>,
depth: f32,
style: &TextRunStyle,
glyphs: &[Glyph],
order: u8,
) {
let rect = rect.into();
let underline = self.create_underline_from_decoration(style);
let subpx_bias = (0.125, 0.);
let color = style.color;
if let Some(builtin_character) = style.drawable_char {
if let Some(bg_color) = style.background_color {
let bg_rect =
Rect::new(rect.x, style.topline, rect.width, style.line_height);
self.batches.rect(&bg_rect, depth, &bg_color, 0);
}
if let Some(cursor) = style.cursor {
// Calculate cursor dimensions based on font metrics, not line height
let font_height = style.ascent + style.descent;
let cursor_top = style.baseline - style.ascent;
match cursor.kind {
crate::CursorKind::Block => {
let cursor_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.rect(
&cursor_rect,
depth,
&cursor.color,
cursor.order,
);
}
crate::CursorKind::HollowBlock => {
let outer_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.rect(
&outer_rect,
depth,
&cursor.color,
cursor.order,
);
if let Some(bg_color) = style.background_color {
let inner_rect = Rect::new(
rect.x + 1.0,
cursor_top + 1.0,
rect.width - 2.0,
font_height - 2.0,
);
self.batches.rect(
&inner_rect,
depth,
&bg_color,
cursor.order,
);
}
}
crate::CursorKind::Caret => {
let caret_rect = Rect::new(rect.x, cursor_top, 2.0, font_height);
self.batches.rect(
&caret_rect,
depth,
&cursor.color,
cursor.order,
);
}
crate::CursorKind::Underline => {
let caret_rect =
Rect::new(rect.x, style.baseline + 1.0, rect.width, 2.0);
self.batches.rect(
&caret_rect,
depth,
&cursor.color,
cursor.order,
);
}
}
}
if let Some(underline) = underline {
self.batches.draw_underline(
&underline,
rect.x,
rect.width,
style.baseline,
depth,
style.line_height,
);
}
self.batches.draw_drawable_character(
rect.x,
style.topline,
rect.width,
builtin_character,
color,
depth,
style.line_height,
0,
);
} else {
// Handle regular glyphs
for glyph in glyphs {
let entry = session.get(glyph.id);
if let Some(entry) = entry {
if let Some(img) = session.get_image(entry.image) {
let gx = (glyph.x + subpx_bias.0).floor() + entry.left as f32;
let gy = (glyph.y + subpx_bias.1).floor() - entry.top as f32;
// Scale PUA glyphs to fit constraint width
let glyph_rect =
if let Some((cell_w, cells)) = style.scale_constraint {
let target_w = cell_w * cells as f32;
let target_h = style.line_height;
let orig_w = entry.width as f32;
let orig_h = entry.height as f32;
// Fit proportionally within the target area
let scale =
(target_w / orig_w).min(target_h / orig_h).min(1.0);
let sw = orig_w * scale;
let sh = orig_h * scale;
// Center horizontally within constraint width
let cx = glyph.x + (target_w - sw) / 2.0;
// Align vertically from the baseline
let cy = gy + (orig_h - sh) / 2.0;
Rect::new(cx, cy, sw, sh)
} else {
Rect::new(gx, gy, entry.width as f32, entry.height as f32)
};
let coords = [img.min.0, img.min.1, img.max.0, img.max.1];
if entry.is_bitmap {
let bitmap_color = [1.0, 1.0, 1.0, 1.0];
// Get atlas index for this image (0-based), add 1 for layer (0 = no texture)
let atlas_layer = session
.get_atlas_index(entry.image)
.map(|idx| (idx + 1) as i32)
.unwrap_or(1);
self.batches.add_image_rect(
&glyph_rect,
depth,
&bitmap_color,
&coords,
atlas_layer,
);
} else {
self.batches.add_mask_rect_with_order(
&glyph_rect,
depth,
&color,
&coords,
true,
order,
);
}
}
}
}
if let Some(bg_color) = style.background_color {
let bg_rect =
Rect::new(rect.x, style.topline, rect.width, style.line_height);
self.batches.rect(&bg_rect, depth, &bg_color, 0);
}
if let Some(cursor) = style.cursor {
// Calculate cursor dimensions based on font metrics, not line height
let font_height = style.ascent + style.descent;
let cursor_top = style.baseline - style.ascent;
match cursor.kind {
crate::CursorKind::Block => {
let cursor_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.rect(
&cursor_rect,
depth,
&cursor.color,
cursor.order,
);
}
crate::CursorKind::HollowBlock => {
let outer_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.rect(
&outer_rect,
depth,
&cursor.color,
cursor.order,
);
if let Some(bg_color) = style.background_color {
let inner_rect = Rect::new(
rect.x + 1.0,
cursor_top + 1.0,
rect.width - 2.0,
font_height - 2.0,
);
self.batches.rect(
&inner_rect,
depth,
&bg_color,
cursor.order,
);
}
}
crate::CursorKind::Caret => {
let caret_rect = Rect::new(rect.x, cursor_top, 2.0, font_height);
self.batches.rect(
&caret_rect,
depth,
&cursor.color,
cursor.order,
);
}
crate::CursorKind::Underline => {
let caret_rect =
Rect::new(rect.x, style.baseline + 1.0, rect.width, 2.0);
self.batches.rect(
&caret_rect,
depth,
&cursor.color,
cursor.order,
);
}
}
}
if let Some(underline) = underline {
self.batches.draw_underline(
&underline,
rect.x,
rect.width,
style.baseline,
depth,
style.line_height,
);
}
}
}
}
impl Default for Compositor {
fn default() -> Self {
Self::new()
}
}