diff --git a/Userland/Applications/FontEditor/GlyphEditorWidget.cpp b/Userland/Applications/FontEditor/GlyphEditorWidget.cpp index 6f545c1ccd0..aeabe6a03df 100644 --- a/Userland/Applications/FontEditor/GlyphEditorWidget.cpp +++ b/Userland/Applications/FontEditor/GlyphEditorWidget.cpp @@ -67,7 +67,7 @@ void GlyphEditorWidget::paint_event(GUI::PaintEvent& event) for (int x = 1; x < font().max_glyph_width(); ++x) painter.draw_line({ x * m_scale, 0 }, { x * m_scale, font().glyph_height() * m_scale }, palette().threed_shadow2()); - auto bitmap = font().glyph_bitmap(m_glyph); + auto bitmap = font().glyph(m_glyph).glyph_bitmap(); for (int y = 0; y < font().glyph_height(); ++y) { for (int x = 0; x < font().max_glyph_width(); ++x) { @@ -101,7 +101,7 @@ void GlyphEditorWidget::draw_at_mouse(const GUI::MouseEvent& event) return; int x = (event.x() - 1) / m_scale; int y = (event.y() - 1) / m_scale; - auto bitmap = font().glyph_bitmap(m_glyph); + auto bitmap = font().glyph(m_glyph).glyph_bitmap(); if (x < 0 || x >= bitmap.width()) return; if (y < 0 || y >= bitmap.height()) diff --git a/Userland/Libraries/LibGfx/BitmapFont.cpp b/Userland/Libraries/LibGfx/BitmapFont.cpp index e5e24cbde5c..2ffd87cf6f0 100644 --- a/Userland/Libraries/LibGfx/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/BitmapFont.cpp @@ -221,9 +221,9 @@ bool BitmapFont::write_to_file(const StringView& path) return true; } -GlyphBitmap BitmapFont::glyph_bitmap(u32 code_point) const +Glyph BitmapFont::glyph(u32 code_point) const { - return GlyphBitmap(&m_rows[code_point * m_glyph_height], { glyph_width(code_point), m_glyph_height }); + return Glyph(GlyphBitmap(&m_rows[code_point * m_glyph_height], { glyph_width(code_point), m_glyph_height })); } int BitmapFont::glyph_or_emoji_width(u32 code_point) const diff --git a/Userland/Libraries/LibGfx/BitmapFont.h b/Userland/Libraries/LibGfx/BitmapFont.h index c3ca9f4a1dc..a1c0b4d852a 100644 --- a/Userland/Libraries/LibGfx/BitmapFont.h +++ b/Userland/Libraries/LibGfx/BitmapFont.h @@ -57,7 +57,7 @@ public: u16 weight() const { return m_weight; } void set_weight(u16 weight) { m_weight = weight; } - GlyphBitmap glyph_bitmap(u32 code_point) const; + Glyph glyph(u32 code_point) const; u8 glyph_width(size_t ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[ch]; } int glyph_or_emoji_width(u32 code_point) const; @@ -86,7 +86,7 @@ public: int width(const Utf8View&) const; int width(const Utf32View&) const; - const String& name() const { return m_name; } + String name() const { return m_name; } void set_name(String name) { m_name = move(name); } bool is_fixed_width() const { return m_fixed_width; } @@ -106,8 +106,9 @@ public: FontTypes type() { return m_type; } void set_type(FontTypes type); - const String& family() const { return m_family; } + String family() const { return m_family; } void set_family(String family) { m_family = move(family); } + String variant() const { return String::formatted("{}", weight()); } String qualified_name() const; diff --git a/Userland/Libraries/LibGfx/CMakeLists.txt b/Userland/Libraries/LibGfx/CMakeLists.txt index 59c9a7fd0f8..037f088968a 100644 --- a/Userland/Libraries/LibGfx/CMakeLists.txt +++ b/Userland/Libraries/LibGfx/CMakeLists.txt @@ -31,8 +31,9 @@ set(SOURCES StylePainter.cpp SystemTheme.cpp Triangle.cpp + Typeface.cpp WindowTheme.cpp ) serenity_lib(LibGfx gfx) -target_link_libraries(LibGfx LibM LibCore) +target_link_libraries(LibGfx LibM LibCore LibTTF) diff --git a/Userland/Libraries/LibGfx/Font.h b/Userland/Libraries/LibGfx/Font.h index 2e74798725e..678e039cee6 100644 --- a/Userland/Libraries/LibGfx/Font.h +++ b/Userland/Libraries/LibGfx/Font.h @@ -31,15 +31,21 @@ #include #include #include +#include #include namespace Gfx { // FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead? class GlyphBitmap { - friend class BitmapFont; - public: + GlyphBitmap() = default; + GlyphBitmap(const unsigned* rows, IntSize size) + : m_rows(rows) + , m_size(size) + { + } + const unsigned* rows() const { return m_rows; } unsigned row(unsigned index) const { return m_rows[index]; } @@ -58,14 +64,29 @@ public: int height() const { return m_size.height(); } private: - GlyphBitmap(const unsigned* rows, IntSize size) - : m_rows(rows) - , m_size(size) + const unsigned* m_rows { nullptr }; + IntSize m_size { 0, 0 }; +}; + +class Glyph { +public: + Glyph(const GlyphBitmap& glyph_bitmap) + : m_glyph_bitmap(glyph_bitmap) { } - const unsigned* m_rows { nullptr }; - IntSize m_size; + Glyph(RefPtr bitmap) + : m_bitmap(bitmap) + { + } + + bool is_glyph_bitmap() const { return !m_bitmap; } + GlyphBitmap glyph_bitmap() const { return m_glyph_bitmap; } + RefPtr bitmap() const { return m_bitmap; } + +private: + GlyphBitmap m_glyph_bitmap; + RefPtr m_bitmap; }; class Font : public RefCounted { @@ -78,7 +99,7 @@ public: virtual u8 presentation_size() const = 0; virtual u16 weight() const = 0; - virtual GlyphBitmap glyph_bitmap(u32 code_point) const = 0; + virtual Glyph glyph(u32 code_point) const = 0; virtual u8 glyph_width(size_t ch) const = 0; virtual int glyph_or_emoji_width(u32 code_point) const = 0; @@ -96,7 +117,7 @@ public: virtual int width(const Utf8View&) const = 0; virtual int width(const Utf32View&) const = 0; - virtual const String& name() const = 0; + virtual String name() const = 0; virtual bool is_fixed_width() const = 0; @@ -104,7 +125,8 @@ public: virtual int glyph_count() const = 0; - virtual const String& family() const = 0; + virtual String family() const = 0; + virtual String variant() const = 0; virtual String qualified_name() const = 0; diff --git a/Userland/Libraries/LibGfx/FontDatabase.cpp b/Userland/Libraries/LibGfx/FontDatabase.cpp index 4fe466ece9e..dc3fbd41e94 100644 --- a/Userland/Libraries/LibGfx/FontDatabase.cpp +++ b/Userland/Libraries/LibGfx/FontDatabase.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -86,6 +88,7 @@ Font& FontDatabase::default_bold_font() struct FontDatabase::Private { HashMap> full_name_to_font_map; + Vector> typefaces; }; FontDatabase::FontDatabase() @@ -98,12 +101,20 @@ FontDatabase::FontDatabase() } while (di.has_next()) { String name = di.next_path(); - if (!name.ends_with(".font")) - continue; auto path = String::format("/res/fonts/%s", name.characters()); - if (auto font = Gfx::Font::load_from_file(path)) { - m_private->full_name_to_font_map.set(font->qualified_name(), font); + if (name.ends_with(".font")) { + if (auto font = Gfx::Font::load_from_file(path)) { + m_private->full_name_to_font_map.set(font->qualified_name(), font); + auto typeface = get_or_create_typeface(font->family(), font->variant()); + typeface->add_bitmap_font(font); + } + } else if (name.ends_with(".ttf")) { + // FIXME: What about .otf and .woff + if (auto font = TTF::Font::load_from_file(path)) { + auto typeface = get_or_create_typeface(font->family(), font->variant()); + typeface->set_ttf_font(font); + } } } } @@ -156,4 +167,15 @@ RefPtr FontDatabase::get(const String& family, unsigned size, unsigne return nullptr; } +RefPtr FontDatabase::get_or_create_typeface(const String& family, const String& variant) +{ + for (auto typeface : m_private->typefaces) { + if (typeface->family() == family && typeface->variant() == variant) + return typeface; + } + auto typeface = adopt(*new Typeface(family, variant)); + m_private->typefaces.append(typeface); + return typeface; +} + } diff --git a/Userland/Libraries/LibGfx/FontDatabase.h b/Userland/Libraries/LibGfx/FontDatabase.h index e1c21a20a33..204ab8e18b1 100644 --- a/Userland/Libraries/LibGfx/FontDatabase.h +++ b/Userland/Libraries/LibGfx/FontDatabase.h @@ -30,6 +30,7 @@ #include #include #include +#include namespace Gfx { @@ -48,6 +49,8 @@ public: void for_each_font(Function); void for_each_fixed_width_font(Function); + RefPtr get_or_create_typeface(const String& family, const String& variant); + private: FontDatabase(); ~FontDatabase(); diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index ae1cfe847ab..bdf40d2fdac 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -900,7 +900,7 @@ FLATTEN void Painter::draw_glyph(const IntPoint& point, u32 code_point, Color co FLATTEN void Painter::draw_glyph(const IntPoint& point, u32 code_point, const Font& font, Color color) { - draw_bitmap(point, font.glyph_bitmap(code_point), color); + draw_bitmap(point, font.glyph(code_point).glyph_bitmap(), color); } void Painter::draw_emoji(const IntPoint& point, const Gfx::Bitmap& emoji, const Font& font) diff --git a/Userland/Libraries/LibGfx/Typeface.cpp b/Userland/Libraries/LibGfx/Typeface.cpp new file mode 100644 index 00000000000..478ff6e5e1f --- /dev/null +++ b/Userland/Libraries/LibGfx/Typeface.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020, Stephan Unverwerth + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +namespace Gfx { + +void Typeface::add_bitmap_font(RefPtr font) +{ + m_bitmap_fonts.append(font); +} + +void Typeface::set_ttf_font(RefPtr font) +{ + m_ttf_font = font; +} + +RefPtr Typeface::get_font(unsigned size) +{ + for (auto font : m_bitmap_fonts) { + if (font->presentation_size() == size) + return font; + } + + if (m_ttf_font) { + auto font = adopt(*new TTF::ScaledFont(m_ttf_font, size, size)); + return font; + } + + return {}; +} + +} diff --git a/Userland/Libraries/LibGfx/Typeface.h b/Userland/Libraries/LibGfx/Typeface.h new file mode 100644 index 00000000000..5aa8218c1d4 --- /dev/null +++ b/Userland/Libraries/LibGfx/Typeface.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2020, Stephan Unverwerth + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Gfx { + +class Typeface : public RefCounted { +public: + Typeface(const String& family, const String& variant) + : m_family(family) + , m_variant(variant) + {} + + String family() const { return m_family; } + String variant() const { return m_variant; } + unsigned weight() const { return 100; /*TODO*/ } + + void add_bitmap_font(RefPtr); + void set_ttf_font(RefPtr); + + RefPtr get_font(unsigned size); + +private: + String m_family; + String m_variant; + + Vector> m_bitmap_fonts; + RefPtr m_ttf_font; +}; + +} diff --git a/Userland/Libraries/LibTTF/CMakeLists.txt b/Userland/Libraries/LibTTF/CMakeLists.txt index 96918db743b..f4233e2760d 100644 --- a/Userland/Libraries/LibTTF/CMakeLists.txt +++ b/Userland/Libraries/LibTTF/CMakeLists.txt @@ -5,4 +5,4 @@ set(SOURCES ) serenity_lib(LibTTF ttf) -target_link_libraries(LibTTF LibGfx LibM LibCore) +target_link_libraries(LibGfx LibM LibCore) diff --git a/Userland/Libraries/LibTTF/Font.cpp b/Userland/Libraries/LibTTF/Font.cpp index 083d9db405d..5601582c4a1 100644 --- a/Userland/Libraries/LibTTF/Font.cpp +++ b/Userland/Libraries/LibTTF/Font.cpp @@ -473,7 +473,7 @@ String Font::family() const return m_name.family_name(); } -String Font::subfamily() const +String Font::variant() const { auto string = m_name.typographic_subfamily_name(); if (!string.is_empty()) @@ -520,4 +520,30 @@ RefPtr ScaledFont::raster_glyph(u32 glyph_id) const return glyph_bitmap; } +Gfx::Glyph ScaledFont::glyph(u32 code_point) const +{ + auto id = glyph_id_for_codepoint(code_point); + auto bitmap = raster_glyph(id); + return Gfx::Glyph(bitmap); +} + +u8 ScaledFont::glyph_width(size_t code_point) const +{ + auto id = glyph_id_for_codepoint(code_point); + auto metrics = glyph_metrics(id); + return metrics.advance_width; +} + +int ScaledFont::glyph_or_emoji_width(u32 code_point) const +{ + auto id = glyph_id_for_codepoint(code_point); + auto metrics = glyph_metrics(id); + return metrics.advance_width; +} + +u8 ScaledFont::glyph_fixed_width() const +{ + return (u8)m_x_scale; +} + } diff --git a/Userland/Libraries/LibTTF/Font.h b/Userland/Libraries/LibTTF/Font.h index 939f1944e4c..44ffa314597 100644 --- a/Userland/Libraries/LibTTF/Font.h +++ b/Userland/Libraries/LibTTF/Font.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -75,7 +76,7 @@ public: u16 units_per_em() const; u32 glyph_id_for_codepoint(u32 codepoint) const { return m_cmap.glyph_id_for_codepoint(codepoint); } String family() const; - String subfamily() const; + String variant() const; private: enum class Offsets { @@ -116,7 +117,7 @@ private: Cmap m_cmap; }; -class ScaledFont { +class ScaledFont : public Gfx::Font { public: ScaledFont(RefPtr font, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI) : m_font(font) @@ -129,13 +130,35 @@ public: ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); } ScaledGlyphMetrics glyph_metrics(u32 glyph_id) const { return m_font->glyph_metrics(glyph_id, m_x_scale, m_y_scale); } RefPtr raster_glyph(u32 glyph_id) const; - u32 glyph_count() const { return m_font->glyph_count(); } - int width(const StringView&) const; - int width(const Utf8View&) const; - int width(const Utf32View&) const; + + // Gfx::Font implementation + virtual NonnullRefPtr clone() const override { return *this; } /* TODO */ + virtual u8 presentation_size() const override { return (u8)m_y_scale; } + virtual u16 weight() const override { return 400; } /* TODO */ + virtual Gfx::Glyph glyph(u32 code_point) const override; + virtual u8 glyph_width(size_t ch) const override; + virtual int glyph_or_emoji_width(u32 code_point) const override; + virtual u8 glyph_height() const override { return m_y_scale; } /* TODO */ + virtual int x_height() const override { return m_y_scale; } /* TODO */ + virtual u8 min_glyph_width() const override { return 1; } /* TODO */ + virtual u8 max_glyph_width() const override { return m_x_scale; } /* TODO */ + virtual u8 glyph_fixed_width() const override; + virtual u8 baseline() const override { return m_y_scale; } /* TODO */ + virtual u8 mean_line() const override { return m_y_scale; } /* TODO */ + virtual int width(const StringView&) const override; + virtual int width(const Utf8View&) const override; + virtual int width(const Utf32View&) const override; + virtual String name() const override { return String::formatted("{} {}", family(), variant()); } + virtual bool is_fixed_width() const override { return false; } /* TODO */ + virtual u8 glyph_spacing() const override { return m_x_scale; } /* TODO */ + virtual int glyph_count() const override { return m_font->glyph_count(); } + virtual String family() const override { return m_font->family(); } + virtual String variant() const override { return m_font->variant(); } + virtual String qualified_name() const override { return String::formatted("{} {} {}", family(), presentation_size(), weight()); } + virtual const Font& bold_variant() const override { return *this; } /* TODO */ private: - RefPtr m_font; + RefPtr m_font; float m_x_scale { 0.0 }; float m_y_scale { 0.0 }; mutable AK::HashMap> m_cached_glyph_bitmaps;