Skip to main content

LIBRARY

Constant LIBRARY 

Source
pub const LIBRARY: &str = "// The draw.land library, as JavaScript sees it. The library itself is Rust\n// (`drawland-graph`): every check, default and message is there, the same for\n// Python and Rust. This is only its JavaScript face: the namespaces, handles\n// as objects, and the three calls that take a function (`view`, a `remap`\n// warp, `burin.terrain`), which run here, in JavaScript.\n//\n// It is a function of `dl`, the library\'s calls: a profile\'s interpreter\n// (drawland-script) gives it the graph the script builds and puts what it\n// returns in the script\'s globals; the Node package gives it a `Graph` of its\n// own and returns it as that graph\'s face.\n(function (dl) {\n  \"use strict\";\n\n  // A handle from the library: features, an area, lines. `$` is what the\n  // library knows it by.\n  class Handle {\n    constructor(marked) { this.$ = marked; Object.freeze(this); }\n  }\n\n  // A value on its way to the library: handles as they are marked, functions\n  // as a mark (the library says where it takes none).\n  const enc = (v) => {\n    if (v instanceof Handle) return v.$;\n    if (typeof v === \"function\") return { $function: true };\n    if (Array.isArray(v)) return v.map(enc);\n    if (v !== null && typeof v === \"object\" && !(v instanceof Part)) {\n      const out = {};\n      for (const k of Object.keys(v)) if (v[k] !== undefined) out[k] = enc(v[k]);\n      return out;\n    }\n    if (v instanceof Part) return v.$;\n    return v;\n  };\n\n  // A module\'s parts made when first read (`graticule.keys`, `labels.area`).\n  class Part {\n    constructor(marked) {\n      this.$ = marked;\n      for (const name of marked.parts) {\n        let made;\n        Object.defineProperty(this, name, { enumerable: true, get: () => made || (made = dec(dl.get(marked, name))) });\n      }\n      Object.freeze(this);\n    }\n  }\n\n  // A value from the library.\n  const dec = (v) => {\n    if (Array.isArray(v)) return v.map(dec);\n    if (v === null || typeof v !== \"object\") return v;\n    if (\"$features\" in v || \"$area\" in v || \"$lines\" in v) return new Handle(v);\n    if (\"$lazy\" in v) return new Part(v);\n    const out = {};\n    for (const k of Object.keys(v)) out[k] = dec(v[k]);\n    return Object.freeze(out);\n  };\n\n  const call = (name) => (...args) => dec(dl.call(name, enc(args)));\n  const prop = (name) => () => dec(dl.prop(name));\n\n  const lib = {};\n  for (const name of dl.functions()) {\n    const [space, fn] = name.split(\".\");\n    if (fn === undefined) lib[space] = call(name);\n    else (lib[space] || (lib[space] = {}))[fn] = call(name);\n  }\n  for (const name of dl.properties()) {\n    const [space, key] = name.split(\".\");\n    Object.defineProperty(lib[space] || (lib[space] = {}), key, { enumerable: true, get: prop(name) });\n  }\n\n  // `output` samples a `remap` warp here: the function runs on a grid of\n  // page points, `page.toWorld` answering through the warps before it, and\n  // what it gives goes to the engine as a mesh.\n  lib.output = (spec) => {\n    if (!dl.outputBegin(enc(spec))) return;\n    const warps = spec.warp == null ? [] : Array.isArray(spec.warp) ? spec.warp : [spec.warp];\n    const done = [];\n    warps.forEach((w, n) => {\n      if (w != null && typeof w === \"object\" && \"remap\" in w) {\n        dl.outputWarps(enc(done));\n        const { columns, rows, points } = dl.remapGrid(enc(w), n);\n        const went = points.map((p) => w.remap(p));\n        done.push(dec(dl.remapMesh(n, columns, rows, points, enc(went))));\n      } else {\n        done.push(w);\n      }\n    });\n    dl.outputWarps(enc(done));\n  };\n\n  // Another place, at its own scale, drawn into a rectangle of this page:\n  //   view({ id: \"beach\", at: [0, 0], size: [74, 50], lat, lng, scale: \"1:6000\" }, \"city-grain\")\n  //   view({ id: \"park\", \u{2026} }, () => { draw({ \u{2026} }) })\n  // The style is a script the host lends by name, or a function whose calls\n  // build the view\'s own graph. Returns the view\'s id.\n  lib.view = (spec, style) => {\n    if (typeof style === \"string\") return dl.viewStyle(enc(spec), style);\n    if (typeof style !== \"function\") {\n      dl.fail(`view: the style is a name the host knows (\"city-grain\") or a function that draws, got ${dl.describe(enc(style))}`);\n    }\n    dl.viewBegin(enc(spec));\n    let ok = false;\n    try {\n      style();\n      ok = true;\n    } finally {\n      dl.viewEnd(ok);\n    }\n    return spec.id;\n  };\n\n  // A burin document, written here in burin\'s own language: the callback\n  // runs in this engine, so it is linted, completed and its errors carry\n  // this script\'s positions. The atlas it builds is drawn by the engine.\n  //   burin.terrain((b) => { b.batch({ angle: 90 }); b.shore({ lines: 2 }) })\n  // Or the document\'s JSON, as burin\'s own front ends build it.\n  const burinTerrain = (build, options) => {\n    if (typeof build !== \"function\" && (build === null || typeof build !== \"object\")) {\n      dl.fail(`burin.terrain expects a function: burin.terrain((b) => { \u{2026} }), got ${dl.describe(enc(build))}`);\n    }\n    const atlas = typeof build === \"function\" ? dl.burin(build) : build;\n    return dec(dl.call(\"burin.terrain\", enc([atlas, options])));\n  };\n  lib.burin.terrain = burinTerrain;\n  lib.burin.script = burinTerrain;\n\n  for (const value of Object.values(lib)) if (typeof value === \"object\") Object.freeze(value);\n  return Object.freeze(lib);\n})\n";
Expand description

The library’s JavaScript face: a function of the library’s calls (dl) that returns the namespaces a script calls. The Node package runs it too.