BitmapFont.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/MappedFile.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/String.h>
  12. #include <AK/Types.h>
  13. #include <LibGfx/Font.h>
  14. #include <LibGfx/Size.h>
  15. namespace Gfx {
  16. class BitmapFont final : public Font {
  17. public:
  18. NonnullRefPtr<Font> clone() const override;
  19. static NonnullRefPtr<BitmapFont> create(u8 glyph_height, u8 glyph_width, bool fixed, size_t glyph_count);
  20. NonnullRefPtr<BitmapFont> masked_character_set() const;
  21. NonnullRefPtr<BitmapFont> unmasked_character_set() const;
  22. static RefPtr<BitmapFont> load_from_file(String const& path);
  23. bool write_to_file(String const& path);
  24. ~BitmapFont();
  25. u8 presentation_size() const override { return m_presentation_size; }
  26. void set_presentation_size(u8 size) { m_presentation_size = size; }
  27. u16 weight() const override { return m_weight; }
  28. void set_weight(u16 weight) { m_weight = weight; }
  29. u8 slope() const { return m_slope; }
  30. void set_slope(u8 slope) { m_slope = slope; }
  31. Glyph glyph(u32 code_point) const override;
  32. Glyph raw_glyph(u32 code_point) const;
  33. bool contains_glyph(u32 code_point) const override;
  34. ALWAYS_INLINE int glyph_or_emoji_width(u32 code_point) const override
  35. {
  36. if (m_fixed_width)
  37. return m_glyph_width;
  38. return glyph_or_emoji_width_for_variable_width_font(code_point);
  39. }
  40. u8 glyph_height() const override { return m_glyph_height; }
  41. int x_height() const override { return m_x_height; }
  42. u8 glyph_width(u32 code_point) const override;
  43. u8 raw_glyph_width(u32 code_point) const { return m_glyph_widths[code_point]; }
  44. u8 min_glyph_width() const override { return m_min_glyph_width; }
  45. u8 max_glyph_width() const override { return m_max_glyph_width; }
  46. u8 glyph_fixed_width() const override { return m_glyph_width; }
  47. u8 baseline() const override { return m_baseline; }
  48. void set_baseline(u8 baseline)
  49. {
  50. m_baseline = baseline;
  51. update_x_height();
  52. }
  53. u8 mean_line() const override { return m_mean_line; }
  54. void set_mean_line(u8 mean_line)
  55. {
  56. m_mean_line = mean_line;
  57. update_x_height();
  58. }
  59. int width(StringView const&) const override;
  60. int width(Utf8View const&) const override;
  61. int width(Utf32View const&) const override;
  62. String name() const override { return m_name; }
  63. void set_name(String name) { m_name = move(name); }
  64. bool is_fixed_width() const override { return m_fixed_width; }
  65. void set_fixed_width(bool b) { m_fixed_width = b; }
  66. u8 glyph_spacing() const override { return m_glyph_spacing; }
  67. void set_glyph_spacing(u8 spacing) { m_glyph_spacing = spacing; }
  68. void set_glyph_width(u32 code_point, u8 width)
  69. {
  70. VERIFY(m_glyph_widths);
  71. m_glyph_widths[code_point] = width;
  72. }
  73. size_t glyph_count() const override { return m_glyph_count; }
  74. Optional<size_t> glyph_index(u32 code_point) const;
  75. u16 range_size() const { return m_range_mask_size; }
  76. bool is_range_empty(u32 code_point) const { return !(m_range_mask[code_point / 256 / 8] & 1 << (code_point / 256 % 8)); }
  77. String family() const override { return m_family; }
  78. void set_family(String family) { m_family = move(family); }
  79. String variant() const override;
  80. String qualified_name() const override;
  81. private:
  82. BitmapFont(String name, String family, u32* rows, u8* widths, bool is_fixed_width,
  83. u8 glyph_width, u8 glyph_height, u8 glyph_spacing, u16 range_mask_size, u8* range_mask,
  84. u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, u8 slope, bool owns_arrays = false);
  85. static RefPtr<BitmapFont> load_from_memory(u8 const*);
  86. template<typename T>
  87. int unicode_view_width(T const& view) const;
  88. void update_x_height() { m_x_height = m_baseline - m_mean_line; };
  89. int glyph_or_emoji_width_for_variable_width_font(u32 code_point) const;
  90. String m_name;
  91. String m_family;
  92. size_t m_glyph_count { 0 };
  93. u16 m_range_mask_size { 0 };
  94. u8* m_range_mask { nullptr };
  95. Vector<Optional<size_t>> m_range_indices;
  96. u32* m_rows { nullptr };
  97. u8* m_glyph_widths { nullptr };
  98. RefPtr<MappedFile> m_mapped_file;
  99. u8 m_glyph_width { 0 };
  100. u8 m_glyph_height { 0 };
  101. u8 m_x_height { 0 };
  102. u8 m_min_glyph_width { 0 };
  103. u8 m_max_glyph_width { 0 };
  104. u8 m_glyph_spacing { 0 };
  105. u8 m_baseline { 0 };
  106. u8 m_mean_line { 0 };
  107. u8 m_presentation_size { 0 };
  108. u16 m_weight { 0 };
  109. u8 m_slope { 0 };
  110. bool m_fixed_width { false };
  111. bool m_owns_arrays { false };
  112. };
  113. }