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/CoreExport.h"
27 #include "core/dom/Text.h"
28 #include "core/layout/LayoutObject.h"
29 #include "core/layout/TextRunConstructor.h"
30 #include "platform/LengthFunctions.h"
31 #include "platform/text/TextPath.h"
32 #include "wtf/Forward.h"
33 #include "wtf/PassRefPtr.h"
37 class AbstractInlineTextBox
;
40 // LayoutText is the root class for anything that represents
41 // a text node (see core/dom/Text.h).
43 // This is a common node in the tree so to the limit memory overhead,
44 // this class inherits directly from LayoutObject.
45 // Also this class is used by both CSS and SVG layouts so LayoutObject
46 // was a natural choice.
48 // The actual layout of text is handled by the containing inline
49 // (LayoutInline) or block (LayoutBlock). They will invoke the Unicode
50 // Bidirectional Algorithm to break the text into actual lines.
51 // The result of layout is the line box tree, which represents lines
52 // on the screen. It is stored into m_firstTextBox and m_lastTextBox.
53 // To understand how lines are broken by the bidi algorithm, read e.g.
54 // LayoutBlockFlow::layoutInlineChildren.
57 // ***** LINE BOXES OWNERSHIP *****
58 // m_firstTextBox and m_lastTextBox are not owned by LayoutText
59 // but are pointers into the enclosing inline / block (see LayoutInline's
60 // and LayoutBlock's m_lineBoxes).
63 // This class implements the preferred logical widths computation
64 // for its underlying text. The widths are stored into m_minWidth
65 // and m_maxWidth. They are computed lazily based on
66 // m_preferredLogicalWidthsDirty.
68 // The previous comment applies also for painting. See e.g.
69 // BlockPainter::paintContents in particular the use of LineBoxListPainter.
70 class CORE_EXPORT LayoutText
: public LayoutObject
{
72 // FIXME: If the node argument is not a Text node or the string argument is
73 // not the content of the Text node, updating text-transform property
74 // doesn't re-transform the string.
75 LayoutText(Node
*, PassRefPtr
<StringImpl
>);
77 ~LayoutText() override
;
80 const char* name() const override
{ return "LayoutText"; }
82 virtual bool isTextFragment() const;
83 virtual bool isWordBreak() const;
85 virtual PassRefPtr
<StringImpl
> originalText() const;
87 void extractTextBox(InlineTextBox
*);
88 void attachTextBox(InlineTextBox
*);
89 void removeTextBox(InlineTextBox
*);
91 const String
& text() const { return m_text
; }
92 virtual unsigned textStartOffset() const { return 0; }
93 String
plainText() const;
95 InlineTextBox
* createInlineTextBox(int start
, unsigned short length
);
96 void dirtyOrDeleteLineBoxesIfNeeded(bool fullLayout
);
97 void dirtyLineBoxes();
99 void absoluteRects(Vector
<IntRect
>&, const LayoutPoint
& accumulatedOffset
) const final
;
100 void absoluteRectsForRange(Vector
<IntRect
>&, unsigned startOffset
= 0, unsigned endOffset
= INT_MAX
, bool useSelectionHeight
= false, bool* wasFixed
= nullptr);
102 void absoluteQuads(Vector
<FloatQuad
>&, bool* wasFixed
) const final
;
103 void absoluteQuadsForRange(Vector
<FloatQuad
>&, unsigned startOffset
= 0, unsigned endOffset
= INT_MAX
, bool useSelectionHeight
= false, bool* wasFixed
= nullptr);
105 enum ClippingOption
{ NoClipping
, ClipToEllipsis
};
106 void absoluteQuads(Vector
<FloatQuad
>&, bool* wasFixed
= nullptr, ClippingOption
= NoClipping
) const;
108 PositionWithAffinity
positionForPoint(const LayoutPoint
&) override
;
110 bool is8Bit() const { return m_text
.is8Bit(); }
111 const LChar
* characters8() const { return m_text
.impl()->characters8(); }
112 const UChar
* characters16() const { return m_text
.impl()->characters16(); }
113 bool hasEmptyText() const { return m_text
.isEmpty(); }
114 UChar
characterAt(unsigned) const;
115 UChar
uncheckedCharacterAt(unsigned) const;
116 UChar
operator[](unsigned i
) const { return uncheckedCharacterAt(i
); }
117 unsigned textLength() const { return m_text
.length(); } // non virtual implementation of length()
118 void positionLineBox(InlineBox
*);
120 virtual float width(unsigned from
, unsigned len
, const Font
&, LayoutUnit xPos
, TextDirection
, HashSet
<const SimpleFontData
*>* fallbackFonts
= nullptr, FloatRect
* glyphBounds
= nullptr) const;
121 virtual float width(unsigned from
, unsigned len
, LayoutUnit xPos
, TextDirection
, bool firstLine
= false, HashSet
<const SimpleFontData
*>* fallbackFonts
= nullptr, FloatRect
* glyphBounds
= nullptr) const;
123 float minLogicalWidth() const;
124 float maxLogicalWidth() const;
126 void trimmedPrefWidths(LayoutUnit leadWidth
,
127 LayoutUnit
& firstLineMinWidth
, bool& hasBreakableStart
,
128 LayoutUnit
& lastLineMinWidth
, bool& hasBreakableEnd
,
129 bool& hasBreakableChar
, bool& hasBreak
,
130 LayoutUnit
& firstLineMaxWidth
, LayoutUnit
& lastLineMaxWidth
,
131 LayoutUnit
& minWidth
, LayoutUnit
& maxWidth
, bool& stripFrontSpaces
,
134 virtual IntRect
linesBoundingBox() const;
136 // Returns the bounding box of visual overflow rects of all line boxes.
137 LayoutRect
visualOverflowRect() const;
139 FloatPoint
firstRunOrigin() const;
140 float firstRunX() const;
141 float firstRunY() const;
143 virtual void setText(PassRefPtr
<StringImpl
>, bool force
= false);
144 void setTextWithOffset(PassRefPtr
<StringImpl
>, unsigned offset
, unsigned len
, bool force
= false);
146 virtual void transformText();
148 bool canBeSelectionLeaf() const override
{ return true; }
149 void setSelectionState(SelectionState
) final
;
150 LayoutRect
selectionRectForPaintInvalidation(const LayoutBoxModelObject
* paintInvalidationContainer
) const override
;
151 LayoutRect
localCaretRect(InlineBox
*, int caretOffset
, LayoutUnit
* extraWidthToEndOfLine
= nullptr) override
;
153 InlineTextBox
* firstTextBox() const { return m_firstTextBox
; }
154 InlineTextBox
* lastTextBox() const { return m_lastTextBox
; }
156 int caretMinOffset() const override
;
157 int caretMaxOffset() const override
;
158 unsigned resolvedTextLength() const;
160 int previousOffset(int current
) const final
;
161 int previousOffsetForBackwardDeletion(int current
) const final
;
162 int nextOffset(int current
) const final
;
164 bool containsReversedText() const { return m_containsReversedText
; }
166 bool isSecure() const { return style()->textSecurity() != TSNONE
; }
167 void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset
);
169 bool isAllCollapsibleWhitespace() const;
170 bool isRenderedCharacter(int offsetInNode
) const;
172 bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath
; }
174 void removeAndDestroyTextBoxes();
176 PassRefPtr
<AbstractInlineTextBox
> firstAbstractInlineTextBox();
178 float hyphenWidth(const Font
&, TextDirection
);
181 void willBeDestroyed() override
;
183 void styleWillChange(StyleDifference
, const ComputedStyle
&) final
{ }
184 void styleDidChange(StyleDifference
, const ComputedStyle
* oldStyle
) override
;
186 virtual void setTextInternal(PassRefPtr
<StringImpl
>);
187 virtual UChar
previousCharacter() const;
189 void addLayerHitTestRects(LayerHitTestRects
&, const DeprecatedPaintLayer
* currentLayer
, const LayoutPoint
& layerOffset
, const LayoutRect
& containerRect
) const override
;
191 virtual InlineTextBox
* createTextBox(int start
, unsigned short length
); // Subclassed by SVG.
193 void invalidateDisplayItemClients(const LayoutBoxModelObject
& paintInvalidationContainer
) const override
;
196 void computePreferredLogicalWidths(float leadWidth
);
197 void computePreferredLogicalWidths(float leadWidth
, HashSet
<const SimpleFontData
*>& fallbackFonts
, FloatRect
& glyphBounds
);
199 bool computeCanUseSimpleFontCodePath() const;
201 // Make length() private so that callers that have a LayoutText*
202 // will use the more efficient textLength() instead, while
203 // callers with a LayoutObject* can continue to use length().
204 unsigned length() const final
{ return textLength(); }
206 // See the class comment as to why we shouldn't call this function directly.
207 void paint(const PaintInfo
&, const LayoutPoint
&) final
{ ASSERT_NOT_REACHED(); }
208 void layout() final
{ ASSERT_NOT_REACHED(); }
209 bool nodeAtPoint(HitTestResult
&, const HitTestLocation
&, const LayoutPoint
&, HitTestAction
) final
{ ASSERT_NOT_REACHED(); return false; }
211 void deleteTextBoxes();
212 bool containsOnlyWhitespace(unsigned from
, unsigned len
) const;
213 float widthFromFont(const Font
&, int start
, int len
, float leadWidth
, float textWidthSoFar
, TextDirection
, HashSet
<const SimpleFontData
*>* fallbackFonts
, FloatRect
* glyphBoundsAccumulation
) const;
215 void secureText(UChar mask
);
217 bool isText() const = delete; // This will catch anyone doing an unnecessary check.
219 LayoutRect
clippedOverflowRectForPaintInvalidation(const LayoutBoxModelObject
* paintInvalidationContainer
, const PaintInvalidationState
* = nullptr) const override
;
221 void checkConsistency() const;
223 // We put the bitfield first to minimize padding on 64-bit.
224 bool m_hasBreakableChar
: 1; // Whether or not we can be broken into multiple lines.
225 bool m_hasBreak
: 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
226 bool m_hasTab
: 1; // Whether or not we have a variable width tab character (e.g., <pre> with '\t').
227 bool m_hasBreakableStart
: 1;
228 bool m_hasBreakableEnd
: 1;
229 bool m_hasEndWhiteSpace
: 1;
230 // This bit indicates that the text run has already dirtied specific
231 // line boxes, and this hint will enable layoutInlineChildren to avoid
232 // just dirtying everything when character data is modified (e.g., appended/inserted
234 bool m_linesDirty
: 1;
235 bool m_containsReversedText
: 1;
236 bool m_canUseSimpleFontCodePath
: 1;
237 mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts
: 1;
241 float m_firstLineMinWidth
;
242 float m_lastLineLineMinWidth
;
246 // The line boxes associated with this object.
247 // Read the LINE BOXES OWNERSHIP section in the class header comment.
248 InlineTextBox
* m_firstTextBox
;
249 InlineTextBox
* m_lastTextBox
;
252 inline UChar
LayoutText::uncheckedCharacterAt(unsigned i
) const
254 ASSERT_WITH_SECURITY_IMPLICATION(i
< textLength());
255 return is8Bit() ? characters8()[i
] : characters16()[i
];
258 inline UChar
LayoutText::characterAt(unsigned i
) const
260 if (i
>= textLength())
263 return uncheckedCharacterAt(i
);
266 inline float LayoutText::hyphenWidth(const Font
& font
, TextDirection direction
)
268 const ComputedStyle
& style
= styleRef();
269 return font
.width(constructTextRun(font
, style
.hyphenString().string(), style
, direction
));
272 DEFINE_LAYOUT_OBJECT_TYPE_CASTS(LayoutText
, isText());
275 inline void LayoutText::checkConsistency() const
280 inline LayoutText
* Text::layoutObject() const
282 return toLayoutText(CharacterData::layoutObject());
285 void applyTextTransform(const ComputedStyle
*, String
&, UChar
);
289 #endif // LayoutText_h