diff --git a/crates/gpui3/src/elements/svg.rs b/crates/gpui3/src/elements/svg.rs index b57fafe98c52c76cb84d232c6d7b5e48b8d949cc..70abf63cb4e62fd95af760c8a8538b96e70cf772 100644 --- a/crates/gpui3/src/elements/svg.rs +++ b/crates/gpui3/src/elements/svg.rs @@ -41,14 +41,15 @@ impl Element for Svg { fn paint( &mut self, - layout: Layout, + _layout: Layout, _: &mut Self::State, _: &mut Self::FrameState, - cx: &mut crate::ViewContext, + _cx: &mut crate::ViewContext, ) -> 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() { diff --git a/crates/gpui3/src/geometry.rs b/crates/gpui3/src/geometry.rs index ec468bc50a698dded7690cd352a3aa967248f5a8..20a291a222efae9c86c29aab633ca75979c5713f 100644 --- a/crates/gpui3/src/geometry.rs +++ b/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 + Copy> AddAssign for Point { } } +impl, S: Clone> Div for Point { + type Output = Self; + + fn div(self, rhs: S) -> Self::Output { + Self { + x: self.x / rhs.clone(), + y: self.y / rhs, + } + } +} + impl Point { pub fn max(&self, other: &Self) -> Self { Point { @@ -81,7 +92,7 @@ impl Clone for Point { unsafe impl Zeroable for Point {} unsafe impl Pod for Point {} -#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq)] +#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq, Div)] #[refineable(debug)] #[repr(C)] pub struct Size { @@ -166,6 +177,20 @@ impl, S: Clone> MulAssign for Bounds } } +impl, S: Clone> Div for Bounds +where + Size: Div>, +{ + type Output = Self; + + fn div(self, rhs: S) -> Self { + Self { + origin: self.origin / rhs.clone(), + size: self.size / rhs, + } + } +} + impl> Bounds { pub fn upper_right(&self) -> Point { Point { @@ -189,6 +214,13 @@ impl> Bounds { && point.y >= self.origin.y && point.y <= self.origin.y.clone() + self.size.height.clone() } + + pub fn map U>(&self, f: F) -> Bounds { + Bounds { + origin: self.origin.map(&f), + size: self.size.map(f), + } + } } impl Copy for Bounds {} diff --git a/crates/gpui3/src/text_system.rs b/crates/gpui3/src/text_system.rs index 12cdf7863c7ddf5d89bcd810705579a9e74fede9..f8b806fd4d88b8cafa2ce9a0788a4e242102b192 100644 --- a/crates/gpui3/src/text_system.rs +++ b/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 { 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, + /// 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, +} + +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 { + (self.bounding_box / font_size.0).map(px) + } }