1use crate::font::registry::FontRegistry;
2use crate::font::resolve::{ResolvedFont, resolve_font};
3use crate::layout::line::LayoutLine;
4use crate::layout::paragraph::{Alignment, break_into_lines};
5use crate::shaping::run::ShapedRun;
6use crate::shaping::shaper::{FontMetricsPx, font_metrics_px, shape_text};
7
8pub struct BlockLayout {
10 pub block_id: usize,
11 pub position: usize,
13 pub lines: Vec<LayoutLine>,
15 pub y: f32,
17 pub height: f32,
19 pub top_margin: f32,
20 pub bottom_margin: f32,
21 pub left_margin: f32,
22 pub right_margin: f32,
23 pub list_marker: Option<ShapedListMarker>,
26 pub background_color: Option<[f32; 4]>,
28}
29
30pub struct ShapedListMarker {
32 pub run: ShapedRun,
33 pub x: f32,
35}
36
37pub struct BlockLayoutParams {
40 pub block_id: usize,
41 pub position: usize,
42 pub text: String,
43 pub fragments: Vec<FragmentParams>,
44 pub alignment: Alignment,
45 pub top_margin: f32,
46 pub bottom_margin: f32,
47 pub left_margin: f32,
48 pub right_margin: f32,
49 pub text_indent: f32,
50 pub list_marker: String,
52 pub list_indent: f32,
54 pub tab_positions: Vec<f32>,
56 pub line_height_multiplier: Option<f32>,
59 pub non_breakable_lines: bool,
61 pub checkbox: Option<bool>,
63 pub background_color: Option<[f32; 4]>,
65}
66
67pub struct FragmentParams {
69 pub text: String,
70 pub offset: usize,
71 pub length: usize,
72 pub font_family: Option<String>,
73 pub font_weight: Option<u32>,
74 pub font_bold: Option<bool>,
75 pub font_italic: Option<bool>,
76 pub font_point_size: Option<u32>,
77 pub underline: bool,
78 pub overline: bool,
79 pub strikeout: bool,
80 pub is_link: bool,
81 pub letter_spacing: f32,
83 pub word_spacing: f32,
85}
86
87pub fn layout_block(
89 registry: &FontRegistry,
90 params: &BlockLayoutParams,
91 available_width: f32,
92) -> BlockLayout {
93 let effective_left_margin = params.left_margin + params.list_indent;
94 let content_width = (available_width - effective_left_margin - params.right_margin).max(0.0);
95
96 let mut shaped_runs = Vec::new();
98 let mut default_metrics: Option<FontMetricsPx> = None;
99
100 for frag in ¶ms.fragments {
101 let resolved = resolve_font(
102 registry,
103 frag.font_family.as_deref(),
104 frag.font_weight,
105 frag.font_bold,
106 frag.font_italic,
107 frag.font_point_size,
108 );
109
110 if let Some(resolved) = resolved {
111 if default_metrics.is_none() {
113 default_metrics = font_metrics_px(registry, &resolved);
114 }
115
116 if let Some(mut run) = shape_text(registry, &resolved, &frag.text, frag.offset) {
117 run.underline = frag.underline;
118 run.overline = frag.overline;
119 run.strikeout = frag.strikeout;
120 run.is_link = frag.is_link;
121
122 if frag.letter_spacing != 0.0 || frag.word_spacing != 0.0 {
124 apply_spacing(&mut run, &frag.text, frag.letter_spacing, frag.word_spacing);
125 }
126
127 if !params.tab_positions.is_empty() {
129 apply_tab_stops(&mut run, &frag.text, ¶ms.tab_positions);
130 }
131
132 shaped_runs.push(run);
133 }
134 }
135 }
136
137 let metrics = default_metrics.unwrap_or_else(|| get_default_metrics(registry));
139
140 let wrap_width = if params.non_breakable_lines {
142 f32::INFINITY
143 } else {
144 content_width
145 };
146
147 let mut lines = break_into_lines(
149 shaped_runs,
150 ¶ms.text,
151 wrap_width,
152 params.alignment,
153 params.text_indent,
154 &metrics,
155 );
156
157 let line_height_mul = params.line_height_multiplier.unwrap_or(1.0).max(0.1);
159
160 let mut y = 0.0f32;
162 for line in &mut lines {
163 if line_height_mul != 1.0 {
164 line.line_height *= line_height_mul;
165 }
166 line.y = y + line.ascent; y += line.line_height;
168 }
169
170 let content_height = y;
171 let total_height = params.top_margin + content_height + params.bottom_margin;
172
173 let list_marker = if params.checkbox.is_some() {
175 shape_checkbox_marker(registry, &metrics, params)
176 } else if !params.list_marker.is_empty() {
177 shape_list_marker(registry, &metrics, params)
178 } else {
179 None
180 };
181
182 BlockLayout {
183 block_id: params.block_id,
184 position: params.position,
185 lines,
186 y: 0.0, height: total_height,
188 top_margin: params.top_margin,
189 bottom_margin: params.bottom_margin,
190 left_margin: effective_left_margin,
191 right_margin: params.right_margin,
192 list_marker,
193 background_color: params.background_color,
194 }
195}
196
197fn apply_spacing(run: &mut ShapedRun, text: &str, letter_spacing: f32, word_spacing: f32) {
199 let mut extra_advance = 0.0f32;
200 for glyph in &mut run.glyphs {
201 glyph.x_advance += letter_spacing;
202 extra_advance += letter_spacing;
203
204 if word_spacing != 0.0 {
207 let byte_offset = glyph.cluster as usize;
208 if let Some(ch) = text.get(byte_offset..).and_then(|s| s.chars().next())
209 && ch == ' '
210 {
211 glyph.x_advance += word_spacing;
212 extra_advance += word_spacing;
213 }
214 }
215 }
216 run.advance_width += extra_advance;
217}
218
219fn shape_list_marker(
221 registry: &FontRegistry,
222 _metrics: &FontMetricsPx,
223 params: &BlockLayoutParams,
224) -> Option<ShapedListMarker> {
225 let resolved = resolve_font(registry, None, None, None, None, None)?;
227 let run = shape_text(registry, &resolved, ¶ms.list_marker, 0)?;
228
229 let gap = 4.0; let marker_x = params.left_margin + params.list_indent - run.advance_width - gap;
232 let marker_x = marker_x.max(params.left_margin);
233
234 Some(ShapedListMarker { run, x: marker_x })
235}
236
237fn apply_tab_stops(run: &mut ShapedRun, text: &str, tab_positions: &[f32]) {
239 let default_tab = 48.0; let mut pen_x = 0.0f32;
241
242 for glyph in &mut run.glyphs {
243 let byte_offset = glyph.cluster as usize;
244 if let Some(ch) = text.get(byte_offset..).and_then(|s| s.chars().next())
245 && ch == '\t'
246 {
247 let next_stop = tab_positions
249 .iter()
250 .find(|&&stop| stop > pen_x + 1.0)
251 .copied()
252 .unwrap_or_else(|| {
253 let last = tab_positions.last().copied().unwrap_or(0.0);
255 let increment = if tab_positions.len() >= 2 {
256 tab_positions[1] - tab_positions[0]
257 } else {
258 default_tab
259 };
260 let mut stop = last + increment;
261 while stop <= pen_x + 1.0 {
262 stop += increment;
263 }
264 stop
265 });
266
267 let tab_advance = next_stop - pen_x;
268 let delta = tab_advance - glyph.x_advance;
269 glyph.x_advance = tab_advance;
270 run.advance_width += delta;
271 }
272 pen_x += glyph.x_advance;
273 }
274}
275
276fn shape_checkbox_marker(
278 registry: &FontRegistry,
279 _metrics: &FontMetricsPx,
280 params: &BlockLayoutParams,
281) -> Option<ShapedListMarker> {
282 let checked = params.checkbox?;
283 let marker_text = if checked { "\u{2611}" } else { "\u{2610}" }; let resolved = resolve_font(registry, None, None, None, None, None)?;
286 let run = shape_text(registry, &resolved, marker_text, 0)?;
287
288 let run = if run.glyphs.iter().any(|g| g.glyph_id == 0) {
290 let fallback_text = if checked { "[x]" } else { "[ ]" };
291 shape_text(registry, &resolved, fallback_text, 0)?
292 } else {
293 run
294 };
295
296 let gap = 4.0;
297 let marker_x = params.left_margin + params.list_indent - run.advance_width - gap;
298 let marker_x = marker_x.max(params.left_margin);
299
300 Some(ShapedListMarker { run, x: marker_x })
301}
302
303fn get_default_metrics(registry: &FontRegistry) -> FontMetricsPx {
304 if let Some(default_id) = registry.default_font() {
305 let resolved = ResolvedFont {
306 font_face_id: default_id,
307 size_px: registry.default_size_px(),
308 face_index: registry.get(default_id).map(|e| e.face_index).unwrap_or(0),
309 swash_cache_key: registry
310 .get(default_id)
311 .map(|e| e.swash_cache_key)
312 .unwrap_or_default(),
313 };
314 if let Some(m) = font_metrics_px(registry, &resolved) {
315 return m;
316 }
317 }
318 FontMetricsPx {
320 ascent: 14.0,
321 descent: 4.0,
322 leading: 0.0,
323 underline_offset: -2.0,
324 strikeout_offset: 5.0,
325 stroke_size: 1.0,
326 }
327}