1
0
Fork 0
freesewing/plugins/plugin-cutonfold/src/index.mjs

69 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-08-28 12:26:34 +02:00
import { name, version } from '../package.json' assert { type: 'json' }
2022-08-28 12:26:34 +02:00
const markers = `
<marker orient="auto" refY="4.0" refX="0.0" id="cutonfoldFrom" style="overflow:visible;" markerWidth="12" markerHeight="8">
<path class="note fill-note" d="M 0,4 L 12,0 C 10,2 10,6 12,8 z" />
</marker>
<marker orient="auto" refY="4.0" refX="12.0" id="cutonfoldTo" style="overflow:visible;" markerWidth="12" markerHeight="8">
<path class="note fill-note" d="M 12,4 L 0,0 C 2,2 2,6 0,8 z" />
</marker>
`
export const plugin = {
name,
version,
2018-07-21 15:34:46 +02:00
hooks: {
preRender: (svg) => {
2022-08-28 12:26:34 +02:00
if (svg.defs.indexOf(markers) === -1) svg.defs += markers
2021-04-24 10:16:31 +02:00
},
},
macros: {
2021-01-31 09:22:15 +01:00
cutonfold: function (so) {
const { points, complete } = this.shorthand()
if (so === false) {
2019-08-03 15:03:33 +02:00
delete this.points.cutonfoldFrom
delete this.points.cutonfoldTo
delete this.points.cutonfoldVia1
delete this.points.cutonfoldVia2
delete this.paths.cutonfold
this.setCutOnFold(false) // Restore default
2019-08-03 15:03:33 +02:00
return true
}
so = {
2019-09-29 16:58:21 +02:00
offset: 15,
margin: 5,
2019-08-03 15:03:33 +02:00
prefix: '',
2021-04-24 10:16:31 +02:00
...so,
2019-08-03 15:03:33 +02:00
}
this.setCutOnFold(so.from, so.to)
if (so.grainline) this.setGrain(so.from.angle(so.to))
if (complete) {
points['cutonfoldFrom' + so.prefix] = so.from.shiftFractionTowards(so.to, so.margin / 100)
points['cutonfoldTo' + so.prefix] = so.to.shiftFractionTowards(so.from, so.margin / 100)
points['cutonfoldVia1' + so.prefix] = points['cutonfoldFrom' + so.prefix]
.shiftTowards(so.from, so.offset * this.context.settings.scale)
.rotate(-90, points['cutonfoldFrom' + so.prefix])
points['cutonfoldVia2' + so.prefix] = points['cutonfoldTo' + so.prefix]
.shiftTowards(so.to, so.offset * this.context.settings.scale)
.rotate(90, points['cutonfoldTo' + so.prefix])
const text = so.grainline ? 'cutOnFoldAndGrainline' : 'cutOnFold'
this.paths['cutonfold' + so.prefix] = new this.Path()
.move(points['cutonfoldFrom' + so.prefix])
.line(points['cutonfoldVia1' + so.prefix])
.line(points['cutonfoldVia2' + so.prefix])
.line(points['cutonfoldTo' + so.prefix])
.attr('class', 'note')
.attr('marker-start', 'url(#cutonfoldFrom)')
.attr('marker-end', 'url(#cutonfoldTo)')
.attr('data-text', text)
.attr('data-text-class', 'center fill-note')
}
2021-04-24 10:16:31 +02:00
},
},
2019-08-03 15:03:33 +02:00
}
// More specifically named exports
export const cutonfoldPlugin = plugin
export const pluginCutonfold = plugin