Checkpoint

Nathan Sobo created

Change summary

crates/gpui3/src/elements/svg.rs |  5 +
crates/gpui3/src/geometry.rs     | 38 +++++++++++++-
crates/gpui3/src/text_system.rs  | 90 ++++++++++++++++++++++++++++++---
3 files changed, 118 insertions(+), 15 deletions(-)

Detailed changes

crates/gpui3/src/elements/svg.rs 🔗

@@ -41,14 +41,15 @@ impl<S: 'static> Element for Svg<S> {
 
     fn paint(
         &mut self,
-        layout: Layout,
+        _layout: Layout,
         _: &mut Self::State,
         _: &mut Self::FrameState,
-        cx: &mut crate::ViewContext<S>,
+        _cx: &mut crate::ViewContext<S>,
     ) -> Result<()>
     where
         Self: Sized,
     {
+        // todo!
         // let fill_color = self.computed_style().fill.and_then(|fill| fill.color());
         // if let Some((path, fill_color)) = self.path.as_ref().zip(fill_color) {
         //     if let Some(svg_tree) = cx.asset_cache.svg(path).log_err() {

crates/gpui3/src/geometry.rs 🔗

@@ -2,10 +2,10 @@ use bytemuck::{Pod, Zeroable};
 use core::fmt::Debug;
 use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
 use refineable::Refineable;
-use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
+use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign};
 
 #[derive(
-    Refineable, Default, Add, AddAssign, Sub, SubAssign, Mul, Div, Copy, Debug, PartialEq, Eq, Hash,
+    Refineable, Default, Add, AddAssign, Sub, SubAssign, Mul, Copy, Debug, PartialEq, Eq, Hash,
 )]
 #[refineable(debug)]
 #[repr(C)]
@@ -52,6 +52,17 @@ impl<T: Clone + Debug + Add<Output = T> + Copy> AddAssign<T> for Point<T> {
     }
 }
 
+impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Point<T> {
+    type Output = Self;
+
+    fn div(self, rhs: S) -> Self::Output {
+        Self {
+            x: self.x / rhs.clone(),
+            y: self.y / rhs,
+        }
+    }
+}
+
 impl<T: Clone + Debug + std::cmp::PartialOrd> Point<T> {
     pub fn max(&self, other: &Self) -> Self {
         Point {
@@ -81,7 +92,7 @@ impl<T: Clone + Debug> Clone for Point<T> {
 unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Point<T> {}
 unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Point<T> {}
 
-#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq)]
+#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq, Div)]
 #[refineable(debug)]
 #[repr(C)]
 pub struct Size<T: Clone + Debug> {
@@ -166,6 +177,20 @@ impl<T: Clone + Debug + Mul<S, Output = T>, S: Clone> MulAssign<S> for Bounds<T>
     }
 }
 
+impl<T: Clone + Debug + Div<S, Output = T>, S: Clone> Div<S> for Bounds<T>
+where
+    Size<T>: Div<S, Output = Size<T>>,
+{
+    type Output = Self;
+
+    fn div(self, rhs: S) -> Self {
+        Self {
+            origin: self.origin / rhs.clone(),
+            size: self.size / rhs,
+        }
+    }
+}
+
 impl<T: Clone + Debug + Add<T, Output = T>> Bounds<T> {
     pub fn upper_right(&self) -> Point<T> {
         Point {
@@ -189,6 +214,13 @@ impl<T: Clone + Debug + PartialOrd + Add<T, Output = T>> Bounds<T> {
             && point.y >= self.origin.y
             && point.y <= self.origin.y.clone() + self.size.height.clone()
     }
+
+    pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Bounds<U> {
+        Bounds {
+            origin: self.origin.map(&f),
+            size: self.size.map(f),
+        }
+    }
 }
 
 impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}

crates/gpui3/src/text_system.rs 🔗

@@ -7,12 +7,13 @@ use line_wrapper::*;
 pub use text_layout_cache::*;
 
 use crate::{
-    Bounds, Hsla, Pixels, PlatformTextSystem, Point, Result, SharedString, Size, UnderlineStyle,
+    px, Bounds, Hsla, Pixels, PlatformTextSystem, Point, Result, SharedString, Size, UnderlineStyle,
 };
 use collections::HashMap;
 use core::fmt;
 use parking_lot::Mutex;
 use std::{
+    borrow::BorrowMut,
     fmt::{Debug, Display, Formatter},
     hash::{Hash, Hasher},
     ops::{Deref, DerefMut},
@@ -46,6 +47,7 @@ impl TextSystem {
 
     pub fn bounding_box(&self, font_id: FontId, font_size: Pixels) -> Size<Pixels> {
         let metrics = self.platform_text_system.font_metrics(font_id);
+        metrics.bounding_box(font_size);
 
         todo!()
         // self.font_cache.bounding_box(font_id, font_size)
@@ -293,15 +295,83 @@ impl Font {
     }
 }
 
+/// A struct for storing font metrics.
+/// It is used to define the measurements of a typeface.
 #[derive(Clone, Copy, Debug)]
 pub struct FontMetrics {
-    pub units_per_em: u32,
-    pub ascent: f32,
-    pub descent: f32,
-    pub line_gap: f32,
-    pub underline_position: f32,
-    pub underline_thickness: f32,
-    pub cap_height: f32,
-    pub x_height: f32,
-    pub bounding_box: Bounds<f32>,
+    /// The number of font units that make up the "em square",
+    /// a scalable grid for determining the size of a typeface.
+    pub(crate) units_per_em: u32,
+
+    /// The vertical distance from the baseline of the font to the top of the glyph covers.
+    pub(crate) ascent: f32,
+
+    /// The vertical distance from the baseline of the font to the bottom of the glyph covers.
+    pub(crate) descent: f32,
+
+    /// The recommended additional space to add between lines of type.
+    pub(crate) line_gap: f32,
+
+    /// The suggested position of the underline.
+    pub(crate) underline_position: f32,
+
+    /// The suggested thickness of the underline.
+    pub(crate) underline_thickness: f32,
+
+    /// The height of a capital letter measured from the baseline of the font.
+    pub(crate) cap_height: f32,
+
+    /// The height of a lowercase x.
+    pub(crate) x_height: f32,
+
+    /// The outer limits of the area that the font covers.
+    pub(crate) bounding_box: Bounds<f32>,
+}
+
+impl FontMetrics {
+    /// Returns the number of pixels that make up the "em square",
+    /// a scalable grid for determining the size of a typeface.
+    pub fn units_per_em(&self, font_size: Pixels) -> Pixels {
+        Pixels((self.units_per_em as f32 / font_size.0).ceil())
+    }
+
+    /// Returns the vertical distance from the baseline of the font to the top of the glyph covers in pixels.
+    pub fn ascent(&self, font_size: Pixels) -> Pixels {
+        Pixels((self.ascent / font_size.0).ceil() as f32)
+    }
+
+    /// Returns the vertical distance from the baseline of the font to the bottom of the glyph covers in pixels.
+    pub fn descent(&self, font_size: Pixels) -> Pixels {
+        Pixels((self.descent / font_size.0).ceil() as f32)
+    }
+
+    /// Returns the recommended additional space to add between lines of type in pixels.
+    pub fn line_gap(&self, font_size: Pixels) -> Pixels {
+        Pixels((self.line_gap / font_size.0).ceil() as f32)
+    }
+
+    /// Returns the suggested position of the underline in pixels.
+    pub fn underline_position(&self, font_size: Pixels) -> Pixels {
+        Pixels((self.underline_position / font_size.0).ceil() as f32)
+    }
+
+    /// Returns the suggested thickness of the underline in pixels.
+    pub fn underline_thickness(&self, font_size: Pixels) -> Pixels {
+        Pixels((self.underline_thickness / font_size.0).ceil() as f32)
+    }
+
+    /// Returns the height of a capital letter measured from the baseline of the font in pixels.
+    pub fn cap_height(&self, font_size: Pixels) -> Pixels {
+        Pixels((self.cap_height / font_size.0).ceil() as f32)
+    }
+
+    /// Returns the height of a lowercase x in pixels.
+    pub fn x_height(&self, font_size: Pixels) -> Pixels {
+        Pixels((self.x_height / font_size.0).ceil() as f32)
+    }
+
+    /// Returns the outer limits of the area that the font covers in pixels.
+    pub fn bounding_box(&self, font_size: Pixels) -> Bounds<Pixels> {
+        (self.bounding_box / font_size.0).map(px)
+    }
 }