Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: third_party/WebKit/Source/core/svg/SVGLengthContext.cpp

Issue 1544543003: Adding support of viewport units to SVG (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Align with review comments Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/svg/SVGLength.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org> 2 * Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org> 3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2007 Apple Inc. All rights reserved. 4 * Copyright (C) 2007 Apple Inc. All rights reserved.
5 * Copyright (C) Research In Motion Limited 2011. All rights reserved. 5 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public 8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details. 15 * Library General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Library General Public License 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 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, 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA. 20 * Boston, MA 02110-1301, USA.
21 */ 21 */
22 22
23 #include "config.h" 23 #include "config.h"
24 #include "core/svg/SVGLengthContext.h" 24 #include "core/svg/SVGLengthContext.h"
25 25
26 #include "core/css/CSSHelper.h" 26 #include "core/css/CSSHelper.h"
27 #include "core/css/CSSPrimitiveValue.h" 27 #include "core/css/CSSPrimitiveValue.h"
28 #include "core/dom/NodeComputedStyle.h" 28 #include "core/dom/NodeComputedStyle.h"
29 #include "core/frame/FrameView.h"
29 #include "core/layout/LayoutObject.h" 30 #include "core/layout/LayoutObject.h"
30 #include "core/style/ComputedStyle.h" 31 #include "core/style/ComputedStyle.h"
31 #include "core/svg/SVGSVGElement.h" 32 #include "core/svg/SVGSVGElement.h"
32 #include "platform/LengthFunctions.h" 33 #include "platform/LengthFunctions.h"
33 #include "platform/fonts/FontMetrics.h" 34 #include "platform/fonts/FontMetrics.h"
34 35
35 namespace blink { 36 namespace blink {
36 37
37 static inline float dimensionForLengthMode(SVGLengthMode mode, const FloatSize& viewportSize) 38 static inline float dimensionForLengthMode(SVGLengthMode mode, const FloatSize& viewportSize)
38 { 39 {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 return value / fontSize; 95 return value / fontSize;
95 } 96 }
96 97
97 static float convertValueFromEMSToUserUnits(const ComputedStyle* style, float va lue) 98 static float convertValueFromEMSToUserUnits(const ComputedStyle* style, float va lue)
98 { 99 {
99 if (!style) 100 if (!style)
100 return 0; 101 return 0;
101 return value * style->specifiedFontSize(); 102 return value * style->specifiedFontSize();
102 } 103 }
103 104
105 static inline float viewportLengthPercent(const float widthOrHeight)
106 {
107 return widthOrHeight / 100;
108 }
109
110 static inline float viewportMinPercent(const FloatSize& viewportSize)
111 {
112 return std::min(viewportSize.width(), viewportSize.height()) / 100;
113 }
114
115 static inline float viewportMaxPercent(const FloatSize& viewportSize)
116 {
117 return std::max(viewportSize.width(), viewportSize.height()) / 100;
118 }
119
120 static inline float dimensionForViewportUnit(const SVGElement* context, CSSPrimi tiveValue::UnitType unit)
121 {
122 if (!context)
123 return 0;
124
125 const Document& document = context->document();
126 FrameView* view = document.view();
127 if (!view)
128 return 0;
129
130 const ComputedStyle* style = computedStyleForLengthResolving(context);
131 if (!style)
132 return 0;
133
134 FloatSize viewportSize(view->width(), view->height());
135
136 switch (unit) {
137 case CSSPrimitiveValue::UnitType::ViewportWidth:
138 return viewportLengthPercent(viewportSize.width()) / style->effectiveZoo m();
139
140 case CSSPrimitiveValue::UnitType::ViewportHeight:
141 return viewportLengthPercent(viewportSize.height()) / style->effectiveZo om();
142
143 case CSSPrimitiveValue::UnitType::ViewportMin:
144 return viewportMinPercent(viewportSize) / style->effectiveZoom();
145
146 case CSSPrimitiveValue::UnitType::ViewportMax:
147 return viewportMaxPercent(viewportSize) / style->effectiveZoom();
148 default:
149 break;
150 }
151
152 ASSERT_NOT_REACHED();
153 return 0;
154 }
155
104 SVGLengthContext::SVGLengthContext(const SVGElement* context) 156 SVGLengthContext::SVGLengthContext(const SVGElement* context)
105 : m_context(context) 157 : m_context(context)
106 { 158 {
107 } 159 }
108 160
109 FloatRect SVGLengthContext::resolveRectangle(const SVGElement* context, SVGUnitT ypes::SVGUnitType type, const FloatRect& viewport, const SVGLength& x, const SVG Length& y, const SVGLength& width, const SVGLength& height) 161 FloatRect SVGLengthContext::resolveRectangle(const SVGElement* context, SVGUnitT ypes::SVGUnitType type, const FloatRect& viewport, const SVGLength& x, const SVG Length& y, const SVGLength& width, const SVGLength& height)
110 { 162 {
111 ASSERT(type != SVGUnitTypes::SVG_UNIT_TYPE_UNKNOWN); 163 ASSERT(type != SVGUnitTypes::SVG_UNIT_TYPE_UNKNOWN);
112 if (type != SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE) { 164 if (type != SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE) {
113 const FloatSize& viewportSize = viewport.size(); 165 const FloatSize& viewportSize = viewport.size();
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 break; 271 break;
220 case CSSPrimitiveValue::UnitType::Picas: 272 case CSSPrimitiveValue::UnitType::Picas:
221 userUnits = value * cssPixelsPerPica; 273 userUnits = value * cssPixelsPerPica;
222 break; 274 break;
223 case CSSPrimitiveValue::UnitType::Rems: 275 case CSSPrimitiveValue::UnitType::Rems:
224 userUnits = convertValueFromEMSToUserUnits(rootElementStyle(m_context), value); 276 userUnits = convertValueFromEMSToUserUnits(rootElementStyle(m_context), value);
225 break; 277 break;
226 case CSSPrimitiveValue::UnitType::Chs: 278 case CSSPrimitiveValue::UnitType::Chs:
227 userUnits = convertValueFromCHSToUserUnits(value); 279 userUnits = convertValueFromCHSToUserUnits(value);
228 break; 280 break;
281 case CSSPrimitiveValue::UnitType::ViewportWidth:
282 case CSSPrimitiveValue::UnitType::ViewportHeight:
283 case CSSPrimitiveValue::UnitType::ViewportMin:
284 case CSSPrimitiveValue::UnitType::ViewportMax:
285 userUnits = value * dimensionForViewportUnit(m_context, fromUnit);
286 break;
229 default: 287 default:
230 ASSERT_NOT_REACHED(); 288 ASSERT_NOT_REACHED();
231 break; 289 break;
232 } 290 }
233 291
234 // Since we mix css <length> values with svg's length values we need to 292 // Since we mix css <length> values with svg's length values we need to
235 // clamp values to the narrowest range, otherwise it can result in 293 // clamp values to the narrowest range, otherwise it can result in
236 // rendering issues. 294 // rendering issues.
237 return CSSPrimitiveValue::clampToCSSLengthRange(userUnits); 295 return CSSPrimitiveValue::clampToCSSLengthRange(userUnits);
238 } 296 }
(...skipping 27 matching lines...) Expand all
266 case CSSPrimitiveValue::UnitType::Centimeters: 324 case CSSPrimitiveValue::UnitType::Centimeters:
267 return value / cssPixelsPerCentimeter; 325 return value / cssPixelsPerCentimeter;
268 case CSSPrimitiveValue::UnitType::Millimeters: 326 case CSSPrimitiveValue::UnitType::Millimeters:
269 return value / cssPixelsPerMillimeter; 327 return value / cssPixelsPerMillimeter;
270 case CSSPrimitiveValue::UnitType::Inches: 328 case CSSPrimitiveValue::UnitType::Inches:
271 return value / cssPixelsPerInch; 329 return value / cssPixelsPerInch;
272 case CSSPrimitiveValue::UnitType::Points: 330 case CSSPrimitiveValue::UnitType::Points:
273 return value / cssPixelsPerPoint; 331 return value / cssPixelsPerPoint;
274 case CSSPrimitiveValue::UnitType::Picas: 332 case CSSPrimitiveValue::UnitType::Picas:
275 return value / cssPixelsPerPica; 333 return value / cssPixelsPerPica;
334 case CSSPrimitiveValue::UnitType::ViewportWidth:
335 case CSSPrimitiveValue::UnitType::ViewportHeight:
336 case CSSPrimitiveValue::UnitType::ViewportMin:
337 case CSSPrimitiveValue::UnitType::ViewportMax:
338 return value / dimensionForViewportUnit(m_context, toUnit);
276 default: 339 default:
277 break; 340 break;
278 } 341 }
279 342
280 ASSERT_NOT_REACHED(); 343 ASSERT_NOT_REACHED();
281 return 0; 344 return 0;
282 } 345 }
283 346
284 float SVGLengthContext::convertValueFromUserUnitsToCHS(float value) const 347 float SVGLengthContext::convertValueFromUserUnitsToCHS(float value) const
285 { 348 {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 410
348 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement); 411 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement);
349 viewportSize = svg.currentViewBoxRect().size(); 412 viewportSize = svg.currentViewBoxRect().size();
350 if (viewportSize.isEmpty()) 413 if (viewportSize.isEmpty())
351 viewportSize = svg.currentViewportSize(); 414 viewportSize = svg.currentViewportSize();
352 415
353 return true; 416 return true;
354 } 417 }
355 418
356 } 419 }
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/svg/SVGLength.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698