summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/print_preview/data/measurement_system.js
blob: ef28f86f4d16b9496461d92d682798eb2c06555b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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;
  };

  /**
   * 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) {
      return [',', '.'];
    }
    var regex = /^(\d+)(\W?)(\d+)(\W?)(\d+)$/;
    var matches = numberFormat.match(regex) || ['', '', ',', '', '.'];
    return [matches[2], matches[4]];
  };

  /**
   * Enumeration of measurement unit types.
   * @enum {number}
   */
  MeasurementSystem.UnitType = {
    METRIC: 0, // millimeters
    IMPERIAL: 1 // inches
  };

  /**
   * Maximum resolution of local unit values.
   * @type {!Object.<!print_preview.MeasurementSystem.UnitType, number>}
   * @private
   */
  MeasurementSystem.Precision_ = {};
  MeasurementSystem.Precision_[MeasurementSystem.UnitType.METRIC] = 0.5;
  MeasurementSystem.Precision_[MeasurementSystem.UnitType.IMPERIAL] = 0.01;

  /**
   * Maximum number of decimal places to keep for local unit.
   * @type {!Object.<!print_preview.MeasurementSystem.UnitType, number>}
   * @private
   */
  MeasurementSystem.DecimalPlaces_ = {};
  MeasurementSystem.DecimalPlaces_[MeasurementSystem.UnitType.METRIC] = 1;
  MeasurementSystem.DecimalPlaces_[MeasurementSystem.UnitType.IMPERIAL] = 2;

  /**
   * 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 thousands delimeter character of the measurement
     *     system.
     */
    get thousandsDelimeter() {
      return this.thousandsDelimeter_;
    },

    /**
     * @return {string} The decimal delimeter character of the measurement
     *     system.
     */
    get decimalDelimeter() {
      return this.decimalDelimeter_;
    },

    setSystem: function(thousandsDelimeter, decimalDelimeter, unitType) {
      this.thousandsDelimeter_ = thousandsDelimeter;
      this.decimalDelimeter_ = decimalDelimeter;
      this.unitType_ = unitType;
    },

    /**
     * Rounds a value in the local system's units to the appropriate precision.
     * @param {number} value Value to round.
     * @return {number} Rounded value.
     */
    roundValue: function(value) {
      var precision = MeasurementSystem.Precision_[this.unitType_];
      var roundedValue = Math.round(value / precision) * precision;
      // Truncate
      return +roundedValue.toFixed(
          MeasurementSystem.DecimalPlaces_[this.unitType_]);
    },

    /**
     * @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} localUnits 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
  };
});