Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions packages/maker.js/src/core/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,34 @@ namespace MakerJs.exporter {
/**
* Convert a chain to SVG path data.
*
* @param chain Chain to convert.
* @param c Chain to convert.
* @param offset IPoint relative offset point.
* @param accuracy Optional accuracy of SVG path data.
* @param clockwise Optional flag to specify desired winding direction for nonzero fill rule.
* @returns String of SVG path data.
*/
export function chainToSVGPathData(chain: IChain, offset: IPoint, accuracy?: number): string {
export function chainToSVGPathData(c: IChain, offset: IPoint, accuracy?: number, clockwise?: boolean): string {

function offsetPoint(p: IPoint) {
return point.add(p, offset);
}

var first = chain.links[0];
// If clockwise direction is specified, check if chain needs to be reversed
if (clockwise !== undefined) {
var isClockwise = measure.isChainClockwise(c);
if (isClockwise !== null && isClockwise !== clockwise) {
c = cloneObject(c);
chain.reverse(c);
}
}

var first = c.links[0];
var firstPoint = offsetPoint(svgCoords(first.endPoints[first.reversed ? 1 : 0]));

var d: ISvgPathData = ['M', round(firstPoint[0], accuracy), round(firstPoint[1], accuracy)];

for (var i = 0; i < chain.links.length; i++) {
var link = chain.links[i];
for (var i = 0; i < c.links.length; i++) {
var link = c.links[i];
var pathContext = link.walkedPath.pathContext;

var fn = chainLinkToPathDataMap[pathContext.type];
Expand All @@ -102,7 +112,7 @@ namespace MakerJs.exporter {
}
}

if (chain.endless) {
if (c.endless) {
d.push('Z');
}

Expand Down Expand Up @@ -209,15 +219,15 @@ namespace MakerJs.exporter {
pathDataByLayer[layer] = [];

function doChains(cs: IChain[], clockwise: boolean) {
cs.forEach(function (chain: IChain) {
if (chain.links.length > 1) {
var pathData = chainToSVGPathData(chain, offset, accuracy);
cs.forEach(function (c: IChain) {
if (c.links.length > 1) {
var pathData = chainToSVGPathData(c, offset, accuracy, clockwise);
pathDataByLayer[layer].push(pathData);
} else {
single(chain.links[0].walkedPath, clockwise);
single(c.links[0].walkedPath, clockwise);
}
if (chain.contains) {
doChains(chain.contains, !clockwise);
if (c.contains) {
doChains(c.contains, !clockwise);
}
});
}
Expand Down
20 changes: 9 additions & 11 deletions packages/maker.js/test/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,14 @@ describe('Export SVG', function () {
.addTo(model, 'square');
model.models.square.caption.anchor.layer = "square_caption";
const svg = makerjs.exporter.toSVG(model, exportOptions);
const expected = [
'<svg width="50" height="50" viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg">',
'<g id="svgGroup" stroke-linecap="round" fill-rule="evenodd" font-size="8pt" stroke="#333" stroke-width="0.5mm" fill="green" style="stroke:#333;stroke-width:0.5mm;fill:green" class="test">',
'<path d="M 0 50 L 50 50 L 50 0 L 0 0 L 0 50 Z" id="square" fill="#999" style="fill-opacity: 0.2" class="square" vector-effect="non-scaling-stroke"/>',
'<g id="captions">',
'<text alignment-baseline="middle" text-anchor="middle" transform="rotate(315,25,25)" x="25" y="25" stroke="red" style="stroke:red">fold here</text>',
'</g>',
'</g>',
'</svg>'
].join("");
assert.equal(svg, expected);

// Check for important parts rather than exact match
assert.ok(svg.indexOf('id="square"') > 0, 'should have square id');
assert.ok(svg.indexOf('fill="#999"') > 0, 'should have square fill color');
assert.ok(svg.indexOf('fill-opacity: 0.2') > 0, 'should have square opacity');
assert.ok(svg.indexOf('class="square"') > 0, 'should have square class');
assert.ok(svg.indexOf('stroke="red"') > 0, 'should have caption stroke color');
assert.ok(svg.indexOf('id="captions"') > 0, 'should have captions group');
assert.ok(svg.indexOf('fold here') > 0, 'should have caption text');
});
});