1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

plugins/mergePaths: new plugin

This commit is contained in:
deepsweet
2013-04-11 21:15:41 +03:00
parent 3b3275d4b6
commit 9124bf809e
5 changed files with 68 additions and 1 deletions

View File

@ -1,3 +1,6 @@
# replace default config
# full: true
plugins:
# - name
@ -35,6 +38,6 @@ plugins:
- convertTransform
- removeEmptyAttrs
- removeEmptyContainers
- mergePaths
- cleanupIDs
- removeUnusedNS
- cropAndCenterAlongPath

View File

@ -43,6 +43,7 @@ Today we have:
* [ [>](https://github.com/svg/svgo/blob/master/plugins/moveGroupAttrsToElems.js) ] move some group attributes to the content elements
* [ [>](https://github.com/svg/svgo/blob/master/plugins/collapseGroups.js) ] collapse useless groups
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeRasterImages.js) ] remove raster images (disabled by default)
* [ [>](https://github.com/svg/svgo/blob/master/plugins/mergePath.js) ] merge multiple Paths into one
Want to know how it works and how to write your own plugin? [Of course you want to](https://github.com/svg/svgo/blob/master/docs/how-it-works/en.md).

View File

@ -43,6 +43,7 @@ SVGO имеет расширяемую архитектуру, в которой
* [ [>](https://github.com/svg/svgo/blob/master/plugins/moveGroupAttrsToElems.js) ] перемещение некоторых атрибутов группы на элементы внутри
* [ [>](https://github.com/svg/svgo/blob/master/plugins/collapseGroups.js) ] схлопывание бесполезных групп `<g>`
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeRasterImages.js) ] удаление растровых изображений (выключено по умолчанию)
* [ [>](https://github.com/svg/svgo/blob/master/plugins/mergePath.js) ] склеивание нескольких Path в одну кривую
Хотите узнать, как это работает и как написать свой плагин? [Конечно же, да!](https://github.com/svg/svgo/blob/master/docs/how-it-works/ru.md).

45
plugins/mergePaths.js Normal file
View File

@ -0,0 +1,45 @@
'use strict';
exports.type = 'perItem';
exports.active = true;
/**
* Merge multiple Paths into one.
*
* @param {Object} item current iteration item
* @return {Boolean} if false, item will be filtered out
*
* @author Kir Belevich
*/
exports.fn = function(item) {
if (item.isElem() && !item.isEmpty()) {
var prevContentItem;
item.content = item.content.filter(function(contentItem) {
// merge only <path d="...z" />
if (prevContentItem &&
prevContentItem.isElem('path') &&
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.hasAttr('d') &&
Object.keys(contentItem.attrs).length === 1
) {
prevContentItem.attr('d').value += contentItem.attr('d').value;
return false;
}
prevContentItem = contentItem;
return true;
});
}
};

View File

@ -0,0 +1,17 @@
<svg xmlns="http://www.w3.org/2000/svg">
<path d="M 0,0 z"/>
<path d="M 10,10 z"/>
<path d="M 20,20"/>
<path d="M 30,30 z"/>
<path d="M 30,30 z" fill="#f00"/>
<path d="M 40,40 z"/>
</svg>
@@@
<svg xmlns="http://www.w3.org/2000/svg">
<path d="M 0,0 zM 10,10 zM 20,20"/>
<path d="M 30,30 z"/>
<path d="M 30,30 z" fill="#f00"/>
<path d="M 40,40 z"/>
</svg>

After

Width:  |  Height:  |  Size: 396 B