Example #1
0
def extractComposites(glyph):
    """Return a new glyph with outline copies of each composite from the source glyph."""

    decomposedComposites = RGlyph()

    if len(glyph.components):
        font = glyph.getParent()

        for comp in reversed(glyph.components):

            # obtain source data
            baseGlyphName = comp.baseGlyph
            baseGlyph = font[baseGlyphName]
            t = transform.Transform(*comp.transformation)

            # create a temporary glyph on which to draw the decomposed composite
            single_decomposedComposite = RGlyph()
            decompPen = single_decomposedComposite.getPen()
            baseGlyph.draw(decompPen)
            single_decomposedComposite.transform(t)

            # add single composite to the returned glyph
            decomposedComposites.appendGlyph(single_decomposedComposite)

    return decomposedComposites
Example #2
0
def removeOverlap(glyph):

    toRFGlyph = RGlyph()
    toRFpen = toRFGlyph.getPen()
    glyph.draw(toRFpen)

    singleContourGlyph = RGlyph()
    singleContourGlyph.width = glyph.width
    singleContourGlyph.name = glyph.name
    pointPen = singleContourGlyph.getPointPen()

    if len(toRFGlyph) > 1:

        booleanGlyphs = []

        for c in toRFGlyph.contours:
            if len(c) > 1:
                b = BooleanGlyph()
                pen = b.getPen()
                c.draw(pen)
                booleanGlyphs.append(b)

        finalBooleanGlyph = reduce(lambda g1, g2: g1 | g2, booleanGlyphs)
        finalBooleanGlyph.drawPoints(pointPen)

    else:
        glyph.drawPoints(pointPen)

    return singleContourGlyph
Example #3
0
def extractComposites(glyph):
    """Return a new glyph with outline copies of each composite from the source glyph."""

    decomposedComposites = RGlyph()

    if len(glyph.components):
        font = glyph.getParent()

        for comp in reversed(glyph.components):

            # obtain source data
            baseGlyphName = comp.baseGlyph
            baseGlyph = font[baseGlyphName]
            t = transform.Transform(*comp.transformation)

            # create a temporary glyph on which to draw the decomposed composite
            single_decomposedComposite = RGlyph()
            decompPen = single_decomposedComposite.getPen()
            baseGlyph.draw(decompPen)
            single_decomposedComposite.transform(t)

            # add single composite to the returned glyph
            decomposedComposites.appendGlyph(single_decomposedComposite)

    return decomposedComposites
Example #4
0
def copyContours(glyph):
    glyphCopy = RGlyph()
    glyphCopy.width = glyph.width
    pen = glyphCopy.getPen()
    glyph.draw(pen)
    glyphCopy.unicode = glyph.unicode
    glyphCopy.name = glyph.name
    return glyphCopy
Example #5
0
def copyContours(glyph):
    glyphCopy = RGlyph()
    glyphCopy.width = glyph.width
    pen = glyphCopy.getPen()
    glyph.draw(pen)
    glyphCopy.unicode = glyph.unicode
    glyphCopy.name = glyph.name
    return glyphCopy
    def __call__(self, glyph, font, **arguments):
        try:
            filteredGlyph = RGlyph()
            filteredGlyph.width = glyph.width
            drawingPen = filteredGlyph.getPen()
            filterPen = self.filterObject(drawingPen, **arguments)
            glyph.draw(filterPen)
        except:
            try:
                sourceGlyph = RGlyph()
                sourceGlyph.appendGlyph(glyph)
                sourceGlyph.width = glyph.width
                filteredGlyph = self.filterObject(sourceGlyph, **arguments)
            except:
                filteredGlyph = ErrorGlyph()

        return filteredGlyph
Example #7
0
def freezeGlyph(glyph):
    """Return a copy of a glyph, with components decomposed and all overlap removed."""

    toRFGlyph = RGlyph()
    toRFpen = toRFGlyph.getPen()
    glyph.draw(toRFpen)

    if len(glyph.components):
        decomposedComponents = extractComposites(glyph)
        decomposedComponents.draw(toRFpen)

    singleContourGlyph = RGlyph()
    singleContourGlyph.width = glyph.width
    singleContourGlyph.name = glyph.name
    pointPen = singleContourGlyph.getPointPen()

    if len(toRFGlyph.contours) > 1:

        try:
            booleanGlyphs = []

            for c in toRFGlyph.contours:
                if len(c) > 1:
                    b = BooleanGlyph()
                    pen = b.getPen()
                    c.draw(pen)
                    booleanGlyphs.append(b)

            finalBooleanGlyph = reduce(lambda g1, g2: g1 | g2, booleanGlyphs)
            finalBooleanGlyph.drawPoints(pointPen)

        except:
            toRFGlyph.drawPoints(pointPen)
    else:
        toRFGlyph.drawPoints(pointPen)

    return singleContourGlyph
