8000 Extend name table with more general functions by ollimeier · Pull Request #2526 · fonttools/fonttools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Extend name table with more general functions #2526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dist/
*.egg-info/
*.egg
MANIFEST
.idea

# Installer logs
pip-log.txt
Expand Down
38 changes: 38 additions & 0 deletions Lib/fontTools/ttLib/tables/_n_a_m_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,44 @@ def getDebugName(self, nameID):
else:
return None

def getFirstDebugName(self, nameIDs):
for nameID in nameIDs:
name = self.getDebugName(nameID)
if name is not None:
return name
return None

def getBestFamilyName(self):
# 21 = WWS Family Name
# 16 = Typographic Family Name
# 1 = Family Name
return self.getFirstDebugName((21, 16, 1))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should always priorise the record 21 and 16.

There should be a parameter to specify what we want prioritized. For example, in my case, I would like to get the "real" Family Name.


def getBestSubFamilyName(self):
# 22 = WWS SubFamily Name
# 17 = Typographic SubFamily Name
# 2 = SubFamily Name
return self.getFirstDebugName((22, 17, 2))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should always priorise the record 22 and 17.

There should be a parameter. For example, in my case, I would like to get the "real" SubFamily Name.


def getBestFullName(self):
# 4 = Full Name
# 6 = PostScript Name
for nameIDs in ((21, 22), (16, 17), (1, 2), (4, ), (6, )):
if len(nameIDs) == 2:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you check if the lenght of nameIDs is 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @moi15moi , thanks for asking. The reason behind is, because the loop starts with the preferred name table ID pairs (21, 22), (16, 17), (1, 2). And if none of them are present (which might be the case in obfuscated web font) then use only the (4, ), (6, ).

Copy link
@moi15moi moi15moi May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks.

I am trying to understand why getBestFullName, getBestFamilyName and getBestSubFamilyName does not always return me the good name.

I did some commit to make to get the right name, but I am not too sure if I am allow to use: utf-16-be
https://github.com/moi15moi/fonttools

Edit: Here is how FontConfig get the right name: https://gitlab.freedesktop.org/fontconfig/fontconfig/-/blob/main/src/fcfreetype.c#L693
I don't know a lot of c, but from what I can see, that's should be the if we should try to replicate: https://gitlab.freedesktop.org/fontconfig/fontconfig/-/blob/main/src/fcfreetype.c#L715

name_fam = self.getDebugName(nameIDs[0])
name_subfam = self.getDebugName(nameIDs[1])
if None in [name_fam, name_subfam]:
continue # if any is None, skip
name = f"{name_fam} {name_subfam}"
if name_subfam.lower() == 'regular':
name = f"{name_fam}"
return name
else:
name = self.getDebugName(nameIDs[0])
if name is not None:
return name
return None

