Font.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "Font.h"
  2. #include <AK/kmalloc.h>
  3. #include <AK/BufferStream.h>
  4. #include <AK/StdLibExtras.h>
  5. #include <LibC/unistd.h>
  6. #include <LibC/stdio.h>
  7. #include <LibC/fcntl.h>
  8. #include <LibC/errno.h>
  9. #include <LibC/mman.h>
  10. struct [[gnu::packed]] FontFileHeader {
  11. char magic[4];
  12. byte glyph_width;
  13. byte glyph_height;
  14. byte type;
  15. byte is_variable_width;
  16. byte unused[6];
  17. char name[64];
  18. };
  19. Font& Font::default_font()
  20. {
  21. static Font* s_default_font;
  22. static const char* default_font_path = "/res/fonts/Katica10.font";
  23. if (!s_default_font) {
  24. s_default_font = Font::load_from_file(default_font_path).leak_ref();
  25. ASSERT(s_default_font);
  26. }
  27. return *s_default_font;
  28. }
  29. Font& Font::default_fixed_width_font()
  30. {
  31. static Font* s_default_fixed_width_font;
  32. static const char* default_fixed_width_font_path = "/res/fonts/CsillaThin7x10.font";
  33. if (!s_default_fixed_width_font) {
  34. s_default_fixed_width_font = Font::load_from_file(default_fixed_width_font_path).leak_ref();
  35. ASSERT(s_default_fixed_width_font);
  36. }
  37. return *s_default_fixed_width_font;
  38. }
  39. Font& Font::default_bold_font()
  40. {
  41. static Font* s_default_bold_font;
  42. static const char* default_bold_font_path = "/res/fonts/KaticaBold10.font";
  43. if (!s_default_bold_font) {
  44. s_default_bold_font = Font::load_from_file(default_bold_font_path).leak_ref();
  45. ASSERT(s_default_bold_font);
  46. }
  47. return *s_default_bold_font;
  48. }
  49. RetainPtr<Font> Font::clone() const
  50. {
  51. size_t bytes_per_glyph = sizeof(dword) * glyph_height();
  52. // FIXME: This is leaked!
  53. auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * 256));
  54. memcpy(new_rows, m_rows, bytes_per_glyph * 256);
  55. auto* new_widths = static_cast<byte*>(kmalloc(256));
  56. if (m_glyph_widths)
  57. memcpy(new_widths, m_glyph_widths, 256);
  58. else
  59. memset(new_widths, m_glyph_width, 256);
  60. return adopt(*new Font(m_name, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height));
  61. }
  62. Font::Font(const String& name, unsigned* rows, byte* widths, bool is_fixed_width, byte glyph_width, byte glyph_height)
  63. : m_name(name)
  64. , m_rows(rows)
  65. , m_glyph_widths(widths)
  66. , m_glyph_width(glyph_width)
  67. , m_glyph_height(glyph_height)
  68. , m_min_glyph_width(glyph_width)
  69. , m_max_glyph_width(glyph_width)
  70. , m_fixed_width(is_fixed_width)
  71. {
  72. if (!m_fixed_width) {
  73. byte maximum = 0;
  74. byte minimum = 255;
  75. for (int i = 0; i < 256; ++i) {
  76. minimum = min(minimum, m_glyph_widths[i]);
  77. maximum = max(maximum, m_glyph_widths[i]);
  78. }
  79. m_min_glyph_width = minimum;
  80. m_max_glyph_width = maximum;
  81. }
  82. }
  83. Font::~Font()
  84. {
  85. if (m_mmap_ptr) {
  86. int rc = munmap(m_mmap_ptr, 4096 * 3);
  87. ASSERT(rc == 0);
  88. }
  89. }
  90. RetainPtr<Font> Font::load_from_memory(const byte* data)
  91. {
  92. auto& header = *reinterpret_cast<const FontFileHeader*>(data);
  93. if (memcmp(header.magic, "!Fnt", 4)) {
  94. dbgprintf("header.magic != '!Fnt', instead it's '%c%c%c%c'\n", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
  95. return nullptr;
  96. }
  97. if (header.name[63] != '\0') {
  98. dbgprintf("Font name not fully null-terminated\n");
  99. return nullptr;
  100. }
  101. size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
  102. auto* rows = (unsigned*)(data + sizeof(FontFileHeader));
  103. byte* widths = nullptr;
  104. if (header.is_variable_width)
  105. widths = (byte*)(rows) + 256 * bytes_per_glyph;
  106. return adopt(*new Font(String(header.name), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height));
  107. }
  108. RetainPtr<Font> Font::load_from_file(const String& path)
  109. {
  110. int fd = open(path.characters(), O_RDONLY, 0644);
  111. if (fd < 0) {
  112. dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
  113. perror("open");
  114. return nullptr;
  115. }
  116. auto* mapped_file = (byte*)mmap(nullptr, 4096 * 3, PROT_READ, MAP_SHARED, fd, 0);
  117. if (mapped_file == MAP_FAILED) {
  118. int rc = close(fd);
  119. ASSERT(rc == 0);
  120. return nullptr;
  121. }
  122. auto font = load_from_memory(mapped_file);
  123. font->m_mmap_ptr = mapped_file;
  124. int rc = close(fd);
  125. ASSERT(rc == 0);
  126. return font;
  127. }
  128. bool Font::write_to_file(const String& path)
  129. {
  130. int fd = open(path.characters(), O_WRONLY | O_CREAT, 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.pointer(), 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::width(const String& string) const
  157. {
  158. if (m_fixed_width)
  159. return string.length() * m_glyph_width;
  160. int width = 0;
  161. for (int i = 0; i < string.length(); ++i)
  162. width += glyph_width(string[i]) + 1;
  163. return width;
  164. }