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.
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.
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.
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.
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"
35 class AbstractInlineTextBox;
38 class RenderText : public RenderObject {
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>);
45 virtual ~RenderText();
48 virtual const char* renderName() const OVERRIDE;
50 virtual bool isTextFragment() const;
51 virtual bool isWordBreak() const;
53 virtual PassRefPtr<StringImpl> originalText() const;
55 void extractTextBox(InlineTextBox*);
56 void attachTextBox(InlineTextBox*);
57 void removeTextBox(InlineTextBox*);
59 const String& text() const { return m_text; }
60 virtual unsigned textStartOffset() const { return 0; }
61 String plainText() const;
63 InlineTextBox* createInlineTextBox();
64 void dirtyLineBoxes(bool fullLayout);
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);
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);
72 enum ClippingOption { NoClipping, ClipToEllipsis };
73 void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed = 0, ClippingOption = NoClipping) const;
75 virtual PositionWithAffinity positionForPoint(const LayoutPoint&) OVERRIDE;
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 String substring(unsigned position, unsigned length) const { return m_text.substring(position, length); }
82 UChar characterAt(unsigned) const;
83 UChar uncheckedCharacterAt(unsigned) const;
84 UChar operator[](unsigned i) const { return uncheckedCharacterAt(i); }
85 unsigned textLength() const { return m_text.length(); } // non virtual implementation of length()
86 void positionLineBox(InlineBox*);
88 virtual float width(unsigned from, unsigned len, const Font&, float xPos, TextDirection, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
89 virtual float width(unsigned from, unsigned len, float xPos, TextDirection, bool firstLine = false, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
91 float minLogicalWidth() const;
92 float maxLogicalWidth() const;
94 void trimmedPrefWidths(float leadWidth,
95 float& firstLineMinWidth, bool& hasBreakableStart,
96 float& lastLineMinWidth, bool& hasBreakableEnd,
97 bool& hasBreakableChar, bool& hasBreak,
98 float& firstLineMaxWidth, float& lastLineMaxWidth,
99 float& minWidth, float& maxWidth, bool& stripFrontSpaces,
102 virtual IntRect linesBoundingBox() const;
103 LayoutRect linesVisualOverflowBoundingBox() const;
105 FloatPoint firstRunOrigin() const;
106 float firstRunX() const;
107 float firstRunY() const;
109 virtual void setText(PassRefPtr<StringImpl>, bool force = false);
110 void setTextWithOffset(PassRefPtr<StringImpl>, unsigned offset, unsigned len, bool force = false);
112 virtual void transformText();
114 virtual bool canBeSelectionLeaf() const OVERRIDE { return true; }
115 virtual void setSelectionState(SelectionState s) OVERRIDE FINAL;
116 virtual LayoutRect selectionRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer) const OVERRIDE;
117 virtual LayoutRect localCaretRect(InlineBox*, int caretOffset, LayoutUnit* extraWidthToEndOfLine = 0) OVERRIDE;
119 LayoutUnit marginLeft() const { return minimumValueForLength(style()->marginLeft(), 0); }
120 LayoutUnit marginRight() const { return minimumValueForLength(style()->marginRight(), 0); }
122 InlineTextBox* firstTextBox() const { return m_firstTextBox; }
123 InlineTextBox* lastTextBox() const { return m_lastTextBox; }
125 virtual int caretMinOffset() const OVERRIDE;
126 virtual int caretMaxOffset() const OVERRIDE;
127 unsigned renderedTextLength() const;
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;
133 bool containsReversedText() const { return m_containsReversedText; }
135 bool isSecure() const { return style()->textSecurity() != TSNONE; }
136 void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);
138 void checkConsistency() const;
140 bool isAllCollapsibleWhitespace() const;
142 bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath; }
144 void removeAndDestroyTextBoxes();
146 PassRefPtr<AbstractInlineTextBox> firstAbstractInlineTextBox();
149 virtual void willBeDestroyed() OVERRIDE;
151 virtual void styleWillChange(StyleDifference, const RenderStyle&) OVERRIDE FINAL { }
152 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) OVERRIDE;
154 virtual void setTextInternal(PassRefPtr<StringImpl>);
155 virtual UChar previousCharacter() const;
157 virtual void addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const OVERRIDE;
159 virtual InlineTextBox* createTextBox(); // Subclassed by SVG.
162 void computePreferredLogicalWidths(float leadWidth);
163 void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
165 bool computeCanUseSimpleFontCodePath() const;
167 // Make length() private so that callers that have a RenderText*
168 // will use the more efficient textLength() instead, while
169 // callers with a RenderObject* can continue to use length().
170 virtual unsigned length() const OVERRIDE FINAL { return textLength(); }
172 virtual void paint(PaintInfo&, const LayoutPoint&) OVERRIDE FINAL { ASSERT_NOT_REACHED(); }
173 virtual void layout() OVERRIDE FINAL { ASSERT_NOT_REACHED(); }
174 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction) OVERRIDE FINAL { ASSERT_NOT_REACHED(); return false; }
176 void deleteTextBoxes();
177 bool containsOnlyWhitespace(unsigned from, unsigned len) const;
178 float widthFromCache(const Font&, int start, int len, float xPos, TextDirection, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*) const;
179 bool isAllASCII() const { return m_isAllASCII; }
181 void secureText(UChar mask);
183 bool isText() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
185 virtual LayoutRect clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer, const PaintInvalidationState* = 0) const OVERRIDE FINAL;
186 // The parent invalidates for RenderText, so RenderText does nothing.
187 virtual InvalidationReason invalidatePaintIfNeeded(const PaintInvalidationState&, const RenderLayerModelObject&) OVERRIDE FINAL { return InvalidationNone; }
189 // We put the bitfield first to minimize padding on 64-bit.
190 bool m_hasBreakableChar : 1; // Whether or not we can be broken into multiple lines.
191 bool m_hasBreak : 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
192 bool m_hasTab : 1; // Whether or not we have a variable width tab character (e.g., <pre> with '\t').
193 bool m_hasBreakableStart : 1;
194 bool m_hasBreakableEnd : 1;
195 bool m_hasEndWhiteSpace : 1;
196 bool m_linesDirty : 1; // This bit indicates that the text run has already dirtied specific
197 // line boxes, and this hint will enable layoutInlineChildren to avoid
198 // just dirtying everything when character data is modified (e.g., appended/inserted
200 bool m_containsReversedText : 1;
201 bool m_isAllASCII : 1;
202 bool m_canUseSimpleFontCodePath : 1;
203 mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
207 float m_firstLineMinWidth;
208 float m_lastLineLineMinWidth;
212 InlineTextBox* m_firstTextBox;
213 InlineTextBox* m_lastTextBox;
216 inline UChar RenderText::uncheckedCharacterAt(unsigned i) const
218 ASSERT_WITH_SECURITY_IMPLICATION(i < textLength());
219 return is8Bit() ? characters8()[i] : characters16()[i];
222 inline UChar RenderText::characterAt(unsigned i) const
224 if (i >= textLength())
227 return uncheckedCharacterAt(i);
230 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderText, isText());
233 inline void RenderText::checkConsistency() const
238 inline RenderText* Text::renderer() const
240 return toRenderText(CharacterData::renderer());
243 void applyTextTransform(const RenderStyle*, String&, UChar);
247 #endif // RenderText_h