mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
move or just leave transform attr from Group to the inner Path Elems (close #86)
This commit is contained in:
12
.svgo.yml
12
.svgo.yml
@ -103,6 +103,14 @@ plugins:
|
|||||||
tspan: true
|
tspan: true
|
||||||
tref: true
|
tref: true
|
||||||
|
|
||||||
|
- name: moveElemsAttrsToGroup
|
||||||
|
active: true
|
||||||
|
type: perItemReverse
|
||||||
|
|
||||||
|
- name: moveGroupAttrsToElems
|
||||||
|
active: true
|
||||||
|
type: perItemReverse
|
||||||
|
|
||||||
- name: convertPathData
|
- name: convertPathData
|
||||||
active: true
|
active: true
|
||||||
type: perItem
|
type: perItem
|
||||||
@ -117,10 +125,6 @@ plugins:
|
|||||||
leadingZero: true
|
leadingZero: true
|
||||||
negativeExtraSpace: true
|
negativeExtraSpace: true
|
||||||
|
|
||||||
- name: moveElemsAttrsToGroup
|
|
||||||
active: true
|
|
||||||
type: perItemReverse
|
|
||||||
|
|
||||||
- name: convertTransform
|
- name: convertTransform
|
||||||
active: true
|
active: true
|
||||||
type: perItem
|
type: perItem
|
||||||
|
@ -13,6 +13,8 @@ exports.elemsGroups = {
|
|||||||
filterPrimitive: ['feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feFlood', 'feGaussianBlur', 'feImage', 'feMerge', 'feMorphology', 'feOffset', 'feSpecularLighting', 'feTile', 'feTurbulence']
|
filterPrimitive: ['feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feFlood', 'feGaussianBlur', 'feImage', 'feMerge', 'feMorphology', 'feOffset', 'feSpecularLighting', 'feTile', 'feTurbulence']
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.pathElems = ['path', 'glyph', 'missing-glyph'];
|
||||||
|
|
||||||
// var defaults = exports.defaults = {
|
// var defaults = exports.defaults = {
|
||||||
// 'externalResourcesRequired': 'false',
|
// 'externalResourcesRequired': 'false',
|
||||||
// 'xlink:type': 'simple'
|
// 'xlink:type': 'simple'
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
var cleanupOutData = require('../lib/svgo/tools').cleanupOutData,
|
var cleanupOutData = require('../lib/svgo/tools').cleanupOutData,
|
||||||
regPathInstructions = /([MmLlHhVvCcSsQqTtAaZz])\s*/,
|
regPathInstructions = /([MmLlHhVvCcSsQqTtAaZz])\s*/,
|
||||||
regPathData = /[\-+]?\d*\.?\d+([eE][\-+]?\d+)?/g,
|
regPathData = /[\-+]?\d*\.?\d+([eE][\-+]?\d+)?/g,
|
||||||
pathElems = ['path', 'glyph', 'missing-glyph'],
|
pathElems = require('./_collections.js').pathElems,
|
||||||
hasMarkerMid,
|
hasMarkerMid,
|
||||||
transform2js = require('./_transforms.js').transform2js,
|
transform2js = require('./_transforms.js').transform2js,
|
||||||
transformsMultiply = require('./_transforms.js').transformsMultiply;
|
transformsMultiply = require('./_transforms.js').transformsMultiply;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var inheritableAttrs = require('./_collections').inheritableAttrs;
|
var inheritableAttrs = require('./_collections').inheritableAttrs,
|
||||||
|
pathElems = require('./_collections.js').pathElems;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collapse content's intersected and inheritable
|
* Collapse content's intersected and inheritable
|
||||||
@ -32,34 +33,47 @@ exports.moveElemsAttrsToGroup = function(item) {
|
|||||||
|
|
||||||
var intersection = {},
|
var intersection = {},
|
||||||
hasTransform = false,
|
hasTransform = false,
|
||||||
every = item.content.every(function(g) {
|
intersected = item.content.every(function(inner) {
|
||||||
if (g.elem && g.attrs) {
|
if (inner.isElem() && inner.hasAttr()) {
|
||||||
if (!Object.keys(intersection).length) {
|
if (!Object.keys(intersection).length) {
|
||||||
intersection = g.attrs;
|
intersection = inner.attrs;
|
||||||
} else {
|
} else {
|
||||||
intersection = intersectInheritableAttrs(intersection, g.attrs);
|
intersection = intersectInheritableAttrs(intersection, inner.attrs);
|
||||||
|
|
||||||
if (!intersection) return false;
|
if (!intersection) return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
allPath = item.content.every(function(inner) {
|
||||||
|
return inner.isElem(pathElems);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (every) {
|
if (intersected) {
|
||||||
|
|
||||||
item.content.forEach(function(g) {
|
item.content.forEach(function(g) {
|
||||||
|
|
||||||
for (var name in intersection) {
|
for (var name in intersection) {
|
||||||
g.removeAttr(name);
|
|
||||||
|
|
||||||
if (name === 'transform') {
|
if (!allPath || name !== 'transform') {
|
||||||
if (!hasTransform && item.hasAttr('transform')) {
|
|
||||||
item.attr('transform').value += ' ' + intersection[name].value;
|
g.removeAttr(name);
|
||||||
hasTransform = true;
|
|
||||||
|
if (name === 'transform') {
|
||||||
|
if (!hasTransform) {
|
||||||
|
if (item.hasAttr('transform')) {
|
||||||
|
item.attr('transform').value += ' ' + intersection[name].value;
|
||||||
|
} else {
|
||||||
|
item.addAttr(intersection[name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
hasTransform = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
item.addAttr(intersection[name]);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
item.addAttr(intersection[name]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
46
plugins/moveGroupAttrsToElems.js
Normal file
46
plugins/moveGroupAttrsToElems.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var pathElems = require('./_collections.js').pathElems;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move group attrs to the content elements.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* <g transform="scale(2)">
|
||||||
|
* <path transform="rotate(45)" d="M0,0 L10,20"/>
|
||||||
|
* <path transform="translate(10, 20)" d="M0,10 L20,30"/>
|
||||||
|
* </g>
|
||||||
|
* ⬇
|
||||||
|
* <g>
|
||||||
|
* <path transform="scale(2) rotate(45)" d="M0,0 L10,20"/>
|
||||||
|
* <path transform="scale(2) translate(10, 20)" d="M0,10 L20,30"/>
|
||||||
|
* </g>
|
||||||
|
*
|
||||||
|
* @param {Object} item current iteration item
|
||||||
|
* @return {Boolean} if false, item will be filtered out
|
||||||
|
*
|
||||||
|
* @author Kir Belevich
|
||||||
|
*/
|
||||||
|
exports.moveGroupAttrsToElems = function(item) {
|
||||||
|
|
||||||
|
// move group transform attr to content's pathElems
|
||||||
|
if (
|
||||||
|
item.isElem('g') &&
|
||||||
|
item.hasAttr('transform') &&
|
||||||
|
!item.isEmpty() &&
|
||||||
|
item.content.every(function(inner) {
|
||||||
|
return inner.isElem(pathElems);
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
item.content.forEach(function(inner) {
|
||||||
|
if (inner.hasAttr('transform')) {
|
||||||
|
inner.attr('transform').value = item.attr('transform').value + ' ' + inner.attr('transform').value;
|
||||||
|
} else {
|
||||||
|
inner.addAttr(item.attr('transform'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
item.removeAttr('transform');
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
6
test/plugins/moveElemsAttrsToGroup.06.orig.svg
Normal file
6
test/plugins/moveElemsAttrsToGroup.06.orig.svg
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<g>
|
||||||
|
<path transform="scale(2)" d="M0,0 L10,20"/>
|
||||||
|
<path transform="scale(2)" d="M0,10 L20,30"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 172 B |
6
test/plugins/moveElemsAttrsToGroup.06.should.svg
Normal file
6
test/plugins/moveElemsAttrsToGroup.06.should.svg
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<g>
|
||||||
|
<path transform="scale(2)" d="M0,0 L10,20"/>
|
||||||
|
<path transform="scale(2)" d="M0,10 L20,30"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 172 B |
6
test/plugins/moveGroupAttrsToElems.01.orig.svg
Normal file
6
test/plugins/moveGroupAttrsToElems.01.orig.svg
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<g transform="scale(2)">
|
||||||
|
<path transform="rotate(45)" d="M0,0 L10,20"/>
|
||||||
|
<path transform="translate(10, 20)" d="M0,10 L20,30"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 204 B |
6
test/plugins/moveGroupAttrsToElems.01.should.svg
Normal file
6
test/plugins/moveGroupAttrsToElems.01.should.svg
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<g>
|
||||||
|
<path transform="scale(2) rotate(45)" d="M0,0 L10,20"/>
|
||||||
|
<path transform="scale(2) translate(10, 20)" d="M0,10 L20,30"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 201 B |
6
test/plugins/moveGroupAttrsToElems.02.orig.svg
Normal file
6
test/plugins/moveGroupAttrsToElems.02.orig.svg
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<g transform="scale(2)">
|
||||||
|
<path d="M0,0 L10,20"/>
|
||||||
|
<path d="M0,10 L20,30"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 151 B |
6
test/plugins/moveGroupAttrsToElems.02.should.svg
Normal file
6
test/plugins/moveGroupAttrsToElems.02.should.svg
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<g>
|
||||||
|
<path d="M0,0 L10,20" transform="scale(2)"/>
|
||||||
|
<path d="M0,10 L20,30" transform="scale(2)"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 172 B |
Reference in New Issue
Block a user