Bladeren bron

LibGfx: Add a simple Gfx::FontMetrics and Gfx::Font::metrics(code_point)

This is used to get a handy set of glyph metrics.
Andreas Kling 3 jaren geleden
bovenliggende
commit
d7586aff53

+ 10 - 0
Userland/Libraries/LibGfx/BitmapFont.cpp

@@ -373,4 +373,14 @@ Font const& Font::bold_variant() const
     return *m_bold_variant;
     return *m_bold_variant;
 }
 }
 
 
+FontMetrics Font::metrics(u32 code_point) const
+{
+    return FontMetrics {
+        .size = (float)presentation_size(),
+        .x_height = (float)x_height(),
+        .glyph_width = (float)glyph_width(code_point),
+        .glyph_spacing = (float)glyph_spacing(),
+    };
+}
+
 }
 }

+ 9 - 0
Userland/Libraries/LibGfx/Font.h

@@ -81,11 +81,20 @@ private:
     int m_ascent;
     int m_ascent;
 };
 };
 
 
+struct FontMetrics {
+    float size { 0 };
+    float x_height { 0 };
+    float glyph_width { 0 };
+    float glyph_spacing { 0 };
+};
+
 class Font : public RefCounted<Font> {
 class Font : public RefCounted<Font> {
 public:
 public:
     virtual NonnullRefPtr<Font> clone() const = 0;
     virtual NonnullRefPtr<Font> clone() const = 0;
     virtual ~Font() {};
     virtual ~Font() {};
 
 
+    FontMetrics metrics(u32 code_point) const;
+
     virtual u8 presentation_size() const = 0;
     virtual u8 presentation_size() const = 0;
 
 
     virtual u16 weight() const = 0;
     virtual u16 weight() const = 0;

+ 1 - 0
Userland/Libraries/LibGfx/Forward.h

@@ -14,6 +14,7 @@ class Color;
 class DisjointRectSet;
 class DisjointRectSet;
 class Emoji;
 class Emoji;
 class Font;
 class Font;
+class FontMetrics;
 class GlyphBitmap;
 class GlyphBitmap;
 class ImageDecoder;
 class ImageDecoder;