Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

demo.gno

// Code generated from the Katman kit source (docs/engines/katman.md, "Demo kit"). The TS twin
// (frontend/src/engines/katman.ts) holds the same literals; katman_test.gno pins both.

package katman

// demoLayers is the demo kit (params store 0): layer name, then part name, weight and
// inner SVG ("" = the layer is empty for this part). Layer order is z-order, bottom first.
var demoLayers = []layer{
	{"Ground", []part{
		{"Sun Disc", 30, `<circle cx="200" cy="200" r="152" fill="currentColor"/><circle cx="200" cy="200" r="136" fill="none" stroke="#fff" stroke-opacity=".3" stroke-width="3" stroke-dasharray="2 9" stroke-linecap="round"/>`},
		{"Bands", 25, `<path d="M0 58h400v40H0zM0 168h400v40H0zM0 278h400v40H0z" fill="currentColor"/><path d="M0 116h400M0 226h400M0 336h400" stroke="currentColor" stroke-width="3" stroke-opacity=".5"/>`},
		{"Arch", 25, `<path d="M64 400V214a136 136 0 0 1 272 0v186z" fill="currentColor"/><path d="M88 400V218a112 112 0 0 1 224 0v182" fill="none" stroke="#fff" stroke-opacity=".3" stroke-width="3"/>`},
		{"Polka", 12, `<g fill="currentColor"><circle cx="50" cy="50" r="10"/><circle cx="150" cy="50" r="10"/><circle cx="250" cy="50" r="10"/><circle cx="350" cy="50" r="10"/><circle cx="50" cy="150" r="10"/><circle cx="150" cy="150" r="10"/><circle cx="250" cy="150" r="10"/><circle cx="350" cy="150" r="10"/><circle cx="50" cy="250" r="10"/><circle cx="150" cy="250" r="10"/><circle cx="250" cy="250" r="10"/><circle cx="350" cy="250" r="10"/><circle cx="50" cy="350" r="10"/><circle cx="150" cy="350" r="10"/><circle cx="250" cy="350" r="10"/><circle cx="350" cy="350" r="10"/><circle cx="100" cy="100" r="6"/><circle cx="200" cy="100" r="6"/><circle cx="300" cy="100" r="6"/><circle cx="100" cy="200" r="6"/><circle cx="200" cy="200" r="6"/><circle cx="300" cy="200" r="6"/><circle cx="100" cy="300" r="6"/><circle cx="200" cy="300" r="6"/><circle cx="300" cy="300" r="6"/></g>`},
		{"Plain", 8, ``},
	}},
	{"Body", []part{
		{"Jar", 35, `<ellipse cx="200" cy="374" rx="112" ry="11" fill="#2a1d17" fill-opacity=".16"/><path d="M150 130h100v26q72 36 64 124q-8 84-114 84T86 280q-8-88 64-124z" fill="currentColor" stroke="#2a1d17" stroke-width="6" stroke-linejoin="round"/><path d="M150 156h100" stroke="#2a1d17" stroke-width="5"/><path d="M112 262q4-56 50-86" fill="none" stroke="#fff" stroke-opacity=".45" stroke-width="10" stroke-linecap="round"/>`},
		{"Amphora", 25, `<ellipse cx="200" cy="392" rx="64" ry="11" fill="#2a1d17" fill-opacity=".16"/><path d="M170 96h60v42q64 40 54 140q-8 72-84 112q-76-40-84-112q-10-100 54-140z" fill="currentColor" stroke="#2a1d17" stroke-width="6" stroke-linejoin="round"/><path d="M170 138h60" stroke="#2a1d17" stroke-width="5"/><path d="M138 268q0-62 40-102" fill="none" stroke="#fff" stroke-opacity=".45" stroke-width="10" stroke-linecap="round"/>`},
		{"Pot", 25, `<ellipse cx="200" cy="366" rx="116" ry="11" fill="#2a1d17" fill-opacity=".16"/><path d="M130 160h140q66 54 46 140q-18 58-116 58T84 300q-20-86 46-140z" fill="currentColor" stroke="#2a1d17" stroke-width="6" stroke-linejoin="round"/><ellipse cx="200" cy="160" rx="74" ry="9" fill="currentColor" stroke="#2a1d17" stroke-width="5"/><path d="M106 286q-2-58 40-104" fill="none" stroke="#fff" stroke-opacity=".45" stroke-width="10" stroke-linecap="round"/>`},
		{"Pitcher", 15, `<ellipse cx="200" cy="374" rx="104" ry="11" fill="#2a1d17" fill-opacity=".16"/><path d="M242 120l30-16-10 34" fill="currentColor" stroke="#2a1d17" stroke-width="6" stroke-linejoin="round"/><path d="M158 118h84l-8 34q78 38 72 128q-6 84-106 86q-100-2-106-86q-6-90 72-128z" fill="currentColor" stroke="#2a1d17" stroke-width="6" stroke-linejoin="round"/><path d="M114 270q0-60 48-100" fill="none" stroke="#fff" stroke-opacity=".45" stroke-width="10" stroke-linecap="round"/>`},
	}},
	{"Glaze", []part{
		{"Zigzag", 30, `<path d="M136 302l22-16 22 16 22-16 22 16 22-16 22 16" fill="none" stroke="currentColor" stroke-width="8" stroke-linejoin="round" stroke-linecap="round"/>`},
		{"Dots", 28, `<path d="M148 300h.1M174 316h.1M200 300h.1M226 316h.1M252 300h.1" stroke="currentColor" stroke-width="16" stroke-linecap="round"/>`},
		{"Wave", 22, `<path d="M136 306q16-18 32 0t32 0 32 0 32 0" fill="none" stroke="currentColor" stroke-width="9" stroke-linecap="round"/>`},
		{"Twin Bands", 12, `<path d="M140 292h120M140 320h120" stroke="currentColor" stroke-width="7" stroke-linecap="round"/>`},
		{"Bare", 8, ``},
	}},
	{"Face", []part{
		{"Happy", 30, `<g fill="#e07a5f" fill-opacity=".45"><ellipse cx="154" cy="244" rx="12" ry="7"/><ellipse cx="246" cy="244" rx="12" ry="7"/></g><g fill="#2a1d17"><circle cx="172" cy="220" r="8"/><circle cx="228" cy="220" r="8"/></g><path d="M178 248q22 20 44 0" fill="none" stroke="#2a1d17" stroke-width="6" stroke-linecap="round"/>`},
		{"Sleepy", 25, `<path d="M160 222q12 8 24 0M216 222q12 8 24 0" fill="none" stroke="#2a1d17" stroke-width="6" stroke-linecap="round"/><circle cx="200" cy="252" r="7" fill="#2a1d17"/>`},
		{"Surprised", 18, `<g fill="#fff" stroke="#2a1d17" stroke-width="5"><circle cx="172" cy="218" r="14"/><circle cx="228" cy="218" r="14"/></g><g fill="#2a1d17"><circle cx="175" cy="220" r="5"/><circle cx="231" cy="220" r="5"/><ellipse cx="200" cy="256" rx="9" ry="12"/></g>`},
		{"Wink", 15, `<g fill="#e07a5f" fill-opacity=".45"><ellipse cx="154" cy="244" rx="12" ry="7"/><ellipse cx="246" cy="244" rx="12" ry="7"/></g><path d="M160 222q12-10 24 0M176 246q24 22 48 0" fill="none" stroke="#2a1d17" stroke-width="6" stroke-linecap="round"/><circle cx="228" cy="220" r="8" fill="#2a1d17"/>`},
		{"Grumpy", 12, `<path d="M158 204l26 8M242 204l-26 8M180 258h40" fill="none" stroke="#2a1d17" stroke-width="6" stroke-linecap="round"/><g fill="#2a1d17"><circle cx="172" cy="224" r="7"/><circle cx="228" cy="224" r="7"/></g>`},
	}},
	{"Handle", []part{
		{"Single", 35, `<path d="M286 176q78 8 44 100" fill="none" stroke="#2a1d17" stroke-width="18" stroke-linecap="round"/><path d="M286 176q78 8 44 100" fill="none" stroke="currentColor" stroke-width="8" stroke-linecap="round"/>`},
		{"Pair", 30, `<path d="M114 176q-78 8-44 100M286 176q78 8 44 100" fill="none" stroke="#2a1d17" stroke-width="18" stroke-linecap="round"/><path d="M114 176q-78 8-44 100M286 176q78 8 44 100" fill="none" stroke="currentColor" stroke-width="8" stroke-linecap="round"/>`},
		{"Ears", 10, `<path d="M152 150q-42-8-36 30M248 150q42-8 36 30" fill="none" stroke="#2a1d17" stroke-width="14" stroke-linecap="round"/><path d="M152 150q-42-8-36 30M248 150q42-8 36 30" fill="none" stroke="currentColor" stroke-width="6" stroke-linecap="round"/>`},
		{"None", 25, ``},
	}},
	{"Crown", []part{
		{"Flower", 25, `<path d="M200 130V70" stroke="#4f7a3a" stroke-width="6"/><g fill="currentColor" stroke="#2a1d17" stroke-width="4"><circle cx="200" cy="52" r="14"/><circle cx="182" cy="68" r="14"/><circle cx="218" cy="68" r="14"/></g><circle cx="200" cy="64" r="7" fill="#f2cc8f"/>`},
		{"Lid", 25, `<ellipse cx="200" cy="124" rx="62" ry="14" fill="currentColor" stroke="#2a1d17" stroke-width="5"/><circle cx="200" cy="104" r="11" fill="currentColor" stroke="#2a1d17" stroke-width="5"/>`},
		{"Steam", 20, `<path d="M180 110q-16-20 0-40t0-40M220 110q-16-20 0-40t0-40" fill="none" stroke="currentColor" stroke-width="7" stroke-linecap="round"/>`},
		{"Sprout", 18, `<path d="M200 128V84" stroke="#4f7a3a" stroke-width="6" stroke-linecap="round"/><path d="M200 100q-32-2-38-30q32-2 38 30zM200 92q30-4 36-30q-32 0-36 30z" fill="currentColor" stroke="#2a1d17" stroke-width="4" stroke-linejoin="round"/>`},
		{"Bird", 12, `<path d="M176 120q4-34 40-30q18-14 26 4l-14 6q4 20-26 22z" fill="currentColor" stroke="#2a1d17" stroke-width="5" stroke-linejoin="round"/><path d="M194 104q10 8 22 0" fill="none" stroke="#2a1d17" stroke-width="4" stroke-linecap="round"/><circle cx="226" cy="98" r="3" fill="#2a1d17"/>`},
	}},
}

