oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package layout is the print half of the one rule (spec §0.1): the
2// same block tree internal/slides/render turns into HTML, measured and
3// placed into a fixed 1920 by 1080 frame.
4//
5// What comes out is a paint list — a background and a flat, ordered
6// sequence of rectangles, ellipses, lines, images and positioned runs
7// of text. It is the ONLY input to the SVG serialiser and to the PDF
8// writer (spec §4.2, and the slice 3 plan's global constraints say it
9// out loud): nothing downstream of here reads a deck. That is what
10// keeps a thumbnail and a printed page from being two opinions about
11// one slide.
12//
13// # Where the numbers come from
14//
15// internal/slides/static/slides.css, evaluated at 1920 by 1080 —
16// sizes.go for the scale, regions.go for each template's grid. Both
17// are written as the arithmetic they are so a stylesheet change can be
18// followed here by reading it, and both were CHECKED against a real
19// Chromium rather than reasoned about: internal/slidestest's
20// TestTheStylesheetMeasuresWhatTheLayoutEngineAssumes is that check,
21// and it found two mistakes the Go tests could not have (a container
22// query unit measures the content box, and a picture in a region is a
23// flex item that shrinks).
24//
25// # What this does not claim
26//
27// Pixel parity with the browser. It is close — every region box and
28// type size above agrees with Chromium to within a pixel — and these
29// are the places it is not:
30//
31// - Line breaking, which spec §4.2 names as the expected divergence.
32// Both sides measure with the same font programs at the same
33// sizes, but this one does not kern (internal/slides/fonts reads
34// no GPOS), so a measured string is about a percent narrower in
35// the browser and a line can break one word earlier here.
36// - A shape's label is drawn here and is not on screen: render puts
37// it in the SVG's <title>, where it is announced and not painted,
38// and a paint list has no accessibility tree to put it in.
39// - image-full's heading scrim is opaque, where the stylesheet mixes
40// it 82% towards transparent (layout.go says why), and the four
41// greys the stylesheet builds with color-mix() in oklab are mixed
42// in plain sRGB (props.go says why).
43//
44// Text is never clipped. A block whose content outgrows its box draws
45// past it and records a Warning, exactly as the stylesheet lets it
46// overflow rather than cutting a sentence in half (slides.css's canvas
47// rules say why: losing the end of a line to `overflow: hidden` is a
48// WCAG 1.4.10 failure nobody ever reports).
49//
50// The phone layer is never read (§2.5: the print engine does not).
51package layout
52
53import (
54 "bytes"
55 "encoding/json"
56 "math"
57
58 "amadan.net/oficina/slides/internal/slides/fonts"
59)
60
61// Fill is a colour, as `#rrggbb`, or "" for none.
62//
63// A string rather than a struct because every one of these came from
64// the theme (render.SlideVars) or from a prop render already
65// allowlisted, and every consumer — SVG, PDF — wants it back as text.
66// "" is the absence of a colour, which is a real value: a shape with
67// no fill is an outline.
68type Fill string
69
70// Paint is one slide, drawn.
71//
72// W and H are always the 1920 by 1080 frame (§9: other aspect ratios
73// are not built). Ops are in paint order — first drawn is furthest
74// back — which for a canvas slide is z order and for a flow slide is
75// the order the regions and their blocks are read in.
76type Paint struct {
77 W, H float64
78 Background Fill
79 Ops []Op
80}
81
82// Op is one thing to draw. The interface is closed on purpose: the SVG
83// serialiser and the PDF writer both switch over these five, and a
84// sixth added without their knowing would be silently skipped by one
85// of them. Adding one means adding it there too, which is what an
86// unexported method makes unavoidable.
87type Op interface {
88 // kind is the name this op is written under in a paint list.
89 kind() string
90}
91
92// Rect is a rectangle: a code block's panel, a table rule, a
93// highlight, a shape.
94//
95// Crisp draws it without antialiasing, snapped to the pixels: a
96// follow-along code's modules (planQR), whose rows of rectangles would
97// otherwise meet with a pale seam wherever an edge falls inside a
98// pixel, which is everywhere in a thumbnail.
99type Rect struct {
100 X, Y, W, H float64
101 Fill Fill
102 Stroke Fill
103 StrokeW float64
104 Radius float64
105 Crisp bool
106}
107
108// Ellipse is an ellipse inscribed in the box X, Y, W, H — the box, not
109// a centre and two radii, because every caller here has a box.
110type Ellipse struct {
111 X, Y, W, H float64
112 Fill Fill
113 Stroke Fill
114 StrokeW float64
115}
116
117// Line is a straight line. Arrow puts a solid head at the second end,
118// which is the `arrow` shape and the only decoration this vocabulary
119// has.
120type Line struct {
121 X1, Y1, X2, Y2 float64
122 Stroke Fill
123 W float64
124 Arrow bool
125}
126
127// Image is a picture to place in a box. Asset is the deck's own asset
128// id (§2.7) and NOT a URL or a ref: what a consumer does with it
129// differs — a thumbnail inlines the bytes as a data URI, a PDF embeds
130// them, an SVG for the editor points at a route — and each of those
131// needs the deck to resolve it. Fit is `cover` or `contain`, Alt is
132// what §2.4 requires of every image.
133type Image struct {
134 X, Y, W, H float64
135 Asset string
136 Fit string
137 Alt string
138}
139
140// Text is one line of text at a baseline. X is where the line starts
141// and Y is the baseline — not the top — because that is what both a
142// PDF text object and an SVG <text> take, and because runs of
143// different sizes on one line share a baseline and share nothing else.
144//
145// One op per line. The line breaking already happened (text.go); a
146// consumer draws the runs left to right, advancing by each run's own
147// measured width, and never re-wraps.
148type Text struct {
149 X, Y float64
150 Runs []Run
151}
152
153// Run is a stretch of text in one face at one size in one colour.
154// Href is the link a run carries (§2.6's `href` mark), for a consumer
155// that can make one; the underline a link is drawn with is a separate
156// Line op, because §8 does not let colour alone carry state and a PDF
157// annotation would not put a mark on the page.
158type Run struct {
159 S string
160 Face fonts.Face
161 Size float64
162 Fill Fill
163 Href string
164}
165
166func (Rect) kind() string { return "rect" }
167func (Ellipse) kind() string { return "ellipse" }
168func (Line) kind() string { return "line" }
169func (Image) kind() string { return "image" }
170func (Text) kind() string { return "text" }
171
172// ── Warnings ─────────────────────────────────────────────────────────
173
174// Warning is something the caller should be able to say out loud about
175// an export: this block ran past its box, this deck asked for a
176// character these faces cannot draw. §11's degrade-loudly rule — the
177// slide is still drawn, and the reason it looks wrong is reported
178// rather than left for somebody to discover in a printed handout.
179type Warning struct {
180 Kind string `json:"kind"`
181 // Slide is the id of the slide the warning is about. Layout stamps
182 // it on everything it reports, so a caller holding the warnings
183 // from a whole deck — an export's status page — can say WHICH
184 // slide is wrong. Without it a warning list is a set of true
185 // sentences about a deck and the person reading it has to open
186 // every slide to find the picture that went missing. Empty only on
187 // a warning made outside any slide.
188 Slide string `json:"slide,omitempty"`
189 Block string `json:"block,omitempty"`
190 Detail string `json:"detail"`
191}
192
193const (
194 // WarnOverflow is content drawn past the box it was given.
195 WarnOverflow = "overflow"
196 // WarnGlyph is a rune no embedded face has a glyph for. It is
197 // drawn at .notdef's width, which is what a browser shows a box
198 // at.
199 WarnGlyph = "glyph"
200 // WarnAsset is an image block whose asset this deck never
201 // registered, or registered with a ref nothing can serve.
202 WarnAsset = "asset"
203 // WarnFont is a face this deck asked for that this build does not
204 // carry (meta.fonts, §2.1) — measured in a substitute, which the
205 // browser will not be using.
206 WarnFont = "font"
207 // WarnUnreached is a block named as a child by a columns block
208 // that nothing on the slide reaches — a cycle, which the ops refuse
209 // but a merge of two peers' edits can still produce (codex 7). It
210 // is drawn after everything else rather than dropped, and this says
211 // so.
212 WarnUnreached = "unreached"
213)
214
215// ── The paint list as JSON ───────────────────────────────────────────
216//
217// Only ever written, never read back: it is what the golden files in
218// testdata are, and a paint list is produced from a deck rather than
219// loaded. So there is no UnmarshalJSON here, and the shapes below are
220// chosen for a person reading a diff — an op says its own name first,
221// and a face is its PostScript name rather than three fields.
222
223func (p Paint) MarshalJSON() ([]byte, error) {
224 type paint struct {
225 W float64 `json:"w"`
226 H float64 `json:"h"`
227 Background Fill `json:"background"`
228 Ops []Op `json:"ops"`
229 }
230 return json.Marshal(paint{p.W, p.H, p.Background, p.Ops})
231}
232
233// r4 rounds a coordinate to four decimals for the paint list's JSON.
234//
235// The arithmetic here runs through ratios of a 19.2px unit, so a
236// coordinate that is 126.72 comes out of the floats as
237// 126.72000000000001 about as often as not. Nothing downstream can see
238// a ten-thousandth of a pixel, and a golden file that changes because
239// a sum was reassociated is a golden file nobody reads.
240func r4(v float64) float64 { return math.Round(v*1e4) / 1e4 }
241
242func (r Rect) MarshalJSON() ([]byte, error) {
243 type alias struct {
244 X float64 `json:"x"`
245 Y float64 `json:"y"`
246 W float64 `json:"w"`
247 H float64 `json:"h"`
248 Fill Fill `json:"fill,omitempty"`
249 Stroke Fill `json:"stroke,omitempty"`
250 StrokeW float64 `json:"strokeWidth,omitempty"`
251 Radius float64 `json:"radius,omitempty"`
252 Crisp bool `json:"crisp,omitempty"`
253 }
254 return opJSON(r.kind(), alias{r4(r.X), r4(r.Y), r4(r.W), r4(r.H), r.Fill, r.Stroke, r4(r.StrokeW), r4(r.Radius), r.Crisp})
255}
256
257func (e Ellipse) MarshalJSON() ([]byte, error) {
258 type alias struct {
259 X float64 `json:"x"`
260 Y float64 `json:"y"`
261 W float64 `json:"w"`
262 H float64 `json:"h"`
263 Fill Fill `json:"fill,omitempty"`
264 Stroke Fill `json:"stroke,omitempty"`
265 StrokeW float64 `json:"strokeWidth,omitempty"`
266 }
267 return opJSON(e.kind(), alias{r4(e.X), r4(e.Y), r4(e.W), r4(e.H), e.Fill, e.Stroke, r4(e.StrokeW)})
268}
269
270func (l Line) MarshalJSON() ([]byte, error) {
271 type alias struct {
272 X1 float64 `json:"x1"`
273 Y1 float64 `json:"y1"`
274 X2 float64 `json:"x2"`
275 Y2 float64 `json:"y2"`
276 Stroke Fill `json:"stroke,omitempty"`
277 W float64 `json:"width"`
278 Arrow bool `json:"arrow,omitempty"`
279 }
280 return opJSON(l.kind(), alias{r4(l.X1), r4(l.Y1), r4(l.X2), r4(l.Y2), l.Stroke, r4(l.W), l.Arrow})
281}
282
283func (i Image) MarshalJSON() ([]byte, error) {
284 type alias struct {
285 X float64 `json:"x"`
286 Y float64 `json:"y"`
287 W float64 `json:"w"`
288 H float64 `json:"h"`
289 Asset string `json:"asset"`
290 Fit string `json:"fit"`
291 Alt string `json:"alt"`
292 }
293 return opJSON(i.kind(), alias{r4(i.X), r4(i.Y), r4(i.W), r4(i.H), i.Asset, i.Fit, i.Alt})
294}
295
296func (t Text) MarshalJSON() ([]byte, error) {
297 type alias struct {
298 X float64 `json:"x"`
299 Y float64 `json:"y"`
300 Runs []Run `json:"runs"`
301 }
302 return opJSON(t.kind(), alias{r4(t.X), r4(t.Y), t.Runs})
303}
304
305// MarshalJSON writes the face by name — "LiberationSans-Bold", or an
306// uploaded face's family, weight and slope ("Brand Sans 700 italic") —
307// which is what a person reading a golden file wants to see, and what
308// the PDF registers a built-in face under anyway.
309func (r Run) MarshalJSON() ([]byte, error) {
310 type alias struct {
311 S string `json:"s"`
312 Face string `json:"face"`
313 Size float64 `json:"size"`
314 Fill Fill `json:"fill,omitempty"`
315 Href string `json:"href,omitempty"`
316 }
317 return json.Marshal(alias{r.S, r.Face.Name(), r4(r.Size), r.Fill, r.Href})
318}
319
320// opJSON marshals v and splices the op's name in as the first key, so
321// a paint list reads as a list of named things rather than of
322// coordinates that have to be guessed at.
323func opJSON(kind string, v any) ([]byte, error) {
324 b, err := json.Marshal(v)
325 if err != nil {
326 return nil, err
327 }
328 name, err := json.Marshal(kind)
329 if err != nil {
330 return nil, err
331 }
332 var buf bytes.Buffer
333 buf.WriteString(`{"op":`)
334 buf.Write(name)
335 if len(b) > 2 {
336 buf.WriteByte(',')
337 buf.Write(b[1 : len(b)-1])
338 }
339 buf.WriteByte('}')
340 return buf.Bytes(), nil
341}
342