Example #1
0
 def test_write_path(self):
     path = classes.GSPath()
     # http://docu.glyphsapp.com/#gspath
     # parent: not written
     # nodes
     node = classes.GSNode()
     path.nodes.append(node)
     # segments: computed, objective-c
     # closed
     path.closed = True
     # direction: computed
     # bounds: computed
     # selected: not written
     # bezierPath: computed
     self.assertWrites(
         path,
         dedent("""\
         {
         closed = 1;
         nodes = (
         "0 0 LINE"
         );
         }
     """),
     )
Example #2
0
    def test_write_hint(self):
        hint = classes.GSHint()
        # http://docu.glyphsapp.com/#gshint
        layer = classes.GSLayer()
        path1 = classes.GSPath()
        layer.paths.append(path1)
        node1 = classes.GSNode(Point(100, 100))
        path1.nodes.append(node1)
        hint.originNode = node1

        node2 = classes.GSNode(Point(200, 200))
        path1.nodes.append(node2)
        hint.targetNode = node2

        node3 = classes.GSNode(Point(300, 300))
        path1.nodes.append(node3)
        hint.otherNode1 = node3

        path2 = classes.GSPath()
        layer.paths.append(path2)
        node4 = classes.GSNode(Point(400, 400))
        path2.nodes.append(node4)
        hint.otherNode2 = node4

        hint.type = classes.CORNER
        hint.options = classes.TTROUND | classes.TRIPLE
        hint.horizontal = True
        # selected: not written
        hint.name = "My favourite hint"
        self.assertWrites(
            hint,
            dedent(
                """\
            {
            horizontal = 1;
            origin = "{0, 0}";
            target = "{0, 1}";
            other1 = "{0, 2}";
            other2 = "{1, 0}";
            type = 16;
            name = "My favourite hint";
            options = 128;
            }
        """
            ),
        )
Example #3
0
    def test_write_node(self):
        node = classes.GSNode(Point(10, 30), classes.GSNode.CURVE)
        # http://docu.glyphsapp.com/#gsnode
        # position: already set
        # #type: already set
        # smooth
        node.smooth = True
        # connection: deprecated
        # selected: not written
        # index, nextNode, prevNode: computed
        # name
        node.name = "top-left corner"
        # userData
        node.userData["rememberToDownloadARealRemindersApp"] = True
        self.assertWritesValue(
            node,
            '"10 30 CURVE SMOOTH {name = \\"top-left corner\\";\\n\
rememberToDownloadARealRemindersApp = 1;}"',
        )

        # Write floating point coordinates
        node = classes.GSNode(Point(499.99, 512.01), classes.GSNode.OFFCURVE)
        self.assertWritesValue(node, '"499.99 512.01 OFFCURVE"')

        # Write userData with special characters
        test_user_data = {
            "\nkey\"';\n\n\n": "\"'value\nseveral lines\n;\n",
            ";": ";\n",
            "escapeception": "\\\"\\'\\n\\\\n",
        }
        node = classes.GSNode(Point(130, 431), classes.GSNode.LINE)
        for key, value in test_user_data.items():
            node.userData[key] = value
        # This is the output of Glyphs 1089
        expected_output = (
            '"130 431 LINE {\\"\\012key\\\\"\';\\012\\012\\012\\" '
            '= \\"\\\\"\'value\\012several lines\\012;\\012\\"'
            ';\\n\\";\\" = \\";\\012\\";\\n'
            'escapeception = \\"\\\\\\\\"\\\\\'\\\\\\n\\\\\\\\\\n\\";}"'
        )
        self.assertWritesValue(node, expected_output)
        # Check that we can read the userData back
        node = Parser(classes.GSNode).parse(expected_output)
        self.assertEqual(test_user_data, dict(node.userData))
