Font.cpp 6.0 KB

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