ScaledFont.h 5.1 KB

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