Skip to content

Commit

Permalink
Prevent merging path when marker-end style is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Mar 5, 2021
1 parent 555a961 commit de4fd79
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 52 deletions.
111 changes: 59 additions & 52 deletions plugins/mergePaths.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
'use strict';

const { computeStyle } = require('../lib/style.js');
const { path2js, js2path, intersects } = require('./_path.js');

exports.type = 'perItem';

exports.active = true;

exports.description = 'merges multiple paths in one if possible';

exports.params = {
collapseRepeated: true,
force: false,
leadingZero: true,
negativeExtraSpace: true,
noSpaceAfterFlags: false, // a20 60 45 0 1 30 20 → a20 60 45 0130 20
collapseRepeated: true,
force: false,
leadingZero: true,
negativeExtraSpace: true,
noSpaceAfterFlags: false, // a20 60 45 0 1 30 20 → a20 60 45 0130 20
};

var path2js = require('./_path.js').path2js,
js2path = require('./_path.js').js2path,
intersects = require('./_path.js').intersects;

/**
* Merge multiple Paths into one.
*
Expand All @@ -26,48 +25,56 @@ var path2js = require('./_path.js').path2js,
*
* @author Kir Belevich, Lev Solntsev
*/
exports.fn = function(item, params) {

if (!item.isElem() || item.isEmpty()) return;

var prevContentItem = null,
prevContentItemKeys = null;

item.content = item.content.filter(function(contentItem) {

if (prevContentItem &&
prevContentItem.isElem('path') &&
prevContentItem.isEmpty() &&
prevContentItem.hasAttr('d') &&
contentItem.isElem('path') &&
contentItem.isEmpty() &&
contentItem.hasAttr('d')
) {

if (!prevContentItemKeys) {
prevContentItemKeys = Object.keys(prevContentItem.attrs);
}

var contentItemAttrs = Object.keys(contentItem.attrs),
equalData = prevContentItemKeys.length == contentItemAttrs.length &&
contentItemAttrs.every(function(key) {
return key == 'd' ||
prevContentItem.hasAttr(key) &&
prevContentItem.attr(key).value == contentItem.attr(key).value;
}),
prevPathJS = path2js(prevContentItem),
curPathJS = path2js(contentItem);

if (equalData && (params.force || !intersects(prevPathJS, curPathJS))) {
js2path(prevContentItem, prevPathJS.concat(curPathJS), params);
return false;
}
}

prevContentItem = contentItem;
prevContentItemKeys = null;
exports.fn = function (item, params) {
if (!item.isElem() || item.isEmpty()) return;

var prevContentItem = null,
prevContentItemKeys = null;

item.content = item.content.filter(function (contentItem) {
if (
prevContentItem &&
prevContentItem.isElem('path') &&
prevContentItem.isEmpty() &&
prevContentItem.hasAttr('d') &&
contentItem.isElem('path') &&
contentItem.isEmpty() &&
contentItem.hasAttr('d')
) {
const computedStyle = computeStyle(contentItem);
// keep path to not break markers
if (
computedStyle['marker-start'] ||
computedStyle['marker-mid'] ||
computedStyle['marker-end']
) {
return true;

});

}
if (!prevContentItemKeys) {
prevContentItemKeys = Object.keys(prevContentItem.attrs);
}

var contentItemAttrs = Object.keys(contentItem.attrs),
equalData =
prevContentItemKeys.length == contentItemAttrs.length &&
contentItemAttrs.every(function (key) {
return (
key == 'd' ||
(prevContentItem.hasAttr(key) &&
prevContentItem.attr(key).value == contentItem.attr(key).value)
);
}),
prevPathJS = path2js(prevContentItem),
curPathJS = path2js(contentItem);

if (equalData && (params.force || !intersects(prevPathJS, curPathJS))) {
js2path(prevContentItem, prevPathJS.concat(curPathJS), params);
return false;
}
}

prevContentItem = contentItem;
prevContentItemKeys = null;
return true;
});
};
35 changes: 35 additions & 0 deletions test/plugins/mergePaths.07.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 de4fd79

Please sign in to comment.