blob: 69b2a1d756b7b46c8c1a6644f7f9326de83774fd [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010026#include "core/rendering/InlineBox.h"
27#include "core/rendering/RenderText.h" // so textRenderer() can be inline
Torne (Richard Coles)09380292014-02-21 12:17:33 +000028#include "platform/graphics/GraphicsContext.h"
Torne (Richard Coles)a854de02013-12-18 16:25:25 +000029#include "platform/text/TextRun.h"
Ben Murdoch3c9e4ae2013-08-12 14:20:44 +010030#include "wtf/Forward.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010031
32namespace WebCore {
33
34struct CompositionUnderline;
35class DocumentMarker;
36
37const unsigned short cNoTruncation = USHRT_MAX;
38const unsigned short cFullTruncation = USHRT_MAX - 1;
39
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040// Helper functions shared by InlineTextBox / SVGRootInlineBox
41void updateGraphicsContext(GraphicsContext*, const Color& fillColor, const Color& strokeColor, float strokeThickness, ColorSpace);
42Color correctedTextColor(Color textColor, Color backgroundColor);
43
44class InlineTextBox : public InlineBox {
45public:
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000046 InlineTextBox(RenderObject& obj)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010047 : InlineBox(obj)
48 , m_prevTextBox(0)
49 , m_nextTextBox(0)
50 , m_start(0)
51 , m_len(0)
52 , m_truncation(cNoTruncation)
53 {
54 }
55
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010056 virtual void destroy() OVERRIDE FINAL;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010057
58 InlineTextBox* prevTextBox() const { return m_prevTextBox; }
59 InlineTextBox* nextTextBox() const { return m_nextTextBox; }
60 void setNextTextBox(InlineTextBox* n) { m_nextTextBox = n; }
61 void setPreviousTextBox(InlineTextBox* p) { m_prevTextBox = p; }
62
63 // FIXME: These accessors should ASSERT(!isDirty()). See https://bugs.webkit.org/show_bug.cgi?id=97264
64 unsigned start() const { return m_start; }
65 unsigned end() const { return m_len ? m_start + m_len - 1 : m_start; }
66 unsigned len() const { return m_len; }
67
68 void setStart(unsigned start) { m_start = start; }
69 void setLen(unsigned len) { m_len = len; }
70
71 void offsetRun(int d) { ASSERT(!isDirty()); m_start += d; }
72
73 unsigned short truncation() { return m_truncation; }
74
75 virtual void markDirty(bool dirty = true) OVERRIDE FINAL;
76
77 using InlineBox::hasHyphen;
78 using InlineBox::setHasHyphen;
79 using InlineBox::canHaveLeadingExpansion;
80 using InlineBox::setCanHaveLeadingExpansion;
81
82 static inline bool compareByStart(const InlineTextBox* first, const InlineTextBox* second) { return first->start() < second->start(); }
83
84 virtual int baselinePosition(FontBaseline) const OVERRIDE FINAL;
85 virtual LayoutUnit lineHeight() const OVERRIDE FINAL;
86
87 bool getEmphasisMarkPosition(RenderStyle*, TextEmphasisPosition&) const;
88
89 LayoutRect logicalOverflowRect() const;
90 void setLogicalOverflowRect(const LayoutRect&);
91 LayoutUnit logicalTopVisualOverflow() const { return logicalOverflowRect().y(); }
92 LayoutUnit logicalBottomVisualOverflow() const { return logicalOverflowRect().maxY(); }
93 LayoutUnit logicalLeftVisualOverflow() const { return logicalOverflowRect().x(); }
94 LayoutUnit logicalRightVisualOverflow() const { return logicalOverflowRect().maxX(); }
95
96#ifndef NDEBUG
Torne (Richard Coles)09380292014-02-21 12:17:33 +000097 virtual void showBox(int = 0) const OVERRIDE;
98 virtual const char* boxName() const OVERRIDE;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010099#endif
100
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000101 enum RotationDirection { Counterclockwise, Clockwise };
102 static AffineTransform rotation(const FloatRect& boxRect, RotationDirection);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100103private:
104 LayoutUnit selectionTop();
105 LayoutUnit selectionBottom();
106 LayoutUnit selectionHeight();
107
Ben Murdoch3c9e4ae2013-08-12 14:20:44 +0100108 // charactersWithHyphen, if provided, must not be destroyed before the TextRun.
109 TextRun constructTextRun(RenderStyle*, const Font&, StringBuilder* charactersWithHyphen = 0) const;
110 TextRun constructTextRun(RenderStyle*, const Font&, StringView, int maximumLength, StringBuilder* charactersWithHyphen = 0) const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100111
112public:
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100113 TextRun constructTextRunForInspector(RenderStyle*, const Font&) const;
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000114 virtual FloatRect calculateBoundaries() const OVERRIDE { return FloatRect(x(), y(), width(), height()); }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100115
116 virtual LayoutRect localSelectionRect(int startPos, int endPos);
117 bool isSelected(int startPos, int endPos) const;
118 void selectionStartEnd(int& sPos, int& ePos);
119
120protected:
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000121 virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom) OVERRIDE;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100122 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom) OVERRIDE;
123
124public:
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000125 RenderText& textRenderer() const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100126
127private:
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100128 virtual void deleteLine() OVERRIDE FINAL;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100129 virtual void extractLine() OVERRIDE FINAL;
130 virtual void attachLine() OVERRIDE FINAL;
131
132public:
133 virtual RenderObject::SelectionState selectionState() OVERRIDE FINAL;
134
135private:
136 virtual void clearTruncation() OVERRIDE FINAL { m_truncation = cNoTruncation; }
137 virtual float placeEllipsisBox(bool flowIsLTR, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox) OVERRIDE FINAL;
138
139public:
140 virtual bool isLineBreak() const OVERRIDE FINAL;
141
142 void setExpansion(int newExpansion)
143 {
144 m_logicalWidth -= expansion();
145 InlineBox::setExpansion(newExpansion);
146 m_logicalWidth += newExpansion;
147 }
148
149private:
150 virtual bool isInlineTextBox() const OVERRIDE FINAL { return true; }
151
152public:
153 virtual int caretMinOffset() const OVERRIDE FINAL;
154 virtual int caretMaxOffset() const OVERRIDE FINAL;
155
156private:
157 float textPos() const; // returns the x position relative to the left start of the text line.
158
159public:
160 virtual int offsetForPosition(float x, bool includePartialGlyphs = true) const;
161 virtual float positionForOffset(int offset) const;
162
163 bool containsCaretOffset(int offset) const; // false for offset after line break
164
Torne (Richard Coles)bfe35902013-10-22 16:41:51 +0100165 // Fills a vector with the pixel width of each character.
166 void characterWidths(Vector<float>&) const;
167
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100168private:
169 InlineTextBox* m_prevTextBox; // The previous box that also uses our RenderObject
170 InlineTextBox* m_nextTextBox; // The next box that also uses our RenderObject
171
172 int m_start;
173 unsigned short m_len;
174
175 unsigned short m_truncation; // Where to truncate when text overflow is applied. We use special constants to
176 // denote no truncation (the whole run paints) and full truncation (nothing paints at all).
177
178protected:
179 void paintCompositionBackground(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, int startPos, int endPos);
180 void paintDocumentMarkers(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, bool background);
181 void paintCompositionUnderline(GraphicsContext*, const FloatPoint& boxOrigin, const CompositionUnderline&);
182
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000183 // These functions both paint markers and update the DocumentMarker's renderedRect.
184 virtual void paintDocumentMarker(GraphicsContext*, const FloatPoint& boxOrigin, DocumentMarker*, RenderStyle*, const Font&, bool grammar);
185 virtual void paintTextMatchMarker(GraphicsContext*, const FloatPoint& boxOrigin, DocumentMarker*, RenderStyle*, const Font&);
186
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100187private:
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000188 void paintDecoration(GraphicsContext*, const FloatPoint& boxOrigin, TextDecoration, const ShadowList*);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100189 void paintSelection(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, Color textColor);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100190
191 TextRun::ExpansionBehavior expansionBehavior() const
192 {
193 return (canHaveLeadingExpansion() ? TextRun::AllowLeadingExpansion : TextRun::ForbidLeadingExpansion)
194 | (expansion() && nextLeafChild() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion);
195 }
196};
197
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000198DEFINE_INLINE_BOX_TYPE_CASTS(InlineTextBox);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100199
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000200inline RenderText& InlineTextBox::textRenderer() const
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100201{
202 return toRenderText(renderer());
203}
204
205void alignSelectionRectToDevicePixels(FloatRect&);
206
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000207inline AffineTransform InlineTextBox::rotation(const FloatRect& boxRect, RotationDirection rotationDirection)
208{
209 return rotationDirection == Clockwise ? AffineTransform(0, 1, -1, 0, boxRect.x() + boxRect.maxY(), boxRect.maxY() - boxRect.x())
210 : AffineTransform(0, -1, 1, 0, boxRect.x() - boxRect.maxY(), boxRect.x() + boxRect.maxY());
211}
212
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100213} // namespace WebCore
214
215#endif // InlineTextBox_h