mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:52:45 +00:00 
			
		
		
		
	LibGfx: Insert pixel and ttf fonts into Typeface structure
This adds a new structure 'Typeface' to the FontDatabase that represents all fonts of the same family and variant. It can contain a list of BitmapFonts with varying size but of the same family and weight or a pointer to a single TTF font for all sizes of this Typeface.
This commit is contained in:
		
							parent
							
								
									2c4e13f14a
								
							
						
					
					
						commit
						179dba652e
					
				
					 13 changed files with 248 additions and 32 deletions
				
			
		|  | @ -67,7 +67,7 @@ void GlyphEditorWidget::paint_event(GUI::PaintEvent& event) | ||||||
|     for (int x = 1; x < font().max_glyph_width(); ++x) |     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()); |         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 y = 0; y < font().glyph_height(); ++y) { | ||||||
|         for (int x = 0; x < font().max_glyph_width(); ++x) { |         for (int x = 0; x < font().max_glyph_width(); ++x) { | ||||||
|  | @ -101,7 +101,7 @@ void GlyphEditorWidget::draw_at_mouse(const GUI::MouseEvent& event) | ||||||
|         return; |         return; | ||||||
|     int x = (event.x() - 1) / m_scale; |     int x = (event.x() - 1) / m_scale; | ||||||
|     int y = (event.y() - 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()) |     if (x < 0 || x >= bitmap.width()) | ||||||
|         return; |         return; | ||||||
|     if (y < 0 || y >= bitmap.height()) |     if (y < 0 || y >= bitmap.height()) | ||||||
|  |  | ||||||
|  | @ -221,9 +221,9 @@ bool BitmapFont::write_to_file(const StringView& path) | ||||||
|     return true; |     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 | int BitmapFont::glyph_or_emoji_width(u32 code_point) const | ||||||
|  |  | ||||||
|  | @ -57,7 +57,7 @@ public: | ||||||
|     u16 weight() const { return m_weight; } |     u16 weight() const { return m_weight; } | ||||||
|     void set_weight(u16 weight) { m_weight = 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]; } |     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; |     int glyph_or_emoji_width(u32 code_point) const; | ||||||
|  | @ -86,7 +86,7 @@ public: | ||||||
|     int width(const Utf8View&) const; |     int width(const Utf8View&) const; | ||||||
|     int width(const Utf32View&) 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); } |     void set_name(String name) { m_name = move(name); } | ||||||
| 
 | 
 | ||||||
|     bool is_fixed_width() const { return m_fixed_width; } |     bool is_fixed_width() const { return m_fixed_width; } | ||||||
|  | @ -106,8 +106,9 @@ public: | ||||||
|     FontTypes type() { return m_type; } |     FontTypes type() { return m_type; } | ||||||
|     void set_type(FontTypes 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); } |     void set_family(String family) { m_family = move(family); } | ||||||
|  |     String variant() const { return String::formatted("{}", weight()); } | ||||||
| 
 | 
 | ||||||