// palettes: name, paper ("" = no paper rect) and eight inks. The last one, Kit Colours,
// leaves the kit's own colours alone (no paper, no color attributes).
var palettes = []palette{
	{"Terracotta", "#f6efe3", []string{"#e07a5f", "#3d405b", "#81b29a", "#f2cc8f", "#6d597a", "#b56576", "#5e8c9a", "#c9a227"}},
	{"Riso", "#f4ede0", []string{"#ff48b0", "#0078bf", "#ffe14a", "#00a95c", "#ff6c2f", "#765ba7", "#5ec8e5", "#f15060"}},
	{"Iznik", "#f7f3ea", []string{"#1f5fa8", "#2a9d8f", "#c8102e", "#e9c46a", "#264653", "#6fb7d6", "#b5432c", "#3a7d44"}},
	{"Sea Glass", "#eef3ef", []string{"#7fb7be", "#bc4b51", "#5b8e7d", "#f4e285", "#8cb369", "#3c6e71", "#e7b7a3", "#d3a588"}},
	{"Dusk", "#262338", []string{"#f2a65a", "#e85d75", "#8d5a97", "#5bc0be", "#f9dc5c", "#c3bef7", "#ee6c4d", "#6c91c2"}},
	{"Sinopia", "#efe4cc", []string{"#b5432c", "#2e4a3a", "#d9a441", "#7a2e1f", "#4a6670", "#c97b5a", "#8c6d46", "#e3c08d"}},
	{"Mono", "#f2f0eb", []string{"#1c1c1c", "#4a4a4a", "#7a7a7a", "#a8a8a8", "#d0d0d0", "#2f2f2f", "#5e5e5e", "#8f8f8f"}},
	{"Kit Colours", "", []string{}},
}

