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.
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.
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.
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.
23 #ifndef InlineTextBox_h
24 #define InlineTextBox_h
26 #include "core/rendering/InlineBox.h"
27 #include "core/rendering/RenderText.h" // so textRenderer() can be inline
28 #include "platform/graphics/GraphicsContext.h"
29 #include "platform/text/TextRun.h"
30 #include "wtf/Forward.h"
34 struct CompositionUnderline;
37 const unsigned short cNoTruncation = USHRT_MAX;
38 const unsigned short cFullTruncation = USHRT_MAX - 1;
40 // Helper functions shared by InlineTextBox / SVGRootInlineBox
41 void updateGraphicsContext(GraphicsContext*, const Color& fillColor, const Color& strokeColor, float strokeThickness, ColorSpace);
42 Color correctedTextColor(Color textColor, Color backgroundColor);
44 class InlineTextBox : public InlineBox {
46 InlineTextBox(RenderObject* obj)
52 , m_truncation(cNoTruncation)
56 virtual void destroy() OVERRIDE FINAL;
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; }
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; }
68 void setStart(unsigned start) { m_start = start; }
69 void setLen(unsigned len) { m_len = len; }
71 void offsetRun(int d) { ASSERT(!isDirty()); m_start += d; }
73 unsigned short truncation() { return m_truncation; }
75 virtual void markDirty(bool dirty = true) OVERRIDE FINAL;
77 using InlineBox::hasHyphen;
78 using InlineBox::setHasHyphen;
79 using InlineBox::canHaveLeadingExpansion;
80 using InlineBox::setCanHaveLeadingExpansion;
82 static inline bool compareByStart(const InlineTextBox* first, const InlineTextBox* second) { return first->start() < second->start(); }
84 virtual int baselinePosition(FontBaseline) const OVERRIDE FINAL;
85 virtual LayoutUnit lineHeight() const OVERRIDE FINAL;
87 bool getEmphasisMarkPosition(RenderStyle*, TextEmphasisPosition&) const;
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(); }
97 virtual void showBox(int = 0) const OVERRIDE;
98 virtual const char* boxName() const OVERRIDE;
101 enum RotationDirection { Counterclockwise, Clockwise };
102 static AffineTransform rotation(const FloatRect& boxRect, RotationDirection);
104 LayoutUnit selectionTop();
105 LayoutUnit selectionBottom();
106 LayoutUnit selectionHeight();
108 // 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;
113 TextRun constructTextRunForInspector(RenderStyle*, const Font&) const;
114 virtual FloatRect calculateBoundaries() const OVERRIDE { return FloatRect(x(), y(), width(), height()); }
116 virtual LayoutRect localSelectionRect(int startPos, int endPos);
117 bool isSelected(int startPos, int endPos) const;
118 void selectionStartEnd(int& sPos, int& ePos);
121 virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom) OVERRIDE;
122 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom) OVERRIDE;
125 RenderText* textRenderer() const;
128 virtual void deleteLine() OVERRIDE FINAL;
129 virtual void extractLine() OVERRIDE FINAL;
130 virtual void attachLine() OVERRIDE FINAL;
133 virtual RenderObject::SelectionState selectionState() OVERRIDE FINAL;
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;
140 virtual bool isLineBreak() const OVERRIDE FINAL;
142 void setExpansion(int newExpansion)
144 m_logicalWidth -= expansion();
145 InlineBox::setExpansion(newExpansion);
146 m_logicalWidth += newExpansion;
150 virtual bool isInlineTextBox() const OVERRIDE FINAL { return true; }
153 virtual int caretMinOffset() const OVERRIDE FINAL;
154 virtual int caretMaxOffset() const OVERRIDE FINAL;
157 float textPos() const; // returns the x position relative to the left start of the text line.
160 virtual int offsetForPosition(float x, bool includePartialGlyphs = true) const;
161 virtual float positionForOffset(int offset) const;
163 bool containsCaretOffset(int offset) const; // false for offset after line break
165 // Fills a vector with the pixel width of each character.
166 void characterWidths(Vector<float>&) const;
169 InlineTextBox* m_prevTextBox; // The previous box that also uses our RenderObject
170 InlineTextBox* m_nextTextBox; // The next box that also uses our RenderObject
173 unsigned short m_len;
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).
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&);
183 // 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&);
188 void paintDecoration(GraphicsContext*, const FloatPoint& boxOrigin, TextDecoration, TextDecorationStyle, const ShadowList*);
189 void paintSelection(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, Color textColor);
191 TextRun::ExpansionBehavior expansionBehavior() const
193 return (canHaveLeadingExpansion() ? TextRun::AllowLeadingExpansion : TextRun::ForbidLeadingExpansion)
194 | (expansion() && nextLeafChild() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion);
198 DEFINE_INLINE_BOX_TYPE_CASTS(InlineTextBox);
200 inline RenderText* InlineTextBox::textRenderer() const
202 return toRenderText(renderer());
205 void alignSelectionRectToDevicePixels(FloatRect&);
207 inline AffineTransform InlineTextBox::rotation(const FloatRect& boxRect, RotationDirection rotationDirection)
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());
213 } // namespace WebCore
215 #endif // InlineTextBox_h