BitmapFont.cpp 9.8 KB

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