def test_glyphsWithOutlines(self): font = Font(getTestFontPath()) self.assertEqual(sorted(font.glyphsWithOutlines), ["A", "B"]) font = Font(getTestFontPath()) for glyph in font: pass self.assertEqual(sorted(font.glyphsWithOutlines), ["A", "B"])
def test_path_set(self): path1 = getTestFontPath() font = Font(path1) path2 = getTestFontPath("setPathTest.ufo") shutil.copytree(path1, path2) font.path = path2 self.assertEqual(font.path, path2) shutil.rmtree(path2)
def test_glyphsWithOutlines(self): font = Font(getTestFontPath()) layer = font.layers["public.default"] self.assertEqual(sorted(layer.glyphsWithOutlines), ["A", "B"]) font = Font(getTestFontPath()) layer = font.layers["public.default"] for glyph in layer: pass self.assertEqual(sorted(layer.glyphsWithOutlines), ["A", "B"])
def test_name_set(self): font = Font(getTestFontPath()) glyph = font["A"] glyph.name = "RenamedGlyph" self.assertEqual(glyph.name, "RenamedGlyph") self.assertEqual(sorted(font.keys()), ["B", "C", "RenamedGlyph"]) font = Font(getTestFontPath()) glyph = font["A"] glyph.name = "A" self.assertFalse(glyph.dirty)
def test_glyph_unicodes_changed(self): font = Font(getTestFontPath()) glyph = font["A"] glyph.unicodes = [123, 456] self.assertEqual(font.unicodeData[123], ["A"]) self.assertEqual(font.unicodeData[456], ["A"]) self.assertEqual(font.unicodeData[66], ["B"]) font = Font(getTestFontPath()) glyph = font.newGlyph("test") glyph.unicodes = [65] self.assertEqual(font.unicodeData[65], ["test", "A"])
def test_insertGlyph(self): font = Font(getTestFontPath()) glyph = Glyph() glyph.name = "NewGlyphTest" self.assertEqual(sorted(font.keys()), ["A", "B", "C"]) font.insertGlyph(glyph) self.assertEqual(sorted(font.keys()), ["A", "B", "C", "NewGlyphTest"])
def test_getitem(self): font = Font(getTestFontPath()) layer = font.layers["public.default"] self.assertEqual(layer["A"].name, "A") self.assertEqual(layer["B"].name, "B") with self.assertRaises(KeyError): layer["NotInFont"]
def test_updateGlyphOrder_remove(self): font = Font(getTestFontPath()) self.assertEqual(font.glyphOrder, []) font.glyphOrder = ["test"] self.assertEqual(font.glyphOrder, ["test"]) font.updateGlyphOrder(removedGlyph="test") self.assertEqual(font.glyphOrder, [])
def test_removeComponent(self): font = Font(getTestFontPath()) glyph = font["C"] component = glyph.components[0] glyph.removeComponent(component) self.assertFalse(component in glyph.components) self.assertIsNone(component.getParent())
def test_layerOrder(self): font = Font(getTestFontPath()) layers = font.layers self.assertEqual(layers.layerOrder, ["public.default", "public.background", "Layer 1"]) self.assertEqual(list(reversed(layers.layerOrder)), ["Layer 1", "public.background", "public.default"])
def test_testForExternalChanges_change_layer_order(self): for ufo in (u"TestExternalEditing.ufo", u"TestExternalEditing.ufoz"): path = getTestFontPath(ufo) path = makeTestFontCopy(path) fileSystem = openTestFontAsFileSystem(path) fs.copy.copy_dir(fileSystem, "glyphs", fileSystem, "glyphs.test") with fileSystem.open(u"layercontents.plist", "rb") as f: contents = load(f) contents.append(("test", "glyphs.test")) with fileSystem.open(u"layercontents.plist", "wb") as f: dump(contents, f) closeTestFontAsFileSystem(fileSystem, path) font = Font(path) fileSystem = openTestFontAsFileSystem(path) with fileSystem.open(u"layercontents.plist", "rb") as f: contents = load(f) contents.reverse() with fileSystem.open(u"layercontents.plist", "wb") as f: dump(contents, f) closeTestFontAsFileSystem(fileSystem, path) reader = UFOReader(path) self.assertEqual(font.layers.testForExternalChanges(reader), {"deleted": [], "added": [], "modified": {}, "defaultLayer": False, "order": True}) tearDownTestFontCopy(font.path)
def test_testForExternalChanges_remove_a_layer(self): for ufo in (u"TestExternalEditing.ufo", u"TestExternalEditing.ufoz"): path = getTestFontPath(ufo) path = makeTestFontCopy(path) fileSystem = openTestFontAsFileSystem(path) fs.copy.copy_dir(fileSystem, "glyphs", fileSystem, "glyphs.test") with fileSystem.open(u"layercontents.plist", "rb") as f: contents = load(f) contents.append(("test", "glyphs.test")) with fileSystem.open(u"layercontents.plist", "wb") as f: dump(contents, f) closeTestFontAsFileSystem(fileSystem, path) font = Font(path) fileSystem = openTestFontAsFileSystem(path) fileSystem.removetree(u"glyphs.test") with fileSystem.open(u"layercontents.plist", "rb") as f: contents = load(f) del contents[-1] with fileSystem.open(u"layercontents.plist", "wb") as f: dump(contents, f) closeTestFontAsFileSystem(fileSystem, path) reader = UFOReader(path) self.assertEqual(font.layers.testForExternalChanges(reader)["deleted"], ["test"]) tearDownTestFontCopy(font.path)
def test_glyph_unicodes_changed(self): font = Font(getTestFontPath()) layer = font.layers["public.default"] glyph = layer["A"] glyph.unicodes = [123, 456] self.assertEqual(layer.unicodeData[123], ["A"]) self.assertEqual(layer.unicodeData[456], ["A"]) self.assertEqual(layer.unicodeData[66], ["B"]) self.assertIsNone(layer.unicodeData.get(65)) font = Font(getTestFontPath()) layer = font.layers["public.default"] layer.newGlyph("test") glyph = layer["test"] glyph.unicodes = [65] self.assertEqual(layer.unicodeData[65], ["test", "A"])
def test_delitem_glyph_dirty(self): for ufo in (u"TestExternalEditing.ufo", u"TestExternalEditing.ufoz"): path = getTestFontPath(ufo) path = makeTestFontCopy(path) font = Font(path) glyph = font["A"] glyph.dirty = True fileSystem = openTestFontAsFileSystem(path) glyphPath = fs.path.join("glyphs", "A_.glif") fileSystem.remove(glyphPath) contentsPath = fs.path.join("glyphs", "contents.plist") with fileSystem.open(contentsPath, "rb") as f: plist = load(f) del plist["A"] with fileSystem.open(contentsPath, "wb") as f: dump(plist, f) closeTestFontAsFileSystem(fileSystem, path) r = font.testForExternalChanges() self.assertEqual(r["deletedGlyphs"], ["A"]) del font["A"] font.save() fileSystem = openTestFontAsFileSystem(path) self.assertFalse(fileSystem.exists(glyphPath)) closeTestFontAsFileSystem(fileSystem, path) tearDownTestFontCopy(font.path)
def test_updateGlyphOrder_rename(self): font = Font(getTestFontPath()) self.assertEqual(font.glyphOrder, []) font.glyphOrder = sorted(font.keys()) self.assertEqual(font.glyphOrder, ["A", "B", "C"]) font.updateGlyphOrder(addedGlyph="new", removedGlyph="B") self.assertEqual(font.glyphOrder, ["A", "new", "C"])
def test_glyph_name_change(self): font = Font(getTestFontPath()) layer = font.layers["public.default"] glyph = layer["A"] glyph.name = "NameChangeTest" self.assertEqual(sorted(layer.keys()), ["B", "C", "NameChangeTest"]) self.assertTrue(layer.dirty)
def test_read(self): font = Font(getTestFontPath()) glyph = font.layers["Layer 1"]["A"] image = glyph.image self.assertEqual(image.fileName, 'image 1.png') self.assertEqual(image.color, '0.1,0.2,0.3,0.4') self.assertEqual(image.transformation, (0.5, 0, 0, 0.5, 0, 0))
def test_update(self): font = Font(getTestFontPath()) other = {("X", "X"): 500} font.kerning.update(other) self.assertEqual(sorted(font.kerning.keys()), [("A", "A"), ("A", "B"), ("X", "X")]) self.assertTrue(font.kerning.dirty)
def test_newGlyph(self): font = Font(getTestFontPath()) glyph = font.newGlyph("NewGlyphTest") self.assertEqual(glyph.name, "NewGlyphTest") self.assertTrue(glyph.dirty) self.assertTrue(font.dirty) self.assertEqual(sorted(font.keys()), ["A", "B", "C", "NewGlyphTest"])
def test_removeContour(self): font = Font(getTestFontPath()) glyph = font["A"] contour = glyph[0] glyph.removeContour(contour) self.assertFalse(contour in glyph._contours) self.assertIsNone(contour.getParent())
def test_removeGuideline(self): font = Font(getTestFontPath()) glyph = font.layers["Layer 1"]["A"] guideline = glyph.guidelines[0] glyph.removeGuideline(guideline) self.assertFalse(guideline in glyph.guidelines) self.assertIsNone(guideline.getParent())
def test_leftMargin_set(self): font = Font(getTestFontPath()) glyph = font["A"] glyph.leftMargin = 100 self.assertEqual(glyph.leftMargin, 100) self.assertEqual(glyph.width, 800) self.assertTrue(glyph.dirty)
def test_anchorIndex(self): font = Font(getTestFontPath()) glyph = font["A"] anchor = glyph.anchors[0] self.assertEqual(glyph.anchorIndex(anchor), 0) anchor = glyph.anchors[1] self.assertEqual(glyph.anchorIndex(anchor), 1)
def test_removeGlyphData(self): path = getTestFontPath() font = Font(path) font.newGlyph("XXX") font.unicodeData.addGlyphData("XXX", [65]) font.unicodeData.removeGlyphData("A", [65]) self.assertEqual(font.unicodeData[65], ['XXX'])
def test_contourIndex(self): font = Font(getTestFontPath()) glyph = font["A"] contour = glyph[0] self.assertEqual(glyph.contourIndex(contour), 0) contour = glyph[1] self.assertEqual(glyph.contourIndex(contour), 1)
def test_removeAnchor(self): font = Font(getTestFontPath()) glyph = font["A"] anchor = glyph.anchors[0] glyph.removeAnchor(anchor) self.assertFalse(anchor in glyph.anchors) self.assertIsNone(anchor.getParent())
def test_height_set(self): font = Font(getTestFontPath()) glyph = font["A"] glyph.height = 100 self.assertEqual(glyph.height, 100) self.assertEqual(glyph.verticalOrigin, None) self.assertTrue(glyph.dirty)
def test_guidelineIndex(self): font = Font(getTestFontPath()) guideline1 = Guideline(guidelineDict={"x": 100}) guideline2 = Guideline(guidelineDict={"y": 200}) font.guidelines = [guideline1, guideline2] self.assertEqual(font.guidelineIndex(guideline1), 0) self.assertEqual(font.guidelineIndex(guideline2), 1)
def test_removeGuideline(self): font = Font(getTestFontPath()) guideline1 = Guideline(guidelineDict={"x": 100}) guideline2 = Guideline(guidelineDict={"y": 200}) font.guidelines = [guideline1, guideline2] font.removeGuideline(guideline1) self.assertEqual(font.guidelines, [guideline2])
def test_componentIndex(self): font = Font(getTestFontPath()) glyph = font["C"] component = glyph.components[0] self.assertEqual(glyph.componentIndex(component), 0) component = glyph.components[1] self.assertEqual(glyph.componentIndex(component), 1)
def test_bounds(self): font = Font(getTestFontPath()) glyph = font["A"] self.assertEqual(glyph.bounds, (0, 0, 700, 700)) glyph = font["B"] self.assertEqual(glyph.bounds, (0, 0, 700, 700)) glyph = font["C"] self.assertEqual(glyph.bounds, (0.0, 0.0, 700.0, 700.0))
def test_len(self): font = Font(getTestFontPath()) layer = font.layers["public.default"] self.assertEqual(len(layer), 3) font = Font() layer = font.layers["public.default"] self.assertEqual(len(layer), 0)
def test_defaultLayer(self): font = Font(getTestFontPath()) layers = font.layers layer = layers.defaultLayer self.assertEqual(layer, layers["public.default"]) layer = layers["Layer 1"] layers.defaultLayer = layer self.assertEqual(layer, layers.defaultLayer)
def test_lib(self): font = Font(getTestFontPath()) layer = font.layers["Layer 1"] self.assertEqual(layer.lib, {"com.typesupply.defcon.test": "1 2 3"}) layer.lib.dirty = False layer.lib["blah"] = "abc" self.assertEqual(layer.lib["blah"], "abc") self.assertTrue(layer.lib.dirty)
def test_setitem(self): path = getTestFontPath() font = Font(path) font.newGlyph("XXX") font.unicodeData[1000] = ["XXX"] self.assertEqual(font.unicodeData[1000], ['XXX']) font.unicodeData[65] = ["YYY"] self.assertEqual(font.unicodeData[65], ['A', 'YYY'])
def test_clearGuidelines(self): font = Font(getTestFontPath()) guideline1 = Guideline(guidelineDict={"x": 100}) guideline2 = Guideline(guidelineDict={"y": 200}) font.guidelines = [guideline1, guideline2] self.assertEqual(font.guidelines, [guideline1, guideline2]) font.clearGuidelines() self.assertEqual(font.guidelines, [])
def test_controlPointBounds(self): self.font = Font(getTestFontPath()) self.glyph = self.font["C"] self.component = self.glyph.components[0] self.assertEqual(self.component.controlPointBounds, (0.0, 0.0, 350.0, 350.0)) with self.assertRaises(AttributeError): self.component.layer = (0.0, 0.0, 350.0, 350.0)
def test_removeSegment_last_segment(self): font = Font(getTestFontPath()) glyph = font["A"] contour = glyph[0] contour.removeSegment(len(contour.segments) - 1) self.assertEqual( [simpleSegment(segment) for segment in contour.segments], [[(700, 700, "line")], [(0, 700, "line")], [(700, 0, "line")]])
def test_addGlyphData(self): path = getTestFontPath() font = Font(path) font.newGlyph("XXX") font.unicodeData.addGlyphData("XXX", [1000]) self.assertEqual(font.unicodeData[1000], ['XXX']) font.unicodeData.addGlyphData("XXX", [65]) self.assertEqual(font.unicodeData[65], ['A', 'XXX'])
def test_endSelfGuidelineNotificationObservation(self): font = Font(getTestFontPath()) guideline = font.instantiateGuideline() font.beginSelfGuidelineNotificationObservation(guideline) self.assertTrue(guideline.hasObserver(font, "Guideline.Changed")) font.endSelfGuidelineNotificationObservation(guideline) self.assertIsNone(guideline.dispatcher) self.assertFalse(guideline.hasObserver(font, "Guideline.Changed"))
def test_len(self): font = Font(getTestFontPath()) layers = font.layers self.assertEqual(len(layers), 3) font = Font() layers = font.layers self.assertEqual(len(layers), 1)
def test_clockwise_set(self): font = Font(getTestFontPath()) contour = font["A"][0] contour.clockwise = False self.assertFalse(contour.clockwise) contour._clockwiseCache = None contour.clockwise = True self.assertTrue(contour.clockwise)
def test_componentReferences(self): font = Font(getTestFontPath()) layer = font.layers["public.default"] self.assertEqual(sorted(layer.componentReferences.items()), [("A", set(["C"])), ("B", set(["C"]))]) layer["C"] self.assertEqual(sorted(layer.componentReferences.items()), [("A", set(["C"])), ("B", set(["C"]))])
def test_categoryForGlyphName(self): path = getTestFontPath() font = Font(path) font.newGlyph("A.alt") self.assertEqual(font.unicodeData.categoryForGlyphName("A"), 'Lu') self.assertEqual(font.unicodeData.categoryForGlyphName("A.alt"), 'Lu') self.assertEqual(font.unicodeData.categoryForGlyphName("A.alt", False), 'Cn')
def test_baseGlyph(self): self.assertIsNone(self.component.baseGlyph) self.font = Font(getTestFontPath()) self.glyph = self.font["C"] self.component = self.glyph.components[0] self.assertEqual(self.component.baseGlyph, "A") self.component.baseGlyph = "B" self.assertEqual(self.component.baseGlyph, "B")
def test_layers(self): font = Font(getTestFontPath()) self.assertIsInstance(font.layers, LayerSet) self.assertEqual(font.layers.layerOrder, ["public.default", "public.background", "Layer 1"]) self.assertTrue(font.layers.hasObserver(font, "LayerSet.Changed")) self.assertTrue(font.layers.hasObserver(font, "LayerSet.LayerAdded")) self.assertTrue( font.layers.hasObserver(font, "LayerSet.LayerWillBeDeleted"))
def test_save_same_path_different_structure(self): for ufo in ("TestFont.ufo", "TestFont.ufoz"): path = getTestFontPath(ufo) isZip = zipfile.is_zipfile(path) font = Font(path) with self.assertRaisesRegex( DefconError, "Can't save font in-place with a different structure"): font.save(path, structure="package" if isZip else "zip")
def test_bottomMargin_get(self): font = Font(getTestFontPath()) glyph = font["A"] self.assertEqual(glyph.bottomMargin, 0) glyph = font["B"] self.assertEqual(glyph.bottomMargin, 0) # empty glyph glyph = font.newGlyph("D") self.assertIsNone(glyph.bottomMargin)
def test_topMargin_get(self): from defcon.test.testTools import getTestFontPath from defcon.objects.font import Font font = Font(getTestFontPath()) glyph = font["A"] self.assertEqual(glyph.topMargin, -200) # empty glyph glyph = font.newGlyph("D") self.assertIsNone(glyph.topMargin)
def test_open(self): font = Font(getTestFontPath("TestOpenContour.ufo")) glyph = font["A"] self.assertTrue(glyph[0].open) self.assertFalse(glyph[1].open) self.assertTrue(glyph[2].open) self.assertFalse(glyph[3].open) contour = Contour() self.assertTrue(contour.open)
def test_rightMargin_set(self): from defcon.test.testTools import getTestFontPath from defcon.objects.font import Font font = Font(getTestFontPath()) glyph = font["A"] glyph.rightMargin = 100 self.assertEqual(glyph.rightMargin, 100) self.assertEqual(glyph.width, 800) self.assertTrue(glyph.dirty)
def test_reverse(self): font = Font(getTestFontPath()) contour = font["A"][0] contour.reverse() self.assertEqual([(point.x, point.y) for point in contour._points], [(0, 0), (0, 700), (700, 700), (700, 0)]) contour.reverse() self.assertEqual([(point.x, point.y) for point in contour._points], [(0, 0), (700, 0), (700, 700), (0, 700)])
def test_pseudoUnicodeForGlyphName(self): path = getTestFontPath() font = Font(path) self.assertEqual(font.unicodeData.pseudoUnicodeForGlyphName("A"), 65) font.newGlyph("A.foo") self.assertEqual(font.unicodeData.pseudoUnicodeForGlyphName("A.foo"), 65) font.newGlyph("B_A") self.assertEqual(font.unicodeData.pseudoUnicodeForGlyphName("B_A"), 66)
def test_testExternalChanges_remove_in_memory_and_scan(self): for ufo in (u"TestExternalEditing.ufo", u"TestExternalEditing.ufoz"): path = getTestFontPath(ufo) path = makeTestFontCopy(path) with Font(path) as font, UFOReader(path) as reader: del font.images["image 1.png"] self.assertEqual(font.images.testForExternalChanges(reader), ([], [], [])) tearDownTestFontCopy(font.path)
def test_testForExternalChanges(self): for ufo in (u"TestExternalEditing.ufo", u"TestExternalEditing.ufoz"): path = getTestFontPath(ufo) path = makeTestFontCopy(path) with Font(path) as font, UFOReader(path) as reader: self.assertEqual(font.layers.testForExternalChanges(reader), {"deleted": [], "added": [], "modified": {}, "defaultLayer": False, "order": False}) tearDownTestFontCopy(font.path)
def test_contains(self): font = Font(getTestFontPath()) layer = font.layers["public.default"] self.assertTrue("A" in layer) self.assertFalse("NotInFont" in layer) font = Font() layer = font.layers["public.default"] self.assertFalse("A" in layer)
def test_newGlyph(self): font = Font(getTestFontPath()) layer = font.layers["public.default"] layer.newGlyph("NewGlyphTest") glyph = layer["NewGlyphTest"] self.assertEqual(glyph.name, "NewGlyphTest") self.assertTrue(glyph.dirty) self.assertTrue(font.dirty) self.assertEqual(sorted(layer.keys()), ["A", "B", "C", "NewGlyphTest"])
def test_testForExternalChanges(self): path = getTestFontPath("TestExternalEditing.ufo") path = makeTestFontCopy(path) font = Font(path) reader = UFOReader(path) self.assertEqual(font.layers.testForExternalChanges(reader), {"deleted": [], "added": [], "modified": {}, "defaultLayer": False, "order": False}) tearDownTestFontCopy(font.path)
def test_save_new_font_to_exsisting_directory(self): for ufo in ("TestFont.ufo", "TestFont.ufoz"): path = makeTestFontCopy(getTestFontPath(ufo)) try: self.assertTrue(os.path.exists(path)) font = Font() font.save(path) self.assertTrue(os.path.isdir(path)) finally: tearDownTestFontCopy(path)
def test_iter(self): font = Font(getTestFontPath()) self.assertEqual(sorted(glyph.name for glyph in font), ["A", "B", "C"]) names = [] for glyph1 in font: for glyph2 in font: names.append((glyph1.name, glyph2.name)) self.assertEqual(sorted(names), [("A", "A"), ("A", "B"), ("A", "C"), ("B", "A"), ("B", "B"), ("B", "C"), ("C", "A"), ("C", "B"), ("C", "C")])