VectorFont.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/Noncopyable.h>
  9. #include <AK/RefCounted.h>
  10. #include <LibGfx/Font/Font.h>
  11. #include <LibGfx/Forward.h>
  12. #include <LibGfx/Path.h>
  13. #define POINTS_PER_INCH 72.0f
  14. #define DEFAULT_DPI 96
  15. namespace Gfx {
  16. class ScaledFont;
  17. struct ScaledFontMetrics {
  18. float ascender { 0 };
  19. float descender { 0 };
  20. float line_gap { 0 };
  21. float x_height { 0 };
  22. float height() const
  23. {
  24. return ascender + descender;
  25. }
  26. };
  27. struct ScaledGlyphMetrics {
  28. float ascender;
  29. float descender;
  30. float advance_width;
  31. float left_side_bearing;
  32. };
  33. class VectorFont : public RefCounted<VectorFont> {
  34. public:
  35. virtual ~VectorFont();
  36. virtual ScaledFontMetrics metrics(float x_scale, float y_scale) const = 0;
  37. virtual ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const = 0;
  38. virtual float glyph_advance(u32 glyph_id, float x_scale, float y_scale, float point_width, float point_height) const = 0;
  39. virtual float glyphs_horizontal_kerning(u32 left_glyph_id, u32 right_glyph_id, float x_scale) const = 0;
  40. virtual RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, float x_scale, float y_scale, GlyphSubpixelOffset) const = 0;
  41. virtual bool append_glyph_path_to(Gfx::Path&, u32 glyph_id, float x_scale, float y_scale) const = 0;
  42. virtual u32 glyph_count() const = 0;
  43. virtual u16 units_per_em() const = 0;
  44. virtual u32 glyph_id_for_code_point(u32 code_point) const = 0;
  45. virtual String family() const = 0;
  46. virtual String variant() const = 0;
  47. virtual u16 weight() const = 0;
  48. virtual u16 width() const = 0;
  49. virtual u8 slope() const = 0;
  50. virtual bool is_fixed_width() const = 0;
  51. virtual bool has_color_bitmaps() const = 0;
  52. [[nodiscard]] NonnullRefPtr<ScaledFont> scaled_font(float point_size) const;
  53. protected:
  54. VectorFont();
  55. private:
  56. mutable HashMap<float, NonnullRefPtr<ScaledFont>> m_scaled_fonts;
  57. };
  58. }