tikzify 0.0.10 → 0.0.12

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/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "tikzify",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "type": "module",
5
- "module": "dist/tikzsvg.es.js",
6
- "types": "dist/tikzsvg.d.ts",
5
+ "exports": "./src/index.ts",
7
6
  "files": [
8
7
  "src"
9
8
  ],
package/src/book.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import assert from 'assert'
2
2
  import z from 'zod'
3
- import { colorMap, defineColors, Emoji, fromSvg, getColors, gradient, svgTex } from './common'
3
+ import { colorMap, defineColors, Emoji, fromSvg, getColors, gradient, svgTex, type Transform } from './common'
4
4
  import { emojiMap } from './emojis'
5
5
 
6
6
 
@@ -10,8 +10,8 @@ const Page = z.object({
10
10
  textBg: z.string(),
11
11
  text: z.array(z.string()),
12
12
  emojis: z.object({
13
- text: z.array(Emoji),
14
- image: z.array(Emoji)
13
+ text: z.array(Emoji).length(3),
14
+ image: z.array(Emoji).length(3),
15
15
  }),
16
16
  jpgBase64: z.string().max(256_000)
17
17
  })
@@ -22,22 +22,30 @@ export const Book = z.object({
22
22
  pages: z.array(Page),
23
23
  })
24
24
 
25
+ const TRANSFORMS_TEXT: Transform[] = [
26
+ { x: 10, y: 15, scale: 2.3, rotate: -15, },
27
+ { x: 300, y: 220, scale: 2.1, rotate: 15, },
28
+ { x: 10, y: 280, scale: 1.7, rotate: -10, },
29
+ ]
30
+
31
+ const TRANSFORMS_IMAGE: Transform[] = [
32
+ { x: -170, y: -120, scale: 1.8, rotate: -15 },
33
+ { x: 130, y: -120, scale: 2.2, rotate: 15 },
34
+ { x: 100, y: 80, scale: 2.3, rotate: 10 }
35
+ ]
36
+
25
37
  export function bookTex(book: Book) {
26
38
  const pages = book.pages
27
39
 
28
- const emojis = Object.fromEntries(
29
- pages.flatMap(p => [...p.emojis.text, ...p.emojis.image].map(e => {
30
- const svg = emojiMap[e.emoji]
31
- return [e.emoji, {
32
- ...e,
33
- emoji: fromSvg(svg!)
34
- }]
35
- }
36
- )))
40
+ const emojiElements = Object.fromEntries(
41
+ pages.flatMap(p => [...p.emojis.text, ...p.emojis.image].map(emoji => {
42
+ const svg = emojiMap[emoji]
43
+ return [emoji, fromSvg(svg!)]
44
+ })))
37
45
 
38
46
  const gradColors = pages.flatMap(p => p.gradient)
39
47
  const textBgColors = pages.map(p => p.textBg)
40
- const emojiColors = Object.values(emojis).flatMap(es => es.emoji.flatMap(getColors))
48
+ const emojiColors = Object.values(emojiElements).flatMap(es => es.flatMap(getColors))
41
49
  const colors = colorMap(new Set([
42
50
  ...gradColors,
43
51
  ...textBgColors,
@@ -81,15 +89,16 @@ ${book.pages.map((page, i) => {
81
89
 
82
90
  assert(c1 && c2, `Gradient colors ${page.gradient} must be defined in page ${i}`)
83
91
 
84
- function es(es: Emoji[]) {
85
- return es.map(({ emoji, x, y, scale, rotate }) => {
86
- assert(emoji && emoji in emojis, `Emoji ${emoji} not found`)
87
- return svgTex({ x, y, scale, rotate }, emojis[emoji]!.emoji, colors)
92
+ function emojisTex(emojis: string[], transforms: Transform[]) {
93
+ assert(emojis.length <= transforms.length)
94
+ return emojis.map((emoji, i) => {
95
+ const els = emojiElements[emoji]!
96
+ return svgTex(transforms[i]!, els, colors)
88
97
  }).join('\n')
89
98
  }
90
99
 
91
- const esText = es(page.emojis.text)
92
- const esImage = es(page.emojis.image)
100
+ const esText = emojisTex(page.emojis.text, TRANSFORMS_TEXT)
101
+ const esImage = emojisTex(page.emojis.image, TRANSFORMS_IMAGE)
93
102
  const rtl = book.dir === 'rtl'
94
103
 
95
104
  const image = String.raw`
package/src/common.ts CHANGED
@@ -4,18 +4,7 @@ import { z } from 'zod'
4
4
 
5
5
  export const gradient = z.array(z.string()).length(2)
6
6
 
7
- export type Transform = z.infer<typeof Transform>
8
- export const Transform = z.object({
9
- x: z.number(),
10
- y: z.number(),
11
- scale: z.number(),
12
- rotate: z.number(),
13
- })
14
-
15
- export type Emoji = z.infer<typeof Emoji>
16
- export const Emoji = Transform.extend({
17
- emoji: z.string().max(8)
18
- })
7
+ export const Emoji = z.string().max(8)
19
8
 
20
9
  type Path = z.infer<typeof Path>
21
10
  const Path = z.object({
@@ -164,6 +153,13 @@ export function defineColors(colors: Record<string, number>) {
164
153
  return Object.entries(colors).map(([color, i]) => `\\definecolor{c${i}}{HTML}{${color.replace('#', '')}}`).join('\n')
165
154
  }
166
155
 
156
+ export interface Transform {
157
+ x: number
158
+ y: number
159
+ scale: number
160
+ rotate: number
161
+ }
162
+
167
163
  export function svgTex({ x, y, scale, rotate }: Transform, es: Element[], colors: Record<string, number>) {
168
164
  return String.raw`
169
165
  \begin{scope}[x=1pt, y=1pt, xshift=${x}, scale=${scale}, yscale=-1, yshift=${y}, rotate=${rotate}]
package/src/cover.ts CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  import assert from "assert"
3
3
  import { z } from "zod"
4
- import { colorMap, defineColors, Emoji, fromSvg, getColors, svgTex } from "./common"
4
+ import { colorMap, defineColors, Emoji, fromSvg, getColors, svgTex, type Transform } from "./common"
5
5
  import { emojiMap } from "./emojis"
6
6
 
7
7
 
@@ -19,6 +19,7 @@ export const Cover = z.object({
19
19
  jpgBase64: z.string().max(256_000),
20
20
  })
21
21
 
22
+ const TRANSFORM: Transform = { x: -220, y: 60, scale: 2.3, rotate: -15 }
22
23
  export function coverTex({
23
24
  gradient,
24
25
  emoji,
@@ -33,7 +34,7 @@ export function coverTex({
33
34
 
34
35
  const [c1, c2] = gradient
35
36
  assert(c1 && c2, "Gradient must have two colors")
36
- const el = fromSvg(emojiMap[emoji.emoji]!)
37
+ const el = fromSvg(emojiMap[emoji]!)
37
38
  const colors = colorMap(new Set([...gradient, ...el.flatMap(getColors)]))
38
39
 
39
40
  return String.raw`
@@ -153,7 +154,7 @@ xshift=-170,
153
154
  scale=340] 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";
154
155
  \node[opacity=.75] at (0,0) {\includegraphics[width=12cm]{cover.jpg}};
155
156
  \end{scope}
156
- ${svgTex(emoji, el, colors)}
157
+ ${svgTex(TRANSFORM, el, colors)}
157
158
  \end{tikzpicture}
158
159
  \end{minipage}
159
160
  \end{minipage}
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { Book } from "./book"
2
+ export { Cover } from "./cover"
3
+ export { emojiMap } from "./emojis"
4
+