1/*
2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB.  If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifndef RenderText_h
24#define RenderText_h
25
26#include "RenderElement.h"
27#include "RenderTextLineBoxes.h"
28#include "SimpleLineLayout.h"
29#include "Text.h"
30#include <wtf/Forward.h>
31
32namespace WebCore {
33
34class InlineTextBox;
35
36class RenderText : public RenderObject {
37public:
38    RenderText(Text&, const String&);
39    RenderText(Document&, const String&);
40
41    virtual ~RenderText();
42
43    virtual const char* renderName() const override;
44
45    Text* textNode() const;
46
47    virtual bool isTextFragment() const;
48
49    RenderStyle& style() const;
50    RenderStyle& firstLineStyle() const;
51
52    virtual String originalText() const;
53
54    void extractTextBox(InlineTextBox& box) { m_lineBoxes.extract(box); }
55    void attachTextBox(InlineTextBox& box) { m_lineBoxes.attach(box); }
56    void removeTextBox(InlineTextBox& box) { m_lineBoxes.remove(box); }
57
58    StringImpl* text() const { return m_text.impl(); }
59    String textWithoutConvertingBackslashToYenSymbol() const;
60
61    InlineTextBox* createInlineTextBox() { return m_lineBoxes.createAndAppendLineBox(*this); }
62    void dirtyLineBoxes(bool fullLayout);
63
64    virtual void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const override final;
65    Vector<IntRect> absoluteRectsForRange(unsigned startOffset = 0, unsigned endOffset = UINT_MAX, bool useSelectionHeight = false, bool* wasFixed = nullptr) const;
66#if PLATFORM(IOS)
67    virtual void collectSelectionRects(Vector<SelectionRect>&, unsigned startOffset = 0, unsigned endOffset = std::numeric_limits<unsigned>::max()) override;
68#endif
69
70    virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const override final;
71    Vector<FloatQuad> absoluteQuadsForRange(unsigned startOffset = 0, unsigned endOffset = UINT_MAX, bool useSelectionHeight = false, bool* wasFixed = nullptr) const;
72
73    Vector<FloatQuad> absoluteQuadsClippedToEllipsis() const;
74
75    virtual VisiblePosition positionForPoint(const LayoutPoint&, const RenderRegion*) override;
76
77    bool is8Bit() const { return m_text.impl()->is8Bit(); }
78    const LChar* characters8() const { return m_text.impl()->characters8(); }
79    const UChar* characters16() const { return m_text.impl()->characters16(); }
80    UChar characterAt(unsigned) const;
81    UChar uncheckedCharacterAt(unsigned) const;
82    UChar operator[](unsigned i) const { return uncheckedCharacterAt(i); }
83    unsigned textLength() const { return m_text.impl()->length(); } // non virtual implementation of length()
84    void positionLineBox(InlineTextBox&);
85
86    virtual float width(unsigned from, unsigned len, const Font&, float xPos, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
87    virtual float width(unsigned from, unsigned len, float xPos, bool firstLine = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
88
89    float minLogicalWidth() const;
90    float maxLogicalWidth() const;
91
92    void trimmedPrefWidths(float leadWidth,
93                           float& beginMinW, bool& beginWS,
94                           float& endMinW, bool& endWS,
95                           bool& hasBreakableChar, bool& hasBreak,
96                           float& beginMaxW, float& endMaxW,
97                           float& minW, float& maxW, bool& stripFrontSpaces);
98
99    virtual IntRect linesBoundingBox() const;
100    LayoutRect linesVisualOverflowBoundingBox() const;
101
102    IntPoint firstRunLocation() const;
103
104    virtual void setText(const String&, bool force = false);
105    void setTextWithOffset(const String&, unsigned offset, unsigned len, bool force = false);
106
107    virtual bool canBeSelectionLeaf() const override { return true; }
108    virtual void setSelectionState(SelectionState) override final;
109    virtual LayoutRect selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent = true) override;
110    virtual LayoutRect localCaretRect(InlineBox*, int caretOffset, LayoutUnit* extraWidthToEndOfLine = 0) override;
111
112    LayoutRect collectSelectionRectsForLineBoxes(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent, Vector<LayoutRect>& rects);
113
114    LayoutUnit marginLeft() const { return minimumValueForLength(style().marginLeft(), 0); }
115    LayoutUnit marginRight() const { return minimumValueForLength(style().marginRight(), 0); }
116
117    virtual LayoutRect clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const override final;
118
119    InlineTextBox* firstTextBox() const { return m_lineBoxes.first(); }
120    InlineTextBox* lastTextBox() const { return m_lineBoxes.last(); }
121
122    virtual int caretMinOffset() const override;
123    virtual int caretMaxOffset() const override;
124    unsigned countRenderedCharacterOffsetsUntil(unsigned) const;
125    bool containsRenderedCharacterOffset(unsigned) const;
126    bool containsCaretOffset(unsigned) const;
127    bool hasRenderedText() const;
128
129    virtual int previousOffset(int current) const override final;
130    virtual int previousOffsetForBackwardDeletion(int current) const override final;
131    virtual int nextOffset(int current) const override final;
132
133    bool containsReversedText() const { return m_containsReversedText; }
134
135    bool isSecure() const { return style().textSecurity() != TSNONE; }
136    void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);
137
138    InlineTextBox* findNextInlineTextBox(int offset, int& pos) const { return m_lineBoxes.findNext(offset, pos); }
139
140    bool isAllCollapsibleWhitespace() const;
141
142    bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath; }
143
144    void removeAndDestroyTextBoxes();
145
146    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
147
148    virtual std::unique_ptr<InlineTextBox> createTextBox(); // Subclassed by RenderSVGInlineText.
149
150#if ENABLE(IOS_TEXT_AUTOSIZING)
151    float candidateComputedTextSize() const { return m_candidateComputedTextSize; }
152    void setCandidateComputedTextSize(float s) { m_candidateComputedTextSize = s; }
153#endif
154
155    void ensureLineBoxes();
156    void deleteLineBoxesBeforeSimpleLineLayout();
157    const SimpleLineLayout::Layout* simpleLineLayout() const;
158
159protected:
160    virtual void computePreferredLogicalWidths(float leadWidth);
161    virtual void willBeDestroyed() override;
162
163    virtual void setRenderedText(const String&);
164    virtual UChar previousCharacter() const;
165
166private:
167    RenderText(Node&, const String&);
168
169    virtual bool canHaveChildren() const override final { return false; }
170
171    void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
172
173    bool computeCanUseSimpleFontCodePath() const;
174
175    // Make length() private so that callers that have a RenderText*
176    // will use the more efficient textLength() instead, while
177    // callers with a RenderObject* can continue to use length().
178    virtual unsigned length() const override final { return textLength(); }
179
180    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction) override final { ASSERT_NOT_REACHED(); return false; }
181
182    bool containsOnlyWhitespace(unsigned from, unsigned len) const;
183    float widthFromCache(const Font&, int start, int len, float xPos, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*, const RenderStyle&) const;
184    bool isAllASCII() const { return m_isAllASCII; }
185    bool computeUseBackslashAsYenSymbol() const;
186
187    void secureText(UChar mask);
188
189    LayoutRect collectSelectionRectsForLineBoxes(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent, Vector<LayoutRect>*);
190
191    void node() const = delete;
192
193    // We put the bitfield first to minimize padding on 64-bit.
194    bool m_hasBreakableChar : 1; // Whether or not we can be broken into multiple lines.
195    bool m_hasBreak : 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
196    bool m_hasTab : 1; // Whether or not we have a variable width tab character (e.g., <pre> with '\t').
197    bool m_hasBeginWS : 1; // Whether or not we begin with WS (only true if we aren't pre)
198    bool m_hasEndWS : 1; // Whether or not we end with WS (only true if we aren't pre)
199    bool m_linesDirty : 1; // This bit indicates that the text run has already dirtied specific
200                           // line boxes, and this hint will enable layoutInlineChildren to avoid
201                           // just dirtying everything when character data is modified (e.g., appended/inserted
202                           // or removed).
203    bool m_containsReversedText : 1;
204    bool m_isAllASCII : 1;
205    bool m_canUseSimpleFontCodePath : 1;
206    mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
207    bool m_useBackslashAsYenSymbol : 1;
208    bool m_originalTextDiffersFromRendered : 1;
209
210#if ENABLE(IOS_TEXT_AUTOSIZING)
211    // FIXME: This should probably be part of the text sizing structures in Document instead. That would save some memory.
212    float m_candidateComputedTextSize;
213#endif
214    float m_minWidth;
215    float m_maxWidth;
216    float m_beginMinWidth;
217    float m_endMinWidth;
218
219    String m_text;
220
221    RenderTextLineBoxes m_lineBoxes;
222};
223
224RENDER_OBJECT_TYPE_CASTS(RenderText, isText())
225
226inline UChar RenderText::uncheckedCharacterAt(unsigned i) const
227{
228    ASSERT_WITH_SECURITY_IMPLICATION(i < textLength());
229    return is8Bit() ? characters8()[i] : characters16()[i];
230}
231
232inline UChar RenderText::characterAt(unsigned i) const
233{
234    if (i >= textLength())
235        return 0;
236
237    return uncheckedCharacterAt(i);
238}
239
240inline RenderStyle& RenderText::style() const
241{
242    return parent()->style();
243}
244
245inline RenderStyle& RenderText::firstLineStyle() const
246{
247    return parent()->firstLineStyle();
248}
249
250void applyTextTransform(const RenderStyle&, String&, UChar);
251void makeCapitalized(String*, UChar previous);
252
253inline RenderText* Text::renderer() const
254{
255    return toRenderText(Node::renderer());
256}
257
258} // namespace WebCore
259
260#endif // RenderText_h
261