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 3// found in the LICENSE file. 4 5cr.define('print_preview', function() { 6 'use strict'; 7 8 /** 9 * Measurement system of the print preview. Used to parse and serialize point 10 * measurements into the system's local units (e.g. millimeters, inches). 11 * @param {string} thousandsDelimeter Delimeter between thousands digits. 12 * @param {string} decimalDelimeter Delimeter between integers and decimals. 13 * @param {!print_preview.MeasurementSystem.UnitType} unitType Measurement 14 * unit type of the system. 15 * @constructor 16 */ 17 function MeasurementSystem(thousandsDelimeter, decimalDelimeter, unitType) { 18 this.thousandsDelimeter_ = thousandsDelimeter || ','; 19 this.decimalDelimeter_ = decimalDelimeter || '.'; 20 this.unitType_ = unitType; 21 }; 22 23 /** 24 * Parses |numberFormat| and extracts the symbols used for the thousands point 25 * and decimal point. 26 * @param {string} numberFormat The formatted version of the number 12345678. 27 * @return {!Array.<string>} The extracted symbols in the order 28 * [thousandsSymbol, decimalSymbol]. For example, 29 * parseNumberFormat("123,456.78") returns [",", "."]. 30 */ 31 MeasurementSystem.parseNumberFormat = function(numberFormat) { 32 if (!numberFormat) { 33 return [',', '.']; 34 } 35 var regex = /^(\d+)(\W?)(\d+)(\W?)(\d+)$/; 36 var matches = numberFormat.match(regex) || ['', '', ',', '', '.']; 37 return [matches[2], matches[4]]; 38 }; 39 40 /** 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. 51 * @type {!Object.<!print_preview.MeasurementSystem.UnitType, number>} 52 * @private 53 */ 54 MeasurementSystem.Precision_ = {}; 55 MeasurementSystem.Precision_[MeasurementSystem.UnitType.METRIC] = 0.5; 56 MeasurementSystem.Precision_[MeasurementSystem.UnitType.IMPERIAL] = 0.01; 57 58 /** 59 * Maximum number of decimal places to keep for local unit. 60 * @type {!Object.<!print_preview.MeasurementSystem.UnitType, number>} 61 * @private 62 */ 63 MeasurementSystem.DecimalPlaces_ = {}; 64 MeasurementSystem.DecimalPlaces_[MeasurementSystem.UnitType.METRIC] = 1; 65 MeasurementSystem.DecimalPlaces_[MeasurementSystem.UnitType.IMPERIAL] = 2; 66 67 /** 68 * Number of points per inch. 69 * @type {number} 70 * @const 71 * @private 72 */ 73 MeasurementSystem.PTS_PER_INCH_ = 72.0; 74 75 /** 76 * Number of points per millimeter. 77 * @type {number} 78 * @const 79 * @private 80 */ 81 MeasurementSystem.PTS_PER_MM_ = MeasurementSystem.PTS_PER_INCH_ / 25.4; 82 83 MeasurementSystem.prototype = { 84 /** @return {string} The unit type symbol of the measurement system. */ 85 get unitSymbol() { 86 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { 87 return 'mm'; 88 } else if (this.unitType_ == MeasurementSystem.UnitType.IMPERIAL) { 89 return '"'; 90 } else { 91 throw Error('Unit type not supported: ' + this.unitType_); 92 } 93 }, 94 95 /** 96 * @return {string} The thousands delimeter character of the measurement 97 * system. 98 */ 99 get thousandsDelimeter() { 100 return this.thousandsDelimeter_; 101 }, 102 103 /** 104 * @return {string} The decimal delimeter character of the measurement 105 * system. 106 */ 107 get decimalDelimeter() { 108 return this.decimalDelimeter_; 109 }, 110 111 setSystem: function(thousandsDelimeter, decimalDelimeter, unitType) { 112 this.thousandsDelimeter_ = thousandsDelimeter; 113 this.decimalDelimeter_ = decimalDelimeter; 114 this.unitType_ = unitType; 115 }, 116 117 /** 118 * Rounds a value in the local system's units to the appropriate precision. 119 * @param {number} value Value to round. 120 * @return {number} Rounded value. 121 */ 122 roundValue: function(value) { 123 var precision = MeasurementSystem.Precision_[this.unitType_]; 124 var roundedValue = Math.round(value / precision) * precision; 125 // Truncate 126 return roundedValue.toFixed( 127 MeasurementSystem.DecimalPlaces_[this.unitType_]); 128 }, 129 130 /** 131 * @param {number} pts Value in points to convert to local units. 132 * @return {number} Value in local units. 133 */ 134 convertFromPoints: function(pts) { 135 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { 136 return pts / MeasurementSystem.PTS_PER_MM_; 137 } else { 138 return pts / MeasurementSystem.PTS_PER_INCH_; 139 } 140 }, 141 142 /** 143 * @param {number} Value in local units to convert to points. 144 * @return {number} Value in points. 145 */ 146 convertToPoints: function(localUnits) { 147 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { 148 return localUnits * MeasurementSystem.PTS_PER_MM_; 149 } else { 150 return localUnits * MeasurementSystem.PTS_PER_INCH_; 151 } 152 } 153 }; 154 155 // Export 156 return { 157 MeasurementSystem: MeasurementSystem 158 }; 159}); 160