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
// 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::components::rich_text::batch::{BatchManager, RunUnderline};
pub use crate::components::rich_text::batch::{Rect, Vertex};
use crate::components::rich_text::image_cache::glyph::GlyphCacheSession;
use crate::components::rich_text::text::*;
use crate::layout::{FragmentStyleDecoration, UnderlineShape};
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,
rect: &Rect,
) -> Option<RunUnderline> {
match style.decoration {
Some(FragmentStyleDecoration::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(FragmentStyleDecoration::Strikethrough) => {
// Use font's built-in strikeout offset if available, otherwise use x-height/2
// This matches standard typographic positioning for strikethrough
let strikethrough_offset = if style.strikeout_offset != 0.0 {
// Font provides explicit strikeout position (negative because it's above baseline)
-style.strikeout_offset
} else if style.x_height > 0.0 {
// Use half of x-height above baseline - this is the typographically correct position
-(style.x_height / 2.0)
} else {
// Final fallback: approximate x-height position
-(rect.height * 0.3)
};
// Use font metrics for thickness when available
let strikethrough_thickness = if style.underline_thickness > 0.0 {
style.underline_thickness
} else {
1.5 // Slightly thinner than before for better appearance
};
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
}
}
pub fn begin(&mut self) {
self.batches.reset();
}
pub fn finish(&mut self, vertices: &mut Vec<Vertex>) {
self.batches.build_display_list(vertices);
}
/// 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],
_cache_operations: Option<&mut Vec<()>>, // Ignored - legacy parameter
) {
self.draw_run_internal(session, rect, depth, style, glyphs);
}
/// Internal rendering implementation
#[inline]
fn draw_run_internal(
&mut self,
session: &mut GlyphCacheSession,
rect: impl Into<Rect>,
depth: f32,
style: &TextRunStyle,
glyphs: &[Glyph],
) {
let rect = rect.into();
let underline = self.create_underline_from_decoration(style, &rect);
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.add_rect(&bg_rect, depth, &bg_color);
}
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 {
crate::SugarCursor::Block(cursor_color) => {
let cursor_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.add_rect(&cursor_rect, depth, &cursor_color);
}
crate::SugarCursor::HollowBlock(cursor_color) => {
let outer_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.add_rect(&outer_rect, depth, &cursor_color);
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.add_rect(&inner_rect, depth, &bg_color);
}
}
crate::SugarCursor::Caret(cursor_color) => {
let outer_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.add_rect(&outer_rect, depth, &cursor_color);
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.add_rect(&inner_rect, depth, &bg_color);
}
}
crate::SugarCursor::Underline(cursor_color) => {
let caret_rect =
Rect::new(rect.x, style.baseline + 1.0, rect.width, 2.0);
self.batches.add_rect(&caret_rect, depth, &cursor_color);
}
}
}
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,
);
} 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;
let glyph_rect =
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];
self.batches.add_image_rect(
&glyph_rect,
depth,
&bitmap_color,
&coords,
entry.image.has_alpha(),
);
} else {
self.batches.add_mask_rect(
&glyph_rect,
depth,
&color,
&coords,
true,
);
}
}
}
}
if let Some(bg_color) = style.background_color {
let bg_rect =
Rect::new(rect.x, style.topline, rect.width, style.line_height);
self.batches.add_rect(&bg_rect, depth, &bg_color);
}
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 {
crate::SugarCursor::Block(cursor_color) => {
let cursor_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.add_rect(&cursor_rect, depth, &cursor_color);
}
crate::SugarCursor::HollowBlock(cursor_color) => {
let outer_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.add_rect(&outer_rect, depth, &cursor_color);
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.add_rect(&inner_rect, depth, &bg_color);
}
}
crate::SugarCursor::Caret(cursor_color) => {
let outer_rect =
Rect::new(rect.x, cursor_top, rect.width, font_height);
self.batches.add_rect(&outer_rect, depth, &cursor_color);
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.add_rect(&inner_rect, depth, &bg_color);
}
}
crate::SugarCursor::Underline(cursor_color) => {
let caret_rect =
Rect::new(rect.x, style.baseline + 1.0, rect.width, 2.0);
self.batches.add_rect(&caret_rect, depth, &cursor_color);
}
}
}
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()
}
}