tikzify 0.0.0

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.
package/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # React + TypeScript + Vite
2
+
3
+ This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
4
+
5
+ Currently, two official plugins are available:
6
+
7
+ - [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) (or [oxc](https://oxc.rs) when used in [rolldown-vite](https://vite.dev/guide/rolldown)) for Fast Refresh
8
+ - [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9
+
10
+ ## React Compiler
11
+
12
+ The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation).
13
+
14
+ ## Expanding the ESLint configuration
15
+
16
+ If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:
17
+
18
+ ```js
19
+ export default defineConfig([
20
+ globalIgnores(['dist']),
21
+ {
22
+ files: ['**/*.{ts,tsx}'],
23
+ extends: [
24
+ // Other configs...
25
+
26
+ // Remove tseslint.configs.recommended and replace with this
27
+ tseslint.configs.recommendedTypeChecked,
28
+ // Alternatively, use this for stricter rules
29
+ tseslint.configs.strictTypeChecked,
30
+ // Optionally, add this for stylistic rules
31
+ tseslint.configs.stylisticTypeChecked,
32
+
33
+ // Other configs...
34
+ ],
35
+ languageOptions: {
36
+ parserOptions: {
37
+ project: ['./tsconfig.node.json', './tsconfig.app.json'],
38
+ tsconfigRootDir: import.meta.dirname,
39
+ },
40
+ // other options...
41
+ },
42
+ },
43
+ ])
44
+ ```
45
+
46
+ You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
47
+
48
+ ```js
49
+ // eslint.config.js
50
+ import reactX from 'eslint-plugin-react-x'
51
+ import reactDom from 'eslint-plugin-react-dom'
52
+
53
+ export default defineConfig([
54
+ globalIgnores(['dist']),
55
+ {
56
+ files: ['**/*.{ts,tsx}'],
57
+ extends: [
58
+ // Other configs...
59
+ // Enable lint rules for React
60
+ reactX.configs['recommended-typescript'],
61
+ // Enable lint rules for React DOM
62
+ reactDom.configs.recommended,
63
+ ],
64
+ languageOptions: {
65
+ parserOptions: {
66
+ project: ['./tsconfig.node.json', './tsconfig.app.json'],
67
+ tsconfigRootDir: import.meta.dirname,
68
+ },
69
+ // other options...
70
+ },
71
+ },
72
+ ])
73
+ ```
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "tikzify",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "module": "dist/tikzsvg.es.js",
6
+ "types": "dist/tikzsvg.d.ts",
7
+ "files": [
8
+ "src"
9
+ ],
10
+ "scripts": {},
11
+ "devDependencies": {
12
+ "@types/bun": "^1.3.6",
13
+ "@types/node": "^22.10.0",
14
+ "@types/sax": "^1.2.7",
15
+ "bun": "^1.3.6"
16
+ },
17
+ "dependencies": {
18
+ "assert": "^2.1.0",
19
+ "p-queue": "^9.1.0",
20
+ "sax": "^1.4.4",
21
+ "zod": "^4.3.5"
22
+ }
23
+ }
package/src/book.ts ADDED
@@ -0,0 +1,313 @@
1
+ import assert from 'assert'
2
+ import sax from 'sax'
3
+ import z from 'zod'
4
+ import { emojiMap } from './emojis'
5
+
6
+ type Path = z.infer<typeof Path>
7
+ const Path = z.object({
8
+ type: z.literal("path"),
9
+ d: z.string(),
10
+ fill: z.string().optional(),
11
+ })
12
+
13
+ type Circle = z.infer<typeof Circle>
14
+ const Circle = z.object({
15
+ type: z.literal("circle"),
16
+ cx: z.number(),
17
+ cy: z.number(),
18
+ r: z.number(),
19
+ fill: z.string().optional(),
20
+ })
21
+
22
+ type Group = z.infer<typeof Group>
23
+ const Group = z.object({
24
+ type: z.literal("g"),
25
+ fill: z.string().optional(),
26
+ get kids() {
27
+ return z.array(Element)
28
+ }
29
+ })
30
+
31
+
32
+
33
+ type types = Element["type"]
34
+ type Element = z.infer<typeof Element>
35
+ const Element = z.union([Group, Path, Circle])
36
+
37
+
38
+ type Emoji = z.infer<typeof Emoji>
39
+ const Emoji = z.object({
40
+ x: z.number(),
41
+ y: z.number(),
42
+ scale: z.number(),
43
+ rotate: z.number(),
44
+ emoji: z.string()
45
+ })
46
+
47
+ type Open = {
48
+ [t in types]: (args: Omit<Extract<Element, { type: t }>, "type">) => void
49
+ }
50
+
51
+ type Close = {
52
+ [t in types]: () => void
53
+ }
54
+
55
+ function hex(s: string | undefined): string | undefined {
56
+ if (!s) return
57
+ const res = s.replace('#', '')
58
+ assert([3, 6].includes(res.length), `Color ${s} is not valid hex`)
59
+ return res.length === 3 ? res.split('').map(c => c + c).join('') : res
60
+ }
61
+
62
+ function fromSvg(svg: string): Group[] {
63
+
64
+ const res: Group[] = [{
65
+ type: "g",
66
+ kids: [],
67
+ }]
68
+
69
+ function push(e: Element) {
70
+ res[res.length - 1]?.kids.push({ ...e, fill: hex(e.fill) })
71
+ }
72
+
73
+ const open: Open = {
74
+ circle(attrs) {
75
+ push({ type: "circle", ...attrs })
76
+ },
77
+ path(attrs) {
78
+ push({ type: "path", ...attrs })
79
+ },
80
+ g({ fill }) {
81
+ res.push({
82
+ type: "g",
83
+ fill: hex(fill),
84
+ kids: [],
85
+ })
86
+ },
87
+ }
88
+
89
+ const close: Close = {
90
+ circle() { },
91
+ path() { },
92
+ g() {
93
+ res.push({
94
+ type: "g",
95
+ kids: [],
96
+ })
97
+ },
98
+ }
99
+
100
+ const p = sax.parser(true)
101
+
102
+ p.onopentag = ({ name, attributes }) => {
103
+ if (!(name in open)) {
104
+ console.log('unknown tag', name)
105
+ return
106
+ }
107
+ open[name as types](attributes as any)
108
+ }
109
+
110
+ p.onclosetag = (name) => {
111
+ if (!(name in close)) {
112
+ console.log('unknown tag', name)
113
+ return
114
+ }
115
+ close[name as types]()
116
+ }
117
+
118
+ p.write(svg).close()
119
+
120
+
121
+
122
+ return res
123
+ }
124
+
125
+
126
+ type Page = z.infer<typeof Page>
127
+ const Page = z.object({
128
+ gradient: z.array(z.string()).length(2),
129
+ textBg: z.string(),
130
+ text: z.array(z.string()),
131
+ emojis: z.object({
132
+ text: z.array(Emoji),
133
+ image: z.array(Emoji)
134
+ }),
135
+ jpgBase64: z.string().max(256_000)
136
+ })
137
+
138
+ export type Book = z.infer<typeof Book>
139
+ export const Book = z.object({
140
+ dir: z.enum(['rtl', 'ltr']),
141
+ pages: z.array(Page),
142
+ })
143
+
144
+ function colorMap(colors: Set<string | undefined>) {
145
+ colors.delete(undefined)
146
+ return Object.fromEntries(Array.from(colors).filter((c): c is string => c !== undefined).map((c, i) => [c, i]))
147
+ }
148
+
149
+ function getColors(e: Element): (string | undefined)[] {
150
+ if (e.type === 'g') return [e.fill, ...e.kids.flatMap(getColors)]
151
+ return [e.fill]
152
+ }
153
+
154
+ export function toTex(book: Book) {
155
+ const pages = book.pages
156
+
157
+ const emojis = Object.fromEntries(
158
+ pages.flatMap(p => [...p.emojis.text, ...p.emojis.image].map(e => {
159
+ const svg = emojiMap[e.emoji]
160
+ return [e.emoji, {
161
+ ...e,
162
+ emoji: fromSvg(svg!)
163
+ }]
164
+ }
165
+ )))
166
+
167
+ const gradColors = pages.flatMap(p => p.gradient)
168
+ const textBgColors = pages.map(p => p.textBg)
169
+ const emojiColors = Object.values(emojis).flatMap(es => es.emoji.flatMap(getColors))
170
+ const colors = colorMap(new Set([
171
+ ...gradColors,
172
+ ...textBgColors,
173
+ ...emojiColors
174
+ ]))
175
+
176
+ type To = {
177
+ [t in types]: (args: Extract<Element, { type: t }>) => string
178
+ }
179
+
180
+ function fillStr(fill?: string) {
181
+ if (!fill) return ''
182
+ assert(fill in colors, `Color ${fill} not in ${Object.keys(colors)}`)
183
+ return `fill=c${colors[fill]}`
184
+ }
185
+
186
+ const ToTikz: To = {
187
+ circle({ cx, cy, r, fill }) {
188
+ return `\\fill[${fillStr(fill)}] (${cx}, ${cy}) circle (${r});`
189
+ },
190
+
191
+ path({ d, fill }) {
192
+ return `\\fill[${fillStr(fill)}] svg {${d}};`
193
+ },
194
+
195
+ g({ fill, kids }) {
196
+ return [
197
+ `\\begin{scope}[${fillStr(fill)}]`,
198
+ ...kids.map(e => ToTikz[e.type](e as any)),
199
+ `\\end{scope}`
200
+ ].join('\n')
201
+ }
202
+ }
203
+
204
+ return String.raw`
205
+ \documentclass[a5paper, oneside]{article}
206
+ \usepackage[margin=0cm,bottom=2cm]{geometry}
207
+ \usepackage{tikz}
208
+ \usetikzlibrary{svg.path}
209
+
210
+ \usepackage{fancyhdr}
211
+
212
+ \fancypagestyle{bigpagenumbers}{
213
+ \fancyhf{}
214
+ \renewcommand{\headrulewidth}{0pt}
215
+ \fancyfoot[C]{\Huge\thepage}
216
+ }
217
+
218
+ \usepackage{polyglossia}
219
+ \setmainlanguage{hebrew}
220
+ \newfontfamily\hebrewfont[
221
+ Script=Hebrew,
222
+ Path=./,
223
+ BoldFont={Fredoka-Bold.ttf}
224
+ ]{Fredoka-Bold.ttf}
225
+
226
+
227
+ ${Object.entries(colors).map(([color, i]) => `\\definecolor{c${i}}{HTML}{${color.replace('#', '')}}`).join('\n')}
228
+
229
+ \begin{document}
230
+
231
+ \pagestyle{bigpagenumbers}
232
+
233
+ \mbox{}
234
+ \newpage
235
+
236
+ ${book.pages.map((page, i) => {
237
+ const [c1, c2] = page.gradient
238
+
239
+ assert(c1 && c2, `Gradient colors ${page.gradient} must be defined in page ${i}`)
240
+
241
+ function es(es: Emoji[]) {
242
+ return es.map(({ emoji, x, y, scale, rotate }) => {
243
+ assert(emoji && emoji in emojis, `Emoji ${emoji} not found`)
244
+
245
+ return String.raw`
246
+ \begin{scope}[x=1pt, y=1pt, xshift=${x}, scale=${scale}, yscale=-1, yshift=${y}, rotate=${rotate}]
247
+ ${emojis[emoji]?.emoji.map(e => {
248
+ return ToTikz[e.type](e as any)
249
+ }).join('\n')}
250
+ \end{scope}`}).join('\n')
251
+ }
252
+
253
+ const esText = es(page.emojis.text)
254
+ const esImage = es(page.emojis.image)
255
+ const rtl = book.dir === 'rtl'
256
+
257
+ const image = String.raw`
258
+ \begin{tikzpicture}[remember picture, overlay]
259
+ \shade[shading=axis, bottom color=c${colors[c1]}, top color=c${colors[c2]}, shading angle=45]
260
+ ([xshift=-148.5mm]current page.south west) rectangle (current page.north east);
261
+ \end{tikzpicture}
262
+
263
+ \vspace*{\fill}
264
+ \begin{tikzpicture}
265
+ \begin{scope}
266
+ \clip[
267
+ xshift=-190,
268
+ yshift=190,
269
+ scale=380,
270
+ yscale=-1] svg {M 0.97 0.37 C 0.95 0.26 0.94 0.11 0.85 0.05 C 0.76 0.00 0.54 0.02 0.41 0.05 C 0.28 0.08 0.12 0.10 0.06 0.24 C 0.00 0.37 0.00 0.73 0.05 0.85 C 0.11 0.97 0.26 0.94 0.39 0.96 C 0.51 0.98 0.71 1.00 0.80 0.96 C 0.90 0.92 0.94 0.82 0.97 0.72 C 1.00 0.62 0.99 0.48 0.97 0.37 C 0.95 0.26 0.94 0.11 0.85 0.05};
271
+ \node[opacity=0.8] {\includegraphics[width=13.1cm]{${i}.jpg}};
272
+ \end{scope}
273
+ ${esImage}
274
+ \end{tikzpicture}
275
+ \vspace*{\fill}
276
+ \newpage
277
+ `
278
+
279
+ const text = String.raw`
280
+ \begin{tikzpicture}[remember picture, overlay]
281
+ \shade[shading=axis, bottom color=c${colors[c1]}, top color=c${colors[c2]}, shading angle=45]
282
+ (current page.south west) rectangle ([xshift=148.5mm]current page.north east);
283
+ \fill[
284
+ opacity=0.5,
285
+ color=c${colors[page.textBg]},
286
+ yshift=-20,
287
+ xscale=380,
288
+ yscale=-500,
289
+ ] svg {M 0.97 0.37 C 0.95 0.26 0.94 0.11 0.85 0.05 C 0.76 0.00 0.54 0.02 0.41 0.05 C 0.28 0.08 0.12 0.10 0.06 0.24 C 0.00 0.37 0.00 0.73 0.05 0.85 C 0.11 0.97 0.26 0.94 0.39 0.96 C 0.51 0.98 0.71 1.00 0.80 0.96 C 0.90 0.92 0.94 0.82 0.97 0.72 C 1.00 0.62 0.99 0.48 0.97 0.37 C 0.95 0.26 0.94 0.11 0.85 0.05};
290
+
291
+ ${esText}
292
+ \end{tikzpicture}
293
+
294
+ \vspace*{\fill}
295
+ \begin{center}
296
+ \begin{minipage}{10cm}
297
+ \Huge
298
+ \raggedleft
299
+ ${page.text.map(line => line.trim()).join('\\\\')}
300
+ \end{minipage}
301
+ \end{center}
302
+ \vspace*{\fill}
303
+ \newpage
304
+ `
305
+ return [
306
+ rtl ? image : text,
307
+ rtl ? text : image,
308
+ ].join('\n\n')
309
+ })}
310
+
311
+ \end{document}`
312
+ }
313
+