Font.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include <SharedGraphics/Rect.h>
  3. #include <AK/Retainable.h>
  4. #include <AK/RetainPtr.h>
  5. #include <AK/AKString.h>
  6. #include <AK/Types.h>
  7. // FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
  8. class GlyphBitmap {
  9. friend class Font;
  10. public:
  11. const unsigned* rows() const { return m_rows; }
  12. unsigned row(unsigned index) const { return m_rows[index]; }
  13. bool bit_at(int x, int y) const { return row(y) & (1 << x); }
  14. void set_bit_at(int x, int y, bool b)
  15. {
  16. auto& mutable_row = const_cast<unsigned*>(m_rows)[y];
  17. if (b)
  18. mutable_row |= 1 << x;
  19. else
  20. mutable_row &= ~(1 << x);
  21. }
  22. Size size() const { return m_size; }
  23. int width() const { return m_size.width(); }
  24. int height() const { return m_size.height(); }
  25. private:
  26. GlyphBitmap(const unsigned* rows, Size size)
  27. : m_rows(rows)
  28. , m_size(size)
  29. {
  30. }
  31. const unsigned* m_rows { nullptr };
  32. Size m_size;
  33. };
  34. class Font : public Retainable<Font> {
  35. public:
  36. static Font& default_font();
  37. static Font& default_bold_font();
  38. RetainPtr<Font> clone() const;
  39. static RetainPtr<Font> load_from_memory(const byte*);
  40. static RetainPtr<Font> load_from_file(const String& path);
  41. bool write_to_file(const String& path);
  42. ~Font();
  43. GlyphBitmap glyph_bitmap(char ch) const { return GlyphBitmap(&m_rows[(byte)ch * m_glyph_height], { m_glyph_width, m_glyph_height }); }
  44. byte glyph_width() const { return m_glyph_width; }
  45. byte glyph_height() const { return m_glyph_height; }
  46. int width(const String& string) const { return string.length() * glyph_width(); }
  47. String name() const { return m_name; }
  48. void set_name(const String& name) { m_name = name; }
  49. private:
  50. Font(const String& name, unsigned* rows, byte glyph_width, byte glyph_height);
  51. String m_name;
  52. unsigned* m_rows { nullptr };
  53. void* m_mmap_ptr { nullptr };
  54. byte m_glyph_width { 0 };
  55. byte m_glyph_height { 0 };
  56. };