1/*
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 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/rendering/RenderObject.h"
27#include "platform/LengthFunctions.h"
28#include "platform/text/TextPath.h"
29#include "wtf/Forward.h"
30#include "wtf/PassRefPtr.h"
31
32namespace WebCore {
33
34class AbstractInlineTextBox;
35class InlineTextBox;
36
37class RenderText : public RenderObject {
38public:
39    // FIXME: If the node argument is not a Text node or the string argument is
40    // not the content of the Text node, updating text-transform property
41    // doesn't re-transform the string.
42    RenderText(Node*, PassRefPtr<StringImpl>);
43#ifndef NDEBUG
44    virtual ~RenderText();
45#endif
46
47    virtual const char* renderName() const OVERRIDE;
48
49    virtual bool isTextFragment() const;
50    virtual bool isWordBreak() const;
51
52    virtual PassRefPtr<StringImpl> originalText() const;
53
54    void extractTextBox(InlineTextBox*);
55    void attachTextBox(InlineTextBox*);
56    void removeTextBox(InlineTextBox*);
57
58    const String& text() const { return m_text; }
59    virtual unsigned textStartOffset() const { return 0; }
60    String plainText() const;
61
62    InlineTextBox* createInlineTextBox();
63    void dirtyLineBoxes(bool fullLayout);
64
65    virtual void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const OVERRIDE FINAL;
66    void absoluteRectsForRange(Vector<IntRect>&, unsigned startOffset = 0, unsigned endOffset = INT_MAX, bool useSelectionHeight = false, bool* wasFixed = 0);
67
68    virtual void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const OVERRIDE FINAL;
69    void absoluteQuadsForRange(Vector<FloatQuad>&, unsigned startOffset = 0, unsigned endOffset = INT_MAX, bool useSelectionHeight = false, bool* wasFixed = 0);
70
71    enum ClippingOption { NoClipping, ClipToEllipsis };
72    void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed = 0, ClippingOption = NoClipping) const;
73
74    virtual PositionWithAffinity positionForPoint(const LayoutPoint&) OVERRIDE;
75
76    bool is8Bit() const { return m_text.is8Bit(); }
77    const LChar* characters8() const { return m_text.impl()->characters8(); }
78    const UChar* characters16() const { return m_text.impl()->characters16(); }
79    bool hasEmptyText() const { return m_text.isEmpty(); }
80    String substring(unsigned position, unsigned length) const { return m_text.substring(position, length); }
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*);
86
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;
89
90    float minLogicalWidth() const;
91    float maxLogicalWidth() const;
92
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,
99        TextDirection);
100
101    virtual IntRect linesBoundingBox() const;
102    LayoutRect linesVisualOverflowBoundingBox() const;
103
104    FloatPoint firstRunOrigin() const;
105    float firstRunX() const;
106    float firstRunY() const;
107
108    virtual void setText(PassRefPtr<StringImpl>, bool force = false);
109    void setTextWithOffset(PassRefPtr<StringImpl>, unsigned offset, unsigned len, bool force = false);
110
111    virtual void transformText();
112
113    virtual bool canBeSelectionLeaf() const OVERRIDE { return true; }
114    virtual void setSelectionState(SelectionState s) OVERRIDE FINAL;
115    virtual LayoutRect selectionRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer, bool clipToVisibleContent = true) OVERRIDE;
116    virtual LayoutRect localCaretRect(InlineBox*, int caretOffset, LayoutUnit* extraWidthToEndOfLine = 0) OVERRIDE;
117
118    LayoutUnit marginLeft() const { return minimumValueForLength(style()->marginLeft(), 0); }
119    LayoutUnit marginRight() const { return minimumValueForLength(style()->marginRight(), 0); }
120
121    virtual LayoutRect clippedOverflowRectForPaintInvalidation(const RenderLayerModelObject* paintInvalidationContainer) const OVERRIDE FINAL;
122
123    InlineTextBox* firstTextBox() const { return m_firstTextBox; }
124    InlineTextBox* lastTextBox() const { return m_lastTextBox; }
125
126    virtual int caretMinOffset() const OVERRIDE;
127    virtual int caretMaxOffset() const OVERRIDE;
128    unsigned renderedTextLength() const;
129
130    virtual int previousOffset(int current) const OVERRIDE FINAL;
131    virtual int previousOffsetForBackwardDeletion(int current) const OVERRIDE FINAL;
132    virtual int nextOffset(int current) const OVERRIDE FINAL;
133
134    bool containsReversedText() const { return m_containsReversedText; }
135
136    bool isSecure() const { return style()->textSecurity() != TSNONE; }
137    void momentarilyRevealLastTypedCharacter(unsigned lastTypedCharacterOffset);
138
139    void checkConsistency() const;
140
141    bool isAllCollapsibleWhitespace() const;
142
143    bool canUseSimpleFontCodePath() const { return m_canUseSimpleFontCodePath; }
144    bool knownToHaveNoOverflowAndNoFallbackFonts() const { return m_knownToHaveNoOverflowAndNoFallbackFonts; }
145
146    void removeAndDestroyTextBoxes();
147
148    PassRefPtr<AbstractInlineTextBox> firstAbstractInlineTextBox();
149
150protected:
151    virtual void computePreferredLogicalWidths(float leadWidth);
152    virtual void willBeDestroyed() OVERRIDE;
153
154    virtual void styleWillChange(StyleDifference, const RenderStyle&) OVERRIDE FINAL { }
155    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) OVERRIDE;
156
157    virtual void setTextInternal(PassRefPtr<StringImpl>);
158    virtual UChar previousCharacter() const;
159
160    virtual void addLayerHitTestRects(LayerHitTestRects&, const RenderLayer* currentLayer, const LayoutPoint& layerOffset, const LayoutRect& containerRect) const OVERRIDE;
161
162    virtual InlineTextBox* createTextBox(); // Subclassed by SVG.
163
164private:
165    void computePreferredLogicalWidths(float leadWidth, HashSet<const SimpleFontData*>& fallbackFonts, GlyphOverflow&);
166
167    bool computeCanUseSimpleFontCodePath() const;
168
169    // Make length() private so that callers that have a RenderText*
170    // will use the more efficient textLength() instead, while
171    // callers with a RenderObject* can continue to use length().
172    virtual unsigned length() const OVERRIDE FINAL { return textLength(); }
173
174    virtual void paint(PaintInfo&, const LayoutPoint&) OVERRIDE FINAL { ASSERT_NOT_REACHED(); }
175    virtual void layout() OVERRIDE FINAL { ASSERT_NOT_REACHED(); }
176    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation&, const LayoutPoint&, HitTestAction) OVERRIDE FINAL { ASSERT_NOT_REACHED(); return false; }
177
178    void deleteTextBoxes();
179    bool containsOnlyWhitespace(unsigned from, unsigned len) const;
180    float widthFromCache(const Font&, int start, int len, float xPos, TextDirection, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow*) const;
181    bool isAllASCII() const { return m_isAllASCII; }
182
183    void secureText(UChar mask);
184
185    // We put the bitfield first to minimize padding on 64-bit.
186    bool m_hasBreakableChar : 1; // Whether or not we can be broken into multiple lines.
187    bool m_hasBreak : 1; // Whether or not we have a hard break (e.g., <pre> with '\n').
188    bool m_hasTab : 1; // Whether or not we have a variable width tab character (e.g., <pre> with '\t').
189    bool m_hasBreakableStart : 1;
190    bool m_hasBreakableEnd : 1;
191    bool m_hasEndWhiteSpace : 1;
192    bool m_linesDirty : 1; // This bit indicates that the text run has already dirtied specific
193                           // line boxes, and this hint will enable layoutInlineChildren to avoid
194                           // just dirtying everything when character data is modified (e.g., appended/inserted
195                           // or removed).
196    bool m_containsReversedText : 1;
197    bool m_isAllASCII : 1;
198    bool m_canUseSimpleFontCodePath : 1;
199    mutable bool m_knownToHaveNoOverflowAndNoFallbackFonts : 1;
200
201    float m_minWidth;
202    float m_maxWidth;
203    float m_firstLineMinWidth;
204    float m_lastLineLineMinWidth;
205
206    String m_text;
207
208    InlineTextBox* m_firstTextBox;
209    InlineTextBox* m_lastTextBox;
210};
211
212inline UChar RenderText::uncheckedCharacterAt(unsigned i) const
213{
214    ASSERT_WITH_SECURITY_IMPLICATION(i < textLength());
215    return is8Bit() ? characters8()[i] : characters16()[i];
216}
217
218inline UChar RenderText::characterAt(unsigned i) const
219{
220    if (i >= textLength())
221        return 0;
222
223    return uncheckedCharacterAt(i);
224}
225
226DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderText, isText());
227
228#ifdef NDEBUG
229inline void RenderText::checkConsistency() const
230{
231}
232#endif
233
234void applyTextTransform(const RenderStyle*, String&, UChar);
235
236} // namespace WebCore
237
238#endif // RenderText_h
239