2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2010 Dirk Schulze <krit@webkit.org>
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 #include "core/svg/SVGPreserveAspectRatio.h"
25 #include "bindings/core/v8/ExceptionState.h"
26 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
27 #include "core/dom/ExceptionCode.h"
28 #include "core/svg/SVGAnimationElement.h"
29 #include "core/svg/SVGParserUtilities.h"
30 #include "platform/geometry/FloatRect.h"
31 #include "platform/transforms/AffineTransform.h"
32 #include "wtf/text/WTFString.h"
36 SVGPreserveAspectRatio::SVGPreserveAspectRatio()
41 void SVGPreserveAspectRatio::setDefault()
43 m_align = SVG_PRESERVEASPECTRATIO_XMIDYMID;
44 m_meetOrSlice = SVG_MEETORSLICE_MEET;
47 PassRefPtr<SVGPreserveAspectRatio> SVGPreserveAspectRatio::clone() const
49 RefPtr<SVGPreserveAspectRatio> preserveAspectRatio = create();
51 preserveAspectRatio->m_align = m_align;
52 preserveAspectRatio->m_meetOrSlice = m_meetOrSlice;
54 return preserveAspectRatio.release();
57 template<typename CharType>
58 bool SVGPreserveAspectRatio::parseInternal(const CharType*& ptr, const CharType* end, bool validate)
60 SVGPreserveAspectRatioType align = SVG_PRESERVEASPECTRATIO_XMIDYMID;
61 SVGMeetOrSliceType meetOrSlice = SVG_MEETORSLICE_MEET;
64 setMeetOrSlice(meetOrSlice);
66 if (!skipOptionalSVGSpaces(ptr, end))
70 if (!skipString(ptr, end, "defer"))
73 // FIXME: We just ignore the "defer" here.
77 if (!skipOptionalSVGSpaces(ptr, end))
82 if (!skipString(ptr, end, "none"))
84 align = SVG_PRESERVEASPECTRATIO_NONE;
85 skipOptionalSVGSpaces(ptr, end);
86 } else if (*ptr == 'x') {
89 if (ptr[1] != 'M' || ptr[4] != 'Y' || ptr[5] != 'M')
95 align = SVG_PRESERVEASPECTRATIO_XMINYMIN;
96 else if (ptr[7] == 'd')
97 align = SVG_PRESERVEASPECTRATIO_XMINYMID;
100 } else if (ptr[6] == 'a' && ptr[7] == 'x') {
101 align = SVG_PRESERVEASPECTRATIO_XMINYMAX;
105 } else if (ptr[3] == 'd') {
108 align = SVG_PRESERVEASPECTRATIO_XMIDYMIN;
109 else if (ptr[7] == 'd')
110 align = SVG_PRESERVEASPECTRATIO_XMIDYMID;
113 } else if (ptr[6] == 'a' && ptr[7] == 'x') {
114 align = SVG_PRESERVEASPECTRATIO_XMIDYMAX;
121 } else if (ptr[2] == 'a' && ptr[3] == 'x') {
124 align = SVG_PRESERVEASPECTRATIO_XMAXYMIN;
125 else if (ptr[7] == 'd')
126 align = SVG_PRESERVEASPECTRATIO_XMAXYMID;
129 } else if (ptr[6] == 'a' && ptr[7] == 'x') {
130 align = SVG_PRESERVEASPECTRATIO_XMAXYMAX;
138 skipOptionalSVGSpaces(ptr, end);
145 if (!skipString(ptr, end, "meet"))
147 skipOptionalSVGSpaces(ptr, end);
148 } else if (*ptr == 's') {
149 if (!skipString(ptr, end, "slice"))
151 skipOptionalSVGSpaces(ptr, end);
152 if (align != SVG_PRESERVEASPECTRATIO_NONE)
153 meetOrSlice = SVG_MEETORSLICE_SLICE;
157 if (end != ptr && validate)
161 setMeetOrSlice(meetOrSlice);
166 void SVGPreserveAspectRatio::setValueAsString(const String& string, ExceptionState& exceptionState)
170 if (string.isEmpty())
174 if (string.is8Bit()) {
175 const LChar* ptr = string.characters8();
176 const LChar* end = ptr + string.length();
177 valid = parseInternal(ptr, end, true);
179 const UChar* ptr = string.characters16();
180 const UChar* end = ptr + string.length();
181 valid = parseInternal(ptr, end, true);
185 exceptionState.throwDOMException(SyntaxError, "The value provided ('" + string + "') is invalid.");
189 bool SVGPreserveAspectRatio::parse(const LChar*& ptr, const LChar* end, bool validate)
191 return parseInternal(ptr, end, validate);
194 bool SVGPreserveAspectRatio::parse(const UChar*& ptr, const UChar* end, bool validate)
196 return parseInternal(ptr, end, validate);
199 void SVGPreserveAspectRatio::transformRect(FloatRect& destRect, FloatRect& srcRect)
201 if (m_align == SVG_PRESERVEASPECTRATIO_NONE)
204 FloatSize imageSize = srcRect.size();
205 float origDestWidth = destRect.width();
206 float origDestHeight = destRect.height();
207 switch (m_meetOrSlice) {
208 case SVGPreserveAspectRatio::SVG_MEETORSLICE_UNKNOWN:
210 case SVGPreserveAspectRatio::SVG_MEETORSLICE_MEET: {
211 float widthToHeightMultiplier = srcRect.height() / srcRect.width();
212 if (origDestHeight > origDestWidth * widthToHeightMultiplier) {
213 destRect.setHeight(origDestWidth * widthToHeightMultiplier);
215 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMINYMID:
216 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMID:
217 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMID:
218 destRect.setY(destRect.y() + origDestHeight / 2 - destRect.height() / 2);
220 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMINYMAX:
221 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMAX:
222 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMAX:
223 destRect.setY(destRect.y() + origDestHeight - destRect.height());
229 if (origDestWidth > origDestHeight / widthToHeightMultiplier) {
230 destRect.setWidth(origDestHeight / widthToHeightMultiplier);
232 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMIN:
233 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMID:
234 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMAX:
235 destRect.setX(destRect.x() + origDestWidth / 2 - destRect.width() / 2);
237 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMIN:
238 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMID:
239 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMAX:
240 destRect.setX(destRect.x() + origDestWidth - destRect.width());
248 case SVGPreserveAspectRatio::SVG_MEETORSLICE_SLICE: {
249 float widthToHeightMultiplier = srcRect.height() / srcRect.width();
250 // if the destination height is less than the height of the image we'll be drawing
251 if (origDestHeight < origDestWidth * widthToHeightMultiplier) {
252 float destToSrcMultiplier = srcRect.width() / destRect.width();
253 srcRect.setHeight(destRect.height() * destToSrcMultiplier);
255 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMINYMID:
256 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMID:
257 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMID:
258 srcRect.setY(srcRect.y() + imageSize.height() / 2 - srcRect.height() / 2);
260 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMINYMAX:
261 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMAX:
262 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMAX:
263 srcRect.setY(srcRect.y() + imageSize.height() - srcRect.height());
269 // if the destination width is less than the width of the image we'll be drawing
270 if (origDestWidth < origDestHeight / widthToHeightMultiplier) {
271 float destToSrcMultiplier = srcRect.height() / destRect.height();
272 srcRect.setWidth(destRect.width() * destToSrcMultiplier);
274 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMIN:
275 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMID:
276 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMAX:
277 srcRect.setX(srcRect.x() + imageSize.width() / 2 - srcRect.width() / 2);
279 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMIN:
280 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMID:
281 case SVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMAX:
282 srcRect.setX(srcRect.x() + imageSize.width() - srcRect.width());
293 AffineTransform SVGPreserveAspectRatio::getCTM(float logicalX, float logicalY, float logicalWidth, float logicalHeight, float physicalWidth, float physicalHeight) const
295 ASSERT(logicalWidth);
296 ASSERT(logicalHeight);
297 ASSERT(physicalWidth);
298 ASSERT(physicalHeight);
300 AffineTransform transform;
301 if (m_align == SVG_PRESERVEASPECTRATIO_UNKNOWN)
304 double extendedLogicalX = logicalX;
305 double extendedLogicalY = logicalY;
306 double extendedLogicalWidth = logicalWidth;
307 double extendedLogicalHeight = logicalHeight;
308 double extendedPhysicalWidth = physicalWidth;
309 double extendedPhysicalHeight = physicalHeight;
310 double logicalRatio = extendedLogicalWidth / extendedLogicalHeight;
311 double physicalRatio = extendedPhysicalWidth / extendedPhysicalHeight;
313 if (m_align == SVG_PRESERVEASPECTRATIO_NONE) {
314 transform.scaleNonUniform(extendedPhysicalWidth / extendedLogicalWidth, extendedPhysicalHeight / extendedLogicalHeight);
315 transform.translate(-extendedLogicalX, -extendedLogicalY);
319 if ((logicalRatio < physicalRatio && (m_meetOrSlice == SVG_MEETORSLICE_MEET)) || (logicalRatio >= physicalRatio && (m_meetOrSlice == SVG_MEETORSLICE_SLICE))) {
320 transform.scaleNonUniform(extendedPhysicalHeight / extendedLogicalHeight, extendedPhysicalHeight / extendedLogicalHeight);
322 if (m_align == SVG_PRESERVEASPECTRATIO_XMINYMIN || m_align == SVG_PRESERVEASPECTRATIO_XMINYMID || m_align == SVG_PRESERVEASPECTRATIO_XMINYMAX)
323 transform.translate(-extendedLogicalX, -extendedLogicalY);
324 else if (m_align == SVG_PRESERVEASPECTRATIO_XMIDYMIN || m_align == SVG_PRESERVEASPECTRATIO_XMIDYMID || m_align == SVG_PRESERVEASPECTRATIO_XMIDYMAX)
325 transform.translate(-extendedLogicalX - (extendedLogicalWidth - extendedPhysicalWidth * extendedLogicalHeight / extendedPhysicalHeight) / 2, -extendedLogicalY);
327 transform.translate(-extendedLogicalX - (extendedLogicalWidth - extendedPhysicalWidth * extendedLogicalHeight / extendedPhysicalHeight), -extendedLogicalY);
332 transform.scaleNonUniform(extendedPhysicalWidth / extendedLogicalWidth, extendedPhysicalWidth / extendedLogicalWidth);
334 if (m_align == SVG_PRESERVEASPECTRATIO_XMINYMIN || m_align == SVG_PRESERVEASPECTRATIO_XMIDYMIN || m_align == SVG_PRESERVEASPECTRATIO_XMAXYMIN)
335 transform.translate(-extendedLogicalX, -extendedLogicalY);
336 else if (m_align == SVG_PRESERVEASPECTRATIO_XMINYMID || m_align == SVG_PRESERVEASPECTRATIO_XMIDYMID || m_align == SVG_PRESERVEASPECTRATIO_XMAXYMID)
337 transform.translate(-extendedLogicalX, -extendedLogicalY - (extendedLogicalHeight - extendedPhysicalHeight * extendedLogicalWidth / extendedPhysicalWidth) / 2);
339 transform.translate(-extendedLogicalX, -extendedLogicalY - (extendedLogicalHeight - extendedPhysicalHeight * extendedLogicalWidth / extendedPhysicalWidth));
344 String SVGPreserveAspectRatio::valueAsString() const
349 case SVG_PRESERVEASPECTRATIO_NONE:
352 case SVG_PRESERVEASPECTRATIO_XMINYMIN:
353 alignType = "xMinYMin";
355 case SVG_PRESERVEASPECTRATIO_XMIDYMIN:
356 alignType = "xMidYMin";
358 case SVG_PRESERVEASPECTRATIO_XMAXYMIN:
359 alignType = "xMaxYMin";
361 case SVG_PRESERVEASPECTRATIO_XMINYMID:
362 alignType = "xMinYMid";
364 case SVG_PRESERVEASPECTRATIO_XMIDYMID:
365 alignType = "xMidYMid";
367 case SVG_PRESERVEASPECTRATIO_XMAXYMID:
368 alignType = "xMaxYMid";
370 case SVG_PRESERVEASPECTRATIO_XMINYMAX:
371 alignType = "xMinYMax";
373 case SVG_PRESERVEASPECTRATIO_XMIDYMAX:
374 alignType = "xMidYMax";
376 case SVG_PRESERVEASPECTRATIO_XMAXYMAX:
377 alignType = "xMaxYMax";
379 case SVG_PRESERVEASPECTRATIO_UNKNOWN:
380 alignType = "unknown";
384 switch (m_meetOrSlice) {
386 case SVG_MEETORSLICE_UNKNOWN:
388 case SVG_MEETORSLICE_MEET:
389 return alignType + " meet";
390 case SVG_MEETORSLICE_SLICE:
391 return alignType + " slice";
395 void SVGPreserveAspectRatio::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement*)
397 ASSERT_NOT_REACHED();
400 void SVGPreserveAspectRatio::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase>, SVGElement*)
402 ASSERT(animationElement);
405 animationElement->animateDiscreteType(percentage, false, true, useToValue);
407 RefPtr<SVGPreserveAspectRatio> preserveAspectRatioToUse = useToValue ? toSVGPreserveAspectRatio(toValue) : toSVGPreserveAspectRatio(fromValue);
409 m_align = preserveAspectRatioToUse->m_align;
410 m_meetOrSlice = preserveAspectRatioToUse->m_meetOrSlice;
413 float SVGPreserveAspectRatio::calculateDistance(PassRefPtr<SVGPropertyBase> toValue, SVGElement* contextElement)
415 // No paced animations for SVGPreserveAspectRatio.