Font.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "Font.h"
  27. #include "Bitmap.h"
  28. #include "Emoji.h"
  29. #include <AK/BufferStream.h>
  30. #include <AK/MappedFile.h>
  31. #include <AK/StdLibExtras.h>
  32. #include <AK/Utf32View.h>
  33. #include <AK/Utf8View.h>
  34. #include <AK/kmalloc.h>
  35. #include <errno.h>
  36. #include <fcntl.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sys/mman.h>
  41. #include <unistd.h>
  42. namespace Gfx {
  43. struct [[gnu::packed]] FontFileHeader
  44. {
  45. char magic[4];
  46. u8 glyph_width;
  47. u8 glyph_height;
  48. u8 type;
  49. u8 is_variable_width;
  50. u8 glyph_spacing;
  51. u8 unused[5];
  52. char name[64];
  53. };
  54. Font& Font::default_font()
  55. {
  56. static Font* s_default_font;
  57. static const char* default_font_path = "/res/fonts/Katica10.font";
  58. if (!s_default_font) {
  59. s_default_font = Font::load_from_file(default_font_path).leak_ref();
  60. ASSERT(s_default_font);
  61. }
  62. return *s_default_font;
  63. }
  64. Font& Font::default_fixed_width_font()
  65. {
  66. static Font* s_default_fixed_width_font;
  67. static const char* default_fixed_width_font_path = "/res/fonts/CsillaThin7x10.font";
  68. if (!s_default_fixed_width_font) {
  69. s_default_fixed_width_font = Font::load_from_file(default_fixed_width_font_path).leak_ref();
  70. ASSERT(s_default_fixed_width_font);
  71. }
  72. return *s_default_fixed_width_font;
  73. }
  74. Font& Font::default_bold_fixed_width_font()
  75. {
  76. static Font* font;
  77. static const char* default_bold_fixed_width_font_path = "/res/fonts/CsillaBold7x10.font";
  78. if (!font) {
  79. font = Font::load_from_file(default_bold_fixed_width_font_path).leak_ref();
  80. ASSERT(font);
  81. }
  82. return *font;
  83. }
  84. Font& Font::default_bold_font()
  85. {
  86. static Font* s_default_bold_font;
  87. static const char* default_bold_font_path = "/res/fonts/KaticaBold10.font";
  88. if (!s_default_bold_font) {
  89. s_default_bold_font = Font::load_from_file(default_bold_font_path).leak_ref();
  90. ASSERT(s_default_bold_font);
  91. }
  92. return *s_default_bold_font;
  93. }
  94. NonnullRefPtr<Font> Font::clone() const
  95. {
  96. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  97. // FIXME: This is leaked!
  98. auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * m_glyph_count));
  99. memcpy(new_rows, m_rows, bytes_per_glyph * m_glyph_count);
  100. auto* new_widths = static_cast<u8*>(kmalloc(m_glyph_count));
  101. if (m_glyph_widths)
  102. memcpy(new_widths, m_glyph_widths, m_glyph_count);
  103. else
  104. memset(new_widths, m_glyph_width, m_glyph_count);
  105. return adopt(*new Font(m_name, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, m_type));
  106. }
  107. NonnullRefPtr<Font> Font::create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type)
  108. {
  109. size_t bytes_per_glyph = sizeof(u32) * glyph_height;
  110. // FIXME: This is leaked!
  111. size_t count = glyph_count_by_type(type);
  112. auto* new_rows = static_cast<unsigned*>(malloc(bytes_per_glyph * count));
  113. memset(new_rows, 0, bytes_per_glyph * count);
  114. auto* new_widths = static_cast<u8*>(malloc(count));
  115. memset(new_widths, glyph_width, count);
  116. return adopt(*new Font("Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, type));
  117. }
  118. Font::Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type)
  119. : m_name(name)
  120. , m_type(type)
  121. , m_rows(rows)
  122. , m_glyph_widths(widths)
  123. , m_glyph_width(glyph_width)
  124. , m_glyph_height(glyph_height)
  125. , m_min_glyph_width(glyph_width)
  126. , m_max_glyph_width(glyph_width)
  127. , m_glyph_spacing(glyph_spacing)
  128. , m_fixed_width(is_fixed_width)
  129. {
  130. m_glyph_count = glyph_count_by_type(m_type);
  131. if (!m_fixed_width) {
  132. u8 maximum = 0;
  133. u8 minimum = 255;
  134. for (size_t i = 0; i < m_glyph_count; ++i) {
  135. minimum = min(minimum, m_glyph_widths[i]);
  136. maximum = max(maximum, m_glyph_widths[i]);
  137. }
  138. m_min_glyph_width = minimum;
  139. m_max_glyph_width = maximum;
  140. }
  141. }
  142. Font::~Font()
  143. {
  144. }
  145. RefPtr<Font> Font::load_from_memory(const u8* data)
  146. {
  147. auto& header = *reinterpret_cast<const FontFileHeader*>(data);
  148. if (memcmp(header.magic, "!Fnt", 4)) {
  149. dbgprintf("header.magic != '!Fnt', instead it's '%c%c%c%c'\n", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
  150. return nullptr;
  151. }
  152. if (header.name[63] != '\0') {
  153. dbgprintf("Font name not fully null-terminated\n");
  154. return nullptr;
  155. }
  156. FontTypes type;
  157. if (header.type == 0)
  158. type = FontTypes::Default;
  159. else if (header.type == 1)
  160. type = FontTypes::LatinExtendedA;
  161. else
  162. ASSERT_NOT_REACHED();
  163. size_t count = glyph_count_by_type(type);
  164. size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
  165. auto* rows = const_cast<unsigned*>((const unsigned*)(data + sizeof(FontFileHeader)));
  166. u8* widths = nullptr;
  167. if (header.is_variable_width)
  168. widths = (u8*)(rows) + count * bytes_per_glyph;
  169. return adopt(*new Font(String(header.name), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height, header.glyph_spacing, type));
  170. }
  171. size_t Font::glyph_count_by_type(FontTypes type)
  172. {
  173. if (type == FontTypes::Default)
  174. return 256;
  175. if (type == FontTypes::LatinExtendedA)
  176. return 384;
  177. dbg() << "Unknown font type:" << type;
  178. ASSERT_NOT_REACHED();
  179. }
  180. RefPtr<Font> Font::load_from_file(const StringView& path)
  181. {
  182. MappedFile mapped_file(path);
  183. if (!mapped_file.is_valid())
  184. return nullptr;
  185. auto font = load_from_memory((const u8*)mapped_file.data());
  186. if (!font)
  187. return nullptr;
  188. font->m_mapped_file = move(mapped_file);
  189. return font;
  190. }
  191. bool Font::write_to_file(const StringView& path)
  192. {
  193. int fd;
  194. #ifdef __serenity__
  195. fd = creat_with_path_length(path.characters_without_null_termination(), path.length(), 0644);
  196. #else
  197. {
  198. String null_terminated_path = path;
  199. fd = creat(null_terminated_path.characters(), 0644);
  200. }
  201. #endif
  202. if (fd < 0) {
  203. perror("open");
  204. return false;
  205. }
  206. FontFileHeader header;
  207. memset(&header, 0, sizeof(FontFileHeader));
  208. memcpy(header.magic, "!Fnt", 4);
  209. header.glyph_width = m_glyph_width;
  210. header.glyph_height = m_glyph_height;
  211. header.type = m_type;
  212. header.is_variable_width = !m_fixed_width;
  213. header.glyph_spacing = m_glyph_spacing;
  214. memcpy(header.name, m_name.characters(), min(m_name.length(), (size_t)63));
  215. size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
  216. size_t count = glyph_count_by_type(m_type);
  217. auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (count * bytes_per_glyph) + count);
  218. BufferStream stream(buffer);
  219. stream << ByteBuffer::wrap(&header, sizeof(FontFileHeader));
  220. stream << ByteBuffer::wrap(m_rows, (count * bytes_per_glyph));
  221. stream << ByteBuffer::wrap(m_glyph_widths, count);
  222. ASSERT(stream.at_end());
  223. ssize_t nwritten = write(fd, buffer.data(), buffer.size());
  224. ASSERT(nwritten == (ssize_t)buffer.size());
  225. int rc = close(fd);
  226. ASSERT(rc == 0);
  227. return true;
  228. }
  229. GlyphBitmap Font::glyph_bitmap(u32 codepoint) const
  230. {
  231. return GlyphBitmap(&m_rows[codepoint * m_glyph_height], { glyph_width(codepoint), m_glyph_height });
  232. }
  233. int Font::glyph_or_emoji_width(u32 codepoint) const
  234. {
  235. if (codepoint < m_glyph_count)
  236. return glyph_width(codepoint);
  237. if (m_fixed_width)
  238. return m_glyph_width;
  239. auto* emoji = Emoji::emoji_for_codepoint(codepoint);
  240. if (emoji == nullptr)
  241. return glyph_width('?');
  242. return emoji->size().width();
  243. }
  244. int Font::width(const StringView& string) const
  245. {
  246. Utf8View utf8 { string };
  247. return width(utf8);
  248. }
  249. int Font::width(const Utf8View& utf8) const
  250. {
  251. bool first = true;
  252. int width = 0;
  253. for (u32 codepoint : utf8) {
  254. if (!first)
  255. width += glyph_spacing();
  256. first = false;
  257. width += glyph_or_emoji_width(codepoint);
  258. }
  259. return width;
  260. }
  261. int Font::width(const Utf32View& view) const
  262. {
  263. if (view.length() == 0)
  264. return 0;
  265. int width = (view.length() - 1) * glyph_spacing();
  266. for (size_t i = 0; i < view.length(); ++i)
  267. width += glyph_or_emoji_width(view.codepoints()[i]);
  268. return width;
  269. }
  270. void Font::set_type(FontTypes type)
  271. {
  272. if (type == m_type)
  273. return;
  274. if (type == FontTypes::Default)
  275. return;
  276. size_t new_glyph_count = glyph_count_by_type(type);
  277. if (new_glyph_count <= m_glyph_count) {
  278. m_glyph_count = new_glyph_count;
  279. return;
  280. }
  281. int item_count_to_copy = min(m_glyph_count, new_glyph_count);
  282. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  283. auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * new_glyph_count));
  284. memset(new_rows, (unsigned)0, bytes_per_glyph * new_glyph_count);
  285. memcpy(new_rows, m_rows, bytes_per_glyph * item_count_to_copy);
  286. auto* new_widths = static_cast<u8*>(kmalloc(new_glyph_count));
  287. memset(new_widths, (u8)0, new_glyph_count);
  288. memcpy(new_widths, m_glyph_widths, item_count_to_copy);
  289. kfree(m_rows);
  290. kfree(m_glyph_widths);
  291. m_type = type;
  292. m_glyph_count = new_glyph_count;
  293. m_rows = new_rows;
  294. m_glyph_widths = new_widths;
  295. }
  296. }