~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/dom/ViewportArguments.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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)
 
9
 * Copyright (C) 2012 Intel Corporation. All rights reserved.
 
10
 *
 
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 "ViewportArguments.h"
 
30
 
 
31
#include "Chrome.h"
 
32
#include "Console.h"
 
33
#include "DOMWindow.h"
 
34
#include "Document.h"
 
35
#include "Frame.h"
 
36
#include "IntSize.h"
 
37
#include "Page.h"
 
38
#include "ScriptableDocumentParser.h"
 
39
#include <wtf/text/WTFString.h>
 
40
 
 
41
using namespace std;
 
42
 
 
43
namespace WebCore {
 
44
 
 
45
const float ViewportArguments::deprecatedTargetDPI = 160;
 
46
 
 
47
static const float& compareIgnoringAuto(const float& value1, const float& value2, const float& (*compare) (const float&, const float&))
 
48
{
 
49
    ASSERT(value1 != ViewportArguments::ValueAuto || value2 != ViewportArguments::ValueAuto);
 
50
 
 
51
    if (value1 == ViewportArguments::ValueAuto)
 
52
        return value2;
 
53
 
 
54
    if (value2 == ViewportArguments::ValueAuto)
 
55
        return value1;
 
56
 
 
57
    return compare(value1, value2);
 
58
}
 
59
 
 
60
static inline float clampLengthValue(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(10000), max(value, float(1)));
 
68
    return value;
 
69
}
 
70
 
 
71
static inline float clampScaleValue(float value)
 
72
{
 
73
    ASSERT(value != ViewportArguments::ValueDeviceWidth);
 
74
    ASSERT(value != ViewportArguments::ValueDeviceHeight);
 
75
 
 
76
    // Limits as defined in the css-device-adapt spec.
 
77
    if (value != ViewportArguments::ValueAuto)
 
78
        return min(float(10), max(value, float(0.1)));
 
79
    return value;
 
80
}
 
81
 
 
82
ViewportAttributes ViewportArguments::resolve(const FloatSize& initialViewportSize, const FloatSize& deviceSize, int defaultWidth) const
 