gnomod.toml

module = "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/art/katman/v1"
gno = "0.9"

[addpkg]
  creator = "g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06"
  height = 1450623
  max_deposit = "10000000ugnot"

katman.gno

// Package katman is the composition logic of Katman, Sinopia's open PFP
// tool engine: an artist uploads trait layers (SVG parts with rarity
// weights) to a sealed assets/v1 store, and every revealed token is one part
// per layer, drawn from the token's final seed by weight and stacked
// bottom-up into one SVG. Parts may paint with currentColor; the
// collection's palette then gives every layer its own ink, shuffled per
// token, over a paper backdrop.
//
// This package is pure: it never reads a store. The adapter realm
// (r/…/sinopia/engines/katman) reads the drawn parts from assets/v1 and
// hands their bytes to ComposeParts. The demo kit in demo.gno (params store
// 0) is the engine's own sample: the jug characters of the engine gallery,
// redrawn. frontend/src/engines/katman.ts is a byte-identical TypeScript
// twin; katman_test.gno and katman.test.ts pin the same golden vectors.
//
// Why composition can never break the SVG allowlist: every part is a whole
// document that passed safe.AssertSVG when its last byte was stored
// (assets/v1 AppendBytes → media.CheckImage). Its root tag is exactly
// Root(w, h) and the document ends with the closing tag of that root
// (AssertSVG refuses content after the root), so the bytes between them are
// a balanced run of allowlisted elements, attributes and text. ComposeParts
// only concatenates such runs inside <g> elements under one Root(w, h); every
// rule of the allowlist is local to an element, an attribute value or a text
// run, so the result passes too. Core re-checks every render anyway.
//
// Pure integer maths: no floats, no time, no block data.
package katman

