Kaynağa Gözat

LibGfx: Make Font::preferred_line_height() more correct

Return a float, and fix a bogus calculation of ascender + descender.
Andreas Kling 2 yıl önce
ebeveyn
işleme
2a61d66b0a

+ 2 - 2
Userland/Applications/PixelPaint/Tools/TextTool.cpp

@@ -130,7 +130,7 @@ void TextTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
     painter.translate(editor_layer_location(*layer));
     auto typed_text = m_text_editor->text();
     auto text_width = max<int>(m_selected_font->width(typed_text), m_selected_font->width(" "sv));
-    auto text_height = m_selected_font->preferred_line_height() * max<int>(static_cast<int>(m_text_editor->line_count()), 1);
+    auto text_height = static_cast<int>(ceilf(m_selected_font->preferred_line_height() * max<int>(static_cast<int>(m_text_editor->line_count()), 1)));
     auto text_location = editor_stroke_position(m_add_text_position, 1);
 
     // Since ImageEditor can be zoomed in/out, we need to be able to render the preview properly scaled
@@ -238,7 +238,7 @@ void TextTool::apply_text_to_layer()
 
     auto demo_text = m_text_editor->text();
     auto text_width = m_selected_font->width(demo_text);
-    auto text_height = m_selected_font->preferred_line_height() * static_cast<int>(m_text_editor->line_count());
+    auto text_height = static_cast<int>(ceilf(m_selected_font->preferred_line_height() * static_cast<int>(m_text_editor->line_count())));
 
     painter.set_font(*m_selected_font);
     auto text_rect = Gfx::Rect<int>(m_add_text_position, { static_cast<int>(ceilf(text_width)), text_height });

+ 1 - 1
Userland/Libraries/LibGUI/TextEditor.cpp

@@ -2193,7 +2193,7 @@ void TextEditor::set_editing_engine(OwnPtr<EditingEngine> editing_engine)
 
 int TextEditor::line_height() const
 {
-    return font().preferred_line_height();
+    return static_cast<int>(ceilf(font().preferred_line_height()));
 }
 
 int TextEditor::fixed_glyph_width() const

+ 1 - 1
Userland/Libraries/LibGfx/Font/BitmapFont.h

@@ -69,7 +69,7 @@ public:
     float glyphs_horizontal_kerning(u32, u32) const override { return 0.f; }
     u8 glyph_height() const override { return m_glyph_height; }
     int x_height() const override { return m_x_height; }
-    int preferred_line_height() const override { return glyph_height() + m_line_gap; }
+    virtual float preferred_line_height() const override { return glyph_height() + m_line_gap; }
 
     virtual float glyph_width(u32 code_point) const override;
     u8 raw_glyph_width(u32 code_point) const { return m_glyph_widths[code_point]; }

+ 1 - 1
Userland/Libraries/LibGfx/Font/Font.h

@@ -156,7 +156,7 @@ public:
     virtual float glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const = 0;
     virtual u8 glyph_height() const = 0;
     virtual int x_height() const = 0;
-    virtual int preferred_line_height() const = 0;
+    virtual float preferred_line_height() const = 0;
 
     virtual u8 min_glyph_width() const = 0;
     virtual u8 max_glyph_width() const = 0;

+ 1 - 1
Userland/Libraries/LibGfx/Font/OpenType/Font.cpp

@@ -534,7 +534,7 @@ Gfx::ScaledFontMetrics Font::metrics([[maybe_unused]] float x_scale, float y_sca
 
     return Gfx::ScaledFontMetrics {
         .ascender = static_cast<float>(raw_ascender) * y_scale,
-        .descender = static_cast<float>(raw_descender) * y_scale,
+        .descender = -static_cast<float>(raw_descender) * y_scale,
         .line_gap = static_cast<float>(raw_line_gap) * y_scale,
     };
 }

+ 1 - 1
Userland/Libraries/LibGfx/Font/ScaledFont.cpp

@@ -111,7 +111,7 @@ Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const
         .advance_of_ascii_zero = (float)glyph_width('0'),
         .glyph_spacing = (float)glyph_spacing(),
         .ascent = metrics.ascender,
-        .descent = -metrics.descender,
+        .descent = metrics.descender,
         .line_gap = metrics.line_gap,
     };
 }

+ 1 - 1
Userland/Libraries/LibGfx/Font/ScaledFont.h

@@ -55,7 +55,7 @@ public:
     virtual float glyph_width(u32 code_point) const override;
     virtual float glyph_or_emoji_width(u32 code_point) const override;
     virtual float glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const override;
-    virtual int preferred_line_height() const override { return metrics().height() + metrics().line_gap; }
+    virtual float preferred_line_height() const override { return metrics().height() + metrics().line_gap; }
     virtual u8 glyph_height() const override { return m_point_height; }
     virtual int x_height() const override { return m_point_height; }      // FIXME: Read from font
     virtual u8 min_glyph_width() const override { return 1; }             // FIXME: Read from font

+ 2 - 2
Userland/Libraries/LibGfx/Font/VectorFont.h

@@ -18,9 +18,9 @@ struct ScaledFontMetrics {
     float descender { 0 };
     float line_gap { 0 };
 
-    int height() const
+    float height() const
     {
-        return ascender - descender;
+        return ascender + descender;
     }
 };