From 8be8047b8d6b92c8b948463af80eee7398b4069f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 27 Sep 2023 22:02:48 -0600 Subject: [PATCH] Checkpoint --- crates/gpui3/src/geometry.rs | 52 ++++++++++++++++++++++----------- crates/gpui3/src/text_system.rs | 20 ++++++++++--- 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/crates/gpui3/src/geometry.rs b/crates/gpui3/src/geometry.rs index 0dc2bb592156c12eb8e9c3ebbd8e11593d051af1..9b4f04ab3f76de2798d1273eaf4f913bc2bc98c8 100644 --- a/crates/gpui3/src/geometry.rs +++ b/crates/gpui3/src/geometry.rs @@ -29,13 +29,17 @@ impl Point { } } -impl, S: Clone> Mul for Point { - type Output = Self; +impl Mul for Point +where + T: Mul + Clone + Debug, + Rhs: Clone + Debug, +{ + type Output = Point; - fn mul(self, rhs: S) -> Self::Output { - Self { - x: self.x.clone() * rhs.clone(), - y: self.y.clone() * rhs, + fn mul(self, rhs: Rhs) -> Self::Output { + Point { + x: self.x * rhs.clone(), + y: self.y * rhs, } } } @@ -125,13 +129,17 @@ impl Size { } } -impl, S: Clone> Mul for Size { - type Output = Self; +impl Mul for Size +where + T: Mul + Debug + Clone, + Rhs: Debug + Clone, +{ + type Output = Size; - fn mul(self, rhs: S) -> Self::Output { - Self { - width: self.width.clone() * rhs.clone(), - height: self.height.clone() * rhs, + fn mul(self, rhs: Rhs) -> Self::Output { + Size { + width: self.width * rhs.clone(), + height: self.height * rhs, } } } @@ -190,14 +198,16 @@ pub struct Bounds { unsafe impl Zeroable for Bounds {} unsafe impl Pod for Bounds {} -impl, S: Clone> Mul for Bounds +// Bounds * Pixels = Bounds +impl Mul for Bounds where - T: Mul, + T: Mul + Clone + Debug, + Rhs: Clone + Debug, { - type Output = Self; + type Output = Bounds; - fn mul(self, rhs: S) -> Self::Output { - Self { + fn mul(self, rhs: Rhs) -> Self::Output { + Bounds { origin: self.origin * rhs.clone(), size: self.size * rhs, } @@ -404,6 +414,14 @@ impl Mul for Pixels { } } +impl Mul for f32 { + type Output = Pixels; + + fn mul(self, rhs: Pixels) -> Self::Output { + Pixels(self * rhs.0) + } +} + impl Pixels { pub fn round(&self) -> Self { Self(self.0.round()) diff --git a/crates/gpui3/src/text_system.rs b/crates/gpui3/src/text_system.rs index bf6b528a425486f04b716e59ee8d768b3d608f8a..f33ee9e0360aa807f437cbd195493fb744a654e0 100644 --- a/crates/gpui3/src/text_system.rs +++ b/crates/gpui3/src/text_system.rs @@ -8,7 +8,7 @@ use line_wrapper::*; pub use text_layout_cache::*; use crate::{ - px, Bounds, Hsla, Pixels, PlatformTextSystem, Point, Result, SharedString, UnderlineStyle, + px, Bounds, Hsla, Pixels, PlatformTextSystem, Point, Result, SharedString, Size, UnderlineStyle, }; use collections::HashMap; use core::fmt; @@ -90,9 +90,21 @@ impl TextSystem { }) } - pub fn em_advance(&self, font_id: FontId, font_size: Pixels) -> Pixels { - todo!() - // self.font_cache.em_advance(font_id, font_size) + pub fn advance(&self, font: &Font, font_size: Pixels, ch: char) -> Result> { + let font_id = self.font_id(font)?; + let glyph_id = self + .platform_text_system + .glyph_for_char(font_id, ch) + .ok_or_else(|| anyhow!("glyph not found for character '{}'", ch))?; + 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!()) + } + + 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 {