import (
	"errors"
	"strconv"
	"strings"

	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/engine/v2"
)

// Kit limits (the adapter enforces them on a store at CreateCollection).
const (
	MaxLayers        = 8      // = assets/v1 MaxTraitTypes = MaxTiers; trait types
	MaxPartsPerLayer = 64     // parts of one layer
	MaxParts         = 128    // parts of a kit
	MaxKitBytes      = 262144 // on-chain bytes of a kit (sum of part files)
	MaxSVGBytes      = 28672  // worst composed token (Manifest.MaxSVGBytes)
	MaxCanvas        = 4096   // viewBox width and height, 1..MaxCanvas
	MaxStore         = 999999999
	DemoCanvas       = 400
)

// ParamsSchema is the Studio form. Studio sends JSON.stringify of the values
// in this property order; ValidateParams accepts exactly that or "".
const ParamsSchema = `{"type":"object","properties":{` +
	`"store":{"type":"integer","title":"Part store (0 = demo kit)","minimum":0,"maximum":999999999,"default":0},` +
	`"palette":{"type":"string","title":"Palette","enum":["Terracotta","Riso","Iznik","Sea Glass","Dusk",` +
	`"Sinopia","Mono","Kit Colours"],"default":"Terracotta"}}}`

// DefaultParams is the canonical form of the defaults; it renders exactly
// like "".
const DefaultParams = `{"store":0,"palette":"Terracotta"}`

// PaletteKit is the index of "Kit Colours": no paper, no layer inks.
const PaletteKit = 7

type part struct {
	name   string
	weight int64
	inner  string
}

type layer struct {
	name  string
	parts []part
}

type palette struct {
	name, paper string
	inks        []string
}