83
{
 
84
    float resultWidth = width;
 
85
    float resultMaxWidth = maxWidth;
 
86
    float resultMinWidth = minWidth;
 
87
    float resultHeight = height;
 
88
    float resultMinHeight = minHeight;
 
89
    float resultMaxHeight = maxHeight;
 
90
    float resultZoom = zoom;
 
91
    float resultMinZoom = minZoom;
 
92
    float resultMaxZoom = maxZoom;
 
93
    float resultUserZoom = userZoom;
 
94
 
 
95
    switch (int(resultWidth)) {
 
96
    case ViewportArguments::ValueDeviceWidth:
 
97
        resultWidth = deviceSize.width();
 
98
        break;
 
99
    case ViewportArguments::ValueDeviceHeight:
 
100
        resultWidth = deviceSize.height();
 
101
        break;
 
102
    }
 
103
 
 
104
    switch (int(resultHeight)) {
 
105
    case ViewportArguments::ValueDeviceWidth:
 
106
        resultHeight = deviceSize.width();
 
107
        break;
 
108
    case ViewportArguments::ValueDeviceHeight:
 
109
        resultHeight = deviceSize.height();
 
110
        break;
 
111
    }
 
112
 
 
113
    if (type == ViewportArguments::CSSDeviceAdaptation) {
 
114
        switch (int(resultMinWidth)) {
 
115
        case ViewportArguments::ValueDeviceWidth:
 
116
            resultMinWidth = deviceSize.width();
 
117
            break;
 
118
        case ViewportArguments::ValueDeviceHeight:
 
119
            resultMinWidth = deviceSize.height();
 
120
            break;
 
121
        }
 
122
 
 
123
        switch (int(resultMaxWidth)) {
 
124
        case ViewportArguments::ValueDeviceWidth:
 
125
            resultMaxWidth = deviceSize.width();
 
126
            break;
 
127
        case ViewportArguments::ValueDeviceHeight:
 
128
            resultMaxWidth = deviceSize.height();
 
129
            break;
 
130
        }
 
131
 
 
132
        switch (int(resultMinHeight)) {
 
133
        case ViewportArguments::ValueDeviceWidth:
 
134
            resultMinHeight = deviceSize.width();
 
135
            break;
 
136
        case ViewportArguments::ValueDeviceHeight:
 
137
            resultMinHeight = deviceSize.height();
 
138
            break;
 
139
        }
 
140
 
 
141
        switch (int(resultMaxHeight)) {
 
142
        case ViewportArguments::ValueDeviceWidth:
 
143
            resultMaxHeight = deviceSize.width();
 
144
            break;
 
145
        case ViewportArguments::ValueDeviceHeight:
 
146
            resultMaxHeight = deviceSize.height();
 
147
            break;
 
148
        }
 
149
 
 
150
        if (resultMinWidth != ViewportArguments::ValueAuto || resultMaxWidth != ViewportArguments::ValueAuto)
 
151
            resultWidth = compareIgnoringAuto(resultMinWidth, compareIgnoringAuto(resultMaxWidth, deviceSize.width(), min), max);
 
152
 
 
153
        if (resultMinHeight != ViewportArguments::ValueAuto || resultMaxHeight != ViewportArguments::ValueAuto)
 
154
            resultHeight = compareIgnoringAuto(resultMinHeight, compareIgnoringAuto(resultMaxHeight, deviceSize.height(), min), max);
 
155
 
 
156
        if (resultMinZoom != ViewportArguments::ValueAuto && resultMaxZoom != ViewportArguments::ValueAuto)
 
157
            resultMaxZoom = max(resultMinZoom, resultMaxZoom);
 
158
 
 
159
        if (resultZoom != ViewportArguments::ValueAuto)
 
160
            resultZoom = compareIgnoringAuto(resultMinZoom, compareIgnoringAuto(resultMaxZoom, resultZoom, min), max);
 
161
 
 
162
        if (resultWidth == ViewportArguments::ValueAuto && resultZoom == ViewportArguments::ValueAuto)
 
163
            resultWidth = deviceSize.width();
 
164
 
 
165
        if (resultWidth == ViewportArguments::ValueAuto && resultHeight == ViewportArguments::ValueAuto)
 
166
            resultWidth = deviceSize.width() / resultZoom;
 
167
 
 
168
        if (resultWidth == ViewportArguments::ValueAuto)
 
169
            resultWidth = resultHeight * deviceSize.width() / deviceSize.height();
 
170
 
 
171
        if (resultHeight == ViewportArguments::ValueAuto)
 
172
            resultHeight = resultWidth * deviceSize.height() / deviceSize.width();
 
173
 
 
174
        if (resultZoom != ViewportArguments::ValueAuto || resultMaxZoom != ViewportArguments::ValueAuto) {
 
175
            resultWidth = compareIgnoringAuto(resultWidth, deviceSize.width() / compareIgnoringAuto(resultZoom, resultMaxZoom, min), max);
 
176
            resultHeight = compareIgnoringAuto(resultHeight, deviceSize.height() / compareIgnoringAuto(resultZoom, resultMaxZoom, min), max);
 
177
        }
 
178
 
 
179
        resultWidth = max<float>(1, resultWidth);
 
180
        resultHeight = max<float>(1, resultHeight);
 
181
    }
 
182
 
 
183
    if (type != ViewportArguments::CSSDeviceAdaptation && type != ViewportArguments::Implicit) {
 
184
        // Clamp values to a valid range, but not for @viewport since is
 
185
        // not mandated by the specification.
 
186
        resultWidth = clampLengthValue(resultWidth);
 
187
        resultHeight = clampLengthValue(resultHeight);
 
188
        resultZoom = clampScaleValue(resultZoom);
 
189
        resultMinZoom = clampScaleValue(resultMinZoom);
 
190
        resultMaxZoom = clampScaleValue(resultMaxZoom);
 
191
    }
 
192
 
 
193
    ViewportAttributes result;
 
194
    result.orientation = orientation;
 
195
 
 
196
    // Resolve minimum-scale and maximum-scale values according to spec.
 
197
    if (resultMinZoom == ViewportArguments::ValueAuto)
 
198
        result.minimumScale = float(0.25);
 
199
    else
 
200
        result.minimumScale = resultMinZoom;
 
201
 
 
202
    if (resultMaxZoom == ViewportArguments::ValueAuto) {
 
203
        result.maximumScale = float(5.0);
 
204
        result.minimumScale = min(float(5.0), result.minimumScale);
 
205
    } else
 
206
        result.maximumScale = resultMaxZoom;
 
207
    result.maximumScale = max(result.minimumScale, result.maximumScale);
 
208
 
 
209
    // Resolve initial-scale value.
 
210
    result.initialScale = resultZoom;
 
211
    if (resultZoom == ViewportArguments::ValueAuto) {
 
212
        result.initialScale = initialViewportSize.width() / defaultWidth;
 
213
        if (resultWidth != ViewportArguments::ValueAuto)
 
214
            result.initialScale = initialViewportSize.width() / resultWidth;
 
215
        if (resultHeight != ViewportArguments::ValueAuto) {
 
216
            // if 'auto', the initial-scale will be negative here and thus ignored.
 
217
            result.initialScale = max<float>(result.initialScale, initialViewportSize.height() / resultHeight);
 
218
        }
 
219
    }
 
220
 
 
221
    // Constrain initial-scale value to minimum-scale/maximum-scale range.
 
222
    result.initialScale = min(result.maximumScale, max(result.minimumScale, result.initialScale));
 
223
 
 
224
    // Resolve width value.
 
225
    if (resultWidth == ViewportArguments::ValueAuto) {
 
226
        if (resultZoom == ViewportArguments::ValueAuto)
 
227
            resultWidth = defaultWidth;
 
228
        else if (resultHeight != ViewportArguments::ValueAuto)
 
229
            resultWidth = resultHeight * (initialViewportSize.width() / initialViewportSize.height());
 
230
        else
 
231
            resultWidth = initialViewportSize.width() / result.initialScale;
 
232
    }
 
233
 
 
234
    // Resolve height value.
 
235
    if (resultHeight == ViewportArguments::ValueAuto)
 
236
        resultHeight = resultWidth * (initialViewportSize.height() / initialViewportSize.width());
 
237
 
 
238
    if (type == ViewportArguments::ViewportMeta) {
 
239
        // Extend width and height to fill the visual viewport for the resolved initial-scale.
 
240
        resultWidth = max<float>(resultWidth, initialViewportSize.width() / result.initialScale);
 
241
        resultHeight = max<float>(resultHeight, initialViewportSize.height() / result.initialScale);
 
242
    }
 
243
 
 
244
    result.layoutSize.setWidth(resultWidth);
 
245
    result.layoutSize.setHeight(resultHeight);
 
246
 
 
247
    // FIXME: This might affect some ports, but is the right thing to do.
 
248
    // Only set initialScale to a value if it was explicitly set.
 
249
    // if (resultZoom == ViewportArguments::ValueAuto)
 
250
    //    result.initialScale = ViewportArguments::ValueAuto;
 
251
 
 
252
    result.userScalable = resultUserZoom;
 
253
    result.orientation = orientation;
 
254
 
 
255
    return result;
 
256
}
 
