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