Beispiel #1
0
def compileInterpolatableOTFsFromDS(
    designSpaceDoc,
    preProcessorClass=OTFPreProcessor,
    outlineCompilerClass=OutlineOTFCompiler,
    featureCompilerClass=None,
    featureWriters=None,
    glyphOrder=None,
    useProductionNames=None,
    roundTolerance=None,
    inplace=False,
    debugFeatureFile=None,
    notdefGlyph=None,
):
    """Create FontTools CFF fonts from the DesignSpaceDocument UFO sources
    with interpolatable outlines.

    Interpolatable means without subroutinization and specializer optimizations
    and no removal of overlaps.

    If the Designspace contains a "public.skipExportGlyphs" lib key, these
    glyphs will not be exported to the final font. If these glyphs are used as
    components in any other glyph, those components get decomposed. If the lib
    key doesn't exist in the Designspace, all glyphs are exported (keys in
    individual UFOs are ignored). UFO groups and kerning will be pruned of
    skipped glyphs.

    The DesignSpaceDocument should contain SourceDescriptor objects with 'font'
    attribute set to an already loaded defcon.Font object (or compatible UFO
    Font class). If 'font' attribute is unset or None, an AttributeError exception
    is thrown.

    Return a copy of the DesignSpaceDocument object (or the same one if
    inplace=True) with the source's 'font' attribute set to the corresponding
    TTFont instance.

    For sources that have the 'layerName' attribute defined, the corresponding TTFont
    object will contain only a minimum set of tables ("head", "hmtx", "CFF ", "maxp",
    "vmtx" and "VORG"), and no OpenType layout tables.
    """
    for source in designSpaceDoc.sources:
        if source.font is None:
            raise AttributeError(
                "designspace source '%s' is missing required 'font' attribute"
                % getattr(source, "name", "<Unknown>"))

    skipExportGlyphs = designSpaceDoc.lib.get("public.skipExportGlyphs", [])

    if notdefGlyph is None:
        notdefGlyph = _getDefaultNotdefGlyph(designSpaceDoc)

    otfs = []
    for source in designSpaceDoc.sources:
        otfs.append(
            compileOTF(
                ufo=source.font,
                layerName=source.layerName,
                preProcessorClass=preProcessorClass,
                outlineCompilerClass=outlineCompilerClass,
                featureCompilerClass=featureCompilerClass,
                featureWriters=featureWriters,
                glyphOrder=glyphOrder,
                useProductionNames=useProductionNames,
                optimizeCFF=CFFOptimization.NONE,
                roundTolerance=roundTolerance,
                removeOverlaps=False,
                overlapsBackend=None,
                inplace=inplace,
                skipExportGlyphs=skipExportGlyphs,
                debugFeatureFile=debugFeatureFile,
                notdefGlyph=notdefGlyph,
                _tables=SPARSE_OTF_MASTER_TABLES if source.layerName else None,
            ))

    if inplace:
        result = designSpaceDoc
    else:
        # TODO try a more efficient copy method that doesn't involve (de)serializing
        result = designSpaceDoc.__class__.fromstring(designSpaceDoc.tostring())

    for source, otf in zip(result.sources, otfs):
        source.font = otf

    return result
Beispiel #2
0
def compileInterpolatableTTFsFromDS(
    designSpaceDoc,
    preProcessorClass=TTFInterpolatablePreProcessor,
    outlineCompilerClass=OutlineTTFCompiler,
    featureCompilerClass=None,
    featureWriters=None,
    glyphOrder=None,
    useProductionNames=None,
    cubicConversionError=None,
    reverseDirection=True,
    flattenComponents=False,
    inplace=False,
    debugFeatureFile=None,
    notdefGlyph=None,
):
    """Create FontTools TrueType fonts from the DesignSpaceDocument UFO sources
    with interpolatable outlines. Cubic curves are converted compatibly to
    quadratic curves using the Cu2Qu conversion algorithm.

    If the Designspace contains a "public.skipExportGlyphs" lib key, these
    glyphs will not be exported to the final font. If these glyphs are used as
    components in any other glyph, those components get decomposed. If the lib
    key doesn't exist in the Designspace, all glyphs are exported (keys in
    individual UFOs are ignored). UFO groups and kerning will be pruned of
    skipped glyphs.

    The DesignSpaceDocument should contain SourceDescriptor objects with 'font'
    attribute set to an already loaded defcon.Font object (or compatible UFO
    Font class). If 'font' attribute is unset or None, an AttributeError exception
    is thrown.

    Return a copy of the DesignSpaceDocument object (or the same one if
    inplace=True) with the source's 'font' attribute set to the corresponding
    TTFont instance.

    For sources that have the 'layerName' attribute defined, the corresponding TTFont
    object will contain only a minimum set of tables ("head", "hmtx", "glyf", "loca",
    "maxp", "post" and "vmtx"), and no OpenType layout tables.
    """
    ufos, layerNames = [], []
    for source in designSpaceDoc.sources:
        if source.font is None:
            raise AttributeError(
                "designspace source '%s' is missing required 'font' attribute"
                % getattr(source, "name", "<Unknown>"))
        ufos.append(source.font)
        # 'layerName' is None for the default layer
        layerNames.append(source.layerName)

    skipExportGlyphs = designSpaceDoc.lib.get("public.skipExportGlyphs", [])

    if notdefGlyph is None:
        notdefGlyph = _getDefaultNotdefGlyph(designSpaceDoc)

    ttfs = compileInterpolatableTTFs(
        ufos,
        preProcessorClass=preProcessorClass,
        outlineCompilerClass=outlineCompilerClass,
        featureCompilerClass=featureCompilerClass,
        featureWriters=featureWriters,
        glyphOrder=glyphOrder,
        useProductionNames=useProductionNames,
        cubicConversionError=cubicConversionError,
        reverseDirection=reverseDirection,
        flattenComponents=flattenComponents,
        inplace=inplace,
        layerNames=layerNames,
        skipExportGlyphs=skipExportGlyphs,
        debugFeatureFile=debugFeatureFile,
        notdefGlyph=notdefGlyph,
    )

    if inplace:
        result = designSpaceDoc
    else:
        # TODO try a more efficient copy method that doesn't involve (de)serializing
        result = designSpaceDoc.__class__.fromstring(designSpaceDoc.tostring())
    for source, ttf in zip(result.sources, ttfs):
        source.font = ttf
    return result