Typeface.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/Font/FontData.h>
  12. #include <LibGfx/Forward.h>
  13. #define POINTS_PER_INCH 72.0f
  14. #define DEFAULT_DPI 96
  15. class SkTypeface;
  16. struct hb_blob_t;
  17. struct hb_face_t;
  18. namespace Gfx {
  19. class ScaledFont;
  20. struct ScaledFontMetrics {
  21. float ascender { 0 };
  22. float descender { 0 };
  23. float line_gap { 0 };
  24. float x_height { 0 };
  25. float height() const
  26. {
  27. return ascender + descender;
  28. }
  29. };
  30. class Typeface : public RefCounted<Typeface> {
  31. public:
  32. static ErrorOr<NonnullRefPtr<Typeface>> try_load_from_resource(Core::Resource const&, int ttc_index = 0);
  33. static ErrorOr<NonnullRefPtr<Typeface>> try_load_from_font_data(NonnullOwnPtr<Gfx::FontData>, int ttc_index = 0);
  34. static ErrorOr<NonnullRefPtr<Typeface>> try_load_from_externally_owned_memory(ReadonlyBytes bytes, int ttc_index = 0);
  35. virtual ~Typeface();
  36. virtual u32 glyph_count() const = 0;
  37. virtual u16 units_per_em() const = 0;
  38. virtual u32 glyph_id_for_code_point(u32 code_point) const = 0;
  39. virtual FlyString const& family() const = 0;
  40. virtual u16 weight() const = 0;
  41. virtual u16 width() const = 0;
  42. virtual u8 slope() const = 0;
  43. [[nodiscard]] NonnullRefPtr<ScaledFont> scaled_font(float point_size) const;
  44. hb_face_t* harfbuzz_typeface() const;
  45. protected:
  46. Typeface();
  47. virtual ReadonlyBytes buffer() const = 0;
  48. virtual unsigned ttc_index() const = 0;
  49. private:
  50. OwnPtr<Gfx::FontData> m_font_data;
  51. mutable HashMap<float, NonnullRefPtr<ScaledFont>> m_scaled_fonts;
  52. mutable hb_blob_t* m_harfbuzz_blob { nullptr };
  53. mutable hb_face_t* m_harfbuzz_face { nullptr };
  54. };
  55. }