def test_node_user_data_into_glif_lib():
    font = classes.GSFont()
    master = classes.GSFontMaster()
    master.id = "M1"
    font.masters.append(master)
    glyph = classes.GSGlyph("a")
    layer = classes.GSLayer()
    layer.layerId = "M1"
    layer.associatedMasterId = "M1"
    glyph.layers.append(layer)
    font.glyphs.append(glyph)
    path = classes.GSPath()
    layer.paths.append(path)
    node1 = classes.GSNode()
    node1.userData["nodeUserDataKey1"] = "nodeUserDataValue1"
    node1.name = "node1"
    node2 = classes.GSNode()
    node2.userData["nodeUserDataKey2"] = "nodeUserDataValue2"
    node2.name = "node2"
    path.nodes.append(classes.GSNode())
    path.nodes.append(node1)
    path.nodes.append(classes.GSNode())
    path.nodes.append(classes.GSNode())
    path.nodes.append(node2)

    (ufo, ) = to_ufos(font, minimize_glyphs_diffs=True)

    assert ufo["a"].lib[GLYPHLIB_PREFIX + "nodeUserData.0.1"] == {
        "nodeUserDataKey1": "nodeUserDataValue1"
    }
    assert ufo["a"][0][2].name == "node1"
    assert ufo["a"].lib[GLYPHLIB_PREFIX + "nodeUserData.0.4"] == {
        "nodeUserDataKey2": "nodeUserDataValue2"
    }
    assert ufo["a"][0][0].name == "node2"

    font = to_glyphs([ufo])

    path = font.glyphs["a"].layers["M1"].paths[0]
    assert path.nodes[1].userData["nodeUserDataKey1"] == "nodeUserDataValue1"
    assert path.nodes[1].name == "node1"
    assert path.nodes[4].userData["nodeUserDataKey2"] == "nodeUserDataValue2"
    assert path.nodes[4].name == "node2"
Example #5
0
    def test_write_node(self):
        node = classes.GSNode(point(10, 30), classes.GSNode.CURVE)
        # http://docu.glyphsapp.com/#gsnode
        # position: already set
        # type: already set
        # smooth
        node.smooth = True
        # connection: deprecated
        # selected: not written
        # index, nextNode, prevNode: computed
        # name
        node.name = "top-left corner"
        # userData
        node.userData["rememberToDownloadARealRemindersApp"] = True
        self.assertWritesValue(
            node, '"10 30 CURVE SMOOTH {\\nname = \\"top-left corner\\";\\n\
rememberToDownloadARealRemindersApp = 1;\\n}"')

        # Write floating point coordinates
        node = classes.GSNode(point(499.99, 512.01), classes.GSNode.OFFCURVE)
        self.assertWritesValue(node, '"499.99 512.01 OFFCURVE"')
def test_node_user_data_into_glif_lib():
    font = classes.GSFont()
    master = classes.GSFontMaster()
    master.id = "M1"
    font.masters.append(master)
    glyph = classes.GSGlyph('a')
    layer = classes.GSLayer()
    layer.layerId = "M1"
    layer.associatedMasterId = "M1"
    glyph.layers.append(layer)
    font.glyphs.append(glyph)
    path = classes.GSPath()
    layer.paths.append(path)
    node1 = classes.GSNode()
    node1.userData['nodeUserDataKey1'] = 'nodeUserDataValue1'
    node2 = classes.GSNode()
    node2.userData['nodeUserDataKey2'] = 'nodeUserDataValue2'
    path.nodes.append(classes.GSNode())
    path.nodes.append(node1)
    path.nodes.append(classes.GSNode())
    path.nodes.append(classes.GSNode())
    path.nodes.append(node2)

    ufo, = to_ufos(font, minimize_glyphs_diffs=True)

    assert ufo['a'].lib[GLYPHLIB_PREFIX + 'nodeUserData.0.1'] == {
        'nodeUserDataKey1': 'nodeUserDataValue1'
    }
    assert ufo['a'].lib[GLYPHLIB_PREFIX + 'nodeUserData.0.4'] == {
        'nodeUserDataKey2': 'nodeUserDataValue2'
    }

    font = to_glyphs([ufo])

    path = font.glyphs['a'].layers['M1'].paths[0]
    assert path.nodes[1].userData['nodeUserDataKey1'] == 'nodeUserDataValue1'
    assert path.nodes[4].userData['nodeUserDataKey2'] == 'nodeUserDataValue2'