// Package rubrica (v2, "Living Language") is a Sinopia engine: every token // invents a language and writes one manuscript leaf in it. // // The language is an alphabet of 12-20 glyphs (strokes on a 3x4 node grid in // one of five hands) and a lexicon of 22-40 words built from it. Glyph use and // word use both follow a Zipf law and the most frequent words are the // shortest, so the text reads like writing rather than noise. The leaf is // laid out like a real codex page: justified lines, paragraphs closed by a // punctus, ink that fades until the scribe dips the pen again, a great // initial, lombards, rubrics and line fillers, a vine border or a full frame, // a marginal drawing, a running head, a catchword and the folio number, // which is the token number written in medieval numerals (f. xiiij v). // // A stateless p/ package implementing engine/v2. Pure integer maths (FNV-1a + // splitmix64 streams, truncating and floor division), so the TypeScript twin // (frontend/src/engines/rubricav2.ts) renders the same bytes. Traits come // from their own cheap stream (seed+"|traits") and never render; the art uses // seed+"|art", so art draws never shift traits. Words are defined once in // and placed with ; a word budget caps every leaf. // // The render depends on Input.TokenID (the folio number) and on nothing else // outside Seed and Params; it is not a living engine. package rubrica import ( "errors" "strconv" "strings" "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2" ) // TraitTypes in the order Traits returns them. var traitTypes = []string{"Hand", "Layout", "Decoration", "Vellum", "Marginalia", "Condition", "Alphabet", "Rarity"} // MaxSVGBytes: the measured maximum is 25,666 B over ~100,000 renders with // every forced hand/layout set (docs/engines/rubrica-v2.md); WordBudget // bounds the text. const MaxSVGBytes = 28672 // WordBudget caps the words placed on one leaf (text, gloss, palimpsest): the // size and gas knob. It binds only on the densest glossed leaves, whose last // gloss (or palimpsest) lines then stay short. const WordBudget = 500 // ParamsSchema is the Studio form: three optional overrides and a wear // switch. Studio sends JSON.stringify of the values in this order. const ParamsSchema = `{"type":"object","properties":{` + `"hand":{"type":"string","title":"Hand","enum":["Any","Angular","Round","Hooked","Dotted","Slanted"],"default":"Any"},` + `"layout":{"type":"string","title":"Layout","enum":["Any","Single Column","Two Columns","Glossed"],"default":"Any"},` + `"vellum":{"type":"string","title":"Vellum","enum":["Any","Calfskin","Goatskin","Rag Paper","Purple"],"default":"Any"},` + `"damage":{"type":"boolean","title":"Wear and damage","default":true}}}` // Manifest describes the engine. Author and FeeBps are set by the adapter. func Manifest() engine.Manifest { return engine.Manifest{ Name: "Rubrica v2 · Living Language", Version: "2.0.0", Description: "Every Rubrica invents a language, an alphabet of twelve to twenty letters and a lexicon of " + "its own words, and writes one leaf of a manuscript in it: justified lines, rubrics, lombards, " + "gilded initials, marginal drawings, and the token's number as the folio.", License: "CC-BY-NC-4.0", ParamsSchema: ParamsSchema, TraitTypes: append([]string{}, traitTypes...), Living: false, MaxSVGBytes: MaxSVGBytes, Kind: engine.KindSVG, } } // --------------------------------------------------------------------------- // Params: "" or exactly the canonical Studio JSON // {"hand":"…","layout":"…","vellum":"…","damage":true|false}. // --------------------------------------------------------------------------- // Params are the decoded collection params. "Any" keeps the seeded draw; // Damage false makes every leaf Fine. type Params struct { Hand, Layout, Vellum string Damage bool } var ( handParam = []string{"Any", "Angular", "Round", "Hooked", "Dotted", "Slanted"} layoutParam = []string{"Any", "Single Column", "Two Columns", "Glossed"} vellumParam = []string{"Any", "Calfskin", "Goatskin", "Rag Paper", "Purple"} errParams = errors.New("rubrica v2: params must be \"\" or the canonical JSON") ) // DefaultParams is what "" means. func DefaultParams() Params { return Params{"Any", "Any", "Any", true} } // ParseParams decodes "" or the canonical JSON and refuses anything else // (other key order, spaces, unknown keys or values, "{}"). func ParseParams(s string) (Params, error) { if s == "" { return DefaultParams(), nil } var p Params rest := s lit := func(l string) bool { if !strings.HasPrefix(rest, l) { return false } rest = rest[len(l):] return true } str := func(allowed []string) (string, bool) { end := strings.IndexByte(rest, '"') if end < 0 { return "", false } v := rest[:end] rest = rest[end:] for _, a := range allowed { if a == v { return v, true } } return "", false } ok := false if !lit(`{"hand":"`) { return p, errParams } if p.Hand, ok = str(handParam); !ok { return p, errors.New("rubrica v2: invalid hand") } if !lit(`","layout":"`) { return p, errParams } if p.Layout, ok = str(layoutParam); !ok { return p, errors.New("rubrica v2: invalid layout") } if !lit(`","vellum":"`) { return p, errParams } if p.Vellum, ok = str(vellumParam); !ok { return p, errors.New("rubrica v2: invalid vellum") } if !lit(`","damage":`) { return p, errParams } switch rest { case "true}": p.Damage = true case "false}": p.Damage = false default: return p, errParams } return p, nil } // ValidateParams accepts "" and the canonical JSON only. func ValidateParams(params string) error { _, err := ParseParams(params) return err } func mustParams(s string) Params { p, err := ParseParams(s) if err != nil { panic(err) } return p } // --------------------------------------------------------------------------- // Deterministic randomness: FNV-1a over the seed, then splitmix64 (as v1). // --------------------------------------------------------------------------- const ( fnvOffset uint64 = 14695981039346656037 fnvPrime uint64 = 1099511628211 ) type rng struct{ s uint64 } func newRNG(seed string) *rng { h := fnvOffset for i := 0; i < len(seed); i++ { h ^= uint64(seed[i]) h *= fnvPrime } if h == 0 { h = fnvPrime } return &rng{s: h} } // n returns a value in [0, k). It inlines next: a GnoVM call costs more // than the arithmetic, and the art draws thousands of these. func (r *rng) n(k int) int { if k <= 0 { return 0 } r.s += 0x9E3779B97F4A7C15 z := r.s z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9 z = (z ^ (z >> 27)) * 0x94D049BB133111EB return int((z ^ (z >> 31)) % uint64(k)) } // rr returns a value in [a, b]. func (r *rng) rr(a, b int) int { return a + r.n(b-a+1) } // w returns an index into a weight list. func (r *rng) w(weights []int) int { total := 0 for _, x := range weights { total += x } roll := r.n(total) for i, x := range weights { if roll < x { return i } roll -= x } return len(weights) - 1 } // cumulative turns positive weights into running sums for pick. func cumulative(weights []int) []int { cum := make([]int, len(weights)) acc := 0 for i, x := range weights { acc += x cum[i] = acc } return cum } // pick is w over running sums: the first index whose sum exceeds the roll, // found by bisection (the same index w's linear scan returns). func (r *rng) pick(cum []int) int { roll := r.n(cum[len(cum)-1]) lo, hi := 0, len(cum)-1 for lo < hi { mid := (lo + hi) / 2 if roll < cum[mid] { hi = mid } else { lo = mid + 1 } } return lo } // --------------------------------------------------------------------------- // Trait tables: names, weights (percent), rarity score. // --------------------------------------------------------------------------- type table struct { names []string weights []int scores []int } var ( hands = table{[]string{"Angular", "Round", "Hooked", "Dotted", "Slanted"}, []int{26, 24, 18, 16, 16}, []int{0, 0, 0, 0, 0}} layouts = table{[]string{"Single Column", "Two Columns", "Glossed"}, []int{40, 36, 24}, []int{0, 0, 2}} decos = table{[]string{"Plain", "Rubricated", "Lombards", "Gilded", "Vine Border", "Full Illumination"}, []int{20, 28, 22, 16, 9, 5}, []int{0, 0, 0, 1, 2, 4}} vellums = table{[]string{"Calfskin", "Goatskin", "Rag Paper", "Purple"}, []int{38, 28, 26, 8}, []int{0, 0, 0, 4}} inks = table{[]string{"Iron Gall", "Carbon", "Sepia", "Lapis"}, []int{40, 26, 24, 10}, []int{0, 0, 0, 0}} margs = table{[]string{"None", "Pointing Hand", "Snail", "Bird", "Flower"}, []int{44, 16, 14, 14, 12}, []int{0, 1, 1, 1, 1}} conds = table{[]string{"Fine", "Wormholes", "Water Stain", "Torn Corner", "Palimpsest", "Burnt Edge"}, []int{40, 18, 17, 12, 8, 5}, []int{0, 1, 1, 1, 3, 3}} ) // Rarity tier cut-offs on the score (10,000 SampleSeeds, default params): // Common 48.3% / Uncommon 30.7% / Rare 14.4% / Legendary 5.0% / Mythic 1.5%. const ( scoreMythic = 9 scoreLegendary = 7 scoreRare = 5 scoreUncommon = 3 ) func indexOf(t table, name string) int { for i, n := range t.names { if n == name { return i } } return 0 } // TraitSet is the decoded token: indexes into the tables above. The SVG and // the trait list both read it. type TraitSet struct { Hand, Layout, Decoration, Vellum, Ink, Marginalia, Condition int Alphabet, Lexicon int Score int Rarity string } func derive(seed string, p Params) TraitSet { r := newRNG(seed + "|traits") // Draw order is part of the contract: every draw happens whatever the params. var t TraitSet t.Hand = r.w(hands.weights) t.Layout = r.w(layouts.weights) t.Decoration = r.w(decos.weights) t.Vellum = r.w(vellums.weights) t.Ink = r.w(inks.weights) t.Marginalia = r.w(margs.weights) t.Condition = r.w(conds.weights) t.Alphabet = r.rr(12, 20) t.Lexicon = r.rr(22, 40) if p.Hand != "Any" { t.Hand = indexOf(hands, p.Hand) } if p.Layout != "Any" { t.Layout = indexOf(layouts, p.Layout) } if p.Vellum != "Any" { t.Vellum = indexOf(vellums, p.Vellum) } if !p.Damage { t.Condition = 0 } score := hands.scores[t.Hand] + layouts.scores[t.Layout] + decos.scores[t.Decoration] + vellums.scores[t.Vellum] + margs.scores[t.Marginalia] + conds.scores[t.Condition] if t.Alphabet >= 19 { score++ } t.Score = score switch { case score >= scoreMythic: t.Rarity = "Mythic" case score >= scoreLegendary: t.Rarity = "Legendary" case score >= scoreRare: t.Rarity = "Rare" case score >= scoreUncommon: t.Rarity = "Uncommon" default: t.Rarity = "Common" } return t } // TraitsOf returns the decoded traits for a seed and params string; it // panics on invalid params (core validates them at CreateCollection). func TraitsOf(seed, params string) TraitSet { return derive(seed, mustParams(params)) } func traitList(t TraitSet) []engine.Trait { return []engine.Trait{ {Type: "Hand", Value: hands.names[t.Hand]}, {Type: "Layout", Value: layouts.names[t.Layout]}, {Type: "Decoration", Value: decos.names[t.Decoration]}, {Type: "Vellum", Value: vellums.names[t.Vellum]}, {Type: "Marginalia", Value: margs.names[t.Marginalia]}, {Type: "Condition", Value: conds.names[t.Condition]}, {Type: "Alphabet", Value: strconv.Itoa(t.Alphabet) + " letters"}, {Type: "Rarity", Value: t.Rarity}, } } // Traits returns the eight traits of in.Seed under in.Params, in TraitTypes // order. It never renders. func Traits(in engine.Input) []engine.Trait { return traitList(TraitsOf(in.Seed, in.Params)) } // Render returns the raw SVG for in.Seed under in.Params; in.TokenID is the // folio number. func Render(in engine.Input) string { return SVG(in.Seed, in.Params, in.TokenID) } // TraitString is "Type=Value;…" in TraitTypes order (golden vectors). func TraitString(seed, params string) string { var b strings.Builder for i, t := range traitList(TraitsOf(seed, params)) { if i > 0 { b.WriteByte(';') } b.WriteString(t.Type) b.WriteByte('=') b.WriteString(t.Value) } return b.String() } // --------------------------------------------------------------------------- // Art // --------------------------------------------------------------------------- const ( hAngular = 0 hRound = 1 hHooked = 2 hDotted = 3 hSlanted = 4 lSingle = 0 lTwo = 1 lGlossed = 2 dPlain = 0 dRubricated = 1 dLombards = 2 dGilded = 3 dVine = 4 dFull = 5 vGoat = 1 vRag = 2 vPurple = 3 mNone = 0 mHand = 1 mSnail = 2 mBird = 3 mFlower = 4 cWorm = 1 cStain = 2 cTorn = 3 cPalimpsest = 4 cBurnt = 5 goldInk = "#e2bd62" green = "#4f7a3a" dark = "#2a221d" adv = 11 // glyph advance inside a word gap = 7 // word space tries = 3 // draws a scribe tries before ending a line maxGap = 9 // justification never widens a word space by more than this // one-letter ids of the lexicon's words (Lexicon <= 40 never reaches "v", // the texture filter) wid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn" ) var ( pageLight = []string{"#efe1c2", "#eadbb9", "#f2ebda", "#4e2346"} pageEdge = []string{"#cfb489", "#c8ad80", "#d6c8a8", "#2a0f26"} inkColor = []string{"#3a2a1c", "#1e1b18", "#5c3c22", "#1f3566"} gx3 = []int{0, 4, 8} gy4 = []int{-12, -8, -4, 0} qx3 = []int{-2, 4, 10} qy3 = []int{-14, -6, 2} stemEnd = []int{0, 0, -4} dotY = []int{-16, 4} lenWeights = []int{5, 14, 20, 20, 16, 11, 8} // word length 1..7 romanVals = []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1} romanSyms = []string{"m", "cm", "d", "cd", "c", "xc", "l", "xl", "x", "ix", "v", "iv", "i"} ) // pairs holds "00".."99"; itoa slices it instead of calling strconv, whose // formatting loop is the most expensive thing an SVG writer does in GnoVM. const pairs = "00010203040506070809101112131415161718192021222324252627282930313233343536373839" + "40414243444546474849505152535455565758596061626364656667686970717273747576777879" + "8081828384858687888990919293949596979899" func itoa(n int) string { switch { case n < 0: return "-" + itoa(-n) case n < 10: return pairs[2*n+1 : 2*n+2] case n < 100: return pairs[2*n : 2*n+2] case n < 1000: q, m := n/100, n%100 return pairs[2*q+1:2*q+2] + pairs[2*m:2*m+2] } return strconv.Itoa(n) } func floorDiv(a, b int) int { q := a / b if a%b != 0 && (a < 0) != (b < 0) { q-- } return q } // RomanFolio writes n in medieval lower-case roman numerals; a final i is // written j (iij, viij). func RomanFolio(n int) string { s := "" for i, v := range romanVals { for n >= v { s += romanSyms[i] n -= v } } if len(s) >= 2 && strings.HasSuffix(s, "ii") { s = s[:len(s)-1] + "j" } return s } // FolioNumber reads Input.TokenID: 1..99999, else 0 (sample, Studio). func FolioNumber(tokenID string) int { if len(tokenID) == 0 || len(tokenID) > 5 { return 0 } n := 0 for i := 0; i < len(tokenID); i++ { c := tokenID[i] if c < '0' || c > '9' { return 0 } n = n*10 + int(c-'0') } return n } // part is one stroke of a glyph, points already slanted. // k: 0 line a-b, 1 line a-c-b, 2 quad a(c)b, 3 hook at a, 4 dot at a. type part struct { k int p [6]int } // sp is the separator before y in "x y": none before a minus sign. func sp(y int) string { if y < 0 { return "" } return " " } // glyphPath is the path data of a glyph shifted right by ox. func glyphPath(parts []part, ox int) string { d := "" for _, q := range parts { p := q.p d += "M" + itoa(p[0]+ox) + sp(p[1]) + itoa(p[1]) switch q.k { case 0: d += "L" + itoa(p[2]+ox) + sp(p[3]) + itoa(p[3]) case 1: d += "L" + itoa(p[4]+ox) + sp(p[5]) + itoa(p[5]) + "L" + itoa(p[2]+ox) + sp(p[3]) + itoa(p[3]) case 2: d += "Q" + itoa(p[4]+ox) + sp(p[5]) + itoa(p[5]) + " " + itoa(p[2]+ox) + sp(p[3]) + itoa(p[3]) case 3: d += "l3 2" default: d += "h.5" } } return d } // opac is the opacity attribute of an ink level 70..100 (100: none). func opac(v int) string { if v >= 100 { return "" } return ` opacity=".` + itoa(v) + `"` } type column struct { x, y int // page x of the column, page y of its first baseline w, n int // width in text units, lines s int // scale x100 ss string } // leaf holds what the layout helpers share. type leaf struct { lex [][]int wl []int // drawn width of each word budget int } // justify returns the final x of each word of a line: justified to width // (the gap left over is spread over the word spaces) unless ragged (a // paragraph's last line). func (l *leaf) justify(words, xs []int, width int, ragged bool) []int { n := len(words) extra := 0 if !ragged && n > 1 { extra = width - (xs[n-1] + l.wl[words[n-1]]) if extra < 0 || extra > maxGap*(n-1) { extra = 0 } } px := make([]int, n) for i := 0; i < n; i++ { px[i] = xs[i] if extra > 0 { px[i] += extra * i / (n - 1) } } return px } // uses is the elements for words[from:to] at px. func uses(words, px []int, from, to int) string { out := "" for i := from; i < to; i++ { w := words[i] if px[i] == 0 { out += `` } else { out += `` } } return out } func leafPath(x, y, s int) string { return "M" + itoa(x) + " " + itoa(y) + "q" + itoa(6*s) + "-9 " + itoa(18*s) + "-3q" + itoa(-7*s) + " 8 " + itoa(-18*s) + " 3z" } // SVG renders the leaf for a seed, params string and token id ("" or "0": // the folio number is drawn from the seed). func SVG(seed, params, tokenID string) string { return svgOf(seed, derive(seed, mustParams(params)), tokenID) } func svgOf(seed string, t TraitSet, tokenID string) string { R := newRNG(seed + "|art") A := t.Alphabet V := t.Lexicon hand := t.Hand purple := t.Vellum == vPurple page := pageLight[t.Vellum] edge := pageEdge[t.Vellum] ink := inkColor[t.Ink] red, blue := "#ae3328", "#2a4f8a" if purple { ink, red, blue = goldInk, "#e8e2d8", "#9fc2e8" } deco := t.Decoration gilded := deco >= dGilded hasLomb := deco == dLombards || deco >= dGilded hasHead := deco == dRubricated || deco == dVine || deco == dFull usesGold := gilded || t.Marginalia == mBird || t.Marginalia == mFlower filterSeed := R.rr(1, 900) // --- the alphabet --- sk := func(y int) int { if hand == hSlanted { return floorDiv(5-3*y, 10) } return 0 } glyphs := make([][]part, A) for g := 0; g < A; g++ { var parts []part ns := R.rr(2, 3) for s := 0; s < ns; s++ { var ax, ay, bx, by int if s == 0 { ax = gx3[R.n(3)] ay = -12 if R.n(5) == 0 { ay = -16 // ascender } bx = gx3[R.n(3)] by = stemEnd[R.n(3)] if R.n(6) == 0 { by = 4 // descender } } else { ai := R.n(12) bi := R.n(12) for bi == ai { bi = R.n(12) } ax, ay = gx3[ai%3], gy4[ai/3] bx, by = gx3[bi%3], gy4[bi/3] } q := part{k: 0, p: [6]int{ax + sk(ay), ay, bx + sk(by), by, 0, 0}} if hand == hRound && R.n(100) < 70 { cy := qy3[R.n(3)] cx := qx3[R.n(3)] q.k, q.p[4], q.p[5] = 2, cx+sk(cy), cy } else if hand == hAngular && R.n(100) < 50 { cx := gx3[R.n(3)] cy := gy4[R.n(4)] q.k, q.p[4], q.p[5] = 1, cx+sk(cy), cy } parts = append(parts, q) } if hand == hHooked { e := parts[0].p parts = append(parts, part{k: 3, p: [6]int{e[2], e[3], 0, 0, 0, 0}}) } if hand == hDotted && R.n(100) < 55 { dx := gx3[R.n(3)] dy := dotY[R.n(2)] parts = append(parts, part{k: 4, p: [6]int{dx + sk(dy), dy, 0, 0, 0, 0}}) } glyphs[g] = parts } // --- the lexicon: Zipf-weighted glyphs, frequent words shortest --- gw := make([]int, A) for i := 0; i < A; i++ { gw[i] = 600 / (i + 1) } gcum := cumulative(gw) lf := &leaf{lex: make([][]int, V), wl: make([]int, V), budget: WordBudget} for w := 0; w < V; w++ { n := 1 + R.w(lenWeights) word := make([]int, n) for i := 0; i < n; i++ { word[i] = R.pick(gcum) } lf.lex[w] = word } // stable insertion sort by length for i := 1; i < V; i++ { for j := i; j > 0 && len(lf.lex[j]) < len(lf.lex[j-1]); j-- { lf.lex[j], lf.lex[j-1] = lf.lex[j-1], lf.lex[j] } } ww := make([]int, V) for i := 0; i < V; i++ { ww[i] = 1000 / (i + 2) lf.wl[i] = len(lf.lex[i])*adv - 3 } wcum := cumulative(ww) wl := lf.wl var defs strings.Builder defs.WriteString("") for i := 0; i < V; i++ { d := "" for k, g := range lf.lex[i] { d += glyphPath(glyphs[g], k*adv) } defs.WriteString(``) } // --- the leaf --- defs.WriteString(``) defs.WriteString(``) if usesGold { defs.WriteString(`` + ``) } pageD := "M16 14H584V806H16z" if t.Condition == cTorn { var pb strings.Builder pb.WriteString("M16 14H584V656") x, y := 584, 656 for y < 806 && x > 414 { x -= R.rr(8, 20) y += R.rr(8, 22) yc := y if yc > 806 { yc = 806 } pb.WriteString("L" + itoa(x) + " " + itoa(yc)) } pb.WriteString("L" + itoa(x) + " 806H16z") pageD = pb.String() } defs.WriteString(``) if t.Vellum == vGoat { defs.WriteString(`` + ``) } else if t.Vellum == vRag { defs.WriteString(`` + ``) } if gilded { defs.WriteString(``) } body := `` if t.Vellum == vGoat || t.Vellum == vRag { body += `` } body += `` // --- the text block --- var cols []column switch t.Layout { case lSingle: cols = []column{{78, 118, 360, 24, 115, "1.15"}} case lTwo: cols = []column{{70, 116, 190, 26, 100, ""}, {282, 116, 190, 26, 100, ""}} default: cols = []column{{182, 126, 210, 23, 120, "1.2"}} } var ruling, fill, textOut strings.Builder var lombards []int // x, y pairs para, dip, firstWord := 0, 100, -1 sw := "1.35" if hand == hAngular { sw = "1.5" } for ci, c := range cols { lh := c.s / 5 // 20 px x scale right := c.x + c.w*c.s/100 bottom := c.y + (c.n-1)*lh ruling.WriteString("M" + itoa(c.x) + " " + itoa(c.y-14) + "V" + itoa(bottom+6) + "M" + itoa(right) + " " + itoa(c.y-14) + "V" + itoa(bottom+6)) ox := c.x * 100 / c.s var inkLines, redLines, puncts strings.Builder for L := 0; L < c.n; L++ { yy := c.y + L*lh uy := yy * 100 / c.s ruling.WriteString("M" + itoa(c.x) + " " + itoa(yy) + "h" + itoa(right-c.x)) x := 0 if ci == 0 && L*lh < 64 { x = 66 * 100 / c.s } start := ci == 0 && L == 0 lineRed := false if start { para = R.rr(4, 7) if hasHead { lineRed = true } } else if para == 0 { para = R.rr(4, 8) if hasLomb { lombards = append(lombards, c.x+x*c.s/100, yy) x += 22 * 100 / c.s } if hasHead { lineRed = true } } last := para == 1 lineW := c.w if last { lineW = c.w * R.rr(35, 80) / 100 } var words, xs []int for miss := 0; miss < tries && lf.budget > 0; { wi := R.pick(wcum) if x+wl[wi] > lineW { miss++ continue } lf.budget-- words = append(words, wi) xs = append(xs, x) x += wl[wi] + gap } if len(words) > 0 { n := len(words) px := lf.justify(words, xs, lineW, last) // a punctus ends some words and every paragraph for i := 0; i < n; i++ { if (last && i == n-1) || (i < n-1 && R.n(9) == 0) { puncts.WriteString("M" + itoa(ox+px[i]+wl[words[i]]+3) + " " + itoa(uy) + "h.1") } } if firstWord < 0 { firstWord = words[0] } tr := `" + uses(words, px, 0, 1) + "") if n > 1 { inkLines.WriteString(tr + opac(dip) + ">" + uses(words, px, 1, n) + "") } case lineRed: redLines.WriteString(tr + ">" + uses(words, px, 0, n) + "") default: inkLines.WriteString(tr + opac(dip) + ">" + uses(words, px, 0, n) + "") } dip -= R.rr(1, 4) if dip < 70 { dip = 100 } } if last && hasHead && x < lineW { // line filler after the paragraph's last word fx := c.x + (x+4)*c.s/100 if right-fx > 12 { fill.WriteString("M" + itoa(fx) + " " + itoa(yy-4) + "H" + itoa(right)) } } para-- } scale := "" if c.ss != "" { scale = ` transform="scale(` + c.ss + `)"` } textOut.WriteString("`) textOut.WriteString(inkLines.String()) if puncts.Len() > 0 { textOut.WriteString(``) } textOut.WriteString("") if redLines.Len() > 0 { textOut.WriteString("`) textOut.WriteString(redLines.String()) textOut.WriteString("") } } // --- gloss around a glossed text --- gloss := "" if t.Layout == lGlossed { var gl strings.Builder for side := 0; side < 2; side++ { gx, lw := 70, 150 if side == 1 { gx, lw = 448, 165 } gox := gx * 100 / 62 y := 128 + R.rr(0, 40) for b := 0; b < 3 && y < 700; b++ { nl := R.rr(4, 9) for L := 0; L < nl && y < 716; L++ { var words, xs []int x := 0 for miss := 0; miss < tries && lf.budget > 0; { wi := R.pick(wcum) if x+wl[wi] > lw { miss++ continue } lf.budget-- words = append(words, wi) xs = append(xs, x) x += wl[wi] + 6 } if len(words) > 0 { gl.WriteString(`` + uses(words, lf.justify(words, xs, lw, L == nl-1), 0, len(words)) + "") } y += 12 } y += R.rr(20, 70) } } gloss = `` + gl.String() + "" } // --- lombards: alternating red and blue, each with a pen flourish in the other colour --- var lomb, lombPen strings.Builder lc := R.n(2) for i := 0; i < len(lombards); i += 2 { lx, ly := lombards[i], lombards[i+1] g := R.n(A) col, pen := red, blue if lc != 0 { col, pen = blue, red } lomb.WriteString(``) lombPen.WriteString(``) lc = 1 - lc } lombOut := "" if lomb.Len() > 0 { lombOut = `` + lombPen.String() + `` + lomb.String() + "" } fillOut := "" if fill.Len() > 0 { fillOut = `` } // --- the great initial --- c0 := cols[0] bx := c0.x - 2 by := c0.y - 18 const bs = 62 if firstWord < 0 { firstWord = 0 } ig := glyphPath(glyphs[lf.lex[firstWord][0]], 0) ini := "" if gilded { var dots strings.Builder for i := 0; i < 6; i++ { dots.WriteString("M" + itoa(bx+11+i*8) + " " + itoa(by+bs-9) + "h.1") } ini = `` + `` + `` + `` + `` } else { letter, penc := red, blue if deco == dLombards { letter, penc = blue, red } if deco == dPlain { ini = `` } else { // pen-flourished initial: a hairline frame, a dotted halo and a tail down the margin ini = `` + `` + `` } ini += `` } // --- vine border (and the full frame) --- if deco == dVine || deco == dFull { // beside the text from the initial down; on a glossed leaf outside the gloss, from the top glossed := t.Layout == lGlossed vx, vy, vx0 := c0.x-30, by+bs, bx+4 if glossed { vx, vy, vx0 = 44, 100, 44 } var d, leafA, leafB, golds strings.Builder d.WriteString("M" + itoa(vx0) + " " + itoa(vy) + "Q" + itoa(vx-10) + " " + itoa(vy+20) + " " + itoa(vx) + " " + itoa(vy+50)) y := vy + 50 side := 1 for y < 700 { ny := y + 56 d.WriteString("T" + itoa(vx) + " " + itoa(ny)) if side > 0 { leafA.WriteString(leafPath(vx, y+28, side)) } else { leafB.WriteString(leafPath(vx, y+28, side)) } golds.WriteString("M" + itoa(vx-side*11) + " " + itoa(y+30) + "h.1") side = -side y = ny } frame := "" if deco == dFull { cl := cols[len(cols)-1] tx, top := cl.x+cl.w*cl.s/100+24, bx+30 if glossed { tx, top = 556, vx } d.WriteString("Q" + itoa(vx) + " 748 " + itoa(vx+60) + " 748H" + itoa(tx) + "V84H" + itoa(top)) for x := vx + 70; x < tx; x += 48 { if side > 0 { leafB.WriteString(leafPath(x, 748, side)) } else { leafA.WriteString(leafPath(x-18, 748, side)) } golds.WriteString("M" + itoa(x+24) + " 748h.1") side = -side } frame = `` + `` } ini = frame + `` + ini } // --- ruling, prickings, running head, catchword, folio --- ruleColor := "#8a6a48" if purple { ruleColor = goldInk } rule := `` var pr strings.Builder for y := 110; y < 720; y += 40 { pr.WriteString("M566 " + itoa(y) + "h.1") } pricks := `` var hw [3]int hwTotal := 0 for i := 0; i < 3; i++ { hw[i] = R.n(V) hwTotal += wl[hw[i]] if i > 0 { hwTotal += 12 } } hx := 375 - hwTotal/2 // centred on x = 300 at scale .8 var head strings.Builder head.WriteString(``) for i := 0; i < 3; i++ { head.WriteString(``) hx += wl[hw[i]] + 12 } head.WriteString("") cw := R.n(V) cl := wl[cw] catchword := `` fallback := R.rr(1, 240) sideMark := "v" if R.n(2) == 0 { sideMark = "r" } fn := FolioNumber(tokenID) if fn == 0 { fn = fallback } folio := `f. ` + RomanFolio(fn) + `` + sideMark + "" // --- marginal drawing --- my := R.rr(260, 600) bxm := R.rr(200, 400) mg := "" if t.Marginalia != mNone { mx, yy := 522, my if t.Layout == lGlossed || deco == dFull { // bas-de-page, standing on y = 748 mx = bxm foot := 12 // lower on a bare page if deco == dFull { foot = 0 // on the frame's lower bar } switch t.Marginalia { case mHand: yy = foot + 739 case mSnail: yy = foot + 738 case mBird: yy = foot + 726 default: yy = foot + 708 } } at := `` + `` case mSnail: shell := blue if R.n(2) == 0 { shell = red } body2 := "#c9c79a" if purple { body2 = "#6f6a4a" } mg = at + ` stroke-width="1.3">` case mBird: mg = at + ` stroke-width="1.2">` default: mg = at + ` stroke-width="1.1">` } } // --- condition --- dm, pal := "", "" switch t.Condition { case cWorm: x := R.rr(120, 460) y := R.rr(150, 650) n := R.rr(5, 11) var holes strings.Builder for i := 0; i < n; i++ { r := R.rr(2, 5) holes.WriteString(``) x += R.rr(-30, 30) y += R.rr(10, 40) } dm = `` + holes.String() + "" case cStain: defs.WriteString(`` + ``) cx := R.rr(100, 500) cy := R.rr(600, 760) rx := R.rr(140, 220) ry := R.rr(90, 150) rot := R.rr(-20, 20) dm = `` case cBurnt: defs.WriteString(`` + `` + ``) dm = `` case cPalimpsest: // the scraped older text, written across the leaf var u strings.Builder for L := 0; L < 9; L++ { var words, xs []int x := 0 for lf.budget > 0 { wi := R.n(V) if x+wl[wi] > 400 { break } lf.budget-- words = append(words, wi) xs = append(xs, x) x += wl[wi] + 10 } u.WriteString(`` + uses(words, lf.justify(words, xs, 400, false), 0, len(words)) + "") } pal = `` + u.String() + "" } var out strings.Builder out.WriteString(``) out.WriteString(defs.String()) out.WriteString("") out.WriteString(body) out.WriteString(``) out.WriteString(pal) out.WriteString(rule) out.WriteString(pricks) out.WriteString(textOut.String()) out.WriteString(gloss) out.WriteString(fillOut) out.WriteString(lombOut) out.WriteString(ini) out.WriteString(head.String()) out.WriteString(catchword) out.WriteString(folio) out.WriteString(mg) out.WriteString(dm) out.WriteString("") return out.String() }