File: test.py

package info (click to toggle)
mutatormath 3.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,820 kB
  • sloc: python: 2,581; makefile: 10
file content (503 lines) | stat: -rw-r--r-- 20,386 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# -*- coding: utf-8 -*-
from __future__ import print_function

"""

    These are tests for writing and processing designspace.designspace documents
        - write various designspaces
        - read them
        - process them using the test fonts
        - show masters can be read from different directories
        - show instances can be generated into different directories
        - do some basic output testing.
    

"""

import os

import defcon.objects.font
import mutatorMath.objects.error

from mutatorMath.ufo.document import DesignSpaceDocumentWriter, DesignSpaceDocumentReader
from mutatorMath.objects.location import Location

def stripPrefix(d):
    # strip the "public.kern" prefixes from d
    new = []
    for pair, value in d:
        a, b = pair
        if "public.kern" in a:
            a = a[13:]
        if "public.kern" in b:
            b = b[13:]
        new.append(((a,b), value))
    return sorted(new)

#t = [(('V', 'public.kern2.@MMK_R_A'), -100), (('public.kern1.@MMK_L_A', 'V'), -100)]
#stripPrefix(t)


def test1():
    """
    >>> import time
    >>> from mutatorMath.ufo.document import initializeLogger
    >>> testRoot = os.path.join(os.path.dirname(__file__), 'data')
    >>> documentPath = os.path.join(testRoot, 'exporttest_basic.designspace')
    >>> sourcePath = os.path.join(testRoot, 'sources')
    >>> instancePath = os.path.join(testRoot, 'instances')
    >>> master1Path = os.path.join(testRoot, )
    >>> #logPath = None # 
    >>> logPath = os.path.join(testRoot, "tests.log")
    >>> if logPath is not None:
    ...     if os.path.exists(logPath):
    ...         os.remove(logPath)
    ...     initializeLogger(logPath)
    >>> ufoVersion=2
    >>> roundGeometry=True      # this will trigger some fails if run with a pre-ufo3 fontMath.

    >>> doc = DesignSpaceDocumentWriter(documentPath, verbose=True)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "light", "LightCondensed.ufo"),
    ...        name="master_1", 
    ...        location=dict(width=0), 
    ...        copyLib=True,            # change to see the copy Lib test fail
    ...        copyGroups=True,         # change to see assertions fail on groupSource and instance groups.
    ...        copyInfo=True, 
    ...        copyFeatures=True,
    ...        muteKerning=False, 
    ...        muteInfo=True,
    ...        mutedGlyphNames=['a',],
    ...        familyName="ExplicitSourceFamilyName",
    ...        styleName="ExplicitSourceStyleName",)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "light", "LightWide.ufo"),
    ...        name="master_2", 
    ...        location=dict(width=1), 
    ...        copyLib=False, 
    ...        copyGroups=False, 
    ...        copyInfo=False, 
    ...        muteKerning=True, 
    ...        muteInfo=True,
    ...        mutedGlyphNames=['b'] )
    >>> testOutputFileName = os.path.join(instancePath, "A", "testOutput_glyphs.ufo")
    >>> glyphMasters = [('M', "master_1", dict(width=0)), ('M', "master_2", dict(width=1)), ]
    >>> doc.startInstance(fileName=testOutputFileName,
    ...       familyName="TestFamily",
    ...       styleName="TestStyleName",
    ...       location=dict(width=(0.2, 0.8)))
    >>> doc.writeGlyph("M", unicodes=[0xff], location=dict(width=0.9), masters=None, note="testnote123")
    >>> doc.writeGlyph("N", location=dict(width=0.7), masters=glyphMasters)
    >>> doc.endInstance()
    >>> doc.save()
    >>> doc = DesignSpaceDocumentReader(documentPath, ufoVersion, roundGeometry=roundGeometry, verbose=True, logPath=logPath)
    >>> doc.process(makeGlyphs=True, makeKerning=False, makeInfo=False)

        # check if we found the UFOs
    >>> assert "master_1" in doc.sources
    >>> assert "master_2" in doc.sources

        # check if we are reading the muting flags
    >>> assert doc.libSource == 'master_1'
    >>> assert doc.groupsSource == 'master_1'
    >>> assert doc.libSource == 'master_1'
    >>> assert doc.muted == {'info': ['master_1', 'master_2'], 'glyphs': {'master_2': ['b'], 'master_1': ['a']}, 'kerning': ['master_2']}

        # check the locations
    >>> fontObj, loc = doc.sources['master_1']
    >>> loc.asTuple()
    (('width', 0.0),)
    >>> loc.asTuple()
    (('width', 0.0),)
    >>> fontObj, loc = doc.sources['master_2']
    >>> loc.asTuple()
    (('width', 1.0),)

        # check the instances
    >>> assert os.path.basename(testOutputFileName) in doc.results
    >>> resultUFOPath = doc.results[os.path.basename(testOutputFileName)]
    >>> instance = defcon.objects.font.Font(resultUFOPath)

        # note: the next assertion will fail if the calculations were made with the
        # pre-ufo3 fontMath.

    >>> assert instance['M'].unicodes == [0xff]

        # check the groups
    >>> ('testGroup', ['E', 'F', 'H']) in list(instance.groups.items())
    True

        # check the lib
    >>> assert "testLibItemKey" in instance.lib.keys()

        # check the feature text was copied from the source
    >>> assert "Hi_this_is_the_feature." in instance.features.text

        # basic kerning processing.
    >>> documentPath = os.path.join(testRoot, 'exporttest_kerning.designspace')
    >>> doc = DesignSpaceDocumentWriter(documentPath, verbose=True)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "light", "LightCondensed.ufo"),
    ...        name="master_1",
    ...        location=dict(weight=0),
    ...        copyLib=True,
    ...        copyGroups=True,
    ...        copyInfo=True,
    ...        muteKerning=False,
    ...        muteInfo=False)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "bold", "BoldCondensed.ufo"),
    ...        name="master_2",
    ...        location=dict(weight=1),
    ...        copyLib=False,
    ...        copyGroups=False,
    ...        copyInfo=False,
    ...        muteKerning=False,
    ...        muteInfo=False )
    >>> testOutputFileName = os.path.join(instancePath, "B", "testOutput_kerning.ufo")
    >>> doc.startInstance(fileName=testOutputFileName, familyName="TestFamily", styleName="TestStyleName", location=dict(weight=0.6))

        # give this kerning master a different location
    >>> #doc.writeKerning(location=dict(weight=1))
    >>> #doc.endInstance()
    >>> #doc.save()
    >>> #doc = DesignSpaceDocumentReader(documentPath, ufoVersion, roundGeometry=roundGeometry, verbose=True, logPath=logPath)
    >>> #doc.process(makeGlyphs=False, makeKerning=True, makeInfo=False)
    >>> #assert os.path.basename(testOutputFileName) in doc.results
    >>> #resultUFOPath = doc.results[os.path.basename(testOutputFileName)]
    >>> #instance = defcon.objects.font.Font(resultUFOPath)
    >>> #assert sorted(instance.kerning.items()) == stripPrefix([(('@MMK_L_A', 'V'), 100), (('V', '@MMK_R_A'), 100)])


        # test the effects of muting the kerning
    >>> documentPath = os.path.join(testRoot, 'exporttest_kerning_muted.designspace')
    >>> doc = DesignSpaceDocumentWriter(documentPath, verbose=True)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "light", "LightCondensed.ufo"),
    ...        name="master_1",
    ...        location=dict(weight=0),
    ...        copyLib=True,
    ...        copyGroups=True,
    ...        copyInfo=True,
    ...        muteKerning=False,
    ...        muteInfo=False)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "bold", "BoldCondensed.ufo"),
    ...        name="master_2",
    ...        location=dict(weight=1),
    ...        copyLib=False,
    ...        copyGroups=False,
    ...        copyInfo=False,
    ...        muteKerning=True,    # mute a master at a non-origin location!
    ...        muteInfo=False )
    >>> testOutputFileName = os.path.join(instancePath, "C", "testOutput_kerning_muted.ufo")
    >>> testLocation = dict(weight=0.6)        # change this location to see calculation assertions fail.
    >>> doc.startInstance(fileName=testOutputFileName, familyName="TestFamily", styleName="TestStyleName", location=testLocation)
    >>> doc.writeKerning()
    >>> doc.endInstance()
    >>> doc.save()
    >>> doc = DesignSpaceDocumentReader(documentPath, ufoVersion, roundGeometry=roundGeometry, verbose=True, logPath=logPath)
    >>> paths = doc.getSourcePaths()
    >>> len(paths)
    2
    >>> doc.process(makeGlyphs=False, makeKerning=True, makeInfo=False)
    >>> assert doc.groupsSource == 'master_1'
    >>> assert os.path.basename(testOutputFileName) in doc.results
    >>> resultUFOPath = doc.results[os.path.basename(testOutputFileName)]
    >>> instance = defcon.objects.font.Font(resultUFOPath)

        # the bold condensed kerning master has been muted, we expect the light condensed data in the instance    
    >>> assert [(('@MMK_L_A', 'V'), -100), (('V', '@MMK_R_A'), -100)] == stripPrefix(sorted(instance.kerning.items()))

        # info data
        #   calculating fields
        #   copying fields
    >>> documentPath = os.path.join(testRoot, 'exporttest_info.designspace')
    >>> doc = DesignSpaceDocumentWriter(documentPath, verbose=True)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "light", "LightCondensed.ufo"),
    ...        name="master_1",
    ...        location=dict(weight=0),
    ...        copyLib=False,
    ...        copyGroups=False,
    ...        copyInfo=True,       # flip to False and see some assertions fail
    ...        muteKerning=True,
    ...        muteInfo=False)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "bold", "BoldCondensed.ufo"),
    ...        name="master_2",
    ...        location=dict(weight=1),
    ...        copyLib=False,
    ...        copyGroups=False,
    ...        copyInfo=False,
    ...        muteKerning=True,
    ...        muteInfo=False )
    >>> testOutputFileName = os.path.join(instancePath, "D", "testOutput_info.ufo")
    >>> testLocation = dict(weight=0.5)       # change this location to see calculation assertions fail.
    >>> doc.startInstance(
    ...        fileName=testOutputFileName,
    ...        familyName="TestFamily",
    ...        styleName="TestStyleName",
    ...        location=testLocation,
    ...        postScriptFontName="TestPostScriptFontNameValue",
    ...        styleMapFamilyName="TestStyleMapFamilyNameValue",
    ...        styleMapStyleName="bold italic",
    ...        )
    >>> doc.writeInfo()
    >>> doc.endInstance()
    >>> doc.save()
    >>> doc = DesignSpaceDocumentReader(documentPath, ufoVersion, roundGeometry=roundGeometry, verbose=True, logPath=logPath)
    >>> doc.process(makeGlyphs=False, makeKerning=False, makeInfo=True)

    >>> assert os.path.basename(testOutputFileName) in doc.results
    >>> resultUFOPath = doc.results[os.path.basename(testOutputFileName)]
    >>> instance = defcon.objects.font.Font(resultUFOPath)

        # example calculated values
    >>> assert instance.info.ascender == 750
    >>> assert instance.info.capHeight == 750

        # example copied values
    >>> assert instance.info.versionMajor == 1
    >>> assert instance.info.openTypeOS2VendorID == "LTTR"
    >>> assert instance.info.copyright == "License same as MutatorMath. BSD 3-clause. [test-token: C]"

        # test the build script
    >>> documentPath = os.path.join(testRoot, 'exporttest_build.designspace')
    >>> doc = DesignSpaceDocumentWriter(documentPath, verbose=True)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "light", "LightCondensed.ufo"),
    ...        name="master_1",
    ...        location=dict(weight=0),
    ...        copyLib=True,
    ...        copyGroups=True,
    ...        copyInfo=True,
    ...        muteKerning=False,
    ...        muteInfo=False)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "bold", "BoldCondensed.ufo"),
    ...        name="master_2",
    ...        location=dict(weight=1),
    ...        copyLib=False,
    ...        copyGroups=False,
    ...        copyInfo=False,
    ...        muteKerning=False,
    ...        muteInfo=False )
    >>> testOutputFileName = os.path.join(instancePath, "E", "testOutput_build.ufo")
    >>> testLocation = dict(weight=0.25)       # change this location to see calculation assertions fail.
    >>> doc.startInstance(
    ...        fileName=testOutputFileName,
    ...        familyName="TestFamily",
    ...        styleName="TestStyleName",
    ...        location=testLocation)
    >>> doc.writeInfo()
    >>> doc.writeKerning()
    >>> doc.endInstance()
    >>> doc.save()

        # test build function -- single designspace file
    >>> from mutatorMath.ufo import build
    >>> import os
    >>> import posixpath
    >>> here = os.path.join(os.path.dirname(__file__), 'data', 'exporttest_basic.designspace')
    >>> results = build(here, outputUFOFormatVersion=2)
    >>> ufoFullPath = results[0]['testOutput_glyphs.ufo']
    >>> ufoRelPath = os.path.relpath(ufoFullPath, os.path.dirname(__file__))
    >>> posixRelPath = posixpath.join(*ufoRelPath.split(os.path.sep))
    >>> posixRelPath
    'data/instances/A/testOutput_glyphs.ufo'

    # test the axes elements
    >>> documentPath = os.path.join(testRoot, 'warpmap_test.designspace')
    >>> doc = DesignSpaceDocumentWriter(documentPath, verbose=True)
    >>> def grow(base, factor, steps):
    ...     return [(i*100, int(round(base*(1+factor)**i))) for i in range(steps)]
    >>> doc.addAxis("wght", "weight", 0, 1000, 0, grow(100,0.55,11))
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "stems", "StemThin.ufo"),
    ...        name="master_1",
    ...        location=dict(weight=0),
    ...        copyLib=True,
    ...        copyGroups=True,
    ...        copyInfo=True,
    ...        muteKerning=False,
    ...        muteInfo=False)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "stems", "StemBold.ufo"),
    ...        name="master_2",
    ...        location=dict(weight=1000),
    ...        copyLib=False,
    ...        copyGroups=False,
    ...        copyInfo=False,
    ...        muteKerning=False,
    ...        muteInfo=False )
    >>> testOutputFileName = os.path.join(instancePath, "W", "StemOutput.ufo")
    >>> testLocation = dict(weight=0)       # change this location to see calculation assertions fail.
    >>> doc.startInstance(
    ...        fileName=testOutputFileName,
    ...        familyName="TestFamily",
    ...        styleName="Warped",
    ...        location=testLocation)
    >>> doc.writeInfo()
    >>> doc.writeKerning()
    >>> glyphMasters = [('I', "master_1", dict(weight=0)), ('I', "master_2", dict(weight=1000)), ]
    >>> for i in range(0, 1000, 50):
    ...    doc.writeGlyph("I.%04d"%i, location=dict(weight=i), masters=glyphMasters)
    ...
    >>> doc.endInstance()
    >>> doc.save()

    >>> doc = DesignSpaceDocumentReader(documentPath, ufoVersion, roundGeometry=roundGeometry, verbose=True, logPath=logPath)
    >>> doc.process(makeGlyphs=True, makeKerning=False, makeInfo=False)

    >>> documentPath = os.path.join(testRoot, 'no_warpmap_test.designspace')
    >>> doc = DesignSpaceDocumentWriter(documentPath, verbose=True)
    >>> doc.addAxis("wght", "weight", 0, 1000, 0)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "stems", "StemThin.ufo"),
    ...        name="master_1",
    ...        location=dict(weight=0),
    ...        copyLib=True,
    ...        copyGroups=True,
    ...        copyInfo=True,
    ...        muteKerning=False,
    ...        muteInfo=False)
    >>> doc.addSource(
    ...        os.path.join(sourcePath, "stems", "StemBold.ufo"),
    ...        name="master_2",
    ...        location=dict(weight=1000),
    ...        copyLib=False,
    ...        copyGroups=False,
    ...        copyInfo=False,
    ...        muteKerning=False,
    ...        muteInfo=False )
    >>> testOutputFileName = os.path.join(instancePath, "W", "StemOutput_nowarp.ufo")
    >>> testLocation = dict(weight=0)       # change this location to see calculation assertions fail.
    >>> doc.startInstance(
    ...        fileName=testOutputFileName,
    ...        familyName="TestFamily",
    ...        styleName="NotWarped",
    ...        location=testLocation)
    >>> doc.writeInfo()
    >>> doc.writeKerning()
    >>> glyphMasters = [('I', "master_1", dict(weight=0)), ('I', "master_2", dict(weight=1000)), ]
    >>> for i in range(0, 1000, 50):
    ...    doc.writeGlyph("I.%04d"%i, location=dict(weight=i), masters=glyphMasters)
    ...
    >>> doc.endInstance()
    >>> doc.save()


    >>> doc = DesignSpaceDocumentReader(documentPath, ufoVersion, roundGeometry=roundGeometry, verbose=True, logPath=logPath)
    >>> doc.process(makeGlyphs=True, makeKerning=False, makeInfo=False)

    # test the axes element
    >>> from pprint import pprint
    >>> documentPath = os.path.join(testRoot, 'axes_test.designspace')
    >>> doc = DesignSpaceDocumentWriter(documentPath, verbose=True)
    >>> def grow(base, factor, steps):
    ...     return [(i*100, int(round(base*(1+factor)**i))) for i in range(steps)]

    >>> # axis with a warp map
    >>> warpMap = grow(100,0.55,11)
    >>> doc.addAxis("wght", "weight", -1000, 1000, 0, warpMap)
    >>> # axis without a warp map
    >>> doc.addAxis("wdth", "width", 0, 1000, 0)
    >>> doc.save()

    >>> doc = DesignSpaceDocumentReader(documentPath, ufoVersion, roundGeometry=roundGeometry, verbose=True, logPath=logPath)
    >>> pprint(doc.axes)
    {'weight': {'default': 0.0,
                'map': [(0.0, 100.0),
                        (100.0, 155.0),
                        (200.0, 240.0),
                        (300.0, 372.0),
                        (400.0, 577.0),
                        (500.0, 895.0),
                        (600.0, 1387.0),
                        (700.0, 2149.0),
                        (800.0, 3332.0),
                        (900.0, 5164.0),
                        (1000.0, 8004.0)],
                'maximum': 1000.0,
                'minimum': -1000.0,
                'name': 'weight',
                'tag': 'wght'},
     'width': {'default': 0.0,
               'map': [],
               'maximum': 1000.0,
               'minimum': 0.0,
               'name': 'width',
               'tag': 'wdth'}}

    >>> doc.process(makeGlyphs=False, makeKerning=False, makeInfo=False)
    """

def bender_and_mutatorTest():
    """
    >>> from mutatorMath.objects.bender import Bender
    >>> from mutatorMath.objects.location import Location
    >>> from mutatorMath.objects.mutator import buildMutator

    >>> w = {'aaaa':{
    ...     'map': [(300, 50),
    ...          (400, 100),
    ...          (700, 150)],
    ...     'name':'aaaaAxis',
    ...     'tag':'aaaa',
    ...     'minimum':0,
    ...     'maximum':1000,
    ...     'default':0}}

    >>> b = Bender(w)
    >>> assert b(dict(aaaa=300)) == {'aaaa': 50}
    >>> assert b(dict(aaaa=400)) == {'aaaa': 100}
    >>> assert b(dict(aaaa=700)) == {'aaaa': 150}

    master locations are always in internal design coordinates, thus they are
    considered to be already mapped or bent.

    >>> items = [
    ...     (Location(aaaa=50), 0),
    ...     (Location(aaaa=100), 50),
    ...     (Location(aaaa=150), 100),
    ... ]

    >>> bias, mut = buildMutator(items, w, bias=Location(aaaa=100))
    >>> bias
    <Location aaaa:100 >

    >>> bias, mut = buildMutator(items, w, bias=Location(aaaa=150))
    >>> bias
    <Location aaaa:150 >

    >>> bias, mut = buildMutator(items, w, bias=Location(aaaa=50))
    >>> bias
    <Location aaaa:50 >

    >>> expect = sorted([(('aaaa', 100),), (('aaaa', 50),), ()])
    >>> expect
    [(), (('aaaa', 50),), (('aaaa', 100),)]

    >>> got = sorted(mut.keys())
    >>> got
    [(), (('aaaa', 50),), (('aaaa', 100),)]

    >>> assert got == expect

    Instance location are not bent by default, i.e. are interpreted as internal
    design coordinates:
    >>> assert mut.makeInstance(Location(aaaa=50)) == 0
    >>> assert mut.makeInstance(Location(aaaa=100)) == 50
    >>> assert mut.makeInstance(Location(aaaa=150)) == 100

    If bend=True, instance locations are interpreted as user-space coordinates
    >>> assert mut.makeInstance(Location(aaaa=300), bend=True) == 0
    >>> assert mut.makeInstance(Location(aaaa=400), bend=True) == 50
    >>> assert mut.makeInstance(Location(aaaa=700), bend=True) == 100
    """

if __name__ == '__main__':
    import sys
    import doctest
    sys.exit(doctest.testmod().failed)