Updates from pull request code review. Refactored getExtraAttributesForConvertToPath.

Updated all formatting requests.
Refactored and renamed addAttributesForConvertToPath to
getExtraAttributesForConvertToPath. Now called from
getBBoxOfElementAsPath and convertToPath.
master
Flint O'Brien 2016-04-26 16:01:39 -04:00
parent 17c3e0fa28
commit 7db3b22c58
3 changed files with 70 additions and 51 deletions

View File

@ -165,8 +165,9 @@ svgedit.select.Selector.prototype.resize = function(bbox) {
m.e *= current_zoom; m.e *= current_zoom;
m.f *= current_zoom; m.f *= current_zoom;
if (!bbox) if (!bbox) {
bbox = svgedit.utilities.getBBox(selected); bbox = svgedit.utilities.getBBox(selected);
}
// TODO: svgedit.utilities.getBBox (previous line) already knows to call getStrokedBBox when tagName === 'g'. Remove this? // TODO: svgedit.utilities.getBBox (previous line) already knows to call getStrokedBBox when tagName === 'g'. Remove this?
// TODO: svgedit.utilities.getBBox doesn't exclude 'gsvg' and calls getStrokedBBox for any 'g'. Should getBBox be updated? // TODO: svgedit.utilities.getBBox doesn't exclude 'gsvg' and calls getStrokedBBox for any 'g'. Should getBBox be updated?
if (tagName === 'g' && !$.data(selected, 'gsvg')) { if (tagName === 'g' && !$.data(selected, 'gsvg')) {

View File

@ -603,7 +603,7 @@ var getIntersectionList = this.getIntersectionList = function(rect) {
// A single bounding box object // A single bounding box object
var getStrokedBBox = this.getStrokedBBox = function(elems) { var getStrokedBBox = this.getStrokedBBox = function(elems) {
if (!elems) {elems = getVisibleElements();} if (!elems) {elems = getVisibleElements();}
return svgedit.utilities.getStrokedBBox( elems, addSvgElementFromJson, pathActions) return svgedit.utilities.getStrokedBBox(elems, addSvgElementFromJson, pathActions)
}; };
// Function: getVisibleElements // Function: getVisibleElements
@ -6452,22 +6452,24 @@ this.convertToPath = function(elem, getBBox) {
}); });
return; return;
} }
if( getBBox) { if (getBBox) {
return svgedit.utilities.getBBoxOfElementAsPath( elem, addSvgElementFromJson, pathActions) return svgedit.utilities.getBBoxOfElementAsPath(elem, addSvgElementFromJson, pathActions)
} else { } else {
// TODO: Why is this applying attributes from cur_shape, then inside utilities.convertToPath it's pulling addition attributes from elem?
// TODO: If convertToPath is called with one elem, cur_shape and elem are probably the same; but calling with multiple is a bug or cool feature.
var attrs = { var attrs = {
'fill': cur_shape.fill, 'fill': cur_shape.fill,
'fill-opacity': cur_shape.fill_opacity, 'fill-opacity': cur_shape.fill_opacity,
'stroke': cur_shape.stroke, 'stroke': cur_shape.stroke,
'stroke-width': cur_shape.stroke_width, 'stroke-width': cur_shape.stroke_width,
'stroke-dasharray': cur_shape.stroke_dasharray, 'stroke-dasharray': cur_shape.stroke_dasharray,
'stroke-linejoin': cur_shape.stroke_linejoin, 'stroke-linejoin': cur_shape.stroke_linejoin,
'stroke-linecap': cur_shape.stroke_linecap, 'stroke-linecap': cur_shape.stroke_linecap,
'stroke-opacity': cur_shape.stroke_opacity, 'stroke-opacity': cur_shape.stroke_opacity,
'opacity': cur_shape.opacity, 'opacity': cur_shape.opacity,
'visibility':'hidden' 'visibility':'hidden'
}; };
return svgedit.utilities.convertToPath( elem, attrs, addSvgElementFromJson, pathActions, clearSelection, addToSelection, svgedit.history, addCommandToHistory) return svgedit.utilities.convertToPath(elem, attrs, addSvgElementFromJson, pathActions, clearSelection, addToSelection, svgedit.history, addCommandToHistory);
} }
}; };

View File

