OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 * Checks if |text| has a valid margin value format. A valid format is |
| 10 * parsable as a number and is greater than zero. |
| 11 * Example: "1.00", "1", ".5", "1.1" are valid values. |
| 12 * Example: "1.4dsf", "-1" are invalid. |
| 13 * Note: The inch symbol (") at the end of |text| is allowed. |
| 14 * |
| 15 * @param {string} text The text to check. |
| 16 * @return {number} The margin value represented by |text| or null if |text| |
| 17 * does not represent a valid number. |
| 18 */ |
| 19 function extractMarginValue(text) { |
| 20 // Remove whitespace anywhere in the string. |
| 21 text.replace(/\s*/g, ''); |
| 22 if (text.length == 0) |
| 23 return -1; |
| 24 // Remove the inch(") symbol at end of string if present. |
| 25 if (text.charAt(text.length - 1) == '\"') |
| 26 text = text.slice(0, text.length - 1); |
| 27 var regex = /^\d*(\.\d+)?$/ |
| 28 if (regex.test(text)) |
| 29 return parseFloat(text); |
| 30 return -1; |
| 31 } |
| 32 |
| 33 /** |
| 34 * @param {sting} text The text to check (in inches). |
| 35 * @param {number} limit The upper bound of the valid margin range (in |
| 36 * points). |
| 37 * @return {boolean} True of |text| can be parsed and it is within the allowed |
| 38 * range. |
| 39 */ |
| 40 function isMarginTextValid(text, limit) { |
| 41 var value = extractMarginValue(text); |
| 42 if (value == -1) |
| 43 return false; |
| 44 value = convertInchesToPoints(value); |
| 45 return value <= limit; |
| 46 } |
| 47 |
| 48 /** |
| 49 * Creates a Rect object. This object describes a rectangle in a 2D plane. The |
| 50 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. |
| 51 * @constructor |
| 52 */ |
| 53 function Rect(x, y, width, height) { |
| 54 // @type {number} Horizontal distance of the upper left corner from origin. |
| 55 this.x = x; |
| 56 // @type {number} Vertical distance of the upper left corner from origin. |
| 57 this.y = y; |
| 58 // @type {number} Width of |this| rectangle. |
| 59 this.width = width; |
| 60 // @type {number} Height of |this| rectangle. |
| 61 this.height = height; |
| 62 }; |
| 63 |
| 64 Rect.prototype = { |
| 65 get right() { |
| 66 return this.x + this.width; |
| 67 }, |
| 68 |
| 69 get bottom() { |
| 70 return this.y + this.height; |
| 71 }, |
| 72 |
| 73 get middleX() { |
| 74 return this.x + this.width / 2; |
| 75 }, |
| 76 |
| 77 get middleY() { |
| 78 return this.y + this.height / 2; |
| 79 } |
| 80 }; |
| 81 |
| 82 return { |
| 83 extractMarginValue: extractMarginValue, |
| 84 isMarginTextValid: isMarginTextValid, |
| 85 Rect: Rect, |
| 86 }; |
| 87 }); |
OLD | NEW |