Beispiel #1
0
    def _parse(self, text, i, _parsing_unicodes=False):
        """Recursive function to parse a single dictionary, list, or value."""

        m = self.start_dict_re.match(text, i)
        if m:
            parsed = m.group(0)
            i += len(parsed)
            return self._parse_dict(text, i)

        m = self.start_list_re.match(text, i)
        if m:
            parsed = m.group(0)
            i += len(parsed)
            return self._parse_list(text, i)

        if _parsing_unicodes:
            m = self.unicode_list_re.match(text, i)
            if m:
                parsed = m.group(0)
                i += len(parsed)
                unicode_list = m.group(1).split(",")
                return unicode_list, i

        m = self.value_re.match(text, i)
        if m:
            parsed, value = m.group(0), self._trim_value(m.group(1))
            i += len(parsed)
            if hasattr(self.current_type, "read"):
                reader = self.current_type()
                # Give the escaped value to `read` to be symetrical with
                # `plistValue` which handles the escaping itself.
                value = reader.read(m.group(1))
                return value, i

            if (self.current_type is None
                    or self.current_type in (dict, OrderedDict)):
                self.current_type = self._guess_current_type(parsed, value)

            if self.current_type == bool:
                value = bool(int(value))  # bool(u'0') returns True
                return value, i

            value = self.current_type(value)

            return value, i

        m = self.hex_re.match(text, i)
        if m:
            from glyphsLib.types import BinaryData
            parsed, value = m.group(0), m.group(1)
            decoded = BinaryData.fromHex(value)
            i += len(parsed)
            return decoded, i
        else:
            self._fail('Unexpected content', text, i)
Beispiel #2
0
def to_glyphs_master_user_data(self, ufo, master):
    """Set the GSFontMaster userData from the UFO master-specific lib data."""
    target_user_data = master.userData
    for key, value in ufo.lib.items():
        if _user_data_has_no_special_meaning(key):
            target_user_data[key] = value

    # Save UFO data files
    if ufo.data.fileNames:
        from glyphsLib.types import BinaryData
        ufo_data = {}
        for os_filename in ufo.data.fileNames:
            filename = posixpath.join(*os_filename.split(os.path.sep))
            ufo_data[filename] = BinaryData(ufo.data[os_filename])
        master.userData[UFO_DATA_KEY] = ufo_data
def test_ufo_data_into_font_master_user_data(tmpdir):
    filename = os.path.join('org.customTool', 'ufoData.bin')
    data = b'\x00\x01\xFF'
    ufo = defcon.Font()
    ufo.data[filename] = data

    font = to_glyphs([ufo])
    # Round-trip to disk for this one because I'm not sure there are other
    # tests that read-write binary data
    path = os.path.join(str(tmpdir), 'font.glyphs')
    font.save(path)
    font = classes.GSFont(path)

    # The path in the glyphs file should be os-agnostic (forward slashes)
    assert font.masters[0].userData[GLYPHLIB_PREFIX + 'ufoData'] == {
        'org.customTool/ufoData.bin': BinaryData(data)
    }

    ufo, = to_ufos(font)

    assert ufo.data[filename] == data
def test_ufo_data_into_font_master_user_data(tmpdir, ufo_module):
    filename = "org.customTool/ufoData.bin"
    data = b"\x00\x01\xFF"
    ufo = ufo_module.Font()
    ufo.data[filename] = data

    font = to_glyphs([ufo])
    # Round-trip to disk for this one because I'm not sure there are other
    # tests that read-write binary data
    path = os.path.join(str(tmpdir), "font.glyphs")
    font.save(path)
    font = classes.GSFont(path)

    # The path in the glyphs file should be os-agnostic (forward slashes)
    assert font.masters[0].userData[GLYPHLIB_PREFIX + "ufoData"] == {
        "org.customTool/ufoData.bin": BinaryData(data)
    }

    (ufo, ) = to_ufos(font)

    assert ufo.data[filename] == data