Font.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "Font.h"
  2. #include <AK/BufferStream.h>
  3. #include <AK/MappedFile.h>
  4. #include <AK/StdLibExtras.h>
  5. #include <AK/kmalloc.h>
  6. #include <LibC/errno.h>
  7. #include <LibC/fcntl.h>
  8. #include <LibC/mman.h>
  9. #include <LibC/stdio.h>
  10. #include <LibC/unistd.h>
  11. #include <AK/Utf8View.h>
  12. #include "Emoji.h"
  13. #include "GraphicsBitmap.h"
  14. struct [[gnu::packed]] FontFileHeader
  15. {
  16. char magic[4];
  17. u8 glyph_width;
  18. u8 glyph_height;
  19. u8 type;
  20. u8 is_variable_width;
  21. u8 glyph_spacing;
  22. u8 unused[5];
  23. char name[64];
  24. };
  25. Font& Font::default_font()
  26. {
  27. static Font* s_default_font;
  28. static const char* default_font_path = "/res/fonts/Katica10.font";
  29. if (!s_default_font) {
  30. s_default_font = Font::load_from_file(default_font_path).leak_ref();
  31. ASSERT(s_default_font);
  32. }
  33. return *s_default_font;
  34. }
  35. Font& Font::default_fixed_width_font()
  36. {
  37. static Font* s_default_fixed_width_font;
  38. static const char* default_fixed_width_font_path = "/res/fonts/CsillaThin7x10.font";
  39. if (!s_default_fixed_width_font) {
  40. s_default_fixed_width_font = Font::load_from_file(default_fixed_width_font_path).leak_ref();
  41. ASSERT(s_default_fixed_width_font);
  42. }
  43. return *s_default_fixed_width_font;
  44. }
  45. Font& Font::default_bold_fixed_width_font()
  46. {
  47. static Font* font;
  48. static const char* default_bold_fixed_width_font_path = "/res/fonts/CsillaBold7x10.font";
  49. if (!font) {
  50. font = Font::load_from_file(default_bold_fixed_width_font_path).leak_ref();
  51. ASSERT(font);
  52. }
  53. return *font;
  54. }
  55. Font& Font::default_bold_font()
  56. {
  57. static Font* s_default_bold_font;
  58. static const char* default_bold_font_path = "/res/fonts/KaticaBold10.font";
  59. if (!s_default_bold_font) {
  60. s_default_bold_font = Font::load_from_file(default_bold_font_path).leak_ref();
  61. ASSERT(s_default_bold_font);
  62. }
  63. return *s_default_bold_font;
  64. }
  65. RefPtr<Font> Font::clone() const
  66. {
  67. size_t bytes_per_glyph = sizeof(u32) * glyph_height();
  68. // FIXME: This is leaked!
  69. auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * 256));
  70. memcpy(new_rows, m_rows, bytes_per_glyph * 256);
  71. auto* new_widths = static_cast<u8*>(kmalloc(256));
  72. if (m_glyph_widths)
  73. memcpy(new_widths, m_glyph_widths, 256);
  74. else
  75. memset(new_widths, m_glyph_width, 256);
  76. return adopt(*new Font(m_name, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing));
  77. }
  78. Font::Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing)
  79. : m_name(name)
  80. , m_rows(rows)
  81. , m_glyph_widths(widths)
  82. , m_glyph_width(glyph_width)
  83. , m_glyph_height(glyph_height)
  84. , m_min_glyph_width(glyph_width)
  85. , m_max_glyph_width(glyph_width)
  86. , m_glyph_spacing(glyph_spacing)
  87. , m_fixed_width(is_fixed_width)
  88. {
  89. if (!m_fixed_width) {
  90. u8 maximum = 0;
  91. u8 minimum = 255;
  92. for (int i = 0; i < 256; ++i) {
  93. minimum = min(minimum, m_glyph_widths[i]);
  94. maximum = max(maximum, m_glyph_widths[i]);
  95. }
  96. m_min_glyph_width = minimum;
  97. m_max_glyph_width = maximum;
  98. }
  99. }
  100. Font::~Font()
  101. {
  102. }
  103. RefPtr<Font> Font::load_from_memory(const u8* data)
  104. {
  105. auto& header = *reinterpret_cast<const FontFileHeader*>(data);
  106. if (memcmp(header.magic, "!Fnt", 4)) {
  107. dbgprintf("header.magic != '!Fnt', instead it's '%c%c%c%c'\n", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
  108. return nullptr;
  109. }
  110. if (header.name[63] != '\0') {
  111. dbgprintf("Font name not fully null-terminated\n");
  112. return nullptr;
  113. }
  114. size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
  115. auto* rows = const_cast<unsigned*>((const unsigned*)(data + sizeof(FontFileHeader)));
  116. u8* widths = nullptr;
  117. if (header.is_variable_width)
  118. widths = (u8*)(rows) + 256 * bytes_per_glyph;
  119. return adopt(*new Font(String(header.name), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height, header.glyph_spacing));
  120. }
  121. RefPtr<Font> Font::load_from_file(const StringView& path)
  122. {
  123. MappedFile mapped_file(path);
  124. if (!mapped_file.is_valid())
  125. return nullptr;
  126. auto font = load_from_memory((const u8*)mapped_file.data());
  127. font->m_mapped_file = move(mapped_file);
  128. return font;
  129. }
  130. bool Font::write_to_file(const StringView& path)
  131. {
  132. int fd = creat_with_path_length(path.characters_without_null_termination(), path.length(), 0644);
  133. if (fd < 0) {
  134. perror("open");
  135. return false;
  136. }
  137. FontFileHeader header;
  138. memset(&header, 0, sizeof(FontFileHeader));
  139. memcpy(header.magic, "!Fnt", 4);
  140. header.glyph_width = m_glyph_width;
  141. header.glyph_height = m_glyph_height;
  142. header.type = 0;
  143. header.is_variable_width = !m_fixed_width;
  144. header.glyph_spacing = m_glyph_spacing;
  145. memcpy(header.name, m_name.characters(), min(m_name.length(), (size_t)63));
  146. size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
  147. auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (256 * bytes_per_glyph) + 256);
  148. BufferStream stream(buffer);
  149. stream << ByteBuffer::wrap(&header, sizeof(FontFileHeader));
  150. stream << ByteBuffer::wrap(m_rows, (256 * bytes_per_glyph));
  151. stream << ByteBuffer::wrap(m_glyph_widths, 256);
  152. ASSERT(stream.at_end());
  153. ssize_t nwritten = write(fd, buffer.data(), buffer.size());
  154. ASSERT(nwritten == (ssize_t)buffer.size());
  155. int rc = close(fd);
  156. ASSERT(rc == 0);
  157. return true;
  158. }
  159. int Font::glyph_or_emoji_width(u32 codepoint) const
  160. {
  161. if (codepoint < 256)
  162. return glyph_width((char)codepoint);
  163. if (m_fixed_width)
  164. return m_glyph_width;
  165. auto* emoji = Emoji::emoji_for_codepoint(codepoint);
  166. if (emoji == nullptr)
  167. return glyph_width('?');
  168. return emoji->size().width();
  169. }
  170. int Font::width(const StringView& string) const
  171. {
  172. Utf8View utf8 { string };
  173. return width(utf8);
  174. }
  175. int Font::width(const Utf8View& utf8) const
  176. {
  177. bool first = true;
  178. int width = 0;
  179. for (u32 codepoint : utf8) {
  180. if (!first)
  181. width += glyph_spacing();
  182. first = false;
  183. width += glyph_or_emoji_width(codepoint);
  184. }
  185. return width;
  186. }