OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/css/MediaValuesCached.h" |
| 7 |
| 8 #include "core/css/CSSHelper.h" |
| 9 #include "core/css/CSSPrimitiveValue.h" |
| 10 #include "core/dom/Document.h" |
| 11 #include "core/rendering/RenderObject.h" |
| 12 |
| 13 namespace WebCore { |
| 14 |
| 15 PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesInitializer& initia
lizer) |
| 16 { |
| 17 return adoptRef(new MediaValuesCached(initializer)); |
| 18 } |
| 19 |
| 20 PassRefPtr<MediaValues> MediaValuesCached::create(Document& document) |
| 21 { |
| 22 Document* executingDocument = getExecutingDocument(document); |
| 23 return MediaValuesCached::create(executingDocument->frame(), executingDocume
nt->renderer()->style()); |
| 24 } |
| 25 |
| 26 PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame, RenderStyle
* style) |
| 27 { |
| 28 return adoptRef(new MediaValuesCached(frame, style)); |
| 29 } |
| 30 |
| 31 MediaValuesCached::MediaValuesCached(LocalFrame* frame, RenderStyle* style) |
| 32 { |
| 33 ASSERT(frame && style); |
| 34 m_viewportWidth = calculateViewportWidth(frame, style); |
| 35 m_viewportHeight = calculateViewportHeight(frame, style); |
| 36 m_deviceWidth = calculateDeviceWidth(frame); |
| 37 m_deviceHeight = calculateDeviceHeight(frame); |
| 38 m_devicePixelRatio = calculateDevicePixelRatio(frame); |
| 39 m_colorBitsPerComponent = calculateColorBitsPerComponent(frame); |
| 40 m_monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(frame); |
| 41 m_pointer = calculateLeastCapablePrimaryPointerDeviceType(frame); |
| 42 m_defaultFontSize = calculateDefaultFontSize(style); |
| 43 m_computedFontSize = calculateComputedFontSize(style); |
| 44 m_hasXHeight = calculateHasXHeight(style); |
| 45 m_xHeight = calculateXHeight(style); |
| 46 m_zeroWidth = calculateZeroWidth(style); |
| 47 m_threeDEnabled = calculateThreeDEnabled(frame); |
| 48 m_scanMediaType = calculateScanMediaType(frame); |
| 49 m_screenMediaType = calculateScreenMediaType(frame); |
| 50 m_printMediaType = calculatePrintMediaType(frame); |
| 51 m_strictMode = calculateStrictMode(frame); |
| 52 } |
| 53 |
| 54 MediaValuesCached::MediaValuesCached(MediaValuesInitializer& initializer) |
| 55 : m_viewportWidth(initializer.viewportWidth) |
| 56 , m_viewportHeight(initializer.viewportHeight) |
| 57 , m_deviceWidth(initializer.deviceWidth) |
| 58 , m_deviceHeight(initializer.deviceHeight) |
| 59 , m_devicePixelRatio(initializer.devicePixelRatio) |
| 60 , m_colorBitsPerComponent(initializer.colorBitsPerComponent) |
| 61 , m_monochromeBitsPerComponent(initializer.monochromeBitsPerComponent) |
| 62 , m_pointer(initializer.pointer) |
| 63 , m_defaultFontSize(initializer.defaultFontSize) |
| 64 , m_computedFontSize(initializer.computedFontSize) |
| 65 , m_hasXHeight(initializer.hasXHeight) |
| 66 , m_xHeight(initializer.xHeight) |
| 67 , m_zeroWidth(initializer.zeroWidth) |
| 68 , m_threeDEnabled(initializer.threeDEnabled) |
| 69 , m_scanMediaType(initializer.scanMediaType) |
| 70 , m_screenMediaType(initializer.screenMediaType) |
| 71 , m_printMediaType(initializer.printMediaType) |
| 72 , m_strictMode(initializer.strictMode) |
| 73 { |
| 74 } |
| 75 |
| 76 PassRefPtr<MediaValues> MediaValuesCached::copy() const |
| 77 { |
| 78 MediaValuesInitializer initializer; |
| 79 initializer.viewportWidth = m_viewportWidth; |
| 80 initializer.viewportHeight = m_viewportHeight; |
| 81 initializer.deviceWidth = m_deviceWidth; |
| 82 initializer.deviceHeight = m_deviceHeight; |
| 83 initializer.devicePixelRatio = m_devicePixelRatio; |
| 84 initializer.colorBitsPerComponent = m_colorBitsPerComponent; |
| 85 initializer.monochromeBitsPerComponent = m_monochromeBitsPerComponent; |
| 86 initializer.pointer = m_pointer; |
| 87 initializer.defaultFontSize = m_defaultFontSize; |
| 88 initializer.computedFontSize = m_computedFontSize; |
| 89 initializer.hasXHeight = m_hasXHeight; |
| 90 initializer.xHeight = m_xHeight; |
| 91 initializer.zeroWidth = m_zeroWidth; |
| 92 initializer.threeDEnabled = m_threeDEnabled; |
| 93 initializer.scanMediaType = m_scanMediaType; |
| 94 initializer.screenMediaType = m_screenMediaType; |
| 95 initializer.printMediaType = m_printMediaType; |
| 96 initializer.strictMode = m_strictMode; |
| 97 |
| 98 return adoptRef(new MediaValuesCached(initializer)); |
| 99 } |
| 100 |
| 101 bool MediaValuesCached::computeLength(double value, unsigned short type, int& re
sult) const |
| 102 { |
| 103 // We're running in a background thread, so RenderStyle is not available. |
| 104 // Nevertheless, we can evaluate lengths using cached values. |
| 105 // The logic here is duplicated from CSSPrimitiveValue::computeLengthDouble
for performance reasons. |
| 106 |
| 107 // FIXME - Unite the logic here with CSSPrimitiveValue is a performant way. |
| 108 int factor = 0; |
| 109 switch (type) { |
| 110 case CSSPrimitiveValue::CSS_EMS: |
| 111 case CSSPrimitiveValue::CSS_REMS: |
| 112 factor = m_defaultFontSize; |
| 113 break; |
| 114 case CSSPrimitiveValue::CSS_PX: |
| 115 factor = 1; |
| 116 break; |
| 117 case CSSPrimitiveValue::CSS_EXS: |
| 118 // FIXME: We have a bug right now where the zoom will be applied twice t
o EX units. |
| 119 // We really need to compute EX using fontMetrics for the original speci
fiedSize and not use |
| 120 // our actual constructed rendering font. |
| 121 if (m_hasXHeight) |
| 122 factor = m_xHeight; |
| 123 else |
| 124 factor = m_defaultFontSize / 2.0; |
| 125 break; |
| 126 case CSSPrimitiveValue::CSS_CHS: |
| 127 factor = m_zeroWidth; |
| 128 break; |
| 129 case CSSPrimitiveValue::CSS_VW: |
| 130 factor = m_viewportWidth / 100; |
| 131 break; |
| 132 case CSSPrimitiveValue::CSS_VH: |
| 133 factor = m_viewportHeight / 100; |
| 134 break; |
| 135 case CSSPrimitiveValue::CSS_VMIN: |
| 136 factor = std::min(m_viewportWidth, m_viewportHeight) / 100; |
| 137 break; |
| 138 case CSSPrimitiveValue::CSS_VMAX: |
| 139 factor = std::max(m_viewportWidth, m_viewportHeight) / 100; |
| 140 break; |
| 141 case CSSPrimitiveValue::CSS_CM: |
| 142 factor = cssPixelsPerCentimeter; |
| 143 break; |
| 144 case CSSPrimitiveValue::CSS_MM: |
| 145 factor = cssPixelsPerMillimeter; |
| 146 break; |
| 147 case CSSPrimitiveValue::CSS_IN: |
| 148 factor = cssPixelsPerInch; |
| 149 break; |
| 150 case CSSPrimitiveValue::CSS_PT: |
| 151 factor = cssPixelsPerPoint; |
| 152 break; |
| 153 case CSSPrimitiveValue::CSS_PC: |
| 154 factor = cssPixelsPerPica; |
| 155 break; |
| 156 default: |
| 157 return false; |
| 158 } |
| 159 |
| 160 ASSERT(factor > 0); |
| 161 result = roundForImpreciseConversion<int>(value*factor); |
| 162 return true; |
| 163 } |
| 164 |
| 165 bool MediaValuesCached::isSafeToSendToAnotherThread() const |
| 166 { |
| 167 return hasOneRef(); |
| 168 } |
| 169 |
| 170 int MediaValuesCached::viewportWidth() const |
| 171 { |
| 172 return m_viewportWidth; |
| 173 } |
| 174 |
| 175 int MediaValuesCached::viewportHeight() const |
| 176 { |
| 177 return m_viewportHeight; |
| 178 } |
| 179 |
| 180 int MediaValuesCached::deviceWidth() const |
| 181 { |
| 182 return m_deviceWidth; |
| 183 } |
| 184 |
| 185 int MediaValuesCached::deviceHeight() const |
| 186 { |
| 187 return m_deviceHeight; |
| 188 } |
| 189 |
| 190 float MediaValuesCached::devicePixelRatio() const |
| 191 { |
| 192 return m_devicePixelRatio; |
| 193 } |
| 194 |
| 195 int MediaValuesCached::colorBitsPerComponent() const |
| 196 { |
| 197 return m_colorBitsPerComponent; |
| 198 } |
| 199 |
| 200 int MediaValuesCached::monochromeBitsPerComponent() const |
| 201 { |
| 202 return m_monochromeBitsPerComponent; |
| 203 } |
| 204 |
| 205 MediaValues::PointerDeviceType MediaValuesCached::pointer() const |
| 206 { |
| 207 return m_pointer; |
| 208 } |
| 209 |
| 210 bool MediaValuesCached::threeDEnabled() const |
| 211 { |
| 212 return m_threeDEnabled; |
| 213 } |
| 214 |
| 215 bool MediaValuesCached::scanMediaType() const |
| 216 { |
| 217 return m_scanMediaType; |
| 218 } |
| 219 |
| 220 bool MediaValuesCached::screenMediaType() const |
| 221 { |
| 222 return m_screenMediaType; |
| 223 } |
| 224 |
| 225 bool MediaValuesCached::printMediaType() const |
| 226 { |
| 227 return m_printMediaType; |
| 228 } |
| 229 |
| 230 bool MediaValuesCached::strictMode() const |
| 231 { |
| 232 return m_strictMode; |
| 233 } |
| 234 |
| 235 Document* MediaValuesCached::document() const |
| 236 { |
| 237 return 0; |
| 238 } |
| 239 |
| 240 bool MediaValuesCached::hasValues() const |
| 241 { |
| 242 return true; |
| 243 } |
| 244 |
| 245 } // namespace |
OLD | NEW |