summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/TextLayout.h
blob: 944876697cda21e8c061891a586e11aa77ec338c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/DeprecatedString.h>
#include <AK/Forward.h>
#include <AK/Utf32View.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Rect.h>
#include <LibGfx/TextElision.h>
#include <LibGfx/TextWrapping.h>

namespace Gfx {

// FIXME: This currently isn't an ideal way of doing things; ideally, TextLayout
// would be doing the rendering by painting individual glyphs. However, this
// would regress our Unicode bidirectional text support. Therefore, fixing this
// requires:
// - Moving the bidirectional algorithm either here, or some place TextLayout
//   can access;
// - Making TextLayout render the given text into something like a Vector<Line>
//   where:
//   using Line = Vector<DirectionalRun>;
//   struct DirectionalRun {
//       Utf32View glyphs;
//       Vector<int> advance;
//       TextDirection direction;
//   };
// - Either;
//   a) Making TextLayout output these Lines directly using a given Painter, or
//   b) Taking the Lines from TextLayout and painting each glyph.
class TextLayout {
public:
    TextLayout(Gfx::Font const& font, Utf8View const& text, FloatRect const& rect)
        : m_font(font)
        , m_font_metrics(font.pixel_metrics())
        , m_text(text)
        , m_rect(rect)
    {
    }

    Vector<DeprecatedString, 32> lines(TextElision elision, TextWrapping wrapping) const
    {
        return wrap_lines(elision, wrapping);
    }

    FloatRect bounding_rect(TextWrapping) const;

private:
    Vector<DeprecatedString, 32> wrap_lines(TextElision, TextWrapping) const;
    DeprecatedString elide_text_from_right(Utf8View) const;

    Font const& m_font;
    FontPixelMetrics m_font_metrics;
    Utf8View m_text;
    FloatRect m_rect;
};

}