From 1ee70a014638afe550ef18bff1a8928b35d501ca Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 27 Sep 2023 23:05:39 -0600 Subject: [PATCH] Checkpoint --- crates/gpui3/src/elements/text.rs | 4 +++- crates/gpui3/src/geometry.rs | 17 +++++++++++++++++ crates/gpui3/src/style.rs | 4 +++- crates/gpui3/src/text_system.rs | 16 +++++++--------- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/gpui3/src/elements/text.rs b/crates/gpui3/src/elements/text.rs index 6530a3c42300527f664729831ef78e8df4b39606..e188370a6f32968828b9929ad3613cd89a98241d 100644 --- a/crates/gpui3/src/elements/text.rs +++ b/crates/gpui3/src/elements/text.rs @@ -42,7 +42,9 @@ impl Element for Text { 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)); diff --git a/crates/gpui3/src/geometry.rs b/crates/gpui3/src/geometry.rs index 9b4f04ab3f76de2798d1273eaf4f913bc2bc98c8..c587096e7c0acfb0ed6742998d7dff7196285c95 100644 --- a/crates/gpui3/src/geometry.rs +++ b/crates/gpui3/src/geometry.rs @@ -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) } diff --git a/crates/gpui3/src/style.rs b/crates/gpui3/src/style.rs index 2d1467d6a72bb5047f853bc3f9654c72ec22706f..a8bac7ed782a8a7c931ed5e25730685a4a0fe7e8 100644 --- a/crates/gpui3/src/style.rs +++ b/crates/gpui3/src/style.rs @@ -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, @@ -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, diff --git a/crates/gpui3/src/text_system.rs b/crates/gpui3/src/text_system.rs index f33ee9e0360aa807f437cbd195493fb744a654e0..c65331a8da5e0e94df9929c82657a25e9dda3326 100644 --- a/crates/gpui3/src/text_system.rs +++ b/crates/gpui3/src/text_system.rs @@ -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 { 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 { 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 { - let line_height = self.line_height(font_size); + pub fn baseline_offset( + &self, + font: &Font, + font_size: Pixels, + line_height: Pixels, + ) -> Result { let ascent = self.ascent(font, font_size)?; let descent = self.descent(font, font_size)?; let padding_top = (line_height - ascent - descent) / 2.;