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.
5 cr.define('print_preview', function() {
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.
17 function MeasurementSystem(thousandsDelimeter, decimalDelimeter, unitType) {
18 this.thousandsDelimeter_ = thousandsDelimeter || ',';
19 this.decimalDelimeter_ = decimalDelimeter || '.';
20 this.unitType_ = unitType;
24 * Parses |numberFormat| and extracts the symbols used for the thousands 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 [",", "."].
31 MeasurementSystem.parseNumberFormat = function(numberFormat) {
35 var regex = /^(\d+)(\W?)(\d+)(\W?)(\d+)$/;
36 var matches = numberFormat.match(regex) || ['', '', ',', '', '.'];
37 return [matches[2], matches[4]];
41 * Enumeration of measurement unit types.
44 MeasurementSystem.UnitType = {
45 METRIC: 0, // millimeters
50 * Maximum resolution of local unit values.
51 * @type {!Object<!print_preview.MeasurementSystem.UnitType, number>}
54 MeasurementSystem.Precision_ = {};
55 MeasurementSystem.Precision_[MeasurementSystem.UnitType.METRIC] = 0.5;
56 MeasurementSystem.Precision_[MeasurementSystem.UnitType.IMPERIAL] = 0.01;
59 * Maximum number of decimal places to keep for local unit.
60 * @type {!Object<!print_preview.MeasurementSystem.UnitType, number>}
63 MeasurementSystem.DecimalPlaces_ = {};
64 MeasurementSystem.DecimalPlaces_[MeasurementSystem.UnitType.METRIC] = 1;
65 MeasurementSystem.DecimalPlaces_[MeasurementSystem.UnitType.IMPERIAL] = 2;
68 * Number of points per inch.
73 MeasurementSystem.PTS_PER_INCH_ = 72.0;
76 * Number of points per millimeter.
81 MeasurementSystem.PTS_PER_MM_ = MeasurementSystem.PTS_PER_INCH_ / 25.4;
83 MeasurementSystem.prototype = {
84 /** @return {string} The unit type symbol of the measurement system. */
86 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) {
88 } else if (this.unitType_ == MeasurementSystem.UnitType.IMPERIAL) {
91 throw Error('Unit type not supported: ' + this.unitType_);
96 * @return {string} The thousands delimeter character of the measurement
99 get thousandsDelimeter() {
100 return this.thousandsDelimeter_;
104 * @return {string} The decimal delimeter character of the measurement
107 get decimalDelimeter() {
108 return this.decimalDelimeter_;
111 setSystem: function(thousandsDelimeter, decimalDelimeter, unitType) {
112 this.thousandsDelimeter_ = thousandsDelimeter;
113 this.decimalDelimeter_ = decimalDelimeter;
114 this.unitType_ = unitType;
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.
122 roundValue: function(value) {
123 var precision = MeasurementSystem.Precision_[this.unitType_];
124 var roundedValue = Math.round(value / precision) * precision;
126 return +roundedValue.toFixed(
127 MeasurementSystem.DecimalPlaces_[this.unitType_]);
131 * @param {number} pts Value in points to convert to local units.
132 * @return {number} Value in local units.
134 convertFromPoints: function(pts) {
135 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) {
136 return pts / MeasurementSystem.PTS_PER_MM_;
138 return pts / MeasurementSystem.PTS_PER_INCH_;
143 * @param {number} localUnits Value in local units to convert to points.
144 * @return {number} Value in points.
146 convertToPoints: function(localUnits) {
147 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) {
148 return localUnits * MeasurementSystem.PTS_PER_MM_;
150 return localUnits * MeasurementSystem.PTS_PER_INCH_;
157 MeasurementSystem: MeasurementSystem