Center line around its bounding box

Antonio Scandurra created

Change summary

gpui/src/lib.rs                       |  2 +-
gpui/src/platform/mac/sprite_cache.rs |  1 -
gpui/src/text_layout.rs               | 11 +++++++----
3 files changed, 8 insertions(+), 6 deletions(-)

Detailed changes

gpui/src/lib.rs 🔗

@@ -8,7 +8,7 @@ pub use font_cache::FontCache;
 pub mod fonts;
 mod presenter;
 mod scene;
-pub use scene::{Border, Scene};
+pub use scene::{Border, Quad, Scene};
 pub mod text_layout;
 pub use text_layout::TextLayoutCache;
 mod util;

gpui/src/platform/mac/sprite_cache.rs 🔗

@@ -112,7 +112,6 @@ impl SpriteCache {
                 // Snap sprite to pixel grid.
                 let offset = glyph_bounds.origin().to_f32()
                     - vec2f(target_position.x().fract(), target_position.y().fract());
-
                 Some(GlyphSprite {
                     atlas_id: atlasses.len() - 1,
                     atlas_origin: atlas_bounds.origin(),

gpui/src/text_layout.rs 🔗

@@ -1,12 +1,14 @@
 use crate::{
     color::ColorU,
     fonts::{FontId, GlyphId},
-    geometry::rect::RectF,
+    geometry::{
+        rect::RectF,
+        vector::{vec2f, Vector2F},
+    },
     platform, scene, PaintContext,
 };
 use ordered_float::OrderedFloat;
 use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard};
-use pathfinder_geometry::vector::Vector2F;
 use smallvec::SmallVec;
 use std::{
     borrow::Borrow,
@@ -185,9 +187,10 @@ impl Line {
 
         for run in &self.runs {
             let bounding_box = ctx.font_cache.bounding_box(run.font_id, self.font_size);
+            let descent = ctx.font_cache.descent(run.font_id, self.font_size);
             let max_glyph_width = bounding_box.x();
             for glyph in &run.glyphs {
-                let glyph_origin = bounds.origin() + glyph.position;
+                let glyph_origin = bounds.origin() + glyph.position - vec2f(0.0, descent);
                 if glyph_origin.x() + max_glyph_width < bounds.origin().x() {
                     continue;
                 }
@@ -208,7 +211,7 @@ impl Line {
                     font_id: run.font_id,
                     font_size: self.font_size,
                     id: glyph.id,
-                    origin: glyph_origin,
+                    origin: glyph_origin + vec2f(0., bounding_box.y() / 2.),
                     color,
                 });
             }