[cffLib.specializer] Add programToString and stringToProgram from _test.py

This commit is contained in:
Behdad Esfahbod 2017-05-07 22:12:19 -06:00
parent a8b841a4a9
commit 30b804003e
2 changed files with 28 additions and 35 deletions

View File

@ -5,6 +5,26 @@
from __future__ import print_function, division, absolute_import from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
def stringToProgram(string):
if isinstance(string, basestring):
string = string.split()
program = []
for token in string:
try:
token = int(token)
except ValueError:
try:
token = float(token)
except ValueError:
pass
program.append(token)
return program
def programToString(program):
return ' '.join(str(x) for x in program)
def programToCommands(program): def programToCommands(program):
"""Takes a T2CharString program list and returns list of commands. """Takes a T2CharString program list and returns list of commands.
Each command is a two-tuple of commandname,arg-list. The commandname might Each command is a two-tuple of commandname,arg-list. The commandname might
@ -53,7 +73,6 @@ def commandsToProgram(commands):
return program return program
def _everyN(el, n): def _everyN(el, n):
"""Group the list el into groups of size n""" """Group the list el into groups of size n"""
if len(el) % n != 0: raise ValueError(el) if len(el) % n != 0: raise ValueError(el)
@ -496,22 +515,13 @@ if __name__ == '__main__':
if len(sys.argv) == 1: if len(sys.argv) == 1:
import doctest import doctest
sys.exit(doctest.testmod().failed) sys.exit(doctest.testmod().failed)
program = [] program = stringToProgram(sys.argv[1:])
for token in sys.argv[1:]: print("Program:"); print(programToString(program))
try:
token = int(token)
except ValueError:
try:
token = float(token)
except ValueError:
pass
program.append(token)
print("Program:"); print(program)
commands = programToCommands(program) commands = programToCommands(program)
print("Commands:"); print(commands) print("Commands:"); print(commands)
program2 = commandsToProgram(commands) program2 = commandsToProgram(commands)
print("Program from commands:"); print(program2) print("Program from commands:"); print(programToString(program2))
assert program == program2 assert program == program2
print("Generalized program:"); print(generalizeProgram(program)) print("Generalized program:"); print(programToString(generalizeProgram(program)))
print("Specialized program:"); print(specializeProgram(program)) print("Specialized program:"); print(programToString(specializeProgram(program)))

View File

@ -1,32 +1,15 @@
from __future__ import print_function, division, absolute_import from __future__ import print_function, division, absolute_import
from fontTools.cffLib.specializer import generalizeProgram, specializeProgram from fontTools.cffLib.specializer import programToString, stringToProgram, generalizeProgram, specializeProgram
import unittest import unittest
def charstr2program(string):
program = []
for token in string.split():
try:
token = int(token)
except ValueError:
try:
token = float(token)
except ValueError:
pass
program.append(token)
return program
def program2charstr(lst):
return ' '.join(str(x) for x in lst)
def get_generalized_charstr(charstr): def get_generalized_charstr(charstr):
return program2charstr(generalizeProgram(charstr2program(charstr))) return programToString(generalizeProgram(stringToProgram(charstr)))
def get_specialized_charstr(charstr, **kwargs): def get_specialized_charstr(charstr, **kwargs):
return program2charstr(specializeProgram(charstr2program(charstr), **kwargs)) return programToString(specializeProgram(stringToProgram(charstr), **kwargs))
class CFFGeneralizeProgramTest(unittest.TestCase): class CFFGeneralizeProgramTest(unittest.TestCase):