slides_shapes.js 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  1. /*
  2. ArozOS Office - Slides shape catalogue
  3. ======================================
  4. Every shape the editor can draw, as geometry rather than as a picture.
  5. The names are the ones PresentationML uses for its preset geometries
  6. (`<a:prstGeom prst="rightBrace"/>`), so an imported deck keeps its own
  7. vocabulary and the converters on the Go side are a lookup rather than a
  8. translation. A handful of older editor names (`round`, `arrow`, `star`,
  9. ...) are kept as aliases, because documents already written carry them.
  10. Each shape produces an SVG path, and deliberately only ever uses M, L, C
  11. and Z:
  12. - `clip-path: path(...)` takes it, which is how a picture is cropped
  13. to a shape,
  14. - the PDF exporter turns it into path operators with a translator of
  15. about thirty lines (slides_pdf.js), and
  16. - there is nothing to get wrong in an arc flag.
  17. Circles and arcs therefore arrive as cubic curves; `arc()` below does
  18. that conversion once so no shape has to think about it.
  19. Some shapes have a second path - the divider bars of a predefined
  20. process, the cross of a summing junction. Those are *drawn*, not filled:
  21. they are the shape's markings, not its outline, so they come back
  22. separately from `detail()` and the renderer strokes them.
  23. Usage:
  24. SlidesShapes.path(kind, w, h, adj) // "M0 0L100 0..." ("" if unknown)
  25. SlidesShapes.detail(kind, w, h, adj) // markings to stroke, or ""
  26. SlidesShapes.points(kind, w, h, adj) // polygon corners, or null
  27. SlidesShapes.handles(kind, w, h, adj) // [[x, y], ...] adjustment handles
  28. SlidesShapes.dragHandle(kind, i, w, h, x, y, adj) // adj with handle i at (x, y)
  29. SlidesShapes.evenOdd(kind) // does the path need even-odd fill
  30. SlidesShapes.canonical(kind) // legacy name -> catalogue name
  31. SlidesShapes.has(kind)
  32. SlidesShapes.label(kind)
  33. SlidesShapes.CATEGORIES // [{ id, label, kinds: [...] }]
  34. */
  35. var SlidesShapes = (function () {
  36. "use strict";
  37. function r2(v) { return Math.round(v * 1000) / 1000; }
  38. /* ---------------- the path builder ---------------- */
  39. // the path remembers where its pen is (px, py), which is what arcTo
  40. // continues from
  41. function Path() { this.s = ""; this.px = 0; this.py = 0; }
  42. Path.prototype.M = function (x, y) {
  43. this.s += "M" + r2(x) + " " + r2(y); this.px = x; this.py = y; return this;
  44. };
  45. Path.prototype.L = function (x, y) {
  46. this.s += "L" + r2(x) + " " + r2(y); this.px = x; this.py = y; return this;
  47. };
  48. Path.prototype.C = function (x1, y1, x2, y2, x, y) {
  49. this.s += "C" + r2(x1) + " " + r2(y1) + " " + r2(x2) + " " + r2(y2) +
  50. " " + r2(x) + " " + r2(y);
  51. this.px = x; this.py = y;
  52. return this;
  53. };
  54. Path.prototype.Z = function () { this.s += "Z"; return this; };
  55. Path.prototype.poly = function (pts) {
  56. for (var i = 0; i < pts.length; i++) {
  57. if (i === 0) this.M(pts[i][0], pts[i][1]);
  58. else this.L(pts[i][0], pts[i][1]);
  59. }
  60. return this.Z();
  61. };
  62. /* arc appends an elliptical arc from a0 to a1 (radians, y down). It
  63. moves to the start when the path is empty or `move` is asked for,
  64. and otherwise draws a line to it first, which is what every shape
  65. below wants. Cut into quarter turns, each a cubic - the standard
  66. approximation, exact to about one part in ten thousand. */
  67. Path.prototype.arc = function (cx, cy, rx, ry, a0, a1, move) {
  68. var span = a1 - a0;
  69. var steps = Math.max(1, Math.ceil(Math.abs(span) / (Math.PI / 2)));
  70. var step = span / steps;
  71. var k = 4 / 3 * Math.tan(step / 4);
  72. var a = a0;
  73. var x0 = cx + rx * Math.cos(a), y0 = cy + ry * Math.sin(a);
  74. if (move || this.s === "") this.M(x0, y0);
  75. else this.L(x0, y0);
  76. for (var i = 0; i < steps; i++) {
  77. var b = a + step;
  78. var cosA = Math.cos(a), sinA = Math.sin(a);
  79. var cosB = Math.cos(b), sinB = Math.sin(b);
  80. var p0x = cx + rx * cosA, p0y = cy + ry * sinA;
  81. var p3x = cx + rx * cosB, p3y = cy + ry * sinB;
  82. this.C(p0x - k * rx * sinA, p0y + k * ry * cosA,
  83. p3x + k * rx * sinB, p3y - k * ry * cosB, p3x, p3y);
  84. a = b;
  85. }
  86. return this;
  87. };
  88. /* arcTo is PresentationML's own arc: from the pen, which lies on an
  89. ellipse of radii wR x hR at the angle stAng, round by swAng
  90. (radians, clockwise on screen, negative goes back). The angles are
  91. *visual* ones - the direction of the point as seen from the centre,
  92. not the ellipse's parameter - which is what presetShapeDefinitions
  93. means by them; on a circle the two are the same. */
  94. function ellParam(a, rx, ry) { return Math.atan2(rx * Math.sin(a), ry * Math.cos(a)); }
  95. Path.prototype.arcTo = function (wR, hR, stAng, swAng) {
  96. if (!swAng || !isFinite(wR) || !isFinite(hR)) return this;
  97. // a radius of nothing still has to end where PowerPoint ends it:
  98. // a flattened ellipse is a straight line along the other axis
  99. wR = Math.max(wR, 1e-6); hR = Math.max(hR, 1e-6);
  100. var t0 = ellParam(stAng, wR, hR);
  101. var cx = this.px - wR * Math.cos(t0), cy = this.py - hR * Math.sin(t0);
  102. var dt;
  103. if (Math.abs(swAng) >= 2 * Math.PI - 1e-9) dt = swAng > 0 ? 2 * Math.PI : -2 * Math.PI;
  104. else {
  105. // the parameter turns the same way as the angle and less than a
  106. // full turn, because one maps onto the other one to one
  107. dt = ellParam(stAng + swAng, wR, hR) - t0;
  108. while (swAng > 0 && dt <= 0) dt += 2 * Math.PI;
  109. while (swAng < 0 && dt >= 0) dt -= 2 * Math.PI;
  110. while (dt > 2 * Math.PI) dt -= 2 * Math.PI;
  111. while (dt < -2 * Math.PI) dt += 2 * Math.PI;
  112. }
  113. // arc() opens with a line to where the pen already is - harmless
  114. return this.arc(cx, cy, wR, hR, t0, t0 + dt);
  115. };
  116. // a quadratic curve, as the cubic it is
  117. Path.prototype.Q = function (qx, qy, x, y) {
  118. var x0 = this.px, y0 = this.py;
  119. return this.C(x0 + 2 / 3 * (qx - x0), y0 + 2 / 3 * (qy - y0),
  120. x + 2 / 3 * (qx - x), y + 2 / 3 * (qy - y), x, y);
  121. };
  122. Path.prototype.circle = function (cx, cy, rx, ry) {
  123. return this.arc(cx, cy, rx, ry, 0, 2 * Math.PI, true).Z();
  124. };
  125. Path.prototype.toString = function () { return this.s; };
  126. function P() { return new Path(); }
  127. // regular polygon inscribed in the box, first corner at the top
  128. function ngon(w, h, n, rotate) {
  129. var pts = [];
  130. var cx = w / 2, cy = h / 2;
  131. var off = (rotate || 0) - Math.PI / 2;
  132. for (var i = 0; i < n; i++) {
  133. var a = off + i * 2 * Math.PI / n;
  134. pts.push([cx + cx * Math.cos(a), cy + cy * Math.sin(a)]);
  135. }
  136. return pts;
  137. }
  138. // a star with n points; inner is the inner radius as a fraction
  139. function starPts(w, h, n, inner) {
  140. var pts = [];
  141. var cx = w / 2, cy = h / 2;
  142. for (var i = 0; i < n * 2; i++) {
  143. var a = -Math.PI / 2 + i * Math.PI / n;
  144. var f = (i % 2 === 0) ? 1 : inner;
  145. pts.push([cx + cx * f * Math.cos(a), cy + cy * f * Math.sin(a)]);
  146. }
  147. return pts;
  148. }
  149. // rounded rectangle, with a radius per corner (tl, tr, br, bl)
  150. function roundRectPath(w, h, tl, tr, br, bl) {
  151. var p = P();
  152. p.M(tl, 0).L(w - tr, 0);
  153. if (tr) p.arc(w - tr, tr, tr, tr, -Math.PI / 2, 0);
  154. p.L(w, h - br);
  155. if (br) p.arc(w - br, h - br, br, br, 0, Math.PI / 2);
  156. p.L(bl, h);
  157. if (bl) p.arc(bl, h - bl, bl, bl, Math.PI / 2, Math.PI);
  158. p.L(0, tl);
  159. if (tl) p.arc(tl, tl, tl, tl, Math.PI, Math.PI * 1.5);
  160. return p.Z().toString();
  161. }
  162. // the straight-sided arrow every four directions share, drawn right
  163. // and then mapped onto the direction asked for
  164. function arrowPts(w, h, headLen, shaft) {
  165. var hx = w * (1 - headLen);
  166. var t = h * (1 - shaft) / 2;
  167. return [[0, t], [hx, t], [hx, 0], [w, h / 2], [hx, h], [hx, h - t], [0, h - t]];
  168. }
  169. function mapPts(pts, fn) { return pts.map(fn); }
  170. /* av reads one of a preset's adjustments (PresentationML's own name and
  171. scale, 1/100000), its default when the shape states none. Presets
  172. that follow PowerPoint's formulas size their features from ss, the
  173. shorter side, as presetShapeDefinitions.xml does - so a long, flat
  174. chevron keeps a shallow point instead of one a quarter of its width. */
  175. function av(adj, name, def) {
  176. var v = adj ? Number(adj[name]) : NaN;
  177. return isFinite(v) ? v : def;
  178. }
  179. function pin(lo, v, hi) { return Math.max(lo, Math.min(hi, v)); }
  180. // a block arrow pointing along +u, in a box len long and thick across
  181. function arrowGeo(len, thick, ss, adj) {
  182. var a1 = pin(0, av(adj, "adj1", 50000), 100000);
  183. var a2 = pin(0, av(adj, "adj2", 50000), 100000 * len / ss);
  184. var dy = thick * a1 / 200000, v1 = thick / 2 - dy, v2 = thick / 2 + dy;
  185. var u1 = len - ss * a2 / 100000;
  186. return [[0, v1], [u1, v1], [u1, 0], [len, thick / 2], [u1, thick], [u1, v2], [0, v2]];
  187. }
  188. /* ---------------- adjustment handles ----------------
  189. An entry with `adj` can be reshaped on the canvas: `adj.defaults`
  190. are PresentationML's defaults for its guides (the values its drawing
  191. uses when the shape states none), and each of `adj.handles` is one
  192. yellow diamond - pos(w, h, a) puts it in the frame for the resolved
  193. adjustments a, set(w, h, x, y) turns the point it was dragged to back
  194. into the guide it controls. The guides keep PowerPoint's names and
  195. scale, so a reshaped shape is written to .pptx as exactly that.
  196. Most presets measure a feature in ss, the shorter side: ssLen is the
  197. handle for one such length. `at` places the handle for a length d,
  198. `from` reads d back off a dragged point, and `max` caps the guide
  199. (a number, or a function of w, h and ss). */
  200. function capOf(max, w, h, ss, a) { return typeof max === "function" ? max(w, h, ss, a || {}) : max; }
  201. function ssLen(name, max, at, from) {
  202. return {
  203. pos: function (w, h, a) {
  204. var ss = Math.min(w, h);
  205. return at(w, h, ss * pin(0, a[name], capOf(max, w, h, ss, a)) / 100000);
  206. },
  207. set: function (w, h, x, y, a) {
  208. var ss = Math.min(w, h), out = {};
  209. out[name] = Math.round(pin(0, from(w, h, x, y) / Math.max(1e-6, ss) * 100000,
  210. capOf(max, w, h, ss, a)));
  211. return out;
  212. }
  213. };
  214. }
  215. /* fit is the handle for a guide whose effect on the handle has no
  216. simple inverse (a curved arrow, a brace, an arrow callout): pos(w, h,
  217. a) places it, and a drag sets `name` to the value in [lo, hi] whose
  218. point lies nearest the pointer, every other guide held. lo and hi
  219. are numbers or functions of (w, h, ss, a), like a cap. The handle's
  220. path is assumed to be one stroke without loops, which every preset's
  221. is: a coarse scan finds the stretch, a golden-section search the
  222. value. */
  223. function fit(name, lo, hi, pos) {
  224. return {
  225. pos: pos,
  226. set: function (w, h, x, y, a) {
  227. var ss = Math.min(w, h), out = {};
  228. var l = capOf(lo, w, h, ss, a), u = capOf(hi, w, h, ss, a);
  229. if (!(u > l)) { out[name] = Math.round(l); return out; }
  230. var trial = {};
  231. Object.keys(a).forEach(function (k) { trial[k] = a[k]; });
  232. function dist(v) {
  233. trial[name] = v;
  234. var pt = pos(w, h, trial);
  235. return (pt[0] - x) * (pt[0] - x) + (pt[1] - y) * (pt[1] - y);
  236. }
  237. var N = 64, best = 0, bd = Infinity;
  238. for (var i = 0; i <= N; i++) {
  239. var d = dist(l + (u - l) * i / N);
  240. if (d < bd) { bd = d; best = i; }
  241. }
  242. var lo2 = l + (u - l) * Math.max(0, best - 1) / N;
  243. var hi2 = l + (u - l) * Math.min(N, best + 1) / N;
  244. var g = (Math.sqrt(5) - 1) / 2;
  245. for (var k = 0; k < 40; k++) {
  246. var m1 = hi2 - g * (hi2 - lo2), m2 = lo2 + g * (hi2 - lo2);
  247. if (dist(m1) <= dist(m2)) hi2 = m2; else lo2 = m1;
  248. }
  249. out[name] = Math.round(pin(l, (lo2 + hi2) / 2, u));
  250. return out;
  251. }
  252. };
  253. }
  254. /* Angles. PresentationML states them in 60000ths of a degree, clockwise
  255. from three o'clock; they are visual angles (see arcTo). */
  256. var DEG = 60000;
  257. function angleOf(v) { return v / DEG * Math.PI / 180; }
  258. function angleGuide(x, y, cx, cy) {
  259. var d = Math.atan2(y - cy, x - cx) * 180 / Math.PI;
  260. if (d < 0) d += 360;
  261. return Math.round(d * DEG) % (360 * DEG);
  262. }
  263. // the sweep from one angle to the next, clockwise; equal angles are a
  264. // full turn, as PowerPoint draws them
  265. function sweepOf(st, en) {
  266. var sw = en - st;
  267. while (sw <= 0) sw += 2 * Math.PI;
  268. while (sw > 2 * Math.PI) sw -= 2 * Math.PI;
  269. return sw;
  270. }
  271. // the point of an ellipse in the direction a, seen from its centre
  272. function onEllipse(cx, cy, rx, ry, a) {
  273. var c = Math.cos(a), s = Math.sin(a);
  274. if (!(rx > 0) || !(ry > 0)) return [cx, cy];
  275. var r = 1 / Math.sqrt(c * c / (rx * rx) + s * s / (ry * ry));
  276. return [cx + r * c, cy + r * s];
  277. }
  278. // the inset t (0..tMax) whose ellipse (rx - t) x (ry - t) passes at
  279. // distance d from the centre in the direction a: a ring's thickness
  280. // read off a dragged point
  281. function insetAt(rx, ry, a, d, tMax) {
  282. var lo = 0, hi = tMax;
  283. for (var i = 0; i < 40; i++) {
  284. var t = (lo + hi) / 2;
  285. var pt = onEllipse(0, 0, Math.max(0, rx - t), Math.max(0, ry - t), a);
  286. if (Math.sqrt(pt[0] * pt[0] + pt[1] * pt[1]) > d) lo = t; else hi = t;
  287. }
  288. return (lo + hi) / 2;
  289. }
  290. // a handle on the frame's ellipse that sets one angle guide
  291. function polar(name, rx, ry) {
  292. return {
  293. pos: function (w, h, a) {
  294. return onEllipse(w / 2, h / 2, rx ? rx(w, h, a) : w / 2, ry ? ry(w, h, a) : h / 2,
  295. angleOf(pin(0, a[name], 21599999)));
  296. },
  297. set: function (w, h, x, y) {
  298. var out = {};
  299. out[name] = angleGuide(x, y, w / 2, h / 2);
  300. return out;
  301. }
  302. };
  303. }
  304. /* mapPath moves every point of a finished path through fn - a mirror
  305. or a transposition, which is how one geometry serves the four ways
  306. a curved arrow or an arrow callout can face. Only M / L / C carry
  307. points, so mapping each pair is exact. */
  308. function mapPath(d, fn) {
  309. return d.replace(/([MLC])([^MLCZ]*)/g, function (all, cmd, nums) {
  310. var v = nums.trim().split(/[\s,]+/).map(Number), out = [];
  311. for (var k = 0; k + 1 < v.length; k += 2) {
  312. var q = fn(v[k], v[k + 1]);
  313. out.push(r2(q[0]), r2(q[1]));
  314. }
  315. return cmd + out.join(" ");
  316. });
  317. }
  318. // the common caps: so much of the frame's width / height, in ss terms
  319. function wCap(k) { return function (w, h, ss) { return k * w / Math.max(1e-6, ss); }; }
  320. function hCap(k) { return function (w, h, ss) { return k * h / Math.max(1e-6, ss); }; }
  321. // a guide that is a share of one side rather than of ss
  322. function share(name, side, max, at, from) {
  323. return {
  324. pos: function (w, h, a) {
  325. var len = side === "w" ? w : h;
  326. return at(w, h, len * pin(0, a[name], max) / 100000);
  327. },
  328. set: function (w, h, x, y) {
  329. var len = side === "w" ? w : h, out = {};
  330. out[name] = Math.round(pin(0, from(w, h, x, y) / Math.max(1e-6, len) * 100000, max));
  331. return out;
  332. }
  333. };
  334. }
  335. /* The four block arrows share one geometry drawn pointing right (u
  336. along the arrow, v across it) and differ only in how (u, v) lands in
  337. the frame. Their handles do too: the shaft's thickness at the tail,
  338. the head's length where it meets the shaft. */
  339. var ARROW_DIRS = {
  340. right: { to: function (w, h, u, v) { return [u, v]; }, back: function (w, h, x, y) { return [x, y]; }, len: "w" },
  341. left: { to: function (w, h, u, v) { return [w - u, v]; }, back: function (w, h, x, y) { return [w - x, y]; }, len: "w" },
  342. up: { to: function (w, h, u, v) { return [v, h - u]; }, back: function (w, h, x, y) { return [h - y, x]; }, len: "h" },
  343. down: { to: function (w, h, u, v) { return [v, u]; }, back: function (w, h, x, y) { return [y, x]; }, len: "h" }
  344. };
  345. function arrowAdj(dir) {
  346. var D = ARROW_DIRS[dir];
  347. function dims(w, h) { return D.len === "w" ? [w, h] : [h, w]; }
  348. return {
  349. defaults: { adj1: 50000, adj2: 50000 },
  350. handles: [{
  351. // the shaft's thickness, at the tail
  352. pos: function (w, h, a) {
  353. var lt = dims(w, h);
  354. return D.to(w, h, 0, lt[1] / 2 - lt[1] * pin(0, a.adj1, 100000) / 200000);
  355. },
  356. set: function (w, h, x, y) {
  357. var lt = dims(w, h), uv = D.back(w, h, x, y);
  358. return { adj1: Math.round(pin(0, (lt[1] / 2 - uv[1]) * 200000 / Math.max(1e-6, lt[1]), 100000)) };
  359. }
  360. }, {
  361. // the head's length, where it meets the shaft's edge
  362. pos: function (w, h, a) {
  363. var lt = dims(w, h), ss = Math.min(w, h);
  364. var hl = ss * pin(0, a.adj2, 100000 * lt[0] / ss) / 100000;
  365. return D.to(w, h, lt[0] - hl, 0);
  366. },
  367. set: function (w, h, x, y) {
  368. var lt = dims(w, h), ss = Math.min(w, h), uv = D.back(w, h, x, y);
  369. return { adj2: Math.round(pin(0, (lt[0] - uv[0]) / Math.max(1e-6, ss) * 100000, 100000 * lt[0] / ss)) };
  370. }
  371. }]
  372. };
  373. }
  374. // a star's inner radius as a share of the outer, PresentationML's
  375. // adj (inner = adj / 50000); the handle sits on the vertical axis
  376. function starAdj(inner) {
  377. return {
  378. defaults: { adj: Math.round(inner * 50000) },
  379. handles: [{
  380. pos: function (w, h, a) { return [w / 2, h / 2 - h / 2 * pin(0, a.adj, 50000) / 50000]; },
  381. set: function (w, h, x, y) {
  382. return { adj: Math.round(pin(0, (h / 2 - y) / Math.max(1e-6, h / 2) * 50000, 50000)) };
  383. }
  384. }]
  385. };
  386. }
  387. function starDef(n, label, inner) {
  388. return {
  389. cat: "callout", label: label, adj: starAdj(inner),
  390. pts: function (w, h, adj) {
  391. return starPts(w, h, n, pin(0, av(adj, "adj", inner * 50000), 50000) / 50000);
  392. }
  393. };
  394. }
  395. function rotPts(pts, w, h, quarter) {
  396. // quarter turns clockwise about the centre of a w x h box, with the
  397. // box itself turning too - so an arrow drawn right becomes one
  398. // drawn down in the same box
  399. return mapPts(pts, function (p) {
  400. var x = p[0] / w, y = p[1] / h;
  401. if (quarter === 1) return [(1 - y) * w, x * h];
  402. if (quarter === 2) return [(1 - x) * w, (1 - y) * h];
  403. if (quarter === 3) return [y * w, (1 - x) * h];
  404. return [x * w, y * h];
  405. });
  406. }
  407. /* ---------------- geometry shared by several presets ---------------- */
  408. // the frame's ellipse from adj1 round to adj2, clockwise: an arc, and
  409. // the start of a chord and a pie
  410. function arcOutline(w, h, adj, d1, d2) {
  411. var st = angleOf(pin(0, av(adj, "adj1", d1), 21599999));
  412. var sw = sweepOf(st, angleOf(pin(0, av(adj, "adj2", d2), 21599999)));
  413. var o = onEllipse(w / 2, h / 2, w / 2, h / 2, st);
  414. return P().M(o[0], o[1]).arcTo(w / 2, h / 2, st, sw);
  415. }
  416. /* The circular arrow: a band round the frame's ellipse from adj4 to
  417. its point at adj3 (clockwise). adj1 is the band's thickness, adj5 how
  418. far the head reaches either side of the band's centre line (so the
  419. head touches the frame), adj2 the angle the head takes up. */
  420. function circGeo(w, h, adj) {
  421. var ss = Math.min(w, h);
  422. var a5 = pin(0, av(adj, "adj5", 12500), 25000);
  423. var a1 = pin(0, av(adj, "adj1", 12500), a5 * 2);
  424. var thh = ss * a5 / 100000, th = ss * a1 / 100000;
  425. var crx = Math.max(0, w / 2 - thh), cry = Math.max(0, h / 2 - thh);
  426. var st = angleOf(pin(0, av(adj, "adj4", 10800000), 21599999));
  427. var en = angleOf(pin(0, av(adj, "adj3", 20457681), 21599999));
  428. var full = sweepOf(st, en);
  429. var ha = Math.min(angleOf(pin(0, av(adj, "adj2", 1142319), 21599999)), full);
  430. return { ss: ss, a5: a5, thh: thh, th: th, crx: crx, cry: cry, st: st, en: en,
  431. full: full, hb: en - ha, body: full - ha };
  432. }
  433. function circPath(w, h, adj) {
  434. var g = circGeo(w, h, adj), cx = w / 2, cy = h / 2;
  435. var orx = g.crx + g.th / 2, ory = g.cry + g.th / 2;
  436. var irx = Math.max(0, g.crx - g.th / 2), iry = Math.max(0, g.cry - g.th / 2);
  437. var s0 = onEllipse(cx, cy, orx, ory, g.st);
  438. var p = P().M(s0[0], s0[1]);
  439. if (g.body > 0) p.arcTo(orx, ory, g.st, g.body);
  440. [onEllipse(cx, cy, g.crx + g.thh, g.cry + g.thh, g.hb),
  441. onEllipse(cx, cy, g.crx, g.cry, g.en),
  442. onEllipse(cx, cy, Math.max(0, g.crx - g.thh), Math.max(0, g.cry - g.thh), g.hb),
  443. onEllipse(cx, cy, irx, iry, g.hb)].forEach(function (q) { p.L(q[0], q[1]); });
  444. if (g.body > 0) p.arcTo(irx, iry, g.hb, -g.body);
  445. return p.Z().toString();
  446. }
  447. /* The curved arrows, as presetShapeDefinitions draws curvedRightArrow
  448. in a W x H box: a band sweeping round the left of the box - quarter
  449. ellipses of radii W x hR, the inner one th lower - that turns into a
  450. head at the bottom right. adj1 is the band's thickness, adj2 the
  451. head's width, adj3 its length. The other three are the same drawing
  452. mirrored, or in a box turned on its side (CURVE_DIRS). */
  453. function curvedGeo(W, H, adj) {
  454. var ss = Math.min(W, H);
  455. var a2 = pin(0, av(adj, "adj2", 50000), 50000 * H / ss);
  456. var a1 = pin(0, av(adj, "adj1", 25000), a2);
  457. var th = ss * a1 / 100000, aw = ss * a2 / 100000;
  458. var hR = H / 2 - (th + aw) / 4;
  459. var q7 = hR * 2;
  460. var idx = q7 > 0 ? Math.sqrt(Math.max(0, q7 * q7 - th * th)) * W / q7 : 0;
  461. var max3 = 100000 * idx / ss;
  462. var a3 = pin(0, av(adj, "adj3", 25000), max3);
  463. var ah = ss * a3 / 100000;
  464. var dy = W > 0 ? Math.sqrt(Math.max(0, W * W - ah * ah)) * hR / W : 0;
  465. var dh = (aw - th) / 2, y3 = hR + th;
  466. return { a2: a2, max3: max3, th: th, hR: hR, y3: y3, x1: W - ah,
  467. y4: hR + dy - dh, y6: H - aw / 2, y7: y3 + dy, y8: y3 + dy + dh,
  468. sw: Math.atan2(dy, ah) };
  469. }
  470. function curvedPath(W, H, adj) {
  471. var g = curvedGeo(W, H, adj);
  472. return P().M(W, 0).arcTo(W, g.hR, Math.PI * 1.5, -Math.PI / 2 - g.sw)
  473. .L(g.x1, g.y4).L(W, g.y6).L(g.x1, g.y8).L(g.x1, g.y7)
  474. .arcTo(W, g.hR, Math.PI - g.sw, g.sw + Math.PI / 2).Z().toString();
  475. }
  476. var CURVE_DIRS = {
  477. right: { side: false, f: function (w, h, x, y) { return [x, y]; } },
  478. left: { side: false, f: function (w, h, x, y) { return [w - x, y]; } },
  479. down: { side: true, f: function (w, h, x, y) { return [y, x]; } },
  480. up: { side: true, f: function (w, h, x, y) { return [y, h - x]; } }
  481. };
  482. function curvedDef(dir, label) {
  483. var D = CURVE_DIRS[dir];
  484. function box(w, h) { return D.side ? [h, w] : [w, h]; }
  485. // a handle placed in the drawing's own box
  486. function at(fn) {
  487. return function (w, h, a) {
  488. var b = box(w, h), q = fn(curvedGeo(b[0], b[1], a));
  489. return D.f(w, h, q[0], q[1]);
  490. };
  491. }
  492. return {
  493. cat: "arrow", label: label,
  494. adj: {
  495. defaults: { adj1: 25000, adj2: 50000, adj3: 25000 },
  496. handles: [
  497. // the band's thickness, where it turns
  498. fit("adj1", 0, function (w, h, ss, a) { var b = box(w, h); return curvedGeo(b[0], b[1], a).a2; },
  499. at(function (g) { return [0, g.y3]; })),
  500. // the head's width, at its outer corner
  501. fit("adj2", 0, function (w, h, ss) { return 50000 * box(w, h)[1] / ss; },
  502. at(function (g) { return [g.x1, g.y8]; })),
  503. // the head's length, at its inner corner
  504. fit("adj3", 0, function (w, h, ss, a) { var b = box(w, h); return curvedGeo(b[0], b[1], a).max3; },
  505. at(function (g) { return [g.x1, g.y4]; }))
  506. ]
  507. },
  508. path: function (w, h, adj) {
  509. var b = box(w, h);
  510. return mapPath(curvedPath(b[0], b[1], adj), function (x, y) { return D.f(w, h, x, y); });
  511. }
  512. };
  513. }
  514. /* The bent arrow: up from the bottom left, round a corner of radius
  515. bd, and right into a head. adj1 the shaft, adj2 the head's half
  516. width, adj3 its length, adj4 the corner. */
  517. function bentGeo(w, h, adj) {
  518. var ss = Math.min(w, h);
  519. var a2 = pin(0, av(adj, "adj2", 25000), 50000);
  520. var a1 = pin(0, av(adj, "adj1", 25000), a2 * 2);
  521. var a3 = pin(0, av(adj, "adj3", 25000), 50000);
  522. var th = ss * a1 / 100000, aw2 = ss * a2 / 100000;
  523. var dh2 = aw2 - th / 2, ah = ss * a3 / 100000;
  524. var max4 = 100000 * Math.max(0, Math.min(w - ah, h - dh2)) / ss;
  525. var bd = ss * pin(0, av(adj, "adj4", 43750), max4) / 100000;
  526. var bd2 = Math.max(bd - th, 0);
  527. return { a2: a2, max4: max4, th: th, aw2: aw2, dh2: dh2, bd: bd, bd2: bd2,
  528. x3: th + bd2, x4: w - ah, y3: dh2 + th, y4: dh2 * 2 + th, y5: dh2 + bd };
  529. }
  530. /* The U-turn arrow: up the left, over the top round two corners of
  531. radius bd, and down into a head at adj5 of the height. adj1 the
  532. shaft, adj2 the head's half width, adj3 its length, adj4 the corners. */
  533. function uturnGeo(w, h, adj) {
  534. var ss = Math.min(w, h);
  535. var a2 = pin(0, av(adj, "adj2", 25000), 25000);
  536. var a1 = pin(0, av(adj, "adj1", 25000), a2 * 2);
  537. var max3 = (100000 - a1 * ss / h) * h / ss;
  538. var a3 = pin(0, av(adj, "adj3", 25000), max3);
  539. var min5 = (a3 + a1) * ss / h;
  540. var a5 = pin(min5, av(adj, "adj5", 75000), 100000);
  541. var th = ss * a1 / 100000, aw2 = ss * a2 / 100000;
  542. var dh2 = aw2 - th / 2;
  543. var y5 = h * a5 / 100000, y4 = y5 - ss * a3 / 100000;
  544. var x9 = w - dh2;
  545. var max4 = Math.max(0, Math.min(x9 / 2, y4)) * 100000 / ss;
  546. var bd = ss * pin(0, av(adj, "adj4", 43750), max4) / 100000;
  547. var bd2 = Math.max(bd - th, 0);
  548. var x6 = w - aw2 * 2, x7 = x6 + dh2;
  549. return { a2: a2, max3: max3, min5: min5, max4: max4, th: th, bd: bd, bd2: bd2,
  550. y4: y4, y5: y5, x3: th + bd2, x4: x9 - bd, x6: x6, x7: x7, x8: w - aw2, x9: x9 };
  551. }
  552. /* The left-up, left-right-up and quad arrows share their guides: adj1
  553. the shafts, adj2 the heads' half width, adj3 the heads' length. */
  554. function multiGeo(w, h, adj, def, max2) {
  555. var ss = Math.min(w, h);
  556. var a2 = pin(0, av(adj, "adj2", def), max2);
  557. var a1 = pin(0, av(adj, "adj1", def), a2 * 2);
  558. return { ss: ss, a1: a1, a2: a2 };
  559. }
  560. function leftUpGeo(w, h, adj) {
  561. var m = multiGeo(w, h, adj, 25000, 50000), ss = m.ss;
  562. var max3 = 100000 - m.a2 * 2;
  563. var x1 = ss * pin(0, av(adj, "adj3", 25000), max3) / 100000;
  564. var dx2 = ss * m.a2 / 50000, dx4 = ss * m.a2 / 100000, dx3 = ss * m.a1 / 200000;
  565. var x4 = w - dx4, y4 = h - dx4;
  566. return { a2: m.a2, max3: max3, x1: x1, x2: w - dx2, y2: h - dx2, x3: x4 - dx3, x4: x4,
  567. x5: x4 + dx3, y3: y4 - dx3, y4: y4, y5: y4 + dx3 };
  568. }
  569. function crossGeo(w, h, adj, def) {
  570. var m = multiGeo(w, h, adj, def, 50000), ss = m.ss;
  571. var max3 = (100000 - m.a2 * 2) / 2;
  572. var x1 = ss * pin(0, av(adj, "adj3", def), max3) / 100000;
  573. var dx2 = ss * m.a2 / 100000, dx3 = ss * m.a1 / 200000;
  574. return { a2: m.a2, max3: max3, x1: x1, dx2: dx2, dx3: dx3,
  575. x2: w / 2 - dx2, x3: w / 2 - dx3, x4: w / 2 + dx3, x5: w / 2 + dx2, x6: w - x1 };
  576. }
  577. function bentUpGeo(w, h, adj) {
  578. var ss = Math.min(w, h);
  579. var a1 = pin(0, av(adj, "adj1", 25000), 50000);
  580. var a2 = pin(0, av(adj, "adj2", 25000), 50000);
  581. var y1 = ss * pin(0, av(adj, "adj3", 25000), 50000) / 100000;
  582. var x3 = w - ss * a2 / 100000, dx2 = ss * a1 / 200000;
  583. return { y1: y1, x1: w - ss * a2 / 50000, x2: x3 - dx2, x3: x3, x4: x3 + dx2,
  584. y2: h - ss * a1 / 100000 };
  585. }
  586. /* The arrow callouts: a box with an arrow out of one side, drawn along
  587. u (towards the point, len long) and v (across, thick wide) and then
  588. turned to face its way (ARROW_DIRS). adj1 the shaft, adj2 the head's
  589. width, adj3 its length, adj4 how much of the length the box takes. */
  590. function arrowCalloutGeo(len, thick, ss, adj) {
  591. var a2 = pin(0, av(adj, "adj2", 25000), 50000 * thick / ss);
  592. var a1 = pin(0, av(adj, "adj1", 25000), a2 * 2);
  593. var a3 = pin(0, av(adj, "adj3", 25000), 100000 * len / ss);
  594. var max4 = 100000 - a3 * ss / len;
  595. var a4 = pin(0, av(adj, "adj4", 64977), max4);
  596. var dy1 = ss * a2 / 100000, dy2 = ss * a1 / 200000;
  597. return { a2: a2, max4: max4, v1: thick / 2 - dy1, v2: thick / 2 - dy2, v3: thick / 2 + dy2,
  598. v4: thick / 2 + dy1, x2: len * a4 / 100000, x3: len - ss * a3 / 100000 };
  599. }
  600. function arrowCalloutDef(dir, label) {
  601. var D = ARROW_DIRS[dir];
  602. function lt(w, h) { return D.len === "w" ? [w, h] : [h, w]; }
  603. function geo(w, h, a) { var b = lt(w, h); return arrowCalloutGeo(b[0], b[1], Math.min(w, h), a); }
  604. function at(fn) {
  605. return function (w, h, a) {
  606. var b = lt(w, h), q = fn(geo(w, h, a), b[0], b[1]);
  607. return D.to(w, h, q[0], q[1]);
  608. };
  609. }
  610. return {
  611. cat: "arrow", label: label,
  612. adj: {
  613. defaults: { adj1: 25000, adj2: 25000, adj3: 25000, adj4: 64977 },
  614. handles: [
  615. fit("adj1", 0, function (w, h, ss, a) { return geo(w, h, a).a2 * 2; },
  616. at(function (g) { return [g.x3, g.v2]; })),
  617. fit("adj2", 0, function (w, h, ss) { return 50000 * lt(w, h)[1] / ss; },
  618. at(function (g, L) { return [L, g.v1]; })),
  619. fit("adj3", 0, function (w, h, ss) { return 100000 * lt(w, h)[0] / ss; },
  620. at(function (g) { return [g.x3, 0]; })),
  621. fit("adj4", 0, function (w, h, ss, a) { return geo(w, h, a).max4; },
  622. at(function (g, L, T) { return [g.x2, T]; }))
  623. ]
  624. },
  625. pts: function (w, h, adj) {
  626. var b = lt(w, h), L = b[0], T = b[1], g = geo(w, h, adj);
  627. return [[0, 0], [g.x2, 0], [g.x2, g.v2], [g.x3, g.v2], [g.x3, g.v1], [L, T / 2],
  628. [g.x3, g.v4], [g.x3, g.v3], [g.x2, g.v3], [g.x2, T], [0, T]].map(function (q) {
  629. return D.to(w, h, q[0], q[1]);
  630. });
  631. }
  632. };
  633. }
  634. function lrCalloutGeo(w, h, adj) {
  635. var ss = Math.min(w, h);
  636. var a2 = pin(0, av(adj, "adj2", 25000), 50000 * h / ss);
  637. var a1 = pin(0, av(adj, "adj1", 25000), a2 * 2);
  638. var a3 = pin(0, av(adj, "adj3", 25000), 50000 * w / ss);
  639. var max4 = 100000 - a3 * ss / (w / 2);
  640. var a4 = pin(0, av(adj, "adj4", 48123), max4);
  641. var dy1 = ss * a2 / 100000, dy2 = ss * a1 / 200000;
  642. var x1 = ss * a3 / 100000, dx2 = w * a4 / 200000;
  643. return { a2: a2, max4: max4, x1: x1, x2: w / 2 - dx2, x3: w / 2 + dx2, x4: w - x1,
  644. y1: h / 2 - dy1, y2: h / 2 - dy2, y3: h / 2 + dy2, y4: h / 2 + dy1 };
  645. }
  646. /* A brace in a w x h box, open side on the right, as PowerPoint draws
  647. leftBrace: quarter ellipses of radii w/2 x y1 out to the point at
  648. adj2 of the height. adj1 is how far the curls run, in ss. */
  649. function braceGeo(w, h, adj) {
  650. var ss = Math.min(w, h);
  651. var a2 = pin(0, av(adj, "adj2", 50000), 100000);
  652. var max1 = Math.min(100000 - a2, a2) / 2 * h / ss;
  653. var y1 = ss * pin(0, av(adj, "adj1", 8333), max1) / 100000;
  654. return { max1: max1, y1: y1, y3: h * a2 / 100000 };
  655. }
  656. function bracePath(w, h, adj) {
  657. var g = braceGeo(w, h, adj), y1 = g.y1;
  658. return P().M(w, h).arcTo(w / 2, y1, Math.PI / 2, Math.PI / 2)
  659. .L(w / 2, g.y3 + y1).arcTo(w / 2, y1, 0, -Math.PI / 2)
  660. .arcTo(w / 2, y1, Math.PI / 2, -Math.PI / 2)
  661. .L(w / 2, y1).arcTo(w / 2, y1, Math.PI, Math.PI / 2).toString();
  662. }
  663. // where a teardrop's point is: adj 100000 puts it in the top right corner
  664. function teardropTip(w, h, adj) {
  665. var a = pin(0, av(adj, "adj", 100000), 200000) / 100000;
  666. return [w / 2 * (1 + a), h / 2 * (1 - a)];
  667. }
  668. function halfFrameGeo(w, h, adj) {
  669. var ss = Math.min(w, h);
  670. var x1 = ss * pin(0, av(adj, "adj2", 33333), 100000 * w / ss) / 100000;
  671. var max1 = 100000 * (h - h * x1 / w) / ss;
  672. var y1 = ss * pin(0, av(adj, "adj1", 33333), max1) / 100000;
  673. return { max1: max1, x1: x1, y1: y1, x2: w - y1 * w / h, y2: h - x1 * h / w };
  674. }
  675. // the equation shapes' bars run across 73490 / 100000 of the width
  676. function mathX(w) { return w * 73490 / 200000; }
  677. function mathMulGeo(w, h, adj) {
  678. var ss = Math.min(w, h);
  679. var th = ss * pin(0, av(adj, "adj1", 23520), 51965) / 100000;
  680. var a = Math.atan2(h, w), sa = Math.sin(a), ca = Math.cos(a), ta = Math.tan(a);
  681. var dl = Math.sqrt(w * w + h * h), lM = dl - dl * 51965 / 100000;
  682. var xM = ca * lM / 2, yM = sa * lM / 2;
  683. var xA = xM - sa * th / 2, yA = yM + ca * th / 2;
  684. var xB = xM + sa * th / 2, yB = yM - ca * th / 2;
  685. var yC = (w / 2 - xB) * ta + yB;
  686. var xFE = (h / 2 - yA) / ta;
  687. return { xA: xA, yA: yA, xB: xB, yB: yB, yC: yC, xD: w - xB, xE: w - xA,
  688. xF: w - xA - xFE, xL: xA + xFE };
  689. }
  690. function mathDivGeo(w, h, adj) {
  691. var a1 = pin(1000, av(adj, "adj1", 23520), 36745);
  692. var max3 = Math.min((73490 - a1) / 4, 36745 * w / h);
  693. var a3 = pin(1000, av(adj, "adj3", 11760), max3);
  694. var max2 = 73490 - 4 * a3 - a1;
  695. var a2 = pin(0, av(adj, "adj2", 5880), max2);
  696. var dy1 = h * a1 / 200000, rad = h * a3 / 100000;
  697. var y3 = h / 2 - dy1, y2 = y3 - (h * a2 / 100000 + rad);
  698. return { max2: max2, max3: max3, rad: rad, x1: w / 2 - mathX(w), x3: w / 2 + mathX(w),
  699. y1: y2 - rad, y2: y2, y3: y3, y4: h / 2 + dy1 };
  700. }
  701. // two bars, adj1 thick and adj2 apart (mathEqual; mathNotEqual's are
  702. // the same with its gap in adj3)
  703. function mathEqGeo(w, h, adj, gap) {
  704. var a1 = pin(0, av(adj, "adj1", 23520), gap === "adj3" ? 50000 : 36745);
  705. var max2 = 100000 - a1 * 2;
  706. var a2 = pin(0, av(adj, gap, 11760), max2);
  707. var dy1 = h * a1 / 100000, dy2 = h * a2 / 200000;
  708. return { max2: max2, dy1: dy1, x1: w / 2 - mathX(w), x2: w / 2 + mathX(w),
  709. y1: h / 2 - dy2 - dy1, y2: h / 2 - dy2, y3: h / 2 + dy2, y4: h / 2 + dy2 + dy1 };
  710. }
  711. // the stroke of "not equal": adj2 is its angle from the horizontal
  712. function notEqSlash(w, h, adj, dy1) {
  713. var cr = angleOf(pin(4200000, av(adj, "adj2", 6600000), 6600000)) - Math.PI / 2;
  714. var xadj = h / 2 * Math.tan(cr);
  715. var bhw = dy1 / Math.cos(cr);
  716. return { top: w / 2 + xadj, bhw: bhw };
  717. }
  718. /* The waves: adj1 is how far each edge swings (a share of the
  719. height), adj2 shifts the swing sideways (negative to the left). A
  720. shifted wave is narrower at one end, as PowerPoint draws it. */
  721. function waveGeo(w, h, adj, def1, max1) {
  722. var a1 = pin(0, av(adj, "adj1", def1), max1);
  723. var a2 = pin(-10000, av(adj, "adj2", 0), 10000);
  724. var y1 = h * a1 / 100000, dy2 = y1 * 10 / 3;
  725. var of2 = w * a2 / 50000;
  726. var dx2 = of2 > 0 ? 0 : of2, dx5 = of2 > 0 ? of2 : 0;
  727. return { y1: y1, y2: y1 - dy2, y3: y1 + dy2, y4: h - y1, y5: h - y1 - dy2, y6: h - y1 + dy2,
  728. xl: -dx2, xr: w - dx5, bl: dx5, br: w + dx2 };
  729. }
  730. // one wave from x0 to x1 along y (up first), as its cubic
  731. function waveTo(p, x0, x1, yUp, yDown, y) {
  732. var d = (x1 - x0) / 3;
  733. return p.C(x0 + d, yUp, x0 + 2 * d, yDown, x1, y);
  734. }
  735. function wavePath(w, h, adj, def1, max1, n) {
  736. var g = waveGeo(w, h, adj, def1, max1);
  737. var p = P().M(g.xl, g.y1), i, span = (g.xr - g.xl) / n, bspan = (g.br - g.bl) / n;
  738. for (i = 0; i < n; i++) waveTo(p, g.xl + span * i, g.xl + span * (i + 1), g.y2, g.y3, g.y1);
  739. p.L(g.br, g.y4);
  740. // back along the bottom: the same swing, run the other way
  741. for (i = n; i > 0; i--) waveTo(p, g.bl + bspan * i, g.bl + bspan * (i - 1), g.y6, g.y5, g.y4);
  742. return p.Z().toString();
  743. }
  744. function waveAdj(def1, max1) {
  745. return {
  746. defaults: { adj1: def1, adj2: 0 },
  747. handles: [share("adj1", "h", max1, function (w, h, d) { return [0, d]; },
  748. function (w, h, x, y) { return y; }),
  749. {
  750. pos: function (w, h, a) { return [w / 2 + w * pin(-10000, a.adj2, 10000) / 100000, h]; },
  751. set: function (w, h, x) {
  752. return { adj2: Math.round(pin(-10000, (x - w / 2) / Math.max(1e-6, w) * 100000, 10000)) };
  753. }
  754. }]
  755. };
  756. }
  757. /* ---------------- the catalogue ---------------- */
  758. /* Each entry is either `pts` (a polygon, which also answers points())
  759. or `path`. `detail` is stroked rather than filled; `evenOdd` says the
  760. path has holes in it. */
  761. var DEFS = {
  762. /* ---------- basic shapes ---------- */
  763. rect: {
  764. cat: "shape", label: "Rectangle",
  765. pts: function (w, h) { return [[0, 0], [w, 0], [w, h], [0, h]]; }
  766. },
  767. roundRect: {
  768. cat: "shape", label: "Rounded rectangle",
  769. path: function (w, h) {
  770. var r = Math.min(w, h) * 0.15;
  771. return roundRectPath(w, h, r, r, r, r);
  772. }
  773. },
  774. /* The snipped and rounded corners follow presetShapeDefinitions:
  775. each guide is a corner size in ss, at most half of it, and the
  776. "same side" / "diagonal" pairs leave their second pair of corners
  777. square until it is dragged. */
  778. snip1Rect: {
  779. cat: "shape", label: "Snip single corner",
  780. adj: {
  781. defaults: { adj: 16667 },
  782. handles: [ssLen("adj", 50000, function (w, h, d) { return [w - d, 0]; },
  783. function (w, h, x) { return w - x; })]
  784. },
  785. pts: function (w, h, adj) {
  786. var s = Math.min(w, h) * pin(0, av(adj, "adj", 16667), 50000) / 100000;
  787. return [[0, 0], [w - s, 0], [w, s], [w, h], [0, h]];
  788. }
  789. },
  790. snip2SameRect: {
  791. cat: "shape", label: "Snip same side corners",
  792. adj: {
  793. defaults: { adj1: 16667, adj2: 0 },
  794. handles: [ssLen("adj1", 50000, function (w, h, d) { return [w - d, 0]; },
  795. function (w, h, x) { return w - x; }),
  796. ssLen("adj2", 50000, function (w, h, d) { return [d, h]; },
  797. function (w, h, x) { return x; })]
  798. },
  799. pts: function (w, h, adj) {
  800. var ss = Math.min(w, h);
  801. var s = ss * pin(0, av(adj, "adj1", 16667), 50000) / 100000;
  802. var b = ss * pin(0, av(adj, "adj2", 0), 50000) / 100000;
  803. return [[s, 0], [w - s, 0], [w, s], [w, h - b], [w - b, h], [b, h], [0, h - b], [0, s]];
  804. }
  805. },
  806. snip2DiagRect: {
  807. cat: "shape", label: "Snip diagonal corners",
  808. adj: {
  809. defaults: { adj1: 0, adj2: 16667 },
  810. handles: [ssLen("adj1", 50000, function (w, h, d) { return [d, 0]; },
  811. function (w, h, x) { return x; }),
  812. ssLen("adj2", 50000, function (w, h, d) { return [w - d, 0]; },
  813. function (w, h, x) { return w - x; })]
  814. },
  815. // adj1 snips the top left and bottom right, adj2 the other two
  816. pts: function (w, h, adj) {
  817. var ss = Math.min(w, h);
  818. var a = ss * pin(0, av(adj, "adj1", 0), 50000) / 100000;
  819. var b = ss * pin(0, av(adj, "adj2", 16667), 50000) / 100000;
  820. return [[a, 0], [w - b, 0], [w, b], [w, h - a], [w - a, h], [b, h], [0, h - b], [0, a]];
  821. }
  822. },
  823. round1Rect: {
  824. cat: "shape", label: "Round single corner",
  825. adj: {
  826. defaults: { adj: 16667 },
  827. handles: [ssLen("adj", 50000, function (w, h, d) { return [w - d, 0]; },
  828. function (w, h, x) { return w - x; })]
  829. },
  830. path: function (w, h, adj) {
  831. var r = Math.min(w, h) * pin(0, av(adj, "adj", 16667), 50000) / 100000;
  832. return roundRectPath(w, h, 0, r, 0, 0);
  833. }
  834. },
  835. round2SameRect: {
  836. cat: "shape", label: "Round same side corners",
  837. adj: {
  838. defaults: { adj1: 16667, adj2: 0 },
  839. handles: [ssLen("adj1", 50000, function (w, h, d) { return [w - d, 0]; },
  840. function (w, h, x) { return w - x; }),
  841. ssLen("adj2", 50000, function (w, h, d) { return [d, h]; },
  842. function (w, h, x) { return x; })]
  843. },
  844. path: function (w, h, adj) {
  845. var ss = Math.min(w, h);
  846. var r = ss * pin(0, av(adj, "adj1", 16667), 50000) / 100000;
  847. var b = ss * pin(0, av(adj, "adj2", 0), 50000) / 100000;
  848. return roundRectPath(w, h, r, r, b, b);
  849. }
  850. },
  851. round2DiagRect: {
  852. cat: "shape", label: "Round diagonal corners",
  853. adj: {
  854. defaults: { adj1: 16667, adj2: 0 },
  855. handles: [ssLen("adj1", 50000, function (w, h, d) { return [d, 0]; },
  856. function (w, h, x) { return x; }),
  857. ssLen("adj2", 50000, function (w, h, d) { return [w - d, 0]; },
  858. function (w, h, x) { return w - x; })]
  859. },
  860. // adj1 rounds the top left and bottom right, adj2 the other two
  861. path: function (w, h, adj) {
  862. var ss = Math.min(w, h);
  863. var r = ss * pin(0, av(adj, "adj1", 16667), 50000) / 100000;
  864. var b = ss * pin(0, av(adj, "adj2", 0), 50000) / 100000;
  865. return roundRectPath(w, h, r, b, r, b);
  866. }
  867. },
  868. ellipse: {
  869. cat: "shape", label: "Ellipse",
  870. path: function (w, h) { return P().circle(w / 2, h / 2, w / 2, h / 2).toString(); }
  871. },
  872. triangle: {
  873. cat: "shape", label: "Triangle",
  874. adj: {
  875. defaults: { adj: 50000 },
  876. handles: [share("adj", "w", 100000, function (w, h, d) { return [d, 0]; },
  877. function (w, h, x) { return x; })]
  878. },
  879. pts: function (w, h, adj) { return [[w * pin(0, av(adj, "adj", 50000), 100000) / 100000, 0], [w, h], [0, h]]; }
  880. },
  881. rtTriangle: {
  882. cat: "shape", label: "Right triangle",
  883. pts: function (w, h) { return [[0, 0], [w, h], [0, h]]; }
  884. },
  885. parallelogram: {
  886. cat: "shape", label: "Parallelogram",
  887. adj: {
  888. defaults: { adj: 25000 },
  889. handles: [ssLen("adj", wCap(100000), function (w, h, d) { return [d, 0]; },
  890. function (w, h, x) { return x; })]
  891. },
  892. pts: function (w, h, adj) {
  893. var ss = Math.min(w, h);
  894. var x2 = ss * pin(0, av(adj, "adj", 25000), 100000 * w / ss) / 100000;
  895. return [[x2, 0], [w, 0], [w - x2, h], [0, h]];
  896. }
  897. },
  898. trapezoid: {
  899. cat: "shape", label: "Trapezoid",
  900. adj: {
  901. defaults: { adj: 25000 },
  902. handles: [ssLen("adj", wCap(50000), function (w, h, d) { return [d, 0]; },
  903. function (w, h, x) { return x; })]
  904. },
  905. pts: function (w, h, adj) {
  906. var ss = Math.min(w, h);
  907. var x2 = ss * pin(0, av(adj, "adj", 25000), 50000 * w / ss) / 100000;
  908. return [[x2, 0], [w - x2, 0], [w, h], [0, h]];
  909. }
  910. },
  911. diamond: {
  912. cat: "shape", label: "Diamond",
  913. pts: function (w, h) { return [[w / 2, 0], [w, h / 2], [w / 2, h], [0, h / 2]]; }
  914. },
  915. pentagon: {
  916. cat: "shape", label: "Pentagon",
  917. pts: function (w, h) { return ngon(w, h, 5); }
  918. },
  919. hexagon: {
  920. cat: "shape", label: "Hexagon",
  921. adj: {
  922. defaults: { adj: 25000 },
  923. handles: [ssLen("adj", wCap(50000), function (w, h, d) { return [d, 0]; },
  924. function (w, h, x) { return x; })]
  925. },
  926. pts: function (w, h, adj) {
  927. var ss = Math.min(w, h);
  928. var x1 = ss * pin(0, av(adj, "adj", 25000), 50000 * w / ss) / 100000;
  929. return [[x1, 0], [w - x1, 0], [w, h / 2], [w - x1, h], [x1, h], [0, h / 2]];
  930. }
  931. },
  932. heptagon: {
  933. cat: "shape", label: "Heptagon",
  934. pts: function (w, h) { return ngon(w, h, 7); }
  935. },
  936. octagon: {
  937. cat: "shape", label: "Octagon",
  938. adj: {
  939. defaults: { adj: 29289 },
  940. handles: [ssLen("adj", 50000, function (w, h, d) { return [d, 0]; },
  941. function (w, h, x) { return x; })]
  942. },
  943. pts: function (w, h, adj) {
  944. var s = Math.min(w, h) * pin(0, av(adj, "adj", 29289), 50000) / 100000;
  945. return [[s, 0], [w - s, 0], [w, s], [w, h - s],
  946. [w - s, h], [s, h], [0, h - s], [0, s]];
  947. }
  948. },
  949. decagon: {
  950. cat: "shape", label: "Decagon",
  951. pts: function (w, h) { return ngon(w, h, 10); }
  952. },
  953. dodecagon: {
  954. cat: "shape", label: "Dodecagon",
  955. pts: function (w, h) { return ngon(w, h, 12); }
  956. },
  957. plus: {
  958. cat: "shape", label: "Cross",
  959. adj: {
  960. defaults: { adj: 25000 },
  961. handles: [ssLen("adj", 50000, function (w, h, d) { return [0, d]; },
  962. function (w, h, x, y) { return y; })]
  963. },
  964. pts: function (w, h, adj) {
  965. var x1 = Math.min(w, h) * pin(0, av(adj, "adj", 25000), 50000) / 100000;
  966. var x2 = w - x1, y2 = h - x1;
  967. return [[x1, 0], [x2, 0], [x2, x1], [w, x1], [w, y2], [x2, y2], [x2, h], [x1, h],
  968. [x1, y2], [0, y2], [0, x1], [x1, x1]];
  969. }
  970. },
  971. teardrop: {
  972. cat: "shape", label: "Teardrop",
  973. /* three quarters of an ellipse, the last quarter pulled out
  974. towards the top right corner - as far as adj says (100000
  975. reaches the corner, 200000 twice as far) */
  976. adj: {
  977. defaults: { adj: 100000 },
  978. handles: [fit("adj", 0, 200000, function (w, h, a) { return teardropTip(w, h, a); })]
  979. },
  980. path: function (w, h, adj) {
  981. var t = teardropTip(w, h, adj);
  982. var x2 = (w / 2 + t[0]) / 2, y2 = (h / 2 + t[1]) / 2;
  983. return P().M(0, h / 2).arcTo(w / 2, h / 2, Math.PI, Math.PI / 2)
  984. .Q(x2, 0, t[0], t[1]).Q(w, y2, w, h / 2)
  985. .arcTo(w / 2, h / 2, 0, Math.PI).Z().toString();
  986. }
  987. },
  988. frame: {
  989. cat: "shape", label: "Frame",
  990. evenOdd: true,
  991. adj: {
  992. defaults: { adj1: 12500 },
  993. handles: [ssLen("adj1", 50000, function (w, h, d) { return [d, 0]; },
  994. function (w, h, x) { return x; })]
  995. },
  996. path: function (w, h, adj) {
  997. var t = Math.min(w, h) * pin(0, av(adj, "adj1", 12500), 50000) / 100000;
  998. return P().poly([[0, 0], [w, 0], [w, h], [0, h]]).toString() +
  999. P().poly([[t, t], [t, h - t], [w - t, h - t], [w - t, t]]).toString();
  1000. }
  1001. },
  1002. halfFrame: {
  1003. cat: "shape", label: "Half frame",
  1004. // adj1 the top bar, adj2 the side bar, both in ss; the ends are
  1005. // cut along the frame's diagonal
  1006. adj: {
  1007. defaults: { adj1: 33333, adj2: 33333 },
  1008. handles: [fit("adj1", 0, function (w, h, ss, a) { return halfFrameGeo(w, h, a).max1; },
  1009. function (w, h, a) { var g = halfFrameGeo(w, h, a); return [g.x2, g.y1]; }),
  1010. fit("adj2", 0, function (w, h, ss) { return 100000 * w / ss; },
  1011. function (w, h, a) { var g = halfFrameGeo(w, h, a); return [g.x1, g.y2]; })]
  1012. },
  1013. pts: function (w, h, adj) {
  1014. var g = halfFrameGeo(w, h, adj);
  1015. return [[0, 0], [w, 0], [g.x2, g.y1], [g.x1, g.y1], [g.x1, g.y2], [0, h]];
  1016. }
  1017. },
  1018. corner: {
  1019. cat: "shape", label: "L shape",
  1020. // adj1 is the foot's height, adj2 the upright's width, in ss
  1021. adj: {
  1022. defaults: { adj1: 50000, adj2: 50000 },
  1023. handles: [ssLen("adj1", hCap(100000), function (w, h, d) { return [w, h - d]; },
  1024. function (w, h, x, y) { return h - y; }),
  1025. ssLen("adj2", wCap(100000), function (w, h, d) { return [d, 0]; },
  1026. function (w, h, x) { return x; })]
  1027. },
  1028. pts: function (w, h, adj) {
  1029. var ss = Math.min(w, h);
  1030. var x1 = ss * pin(0, av(adj, "adj2", 50000), 100000 * w / ss) / 100000;
  1031. var y1 = h - ss * pin(0, av(adj, "adj1", 50000), 100000 * h / ss) / 100000;
  1032. return [[0, 0], [x1, 0], [x1, y1], [w, y1], [w, h], [0, h]];
  1033. }
  1034. },
  1035. diagStripe: {
  1036. cat: "shape", label: "Diagonal stripe",
  1037. adj: {
  1038. defaults: { adj: 50000 },
  1039. handles: [share("adj", "h", 100000, function (w, h, d) { return [0, d]; },
  1040. function (w, h, x, y) { return y; })]
  1041. },
  1042. pts: function (w, h, adj) {
  1043. var a = pin(0, av(adj, "adj", 50000), 100000) / 100000;
  1044. return [[0, h * a], [w * a, 0], [w, 0], [0, h]];
  1045. }
  1046. },
  1047. plaque: {
  1048. cat: "shape", label: "Plaque",
  1049. adj: {
  1050. defaults: { adj: 16667 },
  1051. handles: [ssLen("adj", 50000, function (w, h, d) { return [d, 0]; },
  1052. function (w, h, x) { return x; })]
  1053. },
  1054. path: function (w, h, adj) {
  1055. var r = Math.min(w, h) * pin(0, av(adj, "adj", 16667), 50000) / 100000;
  1056. var p = P();
  1057. p.M(r, 0).L(w - r, 0);
  1058. p.arc(w, 0, r, r, Math.PI * 0.5, Math.PI);
  1059. p.L(w, h - r);
  1060. p.arc(w, h, r, r, Math.PI, Math.PI * 1.5);
  1061. p.L(r, h);
  1062. p.arc(0, h, r, r, Math.PI * 1.5, Math.PI * 2);
  1063. p.L(0, r);
  1064. p.arc(0, 0, r, r, 0, Math.PI * 0.5);
  1065. return p.Z().toString();
  1066. }
  1067. },
  1068. can: {
  1069. cat: "shape", label: "Cylinder",
  1070. // the handle sits at the bottom of the lid
  1071. adj: {
  1072. defaults: { adj: 25000 },
  1073. handles: [ssLen("adj", hCap(50000), function (w, h, d) { return [w / 2, d]; },
  1074. function (w, h, x, y) { return y; })]
  1075. },
  1076. path: function (w, h, adj) {
  1077. var ss = Math.min(w, h);
  1078. var ry = ss * pin(0, av(adj, "adj", 25000), 50000 * h / ss) / 200000;
  1079. var p = P();
  1080. p.M(0, ry).arc(w / 2, ry, w / 2, ry, Math.PI, Math.PI * 2)
  1081. .L(w, h - ry)
  1082. .arc(w / 2, h - ry, w / 2, ry, 0, Math.PI)
  1083. .Z();
  1084. return p.toString();
  1085. },
  1086. detail: function (w, h, adj) {
  1087. var ss = Math.min(w, h);
  1088. var ry = ss * pin(0, av(adj, "adj", 25000), 50000 * h / ss) / 200000;
  1089. return P().arc(w / 2, ry, w / 2, ry, 0, Math.PI, true).toString();
  1090. }
  1091. },
  1092. cube: {
  1093. cat: "shape", label: "Cube",
  1094. adj: {
  1095. defaults: { adj: 25000 },
  1096. handles: [ssLen("adj", 100000, function (w, h, d) { return [0, d]; },
  1097. function (w, h, x, y) { return y; })]
  1098. },
  1099. path: function (w, h, adj) {
  1100. var d = Math.min(w, h) * pin(0, av(adj, "adj", 25000), 100000) / 100000;
  1101. return P().poly([[0, d], [d, 0], [w, 0], [w, h - d], [w - d, h], [0, h]]).toString();
  1102. },
  1103. detail: function (w, h, adj) {
  1104. var d = Math.min(w, h) * pin(0, av(adj, "adj", 25000), 100000) / 100000;
  1105. return P().M(0, d).L(w - d, d).L(w, 0).toString() +
  1106. P().M(w - d, d).L(w - d, h).toString();
  1107. }
  1108. },
  1109. bevel: {
  1110. cat: "shape", label: "Bevel",
  1111. evenOdd: true,
  1112. adj: {
  1113. defaults: { adj: 12500 },
  1114. handles: [ssLen("adj", 50000, function (w, h, d) { return [d, 0]; },
  1115. function (w, h, x) { return x; })]
  1116. },
  1117. path: function (w, h, adj) {
  1118. var t = Math.min(w, h) * pin(0, av(adj, "adj", 12500), 50000) / 100000;
  1119. return P().poly([[0, 0], [w, 0], [w, h], [0, h]]).toString() +
  1120. P().poly([[t, t], [w - t, t], [w - t, h - t], [t, h - t]]).toString();
  1121. },
  1122. detail: function (w, h, adj) {
  1123. var t = Math.min(w, h) * pin(0, av(adj, "adj", 12500), 50000) / 100000;
  1124. return P().M(0, 0).L(t, t).toString() +
  1125. P().M(w, 0).L(w - t, t).toString() +
  1126. P().M(w, h).L(w - t, h - t).toString() +
  1127. P().M(0, h).L(t, h - t).toString();
  1128. }
  1129. },
  1130. donut: {
  1131. cat: "shape", label: "Donut",
  1132. evenOdd: true,
  1133. // adj is the ring's thickness, in ss
  1134. adj: {
  1135. defaults: { adj: 25000 },
  1136. handles: [ssLen("adj", 50000, function (w, h, d) { return [d, h / 2]; },
  1137. function (w, h, x) { return x; })]
  1138. },
  1139. path: function (w, h, adj) {
  1140. var t = Math.min(w, h) * pin(0, av(adj, "adj", 25000), 50000) / 100000;
  1141. return P().circle(w / 2, h / 2, w / 2, h / 2).toString() +
  1142. P().circle(w / 2, h / 2, Math.max(0, w / 2 - t), Math.max(0, h / 2 - t)).toString();
  1143. }
  1144. },
  1145. noSmoking: {
  1146. cat: "shape", label: "\"No\" symbol",
  1147. /* Not even-odd: the ring's hole comes from winding the inner
  1148. ellipse the other way, which leaves the bar free to fill right
  1149. across it. Even-odd would punch the bar out again wherever it
  1150. crosses the ring. The ring and the bar are adj thick, in ss,
  1151. and the bar runs from the top left to the bottom right. */
  1152. adj: {
  1153. defaults: { adj: 18750 },
  1154. handles: [ssLen("adj", 50000, function (w, h, d) { return [d, h / 2]; },
  1155. function (w, h, x) { return x; })]
  1156. },
  1157. path: function (w, h, adj) {
  1158. var t = Math.min(w, h) * pin(0, av(adj, "adj", 18750), 50000) / 100000;
  1159. var ang = Math.atan2(h, w);
  1160. var mid = onEllipse(0, 0, Math.max(0, w / 2 - t / 2), Math.max(0, h / 2 - t / 2), ang);
  1161. var len = Math.sqrt(mid[0] * mid[0] + mid[1] * mid[1]);
  1162. var ux = Math.cos(ang), uy = Math.sin(ang), nx = -uy * t / 2, ny = ux * t / 2;
  1163. var cx = w / 2, cy = h / 2;
  1164. var p = P().circle(cx, cy, w / 2, h / 2);
  1165. if (w / 2 - t > 0 && h / 2 - t > 0) {
  1166. p.arc(cx, cy, w / 2 - t, h / 2 - t, 2 * Math.PI, 0, true).Z();
  1167. p.poly([[cx - ux * len - nx, cy - uy * len - ny], [cx + ux * len - nx, cy + uy * len - ny],
  1168. [cx + ux * len + nx, cy + uy * len + ny], [cx - ux * len + nx, cy - uy * len + ny]]);
  1169. }
  1170. return p.toString();
  1171. }
  1172. },
  1173. blockArc: {
  1174. cat: "shape", label: "Block arc",
  1175. // adj1 / adj2 are where the band starts and ends (clockwise),
  1176. // adj3 its thickness in ss
  1177. adj: {
  1178. defaults: { adj1: 10800000, adj2: 0, adj3: 25000 },
  1179. handles: [{
  1180. // the start, on the inside edge: its angle and the thickness
  1181. pos: function (w, h, a) {
  1182. var dr = Math.min(w, h) * pin(0, a.adj3, 50000) / 100000;
  1183. return onEllipse(w / 2, h / 2, Math.max(0, w / 2 - dr), Math.max(0, h / 2 - dr),
  1184. angleOf(pin(0, a.adj1, 21599999)));
  1185. },
  1186. set: function (w, h, x, y) {
  1187. var ang = Math.atan2(y - h / 2, x - w / 2), ss = Math.min(w, h);
  1188. var dr = insetAt(w / 2, h / 2, ang, Math.sqrt((x - w / 2) * (x - w / 2) +
  1189. (y - h / 2) * (y - h / 2)), ss / 2);
  1190. return { adj1: angleGuide(x, y, w / 2, h / 2),
  1191. adj3: Math.round(pin(0, dr / Math.max(1e-6, ss) * 100000, 50000)) };
  1192. }
  1193. }, polar("adj2")]
  1194. },
  1195. path: function (w, h, adj) {
  1196. var st = angleOf(pin(0, av(adj, "adj1", 10800000), 21599999));
  1197. var sw = sweepOf(st, angleOf(pin(0, av(adj, "adj2", 0), 21599999)));
  1198. var dr = Math.min(w, h) * pin(0, av(adj, "adj3", 25000), 50000) / 100000;
  1199. var iw = w / 2 - dr, ih = h / 2 - dr;
  1200. var o = onEllipse(w / 2, h / 2, w / 2, h / 2, st);
  1201. var p = P().M(o[0], o[1]).arcTo(w / 2, h / 2, st, sw);
  1202. if (iw > 0 && ih > 0) {
  1203. var q = onEllipse(w / 2, h / 2, iw, ih, st + sw);
  1204. p.L(q[0], q[1]).arcTo(iw, ih, st + sw, -sw);
  1205. } else p.L(w / 2, h / 2);
  1206. return p.Z().toString();
  1207. }
  1208. },
  1209. foldedCorner: {
  1210. cat: "shape", label: "Folded corner",
  1211. adj: {
  1212. defaults: { adj: 16667 },
  1213. handles: [ssLen("adj", 50000, function (w, h, d) { return [w - d, h]; },
  1214. function (w, h, x) { return w - x; })]
  1215. },
  1216. path: function (w, h, adj) {
  1217. var f = Math.min(w, h) * pin(0, av(adj, "adj", 16667), 50000) / 100000;
  1218. return P().poly([[0, 0], [w, 0], [w, h - f], [w - f, h], [0, h]]).toString();
  1219. },
  1220. detail: function (w, h, adj) {
  1221. var f = Math.min(w, h) * pin(0, av(adj, "adj", 16667), 50000) / 100000;
  1222. return P().M(w, h - f).L(w - f, h - f).L(w - f, h).toString();
  1223. }
  1224. },
  1225. smileyFace: {
  1226. cat: "shape", label: "Smiley face",
  1227. evenOdd: true,
  1228. // adj bends the mouth: up to 4653 a smile, down to -4653 a frown
  1229. adj: {
  1230. defaults: { adj: 4653 },
  1231. handles: [fit("adj", -4653, 4653, function (w, h, a) {
  1232. return [w / 2, h * 16515 / 21600 + h * pin(-4653, a.adj, 4653) / 100000];
  1233. })]
  1234. },
  1235. path: function (w, h, adj) {
  1236. var a = pin(-4653, av(adj, "adj", 4653), 4653);
  1237. var eyeW = w * 1125 / 21600, eyeH = h * 1125 / 21600, eyeY = h * 7570 / 21600;
  1238. var x1 = w * 4969 / 21699, x4 = w * 16640 / 21600;
  1239. var dy2 = h * a / 100000, y2 = h * 16515 / 21600 - dy2;
  1240. var y5 = h * 16515 / 21600 + dy2 + h * a / 50000;
  1241. // the mouth is a thin crescent, so it reads without a stroke
  1242. var lip = h * 0.12 * (a >= 0 ? 1 : -1);
  1243. return P().circle(w / 2, h / 2, w / 2, h / 2).toString() +
  1244. P().circle(w * 6215 / 21600 + eyeW, eyeY, eyeW, eyeH).toString() +
  1245. P().circle(w * 13135 / 21600 + eyeW, eyeY, eyeW, eyeH).toString() +
  1246. P().M(x1, y2).Q(w / 2, y5, x4, y2).Q(w / 2, y5 - lip, x1, y2).Z().toString();
  1247. }
  1248. },
  1249. heart: {
  1250. cat: "shape", label: "Heart",
  1251. path: function (w, h) {
  1252. return P().M(w / 2, h)
  1253. .C(w * -0.06, h * 0.6, w * 0.08, h * -0.1, w / 2, h * 0.24)
  1254. .C(w * 0.92, h * -0.1, w * 1.06, h * 0.6, w / 2, h)
  1255. .Z().toString();
  1256. }
  1257. },
  1258. lightningBolt: {
  1259. cat: "shape", label: "Lightning bolt",
  1260. pts: function (w, h) {
  1261. return [[w * 0.44, 0], [w * 0.84, h * 0.40], [w * 0.60, h * 0.44],
  1262. [w * 0.93, h * 0.75], [w * 0.68, h * 0.74], [w * 0.83, h],
  1263. [w * 0.30, h * 0.56], [w * 0.53, h * 0.52], [w * 0.19, h * 0.24],
  1264. [w * 0.42, h * 0.26], [w * 0.16, h * 0.03]];
  1265. }
  1266. },
  1267. sun: {
  1268. cat: "shape", label: "Sun",
  1269. /* eight rays off a disc: the ring of "mid" points is the disc.
  1270. adj is the gap from the frame to the disc, a share of the
  1271. width (and height) - 12500 a small sun, 46875 almost no rays */
  1272. adj: {
  1273. defaults: { adj: 25000 },
  1274. handles: [fit("adj", 12500, 46875, function (w, h, a) {
  1275. return [w * pin(12500, a.adj, 46875) / 100000, h / 2];
  1276. })]
  1277. },
  1278. pts: function (w, h, adj) {
  1279. var cx = w / 2, cy = h / 2;
  1280. var mid = 1 - 2 * pin(12500, av(adj, "adj", 25000), 46875) / 100000;
  1281. var pts = [];
  1282. for (var i = 0; i < 8; i++) {
  1283. var a = -Math.PI / 2 + i * Math.PI / 4;
  1284. var a1 = a - Math.PI / 10, a2 = a + Math.PI / 10;
  1285. pts.push([cx + cx * mid * Math.cos(a1), cy + cy * mid * Math.sin(a1)]);
  1286. pts.push([cx + cx * Math.cos(a), cy + cy * Math.sin(a)]);
  1287. pts.push([cx + cx * mid * Math.cos(a2), cy + cy * mid * Math.sin(a2)]);
  1288. }
  1289. return pts;
  1290. }
  1291. },
  1292. moon: {
  1293. cat: "shape", label: "Crescent moon",
  1294. // adj is how thick the crescent is at its middle, in ss
  1295. adj: {
  1296. defaults: { adj: 50000 },
  1297. handles: [ssLen("adj", 87500, function (w, h, d) { return [d * w / Math.min(w, h), h / 2]; },
  1298. function (w, h, x) { return x * Math.min(w, h) / w; })]
  1299. },
  1300. path: function (w, h, adj) {
  1301. var g0w = w * pin(0, av(adj, "adj", 50000), 87500) / 100000;
  1302. return P().M(w, h).arcTo(w, h / 2, Math.PI / 2, Math.PI)
  1303. .arcTo(w - g0w, h / 2, Math.PI * 1.5, -Math.PI).Z().toString();
  1304. }
  1305. },
  1306. cloud: {
  1307. cat: "shape", label: "Cloud",
  1308. path: function (w, h) {
  1309. var p = P();
  1310. p.M(w * 0.22, h * 0.92)
  1311. .C(w * 0.02, h * 0.92, w * -0.05, h * 0.58, w * 0.13, h * 0.5)
  1312. .C(w * 0.06, h * 0.28, w * 0.26, h * 0.1, w * 0.42, h * 0.2)
  1313. .C(w * 0.5, h * -0.03, w * 0.8, h * -0.03, w * 0.86, h * 0.22)
  1314. .C(w * 1.04, h * 0.26, w * 1.04, h * 0.6, w * 0.87, h * 0.66)
  1315. .C(w * 0.93, h * 0.88, w * 0.7, h * 1.02, w * 0.57, h * 0.9)
  1316. .C(w * 0.48, h * 1.0, w * 0.3, h * 1.0, w * 0.22, h * 0.92)
  1317. .Z();
  1318. return p.toString();
  1319. }
  1320. },
  1321. arc: {
  1322. cat: "shape", label: "Arc",
  1323. open: true,
  1324. // adj1 / adj2: where it starts and ends, clockwise
  1325. adj: { defaults: { adj1: 16200000, adj2: 0 }, handles: [polar("adj1"), polar("adj2")] },
  1326. path: function (w, h, adj) { return arcOutline(w, h, adj, 16200000, 0).toString(); }
  1327. },
  1328. chord: {
  1329. cat: "shape", label: "Chord",
  1330. adj: { defaults: { adj1: 2700000, adj2: 16200000 }, handles: [polar("adj1"), polar("adj2")] },
  1331. path: function (w, h, adj) { return arcOutline(w, h, adj, 2700000, 16200000).Z().toString(); }
  1332. },
  1333. pie: {
  1334. cat: "shape", label: "Pie",
  1335. adj: { defaults: { adj1: 0, adj2: 16200000 }, handles: [polar("adj1"), polar("adj2")] },
  1336. path: function (w, h, adj) {
  1337. return arcOutline(w, h, adj, 0, 16200000).L(w / 2, h / 2).Z().toString();
  1338. }
  1339. },
  1340. /* ---------- flow chart ---------- */
  1341. flowChartProcess: {
  1342. cat: "shape", label: "Flowchart: process",
  1343. pts: function (w, h) { return [[0, 0], [w, 0], [w, h], [0, h]]; }
  1344. },
  1345. flowChartAlternateProcess: {
  1346. cat: "shape", label: "Flowchart: alternate process",
  1347. path: function (w, h) {
  1348. var r = Math.min(w, h) * 0.17;
  1349. return roundRectPath(w, h, r, r, r, r);
  1350. }
  1351. },
  1352. flowChartDecision: {
  1353. cat: "shape", label: "Flowchart: decision",
  1354. pts: function (w, h) { return [[w / 2, 0], [w, h / 2], [w / 2, h], [0, h / 2]]; }
  1355. },
  1356. flowChartInputOutput: {
  1357. cat: "shape", label: "Flowchart: data",
  1358. pts: function (w, h) { return [[w * 0.2, 0], [w, 0], [w * 0.8, h], [0, h]]; }
  1359. },
  1360. flowChartPredefinedProcess: {
  1361. cat: "shape", label: "Flowchart: predefined process",
  1362. pts: function (w, h) { return [[0, 0], [w, 0], [w, h], [0, h]]; },
  1363. detail: function (w, h) {
  1364. return P().M(w * 0.12, 0).L(w * 0.12, h).toString() +
  1365. P().M(w * 0.88, 0).L(w * 0.88, h).toString();
  1366. }
  1367. },
  1368. flowChartInternalStorage: {
  1369. cat: "shape", label: "Flowchart: internal storage",
  1370. pts: function (w, h) { return [[0, 0], [w, 0], [w, h], [0, h]]; },
  1371. detail: function (w, h) {
  1372. return P().M(w * 0.14, 0).L(w * 0.14, h).toString() +
  1373. P().M(0, h * 0.14).L(w, h * 0.14).toString();
  1374. }
  1375. },
  1376. flowChartDocument: {
  1377. cat: "shape", label: "Flowchart: document",
  1378. path: function (w, h) {
  1379. return P().M(0, 0).L(w, 0).L(w, h * 0.83)
  1380. .C(w * 0.72, h * 1.07, w * 0.28, h * 0.6, 0, h * 0.86)
  1381. .Z().toString();
  1382. }
  1383. },
  1384. flowChartMultidocument: {
  1385. cat: "shape", label: "Flowchart: multidocument",
  1386. path: function (w, h) {
  1387. return P().M(0, h * 0.13).L(w * 0.9, h * 0.13).L(w * 0.9, h * 0.9)
  1388. .C(w * 0.65, h * 1.1, w * 0.25, h * 0.68, 0, h * 0.92).Z().toString() +
  1389. P().M(w * 0.05, h * 0.07).L(w * 0.95, h * 0.07).L(w * 0.95, h * 0.8)
  1390. .L(w * 0.9, h * 0.8).L(w * 0.9, h * 0.13).L(w * 0.05, h * 0.13).Z().toString() +
  1391. P().M(w * 0.1, 0).L(w, 0).L(w, h * 0.72).L(w * 0.95, h * 0.72)
  1392. .L(w * 0.95, h * 0.07).L(w * 0.1, h * 0.07).Z().toString();
  1393. }
  1394. },
  1395. flowChartTerminator: {
  1396. cat: "shape", label: "Flowchart: terminator",
  1397. path: function (w, h) {
  1398. var r = Math.min(w / 2, h / 2);
  1399. return roundRectPath(w, h, r, r, r, r);
  1400. }
  1401. },
  1402. flowChartPreparation: {
  1403. cat: "shape", label: "Flowchart: preparation",
  1404. pts: function (w, h) {
  1405. return [[w * 0.2, 0], [w * 0.8, 0], [w, h / 2],
  1406. [w * 0.8, h], [w * 0.2, h], [0, h / 2]];
  1407. }
  1408. },
  1409. flowChartManualInput: {
  1410. cat: "shape", label: "Flowchart: manual input",
  1411. pts: function (w, h) { return [[0, h * 0.25], [w, 0], [w, h], [0, h]]; }
  1412. },
  1413. flowChartManualOperation: {
  1414. cat: "shape", label: "Flowchart: manual operation",
  1415. pts: function (w, h) { return [[0, 0], [w, 0], [w * 0.8, h], [w * 0.2, h]]; }
  1416. },
  1417. flowChartConnector: {
  1418. cat: "shape", label: "Flowchart: connector",
  1419. path: function (w, h) { return P().circle(w / 2, h / 2, w / 2, h / 2).toString(); }
  1420. },
  1421. flowChartOffpageConnector: {
  1422. cat: "shape", label: "Flowchart: off-page connector",
  1423. pts: function (w, h) {
  1424. return [[0, 0], [w, 0], [w, h * 0.8], [w / 2, h], [0, h * 0.8]];
  1425. }
  1426. },
  1427. flowChartPunchedCard: {
  1428. cat: "shape", label: "Flowchart: card",
  1429. pts: function (w, h) {
  1430. var s = Math.min(w, h) * 0.22;
  1431. return [[s, 0], [w, 0], [w, h], [0, h], [0, s]];
  1432. }
  1433. },
  1434. flowChartPunchedTape: {
  1435. cat: "shape", label: "Flowchart: punched tape",
  1436. path: function (w, h) {
  1437. return P().M(0, h * 0.12)
  1438. .C(w * 0.25, h * -0.1, w * 0.75, h * 0.34, w, h * 0.12)
  1439. .L(w, h * 0.88)
  1440. .C(w * 0.75, h * 1.1, w * 0.25, h * 0.66, 0, h * 0.88)
  1441. .Z().toString();
  1442. }
  1443. },
  1444. flowChartSummingJunction: {
  1445. cat: "shape", label: "Flowchart: summing junction",
  1446. path: function (w, h) { return P().circle(w / 2, h / 2, w / 2, h / 2).toString(); },
  1447. detail: function (w, h) {
  1448. var k = 0.1464; // where the diagonal meets the circle
  1449. return P().M(w * k, h * k).L(w * (1 - k), h * (1 - k)).toString() +
  1450. P().M(w * (1 - k), h * k).L(w * k, h * (1 - k)).toString();
  1451. }
  1452. },
  1453. flowChartOr: {
  1454. cat: "shape", label: "Flowchart: or",
  1455. path: function (w, h) { return P().circle(w / 2, h / 2, w / 2, h / 2).toString(); },
  1456. detail: function (w, h) {
  1457. return P().M(w / 2, 0).L(w / 2, h).toString() +
  1458. P().M(0, h / 2).L(w, h / 2).toString();
  1459. }
  1460. },
  1461. flowChartCollate: {
  1462. cat: "shape", label: "Flowchart: collate",
  1463. pts: function (w, h) { return [[0, 0], [w, 0], [0, h], [w, h]]; }
  1464. },
  1465. flowChartSort: {
  1466. cat: "shape", label: "Flowchart: sort",
  1467. pts: function (w, h) { return [[w / 2, 0], [w, h / 2], [w / 2, h], [0, h / 2]]; },
  1468. detail: function (w, h) { return P().M(0, h / 2).L(w, h / 2).toString(); }
  1469. },
  1470. flowChartExtract: {
  1471. cat: "shape", label: "Flowchart: extract",
  1472. pts: function (w, h) { return [[w / 2, 0], [w, h], [0, h]]; }
  1473. },
  1474. flowChartMerge: {
  1475. cat: "shape", label: "Flowchart: merge",
  1476. pts: function (w, h) { return [[0, 0], [w, 0], [w / 2, h]]; }
  1477. },
  1478. flowChartDelay: {
  1479. cat: "shape", label: "Flowchart: delay",
  1480. path: function (w, h) {
  1481. var r = Math.min(w / 2, h / 2);
  1482. return roundRectPath(w, h, 0, r, r, 0);
  1483. }
  1484. },
  1485. flowChartMagneticDisk: {
  1486. cat: "shape", label: "Flowchart: stored data",
  1487. path: function (w, h) {
  1488. var ry = Math.min(h * 0.18, w * 0.5);
  1489. return P().M(0, ry).arc(w / 2, ry, w / 2, ry, Math.PI, Math.PI * 2)
  1490. .L(w, h - ry).arc(w / 2, h - ry, w / 2, ry, 0, Math.PI).Z().toString();
  1491. },
  1492. detail: function (w, h) {
  1493. var ry = Math.min(h * 0.18, w * 0.5);
  1494. return P().arc(w / 2, ry, w / 2, ry, 0, Math.PI, true).toString();
  1495. }
  1496. },
  1497. flowChartDisplay: {
  1498. cat: "shape", label: "Flowchart: display",
  1499. path: function (w, h) {
  1500. return P().M(0, h / 2).L(w * 0.17, 0).L(w * 0.83, 0)
  1501. .arc(w * 0.83, h / 2, w * 0.17, h / 2, -Math.PI / 2, Math.PI / 2)
  1502. .L(w * 0.17, h).Z().toString();
  1503. }
  1504. },
  1505. /* ---------- arrows ---------- */
  1506. rightArrow: {
  1507. cat: "arrow", label: "Arrow: right", adj: arrowAdj("right"),
  1508. pts: function (w, h, adj) { return arrowGeo(w, h, Math.min(w, h), adj); }
  1509. },
  1510. leftArrow: {
  1511. cat: "arrow", label: "Arrow: left", adj: arrowAdj("left"),
  1512. pts: function (w, h, adj) {
  1513. return arrowGeo(w, h, Math.min(w, h), adj).map(function (p) { return [w - p[0], p[1]]; });
  1514. }
  1515. },
  1516. upArrow: {
  1517. cat: "arrow", label: "Arrow: up", adj: arrowAdj("up"),
  1518. pts: function (w, h, adj) {
  1519. return arrowGeo(h, w, Math.min(w, h), adj).map(function (p) { return [p[1], h - p[0]]; });
  1520. }
  1521. },
  1522. downArrow: {
  1523. cat: "arrow", label: "Arrow: down", adj: arrowAdj("down"),
  1524. pts: function (w, h, adj) {
  1525. return arrowGeo(h, w, Math.min(w, h), adj).map(function (p) { return [p[1], p[0]]; });
  1526. }
  1527. },
  1528. leftRightArrow: {
  1529. cat: "arrow", label: "Arrow: left-right",
  1530. // the shaft's thickness at the right head, the heads' length at the left
  1531. adj: {
  1532. defaults: { adj1: 50000, adj2: 50000 },
  1533. handles: [{
  1534. pos: function (w, h, a) {
  1535. var ss = Math.min(w, h);
  1536. var hl = ss * pin(0, a.adj2, 50000 * w / ss) / 100000;
  1537. return [w - hl, h / 2 - h * pin(0, a.adj1, 100000) / 200000];
  1538. },
  1539. set: function (w, h, x, y) {
  1540. return { adj1: Math.round(pin(0, (h / 2 - y) * 200000 / Math.max(1e-6, h), 100000)) };
  1541. }
  1542. }, ssLen("adj2", wCap(50000), function (w, h, d) { return [d, 0]; },
  1543. function (w, h, x) { return x; })]
  1544. },
  1545. pts: function (w, h, adj) {
  1546. var ss = Math.min(w, h);
  1547. var t = h / 2 - h * pin(0, av(adj, "adj1", 50000), 100000) / 200000;
  1548. var a = ss * pin(0, av(adj, "adj2", 50000), 50000 * w / ss) / 100000;
  1549. return [[0, h / 2], [a, 0], [a, t], [w - a, t], [w - a, 0], [w, h / 2],
  1550. [w - a, h], [w - a, h - t], [a, h - t], [a, h]];
  1551. }
  1552. },
  1553. upDownArrow: {
  1554. cat: "arrow", label: "Arrow: up-down",
  1555. // adj1 is the shaft's width (a share of the frame's), adj2 the
  1556. // heads' length in ss
  1557. adj: {
  1558. defaults: { adj1: 50000, adj2: 50000 },
  1559. handles: [{
  1560. pos: function (w, h, a) {
  1561. var ss = Math.min(w, h);
  1562. var hl = ss * pin(0, a.adj2, 50000 * h / ss) / 100000;
  1563. return [w / 2 - w * pin(0, a.adj1, 100000) / 200000, h - hl];
  1564. },
  1565. set: function (w, h, x) {
  1566. return { adj1: Math.round(pin(0, (w / 2 - x) * 200000 / Math.max(1e-6, w), 100000)) };
  1567. }
  1568. }, ssLen("adj2", hCap(50000), function (w, h, d) { return [0, d]; },
  1569. function (w, h, x, y) { return y; })]
  1570. },
  1571. pts: function (w, h, adj) {
  1572. var ss = Math.min(w, h);
  1573. var t = w / 2 - w * pin(0, av(adj, "adj1", 50000), 100000) / 200000;
  1574. var a = ss * pin(0, av(adj, "adj2", 50000), 50000 * h / ss) / 100000;
  1575. return [[w / 2, 0], [w, a], [w - t, a], [w - t, h - a], [w, h - a], [w / 2, h],
  1576. [0, h - a], [t, h - a], [t, a], [0, a]];
  1577. }
  1578. },
  1579. quadArrow: {
  1580. cat: "arrow", label: "Arrow: quad",
  1581. adj: {
  1582. defaults: { adj1: 22500, adj2: 22500, adj3: 22500 },
  1583. handles: [fit("adj1", 0, function (w, h, ss, a) { return crossGeo(w, h, a, 22500).a2 * 2; },
  1584. function (w, h, a) { var g = crossGeo(w, h, a, 22500); return [g.x3, g.x1]; }),
  1585. fit("adj2", 0, 50000, function (w, h, a) { var g = crossGeo(w, h, a, 22500); return [g.x2, 0]; }),
  1586. fit("adj3", 0, function (w, h, ss, a) { return crossGeo(w, h, a, 22500).max3; },
  1587. function (w, h, a) { var g = crossGeo(w, h, a, 22500); return [g.x1, h / 2 - g.dx2]; })]
  1588. },
  1589. pts: function (w, h, adj) {
  1590. var g = crossGeo(w, h, adj, 22500), hc = w / 2, vc = h / 2;
  1591. var y2 = vc - g.dx2, y5 = vc + g.dx2, y3 = vc - g.dx3, y4 = vc + g.dx3, y6 = h - g.x1;
  1592. return [[0, vc], [g.x1, y2], [g.x1, y3], [g.x3, y3], [g.x3, g.x1], [g.x2, g.x1], [hc, 0],
  1593. [g.x5, g.x1], [g.x4, g.x1], [g.x4, y3], [g.x6, y3], [g.x6, y2], [w, vc], [g.x6, y5],
  1594. [g.x6, y4], [g.x4, y4], [g.x4, y6], [g.x5, y6], [hc, h], [g.x2, y6], [g.x3, y6],
  1595. [g.x3, y4], [g.x1, y4], [g.x1, y5]];
  1596. }
  1597. },
  1598. leftRightUpArrow: {
  1599. cat: "arrow", label: "Arrow: left-right-up",
  1600. adj: {
  1601. defaults: { adj1: 25000, adj2: 25000, adj3: 25000 },
  1602. handles: [fit("adj1", 0, function (w, h, ss, a) { return crossGeo(w, h, a, 25000).a2 * 2; },
  1603. function (w, h, a) { var g = crossGeo(w, h, a, 25000); return [g.x3, h - g.dx2 - g.dx3]; }),
  1604. fit("adj2", 0, 50000, function (w, h, a) { var g = crossGeo(w, h, a, 25000); return [g.x2, 0]; }),
  1605. fit("adj3", 0, function (w, h, ss, a) { return crossGeo(w, h, a, 25000).max3; },
  1606. function (w, h, a) { var g = crossGeo(w, h, a, 25000); return [g.x1, h - g.dx2 * 2]; })]
  1607. },
  1608. pts: function (w, h, adj) {
  1609. var g = crossGeo(w, h, adj, 25000), hc = w / 2;
  1610. var y2 = h - g.dx2 * 2, y4 = h - g.dx2, y3 = y4 - g.dx3, y5 = y4 + g.dx3;
  1611. return [[0, y4], [g.x1, y2], [g.x1, y3], [g.x3, y3], [g.x3, g.x1], [g.x2, g.x1], [hc, 0],
  1612. [g.x5, g.x1], [g.x4, g.x1], [g.x4, y3], [g.x6, y3], [g.x6, y2], [w, y4], [g.x6, h],
  1613. [g.x6, y5], [g.x1, y5], [g.x1, h]];
  1614. }
  1615. },
  1616. bentArrow: {
  1617. cat: "arrow", label: "Arrow: bent",
  1618. adj: {
  1619. defaults: { adj1: 25000, adj2: 25000, adj3: 25000, adj4: 43750 },
  1620. handles: [fit("adj1", 0, function (w, h, ss, a) { return bentGeo(w, h, a).a2 * 2; },
  1621. function (w, h, a) { return [bentGeo(w, h, a).th, h]; }),
  1622. fit("adj2", 0, 50000, function (w, h, a) { return [w, bentGeo(w, h, a).y4]; }),
  1623. fit("adj3", 0, 50000, function (w, h, a) { return [bentGeo(w, h, a).x4, 0]; }),
  1624. fit("adj4", 0, function (w, h, ss, a) { return bentGeo(w, h, a).max4; },
  1625. function (w, h, a) { return [bentGeo(w, h, a).bd, 0]; })]
  1626. },
  1627. path: function (w, h, adj) {
  1628. var g = bentGeo(w, h, adj);
  1629. return P().M(0, h).L(0, g.y5).arcTo(g.bd, g.bd, Math.PI, Math.PI / 2)
  1630. .L(g.x4, g.dh2).L(g.x4, 0).L(w, g.aw2).L(g.x4, g.y4).L(g.x4, g.y3).L(g.x3, g.y3)
  1631. .arcTo(g.bd2, g.bd2, Math.PI * 1.5, -Math.PI / 2).L(g.th, h).Z().toString();
  1632. }
  1633. },
  1634. uturnArrow: {
  1635. cat: "arrow", label: "Arrow: U-turn",
  1636. adj: {
  1637. defaults: { adj1: 25000, adj2: 25000, adj3: 25000, adj4: 43750, adj5: 75000 },
  1638. handles: [fit("adj1", 0, function (w, h, ss, a) { return uturnGeo(w, h, a).a2 * 2; },
  1639. function (w, h, a) { return [uturnGeo(w, h, a).th, h]; }),
  1640. fit("adj2", 0, 25000, function (w, h, a) { var g = uturnGeo(w, h, a); return [g.x6, g.y4]; }),
  1641. fit("adj3", 0, function (w, h, ss, a) { return uturnGeo(w, h, a).max3; },
  1642. function (w, h, a) { return [w, uturnGeo(w, h, a).y4]; }),
  1643. fit("adj4", 0, function (w, h, ss, a) { return uturnGeo(w, h, a).max4; },
  1644. function (w, h, a) { return [uturnGeo(w, h, a).bd, 0]; }),
  1645. fit("adj5", function (w, h, ss, a) { return uturnGeo(w, h, a).min5; }, 100000,
  1646. function (w, h, a) { var g = uturnGeo(w, h, a); return [g.x8, g.y5]; })]
  1647. },
  1648. path: function (w, h, adj) {
  1649. var g = uturnGeo(w, h, adj), H = Math.PI / 2;
  1650. return P().M(0, h).L(0, g.bd).arcTo(g.bd, g.bd, Math.PI, H).L(g.x4, 0)
  1651. .arcTo(g.bd, g.bd, Math.PI * 1.5, H).L(g.x9, g.y4).L(w, g.y4).L(g.x8, g.y5)
  1652. .L(g.x6, g.y4).L(g.x7, g.y4).L(g.x7, g.x3).arcTo(g.bd2, g.bd2, 0, -H)
  1653. .L(g.x3, g.th).arcTo(g.bd2, g.bd2, Math.PI * 1.5, -H).L(g.th, h).Z().toString();
  1654. }
  1655. },
  1656. leftUpArrow: {
  1657. cat: "arrow", label: "Arrow: left-up",
  1658. // an L with a head on each end: one points up, one points left
  1659. adj: {
  1660. defaults: { adj1: 25000, adj2: 25000, adj3: 25000 },
  1661. handles: [fit("adj1", 0, function (w, h, ss, a) { return leftUpGeo(w, h, a).a2 * 2; },
  1662. function (w, h, a) { var g = leftUpGeo(w, h, a); return [g.x3, g.x1]; }),
  1663. fit("adj2", 0, 50000, function (w, h, a) { return [leftUpGeo(w, h, a).x2, 0]; }),
  1664. fit("adj3", 0, function (w, h, ss, a) { return leftUpGeo(w, h, a).max3; },
  1665. function (w, h, a) { var g = leftUpGeo(w, h, a); return [g.x1, g.y2]; })]
  1666. },
  1667. pts: function (w, h, adj) {
  1668. var g = leftUpGeo(w, h, adj);
  1669. return [[0, g.y4], [g.x1, g.y2], [g.x1, g.y3], [g.x3, g.y3], [g.x3, g.x1], [g.x2, g.x1],
  1670. [g.x4, 0], [w, g.x1], [g.x5, g.x1], [g.x5, g.y5], [g.x1, g.y5], [g.x1, h]];
  1671. }
  1672. },
  1673. bentUpArrow: {
  1674. cat: "arrow", label: "Arrow: bent up",
  1675. adj: {
  1676. defaults: { adj1: 25000, adj2: 25000, adj3: 25000 },
  1677. handles: [fit("adj1", 0, 50000, function (w, h, a) { return [0, bentUpGeo(w, h, a).y2]; }),
  1678. fit("adj2", 0, 50000, function (w, h, a) { return [bentUpGeo(w, h, a).x1, 0]; }),
  1679. fit("adj3", 0, 50000, function (w, h, a) { return [w, bentUpGeo(w, h, a).y1]; })]
  1680. },
  1681. pts: function (w, h, adj) {
  1682. var g = bentUpGeo(w, h, adj);
  1683. return [[0, g.y2], [g.x2, g.y2], [g.x2, g.y1], [g.x1, g.y1], [g.x3, 0], [w, g.y1],
  1684. [g.x4, g.y1], [g.x4, h], [0, h]];
  1685. }
  1686. },
  1687. stripedRightArrow: {
  1688. cat: "arrow", label: "Arrow: striped right",
  1689. adj: {
  1690. defaults: { adj1: 50000, adj2: 50000 },
  1691. handles: [share("adj1", "h", 100000, function (w, h, d) { return [0, h / 2 - d / 2]; },
  1692. function (w, h, x, y) { return (h / 2 - y) * 2; }),
  1693. ssLen("adj2", wCap(84375), function (w, h, d) { return [w - d, 0]; },
  1694. function (w, h, x) { return w - x; })]
  1695. },
  1696. // two stripes and the arrow, side by side
  1697. path: function (w, h, adj) {
  1698. var ss = Math.min(w, h);
  1699. var dy1 = h * pin(0, av(adj, "adj1", 50000), 100000) / 200000;
  1700. var x5 = w - ss * pin(0, av(adj, "adj2", 50000), 84375 * w / ss) / 100000;
  1701. var y1 = h / 2 - dy1, y2 = h / 2 + dy1;
  1702. function bar(x0, x1) { return P().poly([[x0, y1], [x1, y1], [x1, y2], [x0, y2]]).toString(); }
  1703. return bar(0, ss / 32) + bar(ss / 16, ss / 8) +
  1704. P().poly([[ss * 5 / 32, y1], [x5, y1], [x5, 0], [w, h / 2], [x5, h], [x5, y2],
  1705. [ss * 5 / 32, y2]]).toString();
  1706. }
  1707. },
  1708. notchedRightArrow: {
  1709. cat: "arrow", label: "Arrow: notched right", adj: arrowAdj("right"),
  1710. pts: function (w, h, adj) {
  1711. var ss = Math.min(w, h);
  1712. var dx2 = ss * pin(0, av(adj, "adj2", 50000), 100000 * w / ss) / 100000;
  1713. var dy1 = h * pin(0, av(adj, "adj1", 50000), 100000) / 200000;
  1714. var hx = w - dx2, t = h / 2 - dy1;
  1715. return [[0, t], [hx, t], [hx, 0], [w, h / 2], [hx, h], [hx, h - t],
  1716. [0, h - t], [dy1 * dx2 / (h / 2), h / 2]];
  1717. }
  1718. },
  1719. chevron: {
  1720. cat: "arrow", label: "Chevron",
  1721. adj: {
  1722. defaults: { adj: 50000 },
  1723. handles: [ssLen("adj", wCap(100000), function (w, h, d) { return [w - d, 0]; },
  1724. function (w, h, x) { return w - x; })]
  1725. },
  1726. pts: function (w, h, adj) {
  1727. var ss = Math.min(w, h);
  1728. var x1 = ss * pin(0, av(adj, "adj", 50000), 100000 * w / ss) / 100000;
  1729. return [[0, 0], [w - x1, 0], [w, h / 2], [w - x1, h], [0, h], [x1, h / 2]];
  1730. }
  1731. },
  1732. homePlate: {
  1733. cat: "arrow", label: "Pentagon arrow",
  1734. adj: {
  1735. defaults: { adj: 50000 },
  1736. handles: [ssLen("adj", wCap(100000), function (w, h, d) { return [w - d, 0]; },
  1737. function (w, h, x) { return w - x; })]
  1738. },
  1739. pts: function (w, h, adj) {
  1740. var ss = Math.min(w, h);
  1741. var x1 = w - ss * pin(0, av(adj, "adj", 50000), 100000 * w / ss) / 100000;
  1742. return [[0, 0], [x1, 0], [w, h / 2], [x1, h], [0, h]];
  1743. }
  1744. },
  1745. curvedRightArrow: curvedDef("right", "Arrow: curved right"),
  1746. curvedLeftArrow: curvedDef("left", "Arrow: curved left"),
  1747. curvedUpArrow: curvedDef("up", "Arrow: curved up"),
  1748. curvedDownArrow: curvedDef("down", "Arrow: curved down"),
  1749. circularArrow: {
  1750. cat: "arrow", label: "Arrow: circular",
  1751. adj: {
  1752. defaults: { adj1: 12500, adj2: 1142319, adj3: 20457681, adj4: 10800000, adj5: 12500 },
  1753. handles: [{
  1754. // the start, on the inside edge: its angle and the thickness
  1755. pos: function (w, h, a) {
  1756. var g = circGeo(w, h, a);
  1757. return onEllipse(w / 2, h / 2, Math.max(0, g.crx - g.th / 2), Math.max(0, g.cry - g.th / 2), g.st);
  1758. },
  1759. set: function (w, h, x, y, a) {
  1760. var g = circGeo(w, h, a);
  1761. var ang = Math.atan2(y - h / 2, x - w / 2);
  1762. var t = insetAt(g.crx, g.cry, ang, Math.sqrt((x - w / 2) * (x - w / 2) +
  1763. (y - h / 2) * (y - h / 2)), Math.min(g.crx, g.cry));
  1764. return { adj4: angleGuide(x, y, w / 2, h / 2),
  1765. adj1: Math.round(pin(0, 2 * t / Math.max(1e-6, g.ss) * 100000, g.a5 * 2)) };
  1766. }
  1767. }, {
  1768. // the point
  1769. pos: function (w, h, a) {
  1770. var g = circGeo(w, h, a);
  1771. return onEllipse(w / 2, h / 2, g.crx, g.cry, g.en);
  1772. },
  1773. set: function (w, h, x, y) { return { adj3: angleGuide(x, y, w / 2, h / 2) }; }
  1774. }, {
  1775. // the head's inner corner: how long the head is and how wide
  1776. pos: function (w, h, a) {
  1777. var g = circGeo(w, h, a);
  1778. return onEllipse(w / 2, h / 2, Math.max(0, g.crx - g.thh), Math.max(0, g.cry - g.thh), g.hb);
  1779. },
  1780. set: function (w, h, x, y, a) {
  1781. var g = circGeo(w, h, a);
  1782. var ang = Math.atan2(y - h / 2, x - w / 2);
  1783. var span = sweepOf(ang, g.en);
  1784. // dragged past the point: no head at all
  1785. if (span > Math.PI * 1.5) span = 0;
  1786. var t = insetAt(w / 2, h / 2, ang, Math.sqrt((x - w / 2) * (x - w / 2) +
  1787. (y - h / 2) * (y - h / 2)), g.ss / 2);
  1788. return { adj2: Math.round(Math.min(span, g.full) * 180 / Math.PI * DEG),
  1789. adj5: Math.round(pin(0, t / 2 / Math.max(1e-6, g.ss) * 100000, 25000)) };
  1790. }
  1791. }]
  1792. },
  1793. path: function (w, h, adj) { return circPath(w, h, adj); }
  1794. },
  1795. rightArrowCallout: arrowCalloutDef("right", "Callout: right arrow"),
  1796. leftArrowCallout: arrowCalloutDef("left", "Callout: left arrow"),
  1797. upArrowCallout: arrowCalloutDef("up", "Callout: up arrow"),
  1798. downArrowCallout: arrowCalloutDef("down", "Callout: down arrow"),
  1799. leftRightArrowCallout: {
  1800. cat: "arrow", label: "Callout: left-right arrow",
  1801. adj: {
  1802. defaults: { adj1: 25000, adj2: 25000, adj3: 25000, adj4: 48123 },
  1803. handles: [fit("adj1", 0, function (w, h, ss, a) { return lrCalloutGeo(w, h, a).a2 * 2; },
  1804. function (w, h, a) { var g = lrCalloutGeo(w, h, a); return [g.x1, g.y2]; }),
  1805. fit("adj2", 0, function (w, h, ss) { return 50000 * h / ss; },
  1806. function (w, h, a) { return [0, lrCalloutGeo(w, h, a).y1]; }),
  1807. fit("adj3", 0, function (w, h, ss) { return 50000 * w / ss; },
  1808. function (w, h, a) { return [lrCalloutGeo(w, h, a).x1, 0]; }),
  1809. fit("adj4", 0, function (w, h, ss, a) { return lrCalloutGeo(w, h, a).max4; },
  1810. function (w, h, a) { return [lrCalloutGeo(w, h, a).x2, h]; })]
  1811. },
  1812. pts: function (w, h, adj) {
  1813. var g = lrCalloutGeo(w, h, adj), vc = h / 2;
  1814. return [[0, vc], [g.x1, g.y1], [g.x1, g.y2], [g.x2, g.y2], [g.x2, 0], [g.x3, 0],
  1815. [g.x3, g.y2], [g.x4, g.y2], [g.x4, g.y1], [w, vc], [g.x4, g.y4], [g.x4, g.y3],
  1816. [g.x3, g.y3], [g.x3, h], [g.x2, h], [g.x2, g.y3], [g.x1, g.y3], [g.x1, g.y4]];
  1817. }
  1818. },
  1819. /* ---------- call outs: stars, banners, bubbles ---------- */
  1820. star4: starDef(4, "4-point star", 0.3),
  1821. star5: starDef(5, "5-point star", 0.42),
  1822. star6: starDef(6, "6-point star", 0.5),
  1823. star7: starDef(7, "7-point star", 0.55),
  1824. star8: starDef(8, "8-point star", 0.6),
  1825. star10: starDef(10, "10-point star", 0.68),
  1826. star12: starDef(12, "12-point star", 0.72),
  1827. star16: starDef(16, "16-point star", 0.78),
  1828. star24: starDef(24, "24-point star", 0.84),
  1829. star32: starDef(32, "32-point star", 0.88),
  1830. irregularSeal1: {
  1831. cat: "callout", label: "Explosion",
  1832. pts: function (w, h) {
  1833. var f = [[0.22, 0.14], [0.35, 0.30], [0.28, 0.0], [0.47, 0.24], [0.55, 0.06],
  1834. [0.60, 0.28], [0.79, 0.10], [0.74, 0.32], [1.0, 0.28], [0.83, 0.46],
  1835. [0.98, 0.56], [0.79, 0.60], [0.90, 0.83], [0.66, 0.70], [0.64, 1.0],
  1836. [0.50, 0.74], [0.36, 0.94], [0.34, 0.70], [0.12, 0.82], [0.22, 0.60],
  1837. [0.0, 0.56], [0.16, 0.44], [0.04, 0.30]];
  1838. return f.map(function (p) { return [p[0] * w, p[1] * h]; });
  1839. }
  1840. },
  1841. irregularSeal2: {
  1842. cat: "callout", label: "Explosion 2",
  1843. pts: function (w, h) {
  1844. var f = [[0.12, 0.26], [0.28, 0.18], [0.22, 0.02], [0.42, 0.14], [0.50, 0.0],
  1845. [0.58, 0.16], [0.74, 0.06], [0.74, 0.24], [0.94, 0.18], [0.86, 0.36],
  1846. [1.0, 0.44], [0.84, 0.54], [0.96, 0.70], [0.76, 0.68], [0.80, 0.88],
  1847. [0.62, 0.76], [0.54, 1.0], [0.44, 0.80], [0.28, 0.92], [0.30, 0.72],
  1848. [0.10, 0.76], [0.20, 0.58], [0.0, 0.50], [0.18, 0.40]];
  1849. return f.map(function (p) { return [p[0] * w, p[1] * h]; });
  1850. }
  1851. },
  1852. ribbon: {
  1853. cat: "callout", label: "Ribbon",
  1854. pts: function (w, h) {
  1855. var e = w * 0.14, t = h * 0.28;
  1856. return [[0, 0], [e, h * 0.2], [0, h * 0.2], [w * 0.18, h * 0.5],
  1857. [0, h * 0.8], [e, h], [w * 0.18, h * 0.8],
  1858. [w * 0.82, h * 0.8], [w - e, h], [w, h * 0.8],
  1859. [w * 0.82, h * 0.5], [w, h * 0.2], [w - e, h * 0.2], [w, 0],
  1860. [w * 0.82, h * 0.2], [w * 0.18, h * 0.2]];
  1861. }
  1862. },
  1863. ribbon2: {
  1864. cat: "callout", label: "Banner",
  1865. pts: function (w, h) {
  1866. var e = w * 0.14;
  1867. return [[0, h * 0.16], [w * 0.18, 0], [w * 0.82, 0], [w, h * 0.16],
  1868. [w - e, h * 0.16], [w, h * 0.4], [w * 0.82, h * 0.34],
  1869. [w * 0.82, h], [w * 0.5, h * 0.78], [w * 0.18, h],
  1870. [w * 0.18, h * 0.34], [0, h * 0.4], [e, h * 0.16]];
  1871. }
  1872. },
  1873. wave: {
  1874. cat: "callout", label: "Wave",
  1875. adj: waveAdj(12500, 20000),
  1876. path: function (w, h, adj) { return wavePath(w, h, adj, 12500, 20000, 1); }
  1877. },
  1878. doubleWave: {
  1879. cat: "callout", label: "Double wave",
  1880. adj: waveAdj(6250, 12500),
  1881. path: function (w, h, adj) { return wavePath(w, h, adj, 6250, 12500, 2); }
  1882. },
  1883. verticalScroll: {
  1884. cat: "callout", label: "Vertical scroll",
  1885. path: function (w, h) {
  1886. var t = Math.min(w, h) * 0.16;
  1887. return P().M(t, 0).L(w - t / 2, 0)
  1888. .arc(w - t / 2, t / 2, t / 2, t / 2, -Math.PI / 2, Math.PI / 2)
  1889. .L(w - t, t).L(w - t, h).L(t / 2, h)
  1890. .arc(t / 2, h - t / 2, t / 2, t / 2, Math.PI / 2, Math.PI * 1.5)
  1891. .L(t, h - t).Z().toString();
  1892. }
  1893. },
  1894. horizontalScroll: {
  1895. cat: "callout", label: "Horizontal scroll",
  1896. path: function (w, h) {
  1897. var t = Math.min(w, h) * 0.16;
  1898. return P().M(0, t).L(0, h - t / 2)
  1899. .arc(t / 2, h - t / 2, t / 2, t / 2, Math.PI, Math.PI * 2)
  1900. .L(t, h - t).L(w, h - t).L(w, t / 2)
  1901. .arc(w - t / 2, t / 2, t / 2, t / 2, 0, Math.PI)
  1902. .L(w - t, t).Z().toString();
  1903. }
  1904. },
  1905. wedgeRectCallout: {
  1906. cat: "callout", label: "Speech bubble: rectangle",
  1907. pts: function (w, h) {
  1908. var b = h * 0.72;
  1909. return [[0, 0], [w, 0], [w, b], [w * 0.42, b], [w * 0.2, h],
  1910. [w * 0.28, b], [0, b]];
  1911. }
  1912. },
  1913. wedgeRoundRectCallout: {
  1914. cat: "callout", label: "Speech bubble: rounded",
  1915. path: function (w, h) {
  1916. var b = h * 0.72;
  1917. var r = Math.min(w, b) * 0.18;
  1918. var p = P();
  1919. p.M(r, 0).L(w - r, 0).arc(w - r, r, r, r, -Math.PI / 2, 0)
  1920. .L(w, b - r).arc(w - r, b - r, r, r, 0, Math.PI / 2)
  1921. .L(w * 0.42, b).L(w * 0.2, h).L(w * 0.28, b)
  1922. .L(r, b).arc(r, b - r, r, r, Math.PI / 2, Math.PI)
  1923. .L(0, r).arc(r, r, r, r, Math.PI, Math.PI * 1.5);
  1924. return p.Z().toString();
  1925. }
  1926. },
  1927. wedgeEllipseCallout: {
  1928. cat: "callout", label: "Speech bubble: oval",
  1929. path: function (w, h) {
  1930. // all the way round from one side of the tail's base to the
  1931. // other, then out to the tip and back
  1932. var cy = h * 0.36, ry = h * 0.36;
  1933. var a1 = Math.PI * 0.60, a2 = Math.PI * 0.80;
  1934. return P().arc(w / 2, cy, w / 2, ry, a2, a1 + Math.PI * 2, true)
  1935. .L(w * 0.16, h).Z().toString();
  1936. }
  1937. },
  1938. cloudCallout: {
  1939. cat: "callout", label: "Speech bubble: cloud",
  1940. path: function (w, h) {
  1941. var bw = w, bh = h * 0.74;
  1942. var p = P();
  1943. p.M(bw * 0.22, bh * 0.92)
  1944. .C(bw * 0.02, bh * 0.92, bw * -0.05, bh * 0.58, bw * 0.13, bh * 0.5)
  1945. .C(bw * 0.06, bh * 0.28, bw * 0.26, bh * 0.1, bw * 0.42, bh * 0.2)
  1946. .C(bw * 0.5, bh * -0.03, bw * 0.8, bh * -0.03, bw * 0.86, bh * 0.22)
  1947. .C(bw * 1.04, bh * 0.26, bw * 1.04, bh * 0.6, bw * 0.87, bh * 0.66)
  1948. .C(bw * 0.93, bh * 0.88, bw * 0.7, bh * 1.02, bw * 0.57, bh * 0.9)
  1949. .C(bw * 0.48, bh * 1.0, bw * 0.3, bh * 1.0, bw * 0.22, bh * 0.92)
  1950. .Z();
  1951. p.circle(w * 0.22, h * 0.85, w * 0.07, h * 0.07);
  1952. p.circle(w * 0.13, h * 0.96, w * 0.045, h * 0.045);
  1953. return p.toString();
  1954. }
  1955. },
  1956. /* ---------- equation ---------- */
  1957. mathPlus: {
  1958. cat: "equation", label: "Plus",
  1959. // adj1 is the bars' thickness, in ss
  1960. adj: {
  1961. defaults: { adj1: 23520 },
  1962. handles: [ssLen("adj1", 73490, function (w, h, d) { return [w / 2 - mathX(w), h / 2 - d / 2]; },
  1963. function (w, h, x, y) { return (h / 2 - y) * 2; })]
  1964. },
  1965. pts: function (w, h, adj) {
  1966. var dx2 = Math.min(w, h) * pin(0, av(adj, "adj1", 23520), 73490) / 200000;
  1967. var dy1 = h * 73490 / 200000, hc = w / 2, vc = h / 2;
  1968. var x1 = hc - mathX(w), x2 = hc - dx2, x3 = hc + dx2, x4 = hc + mathX(w);
  1969. var y1 = vc - dy1, y2 = vc - dx2, y3 = vc + dx2, y4 = vc + dy1;
  1970. return [[x1, y2], [x2, y2], [x2, y1], [x3, y1], [x3, y2], [x4, y2], [x4, y3], [x3, y3],
  1971. [x3, y4], [x2, y4], [x2, y3], [x1, y3]];
  1972. }
  1973. },
  1974. mathMinus: {
  1975. cat: "equation", label: "Minus",
  1976. adj: {
  1977. defaults: { adj1: 23520 },
  1978. handles: [share("adj1", "h", 100000, function (w, h, d) { return [w / 2 - mathX(w), h / 2 - d / 2]; },
  1979. function (w, h, x, y) { return (h / 2 - y) * 2; })]
  1980. },
  1981. pts: function (w, h, adj) {
  1982. var dy1 = h * pin(0, av(adj, "adj1", 23520), 100000) / 200000;
  1983. var x1 = w / 2 - mathX(w), x2 = w / 2 + mathX(w);
  1984. return [[x1, h / 2 - dy1], [x2, h / 2 - dy1], [x2, h / 2 + dy1], [x1, h / 2 + dy1]];
  1985. }
  1986. },
  1987. mathMultiply: {
  1988. cat: "equation", label: "Multiply",
  1989. // adj1 is the arms' thickness, in ss; they run along the diagonals
  1990. adj: {
  1991. defaults: { adj1: 23520 },
  1992. handles: [fit("adj1", 0, 51965, function (w, h, a) { var g = mathMulGeo(w, h, a); return [g.xB, g.yB]; })]
  1993. },
  1994. pts: function (w, h, adj) {
  1995. var g = mathMulGeo(w, h, adj), hc = w / 2, vc = h / 2;
  1996. return [[g.xA, g.yA], [g.xB, g.yB], [hc, g.yC], [g.xD, g.yB], [g.xE, g.yA], [g.xF, vc],
  1997. [g.xE, h - g.yA], [g.xD, h - g.yB], [hc, h - g.yC], [g.xB, h - g.yB], [g.xA, h - g.yA],
  1998. [g.xL, vc]];
  1999. }
  2000. },
  2001. mathDivide: {
  2002. cat: "equation", label: "Divide",
  2003. // adj1 the bar, adj2 the gap to the dots, adj3 the dots' radius
  2004. adj: {
  2005. defaults: { adj1: 23520, adj2: 5880, adj3: 11760 },
  2006. handles: [fit("adj1", 1000, 36745, function (w, h, a) { var g = mathDivGeo(w, h, a); return [g.x1, g.y3]; }),
  2007. fit("adj2", 0, function (w, h, ss, a) { return mathDivGeo(w, h, a).max2; },
  2008. function (w, h, a) { return [w / 2, mathDivGeo(w, h, a).y2]; }),
  2009. fit("adj3", 1000, function (w, h, ss, a) { return mathDivGeo(w, h, a).max3; },
  2010. function (w, h, a) { var g = mathDivGeo(w, h, a); return [w / 2 - g.rad, g.y1 + g.rad]; })]
  2011. },
  2012. path: function (w, h, adj) {
  2013. var g = mathDivGeo(w, h, adj), hc = w / 2;
  2014. return P().circle(hc, g.y1 + g.rad, g.rad, g.rad).toString() +
  2015. P().circle(hc, h - g.y1 - g.rad, g.rad, g.rad).toString() +
  2016. P().poly([[g.x1, g.y3], [g.x3, g.y3], [g.x3, g.y4], [g.x1, g.y4]]).toString();
  2017. }
  2018. },
  2019. mathEqual: {
  2020. cat: "equation", label: "Equal",
  2021. adj: {
  2022. defaults: { adj1: 23520, adj2: 11760 },
  2023. handles: [fit("adj1", 0, 36745, function (w, h, a) { var g = mathEqGeo(w, h, a, "adj2"); return [g.x2, g.y1]; }),
  2024. fit("adj2", 0, function (w, h, ss, a) { return mathEqGeo(w, h, a, "adj2").max2; },
  2025. function (w, h, a) { var g = mathEqGeo(w, h, a, "adj2"); return [g.x1, g.y2]; })]
  2026. },
  2027. path: function (w, h, adj) {
  2028. var g = mathEqGeo(w, h, adj, "adj2");
  2029. return P().poly([[g.x1, g.y1], [g.x2, g.y1], [g.x2, g.y2], [g.x1, g.y2]]).toString() +
  2030. P().poly([[g.x1, g.y3], [g.x2, g.y3], [g.x2, g.y4], [g.x1, g.y4]]).toString();
  2031. }
  2032. },
  2033. mathNotEqual: {
  2034. cat: "equation", label: "Not equal",
  2035. /* The bars and the stroke are wound the same way and filled
  2036. non-zero, so where they cross is filled rather than punched
  2037. out. adj1 the bars, adj3 their gap, adj2 the stroke's angle. */
  2038. adj: {
  2039. defaults: { adj1: 23520, adj2: 6600000, adj3: 11760 },
  2040. handles: [fit("adj1", 0, 50000, function (w, h, a) { var g = mathEqGeo(w, h, a, "adj3"); return [g.x2, g.y1]; }),
  2041. fit("adj2", 4200000, 6600000, function (w, h, a) {
  2042. return [notEqSlash(w, h, a, mathEqGeo(w, h, a, "adj3").dy1).top, 0];
  2043. }),
  2044. fit("adj3", 0, function (w, h, ss, a) { return mathEqGeo(w, h, a, "adj3").max2; },
  2045. function (w, h, a) { var g = mathEqGeo(w, h, a, "adj3"); return [g.x1, g.y2]; })]
  2046. },
  2047. path: function (w, h, adj) {
  2048. var g = mathEqGeo(w, h, adj, "adj3");
  2049. var sl = notEqSlash(w, h, adj, g.dy1), bot = w - sl.top, b2 = sl.bhw / 2;
  2050. return P().poly([[g.x1, g.y1], [g.x2, g.y1], [g.x2, g.y2], [g.x1, g.y2]]).toString() +
  2051. P().poly([[g.x1, g.y3], [g.x2, g.y3], [g.x2, g.y4], [g.x1, g.y4]]).toString() +
  2052. P().poly([[sl.top - b2, 0], [sl.top + b2, 0], [bot + b2, h], [bot - b2, h]]).toString();
  2053. }
  2054. },
  2055. leftBracket: {
  2056. cat: "equation", size: [70, 220], label: "Left bracket",
  2057. open: true,
  2058. // adj is how far the curls run, in ss
  2059. adj: {
  2060. defaults: { adj: 8333 },
  2061. handles: [ssLen("adj", hCap(50000), function (w, h, d) { return [0, d]; },
  2062. function (w, h, x, y) { return y; })]
  2063. },
  2064. path: function (w, h, adj) {
  2065. var ss = Math.min(w, h), y1 = ss * pin(0, av(adj, "adj", 8333), 50000 * h / ss) / 100000;
  2066. return P().M(w, h).arcTo(w, y1, Math.PI / 2, Math.PI / 2).L(0, y1)
  2067. .arcTo(w, y1, Math.PI, Math.PI / 2).toString();
  2068. }
  2069. },
  2070. rightBracket: {
  2071. cat: "equation", size: [70, 220], label: "Right bracket",
  2072. open: true,
  2073. adj: {
  2074. defaults: { adj: 8333 },
  2075. handles: [ssLen("adj", hCap(50000), function (w, h, d) { return [w, d]; },
  2076. function (w, h, x, y) { return y; })]
  2077. },
  2078. path: function (w, h, adj) {
  2079. var ss = Math.min(w, h), y1 = ss * pin(0, av(adj, "adj", 8333), 50000 * h / ss) / 100000;
  2080. return P().M(0, 0).arcTo(w, y1, Math.PI * 1.5, Math.PI / 2).L(w, h - y1)
  2081. .arcTo(w, y1, 0, Math.PI / 2).toString();
  2082. }
  2083. },
  2084. bracketPair: {
  2085. cat: "equation", size: [70, 220], label: "Bracket pair",
  2086. open: true,
  2087. // the two sides of a rounded rectangle; adj is its corner, in ss
  2088. adj: {
  2089. defaults: { adj: 16667 },
  2090. handles: [ssLen("adj", 50000, function (w, h, d) { return [0, d]; },
  2091. function (w, h, x, y) { return y; })]
  2092. },
  2093. path: function (w, h, adj) {
  2094. var r = Math.min(w, h) * pin(0, av(adj, "adj", 16667), 50000) / 100000, H = Math.PI / 2;
  2095. return P().M(r, h).arcTo(r, r, H, H).L(0, r).arcTo(r, r, Math.PI, H).toString() +
  2096. P().M(w - r, 0).arcTo(r, r, Math.PI * 1.5, H).L(w, h - r).arcTo(r, r, 0, H).toString();
  2097. }
  2098. },
  2099. rightBrace: {
  2100. cat: "equation", size: [70, 220], label: "Right brace",
  2101. open: true,
  2102. // adj1 how far the curls run (in ss), adj2 where the point is
  2103. adj: {
  2104. defaults: { adj1: 8333, adj2: 50000 },
  2105. handles: [fit("adj1", 0, function (w, h, ss, a) { return braceGeo(w, h, a).max1; },
  2106. function (w, h, a) { return [w / 2, braceGeo(w, h, a).y1]; }),
  2107. share("adj2", "h", 100000, function (w, h, d) { return [w, d]; },
  2108. function (w, h, x, y) { return y; })]
  2109. },
  2110. path: function (w, h, adj) {
  2111. return mapPath(bracePath(w, h, adj), function (x, y) { return [w - x, y]; });
  2112. }
  2113. },
  2114. leftBrace: {
  2115. cat: "equation", size: [70, 220], label: "Left brace",
  2116. open: true,
  2117. adj: {
  2118. defaults: { adj1: 8333, adj2: 50000 },
  2119. handles: [fit("adj1", 0, function (w, h, ss, a) { return braceGeo(w, h, a).max1; },
  2120. function (w, h, a) { return [w / 2, braceGeo(w, h, a).y1]; }),
  2121. share("adj2", "h", 100000, function (w, h, d) { return [0, d]; },
  2122. function (w, h, x, y) { return y; })]
  2123. },
  2124. path: function (w, h, adj) { return bracePath(w, h, adj); }
  2125. },
  2126. bracePair: {
  2127. cat: "equation", size: [70, 220], label: "Brace pair",
  2128. open: true,
  2129. // adj is how far the curls run, in ss
  2130. adj: {
  2131. defaults: { adj: 8333 },
  2132. handles: [ssLen("adj", 25000, function (w, h, d) { return [0, d]; },
  2133. function (w, h, x, y) { return y; })]
  2134. },
  2135. path: function (w, h, adj) {
  2136. var r = Math.min(w, h) * pin(0, av(adj, "adj", 8333), 25000) / 100000, H = Math.PI / 2;
  2137. var vc = h / 2;
  2138. return P().M(2 * r, h).arcTo(r, r, H, H).L(r, vc + r).arcTo(r, r, 0, -H)
  2139. .arcTo(r, r, H, -H).L(r, r).arcTo(r, r, Math.PI, H).toString() +
  2140. P().M(w - 2 * r, 0).arcTo(r, r, Math.PI * 1.5, H).L(w - r, vc - r)
  2141. .arcTo(r, r, Math.PI, -H).arcTo(r, r, Math.PI * 1.5, -H).L(w - r, h - r)
  2142. .arcTo(r, r, 0, H).toString();
  2143. }
  2144. }
  2145. };
  2146. /* ---------------- names documents may still carry ---------------- */
  2147. /* Three shapes had editor-invented names before the catalogue existed.
  2148. They are gone; these are what a deck written back then says, and the
  2149. only place that still knows. normalizeBody() in slides.js runs every
  2150. kind through canonical() as a document loads, so a deck is rewritten
  2151. to the catalogue's names the first time it is opened and these never
  2152. have to be thought about again. */
  2153. var ALIASES = {
  2154. round: "roundRect",
  2155. arrow: "rightArrow",
  2156. star: "star5"
  2157. };
  2158. function canonical(kind) {
  2159. var k = String(kind || "").trim();
  2160. if (!k) return "rect";
  2161. if (DEFS[k]) return k;
  2162. if (ALIASES[k]) return ALIASES[k];
  2163. return k;
  2164. }
  2165. function def(kind) { return DEFS[canonical(kind)] || null; }
  2166. function points(kind, w, h, adj) {
  2167. var d = def(kind);
  2168. return d && d.pts ? d.pts(w, h, adj) : null;
  2169. }
  2170. /* A preset's own adjustment handles (see ssLen): where each sits in a
  2171. w x h frame, and the adjustments that result from dragging one of
  2172. them to (x, y). The result always states every guide the preset has,
  2173. so what is written to .pptx never leans on a default. The callouts'
  2174. tips and a rounded rectangle's radius are not in here: those are
  2175. older and slides.js handles them itself (adjustable()). */
  2176. function resolvedAdj(d, adj) {
  2177. var out = {};
  2178. Object.keys(d.adj.defaults).forEach(function (k) {
  2179. var v = adj ? Number(adj[k]) : NaN;
  2180. out[k] = isFinite(v) ? v : d.adj.defaults[k];
  2181. });
  2182. return out;
  2183. }
  2184. function handles(kind, w, h, adj) {
  2185. var d = def(kind);
  2186. if (!d || !d.adj || !(w > 0) || !(h > 0)) return [];
  2187. var a = resolvedAdj(d, adj);
  2188. return d.adj.handles.map(function (hd) { return hd.pos(w, h, a); });
  2189. }
  2190. function dragHandle(kind, i, w, h, x, y, adj) {
  2191. var d = def(kind);
  2192. if (!d || !d.adj || !d.adj.handles[i]) return adj || null;
  2193. var out = resolvedAdj(d, adj);
  2194. var set = d.adj.handles[i].set(w, h, x, y, out);
  2195. Object.keys(set).forEach(function (k) { if (isFinite(set[k])) out[k] = set[k]; });
  2196. return out;
  2197. }
  2198. /* ---------------- adjustments ----------------
  2199. A preset's adjustments, named and scaled as PresentationML's (adj1,
  2200. adj2, ... in 1/100000 of the frame). The speech-bubble callouts are
  2201. drawn the way PowerPoint defines them whenever the object carries
  2202. props.adj: the body fills the frame and the tail runs out to the tip
  2203. at (centre + adj1 * w, centre + adj2 * h), which may well lie outside
  2204. the frame. A callout with no adj was made before adjustments existed
  2205. and keeps the shape it was drawn with (body in the top ~72%, tip at
  2206. the bottom) until its handle is first dragged - see legacyTip. */
  2207. var TIP_DEFAULTS = {
  2208. wedgeRectCallout: { adj1: -20833, adj2: 62500 },
  2209. wedgeRoundRectCallout: { adj1: -20833, adj2: 62500, adj3: 16667 },
  2210. wedgeEllipseCallout: { adj1: -20833, adj2: 62500 },
  2211. cloudCallout: { adj1: -20833, adj2: 62500 }
  2212. };
  2213. // where the old, unadjusted drawings put the tip and how much of the
  2214. // frame their body took
  2215. var LEGACY_TIP = {
  2216. wedgeRectCallout: { tip: [0.2, 1], body: 0.72, round: 0 },
  2217. wedgeRoundRectCallout: { tip: [0.2, 1], body: 0.72, round: 0.18 },
  2218. wedgeEllipseCallout: { tip: [0.16, 1], body: 0.72, round: 0 },
  2219. cloudCallout: { tip: [0.13, 0.96], body: 0.74, round: 0 }
  2220. };
  2221. function adjOf(kind, adj) {
  2222. var out = {}, d = TIP_DEFAULTS[kind] || {};
  2223. Object.keys(d).forEach(function (k) { out[k] = d[k]; });
  2224. Object.keys(adj || {}).forEach(function (k) {
  2225. var v = Number(adj[k]);
  2226. if (isFinite(v)) out[k] = v;
  2227. });
  2228. return out;
  2229. }
  2230. function tipPoint(kind, w, h, adj) {
  2231. var a = adjOf(kind, adj);
  2232. return [w / 2 + a.adj1 * w / 100000, h / 2 + a.adj2 * h / 100000];
  2233. }
  2234. /* The tail of a rectangular callout, as presetShapeDefinitions puts it:
  2235. it leaves the side the tip is furthest beyond (measured in the
  2236. frame's own proportions) between 2/12 and 5/12 of that side, or 7/12
  2237. and 10/12 when the tip is past the middle. Every side gets a tail
  2238. vertex; on the three that do not carry the tail it lies on the side
  2239. itself. */
  2240. function wedgeSides(w, h, a) {
  2241. var dxPos = w * a.adj1 / 100000, dyPos = h * a.adj2 / 100000;
  2242. var xPos = w / 2 + dxPos, yPos = h / 2 + dyPos;
  2243. var dq = dxPos * h / w;
  2244. var dz = Math.abs(dyPos) - Math.abs(dq); // > 0: the tip is above or below
  2245. var x1 = w * (dxPos > 0 ? 7 : 2) / 12, x2 = w * (dxPos > 0 ? 10 : 5) / 12;
  2246. var y1 = h * (dyPos > 0 ? 7 : 2) / 12, y2 = h * (dyPos > 0 ? 10 : 5) / 12;
  2247. var q = function (c, a1, b1) { return c > 0 ? a1 : b1; };
  2248. return {
  2249. x1: x1, x2: x2, y1: y1, y2: y2,
  2250. xt: q(dz, q(dyPos, x1, xPos), x1), yt: q(dz, q(dyPos, 0, yPos), 0),
  2251. xr: q(dz, w, q(dxPos, xPos, w)), yr: q(dz, y1, q(dxPos, yPos, y1)),
  2252. xb: q(dz, q(dyPos, xPos, x1), x1), yb: q(dz, q(dyPos, yPos, h), h),
  2253. xl: q(dz, 0, q(dxPos, 0, xPos)), yl: q(dz, y1, q(dxPos, y1, yPos))
  2254. };
  2255. }
  2256. function adjustedPath(kind, w, h, adj) {
  2257. var a = adjOf(kind, adj);
  2258. var e, t;
  2259. if (kind === "wedgeRectCallout") {
  2260. e = wedgeSides(w, h, a);
  2261. return P().poly([[0, 0], [e.x1, 0], [e.xt, e.yt], [e.x2, 0], [w, 0], [w, e.y1],
  2262. [e.xr, e.yr], [w, e.y2], [w, h], [e.x2, h], [e.xb, e.yb], [e.x1, h], [0, h],
  2263. [0, e.y2], [e.xl, e.yl], [0, e.y1]]).toString();
  2264. }
  2265. if (kind === "wedgeRoundRectCallout") {
  2266. e = wedgeSides(w, h, a);
  2267. var u = Math.min(w, h) * Math.max(0, Math.min(50000, a.adj3)) / 100000;
  2268. var p = P();
  2269. p.M(u, 0).L(e.x1, 0).L(e.xt, e.yt).L(e.x2, 0).L(w - u, 0)
  2270. .arc(w - u, u, u, u, -Math.PI / 2, 0)
  2271. .L(w, e.y1).L(e.xr, e.yr).L(w, e.y2).L(w, h - u)
  2272. .arc(w - u, h - u, u, u, 0, Math.PI / 2)
  2273. .L(e.x2, h).L(e.xb, e.yb).L(e.x1, h).L(u, h)
  2274. .arc(u, h - u, u, u, Math.PI / 2, Math.PI)
  2275. .L(0, e.y2).L(e.xl, e.yl).L(0, e.y1).L(0, u)
  2276. .arc(u, u, u, u, Math.PI, Math.PI * 1.5);
  2277. return p.Z().toString();
  2278. }
  2279. if (kind === "wedgeEllipseCallout") {
  2280. // the tail leaves the ellipse 11 degrees either side of the
  2281. // direction of the tip, and the outline goes the long way round
  2282. t = tipPoint(kind, w, h, a);
  2283. var pang = Math.atan2((t[1] - h / 2) * w, (t[0] - w / 2) * h);
  2284. var gap = 11 * Math.PI / 180;
  2285. return P().arc(w / 2, h / 2, w / 2, h / 2, pang + gap, pang - gap + Math.PI * 2, true)
  2286. .L(t[0], t[1]).Z().toString();
  2287. }
  2288. if (kind === "cloudCallout") {
  2289. var c = P();
  2290. c.M(w * 0.22, h * 0.92)
  2291. .C(w * 0.02, h * 0.92, w * -0.05, h * 0.58, w * 0.13, h * 0.5)
  2292. .C(w * 0.06, h * 0.28, w * 0.26, h * 0.1, w * 0.42, h * 0.2)
  2293. .C(w * 0.5, h * -0.03, w * 0.8, h * -0.03, w * 0.86, h * 0.22)
  2294. .C(w * 1.04, h * 0.26, w * 1.04, h * 0.6, w * 0.87, h * 0.66)
  2295. .C(w * 0.93, h * 0.88, w * 0.7, h * 1.02, w * 0.57, h * 0.9)
  2296. .C(w * 0.48, h * 1.0, w * 0.3, h * 1.0, w * 0.22, h * 0.92)
  2297. .Z();
  2298. // three bubbles from the edge of the cloud out to the tip
  2299. t = tipPoint(kind, w, h, a);
  2300. var dx = t[0] - w / 2, dy = t[1] - h / 2;
  2301. var reach = Math.sqrt(Math.pow(dx / (w / 2), 2) + Math.pow(dy / (h / 2), 2));
  2302. if (reach > 1) {
  2303. var ex = w / 2 + dx / reach, ey = h / 2 + dy / reach;
  2304. var ss = Math.min(w, h);
  2305. [[0.3, 0.09], [0.65, 0.06], [1, 0.035]].forEach(function (b) {
  2306. c.circle(ex + (t[0] - ex) * b[0], ey + (t[1] - ey) * b[0], ss * b[1], ss * b[1]);
  2307. });
  2308. }
  2309. return c.toString();
  2310. }
  2311. return "";
  2312. }
  2313. function path(kind, w, h, adj) {
  2314. var d = def(kind);
  2315. if (!d) return "";
  2316. if (adj && TIP_DEFAULTS[kind]) return adjustedPath(kind, w, h, adj);
  2317. // presets that follow PowerPoint's formulas take the adjustments a
  2318. // deck states for them; the rest ignore the extra argument
  2319. if (d.pts) return P().poly(d.pts(w, h, adj)).toString();
  2320. return d.path(w, h, adj);
  2321. }
  2322. /* customPath draws a "custom" shape (an imported freeform, props.geom):
  2323. each path is stored in its own w x h space as M / L / C / Z, the same
  2324. vocabulary as the catalogue, and is stretched over the frame here. */
  2325. function customPath(geom, w, h, which) {
  2326. if (!geom || !Array.isArray(geom.paths)) return "";
  2327. var out = "";
  2328. geom.paths.forEach(function (gp) {
  2329. if (which === "fill" && gp.noFill) return;
  2330. if (which === "stroke" && gp.noStroke) return;
  2331. var kx = w / (Number(gp.w) || 1), ky = h / (Number(gp.h) || 1);
  2332. var re = /([MLCZ])([^MLCZ]*)/g, m;
  2333. while ((m = re.exec(String(gp.d || "")))) {
  2334. var nums = m[2].trim() ? m[2].trim().split(/[\s,]+/).map(Number) : [];
  2335. out += m[1];
  2336. for (var i = 0; i + 1 < nums.length; i += 2) {
  2337. out += (i ? " " : "") + (nums[i] * kx).toFixed(2) + " " + (nums[i + 1] * ky).toFixed(2);
  2338. }
  2339. }
  2340. });
  2341. return out;
  2342. }
  2343. function detail(kind, w, h, adj) {
  2344. var d = def(kind);
  2345. return d && d.detail ? d.detail(w, h, adj) : "";
  2346. }
  2347. /* defaultSize is the box a shape is inserted at. Most want the editor's
  2348. usual one; a brace or a bracket only reads as itself when it is tall
  2349. and narrow, so those say so. */
  2350. function defaultSize(kind) {
  2351. var d = def(kind);
  2352. return (d && d.size) ? d.size.slice() : null;
  2353. }
  2354. /* icon draws a shape as a small outline for a picker, from the very
  2355. same geometry the canvas uses - so an icon cannot come to disagree
  2356. with what choosing it actually inserts. */
  2357. function icon(kind, size) {
  2358. var s = size || 22, pad = 2;
  2359. var w = s - pad * 2, h = s - pad * 2;
  2360. var k = canonical(kind);
  2361. var body = '<path d="' + path(k, w, h) + '"' +
  2362. (evenOdd(k) ? ' fill-rule="evenodd"' : "") + "/>";
  2363. var det = detail(k, w, h);
  2364. if (det) body += '<path d="' + det + '"/>';
  2365. return '<svg width="' + s + '" height="' + s + '" viewBox="0 0 ' + s + " " + s + '" ' +
  2366. 'fill="none" stroke="currentColor" stroke-width="1.3" ' +
  2367. 'stroke-linejoin="round"><g transform="translate(' + pad + "," + pad + ')">' +
  2368. body + "</g></svg>";
  2369. }
  2370. // a freeform fills its inner subpaths as holes, as PowerPoint does
  2371. function evenOdd(kind) { if (kind === "custom") return true; var d = def(kind); return !!(d && d.evenOdd); }
  2372. var CATEGORIES = [
  2373. { id: "shape", label: "Shapes", icon: "square outline" },
  2374. { id: "arrow", label: "Arrows", icon: "long arrow alternate right" },
  2375. { id: "callout", label: "Call outs", icon: "comment outline" },
  2376. { id: "equation", label: "Equation", icon: "plus" }
  2377. ];
  2378. CATEGORIES.forEach(function (c) {
  2379. c.kinds = Object.keys(DEFS).filter(function (k) { return DEFS[k].cat === c.id; });
  2380. });
  2381. return {
  2382. CATEGORIES: CATEGORIES,
  2383. canonical: canonical,
  2384. has: function (kind) { return !!def(kind); },
  2385. label: function (kind) { var d = def(kind); return d ? d.label : "Shape"; },
  2386. category: function (kind) { var d = def(kind); return d ? d.cat : ""; },
  2387. evenOdd: evenOdd,
  2388. icon: icon,
  2389. isOpen: function (kind) { var d = def(kind); return !!(d && d.open); },
  2390. customPath: customPath,
  2391. points: points,
  2392. path: path,
  2393. // adjustments (see TIP_DEFAULTS): "tip" for the speech-bubble
  2394. // callouts, "radius" for a rounded rectangle, "" for the rest
  2395. adjustable: function (kind) {
  2396. kind = canonical(kind);
  2397. return TIP_DEFAULTS[kind] ? "tip" : (kind === "roundRect" ? "radius" : "");
  2398. },
  2399. tipDefaults: function (kind) { return adjOf(canonical(kind), null); },
  2400. handles: handles,
  2401. dragHandle: dragHandle,
  2402. tipPoint: function (kind, w, h, adj) { return tipPoint(canonical(kind), w, h, adj); },
  2403. legacyTip: function (kind) { return LEGACY_TIP[canonical(kind)] || null; },
  2404. detail: detail,
  2405. defaultSize: defaultSize
  2406. };
  2407. })();