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 "InlineBox.h"
27 #include "RenderText.h" // so textRenderer() can be inline
28 #include "TextRun.h"
29 
30 namespace WebCore {
31 
32 struct CompositionUnderline;
33 struct DocumentMarker;
34 
35 const unsigned short cNoTruncation = USHRT_MAX;
36 const unsigned short cFullTruncation = USHRT_MAX - 1;
37 
38 // Helper functions shared by InlineTextBox / SVGRootInlineBox
39 void updateGraphicsContext(GraphicsContext*, const Color& fillColor, const Color& strokeColor, float strokeThickness, ColorSpace);
40 Color correctedTextColor(Color textColor, Color backgroundColor);
41 
42 class InlineTextBox : public InlineBox {
43 public:
InlineTextBox(RenderObject * obj)44     InlineTextBox(RenderObject* obj)
45         : InlineBox(obj)
46         , m_prevTextBox(0)
47         , m_nextTextBox(0)
48         , m_start(0)
49         , m_len(0)
50         , m_truncation(cNoTruncation)
51     {
52     }
53 
54     virtual void destroy(RenderArena*);
55 
prevTextBox()56     InlineTextBox* prevTextBox() const { return m_prevTextBox; }
nextTextBox()57     InlineTextBox* nextTextBox() const { return m_nextTextBox; }
setNextTextBox(InlineTextBox * n)58     void setNextTextBox(InlineTextBox* n) { m_nextTextBox = n; }
setPreviousTextBox(InlineTextBox * p)59     void setPreviousTextBox(InlineTextBox* p) { m_prevTextBox = p; }
60 
start()61     unsigned start() const { return m_start; }
end()62     unsigned end() const { return m_len ? m_start + m_len - 1 : m_start; }
len()63     unsigned len() const { return m_len; }
64 
setStart(unsigned start)65     void setStart(unsigned start) { m_start = start; }
setLen(unsigned len)66     void setLen(unsigned len) { m_len = len; }
67 
offsetRun(int d)68     void offsetRun(int d) { m_start += d; }
69 
truncation()70     unsigned short truncation() { return m_truncation; }
71 
hasHyphen()72     bool hasHyphen() const { return m_hasEllipsisBoxOrHyphen; }
setHasHyphen(bool hasHyphen)73     void setHasHyphen(bool hasHyphen) { m_hasEllipsisBoxOrHyphen = hasHyphen; }
74 
canHaveLeadingExpansion()75     bool canHaveLeadingExpansion() const { return m_hasSelectedChildrenOrCanHaveLeadingExpansion; }
setCanHaveLeadingExpansion(bool canHaveLeadingExpansion)76     void setCanHaveLeadingExpansion(bool canHaveLeadingExpansion) { m_hasSelectedChildrenOrCanHaveLeadingExpansion = canHaveLeadingExpansion; }
77 
compareByStart(const InlineTextBox * first,const InlineTextBox * second)78     static inline bool compareByStart(const InlineTextBox* first, const InlineTextBox* second) { return first->start() < second->start(); }
79 
80     virtual int baselinePosition(FontBaseline) const;
81     virtual int lineHeight() const;
82 
83     bool getEmphasisMarkPosition(RenderStyle*, TextEmphasisPosition&) const;
84 
85     IntRect logicalOverflowRect() const;
86     void setLogicalOverflowRect(const IntRect&);
logicalTopVisualOverflow()87     int logicalTopVisualOverflow() const { return logicalOverflowRect().y(); }
logicalBottomVisualOverflow()88     int logicalBottomVisualOverflow() const { return logicalOverflowRect().maxY(); }
logicalLeftVisualOverflow()89     int logicalLeftVisualOverflow() const { return logicalOverflowRect().x(); }
logicalRightVisualOverflow()90     int logicalRightVisualOverflow() const { return logicalOverflowRect().maxX(); }
91 
92 #ifndef NDEBUG
93     virtual void showBox(int = 0) const;
94     virtual const char* boxName() const;
95 #endif
96 private:
97     int selectionTop();
98     int selectionBottom();
99     int selectionHeight();
100 
101 public:
calculateBoundaries()102     virtual IntRect calculateBoundaries() const { return IntRect(x(), y(), width(), height()); }
103 
104     virtual IntRect selectionRect(int absx, int absy, int startPos, int endPos);
105     bool isSelected(int startPos, int endPos) const;
106     void selectionStartEnd(int& sPos, int& ePos);
107 
108 protected:
109     virtual void paint(PaintInfo&, int tx, int ty, int lineTop, int lineBottom);
110     virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, int x, int y, int tx, int ty, int lineTop, int lineBottom);
111 
112 public:
113     RenderText* textRenderer() const;
114 
115 private:
116     virtual void deleteLine(RenderArena*);
117     virtual void extractLine();
118     virtual void attachLine();
119 
120 public:
121     virtual RenderObject::SelectionState selectionState();
122 
123 private:
clearTruncation()124     virtual void clearTruncation() { m_truncation = cNoTruncation; }
125     virtual float placeEllipsisBox(bool flowIsLTR, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, bool& foundBox);
126 
127 public:
128     virtual bool isLineBreak() const;
129 
setExpansion(int expansion)130     void setExpansion(int expansion) { m_logicalWidth -= m_expansion; m_expansion = expansion; m_logicalWidth += m_expansion; }
131 
132 private:
isInlineTextBox()133     virtual bool isInlineTextBox() const { return true; }
134 
135 public:
136     virtual int caretMinOffset() const;
137     virtual int caretMaxOffset() const;
138 
139 private:
140     virtual unsigned caretMaxRenderedOffset() const;
141 
142     float textPos() const; // returns the x position relative to the left start of the text line.
143 
144 public:
145     virtual int offsetForPosition(float x, bool includePartialGlyphs = true) const;
146     virtual float positionForOffset(int offset) const;
147 
148     bool containsCaretOffset(int offset) const; // false for offset after line break
149 
150     // Needs to be public, so the static paintTextWithShadows() function can use it.
151     static FloatSize applyShadowToGraphicsContext(GraphicsContext*, const ShadowData*, const FloatRect& textRect, bool stroked, bool opaque, bool horizontal);
152 
153 private:
154     InlineTextBox* m_prevTextBox; // The previous box that also uses our RenderObject
155     InlineTextBox* m_nextTextBox; // The next box that also uses our RenderObject
156 
157     int m_start;
158     unsigned short m_len;
159 
160     unsigned short m_truncation; // Where to truncate when text overflow is applied.  We use special constants to
161                       // denote no truncation (the whole run paints) and full truncation (nothing paints at all).
162 
163 protected:
164     void paintCompositionBackground(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, int startPos, int endPos);
165     void paintDocumentMarkers(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&, bool background);
166     void paintCompositionUnderline(GraphicsContext*, const FloatPoint& boxOrigin, const CompositionUnderline&);
167 #if PLATFORM(MAC)
168     void paintCustomHighlight(int tx, int ty, const AtomicString& type);
169 #endif
170 
171 private:
172     void paintDecoration(GraphicsContext*, const FloatPoint& boxOrigin, int decoration, const ShadowData*);
173     void paintSelection(GraphicsContext*, const FloatPoint& boxOrigin, RenderStyle*, const Font&);
174     void paintSpellingOrGrammarMarker(GraphicsContext*, const FloatPoint& boxOrigin, const DocumentMarker&, RenderStyle*, const Font&, bool grammar);
175     void paintTextMatchMarker(GraphicsContext*, const FloatPoint& boxOrigin, const DocumentMarker&, RenderStyle*, const Font&);
176     void computeRectForReplacementMarker(const DocumentMarker&, RenderStyle*, const Font&);
177 
expansionBehavior()178     TextRun::ExpansionBehavior expansionBehavior() const
179     {
180         return (canHaveLeadingExpansion() ? TextRun::AllowLeadingExpansion : TextRun::ForbidLeadingExpansion)
181             | (m_expansion && nextLeafChild() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion);
182     }
183 };
184 
textRenderer()185 inline RenderText* InlineTextBox::textRenderer() const
186 {
187     return toRenderText(renderer());
188 }
189 
190 } // namespace WebCore
191 
192 #endif // InlineTextBox_h
193