dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / layout / dominant_baseline.lucb
#==============================================================================================
#
# dominant_baseline - Layout: the dominant-baseline offsets of inline layout and SVG text
#
# DESCRIPTION:
# Port of LibWeb/Layout/DominantBaseline.h (lines 1-62): the offset of a baseline metric
# from the alphabetic baseline, and the metric dominant-baseline resolves to.
#
# Copyright (c) 2026, Tim Ledbetter <tim.ledbetter@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/Layout/DominantBaseline.h:15 Web::Layout::dominant_baseline_offset.
# https://drafts.csswg.org/css-inline/#dominant-baseline-property
func layout_dominant_baseline_offset(metric: css_data.BaselineMetric, font_metrics: web_fonts.FontPixelMetrics) -> f32:
match metric:
.central:
return (font_metrics.ascent - font_metrics.descent) / 2.0f32
.middle:
return font_metrics.x_height / 2.0f32
.hanging:
# FIXME: Read the hanging baseline from the font's BASE table.
return font_metrics.ascent * 0.8f32
.ideographic:
# FIXME: Read the ideographic baseline from the font's BASE table.
return -font_metrics.descent
.mathematical:
# FIXME: Read the math baseline from the font's BASE table.
return font_metrics.ascent * 0.5f32
.text_top, .text_bottom, .alphabetic:
# FIXME: Support text-top and text-bottom.
return 0.0f32
_:
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/Layout/DominantBaseline.h:40 Web::Layout::resolve_dominant_baseline_metric.
func layout_resolve_dominant_baseline_metric(computed_values: const CssComputedValues*) -> css_data.BaselineMetric:
if let dominant_baseline = css_computed_values_dominant_baseline(computed_values):
return dominant_baseline
# https://drafts.csswg.org/css-inline/#valdef-dominant-baseline-auto
# Equivalent to alphabetic in horizontal writing modes and in vertical writing modes when text-orientation is
# sideways. Equivalent to central in vertical writing modes when text-orientation is mixed or upright.
# FIXME: Take text-orientation into account once it is implemented.
match css_computed_values_writing_mode(computed_values):
.horizontal_tb, .sideways_rl, .sideways_lr:
return css_data.BaselineMetric.alphabetic
.vertical_rl, .vertical_lr:
return css_data.BaselineMetric.central
_:
trap("VERIFY_NOT_REACHED")