BitmapFont.cpp 9.8 KB

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