1/*
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.
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/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"
32
33namespace blink {
34
35class AbstractInlineTextBox;
36class InlineTextBox;
37
38class RenderText : public RenderObject {
39public:
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>);
44#if ENABLE(ASSERT)
45    virtual ~RenderText();
46#endif
47
48    virtual const char* renderName() const OVERRIDE;
49
50    virtual bool isTextFragment() const;
51    virtual bool isWordBreak() const;
52
53    virtual PassRefPtr<StringImpl> originalText() const;
54
55    void extractTextBox(InlineTextBox*);
56    void attachTextBox(InlineTextBox*);
57    void removeTextBox(InlineTextBox*);
58
59    const String& text() const { return m_text; }
60    virtual unsigned textStartOffset() const { return 0; }
61    String plainText() const;
62
63    InlineTextBox* createInlineTextBox();
64    void dirtyLineBoxes(bool fullLayout);
65
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);
68
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);
71
72    enum ClippingOption { NoClipping, ClipToEllipsis };
73    void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed = 0, ClippingOption = NoClipping) const;
74
75    virtual PositionWithAffinity positionForPoint(const LayoutPoint&) OVERRIDE;
76
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*);
87
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;
90
91    float minLogicalWidth() const;
92    float maxLogicalWidth() const;
93
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,
100        TextDirection);
101
102    virtual IntRect linesBoundingBox() const;
103    LayoutRect linesVisualOverflowBoundingBox() const;
104
105    FloatPoint firstRunOrigin() const;
106    float firstRunX() const;
107    float firstRunY() const;
108
109    virtual void setText(PassRefPtr<StringImpl>, bool force = false);
110    void setTextWithOffset(PassRefPtr<StringImpl>, unsigned offset, unsigned len, bool force = false);
111
112    virtual void transformText();
113
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;
118
119    LayoutUnit marginLeft() const { return minimumValueForLength(style()->marginLeft(), 0); }
120    LayoutUnit marginRight() const { return minimumValueForLength(style()->marginRight(), 0); }
121
122    InlineTextBox* firstTextBox() const { return m_firstTextBox; }
123    InlineTextBox* lastTextBox() const { return m_lastTextBox; }
124
125    virtual int caretMinOffset() const OVERRIDE;
126    virtual int caretMaxOffset() const OVERRIDE;
127    unsigned renderedTextLength() const;
128
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;
132
133    bool containsReversedText() const { return m_containsReversedText; }
134
135    bool isSecure() const { return style()->textSecurity() != TSNONE; }
136    void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);
137
138    void checkConsistency() const;
139
140    bool isAllCollapsibleWhitespace() const;
141
142    bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath; }
143
144    void removeAndDestroyTextBoxes();
145
146    PassRefPtr<AbstractInlineTextBox> firstAbstractInlineTextBox();
147
148protected:
149    virtual void willBeDestroyed() OVERRIDE;
150
151    virtual void styleWillChange(StyleDifference, const RenderStyle&) OVERRIDE FINAL { }
152    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) OVERRIDE;
153
154    virtual void setTextInternal(PassRefPtr<StringImpl>);
155    virtual UChar previousCharacter() const;
156
157    virtual void addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const OVERRIDE;
158
159    virtual InlineTextBox* createTextBox(); // Subclassed by SVG.
160
161private:
162    void computePreferredLogicalWidths(float leadWidth);
163    void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
164
165    bool computeCanUseSimpleFontCodePath() const;
166
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(); }
171
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; }
175
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; }
180
181    void secureText(UChar mask);
182
183    bool isText() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
184
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; }
188
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
199                           // or removed).
200    bool m_containsReversedText : 1;
201    bool m_isAllASCII : 1;
202    bool m_canUseSimpleFontCodePath : 1;
203    mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
204
205    float m_minWidth;
206    float m_maxWidth;
207    float m_firstLineMinWidth;
208    float m_lastLineLineMinWidth;
209
210    String m_text;
211
212    InlineTextBox* m_firstTextBox;
213    InlineTextBox* m_lastTextBox;
214};
215
216inline UChar RenderText::uncheckedCharacterAt(unsigned i) const
217{
218    ASSERT_WITH_SECURITY_IMPLICATION(i < textLength());
219    return is8Bit() ? characters8()[i] : characters16()[i];
220}
221
222inline UChar RenderText::characterAt(unsigned i) const
223{
224    if (i >= textLength())
225        return 0;
226
227    return uncheckedCharacterAt(i);
228}
229
230DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderText, isText());
231
232#if !ENABLE(ASSERT)
233inline void RenderText::checkConsistency() const
234{
235}
236#endif
237
238inline RenderText* Text::renderer() const
239{
240    return toRenderText(CharacterData::renderer());
241}
242
243void applyTextTransform(const RenderStyle*, String&, UChar);
244
245} // namespace blink
246
247#endif // RenderText_h
248