ScaledFont.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022, the SerenityOS developers.
  3. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <LibGfx/Bitmap.h>
  10. #include <LibGfx/Font/Font.h>
  11. #include <LibGfx/Font/VectorFont.h>
  12. #define POINTS_PER_INCH 72.0f
  13. #define DEFAULT_DPI 96
  14. namespace Gfx {
  15. struct GlyphIndexWithSubpixelOffset {
  16. u32 glyph_id;
  17. GlyphSubpixelOffset subpixel_offset;
  18. bool operator==(GlyphIndexWithSubpixelOffset const&) const = default;
  19. };
  20. class ScaledFont final : public Gfx::Font {
  21. public:
  22. ScaledFont(NonnullRefPtr<VectorFont>, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI);
  23. u32 glyph_id_for_code_point(u32 code_point) const { return m_font->glyph_id_for_code_point(code_point); }
  24. ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); }
  25. ScaledGlyphMetrics glyph_metrics(u32 glyph_id) const { return m_font->glyph_metrics(glyph_id, m_x_scale, m_y_scale, m_point_width, m_point_height); }
  26. RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, GlyphSubpixelOffset) const;
  27. // ^Gfx::Font
  28. virtual NonnullRefPtr<Font> clone() const override { return MUST(try_clone()); } // FIXME: clone() should not need to be implemented
  29. virtual ErrorOr<NonnullRefPtr<Font>> try_clone() const override { return const_cast<ScaledFont&>(*this); }
  30. virtual u8 presentation_size() const override { return m_point_height; }
  31. virtual float point_size() const override;
  32. virtual float pixel_size() const override;
  33. virtual int pixel_size_rounded_up() const override;
  34. virtual Gfx::FontPixelMetrics pixel_metrics() const override;
  35. virtual u8 slope() const override { return m_font->slope(); }
  36. virtual u16 width() const override { return m_font->width(); }
  37. virtual u16 weight() const override { return m_font->weight(); }
  38. virtual Gfx::Glyph glyph(u32 code_point) const override;
  39. virtual float glyph_left_bearing(u32 code_point) const override;
  40. virtual Glyph glyph(u32 code_point, GlyphSubpixelOffset) const override;
  41. virtual bool contains_glyph(u32 code_point) const override { return m_font->glyph_id_for_code_point(code_point) > 0; }
  42. virtual float glyph_width(u32 code_point) const override;
  43. virtual float glyph_or_emoji_width(Utf8CodePointIterator&) const override;
  44. virtual float glyph_or_emoji_width(Utf32CodePointIterator&) const override;
  45. virtual float glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const override;
  46. virtual float preferred_line_height() const override { return metrics().height() + metrics().line_gap; }
  47. virtual int x_height() const override { return m_point_height; } // FIXME: Read from font
  48. virtual u8 min_glyph_width() const override { return 1; } // FIXME: Read from font
  49. virtual u8 max_glyph_width() const override { return m_point_width; } // FIXME: Read from font
  50. virtual u8 glyph_fixed_width() const override;
  51. virtual u8 baseline() const override { return m_point_height; } // FIXME: Read from font
  52. virtual u8 mean_line() const override { return m_point_height; } // FIXME: Read from font
  53. virtual float width(StringView) const override;
  54. virtual float width(Utf8View const&) const override;
  55. virtual float width(Utf32View const&) const override;
  56. virtual int width_rounded_up(StringView) const override;
  57. virtual DeprecatedString name() const override { return DeprecatedString::formatted("{} {}", family(), variant()); }
  58. virtual bool is_fixed_width() const override { return m_font->is_fixed_width(); }
  59. virtual u8 glyph_spacing() const override { return 0; }
  60. virtual size_t glyph_count() const override { return m_font->glyph_count(); }
  61. virtual DeprecatedString family() const override { return m_font->family(); }
  62. virtual DeprecatedString variant() const override { return m_font->variant(); }
  63. virtual DeprecatedString qualified_name() const override { return DeprecatedString::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope()); }
  64. virtual DeprecatedString human_readable_name() const override { return DeprecatedString::formatted("{} {} {}", family(), variant(), presentation_size()); }
  65. virtual RefPtr<Font> with_size(float point_size) const override;
  66. virtual bool has_color_bitmaps() const override { return m_font->has_color_bitmaps(); }
  67. private:
  68. NonnullRefPtr<VectorFont> m_font;
  69. float m_x_scale { 0.0f };
  70. float m_y_scale { 0.0f };
  71. float m_point_width { 0.0f };
  72. float m_point_height { 0.0f };
  73. mutable HashMap<GlyphIndexWithSubpixelOffset, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps;
  74. Gfx::FontPixelMetrics m_pixel_metrics;
  75. float m_pixel_size { 0.0f };
  76. int m_pixel_size_rounded_up { 0 };
  77. template<typename T>
  78. float unicode_view_width(T const& view) const;
  79. };
  80. }
  81. namespace AK {
  82. template<>
  83. struct Traits<Gfx::GlyphIndexWithSubpixelOffset> : public GenericTraits<Gfx::GlyphIndexWithSubpixelOffset> {
  84. static unsigned hash(Gfx::GlyphIndexWithSubpixelOffset const& index)
  85. {
  86. return pair_int_hash(index.glyph_id, (index.subpixel_offset.x << 8) | index.subpixel_offset.y);
  87. }
  88. };
  89. }