Skip to content

Commit 1f7bc2a

Browse files
committed
Extract glyph replacement with overflow character into it's own function
1 parent f3fc21a commit 1f7bc2a

File tree

1 file changed

+63
-53
lines changed

1 file changed

+63
-53
lines changed

epaint/src/text/text_layout.rs

+63-53
Original file line numberDiff line numberDiff line change
@@ -248,59 +248,7 @@ fn line_break(
248248

249249
if row_start_idx < paragraph.glyphs.len() {
250250
if non_empty_rows == job.wrap.max_rows {
251-
if let (Some(overflow_character), Some(row)) =
252-
(job.wrap.overflow_character, out_rows.last_mut())
253-
{
254-
loop {
255-
let (prev_glyph, last_glyph) = match row.glyphs.as_mut_slice() {
256-
[.., prev, last] => (Some(prev), last),
257-
[.., last] => (None, last),
258-
_ => break,
259-
};
260-
261-
let section = &job.sections[last_glyph.section_index as usize];
262-
let font = fonts.font(&section.format.font_id);
263-
let font_height = font.row_height();
264-
265-
let prev_glyph_id = prev_glyph.map(|prev_glyph| {
266-
let (_, prev_glyph_info) = font.glyph_info_and_font_impl(prev_glyph.chr);
267-
prev_glyph_info.id
268-
});
269-
270-
// undo kerning with previous glyph
271-
let (font_impl, glyph_info) = font.glyph_info_and_font_impl(last_glyph.chr);
272-
last_glyph.pos.x -= font_impl
273-
.zip(prev_glyph_id)
274-
.map(|(font_impl, prev_glyph_id)| {
275-
font_impl.pair_kerning(prev_glyph_id, glyph_info.id)
276-
})
277-
.unwrap_or_default();
278-
279-
// replace the glyph
280-
last_glyph.chr = overflow_character;
281-
let (font_impl, glyph_info) = font.glyph_info_and_font_impl(last_glyph.chr);
282-
last_glyph.size = vec2(glyph_info.advance_width, font_height);
283-
last_glyph.uv_rect = glyph_info.uv_rect;
284-
285-
// reapply kerning
286-
last_glyph.pos.x += font_impl
287-
.zip(prev_glyph_id)
288-
.map(|(font_impl, prev_glyph_id)| {
289-
font_impl.pair_kerning(prev_glyph_id, glyph_info.id)
290-
})
291-
.unwrap_or_default();
292-
293-
// check if we're still within width budget
294-
let row_end_x = last_glyph.max_x();
295-
let row_start_x = row.glyphs.first().unwrap().pos.x; // if `last_mut()` returned `Some`, then so will `first()`
296-
let row_width = row_end_x - row_start_x;
297-
if row_width <= job.wrap.max_width {
298-
break;
299-
}
300-
301-
row.glyphs.pop();
302-
}
303-
}
251+
replace_last_glyph_with_overflow_character(fonts, job, out_rows);
304252
} else {
305253
let glyphs: Vec<Glyph> = paragraph.glyphs[row_start_idx..]
306254
.iter()
@@ -324,6 +272,68 @@ fn line_break(
324272
}
325273
}
326274

275+
fn replace_last_glyph_with_overflow_character(
276+
fonts: &mut FontsImpl,
277+
job: &LayoutJob,
278+
out_rows: &mut Vec<Row>,
279+
) {
280+
let overflow_character = match job.wrap.overflow_character {
281+
Some(c) => c,
282+
None => return,
283+
};
284+
285+
let row = match out_rows.last_mut() {
286+
Some(r) => r,
287+
None => return,
288+
};
289+
290+
loop {
291+
let (prev_glyph, last_glyph) = match row.glyphs.as_mut_slice() {
292+
[.., prev, last] => (Some(prev), last),
293+
[.., last] => (None, last),
294+
_ => break,
295+
};
296+
297+
let section = &job.sections[last_glyph.section_index as usize];
298+
let font = fonts.font(&section.format.font_id);
299+
let font_height = font.row_height();
300+
301+
let prev_glyph_id = prev_glyph.map(|prev_glyph| {
302+
let (_, prev_glyph_info) = font.glyph_info_and_font_impl(prev_glyph.chr);
303+
prev_glyph_info.id
304+
});
305+
306+
// undo kerning with previous glyph
307+
let (font_impl, glyph_info) = font.glyph_info_and_font_impl(last_glyph.chr);
308+
last_glyph.pos.x -= font_impl
309+
.zip(prev_glyph_id)
310+
.map(|(font_impl, prev_glyph_id)| font_impl.pair_kerning(prev_glyph_id, glyph_info.id))
311+
.unwrap_or_default();
312+
313+
// replace the glyph
314+
last_glyph.chr = overflow_character;
315+
let (font_impl, glyph_info) = font.glyph_info_and_font_impl(last_glyph.chr);
316+
last_glyph.size = vec2(glyph_info.advance_width, font_height);
317+
last_glyph.uv_rect = glyph_info.uv_rect;
318+
319+
// reapply kerning
320+
last_glyph.pos.x += font_impl
321+
.zip(prev_glyph_id)
322+
.map(|(font_impl, prev_glyph_id)| font_impl.pair_kerning(prev_glyph_id, glyph_info.id))
323+
.unwrap_or_default();
324+
325+
// check if we're still within width budget
326+
let row_end_x = last_glyph.max_x();
327+
let row_start_x = row.glyphs.first().unwrap().pos.x; // if `last_mut()` returned `Some`, then so will `first()`
328+
let row_width = row_end_x - row_start_x;
329+
if row_width <= job.wrap.max_width {
330+
break;
331+
}
332+
333+
row.glyphs.pop();
334+
}
335+
}
336+
327337
fn halign_and_jusitfy_row(
328338
point_scale: PointScale,
329339
row: &mut Row,

0 commit comments

Comments
 (0)