| 1 | package fonts |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "testing" |
| 7 | "unicode/utf16" |
| 8 | ) |
| 9 | |
| 10 | func serifRegular(t *testing.T) *Font { |
| 11 | t.Helper() |
| 12 | f, err := Parse(Data(Face{Family: Serif})) |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | return f |
| 17 | } |
| 18 | |
| 19 | // A face whose upload has no face for its style is measured in its |
| 20 | // built-in Family, even before that face was ever parsed. |
| 21 | func TestAnUploadWithoutTheStyleMeasuresTheBuiltInFace(t *testing.T) { |
| 22 | parsedMu.Lock() |
| 23 | saved := parsed |
| 24 | parsed = map[string]*Font{} |
| 25 | parsedMu.Unlock() |
| 26 | t.Cleanup(func() { parsedMu.Lock(); parsed = saved; parsedMu.Unlock() }) |
| 27 | |
| 28 | up := UploadedFamily(&Uploaded{CSS: "Upload", Weight: 400, Font: serifRegular(t), Fallback: "Georgia, serif"}) |
| 29 | bold := Face{Family: Sans, Bold: true, Uploaded: up} |
| 30 | if bold.Upload() != nil { |
| 31 | t.Fatal("a bold face found a regular-only upload") |
| 32 | } |
| 33 | got, _ := Advance(bold, 40, "Bold words") |
| 34 | want, _ := Advance(Face{Family: Sans, Bold: true}, 40, "Bold words") |
| 35 | if got == 0 || got != want { |
| 36 | t.Errorf("measured %v, want Liberation Sans Bold's %v", got, want) |
| 37 | } |
| 38 | if bold.Stack() != Sans.Stack() || bold.Name() != "LiberationSans-Bold" { |
| 39 | t.Errorf("stack %q, name %q: the fallback face should be named as itself", bold.Stack(), bold.Name()) |
| 40 | } |
| 41 | regular := Face{Family: Sans, Uploaded: up} |
| 42 | if regular.Stack() != "'Upload', Georgia, serif" || regular.Name() != "Upload 400" { |
| 43 | t.Errorf("stack %q, name %q", regular.Stack(), regular.Name()) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // nameStrings is every (nameID, string) of a font's Windows Unicode |
| 48 | // names. |
| 49 | func nameStrings(t *testing.T, f *Font) map[uint16][]string { |
| 50 | t.Helper() |
| 51 | name := f.tables["name"] |
| 52 | count := int(binary.BigEndian.Uint16(name[2:])) |
| 53 | base := int(binary.BigEndian.Uint16(name[4:])) |
| 54 | out := map[uint16][]string{} |
| 55 | for i := 0; i < count; i++ { |
| 56 | at := 6 + i*12 |
| 57 | if binary.BigEndian.Uint16(name[at:]) != 3 { |
| 58 | continue |
| 59 | } |
| 60 | id := binary.BigEndian.Uint16(name[at+6:]) |
| 61 | length := int(binary.BigEndian.Uint16(name[at+8:])) |
| 62 | off := base + int(binary.BigEndian.Uint16(name[at+10:])) |
| 63 | raw := name[off : off+length] |
| 64 | units := make([]uint16, len(raw)/2) |
| 65 | for j := range units { |
| 66 | units[j] = binary.BigEndian.Uint16(raw[2*j:]) |
| 67 | } |
| 68 | out[id] = append(out[id], string(utf16.Decode(units))) |
| 69 | } |
| 70 | return out |
| 71 | } |
| 72 | |
| 73 | // The copy handed to the PDF calls itself by the deck's CSS name, keeps |
| 74 | // its PostScript name, and is the same font otherwise. |
| 75 | func TestRenamedCallsTheFontByTheDecksName(t *testing.T) { |
| 76 | orig := serifRegular(t) |
| 77 | b, err := orig.Renamed("Test Upload", 700, true) |
| 78 | if err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | renamed, err := Parse(b) |
| 82 | if err != nil { |
| 83 | t.Fatalf("the renamed font does not parse: %v", err) |
| 84 | } |
| 85 | names := nameStrings(t, renamed) |
| 86 | if got := names[1]; len(got) != 1 || got[0] != "Test Upload" { |
| 87 | t.Errorf("family names %q, want only Test Upload", got) |
| 88 | } |
| 89 | if len(names[16]) != 0 { |
| 90 | t.Errorf("a typographic family survived: %q", names[16]) |
| 91 | } |
| 92 | if got := names[6]; len(got) != 1 || got[0] != "LiberationSerif" { |
| 93 | t.Errorf("PostScript names %q, want LiberationSerif", got) |
| 94 | } |
| 95 | os2 := renamed.tables["OS/2"] |
| 96 | if w := binary.BigEndian.Uint16(os2[4:]); w != 700 { |
| 97 | t.Errorf("usWeightClass %d, want 700", w) |
| 98 | } |
| 99 | if sel := binary.BigEndian.Uint16(os2[62:]); sel&1 == 0 { |
| 100 | t.Error("fsSelection is not italic") |
| 101 | } |
| 102 | if !bytes.Equal(renamed.tables["glyf"], orig.tables["glyf"]) || renamed.NumGlyphs != orig.NumGlyphs { |
| 103 | t.Error("the outlines changed") |
| 104 | } |
| 105 | } |
| 106 | |