| 1 | package layout |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/slides/internal/slides/deck" |
| 9 | "amadan.net/oficina/slides/internal/slides/fonts" |
| 10 | "amadan.net/oficina/slides/internal/slides/qr" |
| 11 | ) |
| 12 | |
| 13 | // blocks.go is one function per block type in §2.4's vocabulary. Each |
| 14 | // measures the block at the width it has been given and hands back a |
| 15 | // plan; nothing is positioned until the region or the canvas box says |
| 16 | // where the top of it goes. |
| 17 | // |
| 18 | // Every size, colour and gap in here is slides.css's, evaluated at |
| 19 | // 1920 (sizes.go). Where this file departs from the stylesheet it says |
| 20 | // so on the spot, and there is one such place: a shape's label is |
| 21 | // drawn (planShape says why). |
| 22 | |
| 23 | // slot is what a block is being asked to fit: the column width, a |
| 24 | // height it must fill (a canvas box, or image-full's media region; |
| 25 | // zero means "as tall as your content"), and the inline alignment it |
| 26 | // inherits. |
| 27 | type slot struct { |
| 28 | w, h float64 |
| 29 | align Align |
| 30 | } |
| 31 | |
| 32 | // plan is a measured block: how tall it turned out, how wide its |
| 33 | // content actually is, and how to draw it once its top-left is known. |
| 34 | // A plan with no paint draws nothing (a spacer, an unknown type). |
| 35 | type plan struct { |
| 36 | h, w float64 |
| 37 | // flex says the block can be shrunk to fit the space that is left |
| 38 | // (a picture and a shape can; wrapped text cannot), and minH is |
| 39 | // the floor it shrinks to. See engine.shrink. |
| 40 | flex bool |
| 41 | minH float64 |
| 42 | paint func(x, y, w, h float64) []Op |
| 43 | } |
| 44 | |
| 45 | // plan measures one block. byID and done are the columns machinery: |
| 46 | // a columns block lays its children out itself, and done stops any |
| 47 | // block being drawn twice. |
| 48 | func (e *engine) plan(b deck.Block, s slot, byID map[string]deck.Block, done map[string]bool) plan { |
| 49 | e.block = b.ID |
| 50 | switch b.Type { |
| 51 | case "heading": |
| 52 | return e.planHeading(b, s) |
| 53 | case "text": |
| 54 | return e.planText(b, s) |
| 55 | case "list": |
| 56 | return e.planList(b, s) |
| 57 | case "quote": |
| 58 | return e.planQuote(b, s) |
| 59 | case "code": |
| 60 | return e.planCode(b, s) |
| 61 | case "table": |
| 62 | return e.planTable(b, s) |
| 63 | case "shape": |
| 64 | return e.planShape(b, s) |
| 65 | case "image": |
| 66 | return e.planImage(b, s) |
| 67 | case "scene": |
| 68 | return e.planScene(b, s) |
| 69 | case "clip": |
| 70 | return e.planClip(b, s) |
| 71 | case "divider": |
| 72 | return e.planDivider(s) |
| 73 | case "spacer": |
| 74 | return plan{h: e.spacerHeight(b)} |
| 75 | case "qr": |
| 76 | return e.planQR(b, s) |
| 77 | case "columns": |
| 78 | return e.planColumns(b, s, byID, done) |
| 79 | } |
| 80 | // An unknown type is a block from a newer build of the core. |
| 81 | // Drawing nothing keeps the rest of the slide standing. |
| 82 | return plan{} |
| 83 | } |
| 84 | |
| 85 | // textPlan wraps a block's text and turns it into a plan, reporting a |
| 86 | // line that came out wider than its column — a word longer than the |
| 87 | // box, which is drawn overflowing rather than broken or cut. |
| 88 | func (e *engine) textPlan(t deck.Text, base style, s slot, lead, paraGap float64) plan { |
| 89 | tb := e.wrap(t, base, s.w, lead, paraGap) |
| 90 | e.noteWideLines(tb, s.w) |
| 91 | return plan{ |
| 92 | h: tb.h, w: widest(tb), |
| 93 | paint: func(x, y, w, _ float64) []Op { return e.paint(tb, x, y, w, s.align) }, |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func (e *engine) noteWideLines(tb *textBlock, w float64) { |
| 98 | for _, ln := range tb.lines { |
| 99 | if ln.overflowed { |
| 100 | e.warn(Warning{Kind: WarnOverflow, Block: e.block, Detail: fmt.Sprintf( |
| 101 | "a word is wider than its %.0fpx column (%.0fpx); it is drawn past the edge rather than broken", |
| 102 | w, ln.w)}) |
| 103 | return |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func widest(tb *textBlock) float64 { |
| 109 | var w float64 |
| 110 | for _, ln := range tb.lines { |
| 111 | w = max(w, ln.w) |
| 112 | } |
| 113 | return w |
| 114 | } |
| 115 | |
| 116 | // ── Text-bearing blocks ────────────────────────────────────────────── |
| 117 | |
| 118 | // planHeading is `h1` to `h3`: the heading face, the top of the type |
| 119 | // scale, and `font-weight: 600`, which with a 400/700 pair on offer is |
| 120 | // the bold face (CSS matches a weight over 500 upwards first). A level |
| 121 | // 3 heading is the accent colour, as the stylesheet has it. |
| 122 | func (e *engine) planHeading(b deck.Block, s slot) plan { |
| 123 | level := int(numProp(b.Props, "level", 1)) |
| 124 | if level < 1 || level > 3 { |
| 125 | level = 1 |
| 126 | } |
| 127 | size := [...]float64{e.sc.t1(), e.sc.t2(), e.sc.t3()}[level-1] |
| 128 | fill := e.fg |
| 129 | if level == 3 { |
| 130 | fill = e.accent |
| 131 | } |
| 132 | st := style{face: e.head, size: size, fill: fill, track: trackHeading} |
| 133 | st.face.Bold = true |
| 134 | // A heading is one line by the schema; a stray second paragraph is |
| 135 | // a line break and not a new paragraph, which is what render does |
| 136 | // with it too (<br>, not a second <h1>), so there is no gap. |
| 137 | return e.textPlan(b.Text, st, s, leadHeading, 0) |
| 138 | } |
| 139 | |
| 140 | func (e *engine) planText(b deck.Block, s slot) plan { |
| 141 | size := e.sc.bodySize(sizeProp(b.Props, "m")) |
| 142 | s.align = alignProp(b.Props, s.align) |
| 143 | st := style{face: e.body, size: size, fill: e.fg} |
| 144 | return e.textPlan(b.Text, st, s, leadBody, paraGapEm*size) |
| 145 | } |
| 146 | |
| 147 | // planList is one item per paragraph (§2.6), each with a marker in the |
| 148 | // accent colour hanging in the padding to the left of the text — which |
| 149 | // is what `list-style-position: outside` does, and why a wrapped item |
| 150 | // lines up under its own first line rather than under the bullet. |
| 151 | func (e *engine) planList(b deck.Block, s slot) plan { |
| 152 | size := e.sc.bodySize(sizeProp(b.Props, "m")) |
| 153 | numbered, _ := b.Props["style"].(string) |
| 154 | indent := int(numProp(b.Props, "indent", 0)) |
| 155 | if indent < 0 { |
| 156 | indent = 0 |
| 157 | } |
| 158 | if indent > 3 { |
| 159 | indent = 3 |
| 160 | } |
| 161 | inset := listIndentEm[indent] * size |
| 162 | st := style{face: e.body, size: size, fill: e.fg} |
| 163 | markerSt := style{face: e.body, size: size, fill: e.accent} |
| 164 | itemGap := listGapEm * size |
| 165 | |
| 166 | type item struct { |
| 167 | tb *textBlock |
| 168 | marker string |
| 169 | markW float64 |
| 170 | } |
| 171 | items := make([]item, 0, len(b.Text)) |
| 172 | var h, w float64 |
| 173 | for i, para := range b.Text { |
| 174 | tb := e.wrap(deck.Text{para}, st, s.w-inset, leadBody, 0) |
| 175 | e.noteWideLines(tb, s.w-inset) |
| 176 | marker := "•" |
| 177 | if numbered == "number" { |
| 178 | marker = strconv.Itoa(i+1) + "." |
| 179 | } |
| 180 | it := item{tb: tb, marker: marker, markW: e.measure(markerSt, marker)} |
| 181 | if i > 0 { |
| 182 | h += itemGap |
| 183 | } |
| 184 | h += tb.h |
| 185 | w = max(w, inset+widest(tb)) |
| 186 | items = append(items, it) |
| 187 | } |
| 188 | |
| 189 | return plan{h: h, w: w, paint: func(x, y, colW, _ float64) []Op { |
| 190 | var ops []Op |
| 191 | top := y |
| 192 | for i, it := range items { |
| 193 | if i > 0 { |
| 194 | top += itemGap |
| 195 | } |
| 196 | ops = append(ops, e.paint(it.tb, x+inset, top, colW-inset, s.align)...) |
| 197 | if len(it.tb.lines) > 0 { |
| 198 | ln := it.tb.lines[0] |
| 199 | baseline := top + (ln.height-(ln.asc+ln.desc))/2 + ln.asc |
| 200 | // The marker sits in the padding, its end a third of an |
| 201 | // em clear of the text. |
| 202 | mx := x + inset - 0.35*size - it.markW |
| 203 | ops = append(ops, Text{X: mx, Y: baseline, Runs: []Run{ |
| 204 | {S: it.marker, Face: markerSt.face, Size: size, Fill: e.accent}, |
| 205 | }}) |
| 206 | } |
| 207 | top += it.tb.h |
| 208 | } |
| 209 | return ops |
| 210 | }} |
| 211 | } |
| 212 | |
| 213 | // planQuote is a pull quote: the words in italic at the largest body |
| 214 | // size, and the attribution under them in the accent colour with the |
| 215 | // em dash the stylesheet's ::before puts there. |
| 216 | func (e *engine) planQuote(b deck.Block, s slot) plan { |
| 217 | size := e.sc.bodyL() |
| 218 | st := style{face: e.body, size: size, fill: e.fg} |
| 219 | st.face.Italic = true |
| 220 | tb := e.wrap(b.Text, st, s.w, leadBody, paraGapEm*size) |
| 221 | e.noteWideLines(tb, s.w) |
| 222 | |
| 223 | var cite *textBlock |
| 224 | citeSize := size * citeEm |
| 225 | if c := stringProp(b.Props, "cite"); c != "" { |
| 226 | citeSt := style{face: e.body, size: citeSize, fill: e.accent} |
| 227 | cite = e.wrap(deck.Text{{{T: "— " + c}}}, citeSt, s.w, leadBody, 0) |
| 228 | } |
| 229 | |
| 230 | h, w := tb.h, widest(tb) |
| 231 | if cite != nil { |
| 232 | h += quoteGap*size + cite.h |
| 233 | w = max(w, widest(cite)) |
| 234 | } |
| 235 | return plan{h: h, w: w, paint: func(x, y, colW, _ float64) []Op { |
| 236 | ops := e.paint(tb, x, y, colW, s.align) |
| 237 | if cite != nil { |
| 238 | ops = append(ops, e.paint(cite, x, y+tb.h+quoteGap*size, colW, s.align)...) |
| 239 | } |
| 240 | return ops |
| 241 | }} |
| 242 | } |
| 243 | |
| 244 | // planCode is a panel with one line of monospace per paragraph (§2.4), |
| 245 | // never wrapped: the stylesheet gives a code block `overflow-x: auto`, |
| 246 | // so on screen a long line scrolls, and breaking one here would put a |
| 247 | // line in the PDF that is not in the deck. |
| 248 | func (e *engine) planCode(b deck.Block, s slot) plan { |
| 249 | size := e.sc.code() |
| 250 | inset := codePadEm * size |
| 251 | st := style{face: e.code, size: size, fill: e.fg} |
| 252 | lineH := leadCode * size |
| 253 | asc, desc, _ := fonts.Metrics(st.face, size) |
| 254 | |
| 255 | lines := make([]string, 0, len(b.Text)) |
| 256 | var widest float64 |
| 257 | for _, para := range b.Text { |
| 258 | // `tab-size: 2`. Measured as two spaces because a tab has no |
| 259 | // glyph and would otherwise be drawn at .notdef's width. |
| 260 | text := strings.ReplaceAll(plainText(para), "\t", " ") |
| 261 | lines = append(lines, text) |
| 262 | widest = max(widest, e.measure(st, text)) |
| 263 | } |
| 264 | if len(lines) == 0 { |
| 265 | lines = append(lines, "") |
| 266 | } |
| 267 | h := 2*inset + float64(len(lines))*lineH |
| 268 | if widest > s.w-2*inset { |
| 269 | e.warn(Warning{Kind: WarnOverflow, Block: b.ID, Detail: fmt.Sprintf( |
| 270 | "a code line is %.0fpx wide in a %.0fpx panel; on screen it scrolls, in print it is drawn past the edge", |
| 271 | widest, s.w-2*inset)}) |
| 272 | } |
| 273 | |
| 274 | panel := mix(e.fg, e.bg, mixCodePanel) |
| 275 | return plan{h: h, w: widest + 2*inset, paint: func(x, y, colW, _ float64) []Op { |
| 276 | ops := []Op{Rect{X: x, Y: y, W: colW, H: h, Fill: panel, Radius: codeRadiusEm * size}} |
| 277 | top := y + inset |
| 278 | for _, text := range lines { |
| 279 | baseline := top + (lineH-(asc+desc))/2 + asc |
| 280 | if text != "" { |
| 281 | ops = append(ops, Text{X: x + inset, Y: baseline, Runs: []Run{ |
| 282 | {S: text, Face: st.face, Size: size, Fill: st.fill}, |
| 283 | }}) |
| 284 | } |
| 285 | top += lineH |
| 286 | } |
| 287 | return ops |
| 288 | }} |
| 289 | } |
| 290 | |
| 291 | // planTable is equal columns, a rule under every row, and a header row |
| 292 | // in bold over an accent rule (§2.4, and the stylesheet's `th`). |
| 293 | func (e *engine) planTable(b deck.Block, s slot) plan { |
| 294 | cells := cellsProp(b.Props) |
| 295 | cols := int(numProp(b.Props, "cols", 0)) |
| 296 | if cols < 1 { |
| 297 | cols = len(cells) |
| 298 | } |
| 299 | if cols < 1 { |
| 300 | return plan{} |
| 301 | } |
| 302 | header := boolProp(b.Props, "header") |
| 303 | size := e.sc.bodyS() |
| 304 | padX, padY := cellPadXEm*size, cellPadYEm*size |
| 305 | colW := s.w / float64(cols) |
| 306 | |
| 307 | base := style{face: e.body, size: size, fill: e.fg} |
| 308 | head := base |
| 309 | head.face.Bold = true |
| 310 | |
| 311 | type row struct { |
| 312 | cells []*textBlock |
| 313 | h float64 |
| 314 | head bool |
| 315 | } |
| 316 | var rows []row |
| 317 | for i := 0; i < len(cells); i += cols { |
| 318 | end := min(i+cols, len(cells)) |
| 319 | r := row{head: header && len(rows) == 0} |
| 320 | st := base |
| 321 | if r.head { |
| 322 | st = head |
| 323 | } |
| 324 | for _, cell := range cells[i:end] { |
| 325 | tb := e.wrap(cell, st, colW-2*padX, leadBody, paraGapEm*size) |
| 326 | r.cells = append(r.cells, tb) |
| 327 | r.h = max(r.h, tb.h) |
| 328 | } |
| 329 | r.h += 2 * padY |
| 330 | rows = append(rows, r) |
| 331 | } |
| 332 | |
| 333 | var h float64 |
| 334 | for _, r := range rows { |
| 335 | h += r.h + cellRule |
| 336 | } |
| 337 | ruleFill := mix(e.fg, e.bg, mixTableRule) |
| 338 | return plan{h: h, w: s.w, paint: func(x, y, colWidth, _ float64) []Op { |
| 339 | var ops []Op |
| 340 | cw := colWidth / float64(cols) |
| 341 | top := y |
| 342 | for _, r := range rows { |
| 343 | for i, cell := range r.cells { |
| 344 | ops = append(ops, e.paint(cell, x+float64(i)*cw+padX, top+padY, cw-2*padX, Start)...) |
| 345 | } |
| 346 | bottom := top + r.h |
| 347 | stroke := ruleFill |
| 348 | if r.head { |
| 349 | stroke = e.accent |
| 350 | } |
| 351 | ops = append(ops, Line{X1: x, Y1: bottom, X2: x + colWidth, Y2: bottom, Stroke: stroke, W: cellRule}) |
| 352 | top = bottom + cellRule |
| 353 | } |
| 354 | return ops |
| 355 | }} |
| 356 | } |
| 357 | |
| 358 | // ── Blocks that are not text ───────────────────────────────────────── |
| 359 | |
| 360 | // planShape draws §2.4's four kinds, mapping render's 0..100 viewBox |
| 361 | // onto the box: the 1-unit inset it draws with becomes one percent of |
| 362 | // the box, and a radius is read the same way (`rx="12"` in that |
| 363 | // viewBox is 12% of the width). |
| 364 | // |
| 365 | // A flow shape starts as tall as it is wide: the stylesheet gives it |
| 366 | // `inline-size: 100%; block-size: auto` over a 1:1 viewBox, and a |
| 367 | // replaced element with an intrinsic ratio and no height is square. |
| 368 | // That is nearly always taller than the region, so it is marked |
| 369 | // flexible and engine.shrink squeezes it down to what is left, no |
| 370 | // further than `min-block-size: 8cqw`. Which is what the browser does |
| 371 | // with it. |
| 372 | // |
| 373 | // One departure from the browser, stated rather than hidden: |
| 374 | // |
| 375 | // - The label is DRAWN, centred. render puts it in the SVG's |
| 376 | // <title>, where a browser announces it and does not paint it; a |
| 377 | // paint list has no accessibility tree to put it in, and dropping |
| 378 | // it would lose the words from the export altogether. |
| 379 | func (e *engine) planShape(b deck.Block, s slot) plan { |
| 380 | kind := stringProp(b.Props, "kind") |
| 381 | switch kind { |
| 382 | case "rect", "ellipse", "line", "arrow": |
| 383 | default: |
| 384 | kind = "rect" |
| 385 | } |
| 386 | fill := e.color(stringProp(b.Props, "fill"), "") |
| 387 | stroke := e.color(stringProp(b.Props, "stroke"), e.fg) |
| 388 | strokeW := clamp(numProp(b.Props, "strokeWidth", 1), 0, 50) |
| 389 | radius := clamp(numProp(b.Props, "radius", 0), 0, 50) |
| 390 | |
| 391 | h := s.h |
| 392 | if h == 0 { |
| 393 | h = max(s.w, shapeMinBlockCqw*e.sc.cqw) |
| 394 | } |
| 395 | |
| 396 | label := b.Text.Plain() |
| 397 | labelSt := style{face: e.body, size: e.sc.bodyM(), fill: e.fg} |
| 398 | var labelBlock *textBlock |
| 399 | if label != "" { |
| 400 | labelBlock = e.wrap(deck.Text{{{T: label}}}, labelSt, s.w, leadBody, 0) |
| 401 | } |
| 402 | |
| 403 | return plan{h: h, w: s.w, flex: s.h == 0, minH: shapeMinBlockCqw * e.sc.cqw, paint: func(x, y, w, h float64) []Op { |
| 404 | insetX, insetY := w*0.01, h*0.01 |
| 405 | var ops []Op |
| 406 | switch kind { |
| 407 | case "ellipse": |
| 408 | ops = append(ops, Ellipse{X: x + insetX, Y: y + insetY, W: w - 2*insetX, H: h - 2*insetY, |
| 409 | Fill: fill, Stroke: stroke, StrokeW: strokeW}) |
| 410 | case "line", "arrow": |
| 411 | ops = append(ops, Line{ |
| 412 | X1: x + insetX, Y1: y + h/2, X2: x + w - insetX, Y2: y + h/2, |
| 413 | Stroke: stroke, W: strokeW, Arrow: kind == "arrow"}) |
| 414 | default: |
| 415 | ops = append(ops, Rect{X: x + insetX, Y: y + insetY, W: w - 2*insetX, H: h - 2*insetY, |
| 416 | Fill: fill, Stroke: stroke, StrokeW: strokeW, Radius: radius / 100 * w}) |
| 417 | } |
| 418 | if labelBlock != nil { |
| 419 | ops = append(ops, e.paint(labelBlock, x, y+(h-labelBlock.h)/2, w, Center)...) |
| 420 | } |
| 421 | return ops |
| 422 | }} |
| 423 | } |
| 424 | |
| 425 | // planImage places a picture. In a region it is as tall as its own |
| 426 | // aspect ratio makes it at the column width; in a canvas box or |
| 427 | // image-full's media region it fills the box, which is what |
| 428 | // `block-size: 100%` does. |
| 429 | // |
| 430 | // An asset the deck never registered draws nothing and is reported. |
| 431 | // render puts an empty frame there for the editor to show as |
| 432 | // unfinished; an export has nobody to show it to. |
| 433 | func (e *engine) planImage(b deck.Block, s slot) plan { |
| 434 | id := stringProp(b.Props, "asset") |
| 435 | asset, ok := e.deck.Assets[id] |
| 436 | if !ok { |
| 437 | e.warn(Warning{Kind: WarnAsset, Block: b.ID, Detail: fmt.Sprintf( |
| 438 | "this image names asset %q, which this deck never registered", id)}) |
| 439 | return plan{} |
| 440 | } |
| 441 | fit := "cover" |
| 442 | if f, _ := b.Props["fit"].(string); f == "contain" { |
| 443 | fit = "contain" |
| 444 | } |
| 445 | alt, _ := b.Props["alt"].(string) |
| 446 | if alt == "" && !boolProp(b.Props, "decorative") { |
| 447 | alt = asset.Alt |
| 448 | } |
| 449 | |
| 450 | h := s.h |
| 451 | if h == 0 { |
| 452 | ratio := imageFallback |
| 453 | if asset.Width > 0 && asset.Height > 0 { |
| 454 | ratio = float64(asset.Height) / float64(asset.Width) |
| 455 | } |
| 456 | h = s.w * ratio |
| 457 | } |
| 458 | return plan{h: h, w: s.w, flex: s.h == 0, paint: func(x, y, w, h float64) []Op { |
| 459 | return []Op{Image{X: x, Y: y, W: w, H: h, Asset: id, Fit: fit, Alt: alt}} |
| 460 | }} |
| 461 | } |
| 462 | |
| 463 | // planClip is a clip block in a picture (scenes and clips spec §2): its |
| 464 | // poster, the picture of its video's first frame, sized as an image of |
| 465 | // the video's size is and drawn whole (slides.css's object-fit: contain |
| 466 | // on [data-block="clip"]), described as an image is. A clip |
| 467 | // whose video or poster the deck doesn't hold draws nothing, as an |
| 468 | // image with no asset does. |
| 469 | func (e *engine) planClip(b deck.Block, s slot) plan { |
| 470 | id := stringProp(b.Props, "asset") |
| 471 | poster, _, ok := e.deck.PosterOf(id) |
| 472 | if !ok { |
| 473 | e.warn(Warning{Kind: WarnAsset, Block: b.ID, Detail: fmt.Sprintf( |
| 474 | "this clip names asset %q, which is not a video with a poster this deck registered", id)}) |
| 475 | return plan{} |
| 476 | } |
| 477 | video := e.deck.Assets[id] |
| 478 | // Described as a picture is: the block's own, none when decorative, |
| 479 | // else the video's. |
| 480 | alt, _ := b.Props["alt"].(string) |
| 481 | if alt == "" && !boolProp(b.Props, "decorative") { |
| 482 | alt = video.Alt |
| 483 | } |
| 484 | h := s.h |
| 485 | if h == 0 { |
| 486 | ratio := imageFallback |
| 487 | if video.Width > 0 && video.Height > 0 { |
| 488 | ratio = float64(video.Height) / float64(video.Width) |
| 489 | } |
| 490 | h = s.w * ratio |
| 491 | } |
| 492 | return plan{h: h, w: s.w, flex: s.h == 0, paint: func(x, y, w, h float64) []Op { |
| 493 | return []Op{Image{X: x, Y: y, W: w, H: h, Asset: poster, Fit: "contain", Alt: alt}} |
| 494 | }} |
| 495 | } |
| 496 | |
| 497 | func (e *engine) planDivider(s slot) plan { |
| 498 | fill := mix(e.fg, e.bg, mixDivider) |
| 499 | rule := e.sc.rule() |
| 500 | return plan{h: rule, w: s.w, paint: func(x, y, w, _ float64) []Op { |
| 501 | return []Op{Rect{X: x, Y: y, W: w, H: rule, Fill: fill, Radius: rule / 2}} |
| 502 | }} |
| 503 | } |
| 504 | |
| 505 | // planQR is a follow-along code (render.go's qrBlock): the code for the |
| 506 | // box's follow link, a square of the slide's background with the dark |
| 507 | // modules in its text colour, one rectangle per run of them |
| 508 | // (qr.Runs), and the short address under it in the body face at the |
| 509 | // small body size, centred, breaking anywhere (`overflow-wrap: |
| 510 | // anywhere`), since an address has no spaces to break at. |
| 511 | // |
| 512 | // In a region the code is as wide as the column up to 30cqw; in a box |
| 513 | // it is as large as the box leaves once the address is under it, and |
| 514 | // centred in that space, which is what an SVG's default |
| 515 | // `preserveAspectRatio` does with the space the stylesheet gives it. |
| 516 | // What is drawn is Options.QR's address for the stored url (render.go's |
| 517 | // qrBlock says why); a url not there draws nothing: the editor's empty |
| 518 | // frame is the editor's, and an export has nobody to show it to. |
| 519 | func (e *engine) planQR(b deck.Block, s slot) plan { |
| 520 | url, ok := e.render.opts.QR[stringProp(b.Props, "url")] |
| 521 | address := "" |
| 522 | if ok { |
| 523 | address, ok = deck.FollowAddress(url) |
| 524 | } |
| 525 | if !ok { |
| 526 | return plan{} |
| 527 | } |
| 528 | modules, err := qr.Modules(url) |
| 529 | if err != nil { |
| 530 | return plan{} |
| 531 | } |
| 532 | st := style{face: e.body, size: e.sc.bodyS(), fill: e.fg} |
| 533 | caption := e.breakAnywhere(address, st, s.w) |
| 534 | gap := qrGapEm * st.size |
| 535 | |
| 536 | area := min(s.w, qrFlowCqw*e.sc.cqw) |
| 537 | h := area + gap + caption.h |
| 538 | if s.h > 0 { |
| 539 | area, h = max(s.h-gap-caption.h, 0), s.h |
| 540 | } |
| 541 | n := float64(len(modules)) |
| 542 | runs := qr.Runs(modules) |
| 543 | return plan{h: h, w: s.w, paint: func(x, y, w, _ float64) []Op { |
| 544 | side := min(w, area) |
| 545 | qx, qy := x+(w-side)/2, y+(area-side)/2 |
| 546 | unit := side / n |
| 547 | ops := make([]Op, 0, len(runs)+1+len(caption.lines)) |
| 548 | ops = append(ops, Rect{X: qx, Y: qy, W: side, H: side, Fill: e.bg}) |
| 549 | for _, r := range runs { |
| 550 | ops = append(ops, Rect{X: qx + float64(r.X)*unit, Y: qy + float64(r.Y)*unit, |
| 551 | W: float64(r.N) * unit, H: unit, Fill: e.fg, Crisp: true}) |
| 552 | } |
| 553 | return append(ops, e.paint(caption, x, y+area+gap, w, Center)...) |
| 554 | }} |
| 555 | } |
| 556 | |
| 557 | // breakAnywhere sets s as lines no wider than w, breaking between any |
| 558 | // two characters: as many as fit on each line, and at least one. |
| 559 | func (e *engine) breakAnywhere(s string, st style, w float64) *textBlock { |
| 560 | asc, desc, _ := fonts.Metrics(st.face, st.size) |
| 561 | lineH := leadBody * st.size |
| 562 | out := &textBlock{} |
| 563 | rs := []rune(s) |
| 564 | for len(rs) > 0 { |
| 565 | n := 1 |
| 566 | for n < len(rs) && e.measure(st, string(rs[:n+1])) <= w { |
| 567 | n++ |
| 568 | } |
| 569 | text := string(rs[:n]) |
| 570 | width := e.measure(st, text) |
| 571 | out.lines = append(out.lines, line{ |
| 572 | toks: []token{{s: text, st: st, w: width}}, w: width, height: lineH, |
| 573 | asc: asc, desc: desc, overflowed: width > w, |
| 574 | }) |
| 575 | out.h += lineH |
| 576 | rs = rs[n:] |
| 577 | } |
| 578 | return out |
| 579 | } |
| 580 | |
| 581 | // spacerHeight is `[data-block="spacer"]`'s three sizes. It draws |
| 582 | // nothing; it is the gap it makes. |
| 583 | func (e *engine) spacerHeight(b deck.Block) float64 { |
| 584 | switch sizeProp(b.Props, "l") { |
| 585 | case "s": |
| 586 | return e.sc.gap() / 2 |
| 587 | case "l": |
| 588 | return e.sc.gap() * 2 |
| 589 | } |
| 590 | return e.sc.gap() |
| 591 | } |
| 592 | |
| 593 | // planColumns lays a columns block's children out side by side in the |
| 594 | // order props.children names them (§2.4), each in an equal column. |
| 595 | func (e *engine) planColumns(b deck.Block, s slot, byID map[string]deck.Block, done map[string]bool) plan { |
| 596 | count := int(numProp(b.Props, "count", 2)) |
| 597 | if count != 3 { |
| 598 | count = 2 |
| 599 | } |
| 600 | colWidth := (s.w - e.sc.gap()*float64(count-1)) / float64(count) |
| 601 | |
| 602 | var kids []plan |
| 603 | var h float64 |
| 604 | for _, id := range childIDs(b) { |
| 605 | child, ok := byID[id] |
| 606 | if !ok || done[id] { |
| 607 | continue |
| 608 | } |
| 609 | done[id] = true |
| 610 | p := e.plan(child, slot{w: colWidth, h: 0, align: s.align}, byID, done) |
| 611 | kids = append(kids, p) |
| 612 | h = max(h, p.h) |
| 613 | } |
| 614 | if len(kids) == 0 { |
| 615 | return plan{} |
| 616 | } |
| 617 | return plan{h: h, w: s.w, paint: func(x, y, w, _ float64) []Op { |
| 618 | cw := (w - e.sc.gap()*float64(count-1)) / float64(count) |
| 619 | var ops []Op |
| 620 | for i, p := range kids { |
| 621 | if p.paint == nil { |
| 622 | continue |
| 623 | } |
| 624 | ops = append(ops, p.paint(x+float64(i)*(cw+e.sc.gap()), y, cw, p.h)...) |
| 625 | } |
| 626 | return ops |
| 627 | }} |
| 628 | } |
| 629 | |
| 630 | // plainText is one paragraph's runs with the marks dropped — what a |
| 631 | // code block is (§2.4: plain, one paragraph per line). |
| 632 | func plainText(para []deck.Run) string { |
| 633 | var b strings.Builder |
| 634 | for _, r := range para { |
| 635 | b.WriteString(r.T) |
| 636 | } |
| 637 | return b.String() |
| 638 | } |
| 639 | |
| 640 | func clamp(v, lo, hi float64) float64 { |
| 641 | if v < lo { |
| 642 | return lo |
| 643 | } |
| 644 | if v > hi { |
| 645 | return hi |
| 646 | } |
| 647 | return v |
| 648 | } |
| 649 | |