Skip to content

toSVG throws "Cannot read properties of undefined (reading 'length')" on text with tiny bezier segments #659

Description

Since 0.19.1, exporting text models to SVG can throw
Cannot read properties of undefined (reading 'length'), depending on the
font and size. We hit this with models.Text using Allerta Stencil — the
5 glyph at font sizes around 1.25–2.25 crashes the export.

The winding check added in #643 calls path.toKeyPoints on every chain link,
and that function returns undefined (instead of IPoint[]) for degenerate
bezier seeds. Glyph outlines commonly contain near-zero-length curve
segments, so text is a reliable way to trigger it. 0.19.0 and earlier don't
hit this code path during export.

Repro without fonts, using a seed captured from the Allerta 5:

const makerjs = require('makerjs') // 0.19.2

const seed = {
  type: 'bezier-seed',
  origin: [0.271728515625, 0.642333984375],
  controls: [[0.27099609375, 0.6357421875]],
  end: [0.27099609375, 0.63427734375]
}

console.log(makerjs.path.toKeyPoints(seed)) // undefined

makerjs.exporter.toSVG({
  paths: {
    seed,
    line1: new makerjs.paths.Line(seed.end, [0, 0]),
    line2: new makerjs.paths.Line([0, 0], seed.origin)
  }
})
// TypeError: Cannot read properties of undefined (reading 'length')
//   at chain.toKeyPoints / isChainClockwise / chainToSVGPathData

The cause is in path.toKeyPoints: the BezierSeed branch only assigns
curveKeyPoints when findChains finds exactly one chain or one loose path.
This seed's arc approximation produces two tiny chains, so nothing is
assigned and undefined comes back. chain.toKeyPoints then reads
.length on it.

return curveKeyPoints || [] would fix it — a segment this small doesn't
meaningfully affect the winding result, and the chain's other links still
determine direction. Happy to submit a PR with a test if that sounds right.

We're currently working around this by monkey-patching path.toKeyPoints
to return [] instead of undefined.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions