Re-use the TextMetrics data structure in the Layout 2020 fragment tree (#30823)

This data structure has all of the metrics needed to render a font and
is in `Au`. We'll need more of these metrics for implementing
`vertical-align` and its use doesn't increase the size of the Fragment
tree (as the BoxFragment is still larger). In addition, this will be
helpful when switching layout to `Au`.
This commit is contained in:
Martin Robinson 2023-12-06 10:52:23 +01:00 committed by GitHub
parent f0b4162328
commit 8ded1072ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 46 deletions

View file

@ -281,7 +281,7 @@ impl Fragment {
.to_physical(fragment.parent_style.writing_mode, containing_block)
.translate(containing_block.origin.to_vector());
let mut baseline_origin = rect.origin.clone();
baseline_origin.y += fragment.font_metrics.ascent;
baseline_origin.y += Length::from(fragment.font_metrics.ascent);
let glyphs = glyphs(&fragment.glyphs, baseline_origin);
if glyphs.is_empty() {
return;
@ -306,10 +306,6 @@ impl Fragment {
let color = fragment.parent_style.clone_color();
let font_metrics = &fragment.font_metrics;
let dppx = builder.context.style_context.device_pixel_ratio().get();
let round_to_nearest_device_pixel = |value: Length| -> Length {
// Round to the nearest integer device pixel, ensuring at least one device pixel.
Length::new((value.px() * dppx).round().max(1.0) / dppx)
};
// Underline.
if fragment
@ -317,8 +313,9 @@ impl Fragment {
.contains(TextDecorationLine::UNDERLINE)
{
let mut rect = rect;
rect.origin.y = rect.origin.y + font_metrics.ascent - font_metrics.underline_offset;
rect.size.height = round_to_nearest_device_pixel(font_metrics.underline_size);
rect.origin.y =
rect.origin.y + Length::from(font_metrics.ascent - font_metrics.underline_offset);
rect.size.height = Length::new(font_metrics.underline_size.to_nearest_pixel(dppx));
self.build_display_list_for_text_decoration(fragment, builder, &rect, &color);
}
@ -328,7 +325,7 @@ impl Fragment {
.contains(TextDecorationLine::OVERLINE)
{
let mut rect = rect;
rect.size.height = round_to_nearest_device_pixel(font_metrics.underline_size);
rect.size.height = Length::new(font_metrics.underline_size.to_nearest_pixel(dppx));
self.build_display_list_for_text_decoration(fragment, builder, &rect, &color);
}
@ -349,9 +346,10 @@ impl Fragment {
.contains(TextDecorationLine::LINE_THROUGH)
{
let mut rect = rect;
rect.origin.y = rect.origin.y + font_metrics.ascent - font_metrics.strikeout_offset;
rect.origin.y =
rect.origin.y + Length::from(font_metrics.ascent - font_metrics.strikeout_offset);
// XXX(ferjm) This does not work on MacOS #942
rect.size.height = round_to_nearest_device_pixel(font_metrics.strikeout_size);
rect.size.height = Length::new(font_metrics.strikeout_size.to_nearest_pixel(dppx));
self.build_display_list_for_text_decoration(fragment, builder, &rect, &color);
}
}