Skip to content

Commit e642de4

Browse files
authored
fix(svg-deserializer): svg path should return one geom2
1 parent 5fc3860 commit e642de4

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

packages/io/svg-deserializer/src/shapesMapGeometry.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,22 @@ export const shapesMapGeometry = (obj, objectify, params) => {
133133
const listofpaths = expandPath(obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, svgGroups, segments, pathSelfClosed)
134134
// order is important
135135
const listofentries = Object.entries(listofpaths).sort((a, b) => a[0].localeCompare(b[0]))
136-
const shapes = listofentries.map((entry) => {
137-
const path = entry[1]
138-
if (target === 'geom2' && path.isClosed) {
139-
const points = path2.toPoints(path)
140-
return geom2.create([points])
141-
}
142-
return path
143-
})
144-
return shapes
136+
if (target === 'geom2') {
137+
// concatenate all sides to a single geom2
138+
const outlines = []
139+
listofentries.forEach((entry) => {
140+
const path = entry[1]
141+
// discard unclosed paths
142+
if (path.isClosed) {
143+
const points = path2.toPoints(path)
144+
outlines.push(points)
145+
}
146+
})
147+
if (outlines.length === 0) return []
148+
return geom2.create(outlines)
149+
} else {
150+
return listofentries.map((entry) => entry[1])
151+
}
145152
}
146153
}
147154

@@ -153,6 +160,18 @@ const appendPoints = (points, geometry) => {
153160
return path2.fromPoints({ }, points)
154161
}
155162

163+
/*
164+
* Expands the given SVG path object into a set of path segments.
165+
* @param {object} obj - SVG path object to expand
166+
* @param {number} svgUnitsPmm - SVG units per millimeter
167+
* @param {number} svgUnitsX - X-axis SVG units
168+
* @param {number} svgUnitsY - Y-axis SVG units
169+
* @param {number} svgUnitsV - SVG units value
170+
* @param {object} svgGroups - SVG groups object
171+
* @param {number} segments - number of segments per 360 rotation
172+
* @param {boolean} pathSelfClosed - Whether the path is self-closed
173+
* @returns {object} a object containing the named paths
174+
*/
156175
const expandPath = (obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, svgGroups, segments, pathSelfClosed) => {
157176
const paths = {}
158177
const on = 'path'

packages/io/svg-deserializer/tests/instantiate.test.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,7 @@ test('deserialize : instantiate svg (path: with bezier) to objects', (t) => {
326326
</svg>`
327327

328328
observed = deserialize({ output: 'geometry', target: 'geom2', addMetaData: false }, sourceSvg)
329-
t.is(observed.length, 2)
330-
shape = observed[0]
331-
t.is(shape.points.length, 14) // open path
329+
t.is(observed.length, 0) // open path
332330

333331
observed = deserialize({ output: 'geometry', target: 'path', addMetaData: false }, sourceSvg)
334332
t.is(observed.length, 2)
@@ -344,9 +342,7 @@ test('deserialize : instantiate svg (path: with bezier) to objects', (t) => {
344342
</svg>`
345343

346344
observed = deserialize({ output: 'geometry', target: 'geom2', addMetaData: false }, sourceSvg)
347-
t.is(observed.length, 1)
348-
shape = observed[0]
349-
t.is(shape.points.length, 29) // open path
345+
t.is(observed.length, 0) // open path
350346

351347
observed = deserialize({ output: 'geometry', target: 'path', addMetaData: false }, sourceSvg)
352348
t.is(observed.length, 1)
@@ -420,13 +416,11 @@ test('deserialize : instantiate svg produced by inkscape to objects', (t) => {
420416
`
421417

422418
let observed = deserialize({ filename: 'inkscape', output: 'geometry', target: 'geom2', addMetaData: false }, sourceSvg)
423-
t.is(observed.length, 2)
419+
t.is(observed.length, 1)
424420
let shape = observed[0]
425-
t.is(shape.outlines.length, 1)
421+
t.is(shape.outlines.length, 2)
426422
t.is(shape.outlines[0].length, 19)
427-
shape = observed[1]
428-
t.is(shape.outlines.length, 1)
429-
t.is(shape.outlines[0].length, 20)
423+
t.is(shape.outlines[1].length, 20)
430424

431425
observed = deserialize({ output: 'geometry', target: 'path', addMetaData: false }, sourceSvg)
432426
t.is(observed.length, 2)
@@ -446,13 +440,11 @@ test('deserialize : instantiate shape with a hole to objects', (t) => {
446440
`
447441

448442
let observed = deserialize({ output: 'geometry', target: 'geom2', addMetaData: false }, sourceSvg)
449-
t.is(observed.length, 2)
443+
t.is(observed.length, 1)
450444
let shape = observed[0]
451-
t.is(shape.outlines.length, 1)
452-
t.is(shape.outlines[0].length, 38)
453-
shape = observed[1]
454-
t.is(shape.outlines.length, 1)
445+
t.is(shape.outlines.length, 2)
455446
t.is(shape.outlines[0].length, 38)
447+
t.is(shape.outlines[1].length, 38)
456448

457449
observed = deserialize({ output: 'geometry', target: 'path', addMetaData: false }, sourceSvg)
458450
t.is(observed.length, 2)
@@ -472,9 +464,9 @@ test('deserialize : instantiate shape with a nested hole to objects', (t) => {
472464
`
473465

474466
let observed = deserialize({ output: 'geometry', target: 'geom2', addMetaData: false }, sourceSvg)
475-
t.is(observed.length, 4)
467+
t.is(observed.length, 1)
476468
let shape = observed[0]
477-
t.is(shape.outlines.length, 1)
469+
t.is(shape.outlines.length, 4)
478470
t.is(shape.outlines[0].length, 38)
479471

480472
observed = deserialize({ output: 'geometry', target: 'path', addMetaData: false }, sourceSvg)

packages/modeling/src/geometries/path2/appendBezier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { toPoints } from './toPoints.js'
1414
* In other words, the trailing gradient of the geometry matches the new gradient of the curve.
1515
* @param {object} options - options for construction
1616
* @param {Array} options.controlPoints - list of control points (2D) for the Bézier curve
17-
* @param {number} [options.segment=16] - number of segments per 360 rotation
17+
* @param {number} [options.segments=16] - number of segments per 360 rotation
1818
* @param {Path2} geometry - the path of which to append points
1919
* @returns {Path2} a new path with the appended points
2020
* @alias module:modeling/geometries/path2.appendBezier

0 commit comments

Comments
 (0)