1
0
Fork 0

chore(plugin-annotations): Migrate cutonfold

This commit is contained in:
joostdecock 2023-09-05 20:37:15 +02:00
parent c289f4df66
commit 8d4c6fcf9e

View file

@ -1,65 +1,111 @@
import { getIds } from './utils.mjs'
/*
* Defaults for the cutonfold macro
*/
const macroDefaults = {
classes: {
line: 'note',
text: 'center fill-note',
},
id: 'cutonfold',
grainline: false,
margin: 0.05,
offset: 15,
}
// Export defs // Export defs
export const cutonfoldDefs = [ export const cutonfoldDefs = [
{ {
name: 'cutonfoldFrom', name: 'cutonfoldFrom',
def: ` def: `
<marker orient="auto" refY="4.0" refX="0.0" id="cutonfoldFrom" style="overflow:visible;" markerWidth="12" markerHeight="8"> <marker orient="auto" refY="3" refX="0" id="cutonfoldFrom" style="overflow:visible;" markerWidth="10" markerHeight="6">
<path class="note fill-note" d="M 0,4 L 12,0 C 10,2 10,6 12,8 z" /> <path d="M 0,3 L 10,0 C 8,2 8,4 10,6 z" class="fill-note note" />
</marker>`, </marker>`,
}, },
{ {
name: 'cutonfoldTo', name: 'cutonfoldTo',
def: ` def: `
<marker orient="auto" refY="4.0" refX="12.0" id="cutonfoldTo" style="overflow:visible;" markerWidth="12" markerHeight="8"> <marker orient="auto" refY="3" refX="10" id="cutonfoldTo" style="overflow:visible;" markerWidth="10" markerHeight="6">
<path class="note fill-note" d="M 12,4 L 0,0 C 2,2 2,6 0,8 z" /> <path d="M 10,3 L 0,0 C 2,2 2,4 0,6 z" class="fill-note note" />
</marker>`, </marker>`,
}, },
] ]
// Export macros /*
export const cutonfoldMacros = { * The rmcutonfold macro
cutonfold: function (so, { points, paths, Path, complete, store, scale }) { */
if (so === false) { const rmcutonfold = function (id = macroDefaults.id, { paths, store, part }) {
delete points.cutonfoldFrom for (const pid of Object.values(
delete points.cutonfoldTo store.get(['parts', part.name, 'macros', 'cutonfold', 'ids', id, 'paths'], {})
delete points.cutonfoldVia1 ))
delete points.cutonfoldVia2 delete paths[pid]
delete paths.cutonfoldCutonfold
store.cutlist.setCutOnFold(false) // Restore default
return true
}
so = {
offset: 15,
margin: 5,
prefix: 'cutonfold',
...so,
} }
// store in cutlist /*
store.cutlist.setCutOnFold(so.from, so.to) * The cutonfold macro
if (so.grainline) store.cutlist.setGrain(so.from.angle(so.to)) */
const cutonfold = function (config, { points, paths, Path, complete, store, scale, part }) {
/*
* Don't add a cutonfold indicator when complete is false, unless force is true
*/
if (!complete && !config.force) return
if (complete) { /*
points[so.prefix + 'From'] = so.from.shiftFractionTowards(so.to, so.margin / 100) * Merge macro defaults with user-provided config to create the macro config (mc)
points[so.prefix + 'To'] = so.to.shiftFractionTowards(so.from, so.margin / 100) */
points[so.prefix + 'Via1'] = points[so.prefix + 'From'] const mc = {
.shiftTowards(so.from, so.offset * scale) ...macroDefaults,
.rotate(-90, points[so.prefix + 'From']) ...config,
points[so.prefix + 'Via2'] = points[so.prefix + 'To'] classes: macroDefaults.classes,
.shiftTowards(so.to, so.offset * scale) }
.rotate(90, points[so.prefix + 'To']) if (config.classes) mc.classes = { ...mc.classes, ...config.classes }
const text = so.grainline ? 'cutOnFoldAndGrainline' : 'cutOnFold'
paths[so.prefix + 'Cutonfold'] = new Path() /*
.move(points[so.prefix + 'From']) * Make sure mc.from and mc.to are Point instances
.line(points[so.prefix + 'Via1']) */
.line(points[so.prefix + 'Via2']) if (!mc.from || typeof mc.from.attr !== 'function') {
.line(points[so.prefix + 'To']) log.warn(`Cutonfold macro called without a valid from point. Using (0,0) for from.`)
.attr('class', 'note') mc.from = new Point(0, 0)
}
if (!mc.to || typeof mc.to.attr !== 'function') {
log.warn(`Cutonfold macro called without a valid to point. Using (6660,666) for to.`)
mc.to = new Point(666, 666)
}
/*
* Store cutonfold and optional grainline angle for use in cutlist
*/
store.cutlist.setCutOnFold(mc.from, mc.to)
if (mc.grainline) store.cutlist.setGrain(mc.from.angle(mc.to))
/*
* Get the list of IDs
*/
const ids = getIds(['line'], mc.id, 'cutonfold')
/*
* Draw the path
*/
const from = mc.from.shiftFractionTowards(mc.to, mc.margin / 100)
const to = mc.to.shiftFractionTowards(mc.from, mc.margin / 100)
const via1 = from.shiftTowards(mc.from, mc.offset * scale).rotate(-90, from)
const via2 = to.shiftTowards(mc.to, mc.offset * scale).rotate(90, to)
paths[ids.line] = new Path()
.move(from)
.line(via1)
.line(via2)
.line(to)
.attr('class', mc.classes.line)
.attr('marker-start', 'url(#cutonfoldFrom)') .attr('marker-start', 'url(#cutonfoldFrom)')
.attr('marker-end', 'url(#cutonfoldTo)') .attr('marker-end', 'url(#cutonfoldTo)')
.attr('data-text', text) .addText(mc.grainline ? 'cutOnFoldAndGrainline' : 'cutOnFold', mc.classes.text)
.attr('data-text-class', 'center fill-note')
} /*
}, * Store all IDs in the store so we can remove this macro with rmcutonfold
*/
store.set(['parts', part.name, 'macros', 'cutonfold', 'ids', mc.id, 'paths'], ids)
} }
// Export macros
export const cutonfoldMacros = { cutonfold, rmcutonfold }