oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7
8 "amadan.net/rastrillo/rastrillo"
9
10 "amadan.net/oficina/slides/internal/slides/deck"
11 "amadan.net/oficina/slides/internal/slides/fonts"
12 slidehtml "amadan.net/oficina/slides/internal/slides/render"
13)
14
15// The font gate: what a slide surface carries so that no slide text is
16// ever painted in a face it will later leave (Paul, 2026-10-26: "No
17// flashing, ever!" — text in one face and then another, a visible gap
18// of invisible text, and a layout that moves after the page is shown
19// all count). Docs solved this first (oficina/docs fontgate.go and
20// polish-audit/fonts/NO-FOUT.md); this is that design on Slides' model.
21//
22// - No stylesheet declares a web face. slides.css used to, with
23// font-display: swap, which is a flash by definition. Now the faces
24// exist only once fixtures/fonts-gate.mjs has loaded them and added
25// them to document.fonts itself, so a reader without JavaScript, or
26// whose gate script never arrived, gets the system faces their
27// stacks name for the whole visit: nothing held, nothing swapped.
28// - The page preloads, in <head>, exactly the faces the gate will wait
29// for: every style of every family the deck uses
30// (render.FamiliesUsed — families, not faces, Docs' review 4).
31// - A deck's own uploaded fonts (meta.fontFiles) are families like
32// the catalog's three, keyed "u:" + css, fetched under the page's
33// own base (deckfonts.go's {base}/fonts/{ref}): the same preloads
34// for the ones its stacks lead with (render.UploadedUsed), the same
35// deadline, the same commit to the stack's next family when late.
36// - A blocking script in <head> (fixtures/fonts-hold.js) hides the
37// slides before the first paint. The gate waits for those families
38// until a deadline, commits any family still missing to its system
39// fallback for the rest of the visit (it never joins
40// document.fonts, so it can never swap in), turns the rest on, waits
41// for the editor's first mount on the editor page, and shows the
42// slides once.
43// - After that, nothing paints in a family that has not loaded: an
44// edit, a collaborator's change, a projector's next slide and a
45// resync all wait for the family first (fixtures/font-session.mjs).
46//
47// The presenter view and the remote show their slides as SVG pictures
48// (<img>, which draws in the machine's own faces) and their notes in
49// the chrome's, so they carry no gate: there is nothing on them a web
50// face could ever reach. TestFontThePresenterAndRemoteDrawInNoWebFace
51// holds them to that.
52
53// fontGate is a slide surface's part of <head> (layout.html's
54// font-head), and the status strip's copy on the editor.
55type fontGate struct {
56 // Preloads is every face the gate waits for, as served URLs with
57 // their MIME types.
58 Preloads []fontPreload
59 // Manifest is the JSON fixtures/font-session.mjs reads off
60 // <meta name="slides-fonts">: the families this page needs now, every
61 // family's faces, and the stacks the editor needs to work out the
62 // families of a deck it has just changed.
63 Manifest string
64 Hold string
65 Script string
66
67 // What the editor can say about its fonts, in its status strip
68 // (fixtures/font-status.mjs): one loading, a change refused while it
69 // does, one that could not be loaded, a change that could no longer be
70 // made once its font was in, substitutes in use, and the label of the
71 // button that dismisses a note.
72 Loading string
73 Refused string
74 Unavailable string
75 Interrupted string
76 Substitute string
77 Dismiss string
78}
79
80// gateFace is one face the gate can load. Weight is a number, or for a
81// variable face the CSS range it covers as a string ("100 900"), which
82// FontFace takes as its weight descriptor. Format is the CSS format()
83// of an uploaded face; a catalog face leaves it out and is woff2, so an
84// existing deck's manifest is byte for byte what it was.
85type gateFace struct {
86 Weight any `json:"weight"`
87 Style string `json:"style"`
88 URL string `json:"url"`
89 Format string `json:"format,omitempty"`
90}
91
92type gateFamily struct {
93 CSS string `json:"css"`
94 Faces []gateFace `json:"faces"`
95}
96
97type gateManifest struct {
98 // Need is the families the page's slides use, which the gate waits
99 // for before it shows them.
100 Need []string `json:"need"`
101 // Families is all three, keyed by fonts.Family.Key.
102 Families map[string]gateFamily `json:"families"`
103 // Themes is every theme's [heading, body] stack, DefaultTheme the
104 // one an unknown theme resolves to, and Code the code stack:
105 // fixtures/font-families.mjs reads a changed deck with them exactly
106 // as render.FamiliesUsed reads the deck the server has.
107 Themes map[string][2]string `json:"themes"`
108 DefaultTheme string `json:"defaultTheme"`
109 Code string `json:"code"`
110 // Uploaded is the deck's own fonts (meta.fontFiles), every family it
111 // lists, keyed "u:" + css, at this surface's base; absent when it
112 // has none. Need holds the ones render.UploadedUsed names.
113 Uploaded map[string]gateFamily `json:"uploaded,omitempty"`
114}
115
116// fontPreload is one <link rel=preload as=font> in a gated page's head.
117type fontPreload struct {
118 URL string
119 Type string
120}
121
122// uploadedFormats is meta.fontFiles' format, as CSS format() and as
123// the preload's MIME type.
124var uploadedFormats = map[string][2]string{
125 "ttf": {"truetype", "font/ttf"},
126 "otf": {"opentype", "font/otf"},
127 "woff": {"woff", "font/woff"},
128 "woff2": {"woff2", "font/woff2"},
129}
130
131// fontRef is a face's ref as the core admits it: a SHA-256 in lowercase
132// hex, which is all a URL path segment may hold here.
133func fontRef(ref string) bool {
134 if len(ref) != 64 {
135 return false
136 }
137 for _, c := range ref {
138 if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
139 return false
140 }
141 }
142 return true
143}
144
145// uploadedFamily is fam as the gate loads it from base ("/d/{sqid}",
146// "/s/{token}", "/p/{session}"), and each face's preload type.
147// fixtures/font-families.mjs's uploadedFamily is its twin, for a family
148// a change brings in while the editor is open (editor-fonts.mjs).
149func uploadedFamily(fam deck.FontFamily, base string) (gateFamily, []string) {
150 out := gateFamily{CSS: fam.CSS}
151 var types []string
152 for _, f := range fam.Faces {
153 format, ok := uploadedFormats[f.Format]
154 if !ok || !fontRef(f.Ref) {
155 continue
156 }
157 var weight any = f.Weight[0]
158 if f.Weight[1] != f.Weight[0] {
159 weight = fmt.Sprintf("%d %d", f.Weight[0], f.Weight[1])
160 }
161 style := "normal"
162 if f.Italic {
163 style = "italic"
164 }
165 out.Faces = append(out.Faces, gateFace{Weight: weight, Style: style, URL: base + "/fonts/" + f.Ref, Format: format[0]})
166 types = append(types, format[1])
167 }
168 return out, types
169}
170
171// uploadedFamilies is a deck's meta.fontFiles as the manifest carries
172// them (manifest.uploaded), keyed "u:" + css, fetched under base, and
173// each face's preload type; nil when there is none. The talk carries the
174// same map (presenting_talk.go), so a window holding the talk learns a
175// font the deck took on after it opened (fixtures/talk-store.mjs).
176func uploadedFamilies(families []deck.FontFamily, base string) (map[string]gateFamily, map[string][]string) {
177 var out map[string]gateFamily
178 types := map[string][]string{}
179 for _, fam := range families {
180 key := "u:" + fam.CSS
181 if _, dup := out[key]; dup || fam.CSS == "" {
182 continue
183 }
184 gf, t := uploadedFamily(fam, base)
185 if len(gf.Faces) == 0 {
186 continue
187 }
188 if out == nil {
189 out = map[string]gateFamily{}
190 }
191 out[key] = gf
192 types[key] = t
193 }
194 return out, types
195}
196
197// newFontGate is the gate for a page whose slides are d's. A nil deck
198// is a page with no slide on it yet (an empty deck's editor): it needs
199// nothing now and can still load a family later. base is the
200// capability the page reads the deck under — "/d/{sqid}" for the editor
201// and the viewer, "/s/{token}" for a share link and its follow view,
202// "/p/{session}" for the projector and the presenter window — and the
203// deck's own fonts are fetched under it (deckfonts.go).
204func newFontGate(r *http.Request, d *deck.Deck, base string) *fontGate {
205 m := gateManifest{
206 Need: []string{},
207 Families: map[string]gateFamily{},
208 Themes: slidehtml.ThemeStacks(),
209 DefaultTheme: slidehtml.ThemeID(""),
210 Code: slidehtml.CodeStack(),
211 }
212 for _, f := range fonts.Families() {
213 fam := gateFamily{CSS: f.CSSName()}
214 for _, face := range f.Faces() {
215 fam.Faces = append(fam.Faces, gateFace{Weight: face.Weight(), Style: face.CSSStyle(), URL: assets.Path("static/fonts/" + face.WebFile())})
216 }
217 m.Families[f.Key()] = fam
218 }
219 g := &fontGate{
220 Hold: bundlePath("fontshold"),
221 Script: bundlePath("fontsgate"),
222 }
223 if d != nil {
224 for _, f := range slidehtml.FamiliesUsed(d) {
225 m.Need = append(m.Need, f.Key())
226 for _, face := range m.Families[f.Key()].Faces {
227 g.Preloads = append(g.Preloads, fontPreload{URL: face.URL, Type: "font/woff2"})
228 }
229 }
230 var types map[string][]string
231 m.Uploaded, types = uploadedFamilies(d.Meta.FontFiles, base)
232 for _, fam := range slidehtml.UploadedUsed(d) {
233 key := "u:" + fam.CSS
234 gf, ok := m.Uploaded[key]
235 if !ok {
236 continue
237 }
238 m.Need = append(m.Need, key)
239 for i, face := range gf.Faces {
240 g.Preloads = append(g.Preloads, fontPreload{URL: face.URL, Type: types[key][i]})
241 }
242 }
243 }
244 b, err := json.Marshal(m)
245 if err != nil {
246 panic("slides: marshal font manifest: " + err.Error())
247 }
248 g.Manifest = string(b)
249 g.Loading = rastrillo.T(r, "slides.editor.fonts.loading")
250 g.Refused = rastrillo.T(r, "slides.editor.fonts.refused")
251 g.Unavailable = rastrillo.T(r, "slides.editor.fonts.unavailable")
252 g.Interrupted = rastrillo.T(r, "slides.editor.fonts.interrupted")
253 g.Substitute = rastrillo.T(r, "slides.editor.fonts.substitute")
254 g.Dismiss = rastrillo.T(r, "slides.editor.fonts.dismiss")
255 return g
256}
257