소스 검색

LibGfx+LibWeb: Remove Gfx::Typeface

...and use VectorFont directly in FontDatabase.
Aliaksandr Kalenik 1 년 전
부모
커밋
e2726ce8e4

+ 0 - 1
Userland/Libraries/LibGfx/CMakeLists.txt

@@ -20,7 +20,6 @@ set(SOURCES
     Font/OpenType/Hinting/Opcodes.cpp
     Font/OpenType/Tables.cpp
     Font/ScaledFont.cpp
-    Font/Typeface.cpp
     Font/VectorFont.cpp
     Font/WOFF/Font.cpp
     Font/WOFF2/Font.cpp

+ 19 - 29
Userland/Libraries/LibGfx/Font/FontDatabase.cpp

@@ -12,6 +12,7 @@
 #include <LibGfx/Font/Font.h>
 #include <LibGfx/Font/FontDatabase.h>
 #include <LibGfx/Font/OpenType/Font.h>
+#include <LibGfx/Font/ScaledFont.h>
 #include <LibGfx/Font/WOFF/Font.h>
 
 namespace Gfx {
@@ -23,8 +24,7 @@ FontDatabase& FontDatabase::the()
 }
 
 struct FontDatabase::Private {
-    HashMap<ByteString, NonnullRefPtr<Gfx::Font>, CaseInsensitiveStringTraits> full_name_to_font_map;
-    HashMap<FlyString, Vector<NonnullRefPtr<Typeface>>, AK::ASCIICaseInsensitiveFlyStringTraits> typefaces;
+    HashMap<FlyString, Vector<NonnullRefPtr<VectorFont>>, AK::ASCIICaseInsensitiveFlyStringTraits> typeface_by_family;
 };
 
 void FontDatabase::load_all_fonts_from_uri(StringView uri)
@@ -46,14 +46,18 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri)
             // FIXME: What about .otf
             if (auto font_or_error = OpenType::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
                 auto font = font_or_error.release_value();
-                auto typeface = get_or_create_typeface(font->family(), font->variant());
-                typeface->set_vector_font(move(font));
+                auto& family = m_private->typeface_by_family.ensure(font->family(), [] {
+                    return Vector<NonnullRefPtr<VectorFont>> {};
+                });
+                family.append(font);
             }
         } else if (path.has_extension(".woff"sv)) {
             if (auto font_or_error = WOFF::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
                 auto font = font_or_error.release_value();
-                auto typeface = get_or_create_typeface(font->family(), font->variant());
-                typeface->set_vector_font(move(font));
+                auto& family = m_private->typeface_by_family.ensure(font->family(), [] {
+                    return Vector<NonnullRefPtr<VectorFont>> {};
+                });
+                family.append(font);
             }
         }
         return IterationDecision::Continue;
@@ -68,46 +72,32 @@ FontDatabase::FontDatabase()
 
 RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope)
 {
-    auto it = m_private->typefaces.find(family);
-    if (it == m_private->typefaces.end())
+    auto it = m_private->typeface_by_family.find(family);
+    if (it == m_private->typeface_by_family.end())
         return nullptr;
     for (auto const& typeface : it->value) {
         if (typeface->weight() == weight && typeface->width() == width && typeface->slope() == slope)
-            return typeface->get_font(point_size);
+            return typeface->scaled_font(point_size);
     }
     return nullptr;
 }
 
 RefPtr<Gfx::Font> FontDatabase::get(FlyString const& family, FlyString const& variant, float point_size)
 {
-    auto it = m_private->typefaces.find(family);
-    if (it == m_private->typefaces.end())
+    auto it = m_private->typeface_by_family.find(family);
+    if (it == m_private->typeface_by_family.end())
         return nullptr;
     for (auto const& typeface : it->value) {
         if (typeface->variant() == variant)
-            return typeface->get_font(point_size);
+            return typeface->scaled_font(point_size);
     }
     return nullptr;
 }
 
-RefPtr<Typeface> FontDatabase::get_or_create_typeface(FlyString const& family, FlyString const& variant)
+void FontDatabase::for_each_typeface_with_family_name(FlyString const& family_name, Function<void(VectorFont const&)> callback)
 {
-    auto it = m_private->typefaces.find(family);
-    if (it != m_private->typefaces.end()) {
-        for (auto const& typeface : it->value) {
-            if (typeface->variant() == variant)
-                return typeface;
-        }
-    }
-    auto typeface = adopt_ref(*new Typeface(family, variant));
-    m_private->typefaces.ensure(family).append(typeface);
-    return typeface;
-}
-
-void FontDatabase::for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)> callback)
-{
-    auto it = m_private->typefaces.find(family_name);
-    if (it == m_private->typefaces.end())
+    auto it = m_private->typeface_by_family.find(family_name);
+    if (it == m_private->typeface_by_family.end())
         return;
     for (auto const& typeface : it->value)
         callback(*typeface);

+ 2 - 4
Userland/Libraries/LibGfx/Font/FontDatabase.h

@@ -12,7 +12,7 @@
 #include <AK/HashMap.h>
 #include <AK/OwnPtr.h>
 #include <LibGfx/Font/FontWeight.h>
-#include <LibGfx/Font/Typeface.h>
+#include <LibGfx/Font/VectorFont.h>
 #include <LibGfx/Forward.h>
 
 namespace Gfx {
@@ -24,7 +24,7 @@ public:
     RefPtr<Gfx::Font> get(FlyString const& family, float point_size, unsigned weight, unsigned width, unsigned slope);
     RefPtr<Gfx::Font> get(FlyString const& family, FlyString const& variant, float point_size);
 
-    void for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)>);
+    void for_each_typeface_with_family_name(FlyString const& family_name, Function<void(VectorFont const&)>);
 
     void load_all_fonts_from_uri(StringView);
 
