Checkpoint

This commit is contained in:
Nathan Sobo 2023-09-27 23:05:39 -06:00
parent 8be8047b8d
commit 1ee70a0146
4 changed files with 30 additions and 11 deletions

View File

@ -42,7 +42,9 @@ impl<S: 'static> Element for Text<S> {
let text_system = cx.text_system().clone();
let text_style = cx.text_style();
let font_size = text_style.font_size * cx.rem_size();
let line_height = cx.text_system().line_height(font_size);
let line_height = text_style
.line_height
.to_pixels(font_size.into(), cx.rem_size());
let text = self.text.clone();
let paint_state = Arc::new(Mutex::new(None));

View File

@ -572,6 +572,18 @@ pub enum DefiniteLength {
Fraction(f32),
}
impl DefiniteLength {
pub fn to_pixels(&self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels {
match self {
DefiniteLength::Absolute(size) => size.to_pixels(rem_size),
DefiniteLength::Fraction(fraction) => match base_size {
AbsoluteLength::Pixels(px) => px * *fraction,
AbsoluteLength::Rems(rems) => rems * rem_size * *fraction,
},
}
}
}
impl Debug for DefiniteLength {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
@ -625,6 +637,11 @@ pub fn relative(fraction: f32) -> DefiniteLength {
DefiniteLength::Fraction(fraction).into()
}
/// Returns the Golden Ratio, i.e. `~(1.0 + sqrt(5.0)) / 2.0`.
pub fn phi() -> DefiniteLength {
relative(1.61803398875)
}
pub fn rems(rems: f32) -> Rems {
Rems(rems)
}

View File

@ -1,5 +1,5 @@
use crate::{
rems, AbsoluteLength, Bounds, Corners, CornersRefinement, DefiniteLength, Edges,
phi, rems, AbsoluteLength, Bounds, Corners, CornersRefinement, DefiniteLength, Edges,
EdgesRefinement, Font, FontFeatures, FontStyle, FontWeight, Hsla, Length, Pixels, Point,
PointRefinement, Rems, Result, RunStyle, SharedString, Size, SizeRefinement, ViewContext,
WindowContext,
@ -100,6 +100,7 @@ pub struct TextStyle {
pub font_family: SharedString,
pub font_features: FontFeatures,
pub font_size: Rems,
pub line_height: DefiniteLength,
pub font_weight: FontWeight,
pub font_style: FontStyle,
pub underline: Option<UnderlineStyle>,
@ -112,6 +113,7 @@ impl Default for TextStyle {
font_family: SharedString::default(),
font_features: FontFeatures::default(),
font_size: rems(1.),
line_height: phi(),
font_weight: FontWeight::default(),
font_style: FontStyle::default(),
underline: None,

View File

@ -99,19 +99,13 @@ impl TextSystem {
let result =
self.platform_text_system.advance(font_id, glyph_id)? / self.units_per_em(font)? as f32;
let result = result * font_size;
Ok(todo!())
Ok(result * font_size)
}
pub fn units_per_em(&self, font: &Font) -> Result<u32> {
self.read_metrics(font, |metrics| metrics.units_per_em as u32)
}
pub fn line_height(&self, font_size: Pixels) -> Pixels {
todo!()
// self.font_cache.line_height(font_size)
}
pub fn cap_height(&self, font: &Font, font_size: Pixels) -> Result<Pixels> {
self.read_metrics(font, |metrics| metrics.cap_height(font_size))
}
@ -128,8 +122,12 @@ impl TextSystem {
self.read_metrics(font, |metrics| metrics.descent(font_size))
}
pub fn baseline_offset(&self, font: &Font, font_size: Pixels) -> Result<Pixels> {
let line_height = self.line_height(font_size);
pub fn baseline_offset(
&self,
font: &Font,
font_size: Pixels,
line_height: Pixels,
) -> Result<Pixels> {
let ascent = self.ascent(font, font_size)?;
let descent = self.descent(font, font_size)?;
let padding_top = (line_height - ascent - descent) / 2.;