Font.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include <AK/String.h>
  3. #include <AK/MappedFile.h>
  4. #include <AK/RefPtr.h>
  5. #include <AK/RefCounted.h>
  6. #include <AK/Types.h>
  7. #include <LibDraw/Rect.h>
  8. #include <AK/Utf8View.h>
  9. // FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
  10. class GlyphBitmap {
  11. friend class Font;
  12. public:
  13. const unsigned* rows() const { return m_rows; }
  14. unsigned row(unsigned index) const { return m_rows[index]; }
  15. bool bit_at(int x, int y) const { return row(y) & (1 << x); }
  16. void set_bit_at(int x, int y, bool b)
  17. {
  18. auto& mutable_row = const_cast<unsigned*>(m_rows)[y];
  19. if (b)
  20. mutable_row |= 1 << x;
  21. else
  22. mutable_row &= ~(1 << x);
  23. }
  24. Size size() const { return m_size; }
  25. int width() const { return m_size.width(); }
  26. int height() const { return m_size.height(); }
  27. private:
  28. GlyphBitmap(const unsigned* rows, Size size)
  29. : m_rows(rows)
  30. , m_size(size)
  31. {
  32. }
  33. const unsigned* m_rows { nullptr };
  34. Size m_size;
  35. };
  36. class Font : public RefCounted<Font> {
  37. public:
  38. static Font& default_font();
  39. static Font& default_bold_font();
  40. static Font& default_fixed_width_font();
  41. static Font& default_bold_fixed_width_font();
  42. RefPtr<Font> clone() const;
  43. static RefPtr<Font> load_from_file(const StringView& path);
  44. bool write_to_file(const StringView& path);
  45. ~Font();
  46. GlyphBitmap glyph_bitmap(char ch) const { return GlyphBitmap(&m_rows[(u8)ch * m_glyph_height], { glyph_width(ch), m_glyph_height }); }
  47. u8 glyph_width(char ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[(u8)ch]; }
  48. int glyph_or_emoji_width(u32 codepoint) const;
  49. u8 glyph_height() const { return m_glyph_height; }
  50. u8 min_glyph_width() const { return m_min_glyph_width; }
  51. u8 max_glyph_width() const { return m_max_glyph_width; }
  52. int width(const StringView&) const;
  53. int width(const Utf8View&) const;
  54. String name() const { return m_name; }
  55. void set_name(const StringView& name) { m_name = name; }
  56. bool is_fixed_width() const { return m_fixed_width; }
  57. void set_fixed_width(bool b) { m_fixed_width = b; }
  58. u8 glyph_spacing() const { return m_glyph_spacing; }
  59. void set_glyph_spacing(u8 spacing) { m_glyph_spacing = spacing; }
  60. void set_glyph_width(char ch, u8 width)
  61. {
  62. ASSERT(m_glyph_widths);
  63. m_glyph_widths[(u8)ch] = width;
  64. }
  65. private:
  66. Font(const StringView& name, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing);
  67. static RefPtr<Font> load_from_memory(const u8*);
  68. String m_name;
  69. unsigned* m_rows { nullptr };
  70. u8* m_glyph_widths { nullptr };
  71. MappedFile m_mapped_file;
  72. u8 m_glyph_width { 0 };
  73. u8 m_glyph_height { 0 };
  74. u8 m_min_glyph_width { 0 };
  75. u8 m_max_glyph_width { 0 };
  76. u8 m_glyph_spacing { 0 };
  77. bool m_fixed_width { false };
  78. };