@altnovatrail/picture-kit 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +28 -2
  4. package/lib/emoji.d.ts +16 -0
  5. package/lib/emoji.js +64 -0
  6. package/lib/index.d.ts +9 -0
  7. package/lib/index.js +10 -0
  8. package/lib/kind.d.ts +22 -0
  9. package/lib/kind.js +2 -0
  10. package/lib/kinds/balance.d.ts +34 -0
  11. package/lib/kinds/balance.js +217 -0
  12. package/lib/kinds/branches.d.ts +33 -0
  13. package/lib/kinds/branches.js +473 -0
  14. package/lib/kinds/calendar.d.ts +55 -0
  15. package/lib/kinds/calendar.js +532 -0
  16. package/lib/kinds/chart.d.ts +48 -0
  17. package/lib/kinds/chart.js +645 -0
  18. package/lib/kinds/choices.d.ts +14 -0
  19. package/lib/kinds/choices.js +53 -0
  20. package/lib/kinds/clock.d.ts +36 -0
  21. package/lib/kinds/clock.js +219 -0
  22. package/lib/kinds/coins.d.ts +37 -0
  23. package/lib/kinds/coins.js +550 -0
  24. package/lib/kinds/column.d.ts +37 -0
  25. package/lib/kinds/column.js +410 -0
  26. package/lib/kinds/commands-parts.d.ts +12 -0
  27. package/lib/kinds/commands-parts.js +24 -0
  28. package/lib/kinds/commands.d.ts +66 -0
  29. package/lib/kinds/commands.js +789 -0
  30. package/lib/kinds/containers.d.ts +52 -0
  31. package/lib/kinds/containers.js +758 -0
  32. package/lib/kinds/countfig.d.ts +41 -0
  33. package/lib/kinds/countfig.js +372 -0
  34. package/lib/kinds/cubes.d.ts +70 -0
  35. package/lib/kinds/cubes.js +371 -0
  36. package/lib/kinds/die.d.ts +32 -0
  37. package/lib/kinds/die.js +301 -0
  38. package/lib/kinds/domino.d.ts +37 -0
  39. package/lib/kinds/domino.js +450 -0
  40. package/lib/kinds/dotgrid.d.ts +38 -0
  41. package/lib/kinds/dotgrid.js +411 -0
  42. package/lib/kinds/figure.d.ts +62 -0
  43. package/lib/kinds/figure.js +687 -0
  44. package/lib/kinds/fold-parts.d.ts +73 -0
  45. package/lib/kinds/fold-parts.js +315 -0
  46. package/lib/kinds/fold.d.ts +52 -0
  47. package/lib/kinds/fold.js +650 -0
  48. package/lib/kinds/fraction.d.ts +50 -0
  49. package/lib/kinds/fraction.js +568 -0
  50. package/lib/kinds/gauges.d.ts +53 -0
  51. package/lib/kinds/gauges.js +880 -0
  52. package/lib/kinds/gears.d.ts +40 -0
  53. package/lib/kinds/gears.js +787 -0
  54. package/lib/kinds/grid.d.ts +45 -0
  55. package/lib/kinds/grid.js +591 -0
  56. package/lib/kinds/groups.d.ts +23 -0
  57. package/lib/kinds/groups.js +335 -0
  58. package/lib/kinds/index.d.ts +3 -0
  59. package/lib/kinds/index.js +87 -0
  60. package/lib/kinds/lanes.d.ts +36 -0
  61. package/lib/kinds/lanes.js +220 -0
  62. package/lib/kinds/layers.d.ts +31 -0
  63. package/lib/kinds/layers.js +419 -0
  64. package/lib/kinds/line.d.ts +38 -0
  65. package/lib/kinds/line.js +739 -0
  66. package/lib/kinds/magic-parts.d.ts +72 -0
  67. package/lib/kinds/magic-parts.js +186 -0
  68. package/lib/kinds/magic.d.ts +66 -0
  69. package/lib/kinds/magic.js +664 -0
  70. package/lib/kinds/map.d.ts +32 -0
  71. package/lib/kinds/map.js +370 -0
  72. package/lib/kinds/matchsticks.d.ts +26 -0
  73. package/lib/kinds/matchsticks.js +434 -0
  74. package/lib/kinds/necklace.d.ts +27 -0
  75. package/lib/kinds/necklace.js +342 -0
  76. package/lib/kinds/net.d.ts +47 -0
  77. package/lib/kinds/net.js +241 -0
  78. package/lib/kinds/overlay.d.ts +50 -0
  79. package/lib/kinds/overlay.js +519 -0
  80. package/lib/kinds/pieces.d.ts +41 -0
  81. package/lib/kinds/pieces.js +420 -0
  82. package/lib/kinds/pipes.d.ts +46 -0
  83. package/lib/kinds/pipes.js +657 -0
  84. package/lib/kinds/plane-parts.d.ts +105 -0
  85. package/lib/kinds/plane-parts.js +372 -0
  86. package/lib/kinds/plane.d.ts +35 -0
  87. package/lib/kinds/plane.js +755 -0
  88. package/lib/kinds/pyramid.d.ts +26 -0
  89. package/lib/kinds/pyramid.js +228 -0
  90. package/lib/kinds/ropes.d.ts +30 -0
  91. package/lib/kinds/ropes.js +571 -0
  92. package/lib/kinds/shapes.d.ts +59 -0
  93. package/lib/kinds/shapes.js +490 -0
  94. package/lib/kinds/solids.d.ts +78 -0
  95. package/lib/kinds/solids.js +1058 -0
  96. package/lib/kinds/tangram.d.ts +52 -0
  97. package/lib/kinds/tangram.js +713 -0
  98. package/lib/kinds/target.d.ts +39 -0
  99. package/lib/kinds/target.js +761 -0
  100. package/lib/kinds/tiles.d.ts +55 -0
  101. package/lib/kinds/tiles.js +606 -0
  102. package/lib/kinds/venn.d.ts +38 -0
  103. package/lib/kinds/venn.js +710 -0
  104. package/lib/noto.d.ts +2 -0
  105. package/lib/noto.js +212 -0
  106. package/lib/objects.d.ts +7 -0
  107. package/lib/objects.js +226 -0
  108. package/lib/pen.d.ts +32 -0
  109. package/lib/pen.js +60 -0
  110. package/lib/read.d.ts +9 -0
  111. package/lib/read.js +49 -0
  112. package/lib/render.d.ts +24 -0
  113. package/lib/render.js +68 -0
  114. package/lib/solve/balance.d.ts +46 -0
  115. package/lib/solve/balance.js +118 -0
  116. package/lib/style.d.ts +70 -0
  117. package/lib/style.js +39 -0
  118. package/lib/svg.d.ts +22 -0
  119. package/lib/svg.js +48 -0
  120. package/lib/things.d.ts +40 -0
  121. package/lib/things.js +269 -0
  122. package/package.json +28 -4
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,7 @@
1
+ @altnovatrail/picture-kit
2
+
3
+ The package embeds emoji of Noto Emoji, by Google, under the Apache License, Version 2.0,
4
+ the license in LICENSE: https://github.com/googlefonts/noto-emoji. They are SVG files of its
5
+ folder 2D/svg at the tag v2026-09-24-unicode18_0, commit
6
+ e20cbc2bbec1926686be9f9bee7d1d2cfa1fea0e, and the package holds only the emoji of its list of
7
+ objects. The fonts of Noto Emoji are not taken.
package/README.md CHANGED
@@ -1,3 +1,29 @@
1
- # Temporary Holding Version
1
+ # picture-kit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![CI](https://github.com/altnovatrail/picture-kit/actions/workflows/ci.yml/badge.svg)](https://github.com/altnovatrail/picture-kit/actions/workflows/ci.yml)
4
+ [![Release](https://github.com/altnovatrail/picture-kit/actions/workflows/release.yml/badge.svg)](https://github.com/altnovatrail/picture-kit/actions/workflows/release.yml)
5
+ [![CodeQL](https://github.com/altnovatrail/picture-kit/actions/workflows/codeql.yml/badge.svg)](https://github.com/altnovatrail/picture-kit/actions/workflows/codeql.yml)
6
+ [![Latest release](https://img.shields.io/github/v/release/altnovatrail/picture-kit)](https://github.com/altnovatrail/picture-kit/releases/latest)
7
+ [![TypeScript version](https://img.shields.io/github/package-json/dependency-version/altnovatrail/picture-kit/dev/typescript)](package.json)
8
+ [![Coverage](https://codecov.io/gh/altnovatrail/picture-kit/branch/main/graph/badge.svg)](https://codecov.io/gh/altnovatrail/picture-kit)
9
+ [![OpenSSF Best Practices](https://www.bestpractices.dev/projects/17315/badge)](https://www.bestpractices.dev/projects/17315)
10
+ [![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/altnovatrail/picture-kit/badge)](https://scorecard.dev/viewer/?uri=github.com/altnovatrail/picture-kit)
11
+ [![License](https://img.shields.io/github/license/altnovatrail/picture-kit)](LICENSE)
12
+
13
+ [![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=altnovatrail_picture-kit&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=altnovatrail_picture-kit)
14
+ [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=altnovatrail_picture-kit&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=altnovatrail_picture-kit)
15
+ [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=altnovatrail_picture-kit&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=altnovatrail_picture-kit)
16
+ [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=altnovatrail_picture-kit&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=altnovatrail_picture-kit)
17
+ [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=altnovatrail_picture-kit&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=altnovatrail_picture-kit)
18
+
19
+ picture-kit draws the pictures of tasks from scenes. A scene is data, not a drawing: the kind of the picture — balances, groups of things, a grid, cubes, dice, a clock and others of a closed list — and its members, as JSON. The library checks a scene against its kind, draws it as SVG, the same scene always the same bytes, and words it as the picture's text alternative.
20
+
21
+ A model that writes a task writes its scene, and a program draws it, so the picture shows exactly what the task says, and a solver can read the same scene to prove the task's key. The objects in a picture are Noto Emoji's SVGs; the rest the library draws in one style of its own.
22
+
23
+ A component of [NovaTrail](https://github.com/altnovatrail/spec), a universal open platform for adaptive learning, interactive lessons and AI tutoring.
24
+
25
+ It is a library for the pictures of the tasks of `modules`, and later for a module's task generator and for `app`, which may draw a picture on the device; it calls no other component.
26
+
27
+ To see the pictures, open the repository in its devcontainer and run `just gallery`: it draws every example scene and serves them at http://127.0.0.1:5180/. `just ci` runs the checks CI runs.
28
+
29
+ Its card is in the architecture of [spec](https://github.com/altnovatrail/spec); its own documents, its decisions and its plan are in [docs/](docs/README.md).
package/lib/emoji.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /** EmojiSource gives the text of the SVG file of an emoji by its code. */
2
+ export type EmojiSource = (code: string) => string;
3
+ /**
4
+ * noto gives the emoji of the list of objects from the files the package holds, so that it
5
+ * draws with no file system and no network; a code outside the list throws.
6
+ */
7
+ export declare const noto: EmojiSource;
8
+ export declare const emojiBox = 128;
9
+ /** symbolId is the id of an emoji's symbol in a picture. */
10
+ export declare function symbolId(code: string): string;
11
+ /**
12
+ * symbolOf turns an emoji's SVG file into a <symbol> of the picture: the
13
+ * contents of its <svg>, every id prefixed by the emoji's code, and every
14
+ * reference to an id — url(#…), href and xlink:href — prefixed alike.
15
+ */
16
+ export declare function symbolOf(code: string, file: string): string;
package/lib/emoji.js ADDED
@@ -0,0 +1,64 @@
1
+ import { NOTO } from "./noto.js";
2
+ /**
3
+ * noto gives the emoji of the list of objects from the files the package holds, so that it
4
+ * draws with no file system and no network; a code outside the list throws.
5
+ */
6
+ export const noto = (code) => {
7
+ const text = NOTO[code];
8
+ if (text === undefined)
9
+ throw new Error(`emoji ${code}: not an emoji of the list of objects`);
10
+ return text;
11
+ };
12
+ export const emojiBox = 128;
13
+ /** symbolId is the id of an emoji's symbol in a picture. */
14
+ export function symbolId(code) {
15
+ return `e${code}`;
16
+ }
17
+ /**
18
+ * withoutComments drops every XML comment by walking the text, not by a pattern, and walks again
19
+ * until a pass drops nothing: what is left either side of a comment can join into a new one, and
20
+ * a comment left open runs to the end, so nothing that starts one is ever left behind.
21
+ */
22
+ function withoutComments(text) {
23
+ let rest = text;
24
+ for (;;) {
25
+ let out = "";
26
+ let at = 0;
27
+ for (let open = rest.indexOf("<!--"); open >= 0; open = rest.indexOf("<!--", at)) {
28
+ out += rest.slice(at, open);
29
+ const close = rest.indexOf("-->", open + 4);
30
+ at = close < 0 ? rest.length : close + 3;
31
+ }
32
+ out += rest.slice(at);
33
+ if (out === rest)
34
+ return out;
35
+ rest = out;
36
+ }
37
+ }
38
+ /**
39
+ * symbolOf turns an emoji's SVG file into a <symbol> of the picture: the
40
+ * contents of its <svg>, every id prefixed by the emoji's code, and every
41
+ * reference to an id — url(#…), href and xlink:href — prefixed alike.
42
+ */
43
+ export function symbolOf(code, file) {
44
+ const open = file.search(/<svg[\s>]/);
45
+ const close = file.lastIndexOf("</svg>");
46
+ if (open < 0 || close < 0)
47
+ throw new Error(`emoji ${code}: not an SVG file`);
48
+ const start = file.indexOf(">", open) + 1;
49
+ const viewBox = /viewBox="([^"]+)"/.exec(file.slice(open, start))?.[1] ?? "0 0 128 128";
50
+ const prefix = `${symbolId(code)}-`;
51
+ const inner = withoutComments(file.slice(start, close))
52
+ // Illustrator's private data, in namespaces of its own that the symbol
53
+ // does not declare: an element or attribute with such a prefix breaks the
54
+ // whole picture as XML, and a browser then draws none of it.
55
+ .replace(/<([a-zA-Z]+:[a-zA-Z]+)\b[^>]*\/>/g, "")
56
+ .replace(/<([a-zA-Z]+:[a-zA-Z]+)\b[^>]*>[\s\S]*?<\/\1>/g, "")
57
+ .replace(/\s(?!xlink:|xml:)[a-zA-Z]+:[a-zA-Z]+="[^"]*"/g, "")
58
+ .replace(/\sid="([^"]+)"/g, ` id="${prefix}$1"`)
59
+ .replace(/url\(#([^)]+)\)/g, `url(#${prefix}$1)`)
60
+ .replace(/(\s(?:xlink:)?href)="#([^"]+)"/g, `$1="#${prefix}$2"`)
61
+ .replace(/\s+/g, " ")
62
+ .trim();
63
+ return `<symbol id="${symbolId(code)}" viewBox="${viewBox}">${inner}</symbol>`;
64
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { type EmojiSource, noto } from "./emoji.ts";
2
+ export type { Kind } from "./kind.ts";
3
+ export type { BalanceScene, Scale } from "./kinds/balance.ts";
4
+ export { kinds } from "./kinds/index.ts";
5
+ export { type ObjectEntry, objects } from "./objects.ts";
6
+ export { check, describe, type Rendered, render, SceneError } from "./render.ts";
7
+ export { type Answer, type Givens, type Question, solve as solveBalance } from "./solve/balance.ts";
8
+ export { type ColorName, colorNames } from "./style.ts";
9
+ export { type ShapeName, sameThing, shapeNames, type Thing, wordOf } from "./things.ts";
package/lib/index.js ADDED
@@ -0,0 +1,10 @@
1
+ // The package's one entry point. A scene is drawn with render, its problems listed with check,
2
+ // and its words given with describe; noto is the source of the emoji the package holds. What is
3
+ // not exported here is the package's own and may change in any version.
4
+ export { noto } from "./emoji.js";
5
+ export { kinds } from "./kinds/index.js";
6
+ export { objects } from "./objects.js";
7
+ export { check, describe, render, SceneError } from "./render.js";
8
+ export { solve as solveBalance } from "./solve/balance.js";
9
+ export { colorNames } from "./style.js";
10
+ export { sameThing, shapeNames, wordOf } from "./things.js";
package/lib/kind.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { Pen } from "./pen.ts";
2
+ import type { Obj, Problems } from "./read.ts";
3
+ /** Drawn is the size a kind drew a picture at, margins included. */
4
+ export type Drawn = {
5
+ width: number;
6
+ height: number;
7
+ };
8
+ /**
9
+ * Kind is one kind of scene of the closed list: how to check a scene of it,
10
+ * how to draw it, and how to say it in words. check runs first and draw only
11
+ * on a scene it passed, so draw may trust the scene's shape.
12
+ */
13
+ export interface Kind {
14
+ readonly kind: string;
15
+ /** members are every member a scene of the kind may have, kind among them. */
16
+ readonly members: readonly string[];
17
+ check(scene: Obj, problems: Problems): void;
18
+ draw(scene: Obj, pen: Pen): Drawn;
19
+ describe(scene: Obj): string;
20
+ }
21
+ /** The margin every picture keeps around what it draws. */
22
+ export declare const margin = 12;
package/lib/kind.js ADDED
@@ -0,0 +1,2 @@
1
+ /** The margin every picture keeps around what it draws. */
2
+ export const margin = 12;
@@ -0,0 +1,34 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { Pen } from "../pen.ts";
3
+ import { type Thing } from "../things.ts";
4
+ /**
5
+ * A balance: one to four pan balances, side by side, each with the things on
6
+ * its two pans and the side it tips to. The scene may also hold its facts,
7
+ * the weight of every thing, which the picture does not show: then the
8
+ * check proves that every balance tips as the weights say.
9
+ */
10
+ export type Scale = {
11
+ left: Thing[];
12
+ right: Thing[];
13
+ tilt: "left" | "right" | "level";
14
+ label?: string;
15
+ };
16
+ export type BalanceScene = {
17
+ kind: "balance";
18
+ scales: Scale[];
19
+ facts?: {
20
+ weights: {
21
+ thing: Thing;
22
+ weight: number;
23
+ }[];
24
+ };
25
+ };
26
+ /** tiltOf is the side the weights make a balance tip to. */
27
+ export declare function tiltOf(scale: Scale, weight: (thing: Thing) => number): Scale["tilt"];
28
+ /** group runs of the same thing into counts, in order of first place. */
29
+ export declare function groupThings(things: readonly Thing[]): {
30
+ thing: Thing;
31
+ count: number;
32
+ }[];
33
+ export declare const balance: Kind;
34
+ export { Pen };
@@ -0,0 +1,217 @@
1
+ import { margin } from "../kind.js";
2
+ import { Pen } from "../pen.js";
3
+ import { int, isObj, list, oneOf, only, text } from "../read.js";
4
+ import { colors, ink, outline } from "../style.js";
5
+ import { poly } from "../svg.js";
6
+ import { checkThing, count, drawThing, sameThing } from "../things.js";
7
+ const tilts = ["left", "right", "level"];
8
+ const size = 46;
9
+ const perRow = 4;
10
+ const angle = (9 * Math.PI) / 180;
11
+ function weightOf(facts, thing) {
12
+ return facts?.weights.find((one) => sameThing(one.thing, thing))?.weight;
13
+ }
14
+ /** tiltOf is the side the weights make a balance tip to. */
15
+ export function tiltOf(scale, weight) {
16
+ const left = scale.left.reduce((sum, t) => sum + weight(t), 0);
17
+ const right = scale.right.reduce((sum, t) => sum + weight(t), 0);
18
+ return left > right ? "left" : left < right ? "right" : "level";
19
+ }
20
+ /** group runs of the same thing into counts, in order of first place. */
21
+ export function groupThings(things) {
22
+ const out = [];
23
+ for (const thing of things) {
24
+ const seen = out.find((one) => sameThing(one.thing, thing));
25
+ if (seen)
26
+ seen.count++;
27
+ else
28
+ out.push({ thing, count: 1 });
29
+ }
30
+ return out;
31
+ }
32
+ function sayPan(things) {
33
+ return groupThings(things)
34
+ .map((g) => count(g.thing, g.count))
35
+ .join(" and ");
36
+ }
37
+ /** drawScale draws one balance into a pen of its own, at 0, 0. */
38
+ function drawScale(pen, scale) {
39
+ const rows = Math.max(1, ...[scale.left, scale.right].map((pan) => Math.ceil(pan.length / perRow)));
40
+ const across = Math.max(2, ...[scale.left, scale.right].map((pan) => Math.min(perRow, pan.length)));
41
+ const panW = across * size + 26;
42
+ const half = panW / 2 + 22;
43
+ const tip = scale.tilt === "level" ? 0 : scale.tilt === "left" ? 1 : -1;
44
+ const dx = half * Math.cos(angle * Math.abs(tip));
45
+ const dy = half * Math.sin(angle) * Math.abs(tip);
46
+ const hang = rows * size + 30;
47
+ const depth = 16;
48
+ // Pivot at x = cx, y = py; the higher beam end is py - dy.
49
+ const cx = dx + panW / 2 + margin;
50
+ const top = margin + 6;
51
+ const py = top + dy + 8;
52
+ const ends = [
53
+ { x: cx - dx, y: py + dy * tip, things: scale.left },
54
+ { x: cx + dx, y: py - dy * tip, things: scale.right },
55
+ ];
56
+ const lowest = Math.max(...ends.map((e) => e.y + hang + depth));
57
+ const baseY = lowest + 22;
58
+ // stand
59
+ pen.el("path", {
60
+ d: poly([
61
+ { x: cx - 7, y: py },
62
+ { x: cx + 7, y: py },
63
+ { x: cx + 9, y: baseY - 10 },
64
+ { x: cx - 9, y: baseY - 10 },
65
+ ]),
66
+ fill: colors.brown.fill,
67
+ stroke: ink,
68
+ "stroke-width": outline,
69
+ "stroke-linejoin": "round",
70
+ });
71
+ pen.el("rect", {
72
+ x: cx - 52,
73
+ y: baseY - 12,
74
+ width: 104,
75
+ height: 14,
76
+ rx: 7,
77
+ fill: colors.brown.fill,
78
+ stroke: ink,
79
+ "stroke-width": outline,
80
+ });
81
+ // beam
82
+ const ux = (ends[1].x - ends[0].x) / (2 * half);
83
+ const uy = (ends[1].y - ends[0].y) / (2 * half);
84
+ const nx = -uy * 5;
85
+ const ny = ux * 5;
86
+ pen.el("path", {
87
+ d: poly([
88
+ { x: ends[0].x + nx, y: ends[0].y + ny },
89
+ { x: ends[1].x + nx, y: ends[1].y + ny },
90
+ { x: ends[1].x - nx, y: ends[1].y - ny },
91
+ { x: ends[0].x - nx, y: ends[0].y - ny },
92
+ ]),
93
+ fill: colors.brown.dark,
94
+ stroke: ink,
95
+ "stroke-width": outline,
96
+ "stroke-linejoin": "round",
97
+ });
98
+ pen.el("circle", {
99
+ cx,
100
+ cy: py,
101
+ r: 7,
102
+ fill: colors.yellow.fill,
103
+ stroke: ink,
104
+ "stroke-width": outline,
105
+ });
106
+ for (const end of ends) {
107
+ const rim = end.y + hang;
108
+ const l = end.x - panW / 2;
109
+ const r = end.x + panW / 2;
110
+ pen.el("path", {
111
+ d: `M${l} ${rim}L${end.x} ${end.y}L${r} ${rim}`,
112
+ fill: "none",
113
+ stroke: ink,
114
+ "stroke-width": 1.6,
115
+ });
116
+ pen.el("circle", { cx: end.x, cy: end.y, r: 4, fill: ink });
117
+ end.things.forEach((thing, i) => {
118
+ const row = Math.floor(i / perRow);
119
+ const inRow = Math.min(perRow, end.things.length - row * perRow);
120
+ const col = i % perRow;
121
+ const x = end.x + (col - (inRow - 1) / 2) * size;
122
+ const y = rim - size / 2 - row * (size - 4) - 1;
123
+ drawThing(pen, thing, x, y, size - 4);
124
+ });
125
+ pen.el("path", {
126
+ d: `M${l - 4} ${rim}H${r + 4}Q${r - 6} ${rim + depth * 1.6} ${end.x} ${rim + depth * 1.6}Q${l + 6} ${rim + depth * 1.6} ${l - 4} ${rim}Z`,
127
+ fill: colors.yellow.fill,
128
+ stroke: ink,
129
+ "stroke-width": outline,
130
+ "stroke-linejoin": "round",
131
+ });
132
+ }
133
+ let height = baseY + 2 + margin;
134
+ if (scale.label) {
135
+ pen.text(cx, baseY + 22, scale.label, { size: 18 });
136
+ height += 26;
137
+ }
138
+ return { width: cx + dx + panW / 2 + margin, height };
139
+ }
140
+ export const balance = {
141
+ kind: "balance",
142
+ members: ["kind", "scales", "facts"],
143
+ check(scene, problems) {
144
+ only(scene, this.members, "scene", problems);
145
+ const scales = list(scene.scales, "scales", problems, 1, 4);
146
+ scales.forEach((s, i) => {
147
+ const path = `scales[${i}]`;
148
+ if (!isObj(s)) {
149
+ problems.push(`${path}: must be a balance`);
150
+ return;
151
+ }
152
+ only(s, ["left", "right", "tilt", "label"], path, problems);
153
+ for (const side of ["left", "right"]) {
154
+ list(s[side], `${path}.${side}`, problems, 1, 8).forEach((t, j) => {
155
+ checkThing(t, `${path}.${side}[${j}]`, problems);
156
+ });
157
+ }
158
+ oneOf(s.tilt, tilts, `${path}.tilt`, problems);
159
+ if (s.label !== undefined)
160
+ text(s.label, `${path}.label`, problems, 3);
161
+ });
162
+ if (scene.facts === undefined || problems.length > 0)
163
+ return;
164
+ if (!isObj(scene.facts)) {
165
+ problems.push("facts: must hold weights");
166
+ return;
167
+ }
168
+ only(scene.facts, ["weights"], "facts", problems);
169
+ const weights = list(scene.facts.weights, "facts.weights", problems, 1, 12);
170
+ weights.forEach((w, i) => {
171
+ if (!isObj(w)) {
172
+ problems.push(`facts.weights[${i}]: must be a thing and a weight`);
173
+ return;
174
+ }
175
+ checkThing(w.thing, `facts.weights[${i}].thing`, problems);
176
+ int(w.weight, `facts.weights[${i}].weight`, problems, 1, 10000);
177
+ });
178
+ if (problems.length > 0)
179
+ return;
180
+ const typed = scene;
181
+ typed.scales.forEach((s, i) => {
182
+ for (const t of [...s.left, ...s.right]) {
183
+ if (weightOf(typed.facts, t) === undefined)
184
+ problems.push(`facts.weights: no weight for ${count(t, 1)} of scales[${i}]`);
185
+ }
186
+ if (problems.length > 0)
187
+ return;
188
+ const should = tiltOf(s, (t) => weightOf(typed.facts, t) ?? 0);
189
+ if (should !== s.tilt)
190
+ problems.push(`scales[${i}].tilt: the weights make it "${should}", not "${s.tilt}"`);
191
+ });
192
+ },
193
+ draw(scene, pen) {
194
+ const typed = scene;
195
+ const drawn = typed.scales.map((s) => {
196
+ const sub = pen.sub();
197
+ return { sub, ...drawScale(sub, s) };
198
+ });
199
+ const height = Math.max(...drawn.map((d) => d.height));
200
+ let x = 0;
201
+ for (const d of drawn) {
202
+ pen.place(d.sub, x, height - d.height);
203
+ x += d.width + 8;
204
+ }
205
+ return { width: x - 8, height };
206
+ },
207
+ describe(scene) {
208
+ const typed = scene;
209
+ const one = (s) => `on the left pan ${sayPan(s.left)}, on the right pan ${sayPan(s.right)}; ${s.tilt === "level" ? "the pans are level" : `the ${s.tilt} pan is lower`}`;
210
+ if (typed.scales.length === 1)
211
+ return `A balance: ${one(typed.scales[0])}.`;
212
+ return `${typed.scales.length} balances. ${typed.scales
213
+ .map((s, i) => `${s.label ? `Balance ${s.label}` : `Balance ${i + 1}`}: ${one(s)}.`)
214
+ .join(" ")}`;
215
+ },
216
+ };
217
+ export { Pen };
@@ -0,0 +1,33 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type Thing } from "../things.ts";
4
+ /**
5
+ * Branches: a tree — a decision tree, a family tree, a sorting tree or a
6
+ * knockout bracket. Each node is a box with a label, a thing (with a label
7
+ * under it or not), or "?" where the question asks; a line joins it to each
8
+ * of its children, and a label may stand on that line, such as "yes" and
9
+ * "no". The tree grows from its root down, up, right or left; its lines are
10
+ * straight, or elbows as in a bracket. In a bracket every node that is not a
11
+ * player is the winner of the match between its two children, so it shows
12
+ * one of them, or "?".
13
+ */
14
+ export type TreeNode = {
15
+ label?: string;
16
+ thing?: Thing;
17
+ color?: ColorName;
18
+ ask?: true;
19
+ branch?: string;
20
+ children?: TreeNode[];
21
+ };
22
+ export declare const grows: readonly ["down", "up", "right", "left"];
23
+ export type Grow = (typeof grows)[number];
24
+ export declare const lineStyles: readonly ["straight", "elbow"];
25
+ export type LineStyle = (typeof lineStyles)[number];
26
+ export type BranchesScene = {
27
+ kind: "branches";
28
+ root: TreeNode;
29
+ grow?: Grow;
30
+ lines?: LineStyle;
31
+ bracket?: boolean;
32
+ };
33
+ export declare const branches: Kind;