@ -529,22 +529,39 @@ svgedit.utilities.getBBox = function(elem) {
return ret; return ret;
}; };
svgedit.utilities.getPathDFromSegments = function( pathSegments) { // Function: getPathDFromSegments
// Create a path 'd' attribute from path segments.
// Each segment is an array of the form: [singleChar, [x,y, x,y, ...]]
//
// Parameters:
// pathSegments - An array of path segments to be converted
//
// Returns:
// The converted path d attribute.
svgedit.utilities.getPathDFromSegments = function(pathSegments) {
var d = ''; var d = '';
$.each(pathSegments, function(j, seg) { $.each(pathSegments, function(j, seg) {
var i; var i;
var l = seg[0], pts = seg[1]; var pts = seg[1];
d += l; d += seg[0];
for (i = 0; i < pts.length; i+=2) { for (i = 0; i < pts.length; i+=2) {
d += (pts[i] +','+pts[i+1]) + ' '; d += (pts[i] +','+pts[i+1]) + ' ';
} }
}); });
return d return d;
} };
svgedit.utilities.getPathDFromElement = function( elem) { // Function: getPathDFromElement
// Make a path 'd' attribute from a simple SVG element shape.
//
// Parameters:
// elem - The element to be converted
//
// Returns:
// The path d attribute or undefined if the element type is unknown.
svgedit.utilities.getPathDFromElement = function(elem) {
// Possibly the cubed root of 6, but 1.81 works best // Possibly the cubed root of 6, but 1.81 works best
var num = 1.81; var num = 1.81;
@ -623,14 +640,25 @@ svgedit.utilities.getPathDFromElement = function( elem) {
}; };
svgedit.utilities.addAttributesForConvertToPath = function( elem, attrs) { // Function: getExtraAttributesForConvertToPath
// Get a set of attributes from an element that is useful for convertToPath.
//
// Parameters:
// elem - The element to be probed
//
// Returns:
// An object with attributes.
svgedit.utilities.getExtraAttributesForConvertToPath = function(elem) {
var attrs = {} ;
// TODO: make this list global so that we can properly maintain it // TODO: make this list global so that we can properly maintain it
// TODO: what about @transform, @clip-rule, @fill-rule, etc? // TODO: what about @transform, @clip-rule, @fill-rule, etc?
$.each(['marker-start', 'marker-end', 'marker-mid', 'filter', 'clip-path'], function() { $.each(['marker-start', 'marker-end', 'marker-mid', 'filter', 'clip-path'], function() {
if (elem.getAttribute(this)) { var a = elem.getAttribute(this);
attrs[this] = elem.getAttribute(this); if (a) {
attrs[this] = a;
} }
}); });
return attrs;
}; };
// Function: getBBoxOfElementAsPath // Function: getBBoxOfElementAsPath
@ -638,21 +666,16 @@ svgedit.utilities.addAttributesForConvertToPath = function( elem, attrs) {
// //
// Parameters: // Parameters:
// elem - The DOM element to be probed // elem - The DOM element to be probed
// addSvgElementFromJson - Function to add the path element to the current layer. See canvas.addSvgElementFromJso // addSvgElementFromJson - Function to add the path element to the current layer. See canvas.addSvgElementFromJson
// pathActions - If a transform exists, pathActions.resetOrientation() is used. See: canvas.pathActions. // pathActions - If a transform exists, pathActions.resetOrientation() is used. See: canvas.pathActions.
// //
// Returns: // Returns:
// The resulting path's bounding box object. // The resulting path's bounding box object.
svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson, pathActions) { svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson, pathActions) {
var attrs = {}
// any attribute on the element not covered by the above
svgedit.utilities.addAttributesForConvertToPath( elem, attrs)
var path = addSvgElementFromJson({ var path = addSvgElementFromJson({
'element': 'path', 'element': 'path',
'attr': attrs 'attr': svgedit.utilities.getExtraAttributesForConvertToPath(elem)
}); });
var eltrans = elem.getAttribute('transform'); var eltrans = elem.getAttribute('transform');
@ -660,7 +683,6 @@ svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson,
path.setAttribute('transform', eltrans); path.setAttribute('transform', eltrans);
} }
var id = elem.id;
var parent = elem.parentNode; var parent = elem.parentNode;
if (elem.nextSibling) { if (elem.nextSibling) {
parent.insertBefore(path, elem); parent.insertBefore(path, elem);
@ -668,8 +690,8 @@ svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson,
parent.appendChild(path); parent.appendChild(path);
} }
var d = svgedit.utilities.getPathDFromElement( elem); var d = svgedit.utilities.getPathDFromElement(elem);
if( d) if (d)
path.setAttribute('d', d); path.setAttribute('d', d);
else else
path.parentNode.removeChild(path); path.parentNode.removeChild(path);
@ -684,7 +706,7 @@ svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson,
} }
path.parentNode.removeChild(path); path.parentNode.removeChild(path);
return bb; return bb;
} };
// Function: convertToPath // Function: convertToPath
// Convert selected element to a path. // Convert selected element to a path.
@ -692,7 +714,7 @@ svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson,
// Parameters: // Parameters:
// elem - The DOM element to be converted // elem - The DOM element to be converted
// attrs - Apply attributes to new path. see canvas.convertToPath // attrs - Apply attributes to new path. see canvas.convertToPath
// addSvgElementFromJson - Function to add the path element to the current layer. See canvas.addSvgElementFromJso // addSvgElementFromJson - Function to add the path element to the current layer. See canvas.addSvgElementFromJson
// pathActions - If a transform exists, pathActions.resetOrientation() is used. See: canvas.pathActions. // pathActions - If a transform exists, pathActions.resetOrientation() is used. See: canvas.pathActions.
// clearSelection - see canvas.clearSelection // clearSelection - see canvas.clearSelection
// addToSelection - see canvas.addToSelection // addToSelection - see canvas.addToSelection
@ -705,14 +727,8 @@ svgedit.utilities.convertToPath = function(elem, attrs, addSvgElementFromJson, p
var batchCmd = new history.BatchCommand('Convert element to Path'); var batchCmd = new history.BatchCommand('Convert element to Path');
// any attribute on the element not covered by the above // Any attribute on the element not covered by the passed-in attributes
// TODO: make this list global so that we can properly maintain it attrs = $.extend({}, attrs, svgedit.utilities.getExtraAttributesForConvertToPath(elem));
// TODO: what about @transform, @clip-rule, @fill-rule, etc?
$.each(['marker-start', 'marker-end', 'marker-mid', 'filter', 'clip-path'], function() {
if (elem.getAttribute(this)) {
attrs[this] = elem.getAttribute(this);
}
});
var path = addSvgElementFromJson({ var path = addSvgElementFromJson({
'element': 'path', 'element': 'path',
@ -732,8 +748,8 @@ svgedit.utilities.convertToPath = function(elem, attrs, addSvgElementFromJson, p
parent.appendChild(path); parent.appendChild(path);
} }
var d = svgedit.utilities.getPathDFromElement( elem); var d = svgedit.utilities.getPathDFromElement(elem);
if( d) { if (d) {
path.setAttribute('d', d); path.setAttribute('d', d);
// Replace the current element with the converted one // Replace the current element with the converted one
@ -788,7 +804,7 @@ svgedit.utilities.getBBoxWithTransform = function(elem, addSvgElementFromJson, p
return null; return null;
} }
var tlist = svgedit.transformlist.getTransformList(elem) var tlist = svgedit.transformlist.getTransformList(elem);
var angle = svgedit.utilities.getRotationAngleFromTransformList(tlist); var angle = svgedit.utilities.getRotationAngleFromTransformList(tlist);
if (angle || svgedit.math.hasMatrixTransform(tlist)) { if (angle || svgedit.math.hasMatrixTransform(tlist)) {
@ -810,7 +826,7 @@ svgedit.utilities.getBBoxWithTransform = function(elem, addSvgElementFromJson, p
if (!good_bb) { if (!good_bb) {
var matrix = svgedit.math.transformListToTransform( tlist).matrix; var matrix = svgedit.math.transformListToTransform(tlist).matrix;
bb = svgedit.math.transformBox(bb.x, bb.y, bb.width, bb.height, matrix).aabox; bb = svgedit.math.transformBox(bb.x, bb.y, bb.width, bb.height, matrix).aabox;
// Old technique that was exceedingly slow with large documents. // Old technique that was exceedingly slow with large documents.
@ -870,7 +886,7 @@ svgedit.utilities.getStrokedBBox = function(elems, addSvgElementFromJson, pathAc
var min_y = full_bb.y; var min_y = full_bb.y;
// If only one elem, don't call the potentially slow getBBoxWithTransform method again. // If only one elem, don't call the potentially slow getBBoxWithTransform method again.
if( elems.length === 1) { if (elems.length === 1) {
var offset = getStrokeOffsetForBBox(elems[0]); var offset = getStrokeOffsetForBBox(elems[0]);
min_x -= offset; min_x -= offset;
min_y -= offset; min_y -= offset;
@ -910,7 +926,7 @@ svgedit.utilities.getStrokedBBox = function(elems, addSvgElementFromJson, pathAc
// Returns: // Returns:
// Float with the angle in degrees or radians // Float with the angle in degrees or radians
svgedit.utilities.getRotationAngleFromTransformList = function(tlist, to_rad) { svgedit.utilities.getRotationAngleFromTransformList = function(tlist, to_rad) {
if(!tlist) {return 0;} // <svg> elements have no tlist if (!tlist) {return 0;} // <svg> elements have no tlist
var N = tlist.numberOfItems; var N = tlist.numberOfItems;
var i; var i;
for (i = 0; i < N; ++i) { for (i = 0; i < N; ++i) {