OLD | NEW |
(Empty) | |
| 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 |
| 5 cr.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 * |
| 12 * @param {string} thousandsDelimeter Delimeter between thousands digits. |
| 13 * @param {string} decimalDelimeter Delimeter between integers and decimals. |
| 14 * @param {print_preview.MeasurementSystem.UnitType} unitType Measurement unit |
| 15 * type of the system. |
| 16 * @constructor |
| 17 */ |
| 18 function MeasurementSystem(thousandsDelimeter, decimalDelimeter, unitType) { |
| 19 this.thousandsDelimeter_ = thousandsDelimeter; |
| 20 this.decimalDelimeter_ = decimalDelimeter; |
| 21 this.unitType_ = unitType; |
| 22 }; |
| 23 |
| 24 /** |
| 25 * Parses |numberFormat| and extracts the symbols used for the thousands point |
| 26 * and decimal point. |
| 27 * @param {string} numberFormat The formatted version of the number 12345678. |
| 28 * @return {!Array.<string>} The extracted symbols in the order |
| 29 * [thousandsSymbol, decimalSymbol]. For example, |
| 30 * parseNumberFormat("123,456.78") returns [",", "."]. |
| 31 */ |
| 32 MeasurementSystem.parseNumberFormat = function(numberFormat) { |
| 33 if (!numberFormat) |
| 34 numberFormat = ''; |
| 35 var regex = /^(\d+)(\W{0,1})(\d+)(\W{0,1})(\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, |
| 46 IMPERIAL: 1 |
| 47 }; |
| 48 |
| 49 /** |
| 50 * Number of points per inch. |
| 51 * @type {number} |
| 52 * @const |
| 53 * @private |
| 54 */ |
| 55 MeasurementSystem.PTS_PER_INCH_ = 72.0; |
| 56 |
| 57 /** |
| 58 * Number of points per millimeter. |
| 59 * @type {number} |
| 60 * @const |
| 61 * @private |
| 62 */ |
| 63 MeasurementSystem.PTS_PER_MM_ = MeasurementSystem.PTS_PER_INCH_ / 25.4; |
| 64 |
| 65 MeasurementSystem.prototype = { |
| 66 /** @return {string} The unit type symbol of the measurement system. */ |
| 67 get unitSymbol() { |
| 68 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { |
| 69 return 'mm'; |
| 70 } else if (this.unitType_ == MeasurementSystem.UnitType.IMPERIAL) { |
| 71 return '"'; |
| 72 } else { |
| 73 throw Error('Unit type not supported: ' + this.unitType_); |
| 74 } |
| 75 }, |
| 76 |
| 77 /** |
| 78 * @return {string} The decimal delimeter character of the measurement |
| 79 * system. |
| 80 */ |
| 81 get decimalDelimeter() { |
| 82 return this.decimalDelimeter_; |
| 83 }, |
| 84 |
| 85 /** |
| 86 * @param {number} pts Value in points to convert to local units. |
| 87 * @return {number} Value in local units. |
| 88 */ |
| 89 convertFromPoints: function(pts) { |
| 90 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { |
| 91 return pts / MeasurementSystem.PTS_PER_MM_; |
| 92 } else { |
| 93 return pts / MeasurementSystem.PTS_PER_INCH_; |
| 94 } |
| 95 }, |
| 96 |
| 97 /** |
| 98 * @param {number} Value in local units to convert to points. |
| 99 * @return {number} Value in points. |
| 100 */ |
| 101 convertToPoints: function(localUnits) { |
| 102 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) { |
| 103 return localUnits * MeasurementSystem.PTS_PER_MM_; |
| 104 } else { |
| 105 return localUnits * MeasurementSystem.PTS_PER_INCH_; |
| 106 } |
| 107 } |
| 108 }; |
| 109 |
| 110 // Export |
| 111 return { |
| 112 MeasurementSystem: MeasurementSystem |
| 113 }; |
| 114 }); |
OLD | NEW |