def setName(self, string, nameID, platformID, platEncID, langID):
""" Set the 'string' for the name record identified by 'nameID', 'platformID',
'platEncID' and 'langID'. If a record with that nameID doesn't exist, create it
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Simon Daniels, Peter Dekkers, Behdad Esfahbod, Behnam Esfahbod, Hannes
Famira, Sam Fishman, Matt Fontaine, Takaaki Fuji, Yannis Haralambous, Greg
Hitchcock, Jeremie Hornus, Khaled Hosny, John Hudson, Denis Moyogo Jacquerye,
Jack Jansen, Tom Kacvinsky, Jens Kutilek, Antoine Leca, Werner Lemberg, Tal
Leming, Peter Lofting, Cosimo Lupo, Masaya Nakamura, Dave Opstad,
Leming, Peter Lofting, Cosimo Lupo, Olli Meier, Masaya Nakamura, Dave Opstad,
Laurence Penney, Roozbeh Pournader, Garret Rieger, Read Roberts, Guido
van Rossum, Just van Rossum, Andreas Seidel, Georg Seifert, Chris
Simpkins, Miguel Sousa, Adam Twardoch, Adrien Tétar, Vitaly Volkov,
Expand Down
97 changes: 97 additions & 0 deletions Tests/ttLib/tables/_n_a_m_e_test.py
3DD7
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,103 @@ def test_extended_unknown(self):
self.assertEqual(name.getEncoding(None), None)
self.assertEqual(name.getEncoding(default=None), None)

def test_get_family_name(self):
name = table__n_a_m_e()
name.names = [
makeName("Copyright", 0, 1, 0, 0),
makeName("Family Name ID 1", 1, 1, 0, 0),
makeName("SubFamily Name ID 2", 2, 1, 0, 0),
makeName("Unique Name ID 3", 3, 1, 0, 0),
makeName("Full Name ID 4", 4, 1, 0, 0),
makeName("PS Name ID 6", 6, 1, 0, 0),
makeName("Version Name ID 5", 5, 1, 0, 0),
makeName("Trademark Name ID 7", 7, 1, 0, 0),
]

result_value = name.getBestFamilyName()
self.assertEqual("Family Name ID 1", result_value)

expected_value = "Family Name ID 16"
name.setName(expected_value, 16, 1, 0, 0)
result_value = name.getBestFamilyName()
self.assertEqual(expected_value, result_value)

expected_value = "Family Name ID 21"
name.setName(expected_value, 21, 1, 0, 0)
result_value = name.getBestFamilyName()
self.assertEqual(expected_value, result_value)

def test_get_subfamily_name(self):
name = table__n_a_m_e()
name.names = [
makeName("Copyright", 0, 1, 0, 0),
makeName("Family Name ID 1", 1, 1, 0, 0),
makeName("SubFamily Name ID 2", 2, 1, 0, 0),
makeName("Unique Name ID 3", 3, 1, 0, 0),
makeName("Full Name ID 4", 4, 1, 0, 0),
makeName("PS Name ID 6", 6, 1, 0, 0),
makeName("Version Name ID 5", 5, 1, 0, 0),
makeName("Trademark Name ID 7", 7, 1, 0, 0),
]

result_value = name.getBestSubFamilyName()
self.assertEqual("SubFamily Name ID 2", result_value)

expected_value = "Family Name ID 17"
name.setName(expected_value, 17, 1, 0, 0)
result_value = name.getBestSubFamilyName()
self.assertEqual(expected_value, result_value)

expected_value = "Family Name ID 22"
< 6D40 /td> name.setName(expected_value, 22, 1, 0, 0)
result_value = name.getBestSubFamilyName()
self.assertEqual(expected_value, result_value)

def test_get_nice_full_name(self):
name = table__n_a_m_e()
name.names = [
makeName("NID 1", 1, 1, 0, 0),
makeName("NID 2", 2, 1, 0, 0),
makeName("NID 4", 4, 1, 0, 0),
makeName("NID 6", 6, 1, 0, 0),
]

result_value = name.getBestFullName()
self.assertEqual("NID 1 NID 2", result_value)

expected_value = "NID 1 NID 2"
# expection is still NID 1 NID 2,
# because name ID 17 is missing
name.setName("NID 16", 16, 1, 0, 0)
result_value = name.getBestFullName()
self.assertEqual(expected_value, result_value)

name.setName('NID 17', 17, 1, 0, 0)
result_value = name.getBestFullName()
self.assertEqual("NID 16 NID 17", result_value)

expected_value = "NID 16 NID 17"
# expection is still NID 16 NID 17,
# because name ID 21 is missing
name.setName('NID 21', 21, 1, 0, 0)
result_value = name.getBestFullName()
self.assertEqual(expected_value, result_value)

name.setName('NID 22', 22, 1, 0, 0)
result_value = name.getBestFullName()
self.assertEqual("NID 21 NID 22", result_value)

for NID in [2, 16, 17, 21, 22]:
name.removeNames(NID)

result_value = name.getBestFullName()
self.assertEqual("NID 4", result_value)

name.setName('Regular', 2, 1, 0, 0)
result_value = name.getBestFullName()
self.assertEqual("NID 1", result_value)


if __name__ == "__main__":
import sys
sys.exit(unittest.main())
0