Font.h 6.5 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/RefCounted.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/String.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(Bytes rows, IntSize size)
  23. : m_rows(rows)
  24. , m_size(size)
  25. {
  26. }
  27. unsigned row(unsigned index) const { return ByteReader::load32(bitmap(index).data()); }
  28. bool bit_at(int x, int y) const { return bitmap(y).get(x); }
  29. void set_bit_at(int x, int y, bool b) { bitmap(y).set(x, b); }
  30. IntSize size() const { return m_size; }
  31. int width() const { return m_size.width(); }
  32. int height() const { return m_size.height(); }
  33. static constexpr size_t bytes_per_row() { return sizeof(u32); }
  34. static constexpr int max_width() { return bytes_per_row() * 8; }
  35. static constexpr int max_height() { return max_width() + bytes_per_row(); }
  36. private:
  37. AK::Bitmap bitmap(size_t y) const
  38. {
  39. return { const_cast<u8*>(m_rows.offset_pointer(bytes_per_row() * y)), bytes_per_row() * 8 };
  40. }
  41. Bytes m_rows;
  42. IntSize m_size { 0, 0 };
  43. };
  44. class Glyph {
  45. public:
  46. Glyph(GlyphBitmap const& glyph_bitmap, float left_bearing, float advance, float ascent)
  47. : m_glyph_bitmap(glyph_bitmap)
  48. , m_left_bearing(left_bearing)
  49. , m_advance(advance)
  50. , m_ascent(ascent)
  51. {
  52. }
  53. Glyph(RefPtr<Bitmap> bitmap, float left_bearing, float advance, float ascent, bool is_color_bitmap)
  54. : m_bitmap(bitmap)
  55. , m_left_bearing(left_bearing)
  56. , m_advance(advance)
  57. , m_ascent(ascent)
  58. , m_color_bitmap(is_color_bitmap)
  59. {
  60. }
  61. bool is_color_bitmap() const { return m_color_bitmap; }
  62. bool is_glyph_bitmap() const { return !m_bitmap; }
  63. GlyphBitmap glyph_bitmap() const { return m_glyph_bitmap; }
  64. RefPtr<Bitmap> bitmap() const { return m_bitmap; }
  65. float left_bearing() const { return m_left_bearing; }
  66. float advance() const { return m_advance; }
  67. float ascent() const { return m_ascent; }
  68. private:
  69. GlyphBitmap m_glyph_bitmap;
  70. RefPtr<Bitmap> m_bitmap;
  71. float m_left_bearing;
  72. float m_advance;
  73. float m_ascent;
  74. bool m_color_bitmap { false };
  75. };
  76. struct GlyphSubpixelOffset {
  77. u8 x;
  78. u8 y;
  79. // TODO: Allow setting this at runtime via some config?
  80. static constexpr int subpixel_divisions() { return 3; }
  81. FloatPoint to_float_point() const { return FloatPoint(x / float(subpixel_divisions()), y / float(subpixel_divisions())); }
  82. bool operator==(GlyphSubpixelOffset const&) const = default;
  83. };
  84. struct GlyphRasterPosition {
  85. // Where the glyph bitmap should be drawn/blitted.
  86. IntPoint blit_position;
  87. // A subpixel offset to be used when rendering the glyph.
  88. // This improves kerning and alignment at the expense of caching a few extra bitmaps.
  89. // This is (currently) snapped to thirds of a subpixel (i.e. 0, 0.33, 0.66).
  90. GlyphSubpixelOffset subpixel_offset;
  91. static GlyphRasterPosition get_nearest_fit_for(FloatPoint position);
  92. };
  93. struct FontPixelMetrics {
  94. float size { 0 };
  95. float x_height { 0 };
  96. float advance_of_ascii_zero { 0 };
  97. float glyph_spacing { 0 };
  98. // Number of pixels the font extends above the baseline.
  99. float ascent { 0 };
  100. // Number of pixels the font descends below the baseline.
  101. float descent { 0 };
  102. // Line gap specified by font.
  103. float line_gap { 0 };
  104. float line_spacing() const { return ascent + descent + line_gap; }
  105. };
  106. // https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
  107. enum FontWidth {
  108. UltraCondensed = 1,
  109. ExtraCondensed = 2,
  110. Condensed = 3,
  111. SemiCondensed = 4,
  112. Normal = 5,
  113. SemiExpanded = 6,
  114. Expanded = 7,
  115. ExtraExpanded = 8,
  116. UltraExpanded = 9
  117. };
  118. class Font : public RefCounted<Font> {
  119. public:
  120. enum class AllowInexactSizeMatch {
  121. No,
  122. Yes,
  123. Larger,
  124. Smaller,
  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 String 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 String family() const = 0;
  164. virtual String variant() const = 0;
  165. virtual String qualified_name() const = 0;
  166. virtual String 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. }