257
 
 
258
static FloatSize convertToUserSpace(const FloatSize& deviceSize, float devicePixelRatio)
 
259
{
 
260
    FloatSize result = deviceSize;
 
261
    if (devicePixelRatio != 1)
 
262
        result.scale(1 / devicePixelRatio);
 
263
    return result;
 
264
}
 
265
 
 
266
ViewportAttributes computeViewportAttributes(ViewportArguments args, int desktopWidth, int deviceWidth, int deviceHeight, float devicePixelRatio, IntSize visibleViewport)
 
267
{
 
268
    FloatSize initialViewportSize = convertToUserSpace(visibleViewport, devicePixelRatio);
 
269
    FloatSize deviceSize = convertToUserSpace(FloatSize(deviceWidth, deviceHeight), devicePixelRatio);
 
270
 
 
271
    return args.resolve(initialViewportSize, deviceSize, desktopWidth);
 
272
}
 
273
 
 
274
float computeMinimumScaleFactorForContentContained(const ViewportAttributes& result, const IntSize& visibleViewport, const IntSize& contentsSize, float devicePixelRatio)
 
275
{
 
276
    FloatSize viewportSize = convertToUserSpace(visibleViewport, devicePixelRatio);
 
277
 
 
278
    return max<float>(result.minimumScale, max(viewportSize.width() / contentsSize.width(), viewportSize.height() / contentsSize.height()));
 
279
}
 
280
 
 
281
void restrictMinimumScaleFactorToViewportSize(ViewportAttributes& result, IntSize visibleViewport, float devicePixelRatio)
 
