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