1/*
2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003-2018 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef TextRun_h
25#define TextRun_h
26
27#include "TabSize.h"
28#include "TextFlags.h"
29#include "WritingMode.h"
30#include <wtf/text/StringView.h>
31
32namespace WebCore {
33
34class FloatPoint;
35class FloatRect;
36class FontCascade;
37class GraphicsContext;
38class GlyphBuffer;
39class Font;
40
41struct GlyphData;
42struct WidthIterator;
43
44class TextRun {
45 WTF_MAKE_FAST_ALLOCATED;
46public:
47 explicit TextRun(const String& text, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = DefaultExpansion, TextDirection direction = TextDirection::LTR, bool directionalOverride = false, bool characterScanForCodePath = true)
48 : m_text(text)
49 , m_tabSize(0)
50 , m_xpos(xpos)
51 , m_horizontalGlyphStretch(1)
52 , m_expansion(expansion)
53 , m_expansionBehavior(expansionBehavior)
54 , m_allowTabs(false)
55 , m_direction(static_cast<unsigned>(direction))
56 , m_directionalOverride(directionalOverride)
57 , m_characterScanForCodePath(characterScanForCodePath)
58 , m_disableSpacing(false)
59 {
60 ASSERT(!m_text.isNull());
61 }
62
63 explicit TextRun(StringView stringView, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = DefaultExpansion, TextDirection direction = TextDirection::LTR, bool directionalOverride = false, bool characterScanForCodePath = true)
64 : TextRun(stringView.toStringWithoutCopying(), xpos, expansion, expansionBehavior, direction, directionalOverride, characterScanForCodePath)
65 {
66 }
67
68 TextRun subRun(unsigned startOffset, unsigned length) const
69 {
70 ASSERT_WITH_SECURITY_IMPLICATION(startOffset < m_text.length());
71
72 auto result { *this };
73
74 if (is8Bit())
75 result.setText(data8(startOffset), length);
76 else
77 result.setText(data16(startOffset), length);
78 return result;
79 }
80
81 UChar operator[](unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_text.length()); return m_text[i]; }
82 const LChar* data8(unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_text.length()); ASSERT(is8Bit()); return &m_text.characters8()[i]; }
83 const UChar* data16(unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_text.length()); ASSERT(!is8Bit()); return &m_text.characters16()[i]; }
84
85 const LChar* characters8() const { ASSERT(is8Bit()); return m_text.characters8(); }
86 const UChar* characters16() const { ASSERT(!is8Bit()); return m_text.characters16(); }
87
88 bool is8Bit() const { return m_text.is8Bit(); }
89 unsigned length() const { return m_text.length(); }
90
91 void setText(const LChar* text, unsigned length) { setText({ text, length }); }
92 void setText(const UChar* text, unsigned length) { setText({ text, length }); }
93 void setText(StringView text) { ASSERT(!text.isNull()); m_text = text.toStringWithoutCopying(); }
94
95 float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
96 void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
97
98 bool allowTabs() const { return m_allowTabs; }
99 const TabSize& tabSize() const { return m_tabSize; }
100 void setTabSize(bool, const TabSize&);
101
102 float xPos() const { return m_xpos; }
103 void setXPos(float xPos) { m_xpos = xPos; }
104 float expansion() const { return m_expansion; }
105 ExpansionBehavior expansionBehavior() const { return m_expansionBehavior; }
106 TextDirection direction() const { return static_cast<TextDirection>(m_direction); }
107 bool rtl() const { return static_cast<TextDirection>(m_direction) == TextDirection::RTL; }
108 bool ltr() const { return static_cast<TextDirection>(m_direction) == TextDirection::LTR; }
109 bool directionalOverride() const { return m_directionalOverride; }
110 bool characterScanForCodePath() const { return m_characterScanForCodePath; }
111 bool spacingDisabled() const { return m_disableSpacing; }
112
113 void disableSpacing() { m_disableSpacing = true; }
114 void setDirection(TextDirection direction) { m_direction = static_cast<unsigned>(direction); }
115 void setDirectionalOverride(bool override) { m_directionalOverride = override; }
116 void setCharacterScanForCodePath(bool scan) { m_characterScanForCodePath = scan; }
117 StringView text() const { return m_text; }
118
119private:
120 String m_text;
121
122 TabSize m_tabSize;
123
124 // m_xpos is the x position relative to the left start of the text line, not relative to the left
125 // start of the containing block. In the case of right alignment or center alignment, left start of
126 // the text line is not the same as left start of the containing block. This variable is only used
127 // to calculate the width of \t
128 float m_xpos;
129 float m_horizontalGlyphStretch;
130
131 float m_expansion;
132 unsigned m_expansionBehavior : 4;
133 unsigned m_allowTabs : 1;
134 unsigned m_direction : 1;
135 unsigned m_directionalOverride : 1; // Was this direction set by an override character.
136 unsigned m_characterScanForCodePath : 1;
137 unsigned m_disableSpacing : 1;
138};
139
140inline void TextRun::setTabSize(bool allow, const TabSize& size)
141{
142 m_allowTabs = allow;
143 m_tabSize = size;
144}
145
146}
147
148#endif
149