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"
29 #include <wtf/text/StringBuilder.h>
33 struct CompositionUnderline;
37 const unsigned short cNoTruncation = USHRT_MAX;
38 const unsigned short cFullTruncation = USHRT_MAX - 1;
40 class BufferForAppendingHyphen : public StringBuilder {
42 BufferForAppendingHyphen() { reserveCapacity(256); }
45 class InlineTextBox : public InlineBox {
47 explicit InlineTextBox(RenderText& renderer)
53 , m_truncation(cNoTruncation)
55 setBehavesLikeText(true);
58 virtual ~InlineTextBox();
60 RenderText& renderer() const { return toRenderText(InlineBox::renderer()); }
61 const RenderStyle& lineStyle() const { return isFirstLine() ? renderer().firstLineStyle() : renderer().style(); }
63 InlineTextBox* prevTextBox() const { return m_prevTextBox; }
64 InlineTextBox* nextTextBox() const { return m_nextTextBox; }
65 void setNextTextBox(InlineTextBox* n) { m_nextTextBox = n; }
66 void setPreviousTextBox(InlineTextBox* p) { m_prevTextBox = p; }
68 // FIXME: These accessors should ASSERT(!isDirty()). See https://bugs.webkit.org/show_bug.cgi?id=97264
69 unsigned start() const { return m_start; }
70 unsigned end() const { return m_len ? m_start + m_len - 1 : m_start; }
71 unsigned len() const { return m_len; }
73 void setStart(unsigned start) { m_start = start; }
74 void setLen(unsigned len) { m_len = len; }
76 void offsetRun(int d) { ASSERT(!isDirty()); m_start += d; }
78 unsigned short truncation() const { return m_truncation; }
80 virtual void markDirty(bool dirty = true) override final;
82 using InlineBox::hasHyphen;
83 using InlineBox::setHasHyphen;
84 using InlineBox::canHaveLeadingExpansion;
85 using InlineBox::setCanHaveLeadingExpansion;
87 static inline bool compareByStart(const InlineTextBox* first, const InlineTextBox* second) { return first->start() < second->start(); }
89 virtual int baselinePosition(FontBaseline) const override final;
90 virtual LayoutUnit lineHeight() const override final;
92 bool emphasisMarkExistsAndIsAbove(const RenderStyle&, bool& isAbove) const;
94 LayoutRect logicalOverflowRect() const;
95 void setLogicalOverflowRect(const LayoutRect&);
96 LayoutUnit logicalTopVisualOverflow() const { return logicalOverflowRect().y(); }
97 LayoutUnit logicalBottomVisualOverflow() const { return logicalOverflowRect().maxY(); }
98 LayoutUnit logicalLeftVisualOverflow() const { return logicalOverflowRect().x(); }
99 LayoutUnit logicalRightVisualOverflow() const { return logicalOverflowRect().maxX(); }
101 virtual void dirtyOwnLineBoxes() { dirtyLineBoxes(); }
104 virtual void showBox(int = 0) const;
105 virtual const char* boxName() const;
109 LayoutUnit selectionTop() const;
110 LayoutUnit selectionBottom() const;
111 LayoutUnit selectionHeight() const;
113 TextRun constructTextRun(const RenderStyle&, const Font&, BufferForAppendingHyphen* = 0) const;
114 TextRun constructTextRun(const RenderStyle&, const Font&, String, int maximumLength, BufferForAppendingHyphen* = 0) const;
117 virtual FloatRect calculateBoundaries() const { return FloatRect(x(), y(), width(), height()); }
119 virtual LayoutRect localSelectionRect(int startPos, int endPos) const;
120 bool isSelected(int startPos, int endPos) const;
121 void selectionStartEnd(int& sPos, int& ePos);
124 virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom);
125 virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom) override;
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 // Needs to be public, so the static paintTextWithShadows() function can use it.
164 static FloatSize applyShadowToGraphicsContext(GraphicsContext*, const ShadowData*, const FloatRect& textRect, bool stroked, bool opaque, bool horizontal);
167 void paintCompositionBackground(GraphicsContext*, const FloatPoint& boxOrigin, const RenderStyle&, const Font&, int startPos, int endPos);
168 void paintDocumentMarkers(GraphicsContext*, const FloatPoint& boxOrigin, const RenderStyle&, const Font&, bool background);
169 void paintCompositionUnderline(GraphicsContext*, const FloatPoint& boxOrigin, const CompositionUnderline&);
172 void paintDecoration(GraphicsContext&, const FloatPoint& boxOrigin, TextDecoration, TextDecorationStyle, const ShadowData*, TextPainter&);
173 void paintSelection(GraphicsContext*, const FloatPoint& boxOrigin, const RenderStyle&, const Font&, Color textColor);
174 void paintDocumentMarker(GraphicsContext*, const FloatPoint& boxOrigin, DocumentMarker*, const RenderStyle&, const Font&, bool grammar);
175 void paintTextMatchMarker(GraphicsContext*, const FloatPoint& boxOrigin, DocumentMarker*, const RenderStyle&, const Font&);
177 void computeRectForReplacementMarker(DocumentMarker*, const RenderStyle&, const Font&);
179 TextRun::ExpansionBehavior expansionBehavior() const
181 return (canHaveLeadingExpansion() ? TextRun::AllowLeadingExpansion : TextRun::ForbidLeadingExpansion)
182 | (expansion() && nextLeafChild() && !nextLeafChild()->isLineBreak() ? TextRun::AllowTrailingExpansion : TextRun::ForbidTrailingExpansion);
185 void behavesLikeText() const = delete;
187 InlineTextBox* m_prevTextBox; // The previous box that also uses our RenderObject
188 InlineTextBox* m_nextTextBox; // The next box that also uses our RenderObject
191 unsigned short m_len;
193 // Where to truncate when text overflow is applied. We use special constants to
194 // denote no truncation (the whole run paints) and full truncation (nothing paints at all).
195 unsigned short m_truncation;
198 INLINE_BOX_OBJECT_TYPE_CASTS(InlineTextBox, isInlineTextBox())
200 void alignSelectionRectToDevicePixels(FloatRect&);
202 } // namespace WebCore
204 #endif // InlineTextBox_h