Font.cpp 5.3 KB

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