Font.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. static Font* s_default_font;
  11. static Font* s_default_bold_font;
  12. struct [[gnu::packed]] FontFileHeader {
  13. char magic[4];
  14. byte glyph_width;
  15. byte glyph_height;
  16. byte type;
  17. byte unused[7];
  18. char name[64];
  19. };
  20. Font& Font::default_font()
  21. {
  22. static const char* default_font_path = "/res/fonts/LizaRegular8x10.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_bold_font()
  30. {
  31. static const char* default_bold_font_path = "/res/fonts/LizaBold8x10.font";
  32. if (!s_default_bold_font) {
  33. s_default_bold_font = Font::load_from_file(default_bold_font_path).leak_ref();
  34. ASSERT(s_default_bold_font);
  35. }
  36. return *s_default_bold_font;
  37. }
  38. RetainPtr<Font> Font::clone() const
  39. {
  40. size_t bytes_per_glyph = sizeof(dword) * glyph_height();
  41. // FIXME: This is leaked!
  42. auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * 256));
  43. memcpy(new_rows, m_rows, bytes_per_glyph * 256);
  44. return adopt(*new Font(m_name, new_rows, m_glyph_width, m_glyph_height));
  45. }
  46. Font::Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_height)
  47. : m_name(name)
  48. , m_rows(rows)
  49. , m_glyph_width(glyph_width)
  50. , m_glyph_height(glyph_height)
  51. {
  52. }
  53. Font::~Font()
  54. {
  55. if (m_mmap_ptr) {
  56. int rc = munmap(m_mmap_ptr, 4096 * 3);
  57. ASSERT(rc == 0);
  58. }
  59. }
  60. RetainPtr<Font> Font::load_from_memory(const byte* data)
  61. {
  62. auto& header = *reinterpret_cast<const FontFileHeader*>(data);
  63. if (memcmp(header.magic, "!Fnt", 4)) {
  64. dbgprintf("header.magic != '!Fnt', instead it's '%c%c%c%c'\n", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
  65. return nullptr;
  66. }
  67. if (header.name[63] != '\0') {
  68. dbgprintf("Font name not fully null-terminated\n");
  69. return nullptr;
  70. }
  71. auto* rows = (unsigned*)(data + sizeof(FontFileHeader));
  72. return adopt(*new Font(String(header.name), rows, header.glyph_width, header.glyph_height));
  73. }
  74. RetainPtr<Font> Font::load_from_file(const String& path)
  75. {
  76. int fd = open(path.characters(), O_RDONLY, 0644);
  77. if (fd < 0) {
  78. dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
  79. perror("open");
  80. return nullptr;
  81. }
  82. auto* mapped_file = (byte*)mmap(nullptr, 4096 * 3, PROT_READ, MAP_SHARED, fd, 0);
  83. if (mapped_file == MAP_FAILED) {
  84. int rc = close(fd);
  85. ASSERT(rc == 0);
  86. return nullptr;
  87. }
  88. auto font = load_from_memory(mapped_file);
  89. font->m_mmap_ptr = mapped_file;
  90. int rc = close(fd);
  91. ASSERT(rc == 0);
  92. return font;
  93. }
  94. bool Font::write_to_file(const String& path)
  95. {
  96. int fd = open(path.characters(), O_WRONLY | O_CREAT, 0644);
  97. if (fd < 0) {
  98. perror("open");
  99. return false;
  100. }
  101. FontFileHeader header;
  102. memset(&header, 0, sizeof(FontFileHeader));
  103. memcpy(header.magic, "!Fnt", 4);
  104. header.glyph_width = m_glyph_width;
  105. header.glyph_height = m_glyph_height;
  106. header.type = 0;
  107. memcpy(header.name, m_name.characters(), min(m_name.length(), (size_t)63));
  108. size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
  109. auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (256 * bytes_per_glyph));
  110. BufferStream stream(buffer);
  111. stream << ByteBuffer::wrap((byte*)&header, sizeof(FontFileHeader));
  112. stream << ByteBuffer::wrap((byte*)m_rows, (256 * bytes_per_glyph));
  113. ASSERT(stream.at_end());
  114. ssize_t nwritten = write(fd, buffer.pointer(), buffer.size());
  115. ASSERT(nwritten == (ssize_t)buffer.size());
  116. int rc = close(fd);
  117. ASSERT(rc == 0);
  118. return true;
  119. }