Skip to content

Commit

Permalink
plugins/mergePaths: new plugin
Browse files Browse the repository at this point in the history
deepsweet committed Apr 11, 2013
1 parent 3b3275d commit 9124bf8
Showing 5 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .svgo.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# replace default config
# full: true

plugins:

# - name
@@ -35,6 +38,6 @@ plugins:
- convertTransform
- removeEmptyAttrs
- removeEmptyContainers
- mergePaths
- cleanupIDs
- removeUnusedNS
- cropAndCenterAlongPath
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).

1 change: 1 addition & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
@@ -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 changes: 45 additions & 0 deletions plugins/mergePaths.js
Original file line number Diff line number Diff line change
@@ -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;

});
}

};
17 changes: 17 additions & 0 deletions test/plugins/mergePaths.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9124bf8

Please sign in to comment.