initial import
[vuplus_webkit] / Source / WebCore / rendering / svg / SVGInlineFlowBox.cpp
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 "RenderedDocumentMarker.h"
31 #include "SVGInlineTextBox.h"
32 #include "SVGRenderSupport.h"
33
34 using namespace std;
35
36 namespace WebCore {
37
38 void SVGInlineFlowBox::paintSelectionBackground(PaintInfo& paintInfo)
39 {
40     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
41     ASSERT(!paintInfo.context->paintingDisabled());
42
43     PaintInfo childPaintInfo(paintInfo);
44     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
45         if (child->isSVGInlineTextBox())
46             static_cast<SVGInlineTextBox*>(child)->paintSelectionBackground(childPaintInfo);
47         else if (child->isSVGInlineFlowBox())
48             static_cast<SVGInlineFlowBox*>(child)->paintSelectionBackground(childPaintInfo);
49     }
50 }
51
52 void SVGInlineFlowBox::paint(PaintInfo& paintInfo, const LayoutPoint&, LayoutUnit, LayoutUnit)
53 {
54     ASSERT(paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection);
55     ASSERT(!paintInfo.context->paintingDisabled());
56
57     RenderObject* boxRenderer = renderer();
58     ASSERT(boxRenderer);
59
60     PaintInfo childPaintInfo(paintInfo);
61     GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
62
63     if (SVGRenderSupport::prepareToRenderSVGContent(boxRenderer, childPaintInfo)) {
64         for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
65             if (child->isSVGInlineTextBox())
66                 computeTextMatchMarkerRectForRenderer(toRenderSVGInlineText(static_cast<SVGInlineTextBox*>(child)->textRenderer()));
67
68             child->paint(childPaintInfo, LayoutPoint(), 0, 0);
69         }
70     }
71
72     SVGRenderSupport::finishRenderSVGContent(boxRenderer, childPaintInfo, paintInfo.context);
73 }
74
75 IntRect SVGInlineFlowBox::calculateBoundaries() const
76 {
77     IntRect childRect;
78     for (InlineBox* child = firstChild(); child; child = child->nextOnLine()) {
79         if (!child->isSVGInlineTextBox() && !child->isSVGInlineFlowBox())
80             continue;
81         childRect.unite(child->calculateBoundaries());
82     }
83     return childRect;
84 }
85
86 void SVGInlineFlowBox::computeTextMatchMarkerRectForRenderer(RenderSVGInlineText* textRenderer)
87 {
88     ASSERT(textRenderer);
89
90     Node* node = textRenderer->node();
91     if (!node || !node->inDocument())
92         return;
93
94     RenderStyle* style = textRenderer->style();
95     ASSERT(style);
96
97     AffineTransform fragmentTransform;
98     Document* document = textRenderer->document();
99     Vector<DocumentMarker*> markers = document->markers()->markersFor(textRenderer->node());
100
101     Vector<DocumentMarker*>::iterator markerEnd = markers.end();
102     for (Vector<DocumentMarker*>::iterator markerIt = markers.begin(); markerIt != markerEnd; ++markerIt) {
103         DocumentMarker* marker = *markerIt;
104
105         // SVG is only interessted in the TextMatch marker, for now.
106         if (marker->type() != DocumentMarker::TextMatch)
107             continue;
108
109         FloatRect markerRect;
110         for (InlineTextBox* box = textRenderer->firstTextBox(); box; box = box->nextTextBox()) {
111             if (!box->isSVGInlineTextBox())
112                 continue;
113
114             SVGInlineTextBox* textBox = static_cast<SVGInlineTextBox*>(box);
115
116             int markerStartPosition = max<int>(marker->startOffset() - textBox->start(), 0);
117             int markerEndPosition = min<int>(marker->endOffset() - textBox->start(), textBox->len());
118
119             if (markerStartPosition >= markerEndPosition)
120                 continue;
121
122             int fragmentStartPosition = 0;
123             int fragmentEndPosition = 0;
124
125             const Vector<SVGTextFragment>& fragments = textBox->textFragments();
126             unsigned textFragmentsSize = fragments.size();
127             for (unsigned i = 0; i < textFragmentsSize; ++i) {
128                 const SVGTextFragment& fragment = fragments.at(i);
129
130                 fragmentStartPosition = markerStartPosition;
131                 fragmentEndPosition = markerEndPosition;
132                 if (!textBox->mapStartEndPositionsIntoFragmentCoordinates(fragment, fragmentStartPosition, fragmentEndPosition))
133                     continue;
134
135                 FloatRect fragmentRect = textBox->selectionRectForTextFragment(fragment, fragmentStartPosition, fragmentEndPosition, style);
136                 fragment.buildFragmentTransform(fragmentTransform);
137                 if (!fragmentTransform.isIdentity())
138                     fragmentRect = fragmentTransform.mapRect(fragmentRect);
139
140                 markerRect.unite(fragmentRect);
141             }
142         }
143
144         toRenderedDocumentMarker(marker)->setRenderedRect(textRenderer->localToAbsoluteQuad(markerRect).enclosingBoundingBox());
145     }
146 }
147
148 } // namespace WebCore
149
150 #endif // ENABLE(SVG)