Chromium Code Reviews| Index: chrome/browser/resources/print_preview/data/measurement_system.js |
| diff --git a/chrome/browser/resources/print_preview/data/measurement_system.js b/chrome/browser/resources/print_preview/data/measurement_system.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6dce4d3a5569a3f26a809595e085c0eb8b73a013 |
| --- /dev/null |
| +++ b/chrome/browser/resources/print_preview/data/measurement_system.js |
| @@ -0,0 +1,116 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +cr.define('print_preview', function() { |
| + 'use strict'; |
| + |
| + /** |
| + * Measurement system of the print preview. Used to parse and serialize point |
| + * measurements into the system's local units (e.g. millimeters, inches). |
| + * |
| + * @param {string} thousandsDelimeter Delimeter between thousands digits. |
| + * @param {string} decimalDelimeter Delimeter between integers and decimals. |
| + * @param {print_preview.MeasurementSystem.UnitType} unitType Measurement unit |
| + * type of the system. |
| + * @constructor |
| + */ |
| + function MeasurementSystem(thousandsDelimeter, decimalDelimeter, unitType) { |
| + this.thousandsDelimeter_ = thousandsDelimeter || ','; |
| + this.decimalDelimeter_ = decimalDelimeter || '.'; |
| + this.unitType_ = unitType == null ? |
| + MeasurementSystem.UnitType.IMPERIAL : unitType; |
|
dpapad
2012/04/24 01:24:56
Isn't this equivalent to
this.unitType_ = unitType
Robert Toscano
2012/04/24 22:29:56
Done.
|
| + }; |
| + |
| + /** |
| + * Parses |numberFormat| and extracts the symbols used for the thousands point |
| + * and decimal point. |
| + * @param {string} numberFormat The formatted version of the number 12345678. |
| + * @return {!Array.<string>} The extracted symbols in the order |
| + * [thousandsSymbol, decimalSymbol]. For example, |
| + * parseNumberFormat("123,456.78") returns [",", "."]. |
| + */ |
| + MeasurementSystem.parseNumberFormat = function(numberFormat) { |
| + if (!numberFormat) { |
| + numberFormat = [',', '.']; |
| + } |
| + var regex = /^(\d+)(\W{0,1})(\d+)(\W{0,1})(\d+)$/; |
| + var matches = numberFormat.match(regex) || ['', '', ',', '', '.']; |
| + return [matches[2], matches[4]]; |
| + }; |
| + |
| + /** |
| + * Enumeration of measurement unit types. |
| + * @enum {number} |
| + */ |
| + MeasurementSystem.UnitType = { |
| + METRIC: 0, |
| + IMPERIAL: 1 |
| + }; |
| + |
| + /** |
| + * Number of points per inch. |
| + * @type {number} |
| + * @const |
| + * @private |
| + */ |
| + MeasurementSystem.PTS_PER_INCH_ = 72.0; |
| + |
| + /** |
| + * Number of points per millimeter. |
| + * @type {number} |
| + * @const |
| + * @private |
| + */ |
| + MeasurementSystem.PTS_PER_MM_ = MeasurementSystem.PTS_PER_INCH_ / 25.4; |
| + |
| + MeasurementSystem.prototype = { |
| + /** @return {string} The unit type symbol of the measurement system. */ |
| + get unitSymbol() { |
| + if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { |
| + return 'mm'; |
| + } else if (this.unitType_ == MeasurementSystem.UnitType.IMPERIAL) { |
| + return '"'; |
| + } else { |
| + throw Error('Unit type not supported: ' + this.unitType_); |
| + } |
| + }, |
| + |
| + /** |
| + * @return {string} The decimal delimeter character of the measurement |
| + * system. |
| + */ |
| + get decimalDelimeter() { |
| + return this.decimalDelimeter_; |
| + }, |
| + |
| + /** |
| + * @param {number} pts Value in points to convert to local units. |
| + * @return {number} Value in local units. |
| + */ |
| + convertFromPoints: function(pts) { |
| + if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { |
| + return pts / MeasurementSystem.PTS_PER_MM_; |
| + } else { |
| + return pts / MeasurementSystem.PTS_PER_INCH_; |
| + } |
| + }, |
| + |
| + /** |
| + * @param {number} Value in local units to convert to points. |
| + * @return {number} Value in points. |
| + */ |
| + convertToPoints: function(localUnits) { |
| + if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { |
| + return localUnits * MeasurementSystem.PTS_PER_MM_; |
| + } else { |
| + return localUnits * MeasurementSystem.PTS_PER_INCH_; |
| + } |
| + } |
| + }; |
| + |
| + // Export |
| + return { |
| + MeasurementSystem: MeasurementSystem |
| + }; |
| +}); |