SOURCE / PINNED RELEASE
Made of little things.
Nappakura
- Release
- a75ca0f8b596…
- Author-recorded commit
- 4463126213fa…
- License
- LICENSE
- Author’s source reference
- nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-8db03001b62
Archive hash verified: b448d77b767ae754…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
import { internalEdge, makeMesh } from './mesh.js';
import type { Mesh } from './mesh.js';
import { DEFAULT_SETTINGS, unfold, newPattern } from './unfold.js';
import type { Pattern, PrintSettings } from './unfold.js';
import { deriveSolid } from './modelling.js';
import type { Solid } from './modelling.js';
export interface Document {
mesh: Mesh;
pattern: Pattern;
editable?: Solid;
}
export function serialize(doc: Document): string {
return JSON.stringify({
format: 'nappakura',
version: doc.editable ? 2 : 1,
...(doc.editable ? { editable: doc.editable } : {}),
mesh: {
name: doc.mesh.name,
vertices: doc.mesh.vertices,
faces: doc.mesh.faces,
inverted: doc.mesh.inverted,
},
pattern: {
...doc.pattern,
pieces: doc.pattern.pieces.map(({ id, x, y, rotation, page }) => ({
id,
x,
y,
rotation,
page,
})),
},
});
}
const bounded = (v: unknown, min: number, max: number) =>
typeof v === 'number' && Number.isFinite(v) && v >= min && v <= max;
export function deserialize(text: string): Document {
if (text.length > 8 * 1024 * 1024) throw new Error('Project exceeds 8 MiB.');
try {
const data = JSON.parse(text);
if (data.format !== 'nappakura' || ![1, 2].includes(data.version)) throw new Error();
const mesh =
data.version === 2
? deriveSolid(data.editable, data.mesh.name).mesh
: makeMesh(data.mesh.vertices, data.mesh.faces, data.mesh.name, false);
if (data.mesh.inverted === true) {
mesh.inverted = true;
mesh.normals = mesh.normals.map((n) => n.map((v) => -v) as typeof n);
}
const raw = data.pattern,
s = raw.settings as PrintSettings;
if (
!bounded(s.size, 1, 2000) ||
!['a4', 'letter'].includes(s.paper) ||
typeof s.landscape !== 'boolean' ||
!bounded(s.margin, 8, 40) ||
!bounded(s.tabDepth, 1, 15) ||
!bounded(s.tabTaper, 0.05, 0.45) ||
!bounded(s.lineWidth, 0.1, 1) ||
!bounded(s.labelSize, 1.5, 5) ||
typeof s.labels !== 'boolean' ||
typeof s.tabs !== 'boolean'
)
throw new Error();
if (data.version === 2 && Math.abs(Math.max(...mesh.dimensions) - s.size) > 1e-6)
throw new Error('Physical scale does not match editable geometry.');
for (const list of [raw.joined, raw.flipped, raw.hiddenTabs])
if (
!Array.isArray(list) ||
list.length > mesh.edges.length ||
new Set(list).size !== list.length ||
list.some((i) => !Number.isInteger(i) || i < 0 || i >= mesh.edges.length)
)
throw new Error();
if (raw.joined.some((i: number) => mesh.edges[i].faces.length !== 2)) throw new Error();
if (mesh.edges.some((e) => internalEdge(mesh, e) && !raw.joined.includes(e.id))) {
// Old versions allowed planar rendering diagonals to become paper seams.
// Re-unfold rather than treating old piece coordinates as the new topology.
mesh.warnings.push(
'Rebuilt the paper layout to remove internal polygon diagonals. Review the new sheets before printing.',
);
return {
mesh,
...(data.version === 2 ? { editable: data.editable as Solid } : {}),
pattern: newPattern(mesh, { ...DEFAULT_SETTINGS, ...s }),
};
}
const { pieces, joined } = unfold(mesh, new Set(raw.joined));
if (!Array.isArray(raw.pieces) || raw.pieces.length !== pieces.length) throw new Error();
pieces.forEach((piece, i) => {
const p = raw.pieces[i];
if (
p.id !== i ||
!bounded(p.x, -10000, 10000) ||
!bounded(p.y, -10000, 10000) ||
!bounded(p.rotation, -36000, 36000) ||
!Number.isInteger(p.page) ||
p.page < 0 ||
p.page > 63
)
throw new Error();
Object.assign(piece, {
x: p.x,
y: p.y,
rotation: p.rotation,
page: p.page,
});
});
return {
mesh,
...(data.version === 2 ? { editable: data.editable as Solid } : {}),
pattern: {
pieces,
joined,
flipped: raw.flipped,
hiddenTabs: raw.hiddenTabs,
settings: { ...DEFAULT_SETTINGS, ...s },
},
};
} catch {
throw new Error('This is not a valid Nappakura project. The current document was kept.');
}
}
