Move a couple functions into units.js

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1848 eee81c28-f429-11dd-99c0-75d572ba1ddd
This commit is contained in:
Jeff Schiller
2010-11-06 00:45:21 +00:00
parent 09a6a7c307
commit e64bee32a8
4 changed files with 115 additions and 94 deletions

View File

@@ -38,15 +38,24 @@ var unitNumMap = {
$.merge(unit_attrs, h_attrs);
// Read-only copy of configuration options.
svgedit.units.config_;
/**
* Stores mapping of unit type to user coordinates.
*/
svgedit.units.typeMap = {px: 1};
svgedit.units.typeMap_ = {px: 1};
/**
* Function: svgedit.units.init()
* Initializes this module.
*
* Parameters:
* config - an object containing configuration options.
*/
svgedit.units.init = function() {
svgedit.units.init = function(config) {
svgedit.units.config_ = config;
var svgns = 'http://www.w3.org/2000/svg';
// Get correct em/ex values by creating a temporary SVG.
@@ -61,14 +70,72 @@ svgedit.units.init = function() {
document.body.removeChild(svg);
var inch = bb.x;
svgedit.units.typeMap['em'] = bb.width;
svgedit.units.typeMap['ex'] = bb.height;
svgedit.units.typeMap['in'] = inch;
svgedit.units.typeMap['cm'] = inch / 2.54;
svgedit.units.typeMap['mm'] = inch / 25.4;
svgedit.units.typeMap['pt'] = inch / 72;
svgedit.units.typeMap['pc'] = inch / 6;
svgedit.units.typeMap['%'] = 0;
svgedit.units.typeMap_['em'] = bb.width;
svgedit.units.typeMap_['ex'] = bb.height;
svgedit.units.typeMap_['in'] = inch;
svgedit.units.typeMap_['cm'] = inch / 2.54;
svgedit.units.typeMap_['mm'] = inch / 25.4;
svgedit.units.typeMap_['pt'] = inch / 72;
svgedit.units.typeMap_['pc'] = inch / 6;
svgedit.units.typeMap_['%'] = 0;
};
// Function: svgedit.units.getTypeMap
// Returns the unit object with values for each unit
svgedit.units.getTypeMap = function() {
return svgedit.units.typeMap_;
};
// Function: svgedit.units.convertUnit
// Converts the number to given unit or baseUnit
svgedit.units.convertUnit = function(val, unit) {
unit = unit || svgedit.units.config_.baseUnit;
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
// var val = baseVal.valueInSpecifiedUnits;
// baseVal.convertToSpecifiedUnits(1);
return val / svgedit.units.typeMap_[unit];
};
// Function: svgedit.units.setUnitAttr
// Sets an element's attribute based on the unit in its current value.
//
// Parameters:
// elem - DOM element to be changed
// attr - String with the name of the attribute associated with the value
// val - String with the attribute value to convert
svgedit.units.setUnitAttr = function(elem, attr, val) {
if(!isNaN(val)) {
// New value is a number, so check currently used unit
var old_val = elem.getAttribute(attr);
// Enable this for alternate mode
// if(old_val !== null && (isNaN(old_val) || curConfig.baseUnit !== 'px')) {
// // Old value was a number, so get unit, then convert
// var unit;
// if(old_val.substr(-1) === '%') {
// var res = getResolution();
// unit = '%';
// val *= 100;
// if(w_attrs.indexOf(attr) >= 0) {
// val = val / res.w;
// } else if(h_attrs.indexOf(attr) >= 0) {
// val = val / res.h;
// } else {
// return val / Math.sqrt((res.w*res.w) + (res.h*res.h))/Math.sqrt(2);
// }
// } else {
// if(curConfig.baseUnit !== 'px') {
// unit = svgedit.units.config_.baseUnit;
// } else {
// unit = old_val.substr(-2);
// }
// val = val / svgedit.units.typeMap_[unit];
// }
//
// val += unit;
// }
}
elem.setAttribute(attr, val);
};
})();