BitmapFont.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/MappedFile.h>
  28. #include <AK/RefCounted.h>
  29. #include <AK/RefPtr.h>
  30. #include <AK/String.h>
  31. #include <AK/Types.h>
  32. #include <LibGfx/Font.h>
  33. #include <LibGfx/Size.h>
  34. namespace Gfx {
  35. enum FontTypes {
  36. Default = 0,
  37. LatinExtendedA = 1,
  38. // There are many blocks between LatinExtendedA and Cyrrilic that has to be added later.
  39. // Cyrrilic has to be switched to another number
  40. Cyrillic = 2
  41. };
  42. class BitmapFont : public Font {
  43. public:
  44. NonnullRefPtr<Font> clone() const;
  45. static NonnullRefPtr<BitmapFont> create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type);
  46. static RefPtr<BitmapFont> load_from_file(const StringView& path);
  47. bool write_to_file(const StringView& path);
  48. ~BitmapFont();
  49. u8 presentation_size() const { return m_presentation_size; }
  50. void set_presentation_size(u8 size) { m_presentation_size = size; }
  51. u16 weight() const { return m_weight; }
  52. void set_weight(u16 weight) { m_weight = weight; }
  53. Glyph glyph(u32 code_point) const;
  54. bool contains_glyph(u32 code_point) const { return code_point < (u32)glyph_count(); }
  55. u8 glyph_width(size_t ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[ch]; }
  56. int glyph_or_emoji_width(u32 code_point) const;
  57. u8 glyph_height() const { return m_glyph_height; }
  58. int x_height() const { return m_x_height; }
  59. u8 min_glyph_width() const { return m_min_glyph_width; }
  60. u8 max_glyph_width() const { return m_max_glyph_width; }
  61. u8 glyph_fixed_width() const { return m_glyph_width; }
  62. u8 baseline() const { return m_baseline; }
  63. void set_baseline(u8 baseline)
  64. {
  65. m_baseline = baseline;
  66. update_x_height();
  67. }
  68. u8 mean_line() const { return m_mean_line; }
  69. void set_mean_line(u8 mean_line)
  70. {
  71. m_mean_line = mean_line;
  72. update_x_height();
  73. }
  74. int width(const StringView&) const;
  75. int width(const Utf8View&) const;
  76. int width(const Utf32View&) const;
  77. String name() const { return m_name; }
  78. void set_name(String name) { m_name = move(name); }
  79. bool is_fixed_width() const { return m_fixed_width; }
  80. void set_fixed_width(bool b) { m_fixed_width = b; }
  81. u8 glyph_spacing() const { return m_glyph_spacing; }
  82. void set_glyph_spacing(u8 spacing) { m_glyph_spacing = spacing; }
  83. void set_glyph_width(size_t ch, u8 width)
  84. {
  85. VERIFY(m_glyph_widths);
  86. m_glyph_widths[ch] = width;
  87. }
  88. int glyph_count() const { return m_glyph_count; }
  89. FontTypes type() { return m_type; }
  90. void set_type(FontTypes type);
  91. String family() const { return m_family; }
  92. void set_family(String family) { m_family = move(family); }
  93. String variant() const { return String::formatted("{}", weight()); }
  94. String qualified_name() const;
  95. const Font& bold_variant() const;
  96. private:
  97. 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);
  98. static RefPtr<BitmapFont> load_from_memory(const u8*);
  99. static size_t glyph_count_by_type(FontTypes type);
  100. void update_x_height() { m_x_height = m_baseline - m_mean_line; };
  101. String m_name;
  102. String m_family;
  103. FontTypes m_type;
  104. size_t m_glyph_count { 256 };
  105. unsigned* m_rows { nullptr };
  106. u8* m_glyph_widths { nullptr };
  107. RefPtr<MappedFile> m_mapped_file;
  108. u8 m_glyph_width { 0 };
  109. u8 m_glyph_height { 0 };
  110. u8 m_x_height { 0 };
  111. u8 m_min_glyph_width { 0 };
  112. u8 m_max_glyph_width { 0 };
  113. u8 m_glyph_spacing { 0 };
  114. u8 m_baseline { 0 };
  115. u8 m_mean_line { 0 };
  116. u8 m_presentation_size { 0 };
  117. u16 m_weight { 0 };
  118. bool m_fixed_width { false };
  119. bool m_owns_arrays { false };
  120. mutable RefPtr<Gfx::Font> m_bold_variant;
  121. };
  122. }