Example #8
0
def freezeGlyph(glyph):
    """Return a copy of a glyph, with components decomposed and all overlap removed."""

    toRFGlyph = RGlyph()
    toRFpen = toRFGlyph.getPen()
    glyph.draw(toRFpen)

    if len(glyph.components):
        decomposedComponents = extractComposites(glyph)
        decomposedComponents.draw(toRFpen)

    singleContourGlyph = RGlyph()
    singleContourGlyph.width = glyph.width
    singleContourGlyph.name = glyph.name
    pointPen = singleContourGlyph.getPointPen()

    if len(toRFGlyph.contours) > 1:

        try:
            booleanGlyphs = []

            for c in toRFGlyph.contours:
                if len(c) > 1:
                    b = BooleanGlyph()
                    pen = b.getPen()
                    c.draw(pen)
                    booleanGlyphs.append(b)

            finalBooleanGlyph = reduce(lambda g1, g2: g1 | g2, booleanGlyphs)
            finalBooleanGlyph.drawPoints(pointPen)

        except:
            toRFGlyph.drawPoints(pointPen)
    else:
        toRFGlyph.drawPoints(pointPen)

    return singleContourGlyph
        def filterGroup(glyph, font=None, **overrideGlobalArguments):
            globalArguments = {self.splitSubfilterArgumentName(argumentName): self.arguments[argumentName] for argumentName in self.arguments}
            for key in overrideGlobalArguments:
                _subfilterName_, _overrideArgumentName_, _filterOrder_ = self.splitSubfilterArgumentName(key)
                if (_subfilterName_, _overrideArgumentName_) != (None, None):
                    globalArguments[(_subfilterName_, _overrideArgumentName_, _filterOrder_)] = overrideGlobalArguments[key]

            subfilters = [(self.getSubfilter(subfilterName), mode, source) for subfilterName, mode, source in self.subfilters]
            error = False
            canvasGlyph = RGlyph()
            canvasPen = canvasGlyph.getPen()
            canvasGlyph.width = glyph.width
            glyph.draw(canvasPen)

            steps = []

            for i, (currentFilter, mode, source) in enumerate(subfilters):

                if error == True:
                    continue

                if source is None:
                    sourceGlyph = canvasGlyph
                else:
                    try:
                        sourceGlyph = steps[source]
                    except:
                        layerGlyph = glyph.getLayer(source)
                        if len(layerGlyph) > 0:
                            sourceGlyph = RGlyph()
                            pen = sourceGlyph.getPen()
                            layerGlyph.draw(pen)
                        else:
                            sourceGlyph = canvasGlyph

                sourceGlyph.name = glyph.name

                arguments = {argumentName: globalArguments[(subfilterName, argumentName, filterOrder)] for subfilterName, argumentName, filterOrder in globalArguments if subfilterName == currentFilter.name and filterOrder == i}
                processedGlyph = currentFilter.filterGlyph(sourceGlyph, arguments)

                steps.append(processedGlyph)

                if mode in ['union', 'difference', 'intersection', 'xor']:
                    try:
                        b1 = BooleanGlyph(canvasGlyph)
                        b2 = BooleanGlyph(processedGlyph)
                        operation = getattr(b1, mode)
                        processedGlyph = operation(b2)
                    except:
                        error = True

                if mode != 'add':
                    canvasGlyph.clear()

                processedGlyph.draw(canvasPen)

            if error == True:
                canvasGlyph = ErrorGlyph()
            elif error == False:
                canvasGlyph.name = glyph.name

            canvasGlyph.unicode = glyph.unicode
            if canvasGlyph.width is None:
                canvasGlyph.width = glyph.width
            return canvasGlyph
        def filterGroup(glyph, font=None, **overrideGlobalArguments):
            globalArguments = {
                self.splitSubfilterArgumentName(argumentName):
                self.arguments[argumentName]
                for argumentName in self.arguments
            }
            for key in overrideGlobalArguments:
                _subfilterName_, _overrideArgumentName_, _filterOrder_ = self.splitSubfilterArgumentName(
                    key)
                if (_subfilterName_, _overrideArgumentName_) != (None, None):
                    globalArguments[(
                        _subfilterName_, _overrideArgumentName_,
                        _filterOrder_)] = overrideGlobalArguments[key]

            subfilters = [(self.getSubfilter(subfilterName), mode, source)
                          for subfilterName, mode, source in self.subfilters]
            error = False
            canvasGlyph = RGlyph()
            canvasPen = canvasGlyph.getPen()
            canvasGlyph.width = glyph.width
            glyph.draw(canvasPen)

            steps = []

            for i, (currentFilter, mode, source) in enumerate(subfilters):

                if error == True:
                    continue

                if not source:
                    sourceGlyph = canvasGlyph
                else:
                    try:
                        sourceGlyph = steps[source - 1]
                    except:
                        layerGlyph = glyph.getLayer(source)
                        if len(layerGlyph) > 0:
                            sourceGlyph = RGlyph()
                            pen = sourceGlyph.getPen()
                            layerGlyph.draw(pen)
                        else:
                            sourceGlyph = canvasGlyph

                sourceGlyph.name = glyph.name

                arguments = {
                    argumentName:
                    globalArguments[(subfilterName, argumentName, filterOrder)]
                    for subfilterName, argumentName, filterOrder in
                    globalArguments
                    if subfilterName == currentFilter.name and filterOrder == i
                }
                processedGlyph = currentFilter.filterGlyph(
                    sourceGlyph, arguments)

                if mode in ['union', 'difference', 'intersection', 'xor']:
                    try:
                        # collectedComponents = [component for component in processedGlyph.components]
                        b1 = BooleanGlyph(canvasGlyph)
                        b2 = BooleanGlyph(processedGlyph)
                        operation = getattr(b1, mode)
                        b3 = operation(b2)
                        processedGlyph = RGlyph()
                        processedPen = processedGlyph.getPen()
                        b3.draw(processedPen)
                        # for component in collectedComponents:
                        #     processedGlyph.appendComponent(component.baseGlyph, component.offset, component.scale)
                    except:
                        error = True

                steps.append(processedGlyph)

                if mode == 'ignore' and len(steps) > 1:
                    processedGlyph = steps[-2]
                elif mode == 'ignore':
                    processedGlyph = sourceGlyph

                if mode != 'add':
                    canvasGlyph.clear()

                processedGlyph.draw(canvasPen)

                if processedGlyph.width:
                    canvasGlyph.width = processedGlyph.width

            if error == True:
                canvasGlyph = ErrorGlyph()
            elif error == False:
                cleanPen = FilterPointPen(font)
                canvasGlyph.drawPoints(cleanPen)
                canvasGlyph.clearContours()
                canvasPointPen = canvasGlyph.getPointPen()
                cleanPen.extract(canvasPointPen)
                canvasGlyph.name = glyph.name

            canvasGlyph.unicode = glyph.unicode
            if canvasGlyph.width is None:
                canvasGlyph.width = glyph.width
            return canvasGlyph
