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/fonts/TextBlob.h"
29 #include "platform/text/TextRun.h"
30 #include "wtf/Forward.h"
34 struct CompositionUnderline;
36 class GraphicsContext;
38 const unsigned short cNoTruncation = USHRT_MAX;
39 const unsigned short cFullTruncation = USHRT_MAX - 1;
41 class InlineTextBox : public InlineBox {
43 InlineTextBox(RenderObject& obj)
49 , m_truncation(cNoTruncation)
53 RenderText& renderer() const { return toRenderText(InlineBox::renderer()); }
55 virtual void destroy() OVERRIDE FINAL;
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; }
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; }
67 void setStart(unsigned start) { m_start = start; }
68 void setLen(unsigned len) { m_len = len; }
70 void offsetRun(int d) { ASSERT(!isDirty()); m_start += d; }
72 unsigned short truncation() { return m_truncation; }
74 virtual void markDirty() OVERRIDE FINAL;
76 using InlineBox::hasHyphen;
77 using InlineBox::setHasHyphen;
78 using InlineBox::canHaveLeadingExpansion;
79 using InlineBox::setCanHaveLeadingExpansion;
81 static inline bool compareByStart(const InlineTextBox* first, const InlineTextBox* second) { return first->start() < second->start(); }
83 virtual int baselinePosition(FontBaseline) const OVERRIDE FINAL;
84 virtual LayoutUnit lineHeight() const OVERRIDE FINAL;
86 bool getEmphasisMarkPosition(RenderStyle*, TextEmphasisPosition&) const;
88 LayoutRect logicalOverflowRect() const;
89 void setLogicalOverflowRect(const LayoutRect&);
90 LayoutUnit logicalTopVisualOverflow() const { return logicalOverflowRect().y(); }
91 LayoutUnit logicalBottomVisualOverflow() const { return logicalOverflowRect().maxY(); }
94 virtual void showBox(int = 0) const OVERRIDE;
95 virtual const char* boxName() const OVERRIDE;
98 enum RotationDirection { Counterclockwise, Clockwise };
99 static AffineTransform rotation(const FloatRect& boxRect, RotationDirection);
101 LayoutUnit selectionTop();
102 LayoutUnit selectionBottom();
103 LayoutUnit selectionHeight();
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;
110 TextRun constructTextRunForInspector(RenderStyle*, const Font&) const;
111 virtual FloatRect calculateBoundaries() const OVERRIDE { return FloatRect(x(), y(), width(), height()); }
113 virtual LayoutRect localSelectionRect(int startPos, int endPos);
114 bool isSelected(int startPos, int endPos) const;
115 void selectionStartEnd(int& sPos, int& ePos) const;
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;
122 virtual void deleteLine() OVERRIDE FINAL;
123 virtual void extractLine() OVERRIDE FINAL;
124 virtual void attachLine() OVERRIDE FINAL;
127 virtual RenderObject::SelectionState selectionState() const OVERRIDE FINAL;
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;
134 virtual bool isLineBreak() const OVERRIDE FINAL;
136 void setExpansion(int newExpansion)
138 m_logicalWidth -= expansion();
139 InlineBox::setExpansion(newExpansion);
140 m_logicalWidth += newExpansion;
144 virtual bool isInlineTextBox() const OVERRIDE FINAL { return true; }
147 virtual int caretMinOffset() const OVERRIDE FINAL;
148 virtual int caretMaxOffset() const OVERRIDE FINAL;
151 float textPos() const; // returns the x position relative to the left start of the text line.
154 virtual int offsetForPosition(float x, bool includePartialGlyphs = true) const;
155 virtual float positionForOffset(int offset) const;
157 bool containsCaretOffset(int offset) const; // false for offset after line break
159 // Fills a vector with the pixel width of each character.
160 void characterWidths(Vector<float>&) const;
163 InlineTextBox* m_prevTextBox; // The previous box that also uses our RenderObject
164 InlineTextBox* m_nextTextBox; // The next box that also uses our RenderObject
167 unsigned short m_len;
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).
172 unsigned underlinePaintStart(const CompositionUnderline&);
173 unsigned underlinePaintEnd(const CompositionUnderline&);
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&);
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&);
186 void paintDecoration(GraphicsContext*, const FloatPoint& boxOrigin, TextDecoration);
187 void paintSelection(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, Color textColor);
189 TextRun::ExpansionBehavior expansionBehavior() const
191 return (canHaveLeadingExpansion() ? TextRun::AllowLeadingExpansion : TextRun::ForbidLeadingExpansion)
192 | (expansion() && nextLeafChild() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion);
196 DEFINE_INLINE_BOX_TYPE_CASTS(InlineTextBox);
198 void alignSelectionRectToDevicePixels(FloatRect&);
200 inline AffineTransform InlineTextBox::rotation(const FloatRect& boxRect, RotationDirection rotationDirection)
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());
208 #endif // InlineTextBox_h