| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.exportPath('print_preview'); |
| 6 |
| 7 /** |
| 8 * Enumeration of measurement unit types. |
| 9 * @enum {number} |
| 10 */ |
| 11 print_preview.MeasurementSystemUnitType = { |
| 12 METRIC: 0, // millimeters |
| 13 IMPERIAL: 1 // inches |
| 14 }; |
| 15 |
| 5 cr.define('print_preview', function() { | 16 cr.define('print_preview', function() { |
| 6 'use strict'; | 17 'use strict'; |
| 7 | 18 |
| 8 /** | 19 /** |
| 9 * Measurement system of the print preview. Used to parse and serialize point | 20 * Measurement system of the print preview. Used to parse and serialize point |
| 10 * measurements into the system's local units (e.g. millimeters, inches). | 21 * measurements into the system's local units (e.g. millimeters, inches). |
| 11 * @param {string} thousandsDelimeter Delimeter between thousands digits. | 22 * @param {string} thousandsDelimeter Delimeter between thousands digits. |
| 12 * @param {string} decimalDelimeter Delimeter between integers and decimals. | 23 * @param {string} decimalDelimeter Delimeter between integers and decimals. |
| 13 * @param {!print_preview.MeasurementSystem.UnitType} unitType Measurement | 24 * @param {!print_preview.MeasurementSystemUnitType} unitType Measurement |
| 14 * unit type of the system. | 25 * unit type of the system. |
| 15 * @constructor | 26 * @constructor |
| 16 */ | 27 */ |
| 17 function MeasurementSystem(thousandsDelimeter, decimalDelimeter, unitType) { | 28 function MeasurementSystem(thousandsDelimeter, decimalDelimeter, unitType) { |
| 18 this.thousandsDelimeter_ = thousandsDelimeter || ','; | 29 this.thousandsDelimeter_ = thousandsDelimeter || ','; |
| 19 this.decimalDelimeter_ = decimalDelimeter || '.'; | 30 this.decimalDelimeter_ = decimalDelimeter || '.'; |
| 20 this.unitType_ = unitType; | 31 this.unitType_ = unitType; |
| 21 }; | 32 } |
| 22 | 33 |
| 23 /** | 34 /** |
| 24 * Parses |numberFormat| and extracts the symbols used for the thousands point | 35 * Parses |numberFormat| and extracts the symbols used for the thousands point |
| 25 * and decimal point. | 36 * and decimal point. |
| 26 * @param {string} numberFormat The formatted version of the number 12345678. | 37 * @param {string} numberFormat The formatted version of the number 12345678. |
| 27 * @return {!Array<string>} The extracted symbols in the order | 38 * @return {!Array<string>} The extracted symbols in the order |
| 28 * [thousandsSymbol, decimalSymbol]. For example, | 39 * [thousandsSymbol, decimalSymbol]. For example, |
| 29 * parseNumberFormat("123,456.78") returns [",", "."]. | 40 * parseNumberFormat("123,456.78") returns [",", "."]. |
| 30 */ | 41 */ |
| 31 MeasurementSystem.parseNumberFormat = function(numberFormat) { | 42 MeasurementSystem.parseNumberFormat = function(numberFormat) { |
| 32 if (!numberFormat) { | 43 if (!numberFormat) { |
| 33 return [',', '.']; | 44 return [',', '.']; |
| 34 } | 45 } |
| 35 var regex = /^(\d+)(\W?)(\d+)(\W?)(\d+)$/; | 46 var regex = /^(\d+)(\W?)(\d+)(\W?)(\d+)$/; |
| 36 var matches = numberFormat.match(regex) || ['', '', ',', '', '.']; | 47 var matches = numberFormat.match(regex) || ['', '', ',', '', '.']; |
| 37 return [matches[2], matches[4]]; | 48 return [matches[2], matches[4]]; |
| 38 }; | 49 }; |
| 39 | 50 |
| 40 /** | 51 /** |
| 41 * Enumeration of measurement unit types. | |
| 42 * @enum {number} | |
| 43 */ | |
| 44 MeasurementSystem.UnitType = { | |
| 45 METRIC: 0, // millimeters | |
| 46 IMPERIAL: 1 // inches | |
| 47 }; | |
| 48 | |
| 49 /** | |
| 50 * Maximum resolution of local unit values. | 52 * Maximum resolution of local unit values. |
| 51 * @type {!Object<!print_preview.MeasurementSystem.UnitType, number>} | 53 * @type {!Object<!print_preview.MeasurementSystemUnitType, number>} |
| 52 * @private | 54 * @private |
| 53 */ | 55 */ |
| 54 MeasurementSystem.Precision_ = {}; | 56 MeasurementSystem.Precision_ = {}; |
| 55 MeasurementSystem.Precision_[MeasurementSystem.UnitType.METRIC] = 0.5; | 57 MeasurementSystem.Precision_[ |
| 56 MeasurementSystem.Precision_[MeasurementSystem.UnitType.IMPERIAL] = 0.01; | 58 print_preview.MeasurementSystemUnitType.METRIC] = 0.5; |
| 59 MeasurementSystem.Precision_[ |
| 60 print_preview.MeasurementSystemUnitType.IMPERIAL] = 0.01; |
| 57 | 61 |
| 58 /** | 62 /** |
| 59 * Maximum number of decimal places to keep for local unit. | 63 * Maximum number of decimal places to keep for local unit. |
| 60 * @type {!Object<!print_preview.MeasurementSystem.UnitType, number>} | 64 * @type {!Object<!print_preview.MeasurementSystemUnitType, number>} |
| 61 * @private | 65 * @private |
| 62 */ | 66 */ |
| 63 MeasurementSystem.DecimalPlaces_ = {}; | 67 MeasurementSystem.DecimalPlaces_ = {}; |
| 64 MeasurementSystem.DecimalPlaces_[MeasurementSystem.UnitType.METRIC] = 1; | 68 MeasurementSystem.DecimalPlaces_[ |
| 65 MeasurementSystem.DecimalPlaces_[MeasurementSystem.UnitType.IMPERIAL] = 2; | 69 print_preview.MeasurementSystemUnitType.METRIC] = 1; |
| 70 MeasurementSystem.DecimalPlaces_[ |
| 71 print_preview.MeasurementSystemUnitType.IMPERIAL] = 2; |
| 66 | 72 |
| 67 /** | 73 /** |
| 68 * Number of points per inch. | 74 * Number of points per inch. |
| 69 * @type {number} | 75 * @type {number} |
| 70 * @const | 76 * @const |
| 71 * @private | 77 * @private |
| 72 */ | 78 */ |
| 73 MeasurementSystem.PTS_PER_INCH_ = 72.0; | 79 MeasurementSystem.PTS_PER_INCH_ = 72.0; |
| 74 | 80 |
| 75 /** | 81 /** |
| 76 * Number of points per millimeter. | 82 * Number of points per millimeter. |
| 77 * @type {number} | 83 * @type {number} |
| 78 * @const | 84 * @const |
| 79 * @private | 85 * @private |
| 80 */ | 86 */ |
| 81 MeasurementSystem.PTS_PER_MM_ = MeasurementSystem.PTS_PER_INCH_ / 25.4; | 87 MeasurementSystem.PTS_PER_MM_ = MeasurementSystem.PTS_PER_INCH_ / 25.4; |
| 82 | 88 |
| 83 MeasurementSystem.prototype = { | 89 MeasurementSystem.prototype = { |
| 84 /** @return {string} The unit type symbol of the measurement system. */ | 90 /** @return {string} The unit type symbol of the measurement system. */ |
| 85 get unitSymbol() { | 91 get unitSymbol() { |
| 86 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { | 92 if (this.unitType_ == print_preview.MeasurementSystemUnitType.METRIC) { |
| 87 return 'mm'; | 93 return 'mm'; |
| 88 } else if (this.unitType_ == MeasurementSystem.UnitType.IMPERIAL) { | 94 } else if (this.unitType_ == |
| 95 print_preview.MeasurementSystemUnitType.IMPERIAL) { |
| 89 return '"'; | 96 return '"'; |
| 90 } else { | 97 } else { |
| 91 throw Error('Unit type not supported: ' + this.unitType_); | 98 throw Error('Unit type not supported: ' + this.unitType_); |
| 92 } | 99 } |
| 93 }, | 100 }, |
| 94 | 101 |
| 95 /** | 102 /** |
| 96 * @return {string} The thousands delimeter character of the measurement | 103 * @return {string} The thousands delimeter character of the measurement |
| 97 * system. | 104 * system. |
| 98 */ | 105 */ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 125 // Truncate | 132 // Truncate |
| 126 return +roundedValue.toFixed( | 133 return +roundedValue.toFixed( |
| 127 MeasurementSystem.DecimalPlaces_[this.unitType_]); | 134 MeasurementSystem.DecimalPlaces_[this.unitType_]); |
| 128 }, | 135 }, |
| 129 | 136 |
| 130 /** | 137 /** |
| 131 * @param {number} pts Value in points to convert to local units. | 138 * @param {number} pts Value in points to convert to local units. |
| 132 * @return {number} Value in local units. | 139 * @return {number} Value in local units. |
| 133 */ | 140 */ |
| 134 convertFromPoints: function(pts) { | 141 convertFromPoints: function(pts) { |
| 135 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { | 142 if (this.unitType_ == print_preview.MeasurementSystemUnitType.METRIC) { |
| 136 return pts / MeasurementSystem.PTS_PER_MM_; | 143 return pts / MeasurementSystem.PTS_PER_MM_; |
| 137 } else { | 144 } else { |
| 138 return pts / MeasurementSystem.PTS_PER_INCH_; | 145 return pts / MeasurementSystem.PTS_PER_INCH_; |
| 139 } | 146 } |
| 140 }, | 147 }, |
| 141 | 148 |
| 142 /** | 149 /** |
| 143 * @param {number} localUnits Value in local units to convert to points. | 150 * @param {number} localUnits Value in local units to convert to points. |
| 144 * @return {number} Value in points. | 151 * @return {number} Value in points. |
| 145 */ | 152 */ |
| 146 convertToPoints: function(localUnits) { | 153 convertToPoints: function(localUnits) { |
| 147 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { | 154 if (this.unitType_ == print_preview.MeasurementSystemUnitType.METRIC) { |
| 148 return localUnits * MeasurementSystem.PTS_PER_MM_; | 155 return localUnits * MeasurementSystem.PTS_PER_MM_; |
| 149 } else { | 156 } else { |
| 150 return localUnits * MeasurementSystem.PTS_PER_INCH_; | 157 return localUnits * MeasurementSystem.PTS_PER_INCH_; |
| 151 } | 158 } |
| 152 } | 159 } |
| 153 }; | 160 }; |
| 154 | 161 |
| 155 // Export | 162 // Export |
| 156 return { | 163 return { |
| 157 MeasurementSystem: MeasurementSystem | 164 MeasurementSystem: MeasurementSystem |
| 158 }; | 165 }; |
| 159 }); | 166 }); |
| OLD | NEW |