| 1 | package layout_test |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/slides/internal/slides/deck" |
| 8 | "amadan.net/oficina/slides/internal/slides/fonts" |
| 9 | "amadan.net/oficina/slides/internal/slides/layout" |
| 10 | ) |
| 11 | |
| 12 | // A static face is chosen for a weight as CSS chooses one (final |
| 13 | // review, I5): for regular text 400, then up to 500, then lighter, then |
| 14 | // heavier; for bold 700 and up, then lighter. A family with only a 500 |
| 15 | // was never used: layout took only faces covering exactly 400 or 700. |
| 16 | // The browser draws a missing bold by thickening the face it chose, |
| 17 | // which keeps that face's advances; so a picture measuring with that |
| 18 | // same face breaks its lines where the browser does. Pictures do not |
| 19 | // thicken it: that limit stays. |
| 20 | |
| 21 | // weighted is a face of weight w, marked for pictures, under ref. |
| 22 | func weighted(ref string, w int) deck.FontFace { |
| 23 | return deck.FontFace{Ref: ref, Format: "ttf", Weight: [2]int{w, w}, Pictures: true} |
| 24 | } |
| 25 | |
| 26 | // drawnIn says which file each named run of uploadDeck is measured in: |
| 27 | // the name of the file whose advances it has, or "" for the fallback. |
| 28 | func drawnIn(t *testing.T, files map[string]string, faces ...deck.FontFace) map[string]string { |
| 29 | t.Helper() |
| 30 | bytes := map[string][]byte{} |
| 31 | for ref, file := range files { |
| 32 | bytes[ref] = libFile(t, file) |
| 33 | } |
| 34 | p, warnings, err := layout.New(uploadDeck(faces...), layout.Options{Fonts: newLoader(bytes).load}).Layout(0) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | noFontWarning(t, warnings) |
| 39 | out := map[string]string{} |
| 40 | for _, name := range []string{"plain words", "bold", "Heading"} { |
| 41 | run := runNamed(t, runsOf(p), name) |
| 42 | if run.Face.Upload() == nil { |
| 43 | out[name] = "" |
| 44 | continue |
| 45 | } |
| 46 | got, _ := fonts.Advance(run.Face, run.Size, run.S) |
| 47 | for _, file := range files { |
| 48 | if math.Abs(got-serifWidth(t, file, run.Size, run.S)) < 1e-9 { |
| 49 | out[name] = file |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return out |
| 54 | } |
| 55 | |
| 56 | func TestAStaticFaceIsChosenForAWeightAsCSSChooses(t *testing.T) { |
| 57 | const ( |
| 58 | regular = "LiberationSerif-Regular.ttf" |
| 59 | bold = "LiberationSerif-Bold.ttf" |
| 60 | refA = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 61 | refB = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" |
| 62 | ) |
| 63 | for _, c := range []struct { |
| 64 | name string |
| 65 | files map[string]string |
| 66 | faces []deck.FontFace |
| 67 | want map[string]string |
| 68 | }{ |
| 69 | {"a 500 alone sets everything", map[string]string{refA: regular}, |
| 70 | []deck.FontFace{weighted(refA, 500)}, |
| 71 | map[string]string{"plain words": regular, "bold": regular, "Heading": regular}}, |
| 72 | {"a 700 alone sets regular text too", map[string]string{refB: bold}, |
| 73 | []deck.FontFace{weighted(refB, 700)}, |
| 74 | map[string]string{"plain words": bold, "bold": bold, "Heading": bold}}, |
| 75 | {"a 400 alone sets bold too", map[string]string{refA: regular}, |
| 76 | []deck.FontFace{weighted(refA, 400)}, |
| 77 | map[string]string{"plain words": regular, "bold": regular, "Heading": regular}}, |
| 78 | {"300 and 600: regular takes the lighter, bold the nearer below", map[string]string{refA: regular, refB: bold}, |
| 79 | []deck.FontFace{weighted(refA, 300), weighted(refB, 600)}, |
| 80 | map[string]string{"plain words": regular, "bold": bold, "Heading": bold}}, |
| 81 | {"500 and 900: regular takes 500, bold 900", map[string]string{refA: regular, refB: bold}, |
| 82 | []deck.FontFace{weighted(refB, 900), weighted(refA, 500)}, |
| 83 | map[string]string{"plain words": regular, "bold": bold, "Heading": bold}}, |
| 84 | } { |
| 85 | t.Run(c.name, func(t *testing.T) { |
| 86 | got := drawnIn(t, c.files, c.faces...) |
| 87 | for run, want := range c.want { |
| 88 | if got[run] != want { |
| 89 | t.Errorf("%q is set in %q, want %q", run, got[run], want) |
| 90 | } |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // The deck's CSS name is the one alias pattern the library makes, the |
| 97 | // document takes and layout draws: an upload the library called |
| 98 | // "Brand" from a font named "_Brand" is drawn. |
| 99 | func TestEveryNameTheDocumentTakesIsDrawn(t *testing.T) { |
| 100 | for _, css := range []string{"Brand", "Brand 2", "Brand.Sans_x-1", "9 Point"} { |
| 101 | if !deck.FontCSSName(css) { |
| 102 | t.Errorf("the document refuses %q", css) |
| 103 | } |
| 104 | } |
| 105 | for _, css := range []string{"_Brand", "-x", " Brand", "a,b", "a'b", "", "Café"} { |
| 106 | if deck.FontCSSName(css) { |
| 107 | t.Errorf("the document takes %q", css) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |