Font.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Bitmap.h>
  9. #include <AK/ByteReader.h>
  10. #include <AK/DeprecatedString.h>
  11. #include <AK/RefCounted.h>
  12. #include <AK/RefPtr.h>
  13. #include <AK/Types.h>
  14. #include <LibCore/MappedFile.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/Size.h>
  17. namespace Gfx {
  18. // FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
  19. class GlyphBitmap {
  20. public:
  21. GlyphBitmap() = default;
  22. GlyphBitmap(u8 const* rows, size_t start_index, IntSize size)
  23. : m_rows(rows)
  24. , m_start_index(start_index)
  25. , m_size(size)
  26. {
  27. }
  28. unsigned row(unsigned index) const { return ByteReader::load32(bitmap(index).data()); }
  29. bool bit_at(int x, int y) const { return bitmap(y).get(x); }
  30. void set_bit_at(int x, int y, bool b) { bitmap(y).set(x, b); }
  31. IntSize size() const { return m_size; }
  32. int width() const { return m_size.width(); }
  33. int height() const { return m_size.height(); }
  34. static constexpr size_t bytes_per_row() { return sizeof(u32); }
  35. static constexpr int max_width() { return bytes_per_row() * 8; }
  36. static constexpr int max_height() { return max_width() + bytes_per_row(); }
  37. private:
  38. AK::Bitmap bitmap(size_t y) const
  39. {
  40. return { const_cast<u8*>(m_rows) + bytes_per_row() * (m_start_index + y), bytes_per_row() * 8 };
  41. }
  42. u8 const* m_rows { nullptr };
  43. size_t m_start_index { 0 };
  44. IntSize m_size { 0, 0 };
  45. };
  46. class Glyph {
  47. public:
  48. Glyph(GlyphBitmap const& glyph_bitmap, float left_bearing, float advance, float ascent)
  49. : m_glyph_bitmap(glyph_bitmap)
  50. , m_left_bearing(left_bearing)
  51. , m_advance(advance)
  52. , m_ascent(ascent)
  53. {
  54. }
  55. Glyph(RefPtr<Bitmap> bitmap, float left_bearing, float advance, float ascent, bool is_color_bitmap)
  56. : m_bitmap(bitmap)
  57. , m_left_bearing(left_bearing)
  58. , m_advance(advance)
  59. , m_ascent(ascent)
  60. , m_color_bitmap(is_color_bitmap)
  61. {
  62. }
  63. bool is_color_bitmap() const { return m_color_bitmap; }
  64. bool is_glyph_bitmap() const { return !m_bitmap; }
  65. GlyphBitmap glyph_bitmap() const { return m_glyph_bitmap; }
  66. RefPtr<Bitmap> bitmap() const { return m_bitmap; }
  67. float left_bearing() const { return m_left_bearing; }
  68. float advance() const { return m_advance; }
  69. float ascent() const { return m_ascent; }
  70. private:
  71. GlyphBitmap m_glyph_bitmap;
  72. RefPtr<Bitmap> m_bitmap;
  73. float m_left_bearing;
  74. float m_advance;
  75. float m_ascent;
  76. bool m_color_bitmap { false };
  77. };
  78. struct GlyphSubpixelOffset {
  79. u8 x;
  80. u8 y;
  81. // TODO: Allow setting this at runtime via some config?
  82. static constexpr int subpixel_divisions() { return 3; }
  83. FloatPoint to_float_point() const { return FloatPoint(x / float(subpixel_divisions()), y / float(subpixel_divisions())); }
  84. bool operator==(GlyphSubpixelOffset const&) const = default;
  85. };
  86. struct GlyphRasterPosition {
  87. // Where the glyph bitmap should be drawn/blitted.
  88. IntPoint blit_position;
  89. // A subpixel offset to be used when rendering the glyph.
  90. // This improves kerning and alignment at the expense of caching a few extra bitmaps.
  91. // This is (currently) snapped to thirds of a subpixel (i.e. 0, 0.33, 0.66).
  92. GlyphSubpixelOffset subpixel_offset;
  93. static GlyphRasterPosition get_nearest_fit_for(FloatPoint position);
  94. };
  95. struct FontPixelMetrics {
  96. float size { 0 };
  97. float x_height { 0 };
  98. float advance_of_ascii_zero { 0 };
  99. float glyph_spacing { 0 };
  100. // Number of pixels the font extends above the baseline.
  101. float ascent { 0 };
  102. // Number of pixels the font descends below the baseline.
  103. float descent { 0 };
  104. // Line gap specified by font.
  105. float line_gap { 0 };
  106. float line_spacing() const { return ascent + descent + line_gap; }
  107. };
  108. // https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
  109. enum FontWidth {
  110. UltraCondensed = 1,
  111. ExtraCondensed = 2,
  112. Condensed = 3,
  113. SemiCondensed = 4,
  114. Normal = 5,
  115. SemiExpanded = 6,
  116. Expanded = 7,
  117. ExtraExpanded = 8,
  118. UltraExpanded = 9
  119. };
  120. class Font : public RefCounted<Font> {
  121. public:
  122. enum class AllowInexactSizeMatch {
  123. No,
  124. Yes,
  125. };
  126. virtual NonnullRefPtr<Font> clone() const = 0;
  127. virtual ErrorOr<NonnullRefPtr<Font>> try_clone() const = 0;
  128. virtual ~Font() {};
  129. virtual FontPixelMetrics pixel_metrics() const = 0;
  130. virtual u8 presentation_size() const = 0;
  131. virtual u8 slope() const = 0;
  132. // Font point size (distance between ascender and descender).
  133. virtual float point_size() const = 0;
  134. // Font pixel size (distance between ascender and descender).
  135. virtual float pixel_size() const = 0;
  136. // Font pixel size, rounded up to the nearest integer.
  137. virtual int pixel_size_rounded_up() const = 0;
  138. virtual u16 width() const = 0;
  139. virtual u16 weight() const = 0;
  140. virtual Glyph glyph(u32 code_point) const = 0;
  141. virtual Glyph glyph(u32 code_point, GlyphSubpixelOffset) const = 0;
  142. virtual bool contains_glyph(u32 code_point) const = 0;
  143. virtual float glyph_left_bearing(u32 code_point) const = 0;
  144. virtual float glyph_width(u32 code_point) const = 0;
  145. virtual float glyph_or_emoji_width(Utf8CodePointIterator&) const = 0;
  146. virtual float glyph_or_emoji_width(Utf32CodePointIterator&) const = 0;
  147. virtual float glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const = 0;
  148. virtual int x_height() const = 0;
  149. virtual float preferred_line_height() const = 0;
  150. virtual u8 min_glyph_width() const = 0;
  151. virtual u8 max_glyph_width() const = 0;
  152. virtual u8 glyph_fixed_width() const = 0;
  153. virtual u8 baseline() const = 0;
  154. virtual u8 mean_line() const = 0;
  155. virtual float width(StringView) const = 0;
  156. virtual float width(Utf8View const&) const = 0;
  157. virtual float width(Utf32View const&) const = 0;
  158. virtual int width_rounded_up(StringView) const = 0;
  159. virtual DeprecatedString name() const = 0;
  160. virtual bool is_fixed_width() const = 0;
  161. virtual u8 glyph_spacing() const = 0;
  162. virtual size_t glyph_count() const = 0;
  163. virtual DeprecatedString family() const = 0;
  164. virtual DeprecatedString variant() const = 0;
  165. virtual DeprecatedString qualified_name() const = 0;
  166. virtual DeprecatedString human_readable_name() const = 0;
  167. virtual RefPtr<Font> with_size(float point_size) const = 0;
  168. Font const& bold_variant() const;
  169. virtual bool has_color_bitmaps() const = 0;
  170. private:
  171. mutable RefPtr<Gfx::Font const> m_bold_variant;
  172. };
  173. }