oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3// The layout engine's own drive. internal/slides/layout measures a
4// slide the way slides.css would at 1920 by 1080 — it is a reading of
5// that stylesheet, done in Go, and everything downstream of it (the
6// SVG thumbnail, the PDF) inherits whatever that reading gets wrong.
7//
8// Nothing in the Go tests can catch a misreading: they check the engine
9// against itself. This file is the other half — a real Chromium laying
10// out real slide markup at 1920, with the numbers sizes.go and
11// regions.go were derived from asserted against what the browser
12// actually does.
13//
14// It caught two mistakes when it was written (slice 3 task 2), and
15// they are the reason it exists rather than a comment saying the
16// numbers looked right:
17//
18// - A container query unit is a percent of the container's CONTENT
19// box, so a `cqw` on a slide with the section's 120px padding is
20// 16.8px and not 19.2. Every type size on six of the seven
21// templates was 12.5% too big.
22// - A picture or a shape in a flow region is a flex item that
23// SHRINKS to the space that is left; the engine was stacking a
24// square 1680px picture and running three slides off the bottom.
25package slidestest
26
27import (
28 "encoding/json"
29 "fmt"
30 "strings"
31 "testing"
32
33 "github.com/chromedp/cdproto/runtime"
34 "github.com/chromedp/chromedp"
35)
36
37// awaitPromise makes chromedp.Evaluate wait for the promise the script
38// returns.
39func awaitPromise(p *runtime.EvaluateParams) { p.AwaitPromise = new(true) }
40
41// loadWebFacesJS loads every style of the named families (their file
42// prefixes) as FontFaces and adds them to document.fonts once all of
43// them are in, which is what the font gate does.
44func loadWebFacesJS(prefixes []string) string {
45 var faces []string
46 for _, p := range prefixes {
47 css := "Liberation " + strings.TrimPrefix(p, "Liberation")
48 for _, s := range []struct{ file, weight, style string }{
49 {"Regular", "400", "normal"}, {"Bold", "700", "normal"}, {"Italic", "400", "italic"}, {"BoldItalic", "700", "italic"},
50 } {
51 faces = append(faces, fmt.Sprintf(`new FontFace(%q, 'url("/static/fonts/%s-%s.woff2")', {weight: %q, style: %q})`, css, p, s.file, s.weight, s.style))
52 }
53 }
54 return `(async () => { const faces = [` + strings.Join(faces, ",") + `];
55 await Promise.all(faces.map(f => f.load())); for (const f of faces) document.fonts.add(f); return true })()`
56}
57
58// wantGeometry is what slides.css computes at 1920, and every number
59// in it has a twin in internal/slides/layout (sizes.go's scale, and
60// regions.go's table). When this fails, one of the two has moved: read
61// the diff before changing either.
62//
63// The tolerance is a pixel. Chromium rounds a used font size to a
64// 64th, so 121.968 comes back as 121.95, and pinning tighter than that
65// would fail on a browser upgrade without anything being wrong.
66const tolerance = 1.0
67
68type rect struct{ X, Y, W, H float64 }
69
70type geometry struct {
71 TwoColumnHeading rect `json:"twoColumnHeading"`
72 TwoColumnBody rect `json:"twoColumnBody"`
73 TwoColumnAside rect `json:"twoColumnAside"`
74 TitleHeading rect `json:"titleHeading"`
75 TitleBody rect `json:"titleBody"`
76 ImageFullMedia rect `json:"imageFullMedia"`
77 BlankRegion rect `json:"blankRegion"`
78 BlankText rect `json:"blankText"`
79 BlankImage rect `json:"blankImage"`
80 PaddedT1 float64 `json:"paddedT1"`
81 PaddedBodyM float64 `json:"paddedBodyM"`
82 PaddedBodyL float64 `json:"paddedBodyL"`
83 FullT1 float64 `json:"fullT1"`
84 FullT2 float64 `json:"fullT2"`
85 HeadingLine float64 `json:"headingLine"`
86 BodyLine float64 `json:"bodyLine"`
87 WideGap float64 `json:"wideGap"`
88 NarrowGap float64 `json:"narrowGap"`
89}
90
91func TestTheStylesheetMeasuresWhatTheLayoutEngineAssumes(t *testing.T) {
92 rig, _ := newRig(t)
93
94 const vars = `style="--slides-bg:#faf7f0;--slides-fg:#1c1a17;--slides-accent:#8a5410;--slides-accent-2:#46617a;--slides-font-heading:Liberation Serif, Georgia, serif;--slides-font-body:Liberation Sans, system-ui, sans-serif"`
95 // One section per template under test, at the frame's own width.
96 // Built by hand rather than through render.Slide because the point
97 // is the STYLESHEET: this is the markup render produces, and a
98 // change in either that broke the pairing should fail here.
99 const markup = `
100<div style="width:1920px;position:absolute;left:0;top:0">
101<section slides-slide data-layout="flow" data-template="two-column" data-theme="paper" ` + vars + `>
102<div slides-frame>
103<div slides-region data-region="heading" id="TCH"><h1 slides-block data-block="heading" data-level="1" id="TCH1">Regions, not coordinates</h1></div>
104<div slides-region data-region="body" id="TCB"><div slides-block data-block="text" data-size="m" id="TCT"><p>A flow slide names regions.</p></div></div>
105<div slides-region data-region="aside" id="TCA"><div slides-block data-block="text" data-size="m"><p>The aside.</p></div></div>
106</div>
107</section>
108<section slides-slide data-layout="flow" data-template="image-full" data-theme="paper" ` + vars + `>
109<div slides-frame>
110<div slides-region data-region="heading"><h2 slides-block data-block="heading" data-level="2" id="IFH2">A picture would go here</h2></div>
111<div slides-region data-region="media" id="IFM"><figure slides-block data-block="image" data-fit="cover"><img src="/static/fonts/nothing.png" alt="x" width="1600" height="900"></figure></div>
112</div>
113</section>
114<section slides-slide data-layout="canvas" data-template="blank" data-theme="paper" ` + vars + `>
115<h1 slides-block data-block="heading" data-level="1" data-z="1" id="CVH" style="left:6%;top:8%;width:88%;height:16%;z-index:1">Canvas</h1>
116</section>
117<section slides-slide data-layout="flow" data-template="title" data-theme="paper" ` + vars + `>
118<div slides-frame>
119<div slides-region data-region="heading" id="TH"><h1 slides-block data-block="heading" data-level="1">Renderer fixture</h1></div>
120<div slides-region data-region="body" id="TB"><div slides-block data-block="text" data-size="l" id="TT"><p>One section per slide, one element per block.</p></div></div>
121</div>
122</section>
123<section slides-slide data-layout="flow" data-template="blank" data-theme="paper" ` + vars + `>
124<div slides-frame>
125<div slides-region data-region="body" id="BR">
126<div slides-block data-block="text" data-size="m" id="BT"><p>One line.</p></div>
127<figure slides-block data-block="image" data-fit="contain" id="BI"><img src="/static/fonts/nothing.png" alt="x" width="64" height="64"></figure>
128</div>
129</div></section>
130</div>
131<div style="width:780px;position:absolute;left:0;top:2000px">
132<section slides-slide data-layout="flow" data-template="title-body" data-theme="paper" ` + vars + `>
133<div slides-frame>
134<div slides-region data-region="heading" id="NGH"><h1 slides-block data-block="heading" data-level="1">Narrow</h1></div>
135<div slides-region data-region="body" id="NGB"><div slides-block data-block="text" data-size="m"><p>Same slide, smaller.</p></div></div>
136</div>
137</section>
138</div>`
139
140 var raw string
141 rig.Run(
142 // A 1920 viewport, which is the projector and the export. The
143 // slide is drawn at 1920 in the page too — and the second copy
144 // at 780, which is what proves the gap no longer follows the
145 // window (see narrowGap below).
146 chromedp.EmulateViewport(1920, 1080),
147 chromedp.Navigate(rig.Origin+"/signin"),
148 evalInto(new(string), `(() => {
149 const link = document.createElement('link');
150 link.rel = 'stylesheet';
151 link.href = '/static/slides.css';
152 document.head.appendChild(link);
153 document.body.insertAdjacentHTML('beforeend', `+"`"+markup+"`"+`);
154 return 'ok';
155 })()`),
156 // The faces, the way the font gate puts them on a slide surface
157 // (fixtures/font-session.mjs): no stylesheet declares them any
158 // more, so a page that never ran the gate measures the system
159 // faces, which is not what this file is about.
160 chromedp.Evaluate[chromedp.Void](loadWebFacesJS([]string{"LiberationSans", "LiberationSerif"}), awaitPromise),
161 // Long enough for the stylesheet.
162 chromedp.Sleep(1_000_000_000),
163 evalInto(&raw, `(() => {
164 const q = id => { const e = document.getElementById(id); const r = e.getBoundingClientRect();
165 const p = e.closest('[slides-slide]').getBoundingClientRect();
166 return {X: r.x-p.x, Y: r.y-p.y, W: r.width, H: r.height}; };
167 const px = (id, prop) => parseFloat(getComputedStyle(document.getElementById(id))[prop]);
168 return JSON.stringify({
169 twoColumnHeading: q('TCH'), twoColumnBody: q('TCB'), twoColumnAside: q('TCA'),
170 titleHeading: q('TH'), titleBody: q('TB'),
171 imageFullMedia: q('IFM'),
172 blankRegion: q('BR'), blankText: q('BT'), blankImage: q('BI'),
173 paddedT1: px('TCH1','fontSize'), paddedBodyM: px('TCT','fontSize'), paddedBodyL: px('TT','fontSize'),
174 fullT1: px('CVH','fontSize'), fullT2: px('IFH2','fontSize'),
175 headingLine: px('TCH1','lineHeight'), bodyLine: px('TCT','lineHeight'),
176 wideGap: q('TCB').Y - (q('TCH').Y + q('TCH').H),
177 narrowGap: (() => { const h = document.getElementById('NGH').getBoundingClientRect();
178 const b = document.getElementById('NGB').getBoundingClientRect();
179 return b.y - (h.y + h.height); })(),
180 });
181 })()`),
182 )
183
184 var got geometry
185 if err := json.Unmarshal([]byte(raw), &got); err != nil {
186 t.Fatalf("read the browser's geometry: %v\n%s", err, raw)
187 }
188
189 // The type scale. 16.8 per cqw with the section's padding, 19.2
190 // without it (image-full and canvas both set `padding: 0`).
191 for _, c := range []struct {
192 name string
193 got, want float64
194 derivation string
195 }{
196 {"--slides-t-1 on a padded template", got.PaddedT1, 110.88, "6.6 × 16.8"},
197 {"--slides-body-m on a padded template", got.PaddedBodyM, 47.04, "2.8 × 16.8"},
198 {"--slides-body-l on a padded template", got.PaddedBodyL, 57.12, "3.4 × 16.8"},
199 {"--slides-t-1 on a canvas slide", got.FullT1, 126.72, "6.6 × 19.2"},
200 {"--slides-t-2 on image-full", got.FullT2, 96, "5 × 19.2"},
201 {"a heading's line box", got.HeadingLine, 121.968, "110.88 × 1.1"},
202 {"a body line box", got.BodyLine, 61.152, "47.04 × 1.3"},
203 } {
204 if diff := c.got - c.want; diff > tolerance || diff < -tolerance {
205 t.Errorf("%s is %v in the browser; layout/sizes.go says %v (%s)",
206 c.name, c.got, c.want, c.derivation)
207 }
208 }
209
210 // The region boxes, which are layout/regions.go's table.
211 for _, c := range []struct {
212 name string
213 got rect
214 want rect
215 }{
216 {"two-column heading", got.TwoColumnHeading, rect{120, 120, 1680, 121.968}},
217 {"two-column body", got.TwoColumnBody, rect{120, 285.648, 805.056, 674.352}},
218 {"two-column aside", got.TwoColumnAside, rect{994.944, 285.648, 805.056, 674.352}},
219 // title centres its two auto rows as a pair, so where each one
220 // lands depends on how tall the other is.
221 {"title heading", got.TitleHeading, rect{573.19, 420.048, 773.61, 121.968}},
222 {"title body", got.TitleBody, rect{386.98, 585.696, 1146.02, 74.256}},
223 {"image-full media", got.ImageFullMedia, rect{0, 0, 1920, 1080}},
224 {"a blank slide's body region", got.BlankRegion, rect{120, 120, 1680, 840}},
225 } {
226 compareRect(t, c.name, c.got, c.want)
227 }
228
229 // A picture in a flow region is a flex item and shrinks to what is
230 // left: 840 less the text above it and the gap between them.
231 compareRect(t, "a picture under one line of text",
232 got.BlankImage, rect{120, 224.832, 1680, 735.168})
233 compareRect(t, "that line of text", got.BlankText, rect{120, 120, 1680, 61.152})
234
235 // The gap between regions, and the reason slice 3 moved the
236 // template grid onto [slides-frame]. It used to be written on the
237 // section, which is the query container, so the browser measured
238 // it against whatever was outside the slide: 49.92px here and
239 // 20.27px in a 780px window, for the same slide. On the frame it
240 // is 2.6% of the section's content box, so it is 43.68 at 1920 and
241 // 17.745 at 780 — the same slide, scaled, which is what the rest
242 // of the stylesheet already promised and what lets
243 // layout/sizes.go state one number for it.
244 //
245 // If either of these ever equals the other, the gap has stopped
246 // scaling with the slide and gone back to following the window.
247 for _, c := range []struct {
248 name string
249 got, want float64
250 derivation string
251 }{
252 {"the gap between regions at 1920", got.WideGap, 43.68, "2.6% of 1680"},
253 {"the same gap on a 780px slide", got.NarrowGap, 17.745, "2.6% of 682.5"},
254 } {
255 if diff := c.got - c.want; diff > tolerance || diff < -tolerance {
256 t.Errorf("%s is %v in the browser; slides.css means %v (%s)",
257 c.name, c.got, c.want, c.derivation)
258 }
259 }
260}
261
262func compareRect(t *testing.T, name string, got, want rect) {
263 t.Helper()
264 for _, c := range []struct {
265 axis string
266 got, want float64
267 }{{"x", got.X, want.X}, {"y", got.Y, want.Y}, {"width", got.W, want.W}, {"height", got.H, want.H}} {
268 if diff := c.got - c.want; diff > tolerance || diff < -tolerance {
269 t.Errorf("%s: the browser puts its %s at %v; internal/slides/layout says %v",
270 name, c.axis, c.got, c.want)
271 }
272 }
273}
274