Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderText.h
1 /*
2  * (C) 1999 Lars Knoll (knoll@kde.org)
3  * (C) 2000 Dirk Mueller (mueller@kde.org)
4  * Copyright (C) 2004-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 "core/dom/Text.h"
27 #include "core/rendering/RenderObject.h"
28 #include "platform/LengthFunctions.h"
29 #include "platform/text/TextPath.h"
30 #include "wtf/Forward.h"
31 #include "wtf/PassRefPtr.h"
32
33 namespace blink {
34
35 class AbstractInlineTextBox;
36 class InlineTextBox;
37
38 class RenderText : public RenderObject {
39 public:
40     // FIXME: If the node argument is not a Text node or the string argument is
41     // not the content of the Text node, updating text-transform property
42     // doesn't re-transform the string.
43     RenderText(Node*, PassRefPtr<StringImpl>);
44 #if ENABLE(ASSERT)
45     virtual ~RenderText();
46 #endif
47
48     virtual const char* renderName() const override;
49
50     virtual bool isTextFragment() const;
51     virtual bool isWordBreak() const;
52
53     virtual PassRefPtr<StringImpl> originalText() const;
54
55     void extractTextBox(InlineTextBox*);
56     void attachTextBox(InlineTextBox*);
57     void removeTextBox(InlineTextBox*);
58
59     const String& text() const { return m_text; }
60     virtual unsigned textStartOffset() const { return 0; }
61     String plainText() const;
62
63     InlineTextBox* createInlineTextBox(int start, unsigned short length);
64     void dirtyLineBoxes(bool fullLayout);
65
66     virtual void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const override final;
67     void absoluteRectsForRange(Vector<IntRect>&, unsigned startOffset = 0, unsigned endOffset = INT_MAX, bool useSelectionHeight = false, bool* wasFixed = 0);
68
69     virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const override final;
70     void absoluteQuadsForRange(Vector<FloatQuad>&, unsigned startOffset = 0, unsigned endOffset = INT_MAX, bool useSelectionHeight = false, bool* wasFixed = 0);
71
72     enum ClippingOption { NoClipping, ClipToEllipsis };
73     void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed = 0, ClippingOption = NoClipping) const;
74
75     virtual PositionWithAffinity positionForPoint(const LayoutPoint&) override;
76
77     bool is8Bit() const { return m_text.is8Bit(); }
78     const LChar* characters8() const { return m_text.impl()->characters8(); }
79     const UChar* characters16() const { return m_text.impl()->characters16(); }
80     bool hasEmptyText() const { return m_text.isEmpty(); }
81     UChar characterAt(unsigned) const;
82     UChar uncheckedCharacterAt(unsigned) const;
83     UChar operator[](unsigned i) const { return uncheckedCharacterAt(i); }
84     unsigned textLength() const { return m_text.length(); } // non virtual implementation of length()
85     void positionLineBox(InlineBox*);
86
87     virtual float width(unsigned from, unsigned len, const Font&, float xPos, TextDirection, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
88     virtual float width(unsigned from, unsigned len, float xPos, TextDirection, bool firstLine = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
89
90     float minLogicalWidth() const;
91     float maxLogicalWidth() const;
92
93     void trimmedPrefWidths(float leadWidth,
94         float& firstLineMinWidth, bool& hasBreakableStart,
95         float& lastLineMinWidth, bool& hasBreakableEnd,
96         bool& hasBreakableChar, bool& hasBreak,
97         float& firstLineMaxWidth, float& lastLineMaxWidth,
98         float& minWidth, float& maxWidth, bool& stripFrontSpaces,
99         TextDirection);
100
101     virtual IntRect linesBoundingBox() const;
102     LayoutRect linesVisualOverflowBoundingBox() const;
103
104     FloatPoint firstRunOrigin() const;
105     float firstRunX() const;
106     float firstRunY() const;
107
108     virtual void setText(PassRefPtr<StringImpl>, bool force = false);
109     void setTextWithOffset(PassRefPtr<StringImpl>, unsigned offset, unsigned len, bool force = false);
110
111     virtual void transformText();
112
113     virtual bool canBeSelectionLeaf() const override { return true; }
114     virtual void setSelectionState(SelectionState) override final;
115     virtual LayoutRect selectionRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer) const override;
116     virtual LayoutRect localCaretRect(InlineBox*, int caretOffset, LayoutUnit* extraWidthToEndOfLine = 0) override;
117
118     InlineTextBox* firstTextBox() const { return m_firstTextBox; }
119     InlineTextBox* lastTextBox() const { return m_lastTextBox; }
120
121     virtual int caretMinOffset() const override;
122     virtual int caretMaxOffset() const override;
123     unsigned renderedTextLength() const;
124
125     virtual int previousOffset(int current) const override final;
126     virtual int previousOffsetForBackwardDeletion(int current) const override final;
127     virtual int nextOffset(int current) const override final;
128
129     bool containsReversedText() const { return m_containsReversedText; }
130
131     bool isSecure() const { return style()->textSecurity() != TSNONE; }
132     void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);
133
134     bool isAllCollapsibleWhitespace() const;
135
136     bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath; }
137
138     void removeAndDestroyTextBoxes();
139
140     PassRefPtr<AbstractInlineTextBox> firstAbstractInlineTextBox();
141
142 protected:
143     virtual void willBeDestroyed() override;
144
145     virtual void styleWillChange(StyleDifference, const RenderStyle&) override final { }
146     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
147
148     virtual void setTextInternal(PassRefPtr<StringImpl>);
149     virtual UChar previousCharacter() const;
150
151     virtual void addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const override;
152
153     virtual InlineTextBox* createTextBox(int start, unsigned short length); // Subclassed by SVG.
154
155 private:
156     void computePreferredLogicalWidths(float leadWidth);
157     void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
158
159     bool computeCanUseSimpleFontCodePath() const;
160
161     // Make length() private so that callers that have a RenderText*
162     // will use the more efficient textLength() instead, while
163     // callers with a RenderObject* can continue to use length().
164     virtual unsigned length() const override final { return textLength(); }
165
166     virtual void paint(PaintInfo&, const LayoutPoint&) override final { ASSERT_NOT_REACHED(); }
167     virtual void layout() override final { ASSERT_NOT_REACHED(); }
168     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction) override final { ASSERT_NOT_REACHED(); return false; }
169
170     void deleteTextBoxes();
171     bool containsOnlyWhitespace(unsigned from, unsigned len) const;
172     float widthFromCache(const Font&, int start, int len, float xPos, TextDirection, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*) const;
173     bool isAllASCII() const { return m_isAllASCII; }
174
175     void secureText(UChar mask);
176
177     bool isText() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
178
179     virtual LayoutRect clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer, const PaintInvalidationState* = 0) const override;
180
181     void checkConsistency() const;
182
183     // We put the bitfield first to minimize padding on 64-bit.
184     bool m_hasBreakableChar : 1; // Whether or not we can be broken into multiple lines.
185     bool m_hasBreak : 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
186     bool m_hasTab : 1; // Whether or not we have a variable width tab character (e.g., <pre> with '\t').
187     bool m_hasBreakableStart : 1;
188     bool m_hasBreakableEnd : 1;
189     bool m_hasEndWhiteSpace : 1;
190     bool m_linesDirty : 1; // This bit indicates that the text run has already dirtied specific
191                            // line boxes, and this hint will enable layoutInlineChildren to avoid
192                            // just dirtying everything when character data is modified (e.g., appended/inserted
193                            // or removed).
194     bool m_containsReversedText : 1;
195     bool m_isAllASCII : 1;
196     bool m_canUseSimpleFontCodePath : 1;
197     mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
198
199     float m_minWidth;
200     float m_maxWidth;
201     float m_firstLineMinWidth;
202     float m_lastLineLineMinWidth;
203
204     String m_text;
205
206     InlineTextBox* m_firstTextBox;
207     InlineTextBox* m_lastTextBox;
208 };
209
210 inline UChar RenderText::uncheckedCharacterAt(unsigned i) const
211 {
212     ASSERT_WITH_SECURITY_IMPLICATION(i < textLength());
213     return is8Bit() ? characters8()[i] : characters16()[i];
214 }
215
216 inline UChar RenderText::characterAt(unsigned i) const
217 {
218     if (i >= textLength())
219         return 0;
220
221     return uncheckedCharacterAt(i);
222 }
223
224 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderText, isText());
225
226 #if !ENABLE(ASSERT)
227 inline void RenderText::checkConsistency() const
228 {
229 }
230 #endif
231
232 inline RenderText* Text::renderer() const
233 {
234     return toRenderText(CharacterData::renderer());
235 }
236
237 void applyTextTransform(const RenderStyle*, String&, UChar);
238
239 } // namespace blink
240
241 #endif // RenderText_h