| 1 | package layout |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | "sort" |
| 7 | "strings" |
| 8 | |
| 9 | "amadan.net/oficina/slides/internal/slides/deck" |
| 10 | "amadan.net/oficina/slides/internal/slides/fonts" |
| 11 | "amadan.net/oficina/slides/internal/slides/render" |
| 12 | ) |
| 13 | |
| 14 | // Layout draws d's slide at index into a paint list. |
| 15 | // |
| 16 | // It fails only on a caller mistake — no deck, or an index that is not |
| 17 | // a slide. Everything a deck can contain lays out as something: an |
| 18 | // unknown theme falls back to the default, an unknown block type draws |
| 19 | // nothing rather than breaking the slide around it, and a prop with |
| 20 | // the wrong type takes its default. A half-written document that |
| 21 | // arrived from a peer mid-edit is the ordinary case, not an error. |
| 22 | // |
| 23 | // The warnings are the things worth saying out loud about the result: |
| 24 | // a block that outgrew its box, a rune no embedded face can draw, an |
| 25 | // image the deck never registered. None of them stops the drawing. |
| 26 | func Layout(d *deck.Deck, index int) (Paint, []Warning, error) { |
| 27 | return New(d, Options{}).Layout(index) |
| 28 | } |
| 29 | |
| 30 | // Options is what a render needs beyond the deck. |
| 31 | type Options struct { |
| 32 | // Fonts reads an uploaded face's file by its ref (deck.FontFace.Ref). |
| 33 | // Nil reads none, and an error is a face that is not there: either |
| 34 | // way the text is set in the next family of its stack, as it would |
| 35 | // be in a browser that never got the file. |
| 36 | Fonts func(ref string) ([]byte, error) |
| 37 | // QR is the follow-along codes this picture may draw, as |
| 38 | // render.Options.QR: a qr block's stored url to the address drawn. |
| 39 | // Nil draws none. |
| 40 | QR map[string]string |
| 41 | } |
| 42 | |
| 43 | // Renderer lays out one deck's slides for one render — a thumbnail, or |
| 44 | // every page of an export. It reads and parses each uploaded face the |
| 45 | // deck's stacks call for at most once, and keeps them only as long as |
| 46 | // it is kept: nothing here holds a person's font past the render. |
| 47 | type Renderer struct { |
| 48 | d *deck.Deck |
| 49 | opts Options |
| 50 | |
| 51 | // parsed is each ref read so far: its font, or nil for one that |
| 52 | // did not load or is not a face a picture may draw with. |
| 53 | parsed map[string]*fonts.Font |
| 54 | // families is each uploaded family resolved so far, by its CSS name |
| 55 | // folded to lower case and the stack after it: one of its faces |
| 56 | // (fonts.UploadedFamily), or nil for a family with none to draw |
| 57 | // with. The stack is part of the key because a picture names it. |
| 58 | families map[string]*fonts.Uploaded |
| 59 | // used is every uploaded face some run of this render is drawn |
| 60 | // in, once per family, weight and slope, in the order first drawn — |
| 61 | // what the PDF has to be handed, and no more: a style uploaded and |
| 62 | // never set costs an export nothing. |
| 63 | used []*fonts.Uploaded |
| 64 | seen map[usedKey]bool |
| 65 | } |
| 66 | |
| 67 | // usedKey is one face as the PDF tells faces apart. The same face |
| 68 | // resolved under two stacks (a heading's and a body's) is two Uploaded |
| 69 | // values and one font. |
| 70 | type usedKey struct { |
| 71 | css string |
| 72 | weight int |
| 73 | italic bool |
| 74 | } |
| 75 | |
| 76 | // New is a render of d. |
| 77 | func New(d *deck.Deck, o Options) *Renderer { |
| 78 | return &Renderer{d: d, opts: o, parsed: map[string]*fonts.Font{}, |
| 79 | families: map[string]*fonts.Uploaded{}, seen: map[usedKey]bool{}} |
| 80 | } |
| 81 | |
| 82 | // Layout draws the deck's slide at index; see the package's Layout. |
| 83 | func (r *Renderer) Layout(index int) (Paint, []Warning, error) { |
| 84 | d := r.d |
| 85 | if d == nil { |
| 86 | return Paint{}, nil, fmt.Errorf("layout: no deck") |
| 87 | } |
| 88 | if index < 0 || index >= len(d.Slides) { |
| 89 | return Paint{}, nil, fmt.Errorf("layout: slide %d is outside a deck of %d", index, len(d.Slides)) |
| 90 | } |
| 91 | e := newEngine(r, index) |
| 92 | e.slide(d.Slides[index]) |
| 93 | r.note(e.ops) |
| 94 | return Paint{W: FrameW, H: FrameH, Background: e.bg, Ops: e.ops}, e.warnings, nil |
| 95 | } |
| 96 | |
| 97 | // note records the uploaded faces ops draw runs in. |
| 98 | func (r *Renderer) note(ops []Op) { |
| 99 | for _, op := range ops { |
| 100 | t, ok := op.(Text) |
| 101 | if !ok { |
| 102 | continue |
| 103 | } |
| 104 | for _, run := range t.Runs { |
| 105 | u := run.Face.Upload() |
| 106 | if u == nil { |
| 107 | continue |
| 108 | } |
| 109 | if k := (usedKey{strings.ToLower(u.CSS), u.Weight, u.Italic}); !r.seen[k] { |
| 110 | r.seen[k] = true |
| 111 | r.used = append(r.used, u) |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Uploaded is every uploaded face a run this render laid out is drawn |
| 118 | // in, in the order first drawn. |
| 119 | func (r *Renderer) Uploaded() []*fonts.Uploaded { return r.used } |
| 120 | |
| 121 | // declared is the deck's uploaded family whose CSS name is name, if it |
| 122 | // has one. |
| 123 | func (r *Renderer) declared(name string) (deck.FontFamily, bool) { |
| 124 | for _, fam := range r.d.Meta.FontFiles { |
| 125 | if strings.EqualFold(fam.CSS, name) { |
| 126 | return fam, true |
| 127 | } |
| 128 | } |
| 129 | return deck.FontFamily{}, false |
| 130 | } |
| 131 | |
| 132 | // family resolves an uploaded family, named first in a stack whose |
| 133 | // remainder is rest, to the faces a picture can draw with, once per |
| 134 | // render and stack: for each of regular, italic, bold and bold italic, |
| 135 | // the face CSS would choose for that weight (400 or 700) among the |
| 136 | // faces marked for pictures with the slope (byWeight), whose bytes |
| 137 | // load, parse, are not a variable font and allow embedding. The checks |
| 138 | // after the mark are there because a deck's meta.fontFiles is a |
| 139 | // document anyone with the deck can write, and the mark is only what it |
| 140 | // says. A slope with no such face is drawn in the rest of the stack. |
| 141 | // |
| 142 | // The face CSS chooses for a weight it has no face for is drawn as it |
| 143 | // is: the browser thickens a face it chose for bold, keeping that |
| 144 | // face's advances, and a picture does not; it measures with the same |
| 145 | // face, so its lines break where the browser's do. |
| 146 | func (r *Renderer) family(fam deck.FontFamily, rest string) *fonts.Uploaded { |
| 147 | rest = strings.TrimSpace(rest) |
| 148 | key := strings.ToLower(fam.CSS) + "\x00" + rest |
| 149 | if u, ok := r.families[key]; ok { |
| 150 | return u |
| 151 | } |
| 152 | var faces []*fonts.Uploaded |
| 153 | if deck.FontCSSName(fam.CSS) { |
| 154 | for _, weight := range []int{400, 700} { |
| 155 | for _, italic := range []bool{false, true} { |
| 156 | var marked []deck.FontFace |
| 157 | for _, face := range fam.Faces { |
| 158 | if face.Pictures && face.Italic == italic { |
| 159 | marked = append(marked, face) |
| 160 | } |
| 161 | } |
| 162 | for _, face := range byWeight(marked, weight) { |
| 163 | if font := r.font(face.Ref); font != nil { |
| 164 | faces = append(faces, &fonts.Uploaded{CSS: fam.CSS, Weight: weight, Italic: italic, Font: font, Fallback: rest}) |
| 165 | break |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | u := fonts.UploadedFamily(faces...) |
| 172 | r.families[key] = u |
| 173 | return u |
| 174 | } |
| 175 | |
| 176 | // byWeight is faces in the order CSS's font matching prefers them for |
| 177 | // weight w, 400 or 700 here (CSS Fonts 4, §5.2): a face whose range |
| 178 | // holds w first; then, for w from 400 to 500, the heavier faces up to |
| 179 | // 500, nearest first, then the lighter ones, nearest first, then the |
| 180 | // heavier past 500; for w over 500, the heavier, nearest first, then |
| 181 | // the lighter. A face is placed by the nearest end of its range. |
| 182 | func byWeight(faces []deck.FontFace, w int) []deck.FontFace { |
| 183 | type ranked struct { |
| 184 | face deck.FontFace |
| 185 | band, gap int |
| 186 | } |
| 187 | var rs []ranked |
| 188 | for _, f := range faces { |
| 189 | lo, hi := f.Weight[0], f.Weight[1] |
| 190 | r := ranked{face: f} |
| 191 | switch { |
| 192 | case lo <= w && w <= hi: |
| 193 | case lo > w && (w > 500 || lo <= 500): |
| 194 | r.band, r.gap = 1, lo-w |
| 195 | case hi < w: |
| 196 | r.band, r.gap = 2, w-hi |
| 197 | default: // heavier than 500, for a w from 400 to 500 |
| 198 | r.band, r.gap = 3, lo-w |
| 199 | } |
| 200 | rs = append(rs, r) |
| 201 | } |
| 202 | sort.SliceStable(rs, func(i, j int) bool { |
| 203 | if rs[i].band != rs[j].band { |
| 204 | return rs[i].band < rs[j].band |
| 205 | } |
| 206 | return rs[i].gap < rs[j].gap |
| 207 | }) |
| 208 | out := make([]deck.FontFace, len(rs)) |
| 209 | for i, r := range rs { |
| 210 | out[i] = r.face |
| 211 | } |
| 212 | return out |
| 213 | } |
| 214 | |
| 215 | // font reads and parses one uploaded face, once per render. |
| 216 | func (r *Renderer) font(ref string) *fonts.Font { |
| 217 | if font, ok := r.parsed[ref]; ok { |
| 218 | return font |
| 219 | } |
| 220 | var font *fonts.Font |
| 221 | if r.opts.Fonts != nil { |
| 222 | if data, err := r.opts.Fonts(ref); err == nil { |
| 223 | if f, err := fonts.Parse(data); err == nil && !f.Variable() && f.Embeddable() { |
| 224 | font = f |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | r.parsed[ref] = font |
| 229 | return font |
| 230 | } |
| 231 | |
| 232 | // engine is one Layout call: the palette it paints in, the faces it |
| 233 | // measures with, and what it has drawn and noticed so far. |
| 234 | type engine struct { |
| 235 | deck *deck.Deck |
| 236 | // render is the Renderer this slide is part of: where the deck's |
| 237 | // uploaded faces are read, once for all its slides. |
| 238 | render *Renderer |
| 239 | // onCanvas is whether this slide's blocks are placed by hand, so a |
| 240 | // scene block is its box (scenes.go). |
| 241 | onCanvas bool |
| 242 | |
| 243 | bg, fg Fill |
| 244 | accent, accent2 Fill |
| 245 | highlight Fill |
| 246 | head, body, code fonts.Face |
| 247 | |
| 248 | // sc is the type and spacing scale this slide is drawn at, which |
| 249 | // depends on whether its section has padding (sizes.go). |
| 250 | sc scale |
| 251 | |
| 252 | ops []Op |
| 253 | warnings []Warning |
| 254 | |
| 255 | // slideID is the slide being drawn. Every warning this engine |
| 256 | // makes carries it (see warn), so a caller that laid out a whole |
| 257 | // deck can say which slide each line is about. |
| 258 | slideID string |
| 259 | |
| 260 | // block is the id warnings are attributed to while a block is |
| 261 | // being measured, and seen keeps a missing glyph from being |
| 262 | // reported once per line it appears on. |
| 263 | block string |
| 264 | seen map[rune]bool |
| 265 | } |
| 266 | |
| 267 | func newEngine(r *Renderer, index int) *engine { |
| 268 | d := r.d |
| 269 | // The same palette the section carries, overrides and contrast |
| 270 | // judgements included — render.SlideVars is the seam, so the paint |
| 271 | // list cannot form a second opinion about what colour a slide is. |
| 272 | vars, _ := render.SlideVars(d, index) |
| 273 | theme := render.ThemeID(d.Meta.Theme) |
| 274 | |
| 275 | e := &engine{ |
| 276 | deck: d, |
| 277 | render: r, |
| 278 | slideID: d.Slides[index].ID, |
| 279 | bg: Fill(vars["--slides-bg"]), |
| 280 | fg: Fill(vars["--slides-fg"]), |
| 281 | code: fonts.Get(theme, fonts.Code, false, false), |
| 282 | seen: map[rune]bool{}, |
| 283 | } |
| 284 | e.accent = Fill(vars["--slides-accent"]) |
| 285 | e.accent2 = Fill(vars["--slides-accent-2"]) |
| 286 | e.highlight = mix(e.accent, e.bg, mixHighlight) |
| 287 | e.head = e.faceFor(theme, fonts.Heading, vars["--slides-font-heading"]) |
| 288 | e.body = e.faceFor(theme, fonts.Body, vars["--slides-font-body"]) |
| 289 | return e |
| 290 | } |
| 291 | |
| 292 | // faceFor is the face a stack names, or the theme's own when the deck |
| 293 | // named a family this binary does not carry (meta.fonts lets it name |
| 294 | // any at all). The substitution is reported rather than made quietly: |
| 295 | // it is the one case where the browser and this engine are knowingly |
| 296 | // setting a slide in two different faces, and an export nobody warned |
| 297 | // about is an export nobody checks. |
| 298 | // |
| 299 | // A stack whose first name is a family uploaded to the deck is set in |
| 300 | // that upload wherever it has a face a picture can draw with |
| 301 | // (Renderer.family), and in the rest of the stack, judged as it always |
| 302 | // was, wherever it has none. Falling back from an upload is expected — |
| 303 | // a variable font, a WOFF2 file, a style never uploaded — so it is not |
| 304 | // reported; a family that is neither built in nor uploaded still is. |
| 305 | func (e *engine) faceFor(theme string, role fonts.Role, stack string) fonts.Face { |
| 306 | return e.stackFace(theme, role, stack, true) |
| 307 | } |
| 308 | |
| 309 | // stackFace is faceFor, drawing in an uploaded family only when it is |
| 310 | // the first name of the deck's stack (upload); one later in the stack |
| 311 | // is passed over in silence. |
| 312 | func (e *engine) stackFace(theme string, role fonts.Role, stack string, upload bool) fonts.Face { |
| 313 | if fam, ok := fonts.FamilyForStack(stack); ok { |
| 314 | return fonts.Face{Family: fam} |
| 315 | } |
| 316 | if first, rest := firstName(stack); first != "" { |
| 317 | if fam, ok := e.render.declared(first); ok { |
| 318 | face := e.stackFace(theme, role, rest, false) |
| 319 | if upload { |
| 320 | face.Uploaded = e.render.family(fam, rest) |
| 321 | } |
| 322 | return face |
| 323 | } |
| 324 | } |
| 325 | if strings.TrimSpace(stack) == "" { |
| 326 | // Only reached past an uploaded family that named nothing after |
| 327 | // it: the theme's own face, as the browser falls back to it. |
| 328 | return fonts.Get(theme, role, false, false) |
| 329 | } |
| 330 | sub := fonts.Get(theme, role, false, false) |
| 331 | e.warn(Warning{Kind: WarnFont, Detail: fmt.Sprintf( |
| 332 | "this deck asks for %q, which is not a face this build carries: measured in %s instead", |
| 333 | strings.TrimSpace(strings.Split(stack, ",")[0]), sub.Family.CSSName())}) |
| 334 | return sub |
| 335 | } |
| 336 | |
| 337 | // firstName splits a CSS font stack into its first family name, |
| 338 | // unquoted, and the rest of the stack. |
| 339 | func firstName(stack string) (first, rest string) { |
| 340 | first, rest, _ = strings.Cut(stack, ",") |
| 341 | return strings.Trim(strings.TrimSpace(first), `"'`), rest |
| 342 | } |
| 343 | |
| 344 | // warn records a warning, stamping the slide being drawn onto it so no |
| 345 | // call site has to remember to. A warning that already names a slide |
| 346 | // keeps the one it names. |
| 347 | func (e *engine) warn(w Warning) { |
| 348 | if w.Slide == "" { |
| 349 | w.Slide = e.slideID |
| 350 | } |
| 351 | e.warnings = append(e.warnings, w) |
| 352 | } |
| 353 | |
| 354 | // noteGlyphs reports runes no face could draw, once each per slide. |
| 355 | func (e *engine) noteGlyphs(rs []rune) { |
| 356 | var fresh []string |
| 357 | for _, r := range rs { |
| 358 | if e.seen[r] { |
| 359 | continue |
| 360 | } |
| 361 | e.seen[r] = true |
| 362 | fresh = append(fresh, fmt.Sprintf("%q", r)) |
| 363 | } |
| 364 | if len(fresh) == 0 { |
| 365 | return |
| 366 | } |
| 367 | e.warn(Warning{Kind: WarnGlyph, Block: e.block, Detail: fmt.Sprintf( |
| 368 | "no embedded face has a glyph for %s; drawn at the width of a box", |
| 369 | strings.Join(fresh, ", "))}) |
| 370 | } |
| 371 | |
| 372 | func (e *engine) draw(ops ...Op) { e.ops = append(e.ops, ops...) } |
| 373 | |
| 374 | // ── One slide ──────────────────────────────────────────────────────── |
| 375 | |
| 376 | // slide paints the background and then the blocks, in the order they |
| 377 | // are read: regions in reading order on a flow slide, z order on a |
| 378 | // canvas one. |
| 379 | func (e *engine) slide(s deck.Slide) { |
| 380 | // A picture behind the slide (§2.2) is an image op under |
| 381 | // everything else, covering the frame — `background-size: cover`. |
| 382 | if s.Background.Kind == "image" { |
| 383 | if id := s.Background.Value; id != "" { |
| 384 | if _, ok := e.deck.Assets[id]; ok { |
| 385 | e.draw(Image{X: 0, Y: 0, W: FrameW, H: FrameH, Asset: id, Fit: "cover"}) |
| 386 | } else { |
| 387 | e.warn(Warning{Kind: WarnAsset, Detail: fmt.Sprintf( |
| 388 | "this slide's background names asset %q, which this deck never registered", id)}) |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | tmpl := s.Template |
| 394 | if !slices.Contains(Templates(), tmpl) { |
| 395 | tmpl = "blank" |
| 396 | } |
| 397 | e.sc = scaleFor(s.Layout, tmpl) |
| 398 | |
| 399 | // A columns block's children are laid out inside it and nowhere |
| 400 | // else (§2.4) — render.body's rule, and it has to be the same rule |
| 401 | // or a child would be drawn twice. |
| 402 | byID := make(map[string]deck.Block, len(s.Blocks)) |
| 403 | claimed := map[string]bool{} |
| 404 | for _, b := range s.Blocks { |
| 405 | byID[b.ID] = b |
| 406 | } |
| 407 | for _, b := range s.Blocks { |
| 408 | if b.Type != "columns" { |
| 409 | continue |
| 410 | } |
| 411 | for _, id := range childIDs(b) { |
| 412 | if _, ok := byID[id]; ok { |
| 413 | claimed[id] = true |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | // done is shared across the whole slide, so a block is drawn at |
| 418 | // most once however many columns blocks name it. |
| 419 | done := map[string]bool{} |
| 420 | |
| 421 | // What no root reaches is drawn last and reported (codex 7): a |
| 422 | // block named as a child by a columns block that nothing arrives |
| 423 | // at — two peers' assignments merged into a cycle, or a document |
| 424 | // written past the ops, which refuse the shape. render.body does |
| 425 | // the same, so the page and the export agree on what is there. |
| 426 | late := unreached(s.Blocks, byID, claimed) |
| 427 | for _, b := range late { |
| 428 | e.warn(Warning{Kind: WarnUnreached, Block: b.ID, Detail: fmt.Sprintf( |
| 429 | "a %s block is named as a child by a columns block nothing reaches (a cycle); it is drawn after everything else", |
| 430 | b.Type)}) |
| 431 | } |
| 432 | |
| 433 | if s.Layout == "canvas" { |
| 434 | e.canvas(s, late, byID, claimed, done) |
| 435 | return |
| 436 | } |
| 437 | |
| 438 | // Flow: group into regions, canonical regions first, then any |
| 439 | // region name the core let through, in first-appearance order; the |
| 440 | // unreached blocks go to the end of the body, which is where the |
| 441 | // browser's unreached region auto-places on every template. |
| 442 | grouped := map[string][]deck.Block{} |
| 443 | var extra []string |
| 444 | for _, b := range s.Blocks { |
| 445 | if claimed[b.ID] { |
| 446 | continue |
| 447 | } |
| 448 | name := b.Region |
| 449 | switch { |
| 450 | case name == "": |
| 451 | name = "body" |
| 452 | case !slices.Contains(regionOrder, name) && !slices.Contains(extra, name): |
| 453 | extra = append(extra, name) |
| 454 | } |
| 455 | grouped[name] = append(grouped[name], b) |
| 456 | } |
| 457 | grouped["body"] = append(grouped["body"], late...) |
| 458 | // Measure every region, in reading order, then place them, then |
| 459 | // draw them in paint order. The measuring has to come first |
| 460 | // because two of the grids depend on what is in them: a |
| 461 | // `grid-template-rows: auto 1fr` heading row is as tall as the |
| 462 | // heading, and `title` centres its two rows as a pair. |
| 463 | var laid []*region |
| 464 | for _, name := range append(slices.Clone(regionOrder), extra...) { |
| 465 | if len(grouped[name]) == 0 { |
| 466 | continue |
| 467 | } |
| 468 | laid = append(laid, e.measureRegion(tmpl, name, grouped[name], byID, done)) |
| 469 | } |
| 470 | e.place(tmpl, laid) |
| 471 | for _, name := range append(paintOrder(tmpl), extra...) { |
| 472 | for _, rg := range laid { |
| 473 | if rg.name == name { |
| 474 | e.drawRegion(tmpl, rg) |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // paintOrder is the order the regions are DRAWN in, which is reading |
| 481 | // order everywhere but one place: image-full puts the heading over the |
| 482 | // picture with a z-index, so the picture has to be painted first. A |
| 483 | // paint list has no z-index, only order, and getting this wrong is not |
| 484 | // subtle — the photograph covers the title. |
| 485 | func paintOrder(tmpl string) []string { |
| 486 | if tmpl == "image-full" { |
| 487 | return []string{"media", "body", "aside", "heading"} |
| 488 | } |
| 489 | return slices.Clone(regionOrder) |
| 490 | } |
| 491 | |
| 492 | // region is one measured region: where the table put it, what is in |
| 493 | // it, how tall that came out, and where the top of it ended up. |
| 494 | type region struct { |
| 495 | name string |
| 496 | box Region |
| 497 | plans []plan |
| 498 | total float64 |
| 499 | y float64 |
| 500 | first string // the id of the first block in it, for a warning |
| 501 | } |
| 502 | |
| 503 | // measure lays one region's blocks out at its width without deciding |
| 504 | // where the top of it goes. |
| 505 | func (e *engine) measureRegion(tmpl, name string, blocks []deck.Block, byID map[string]deck.Block, done map[string]bool) *region { |
| 506 | rg := ®ion{name: name, box: regionFor(tmpl, name), first: firstID(blocks)} |
| 507 | |
| 508 | // image-full's media region is the whole frame and the picture |
| 509 | // fills it (`block-size: 100%`); every other region is as tall as |
| 510 | // what is in it. |
| 511 | fill := 0.0 |
| 512 | if tmpl == "image-full" && name == "media" { |
| 513 | fill = rg.box.H |
| 514 | } |
| 515 | |
| 516 | for _, b := range blocks { |
| 517 | if done[b.ID] { |
| 518 | continue |
| 519 | } |
| 520 | done[b.ID] = true |
| 521 | p := e.plan(b, slot{w: rg.box.W, h: fill, align: rg.box.AlignX}, byID, done) |
| 522 | if p.h == 0 && p.paint == nil { |
| 523 | continue |
| 524 | } |
| 525 | rg.plans = append(rg.plans, p) |
| 526 | } |
| 527 | // The accent bar under a section's heading: slides.css draws it |
| 528 | // with a ::after on the region, so it is the region's and not any |
| 529 | // block's, and it is part of what gets centred with it. |
| 530 | if tmpl == "section" && name == "heading" && len(rg.plans) > 0 { |
| 531 | rg.plans = append(rg.plans, e.sectionRule()) |
| 532 | } |
| 533 | rg.total = e.shrink(rg.plans, rg.box.H) |
| 534 | return rg |
| 535 | } |
| 536 | |
| 537 | // place decides where each region's content starts, and is where the |
| 538 | // two grids that depend on their own contents are handled. |
| 539 | // |
| 540 | // - `title` centres its two auto rows as a PAIR (`align-content: |
| 541 | // center` over `auto auto`), so the heading and the body straddle |
| 542 | // the middle with one gap between them, whatever height each is; |
| 543 | // an empty one is a zero-high row, and the gap stays. |
| 544 | // - `title-body` and `two-column` have `grid-template-rows: auto 1fr`, |
| 545 | // so a heading that runs to two lines pushes the row below it down |
| 546 | // instead of overlapping it. The table's fixed heading row is the |
| 547 | // one-line case; this is every other case. |
| 548 | func (e *engine) place(tmpl string, laid []*region) { |
| 549 | for _, rg := range laid { |
| 550 | rg.y = rg.box.Y |
| 551 | switch rg.box.AlignY { |
| 552 | case Center: |
| 553 | rg.y += (rg.box.H - rg.total) / 2 |
| 554 | case End: |
| 555 | rg.y = rg.box.Bottom() - rg.total |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | byName := map[string]*region{} |
| 560 | for _, rg := range laid { |
| 561 | byName[rg.name] = rg |
| 562 | } |
| 563 | head := byName["heading"] |
| 564 | |
| 565 | switch tmpl { |
| 566 | case "title": |
| 567 | // An empty region is still its row, zero high, with the gap |
| 568 | // beside it: a heading alone sits half a gap above the middle, |
| 569 | // as the browser puts it, not where its own row's box would. |
| 570 | body := byName["body"] |
| 571 | if head == nil && body == nil { |
| 572 | return |
| 573 | } |
| 574 | var headH, bodyH float64 |
| 575 | if head != nil { |
| 576 | headH = head.total |
| 577 | } |
| 578 | if body != nil { |
| 579 | bodyH = body.total |
| 580 | } |
| 581 | top := contentY + (contentH-(headH+sectionGap+bodyH))/2 |
| 582 | if head != nil { |
| 583 | head.y = top |
| 584 | } |
| 585 | if body != nil { |
| 586 | body.y = top + headH + sectionGap |
| 587 | } |
| 588 | case "title-body", "two-column": |
| 589 | if head == nil { |
| 590 | return |
| 591 | } |
| 592 | below := head.y + head.total + sectionGap |
| 593 | for _, name := range []string{"body", "aside"} { |
| 594 | rg := byName[name] |
| 595 | if rg == nil || below <= rg.box.Y { |
| 596 | continue |
| 597 | } |
| 598 | // The row below grew downwards, so it starts lower and has |
| 599 | // less room; a Start-aligned region simply moves. A |
| 600 | // heading long enough to leave it no room at all leaves it |
| 601 | // none — not a negative amount, which would read as |
| 602 | // nonsense in the overflow warning. |
| 603 | rg.box.H = max(0, rg.box.H-(below-rg.box.Y)) |
| 604 | rg.box.Y = below |
| 605 | rg.y = below |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | // drawRegion puts one measured region's ops in the paint list. |
| 611 | func (e *engine) drawRegion(tmpl string, rg *region) { |
| 612 | if len(rg.plans) == 0 { |
| 613 | return |
| 614 | } |
| 615 | // image-full's heading sits on a scrim, so the words survive a |
| 616 | // light picture. Drawn from the measured content rather than from |
| 617 | // the region box, because the stylesheet sizes that box to fit |
| 618 | // (`justify-self: start` inside `max-inline-size: 80%`). |
| 619 | if tmpl == "image-full" && rg.name == "heading" { |
| 620 | e.draw(e.scrim(rg.plans, rg.box, rg.y, rg.total)) |
| 621 | } |
| 622 | |
| 623 | top := rg.y |
| 624 | for i, p := range rg.plans { |
| 625 | if i > 0 { |
| 626 | top += e.sc.gap() |
| 627 | } |
| 628 | // A picture or a shape squeezed to nothing draws nothing — |
| 629 | // which is what the browser shows too, and a zero-sized op in |
| 630 | // an export is noise nobody can act on. The overflow warning |
| 631 | // below is what says the region was over-full. |
| 632 | if p.paint != nil && !(p.flex && p.h <= 0) { |
| 633 | e.draw(p.paint(rg.box.X, top, rg.box.W, p.h)...) |
| 634 | } |
| 635 | top += p.h |
| 636 | } |
| 637 | if rg.total > rg.box.H+0.001 { |
| 638 | e.warn(Warning{Kind: WarnOverflow, Block: rg.first, Detail: fmt.Sprintf( |
| 639 | "the %s region of a %s slide holds %.0fpx of content in %.0fpx; it is drawn past the box rather than cut", |
| 640 | rg.name, tmpl, rg.total, rg.box.H)}) |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | // shrink is the one piece of flexbox this engine implements, and it is |
| 645 | // here because leaving it out is visible from across a room. |
| 646 | // |
| 647 | // A region is a column flex container with a definite height. Text |
| 648 | // blocks cannot shrink — their minimum content size is the wrapped |
| 649 | // text — but a picture and a shape can, and they do: an image block in |
| 650 | // a body region is `display: flex` over an `<img>` at 100%, so its |
| 651 | // hypothetical size is the picture's own aspect ratio at the column |
| 652 | // width (1680 tall for a square picture in a 1680 column), and the |
| 653 | // browser shrinks it to whatever the region has left. Without this a |
| 654 | // slide with one heading and one photograph puts the photograph three |
| 655 | // slides below the bottom of the frame. |
| 656 | // |
| 657 | // The CSS algorithm with equal shrink factors: distribute the excess |
| 658 | // in proportion to each flexible item's base size, freeze anything |
| 659 | // that hits its floor, and go round again. It returns the height the |
| 660 | // region's contents ended up as, gaps included. |
| 661 | func (e *engine) shrink(plans []plan, avail float64) float64 { |
| 662 | total := e.sc.gap() * float64(len(plans)-1) |
| 663 | var flexible []int |
| 664 | for i, p := range plans { |
| 665 | total += p.h |
| 666 | if p.flex { |
| 667 | flexible = append(flexible, i) |
| 668 | } |
| 669 | } |
| 670 | for total > avail+0.001 && len(flexible) > 0 { |
| 671 | excess := total - avail |
| 672 | var base float64 |
| 673 | for _, i := range flexible { |
| 674 | base += plans[i].h |
| 675 | } |
| 676 | if base <= 0 { |
| 677 | break |
| 678 | } |
| 679 | var frozen []int |
| 680 | var shrunk float64 |
| 681 | for _, i := range flexible { |
| 682 | want := plans[i].h - excess*plans[i].h/base |
| 683 | if want <= plans[i].minH { |
| 684 | want = plans[i].minH |
| 685 | frozen = append(frozen, i) |
| 686 | } |
| 687 | shrunk += plans[i].h - want |
| 688 | plans[i].h = want |
| 689 | } |
| 690 | total -= shrunk |
| 691 | if len(frozen) == 0 { |
| 692 | break |
| 693 | } |
| 694 | // Drop the frozen ones and, if the excess is not used up, |
| 695 | // take it out of the rest. |
| 696 | var rest []int |
| 697 | for _, i := range flexible { |
| 698 | if !slices.Contains(frozen, i) { |
| 699 | rest = append(rest, i) |
| 700 | } |
| 701 | } |
| 702 | flexible = rest |
| 703 | } |
| 704 | return total |
| 705 | } |
| 706 | |
| 707 | // scrim is the panel behind image-full's heading: the region's own |
| 708 | // background, its padding, and its corner radius. |
| 709 | // |
| 710 | // The stylesheet mixes it 82% towards transparent over the picture. |
| 711 | // This draws it opaque, because a paint list has no compositing — an |
| 712 | // SVG and a PDF would blend it differently, and the scrim exists to |
| 713 | // make words readable over an unknown photograph, which an opaque |
| 714 | // panel does and a wrongly-composited one might not. |
| 715 | func (e *engine) scrim(plans []plan, r Region, y, total float64) Rect { |
| 716 | w := 0.0 |
| 717 | for _, p := range plans { |
| 718 | w = max(w, p.w) |
| 719 | } |
| 720 | w = min(w+2*scrimPadX, scrimW) |
| 721 | return Rect{ |
| 722 | X: r.X - scrimPadX, Y: y - scrimPadY, |
| 723 | W: w, H: total + 2*scrimPadY, |
| 724 | Fill: e.bg, Radius: 1 * e.sc.cqw, |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | // sectionRule is the accent bar under a section slide's heading — |
| 729 | // `[data-template="section"] [data-region="heading"]::after`, 18cqw of |
| 730 | // the accent colour, and the only decoration in the whole stylesheet. |
| 731 | func (e *engine) sectionRule() plan { |
| 732 | w := 18 * e.sc.cqw |
| 733 | rule := e.sc.rule() |
| 734 | return plan{h: rule, w: w, paint: func(x, y, colW, _ float64) []Op { |
| 735 | return []Op{Rect{X: x + (colW-w)/2, Y: y, W: w, H: rule, Fill: e.accent, Radius: rule / 2}} |
| 736 | }} |
| 737 | } |
| 738 | |
| 739 | // canvas draws blocks where they were put (§2.3): each box is a |
| 740 | // fraction of the frame, and z is paint order. late is what nothing |
| 741 | // reaches (see slide), drawn after the rest at its own box. |
| 742 | func (e *engine) canvas(s deck.Slide, late []deck.Block, byID map[string]deck.Block, claimed, done map[string]bool) { |
| 743 | e.onCanvas = true |
| 744 | blocks := slices.Clone(s.Blocks) |
| 745 | // Stable, so two blocks at the same z keep the order the array |
| 746 | // had — the same tie-break render's reflow order takes. |
| 747 | slices.SortStableFunc(blocks, func(a, b deck.Block) int { return a.Z - b.Z }) |
| 748 | roots := make([]deck.Block, 0, len(blocks)+len(late)) |
| 749 | for _, b := range blocks { |
| 750 | if !claimed[b.ID] { |
| 751 | roots = append(roots, b) |
| 752 | } |
| 753 | } |
| 754 | roots = append(roots, late...) |
| 755 | |
| 756 | for _, b := range roots { |
| 757 | if done[b.ID] { |
| 758 | continue |
| 759 | } |
| 760 | done[b.ID] = true |
| 761 | box := canvasBox(b.Box) |
| 762 | p := e.plan(b, slot{w: box.W, h: box.H, align: Start}, byID, done) |
| 763 | if p.paint != nil { |
| 764 | e.draw(p.paint(box.X, box.Y, box.W, p.h)...) |
| 765 | } |
| 766 | if p.h > box.H { |
| 767 | e.warn(Warning{Kind: WarnOverflow, Block: b.ID, Detail: fmt.Sprintf( |
| 768 | "a %s block holds %.0fpx of content in a %.0fpx box; it is drawn past the box rather than cut", |
| 769 | b.Type, p.h, box.H)}) |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // canvasBox turns §2.3's fractions into frame coordinates. A block |
| 775 | // with no box at all — which the core does not produce and a peer |
| 776 | // mid-edit might — gets the content box, which is where a browser's |
| 777 | // static position would put it. |
| 778 | func canvasBox(b *deck.Box) Box { |
| 779 | if b == nil { |
| 780 | return Box{contentX, contentY, contentW, contentH} |
| 781 | } |
| 782 | return Box{X: b.X * FrameW, Y: b.Y * FrameH, W: b.W * FrameW, H: b.H * FrameH} |
| 783 | } |
| 784 | |
| 785 | // unreached is every block no traversal from the slide's roots — the |
| 786 | // blocks no columns block claims — arrives at: the members of a cycle |
| 787 | // of columns blocks and whatever hangs under them. Array order. The |
| 788 | // same walk as render's, because the two have to agree on what is on |
| 789 | // the slide. |
| 790 | func unreached(blocks []deck.Block, byID map[string]deck.Block, claimed map[string]bool) []deck.Block { |
| 791 | reached := map[string]bool{} |
| 792 | var visit func(b deck.Block) |
| 793 | visit = func(b deck.Block) { |
| 794 | if reached[b.ID] { |
| 795 | return |
| 796 | } |
| 797 | reached[b.ID] = true |
| 798 | if b.Type != "columns" { |
| 799 | return |
| 800 | } |
| 801 | for _, id := range childIDs(b) { |
| 802 | if child, ok := byID[id]; ok { |
| 803 | visit(child) |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | for _, b := range blocks { |
| 808 | if !claimed[b.ID] { |
| 809 | visit(b) |
| 810 | } |
| 811 | } |
| 812 | var out []deck.Block |
| 813 | for _, b := range blocks { |
| 814 | if !reached[b.ID] { |
| 815 | out = append(out, b) |
| 816 | } |
| 817 | } |
| 818 | return out |
| 819 | } |
| 820 | |
| 821 | func firstID(blocks []deck.Block) string { |
| 822 | if len(blocks) == 0 { |
| 823 | return "" |
| 824 | } |
| 825 | return blocks[0].ID |
| 826 | } |
| 827 | |