BitmapFont.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. u16 range_mask_size;
  18. u8 is_variable_width;
  19. u8 glyph_spacing;
  20. u8 baseline;
  21. u8 mean_line;
  22. u8 presentation_size;
  23. u16 weight;
  24. u8 slope;
  25. char name[32];
  26. char family[32];
  27. };
  28. static_assert(AssertSize<FontFileHeader, 80>());
  29. static constexpr size_t s_max_glyph_count = 0x110000;
  30. static constexpr size_t s_max_range_mask_size = s_max_glyph_count / (256 * 8);
  31. NonnullRefPtr<Font> BitmapFont::clone() const
  32. {
  33. auto* new_range_mask = static_cast<u8*>(malloc(m_range_mask_size));
  34. memcpy(new_range_mask, m_range_mask, m_range_mask_size);
  35. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  36. auto* new_rows = static_cast<u32*>(kmalloc_array(m_glyph_count, bytes_per_glyph));
  37. memcpy(new_rows, m_rows, bytes_per_glyph * m_glyph_count);
  38. auto* new_widths = static_cast<u8*>(malloc(m_glyph_count));
  39. memcpy(new_widths, m_glyph_widths, m_glyph_count);
  40. 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_range_mask_size, new_range_mask, m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true));
  41. }
  42. NonnullRefPtr<BitmapFont> BitmapFont::create(u8 glyph_height, u8 glyph_width, bool fixed, size_t glyph_count)
  43. {
  44. glyph_count += 256 - (glyph_count % 256);
  45. glyph_count = min(glyph_count, s_max_glyph_count);
  46. size_t glyphs_per_range = 8 * 256;
  47. u16 range_mask_size = ceil_div(glyph_count, glyphs_per_range);
  48. auto* new_range_mask = static_cast<u8*>(calloc(range_mask_size, 1));
  49. for (size_t i = 0; i < glyph_count; i += 256) {
  50. new_range_mask[i / 256 / 8] |= 1 << (i / 256 % 8);
  51. }
  52. size_t bytes_per_glyph = sizeof(u32) * glyph_height;
  53. auto* new_rows = static_cast<u32*>(calloc(glyph_count, bytes_per_glyph));
  54. auto* new_widths = static_cast<u8*>(calloc(glyph_count, 1));
  55. return adopt_ref(*new BitmapFont("Untitled", "Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, range_mask_size, new_range_mask, 0, 0, 0, 400, 0, true));
  56. }
  57. NonnullRefPtr<BitmapFont> BitmapFont::unmasked_character_set() const
  58. {
  59. auto* new_range_mask = static_cast<u8*>(malloc(s_max_range_mask_size));
  60. constexpr u8 max_bits { 0b1111'1111 };
  61. memset(new_range_mask, max_bits, s_max_range_mask_size);
  62. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  63. auto* new_rows = static_cast<u32*>(kmalloc_array(s_max_glyph_count, bytes_per_glyph));
  64. auto* new_widths = static_cast<u8*>(calloc(s_max_glyph_count, 1));
  65. for (size_t code_point = 0; code_point < s_max_glyph_count; ++code_point) {
  66. auto index = glyph_index(code_point);
  67. if (index.has_value()) {
  68. memcpy(&new_widths[code_point], &m_glyph_widths[index.value()], 1);
  69. memcpy(&new_rows[code_point * glyph_height()], &m_rows[index.value() * glyph_height()], bytes_per_glyph);
  70. }
  71. }
  72. 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, s_max_range_mask_size, new_range_mask, m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true));
  73. }
  74. NonnullRefPtr<BitmapFont> BitmapFont::masked_character_set() const
  75. {
  76. auto* new_range_mask = static_cast<u8*>(calloc(s_max_range_mask_size, 1));
  77. u16 new_range_mask_size { 0 };
  78. for (size_t i = 0; i < s_max_glyph_count; ++i) {
  79. if (m_glyph_widths[i] > 0) {
  80. new_range_mask[i / 256 / 8] |= 1 << (i / 256 % 8);
  81. if (i / 256 / 8 + 1 > new_range_mask_size)
  82. new_range_mask_size = i / 256 / 8 + 1;
  83. }
  84. }
  85. size_t new_glyph_count { 0 };
  86. for (size_t i = 0; i < new_range_mask_size; ++i) {
  87. new_glyph_count += 256 * __builtin_popcount(new_range_mask[i]);
  88. }
  89. size_t bytes_per_glyph = sizeof(u32) * m_glyph_height;
  90. auto* new_rows = static_cast<u32*>(calloc(new_glyph_count, bytes_per_glyph));
  91. auto* new_widths = static_cast<u8*>(calloc(new_glyph_count, 1));
  92. for (size_t i = 0, j = 0; i < s_max_glyph_count; ++i) {
  93. if (!(new_range_mask[i / 256 / 8] & 1 << (i / 256 % 8))) {
  94. j++;
  95. i += 255;
  96. continue;
  97. }
  98. memcpy(&new_widths[i - j * 256], &m_glyph_widths[i], 1);
  99. memcpy(&new_rows[(i - j * 256) * glyph_height()], &m_rows[i * glyph_height()], bytes_per_glyph);
  100. }
  101. 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, new_range_mask_size, new_range_mask, m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true));
  102. }
  103. BitmapFont::BitmapFont(String name, String family, u32* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, u16 range_mask_size, u8* range_mask, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, u8 slope, bool owns_arrays)
  104. : m_name(name)
  105. , m_family(family)
  106. , m_range_mask_size(range_mask_size)
  107. , m_range_mask(range_mask)
  108. , m_rows(rows)
  109. , m_glyph_widths(widths)
  110. , m_glyph_width(glyph_width)
  111. , m_glyph_height(glyph_height)
  112. , m_min_glyph_width(glyph_width)
  113. , m_max_glyph_width(glyph_width)
  114. , m_glyph_spacing(glyph_spacing)
  115. , m_baseline(baseline)
  116. , m_mean_line(mean_line)
  117. , m_presentation_size(presentation_size)
  118. , m_weight(weight)
  119. , m_slope(slope)
  120. , m_fixed_width(is_fixed_width)
  121. , m_owns_arrays(owns_arrays)
  122. {
  123. VERIFY(m_range_mask);
  124. VERIFY(m_rows);
  125. VERIFY(m_glyph_widths);
  126. update_x_height();
  127. for (size_t i = 0, index = 0; i < m_range_mask_size; ++i) {
  128. for (size_t j = 0; j < 8; ++j) {
  129. if (m_range_mask[i] & (1 << j)) {
  130. m_glyph_count += 256;
  131. m_range_indices.append(index++);
  132. } else {
  133. m_range_indices.append({});
  134. }
  135. }
  136. }
  137. if (!m_fixed_width) {
  138. u8 maximum = 0;
  139. u8 minimum = 255;
  140. for (size_t i = 0; i < m_glyph_count; ++i) {
  141. minimum = min(minimum, m_glyph_widths[i]);
  142. maximum = max(maximum, m_glyph_widths[i]);
  143. }
  144. m_min_glyph_width = minimum;
  145. m_max_glyph_width = max(maximum, m_glyph_width);
  146. }
  147. }
  148. BitmapFont::~BitmapFont()
  149. {
  150. if (m_owns_arrays) {
  151. free(m_glyph_widths);
  152. free(m_rows);
  153. free(m_range_mask);
  154. }
  155. }
  156. RefPtr<BitmapFont> BitmapFont::load_from_memory(const u8* data)
  157. {
  158. auto& header = *reinterpret_cast<const FontFileHeader*>(data);
  159. if (memcmp(header.magic, "!Fnt", 4)) {
  160. dbgln("header.magic != '!Fnt', instead it's '{:c}{:c}{:c}{:c}'", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
  161. return nullptr;
  162. }
  163. if (header.name[sizeof(header.name) - 1] != '\0') {
  164. dbgln("Font name not fully null-terminated");
  165. return nullptr;
  166. }
  167. if (header.family[sizeof(header.family) - 1] != '\0') {
  168. dbgln("Font family not fully null-terminated");
  169. return nullptr;
  170. }
  171. size_t bytes_per_glyph = sizeof(u32) * header.glyph_height;
  172. size_t glyph_count { 0 };
  173. u8* range_mask = const_cast<u8*>(data + sizeof(FontFileHeader));
  174. for (size_t i = 0; i < header.range_mask_size; ++i)
  175. glyph_count += 256 * __builtin_popcount(range_mask[i]);
  176. u32* rows = (u32*)(range_mask + header.range_mask_size);
  177. u8* widths = (u8*)(rows) + glyph_count * bytes_per_glyph;
  178. 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, header.range_mask_size, range_mask, header.baseline, header.mean_line, header.presentation_size, header.weight, header.slope));
  179. }
  180. RefPtr<BitmapFont> BitmapFont::load_from_file(String const& path)
  181. {
  182. if (Core::File::is_device(path))
  183. return nullptr;
  184. auto file_or_error = MappedFile::map(path);
  185. if (file_or_error.is_error())
  186. return nullptr;
  187. auto font = load_from_memory((const u8*)file_or_error.value()->data());
  188. if (!font)
  189. return nullptr;
  190. font->m_mapped_file = file_or_error.release_value();
  191. return font;
  192. }
  193. bool BitmapFont::write_to_file(String const& path)
  194. {
  195. FontFileHeader header;
  196. memset(&header, 0, sizeof(FontFileHeader));
  197. memcpy(header.magic, "!Fnt", 4);
  198. header.glyph_width = m_glyph_width;
  199. header.glyph_height = m_glyph_height;
  200. header.range_mask_size = m_range_mask_size;
  201. header.baseline = m_baseline;
  202. header.mean_line = m_mean_line;
  203. header.is_variable_width = !m_fixed_width;
  204. header.glyph_spacing = m_glyph_spacing;
  205. header.presentation_size = m_presentation_size;
  206. header.weight = m_weight;
  207. header.slope = m_slope;
  208. memcpy(header.name, m_name.characters(), min(m_name.length(), sizeof(header.name) - 1));
  209. memcpy(header.family, m_family.characters(), min(m_family.length(), sizeof(header.family) - 1));
  210. auto stream_result = Core::OutputFileStream::open_buffered(path);
  211. if (stream_result.is_error())
  212. return false;
  213. auto& stream = stream_result.value();
  214. size_t bytes_per_glyph = sizeof(u32) * m_glyph_height;
  215. stream << ReadonlyBytes { &header, sizeof(header) };
  216. stream << ReadonlyBytes { m_range_mask, m_range_mask_size };
  217. stream << ReadonlyBytes { m_rows, m_glyph_count * bytes_per_glyph };
  218. stream << ReadonlyBytes { m_glyph_widths, m_glyph_count };
  219. stream.flush();
  220. if (stream.handle_any_error())
  221. return false;
  222. return true;
  223. }
  224. Glyph BitmapFont::glyph(u32 code_point) const
  225. {
  226. // Note: Until all fonts support the 0xFFFD replacement
  227. // character, fall back to painting '?' if necessary.
  228. auto index = glyph_index(code_point).value_or('?');
  229. auto width = m_glyph_widths[index];
  230. return Glyph(
  231. GlyphBitmap(&m_rows[index * m_glyph_height], { width, m_glyph_height }),
  232. 0,
  233. width,
  234. m_glyph_height);
  235. }
  236. Glyph BitmapFont::raw_glyph(u32 code_point) const
  237. {
  238. auto width = m_glyph_widths[code_point];
  239. return Glyph(
  240. GlyphBitmap(&m_rows[code_point * m_glyph_height], { width, m_glyph_height }),
  241. 0,
  242. width,
  243. m_glyph_height);
  244. }
  245. Optional<size_t> BitmapFont::glyph_index(u32 code_point) const
  246. {
  247. auto index = code_point / 256;
  248. if (index >= m_range_indices.size())
  249. return {};
  250. if (!m_range_indices[index].has_value())
  251. return {};
  252. return m_range_indices[index].value() * 256 + code_point % 256;
  253. }
  254. bool BitmapFont::contains_glyph(u32 code_point) const
  255. {
  256. auto index = glyph_index(code_point);
  257. return index.has_value() && m_glyph_widths[index.value()] > 0;
  258. }
  259. u8 BitmapFont::glyph_width(u32 code_point) const
  260. {
  261. if (is_ascii(code_point) && !is_ascii_printable(code_point))
  262. return 0;
  263. auto index = glyph_index(code_point);
  264. return m_fixed_width || !index.has_value() ? m_glyph_width : m_glyph_widths[index.value()];
  265. }
  266. int BitmapFont::glyph_or_emoji_width_for_variable_width_font(u32 code_point) const
  267. {
  268. // FIXME: This is a hack in lieu of proper code point identification.
  269. // 0xFFFF is arbitrary but also the end of the Basic Multilingual Plane.
  270. if (code_point < 0xFFFF) {
  271. auto index = glyph_index(code_point);
  272. if (!index.has_value())
  273. return glyph_width(0xFFFD);
  274. if (m_glyph_widths[index.value()] > 0)
  275. return glyph_width(code_point);
  276. return glyph_width(0xFFFD);
  277. }
  278. auto* emoji = Emoji::emoji_for_code_point(code_point);
  279. if (emoji == nullptr)
  280. return glyph_width(0xFFFD);
  281. return emoji->size().width();
  282. }
  283. int BitmapFont::width(StringView const& view) const { return unicode_view_width(Utf8View(view)); }
  284. int BitmapFont::width(Utf8View const& view) const { return unicode_view_width(view); }
  285. int BitmapFont::width(Utf32View const& view) const { return unicode_view_width(view); }
  286. template<typename T>
  287. ALWAYS_INLINE int BitmapFont::unicode_view_width(T const& view) const
  288. {
  289. if (view.is_empty())
  290. return 0;
  291. bool first = true;
  292. int width = 0;
  293. int longest_width = 0;
  294. for (u32 code_point : view) {
  295. if (code_point == '\n' || code_point == '\r') {
  296. first = true;
  297. longest_width = max(width, longest_width);
  298. width = 0;
  299. continue;
  300. }
  301. if (!first)
  302. width += glyph_spacing();
  303. first = false;
  304. width += glyph_or_emoji_width(code_point);
  305. }
  306. longest_width = max(width, longest_width);
  307. return longest_width;
  308. }
  309. String BitmapFont::qualified_name() const
  310. {
  311. return String::formatted("{} {} {}", family(), presentation_size(), weight());
  312. }
  313. Font const& Font::bold_variant() const
  314. {
  315. if (m_bold_variant)
  316. return *m_bold_variant;
  317. m_bold_variant = Gfx::FontDatabase::the().get(family(), presentation_size(), 700);
  318. if (!m_bold_variant)
  319. m_bold_variant = this;
  320. return *m_bold_variant;
  321. }
  322. }