BitmapFont.h 4.6 KB

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