@@ -32,8 +32,6 @@ private:
     FontDatabase();
     ~FontDatabase() = default;
 
-    RefPtr<Typeface> get_or_create_typeface(FlyString const& family, FlyString const& variant);
-
     struct Private;
     OwnPtr<Private> m_private;
 };

+ 0 - 43
Userland/Libraries/LibGfx/Font/Typeface.cpp

@@ -1,43 +0,0 @@
-/*
- * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGfx/Font/ScaledFont.h>
-#include <LibGfx/Font/Typeface.h>
-
-namespace Gfx {
-
-unsigned Typeface::weight() const
-{
-    return m_vector_font->weight();
-}
-
-unsigned Typeface::width() const
-{
-    return m_vector_font->width();
-}
-
-u8 Typeface::slope() const
-{
-    return m_vector_font->slope();
-}
-
-bool Typeface::is_fixed_width() const
-{
-    return m_vector_font->is_fixed_width();
-}
-
-void Typeface::set_vector_font(RefPtr<VectorFont> font)
-{
-    m_vector_font = move(font);
-}
-
-RefPtr<Font> Typeface::get_font(float point_size) const
-{
-    VERIFY(point_size >= 0);
-    return m_vector_font->scaled_font(point_size);
-}
-
-}

+ 0 - 46
Userland/Libraries/LibGfx/Font/Typeface.h

@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/DeprecatedFlyString.h>
-#include <AK/FlyString.h>
-#include <AK/Function.h>
-#include <AK/RefCounted.h>
-#include <AK/Vector.h>
-#include <LibGfx/Font/Font.h>
-#include <LibGfx/Font/VectorFont.h>
-
-namespace Gfx {
-
-class Typeface : public RefCounted<Typeface> {
-public:
-    Typeface(FlyString family, FlyString variant)
-        : m_family(move(family))
-        , m_variant(move(variant))
-    {
-    }
-
-    FlyString const& family() const { return m_family; }
-    FlyString const& variant() const { return m_variant; }
-    unsigned weight() const;
-    unsigned width() const;
-    u8 slope() const;
-
-    bool is_fixed_width() const;
-
-    void set_vector_font(RefPtr<VectorFont>);
-
-    RefPtr<Font> get_font(float point_size) const;
-
-private:
-    FlyString m_family;
-    FlyString m_variant;
-
-    RefPtr<VectorFont> m_vector_font;
-};
-
-}

+ 3 - 4
Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

@@ -197,7 +197,7 @@ ErrorOr<NonnullRefPtr<Gfx::VectorFont>> FontLoader::try_load_font()
 
 struct StyleComputer::MatchingFontCandidate {
     FontFaceKey key;
-    Variant<FontLoaderList*, Gfx::Typeface const*> loader_or_typeface;
+    Variant<FontLoaderList*, Gfx::VectorFont const*> loader_or_typeface;
 
     [[nodiscard]] RefPtr<Gfx::FontCascadeList const> font_with_point_size(float point_size) const
     {
@@ -210,8 +210,7 @@ struct StyleComputer::MatchingFontCandidate {
             return font_list;
         }
 
-        if (auto font = loader_or_typeface.get<Gfx::Typeface const*>()->get_font(point_size))
-            font_list->add(*font);
+        font_list->add(loader_or_typeface.get<Gfx::VectorFont const*>()->scaled_font(point_size));
         return font_list;
     }
 };
@@ -1854,7 +1853,7 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::font_matching_algorithm(FontFa
         if (font_key_and_loader.key.family_name.equals_ignoring_ascii_case(key.family_name))
             matching_family_fonts.empend(font_key_and_loader.key, const_cast<FontLoaderList*>(&font_key_and_loader.value));
     }
-    Gfx::FontDatabase::the().for_each_typeface_with_family_name(key.family_name, [&](Gfx::Typeface const& typeface) {
+    Gfx::FontDatabase::the().for_each_typeface_with_family_name(key.family_name, [&](Gfx::VectorFont const& typeface) {
         matching_family_fonts.empend(
             FontFaceKey {
                 .family_name = typeface.family(),