282
{
 
283
    FloatSize viewportSize = convertToUserSpace(visibleViewport, devicePixelRatio);
 
284
 
 
285
    result.minimumScale = max<float>(result.minimumScale, max(viewportSize.width() / result.layoutSize.width(), viewportSize.height() / result.layoutSize.height()));
 
286
}
 
287
 
 
288
void restrictScaleFactorToInitialScaleIfNotUserScalable(ViewportAttributes& result)
 
289
{
 
290
    if (!result.userScalable)
 
291
        result.maximumScale = result.minimumScale = result.initialScale;
 
292
}
 
293
 
 
294
static float numericPrefix(const String& keyString, const String& valueString, Document* document, bool* ok = 0)
 
295
{
 
296
    size_t parsedLength;
 
297
    float value;
 
298
    if (valueString.is8Bit())
 
299
        value = charactersToFloat(valueString.characters8(), valueString.length(), parsedLength);
 
300
    else
 
301
        value = charactersToFloat(valueString.characters16(), valueString.length(), parsedLength);
 
302
    if (!parsedLength) {
 
303
        reportViewportWarning(document, UnrecognizedViewportArgumentValueError, valueString, keyString);
 
304
        if (ok)
 
305
            *ok = false;
 
306
        return 0;
 
307
    }
 
308
    if (parsedLength < valueString.length())
 
309
        reportViewportWarning(document, TruncatedViewportArgumentValueError, valueString, keyString);
 
310
    if (ok)
 
311
        *ok = true;
 
312
    return value;
 
313
}
 
314
 
 
315
static float findSizeValue(const String& keyString, const String& valueString, Document* document)
 
316
{
 
317
    // 1) Non-negative number values are translated to px lengths.
 
318
    // 2) Negative number values are translated to auto.
 
319
    // 3) device-width and device-height are used as keywords.
 
320
    // 4) Other keywords and unknown values translate to 0.0.
 
321
 
 
322
    if (equalIgnoringCase(valueString, "device-width"))
 
323
        return ViewportArguments::ValueDeviceWidth;
 
324
    if (equalIgnoringCase(valueString, "device-height"))
 
325
        return ViewportArguments::ValueDeviceHeight;
 
326
 
 
327
    float value = numericPrefix(keyString, valueString, document);
 
328
 
 
329
    if (value < 0)
 
330
        return ViewportArguments::ValueAuto;
 
331
 
 
332
    return value;
 
333
}
 
334
 
 
335
static float findScaleValue(const String& keyString, const String& valueString, Document* document)
 
336
{
 
337
    // 1) Non-negative number values are translated to <number> values.
 
338
    // 2) Negative number values are translated to auto.
 
339
    // 3) yes is translated to 1.0.
 
340
    // 4) device-width and device-height are translated to 10.0.
 
341
    // 5) no and unknown values are translated to 0.0
 
342
 
 
343
    if (equalIgnoringCase(valueString, "yes"))
 
344
        return 1;
 
345
    if (equalIgnoringCase(valueString, "no"))
 
346
        return 0;
 
347
    if (equalIgnoringCase(valueString, "device-width"))
 
348
        return 10;
 
349
    if (equalIgnoringCase(valueString, "device-height"))
 
350
        return 10;
 
351
 
 
352
    float value = numericPrefix(keyString, valueString, document);
 
353
 
 
354
    if (value < 0)
 
355
        return ViewportArguments::ValueAuto;
 
356
 
 
357
    if (value > 10.0)
 
358
        reportViewportWarning(document, MaximumScaleTooLargeError, String(), String());
 
359
 
 
360
    return value;
 
361
}
 
362
 
 
363
static float findUserScalableValue(const String& keyString, const String& valueString, Document* document)
 
364
{
 
365
    // yes and no are used as keywords.
 
366
    // Numbers >= 1, numbers <= -1, device-width and device-height are mapped to yes.
 
367
    // Numbers in the range <-1, 1>, and unknown values, are mapped to no.
 
368
 
 
369
    if (equalIgnoringCase(valueString, "yes"))
 
370
        return 1;
 
371
    if (equalIgnoringCase(valueString, "no"))
 
372
        return 0;
 
373
    if (equalIgnoringCase(valueString, "device-width"))
 
374
        return 1;
 
375
    if (equalIgnoringCase(valueString, "device-height"))
 
376
        return 1;
 
377
 
 
378
    float value = numericPrefix(keyString, valueString, document);
 
379
 
 
380
    if (fabs(value) < 1)
 
381
        return 0;
 
382
 
 
383
    return 1;
 
384
}
 