// Manifest describes the engine. Author and FeeBps are set by the adapter.
func Manifest() engine.Manifest {
	return engine.Manifest{
		Name:    "Katman",
		Version: "1.0.0",
		Description: "Open PFP tool: artists upload their own trait layers (SVG parts with rarity weights) to a " +
			"sealed asset store; at reveal the chain draws one part per layer from each token's seed and stacks " +
			"them. A palette can ink every layer per token. Params store 0 is the built-in demo kit.",
		License:      "CC-BY-NC-4.0",
		ParamsSchema: ParamsSchema,
		TraitTypes:   DemoLayerNames(),
		Living:       false,
		MaxSVGBytes:  MaxSVGBytes,
		Kind:         engine.KindSVG,
	}
}

// DemoLayerNames are the demo kit's layers: its trait types.
func DemoLayerNames() []string {
	out := make([]string, len(demoLayers))
	for i, l := range demoLayers {
		out[i] = l.name
	}
	return out
}

// Config is a parsed params string.
type Config struct {
	Store   int64 // assets/v1 store id; 0 = the demo kit
	Palette int   // index into the palettes; PaletteKit = the kit's own colours
}

// Defaults is params "": the demo kit in Terracotta.
func Defaults() Config { return Config{} }

var errParams = errors.New(`katman: params must be "" or {"store":<0..999999999>,"palette":<enum>} without spaces`)

// ParseParams accepts "" or the canonical JSON Studio produces, nothing else.
func ParseParams(params string) (Config, error) {
	cfg := Defaults()
	if params == "" {
		return cfg, nil
	}
	const pre, mid, post = `{"store":`, `,"palette":"`, `"}`
	if !strings.HasPrefix(params, pre) || !strings.HasSuffix(params, post) {
		return cfg, errParams
	}
	rest := params[len(pre) : len(params)-len(post)]
	k := strings.Index(rest, mid)
	if k < 1 || k > 9 {
		return cfg, errParams
	}
	digits, name := rest[:k], rest[k+len(mid):]
	if len(digits) > 1 && digits[0] == '0' {
		return cfg, errParams
	}
	var n int64
	for i := 0; i < len(digits); i++ {
		c := digits[i]
		if c < '0' || c > '9' {
			return cfg, errParams
		}
		n = n*10 + int64(c-'0')
	}
	cfg.Store = n
	for i, p := range palettes {
		if p.name == name {
			cfg.Palette = i
			return cfg, nil
		}
	}
	return cfg, errParams
}

// ValidateParams accepts "" (the demo kit) and the canonical JSON. Whether a
// store kit is usable is the adapter's call (it reads the store).
func ValidateParams(params string) error {
	_, err := ParseParams(params)
	return err
}

// MustConfig is ParseParams for params core already validated.
func MustConfig(params string) Config {
	cfg, err := ParseParams(params)
	if err != nil {
		panic(err)
	}
	return cfg
}

// PaletteName is the palette's enum value.
func PaletteName(i int) string { return palettes[i].name }

// ---------------------------------------------------------------------------
// Deterministic randomness: FNV-1a over the seed, then splitmix64 (the stream
// of every Sinopia engine). Parts use seed+"|katman|parts", inks
// seed+"|katman|inks", so a palette never moves a token's parts.
// ---------------------------------------------------------------------------

type rng struct{ state uint64 }

func newRNG(seed string) *rng {
	h := uint64(14695981039346656037)
	for i := 0; i < len(seed); i++ {
		h ^= uint64(seed[i])
		h *= 1099511628211
	}
	if h == 0 {
		h = 1099511628211
	}
	return &rng{state: h}
}

func (r *rng) next() uint64 {
	r.state += 0x9E3779B97F4A7C15
	z := r.state
	z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
	z = (z ^ (z >> 27)) * 0x94D049BB133111EB
	return z ^ (z >> 31)
}

func (r *rng) intn(n int) int {
	if n <= 0 {
		return 0
	}
	return int(r.next() % uint64(n))
}

