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