-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSvgWithAddedOutline.mjs
85 lines (74 loc) · 3.03 KB
/
getSvgWithAddedOutline.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as cheerio from 'cheerio';
export default function getSvgWithOutline(svgString, options = {}){
/* Options */
const outlineColor = options.color || '#FFF';
const outlineAddedThickness = options.addedStrokeWidth ?? 4;
const closeFilledPaths = options.closeFilledPaths ?? true;
const backgroundLayerName = options.backgroundLayerName || 'outline';
/* Load SVG */
const $ = cheerio.load(svgString);
/* Copy content and adjust */
let svg = $('svg').first();
const contentCopy = svg.children().clone();
const bgLayerRef = '#' + backgroundLayerName
svg.prepend('<g id="' + backgroundLayerName + '"></g>')
$(bgLayerRef).first().append(contentCopy)
$(bgLayerRef + ' #grid').remove() // Remove grid as we don't want to outline the grid
$(bgLayerRef + ' *').removeAttr('id') // Remove IDs because otherwise they would appear twice in SVG
/* Outline */
const selectors = [
bgLayerRef + ' path',
bgLayerRef + ' rect',
bgLayerRef + ' line',
bgLayerRef + ' circle',
bgLayerRef + ' ellipse',
bgLayerRef + ' polyline',
bgLayerRef + ' polygon',
]
const svgShapes = $( selectors.join(', ') )
svgShapes.each(function(index, element) {
adjustElementIfNeeded($, element, outlineColor, outlineAddedThickness, closeFilledPaths)
});
/* Remove unneeded layers for viewing */
$('#grid').remove()
/* Convert back to SVG string */
const htmlString = $.html()
const svgOnlyString = htmlString.replace('<html><head></head><body>', '').replace('</body></html>', '') // Remove the added HTML from Cheerio as we don't have HTML
return svgOnlyString
}
function adjustElementIfNeeded($, element, outlineColor, outlineAddedThickness, closeFilledPaths){
var elementFill = $(element).attr('fill');
var elementStroke = $(element).attr('stroke');
var hasFill = elementFill && elementFill != 'none'
var hasStroke = elementStroke && elementStroke != 'none'
var strokeWidth = $(element).attr('stroke-width') || 0;
var newStrokeThickness = Math.max(2, strokeWidth) + outlineAddedThickness
if(hasFill){
$(element).attr('fill', outlineColor);
$(element).attr('stroke', outlineColor);
$(element).attr('stroke-width', newStrokeThickness);
$(element).attr('stroke-linecap', 'round');
$(element).attr('stroke-linejoin', 'round');
if(closeFilledPaths === true && element.name === 'path'){
var pointsString = $(element).attr('d');
var lastCharacter = pointsString.slice(-1)
// Z is the command in a path to close it -> looks better for certain emojis, e.g. 1F467-1F3FD
if(lastCharacter.toUpperCase() !== 'Z'){
pointsString = pointsString + 'Z'
$(element).attr('d', pointsString);
}
}
}
if(hasStroke){
$(element).attr('stroke', outlineColor);
$(element).attr('stroke-width', newStrokeThickness);
$(element).attr('stroke-linecap', 'round');
$(element).attr('stroke-linejoin', 'round');
}
if(!hasFill && !hasStroke){
// -> I think that is a default that if neither is set it is filled #000
$(element).attr('fill', outlineColor);
$(element).attr('stroke', outlineColor);
$(element).attr('stroke-width', newStrokeThickness);
}
}