// Draw returns, per layer, a point in 0..totals[k]-1: the token's position
// on that layer's weight line (the sum of its parts' weights). Part i of a
// layer covers [w_0+…+w_{i-1}, w_0+…+w_i): exactly the slot ranges assets/v1
// gives editions, so the adapter maps the point to a part with
// assets.SlotArtwork. One draw per layer, also for an empty layer (0).
func Draw(seed string, totals []int64) []int64 {
	r := newRNG(seed + "|katman|parts")
	out := make([]int64, len(totals))
	for k, t := range totals {
		v := r.next()
		if t > 0 {
			out[k] = int64(v % uint64(t))
		}
	}
	return out
}

// Pick maps Draw onto explicit weight lists: the index of the drawn part of
// every layer (the walk the TS twin and the demo kit use).
func Pick(seed string, weights [][]int64) []int {
	totals := make([]int64, len(weights))
	for k, ws := range weights {
		for _, w := range ws {
			totals[k] += w
		}
	}
	at := Draw(seed, totals)
	out := make([]int, len(weights))
	for k, ws := range weights {
		x := at[k]
		out[k] = len(ws) - 1
		for i, w := range ws {
			if x < w {
				out[k] = i
				break
			}
			x -= w
		}
	}
	return out
}

// Inks is the ink of each of n layers under palette pal for seed: the
// palette's eight inks shuffled per token (Fisher-Yates), layer k taking the
// (k mod 8)-th. nil for Kit Colours.
func Inks(seed string, pal, n int) []string {
	p := palettes[pal]
	m := len(p.inks)
	if m == 0 {
		return nil
	}
	order := make([]int, m)
	for i := range order {
		order[i] = i
	}
	r := newRNG(seed + "|katman|inks")
	for i := m - 1; i > 0; i-- {
		j := r.intn(i + 1)
		order[i], order[j] = order[j], order[i]
	}
	out := make([]string, n)
	for k := range out {
		out[k] = p.inks[order[k%m]]
	}
	return out
}

// ---------------------------------------------------------------------------
// Parts and composition.
// ---------------------------------------------------------------------------

const rootPrefix = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 `

// Root is the one root tag every part of a w x h kit starts with, and the
// root of every composed token.
func Root(w, h int) string {
	return rootPrefix + strconv.Itoa(w) + " " + strconv.Itoa(h) + `">`
}

// ParseRoot reads a part's root tag: exactly Root(w, h) with canonical
// decimals 1..MaxCanvas. ok is false for anything else.
func ParseRoot(svg string) (root string, w, h int, ok bool) {
	if !strings.HasPrefix(svg, rootPrefix) {
		return "", 0, 0, false
	}
	i := len(rootPrefix)
	w, i, ok = dim(svg, i, ' ')
	if !ok {
		return "", 0, 0, false
	}
	h, i, ok = dim(svg, i, '"')
	if !ok || i >= len(svg) || svg[i] != '>' {
		return "", 0, 0, false
	}
	return svg[:i+1], w, h, true
}

// dim parses a canonical decimal 1..MaxCanvas at s[i:] followed by end;
// it returns the index after end.
func dim(s string, i int, end byte) (int, int, bool) {
	v, start := 0, i
	for i < len(s) && i-start <= 4 && s[i] >= '0' && s[i] <= '9' {
		v = v*10 + int(s[i]-'0')
		i++
	}
	n := i - start
	if n == 0 || n > 4 || s[start] == '0' || v > MaxCanvas || i >= len(s) || s[i] != end {
		return 0, 0, false
	}
	return v, i + 1, true
}

// Inner is a part's content between its root tag and its final </svg>, or
// ok false when the part does not start with root or end with </svg>.
func Inner(svg, root string) (string, bool) {
	if len(svg) < len(root)+6 || svg[:len(root)] != root || svg[len(svg)-6:] != "</svg>" {
		return "", false
	}
	return svg[len(root) : len(svg)-6], true
}

