@zsnout/ithkuil
Version:
A set of tools which can generate and parse romanized Ithkuil text and which can generate Ithkuil script from text and JSON data.
104 lines (103 loc) • 4.75 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "@zsnout/ithkuil-jsx/jsx-runtime";
import { Diacritic } from "../other/diacritic.js";
import { Row } from "../other/row.js";
import { Anchor } from "../utilities/anchor.js";
import { getBBox } from "../utilities/get-bbox.js";
import { rotate180AndRotateStartingPoint } from "../utilities/rotate-180.js";
import { Translate } from "../utilities/translate.js";
import { CORES, HANDWRITTEN_CORES } from "./core.js";
import { EXTENSIONS } from "./extension.js";
function noop(x) {
return x;
}
function Extension({ handwritten, name, direction, reversed, }) {
const top = EXTENSIONS[name];
const check = reversed ? rotate180AndRotateStartingPoint : noop;
if (direction == "horiz") {
return _jsx("path", { d: check(handwritten ? top.horiz2 : top.horiz) });
}
if (direction == "vert") {
return _jsx("path", { d: check(handwritten ? top.vert2 : top.vert) });
}
if (direction == "diag") {
return _jsx("path", { d: check(top.diag) });
}
throw new Error("Invalid direction: " + direction + ".");
}
function TopExtension({ core, coreShape, handwritten, name, }) {
return (_jsx("g", { children: _jsx(Translate, { x: core.top[1] +
(handwritten ? 0
: core.top[2] ? -10
: 0), y: getBBox(coreShape).y +
(handwritten ? 0
: core.top[2] ? 10
: 0), children: _jsx(Extension, { name: name, handwritten: handwritten, direction: core.top[0], reversed: handwritten ?
(core.top[0] == "horiz") == !core.top[2]
: !!core.top[2] }) }) }));
}
function BottomExtension({ core, coreShape, handwritten, name, }) {
const reversed = !core.bottom[2];
return (_jsx("g", { children: _jsx(Translate, { x: core.bottom[1] +
(handwritten ? 0
: reversed ? 0
: 10), y: getBBox(coreShape).y +
getBBox(coreShape).height +
(handwritten ? 0
: reversed ? 0
: -10), children: _jsx(Extension, { name: name, direction: core.bottom[0], handwritten: handwritten, reversed: reversed }) }) }));
}
function rotate(core, handwritten) {
return {
top: [
core.bottom[0],
-core.bottom[1],
handwritten ?
core.bottom[0] == "horiz" ?
!core.bottom[2]
: !!core.bottom[2]
: core.bottom[0] == "horiz",
],
bottom: [
core.top[0],
-core.top[1],
handwritten ?
core.top[0] == "horiz" ?
!core.top[2]
: !!core.top[2]
: false,
],
shape: rotate180AndRotateStartingPoint(core.shape),
};
}
/**
* Assembles a secondary character as an group of SVG paths.
*
* @param secondary Properties that modify the character.
* @returns An `SVGGElement` containing the character.
*/
export function Secondary(secondary) {
const handwritten = !!secondary.handwritten;
const core = secondary.rotated ?
rotate((handwritten ? HANDWRITTEN_CORES : CORES)[secondary.core || "STANDARD_PLACEHOLDER"], handwritten)
: (handwritten ? HANDWRITTEN_CORES : CORES)[secondary.core || "STANDARD_PLACEHOLDER"];
const coreShape = (_jsx("path", { d: core.shape }));
let main = (_jsxs("g", { children: [coreShape, secondary.top ?
_jsx(TopExtension, { core: core, coreShape: coreShape, handwritten: handwritten, name: secondary.top })
: undefined, secondary.bottom ?
_jsx(BottomExtension, { core: core, coreShape: coreShape, handwritten: handwritten, name: secondary.bottom })
: undefined] }));
if (secondary.superposed) {
const box = getBBox(main);
const diacritic = (_jsx(Anchor, { at: "bc", x: box.x + box.width / 2, y: box.y - 10, children: _jsx(Diacritic, { handwritten: handwritten, name: secondary.superposed }) }));
main.appendChild(diacritic);
}
if (secondary.underposed) {
const box = getBBox(main);
const diacritic = (_jsx(Anchor, { at: "tc", x: box.x + box.width / 2, y: box.y + box.height + 10, children: _jsx(Diacritic, { handwritten: handwritten, name: secondary.underposed }) }));
main.appendChild(diacritic);
}
if (secondary.right) {
main = (_jsx(Row, { compact: true, space: handwritten ? 15 : 10, intro: [...main.querySelectorAll("path")], children: _jsx(Anchor, { at: "cl", children: _jsx(Diacritic, { handwritten: handwritten, name: secondary.right }) }) }));
}
return main;
}