1 /** 2 * Copyright (C) 2012 KO GmbH <aditya.bhatt@kogmbh.com> 3 * 4 * @licstart 5 * This file is part of WebODF. 6 * 7 * WebODF is free software: you can redistribute it and/or modify it 8 * under the terms of the GNU Affero General Public License (GNU AGPL) 9 * as published by the Free Software Foundation, either version 3 of 10 * the License, or (at your option) any later version. 11 * 12 * WebODF is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Affero General Public License for more details. 16 * 17 * You should have received a copy of the GNU Affero General Public License 18 * along with WebODF. If not, see <http://www.gnu.org/licenses/>. 19 * @licend 20 * 21 * @source: http://www.webodf.org/ 22 * @source: https://github.com/kogmbh/WebODF/ 23 */ 24 25 /*global core*/ 26 /*jslint plusplus: true, bitwise: true */ 27 28 /** 29 * @constructor 30 */ 31 core.CSSUnits = function CSSUnits() { 32 "use strict"; 33 34 // Unit values relative to 1 inch 35 var self = this, 36 sizemap = { 37 "in": 1, 38 "cm": 2.54, 39 "mm": 25.4, 40 "pt": 72, 41 "pc": 12, 42 "px": 96 43 }; 44 45 /** 46 * Takes a number of an oldUnit and returns its value in newUnit 47 * @param {!number} value 48 * @param {!string} oldUnit 49 * @param {!string} newUnit 50 * @return {!number} 51 */ 52 this.convert = function (value, oldUnit, newUnit) { 53 return value * sizemap[newUnit] / sizemap[oldUnit]; 54 }; 55 56 /** 57 * Takes a measure such as "2cm" and returns it's measurement in the new unit, e.g. 20 58 * @param {!string} measure 59 * @param {!string} newUnit 60 * @return {!number|undefined} 61 */ 62 this.convertMeasure = function (measure, newUnit) { 63 var value, oldUnit, newMeasure; 64 if (measure && newUnit) { 65 value = parseFloat(measure); 66 oldUnit = measure.replace(value.toString(), ""); 67 68 newMeasure = self.convert(value, oldUnit, newUnit); 69 } 70 return newMeasure; 71 }; 72 /** 73 * @param {!string} measure 74 * @return {!string} 75 */ 76 this.getUnits = function (measure) { 77 return measure.substr(measure.length - 2, measure.length); 78 }; 79 }; 80