1/*
2 * Copyright (C) 2007 Rob Buis <buis@kde.org>
3 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) Research In Motion Limited 2010. 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#ifndef SVGInlineTextBox_h
23#define SVGInlineTextBox_h
24
25#include "InlineTextBox.h"
26#include "SVGTextLayoutEngine.h"
27#include "RenderSVGInlineText.h"
28
29namespace WebCore {
30
31class RenderSVGResource;
32class SVGRootInlineBox;
33
34class SVGInlineTextBox final : public InlineTextBox {
35public:
36    explicit SVGInlineTextBox(RenderSVGInlineText&);
37
38    RenderSVGInlineText& renderer() const { return toRenderSVGInlineText(InlineTextBox::renderer()); }
39
40    virtual float virtualLogicalHeight() const { return m_logicalHeight; }
41    void setLogicalHeight(float height) { m_logicalHeight = height; }
42
43    virtual int selectionTop() { return top(); }
44    virtual int selectionHeight() { return static_cast<int>(ceilf(m_logicalHeight)); }
45    virtual int offsetForPosition(float x, bool includePartialGlyphs = true) const;
46    virtual float positionForOffset(int offset) const;
47
48    void paintSelectionBackground(PaintInfo&);
49    virtual void paint(PaintInfo&, const LayoutPoint&, LayoutUnit lineTop, LayoutUnit lineBottom);
50    virtual LayoutRect localSelectionRect(int startPosition, int endPosition) const override;
51
52    bool mapStartEndPositionsIntoFragmentCoordinates(const SVGTextFragment&, int& startPosition, int& endPosition) const;
53
54    virtual FloatRect calculateBoundaries() const;
55
56    void clearTextFragments() { m_textFragments.clear(); }
57    Vector<SVGTextFragment>& textFragments() { return m_textFragments; }
58    const Vector<SVGTextFragment>& textFragments() const { return m_textFragments; }
59
60    virtual void dirtyOwnLineBoxes() override final;
61    virtual void dirtyLineBoxes() override final;
62
63    bool startsNewTextChunk() const { return m_startsNewTextChunk; }
64    void setStartsNewTextChunk(bool newTextChunk) { m_startsNewTextChunk = newTextChunk; }
65
66    int offsetForPositionInFragment(const SVGTextFragment&, float position, bool includePartialGlyphs) const;
67    FloatRect selectionRectForTextFragment(const SVGTextFragment&, int fragmentStartPosition, int fragmentEndPosition, RenderStyle*) const;
68
69private:
70    virtual bool isSVGInlineTextBox() const override { return true; }
71
72    TextRun constructTextRun(RenderStyle*, const SVGTextFragment&) const;
73
74    bool acquirePaintingResource(GraphicsContext*&, float scalingFactor, RenderBoxModelObject&, RenderStyle*);
75    void releasePaintingResource(GraphicsContext*&, const Path*);
76
77    bool prepareGraphicsContextForTextPainting(GraphicsContext*&, float scalingFactor, TextRun&, RenderStyle*);
78    void restoreGraphicsContextAfterTextPainting(GraphicsContext*&, TextRun&);
79
80    void paintDecoration(GraphicsContext*, TextDecoration, const SVGTextFragment&);
81    void paintDecorationWithStyle(GraphicsContext*, TextDecoration, const SVGTextFragment&, RenderBoxModelObject& decorationRenderer);
82    void paintTextWithShadows(GraphicsContext*, RenderStyle*, TextRun&, const SVGTextFragment&, int startPosition, int endPosition);
83    void paintText(GraphicsContext*, RenderStyle*, RenderStyle* selectionStyle, const SVGTextFragment&, bool hasSelection, bool paintSelectedTextOnly);
84
85    virtual bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom) override;
86
87private:
88    float m_logicalHeight;
89    unsigned m_paintingResourceMode : 4;
90    unsigned m_startsNewTextChunk : 1;
91    RenderSVGResource* m_paintingResource;
92    Vector<SVGTextFragment> m_textFragments;
93};
94
95INLINE_BOX_OBJECT_TYPE_CASTS(SVGInlineTextBox, isSVGInlineTextBox())
96
97} // namespace WebCore
98
99#endif // SVGInlineTextBox_h
100