385
 
 
386
void setViewportFeature(const String& keyString, const String& valueString, Document* document, void* data)
 
387
{
 
388
    ViewportArguments* arguments = static_cast<ViewportArguments*>(data);
 
389
 
 
390
    if (keyString == "width")
 
391
        arguments->width = findSizeValue(keyString, valueString, document);
 
392
    else if (keyString == "height")
 
393
        arguments->height = findSizeValue(keyString, valueString, document);
 
394
    else if (keyString == "initial-scale")
 
395
        arguments->zoom = findScaleValue(keyString, valueString, document);
 
396
    else if (keyString == "minimum-scale")
 
397
        arguments->minZoom = findScaleValue(keyString, valueString, document);
 
398
    else if (keyString == "maximum-scale")
 
399
        arguments->maxZoom = findScaleValue(keyString, valueString, document);
 
400
    else if (keyString == "user-scalable")
 
401
        arguments->userZoom = findUserScalableValue(keyString, valueString, document);
 
402
    else if (keyString == "target-densitydpi")
 
403
        reportViewportWarning(document, TargetDensityDpiUnsupported, String(), String());
 
404
    else
 
405
        reportViewportWarning(document, UnrecognizedViewportArgumentKeyError, keyString, String());
 
406
}
 
407
 
 
408
static const char* viewportErrorMessageTemplate(ViewportErrorCode errorCode)
 
409
{
 
410
    static const char* const errors[] = {
 
411
        "Viewport argument key \"%replacement1\" not recognized and ignored.",
 
412
        "Viewport argument value \"%replacement1\" for key \"%replacement2\" not recognized. Content ignored.",
 
413
        "Viewport argument value \"%replacement1\" for key \"%replacement2\" was truncated to its numeric prefix.",
 
414
        "Viewport maximum-scale cannot be larger than 10.0. The maximum-scale will be set to 10.0.",
 
415
        "Viewport target-densitydpi is not supported.",
 
416
    };
 
417
 
 
418
    return errors[errorCode];
 
419
}
 
420
 
 
421
static MessageLevel viewportErrorMessageLevel(ViewportErrorCode errorCode)
 
422
{
 
423
    switch (errorCode) {
 
424
    case TruncatedViewportArgumentValueError:
 
425
    case TargetDensityDpiUnsupported:
 
426
        return TipMessageLevel;
 
427
    case UnrecognizedViewportArgumentKeyError:
 
428
    case UnrecognizedViewportArgumentValueError:
 
429
    case MaximumScaleTooLargeError:
 
430
        return ErrorMessageLevel;
 
431
    }
 
432
 
 
433
    ASSERT_NOT_REACHED();
 
434
    return ErrorMessageLevel;
 
435
}
 
436
 
 
437
// FIXME: Why is this different from SVGDocumentExtensions parserLineNumber?
 
438
// FIXME: Callers should probably use ScriptController::eventHandlerLineNumber()
 
439
static int parserLineNumber(Document* document)
 
440
{
 
441
    if (!document)
 
442
        return 0;
 
443
    ScriptableDocumentParser* parser = document->scriptableDocumentParser();
 
444
    if (!parser)
 
445
        return 0;
 
446
    return parser->lineNumber().oneBasedInt();
 
447
}
 
448
 
 
449
void reportViewportWarning(Document* document, ViewportErrorCode errorCode, const String& replacement1, const String& replacement2)
 
450
{
 
451
    Frame* frame = document->frame();
 
452
    if (!frame)
 
453
        return;
 
454
 
 
455
    String message = viewportErrorMessageTemplate(errorCode);
 
456
    if (!replacement1.isNull())
 
457
        message.replace("%replacement1", replacement1);
 
458
    if (!replacement2.isNull())
 
459
        message.replace("%replacement2", replacement2);
 
460
 
 
461
    if ((errorCode == UnrecognizedViewportArgumentValueError || errorCode == TruncatedViewportArgumentValueError) && replacement1.find(';') != WTF::notFound)
 
462
        message.append(" Note that ';' is not a separator in viewport values. The list should be comma-separated.");
 
463
 
 
464
    document->domWindow()->console()->addMessage(HTMLMessageSource, LogMessageType, viewportErrorMessageLevel(errorCode), message, document->url().string(), parserLineNumber(document));
 
465
}
 
466
 
 
467
} // namespace WebCore