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.f *= current_zoom;
if (!bbox)
if (!bbox) {
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 doesn't exclude 'gsvg' and calls getStrokedBBox for any 'g'. Should getBBox be updated?
if (tagName === 'g' && !$.data(selected, 'gsvg')) {

View File

@ -6455,6 +6455,8 @@ this.convertToPath = function(elem, getBBox) {
if (getBBox) {
return svgedit.utilities.getBBoxOfElementAsPath(elem, addSvgElementFromJson, pathActions)
} 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 = {
'fill': cur_shape.fill,
'fill-opacity': cur_shape.fill_opacity,
@ -6467,7 +6469,7 @@ this.convertToPath = function(elem, getBBox) {
'opacity': cur_shape.opacity,
'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,21 +529,38 @@ svgedit.utilities.getBBox = function(elem) {
return ret;
};
// 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 = '';
$.each(pathSegments, function(j, seg) {
var i;
var l = seg[0], pts = seg[1];
d += l;
var pts = seg[1];
d += seg[0];
for (i = 0; i < pts.length; i+=2) {
d += (pts[i] +','+pts[i+1]) + ' ';
}
});
return d
}
return d;
};
// 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
@ -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: 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 a = elem.getAttribute(this);
if (a) {
attrs[this] = a;
}
});
return attrs;
};
// Function: getBBoxOfElementAsPath
@ -638,21 +666,16 @@ svgedit.utilities.addAttributesForConvertToPath = function( elem, attrs) {
//
// Parameters:
// 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.
//
// Returns:
// The resulting path's bounding box object.
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({
'element': 'path',
'attr': attrs
'attr': svgedit.utilities.getExtraAttributesForConvertToPath(elem)
});
var eltrans = elem.getAttribute('transform');
@ -660,7 +683,6 @@ svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson,
path.setAttribute('transform', eltrans);
}
var id = elem.id;
var parent = elem.parentNode;
if (elem.nextSibling) {
parent.insertBefore(path, elem);
@ -684,7 +706,7 @@ svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson,
}
path.parentNode.removeChild(path);
return bb;
}
};
// Function: convertToPath
// Convert selected element to a path.
@ -692,7 +714,7 @@ svgedit.utilities.getBBoxOfElementAsPath = function(elem, addSvgElementFromJson,
// Parameters:
// elem - The DOM element to be converted
// 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.
// clearSelection - see canvas.clearSelection
// 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');
// any attribute on the element not covered by the above
// TODO: make this list global so that we can properly maintain it
// 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);
}
});
// Any attribute on the element not covered by the passed-in attributes
attrs = $.extend({}, attrs, svgedit.utilities.getExtraAttributesForConvertToPath(elem));
var path = addSvgElementFromJson({
'element': 'path',
@ -788,7 +804,7 @@ svgedit.utilities.getBBoxWithTransform = function(elem, addSvgElementFromJson, p
return null;
}
var tlist = svgedit.transformlist.getTransformList(elem)
var tlist = svgedit.transformlist.getTransformList(elem);
var angle = svgedit.utilities.getRotationAngleFromTransformList(tlist);
if (angle || svgedit.math.hasMatrixTransform(tlist)) {