| 1 | package layout_test |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | qrcode "github.com/skip2/go-qrcode" |
| 9 | |
| 10 | "amadan.net/oficina/slides/internal/slides/deck" |
| 11 | "amadan.net/oficina/slides/internal/slides/layout" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | qrURL = "https://slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde?follow=1" |
| 16 | qrAddress = "slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde" |
| 17 | ) |
| 18 | |
| 19 | func qrSlide(layoutName string, b deck.Block) *deck.Deck { |
| 20 | return &deck.Deck{ |
| 21 | Meta: deck.Meta{Title: "Follow", Theme: "paper", Aspect: "16:9"}, |
| 22 | Slides: []deck.Slide{{ |
| 23 | ID: "slide0001", Layout: layoutName, Template: "title-body", |
| 24 | Blocks: []deck.Block{b}, |
| 25 | }}, |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // codeIn reads the paint list back into modules: the first rectangle |
| 30 | // in the background colour is the code's square, quiet zone included, |
| 31 | // and every rectangle in the text colour inside it is a run of dark |
| 32 | // modules, sampled at each module's centre. |
| 33 | func codeIn(t *testing.T, p layout.Paint, fg, bg layout.Fill, n int) (grid [][]bool, square layout.Rect) { |
| 34 | t.Helper() |
| 35 | var runs []layout.Rect |
| 36 | found := false |
| 37 | for _, op := range p.Ops { |
| 38 | r, ok := op.(layout.Rect) |
| 39 | if !ok { |
| 40 | continue |
| 41 | } |
| 42 | if !found && r.Fill == bg { |
| 43 | square, found = r, true |
| 44 | continue |
| 45 | } |
| 46 | if found && r.Fill == fg { |
| 47 | if !r.Crisp { |
| 48 | t.Errorf("a run of modules %+v is not crisp: a thumbnail would show seams between rows", r) |
| 49 | } |
| 50 | runs = append(runs, r) |
| 51 | } |
| 52 | } |
| 53 | if !found { |
| 54 | t.Fatalf("no square behind the code in %d ops", len(p.Ops)) |
| 55 | } |
| 56 | if math.Abs(square.W-square.H) > 1e-9 { |
| 57 | t.Fatalf("the code is %v by %v, not square", square.W, square.H) |
| 58 | } |
| 59 | unit := square.W / float64(n) |
| 60 | grid = make([][]bool, n) |
| 61 | for y := range grid { |
| 62 | grid[y] = make([]bool, n) |
| 63 | for x := range grid[y] { |
| 64 | cx, cy := square.X+(float64(x)+0.5)*unit, square.Y+(float64(y)+0.5)*unit |
| 65 | for _, r := range runs { |
| 66 | if cx > r.X && cx < r.X+r.W && cy > r.Y && cy < r.Y+r.H { |
| 67 | grid[y][x] = true |
| 68 | break |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return grid, square |
| 74 | } |
| 75 | |
| 76 | func textOf(p layout.Paint) string { |
| 77 | var b strings.Builder |
| 78 | for _, op := range p.Ops { |
| 79 | if t, ok := op.(layout.Text); ok { |
| 80 | for _, r := range t.Runs { |
| 81 | b.WriteString(r.S) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return b.String() |
| 86 | } |
| 87 | |
| 88 | // TestAFollowAlongCodeIsInThePictureAndThePDF: the paint list — the |
| 89 | // one input to a thumbnail and to the PDF — carries the same code the |
| 90 | // live slide draws, module for module, in the slide's text colour on |
| 91 | // its background, inside the box, with the short address under it in |
| 92 | // the body face. |
| 93 | func TestAFollowAlongCodeIsInThePictureAndThePDF(t *testing.T) { |
| 94 | want, err := qrcode.New(qrURL, qrcode.Medium) |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | bitmap := want.Bitmap() |
| 99 | box := &deck.Box{X: 0.6, Y: 0.1, W: 0.3, H: 0.7} |
| 100 | for name, d := range map[string]*deck.Deck{ |
| 101 | "flow": qrSlide("flow", deck.Block{ID: "q1", Type: "qr", Region: "body", Props: map[string]any{"url": qrURL}}), |
| 102 | "canvas": qrSlide("canvas", deck.Block{ID: "q1", Type: "qr", Box: box, Props: map[string]any{"url": qrURL}}), |
| 103 | } { |
| 104 | t.Run(name, func(t *testing.T) { |
| 105 | p, warnings, err := layout.New(d, layout.Options{QR: map[string]string{qrURL: qrURL}}).Layout(0) |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | for _, w := range warnings { |
| 110 | t.Errorf("warning: %+v", w) |
| 111 | } |
| 112 | fg, bg := layout.Fill("#1c1a17"), layout.Fill("#faf7f0") |
| 113 | grid, square := codeIn(t, p, fg, bg, len(bitmap)) |
| 114 | for y := range bitmap { |
| 115 | for x := range bitmap[y] { |
| 116 | if grid[y][x] != bitmap[y][x] { |
| 117 | t.Fatalf("module (%d, %d) is %v, want %v", x, y, grid[y][x], bitmap[y][x]) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | if got := textOf(p); got != qrAddress { |
| 122 | t.Errorf("the printed address is %q, want %q", got, qrAddress) |
| 123 | } |
| 124 | for _, op := range p.Ops { |
| 125 | if tx, ok := op.(layout.Text); ok { |
| 126 | if tx.Y < square.Y+square.H { |
| 127 | t.Errorf("the address starts above the bottom of the code: %v < %v", tx.Y, square.Y+square.H) |
| 128 | } |
| 129 | for _, r := range tx.Runs { |
| 130 | if !strings.HasPrefix(r.Face.Name(), "LiberationSans") || r.Fill != fg { |
| 131 | t.Errorf("the address is set in %s %s, want the body face in the text colour", r.Face.Name(), r.Fill) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | if name == "canvas" { |
| 137 | x, y, w, h := box.X*layout.FrameW, box.Y*layout.FrameH, box.W*layout.FrameW, box.H*layout.FrameH |
| 138 | if square.X < x-1e-6 || square.Y < y-1e-6 || square.X+square.W > x+w+1e-6 || square.Y+square.H > y+h+1e-6 { |
| 139 | t.Errorf("the code %+v is outside its box (%v, %v, %v, %v)", square, x, y, w, h) |
| 140 | } |
| 141 | if square.W < w-1e-6 && square.H < h*0.5 { |
| 142 | t.Errorf("the code %+v does not fill its box's width", square) |
| 143 | } |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // TestAFollowAlongCodeWithNoLinkDrawsNothing: no follow link, nothing |
| 150 | // in the picture or the PDF — the editor's empty frame is the editor's. |
| 151 | // A follow link the picture's maker did not vouch for (Options.QR) is |
| 152 | // no link: nothing either. |
| 153 | func TestAFollowAlongCodeWithNoLinkDrawsNothing(t *testing.T) { |
| 154 | vouched := map[string]string{"https://elsewhere.example/phish": "https://elsewhere.example/phish"} |
| 155 | for _, props := range []map[string]any{nil, {"url": "https://elsewhere.example/phish"}, {"url": qrURL}} { |
| 156 | p, warnings, _ := layout.New(qrSlide("canvas", deck.Block{ID: "q1", Type: "qr", Box: &deck.Box{W: 0.3, H: 0.3}, Props: props}), layout.Options{QR: vouched}).Layout(0) |
| 157 | if len(p.Ops) != 0 || len(warnings) != 0 { |
| 158 | t.Errorf("%v: drew %d ops and %d warnings, want none", props, len(p.Ops), len(warnings)) |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |