rubrica.gno
23.63 Kb · 822 lines
1// Package rubrica is an asemic-script engine: every token invents an
2// alphabet of nine to sixteen glyphs (strokes on a 3x4 node grid) and writes
3// one page with it — a ruled manuscript, an inscribed stele, a scroll or a
4// glossed page — with red rubrics (initials, first lines) marking where a
5// reader should begin.
6//
7// It is a stateless p/ package implementing engine/v1. Pure integer maths
8// (FNV-1a + splitmix64 stream, truncating division, a 256-step cosine
9// table), so the TypeScript twin (frontend/src/engines/rubrica.ts) renders
10// the same bytes. Traits come from their own cheap stream (seed+"|traits")
11// and never render; the art uses seed+"|art", so art draws never shift traits.
12//
13// Glyphs are defined once in <defs> and placed with <use>; each line is one
14// translate and each glyph only an x. A hard glyph budget keeps every page
15// small (see GlyphBudget).
16package rubrica
17
18import (
19 "errors"
20 "strconv"
21 "strings"
22
23 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/engine/v1"
24)
25
26// TraitTypes in the order Traits returns them.
27var traitTypes = []string{"Script", "Layout", "Hand", "Rubrication", "Vellum", "Ink", "Damage", "Alphabet", "Rarity"}
28
29// MaxSVGBytes: measured max is ~19.5 KB over thousands of seeds and every
30// forced layout; the glyph budget bounds the worst case well below this.
31const MaxSVGBytes = 28672
32
33// GlyphBudget and GlossBudget cap the glyphs placed on one page: the size
34// (and gas) knob of the engine. A full Fine-hand page holds ~520 glyphs; the
35// glossed page's small gloss would hold ~700, so it gets a tighter cap that is
36// shared out evenly over its five blocks.
37const (
38 GlyphBudget = 560
39 GlossBudget = 440
40)
41
42// ParamsSchema is the Studio form: three optional overrides and a damage
43// switch. Studio sends JSON.stringify of the values in this order.
44const ParamsSchema = `{"type":"object","properties":{` +
45 `"script":{"type":"string","title":"Script","enum":["Any","Uncial","Lapidary","Cursive","Runic","Hanging"],"default":"Any"},` +
46 `"layout":{"type":"string","title":"Layout","enum":["Any","Single Column","Two Columns","Stele","Scroll","Glossed"],"default":"Any"},` +
47 `"vellum":{"type":"string","title":"Vellum","enum":["Any","Calfskin","Goatskin","Rag Paper","Purple"],"default":"Any"},` +
48 `"damage":{"type":"boolean","title":"Damage","default":true}}}`
49
50// Manifest describes the engine. Author and FeeBps are set by the adapter.
51func Manifest() engine.Manifest {
52 return engine.Manifest{
53 Name: "Rubrica",
54 Version: "1.0.0",
55 Description: "Every Rubrica invents an alphabet of nine to sixteen letters and writes one page in it, " +
56 "a manuscript, a stele, a scroll or a glossed text, with red ink marking where a reader should begin.",
57 License: "TBD",
58 ParamsSchema: ParamsSchema,
59 TraitTypes: append([]string{}, traitTypes...),
60 Living: false,
61 MaxSVGBytes: MaxSVGBytes,
62 }
63}
64
65// ---------------------------------------------------------------------------
66// Params: "" (the showcase look) or exactly the canonical Studio JSON
67// {"script":"…","layout":"…","vellum":"…","damage":true|false}.
68// ---------------------------------------------------------------------------
69
70// Params are the decoded collection params. "Any" keeps the seeded draw.
71type Params struct {
72 Script, Layout, Vellum string
73 Damage bool
74}
75
76var (
77 scriptParam = []string{"Any", "Uncial", "Lapidary", "Cursive", "Runic", "Hanging"}
78 layoutParam = []string{"Any", "Single Column", "Two Columns", "Stele", "Scroll", "Glossed"}
79 vellumParam = []string{"Any", "Calfskin", "Goatskin", "Rag Paper", "Purple"}
80 errParams = errors.New("rubrica: params must be \"\" or the canonical JSON")
81)
82
83// DefaultParams is what "" means.
84func DefaultParams() Params { return Params{"Any", "Any", "Any", true} }
85
86// ParseParams decodes "" or the canonical JSON and refuses anything else
87// (other key order, spaces, unknown keys or values, "{}").
88func ParseParams(s string) (Params, error) {
89 if s == "" {
90 return DefaultParams(), nil
91 }
92 var p Params
93 rest := s
94 lit := func(l string) bool {
95 if !strings.HasPrefix(rest, l) {
96 return false
97 }
98 rest = rest[len(l):]
99 return true
100 }
101 str := func(allowed []string) (string, bool) {
102 end := strings.IndexByte(rest, '"')
103 if end < 0 {
104 return "", false
105 }
106 v := rest[:end]
107 rest = rest[end:]
108 for _, a := range allowed {
109 if a == v {
110 return v, true
111 }
112 }
113 return "", false
114 }
115 ok := false
116 if !lit(`{"script":"`) {
117 return p, errParams
118 }
119 if p.Script, ok = str(scriptParam); !ok {
120 return p, errors.New("rubrica: invalid script")
121 }
122 if !lit(`","layout":"`) {
123 return p, errParams
124 }
125 if p.Layout, ok = str(layoutParam); !ok {
126 return p, errors.New("rubrica: invalid layout")
127 }
128 if !lit(`","vellum":"`) {
129 return p, errParams
130 }
131 if p.Vellum, ok = str(vellumParam); !ok {
132 return p, errors.New("rubrica: invalid vellum")
133 }
134 if !lit(`","damage":`) {
135 return p, errParams
136 }
137 switch rest {
138 case "true}":
139 p.Damage = true
140 case "false}":
141 p.Damage = false
142 default:
143 return p, errParams
144 }
145 return p, nil
146}
147
148// ValidateParams accepts "" and the canonical JSON only.
149func ValidateParams(params string) error {
150 _, err := ParseParams(params)
151 return err
152}
153
154func mustParams(s string) Params {
155 p, err := ParseParams(s)
156 if err != nil {
157 panic(err)
158 }
159 return p
160}
161
162// Traits returns the nine traits of in.Seed under in.Params, in TraitTypes
163// order. It never renders.
164func Traits(in engine.Input) []engine.Trait {
165 t := TraitsOf(in.Seed, in.Params)
166 return []engine.Trait{
167 {Type: "Script", Value: t.Script},
168 {Type: "Layout", Value: t.Layout},
169 {Type: "Hand", Value: t.Hand},
170 {Type: "Rubrication", Value: t.Rubrication},
171 {Type: "Vellum", Value: t.Vellum},
172 {Type: "Ink", Value: t.Ink},
173 {Type: "Damage", Value: t.Damage},
174 {Type: "Alphabet", Value: strconv.Itoa(t.Alphabet)},
175 {Type: "Rarity", Value: t.Rarity},
176 }
177}
178
179// Render returns the raw SVG for in.Seed under in.Params.
180func Render(in engine.Input) string { return SVG(in.Seed, in.Params) }
181
182// ---------------------------------------------------------------------------
183// Deterministic randomness: FNV-1a over the seed, then splitmix64 (Genesis).
184// ---------------------------------------------------------------------------
185
186const (
187 fnvOffset uint64 = 14695981039346656037
188 fnvPrime uint64 = 1099511628211
189)
190
191type rng struct{ s uint64 }
192
193func newRNG(seed string) *rng {
194 h := fnvOffset
195 for i := 0; i < len(seed); i++ {
196 h ^= uint64(seed[i])
197 h *= fnvPrime
198 }
199 if h == 0 {
200 h = fnvPrime
201 }
202 return &rng{s: h}
203}
204
205func (r *rng) next() uint64 {
206 r.s += 0x9E3779B97F4A7C15
207 z := r.s
208 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
209 z = (z ^ (z >> 27)) * 0x94D049BB133111EB
210 return z ^ (z >> 31)
211}
212
213// n returns a value in [0, k).
214func (r *rng) n(k int) int {
215 if k <= 0 {
216 return 0
217 }
218 return int(r.next() % uint64(k))
219}
220
221// rr returns a value in [a, b].
222func (r *rng) rr(a, b int) int { return a + r.n(b-a+1) }
223
224// ---------------------------------------------------------------------------
225// Trait tables. Weights read as percent; score feeds the rarity tier.
226// ---------------------------------------------------------------------------
227
228type option struct {
229 name string
230 weight int
231 score int
232 v int // Hand: glyph height
233}
234
235func pick(r *rng, table []option) option {
236 total := 0
237 for _, o := range table {
238 total += o.weight
239 }
240 roll := r.n(total)
241 acc := 0
242 for _, o := range table {
243 acc += o.weight
244 if roll < acc {
245 return o
246 }
247 }
248 return table[len(table)-1]
249}
250
251func byName(table []option, name string) option {
252 for _, o := range table {
253 if o.name == name {
254 return o
255 }
256 }
257 return table[0]
258}
259
260var (
261 scripts = []option{{"Uncial", 26, 0, 0}, {"Lapidary", 22, 0, 0}, {"Cursive", 22, 0, 0}, {"Runic", 18, 1, 0}, {"Hanging", 12, 2, 0}}
262 layouts = []option{{"Single Column", 30, 0, 0}, {"Two Columns", 26, 0, 0}, {"Stele", 16, 1, 0}, {"Scroll", 16, 1, 0}, {"Glossed", 12, 2, 0}}
263 hands = []option{{"Fine", 30, 0, 22}, {"Book", 45, 0, 30}, {"Display", 25, 0, 40}}
264 rubrics = []option{{"None", 18, 0, 0}, {"Initials", 32, 0, 0}, {"Rubrics", 28, 0, 0}, {"Full", 22, 1, 0}}
265 vellums = []option{{"Calfskin", 40, 0, 0}, {"Goatskin", 30, 0, 0}, {"Rag Paper", 24, 0, 0}, {"Purple", 6, 4, 0}}
266 inks = []option{{"Iron Gall", 40, 0, 0}, {"Carbon", 30, 0, 0}, {"Sepia", 22, 0, 0}, {"Lapis", 8, 2, 0}}
267 damages = []option{{"None", 62, 0, 0}, {"Wormholes", 16, 1, 0}, {"Water Stain", 14, 1, 0}, {"Torn Corner", 8, 2, 0}}
268 rulings = []option{{"None", 30, 0, 0}, {"Dry-point", 45, 0, 0}, {"Pricked", 25, 0, 0}}
269)
270
271// Rarity tier cut-offs on the score (10,000-seed simulation, default
272// params): Common 43% / Uncommon 26% / Rare 23% / Legendary 6% / Mythic 1.4%.
273const (
274 scoreMythic = 7
275 scoreLegendary = 5
276 scoreRare = 3
277 scoreUncommon = 2
278)
279
280// TraitSet is the decoded token. The SVG and the trait list both read it.
281type TraitSet struct {
282 Script, Layout, Hand, Rubrication string
283 Vellum, Ink, Damage string // as shown (Stone, Incised, Chrysography)
284 Alphabet int
285 Score int
286 Rarity string
287
288 ruling string
289 glyphH int
290 vellumRaw string // colour sources
291 inkRaw string
292}
293
294func derive(seed string, p Params) TraitSet {
295 r := newRNG(seed + "|traits")
296 // Draw order is part of the contract: every draw happens whatever the params.
297 script := pick(r, scripts)
298 layout := pick(r, layouts)
299 hand := pick(r, hands)
300 rub := pick(r, rubrics)
301 vellum := pick(r, vellums)
302 ink := pick(r, inks)
303 damage := pick(r, damages)
304 ruling := pick(r, rulings)
305 alphabet := 9 + r.n(8)
306
307 if p.Script != "Any" {
308 script = byName(scripts, p.Script)
309 }
310 if p.Layout != "Any" {
311 layout = byName(layouts, p.Layout)
312 }
313 if p.Vellum != "Any" {
314 vellum = byName(vellums, p.Vellum)
315 }
316 if !p.Damage {
317 damage = damages[0]
318 }
319
320 // What the page can actually show.
321 if layout.name == "Stele" {
322 if rub.name == "Initials" {
323 rub = rubrics[0]
324 }
325 if rub.name == "Full" {
326 rub = rubrics[2]
327 }
328 }
329 if (layout.name == "Stele" || layout.name == "Scroll") && damage.name == "Torn Corner" {
330 damage = damages[0]
331 }
332 t := TraitSet{
333 Script: script.name, Layout: layout.name, Hand: hand.name, Rubrication: rub.name,
334 Vellum: vellum.name, Ink: ink.name, Damage: damage.name, Alphabet: alphabet,
335 ruling: ruling.name, glyphH: hand.v, vellumRaw: vellum.name, inkRaw: ink.name,
336 }
337 score := script.score + layout.score + rub.score + damage.score
338 switch {
339 case layout.name == "Stele":
340 t.Vellum, t.Ink = "Stone", "Incised"
341 case vellum.name == "Purple":
342 t.Ink = "Chrysography"
343 score += vellum.score
344 default:
345 score += ink.score
346 }
347 if alphabet == 16 {
348 score++
349 }
350 t.Score = score
351 switch {
352 case score >= scoreMythic:
353 t.Rarity = "Mythic"
354 case score >= scoreLegendary:
355 t.Rarity = "Legendary"
356 case score >= scoreRare:
357 t.Rarity = "Rare"
358 case score >= scoreUncommon:
359 t.Rarity = "Uncommon"
360 default:
361 t.Rarity = "Common"
362 }
363 return t
364}
365
366// TraitsOf returns the decoded traits for a seed and params string; it
367// panics on invalid params (core validates them at CreateCollection).
368func TraitsOf(seed, params string) TraitSet { return derive(seed, mustParams(params)) }
369
370// TraitString is "Type=Value;…" in TraitTypes order (golden vectors).
371func TraitString(seed, params string) string {
372 ts := Traits(engine.Input{Seed: seed, Params: params})
373 var b strings.Builder
374 for i, t := range ts {
375 if i > 0 {
376 b.WriteByte(';')
377 }
378 b.WriteString(t.Type)
379 b.WriteByte('=')
380 b.WriteString(t.Value)
381 }
382 return b.String()
383}
384
385// ---------------------------------------------------------------------------
386// Art
387// ---------------------------------------------------------------------------
388
389const (
390 pageW = 800
391 pageH = 1100
392 red0 = "#b03a22"
393 ids = "abcdefghijklmnop"
394)
395
396var (
397 dirs8 = [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}
398 dirsRune = [][2]int{{0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}
399 // quarter cosine, x1024, 0..64 of a 256-step turn
400 cosQ = []int{0, 25, 50, 75, 100, 125, 150, 175, 200, 224, 249, 273, 297, 321, 345, 369, 392, 415, 438, 460, 483, 505, 526,
401 548, 569, 590, 610, 630, 650, 669, 688, 706, 724, 742, 759, 775, 792, 807, 822, 837, 851, 865, 878, 891, 903,
402 915, 926, 936, 946, 955, 964, 972, 980, 987, 993, 999, 1004, 1009, 1013, 1016, 1019, 1021, 1023, 1024, 1024}
403)
404
405func cos256(a int) int {
406 s := (a + 64) % 256
407 switch {
408 case s < 64:
409 return cosQ[s]
410 case s < 128:
411 return cosQ[128-s]
412 case s < 192:
413 return -cosQ[s-128]
414 default:
415 return -cosQ[256-s]
416 }
417}
418
419func vellumColor(name string) string {
420 switch name {
421 case "Goatskin":
422 return "#e6d3ac"
423 case "Rag Paper":
424 return "#f2ecdd"
425 case "Purple":
426 return "#3e1a33"
427 default: // Calfskin
428 return "#efe3c6"
429 }
430}
431
432func inkColor(name string) string {
433 switch name {
434 case "Carbon":
435 return "#1b1a19"
436 case "Sepia":
437 return "#5e4128"
438 case "Lapis":
439 return "#2a3f7a"
440 default: // Iron Gall
441 return "#3b2a1e"
442 }
443}
444
445func strokeDiv(script string) int {
446 switch script {
447 case "Lapidary":
448 return 7
449 case "Cursive":
450 return 10
451 case "Runic":
452 return 9
453 default: // Uncial, Hanging
454 return 8
455 }
456}
457
458const hexDigits = "0123456789abcdef"
459
460func hexVal(c byte) int {
461 switch {
462 case c >= '0' && c <= '9':
463 return int(c - '0')
464 case c >= 'a' && c <= 'f':
465 return int(c-'a') + 10
466 default:
467 return int(c-'A') + 10
468 }
469}
470
471// shade adds d to each channel of a #rrggbb colour, clamped to 0..255.
472func shade(c string, d int) string {
473 out := []byte{'#'}
474 for _, i := range []int{1, 3, 5} {
475 v := hexVal(c[i])*16 + hexVal(c[i+1]) + d
476 if v < 0 {
477 v = 0
478 }
479 if v > 255 {
480 v = 255
481 }
482 out = append(out, hexDigits[v/16], hexDigits[v%16])
483 }
484 return string(out)
485}
486
487func itoa(n int) string { return strconv.Itoa(n) }
488
489func maxInt(a, b int) int {
490 if a > b {
491 return a
492 }
493 return b
494}
495
496func makeGlyph(r *rng, script string, gw, gh int) string {
497 slant := script == "Cursive" // glyph-local skew: x shifts by -y/4
498 pt := func(gx, gy int) (int, int) {
499 y := gy*gh/3 - gh // baseline at y=0
500 x := gx * gw / 2
501 if slant {
502 x -= y / 4
503 }
504 return x, y
505 }
506 dirs := dirs8
507 if script == "Runic" {
508 dirs = dirsRune
509 }
510 var d strings.Builder
511 strokes := r.rr(2, 4)
512 for s := 0; s < strokes; s++ {
513 gx := r.n(3)
514 gy := 0
515 if !(script == "Hanging" && s == 0) {
516 gy = r.n(4)
517 }
518 path := [][2]int{{gx, gy}}
519 steps := r.rr(1, 3)
520 for st := 0; st < steps; st++ {
521 for tries := 0; tries < 8; tries++ {
522 dir := dirs[r.n(len(dirs))]
523 nx, ny := gx+dir[0], gy+dir[1]
524 if nx < 0 || nx > 2 || ny < 0 || ny > 3 {
525 continue
526 }
527 seen := false
528 for _, q := range path {
529 if q[0] == nx && q[1] == ny {
530 seen = true
531 break
532 }
533 }
534 if seen {
535 continue
536 }
537 gx, gy = nx, ny
538 path = append(path, [2]int{gx, gy})
539 break
540 }
541 }
542 if len(path) < 2 {
543 continue
544 }
545 px := make([]int, len(path))
546 py := make([]int, len(path))
547 for k, q := range path {
548 px[k], py[k] = pt(q[0], q[1])
549 }
550 last := len(path) - 1
551 d.WriteString("M" + itoa(px[0]) + " " + itoa(py[0]))
552 if (script == "Uncial" || script == "Cursive") && len(path) > 2 {
553 for k := 1; k < last; k++ {
554 mx := (px[k] + px[k+1]) / 2
555 my := (py[k] + py[k+1]) / 2
556 d.WriteString("Q" + itoa(px[k]) + " " + itoa(py[k]) + " " + itoa(mx) + " " + itoa(my))
557 }
558 d.WriteString("L" + itoa(px[last]) + " " + itoa(py[last]))
559 } else {
560 for k := 1; k <= last; k++ {
561 d.WriteString("L" + itoa(px[k]) + " " + itoa(py[k]))
562 }
563 }
564 }
565 if script != "Runic" && r.n(4) == 0 {
566 x, y := pt(r.n(3), 0)
567 d.WriteString("M" + itoa(x) + " " + itoa(y-gh/4) + "h0")
568 }
569 if d.Len() == 0 {
570 return "M0 0V" + itoa(-gh)
571 }
572 return d.String()
573}
574
575type block struct{ x0, y0, x1, y1, sc int }
576
577// SVG renders the page for a seed and params string as a standalone SVG.
578func SVG(seed, params string) string {
579 return svgOf(seed, derive(seed, mustParams(params)))
580}
581
582func svgOf(seed string, t TraitSet) string {
583 r := newRNG(seed + "|art")
584 script, layout, rub := t.Script, t.Layout, t.Rubrication
585 gh := t.glyphH
586 gw := gh * 2 / 3
587 vel := vellumColor(t.vellumRaw)
588 ink := inkColor(t.inkRaw)
589 red := red0
590 if layout == "Stele" {
591 vel = "#bdb5a6"
592 ink = "#4a4238"
593 } else if t.vellumRaw == "Purple" {
594 ink = "#d4af5a"
595 red = "#d8d4cc"
596 }
597 ng := t.Alphabet
598 caps := "round"
599 if script == "Lapidary" {
600 caps = "square"
601 } else if script == "Runic" {
602 caps = "butt"
603 }
604 sw := maxInt(2, gh/strokeDiv(script))
605
606 var b strings.Builder
607 b.WriteString(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 1100" width="800" height="1100"><defs>`)
608 for k := 0; k < ng; k++ {
609 b.WriteString(`<path id="` + ids[k:k+1] + `" d="` + makeGlyph(r, script, gw, gh) + `"/>`)
610 }
611 b.WriteString(`<radialGradient id="v"><stop offset=".6" stop-color="#000" stop-opacity="0"/><stop offset="1" stop-color="#3a2410" stop-opacity=".2"/></radialGradient></defs>`)
612 b.WriteString(`<rect width="800" height="1100" fill="#2b2420"/>`)
613
614 const px0, py0, px1, py1 = 40, 40, pageW - 40, pageH - 40
615 var page string
616 switch {
617 case layout == "Stele":
618 page = "M110 " + itoa(py1) + "V300A290 290 0 0 1 690 300V" + itoa(py1) + "Z"
619 case layout == "Scroll":
620 page = "M190 90H610V1010H190Z"
621 case t.Damage == "Torn Corner":
622 cut := r.rr(160, 260)
623 var pb strings.Builder
624 pb.WriteString("M" + itoa(px0) + " " + itoa(py0) + "H" + itoa(px1) + "V" + itoa(py1-cut) +
625 "L" + itoa(px1-4) + " " + itoa(py1-cut))
626 for k := 1; k < 6; k++ {
627 jx := px1 - cut*k/6 + r.rr(-14, 14)
628 jy := py1 - cut + cut*k/6 + r.rr(-14, 14)
629 pb.WriteString("L" + itoa(jx) + " " + itoa(jy))
630 }
631 pb.WriteString("L" + itoa(px1-cut) + " " + itoa(py1) + "H" + itoa(px0) + "Z")
632 page = pb.String()
633 default:
634 page = "M" + itoa(px0) + " " + itoa(py0) + "H" + itoa(px1) + "V" + itoa(py1) + "H" + itoa(px0) + "Z"
635 }
636 b.WriteString(`<path d="` + page + `" fill="` + vel + `"/>`)
637 if layout == "Scroll" {
638 for _, y := range []int{60, 1010} {
639 b.WriteString(`<rect x="160" y="` + itoa(y) + `" width="480" height="30" rx="15" fill="#6a4a2a"/><rect x="170" y="` +
640 itoa(y+6) + `" width="460" height="8" rx="4" fill="#9a7a4a"/>`)
641 }
642 }
643 if t.Damage == "Water Stain" {
644 cx := r.rr(250, 550)
645 cy := r.rr(300, 800)
646 rr := r.rr(140, 240)
647 b.WriteString(`<path d="M`)
648 for k := 0; k < 16; k++ {
649 ang := k * 16
650 rad := rr + r.rr(-30, 30)
651 if k > 0 {
652 b.WriteString("L")
653 }
654 b.WriteString(itoa(cx+rad*cos256(ang)/1024) + " " + itoa(cy+rad*cos256(ang+192)/1024))
655 }
656 b.WriteString(`Z" fill="` + shade(vel, -14) + `" stroke="` + shade(vel, -40) + `" stroke-width="3" opacity=".8"/>`)
657 }
658
659 var blocks []block
660 switch layout {
661 case "Single Column":
662 blocks = []block{{130, 130, 670, 920, 100}}
663 case "Two Columns":
664 blocks = []block{{110, 130, 385, 930, 100}, {415, 130, 690, 930, 100}}
665 case "Stele":
666 blocks = []block{{170, 330, 630, 980, 100}}
667 case "Scroll":
668 blocks = []block{{230, 150, 570, 960, 100}}
669 default: // Glossed: main text in the centre, a fine gloss around it
670 gs := maxInt(60, 1900/gh)
671 blocks = []block{{250, 300, 550, 700, 100}, {90, 120, 710, 280, gs}, {90, 300, 230, 960, gs},
672 {570, 300, 710, 960, gs}, {250, 720, 550, 960, gs}}
673 }
674
675 freqTotal := ng * (ng + 5) / 2 // sum of (ng + 2 - k), k = 0..ng-1
676
677 budget := GlyphBudget
678 if layout == "Glossed" {
679 budget = GlossBudget
680 }
681 var ruleD, prick strings.Builder
682 var linesOut strings.Builder
683 display := t.Hand == "Display"
684 for bi, bl := range blocks {
685 x0, y0, x1, y1, sc := bl.x0, bl.y0, bl.x1, bl.y1, bl.sc
686 g := gh
687 adv := gw * 5 / 4
688 lead := g * 150 / 100
689 if display {
690 lead = g * 135 / 100
691 }
692 ymax := y0 + (y1-y0)*100/sc
693 xmax := x0 + (x1-x0)*100/sc
694 box := 2*lead - 6
695 allow := budget / (len(blocks) - bi) // every block gets its share
696 left := allow
697 y := y0 + g
698 paraLeft, indLeft := 0, 0
699 var uses, reds, ligs strings.Builder
700 for y <= ymax {
701 if sc == 100 && t.ruling == "Dry-point" {
702 ruleD.WriteString("M" + itoa(x0-10) + " " + itoa(y) + "H" + itoa(x1+10))
703 }
704 if sc == 100 && t.ruling == "Pricked" {
705 prick.WriteString("M" + itoa(x1+40) + " " + itoa(y) + "h0")
706 }
707 first := paraLeft == 0
708 if first {
709 paraLeft = r.rr(3, 8)
710 }
711 x, xend := x0, xmax
712 if layout == "Stele" {
713 width := (x1 - x0) * r.rr(70, 100) / 100
714 x = x0 + (x1-x0-width)/2
715 xend = x + width
716 }
717 if first && (rub == "Initials" || rub == "Full") && sc == 100 && layout != "Stele" {
718 k := r.n(ng)
719 boxFill, glyphStroke := "none", red
720 if rub == "Full" {
721 boxFill, glyphStroke = "#2a3f7a", "#e8c46a"
722 }
723 reds.WriteString(`<rect x="` + itoa(x) + `" y="` + itoa(y-g-4) + `" width="` + itoa(box) + `" height="` + itoa(box) +
724 `" fill="` + boxFill + `" stroke="` + red + `" stroke-width="3"/>`)
725 reds.WriteString(`<use href="#` + ids[k:k+1] + `" transform="translate(` + itoa(x+box/5) + " " +
726 itoa(y-g-4+box*85/100) + `) scale(1.9)" stroke="` + glyphStroke + `"/>`)
727 indLeft = 2
728 }
729 if indLeft > 0 {
730 x += box + 10
731 indLeft--
732 }
733 limit := xend
734 if paraLeft == 1 {
735 limit = xend - (xend-x)*r.rr(30, 70)/100
736 }
737 redLine := first && (rub == "Rubrics" || rub == "Full")
738 if redLine && layout != "Stele" {
739 reds.WriteString(`<path d="M` + itoa(x+4) + " " + itoa(y-g/2) + `h0" stroke="` + red + `" stroke-width="` +
740 itoa(g/2+4) + `" stroke-linecap="round"/>`)
741 x += g
742 }
743 var line strings.Builder
744 for {
745 wl := r.rr(1, 7)
746 if x+wl*adv > limit || left < wl {
747 break
748 }
749 left -= wl
750 wx0 := x
751 for i := 0; i < wl; i++ {
752 roll := r.n(freqTotal)
753 acc, k := 0, ng-1
754 for j := 0; j < ng; j++ {
755 acc += ng + 2 - j
756 if roll < acc {
757 k = j
758 break
759 }
760 }
761 line.WriteString(`<use href="#` + ids[k:k+1] + `" x="` + itoa(x) + `"/>`)
762 x += adv
763 }
764 if script == "Cursive" {
765 ligs.WriteString("M" + itoa(wx0) + " " + itoa(y) + "H" + itoa(x-adv+gw/2))
766 }
767 if script == "Hanging" {
768 ligs.WriteString("M" + itoa(wx0-2) + " " + itoa(y-g) + "H" + itoa(x-adv+gw+2))
769 }
770 x += adv * 4 / 5
771 }
772 if line.Len() > 0 {
773 // one translate per line: each glyph then only needs x
774 if redLine {
775 reds.WriteString(`<g transform="translate(0 ` + itoa(y) + `)" stroke="` + red + `">` + line.String() + `</g>`)
776 } else {
777 uses.WriteString(`<g transform="translate(0 ` + itoa(y) + `)">` + line.String() + `</g>`)
778 }
779 }
780 paraLeft--
781 y += lead
782 }
783 budget -= allow - left
784 body := uses.String()
785 if ligs.Len() > 0 {
786 body += `<path d="` + ligs.String() + `" stroke-width="` + itoa(maxInt(1, sw-1)) + `"/>`
787 }
788 grp := `<g fill="none" stroke="` + ink + `" stroke-width="` + itoa(sw) + `" stroke-linecap="` + caps + `" stroke-linejoin="round">`
789 switch {
790 case layout == "Stele":
791 // the incised light edge reuses the text body instead of repeating it
792 linesOut.WriteString(`<g fill="none" stroke="#f1ece2" stroke-width="` + itoa(sw) + `" stroke-linecap="` + caps +
793 `" transform="translate(2 2)" opacity=".7"><g id="t">` + body + `</g></g>`)
794 linesOut.WriteString(grp + `<use href="#t"/>` + reds.String() + `</g>`)
795 case sc == 100:
796 linesOut.WriteString(grp + body + reds.String() + `</g>`)
797 default:
798 linesOut.WriteString(grp + `<g transform="translate(` + itoa(x0) + " " + itoa(y0) + ") scale(." + itoa(sc) +
799 ") translate(-" + itoa(x0) + " -" + itoa(y0) + `)">` + body + reds.String() + `</g></g>`)
800 }
801 }
802 if ruleD.Len() > 0 {
803 b.WriteString(`<path d="` + ruleD.String() + `" stroke="` + shade(vel, -30) + `" stroke-width="1" opacity=".6"/>`)
804 }
805 if prick.Len() > 0 {
806 b.WriteString(`<path d="` + prick.String() + `" stroke="` + shade(vel, -80) + `" stroke-width="4" stroke-linecap="round"/>`)
807 }
808 b.WriteString(linesOut.String())
809 if t.Damage == "Wormholes" {
810 b.WriteString(`<g fill="#2b2420">`)
811 n := r.rr(8, 20)
812 for i := 0; i < n; i++ {
813 cx := r.rr(80, 720)
814 cy := r.rr(80, 1020)
815 rad := r.rr(3, 8)
816 b.WriteString(`<circle cx="` + itoa(cx) + `" cy="` + itoa(cy) + `" r="` + itoa(rad) + `"/>`)
817 }
818 b.WriteString(`</g>`)
819 }
820 b.WriteString(`<path d="` + page + `" fill="url(#v)"/></svg>`)
821 return b.String()
822}