BitmapFont.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "BitmapFont.h"
  27. #include "Bitmap.h"
  28. #include "Emoji.h"
  29. #include <AK/StdLibExtras.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/Utf32View.h>
  32. #include <AK/Utf8View.h>
  33. #include <AK/Vector.h>
  34. #include <LibCore/FileStream.h>
  35. #include <LibGfx/FontDatabase.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/mman.h>
  40. #include <unistd.h>
  41. namespace Gfx {
  42. struct [[gnu::packed]] FontFileHeader {
  43. char magic[4];
  44. u8 glyph_width;
  45. u8 glyph_height;
  46. u8 type;
  47. u8 is_variable_width;
  48. u8 glyph_spacing;
  49. u8 baseline;
  50. u8 mean_line;
  51. u8 presentation_size;
  52. u16 weight;
  53. char name[32];
  54. char family[32];
  55. };
  56. NonnullRefPtr<Font> BitmapFont::clone() const
  57. {
  58. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  59. auto* new_rows = static_cast<unsigned*>(malloc(bytes_per_glyph * m_glyph_count));
  60. memcpy(new_rows, m_rows, bytes_per_glyph * m_glyph_count);
  61. auto* new_widths = static_cast<u8*>(malloc(m_glyph_count));
  62. memcpy(new_widths, m_glyph_widths, m_glyph_count);
  63. return adopt(*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));
  64. }
  65. NonnullRefPtr<BitmapFont> BitmapFont::create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type)
  66. {
  67. size_t bytes_per_glyph = sizeof(u32) * glyph_height;
  68. size_t count = glyph_count_by_type(type);
  69. auto* new_rows = static_cast<unsigned*>(malloc(bytes_per_glyph * count));
  70. memset(new_rows, 0, bytes_per_glyph * count);
  71. auto* new_widths = static_cast<u8*>(malloc(count));
  72. memset(new_widths, 0, count);
  73. return adopt(*new BitmapFont("Untitled", "Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, type, 0, 0, 0, 400, true));
  74. }
  75. 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)
  76. : m_name(name)
  77. , m_family(family)
  78. , m_type(type)
  79. , m_rows(rows)
  80. , m_glyph_widths(widths)
  81. , m_glyph_width(glyph_width)
  82. , m_glyph_height(glyph_height)
  83. , m_min_glyph_width(glyph_width)
  84. , m_max_glyph_width(glyph_width)
  85. , m_glyph_spacing(glyph_spacing)
  86. , m_baseline(baseline)
  87. , m_mean_line(mean_line)
  88. , m_presentation_size(presentation_size)
  89. , m_weight(weight)
  90. , m_fixed_width(is_fixed_width)
  91. , m_owns_arrays(owns_arrays)
  92. {
  93. VERIFY(m_rows);
  94. VERIFY(m_glyph_widths);
  95. update_x_height();
  96. m_glyph_count = glyph_count_by_type(m_type);
  97. if (!m_fixed_width) {
  98. u8 maximum = 0;
  99. u8 minimum = 255;
  100. for (size_t i = 0; i < m_glyph_count; ++i) {
  101. minimum = min(minimum, m_glyph_widths[i]);
  102. maximum = max(maximum, m_glyph_widths[i]);
  103. }
  104. m_min_glyph_width = minimum;
  105. m_max_glyph_width = max(maximum, m_glyph_width);
  106. }
  107. }
  108. BitmapFont::~BitmapFont()
  109. {
  110. if (m_owns_arrays) {
  111. free(m_glyph_widths);
  112. free(m_rows);
  113. }
  114. }
  115. RefPtr<BitmapFont> BitmapFont::load_from_memory(const u8* data)
  116. {
  117. auto& header = *reinterpret_cast<const FontFileHeader*>(data);
  118. if (memcmp(header.magic, "!Fnt", 4)) {
  119. dbgln("header.magic != '!Fnt', instead it's '{:c}{:c}{:c}{:c}'", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
  120. return nullptr;
  121. }
  122. if (header.name[sizeof(header.name) - 1] != '\0') {
  123. dbgln("Font name not fully null-terminated");
  124. return nullptr;
  125. }
  126. if (header.family[sizeof(header.family) - 1] != '\0') {
  127. dbgln("Font family not fully null-terminated");
  128. return nullptr;
  129. }
  130. FontTypes type;
  131. if (header.type == 0)
  132. type = FontTypes::Default;
  133. else if (header.type == 1)
  134. type = FontTypes::LatinExtendedA;
  135. else if (header.type == 2)
  136. type = FontTypes::Cyrillic;
  137. else if (header.type == 3)
  138. type = FontTypes::Hebrew;
  139. else
  140. VERIFY_NOT_REACHED();
  141. size_t count = glyph_count_by_type(type);
  142. size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
  143. auto* rows = const_cast<unsigned*>((const unsigned*)(data + sizeof(FontFileHeader)));
  144. u8* widths = (u8*)(rows) + count * bytes_per_glyph;
  145. return adopt(*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));
  146. }
  147. size_t BitmapFont::glyph_count_by_type(FontTypes type)
  148. {
  149. if (type == FontTypes::Default)
  150. return 256;
  151. if (type == FontTypes::LatinExtendedA)
  152. return 384;
  153. if (type == FontTypes::Cyrillic)
  154. return 1280;
  155. if (type == FontTypes::Hebrew)
  156. return 1536;
  157. dbgln("Unknown font type: {}", (int)type);
  158. VERIFY_NOT_REACHED();
  159. }
  160. String BitmapFont::type_name_by_type(FontTypes type)
  161. {
  162. if (type == FontTypes::Default)
  163. return "Default";
  164. if (type == FontTypes::LatinExtendedA)
  165. return "LatinExtendedA";
  166. if (type == FontTypes::Cyrillic)
  167. return "Cyrillic";
  168. if (type == FontTypes::Hebrew)
  169. return "Hebrew";
  170. dbgln("Unknown font type: {}", (int)type);
  171. VERIFY_NOT_REACHED();
  172. }
  173. RefPtr<BitmapFont> BitmapFont::load_from_file(String const& path)
  174. {
  175. if (Core::File::is_device(path))
  176. return nullptr;
  177. auto file_or_error = MappedFile::map(path);
  178. if (file_or_error.is_error())
  179. return nullptr;
  180. auto font = load_from_memory((const u8*)file_or_error.value()->data());
  181. if (!font)
  182. return nullptr;
  183. font->m_mapped_file = file_or_error.release_value();
  184. return font;
  185. }
  186. bool BitmapFont::write_to_file(String const& path)
  187. {
  188. FontFileHeader header;
  189. memset(&header, 0, sizeof(FontFileHeader));
  190. memcpy(header.magic, "!Fnt", 4);
  191. header.glyph_width = m_glyph_width;
  192. header.glyph_height = m_glyph_height;
  193. header.type = m_type;
  194. header.baseline = m_baseline;
  195. header.mean_line = m_mean_line;
  196. header.is_variable_width = !m_fixed_width;
  197. header.glyph_spacing = m_glyph_spacing;
  198. header.presentation_size = m_presentation_size;
  199. header.weight = m_weight;
  200. memcpy(header.name, m_name.characters(), min(m_name.length(), sizeof(header.name) - 1));
  201. memcpy(header.family, m_family.characters(), min(m_family.length(), sizeof(header.family) - 1));
  202. size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
  203. size_t count = glyph_count_by_type(m_type);
  204. auto stream_result = Core::OutputFileStream::open_buffered(path);
  205. if (stream_result.is_error())
  206. return false;
  207. auto& stream = stream_result.value();
  208. stream << ReadonlyBytes { &header, sizeof(header) };
  209. stream << ReadonlyBytes { m_rows, count * bytes_per_glyph };
  210. stream << ReadonlyBytes { m_glyph_widths, count };
  211. stream.flush();
  212. if (stream.handle_any_error())
  213. return false;
  214. return true;
  215. }
  216. Glyph BitmapFont::glyph(u32 code_point) const
  217. {
  218. auto width = glyph_width(code_point);
  219. return Glyph(
  220. GlyphBitmap(&m_rows[code_point * m_glyph_height], { width, m_glyph_height }),
  221. 0,
  222. width,
  223. m_glyph_height);
  224. }
  225. int BitmapFont::glyph_or_emoji_width(u32 code_point) const
  226. {
  227. if (code_point < m_glyph_count)
  228. return glyph_width(code_point);
  229. if (m_fixed_width)
  230. return m_glyph_width;
  231. auto* emoji = Emoji::emoji_for_code_point(code_point);
  232. if (emoji == nullptr)
  233. return glyph_width('?');
  234. return emoji->size().width();
  235. }
  236. int BitmapFont::width(const StringView& string) const
  237. {
  238. Utf8View utf8 { string };
  239. return width(utf8);
  240. }
  241. int BitmapFont::width(const Utf8View& utf8) const
  242. {
  243. bool first = true;
  244. int width = 0;
  245. for (u32 code_point : utf8) {
  246. if (!first)
  247. width += glyph_spacing();
  248. first = false;
  249. width += glyph_or_emoji_width(code_point);
  250. }
  251. return width;
  252. }
  253. int BitmapFont::width(const Utf32View& view) const
  254. {
  255. if (view.length() == 0)
  256. return 0;
  257. int width = (view.length() - 1) * glyph_spacing();
  258. for (size_t i = 0; i < view.length(); ++i)
  259. width += glyph_or_emoji_width(view.code_points()[i]);
  260. return width;
  261. }
  262. void BitmapFont::set_type(FontTypes type)
  263. {
  264. if (type == m_type)
  265. return;
  266. if (type == FontTypes::Default)
  267. return;
  268. size_t new_glyph_count = glyph_count_by_type(type);
  269. if (new_glyph_count <= m_glyph_count) {
  270. m_glyph_count = new_glyph_count;
  271. return;
  272. }
  273. int item_count_to_copy = min(m_glyph_count, new_glyph_count);
  274. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  275. auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * new_glyph_count));
  276. memset(new_rows, (unsigned)0, bytes_per_glyph * new_glyph_count);
  277. memcpy(new_rows, m_rows, bytes_per_glyph * item_count_to_copy);
  278. auto* new_widths = static_cast<u8*>(kmalloc(new_glyph_count));
  279. memset(new_widths, (u8)0, new_glyph_count);
  280. memcpy(new_widths, m_glyph_widths, item_count_to_copy);
  281. kfree(m_rows);
  282. kfree(m_glyph_widths);
  283. m_type = type;
  284. m_glyph_count = new_glyph_count;
  285. m_rows = new_rows;
  286. m_glyph_widths = new_widths;
  287. }
  288. String BitmapFont::qualified_name() const
  289. {
  290. return String::formatted("{} {} {}", family(), presentation_size(), weight());
  291. }
  292. const Font& BitmapFont::bold_variant() const
  293. {
  294. if (m_bold_variant)
  295. return *m_bold_variant;
  296. m_bold_variant = Gfx::FontDatabase::the().get(m_family, m_presentation_size, 700);
  297. if (!m_bold_variant)
  298. m_bold_variant = this;
  299. return *m_bold_variant;
  300. }
  301. }