浏览代码

LibPDF: Ask OpenType font programs for glyph widths if needed

If the font dictionary didn't specify custom glyph widths, we would fall
back to the specified "missing width" (or 0 in most cases!), which meant
that we would draw glyphs on top of each other in a lot of cases, namely
for TrueTypeFonts or standard Type1Fonts with an OpenType fallback.

What we actually want to do in this case is ask the OpenType font for
the correct width.
Julian Offenhäuser 2 年之前
父节点
当前提交
fec7ccf020

+ 8 - 7
Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp

@@ -45,17 +45,18 @@ PDFErrorOr<void> SimpleFont::initialize(Document* document, NonnullRefPtr<DictOb
     return {};
 }
 
-float SimpleFont::get_char_width(u8 char_code) const
-{
-    return static_cast<float>(m_widths.get(char_code).value_or(m_missing_width)) / 1000.0f;
-}
-
 PDFErrorOr<Gfx::FloatPoint> SimpleFont::draw_string(Gfx::Painter& painter, Gfx::FloatPoint glyph_position, DeprecatedString const& string, Color const& paint_color, float font_size, float character_spacing, float horizontal_scaling)
 {
     auto so = make_object<StringObject>(string, true);
     for (auto char_code : string.bytes()) {
-        auto char_width = get_char_width(char_code);
-        auto glyph_width = char_width * font_size;
+        // Use the width specified in the font's dictionary if available,
+        // and use the default width for the given font otherwise.
+        float glyph_width;
+        if (auto width = m_widths.get(char_code); width.has_value())
+            glyph_width = font_size * width.value() / 1000.0f;
+        else
+            glyph_width = get_glyph_width(char_code);
+
         draw_glyph(painter, glyph_position, glyph_width, char_code, paint_color);
         auto tx = glyph_width;
         tx += character_spacing;

+ 1 - 2
Userland/Libraries/LibPDF/Fonts/SimpleFont.h

@@ -17,13 +17,12 @@ public:
 
 protected:
     PDFErrorOr<void> initialize(Document* document, NonnullRefPtr<DictObject> const& dict, float font_size) override;
+    virtual float get_glyph_width(u8 char_code) const = 0;
     virtual void draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color) = 0;
     RefPtr<Encoding>& encoding() { return m_encoding; }
     RefPtr<Encoding> const& encoding() const { return m_encoding; }
 
 private:
-    float get_char_width(u8 char_code) const;
-
     RefPtr<Encoding> m_encoding;
     RefPtr<StreamObject> m_to_unicode;
     HashMap<u8, u16> m_widths;

+ 5 - 3
Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp

@@ -30,11 +30,13 @@ PDFErrorOr<void> TrueTypeFont::initialize(Document* document, NonnullRefPtr<Dict
     return {};
 }
 
-void TrueTypeFont::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float, u8 char_code, Color color)
+float TrueTypeFont::get_glyph_width(u8 char_code) const
 {
-    if (!m_font)
-        return;
+    return m_font->glyph_width(char_code);
+}
 
+void TrueTypeFont::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float, u8 char_code, Color color)
+{
     // Account for the reversed font baseline
     auto position = point.translated(0, -m_font->baseline());
     painter.draw_glyph(position, char_code, *m_font, color);

+ 1 - 0
Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h

@@ -14,6 +14,7 @@ namespace PDF {
 
 class TrueTypeFont : public SimpleFont {
 public:
+    float get_glyph_width(u8 char_code) const override;
     void draw_glyph(Gfx::Painter&, Gfx::FloatPoint, float, u8, Color) override;
     Type type() const override { return PDFFont::Type::TrueType; }
 

+ 5 - 0
Userland/Libraries/LibPDF/Fonts/Type1Font.cpp

@@ -49,6 +49,11 @@ PDFErrorOr<void> Type1Font::initialize(Document* document, NonnullRefPtr<DictObj
     return {};
 }
 
+float Type1Font::get_glyph_width(u8 char_code) const
+{
+    return m_font->glyph_width(char_code);
+}
+
 void Type1Font::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color)
 {
     if (!m_font_program) {

+ 1 - 0
Userland/Libraries/LibPDF/Fonts/Type1Font.h

@@ -14,6 +14,7 @@ namespace PDF {
 
 class Type1Font : public SimpleFont {
 public:
+    float get_glyph_width(u8 char_code) const override;
     void draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color) override;
     Type type() const override { return PDFFont::Type::Type1; }