Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/rendering/InlineBox.h"
27 #include "core/rendering/RenderText.h" // so textRenderer() can be inline
28 #include "platform/fonts/TextBlob.h"
29 #include "platform/text/TextRun.h"
30 #include "wtf/Forward.h"
31
32 namespace blink {
33
34 struct CompositionUnderline;
35 class DocumentMarker;
36 class GraphicsContext;
37
38 const unsigned short cNoTruncation = USHRT_MAX;
39 const unsigned short cFullTruncation = USHRT_MAX - 1;
40
41 class InlineTextBox : public InlineBox {
42 public:
43     InlineTextBox(RenderObject& obj)
44         : InlineBox(obj)
45         , m_prevTextBox(0)
46         , m_nextTextBox(0)
47         , m_start(0)
48         , m_len(0)
49         , m_truncation(cNoTruncation)
50     {
51     }
52
53     RenderText& renderer() const { return toRenderText(InlineBox::renderer()); }
54
55     virtual void destroy() OVERRIDE FINAL;
56
57     InlineTextBox* prevTextBox() const { return m_prevTextBox; }
58     InlineTextBox* nextTextBox() const { return m_nextTextBox; }
59     void setNextTextBox(InlineTextBox* n) { m_nextTextBox = n; }
60     void setPreviousTextBox(InlineTextBox* p) { m_prevTextBox = p; }
61
62     // FIXME: These accessors should ASSERT(!isDirty()). See https://bugs.webkit.org/show_bug.cgi?id=97264
63     unsigned start() const { return m_start; }
64     unsigned end() const { return m_len ? m_start + m_len - 1 : m_start; }
65     unsigned len() const { return m_len; }
66
67     void setStart(unsigned start) { m_start = start; }
68     void setLen(unsigned len) { m_len = len; }
69
70     void offsetRun(int d) { ASSERT(!isDirty()); m_start += d; }
71
72     unsigned short truncation() { return m_truncation; }
73
74     virtual void markDirty() OVERRIDE FINAL;
75
76     using InlineBox::hasHyphen;
77     using InlineBox::setHasHyphen;
78     using InlineBox::canHaveLeadingExpansion;
79     using InlineBox::setCanHaveLeadingExpansion;
80
81     static inline bool compareByStart(const InlineTextBox* first, const InlineTextBox* second) { return first->start() < second->start(); }
82
83     virtual int baselinePosition(FontBaseline) const OVERRIDE FINAL;
84     virtual LayoutUnit lineHeight() const OVERRIDE FINAL;
85
86     bool getEmphasisMarkPosition(RenderStyle*, TextEmphasisPosition&) const;
87
88     LayoutRect logicalOverflowRect() const;
89     void setLogicalOverflowRect(const LayoutRect&);
90     LayoutUnit logicalTopVisualOverflow() const { return logicalOverflowRect().y(); }
91     LayoutUnit logicalBottomVisualOverflow() const { return logicalOverflowRect().maxY(); }
92
93 #ifndef NDEBUG
94     virtual void showBox(int = 0) const OVERRIDE;
95     virtual const char* boxName() const OVERRIDE;
96 #endif
97
98     enum RotationDirection { Counterclockwise, Clockwise };
99     static AffineTransform rotation(const FloatRect& boxRect, RotationDirection);
100 private:
101     LayoutUnit selectionTop();
102     LayoutUnit selectionBottom();
103     LayoutUnit selectionHeight();
104
105     // charactersWithHyphen, if provided, must not be destroyed before the TextRun.
106     TextRun constructTextRun(RenderStyle*, const Font&, StringBuilder* charactersWithHyphen = 0) const;
107     TextRun constructTextRun(RenderStyle*, const Font&, StringView, int maximumLength, StringBuilder* charactersWithHyphen = 0) const;
108
109 public:
110     TextRun constructTextRunForInspector(RenderStyle*, const Font&) const;
111     virtual FloatRect calculateBoundaries() const OVERRIDE { return FloatRect(x(), y(), width(), height()); }
112
113     virtual LayoutRect localSelectionRect(int startPos, int endPos);
114     bool isSelected(int startPos, int endPos) const;
115     void selectionStartEnd(int& sPos, int& ePos) const;
116
117 protected:
118     virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom) OVERRIDE;
119     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom) OVERRIDE;
120
121 private:
122     virtual void deleteLine() OVERRIDE FINAL;
123     virtual void extractLine() OVERRIDE FINAL;
124     virtual void attachLine() OVERRIDE FINAL;
125
126 public:
127     virtual RenderObject::SelectionState selectionState() const OVERRIDE FINAL;
128
129 private:
130     virtual void clearTruncation() OVERRIDE FINAL { m_truncation = cNoTruncation; }
131     virtual float placeEllipsisBox(bool flowIsLTR, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox) OVERRIDE FINAL;
132
133 public:
134     virtual bool isLineBreak() const OVERRIDE FINAL;
135
136     void setExpansion(int newExpansion)
137     {
138         m_logicalWidth -= expansion();
139         InlineBox::setExpansion(newExpansion);
140         m_logicalWidth += newExpansion;
141     }
142
143 private:
144     virtual bool isInlineTextBox() const OVERRIDE FINAL { return true; }
145
146 public:
147     virtual int caretMinOffset() const OVERRIDE FINAL;
148     virtual int caretMaxOffset() const OVERRIDE FINAL;
149
150 private:
151     float textPos() const; // returns the x position relative to the left start of the text line.
152
153 public:
154     virtual int offsetForPosition(float x, bool includePartialGlyphs = true) const;
155     virtual float positionForOffset(int offset) const;
156
157     bool containsCaretOffset(int offset) const; // false for offset after line break
158
159     // Fills a vector with the pixel width of each character.
160     void characterWidths(Vector<float>&) const;
161
162 private:
163     InlineTextBox* m_prevTextBox; // The previous box that also uses our RenderObject
164     InlineTextBox* m_nextTextBox; // The next box that also uses our RenderObject
165
166     int m_start;
167     unsigned short m_len;
168
169     unsigned short m_truncation; // Where to truncate when text overflow is applied.  We use special constants to
170                       // denote no truncation (the whole run paints) and full truncation (nothing paints at all).
171
172     unsigned underlinePaintStart(const CompositionUnderline&);
173     unsigned underlinePaintEnd(const CompositionUnderline&);
174
175 protected:
176     void paintSingleCompositionBackgroundRun(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, Color backgroundColor, int startPos, int endPos);
177     void paintCompositionBackgrounds(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, bool useCustomUnderlines);
178     void paintDocumentMarkers(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, bool background);
179     void paintCompositionUnderline(GraphicsContext*, const FloatPoint& boxOrigin, const CompositionUnderline&);
180
181     // These functions both paint markers and update the DocumentMarker's renderedRect.
182     virtual void paintDocumentMarker(GraphicsContext*, const FloatPoint& boxOrigin, DocumentMarker*, RenderStyle*, const Font&, bool grammar);
183     virtual void paintTextMatchMarker(GraphicsContext*, const FloatPoint& boxOrigin, DocumentMarker*, RenderStyle*, const Font&);
184
185 private:
186     void paintDecoration(GraphicsContext*, const FloatPoint& boxOrigin, TextDecoration);
187     void paintSelection(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, Color textColor);
188
189     TextRun::ExpansionBehavior expansionBehavior() const
190     {
191         return (canHaveLeadingExpansion() ? TextRun::AllowLeadingExpansion : TextRun::ForbidLeadingExpansion)
192             | (expansion() && nextLeafChild() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion);
193     }
194 };
195
196 DEFINE_INLINE_BOX_TYPE_CASTS(InlineTextBox);
197
198 void alignSelectionRectToDevicePixels(FloatRect&);
199
200 inline AffineTransform InlineTextBox::rotation(const FloatRect& boxRect, RotationDirection rotationDirection)
201 {
202     return rotationDirection == Clockwise ? AffineTransform(0, 1, -1, 0, boxRect.x() + boxRect.maxY(), boxRect.maxY() - boxRect.x())
203         : AffineTransform(0, -1, 1, 0, boxRect.x() - boxRect.maxY(), boxRect.x() + boxRect.maxY());
204 }
205
206 } // namespace blink
207
208 #endif // InlineTextBox_h