summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/print_preview/margin_utils.js
blob: a93ef0e72083cd06bada7b2a4b39ad409188c700 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// 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';

  /**
   * Checks if |text| has a valid margin value format. A valid format is
   * parsable as a number and is greater than zero.
   * Example: "1.00", "1", ".5", "1.1" are valid values.
   * Example: "1.4dsf", "-1" are invalid.
   * Note: The inch symbol (") at the end of |text| is allowed.
   *
   * @param {string} text The text to check.
   * @return {number} The margin value represented by |text| or null if |text|
   *     does not represent a valid number.
   */
  function extractMarginValue(text) {
    // Removing whitespace anywhere in the string.
    text = text.replace(/\s*/g, '');
    if (text.length == 0)
      return -1;
    var validationRegex = getValidationRegExp();
    if (validationRegex.test(text)) {
      // Replacing decimal point with the dot symbol in order to use
      // parseFloat() properly.
      var replacementRegex = new RegExp('\\' +
          print_preview.MarginSettings.decimalPoint + '{1}');
      text = text.replace(replacementRegex, '.');
      return parseFloat(text);
    }
    return -1;
  }

  /**
   * @return {RegExp} A regular expression for validating the input of the user.
   *     It takes into account the user's locale.
   */
  function getValidationRegExp() {
    var regex = new RegExp('(^\\d+)(\\' +
       print_preview.MarginSettings.thousandsPoint + '\\d{3})*(\\' +
       print_preview.MarginSettings.decimalPoint + '\\d+)*' +
       (!print_preview.MarginSettings.useMetricSystem ? '"?' : '(mm)?') + '$');
    return regex;
  }

  var marginValidationStates = {
    TOO_SMALL: 0,
    WITHIN_RANGE: 1,
    TOO_BIG: 2,
    NOT_A_NUMBER: 3
  };

  /**
   * Checks  whether |value| is within range [0, limit].
   * @return {number} An appropriate value from enum |marginValidationStates|.
   */
  function validateMarginValue(value, limit) {
    if (value <= limit && value >= 0)
      return marginValidationStates.WITHIN_RANGE;
    else if (value < 0)
      return marginValidationStates.TOO_SMALL;
    else
      return marginValidationStates.TOO_BIG;
  }

  /**
   * @param {string} text The text to check in user's locale units.
   * @param {number} limit The upper bound of the valid margin range (in
   *     points).
   * @return {number} An appropriate value from enum |marginValidationStates|.
   */
  function validateMarginText(text, limit) {
    var value = extractMarginValue(text);
    if (value == -1)
      return marginValidationStates.NOT_A_NUMBER;
    value = print_preview.convertLocaleUnitsToPoints(value);
    return validateMarginValue(value, limit);
  }

  /**
   * @param {number} value The value to convert in points.
   * @return {number} The corresponding value after converting to user's locale
   *     units.
   */
  function convertPointsToLocaleUnits(value) {
    return print_preview.MarginSettings.useMetricSystem ?
        convertPointsToMillimeters(value) : convertPointsToInches(value);
  }

  /**
   * @param {number} value The value to convert in user's locale units.
   * @return {number} The corresponding value after converting to points.
   */
  function convertLocaleUnitsToPoints(value) {
    return print_preview.MarginSettings.useMetricSystem ?
        convertMillimetersToPoints(value) : convertInchesToPoints(value);
  }

  /**
   * Converts |value| to user's locale units and then back to points. Note:
   * Because of the precision the return value might be different than |value|.
   * @param {number} value The value in points to convert.
   * @return {number} The value in points after converting to user's locale
   *     units  with a certain precision and back.
   */
  function convertPointsToLocaleUnitsAndBack(value) {
    var inLocaleUnits =
        convertPointsToLocaleUnits(value).toFixed(getDesiredPrecision());
    return convertLocaleUnitsToPoints(inLocaleUnits);
  }

  /**
   * @return {number} The number of decimal digits to keep based on the
   *     measurement system used.
   */
  function getDesiredPrecision() {
    return print_preview.MarginSettings.useMetricSystem ? 0 : 2;
  }

  /**
   * @param {number} value The value to convert in points.
   * @return {string} The equivalent text in user locale units with precision
   *     of two digits.
   */
  function convertPointsToLocaleUnitsText(value) {
    var inLocaleUnits =
        convertPointsToLocaleUnits(value).toFixed(getDesiredPrecision());
    var inLocaleUnitsText = inLocaleUnits.toString(10).replace(
        /\./g, print_preview.MarginSettings.decimalPoint);
    return !print_preview.MarginSettings.useMetricSystem ?
        inLocaleUnitsText + '"' : inLocaleUnitsText + 'mm';
  }

  /**
   * Creates a Rect object. This object describes a rectangle in a 2D plane. The
   * units of |x|, |y|, |width|, |height| are chosen by clients of this class.
   * @constructor
   */
  function Rect(x, y, width, height) {
    // @type {number} Horizontal distance of the upper left corner from origin.
    this.x = x;
    // @type {number} Vertical distance of the upper left corner from origin.
    this.y = y;
    // @type {number} Width of |this| rectangle.
    this.width = width;
    // @type {number} Height of |this| rectangle.
    this.height = height;
  };

  Rect.prototype = {
    /**
     * @type {number} The x coordinate of the right-most point.
     */
    get right() {
      return this.x + this.width;
    },

    /**
     * @type {number} The y coordinate of the lower-most point.
     */
    get bottom() {
      return this.y + this.height;
    },

    /**
     * Clones |this| and returns the cloned object.
     * @return {Rect} A copy of |this|.
     */
    clone: function() {
      return new Rect(this.x, this.y, this.width, this.height);
    }
  };

  return {
    convertPointsToLocaleUnitsAndBack:
        convertPointsToLocaleUnitsAndBack,
    convertPointsToLocaleUnitsText: convertPointsToLocaleUnitsText,
    convertLocaleUnitsToPoints: convertLocaleUnitsToPoints,
    extractMarginValue: extractMarginValue,
    marginValidationStates: marginValidationStates,
    Rect: Rect,
    validateMarginText: validateMarginText,
    validateMarginValue: validateMarginValue
  };
});