Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 | 59 |
| 60 WebInspector.OverridesSupport.Events = { | 60 WebInspector.OverridesSupport.Events = { |
| 61 OverridesWarningUpdated: "OverridesWarningUpdated", | 61 OverridesWarningUpdated: "OverridesWarningUpdated", |
| 62 } | 62 } |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * @constructor | 65 * @constructor |
| 66 * @param {number} width | 66 * @param {number} width |
| 67 * @param {number} height | 67 * @param {number} height |
| 68 * @param {number} deviceScaleFactor | 68 * @param {number} deviceScaleFactor |
| 69 * @param {number} fontScaleFactor | |
| 70 * @param {boolean} textAutosizing | 69 * @param {boolean} textAutosizing |
| 70 * @param {boolean} useAndroidFontMetrics | |
| 71 */ | 71 */ |
| 72 WebInspector.OverridesSupport.DeviceMetrics = function(width, height, deviceScal eFactor, fontScaleFactor, textAutosizing) | 72 WebInspector.OverridesSupport.DeviceMetrics = function(width, height, deviceScal eFactor, textAutosizing, useAndroidFontMetrics) |
| 73 { | 73 { |
| 74 this.width = width; | 74 this.width = width; |
| 75 this.height = height; | 75 this.height = height; |
| 76 this.deviceScaleFactor = deviceScaleFactor; | 76 this.deviceScaleFactor = deviceScaleFactor; |
| 77 this.fontScaleFactor = fontScaleFactor; | |
| 78 this.textAutosizing = textAutosizing; | 77 this.textAutosizing = textAutosizing; |
| 78 this.useAndroidFontMetrics = useAndroidFontMetrics; | |
| 79 } | 79 } |
| 80 | 80 |
| 81 /** | 81 /** |
| 82 * Android computes a font scale factor fudged for improved legibility. This val ue is | |
| 83 * not computed on non-Android builds so we duplicate the Android-specific logic here. | |
| 84 * For a description of this algorithm, see: | |
| 85 * chrome/browser/chrome_content_browser_client.cc, GetFontScaleMultiplier(. ..) | |
| 86 * | |
| 87 * @param {number} width | |
| 88 * @param {number} height | |
| 89 * @param {number} deviceScaleFactor | |
| 90 * @return {number} fontScaleFactor for Android. | |
| 91 */ | |
| 92 // FIXME(crbug.com/313410): Unify this calculation with the c++ side so we don't duplicate this logic. | |
| 93 WebInspector.OverridesSupport.DeviceMetrics._computeFontScaleFactorForAndroid = function(width, height, deviceScaleFactor) | |
| 94 { | |
| 95 var minWidth = Math.min(width, height) / deviceScaleFactor; | |
| 96 | |
| 97 var kMinFSM = 1.05; | |
| 98 var kWidthForMinFSM = 320; | |
| 99 var kMaxFSM = 1.3; | |
| 100 var kWidthForMaxFSM = 800; | |
| 101 | |
| 102 if (minWidth <= kWidthForMinFSM) | |
| 103 return kMinFSM; | |
| 104 if (minWidth >= kWidthForMaxFSM) | |
| 105 return kMaxFSM; | |
| 106 | |
| 107 // The font scale multiplier varies linearly between kMinFSM and kMaxFSM. | |
| 108 var ratio = (minWidth - kWidthForMinFSM) / (kWidthForMaxFSM - kWidthForMinFS M); | |
| 109 var fontScaleFactor = ratio * (kMaxFSM - kMinFSM) + kMinFSM; | |
| 110 return Math.round(fontScaleFactor * 1000) / 1000; | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * @return {WebInspector.OverridesSupport.DeviceMetrics} | 82 * @return {WebInspector.OverridesSupport.DeviceMetrics} |
| 115 */ | 83 */ |
| 116 WebInspector.OverridesSupport.DeviceMetrics.parseSetting = function(value) | 84 WebInspector.OverridesSupport.DeviceMetrics.parseSetting = function(value) |
| 117 { | 85 { |
| 118 var width = 0; | 86 var width = 0; |
| 119 var height = 0; | 87 var height = 0; |
| 120 var deviceScaleFactor = 1; | 88 var deviceScaleFactor = 1; |
| 121 var fontScaleFactor = 1; | |
| 122 var textAutosizing = false; | 89 var textAutosizing = false; |
| 90 var useAndroidFontMetrics = false; | |
| 123 if (value) { | 91 if (value) { |
| 124 var splitMetrics = value.split("x"); | 92 var splitMetrics = value.split("x"); |
| 125 if (splitMetrics.length === 5) { | 93 if (splitMetrics.length === 5) { |
| 126 width = parseInt(splitMetrics[0], 10); | 94 width = parseInt(splitMetrics[0], 10); |
| 127 height = parseInt(splitMetrics[1], 10); | 95 height = parseInt(splitMetrics[1], 10); |
| 128 deviceScaleFactor = parseFloat(splitMetrics[2]); | 96 deviceScaleFactor = parseFloat(splitMetrics[2]); |
| 129 fontScaleFactor = parseFloat(splitMetrics[3]); | 97 useAndroidFontMetrics = splitMetrics[3] == 1; |
| 130 if (fontScaleFactor == 0) | |
| 131 fontScaleFactor = WebInspector.OverridesSupport.DeviceMetrics._c omputeFontScaleFactorForAndroid(width, height, deviceScaleFactor); | |
| 132 textAutosizing = splitMetrics[4] == 1; | 98 textAutosizing = splitMetrics[4] == 1; |
| 133 } | 99 } |
| 134 } | 100 } |
| 135 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device ScaleFactor, fontScaleFactor, textAutosizing); | 101 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device ScaleFactor, textAutosizing, useAndroidFontMetrics); |
| 136 } | 102 } |
| 137 | 103 |
| 138 /** | 104 /** |
| 139 * @return {?WebInspector.OverridesSupport.DeviceMetrics} | 105 * @return {?WebInspector.OverridesSupport.DeviceMetrics} |
| 140 */ | 106 */ |
| 141 WebInspector.OverridesSupport.DeviceMetrics.parseUserInput = function(widthStrin g, heightString, deviceScaleFactorString, fontScaleFactorString, textAutosizing) | 107 WebInspector.OverridesSupport.DeviceMetrics.parseUserInput = function(widthStrin g, heightString, deviceScaleFactorString, textAutosizing, useAndroidFontMetrics) |
| 142 { | 108 { |
| 143 function isUserInputValid(value, isInteger) | 109 function isUserInputValid(value, isInteger) |
| 144 { | 110 { |
| 145 if (!value) | 111 if (!value) |
| 146 return true; | 112 return true; |
| 147 return isInteger ? /^[0]*[1-9][\d]*$/.test(value) : /^[0]*([1-9][\d]*(\. \d+)?|\.\d+)$/.test(value); | 113 return isInteger ? /^[0]*[1-9][\d]*$/.test(value) : /^[0]*([1-9][\d]*(\. \d+)?|\.\d+)$/.test(value); |
| 148 } | 114 } |
| 149 | 115 |
| 150 if (!widthString ^ !heightString) | 116 if (!widthString ^ !heightString) |
| 151 return null; | 117 return null; |
| 152 | 118 |
| 153 var isWidthValid = isUserInputValid(widthString, true); | 119 var isWidthValid = isUserInputValid(widthString, true); |
| 154 var isHeightValid = isUserInputValid(heightString, true); | 120 var isHeightValid = isUserInputValid(heightString, true); |
| 155 var isDeviceScaleFactorValid = isUserInputValid(deviceScaleFactorString, fal se); | 121 var isDeviceScaleFactorValid = isUserInputValid(deviceScaleFactorString, fal se); |
| 156 var isFontScaleFactorValid = isUserInputValid(fontScaleFactorString, false); | |
| 157 | 122 |
| 158 if (!isWidthValid && !isHeightValid && !isDeviceScaleFactorValid && !isFontS caleFactorValid) | 123 if (!isWidthValid && !isHeightValid && !isDeviceScaleFactorValid) |
| 159 return null; | 124 return null; |
| 160 | 125 |
| 161 var width = isWidthValid ? parseInt(widthString || "0", 10) : -1; | 126 var width = isWidthValid ? parseInt(widthString || "0", 10) : -1; |
| 162 var height = isHeightValid ? parseInt(heightString || "0", 10) : -1; | 127 var height = isHeightValid ? parseInt(heightString || "0", 10) : -1; |
| 163 var deviceScaleFactor = isDeviceScaleFactorValid ? parseFloat(deviceScaleFac torString) : -1; | 128 var deviceScaleFactor = isDeviceScaleFactorValid ? parseFloat(deviceScaleFac torString) : -1; |
| 164 var fontScaleFactor = isFontScaleFactorValid ? parseFloat(fontScaleFactorStr ing) : -1; | |
| 165 | 129 |
| 166 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device ScaleFactor, fontScaleFactor, textAutosizing); | 130 return new WebInspector.OverridesSupport.DeviceMetrics(width, height, device ScaleFactor, textAutosizing, useAndroidFontMetrics); |
| 167 } | 131 } |
| 168 | 132 |
| 169 WebInspector.OverridesSupport.DeviceMetrics.prototype = { | 133 WebInspector.OverridesSupport.DeviceMetrics.prototype = { |
| 170 /** | 134 /** |
| 171 * @return {boolean} | 135 * @return {boolean} |
| 172 */ | 136 */ |
| 173 isValid: function() | 137 isValid: function() |
| 174 { | 138 { |
| 175 return this.isWidthValid() && this.isHeightValid() && this.isDeviceScale FactorValid() && this.isFontScaleFactorValid(); | 139 return this.isWidthValid() && this.isHeightValid() && this.isDeviceScale FactorValid(); |
| 176 }, | 140 }, |
| 177 | 141 |
| 178 /** | 142 /** |
| 179 * @return {boolean} | 143 * @return {boolean} |
| 180 */ | 144 */ |
| 181 isWidthValid: function() | 145 isWidthValid: function() |
| 182 { | 146 { |
| 183 return this.width >= 0; | 147 return this.width >= 0; |
| 184 }, | 148 }, |
| 185 | 149 |
| 186 /** | 150 /** |
| 187 * @return {boolean} | 151 * @return {boolean} |
| 188 */ | 152 */ |
| 189 isHeightValid: function() | 153 isHeightValid: function() |
| 190 { | 154 { |
| 191 return this.height >= 0; | 155 return this.height >= 0; |
| 192 }, | 156 }, |
| 193 | 157 |
| 194 /** | 158 /** |
| 195 * @return {boolean} | 159 * @return {boolean} |
| 196 */ | 160 */ |
| 197 isDeviceScaleFactorValid: function() | 161 isDeviceScaleFactorValid: function() |
| 198 { | 162 { |
| 199 return this.deviceScaleFactor > 0; | 163 return this.deviceScaleFactor > 0; |
| 200 }, | 164 }, |
| 201 | 165 |
| 202 /** | 166 /** |
| 203 * @return {boolean} | |
| 204 */ | |
| 205 isFontScaleFactorValid: function() | |
| 206 { | |
| 207 return this.fontScaleFactor > 0; | |
| 208 }, | |
| 209 | |
| 210 /** | |
| 211 * @return {boolean} | |
| 212 */ | |
| 213 isTextAutosizingValid: function() | |
| 214 { | |
| 215 return true; | |
| 216 }, | |
| 217 | |
| 218 /** | |
| 219 * @return {string} | 167 * @return {string} |
| 220 */ | 168 */ |
| 221 toSetting: function() | 169 toSetting: function() |
| 222 { | 170 { |
| 223 if (!this.isValid()) | 171 if (!this.isValid()) |
| 224 return ""; | 172 return ""; |
| 225 | 173 |
| 226 return this.width && this.height ? this.width + "x" + this.height + "x" + this.deviceScaleFactor + "x" + this.fontScaleFactor + "x" + (this.textAutosizi ng ? "1" : "0") : ""; | 174 return this.width && this.height ? this.width + "x" + this.height + "x" + this.deviceScaleFactor + "x" + (this.useAndroidFontMetrics ? "1" : "0") + "x" + (this.textAutosizing ? "1" : "0") : ""; |
| 227 }, | 175 }, |
| 228 | 176 |
| 229 /** | 177 /** |
| 230 * @return {string} | 178 * @return {string} |
| 231 */ | 179 */ |
| 232 widthToInput: function() | 180 widthToInput: function() |
| 233 { | 181 { |
| 234 return this.isWidthValid() && this.width ? String(this.width) : ""; | 182 return this.isWidthValid() && this.width ? String(this.width) : ""; |
| 235 }, | 183 }, |
| 236 | 184 |
| 237 /** | 185 /** |
| 238 * @return {string} | 186 * @return {string} |
| 239 */ | 187 */ |
| 240 heightToInput: function() | 188 heightToInput: function() |
| 241 { | 189 { |
| 242 return this.isHeightValid() && this.height ? String(this.height) : ""; | 190 return this.isHeightValid() && this.height ? String(this.height) : ""; |
| 243 }, | 191 }, |
| 244 | 192 |
| 245 /** | 193 /** |
| 246 * @return {string} | 194 * @return {string} |
| 247 */ | 195 */ |
| 248 deviceScaleFactorToInput: function() | 196 deviceScaleFactorToInput: function() |
| 249 { | 197 { |
| 250 return this.isDeviceScaleFactorValid() && this.deviceScaleFactor ? Strin g(this.deviceScaleFactor) : ""; | 198 return this.isDeviceScaleFactorValid() && this.deviceScaleFactor ? Strin g(this.deviceScaleFactor) : ""; |
| 251 }, | 199 }, |
| 252 | 200 |
| 253 /** | 201 /** |
| 254 * @return {string} | 202 * Compute the font scale factor. |
| 203 * | |
| 204 * Android uses a font scale factor for text autosizing that is fudged for i mproved | |
|
skobes
2013/11/15 19:30:40
Note that:
(1) I renamed the fudge factor to "dev
pdr.
2013/11/15 23:13:16
I'll update this wording in the comment. The renam
skobes
2013/11/15 23:26:03
Are you sure? It looks like the inspector hooks i
| |
| 205 * legibility. This function computes this fudged value if useAndroidFontMet rics is true. | |
| 206 * | |
| 207 * For a description of the Android font scale factor algorithm, see: | |
| 208 * chrome/browser/chrome_content_browser_client.cc, GetFontScaleMultipli er(...) | |
| 209 * | |
| 210 * @return {number} font scale factor for Android if useAndroidFontMetrics, or 1. | |
| 255 */ | 211 */ |
| 256 fontScaleFactorToInput: function() | 212 fontScaleFactor: function() |
| 257 { | 213 { |
| 258 return this.isFontScaleFactorValid() && this.fontScaleFactor ? String(th is.fontScaleFactor) : ""; | 214 if (this.useAndroidFontMetrics && this.isValid()) { |
| 215 var minWidth = Math.min(this.width, this.height) / this.deviceScaleF actor; | |
| 216 | |
| 217 var kMinFSM = 1.05; | |
| 218 var kWidthForMinFSM = 320; | |
| 219 var kMaxFSM = 1.3; | |
| 220 var kWidthForMaxFSM = 800; | |
| 221 | |
| 222 if (minWidth <= kWidthForMinFSM) | |
| 223 return kMinFSM; | |
| 224 if (minWidth >= kWidthForMaxFSM) | |
| 225 return kMaxFSM; | |
| 226 | |
| 227 // The font scale multiplier varies linearly between kMinFSM and kMa xFSM. | |
| 228 var ratio = (minWidth - kWidthForMinFSM) / (kWidthForMaxFSM - kWidth ForMinFSM); | |
| 229 | |
| 230 return ratio * (kMaxFSM - kMinFSM) + kMinFSM; | |
| 231 } | |
| 232 | |
| 233 return 1; | |
| 259 } | 234 } |
| 260 } | 235 } |
| 261 | 236 |
| 262 /** | 237 /** |
| 263 * @constructor | 238 * @constructor |
| 264 * @param {number} latitude | 239 * @param {number} latitude |
| 265 * @param {number} longitude | 240 * @param {number} longitude |
| 266 */ | 241 */ |
| 267 WebInspector.OverridesSupport.GeolocationPosition = function(latitude, longitude , error) | 242 WebInspector.OverridesSupport.GeolocationPosition = function(latitude, longitude , error) |
| 268 { | 243 { |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 461 | 436 |
| 462 var dipWidth = Math.round(metrics.width / metrics.deviceScaleFactor); | 437 var dipWidth = Math.round(metrics.width / metrics.deviceScaleFactor); |
| 463 var dipHeight = Math.round(metrics.height / metrics.deviceScaleFactor); | 438 var dipHeight = Math.round(metrics.height / metrics.deviceScaleFactor); |
| 464 | 439 |
| 465 // Disable override without checks. | 440 // Disable override without checks. |
| 466 if (dipWidth && dipHeight && WebInspector.isInspectingDevice()) { | 441 if (dipWidth && dipHeight && WebInspector.isInspectingDevice()) { |
| 467 this._updateWarningMessage(WebInspector.UIString("Screen emulation o n the device is not available.")); | 442 this._updateWarningMessage(WebInspector.UIString("Screen emulation o n the device is not available.")); |
| 468 return; | 443 return; |
| 469 } | 444 } |
| 470 | 445 |
| 471 PageAgent.setDeviceMetricsOverride(dipWidth, dipHeight, metrics.deviceSc aleFactor, WebInspector.settings.emulateViewport.get(), WebInspector.settings.de viceFitWindow.get(), metrics.textAutosizing, metrics.fontScaleFactor, apiCallbac k.bind(this)); | 446 PageAgent.setDeviceMetricsOverride(dipWidth, dipHeight, metrics.deviceSc aleFactor, WebInspector.settings.emulateViewport.get(), WebInspector.settings.de viceFitWindow.get(), metrics.textAutosizing, metrics.fontScaleFactor(), apiCallb ack.bind(this)); |
| 472 | 447 |
| 473 /** | 448 /** |
| 474 * param {?Protocol.Error} error | 449 * param {?Protocol.Error} error |
| 475 */ | 450 */ |
| 476 function apiCallback(error) | 451 function apiCallback(error) |
| 477 { | 452 { |
| 478 if (error) { | 453 if (error) { |
| 479 this._updateWarningMessage(WebInspector.UIString("Screen emulati on is not available on this page.")); | 454 this._updateWarningMessage(WebInspector.UIString("Screen emulati on is not available on this page.")); |
| 480 return; | 455 return; |
| 481 } | 456 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 }, | 541 }, |
| 567 | 542 |
| 568 __proto__: WebInspector.Object.prototype | 543 __proto__: WebInspector.Object.prototype |
| 569 } | 544 } |
| 570 | 545 |
| 571 | 546 |
| 572 /** | 547 /** |
| 573 * @type {WebInspector.OverridesSupport} | 548 * @type {WebInspector.OverridesSupport} |
| 574 */ | 549 */ |
| 575 WebInspector.overridesSupport; | 550 WebInspector.overridesSupport; |
| OLD | NEW |