// compose stacks the layers' inner SVG bottom-up: root, the palette's paper,
// then one <g> per non-empty layer (with its ink when the palette has
// inks), then </svg>.
func compose(root string, w, h, pal int, inks []string, inners []string) string {
	var b strings.Builder
	b.WriteString(root)
	if p := palettes[pal].paper; p != "" {
		b.WriteString(`<rect width="` + strconv.Itoa(w) + `" height="` + strconv.Itoa(h) + `" fill="` + p + `"/>`)
	}
	for k, in := range inners {
		if in == "" {
			continue
		}
		if inks != nil {
			b.WriteString(`<g color="` + inks[k] + `">`)
		} else {
			b.WriteString(`<g>`)
		}
		b.WriteString(in)
		b.WriteString(`</g>`)
	}
	b.WriteString(`</svg>`)
	return b.String()
}

// ComposeParts is a store kit's token: parts[k] is the whole file of the
// part drawn for layer k (bottom first). The canvas is the first part whose
// root parses; a part with another root, or no file (""), leaves its layer
// empty, so an unchecked kit still composes a valid document (a checked kit
// never has such a part: the adapter refuses it at CreateCollection).
func ComposeParts(seed string, pal int, parts []string) string {
	root, w, h := "", 0, 0
	for _, p := range parts {
		if r, ww, hh, ok := ParseRoot(p); ok {
			root, w, h = r, ww, hh
			break
		}
	}
	if root == "" {
		root, w, h = Root(DemoCanvas, DemoCanvas), DemoCanvas, DemoCanvas
	}
	inners := make([]string, len(parts))
	for k, p := range parts {
		if in, ok := Inner(p, root); ok {
			inners[k] = in
		}
	}
	return compose(root, w, h, pal, Inks(seed, pal, len(parts)), inners)
}

// WorstBytes is the largest token a kit can compose under palette pal:
// maxInner[k] is the largest inner part of layer k (0 = all empty).
func WorstBytes(root string, w, h, pal int, maxInner []int) int {
	n := len(root) + len(`</svg>`)
	if p := palettes[pal].paper; p != "" {
		n += len(`<rect width="` + strconv.Itoa(w) + `" height="` + strconv.Itoa(h) + `" fill="` + p + `"/>`)
	}
	open := len(`<g>`)
	if len(palettes[pal].inks) > 0 {
		open = len(`<g color="#000000">`)
	}
	for _, m := range maxInner {
		if m > 0 {
			n += open + m + len(`</g>`)
		}
	}
	return n
}

// ---------------------------------------------------------------------------
// The demo kit (params store 0): the engine's sample and Studio preview.
// ---------------------------------------------------------------------------

func demoPick(seed string) []int {
	ws := make([][]int64, len(demoLayers))
	for k, l := range demoLayers {
		ws[k] = make([]int64, len(l.parts))
		for i, p := range l.parts {
			ws[k][i] = p.weight
		}
	}
	return Pick(seed, ws)
}

// SVG is the demo kit's token for seed under palette pal.
func SVG(seed string, pal int) string {
	at := demoPick(seed)
	inners := make([]string, len(demoLayers))
	for k, l := range demoLayers {
		inners[k] = l.parts[at[k]].inner
	}
	return compose(Root(DemoCanvas, DemoCanvas), DemoCanvas, DemoCanvas, pal,
		Inks(seed, pal, len(demoLayers)), inners)
}

// DemoTraits are the demo kit's part names for seed, in layer order.
func DemoTraits(seed string) []engine.Trait {
	at := demoPick(seed)
	out := make([]engine.Trait, len(demoLayers))
	for k, l := range demoLayers {
		out[k] = engine.Trait{Type: l.name, Value: l.parts[at[k]].name}
	}
	return out
}

// Render is the demo kit's art (params store 0). Store kits are composed by
// the adapter (ComposeParts); asking this package for one panics.
func Render(in engine.Input) string {
	cfg := MustConfig(in.Params)
	if cfg.Store != 0 {
		panic("katman: a store kit is composed by the adapter")
	}
	return SVG(in.Seed, cfg.Palette)
}

// Traits are the demo kit's traits (params store 0): one per layer.
func Traits(in engine.Input) []engine.Trait {
	cfg := MustConfig(in.Params)
	if cfg.Store != 0 {
		panic("katman: a store kit's traits come from the adapter")
	}
	return DemoTraits(in.Seed)
}