tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / rendering / InlineTextBox.h
1 /*
2  * (C) 1999 Lars Knoll (knoll@kde.org)
3  * (C) 2000 Dirk Mueller (mueller@kde.org)
4  * Copyright (C) 2004, 2005, 2006, 2009, 2010, 2011 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 InlineTextBox_h
24 #define InlineTextBox_h
25
26 #include "InlineBox.h"
27 #include "RenderText.h" // so textRenderer() can be inline
28 #include "TextRun.h"
29 #include <wtf/text/StringBuilder.h>
30
31 namespace WebCore {
32
33 struct CompositionUnderline;
34 class DocumentMarker;
35
36 const unsigned short cNoTruncation = USHRT_MAX;
37 const unsigned short cFullTruncation = USHRT_MAX - 1;
38
39 class BufferForAppendingHyphen : public StringBuilder {
40 public:
41     BufferForAppendingHyphen() { reserveCapacity(256); }
42 };
43
44 // Helper functions shared by InlineTextBox / SVGRootInlineBox
45 void updateGraphicsContext(GraphicsContext*, const Color& fillColor, const Color& strokeColor, float strokeThickness, ColorSpace);
46 Color correctedTextColor(Color textColor, Color backgroundColor);
47
48 class InlineTextBox : public InlineBox {
49 public:
50     InlineTextBox(RenderObject* obj)
51         : InlineBox(obj)
52         , m_prevTextBox(0)
53         , m_nextTextBox(0)
54         , m_start(0)
55         , m_len(0)
56         , m_truncation(cNoTruncation)
57     {
58     }
59
60     virtual void destroy(RenderArena*);
61
62     InlineTextBox* prevTextBox() const { return m_prevTextBox; }
63     InlineTextBox* nextTextBox() const { return m_nextTextBox; }
64     void setNextTextBox(InlineTextBox* n) { m_nextTextBox = n; }
65     void setPreviousTextBox(InlineTextBox* p) { m_prevTextBox = p; }
66
67     unsigned start() const { return m_start; }
68     unsigned end() const { return m_len ? m_start + m_len - 1 : m_start; }
69     unsigned len() const { return m_len; }
70
71     void setStart(unsigned start) { m_start = start; }
72     void setLen(unsigned len) { m_len = len; }
73
74     void offsetRun(int d) { m_start += d; }
75
76     unsigned short truncation() { return m_truncation; }
77
78     bool hasHyphen() const { return m_hasEllipsisBoxOrHyphen; }
79     void setHasHyphen(bool hasHyphen) { m_hasEllipsisBoxOrHyphen = hasHyphen; }
80
81     bool canHaveLeadingExpansion() const { return m_hasSelectedChildrenOrCanHaveLeadingExpansion; }
82     void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_hasSelectedChildrenOrCanHaveLeadingExpansion = canHaveLeadingExpansion; }
83
84     static inline bool compareByStart(const InlineTextBox* first, const InlineTextBox* second) { return first->start() < second->start(); }
85
86     virtual LayoutUnit baselinePosition(FontBaseline) const;
87     virtual LayoutUnit lineHeight() const;
88
89     bool getEmphasisMarkPosition(RenderStyle*, TextEmphasisPosition&) const;
90
91     LayoutRect logicalOverflowRect() const;
92     void setLogicalOverflowRect(const LayoutRect&);
93     LayoutUnit logicalTopVisualOverflow() const { return logicalOverflowRect().y(); }
94     LayoutUnit logicalBottomVisualOverflow() const { return logicalOverflowRect().maxY(); }
95     LayoutUnit logicalLeftVisualOverflow() const { return logicalOverflowRect().x(); }
96     LayoutUnit logicalRightVisualOverflow() const { return logicalOverflowRect().maxX(); }
97
98 #ifndef NDEBUG
99     virtual void showBox(int = 0) const;
100     virtual const char* boxName() const;
101 #endif
102 private:
103     LayoutUnit selectionTop();
104     LayoutUnit selectionBottom();
105     LayoutUnit selectionHeight();
106
107     TextRun constructTextRun(RenderStyle*, const Font&, BufferForAppendingHyphen* = 0) const;
108     TextRun constructTextRun(RenderStyle*, const Font&, const UChar*, int length, int maximumLength, BufferForAppendingHyphen* = 0) const;
109
110 public:
111     virtual FloatRect calculateBoundaries() const { return FloatRect(x(), y(), width(), height()); }
112
113     virtual IntRect localSelectionRect(int startPos, int endPos);
114     bool isSelected(int startPos, int endPos) const;
115     void selectionStartEnd(int& sPos, int& ePos);
116
117 protected:
118     virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom);
119     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom);
120
121 public:
122     RenderText* textRenderer() const;
123
124 private:
125     virtual void deleteLine(RenderArena*);
126     virtual void extractLine();
127     virtual void attachLine();
128
129 public:
130     virtual RenderObject::SelectionState selectionState();
131
132 private:
133     virtual void clearTruncation() { m_truncation = cNoTruncation; }
134     virtual float placeEllipsisBox(bool flowIsLTR, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, bool& foundBox);
135
136 public:
137     virtual bool isLineBreak() const;
138
139     void setExpansion(int expansion) { m_logicalWidth -= m_expansion; m_expansion = expansion; m_logicalWidth += m_expansion; }
140
141 private:
142     virtual bool isInlineTextBox() const { return true; }    
143
144 public:
145     virtual int caretMinOffset() const;
146     virtual int caretMaxOffset() const;
147
148 private:
149     float textPos() const; // returns the x position relative to the left start of the text line.
150
151 public:
152     virtual int offsetForPosition(float x, bool includePartialGlyphs = true) const;
153     virtual float positionForOffset(int offset) const;
154
155     bool containsCaretOffset(int offset) const; // false for offset after line break
156
157     // Needs to be public, so the static paintTextWithShadows() function can use it.
158     static FloatSize applyShadowToGraphicsContext(GraphicsContext*, const ShadowData*, const FloatRect& textRect, bool stroked, bool opaque, bool horizontal);
159
160 private:
161     InlineTextBox* m_prevTextBox; // The previous box that also uses our RenderObject
162     InlineTextBox* m_nextTextBox; // The next box that also uses our RenderObject
163
164     int m_start;
165     unsigned short m_len;
166
167     unsigned short m_truncation; // Where to truncate when text overflow is applied.  We use special constants to
168                       // denote no truncation (the whole run paints) and full truncation (nothing paints at all).
169
170 protected:
171     void paintCompositionBackground(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, int startPos, int endPos);
172     void paintDocumentMarkers(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, bool background);
173     void paintCompositionUnderline(GraphicsContext*, const FloatPoint& boxOrigin, const CompositionUnderline&);
174 #if PLATFORM(MAC)
175     void paintCustomHighlight(const LayoutPoint&, const AtomicString& type);
176 #endif
177
178 private:
179     void paintDecoration(GraphicsContext*, const FloatPoint& boxOrigin, int decoration, const ShadowData*);
180     void paintSelection(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&);
181     void paintSpellingOrGrammarMarker(GraphicsContext*, const FloatPoint& boxOrigin, DocumentMarker*, RenderStyle*, const Font&, bool grammar);
182     void paintTextMatchMarker(GraphicsContext*, const FloatPoint& boxOrigin, DocumentMarker*, RenderStyle*, const Font&);
183     void computeRectForReplacementMarker(DocumentMarker*, RenderStyle*, const Font&);
184
185     TextRun::ExpansionBehavior expansionBehavior() const
186     {
187         return (canHaveLeadingExpansion() ? TextRun::AllowLeadingExpansion : TextRun::ForbidLeadingExpansion)
188             | (m_expansion && nextLeafChild() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion);
189     }
190 };
191
192 inline InlineTextBox* toInlineTextBox(InlineBox* inlineBox)
193 {
194     ASSERT(!inlineBox || inlineBox->isInlineTextBox());
195     return static_cast<InlineTextBox*>(inlineBox);
196 }
197
198 inline const InlineTextBox* toInlineTextBox(const InlineBox* inlineBox)
199 {
200     ASSERT(!inlineBox || inlineBox->isInlineTextBox());
201     return static_cast<const InlineTextBox*>(inlineBox);
202 }
203
204 // This will catch anyone doing an unnecessary cast.
205 void toInlineTextBox(const InlineTextBox*);
206
207 inline RenderText* InlineTextBox::textRenderer() const
208 {
209     return toRenderText(renderer());
210 }
211
212 } // namespace WebCore
213
214 #endif // InlineTextBox_h