Example #11
0
        def filterGroup(glyph, font=None, **overrideGlobalArguments):
            globalArguments = {
                self.splitSubfilterArgumentName(argumentName):
                self.arguments[argumentName]
                for argumentName in self.arguments
            }
            for key in overrideGlobalArguments:
                _subfilterName_, _overrideArgumentName_, _filterOrder_ = self.splitSubfilterArgumentName(
                    key)
                if (_subfilterName_, _overrideArgumentName_) != (None, None):
                    globalArguments[(
                        _subfilterName_, _overrideArgumentName_,
                        _filterOrder_)] = overrideGlobalArguments[key]

            subfilters = [(self.getSubfilter(subfilterName), mode, source)
                          for subfilterName, mode, source in self.subfilters]
            error = False
            canvasGlyph = RGlyph()
            canvasPen = canvasGlyph.getPen()
            canvasGlyph.width = glyph.width
            glyph.draw(canvasPen)

            steps = []

            for i, (currentFilter, mode, source) in enumerate(subfilters):

                if error == True:
                    continue

                if source is None:
                    sourceGlyph = canvasGlyph
                else:
                    try:
                        sourceGlyph = steps[source]
                    except:
                        layerGlyph = glyph.getLayer(source)
                        if len(layerGlyph) > 0:
                            sourceGlyph = RGlyph()
                            pen = sourceGlyph.getPen()
                            layerGlyph.draw(pen)
                        else:
                            sourceGlyph = canvasGlyph

                sourceGlyph.name = glyph.name

                arguments = {
                    argumentName:
                    globalArguments[(subfilterName, argumentName, filterOrder)]
                    for subfilterName, argumentName, filterOrder in
                    globalArguments
                    if subfilterName == currentFilter.name and filterOrder == i
                }
                processedGlyph = currentFilter.filterGlyph(
                    sourceGlyph, arguments)

                steps.append(processedGlyph)

                if mode in ['union', 'difference', 'intersection', 'xor']:
                    try:
                        b1 = BooleanGlyph(canvasGlyph)
                        b2 = BooleanGlyph(processedGlyph)
                        operation = getattr(b1, mode)
                        processedGlyph = operation(b2)
                    except:
                        error = True

                if mode != 'add':
                    canvasGlyph.clear()

                processedGlyph.draw(canvasPen)

            if error == True:
                canvasGlyph = ErrorGlyph()
            elif error == False:
                canvasGlyph.name = glyph.name

            canvasGlyph.unicode = glyph.unicode
            if canvasGlyph.width is None:
                canvasGlyph.width = glyph.width
            return canvasGlyph
        def filterGroup(glyph, font=None, **overrideGlobalArguments):
            globalArguments = {self.splitSubfilterArgumentName(argumentName): self.arguments[argumentName] for argumentName in self.arguments}
            for key in overrideGlobalArguments:
                _subfilterName_, _overrideArgumentName_, _filterOrder_ = self.splitSubfilterArgumentName(key)
                if (_subfilterName_, _overrideArgumentName_) != (None, None):
                    globalArguments[(_subfilterName_, _overrideArgumentName_, _filterOrder_)] = overrideGlobalArguments[key]

            subfilters = [(self.getSubfilter(subfilterName), mode, source) for subfilterName, mode, source in self.subfilters]
            error = False
            canvasGlyph = RGlyph()
            canvasPen = canvasGlyph.getPen()
            canvasGlyph.width = glyph.width
            glyph.draw(canvasPen)

            steps = []

            for i, (currentFilter, mode, source) in enumerate(subfilters):

                if error == True:
                    continue

                if not source:
                    sourceGlyph = canvasGlyph
                else:
                    try:
                        sourceGlyph = steps[source-1]
                    except:
                        layerGlyph = glyph.getLayer(source)
                        if len(layerGlyph) > 0:
                            sourceGlyph = RGlyph()
                            pen = sourceGlyph.getPen()
                            layerGlyph.draw(pen)
                        else:
                            sourceGlyph = canvasGlyph

                sourceGlyph.name = glyph.name

                arguments = {argumentName: globalArguments[(subfilterName, argumentName, filterOrder)] for subfilterName, argumentName, filterOrder in globalArguments if subfilterName == currentFilter.name and filterOrder == i}
                processedGlyph = currentFilter.filterGlyph(sourceGlyph, arguments)

                if mode in ['union', 'difference', 'intersection', 'xor']:
                    try:
                        # collectedComponents = [component for component in processedGlyph.components]
                        b1 = BooleanGlyph(canvasGlyph)
                        b2 = BooleanGlyph(processedGlyph)
                        operation = getattr(b1, mode)
                        b3 = operation(b2)
                        processedGlyph = RGlyph()
                        processedPen = processedGlyph.getPen()
                        b3.draw(processedPen)
                        # for component in collectedComponents:
                        #     processedGlyph.appendComponent(component.baseGlyph, component.offset, component.scale)
                    except:
                        error = True

                steps.append(processedGlyph)

                if mode == 'ignore' and len(steps) > 1:
                    processedGlyph = steps[-2]
                elif mode == 'ignore':
                    processedGlyph = sourceGlyph

                if mode != 'add':
                    canvasGlyph.clear()

                processedGlyph.draw(canvasPen)

                if processedGlyph.width:
                    canvasGlyph.width = processedGlyph.width

            if error == True:
                canvasGlyph = ErrorGlyph()
            elif error == False:
                cleanPen = FilterPointPen(font)
                canvasGlyph.drawPoints(cleanPen)
                canvasGlyph.clearContours()
                canvasPointPen = canvasGlyph.getPointPen()
                cleanPen.extract(canvasPointPen)
                canvasGlyph.name = glyph.name


            canvasGlyph.unicode = glyph.unicode
            if canvasGlyph.width is None:
                canvasGlyph.width = glyph.width
            return canvasGlyph