Package: pyots
pyots (PYthon OT Sanitizer)Python wrapper forOpenType Sanitizer, also known as just "OTS". It is similar to and partially based onots-python, but builds OTS as a Python C Extension (instead of as an executable and calling throughsubprocessas ots-python does).NOTE:Although this package is similar toots-python, it isnota drop-in replacement for it, as the Python API is different.RequirementsThe project buildspip-installable wheels for Python 3.8, 3.9, 3.10 or 3.11 under Mac or Linux. It is possible this project will build and run with other Pythons and other operating systems, but it has only been tested with the listed configurations.Installation withpipIf you just want tousepyots, you can simply runpython -m pip install -U pyots(in one of the supported platforms/Python versions) which will install pre-built, compiled, ready-to-use Python wheels. Then you can skip down to theUsesection.Installation/setup for developingpyotsIf you'd like to tinker with thepyotscode, you will want to get your local setup ready:clone this reporunpython setup.py downloadto download the OTS source (which isnot includedin this project). You can modify theversionvalue insetup.cfgunder[download]to specify a different version of OTS. You'll also need to change thesha256hash value that corresponds to the OTS tar.xz package. Note that this scheme has some limitations: OTS sources older than 8.1.3 might not build correctly since they used different build systems. Also, versions newer than the one specified in this repo might require adjustments in order to build correctly. What can we say, we're dependent onots...to build and installpyotsafter downloading OTS, you can runpython setup.py installorpython -m pip install .while iterating changes, you will want to delete the temporarybuildandsrc/ots/buildfolders.TestingThere is a test suite defined for exercising the Python extension. It makes use (and assumes the presence of) the downloaded OTS library source's test font data insrc/otsso ensure you have runpython setup.py downloadand have theotsfolder undersrc. Invoke the tests withpython -m pytest.If you wish to run tests comparing results fromots-pythonagainstpyots, be sure topython -m pip install opentype-sanitizerfirst, otherwise that set of tests will be skipped.UseSimplest case:importpyotsresult=pyots.sanitize('/path/to/font/file.ttf')resultis anOTSResultobject with 3 attributes:sanitizedBoolean indicating whether the file was successfully sanitizedmodifiedBoolean indicating whether the file was modified* during sanitizationmessagesTuple of message strings generated during sanitization (may be empty)Note:currently the back-end OTS code can modify fonts that are successfully sanitized, even when no changes are performed. Thusmodifiedcan sometimes be True whensanitizedis True. Usually the modification is only to the modification date and related checksums. Thus, it might be possible to devise a better detection of modification, i.e. ignoringhead.modifiedand other inconsequential modifications, but that was out-of-scope for this work.Example: sanitizing a folder of font files# sanitize a folder of fonts. Print messages for any that were not successfully sanitized.importpyotsfrompathlibimportPathforfilenameinPath("src/ots/tests/fonts/good").rglob("*"):result=pyots.sanitize(filename.absolute())ifnotresult.sanitized:print('{}:\n{}'.format(filename,"\n".join([mforminresult.messages])))Options forsanitize()Specify keywordoutput=<path_to_output_file>to thesanitize()command and the sanitized file will be saved to that locationUsequiet=Trueforsanitize()to suppress messagesSpecifyfont_index=<index_in_TTC>when sanitizing a Collection (OTC/TTC) file and you want to sanitize only a particular index within the Collection (otherwise all will be sanitized per OTS's default behavior)