BitmapFont.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "BitmapFont.h"
  7. #include "Emoji.h"
  8. #include <AK/Utf32View.h>
  9. #include <AK/Utf8View.h>
  10. #include <LibCore/FileStream.h>
  11. #include <LibGfx/FontDatabase.h>
  12. namespace Gfx {
  13. struct [[gnu::packed]] FontFileHeader {
  14. char magic[4];
  15. u8 glyph_width;
  16. u8 glyph_height;
  17. u8 type;
  18. u8 is_variable_width;
  19. u8 glyph_spacing;
  20. u8 baseline;
  21. u8 mean_line;
  22. u8 presentation_size;
  23. u16 weight;
  24. char name[32];
  25. char family[32];
  26. u16 unused;
  27. };
  28. static_assert(sizeof(FontFileHeader) == 80);
  29. NonnullRefPtr<Font> BitmapFont::clone() const
  30. {
  31. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  32. auto* new_rows = static_cast<unsigned*>(malloc(bytes_per_glyph * m_glyph_count));
  33. memcpy(new_rows, m_rows, bytes_per_glyph * m_glyph_count);
  34. auto* new_widths = static_cast<u8*>(malloc(m_glyph_count));
  35. memcpy(new_widths, m_glyph_widths, m_glyph_count);
  36. return adopt_ref(*new BitmapFont(m_name, m_family, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, m_type, m_baseline, m_mean_line, m_presentation_size, m_weight, true));
  37. }
  38. NonnullRefPtr<BitmapFont> BitmapFont::create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type)
  39. {
  40. size_t bytes_per_glyph = sizeof(u32) * glyph_height;
  41. size_t count = glyph_count_by_type(type);
  42. auto* new_rows = static_cast<unsigned*>(calloc(count, bytes_per_glyph));
  43. auto* new_widths = static_cast<u8*>(calloc(count, 1));
  44. return adopt_ref(*new BitmapFont("Untitled", "Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, type, 0, 0, 0, 400, true));
  45. }
  46. BitmapFont::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)
  47. : m_name(name)
  48. , m_family(family)
  49. , m_type(type)
  50. , m_rows(rows)
  51. , m_glyph_widths(widths)
  52. , m_glyph_width(glyph_width)
  53. , m_glyph_height(glyph_height)
  54. , m_min_glyph_width(glyph_width)
  55. , m_max_glyph_width(glyph_width)
  56. , m_glyph_spacing(glyph_spacing)
  57. , m_baseline(baseline)
  58. , m_mean_line(mean_line)
  59. , m_presentation_size(presentation_size)
  60. , m_weight(weight)
  61. , m_fixed_width(is_fixed_width)
  62. , m_owns_arrays(owns_arrays)
  63. {
  64. VERIFY(m_rows);
  65. VERIFY(m_glyph_widths);
  66. update_x_height();
  67. m_glyph_count = glyph_count_by_type(m_type);
  68. if (!m_fixed_width) {
  69. u8 maximum = 0;
  70. u8 minimum = 255;
  71. for (size_t i = 0; i < m_glyph_count; ++i) {
  72. minimum = min(minimum, m_glyph_widths[i]);
  73. maximum = max(maximum, m_glyph_widths[i]);
  74. }
  75. m_min_glyph_width = minimum;
  76. m_max_glyph_width = max(maximum, m_glyph_width);
  77. }
  78. }
  79. BitmapFont::~BitmapFont()
  80. {
  81. if (m_owns_arrays) {
  82. free(m_glyph_widths);
  83. free(m_rows);
  84. }
  85. }
  86. RefPtr<BitmapFont> BitmapFont::load_from_memory(const u8* data)
  87. {
  88. auto& header = *reinterpret_cast<const FontFileHeader*>(data);
  89. if (memcmp(header.magic, "!Fnt", 4)) {
  90. dbgln("header.magic != '!Fnt', instead it's '{:c}{:c}{:c}{:c}'", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
  91. return nullptr;
  92. }
  93. if (header.name[sizeof(header.name) - 1] != '\0') {
  94. dbgln("Font name not fully null-terminated");
  95. return nullptr;
  96. }
  97. if (header.family[sizeof(header.family) - 1] != '\0') {
  98. dbgln("Font family not fully null-terminated");
  99. return nullptr;
  100. }
  101. FontTypes type;
  102. if (header.type == 0)
  103. type = FontTypes::Default;
  104. else if (header.type == 1)
  105. type = FontTypes::LatinExtendedA;
  106. else if (header.type == 2)
  107. type = FontTypes::Cyrillic;
  108. else if (header.type == 3)
  109. type = FontTypes::Hebrew;
  110. else
  111. VERIFY_NOT_REACHED();
  112. size_t count = glyph_count_by_type(type);
  113. size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
  114. auto* rows = const_cast<unsigned*>((const unsigned*)(data + sizeof(FontFileHeader)));
  115. u8* widths = (u8*)(rows) + count * bytes_per_glyph;
  116. return adopt_ref(*new BitmapFont(String(header.name), String(header.family), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height, header.glyph_spacing, type, header.baseline, header.mean_line, header.presentation_size, header.weight));
  117. }
  118. size_t BitmapFont::glyph_count_by_type(FontTypes type)
  119. {
  120. if (type == FontTypes::Default)
  121. return 256;
  122. if (type == FontTypes::LatinExtendedA)
  123. return 384;
  124. if (type == FontTypes::Cyrillic)
  125. return 1280;
  126. if (type == FontTypes::Hebrew)
  127. return 1536;
  128. dbgln("Unknown font type: {}", (int)type);
  129. VERIFY_NOT_REACHED();
  130. }
  131. String BitmapFont::type_name_by_type(FontTypes type)
  132. {
  133. if (type == FontTypes::Default)
  134. return "Default";
  135. if (type == FontTypes::LatinExtendedA)
  136. return "LatinExtendedA";
  137. if (type == FontTypes::Cyrillic)
  138. return "Cyrillic";
  139. if (type == FontTypes::Hebrew)
  140. return "Hebrew";
  141. dbgln("Unknown font type: {}", (int)type);
  142. VERIFY_NOT_REACHED();
  143. }
  144. RefPtr<BitmapFont> BitmapFont::load_from_file(String const& path)
  145. {
  146. if (Core::File::is_device(path))
  147. return nullptr;
  148. auto file_or_error = MappedFile::map(path);
  149. if (file_or_error.is_error())
  150. return nullptr;
  151. auto font = load_from_memory((const u8*)file_or_error.value()->data());
  152. if (!font)
  153. return nullptr;
  154. font->m_mapped_file = file_or_error.release_value();
  155. return font;
  156. }
  157. bool BitmapFont::write_to_file(String const& path)
  158. {
  159. FontFileHeader header;
  160. memset(&header, 0, sizeof(FontFileHeader));
  161. memcpy(header.magic, "!Fnt", 4);
  162. header.glyph_width = m_glyph_width;
  163. header.glyph_height = m_glyph_height;
  164. header.type = m_type;
  165. header.baseline = m_baseline;
  166. header.mean_line = m_mean_line;
  167. header.is_variable_width = !m_fixed_width;
  168. header.glyph_spacing = m_glyph_spacing;
  169. header.presentation_size = m_presentation_size;
  170. header.weight = m_weight;
  171. memcpy(header.name, m_name.characters(), min(m_name.length(), sizeof(header.name) - 1));
  172. memcpy(header.family, m_family.characters(), min(m_family.length(), sizeof(header.family) - 1));
  173. size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
  174. size_t count = glyph_count_by_type(m_type);
  175. auto stream_result = Core::OutputFileStream::open_buffered(path);
  176. if (stream_result.is_error())
  177. return false;
  178. auto& stream = stream_result.value();
  179. stream << ReadonlyBytes { &header, sizeof(header) };
  180. stream << ReadonlyBytes { m_rows, count * bytes_per_glyph };
  181. stream << ReadonlyBytes { m_glyph_widths, count };
  182. stream.flush();
  183. if (stream.handle_any_error())
  184. return false;
  185. return true;
  186. }
  187. Glyph BitmapFont::glyph(u32 code_point) const
  188. {
  189. auto width = glyph_width(code_point);
  190. return Glyph(
  191. GlyphBitmap(&m_rows[code_point * m_glyph_height], { width, m_glyph_height }),
  192. 0,
  193. width,
  194. m_glyph_height);
  195. }
  196. int BitmapFont::glyph_or_emoji_width_for_variable_width_font(u32 code_point) const
  197. {
  198. if (code_point < m_glyph_count) {
  199. if (m_glyph_widths[code_point] > 0)
  200. return glyph_width(code_point);
  201. else
  202. return glyph_width('?');
  203. }
  204. auto* emoji = Emoji::emoji_for_code_point(code_point);
  205. if (emoji == nullptr)
  206. return glyph_width('?');
  207. return emoji->size().width();
  208. }
  209. int BitmapFont::width(StringView const& view) const { return unicode_view_width(Utf8View(view)); }
  210. int BitmapFont::width(Utf8View const& view) const { return unicode_view_width(view); }
  211. int BitmapFont::width(Utf32View const& view) const { return unicode_view_width(view); }
  212. template<typename T>
  213. ALWAYS_INLINE int BitmapFont::unicode_view_width(T const& view) const
  214. {
  215. if (view.is_empty())
  216. return 0;
  217. bool first = true;
  218. int width = 0;
  219. int longest_width = 0;
  220. for (u32 code_point : view) {
  221. if (code_point == '\n' || code_point == '\r') {
  222. first = true;
  223. longest_width = max(width, longest_width);
  224. width = 0;
  225. continue;
  226. }
  227. if (!first)
  228. width += glyph_spacing();
  229. first = false;
  230. width += glyph_or_emoji_width(code_point);
  231. }
  232. longest_width = max(width, longest_width);
  233. return longest_width;
  234. }
  235. void BitmapFont::set_type(FontTypes type)
  236. {
  237. if (type == m_type)
  238. return;
  239. if (type == FontTypes::Default)
  240. return;
  241. size_t new_glyph_count = glyph_count_by_type(type);
  242. if (new_glyph_count <= m_glyph_count) {
  243. m_glyph_count = new_glyph_count;
  244. return;
  245. }
  246. int item_count_to_copy = min(m_glyph_count, new_glyph_count);
  247. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  248. auto* new_rows = static_cast<unsigned*>(calloc(new_glyph_count, bytes_per_glyph));
  249. memcpy(new_rows, m_rows, bytes_per_glyph * item_count_to_copy);
  250. auto* new_widths = static_cast<u8*>(calloc(new_glyph_count, 1));
  251. memcpy(new_widths, m_glyph_widths, item_count_to_copy);
  252. kfree(m_rows);
  253. kfree(m_glyph_widths);
  254. m_type = type;
  255. m_glyph_count = new_glyph_count;
  256. m_rows = new_rows;
  257. m_glyph_widths = new_widths;
  258. }
  259. String BitmapFont::qualified_name() const
  260. {
  261. return String::formatted("{} {} {}", family(), presentation_size(), weight());
  262. }
  263. Font const& Font::bold_variant() const
  264. {
  265. if (m_bold_variant)
  266. return *m_bold_variant;
  267. m_bold_variant = Gfx::FontDatabase::the().get(family(), presentation_size(), 700);
  268. if (!m_bold_variant)
  269. m_bold_variant = this;
  270. return *m_bold_variant;
  271. }
  272. }