|     String qualified_name() const; |     String qualified_name() const; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -31,8 +31,9 @@ set(SOURCES | ||||||
|     StylePainter.cpp |     StylePainter.cpp | ||||||
|     SystemTheme.cpp |     SystemTheme.cpp | ||||||
|     Triangle.cpp |     Triangle.cpp | ||||||
|  |     Typeface.cpp | ||||||
|     WindowTheme.cpp |     WindowTheme.cpp | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| serenity_lib(LibGfx gfx) | serenity_lib(LibGfx gfx) | ||||||
| target_link_libraries(LibGfx LibM LibCore) | target_link_libraries(LibGfx LibM LibCore LibTTF) | ||||||
|  |  | ||||||
|  | @ -31,15 +31,21 @@ | ||||||
| #include <AK/RefPtr.h> | #include <AK/RefPtr.h> | ||||||
| #include <AK/String.h> | #include <AK/String.h> | ||||||
| #include <AK/Types.h> | #include <AK/Types.h> | ||||||
|  | #include <LibGfx/Bitmap.h> | ||||||
| #include <LibGfx/Size.h> | #include <LibGfx/Size.h> | ||||||
| 
 | 
 | ||||||
| namespace Gfx { | namespace Gfx { | ||||||
| 
 | 
 | ||||||
| // FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
 | // FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
 | ||||||
| class GlyphBitmap { | class GlyphBitmap { | ||||||
|     friend class BitmapFont; |  | ||||||
| 
 |  | ||||||
| public: | public: | ||||||
|  |     GlyphBitmap() = default; | ||||||
|  |     GlyphBitmap(const unsigned* rows, IntSize size) | ||||||
|  |         : m_rows(rows) | ||||||
|  |         , m_size(size) | ||||||
|  |     { | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     const unsigned* rows() const { return m_rows; } |     const unsigned* rows() const { return m_rows; } | ||||||
|     unsigned row(unsigned index) const { return m_rows[index]; } |     unsigned row(unsigned index) const { return m_rows[index]; } | ||||||
| 
 | 
 | ||||||
|  | @ -58,14 +64,29 @@ public: | ||||||
|     int height() const { return m_size.height(); } |     int height() const { return m_size.height(); } | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     GlyphBitmap(const unsigned* rows, IntSize size) |     const unsigned* m_rows { nullptr }; | ||||||
|         : m_rows(rows) |     IntSize m_size { 0, 0 }; | ||||||
|         , m_size(size) | }; | ||||||
|  | 
 | ||||||
|  | class Glyph { | ||||||
|  | public: | ||||||
|  |     Glyph(const GlyphBitmap& glyph_bitmap) | ||||||
|  |         : m_glyph_bitmap(glyph_bitmap) | ||||||
|     { |     { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const unsigned* m_rows { nullptr }; |     Glyph(RefPtr<Bitmap> bitmap) | ||||||
|     IntSize m_size; |         : m_bitmap(bitmap) | ||||||
|  |     { | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     bool is_glyph_bitmap() const { return !m_bitmap; } | ||||||
|  |     GlyphBitmap glyph_bitmap() const { return m_glyph_bitmap; } | ||||||
|  |     RefPtr<Bitmap> bitmap() const { return m_bitmap; } | ||||||
|  | 
 | ||||||
|  | private: | ||||||
|  |     GlyphBitmap m_glyph_bitmap; | ||||||
|  |     RefPtr<Bitmap> m_bitmap; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| class Font : public RefCounted<Font> { | class Font : public RefCounted<Font> { | ||||||
|  | @ -78,7 +99,7 @@ public: | ||||||
|     virtual u8 presentation_size() const = 0; |     virtual u8 presentation_size() const = 0; | ||||||
| 
 | 
 | ||||||
|     virtual u16 weight() 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 u8 glyph_width(size_t ch) const = 0; | ||||||
|     virtual int glyph_or_emoji_width(u32 code_point) 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 Utf8View&) const = 0; | ||||||
|     virtual int width(const Utf32View&) 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; |     virtual bool is_fixed_width() const = 0; | ||||||
| 
 | 
 | ||||||
|  | @ -104,7 +125,8 @@ public: | ||||||
| 
 | 
 | ||||||
|     virtual int glyph_count() const = 0; |     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; |     virtual String qualified_name() const = 0; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -29,6 +29,8 @@ | ||||||
| #include <LibCore/DirIterator.h> | #include <LibCore/DirIterator.h> | ||||||
| #include <LibGfx/Font.h> | #include <LibGfx/Font.h> | ||||||
| #include <LibGfx/FontDatabase.h> | #include <LibGfx/FontDatabase.h> | ||||||
|  | #include <LibGfx/Typeface.h> | ||||||
|  | #include <LibTTF/Font.h> | ||||||
| #include <dirent.h> | #include <dirent.h> | ||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
|  | @ -86,6 +88,7 @@ Font& FontDatabase::default_bold_font() | ||||||
| 
 | 
 | ||||||
| struct FontDatabase::Private { | struct FontDatabase::Private { | ||||||
|     HashMap<String, RefPtr<Gfx::Font>> full_name_to_font_map; |     HashMap<String, RefPtr<Gfx::Font>> full_name_to_font_map; | ||||||
|  |     Vector<RefPtr<Typeface>> typefaces; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| FontDatabase::FontDatabase() | FontDatabase::FontDatabase() | ||||||
|  | @ -98,12 +101,20 @@ FontDatabase::FontDatabase() | ||||||
|     } |     } | ||||||
|     while (di.has_next()) { |     while (di.has_next()) { | ||||||
|         String name = di.next_path(); |         String name = di.next_path(); | ||||||
|         if (!name.ends_with(".font")) |  | ||||||
|             continue; |  | ||||||
| 
 | 
 | ||||||
|         auto path = String::format("/res/fonts/%s", name.characters()); |         auto path = String::format("/res/fonts/%s", name.characters()); | ||||||
|         if (auto font = Gfx::Font::load_from_file(path)) { |         if (name.ends_with(".font")) { | ||||||
|             m_private->full_name_to_font_map.set(font->qualified_name(), 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<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigne | ||||||
|     return nullptr; |     return nullptr; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | RefPtr<Typeface> 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; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -30,6 +30,7 @@ | ||||||
| #include <AK/HashMap.h> | #include <AK/HashMap.h> | ||||||
| #include <AK/String.h> | #include <AK/String.h> | ||||||
| #include <LibGfx/Forward.h> | #include <LibGfx/Forward.h> | ||||||
|  | #include <LibGfx/Typeface.h> | ||||||
| 
 | 
 | ||||||
| namespace Gfx { | namespace Gfx { | ||||||
| 
 | 
 | ||||||
|  | @ -48,6 +49,8 @@ public: | ||||||
|     void for_each_font(Function<void(const Gfx::Font&)>); |     void for_each_font(Function<void(const Gfx::Font&)>); | ||||||
|     void for_each_fixed_width_font(Function<void(const Gfx::Font&)>); |     void for_each_fixed_width_font(Function<void(const Gfx::Font&)>); | ||||||
| 
 | 
 | ||||||
|  |     RefPtr<Typeface> get_or_create_typeface(const String& family, const String& variant); | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     FontDatabase(); |     FontDatabase(); | ||||||
|     ~FontDatabase(); |     ~FontDatabase(); | ||||||
|  |  | ||||||
|  | @ -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) | 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) | void Painter::draw_emoji(const IntPoint& point, const Gfx::Bitmap& emoji, const Font& font) | ||||||
|  |  | ||||||
							
								
								
									
										56
									
								
								Userland/Libraries/LibGfx/Typeface.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								Userland/Libraries/LibGfx/Typeface.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,56 @@ | ||||||
|  | /*
 | ||||||
|  |  * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de> | ||||||
|  |  * 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 <LibGfx/Typeface.h> | ||||||
|  | 
 | ||||||
|  | namespace Gfx { | ||||||
|  | 
 | ||||||
|  | void Typeface::add_bitmap_font(RefPtr<BitmapFont> font) | ||||||
|  | { | ||||||
|  |     m_bitmap_fonts.append(font); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void Typeface::set_ttf_font(RefPtr<TTF::Font> font) | ||||||
|  | { | ||||||
|  |     m_ttf_font = font; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | RefPtr<Font> 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 {}; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | } | ||||||
							
								
								
									
										62
									
								
								Userland/Libraries/LibGfx/Typeface.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								Userland/Libraries/LibGfx/Typeface.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,62 @@ | ||||||
|  | /*
 | ||||||
|  |  * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de> | ||||||
|  |  * 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 <AK/RefCounted.h> | ||||||
|  | #include <AK/String.h> | ||||||
|  | #include <AK/Vector.h> | ||||||
|  | #include <LibGfx/BitmapFont.h> | ||||||
|  | #include <LibGfx/Font.h> | ||||||
|  | #include <LibTTF/Font.h> | ||||||
|  | 
 | ||||||
|  | namespace Gfx { | ||||||
|  | 
 | ||||||
|  | class Typeface : public RefCounted<Typeface> { | ||||||
|  | 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<BitmapFont>); | ||||||
|  |     void set_ttf_font(RefPtr<TTF::Font>); | ||||||
|  | 
 | ||||||
|  |     RefPtr<Font> get_font(unsigned size); | ||||||
|  | 
 | ||||||
|  | private: | ||||||
|  |     String m_family; | ||||||
|  |     String m_variant; | ||||||
|  | 
 | ||||||
|  |     Vector<RefPtr<BitmapFont>> m_bitmap_fonts; | ||||||
|  |     RefPtr<TTF::Font> m_ttf_font; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -5,4 +5,4 @@ set(SOURCES | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| serenity_lib(LibTTF ttf) | serenity_lib(LibTTF ttf) | ||||||
| target_link_libraries(LibTTF LibGfx LibM LibCore) | target_link_libraries(LibGfx LibM LibCore) | ||||||
|  |  | ||||||
|  | @ -473,7 +473,7 @@ String Font::family() const | ||||||
|     return m_name.family_name(); |     return m_name.family_name(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| String Font::subfamily() const | String Font::variant() const | ||||||
| { | { | ||||||
|     auto string = m_name.typographic_subfamily_name(); |     auto string = m_name.typographic_subfamily_name(); | ||||||
|     if (!string.is_empty()) |     if (!string.is_empty()) | ||||||
|  | @ -520,4 +520,30 @@ RefPtr<Gfx::Bitmap> ScaledFont::raster_glyph(u32 glyph_id) const | ||||||
|     return glyph_bitmap; |     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; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -32,6 +32,7 @@ | ||||||
| #include <AK/RefCounted.h> | #include <AK/RefCounted.h> | ||||||
| #include <AK/StringView.h> | #include <AK/StringView.h> | ||||||
| #include <LibGfx/Bitmap.h> | #include <LibGfx/Bitmap.h> | ||||||
|  | #include <LibGfx/Font.h> | ||||||
| #include <LibGfx/Size.h> | #include <LibGfx/Size.h> | ||||||
| #include <LibTTF/Cmap.h> | #include <LibTTF/Cmap.h> | ||||||
| #include <LibTTF/Glyf.h> | #include <LibTTF/Glyf.h> | ||||||
|  | @ -75,7 +76,7 @@ public: | ||||||
|     u16 units_per_em() const; |     u16 units_per_em() const; | ||||||
|     u32 glyph_id_for_codepoint(u32 codepoint) const { return m_cmap.glyph_id_for_codepoint(codepoint); } |     u32 glyph_id_for_codepoint(u32 codepoint) const { return m_cmap.glyph_id_for_codepoint(codepoint); } | ||||||
|     String family() const; |     String family() const; | ||||||
|     String subfamily() const; |     String variant() const; | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     enum class Offsets { |     enum class Offsets { | ||||||
|  | @ -116,7 +117,7 @@ private: | ||||||
|     Cmap m_cmap; |     Cmap m_cmap; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| class ScaledFont { | class ScaledFont : public Gfx::Font { | ||||||
| public: | public: | ||||||
|     ScaledFont(RefPtr<Font> font, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI) |     ScaledFont(RefPtr<Font> font, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI) | ||||||
|         : m_font(font) |         : m_font(font) | ||||||
|  | @ -129,13 +130,35 @@ public: | ||||||
|     ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); } |     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); } |     ScaledGlyphMetrics glyph_metrics(u32 glyph_id) const { return m_font->glyph_metrics(glyph_id, m_x_scale, m_y_scale); } | ||||||
|     RefPtr<Gfx::Bitmap> raster_glyph(u32 glyph_id) const; |     RefPtr<Gfx::Bitmap> raster_glyph(u32 glyph_id) const; | ||||||
|     u32 glyph_count() const { return m_font->glyph_count(); } | 
 | ||||||
|     int width(const StringView&) const; |     // Gfx::Font implementation
 | ||||||
|     int width(const Utf8View&) const; |     virtual NonnullRefPtr<Font> clone() const override { return *this; } /* TODO */ | ||||||
|     int width(const Utf32View&) const; |     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: | private: | ||||||
|     RefPtr<Font> m_font; |     RefPtr<TTF::Font> m_font; | ||||||
|     float m_x_scale { 0.0 }; |     float m_x_scale { 0.0 }; | ||||||
|     float m_y_scale { 0.0 }; |     float m_y_scale { 0.0 }; | ||||||
|     mutable AK::HashMap<u32, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps; |     mutable AK::HashMap<u32, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Stephan Unverwerth
						Stephan Unverwerth