def test_different_ttfonts_have_different_configs(COMPRESSION_LEVEL): cfg = Config({COMPRESSION_LEVEL: 5}) ttFont1 = TTFont(cfg=cfg) ttFont2 = TTFont(cfg=cfg) ttFont2.cfg.set(COMPRESSION_LEVEL, 6) assert 5 == ttFont1.cfg.get(COMPRESSION_LEVEL) assert 6 == ttFont2.cfg.get(COMPRESSION_LEVEL)
def test_implements_mutable_mapping(COMPRESSION_LEVEL): cfg = Config() cfg[COMPRESSION_LEVEL] = 2 assert 2 == cfg[COMPRESSION_LEVEL] assert list(iter(cfg)) assert 1 == len(cfg) del cfg[COMPRESSION_LEVEL] assert 0 == cfg[COMPRESSION_LEVEL] assert not list(iter(cfg)) assert 0 == len(cfg)
def test_can_register_option(): MY_OPTION = Config.register_option( name="tests:MY_OPTION", help="Test option, value should be True or False, default = True", default=True, parse=lambda v: v in ("True", "true", 1, True), validate=lambda v: v == True or v == False, ) assert MY_OPTION.name == "tests:MY_OPTION" assert (MY_OPTION.help == "Test option, value should be True or False, default = True") assert MY_OPTION.default == True assert MY_OPTION.parse("true") == True assert MY_OPTION.validate("hello") == False ttFont = TTFont(cfg={"tests:MY_OPTION": True}) assert True == ttFont.cfg.get("tests:MY_OPTION") assert True == ttFont.cfg.get(MY_OPTION)
def __init__(self, file=None, res_name_or_index=None, sfntVersion="\000\001\000\000", flavor=None, checkChecksums=0, verbose=None, recalcBBoxes=True, allowVID=NotImplemented, ignoreDecompileErrors=False, recalcTimestamp=True, fontNumber=-1, lazy=None, quiet=None, _tableCache=None, cfg={}): for name in ("verbose", "quiet"): val = locals().get(name) if val is not None: deprecateArgument(name, "configure logging instead") setattr(self, name, val) self.lazy = lazy self.recalcBBoxes = recalcBBoxes self.recalcTimestamp = recalcTimestamp self.tables = {} self.reader = None self.cfg = cfg.copy() if isinstance(cfg, AbstractConfig) else Config(cfg) self.ignoreDecompileErrors = ignoreDecompileErrors if not file: self.sfntVersion = sfntVersion self.flavor = flavor self.flavorData = None return if not hasattr(file, "read"): closeStream = True # assume file is a string if res_name_or_index is not None: # see if it contains 'sfnt' resources in the resource or data fork from . import macUtils if res_name_or_index == 0: if macUtils.getSFNTResIndices(file): # get the first available sfnt font. file = macUtils.SFNTResourceReader(file, 1) else: file = open(file, "rb") else: file = macUtils.SFNTResourceReader(file, res_name_or_index) else: file = open(file, "rb") else: # assume "file" is a readable file object closeStream = False file.seek(0) if not self.lazy: # read input file in memory and wrap a stream around it to allow overwriting file.seek(0) tmp = BytesIO(file.read()) if hasattr(file, 'name'): # save reference to input file name tmp.name = file.name if closeStream: file.close() file = tmp self._tableCache = _tableCache self.reader = SFNTReader(file, checkChecksums, fontNumber=fontNumber) self.sfntVersion = self.reader.sfntVersion self.flavor = self.reader.flavor self.flavorData = self.reader.flavorData
def __init__(self, glyphs): self.glyphOrder_ = glyphs self.reverseGlyphOrderDict_ = {g: i for i, g in enumerate(glyphs)} self.lazy = False self.tables = {} self.cfg = Config()
def test_value_parsing_can_error(COMPRESSION_LEVEL): with pytest.raises(ConfigValueParsingError): TTFont(cfg=Config( {COMPRESSION_LEVEL: "not an int"}, parse_values=True, ))
def test_value_gets_parsed_if_asked(COMPRESSION_LEVEL): # Note: value given as a string ttFont = TTFont(cfg=Config({COMPRESSION_LEVEL: "8"}, parse_values=True)) assert 8 == ttFont.cfg.get(COMPRESSION_LEVEL)