Move a couple functions into units.js
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1848 eee81c28-f429-11dd-99c0-75d572ba1dddmaster
parent
09a6a7c307
commit
e64bee32a8
|
@ -8,6 +8,10 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Dependencies:
|
||||||
|
// 1) units.js
|
||||||
|
// 2) everything else
|
||||||
|
|
||||||
svgEditor.addExtension("view_grid", function(s) {
|
svgEditor.addExtension("view_grid", function(s) {
|
||||||
|
|
||||||
var svgdoc = document.getElementById("svgcanvas").ownerDocument,
|
var svgdoc = document.getElementById("svgcanvas").ownerDocument,
|
||||||
|
@ -78,7 +82,7 @@ svgEditor.addExtension("view_grid", function(s) {
|
||||||
var bgwidth = +canvBG.attr('width');
|
var bgwidth = +canvBG.attr('width');
|
||||||
var bgheight = +canvBG.attr('height');
|
var bgheight = +canvBG.attr('height');
|
||||||
|
|
||||||
var units = svgCanvas.getUnits();
|
var units = svgedit.units.getTypeMap();
|
||||||
var unit = units[svgEditor.curConfig.baseUnit]; // 1 = 1px
|
var unit = units[svgEditor.curConfig.baseUnit]; // 1 = 1px
|
||||||
var r_intervals = [.01, .1, 1, 10, 100, 1000];
|
var r_intervals = [.01, .1, 1, 10, 100, 1000];
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Dependencies:
|
||||||
|
// 1) svgcanvas.js
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
if(!window.svgEditor) window.svgEditor = function($) {
|
if(!window.svgEditor) window.svgEditor = function($) {
|
||||||
|
@ -1816,7 +1819,7 @@
|
||||||
} else if(curConfig.baseUnit !== 'px') {
|
} else if(curConfig.baseUnit !== 'px') {
|
||||||
// Convert unitless value to one with given unit
|
// 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") {
|
if(selectedElement[attr] || svgCanvas.getMode() === "pathedit" || attr === "x" || attr === "y") {
|
||||||
val *= unitData[curConfig.baseUnit];
|
val *= unitData[curConfig.baseUnit];
|
||||||
|
@ -4270,7 +4273,7 @@
|
||||||
|
|
||||||
var c_elem = svgCanvas.getContentElem();
|
var c_elem = svgCanvas.getContentElem();
|
||||||
|
|
||||||
var units = svgCanvas.getUnits();
|
var units = svgedit.units.getTypeMap();
|
||||||
var unit = units[curConfig.baseUnit]; // 1 = 1px
|
var unit = units[curConfig.baseUnit]; // 1 = 1px
|
||||||
|
|
||||||
for(var d = 0; d < 2; d++) {
|
for(var d = 0; d < 2; d++) {
|
||||||
|
|
|
@ -97,6 +97,16 @@ if(window.opera) {
|
||||||
// config - An object that contains configuration data
|
// config - An object that contains configuration data
|
||||||
$.SvgCanvas = function(container, config)
|
$.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
|
// import svgtransformlist.js
|
||||||
var getTransformList = this.getTransformList = svgedit.transformlist.getTransformList;
|
var getTransformList = this.getTransformList = svgedit.transformlist.getTransformList;
|
||||||
|
@ -111,8 +121,9 @@ var transformListToTransform = this.transformListToTransform = svgedit.math.tran
|
||||||
var snapToAngle = svgedit.math.snapToAngle;
|
var snapToAngle = svgedit.math.snapToAngle;
|
||||||
|
|
||||||
// import from units.js
|
// import from units.js
|
||||||
svgedit.units.init();
|
svgedit.units.init(curConfig);
|
||||||
var unit_types = svgedit.units.typeMap;
|
var unit_types = svgedit.units.getTypeMap();
|
||||||
|
|
||||||
|
|
||||||
// import from svgutils.js
|
// import from svgutils.js
|
||||||
var getUrlFromAttr = this.getUrlFromAttr = svgedit.utilities.getUrlFromAttr;
|
var getUrlFromAttr = this.getUrlFromAttr = svgedit.utilities.getUrlFromAttr;
|
||||||
|
@ -219,21 +230,10 @@ var isOpera = svgedit.browsersupport.isOpera,
|
||||||
"exportNoforeignObject": "foreignObject elements will not appear",
|
"exportNoforeignObject": "foreignObject elements will not appear",
|
||||||
"exportNoDashArray": "Strokes will appear filled",
|
"exportNoDashArray": "Strokes will appear filled",
|
||||||
"exportNoText": "Text may not appear as expected"
|
"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 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 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 elData = $.data;
|
var elData = $.data;
|
||||||
|
|
||||||
|
@ -339,10 +339,12 @@ $(opac_ani).attr({
|
||||||
|
|
||||||
// Group: Unit conversion functions
|
// Group: Unit conversion functions
|
||||||
|
|
||||||
|
// TODO(codedread): Migrate this into units.js
|
||||||
// Set the scope for these functions
|
// Set the scope for these functions
|
||||||
var convertToNum, convertToUnit, setUnitAttr, unitConvertAttrs;
|
var convertToNum, unitConvertAttrs;
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
// TODO(codedread): Remove these arrays and maps, they are now in units.js.
|
||||||
var w_attrs = ['x', 'x1', 'cx', 'rx', 'width'];
|
var w_attrs = ['x', 'x1', 'cx', 'rx', 'width'];
|
||||||
var h_attrs = ['y', 'y1', 'cy', 'ry', 'height'];
|
var h_attrs = ['y', 'y1', 'cy', 'ry', 'height'];
|
||||||
var unit_attrs = $.merge(['r','radius'], w_attrs);
|
var unit_attrs = $.merge(['r','radius'], w_attrs);
|
||||||
|
@ -392,49 +394,6 @@ var convertToNum, convertToUnit, setUnitAttr, unitConvertAttrs;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// 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
|
// Function: isValidUnit
|
||||||
// Check if an attribute's value is in a valid format
|
// Check if an attribute's value is in a valid format
|
||||||
//
|
//
|
||||||
|
@ -475,22 +434,6 @@ var convertToNum, convertToUnit, setUnitAttr, unitConvertAttrs;
|
||||||
return valid;
|
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
|
// Function: unitConvertAttrs
|
||||||
// Converts all applicable attributes to the given baseUnit
|
// Converts all applicable attributes to the given baseUnit
|
||||||
unitConvertAttrs = canvas.unitConvertAttrs = function(element) {
|
unitConvertAttrs = canvas.unitConvertAttrs = function(element) {
|
||||||
|
@ -1450,7 +1393,7 @@ var SelectorManager;
|
||||||
// node - DOM element to apply new attribute values to
|
// node - DOM element to apply new attribute values to
|
||||||
// attrs - Object with attribute keys/values
|
// attrs - Object with attribute keys/values
|
||||||
// suspendLength - Optional integer of milliseconds to suspend redraw
|
// 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) {
|
var assignAttributes = this.assignAttributes = function(node, attrs, suspendLength, unitCheck) {
|
||||||
if(!suspendLength) suspendLength = 0;
|
if(!suspendLength) suspendLength = 0;
|
||||||
// Opera has a problem with suspendRedraw() apparently
|
// Opera has a problem with suspendRedraw() apparently
|
||||||
|
@ -1466,7 +1409,7 @@ var assignAttributes = this.assignAttributes = function(node, attrs, suspendLeng
|
||||||
} else if(!unitCheck) {
|
} else if(!unitCheck) {
|
||||||
node.setAttribute(i, attrs[i]);
|
node.setAttribute(i, attrs[i]);
|
||||||
} else {
|
} else {
|
||||||
setUnitAttr(node, i, attrs[i]);
|
svgedit.units.setUnitAttr(node, i, attrs[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1731,6 +1674,10 @@ var shortFloat = function(val) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// This method rounds the incoming value to the nearest value based on the current_zoom
|
||||||
var round = this.round = function(val) {
|
var round = this.round = function(val) {
|
||||||
return parseInt(val*current_zoom)/current_zoom;
|
return parseInt(val*current_zoom)/current_zoom;
|
||||||
|
@ -7313,8 +7260,8 @@ var svgToString = this.svgToString = function(elem, indent) {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if(unit !== "px") {
|
if(unit !== "px") {
|
||||||
res.w = canvas.convertUnit(res.w, unit) + unit;
|
res.w = shortFloat(svgedit.units.convertUnit(res.w, unit)) + unit;
|
||||||
res.h = canvas.convertUnit(res.h, unit) + unit;
|
res.h = shortFloat(svgedit.units.convertUnit(res.h, unit)) + unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="'+svgns+'"');
|
out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="'+svgns+'"');
|
||||||
|
|
|
@ -38,15 +38,24 @@ var unitNumMap = {
|
||||||
|
|
||||||
$.merge(unit_attrs, h_attrs);
|
$.merge(unit_attrs, h_attrs);
|
||||||
|
|
||||||
|
// Read-only copy of configuration options.
|
||||||
|
svgedit.units.config_;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores mapping of unit type to user coordinates.
|
* 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.
|
* 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';
|
var svgns = 'http://www.w3.org/2000/svg';
|
||||||
|
|
||||||
// Get correct em/ex values by creating a temporary SVG.
|
// Get correct em/ex values by creating a temporary SVG.
|
||||||
|
@ -61,14 +70,72 @@ svgedit.units.init = function() {
|
||||||
document.body.removeChild(svg);
|
document.body.removeChild(svg);
|
||||||
|
|
||||||
var inch = bb.x;
|
var inch = bb.x;
|
||||||
svgedit.units.typeMap['em'] = bb.width;
|
svgedit.units.typeMap_['em'] = bb.width;
|
||||||
svgedit.units.typeMap['ex'] = bb.height;
|
svgedit.units.typeMap_['ex'] = bb.height;
|
||||||
svgedit.units.typeMap['in'] = inch;
|
svgedit.units.typeMap_['in'] = inch;
|
||||||
svgedit.units.typeMap['cm'] = inch / 2.54;
|
svgedit.units.typeMap_['cm'] = inch / 2.54;
|
||||||
svgedit.units.typeMap['mm'] = inch / 25.4;
|
svgedit.units.typeMap_['mm'] = inch / 25.4;
|
||||||
svgedit.units.typeMap['pt'] = inch / 72;
|
svgedit.units.typeMap_['pt'] = inch / 72;
|
||||||
svgedit.units.typeMap['pc'] = inch / 6;
|
svgedit.units.typeMap_['pc'] = inch / 6;
|
||||||
svgedit.units.typeMap['%'] = 0;
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
Loading…
Reference in New Issue