//go:build browser package slidestest // A slide picture shown in an draws an uploaded font's own glyphs // (final review, I2). The picture is measured with the upload, and an // SVG in an sees none of the page's fonts, so the picture carries // the face itself: before, it named a family the browser did not have // and drew the stack's next one at positions measured for another. // // The upload is Liberation Mono's regular called "Oddity Sans 2", a // name no machine has installed. The heading is a row of i, whose ink // in a monospace face is far wider than in the serif after it in the // stack. The picture's ink is set beside the same text drawn on a // canvas in the same upload, loaded from the deck as the page loads it. import ( "math" "testing" "time" "github.com/chromedp/chromedp" ) func TestASlidePictureDrawsInItsUploadedFont(t *testing.T) { fr := newFontRigWith(t, dirStore(t)) css, ref := uploadFamily(t, fr, "LiberationMono-Regular.ttf", "Oddity Sans 2") const text = "iiiiiiiiiiiiiiii" sqid := fr.deck(t, "Picture", `[{"op":"set_meta","theme":"paper","title":"Picture"}, {"op":"add_slide","id":"slideone0001","layout":"flow","template":"title"}, {"op":"add_block","slide":"slideone0001","id":"head00000001","type":"heading","region":"heading","props":{"level":1},"markdown":"`+text+`"}]`) fam := recordFamily(t, fr, sqid, css) fr.ops(t, sqid, `[{"op":"set_meta","fontFiles":[`+fam+`],"fonts":{"heading":"'`+css+`', Liberation Serif, Georgia, serif","body":""}}]`) fr.rig.Run(fr.navigate("/fonts"), chromedp.WaitReady(`body`)) fr.rig.Run(chromedp.Sleep(300 * time.Millisecond)) var got struct { Picture, Canvas float64 Size float64 Error string } fr.eval(t, `(async () => { try { const url = '/d/`+sqid+`/slides/slideone0001.svg' const svg = new DOMParser().parseFromString(await (await fetch(url)).text(), 'image/svg+xml') const [, , W, H] = svg.documentElement.getAttribute('viewBox').split(' ').map(Number) const span = [...svg.querySelectorAll('tspan')].find((s) => s.textContent.trim() === '`+text+`') if (!span) return { Error: 'no tspan for the heading' } const size = Number(span.getAttribute('font-size')) const weight = span.getAttribute('font-weight') || '400' const y = Number(span.parentElement.getAttribute('y')) // ink is the width of everything not the background in rows y-size..y+size/4. const ink = (ctx) => { const top = Math.max(0, Math.floor(y - size)), h = Math.ceil(size * 1.25) const d = ctx.getImageData(0, top, W, h).data const bg = ctx.getImageData(0, 0, 1, 1).data let lo = W, hi = -1 for (let r = 0; r < h; r++) for (let x = 0; x < W; x++) { const i = (r * W + x) * 4 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) } } return hi < 0 ? 0 : hi - lo + 1 } const img = new Image() img.src = url await img.decode() const a = Object.assign(document.createElement('canvas'), { width: W, height: H }).getContext('2d') a.drawImage(img, 0, 0, W, H) const face = new FontFace('Reference Upload', 'url("/d/`+sqid+`/fonts/`+ref+`")', { weight }) await face.load() document.fonts.add(face) const b = Object.assign(document.createElement('canvas'), { width: W, height: H }).getContext('2d') b.fillStyle = '#ffffff' b.fillRect(0, 0, W, H) b.fillStyle = '#000000' b.font = weight + ' ' + size + 'px "Reference Upload"' b.fillText('`+text+`', 20, y) return { Picture: ink(a), Canvas: ink(b), Size: size } } catch (e) { return { Error: String(e) } } })()`, &got) if got.Error != "" { t.Fatal(got.Error) } t.Logf("ink: picture %v px, the upload on a canvas %v px, at %v px", got.Picture, got.Canvas, got.Size) if got.Canvas < 100 { t.Fatalf("the reference text is %v px wide: the check cannot see the font", got.Canvas) } if math.Abs(got.Picture-got.Canvas) > 0.05*got.Canvas { 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) } }