Skip to content

Commit

Permalink
Merge pull request #7 from sampila/fix-get-font-names
Browse files Browse the repository at this point in the history
Fix get multiple languages of font names
  • Loading branch information
gunnsth authored Nov 1, 2022
2 parents 05894c9 + 5f17edb commit 2ddb2e4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.DS_Store
17 changes: 11 additions & 6 deletions table_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ func (f *font) GetNameByID(nameID int) string {
return ""
}

// GetNameRecords returns name records as slice map.
func (f *font) GetNameRecords() []map[uint16]string {
var nameRecords []map[uint16]string
nameRecord := make(map[uint16]string)
// GetNameRecords returns name records as map of language ID
// that contais name ID and it's value.
func (f *font) GetNameRecords() map[uint16]map[uint16]string {
var nameRecords = make(map[uint16]map[uint16]string, 0)
for _, nr := range f.name.nameRecords {
nameRecord[nr.nameID] = nr.Decoded()
nameRecords = append(nameRecords, nameRecord)
nameRec, ok := nameRecords[nr.languageID]
if ok {
nameRec[nr.nameID] = nr.Decoded()
} else {
nameRec = map[uint16]string{nr.nameID: nr.Decoded()}
}
nameRecords[nr.languageID] = nameRec
}
return nameRecords
}
Expand Down
119 changes: 119 additions & 0 deletions table_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,122 @@ func TestNameTable(t *testing.T) {
})
}
}

func TestGetNameRecords(t *testing.T) {
testcases := []struct {
fontPath string
numNames int
expected map[uint16]map[uint16]string
}{
{
"./testdata/FreeSans.ttf",
3,
map[uint16]map[uint16]string{
0: map[uint16]string{
0: "Copyleft 2002, 2003, 2005 Free Software Foundation.",
1: "FreeSans",
2: "Medium",
3: "FontForge 2.0 : Free Sans : 18-5-2007",
4: "Free Sans",
5: "Version $Revision: 1.79 $ ",
6: "FreeSans",
13: "The use of this font is granted subject to GNU General Public License.",
14: "http://www.gnu.org/copyleft/gpl.html",
19: "The quick brown fox jumps over the lazy dog.",
},
1033: map[uint16]string{
0: "Copyleft 2002, 2003, 2005 Free Software Foundation.",
1: "FreeSans",
2: "Medium",
3: "FontForge 2.0 : Free Sans : 18-5-2007",
4: "Free Sans",
5: "Version $Revision: 1.79 $ ",
6: "FreeSans",
13: "The use of this font is granted subject to GNU General Public License.",
14: "http://www.gnu.org/copyleft/gpl.html",
19: "The quick brown fox jumps over the lazy dog.",
},
1060: map[uint16]string{
2: "navadno",
13: "Dovoljena je uporaba v skladu z licenco GNU General Public License.",
14: "http://www.gnu.org/copyleft/gpl.html",
19: "Šerif bo za vajo spet kuhal domače žgance.",
},
},
},
{
"./testdata/wts11.ttf",
4,
map[uint16]map[uint16]string{
0: map[uint16]string{
0: "(C)Copyright Dr. Hann-Tzong Wang, 2002-2004.",
1: "HanWang KaiBold-Gb5",
2: "Regular",
3: "HanWang KaiBold-Gb5",
4: "HanWang KaiBold-Gb5",
5: "Version 1.3(license under GNU GPL)",
6: "HanWang KaiBold-Gb5",
7: "HanWang KaiBold-Gb5 is a registered trademark of HtWang Graphics Laboratory",
10: "HtWang Fonts(1), March 8, 2002; 1.00, initial release; HtWang Fonts(17), March 5, 2004; GJL(040519). Maintain by CLE Project.",
13: "(C)Copyright Dr. Hann-Tzong Wang, 2002-2004.\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.",
14: "http://www.gnu.org/licenses/gpl.txt",
},
1028: map[uint16]string{
0: "(C)Copyright Dr. Hann-Tzong Wang, 2002-2004.",
1: "王漢宗粗楷體簡",
2: "Regular",
3: "王漢宗粗楷體簡",
4: "王漢宗粗楷體簡",
5: "Version 1.3(license under GNU GPL)",
6: "王漢宗粗楷體簡",
7: "王漢宗粗楷體簡 is a registered trademark of HtWang Graphics Laboratory",
10: "HtWang Fonts(1), March 8, 2002; 1.00, initial release; HtWang Fonts(17), March 5, 2004; GJL(040519). Maintain by CLE Project.",
13: "(C)Copyright Dr. Hann-Tzong Wang, 2002-2004.\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.",
14: "http://www.gnu.org/licenses/gpl.txt",
},
1033: map[uint16]string{
0: "(C)Copyright Dr. Hann-Tzong Wang, 2002-2004.",
1: "HanWang KaiBold-Gb5",
2: "Regular",
3: "HanWang KaiBold-Gb5",
4: "HanWang KaiBold-Gb5",
5: "Version 1.3(license under GNU GPL)",
6: "HanWang KaiBold-Gb5",
7: "HanWang KaiBold-Gb5 is a registered trademark of HtWang Graphics Laboratory",
10: "HtWang Fonts(1), March 8, 2002; 1.00, initial release; HtWang Fonts(17), March 5, 2004; GJL(040519). Maintain by CLE Project.",
13: "(C)Copyright Dr. Hann-Tzong Wang, 2002-2004.\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.",
14: "http://www.gnu.org/licenses/gpl.txt",
},
2052: map[uint16]string{
0: "(C)Copyright Dr. Hann-Tzong Wang, 2002-2004.",
1: "王汉宗粗楷体简",
2: "Regular",
3: "王汉宗粗楷体简",
4: "王汉宗粗楷体简",
5: "Version 1.3(license under GNU GPL)",
6: "王汉宗粗楷体简",
7: "王汉宗粗楷体简 is a registered trademark of HtWang Graphics Laboratory",
10: "HtWang Fonts(1), March 8, 2002; 1.00, initial release; HtWang Fonts(17), March 5, 2004; GJL(040519). Maintain by CLE Project.",
13: "(C)Copyright Dr. Hann-Tzong Wang, 2002-2004.\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.",
14: "http://www.gnu.org/licenses/gpl.txt",
},
},
},
}

for _, tc := range testcases {
f, err := os.Open(tc.fontPath)
require.NoError(t, err)
defer f.Close()

br := newByteReader(f)
fnt, err := parseFont(br)
require.NoError(t, err)

// Get name records.
nameRecords := fnt.GetNameRecords()

assert.Equal(t, tc.numNames, len(nameRecords))
assert.Equal(t, tc.expected, nameRecords)
}
}

0 comments on commit 2ddb2e4

Please sign in to comment.