blob: 92ae2cf0ae0dc57aecb8c5f2da81ca7e8984a384 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
7 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
Ben Murdoch7757ec22013-07-23 11:17:36 +01009 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010010 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public License
22 * along with this library; see the file COPYING.LIB. If not, write to
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
25 *
26 */
27
28#include "config.h"
29#include "core/dom/ViewportArguments.h"
30
31#include "core/dom/Document.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010032#include "wtf/text/WTFString.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033
34using namespace std;
35
36namespace WebCore {
37
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010038static const float& compareIgnoringAuto(const float& value1, const float& value2, const float& (*compare) (const float&, const float&))
39{
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040 if (value1 == ViewportArguments::ValueAuto)
41 return value2;
42
43 if (value2 == ViewportArguments::ValueAuto)
44 return value1;
45
46 return compare(value1, value2);
47}
48
49static inline float clampLengthValue(float value)
50{
51 ASSERT(value != ViewportArguments::ValueDeviceWidth);
52 ASSERT(value != ViewportArguments::ValueDeviceHeight);
53
54 // Limits as defined in the css-device-adapt spec.
55 if (value != ViewportArguments::ValueAuto)
56 return min(float(10000), max(value, float(1)));
57 return value;
58}
59
60static inline float clampScaleValue(float value)
61{
62 ASSERT(value != ViewportArguments::ValueDeviceWidth);
63 ASSERT(value != ViewportArguments::ValueDeviceHeight);
64
65 // Limits as defined in the css-device-adapt spec.
66 if (value != ViewportArguments::ValueAuto)
67 return min(float(10), max(value, float(0.1)));
68 return value;
69}
70
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010071PageScaleConstraints ViewportArguments::resolve(const FloatSize& initialViewportSize, const FloatSize& deviceSize, int defaultWidth) const
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010072{
73 float resultWidth = width;
74 float resultMaxWidth = maxWidth;
75 float resultMinWidth = minWidth;
76 float resultHeight = height;
77 float resultMinHeight = minHeight;
78 float resultMaxHeight = maxHeight;
Ben Murdoch7757ec22013-07-23 11:17:36 +010079
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010080 float resultZoom = zoom;
81 float resultMinZoom = minZoom;
82 float resultMaxZoom = maxZoom;
83 float resultUserZoom = userZoom;
84
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010085 if (type == ViewportArguments::CSSDeviceAdaptation) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010086
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010087 // device-width/device-height not supported for @viewport.
88 ASSERT(resultMinWidth != ViewportArguments::ValueDeviceWidth);
89 ASSERT(resultMinWidth != ViewportArguments::ValueDeviceHeight);
90 ASSERT(resultMaxWidth != ViewportArguments::ValueDeviceWidth);
91 ASSERT(resultMaxWidth != ViewportArguments::ValueDeviceHeight);
92 ASSERT(resultMinHeight != ViewportArguments::ValueDeviceWidth);
93 ASSERT(resultMinHeight != ViewportArguments::ValueDeviceHeight);
94 ASSERT(resultMaxHeight != ViewportArguments::ValueDeviceWidth);
95 ASSERT(resultMaxHeight != ViewportArguments::ValueDeviceHeight);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010096
Ben Murdoch7757ec22013-07-23 11:17:36 +010097 // 1. Resolve min-zoom and max-zoom values.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010098 if (resultMinZoom != ViewportArguments::ValueAuto && resultMaxZoom != ViewportArguments::ValueAuto)
99 resultMaxZoom = max(resultMinZoom, resultMaxZoom);
100
Ben Murdoch7757ec22013-07-23 11:17:36 +0100101 // 2. Constrain zoom value to the [min-zoom, max-zoom] range.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100102 if (resultZoom != ViewportArguments::ValueAuto)
103 resultZoom = compareIgnoringAuto(resultMinZoom, compareIgnoringAuto(resultMaxZoom, resultZoom, min), max);
104
Ben Murdoch7757ec22013-07-23 11:17:36 +0100105 float extendZoom = compareIgnoringAuto(resultZoom, resultMaxZoom, min);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100106
Ben Murdoch7757ec22013-07-23 11:17:36 +0100107 if (extendZoom == ViewportArguments::ValueAuto) {
108 if (resultMaxWidth == ViewportArguments::ValueExtendToZoom)
109 resultMaxWidth = ViewportArguments::ValueAuto;
110
111 if (resultMaxHeight == ViewportArguments::ValueExtendToZoom)
112 resultMaxHeight = ViewportArguments::ValueAuto;
113
114 if (resultMinWidth == ViewportArguments::ValueExtendToZoom)
115 resultMinWidth = resultMaxWidth;
116
117 if (resultMinHeight == ViewportArguments::ValueExtendToZoom)
118 resultMinHeight = resultMaxHeight;
119 } else {
120 float extendWidth = initialViewportSize.width() / extendZoom;
121 float extendHeight = initialViewportSize.height() / extendZoom;
122
123 if (resultMaxWidth == ViewportArguments::ValueExtendToZoom)
124 resultMaxWidth = extendWidth;
125
126 if (resultMaxHeight == ViewportArguments::ValueExtendToZoom)
127 resultMaxHeight = extendHeight;
128
129 if (resultMinWidth == ViewportArguments::ValueExtendToZoom)
130 resultMinWidth = compareIgnoringAuto(extendWidth, resultMaxWidth, max);
131
132 if (resultMinHeight == ViewportArguments::ValueExtendToZoom)
133 resultMinHeight = compareIgnoringAuto(extendHeight, resultMaxHeight, max);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100134 }
135
Ben Murdoch7757ec22013-07-23 11:17:36 +0100136 // 4. Resolve initial width from min/max descriptors.
137 if (resultMinWidth != ViewportArguments::ValueAuto || resultMaxWidth != ViewportArguments::ValueAuto)
138 resultWidth = compareIgnoringAuto(resultMinWidth, compareIgnoringAuto(resultMaxWidth, initialViewportSize.width(), min), max);
139
140 // 5. Resolve initial height from min/max descriptors.
141 if (resultMinHeight != ViewportArguments::ValueAuto || resultMaxHeight != ViewportArguments::ValueAuto)
142 resultHeight = compareIgnoringAuto(resultMinHeight, compareIgnoringAuto(resultMaxHeight, initialViewportSize.height(), min), max);
143
144 // 6-7. Resolve width value.
145 if (resultWidth == ViewportArguments::ValueAuto) {
146 if (resultHeight == ViewportArguments::ValueAuto || !initialViewportSize .height())
147 resultWidth = initialViewportSize.width();
148 else
149 resultWidth = resultHeight * (initialViewportSize.width() / initialViewportSize.height());
150 }
151
152 // 8. Resolve height value.
Ben Murdoche69819b2013-07-17 14:56:49 +0100153 if (resultHeight == ViewportArguments::ValueAuto) {
154 if (!initialViewportSize.width())
155 resultHeight = initialViewportSize.height();
156 else
157 resultHeight = resultWidth * initialViewportSize.height() / initialViewportSize.width();
158 }
Ben Murdoch7757ec22013-07-23 11:17:36 +0100159
160 PageScaleConstraints result;
161 result.minimumScale = resultMinZoom;
162 result.maximumScale = resultMaxZoom;
163 result.initialScale = resultZoom;
164 result.layoutSize.setWidth(resultWidth);
165 result.layoutSize.setHeight(resultHeight);
166 return result;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100167 }
168
Ben Murdoch7757ec22013-07-23 11:17:36 +0100169 switch (static_cast<int>(resultWidth)) {
170 case ViewportArguments::ValueDeviceWidth:
171 resultWidth = deviceSize.width();
172 break;
173 case ViewportArguments::ValueDeviceHeight:
174 resultWidth = deviceSize.height();
175 break;
176 }
177
178 switch (static_cast<int>(resultHeight)) {
179 case ViewportArguments::ValueDeviceWidth:
180 resultHeight = deviceSize.width();
181 break;
182 case ViewportArguments::ValueDeviceHeight:
183 resultHeight = deviceSize.height();
184 break;
185 }
186
187 if (type != ViewportArguments::Implicit) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100188 // Clamp values to a valid range, but not for @viewport since is
189 // not mandated by the specification.
190 resultWidth = clampLengthValue(resultWidth);
191 resultHeight = clampLengthValue(resultHeight);
192 resultZoom = clampScaleValue(resultZoom);
193 resultMinZoom = clampScaleValue(resultMinZoom);
194 resultMaxZoom = clampScaleValue(resultMaxZoom);
195 }
196
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100197 PageScaleConstraints result;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100198
199 // Resolve minimum-scale and maximum-scale values according to spec.
200 if (resultMinZoom == ViewportArguments::ValueAuto)
201 result.minimumScale = float(0.25);
202 else
203 result.minimumScale = resultMinZoom;
204
205 if (resultMaxZoom == ViewportArguments::ValueAuto) {
206 result.maximumScale = float(5.0);
207 result.minimumScale = min(float(5.0), result.minimumScale);
208 } else
209 result.maximumScale = resultMaxZoom;
210 result.maximumScale = max(result.minimumScale, result.maximumScale);
211
212 // Resolve initial-scale value.
213 result.initialScale = resultZoom;
214 if (resultZoom == ViewportArguments::ValueAuto) {
215 result.initialScale = initialViewportSize.width() / defaultWidth;
Ben Murdoche69819b2013-07-17 14:56:49 +0100216 if (resultWidth != ViewportArguments::ValueAuto && resultWidth > 0)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100217 result.initialScale = initialViewportSize.width() / resultWidth;
Ben Murdoche69819b2013-07-17 14:56:49 +0100218 if (resultHeight != ViewportArguments::ValueAuto && resultHeight > 0) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100219 // if 'auto', the initial-scale will be negative here and thus ignored.
220 result.initialScale = max<float>(result.initialScale, initialViewportSize.height() / resultHeight);
221 }
222 }
223
224 // Constrain initial-scale value to minimum-scale/maximum-scale range.
225 result.initialScale = min(result.maximumScale, max(result.minimumScale, result.initialScale));
226
227 // Resolve width value.
228 if (resultWidth == ViewportArguments::ValueAuto) {
229 if (resultZoom == ViewportArguments::ValueAuto)
230 resultWidth = defaultWidth;
231 else if (resultHeight != ViewportArguments::ValueAuto)
232 resultWidth = resultHeight * (initialViewportSize.width() / initialViewportSize.height());
233 else
234 resultWidth = initialViewportSize.width() / result.initialScale;
235 }
236
237 // Resolve height value.
238 if (resultHeight == ViewportArguments::ValueAuto)
239 resultHeight = resultWidth * (initialViewportSize.height() / initialViewportSize.width());
240
241 if (type == ViewportArguments::ViewportMeta) {
242 // Extend width and height to fill the visual viewport for the resolved initial-scale.
243 resultWidth = max<float>(resultWidth, initialViewportSize.width() / result.initialScale);
244 resultHeight = max<float>(resultHeight, initialViewportSize.height() / result.initialScale);
245 }
246
247 result.layoutSize.setWidth(resultWidth);
248 result.layoutSize.setHeight(resultHeight);
249
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100250 // If user-scalable = no, lock the min/max scale to the computed initial
251 // scale.
252 if (!resultUserZoom)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100253 result.maximumScale = result.minimumScale = result.initialScale;
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100254
255 // Only set initialScale to a value if it was explicitly set.
256 if (resultZoom == ViewportArguments::ValueAuto)
257 result.initialScale = ViewportArguments::ValueAuto;
258
259 return result;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100260}
261
262static float numericPrefix(const String& keyString, const String& valueString, Document* document, bool* ok = 0)
263{
264 size_t parsedLength;
265 float value;
266 if (valueString.is8Bit())
267 value = charactersToFloat(valueString.characters8(), valueString.length(), parsedLength);
268 else
269 value = charactersToFloat(valueString.characters16(), valueString.length(), parsedLength);
270 if (!parsedLength) {
271 reportViewportWarning(document, UnrecognizedViewportArgumentValueError, valueString, keyString);
272 if (ok)
273 *ok = false;
274 return 0;
275 }
276 if (parsedLength < valueString.length())
277 reportViewportWarning(document, TruncatedViewportArgumentValueError, valueString, keyString);
278 if (ok)
279 *ok = true;
280 return value;
281}
282
283static float findSizeValue(const String& keyString, const String& valueString, Document* document)
284{
285 // 1) Non-negative number values are translated to px lengths.
286 // 2) Negative number values are translated to auto.
287 // 3) device-width and device-height are used as keywords.
288 // 4) Other keywords and unknown values translate to 0.0.
289
290 if (equalIgnoringCase(valueString, "device-width"))
291 return ViewportArguments::ValueDeviceWidth;
292 if (equalIgnoringCase(valueString, "device-height"))
293 return ViewportArguments::ValueDeviceHeight;
294
295 float value = numericPrefix(keyString, valueString, document);
296
297 if (value < 0)
298 return ViewportArguments::ValueAuto;
299
300 return value;
301}
302
303static float findScaleValue(const String& keyString, const String& valueString, Document* document)
304{
305 // 1) Non-negative number values are translated to <number> values.
306 // 2) Negative number values are translated to auto.
307 // 3) yes is translated to 1.0.
308 // 4) device-width and device-height are translated to 10.0.
309 // 5) no and unknown values are translated to 0.0
310
311 if (equalIgnoringCase(valueString, "yes"))
312 return 1;
313 if (equalIgnoringCase(valueString, "no"))
314 return 0;
315 if (equalIgnoringCase(valueString, "device-width"))
316 return 10;
317 if (equalIgnoringCase(valueString, "device-height"))
318 return 10;
319
320 float value = numericPrefix(keyString, valueString, document);
321
322 if (value < 0)
323 return ViewportArguments::ValueAuto;
324
325 if (value > 10.0)
326 reportViewportWarning(document, MaximumScaleTooLargeError, String(), String());
327
328 return value;
329}
330
331static float findUserScalableValue(const String& keyString, const String& valueString, Document* document)
332{
333 // yes and no are used as keywords.
334 // Numbers >= 1, numbers <= -1, device-width and device-height are mapped to yes.
335 // Numbers in the range <-1, 1>, and unknown values, are mapped to no.
336
337 if (equalIgnoringCase(valueString, "yes"))
338 return 1;
339 if (equalIgnoringCase(valueString, "no"))
340 return 0;
341 if (equalIgnoringCase(valueString, "device-width"))
342 return 1;
343 if (equalIgnoringCase(valueString, "device-height"))
344 return 1;
345
346 float value = numericPrefix(keyString, valueString, document);
347
348 if (fabs(value) < 1)
349 return 0;
350
351 return 1;
352}
353
354static float findTargetDensityDPIValue(const String& keyString, const String& valueString, Document* document)
355{
356 if (equalIgnoringCase(valueString, "device-dpi"))
357 return ViewportArguments::ValueDeviceDPI;
358 if (equalIgnoringCase(valueString, "low-dpi"))
359 return ViewportArguments::ValueLowDPI;
360 if (equalIgnoringCase(valueString, "medium-dpi"))
361 return ViewportArguments::ValueMediumDPI;
362 if (equalIgnoringCase(valueString, "high-dpi"))
363 return ViewportArguments::ValueHighDPI;
364
365 bool ok;
366 float value = numericPrefix(keyString, valueString, document, &ok);
367 if (!ok || value < 70 || value > 400)
368 return ViewportArguments::ValueAuto;
369
370 return value;
371}
372
373void setViewportFeature(const String& keyString, const String& valueString, Document* document, void* data)
374{
375 ViewportArguments* arguments = static_cast<ViewportArguments*>(data);
376
377 if (keyString == "width")
378 arguments->width = findSizeValue(keyString, valueString, document);
379 else if (keyString == "height")
380 arguments->height = findSizeValue(keyString, valueString, document);
381 else if (keyString == "initial-scale")
382 arguments->zoom = findScaleValue(keyString, valueString, document);
383 else if (keyString == "minimum-scale")
384 arguments->minZoom = findScaleValue(keyString, valueString, document);
385 else if (keyString == "maximum-scale")
386 arguments->maxZoom = findScaleValue(keyString, valueString, document);
387 else if (keyString == "user-scalable")
388 arguments->userZoom = findUserScalableValue(keyString, valueString, document);
389 else if (keyString == "target-densitydpi") {
390 arguments->deprecatedTargetDensityDPI = findTargetDensityDPIValue(keyString, valueString, document);
391 reportViewportWarning(document, TargetDensityDpiUnsupported, String(), String());
392 } else
393 reportViewportWarning(document, UnrecognizedViewportArgumentKeyError, keyString, String());
394}
395
396static const char* viewportErrorMessageTemplate(ViewportErrorCode errorCode)
397{
398 static const char* const errors[] = {
399 "Viewport argument key \"%replacement1\" not recognized and ignored.",
400 "Viewport argument value \"%replacement1\" for key \"%replacement2\" is invalid, and has been ignored.",
401 "Viewport argument value \"%replacement1\" for key \"%replacement2\" was truncated to its numeric prefix.",
402 "Viewport maximum-scale cannot be larger than 10.0. The maximum-scale will be set to 10.0.",
403 "Viewport target-densitydpi is not supported.",
404 };
405
406 return errors[errorCode];
407}
408
409static MessageLevel viewportErrorMessageLevel(ViewportErrorCode errorCode)
410{
411 switch (errorCode) {
412 case TruncatedViewportArgumentValueError:
413 case TargetDensityDpiUnsupported:
414 return WarningMessageLevel;
415 case UnrecognizedViewportArgumentKeyError:
416 case UnrecognizedViewportArgumentValueError:
417 case MaximumScaleTooLargeError:
418 return ErrorMessageLevel;
419 }
420
421 ASSERT_NOT_REACHED();
422 return ErrorMessageLevel;
423}
424
425void reportViewportWarning(Document* document, ViewportErrorCode errorCode, const String& replacement1, const String& replacement2)
426{
427 Frame* frame = document->frame();
428 if (!frame)
429 return;
430
431 String message = viewportErrorMessageTemplate(errorCode);
432 if (!replacement1.isNull())
433 message.replace("%replacement1", replacement1);
434 if (!replacement2.isNull())
435 message.replace("%replacement2", replacement2);
436
437 if ((errorCode == UnrecognizedViewportArgumentValueError || errorCode == TruncatedViewportArgumentValueError) && replacement1.find(';') != WTF::notFound)
438 message.append(" Note that ';' is not a separator in viewport values. The list should be comma-separated.");
439
440 // FIXME: This message should be moved off the console once a solution to https://bugs.webkit.org/show_bug.cgi?id=103274 exists.
441 document->addConsoleMessage(RenderingMessageSource, viewportErrorMessageLevel(errorCode), message);
442}
443
444} // namespace WebCore