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 "InlineBox.h"
27 #include "RenderText.h" // so textRenderer() can be inline
29 #include <wtf/text/StringBuilder.h>
33 struct CompositionUnderline;
36 const unsigned short cNoTruncation = USHRT_MAX;
37 const unsigned short cFullTruncation = USHRT_MAX - 1;
39 class BufferForAppendingHyphen : public StringBuilder {
41 BufferForAppendingHyphen() { reserveCapacity(256); }
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);
48 class InlineTextBox : public InlineBox {
50 InlineTextBox(RenderObject* obj)
56 , m_truncation(cNoTruncation)
60 virtual void destroy(RenderArena*);
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; }
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; }
71 void setStart(unsigned start) { m_start = start; }
72 void setLen(unsigned len) { m_len = len; }
74 void offsetRun(int d) { m_start += d; }
76 unsigned short truncation() { return m_truncation; }
78 bool hasHyphen() const { return m_hasEllipsisBoxOrHyphen; }
79 void setHasHyphen(bool hasHyphen) { m_hasEllipsisBoxOrHyphen = hasHyphen; }
81 bool canHaveLeadingExpansion() const { return m_hasSelectedChildrenOrCanHaveLeadingExpansion; }
82 void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_hasSelectedChildrenOrCanHaveLeadingExpansion = canHaveLeadingExpansion; }
84 static inline bool compareByStart(const InlineTextBox* first, const InlineTextBox* second) { return first->start() < second->start(); }
86 virtual LayoutUnit baselinePosition(FontBaseline) const;
87 virtual LayoutUnit lineHeight() const;
89 bool getEmphasisMarkPosition(RenderStyle*, TextEmphasisPosition&) const;
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(); }
99 virtual void showBox(int = 0) const;
100 virtual const char* boxName() const;
103 LayoutUnit selectionTop();
104 LayoutUnit selectionBottom();
105 LayoutUnit selectionHeight();
107 TextRun constructTextRun(RenderStyle*, const Font&, BufferForAppendingHyphen* = 0) const;
108 TextRun constructTextRun(RenderStyle*, const Font&, const UChar*, int length, int maximumLength, BufferForAppendingHyphen* = 0) const;
111 virtual FloatRect calculateBoundaries() const { return FloatRect(x(), y(), width(), height()); }
113 virtual IntRect localSelectionRect(int startPos, int endPos);
114 bool isSelected(int startPos, int endPos) const;
115 void selectionStartEnd(int& sPos, int& ePos);
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);
122 RenderText* textRenderer() const;
125 virtual void deleteLine(RenderArena*);
126 virtual void extractLine();
127 virtual void attachLine();
130 virtual RenderObject::SelectionState selectionState();
133 virtual void clearTruncation() { m_truncation = cNoTruncation; }
134 virtual float placeEllipsisBox(bool flowIsLTR, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, bool& foundBox);
137 virtual bool isLineBreak() const;
139 void setExpansion(int expansion) { m_logicalWidth -= m_expansion; m_expansion = expansion; m_logicalWidth += m_expansion; }
142 virtual bool isInlineTextBox() const { return true; }
145 virtual int caretMinOffset() const;
146 virtual int caretMaxOffset() const;
149 float textPos() const; // returns the x position relative to the left start of the text line.
152 virtual int offsetForPosition(float x, bool includePartialGlyphs = true) const;
153 virtual float positionForOffset(int offset) const;
155 bool containsCaretOffset(int offset) const; // false for offset after line break
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);
161 InlineTextBox* m_prevTextBox; // The previous box that also uses our RenderObject
162 InlineTextBox* m_nextTextBox; // The next box that also uses our RenderObject
165 unsigned short m_len;
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).
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&);
175 void paintCustomHighlight(const LayoutPoint&, const AtomicString& type);
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&);
185 TextRun::ExpansionBehavior expansionBehavior() const
187 return (canHaveLeadingExpansion() ? TextRun::AllowLeadingExpansion : TextRun::ForbidLeadingExpansion)
188 | (m_expansion && nextLeafChild() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion);
192 inline InlineTextBox* toInlineTextBox(InlineBox* inlineBox)
194 ASSERT(!inlineBox || inlineBox->isInlineTextBox());
195 return static_cast<InlineTextBox*>(inlineBox);
198 inline const InlineTextBox* toInlineTextBox(const InlineBox* inlineBox)
200 ASSERT(!inlineBox || inlineBox->isInlineTextBox());
201 return static_cast<const InlineTextBox*>(inlineBox);
204 // This will catch anyone doing an unnecessary cast.
205 void toInlineTextBox(const InlineTextBox*);
207 inline RenderText* InlineTextBox::textRenderer() const
209 return toRenderText(renderer());
212 } // namespace WebCore
214 #endif // InlineTextBox_h