File indexing completed on 2024-04-28 15:24:18

0001 /*
0002  * This file is part of the WebKit project.
0003  *
0004  * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  *
0021  */
0022 
0023 #include "wtf/Platform.h"
0024 
0025 #if ENABLE(SVG)
0026 #include "RenderSVGTextPath.h"
0027 
0028 #include "FloatRect.h"
0029 #include "SVGInlineTextBox.h"
0030 #include "SVGPathElement.h"
0031 #include "SVGRootInlineBox.h"
0032 #include "SVGTextPathElement.h"
0033 #include "SVGTransformList.h"
0034 
0035 namespace WebCore
0036 {
0037 
0038 RenderSVGTextPath::RenderSVGTextPath(DOM::NodeImpl *n)
0039     : RenderSVGInline(n)
0040     , m_startOffset(0.0f)
0041     , m_exactAlignment(true)
0042     , m_stretchMethod(false)
0043 {
0044 }
0045 
0046 Path RenderSVGTextPath::layoutPath() const
0047 {
0048     SVGTextPathElement *textPathElement = static_cast<SVGTextPathElement *>(element());
0049     String pathId = SVGURIReference::getTarget(textPathElement->href());
0050     Element *targetElement = textPathElement->document()->getElementById(pathId);
0051     if (!targetElement || !targetElement->hasTagName(SVGNames::pathTag)) {
0052         return Path();
0053     }
0054 
0055     SVGPathElement *pathElement = static_cast<SVGPathElement *>(targetElement);
0056 
0057     Path pathData = pathElement->toPathData();
0058     // Spec:  The transform attribute on the referenced 'path' element represents a
0059     // supplemental transformation relative to the current user coordinate system for
0060     // the current 'text' element, including any adjustments to the current user coordinate
0061     // system due to a possible transform attribute on the current 'text' element.
0062     // https://www.w3.org/TR/SVG/text.html#TextPathElement
0063     pathData.transform(pathElement->animatedLocalTransform());
0064     return pathData;
0065 }
0066 
0067 float RenderSVGTextPath::startOffset() const
0068 {
0069     return static_cast<SVGTextPathElement *>(element())->startOffset().valueAsPercentage();
0070 }
0071 
0072 bool RenderSVGTextPath::exactAlignment() const
0073 {
0074     return static_cast<SVGTextPathElement *>(element())->spacing() == SVG_TEXTPATH_SPACINGTYPE_EXACT;
0075 }
0076 
0077 bool RenderSVGTextPath::stretchMethod() const
0078 {
0079     return static_cast<SVGTextPathElement *>(element())->method() == SVG_TEXTPATH_METHODTYPE_STRETCH;
0080 }
0081 
0082 void RenderSVGTextPath::absoluteRects(Vector<IntRect> &rects, int, int)
0083 {
0084     InlineRunBox *firstBox = firstLineBox();
0085 
0086     SVGRootInlineBox *rootBox = firstBox ? static_cast<SVGInlineTextBox *>(firstBox)->svgRootInlineBox() : nullptr;
0087     RenderObject *object = rootBox ? rootBox->object() : nullptr;
0088 
0089     if (!object) {
0090         return;
0091     }
0092 
0093     int xRef = object->xPos() + xPos();
0094     int yRef = object->yPos() + yPos();
0095 
0096     for (InlineRunBox *curr = firstBox; curr; curr = curr->nextLineBox()) {
0097         FloatRect rect(xRef + curr->xPos(), yRef + curr->yPos(), curr->width(), curr->height());
0098         rects.append(enclosingIntRect(absoluteTransform().mapRect(rect)));
0099     }
0100 }
0101 
0102 }
0103 
0104 #endif // ENABLE(SVG)