TextLayout.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteString.h>
  9. #include <AK/CharacterTypes.h>
  10. #include <AK/Forward.h>
  11. #include <AK/Utf32View.h>
  12. #include <AK/Utf8View.h>
  13. #include <AK/Vector.h>
  14. #include <LibGfx/Font/Font.h>
  15. #include <LibGfx/FontCascadeList.h>
  16. #include <LibGfx/Forward.h>
  17. #include <LibGfx/Rect.h>
  18. namespace Gfx {
  19. struct DrawGlyph {
  20. FloatPoint position;
  21. u32 glyph_id;
  22. void translate_by(FloatPoint const& delta)
  23. {
  24. position.translate_by(delta);
  25. }
  26. };
  27. class GlyphRun : public RefCounted<GlyphRun> {
  28. public:
  29. enum class TextType {
  30. Common,
  31. ContextDependent,
  32. EndPadding,
  33. Ltr,
  34. Rtl,
  35. };
  36. GlyphRun(Vector<DrawGlyph>&& glyphs, NonnullRefPtr<Font> font, TextType text_type, float width)
  37. : m_glyphs(move(glyphs))
  38. , m_font(move(font))
  39. , m_text_type(text_type)
  40. , m_width(width)
  41. {
  42. }
  43. [[nodiscard]] Font const& font() const { return m_font; }
  44. [[nodiscard]] TextType text_type() const { return m_text_type; }
  45. [[nodiscard]] Vector<DrawGlyph> const& glyphs() const { return m_glyphs; }
  46. [[nodiscard]] Vector<DrawGlyph>& glyphs() { return m_glyphs; }
  47. [[nodiscard]] bool is_empty() const { return m_glyphs.is_empty(); }
  48. [[nodiscard]] float width() const { return m_width; }
  49. void append(DrawGlyph glyph) { m_glyphs.append(glyph); }
  50. private:
  51. Vector<DrawGlyph> m_glyphs;
  52. NonnullRefPtr<Font> m_font;
  53. TextType m_text_type;
  54. float m_width { 0 };
  55. };
  56. RefPtr<GlyphRun> shape_text(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, GlyphRun::TextType);
  57. float measure_text_width(Utf8View const& string, Gfx::Font const& font);
  58. }