blob: 4480d7f47ed72473c1df70a02e517c538613d8a5 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
Ben Murdoch7757ec22013-07-23 11:17:36 +01002 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include "config.h"
Torne (Richard Coles)81a51572013-05-13 16:52:28 +010031#include "core/css/resolver/ViewportStyleResolver.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010032
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033#include "CSSValueKeywords.h"
34#include "core/css/StylePropertySet.h"
35#include "core/css/StyleRule.h"
36#include "core/dom/Document.h"
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010037#include "core/dom/NodeRenderStyle.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010038#include "core/dom/ViewportArguments.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010039
40namespace WebCore {
41
42ViewportStyleResolver::ViewportStyleResolver(Document* document)
43 : m_document(document)
44{
45 ASSERT(m_document);
46}
47
48ViewportStyleResolver::~ViewportStyleResolver()
49{
50}
51
52void ViewportStyleResolver::addViewportRule(StyleRuleViewport* viewportRule)
53{
54 StylePropertySet* propertySet = viewportRule->mutableProperties();
55
56 unsigned propertyCount = propertySet->propertyCount();
57 if (!propertyCount)
58 return;
59
60 if (!m_propertySet) {
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010061 m_propertySet = propertySet->mutableCopy();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010062 return;
63 }
64
65 // We cannot use mergeAndOverrideOnConflict() here because it doesn't
66 // respect the !important declaration (but addParsedProperty() does).
67 for (unsigned i = 0; i < propertyCount; ++i)
68 m_propertySet->addParsedProperty(propertySet->propertyAt(i).toCSSProperty());
69}
70
71void ViewportStyleResolver::clearDocument()
72{
73 m_document = 0;
74}
75
76void ViewportStyleResolver::resolve()
77{
78 if (!m_document || !m_propertySet)
79 return;
80
81 ViewportArguments arguments(ViewportArguments::CSSDeviceAdaptation);
82
83 arguments.userZoom = getViewportArgumentValue(CSSPropertyUserZoom);
84 arguments.zoom = getViewportArgumentValue(CSSPropertyZoom);
85 arguments.minZoom = getViewportArgumentValue(CSSPropertyMinZoom);
86 arguments.maxZoom = getViewportArgumentValue(CSSPropertyMaxZoom);
87 arguments.minWidth = getViewportArgumentValue(CSSPropertyMinWidth);
88 arguments.maxWidth = getViewportArgumentValue(CSSPropertyMaxWidth);
89 arguments.minHeight = getViewportArgumentValue(CSSPropertyMinHeight);
90 arguments.maxHeight = getViewportArgumentValue(CSSPropertyMaxHeight);
91 arguments.orientation = getViewportArgumentValue(CSSPropertyOrientation);
92
93 m_document->setViewportArguments(arguments);
94 m_document->updateViewportArguments();
95
96 m_propertySet = 0;
97}
98
99float ViewportStyleResolver::getViewportArgumentValue(CSSPropertyID id) const
100{
101 float defaultValue = ViewportArguments::ValueAuto;
102
103 // UserZoom default value is CSSValueZoom, which maps to true, meaning that
104 // yes, it is user scalable. When the value is set to CSSValueFixed, we
105 // return false.
106 if (id == CSSPropertyUserZoom)
107 defaultValue = 1;
108
109 RefPtr<CSSValue> value = m_propertySet->getPropertyCSSValue(id);
110 if (!value || !value->isPrimitiveValue())
111 return defaultValue;
112
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100113 CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value.get());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100114
115 if (primitiveValue->isNumber() || primitiveValue->isPx())
116 return primitiveValue->getFloatValue();
117
118 if (primitiveValue->isFontRelativeLength())
Ben Murdoche69819b2013-07-17 14:56:49 +0100119 return primitiveValue->getFloatValue() * m_document->renderStyle()->fontDescription().computedSize();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100120
121 if (primitiveValue->isPercentage()) {
122 float percentValue = primitiveValue->getFloatValue() / 100.0f;
123 switch (id) {
124 case CSSPropertyMaxHeight:
125 case CSSPropertyMinHeight:
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100126 return percentValue * m_document->initialViewportSize().height();
127 case CSSPropertyMaxWidth:
128 case CSSPropertyMinWidth:
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100129 return percentValue * m_document->initialViewportSize().width();
130 case CSSPropertyMaxZoom:
131 case CSSPropertyMinZoom:
132 case CSSPropertyZoom:
133 return percentValue;
134 default:
135 ASSERT_NOT_REACHED();
136 break;
137 }
138 }
139
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100140 switch (primitiveValue->getValueID()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100141 case CSSValueAuto:
142 return defaultValue;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100143 case CSSValueLandscape:
144 return ViewportArguments::ValueLandscape;
145 case CSSValuePortrait:
146 return ViewportArguments::ValuePortrait;
147 case CSSValueZoom:
148 return defaultValue;
Ben Murdoch7757ec22013-07-23 11:17:36 +0100149 case CSSValueInternalExtendToZoom:
150 return ViewportArguments::ValueExtendToZoom;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100151 case CSSValueFixed:
152 return 0;
153 default:
154 return defaultValue;
155 }
156}
157
158} // namespace WebCore