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.
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 "RenderElement.h"
27 #include "RenderTextLineBoxes.h"
28 #include "SimpleLineLayout.h"
30 #include <wtf/Forward.h>
36 class RenderText : public RenderObject {
38 RenderText(Text&, const String&);
39 RenderText(Document&, const String&);
41 virtual ~RenderText();
43 virtual const char* renderName() const override;
45 Text* textNode() const;
47 virtual bool isTextFragment() const;
49 RenderStyle& style() const;
50 RenderStyle& firstLineStyle() const;
52 virtual String originalText() const;
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); }
58 StringImpl* text() const { return m_text.impl(); }
59 String textWithoutConvertingBackslashToYenSymbol() const;
61 InlineTextBox* createInlineTextBox() { return m_lineBoxes.createAndAppendLineBox(*this); }
62 void dirtyLineBoxes(bool fullLayout);
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;
67 virtual void collectSelectionRects(Vector<SelectionRect>&, unsigned startOffset = 0, unsigned endOffset = std::numeric_limits<unsigned>::max()) override;
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;
73 Vector<FloatQuad> absoluteQuadsClippedToEllipsis() const;
75 virtual VisiblePosition positionForPoint(const LayoutPoint&, const RenderRegion*) override;
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&);
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;
89 float minLogicalWidth() const;
90 float maxLogicalWidth() const;
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);
99 WEBCORE_EXPORT virtual IntRect linesBoundingBox() const;
100 LayoutRect linesVisualOverflowBoundingBox() const;
102 WEBCORE_EXPORT IntPoint firstRunLocation() const;
104 virtual void setText(const String&, bool force = false);
105 void setTextWithOffset(const String&, unsigned offset, unsigned len, bool force = false);
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;
112 LayoutRect collectSelectionRectsForLineBoxes(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent, Vector<LayoutRect>& rects);
114 LayoutUnit marginLeft() const { return minimumValueForLength(style().marginLeft(), 0); }
115 LayoutUnit marginRight() const { return minimumValueForLength(style().marginRight(), 0); }
117 virtual LayoutRect clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const override final;
119 InlineTextBox* firstTextBox() const { return m_lineBoxes.first(); }
120 InlineTextBox* lastTextBox() const { return m_lineBoxes.last(); }
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;
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 InlineTextBox* findNextInlineTextBox(int offset, int& pos) const { return m_lineBoxes.findNext(offset, pos); }
140 bool isAllCollapsibleWhitespace() const;
142 bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath; }
144 void removeAndDestroyTextBoxes();
146 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
148 virtual std::unique_ptr<InlineTextBox> createTextBox(); // Subclassed by RenderSVGInlineText.
150 #if ENABLE(IOS_TEXT_AUTOSIZING)
151 float candidateComputedTextSize() const { return m_candidateComputedTextSize; }
152 void setCandidateComputedTextSize(float s) { m_candidateComputedTextSize = s; }
155 void ensureLineBoxes();
156 void deleteLineBoxesBeforeSimpleLineLayout();
157 const SimpleLineLayout::Layout* simpleLineLayout() const;
159 StringView stringView(int start, int stop) const;
162 virtual void computePreferredLogicalWidths(float leadWidth);
163 virtual void willBeDestroyed() override;
165 virtual void setRenderedText(const String&);
166 virtual UChar previousCharacter() const;
169 RenderText(Node&, const String&);
171 virtual bool canHaveChildren() const override final { return false; }
173 void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
175 bool computeCanUseSimpleFontCodePath() const;
177 // Make length() private so that callers that have a RenderText*
178 // will use the more efficient textLength() instead, while
179 // callers with a RenderObject* can continue to use length().
180 virtual unsigned length() const override final { return textLength(); }
182 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction) override final { ASSERT_NOT_REACHED(); return false; }
184 bool containsOnlyWhitespace(unsigned from, unsigned len) const;
185 float widthFromCache(const Font&, int start, int len, float xPos, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*, const RenderStyle&) const;
186 bool isAllASCII() const { return m_isAllASCII; }
187 bool computeUseBackslashAsYenSymbol() const;
189 void secureText(UChar mask);
191 LayoutRect collectSelectionRectsForLineBoxes(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent, Vector<LayoutRect>*);
193 void node() const = delete;
195 // We put the bitfield first to minimize padding on 64-bit.
196 bool m_hasBreakableChar : 1; // Whether or not we can be broken into multiple lines.
197 bool m_hasBreak : 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
198 bool m_hasTab : 1; // Whether or not we have a variable width tab character (e.g., <pre> with '\t').
199 bool m_hasBeginWS : 1; // Whether or not we begin with WS (only true if we aren't pre)
200 bool m_hasEndWS : 1; // Whether or not we end with WS (only true if we aren't pre)
201 bool m_linesDirty : 1; // This bit indicates that the text run has already dirtied specific
202 // line boxes, and this hint will enable layoutInlineChildren to avoid
203 // just dirtying everything when character data is modified (e.g., appended/inserted
205 bool m_containsReversedText : 1;
206 bool m_isAllASCII : 1;
207 bool m_canUseSimpleFontCodePath : 1;
208 mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
209 bool m_useBackslashAsYenSymbol : 1;
210 bool m_originalTextDiffersFromRendered : 1;
212 #if ENABLE(IOS_TEXT_AUTOSIZING)
213 // FIXME: This should probably be part of the text sizing structures in Document instead. That would save some memory.
214 float m_candidateComputedTextSize;
218 float m_beginMinWidth;
223 RenderTextLineBoxes m_lineBoxes;
226 RENDER_OBJECT_TYPE_CASTS(RenderText, isText())
228 inline UChar RenderText::uncheckedCharacterAt(unsigned i) const
230 ASSERT_WITH_SECURITY_IMPLICATION(i < textLength());
231 return is8Bit() ? characters8()[i] : characters16()[i];
234 inline UChar RenderText::characterAt(unsigned i) const
236 if (i >= textLength())
239 return uncheckedCharacterAt(i);
242 inline RenderStyle& RenderText::style() const
244 return parent()->style();
247 inline RenderStyle& RenderText::firstLineStyle() const
249 return parent()->firstLineStyle();
252 void applyTextTransform(const RenderStyle&, String&, UChar);
253 void makeCapitalized(String*, UChar previous);
255 inline RenderText* Text::renderer() const
257 return toRenderText(Node::renderer());
260 } // namespace WebCore
262 #endif // RenderText_h