BitmapFont.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/CharacterTypes.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/Types.h>
  12. #include <AK/Vector.h>
  13. #include <LibCore/MappedFile.h>
  14. #include <LibGfx/Font/Font.h>
  15. #include <LibGfx/Size.h>
  16. namespace Gfx {
  17. class BitmapFont final : public Font {
  18. public:
  19. virtual NonnullRefPtr<Font> clone() const override;
  20. ErrorOr<NonnullRefPtr<Font>> try_clone() const override;
  21. static NonnullRefPtr<BitmapFont> create(u8 glyph_height, u8 glyph_width, bool fixed, size_t glyph_count);
  22. static ErrorOr<NonnullRefPtr<BitmapFont>> try_create(u8 glyph_height, u8 glyph_width, bool fixed, size_t glyph_count);
  23. virtual FontPixelMetrics pixel_metrics() const override;
  24. ErrorOr<NonnullRefPtr<BitmapFont>> masked_character_set() const;
  25. ErrorOr<NonnullRefPtr<BitmapFont>> unmasked_character_set() const;
  26. static RefPtr<BitmapFont> load_from_file(DeprecatedString const& path);
  27. static ErrorOr<NonnullRefPtr<BitmapFont>> try_load_from_file(DeprecatedString const& path);
  28. ErrorOr<void> write_to_file(DeprecatedString const& path);
  29. ~BitmapFont();
  30. u8* rows() { return m_rows; }
  31. u8* widths() { return m_glyph_widths; }
  32. virtual float point_size() const override { return m_presentation_size; }
  33. u8 presentation_size() const override { return m_presentation_size; }
  34. void set_presentation_size(u8 size) { m_presentation_size = size; }
  35. virtual float pixel_size() const override { return m_glyph_height; }
  36. virtual int pixel_size_rounded_up() const override { return m_glyph_height; }
  37. u16 width() const override { return FontWidth::Normal; }
  38. u16 weight() const override { return m_weight; }
  39. void set_weight(u16 weight) { m_weight = weight; }
  40. virtual u8 slope() const override { return m_slope; }
  41. void set_slope(u8 slope) { m_slope = slope; }
  42. Glyph glyph(u32 code_point) const override;
  43. Glyph glyph(u32 code_point, GlyphSubpixelOffset) const override { return glyph(code_point); }
  44. float glyph_left_bearing(u32) const override { return 0; }
  45. Glyph raw_glyph(u32 code_point) const;
  46. bool contains_glyph(u32 code_point) const override;
  47. bool contains_raw_glyph(u32 code_point) const { return m_glyph_widths[code_point] > 0; }
  48. virtual float glyph_or_emoji_width(Utf8CodePointIterator&) const override;
  49. virtual float glyph_or_emoji_width(Utf32CodePointIterator&) const override;
  50. float glyphs_horizontal_kerning(u32, u32) const override { return 0.f; }
  51. u8 glyph_height() const { return m_glyph_height; }
  52. int x_height() const override { return m_x_height; }
  53. virtual float preferred_line_height() const override { return glyph_height() + m_line_gap; }
  54. virtual float glyph_width(u32 code_point) const override;
  55. u8 raw_glyph_width(u32 code_point) const { return m_glyph_widths[code_point]; }
  56. u8 min_glyph_width() const override { return m_min_glyph_width; }
  57. u8 max_glyph_width() const override { return m_max_glyph_width; }
  58. u8 glyph_fixed_width() const override { return m_glyph_width; }
  59. u8 baseline() const override { return m_baseline; }
  60. void set_baseline(u8 baseline)
  61. {
  62. m_baseline = baseline;
  63. update_x_height();
  64. }
  65. u8 mean_line() const override { return m_mean_line; }
  66. void set_mean_line(u8 mean_line)
  67. {
  68. m_mean_line = mean_line;
  69. update_x_height();
  70. }
  71. virtual float width(StringView) const override;
  72. virtual float width(Utf8View const&) const override;
  73. virtual float width(Utf32View const&) const override;
  74. virtual int width_rounded_up(StringView) const override;
  75. DeprecatedString name() const override { return m_name; }
  76. void set_name(DeprecatedString name) { m_name = move(name); }
  77. bool is_fixed_width() const override { return m_fixed_width; }
  78. void set_fixed_width(bool b) { m_fixed_width = b; }
  79. u8 glyph_spacing() const override { return m_glyph_spacing; }
  80. void set_glyph_spacing(u8 spacing) { m_glyph_spacing = spacing; }
  81. void set_glyph_width(u32 code_point, u8 width)
  82. {
  83. VERIFY(m_glyph_widths);
  84. m_glyph_widths[code_point] = width;
  85. }
  86. size_t glyph_count() const override { return m_glyph_count; }
  87. Optional<size_t> glyph_index(u32 code_point) const;
  88. u16 range_size() const { return m_range_mask_size; }
  89. bool is_range_empty(u32 code_point) const { return !(m_range_mask[code_point / 256 / 8] & 1 << (code_point / 256 % 8)); }
  90. DeprecatedString family() const override { return m_family; }
  91. void set_family(DeprecatedString family) { m_family = move(family); }
  92. DeprecatedString variant() const override;
  93. DeprecatedString qualified_name() const override;
  94. DeprecatedString human_readable_name() const override { return DeprecatedString::formatted("{} {} {}", family(), variant(), presentation_size()); }
  95. virtual RefPtr<Font> with_size(float point_size) const override;
  96. private:
  97. BitmapFont(DeprecatedString name, DeprecatedString family, u8* rows, u8* widths, bool is_fixed_width,
  98. u8 glyph_width, u8 glyph_height, u8 glyph_spacing, u16 range_mask_size, u8* range_mask,
  99. u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, u8 slope, bool owns_arrays = false);
  100. static ErrorOr<NonnullRefPtr<BitmapFont>> load_from_memory(u8 const*);
  101. template<typename T>
  102. int unicode_view_width(T const& view) const;
  103. void update_x_height() { m_x_height = m_baseline - m_mean_line; };
  104. virtual bool has_color_bitmaps() const override { return false; }
  105. DeprecatedString m_name;
  106. DeprecatedString m_family;
  107. size_t m_glyph_count { 0 };
  108. u16 m_range_mask_size { 0 };
  109. u8* m_range_mask { nullptr };
  110. Vector<Optional<size_t>> m_range_indices;
  111. u8* m_rows { nullptr };
  112. u8* m_glyph_widths { nullptr };
  113. RefPtr<Core::MappedFile> m_mapped_file;
  114. u8 m_glyph_width { 0 };
  115. u8 m_glyph_height { 0 };
  116. u8 m_x_height { 0 };
  117. u8 m_min_glyph_width { 0 };
  118. u8 m_max_glyph_width { 0 };
  119. u8 m_glyph_spacing { 0 };
  120. u8 m_baseline { 0 };
  121. u8 m_mean_line { 0 };
  122. u8 m_presentation_size { 0 };
  123. u16 m_weight { 0 };
  124. u8 m_slope { 0 };
  125. u8 m_line_gap { 4 };
  126. bool m_fixed_width { false };
  127. bool m_owns_arrays { false };
  128. };
  129. }