OLD | NEW |
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. |
(...skipping 13 matching lines...) Expand all Loading... |
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/layout/LayoutObject.h" | 27 #include "core/layout/LayoutObject.h" |
28 #include "core/layout/style/LayoutStyle.h" | 28 #include "core/layout/style/LayoutStyle.h" |
29 #include "core/svg/SVGSVGElement.h" | 29 #include "core/svg/SVGSVGElement.h" |
30 #include "platform/fonts/FontMetrics.h" | 30 #include "platform/fonts/FontMetrics.h" |
31 | 31 |
32 namespace blink { | 32 namespace blink { |
33 | 33 |
| 34 static inline float dimensionForLengthMode(SVGLengthMode mode, const FloatSize&
viewportSize) |
| 35 { |
| 36 switch (mode) { |
| 37 case LengthModeWidth: |
| 38 return viewportSize.width(); |
| 39 case LengthModeHeight: |
| 40 return viewportSize.height(); |
| 41 case LengthModeOther: |
| 42 return sqrtf(viewportSize.diagonalLengthSquared() / 2); |
| 43 } |
| 44 ASSERT_NOT_REACHED(); |
| 45 return 0; |
| 46 } |
| 47 |
| 48 static float convertValueFromPercentageToUserUnits(const SVGLength& value, const
FloatSize& viewportSize) |
| 49 { |
| 50 return value.scaleByPercentage(dimensionForLengthMode(value.unitMode(), view
portSize)); |
| 51 } |
| 52 |
34 SVGLengthContext::SVGLengthContext(const SVGElement* context) | 53 SVGLengthContext::SVGLengthContext(const SVGElement* context) |
35 : m_context(context) | 54 : m_context(context) |
36 { | 55 { |
37 } | 56 } |
38 | 57 |
39 FloatRect SVGLengthContext::resolveRectangle(const SVGElement* context, SVGUnitT
ypes::SVGUnitType type, const FloatRect& viewport, PassRefPtrWillBeRawPtr<SVGLen
gth> passX, PassRefPtrWillBeRawPtr<SVGLength> passY, PassRefPtrWillBeRawPtr<SVGL
ength> passWidth, PassRefPtrWillBeRawPtr<SVGLength> passHeight) | 58 FloatRect SVGLengthContext::resolveRectangle(const SVGElement* context, SVGUnitT
ypes::SVGUnitType type, const FloatRect& viewport, PassRefPtrWillBeRawPtr<SVGLen
gth> passX, PassRefPtrWillBeRawPtr<SVGLength> passY, PassRefPtrWillBeRawPtr<SVGL
ength> passWidth, PassRefPtrWillBeRawPtr<SVGLength> passHeight) |
40 { | 59 { |
41 RefPtrWillBeRawPtr<SVGLength> x = passX; | 60 RefPtrWillBeRawPtr<SVGLength> x = passX; |
42 RefPtrWillBeRawPtr<SVGLength> y = passY; | 61 RefPtrWillBeRawPtr<SVGLength> y = passY; |
43 RefPtrWillBeRawPtr<SVGLength> width = passWidth; | 62 RefPtrWillBeRawPtr<SVGLength> width = passWidth; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 case LengthTypeUnknown: | 111 case LengthTypeUnknown: |
93 return 0; | 112 return 0; |
94 case LengthTypeNumber: | 113 case LengthTypeNumber: |
95 return value; | 114 return value; |
96 case LengthTypePX: | 115 case LengthTypePX: |
97 return value; | 116 return value; |
98 case LengthTypePercentage: { | 117 case LengthTypePercentage: { |
99 FloatSize viewportSize; | 118 FloatSize viewportSize; |
100 if (!determineViewport(viewportSize)) | 119 if (!determineViewport(viewportSize)) |
101 return 0; | 120 return 0; |
102 return convertValueFromPercentageToUserUnits(value, mode, viewportSize)
/ 100; | 121 return value * dimensionForLengthMode(mode, viewportSize) / 100; |
103 } | 122 } |
104 case LengthTypeEMS: | 123 case LengthTypeEMS: |
105 return convertValueFromEMSToUserUnits(value); | 124 return convertValueFromEMSToUserUnits(value); |
106 case LengthTypeEXS: | 125 case LengthTypeEXS: |
107 return convertValueFromEXSToUserUnits(value); | 126 return convertValueFromEXSToUserUnits(value); |
108 case LengthTypeCM: | 127 case LengthTypeCM: |
109 return value * cssPixelsPerCentimeter; | 128 return value * cssPixelsPerCentimeter; |
110 case LengthTypeMM: | 129 case LengthTypeMM: |
111 return value * cssPixelsPerMillimeter; | 130 return value * cssPixelsPerMillimeter; |
112 case LengthTypeIN: | 131 case LengthTypeIN: |
(...skipping 12 matching lines...) Expand all Loading... |
125 { | 144 { |
126 switch (toUnit) { | 145 switch (toUnit) { |
127 case LengthTypeUnknown: | 146 case LengthTypeUnknown: |
128 return 0; | 147 return 0; |
129 case LengthTypeNumber: | 148 case LengthTypeNumber: |
130 return value; | 149 return value; |
131 case LengthTypePercentage: { | 150 case LengthTypePercentage: { |
132 FloatSize viewportSize; | 151 FloatSize viewportSize; |
133 if (!determineViewport(viewportSize)) | 152 if (!determineViewport(viewportSize)) |
134 return 0; | 153 return 0; |
135 return convertValueFromUserUnitsToPercentage(value * 100, mode, viewport
Size); | 154 // LengthTypePercentage is represented with 100% = 100.0. |
| 155 // Good for accuracy but could eventually be changed. |
| 156 return value * 100 / dimensionForLengthMode(mode, viewportSize); |
136 } | 157 } |
137 case LengthTypeEMS: | 158 case LengthTypeEMS: |
138 return convertValueFromUserUnitsToEMS(value); | 159 return convertValueFromUserUnitsToEMS(value); |
139 case LengthTypeEXS: | 160 case LengthTypeEXS: |
140 return convertValueFromUserUnitsToEXS(value); | 161 return convertValueFromUserUnitsToEXS(value); |
141 case LengthTypePX: | 162 case LengthTypePX: |
142 return value; | 163 return value; |
143 case LengthTypeCM: | 164 case LengthTypeCM: |
144 return value / cssPixelsPerCentimeter; | 165 return value / cssPixelsPerCentimeter; |
145 case LengthTypeMM: | 166 case LengthTypeMM: |
146 return value / cssPixelsPerMillimeter; | 167 return value / cssPixelsPerMillimeter; |
147 case LengthTypeIN: | 168 case LengthTypeIN: |
148 return value / cssPixelsPerInch; | 169 return value / cssPixelsPerInch; |
149 case LengthTypePT: | 170 case LengthTypePT: |
150 return value / cssPixelsPerPoint; | 171 return value / cssPixelsPerPoint; |
151 case LengthTypePC: | 172 case LengthTypePC: |
152 return value / cssPixelsPerPica; | 173 return value / cssPixelsPerPica; |
153 } | 174 } |
154 | 175 |
155 ASSERT_NOT_REACHED(); | 176 ASSERT_NOT_REACHED(); |
156 return 0; | 177 return 0; |
157 } | 178 } |
158 | 179 |
159 static inline float dimensionForLengthMode(SVGLengthMode mode, const FloatSize&
viewportSize) | |
160 { | |
161 switch (mode) { | |
162 case LengthModeWidth: | |
163 return viewportSize.width(); | |
164 case LengthModeHeight: | |
165 return viewportSize.height(); | |
166 case LengthModeOther: | |
167 return sqrtf(viewportSize.diagonalLengthSquared() / 2); | |
168 } | |
169 ASSERT_NOT_REACHED(); | |
170 return 0; | |
171 } | |
172 | |
173 float SVGLengthContext::convertValueFromUserUnitsToPercentage(float value, SVGLe
ngthMode mode, const FloatSize& viewportSize) | |
174 { | |
175 return value / dimensionForLengthMode(mode, viewportSize) * 100; | |
176 } | |
177 | |
178 float SVGLengthContext::convertValueFromPercentageToUserUnits(float value, SVGLe
ngthMode mode, const FloatSize& viewportSize) | |
179 { | |
180 return value * dimensionForLengthMode(mode, viewportSize); | |
181 } | |
182 | |
183 float SVGLengthContext::convertValueFromPercentageToUserUnits(const SVGLength& v
alue, const FloatSize& viewportSize) | |
184 { | |
185 return value.scaleByPercentage(dimensionForLengthMode(value.unitMode(), view
portSize)); | |
186 } | |
187 | |
188 static inline LayoutStyle* layoutStyleForLengthResolving(const SVGElement* conte
xt) | 180 static inline LayoutStyle* layoutStyleForLengthResolving(const SVGElement* conte
xt) |
189 { | 181 { |
190 if (!context) | 182 if (!context) |
191 return 0; | 183 return 0; |
192 | 184 |
193 const ContainerNode* currentContext = context; | 185 const ContainerNode* currentContext = context; |
194 do { | 186 do { |
195 if (currentContext->renderer()) | 187 if (currentContext->renderer()) |
196 return currentContext->renderer()->style(); | 188 return currentContext->renderer()->style(); |
197 currentContext = currentContext->parentNode(); | 189 currentContext = currentContext->parentNode(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 259 |
268 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement); | 260 const SVGSVGElement& svg = toSVGSVGElement(*viewportElement); |
269 viewportSize = svg.currentViewBoxRect().size(); | 261 viewportSize = svg.currentViewBoxRect().size(); |
270 if (viewportSize.isEmpty()) | 262 if (viewportSize.isEmpty()) |
271 viewportSize = svg.currentViewportSize(); | 263 viewportSize = svg.currentViewportSize(); |
272 | 264 |
273 return true; | 265 return true; |
274 } | 266 } |
275 | 267 |
276 } | 268 } |
OLD | NEW |