BitmapFont.cpp 13 KB

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