mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
Updated mergePaths to join paths with the same attributes & values.
This commit is contained in:
@ -17,7 +17,13 @@ exports.fn = function(item) {
|
|||||||
if (item.isElem() && !item.isEmpty()) {
|
if (item.isElem() && !item.isEmpty()) {
|
||||||
|
|
||||||
var prevContentItem,
|
var prevContentItem,
|
||||||
delim = '';
|
delim = '',
|
||||||
|
prevContentItemKeys = null,
|
||||||
|
prevItemPathClosed = false,
|
||||||
|
contentItemKeys = null,
|
||||||
|
contentItemPathClosed = false,
|
||||||
|
equalData,
|
||||||
|
attrName;
|
||||||
|
|
||||||
item.content = item.content.filter(function(contentItem) {
|
item.content = item.content.filter(function(contentItem) {
|
||||||
|
|
||||||
@ -25,25 +31,65 @@ exports.fn = function(item) {
|
|||||||
if (prevContentItem &&
|
if (prevContentItem &&
|
||||||
prevContentItem.isElem('path') &&
|
prevContentItem.isElem('path') &&
|
||||||
prevContentItem.hasAttr('d') &&
|
prevContentItem.hasAttr('d') &&
|
||||||
Object.keys(prevContentItem.attrs).length === 1 &&
|
|
||||||
prevContentItem.attr('d').value.charAt(prevContentItem.attr('d').value.length - 1) === 'z' &&
|
|
||||||
contentItem.isElem('path') &&
|
contentItem.isElem('path') &&
|
||||||
contentItem.hasAttr('d') &&
|
contentItem.hasAttr('d')
|
||||||
Object.keys(contentItem.attrs).length === 1
|
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
prevItemPathClosed = prevContentItem.attr('d').value.charAt(prevContentItem.attr('d').value.length-1) === 'z';
|
||||||
|
contentItemPathClosed = contentItem.attr('d').value.charAt(contentItem.attr('d').value.length-1) === 'z';
|
||||||
|
|
||||||
|
if (!prevItemPathClosed && contentItemPathClosed){
|
||||||
|
//console.log('Previous path not closed, current plath closed', prevContentItem.attr('d').value, contentItem.attr('d').value);
|
||||||
|
prevContentItem = contentItem;
|
||||||
|
prevContentItemKeys = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prevContentItemKeys === null){
|
||||||
|
prevContentItemKeys = Object.keys(prevContentItem.attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
contentItemKeys = Object.keys(contentItem.attrs);
|
||||||
|
if (contentItemKeys.length !== 1 || prevContentItemKeys.length !== 1){
|
||||||
|
if (contentItemKeys.length !== prevContentItemKeys.length){
|
||||||
|
prevContentItem = contentItem;
|
||||||
|
prevContentItemKeys = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
equalData = true;
|
||||||
|
for(var i = 0, I = contentItemKeys.length; i < I; i++){
|
||||||
|
attrName = contentItemKeys[i];
|
||||||
|
if (attrName != 'd'){
|
||||||
|
if(typeof prevContentItem.attrs[attrName] === "undefined"){
|
||||||
|
equalData = false;
|
||||||
|
break;
|
||||||
|
} else if (prevContentItem.attrs[attrName].value !== contentItem.attrs[attrName].value){
|
||||||
|
equalData = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!equalData){
|
||||||
|
prevContentItem = contentItem;
|
||||||
|
prevContentItemKeys = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
// "zM", but "z m"
|
// "zM", but "z m"
|
||||||
// looks like a FontForge parsing bug
|
// looks like a FontForge parsing bug
|
||||||
if (contentItem.attr('d').value.charAt(0) === 'm') {
|
if (contentItem.attr('d').value.charAt(0) === 'm') {
|
||||||
delim = ' ';
|
delim = ' ';
|
||||||
|
} else {
|
||||||
|
delim = ''; // reset delim from looping
|
||||||
}
|
}
|
||||||
|
|
||||||
prevContentItem.attr('d').value += delim + contentItem.attr('d').value;
|
prevContentItem.attr('d').value += delim + contentItem.attr('d').value;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
prevContentItem = contentItem;
|
prevContentItem = contentItem;
|
||||||
|
prevContentItemKeys = null;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
27
test/plugins/mergePaths.02.svg
Normal file
27
test/plugins/mergePaths.02.svg
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M 0,0 z" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="M 10,10 z" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="M 20,20" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="M 30,30 z" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="M 30,30 z" fill="#f00"/>
|
||||||
|
<path d="M 40,40 z"/>
|
||||||
|
<path d="m 50,50 z"/>
|
||||||
|
<path d="M 40,40"/>
|
||||||
|
<path d="m 50,50"/>
|
||||||
|
<path d="M 40,40 z" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="m 50,50 z" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="M 40,40" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="m 50,50" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="m 50,50 z" fill="#fff" stroke="#333"/>
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
@@@
|
||||||
|
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M 0,0 zM 10,10 zM 20,20" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="M 30,30 z" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="M 30,30 z" fill="#f00"/>
|
||||||
|
<path d="M 40,40 z m 50,50 zM 40,40 m 50,50"/>
|
||||||
|
<path d="M 40,40 z m 50,50 zM 40,40 m 50,50" fill="#fff" stroke="#333"/>
|
||||||
|
<path d="m 50,50 z" fill="#fff" stroke="#333"/>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 1.0 KiB |
Reference in New Issue
Block a user