Move a couple functions into units.js

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1848 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
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

@ -8,6 +8,10 @@
*
*/
// Dependencies:
// 1) units.js
// 2) everything else
svgEditor.addExtension("view_grid", function(s) {
var svgdoc = document.getElementById("svgcanvas").ownerDocument,
@ -78,7 +82,7 @@ svgEditor.addExtension("view_grid", function(s) {
var bgwidth = +canvBG.attr('width');
var bgheight = +canvBG.attr('height');
var units = svgCanvas.getUnits();
var units = svgedit.units.getTypeMap();
var unit = units[svgEditor.curConfig.baseUnit]; // 1 = 1px
var r_intervals = [.01, .1, 1, 10, 100, 1000];

View File

@ -10,6 +10,9 @@
*
*/
// Dependencies:
// 1) svgcanvas.js
(function() {
if(!window.svgEditor) window.svgEditor = function($) {
@ -1816,7 +1819,7 @@
} else if(curConfig.baseUnit !== 'px') {
// Convert unitless value to one with given unit
var unitData = svgCanvas.getUnits();
var unitData = svgedit.units.getTypeMap();
if(selectedElement[attr] || svgCanvas.getMode() === "pathedit" || attr === "x" || attr === "y") {
val *= unitData[curConfig.baseUnit];
@ -4270,7 +4273,7 @@
var c_elem = svgCanvas.getContentElem();
var units = svgCanvas.getUnits();
var units = svgedit.units.getTypeMap();
var unit = units[curConfig.baseUnit]; // 1 = 1px
for(var d = 0; d < 2; d++) {

View File

@ -97,6 +97,16 @@ if(window.opera) {
// config - An object that contains configuration data
$.SvgCanvas = function(container, config)
{
// Default configuration options
var curConfig = {
show_outside_canvas: true,
dimensions: [640, 480]
};
// Update config with new one if given
if(config) {
$.extend(curConfig, config);
}
// import svgtransformlist.js
var getTransformList = this.getTransformList = svgedit.transformlist.getTransformList;
@ -111,8 +121,9 @@ var transformListToTransform = this.transformListToTransform = svgedit.math.tran
var snapToAngle = svgedit.math.snapToAngle;
// import from units.js
svgedit.units.init();
var unit_types = svgedit.units.typeMap;
svgedit.units.init(curConfig);
var unit_types = svgedit.units.getTypeMap();
// import from svgutils.js
var getUrlFromAttr = this.getUrlFromAttr = svgedit.utilities.getUrlFromAttr;
@ -129,7 +140,7 @@ svgedit.utilities.snapToGrid = function(value){
var stepSize = curConfig.snappingStep;
var unit = curConfig.baseUnit;
if(unit !== "px") {
stepSize *= svgedit.units.typeMap[unit];
stepSize *= svgedit.units.typeMap[unit];
}
value = Math.round(value/stepSize)*stepSize;
return value;
@ -219,21 +230,10 @@ var isOpera = svgedit.browsersupport.isOpera,
"exportNoforeignObject": "foreignObject elements will not appear",
"exportNoDashArray": "Strokes will appear filled",
"exportNoText": "Text may not appear as expected"
},
// Default configuration options
curConfig = {
show_outside_canvas: true,
dimensions: [640, 480]
};
var visElems = 'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use';
var ref_attrs = ["clip-path", "fill", "filter", "marker-end", "marker-mid", "marker-start", "mask", "stroke"];
// Update config with new one if given
if(config) {
$.extend(curConfig, config);
}
var visElems = 'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use';
var ref_attrs = ["clip-path", "fill", "filter", "marker-end", "marker-mid", "marker-start", "mask", "stroke"];
var elData = $.data;
@ -339,10 +339,12 @@ $(opac_ani).attr({
// Group: Unit conversion functions
// TODO(codedread): Migrate this into units.js
// Set the scope for these functions
var convertToNum, convertToUnit, setUnitAttr, unitConvertAttrs;
var convertToNum, unitConvertAttrs;
(function() {
// TODO(codedread): Remove these arrays and maps, they are now in units.js.
var w_attrs = ['x', 'x1', 'cx', 'rx', 'width'];
var h_attrs = ['y', 'y1', 'cy', 'ry', 'height'];
var unit_attrs = $.merge(['r','radius'], w_attrs);
@ -391,49 +393,6 @@ var convertToNum, convertToUnit, setUnitAttr, unitConvertAttrs;
return num * unit_types[unit];
}
};
// Function: 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
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 = curConfig.baseUnit;
// } else {
// unit = old_val.substr(-2);
// }
// val = val / unit_types[unit];
// }
//
// val += unit;
// }
}
elem.setAttribute(attr, val);
};
// Function: isValidUnit
// Check if an attribute's value is in a valid format
@ -475,22 +434,6 @@ var convertToNum, convertToUnit, setUnitAttr, unitConvertAttrs;
return valid;
};
// Function: getUnits
// Returns the unit object with values for each unit
canvas.getUnits = function() {
return unit_types;
};
// Function: convertUnit
// Converts the number to given unit or baseUnit
canvas.convertUnit = function(val, unit) {
unit = unit || curConfig.baseUnit;
// baseVal.convertToSpecifiedUnits(unitNumMap[unit]);
// var val = baseVal.valueInSpecifiedUnits;
// baseVal.convertToSpecifiedUnits(1);
return shortFloat(val / unit_types[unit]);
};
// Function: unitConvertAttrs
// Converts all applicable attributes to the given baseUnit
unitConvertAttrs = canvas.unitConvertAttrs = function(element) {
@ -1450,7 +1393,7 @@ var SelectorManager;
// node - DOM element to apply new attribute values to
// attrs - Object with attribute keys/values
// suspendLength - Optional integer of milliseconds to suspend redraw
// unitCheck - Boolean to indicate the need to use setUnitAttr
// unitCheck - Boolean to indicate the need to use svgedit.units.setUnitAttr
var assignAttributes = this.assignAttributes = function(node, attrs, suspendLength, unitCheck) {
if(!suspendLength) suspendLength = 0;
// Opera has a problem with suspendRedraw() apparently
@ -1466,7 +1409,7 @@ var assignAttributes = this.assignAttributes = function(node, attrs, suspendLeng
} else if(!unitCheck) {
node.setAttribute(i, attrs[i]);
} else {
setUnitAttr(node, i, attrs[i]);
svgedit.units.setUnitAttr(node, i, attrs[i]);
}
}
@ -1730,6 +1673,10 @@ var shortFloat = function(val) {
return parseFloat(val).toFixed(digits) - 0;
}
}
this.convertUnit = function(val, unit) {
return shortFloat(svgedit.units.convertUnit(val, unit));
};
// This method rounds the incoming value to the nearest value based on the current_zoom
var round = this.round = function(val) {
@ -7313,8 +7260,8 @@ var svgToString = this.svgToString = function(elem, indent) {
// }
if(unit !== "px") {
res.w = canvas.convertUnit(res.w, unit) + unit;
res.h = canvas.convertUnit(res.h, unit) + unit;
res.w = shortFloat(svgedit.units.convertUnit(res.w, unit)) + unit;
res.h = shortFloat(svgedit.units.convertUnit(res.h, unit)) + unit;
}
out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="'+svgns+'"');

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);
};
})();