Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(756)

Side by Side Diff: chrome/browser/resources/print_preview/data/measurement_system.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes broken tests Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 * @param {string} thousandsDelimeter Delimeter between thousands digits.
12 * @param {string} decimalDelimeter Delimeter between integers and decimals.
13 * @param {print_preview.MeasurementSystem.UnitType} unitType Measurement unit
14 * 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 number of decimals to keep for local measurements.
51 * @type {Object.<print_preview.MeasurementSystem.UnitType, number>}
52 */
53 MeasurementSystem.Precision = {};
54 MeasurementSystem.Precision[MeasurementSystem.UnitType.METRIC] = 0;
55 MeasurementSystem.Precision[MeasurementSystem.UnitType.IMPERIAL] = 3;
56
57 /**
58 * Number of points per inch.
59 * @type {number}
60 * @const
61 * @private
62 */
63 MeasurementSystem.PTS_PER_INCH_ = 72.0;
64
65 /**
66 * Number of points per millimeter.
67 * @type {number}
68 * @const
69 * @private
70 */
71 MeasurementSystem.PTS_PER_MM_ = MeasurementSystem.PTS_PER_INCH_ / 25.4;
72
73 MeasurementSystem.prototype = {
74 /** @return {string} The unit type symbol of the measurement system. */
75 get unitSymbol() {
76 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) {
77 return 'mm';
78 } else if (this.unitType_ == MeasurementSystem.UnitType.IMPERIAL) {
79 return '"';
80 } else {
81 throw Error('Unit type not supported: ' + this.unitType_);
82 }
83 },
84
85 /**
86 * @return {string} The thousands delimeter character of the measurement
87 * system.
88 */
89 get thousandsDelimeter() {
90 return this.thousandsDelimeter_;
91 },
92
93 /**
94 * @return {string} The decimal delimeter character of the measurement
95 * system.
96 */
97 get decimalDelimeter() {
98 return this.decimalDelimeter_;
99 },
100
101 setSystem: function(thousandsDelimeter, decimalDelimeter, unitType) {
102 this.thousandsDelimeter_ = thousandsDelimeter;
103 this.decimalDelimeter_ = decimalDelimeter;
104 this.unitType_ = unitType;
105 },
106
107 /**
108 * @param {number} pts Value in points to convert to local units.
109 * @param {boolean=} opt_roundUp Forces round up to the next least
110 * significant digit.
111 * @return {number} Value in local units.
112 */
113 convertFromPoints: function(pts, opt_roundUp) {
114 var localUnits;
115 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) {
116 localUnits = pts / MeasurementSystem.PTS_PER_MM_;
117 } else {
118 localUnits = pts / MeasurementSystem.PTS_PER_INCH_;
119 }
120 var precision = MeasurementSystem.Precision[this.unitType_];
121 if (opt_roundUp != null) {
122 var delta = 0.5 * Math.pow(10, -precision);
123 localUnits = opt_roundUp ? localUnits + delta : localUnits - delta;
124 }
125 return parseFloat(localUnits.toFixed(precision));
126 },
127
128 /**
129 * @param {number} Value in local units to convert to points.
130 * @return {number} Value in points.
131 */
132 convertToPoints: function(localUnits) {
133 if (this.unitType_ == MeasurementSystem.UnitType.METRIC) {
134 return localUnits * MeasurementSystem.PTS_PER_MM_;
135 } else {
136 return localUnits * MeasurementSystem.PTS_PER_INCH_;
137 }
138 }
139 };
140
141 // Export
142 return {
143 MeasurementSystem: MeasurementSystem
144 };
145 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698