1 /*
2  * Copyright (C) 2006 Oliver Hunt <ojh16@student.canterbury.ac.nz>
3  * Copyright (C) 2006 Apple Computer Inc.
4  * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
5  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "config.h"
24 #include "SVGInlineFlowBox.h"
25 
26 #if ENABLE(SVG)
27 #include "DocumentMarkerController.h"
28 #include "GraphicsContext.h"
29 #include "RenderSVGInlineText.h"
30 #include "SVGInlineTextBox.h"
31 #include "SVGRenderSupport.h"
32 
33 using namespace std;
34 
35 namespace WebCore {
36 
paintSelectionBackground(PaintInfo & paintInfo)37 void SVGInlineFlowBox::paintSelectionBackground(PaintInfo& paintInfo)
38 {
39     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
40     ASSERT(!paintInfo.context->paintingDisabled());
41 
42     PaintInfo childPaintInfo(paintInfo);
43     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
44         if (child->isSVGInlineTextBox())
45             static_cast<SVGInlineTextBox*>(child)->paintSelectionBackground(childPaintInfo);
46         else if (child->isSVGInlineFlowBox())
47             static_cast<SVGInlineFlowBox*>(child)->paintSelectionBackground(childPaintInfo);
48     }
49 }
50 
paint(PaintInfo & paintInfo,int,int,int,int)51 void SVGInlineFlowBox::paint(PaintInfo& paintInfo, int, int, int, int)
52 {
53     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
54     ASSERT(!paintInfo.context->paintingDisabled());
55 
56     RenderObject* boxRenderer = renderer();
57     ASSERT(boxRenderer);
58 
59     PaintInfo childPaintInfo(paintInfo);
60     GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
61 
62     if (SVGRenderSupport::prepareToRenderSVGContent(boxRenderer, childPaintInfo)) {
63         for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
64             if (child->isSVGInlineTextBox())
65                 computeTextMatchMarkerRectForRenderer(toRenderSVGInlineText(static_cast<SVGInlineTextBox*>(child)->textRenderer()));
66 
67             child->paint(childPaintInfo, 0, 0, 0, 0);
68         }
69     }
70 
71     SVGRenderSupport::finishRenderSVGContent(boxRenderer, childPaintInfo, paintInfo.context);
72 }
73 
calculateBoundaries() const74 IntRect SVGInlineFlowBox::calculateBoundaries() const
75 {
76     IntRect childRect;
77     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
78         if (!child->isSVGInlineTextBox() && !child->isSVGInlineFlowBox())
79             continue;
80         childRect.unite(child->calculateBoundaries());
81     }
82     return childRect;
83 }
84 
computeTextMatchMarkerRectForRenderer(RenderSVGInlineText * textRenderer)85 void SVGInlineFlowBox::computeTextMatchMarkerRectForRenderer(RenderSVGInlineText* textRenderer)
86 {
87     ASSERT(textRenderer);
88 
89     Node* node = textRenderer->node();
90     if (!node || !node->inDocument())
91         return;
92 
93     RenderStyle* style = textRenderer->style();
94     ASSERT(style);
95 
96     AffineTransform fragmentTransform;
97     Document* document = textRenderer->document();
98     Vector<DocumentMarker> markers = document->markers()->markersForNode(textRenderer->node());
99 
100     Vector<DocumentMarker>::iterator markerEnd = markers.end();
101     for (Vector<DocumentMarker>::iterator markerIt = markers.begin(); markerIt != markerEnd; ++markerIt) {
102         const DocumentMarker& marker = *markerIt;
103 
104         // SVG is only interessted in the TextMatch marker, for now.
105         if (marker.type != DocumentMarker::TextMatch)
106             continue;
107 
108         FloatRect markerRect;
109         for (InlineTextBox* box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
110             if (!box->isSVGInlineTextBox())
111                 continue;
112 
113             SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(box);
114 
115             int markerStartPosition = max<int>(marker.startOffset - textBox->start(), 0);
116             int markerEndPosition = min<int>(marker.endOffset - textBox->start(), textBox->len());
117 
118             if (markerStartPosition >= markerEndPosition)
119                 continue;
120 
121             int fragmentStartPosition = 0;
122             int fragmentEndPosition = 0;
123 
124             const Vector<SVGTextFragment>& fragments = textBox->textFragments();
125             unsigned textFragmentsSize = fragments.size();
126             for (unsigned i = 0; i < textFragmentsSize; ++i) {
127                 const SVGTextFragment& fragment = fragments.at(i);
128 
129                 fragmentStartPosition = markerStartPosition;
130                 fragmentEndPosition = markerEndPosition;
131                 if (!textBox->mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
132                     continue;
133 
134                 FloatRect fragmentRect = textBox->selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style);
135                 fragment.buildFragmentTransform(fragmentTransform);
136                 if (!fragmentTransform.isIdentity())
137                     fragmentRect = fragmentTransform.mapRect(fragmentRect);
138 
139                 markerRect.unite(fragmentRect);
140             }
141         }
142 
143         document->markers()->setRenderedRectForMarker(node, marker, textRenderer->localToAbsoluteQuad(markerRect).enclosingBoundingBox());
144     }
145 }
146 
147 } // namespace WebCore
148 
149 #endif // ENABLE(SVG)
150