text: Assume at least one span is present during layout

After some past refactors, it's true now that during layout there's
at least one text span, which is ensured by the span normalization.
This refactor simplifies code.
This commit is contained in:
Kamil Jarosz 2024-06-27 14:41:45 +02:00 committed by Nathan Adams
parent a606fcc8ed
commit dcafb3d291
1 changed files with 10 additions and 20 deletions

View File

@ -257,7 +257,8 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
context: &mut UpdateContext<'_, 'gc>, context: &mut UpdateContext<'_, 'gc>,
only_line: bool, only_line: bool,
final_line_of_para: bool, final_line_of_para: bool,
text: Option<(&'a WStr, usize, &TextSpan)>, end: usize,
span: &TextSpan,
font_type: FontType, font_type: FontType,
) { ) {
let mut line_bounds = None; let mut line_bounds = None;
@ -335,10 +336,8 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
box_count += 1; box_count += 1;
} }
if let Some((text, end, span)) = text { if self.boxes.is_empty() {
if box_count == 0 { self.append_text(WStr::empty(), end, end, span);
self.append_text(&text[end..end], end, end, span);
}
} }
self.append_underlines(); self.append_underlines();
@ -388,12 +387,11 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
fn explicit_newline( fn explicit_newline(
&mut self, &mut self,
context: &mut UpdateContext<'_, 'gc>, context: &mut UpdateContext<'_, 'gc>,
text: &'a WStr,
end: usize, end: usize,
span: &TextSpan, span: &TextSpan,
font_type: FontType, font_type: FontType,
) { ) {
self.fixup_line(context, false, true, Some((text, end, span)), font_type); self.fixup_line(context, false, true, end, span, font_type);
self.cursor.set_x(Twips::ZERO); self.cursor.set_x(Twips::ZERO);
self.cursor += ( self.cursor += (
@ -417,12 +415,11 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
fn newline( fn newline(
&mut self, &mut self,
context: &mut UpdateContext<'_, 'gc>, context: &mut UpdateContext<'_, 'gc>,
text: &'a WStr,
end: usize, end: usize,
span: &TextSpan, span: &TextSpan,
font_type: FontType, font_type: FontType,
) { ) {
self.fixup_line(context, false, false, Some((text, end, span)), font_type); self.fixup_line(context, false, false, end, span, font_type);
self.cursor.set_x(Twips::ZERO); self.cursor.set_x(Twips::ZERO);
self.cursor += ( self.cursor += (
@ -701,12 +698,13 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {
fs: &'a FormatSpans, fs: &'a FormatSpans,
font_type: FontType, font_type: FontType,
) -> Layout<'gc> { ) -> Layout<'gc> {
let last_span = fs.last_span().expect("At least one span should be present");
self.fixup_line( self.fixup_line(
context, context,
!self.has_line_break, !self.has_line_break,
true, true,
fs.last_span() fs.displayed_text().len(),
.map(|ls| (fs.displayed_text(), fs.displayed_text().len(), ls)), last_span,
font_type, font_type,
); );
@ -984,7 +982,6 @@ pub fn lower_from_text_spans<'gc>(
match delimiter { match delimiter {
Some(b'\n' | b'\r') => layout_context.explicit_newline( Some(b'\n' | b'\r') => layout_context.explicit_newline(
context, context,
fs.displayed_text(),
span_start + slice_start - 1, span_start + slice_start - 1,
span, span,
font_type, font_type,
@ -1022,7 +1019,6 @@ pub fn lower_from_text_spans<'gc>(
} else if breakpoint == 0 { } else if breakpoint == 0 {
layout_context.newline( layout_context.newline(
context, context,
fs.displayed_text(),
start + next_breakpoint, start + next_breakpoint,
span, span,
font_type, font_type,
@ -1052,13 +1048,7 @@ pub fn lower_from_text_spans<'gc>(
break; break;
} }
layout_context.newline( layout_context.newline(context, start + next_breakpoint, span, font_type);
context,
fs.displayed_text(),
start + next_breakpoint,
span,
font_type,
);
let next_dim = layout_context.wrap_dimensions(span); let next_dim = layout_context.wrap_dimensions(span);
width = next_dim.0; width = next_dim.0;