Fonts: Chromium checks of a variable WOFF2 upload against the gate
The upload tests used Chromium and renamed static TTFs only, so nothing showed the motivating case, a variable WOFF2 like Timeless Sans, held to the no-flash rule (final review, I7). A variable WOFF2 is now built in the test (Liberation Mono renamed, an fvar with a wght axis from 100 to 900, the brotli writer) and checked on the viewer, a share link and the editor: loaded in time it is the only face the headings are drawn in, as one face weighing "100 900"; late past the deadline it commits to its fallback and never joins; a collaborator bringing it in while the editor is open waits for it. WebKit is not checked: this machine has no Playwright engines.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+259
−0
internal/slidestest/fonts_woff2_browser_test.go+259 −0
diff --git a/internal/slidestest/fonts_woff2_browser_test.go b/internal/slidestest/fonts_woff2_browser_test.go| new file mode 100644 |
| index 0000000..ae9395c |
| --- /dev/null |
| +++ b/internal/slidestest/fonts_woff2_browser_test.go |
| @@ -0,0 +1,259 @@ |
| +//go:build browser |
| + |
| +package slidestest |
| + |
| +// The motivating case, in Chromium (final review, I7): a variable WOFF2 |
| +// upload, as Timeless Sans is. Each file here is built in the test from |
| +// Liberation Mono (SIL OFL) renamed "Var Sans", given an fvar with a |
| +// wght axis from 100 to 900, and wrapped in WOFF2 with the brotli writer, |
| +// as estilo's typeface tests build theirs. It is held to the gate's rule |
| +// as a static TTF is: loaded in time, it is the first and only face the |
| +// headings are drawn in; late past the deadline, it commits to the |
| +// stack's next family and never swaps in; brought in by a collaborator |
| +// while the editor is open, the change waits for it. |
| +// |
| +// WebKit is not checked: this machine has no Playwright engines, so the |
| +// WebKit tests skip here (see the final fix report). |
| + |
| +import ( |
| + "bytes" |
| + "encoding/binary" |
| + "net/http" |
| + "sort" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/andybalholm/brotli" |
| + "github.com/chromedp/chromedp" |
| + |
| + "amadan.net/oficina/slides/internal/slides" |
| + "amadan.net/oficina/slides/internal/slides/fonts" |
| +) |
| + |
| +type sfntTable struct { |
| + tag string |
| + data []byte |
| +} |
| + |
| +func sfntTablesOf(t *testing.T, sfnt []byte) (uint32, []sfntTable) { |
| + t.Helper() |
| + n := int(binary.BigEndian.Uint16(sfnt[4:])) |
| + var out []sfntTable |
| + for i := 0; i < n; i++ { |
| + rec := sfnt[12+16*i:] |
| + off, length := binary.BigEndian.Uint32(rec[8:]), binary.BigEndian.Uint32(rec[12:]) |
| + out = append(out, sfntTable{string(rec[:4]), bytes.Clone(sfnt[off : off+length])}) |
| + } |
| + sort.Slice(out, func(i, j int) bool { return out[i].tag < out[j].tag }) |
| + return binary.BigEndian.Uint32(sfnt), out |
| +} |
| + |
| +// wghtAxis is an fvar of one wght axis, 100 to 900, default 400. |
| +func wghtAxis() []byte { |
| + var b []byte |
| + for _, v := range []uint16{1, 0, 16, 2, 1, 20, 0, 8} { |
| + b = binary.BigEndian.AppendUint16(b, v) |
| + } |
| + b = append(b, "wght"...) |
| + for _, v := range []float64{100, 400, 900} { |
| + b = binary.BigEndian.AppendUint32(b, uint32(int32(v*65536))) |
| + } |
| + b = binary.BigEndian.AppendUint16(b, 0) |
| + return binary.BigEndian.AppendUint16(b, 256) |
| +} |
| + |
| +var woff2KnownTags = []string{"cmap", "head", "hhea", "hmtx", "maxp", "name", "OS/2", "post", "cvt ", "fpgm", "glyf", "loca", "prep", "CFF ", "VORG", "EBDT", "EBLC", "gasp", "hdmx", "kern", "LTSH", "PCLT", "VDMX", "vhea", "vmtx", "BASE", "GDEF", "GPOS", "GSUB", "EBSC", "JSTF", "MATH", "CBDT", "CBLC", "COLR", "CPAL", "SVG ", "sbix", "acnt", "avar", "bdat", "bloc", "bsln", "cvar", "fdsc", "feat", "fmtx", "fvar", "gvar", "hsty", "just", "lcar", "mort", "morx", "opbd", "prop", "trak", "Zapf", "Silf", "Glat", "Gloc", "Feat", "Sill"} |
| + |
| +func base128(n uint32) []byte { |
| + var out []byte |
| + for { |
| + out = append([]byte{byte(n & 0x7F)}, out...) |
| + if n >>= 7; n == 0 { |
| + break |
| + } |
| + } |
| + for i := 0; i < len(out)-1; i++ { |
| + out[i] |= 0x80 |
| + } |
| + return out |
| +} |
| + |
| +// woff2Of wraps tables in WOFF2: glyf and loca with the null transform, |
| +// the rest untransformed, one brotli stream. |
| +func woff2Of(flavor uint32, ts []sfntTable) []byte { |
| + var dir, stream []byte |
| + sfntSize := 12 + 16*len(ts) |
| + for _, tb := range ts { |
| + idx := 63 |
| + for i, k := range woff2KnownTags { |
| + if k == tb.tag { |
| + idx = i |
| + } |
| + } |
| + version := byte(0) |
| + if tb.tag == "glyf" || tb.tag == "loca" { |
| + version = 3 |
| + } |
| + dir = append(dir, version<<6|byte(idx)) |
| + if idx == 63 { |
| + dir = append(dir, tb.tag...) |
| + } |
| + dir = append(dir, base128(uint32(len(tb.data)))...) |
| + stream = append(stream, tb.data...) |
| + sfntSize += (len(tb.data) + 3) &^ 3 |
| + } |
| + var z bytes.Buffer |
| + w := brotli.NewWriterLevel(&z, 5) |
| + w.Write(stream) |
| + w.Close() |
| + total := 48 + len(dir) + z.Len() |
| + padded := (total + 3) &^ 3 |
| + var b []byte |
| + b = append(b, "wOF2"...) |
| + b = binary.BigEndian.AppendUint32(b, flavor) |
| + b = binary.BigEndian.AppendUint32(b, uint32(padded)) |
| + b = binary.BigEndian.AppendUint16(b, uint16(len(ts))) |
| + b = binary.BigEndian.AppendUint16(b, 0) |
| + b = binary.BigEndian.AppendUint32(b, uint32(sfntSize)) |
| + b = binary.BigEndian.AppendUint32(b, uint32(z.Len())) |
| + b = append(b, make([]byte, 4+20)...) |
| + b = append(b, dir...) |
| + b = append(b, z.Bytes()...) |
| + return append(b, make([]byte, padded-total)...) |
| +} |
| + |
| +// variableWOFF2 is Liberation Mono's regular called name, variable in |
| +// weight from 100 to 900, as a WOFF2. |
| +func variableWOFF2(t *testing.T, name string) []byte { |
| + t.Helper() |
| + f, err := fonts.Parse(libFont(t, "LiberationMono-Regular.ttf")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + renamed, err := f.Renamed(name, 400, false) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + flavor, ts := sfntTablesOf(t, renamed) |
| + ts = append(ts, sfntTable{"fvar", wghtAxis()}) |
| + sort.Slice(ts, func(i, j int) bool { return ts[i].tag < ts[j].tag }) |
| + return woff2Of(flavor, ts) |
| +} |
| + |
| +// variableRig is a font rig with a paper deck, and Var Sans in the |
| +// library as a variable WOFF2, recorded for the deck. use sets the |
| +// headings in it; without it the deck takes it on later. |
| +func variableRig(t *testing.T, use bool) (fr *fontRig, sqid, ref, fam string) { |
| + t.Helper() |
| + fr = newFontRigWith(t, dirStore(t)) |
| + data := variableWOFF2(t, "Var Sans") |
| + if rec := uploadFonts(t, fr.app, fr.cookies, upFile{"var.woff2", data}); rec.Code != http.StatusOK { |
| + t.Fatalf("upload: %d\n%s", rec.Code, rec.Body) |
| + } |
| + var row slides.LibraryFont |
| + if err := fr.app.db.G.Where("ref = ?", sha(data)).First(&row).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + if !row.Variable || row.Format != "woff2" || row.WeightMin != 100 || row.WeightMax != 900 || row.Pictures { |
| + t.Fatalf("the upload is %+v, want a variable woff2, 100 to 900, not for pictures", row) |
| + } |
| + sqid = fr.deck(t, "Variable", paperDeck) |
| + fam = recordFamily(t, fr, sqid, row.CSSName) |
| + if use { |
| + fr.ops(t, sqid, `[{"op":"set_meta","fontFiles":[`+fam+`],"fonts":{"heading":"'Var Sans', Liberation Serif, Georgia, serif","body":""}}]`) |
| + } |
| + fr.probeAlso("Var Sans") |
| + return fr, sqid, sha(data), fam |
| +} |
| + |
| +// seenIn sorts the faces runs were seen in: any in Var Sans loaded, and |
| +// any in Var Sans's fallback. |
| +func seenIn(p probe) (web, fallback bool) { |
| + for _, faces := range p.Seen { |
| + for f := range faces { |
| + web = web || strings.Contains(f, "var sans:web") |
| + fallback = fallback || strings.Contains(f, "var sans:system") |
| + } |
| + } |
| + return web, fallback |
| +} |
| + |
| +func TestAVariableWOFF2UploadLoadsInTime(t *testing.T) { |
| + fr, sqid, ref, _ := variableRig(t, true) |
| + share := "/s/" + mintShareLink(t, fr.app, "/d/"+sqid, fr.cookies) |
| + fr.match.Store(ref) |
| + fr.slow.Store(int64(time.Second)) |
| + for _, c := range []struct{ name, path string }{ |
| + {"viewer", "/d/" + sqid + "?slide=1"}, |
| + {"share link", share + "?slide=1"}, |
| + {"editor", "/d/" + sqid + "/edit"}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + start := time.Now() |
| + fr.goTo(t, c.path) |
| + if waited := time.Since(start); waited < 950*time.Millisecond { |
| + t.Fatalf("the slide was shown %v after navigating, before the variable face could have arrived", waited) |
| + } |
| + fr.rig.Run(chromedp.Sleep(1500 * time.Millisecond)) |
| + p := fr.clean(t) |
| + webEverywhere(t, p) |
| + sameLayout(t, p) |
| + if web, fallback := seenIn(p); !web || fallback { |
| + t.Errorf("headings seen in the variable face %v, in its fallback %v", web, fallback) |
| + } |
| + var weights []string |
| + fr.eval(t, `[...document.fonts].filter((f) => f.family.replace(/["']/g, '') === 'Var Sans').map((f) => f.weight + ' ' + f.status)`, &weights) |
| + if strings.Join(weights, "|") != "100 900 loaded" { |
| + t.Errorf("the page's Var Sans faces: %v, want one, its weight the range", weights) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestAVariableWOFF2UploadLateCommitsToItsFallback(t *testing.T) { |
| + fr, sqid, ref, _ := variableRig(t, true) |
| + fr.match.Store(ref) |
| + fr.slow.Store(int64(4 * time.Second)) |
| + fr.rig.Run(fr.navigate("/d/" + sqid + "?slide=1")) |
| + fr.shown(t) |
| + fr.rig.Run(chromedp.Sleep(4 * time.Second)) |
| + if fr.hits(ref) == 0 { |
| + t.Fatal("the variable face was never requested") |
| + } |
| + p := fr.clean(t) |
| + sameLayout(t, p) |
| + if web, fallback := seenIn(p); web || !fallback { |
| + t.Errorf("late: headings seen in the variable face %v (it arrived after the page was shown), in its fallback %v", web, fallback) |
| + } |
| + var st struct { |
| + Committed bool |
| + Faces int |
| + } |
| + fr.eval(t, `({ Committed: window.slidesFonts.committed('u:Var Sans'), Faces: [...document.fonts].filter((f) => f.family.replace(/["']/g, '') === 'Var Sans').length })`, &st) |
| + if !st.Committed || st.Faces != 0 { |
| + t.Errorf("committed %v, %d of its faces in the page", st.Committed, st.Faces) |
| + } |
| +} |
| + |
| +func TestAVariableWOFF2UploadArrivingLiveWaits(t *testing.T) { |
| + fr, sqid, ref, fam := variableRig(t, false) |
| + fr.goTo(t, "/d/"+sqid+"/edit") |
| + fr.editable(t) |
| + fr.until(t, `document.documentElement.getAttribute('data-slides-fonts-prefetch') === 'done'`) |
| + fr.match.Store(ref) |
| + fr.slow.Store(int64(time.Second)) |
| + fr.ops(t, sqid, `[{"op":"set_meta","fontFiles":[`+fam+`],"fonts":{"heading":"'Var Sans', Liberation Serif, Georgia, serif","body":""}}]`) |
| + fr.until(t, `(() => { const h = document.querySelector('[slides-stage] [data-id="head00000001"]'); |
| + return !!h && /var sans/i.test(getComputedStyle(h).fontFamily) })()`) |
| + fr.rig.Run(chromedp.Sleep(2 * time.Second)) |
| + p := fr.cleanAcross(t) |
| + if web, fallback := seenIn(p); !web || fallback { |
| + t.Errorf("live: headings seen in the variable face %v, in its fallback %v", web, fallback) |
| + } |
| + var loaded bool |
| + fr.eval(t, `window.slidesFonts.loaded('u:Var Sans')`, &loaded) |
| + if !loaded { |
| + t.Error("the variable family is not loaded") |
| + } |
| +} |