Skip to content

Commit

Permalink
Possible prevention of issue #90
Browse files Browse the repository at this point in the history
This shuffles the order of evaluating a uni value to a name.
* if the value is None, return None
* if the value is in the uniXXXX ranges, return one of thos ^1
* otherwise return a name we have stored

^1 - this does not check if the uniXXXX values actually exist in unicode.
  • Loading branch information
LettError committed Mar 2, 2022
1 parent 79ed588 commit f776b14
Showing 1 changed file with 56 additions and 62 deletions.
118 changes: 56 additions & 62 deletions Lib/glyphNameFormatter/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ def uniPatternName(v):

def u2n(value):
"""Unicode value to glyphname"""
global uni2name
v = uni2name.get(value)
if v is not None:
return v
if value is None:
return None
if isIdeograph(value):
return uniPatternName(value)
global uni2name
return uni2name.get(value)

def n2u(name):
"""Glyphname to Unicode value"""
Expand Down Expand Up @@ -198,61 +198,55 @@ def isUpper(uni):
return chr(uni).isupper()

if __name__ == "__main__":
print("upperToLower map:", len(upperToLower))
print("lowerToUpper map:", len(lowerToUpper))

print("A", isUpper(ord("A")))
print("a", isUpper(ord("a")))
print("!", isUpper(ord("!")))

allNames = list(name2uni.keys())
allNames.sort()
print("\ntest lower -> upper -> lower")
for n in allNames:
upr = n2N(n)
if upr != n and upr is not None:
lwr = N2n(upr)
if n != lwr:
print("\t\tn2N failed", n, "->", upr, "->", lwr)
#else:
# print("\ta ok", n, "->", upr, "->", lwr)

lwr = N2n(n)
if lwr != n and lwr is not None:
upr = n2N(lwr)
if n != upr:
print("\t\tN2n failed", n, "->", lwr, "->", upr)
#else:
# print("\tb ok", n, "->", lwr, "->", upr)
assert N2n("non-existing-glyphname") == "non-existing-glyphname"
assert n2N("non-existing-glyphname") == "non-existing-glyphname"
assert n2N("germandbls") == "germandbls"
assert N2n("A") == 'a'
assert n2N("a") == 'A'
assert U2u(65) == 97 # A -> a
assert u2U(97) == 65 # a -> A

# ideograph support
assert isIdeograph(1) == False
assert isIdeograph(0x2A700) == True
testIdeographValues = [0x2A700, 0x3401, 0x9FFF]
for v in testIdeographValues:
assert n2u(u2n(v)) == v
# check if a value an ideographRange returns the proper name
for k, v in ideographRanges.items():
a, b = k
c = int(.5*(a+b))
assert u2r(c) == v

if False:
allNames = list(name2uni.keys())
allNames.sort()
for n in allNames:
print(n)
assert(n == u2n(n2u(n)))


for n in allNames:
u = n2u(n)
print(n, u2r(u))

print("upperToLower map:", len(upperToLower))
print("lowerToUpper map:", len(lowerToUpper))

print("A", isUpper(ord("A")))
print("a", isUpper(ord("a")))
print("!", isUpper(ord("!")))

allNames = list(name2uni.keys())
allNames.sort()
print("\ntest lower -> upper -> lower")
if False:
for n in allNames:
upr = n2N(n)
if upr != n and upr is not None:
lwr = N2n(upr)
if n != lwr:
print("\t\tn2N failed", n, "->", upr, "->", lwr)

lwr = N2n(n)
if lwr != n and lwr is not None:
upr = n2N(lwr)
if n != upr:
print("\t\tN2n failed", n, "->", lwr, "->", upr)
assert N2n("non-existing-glyphname") == "non-existing-glyphname"
assert n2N("non-existing-glyphname") == "non-existing-glyphname"
assert n2N("germandbls") == "germandbls"
assert N2n("A") == 'a'
assert n2N("a") == 'A'
assert U2u(65) == 97 # A -> a
assert u2U(97) == 65 # a -> A

# ideograph support
assert isIdeograph(1) == False
assert isIdeograph(0x2A700) == True
print(u2n(0x2A700))
testIdeographValues = [0x2A700, 0x3401, 0x9FFF]
for v in testIdeographValues:
assert n2u(u2n(v)) == v
# check if a value an ideographRange returns the proper name

for k, v in ideographRanges.items():
a, b = k
c = int(.5*(a+b))
assert u2r(c) == v

if False: # really long print of all possible names
minUni = min(uni2name.keys())
maxUni = max(uni2name.keys())
for u in range(minUni, maxUni):
r = u2n(u)
if r is not None:
print(f'{u}:\t{u2n(u)}')

0 comments on commit f776b14

Please sign in to comment.