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(int start, unsigned short length);
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 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*);
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;
90 float minLogicalWidth() const;
91 float maxLogicalWidth() const;
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,
101 virtual IntRect linesBoundingBox() const;
102 LayoutRect linesVisualOverflowBoundingBox() const;
104 FloatPoint firstRunOrigin() const;
105 float firstRunX() const;
106 float firstRunY() const;
108 virtual void setText(PassRefPtr<StringImpl>, bool force = false);
109 void setTextWithOffset(PassRefPtr<StringImpl>, unsigned offset, unsigned len, bool force = false);
111 virtual void transformText();
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;
118 InlineTextBox* firstTextBox() const { return m_firstTextBox; }
119 InlineTextBox* lastTextBox() const { return m_lastTextBox; }
121 virtual int caretMinOffset() const override;
122 virtual int caretMaxOffset() const override;
123 unsigned renderedTextLength() const;
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;
129 bool containsReversedText() const { return m_containsReversedText; }
131 bool isSecure() const { return style()->textSecurity() != TSNONE; }
132 void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);
134 bool isAllCollapsibleWhitespace() const;
136 bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath; }
138 void removeAndDestroyTextBoxes();
140 PassRefPtr<AbstractInlineTextBox> firstAbstractInlineTextBox();
143 virtual void willBeDestroyed() override;
145 virtual void styleWillChange(StyleDifference, const RenderStyle&) override final { }
146 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
148 virtual void setTextInternal(PassRefPtr<StringImpl>);
149 virtual UChar previousCharacter() const;
151 virtual void addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const override;
153 virtual InlineTextBox* createTextBox(int start, unsigned short length); // Subclassed by SVG.
156 void computePreferredLogicalWidths(float leadWidth);
157 void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
159 bool computeCanUseSimpleFontCodePath() const;
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(); }
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; }
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; }
175 void secureText(UChar mask);
177 bool isText() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
179 virtual LayoutRect clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer, const PaintInvalidationState* = 0) const override;
181 void checkConsistency() const;
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
194 bool m_containsReversedText : 1;
195 bool m_isAllASCII : 1;
196 bool m_canUseSimpleFontCodePath : 1;
197 mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
201 float m_firstLineMinWidth;
202 float m_lastLineLineMinWidth;
206 InlineTextBox* m_firstTextBox;
207 InlineTextBox* m_lastTextBox;
210 inline UChar RenderText::uncheckedCharacterAt(unsigned i) const
212 ASSERT_WITH_SECURITY_IMPLICATION(i < textLength());
213 return is8Bit() ? characters8()[i] : characters16()[i];
216 inline UChar RenderText::characterAt(unsigned i) const
218 if (i >= textLength())
221 return uncheckedCharacterAt(i);
224 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderText, isText());
227 inline void RenderText::checkConsistency() const
232 inline RenderText* Text::renderer() const
234 return toRenderText(CharacterData::renderer());
237 void applyTextTransform(const RenderStyle*, String&, UChar);
241 #endif // RenderText_h