Font.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "Font.h"
  2. #include <AK/kmalloc.h>
  3. #include <AK/BufferStream.h>
  4. #include <AK/StdLibExtras.h>
  5. #ifdef KERNEL
  6. #include <Kernel/FileDescriptor.h>
  7. #include <Kernel/VirtualFileSystem.h>
  8. #endif
  9. #ifdef USERLAND
  10. #include <LibC/unistd.h>
  11. #include <LibC/stdio.h>
  12. #include <LibC/fcntl.h>
  13. #include <LibC/errno.h>
  14. #include <LibC/mman.h>
  15. #endif
  16. static const byte error_glyph_width = 8;
  17. static const byte error_glyph_height = 10;
  18. static constexpr const char* error_glyph {
  19. " #### "
  20. " # # "
  21. " # # "
  22. " # ## # "
  23. " # ## # "
  24. " #### "
  25. " ## "
  26. " ###### "
  27. " ## "
  28. " ## ",
  29. };
  30. static Font* s_default_font;
  31. void Font::initialize()
  32. {
  33. s_default_font = nullptr;
  34. }
  35. Font& Font::default_font()
  36. {
  37. static const char* default_font_path = "/res/fonts/Liza8x10.font";
  38. if (!s_default_font) {
  39. #ifdef USERLAND
  40. s_default_font = Font::load_from_file(default_font_path).leak_ref();
  41. ASSERT(s_default_font);
  42. #else
  43. int error;
  44. auto descriptor = VFS::the().open(default_font_path, error, 0, 0, *VFS::the().root_inode());
  45. if (!descriptor) {
  46. kprintf("Failed to open default font (%s)\n", default_font_path);
  47. ASSERT_NOT_REACHED();
  48. }
  49. auto buffer = descriptor->read_entire_file(*current);
  50. ASSERT(buffer);
  51. s_default_font = Font::load_from_memory(buffer.pointer()).leak_ref();
  52. #endif
  53. }
  54. return *s_default_font;
  55. }
  56. RetainPtr<Font> Font::clone() const
  57. {
  58. size_t bytes_per_glyph = glyph_width() * glyph_height();
  59. // FIXME: This is leaked!
  60. char** new_glyphs = static_cast<char**>(kmalloc(sizeof(char*) * 256));
  61. for (unsigned i = 0; i < 256; ++i) {
  62. new_glyphs[i] = static_cast<char*>(kmalloc(bytes_per_glyph));
  63. if (i >= m_first_glyph && i <= m_last_glyph) {
  64. memcpy(new_glyphs[i], m_glyphs[i - m_first_glyph], bytes_per_glyph);
  65. } else {
  66. memset(new_glyphs[i], ' ', bytes_per_glyph);
  67. }
  68. }
  69. return adopt(*new Font(m_name, new_glyphs, m_glyph_width, m_glyph_height, 0, 255));
  70. }
  71. Font::Font(const String& name, const char* const* glyphs, byte glyph_width, byte glyph_height, byte first_glyph, byte last_glyph)
  72. : m_name(name)
  73. , m_glyphs(glyphs)
  74. , m_glyph_width(glyph_width)
  75. , m_glyph_height(glyph_height)
  76. , m_first_glyph(first_glyph)
  77. , m_last_glyph(last_glyph)
  78. {
  79. ASSERT(m_glyph_width == error_glyph_width);
  80. ASSERT(m_glyph_height == error_glyph_height);
  81. m_error_bitmap = CharacterBitmap::create_from_ascii(error_glyph, error_glyph_width, error_glyph_height);
  82. for (unsigned ch = 0; ch < 256; ++ch) {
  83. if (ch < m_first_glyph || ch > m_last_glyph) {
  84. m_bitmaps[ch] = m_error_bitmap.copy_ref();
  85. continue;
  86. }
  87. const char* data = m_glyphs[(unsigned)ch - m_first_glyph];
  88. m_bitmaps[ch] = CharacterBitmap::create_from_ascii(data, m_glyph_width, m_glyph_height);
  89. }
  90. }
  91. Font::~Font()
  92. {
  93. }
  94. struct FontFileHeader {
  95. char magic[4];
  96. byte glyph_width;
  97. byte glyph_height;
  98. byte type;
  99. byte unused[7];
  100. char name[64];
  101. } PACKED;
  102. RetainPtr<Font> Font::load_from_memory(const byte* data)
  103. {
  104. auto& header = *reinterpret_cast<const FontFileHeader*>(data);
  105. if (memcmp(header.magic, "!Fnt", 4)) {
  106. dbgprintf("header.magic != '!Fnt', instead it's '%c%c%c%c'\n", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
  107. return nullptr;
  108. }
  109. if (header.name[63] != '\0') {
  110. dbgprintf("Font name not fully null-terminated\n");
  111. return nullptr;
  112. }
  113. auto* glyphs_ptr = reinterpret_cast<const unsigned*>(data + sizeof(FontFileHeader));
  114. char** new_glyphs = static_cast<char**>(kmalloc(sizeof(char*) * 256));
  115. for (unsigned glyph_index = 0; glyph_index < 256; ++glyph_index) {
  116. new_glyphs[glyph_index] = static_cast<char*>(kmalloc(header.glyph_width * header.glyph_height));
  117. char* bitptr = new_glyphs[glyph_index];
  118. for (unsigned y = 0; y < header.glyph_height; ++y) {
  119. unsigned pattern = *(glyphs_ptr++);
  120. for (unsigned x = 0; x < header.glyph_width; ++x) {
  121. if (pattern & (1u << x)) {
  122. *(bitptr++) = '#';
  123. } else {
  124. *(bitptr++) = ' ';
  125. }
  126. }
  127. }
  128. }
  129. return adopt(*new Font(String(header.name), new_glyphs, header.glyph_width, header.glyph_height, 0, 255));
  130. }
  131. #ifdef USERLAND
  132. RetainPtr<Font> Font::load_from_file(const String& path)
  133. {
  134. int fd = open(path.characters(), O_RDONLY, 0644);
  135. if (fd < 0) {
  136. dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
  137. perror("open");
  138. return nullptr;
  139. }
  140. auto* mapped_file = (byte*)mmap(nullptr, 4096 * 3, PROT_READ, MAP_SHARED, fd, 0);
  141. if (mapped_file == MAP_FAILED) {
  142. int rc = close(fd);
  143. ASSERT(rc == 0);
  144. return nullptr;
  145. }
  146. auto font = load_from_memory(mapped_file);
  147. int rc = munmap(mapped_file, 4096 * 3);
  148. ASSERT(rc == 0);
  149. rc = close(fd);
  150. ASSERT(rc == 0);
  151. return font;
  152. }
  153. bool Font::write_to_file(const String& path)
  154. {
  155. int fd = open(path.characters(), O_WRONLY | O_CREAT, 0644);
  156. if (fd < 0) {
  157. perror("open");
  158. return false;
  159. }
  160. FontFileHeader header;
  161. memset(&header, 0, sizeof(FontFileHeader));
  162. memcpy(header.magic, "!Fnt", 4);
  163. header.glyph_width = m_glyph_width;
  164. header.glyph_height = m_glyph_height;
  165. header.type = 0;
  166. memcpy(header.name, m_name.characters(), min(m_name.length(), 63u));
  167. size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
  168. auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (256 * bytes_per_glyph));
  169. BufferStream stream(buffer);
  170. stream << ByteBuffer::wrap((byte*)&header, sizeof(FontFileHeader));
  171. for (unsigned glyph_index = 0; glyph_index < 256; ++glyph_index) {
  172. auto* glyph_bits = (byte*)m_glyphs[glyph_index];
  173. for (unsigned y = 0; y < m_glyph_height; ++y) {
  174. unsigned pattern = 0;
  175. for (unsigned x = 0; x < m_glyph_width; ++x) {
  176. if (glyph_bits[y * m_glyph_width + x] == '#') {
  177. pattern |= 1 << x;
  178. }
  179. }
  180. stream << pattern;
  181. }
  182. }
  183. ASSERT(stream.at_end());
  184. ssize_t nwritten = write(fd, buffer.pointer(), buffer.size());
  185. ASSERT(nwritten == (ssize_t)buffer.size());
  186. int rc = close(fd);
  187. ASSERT(rc == 0);
  188. return true;
  189. }
  190. #endif