def test_updatePaths(tmpdir): doc = DesignSpaceDocument() doc.path = str(tmpdir / "foo" / "bar" / "MyDesignspace.designspace") s1 = SourceDescriptor() doc.addSource(s1) doc.updatePaths() # expect no changes assert s1.path is None assert s1.filename is None name1 = "../masters/Source1.ufo" path1 = posix(str(tmpdir / "foo" / "masters" / "Source1.ufo")) s1.path = path1 s1.filename = None doc.updatePaths() assert s1.path == path1 assert s1.filename == name1 # empty filename updated name2 = "../masters/Source2.ufo" s1.filename = name2 doc.updatePaths() # conflicting filename discarded, path always gets precedence assert s1.path == path1 assert s1.filename == "../masters/Source1.ufo" s1.path = None s1.filename = name2 doc.updatePaths() # expect no changes assert s1.path is None assert s1.filename == name2
def test_pathNameResolve(tmpdir): tmpdir = str(tmpdir) # test how descriptor.path and descriptor.filename are resolved testDocPath1 = os.path.join(tmpdir, "testPathName_case1.designspace") testDocPath2 = os.path.join(tmpdir, "testPathName_case2.designspace") testDocPath3 = os.path.join(tmpdir, "testPathName_case3.designspace") testDocPath4 = os.path.join(tmpdir, "testPathName_case4.designspace") testDocPath5 = os.path.join(tmpdir, "testPathName_case5.designspace") testDocPath6 = os.path.join(tmpdir, "testPathName_case6.designspace") masterPath1 = os.path.join(tmpdir, "masters", "masterTest1.ufo") masterPath2 = os.path.join(tmpdir, "masters", "masterTest2.ufo") instancePath1 = os.path.join(tmpdir, "instances", "instanceTest1.ufo") instancePath2 = os.path.join(tmpdir, "instances", "instanceTest2.ufo") a1 = AxisDescriptor() a1.tag = "TAGA" a1.name = "axisName_a" a1.minimum = 0 a1.maximum = 1000 a1.default = 0 # Case 1: filename and path are both empty. Nothing to calculate, nothing to put in the file. doc = DesignSpaceDocument() doc.addAxis(a1) s = SourceDescriptor() s.filename = None s.path = None s.copyInfo = True s.location = dict(weight=0) s.familyName = "MasterFamilyName" s.styleName = "MasterStyleNameOne" doc.addSource(s) doc.write(testDocPath1) verify = DesignSpaceDocument() verify.read(testDocPath1) assert verify.sources[0].filename == None assert verify.sources[0].path == None # Case 2: filename is empty, path points somewhere: calculate a new filename. doc = DesignSpaceDocument() doc.addAxis(a1) s = SourceDescriptor() s.filename = None s.path = masterPath1 s.copyInfo = True s.location = dict(weight=0) s.familyName = "MasterFamilyName" s.styleName = "MasterStyleNameOne" doc.addSource(s) doc.write(testDocPath2) verify = DesignSpaceDocument() verify.read(testDocPath2) assert verify.sources[0].filename == "masters/masterTest1.ufo" assert verify.sources[0].path == posix(masterPath1) # Case 3: the filename is set, the path is None. doc = DesignSpaceDocument() doc.addAxis(a1) s = SourceDescriptor() s.filename = "../somewhere/over/the/rainbow.ufo" s.path = None s.copyInfo = True s.location = dict(weight=0) s.familyName = "MasterFamilyName" s.styleName = "MasterStyleNameOne" doc.addSource(s) doc.write(testDocPath3) verify = DesignSpaceDocument() verify.read(testDocPath3) assert verify.sources[0].filename == "../somewhere/over/the/rainbow.ufo" # make the absolute path for filename so we can see if it matches the path p = os.path.abspath(os.path.join(os.path.dirname(testDocPath3), verify.sources[0].filename)) assert verify.sources[0].path == posix(p) # Case 4: the filename points to one file, the path points to another. The path takes precedence. doc = DesignSpaceDocument() doc.addAxis(a1) s = SourceDescriptor() s.filename = "../somewhere/over/the/rainbow.ufo" s.path = masterPath1 s.copyInfo = True s.location = dict(weight=0) s.familyName = "MasterFamilyName" s.styleName = "MasterStyleNameOne" doc.addSource(s) doc.write(testDocPath4) verify = DesignSpaceDocument() verify.read(testDocPath4) assert verify.sources[0].filename == "masters/masterTest1.ufo" # Case 5: the filename is None, path has a value, update the filename doc = DesignSpaceDocument() doc.addAxis(a1) s = SourceDescriptor() s.filename = None s.path = masterPath1 s.copyInfo = True s.location = dict(weight=0) s.familyName = "MasterFamilyName" s.styleName = "MasterStyleNameOne" doc.addSource(s) doc.write(testDocPath5) # so that the document has a path doc.updateFilenameFromPath() assert doc.sources[0].filename == "masters/masterTest1.ufo" # Case 6: the filename has a value, path has a value, update the filenames with force doc = DesignSpaceDocument() doc.addAxis(a1) s = SourceDescriptor() s.filename = "../somewhere/over/the/rainbow.ufo" s.path = masterPath1 s.copyInfo = True s.location = dict(weight=0) s.familyName = "MasterFamilyName" s.styleName = "MasterStyleNameOne" doc.write(testDocPath5) # so that the document has a path doc.addSource(s) assert doc.sources[0].filename == "../somewhere/over/the/rainbow.ufo" doc.updateFilenameFromPath(force=True) assert doc.sources[0].filename == "masters/masterTest1.ufo"
def test_read_v5_document_simple(datadir): doc = DesignSpaceDocument.fromfile(datadir / "test_v5.designspace") assert_descriptors_equal( doc.axes, [ AxisDescriptor( tag="wght", name="Weight", minimum=200, maximum=1000, default=200, labelNames={ "en": "Wéíght", "fa-IR": "قطر" }, map=[ (200, 0), (300, 100), (400, 368), (600, 600), (700, 824), (900, 1000), ], axisOrdering=None, axisLabels=[ AxisLabelDescriptor( name="Extra Light", userMinimum=200, userValue=200, userMaximum=250, labelNames={ "de": "Extraleicht", "fr": "Extra léger" }, ), AxisLabelDescriptor(name="Light", userMinimum=250, userValue=300, userMaximum=350), AxisLabelDescriptor( name="Regular", userMinimum=350, userValue=400, userMaximum=450, elidable=True, ), AxisLabelDescriptor( name="Semi Bold", userMinimum=450, userValue=600, userMaximum=650, ), AxisLabelDescriptor(name="Bold", userMinimum=650, userValue=700, userMaximum=850), AxisLabelDescriptor(name="Black", userMinimum=850, userValue=900, userMaximum=900), ], ), AxisDescriptor( tag="wdth", name="Width", minimum=50, maximum=150, default=100, hidden=True, labelNames={"fr": "Chasse"}, map=[(50, 10), (100, 20), (125, 66), (150, 990)], axisOrdering=1, axisLabels=[ AxisLabelDescriptor(name="Condensed", userValue=50), AxisLabelDescriptor(name="Normal", elidable=True, olderSibling=True, userValue=100), AxisLabelDescriptor(name="Wide", userValue=125), AxisLabelDescriptor( name="Extra Wide", userValue=150, userMinimum=150), ], ), DiscreteAxisDescriptor( tag="ital", name="Italic", values=[0, 1], default=0, axisOrdering=None, axisLabels=[ AxisLabelDescriptor(name="Roman", userValue=0, elidable=True, linkedUserValue=1), AxisLabelDescriptor(name="Italic", userValue=1), ], ), ], ) assert_descriptors_equal( doc.locationLabels, [ LocationLabelDescriptor( name="Some Style", labelNames={"fr": "Un Style"}, userLocation={ "Weight": 300, "Width": 50, "Italic": 0 }, ), LocationLabelDescriptor(name="Other", userLocation={ "Weight": 700, "Width": 100, "Italic": 1 }), ], ) assert_descriptors_equal( doc.sources, [ SourceDescriptor( filename="masters/masterTest1.ufo", path=posix(str( (datadir / "masters/masterTest1.ufo").resolve())), name="master.ufo1", layerName=None, location={ "Italic": 0.0, "Weight": 0.0, "Width": 20.0 }, copyLib=True, copyInfo=True, copyGroups=False, copyFeatures=True, muteKerning=False, muteInfo=False, mutedGlyphNames=["A", "Z"], familyName="MasterFamilyName", styleName="MasterStyleNameOne", localisedFamilyName={ "fr": "Montserrat", "ja": "モンセラート" }, ), SourceDescriptor( filename="masters/masterTest2.ufo", path=posix(str( (datadir / "masters/masterTest2.ufo").resolve())), name="master.ufo2", layerName=None, location={ "Italic": 0.0, "Weight": 1000.0, "Width": 20.0 }, copyLib=False, copyInfo=False, copyGroups=False, copyFeatures=False, muteKerning=True, muteInfo=False, mutedGlyphNames=[], familyName="MasterFamilyName", styleName="MasterStyleNameTwo", localisedFamilyName={}, ), SourceDescriptor( filename="masters/masterTest2.ufo", path=posix(str( (datadir / "masters/masterTest2.ufo").resolve())), name="master.ufo2", layerName="supports", location={ "Italic": 0.0, "Weight": 1000.0, "Width": 20.0 }, copyLib=False, copyInfo=False, copyGroups=False, copyFeatures=False, muteKerning=False, muteInfo=False, mutedGlyphNames=[], familyName="MasterFamilyName", styleName="Supports", localisedFamilyName={}, ), SourceDescriptor( filename="masters/masterTest2.ufo", path=posix(str( (datadir / "masters/masterTest2.ufo").resolve())), name="master.ufo3", layerName=None, location={ "Italic": 1.0, "Weight": 0.0, "Width": 100.0 }, copyLib=False, copyGroups=False, copyFeatures=False, muteKerning=False, muteInfo=False, mutedGlyphNames=[], familyName="MasterFamilyName", styleName="FauxItalic", localisedFamilyName={}, ), ], ) assert_descriptors_equal( doc.variableFonts, [ VariableFontDescriptor( name="Test_WghtWdth", filename="Test_WghtWdth_different_from_name.ttf", axisSubsets=[ RangeAxisSubsetDescriptor(name="Weight"), RangeAxisSubsetDescriptor(name="Width"), ], lib={"com.vtt.source": "sources/vtt/Test_WghtWdth.vtt"}, ), VariableFontDescriptor( name="Test_Wght", axisSubsets=[RangeAxisSubsetDescriptor(name="Weight")], lib={"com.vtt.source": "sources/vtt/Test_Wght.vtt"}, ), VariableFontDescriptor( name="TestCd_Wght", axisSubsets=[ RangeAxisSubsetDescriptor(name="Weight"), ValueAxisSubsetDescriptor(name="Width", userValue=0), ], ), VariableFontDescriptor( name="TestWd_Wght", axisSubsets=[ RangeAxisSubsetDescriptor(name="Weight"), ValueAxisSubsetDescriptor(name="Width", userValue=1000), ], ), VariableFontDescriptor( name="TestItalic_Wght", axisSubsets=[ RangeAxisSubsetDescriptor(name="Weight"), ValueAxisSubsetDescriptor(name="Italic", userValue=1), ], ), VariableFontDescriptor( name="TestRB_Wght", axisSubsets=[ RangeAxisSubsetDescriptor(name="Weight", userMinimum=400, userDefault=400, userMaximum=700), ValueAxisSubsetDescriptor(name="Italic", userValue=0), ], ), ], ) assert_descriptors_equal( doc.instances, [ InstanceDescriptor( filename="instances/instanceTest1.ufo", path=posix( str((datadir / "instances/instanceTest1.ufo").resolve())), name="instance.ufo1", designLocation={ "Weight": 500.0, "Width": 20.0 }, familyName="InstanceFamilyName", styleName="InstanceStyleName", postScriptFontName="InstancePostscriptName", styleMapFamilyName="InstanceStyleMapFamilyName", styleMapStyleName="InstanceStyleMapStyleName", localisedFamilyName={ "fr": "Montserrat", "ja": "モンセラート" }, localisedStyleName={ "fr": "Demigras", "ja": "半ば" }, localisedStyleMapFamilyName={ "de": "Montserrat Halbfett", "ja": "モンセラート SemiBold", }, localisedStyleMapStyleName={"de": "Standard"}, glyphs={"arrow": { "mute": True, "unicodes": [291, 292, 293] }}, lib={ "com.coolDesignspaceApp.binaryData": b"<binary gunk>", "com.coolDesignspaceApp.specimenText": "Hamburgerwhatever", }, ), InstanceDescriptor( filename="instances/instanceTest2.ufo", path=posix( str((datadir / "instances/instanceTest2.ufo").resolve())), name="instance.ufo2", designLocation={ "Weight": 500.0, "Width": (400.0, 300.0) }, familyName="InstanceFamilyName", styleName="InstanceStyleName", postScriptFontName="InstancePostscriptName", styleMapFamilyName="InstanceStyleMapFamilyName", styleMapStyleName="InstanceStyleMapStyleName", glyphs={ "arrow": { "unicodes": [101, 201, 301], "note": "A note about this glyph", "instanceLocation": { "Weight": 120.0, "Width": 100.0 }, "masters": [ { "font": "master.ufo1", "location": { "Weight": 20.0, "Width": 20.0 }, "glyphName": "BB", }, { "font": "master.ufo2", "location": { "Weight": 900.0, "Width": 900.0 }, "glyphName": "CC", }, ], }, "arrow2": {}, }, ), InstanceDescriptor(locationLabel="Some Style", ), InstanceDescriptor(designLocation={ "Weight": 600.0, "Width": (401.0, 420.0) }, ), InstanceDescriptor( designLocation={ "Weight": 10.0, "Italic": 0.0 }, userLocation={"Width": 100.0}, ), InstanceDescriptor(userLocation={ "Weight": 300.0, "Width": 130.0, "Italic": 1.0 }, ), ], )