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
3package slidestest
4
5// A slide picture shown in an <img> draws an uploaded font's own glyphs
6// (final review, I2). The picture is measured with the upload, and an
7// SVG in an <img> sees none of the page's fonts, so the picture carries
8// the face itself: before, it named a family the browser did not have
9// and drew the stack's next one at positions measured for another.
10//
11// The upload is Liberation Mono's regular called "Oddity Sans 2", a
12// name no machine has installed. The heading is a row of i, whose ink
13// in a monospace face is far wider than in the serif after it in the
14// stack. The picture's ink is set beside the same text drawn on a
15// canvas in the same upload, loaded from the deck as the page loads it.
16
17import (
18 "math"
19 "testing"
20 "time"
21
22 "github.com/chromedp/chromedp"
23)
24
25func TestASlidePictureDrawsInItsUploadedFont(t *testing.T) {
26 fr := newFontRigWith(t, dirStore(t))
27 css, ref := uploadFamily(t, fr, "LiberationMono-Regular.ttf", "Oddity Sans 2")
28 const text = "iiiiiiiiiiiiiiii"
29 sqid := fr.deck(t, "Picture", `[{"op":"set_meta","theme":"paper","title":"Picture"},
30 {"op":"add_slide","id":"slideone0001","layout":"flow","template":"title"},
31 {"op":"add_block","slide":"slideone0001","id":"head00000001","type":"heading","region":"heading","props":{"level":1},"markdown":"`+text+`"}]`)
32 fam := recordFamily(t, fr, sqid, css)
33 fr.ops(t, sqid, `[{"op":"set_meta","fontFiles":[`+fam+`],"fonts":{"heading":"'`+css+`', Liberation Serif, Georgia, serif","body":""}}]`)
34 fr.rig.Run(fr.navigate("/fonts"), chromedp.WaitReady(`body`))
35 fr.rig.Run(chromedp.Sleep(300 * time.Millisecond))
36
37 var got struct {
38 Picture, Canvas float64
39 Size float64
40 Error string
41 }
42 fr.eval(t, `(async () => {
43 try {
44 const url = '/d/`+sqid+`/slides/slideone0001.svg'
45 const svg = new DOMParser().parseFromString(await (await fetch(url)).text(), 'image/svg+xml')
46 const [, , W, H] = svg.documentElement.getAttribute('viewBox').split(' ').map(Number)
47 const span = [...svg.querySelectorAll('tspan')].find((s) => s.textContent.trim() === '`+text+`')
48 if (!span) return { Error: 'no tspan for the heading' }
49 const size = Number(span.getAttribute('font-size'))
50 const weight = span.getAttribute('font-weight') || '400'
51 const y = Number(span.parentElement.getAttribute('y'))
52 // ink is the width of everything not the background in rows y-size..y+size/4.
53 const ink = (ctx) => {
54 const top = Math.max(0, Math.floor(y - size)), h = Math.ceil(size * 1.25)
55 const d = ctx.getImageData(0, top, W, h).data
56 const bg = ctx.getImageData(0, 0, 1, 1).data
57 let lo = W, hi = -1
58 for (let r = 0; r < h; r++) for (let x = 0; x < W; x++) {
59 const i = (r * W + x) * 4
60 if (Math.abs(d[i] - bg[0]) + Math.abs(d[i + 1] - bg[1]) + Math.abs(d[i + 2] - bg[2]) > 96) { lo = Math.min(lo, x); hi = Math.max(hi, x) }
61 }
62 return hi < 0 ? 0 : hi - lo + 1
63 }
64 const img = new Image()
65 img.src = url
66 await img.decode()
67 const a = Object.assign(document.createElement('canvas'), { width: W, height: H }).getContext('2d')
68 a.drawImage(img, 0, 0, W, H)
69 const face = new FontFace('Reference Upload', 'url("/d/`+sqid+`/fonts/`+ref+`")', { weight })
70 await face.load()
71 document.fonts.add(face)
72 const b = Object.assign(document.createElement('canvas'), { width: W, height: H }).getContext('2d')
73 b.fillStyle = '#ffffff'
74 b.fillRect(0, 0, W, H)
75 b.fillStyle = '#000000'
76 b.font = weight + ' ' + size + 'px "Reference Upload"'
77 b.fillText('`+text+`', 20, y)
78 return { Picture: ink(a), Canvas: ink(b), Size: size }
79 } catch (e) { return { Error: String(e) } }
80 })()`, &got)
81 if got.Error != "" {
82 t.Fatal(got.Error)
83 }
84 t.Logf("ink: picture %v px, the upload on a canvas %v px, at %v px", got.Picture, got.Canvas, got.Size)
85 if got.Canvas < 100 {
86 t.Fatalf("the reference text is %v px wide: the check cannot see the font", got.Canvas)
87 }
88 if math.Abs(got.Picture-got.Canvas) > 0.05*got.Canvas {
89 t.Errorf("the heading's ink in the picture is %v px wide, in the uploaded font %v px (size %v): the picture is not drawn in the upload", got.Picture, got.Canvas, got.Size)
90 }
91}
92