sima.gno
32.70 Kb · 914 lines
1// Package sima is a Sinopia engine: an on-chain PFP in the language of a
2// risograph print. Every token is a bust printed in three drum inks (two
3// colours and a skin ink) plus a key ink for the line work; each plate is
4// slightly misregistered, shadows are halftone dots and the signature mark is
5// a nose drawn as one broken line. Rare tokens are print accidents.
6//
7// Overprint is emulated with explicit colour layers: every overlap of two or
8// three plates is drawn again, clipped to the overlap, in the product of the
9// inks (what multiply blending would show). The output never uses
10// mix-blend-mode, so wallets that ignore blending still show the right
11// colours.
12//
13// It is a port of the Sima prototype in the engine gallery (JavaScript) with
14// curated ink sets, named expressions, one accessory slot, emulated overprint
15// and integer maths. frontend/src/engines/sima.ts is a byte-identical
16// TypeScript twin; sima_test.gno and sima.test.ts pin the same golden vectors.
17//
18// Pure integer maths: no floats, no time, no block data.
19package sima
20
21import (
22 "errors"
23 "strconv"
24 "strings"
25
26 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
27)
28
29// TraitTypes in the order Traits returns them.
30var traitTypes = []string{"Inks", "Backdrop", "Head", "Hair", "Expression", "Outfit", "Accessory", "Print"}
31
32// MaxSVGBytes: the largest output over 10,000 seeds is 5,243 B; more
33// than 2x margin.
34const MaxSVGBytes = 12288
35
36// ParamsSchema is the Studio form. Studio sends JSON.stringify of the values
37// in this property order; ValidateParams accepts exactly that or "".
38const ParamsSchema = `{"type":"object","properties":{` +
39 `"inks":{"type":"string","title":"Ink set","enum":["Seeded","Pink & Blue","Tangerine & Teal",` +
40 `"Red & Federal Blue","Green & Violet","Sunflower & Blue","Aqua & Burgundy","Pink & Green","Orange & Violet",` +
41 `"Kraft Press","Blue on Blush","Blueprint","Sinopia Earth"],"default":"Seeded"}}}`
42
43// DefaultParams is the canonical form of the defaults; it renders exactly
44// like "".
45const DefaultParams = `{"inks":"Seeded"}`
46
47// Manifest describes the engine. Author and FeeBps are set by the adapter.
48func Manifest() engine.Manifest {
49 return engine.Manifest{
50 Name: "Sima",
51 Version: "1.0.0",
52 Description: "On-chain portraits printed like a risograph: three misregistered drum inks that overprint, " +
53 "halftone shadows and a one-line nose. Layered traits; the rare ones are print accidents.",
54 License: "CC-BY-NC-4.0",
55 ParamsSchema: ParamsSchema,
56 TraitTypes: append([]string{}, traitTypes...),
57 Living: false,
58 MaxSVGBytes: MaxSVGBytes,
59 Kind: engine.KindSVG,
60 }
61}
62
63// Config is a parsed params string.
64type Config struct {
65 Inks int // -1: drawn from the seed, else an index into the ink sets
66}
67
68// Defaults is the showcase look (params "").
69func Defaults() Config { return Config{Inks: -1} }
70
71var errParams = errors.New(`sima: params must be "" or {"inks":<enum>} without spaces`)
72
73// ParseParams accepts "" or the canonical JSON Studio produces, nothing else.
74func ParseParams(params string) (Config, error) {
75 cfg := Defaults()
76 if params == "" {
77 return cfg, nil
78 }
79 const pre, post = `{"inks":"`, `"}`
80 if !strings.HasPrefix(params, pre) || !strings.HasSuffix(params, post) || len(params) < len(pre)+len(post) {
81 return cfg, errParams
82 }
83 v := params[len(pre) : len(params)-len(post)]
84 if strings.IndexByte(v, '"') >= 0 {
85 return cfg, errParams
86 }
87 if v == "Seeded" {
88 return cfg, nil
89 }
90 for i, s := range inkSets {
91 if s.name == v {
92 cfg.Inks = i
93 return cfg, nil
94 }
95 }
96 return cfg, errParams
97}
98
99// ValidateParams accepts "" (the showcase defaults) and the canonical JSON.
100func ValidateParams(params string) error {
101 _, err := ParseParams(params)
102 return err
103}
104
105func mustConfig(params string) Config {
106 cfg, err := ParseParams(params)
107 if err != nil {
108 panic(err)
109 }
110 return cfg
111}
112
113// Traits returns the eight traits of in.Seed in TraitTypes order. It never
114// renders: eight table draws.
115func Traits(in engine.Input) []engine.Trait {
116 t := TraitsOf(in.Seed, mustConfig(in.Params))
117 return []engine.Trait{
118 {Type: "Inks", Value: t.Inks},
119 {Type: "Backdrop", Value: t.Backdrop},
120 {Type: "Head", Value: t.Head},
121 {Type: "Hair", Value: t.Hair},
122 {Type: "Expression", Value: t.Expression},
123 {Type: "Outfit", Value: t.Outfit},
124 {Type: "Accessory", Value: t.Accessory},
125 {Type: "Print", Value: t.Print},
126 }
127}
128
129// Render returns the raw SVG for in.Seed and in.Params.
130func Render(in engine.Input) string { return SVG(in.Seed, mustConfig(in.Params)) }
131
132// ---------------------------------------------------------------------------
133// Deterministic randomness: FNV-1a over the seed, then splitmix64 (the stream
134// of every Sinopia engine). Traits use seed+"|traits", the art seed+"|art".
135// ---------------------------------------------------------------------------
136
137type rng struct{ state uint64 }
138
139func newRNG(seed string) *rng {
140 h := uint64(14695981039346656037)
141 for i := 0; i < len(seed); i++ {
142 h ^= uint64(seed[i])
143 h *= 1099511628211
144 }
145 if h == 0 {
146 h = 1099511628211
147 }
148 return &rng{state: h}
149}
150
151func (r *rng) next() uint64 {
152 r.state += 0x9E3779B97F4A7C15
153 z := r.state
154 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
155 z = (z ^ (z >> 27)) * 0x94D049BB133111EB
156 return z ^ (z >> 31)
157}
158
159func (r *rng) intn(n int) int {
160 if n <= 0 {
161 return 0
162 }
163 return int(r.next() % uint64(n))
164}
165
166func (r *rng) between(a, b int) int { return a + r.intn(b-a+1) }
167
168// ---------------------------------------------------------------------------
169// Trait tables (weights out of the table total).
170// ---------------------------------------------------------------------------
171
172type option struct {
173 name string
174 weight int
175}
176
177func pick(r *rng, table []option) int {
178 total := 0
179 for _, o := range table {
180 total += o.weight
181 }
182 x := r.intn(total)
183 for i, o := range table {
184 x -= o.weight
185 if x < 0 {
186 return i
187 }
188 }
189 return len(table) - 1
190}
191
192// inkSet: A (accent drum), B (second drum), C (skin drum), K (key), P (paper).
193type inkSet struct {
194 name string
195 weight int
196 a, b, c, k, p string
197}
198
199var inkSets = []inkSet{
200 {"Pink & Blue", 14, "#ff48b0", "#0078bf", "#ffe14a", "#23202e", "#f4ede0"},
201 {"Tangerine & Teal", 12, "#ff6c2f", "#00838a", "#ffc2dc", "#1f2433", "#f4ede0"},
202 {"Red & Federal Blue", 11, "#f15060", "#3d5588", "#f2d3a8", "#23202e", "#f7f3ea"},
203 {"Green & Violet", 10, "#00a95c", "#765ba7", "#ffb994", "#2b2420", "#f4ede0"},
204 {"Sunflower & Blue", 10, "#ffb511", "#3255a4", "#b8e3b2", "#1f2433", "#f7f3ea"},
205 {"Aqua & Burgundy", 9, "#5ec8e5", "#914e72", "#ffe14a", "#2b2420", "#f4ede0"},
206 {"Pink & Green", 8, "#ff48b0", "#00a95c", "#a6d6f0", "#23202e", "#f7f3ea"},
207 {"Orange & Violet", 8, "#ff6c2f", "#765ba7", "#a6d6f0", "#1f2433", "#f4ede0"},
208 {"Kraft Press", 6, "#f15060", "#00838a", "#f7e3b5", "#2b2420", "#d9c09a"},
209 {"Blue on Blush", 5, "#0078bf", "#765ba7", "#ffe14a", "#23202e", "#f7d6d3"},
210 {"Blueprint", 4, "#5ec8e5", "#3255a4", "#d8ecf5", "#1f2433", "#f7f3ea"},
211 {"Sinopia Earth", 3, "#b5432c", "#2e4a3a", "#f1d2b0", "#2a1712", "#efe4cc"},
212}
213
214var inkOpts = func() []option {
215 o := make([]option, len(inkSets))
216 for i, s := range inkSets {
217 o[i] = option{s.name, s.weight}
218 }
219 return o
220}()
221
222const (
223 bgSun = iota
224 bgArch
225 bgStripes
226 bgHalftone
227 bgTwin
228 bgPlain
229)
230
231var backdropOpts = []option{{"Sun", 24}, {"Arch", 18}, {"Stripes", 16}, {"Halftone", 16}, {"Twin Circles", 12}, {"Plain", 14}}
232
233const (
234 headEgg = iota
235 headBean
236 headBlock
237 headLong
238 headRound
239)
240
241var headOpts = []option{{"Egg", 30}, {"Bean", 22}, {"Block", 18}, {"Long", 16}, {"Round", 14}}
242
243const (
244 hairBangs = iota
245 hairBun
246 hairCloud
247 hairBob
248 hairQuiff
249 hairBraid
250 hairWave
251 hairBuzz
252)
253
254var hairOpts = []option{
255 {"Bangs", 16}, {"Bun", 14}, {"Cloud", 13}, {"Bob", 14}, {"Quiff", 12}, {"Braid", 11}, {"Long Wave", 11}, {"Buzz", 9},
256}
257
258const (
259 eyeDot = iota
260 eyeAlmond
261 eyeSleepy
262 eyeClosed
263 eyeRound
264 eyeWink
265)
266
267const (
268 mouthFlat = iota
269 mouthSmile
270 mouthO
271 mouthWhistle
272 mouthGrin
273 mouthLips
274)
275
276// expression: eyes and mouth.
277type expression struct {
278 name string
279 weight int
280 eyes, mouth int
281}
282
283var expressions = []expression{
284 {"Calm", 14, eyeDot, mouthFlat},
285 {"Content", 12, eyeClosed, mouthSmile},
286 {"Bright", 12, eyeAlmond, mouthSmile},
287 {"Dreamy", 10, eyeSleepy, mouthLips},
288 {"Pensive", 10, eyeAlmond, mouthFlat},
289 {"Drowsy", 8, eyeSleepy, mouthO},
290 {"Whistling", 8, eyeDot, mouthWhistle},
291 {"Sly", 8, eyeAlmond, mouthGrin},
292 {"Surprised", 7, eyeRound, mouthO},
293 {"Cheeky", 6, eyeWink, mouthGrin},
294 {"Serene", 5, eyeClosed, mouthLips},
295}
296
297const exprSurprised = 8
298
299var expressionOpts = func() []option {
300 o := make([]option, len(expressions))
301 for i, e := range expressions {
302 o[i] = option{e.name, e.weight}
303 }
304 return o
305}()
306
307const (
308 outfitTurtle = iota
309 outfitShirt
310 outfitKaftan
311 outfitJacket
312 outfitOveralls
313 outfitScarf
314)
315
316var outfitOpts = []option{
317 {"Turtleneck", 20}, {"Shirt Collar", 18}, {"Kaftan", 16}, {"Jacket", 18}, {"Overalls", 14}, {"Scarf", 14},
318}
319
320const (
321 accNone = iota
322 accHoop
323 accEarFlower
324 accPencil
325 accEvilEye
326 accStar
327 accBeret
328 accCap
329 accCrown
330 accPhones
331 accBandana
332 accBird
333)
334
335var accessoryOpts = []option{
336 {"None", 30}, {"Hoop Earring", 12}, {"Ear Flower", 10}, {"Pencil", 7}, {"Evil-Eye Pin", 8}, {"Star Pin", 5},
337 {"Beret", 8}, {"Cap", 7}, {"Flower Crown", 5}, {"Headphones", 4}, {"Bandana", 4}, {"Shoulder Bird", 3},
338}
339
340const (
341 printClean = iota
342 printShifted
343 printDouble
344 printBlot
345 printStarved
346 printGhost
347)
348
349var printOpts = []option{
350 {"Clean", 70}, {"Shifted Plate", 9}, {"Double Key", 9}, {"Ink Blot", 6}, {"Starved Ink", 4}, {"Ghost", 2},
351}
352
353// noseOpts are drawn from the art stream (not a trait).
354var noseOpts = []option{{"Straight", 34}, {"Hooked", 26}, {"Upturned", 22}, {"Button", 18}}
355
356type draw struct {
357 inks, backdrop, head, hair, expression, outfit, accessory, print int
358}
359
360func derive(seed string, cfg Config) draw {
361 r := newRNG(seed + "|traits")
362 // Draw order is part of the contract: changing it changes every token.
363 var d draw
364 d.inks = pick(r, inkOpts)
365 d.backdrop = pick(r, backdropOpts)
366 d.head = pick(r, headOpts)
367 d.hair = pick(r, hairOpts)
368 d.expression = pick(r, expressionOpts)
369 d.outfit = pick(r, outfitOpts)
370 d.accessory = pick(r, accessoryOpts)
371 d.print = pick(r, printOpts)
372 // Params override after the draws, so the stream never shifts.
373 if cfg.Inks >= 0 {
374 d.inks = cfg.Inks
375 }
376 return d
377}
378
379// TraitSet is the decoded properties of a seed under a config.
380type TraitSet struct {
381 Inks, Backdrop, Head, Hair, Expression, Outfit, Accessory, Print string
382}
383
384// TraitsOf returns the decoded traits for a seed.
385func TraitsOf(seed string, cfg Config) TraitSet {
386 d := derive(seed, cfg)
387 return TraitSet{
388 Inks: inkSets[d.inks].name,
389 Backdrop: backdropOpts[d.backdrop].name,
390 Head: headOpts[d.head].name,
391 Hair: hairOpts[d.hair].name,
392 Expression: expressions[d.expression].name,
393 Outfit: outfitOpts[d.outfit].name,
394 Accessory: accessoryOpts[d.accessory].name,
395 Print: printOpts[d.print].name,
396 }
397}
398
399// ---------------------------------------------------------------------------
400// Integer helpers (Go's truncating / everywhere; the TS twin uses Math.trunc).
401// ---------------------------------------------------------------------------
402
403func n(v int) string { return strconv.Itoa(v) }
404
405// pc is v*p/100 (v, p >= 0 in every call).
406func pc(v, p int) int { return v * p / 100 }
407
408// qsin is a quarter-wave sine table, Q10: sin(i*pi/128)*1024 for i = 0..64.
409var qsin = []int{
410 0, 25, 50, 75, 100, 125, 150, 175, 200, 224, 249, 273, 297, 321, 345, 369, 392, 415, 438, 460, 483, 505, 526, 548,
411 569, 590, 610, 630, 650, 669, 688, 706, 724, 742, 759, 775, 792, 807, 822, 837, 851, 865, 878, 891, 903, 915, 926,
412 936, 946, 955, 964, 972, 980, 987, 993, 999, 1004, 1009, 1013, 1016, 1019, 1021, 1023, 1024, 1024,
413}
414
415// sn is sin(a) with 256 steps per turn, Q10; a >= 0.
416func sn(a int) int {
417 q := a % 256
418 switch {
419 case q <= 64:
420 return qsin[q]
421 case q <= 128:
422 return qsin[128-q]
423 case q <= 192:
424 return -qsin[q-128]
425 }
426 return -qsin[256-q]
427}
428
429func cs(a int) int { return sn(a + 64) }
430
431const hexDigits = "0123456789abcdef"
432
433func unhex(c byte) int {
434 switch {
435 case c >= '0' && c <= '9':
436 return int(c - '0')
437 case c >= 'a' && c <= 'f':
438 return int(c-'a') + 10
439 }
440 return 0
441}
442
443// mul is the overprint of two inks: the channel-wise product, rounded.
444func mul(a, b string) string {
445 out := []byte{'#', 0, 0, 0, 0, 0, 0}
446 for i := 1; i < 7; i += 2 {
447 v := ((unhex(a[i])*16+unhex(a[i+1]))*(unhex(b[i])*16+unhex(b[i+1])) + 127) / 255
448 out[i] = hexDigits[v>>4]
449 out[i+1] = hexDigits[v&15]
450 }
451 return string(out)
452}
453
454func tr(x, y int) string { return "translate(" + n(x) + " " + n(y) + ")" }
455
456func dotPattern(id, ink string, s int, rad string, rot int) string {
457 return `<pattern id="` + id + `" width="` + n(s) + `" height="` + n(s) +
458 `" patternUnits="userSpaceOnUse" patternTransform="rotate(` + n(rot) + `)">` +
459 `<circle cx="` + n(s/2) + `" cy="` + n(s/2) + `" r="` + rad + `" fill="` + ink + `"/></pattern>`
460}
461
462// ---------------------------------------------------------------------------
463// Rendering
464// ---------------------------------------------------------------------------
465
466// canvas collects the plates while the figure is built.
467type canvas struct {
468 defs, aEls, bEls, tex, texSkin, key, keyF string
469 aIDs []string
470}
471
472func (c *canvas) addA(tag, attrs string) {
473 id := "a" + n(len(c.aIDs))
474 c.aIDs = append(c.aIDs, id)
475 c.aEls += "<" + tag + ` id="` + id + `" ` + attrs + "/>"
476}
477
478func (c *canvas) addB(tag, attrs string) { c.bEls += "<" + tag + " " + attrs + "/>" }
479
480// eye draws one eye at (x, y), scaled s percent.
481func (c *canvas) eye(x, y, s, kind int) {
482 m := func(v int) int { return v * s / 100 }
483 switch kind {
484 case eyeDot:
485 c.keyF += `<circle cx="` + n(x) + `" cy="` + n(y) + `" r="` + n(m(6)) + `"/>`
486 case eyeAlmond:
487 c.key += "M" + n(x-m(14)) + " " + n(y) + "q" + n(m(14)) + " " + n(-m(12)) + " " + n(m(28)) + " 0q" +
488 n(-m(14)) + " " + n(m(10)) + " " + n(-m(28)) + " 0"
489 c.keyF += `<circle cx="` + n(x+2) + `" cy="` + n(y-1) + `" r="` + n(m(5)) + `"/>`
490 case eyeSleepy:
491 c.key += "M" + n(x-m(14)) + " " + n(y-2) + "h" + n(m(28)) + "M" + n(x-m(11)) + " " + n(y+6) + "q" +
492 n(m(11)) + " 6 " + n(m(22)) + " 0"
493 c.keyF += `<path d="M` + n(x-m(8)) + " " + n(y-2) + "a" + n(m(8)) + " 7 0 0 0 " + n(m(16)) + ` 0z"/>`
494 case eyeClosed:
495 c.key += "M" + n(x-m(13)) + " " + n(y) + "q" + n(m(13)) + " " + n(m(12)) + " " + n(m(26)) + " 0M" +
496 n(x-m(8)) + " " + n(y+6) + "l-4 6M" + n(x) + " " + n(y+8) + "v7M" + n(x+m(8)) + " " + n(y+6) + "l4 6"
497 case eyeRound:
498 c.key += "M" + n(x-m(12)) + " " + n(y) + "a" + n(m(12)) + " " + n(m(12)) + " 0 1 0 " + n(m(24)) + " 0a" +
499 n(m(12)) + " " + n(m(12)) + " 0 1 0 " + n(-m(24)) + " 0"
500 c.keyF += `<circle cx="` + n(x+3) + `" cy="` + n(y) + `" r="` + n(m(4)) + `"/>`
501 default: // wink
502 c.key += "M" + n(x-m(12)) + " " + n(y+4) + "l" + n(m(12)) + "-10l" + n(m(12)) + " 10"
503 }
504}
505
506// SVG renders the artwork for a seed under cfg.
507func SVG(seed string, cfg Config) string {
508 t := derive(seed, cfg)
509 R := newRNG(seed + "|art") // separate stream: trait draws never shift the art
510 ink := inkSets[t.inks]
511 A, B, C, K, P := ink.a, ink.b, ink.c, ink.k, ink.p
512 AB, AC, BC := mul(A, B), mul(A, C), mul(B, C)
513 ABC := mul(AB, C)
514 eyes, mouth := expressions[t.expression].eyes, expressions[t.expression].mouth
515
516 // Plate offsets (misregistration).
517 oAx, oAy := R.between(-5, 5), R.between(-5, 5)
518 oBx, oBy := R.between(-5, 5), R.between(-5, 5)
519 oCx, oCy := R.between(-5, 5), R.between(-5, 5)
520 oKx, oKy := R.between(-2, 2), R.between(-2, 2)
521 if t.print == printShifted {
522 oBx = R.between(12, 18)
523 oBy = R.between(6, 12)
524 if R.intn(2) == 1 {
525 oBx = -oBx
526 }
527 if R.intn(2) == 1 {
528 oBy = -oBy
529 }
530 }
531 left := R.intn(2) == 1
532
533 // Geometry (a bust in three-quarter view, facing right before mirroring).
534 cx := 290 + R.between(-12, 12)
535 cy := 252 + R.between(-10, 10)
536 rx, ry, fwd, kap := R.between(98, 114), R.between(124, 140), R.between(18, 34), 56
537 switch t.head {
538 case headBean:
539 fwd += 18
540 rx += 6
541 case headBlock:
542 kap = 78
543 case headLong:
544 ry += 24
545 rx -= 12
546 case headRound:
547 ry = rx + 6
548 }
549 sw := R.between(150, 190)
550 inkSeed, paperSeed := R.between(1, 900), R.between(1, 900)
551
552 tY, fry, chy := cy-ry, cy+pc(ry, 8), cy+ry
553 k1, k2 := pc(rx, kap), pc(ry, kap)
554 k3, k4, k5 := ry*kap*70/10000, rx*kap*60/10000, rx*kap*130/10000
555 headD := "M" + n(cx) + " " + n(tY) + "C" + n(cx+k1) + " " + n(tY) + " " + n(cx+rx) + " " + n(fry-k2) + " " +
556 n(cx+rx) + " " + n(fry) +
557 "C" + n(cx+rx) + " " + n(fry+k3) + " " + n(cx+fwd+k4) + " " + n(chy) + " " + n(cx+fwd) + " " + n(chy) +
558 "C" + n(cx+fwd-k5) + " " + n(chy) + " " + n(cx-rx) + " " + n(cy+k2) + " " + n(cx-rx) + " " + n(cy) +
559 "C" + n(cx-rx) + " " + n(cy-k2) + " " + n(cx-k1) + " " + n(tY) + " " + n(cx) + " " + n(tY) + "z"
560 neckD := "M" + n(cx-34) + " " + n(cy+pc(ry, 45)) + "L" + n(cx-40) + " 490L" + n(cx+46) + " 490L" + n(cx+38) +
561 " " + n(cy+pc(ry, 55)) + "z"
562 bodyD := "M" + n(cx-sw-70) + " 620C" + n(cx-sw-50) + " 520 " + n(cx-sw+10) + " 478 " + n(cx-10) + " 470" +
563 "C" + n(cx+sw-10) + " 474 " + n(cx+sw+50) + " 520 " + n(cx+sw+70) + " 620z"
564 earX := cx - pc(rx, 55)
565 fx, ey := cx+pc(rx, 36), cy-4
566 neckTop := cy + ry - 40
567
568 c := &canvas{}
569
570 // --- backdrop (A drum; the twin circle adds a B disc) ---
571 bg := ""
572 switch t.backdrop {
573 case bgSun:
574 dx, dy, rr := R.between(-40, 60), R.between(0, 40), R.between(170, 210)
575 bg = `<circle cx="` + n(cx+dx+oAx) + `" cy="` + n(cy-dy+oAy) + `" r="` + n(rr) + `" fill="` + A + `"/>`
576 case bgArch:
577 bg = `<path d="M` + n(cx-200+oAx) + " 600V" + n(cy-20+oAy) + `a200 200 0 0 1 400 0V600z" fill="` + A + `"/>`
578 case bgStripes:
579 rot := []int{30, -30, 60}[R.intn(3)]
580 c.defs += `<pattern id="sr" width="34" height="34" patternUnits="userSpaceOnUse" patternTransform="rotate(` +
581 n(rot) + `)"><rect width="15" height="34" fill="` + A + `"/></pattern>`
582 bg = `<rect width="600" height="600" fill="url(#sr)"/>`
583 case bgHalftone:
584 c.defs += dotPattern("hz", A, 14, "4.2", 30)
585 bg = `<rect width="600" height="600" fill="url(#hz)"/>`
586 case bgTwin:
587 ax, ay, bx, by := cx-70+oAx, cy-30+oAy, cx+90+oBx, cy+10+oBy
588 c.defs += `<clipPath id="ct"><circle cx="` + n(ax) + `" cy="` + n(ay) + `" r="170"/></clipPath>`
589 bg = `<circle cx="` + n(ax) + `" cy="` + n(ay) + `" r="170" fill="` + A + `"/><circle cx="` + n(bx) +
590 `" cy="` + n(by) + `" r="170" fill="` + B + `"/><circle cx="` + n(bx) + `" cy="` + n(by) +
591 `" r="170" fill="` + AB + `" clip-path="url(#ct)"/>`
592 }
593
594 // --- outfit ---
595 if t.outfit == outfitKaftan || t.outfit == outfitOveralls {
596 c.aIDs = append(c.aIDs, "bd")
597 c.aEls += `<path id="bd" d="` + bodyD + `"/>`
598 } else {
599 c.bEls += `<path id="bd" d="` + bodyD + `"/>`
600 }
601 switch t.outfit {
602 case outfitTurtle:
603 c.addB("path", `d="M`+n(cx-46)+" 492L"+n(cx-42)+" "+n(neckTop+30)+"Q"+n(cx+4)+" "+n(neckTop+40)+" "+
604 n(cx+46)+" "+n(neckTop+30)+"L"+n(cx+52)+` 492z"`)
605 c.key += "M" + n(cx-40) + " " + n(neckTop+48) + "q44 10 88 0M" + n(cx-41) + " " + n(neckTop+62) + "q45 10 90 0"
606 case outfitShirt:
607 c.addA("path", `d="M`+n(cx-44)+" 478l40 40 6-46zM"+n(cx+52)+` 478l-40 40-6-46z"`)
608 c.key += "M" + n(cx+3) + " 520v90"
609 for y := 535; y < 600; y += 26 {
610 c.key += "M" + n(cx+3) + " " + n(y) + "h.1"
611 }
612 case outfitKaftan:
613 c.defs += `<pattern id="kf" width="22" height="22" patternUnits="userSpaceOnUse">` +
614 `<path d="M3 0v22M14 0l4 5-4 6 4 5-4 6" stroke="` + AB + `" stroke-width="3" fill="none"/></pattern>`
615 c.tex += `<use href="#bd" transform="` + tr(oBx, oBy) + `" fill="url(#kf)"/>`
616 c.key += "M" + n(cx-40) + " 478L" + n(cx+4) + " 560L" + n(cx+46) + " 478"
617 case outfitJacket:
618 c.addA("path", `d="M`+n(cx-30)+" 480L"+n(cx+4)+" 600L"+n(cx+40)+` 480z"`)
619 c.key += "M" + n(cx-44) + " 478L" + n(cx-60) + " 530L" + n(cx-30) + " 540L" + n(cx+2) + " 600M" +
620 n(cx+50) + " 478L" + n(cx+66) + " 530L" + n(cx+38) + " 540L" + n(cx+6) + " 600"
621 case outfitOveralls:
622 c.addB("path", `d="M`+n(cx-80)+" 600V545h170V600zM"+n(cx-76)+" 548l-30-70h22l30 70zM"+n(cx+86)+
623 ` 548l30-70h-22l-30 70z"`)
624 c.key += "M" + n(cx-60) + " 560h.1M" + n(cx+70) + " 560h.1"
625 default: // outfitScarf
626 c.addA("path", `d="M`+n(cx-64)+" "+n(neckTop+20)+"Q"+n(cx+4)+" "+n(neckTop+70)+" "+n(cx+74)+" "+
627 n(neckTop+20)+"L"+n(cx+80)+" "+n(neckTop+60)+"Q"+n(cx+4)+" "+n(neckTop+110)+" "+n(cx-70)+" "+
628 n(neckTop+60)+"zM"+n(cx+30)+" "+n(neckTop+70)+`l-10 90h34l6-90z"`)
629 for k := 0; k < 4; k++ {
630 c.key += "M" + n(cx+22+10*k) + " " + n(neckTop+160) + "v14"
631 }
632 }
633
634 // --- hair (B drum) ---
635 hair := t.hair
636 capD := func(low int) string {
637 return "M" + n(cx-rx-6) + " " + n(cy+low) + "C" + n(cx-rx-10) + " " + n(tY-40) + " " + n(cx+pc(rx, 70)) +
638 " " + n(tY-46) + " " + n(cx+rx+8) + " " + n(cy-pc(ry, 35)) +
639 "Q" + n(cx+pc(rx, 40)) + " " + n(cy-pc(ry, 55)) + " " + n(cx-pc(rx, 20)) + " " + n(cy-pc(ry, 45)) +
640 "Q" + n(cx-pc(rx, 45)) + " " + n(cy-10) + " " + n(cx-pc(rx, 50)) + " " + n(cy+low) + "z"
641 }
642 if hair == hairWave {
643 c.addB("path", `d="M`+n(cx-rx-20)+" "+n(cy-40)+"Q"+n(cx-rx-50)+" "+n(cy+160)+" "+n(cx-rx-10)+" 540Q"+
644 n(cx-40)+" 560 "+n(cx-pc(rx, 25))+" "+n(cy+ry-10)+"L"+n(cx-pc(rx, 35))+" "+n(cy-30)+`z"`)
645 }
646 if hair == hairBraid {
647 d := ""
648 for k := 0; k < 5; k++ {
649 d += "M" + n(cx-rx-10-6*k) + " " + n(cy+40+36*k) + "a16 20 0 1 0 32 0a16 20 0 1 0-32 0z"
650 }
651 c.addB("path", `d="`+d+`"`)
652 }
653 if hair == hairBangs || hair == hairBun || hair == hairBraid || hair == hairWave {
654 low := 30
655 if hair == hairBangs {
656 low = 0
657 }
658 c.addB("path", `d="`+capD(low)+`"`)
659 }
660 if hair == hairBangs {
661 d := "M" + n(cx-pc(rx, 20)) + " " + n(cy-pc(ry, 45))
662 for k := 0; k < 6; k++ {
663 if k%2 == 1 {
664 d += "l16-14"
665 } else {
666 d += "l16 22"
667 }
668 }
669 c.addB("path", `d="`+d+"L"+n(cx+pc(rx, 70))+" "+n(cy-pc(ry, 70))+`z"`)
670 }
671 if hair == hairBun {
672 c.addB("circle", `cx="`+n(cx-pc(rx, 35))+`" cy="`+n(tY-18)+`" r="`+n(R.between(40, 52))+`"`)
673 }
674 if hair == hairCloud {
675 d := ""
676 for k := 0; k < 11; k++ {
677 a, rr := 96+9*k, R.between(34, 46)
678 x, y := cx+(rx+6)*cs(a)/1024-10, cy+(ry+4)*sn(a)/1024-6
679 d += "M" + n(x-rr) + " " + n(y) + "a" + n(rr) + " " + n(rr) + " 0 1 0 " + n(2*rr) + " 0a" + n(rr) + " " +
680 n(rr) + " 0 1 0-" + n(2*rr) + " 0z"
681 }
682 c.addB("path", `d="`+d+`"`)
683 }
684 if hair == hairBob {
685 c.addB("path", `d="M`+n(cx-rx-14)+" "+n(cy+pc(ry, 55))+"V"+n(cy-20)+"C"+n(cx-rx-14)+" "+n(tY-50)+" "+
686 n(cx+rx+10)+" "+n(tY-50)+" "+n(cx+rx+12)+" "+n(cy-pc(ry, 25))+"H"+n(cx-pc(rx, 30))+"V"+
687 n(cy+pc(ry, 55))+`z"`)
688 }
689 if hair == hairQuiff {
690 c.addB("path", `d="M`+n(cx-rx-4)+" "+n(cy+10)+"C"+n(cx-rx-20)+" "+n(tY-30)+" "+n(cx)+" "+n(tY-110)+" "+
691 n(cx+rx+40)+" "+n(tY-70)+"Q"+n(cx+pc(rx, 30))+" "+n(tY-50)+" "+n(cx+pc(rx, 90))+" "+n(cy-pc(ry, 45))+
692 "Q"+n(cx)+" "+n(cy-pc(ry, 60))+" "+n(cx-pc(rx, 45))+" "+n(cy+10)+`z"`)
693 }
694 if hair == hairBuzz {
695 c.texSkin += `<path d="` + capD(-20) + `" transform="` + tr(oBx-oCx, oBy-oCy) + `" fill="url(#hb)"/>`
696 }
697
698 // --- accessory ---
699 acc := t.accessory
700 if acc == accBeret {
701 c.addB("path", `d="M`+n(cx-rx-20)+" "+n(tY+40)+"Q"+n(cx-40)+" "+n(tY-70)+" "+n(cx+rx+30)+" "+n(tY+20)+
702 "Q"+n(cx)+" "+n(tY+30)+" "+n(cx-rx-20)+" "+n(tY+40)+`z"`)
703 c.addB("circle", `cx="`+n(cx-10)+`" cy="`+n(tY-28)+`" r="7"`)
704 }
705 if acc == accCap {
706 c.addB("path", `d="M`+n(cx-rx-8)+" "+n(tY+60)+"C"+n(cx-rx)+" "+n(tY-40)+" "+n(cx+rx)+" "+n(tY-40)+" "+
707 n(cx+rx+6)+" "+n(tY+44)+"Q"+n(cx+rx+80)+" "+n(tY+40)+" "+n(cx+rx+70)+" "+n(tY+62)+"Q"+n(cx)+" "+
708 n(tY+40)+" "+n(cx-rx-8)+" "+n(tY+60)+`z"`)
709 }
710 if acc == accCrown {
711 for k := 0; k < 6; k++ {
712 a := 150 + 16*k
713 x, y := cx+(rx-6)*cs(a)/1024, cy+(ry-10)*sn(a)/1024-6
714 c.addA("circle", `cx="`+n(x)+`" cy="`+n(y)+`" r="15"`)
715 c.key += "M" + n(x) + " " + n(y) + "h.1"
716 }
717 }
718 if acc == accPhones {
719 l, rr := cx-pc(rx, 60), cx+pc(rx, 30)
720 c.addA("path", `d="M`+n(l)+" "+n(cy+8)+"C"+n(cx-pc(rx, 70))+" "+n(tY-60)+" "+n(cx+pc(rx, 50))+" "+
721 n(tY-60)+" "+n(rr)+" "+n(tY+20)+"L"+n(rr-11)+" "+n(tY+23)+"C"+n(cx+pc(rx, 50)-8)+" "+n(tY-46)+" "+
722 n(cx-pc(rx, 70)+12)+" "+n(tY-46)+" "+n(l+12)+" "+n(cy+8)+`z"`)
723 c.addA("rect", `x="`+n(cx-pc(rx, 55)-24)+`" y="`+n(cy-20)+`" width="44" height="62" rx="16"`)
724 }
725 if acc == accBandana {
726 c.addA("path", `d="M`+n(cx-rx-6)+" "+n(cy-pc(ry, 30))+"Q"+n(cx)+" "+n(cy-pc(ry, 75))+" "+n(cx+rx+6)+" "+
727 n(cy-pc(ry, 55))+"l2 26Q"+n(cx)+" "+n(cy-pc(ry, 50))+" "+n(cx-rx-2)+" "+n(cy-pc(ry, 5))+"zM"+
728 n(cx-rx-4)+" "+n(cy-pc(ry, 20))+`l-40 30 10 10z"`)
729 }
730
731 // --- skin (C drum) ---
732 skin := `<path id="nk" d="` + neckD + `"/><path id="hd" d="` + headD + `"/><ellipse id="ea" cx="` + n(earX) +
733 `" cy="` + n(cy+12) + `" rx="15" ry="24"/>`
734
735 // --- face (key drum) ---
736 nose := pick(R, noseOpts)
737 nl, nt := R.between(44, 60), R.between(26, 40)
738 n0x, n0y, tipX, tipY, bX, bY := fx+4, ey-26, fx+nt, ey+nl, fx+8, ey+nl+12
739 switch nose {
740 case 0:
741 c.key += "M" + n(n0x) + " " + n(n0y) + "L" + n(tipX) + " " + n(tipY) + "L" + n(bX) + " " + n(bY)
742 case 1:
743 c.key += "M" + n(n0x) + " " + n(n0y) + "Q" + n(tipX+14) + " " + n(n0y+20) + " " + n(tipX) + " " + n(tipY+6) +
744 "L" + n(bX) + " " + n(bY)
745 case 2:
746 c.key += "M" + n(n0x) + " " + n(n0y) + "L" + n(tipX-6) + " " + n(tipY-10) + "l12-4L" + n(bX) + " " + n(bY)
747 default:
748 c.key += "M" + n(n0x) + " " + n(n0y) + "L" + n(tipX-10) + " " + n(tipY-14) + "a14 14 0 1 1-2 26L" + n(bX) +
749 " " + n(bY)
750 }
751
752 // near eye (s = 100) left of the nose, far eye (s = 80) beyond it
753 e1x, e1y, e2x, e2y := fx-36, ey+2, fx+38, ey
754 c.eye(e1x, e1y, 100, eyes)
755 if eyes == eyeWink {
756 c.eye(e2x, e2y, 80, eyeDot)
757 } else {
758 c.eye(e2x, e2y, 80, eyes)
759 }
760 bl := R.between(-8, 8)
761 lift := 0
762 if t.expression == exprSurprised {
763 lift = 10
764 }
765 c.key += "M" + n(e1x-16) + " " + n(e1y-24+bl-lift) + "l30 " + n(-bl-4) + "M" + n(e2x-10) + " " +
766 n(e2y-22-bl/2-lift) + "l20 " + n(bl/2)
767 mx, my := fx+12, ey+nl+40
768 switch mouth {
769 case mouthFlat:
770 c.key += "M" + n(mx-14) + " " + n(my) + "h30"
771 case mouthSmile:
772 c.key += "M" + n(mx-18) + " " + n(my-4) + "q18 16 36-2"
773 case mouthO:
774 c.key += "M" + n(mx-6) + " " + n(my) + "a7 9 0 1 0 14 0a7 9 0 1 0-14 0"
775 case mouthWhistle:
776 c.keyF += `<circle cx="` + n(mx+4) + `" cy="` + n(my) + `" r="6"/>`
777 c.key += "M" + n(mx+26) + " " + n(my-12) + "l10-8M" + n(mx+30) + " " + n(my) + "h12"
778 case mouthGrin:
779 c.key += "M" + n(mx-22) + " " + n(my-6) + "q22 26 44 0zM" + n(mx-18) + " " + n(my) + "h36"
780 default: // lips
781 c.addA("path", `d="M`+n(mx-18)+" "+n(my)+`q9-12 18-4q9-8 18 4q-18 16-36 0z"`)
782 }
783 c.key += "M" + n(earX-4) + " " + n(cy+2) + "q10 8 2 22"
784
785 // --- small accessories ---
786 if acc == accHoop {
787 c.key += "M" + n(earX-9) + " " + n(cy+40) + "a10 12 0 1 0 20 0a10 12 0 1 0-20 0"
788 }
789 if acc == accEarFlower {
790 x, y := cx-pc(rx, 62), cy-22
791 for k := 0; k < 5; k++ {
792 c.addA("circle", `cx="`+n(x+15*cs(51*k)/1024)+`" cy="`+n(y+15*sn(51*k)/1024)+`" r="11"`)
793 }
794 c.keyF += `<circle cx="` + n(x) + `" cy="` + n(y) + `" r="5"/>`
795 }
796 if acc == accPencil {
797 x, y := cx-rx-30, cy+4
798 c.addA("path", `d="M`+n(x)+" "+n(y)+`l95-31 5 14-95 31z"`)
799 c.key += "M" + n(x+95) + " " + n(y-31) + "l16 5-11 9"
800 }
801 if acc == accEvilEye {
802 x := cx - 80
803 c.addA("circle", `cx="`+n(x)+`" cy="548" r="18"`)
804 c.tex += `<circle cx="` + n(x+oAx) + `" cy="` + n(548+oAy) + `" r="10" fill="` + P + `"/>`
805 c.keyF += `<circle cx="` + n(x) + `" cy="548" r="5"/>`
806 c.key += "M" + n(x-18) + " 548a18 18 0 1 0 36 0a18 18 0 1 0-36 0"
807 }
808 if acc == accStar {
809 c.addA("path", `d="M`+n(cx+90)+` 532l6 13 14 2-10 10 2 14-12-7-12 7 2-14-10-10 14-2z"`)
810 }
811 if acc == accBird {
812 x := cx + sw - 30
813 c.addA("ellipse", `cx="`+n(x)+`" cy="482" rx="26" ry="18"`)
814 c.addA("circle", `cx="`+n(x+22)+`" cy="466" r="12"`)
815 c.key += "M" + n(x-22) + " 478q20-18 36 2M" + n(x+32) + " 464l10 3-10 4"
816 c.keyF += `<circle cx="` + n(x+24) + `" cy="463" r="2.5"/>`
817 }
818
819 // --- misprints ---
820 if t.print == printBlot {
821 x, y := R.between(80, 520), R.between(80, 520)
822 c.addB("path", `d="M`+n(x)+" "+n(y)+`q36-52 68-8q44 8 12 52q-12 40-56 20q-52 4-24-64z"`)
823 c.addB("circle", `cx="`+n(x+96)+`" cy="`+n(y-18)+`" r="8"`)
824 c.addB("circle", `cx="`+n(x-26)+`" cy="`+n(y+70)+`" r="5"`)
825 }
826
827 // --- shading (halftone dots, pre-multiplied with the skin ink they sit on) ---
828 c.defs += dotPattern("hb", BC, 7, "2.1", 45) + dotPattern("ha", AC, 9, "2.7", 15)
829 shade := `<circle cx="` + n(cx-pc(rx, 125)+oBx-oCx) + `" cy="` + n(cy+20+oBy-oCy) + `" r="` + n(pc(rx, 105)) +
830 `" fill="url(#hb)"/><circle cx="` + n(fx-34+oAx-oCx) + `" cy="` + n(ey+46+oAy-oCy) +
831 `" r="24" fill="url(#ha)"/>`
832 // the head's cast shadow on the neck: the head moved down, clipped to the neck and outside the head
833 neckShade := `<use href="#hd" transform="` + tr(oBx-oCx+6, oBy-oCy+28) + `" fill="url(#hb)"/>`
834
835 // --- assemble ---
836 fa, fb := "url(#ik)", "url(#ik)"
837 if t.print == printStarved {
838 fb = "url(#is)"
839 }
840 clipA := ""
841 for _, id := range c.aIDs {
842 clipA += `<use href="#` + id + `"/>`
843 }
844 out := `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 600 600"><defs>` + c.defs +
845 `<clipPath id="cc"><use href="#nk"/><use href="#hd"/><use href="#ea"/></clipPath>` +
846 `<clipPath id="cn"><use href="#nk"/></clipPath><clipPath id="co"><path clip-rule="evenodd" d="M-50-50h700v700h-700z` +
847 headD + `"/></clipPath><clipPath id="ca">` + clipA + `</clipPath>` +
848 `<g id="rm" fill="none" stroke-width="2"><circle cx="560" cy="40" r="10"/><path d="M542 40h36M560 22v36"/></g>` +
849 `<filter id="ik" x="-5%" y="-5%" width="110%" height="110%"><feTurbulence type="fractalNoise" baseFrequency="1.1" ` +
850 `numOctaves="1" seed="` + n(inkSeed) + `" result="n"/><feColorMatrix in="n" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -14 11.4" ` +
851 `result="m"/><feComposite in="SourceGraphic" in2="m" operator="in"/></filter>`
852 if t.print == printStarved {
853 out += `<filter id="is" x="-5%" y="-5%" width="110%" height="110%"><feTurbulence type="fractalNoise" baseFrequency=".9" ` +
854 `numOctaves="2" seed="` + n(inkSeed) + `" result="n"/><feColorMatrix in="n" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -9 5.6" ` +
855 `result="m"/><feComposite in="SourceGraphic" in2="m" operator="in"/></filter>`
856 }
857 out += `<filter id="pp" x="0" y="0" width="1" height="1"><feTurbulence type="fractalNoise" baseFrequency=".6" ` +
858 `numOctaves="2" seed="` + n(paperSeed) + `"/><feColorMatrix values="0 0 0 0 .4 0 0 0 0 .32 0 0 0 0 .22 0 0 0 -1.5 .8"/></filter>` +
859 `</defs><rect width="600" height="600" fill="` + P + `"/>`
860 if left {
861 out += `<g transform="matrix(-1 0 0 1 600 0)">`
862 } else {
863 out += "<g>"
864 }
865 if bg != "" {
866 out += `<g filter="` + fa + `">` + bg + `</g>`
867 // knock-out: no backdrop ink under the figure (the A plate's own offset)
868 out += `<g transform="` + tr(oAx, oAy) + `" fill="` + P + `"><use href="#C"/><use href="#A"/><use href="#B"/></g>`
869 }
870 if t.print == printGhost {
871 gx := R.between(50, 90)
872 if R.intn(2) == 1 {
873 gx = -gx
874 }
875 gy := R.between(-20, 20)
876 out += `<g transform="` + tr(oBx+gx, oBy+gy) + `" fill="` + B + `" opacity=".22"><use href="#B"/></g>`
877 }
878 // single plates
879 out += `<g transform="` + tr(oCx, oCy) + `" fill="` + C + `" filter="` + fa + `"><g id="C">` + skin + `</g></g>`
880 out += `<g transform="` + tr(oAx, oAy) + `" fill="` + A + `" filter="` + fa + `"><g id="A">` + c.aEls + `</g></g>`
881 out += `<g transform="` + tr(oBx, oBy) + `" fill="` + B + `" filter="` + fb + `"><g id="B">` + c.bEls + `</g></g>`
882 // overprint: each overlap gets the product of its inks
883 out += `<g transform="` + tr(oCx, oCy) + `" clip-path="url(#cc)">` +
884 `<g transform="` + tr(oAx-oCx, oAy-oCy) + `" fill="` + AC + `" filter="` + fa + `"><use href="#A"/></g>` +
885 `<g transform="` + tr(oBx-oCx, oBy-oCy) + `" fill="` + BC + `" filter="` + fb + `"><use href="#B"/></g></g>`
886 if len(c.aIDs) > 0 {
887 out += `<g transform="` + tr(oAx, oAy) + `" clip-path="url(#ca)">` +
888 `<g transform="` + tr(oBx-oAx, oBy-oAy) + `" fill="` + AB + `" filter="` + fb + `"><use href="#B"/></g></g>` +
889 `<g transform="` + tr(oCx, oCy) + `" clip-path="url(#cc)"><g transform="` + tr(oAx-oCx, oAy-oCy) +
890 `" clip-path="url(#ca)"><g transform="` + tr(oBx-oAx, oBy-oAy) + `" fill="` + ABC + `" filter="` + fb +
891 `"><use href="#B"/></g></g></g>`
892 }
893 // halftone shading and textures
894 out += `<g transform="` + tr(oCx, oCy) + `" clip-path="url(#cc)" filter="` + fb + `">` + shade + c.texSkin + `</g>` +
895 `<g transform="` + tr(oCx, oCy) + `" clip-path="url(#cn)"><g clip-path="url(#co)" filter="` + fb + `">` +
896 neckShade + `</g></g>`
897 if c.tex != "" {
898 out += `<g filter="` + fb + `">` + c.tex + `</g>`
899 }
900 // key plate
901 out += `<g id="K" transform="` + tr(oKx, oKy) + `"><path d="` + c.key + `" fill="none" stroke="` + K +
902 `" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/><g fill="` + K + `">` + c.keyF + `</g></g>`
903 if t.print == printDouble {
904 dx := R.between(5, 9)
905 dy := R.between(-7, 7)
906 out += `<use href="#K" transform="` + tr(dx, dy) + `" opacity=".45"/>`
907 }
908 // registration marks, one per plate
909 out += `<use href="#rm" stroke="` + C + `" transform="` + tr(oCx, oCy) + `"/><use href="#rm" stroke="` + A +
910 `" transform="` + tr(oAx, oAy) + `"/><use href="#rm" stroke="` + B + `" transform="` + tr(oBx, oBy) +
911 `"/><use href="#rm" stroke="` + K + `" transform="` + tr(oKx, oKy) + `"/>`
912 out += `</g><rect width="600" height="600" filter="url(#pp)" opacity=".3"/></svg>`
913 return out
914}