2020-05-28 22:18:41 +02:00
|
|
|
const m = require('makerjs')
|
2020-05-31 20:33:23 +02:00
|
|
|
const u = require('./utils')
|
|
|
|
|
|
2020-05-29 22:35:49 +02:00
|
|
|
const push_rotation = (list, angle, origin) => {
|
|
|
|
|
let candidate = origin
|
|
|
|
|
for (const r of list) {
|
|
|
|
|
candidate = m.point.rotate(candidate, r.angle, r.origin)
|
|
|
|
|
}
|
|
|
|
|
list.push({
|
|
|
|
|
angle: angle,
|
|
|
|
|
origin: candidate
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
const render_zone = (cols, rows, anchor=new Point(), reverse=false) => {
|
2020-05-29 22:35:49 +02:00
|
|
|
|
|
|
|
|
const sign = reverse ? -1 : 1
|
2020-05-28 22:18:41 +02:00
|
|
|
const points = {}
|
2020-05-30 15:17:53 +02:00
|
|
|
const rotations = []
|
|
|
|
|
|
|
|
|
|
// transferring the anchor rotation to "real" rotations
|
|
|
|
|
rotations.push({
|
2020-05-29 22:35:49 +02:00
|
|
|
angle: anchor.r,
|
2020-05-30 15:17:53 +02:00
|
|
|
origin: anchor.p
|
|
|
|
|
})
|
|
|
|
|
|
2020-05-29 22:35:49 +02:00
|
|
|
for (const col of cols) {
|
|
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
anchor.y += col.stagger || 0
|
|
|
|
|
const col_anchor = anchor.clone()
|
|
|
|
|
// clear potential rotations, as they will get re-applied anyway
|
|
|
|
|
// and we don't want to apply them twice...
|
|
|
|
|
col_anchor.r = 0
|
|
|
|
|
|
2020-06-03 22:16:45 +02:00
|
|
|
// combine row data from zone-wide defs and col-specific defs
|
|
|
|
|
const col_specific = col.rows || []
|
|
|
|
|
const zone_wide = rows || []
|
|
|
|
|
const actual_rows = []
|
|
|
|
|
for (let i = 0; i < zone_wide.length && i < col_specific.length; ++i) {
|
|
|
|
|
actual_rows.push(Object.assign({}, zone_wide[i], col_specific[i]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const row of actual_rows) {
|
2020-05-30 15:17:53 +02:00
|
|
|
let point = col_anchor.clone()
|
|
|
|
|
for (const r of rotations) {
|
|
|
|
|
point.rotate(r.angle, r.origin)
|
|
|
|
|
}
|
|
|
|
|
point.r += col.angle || 0
|
2020-06-03 22:16:45 +02:00
|
|
|
const name = `${col.name}_${row.name}`
|
|
|
|
|
point.meta = {col, row, name}
|
|
|
|
|
points[name] = point
|
2020-05-29 22:35:49 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
col_anchor.y += row.padding || 19
|
2020-05-29 22:35:49 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
if (col.rotate) {
|
|
|
|
|
push_rotation(
|
|
|
|
|
rotations,
|
|
|
|
|
col.rotate,
|
|
|
|
|
anchor.add(col.origin || [0, 0]).p
|
2020-05-29 22:35:49 +02:00
|
|
|
)
|
2020-05-28 22:18:41 +02:00
|
|
|
}
|
2020-05-29 22:35:49 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
anchor.x += sign * (col.padding || 19)
|
|
|
|
|
}
|
2020-05-29 22:35:49 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
return points
|
|
|
|
|
}
|
2020-05-29 22:35:49 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
const anchor = (raw, points={}) => {
|
|
|
|
|
let a = new Point()
|
|
|
|
|
if (raw) {
|
|
|
|
|
if (raw.ref && points[raw.ref]) {
|
|
|
|
|
a = points[raw.ref].clone()
|
2020-05-28 22:18:41 +02:00
|
|
|
}
|
2020-05-30 15:17:53 +02:00
|
|
|
if (raw.shift) {
|
|
|
|
|
a.x += raw.shift[0]
|
|
|
|
|
a.y += raw.shift[1]
|
|
|
|
|
}
|
|
|
|
|
a.r += raw.angle || 0
|
2020-05-28 22:18:41 +02:00
|
|
|
}
|
2020-05-30 15:17:53 +02:00
|
|
|
return a
|
2020-05-29 22:35:49 +02:00
|
|
|
}
|
2020-05-28 22:18:41 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
exports.parse = (config) => {
|
2020-05-29 22:35:49 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
let points = {}
|
2020-05-29 22:35:49 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
for (const zone of Object.values(config.zones)) {
|
|
|
|
|
points = Object.assign(points, render_zone(
|
|
|
|
|
zone.columns || [],
|
|
|
|
|
zone.rows || [{name: 'default'}],
|
|
|
|
|
anchor(zone.anchor, points),
|
|
|
|
|
!!zone.reverse
|
|
|
|
|
))
|
|
|
|
|
}
|
2020-05-29 22:35:49 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
if (config.angle) {
|
|
|
|
|
for (const p of Object.values(points)) {
|
|
|
|
|
p.rotate(config.angle)
|
2020-05-28 22:18:41 +02:00
|
|
|
}
|
2020-05-29 22:35:49 +02:00
|
|
|
}
|
2020-05-28 22:18:41 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
if (config.mirror) {
|
2020-06-03 22:16:45 +02:00
|
|
|
let axis = anchor(config.mirror, points).x
|
|
|
|
|
axis += (config.mirror.distance || 0) / 2
|
2020-05-30 15:17:53 +02:00
|
|
|
const mirrored_points = {}
|
|
|
|
|
for (const [name, p] of Object.entries(points)) {
|
2020-05-31 20:33:23 +02:00
|
|
|
if (p.meta.col.asym == 'left' || p.meta.row.asym == 'left') continue
|
2020-06-03 22:16:45 +02:00
|
|
|
const mp = p.clone().mirror(axis)
|
2020-05-30 15:17:53 +02:00
|
|
|
mp.meta.mirrored = true
|
2020-05-31 20:33:23 +02:00
|
|
|
delete mp.meta.asym
|
|
|
|
|
mirrored_points[`mirror_${name}`] = mp
|
|
|
|
|
if (p.meta.col.asym == 'right' || p.meta.row.asym == 'right') {
|
|
|
|
|
p.meta.col.skip = true
|
|
|
|
|
}
|
2020-05-30 15:17:53 +02:00
|
|
|
}
|
|
|
|
|
Object.assign(points, mirrored_points)
|
2020-05-28 22:18:41 +02:00
|
|
|
}
|
2020-05-31 20:33:23 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
const filtered = {}
|
|
|
|
|
for (const [k, p] of Object.entries(points)) {
|
|
|
|
|
if (p.meta.col.skip || p.meta.row.skip) continue
|
|
|
|
|
filtered[k] = p
|
|
|
|
|
}
|
2020-05-28 22:18:41 +02:00
|
|
|
|
2020-05-30 15:17:53 +02:00
|
|
|
return filtered
|
2020-05-28 22:18:41 +02:00
|
|
|
}
|