Example #1
0
    def glyph(self, componentFlags=0x4):
        assert self._isClosed(), "Didn't close last contour."

        components = []
        for glyphName, transformation in self.components:
            if self.points:
                # can't have both, so decompose the glyph
                tpen = TransformPen(self, transformation)
                self.glyphSet[glyphName].draw(tpen)
                continue

            component = GlyphComponent()
            component.glyphName = glyphName
            if transformation[:4] != (1, 0, 0, 1):
                component.transform = (transformation[:2], transformation[2:4])
            component.x, component.y = transformation[4:]
            component.flags = componentFlags
            components.append(component)

        glyph = Glyph()
        glyph.coordinates = GlyphCoordinates(self.points)
        glyph.endPtsOfContours = self.endPts
        glyph.flags = array("B", self.types)
        self.init()

        if components:
            glyph.components = components
            glyph.numberOfContours = -1
        else:
            glyph.numberOfContours = len(glyph.endPtsOfContours)
            glyph.program = ttProgram.Program()
            glyph.program.fromBytecode(b"")

        return glyph
Example #2
0
    def glyph(self, componentFlags: int = 0x4) -> Glyph:
        """
        Returns a :py:class:`~._g_l_y_f.Glyph` object representing the glyph.
        """
        if not self._isClosed():
            raise PenError("Didn't close last contour.")
        components = self._buildComponents(componentFlags)

        glyph = Glyph()
        glyph.coordinates = GlyphCoordinates(self.points)
        glyph.coordinates.toInt()
        glyph.endPtsOfContours = self.endPts
        glyph.flags = array("B", self.types)
        self.init()

        if components:
            # If both components and contours were present, they have by now
            # been decomposed by _buildComponents.
            glyph.components = components
            glyph.numberOfContours = -1
        else:
            glyph.numberOfContours = len(glyph.endPtsOfContours)
            glyph.program = ttProgram.Program()
            glyph.program.fromBytecode(b"")

        return glyph
Example #3
0
    def glyph(self, componentFlags=0x4):
        assert self._isClosed(), "Didn't close last contour."

        if self.handleOverflowingTransforms:
            # we can't encode transform values > 2 or < -2 in F2Dot14,
            # so we must decompose the glyph if any transform exceeds these
            overflowing = any(s > 2 or s < -2
                              for (glyphName,
                                   transformation) in self.components
                              for s in transformation[:4])

        components = []
        for glyphName, transformation in self.components:
            if (self.points
                    or (self.handleOverflowingTransforms and overflowing)):
                # can't have both coordinates and components, so decompose
                try:
                    baseGlyph = self.glyphSet[glyphName]
                except KeyError:
                    self.log.debug(
                        "can't decompose non-existing component '%s'; skipped",
                        glyphName)
                    continue
                else:
                    tpen = TransformPen(self, transformation)
                    baseGlyph.draw(tpen)
                    continue

            component = GlyphComponent()
            component.glyphName = glyphName
            component.x, component.y = transformation[4:]
            transformation = transformation[:4]
            if transformation != (1, 0, 0, 1):
                if (self.handleOverflowingTransforms
                        and any(MAX_F2DOT14 < s <= 2 for s in transformation)):
                    # clamp values ~= +2.0 so we can keep the component
                    transformation = tuple(
                        MAX_F2DOT14 if MAX_F2DOT14 < s <= 2 else s
                        for s in transformation)
                component.transform = (transformation[:2], transformation[2:])
            component.flags = componentFlags
            components.append(component)

        glyph = Glyph()
        glyph.coordinates = GlyphCoordinates(self.points)
        glyph.endPtsOfContours = self.endPts
        glyph.flags = array("B", self.types)
        self.init()

        if components:
            glyph.components = components
            glyph.numberOfContours = -1
        else:
            glyph.numberOfContours = len(glyph.endPtsOfContours)
            glyph.program = ttProgram.Program()
            glyph.program.fromBytecode(b"")

        return glyph
Example #4
0
def makeTTFGlyph(polygons):
    result = Glyph()
    result.numberOfContours = len(polygons)
    result.coordinates = GlyphCoordinates([coordinate for polygon in polygons for coordinate in polygon])
    result.flags = array.array("B", [1] * len(result.coordinates))
    result.endPtsOfContours = [sum(len(polygon) for polygon in polygons[:idx + 1]) - 1 for idx in range(len(polygons))]
    result.program = ttProgram.Program()
    result.program.assembly = []
    return result
Example #5
0
    def glyph(self):
        glyph = Glyph()

        glyph.coordinates = GlyphCoordinates(self.points)
        glyph.endPtsOfContours = self.endPts
        glyph.flags = array("B", self.types)
        glyph.components = self.components

        if glyph.components:
            assert not glyph.endPtsOfContours, "TrueType glyph can't have both contours and components."
            glyph.numberOfContours = -1
        else:
            glyph.numberOfContours = len(glyph.endPtsOfContours)

        glyph.program = ttProgram.Program()
        glyph.program.fromBytecode("")

        return glyph
Example #6
0
    def glyph(self):
        glyph = Glyph()

        glyph.coordinates = GlyphCoordinates(self.points)
        glyph.endPtsOfContours = self.endPts
        glyph.flags = array("B", self.types)
        glyph.components = self.components

        if glyph.components:
            assert not glyph.endPtsOfContours, (
                "TrueType glyph can't have both contours and components.")
            glyph.numberOfContours = -1
        else:
            glyph.numberOfContours = len(glyph.endPtsOfContours)

        glyph.program = ttProgram.Program()
        glyph.program.fromBytecode("")

        return glyph
Example #7
0
    def glyph(self, componentFlags=0x4):
        assert self._isClosed(), "Didn't close last contour."

        components = self._buildComponents(componentFlags)

        glyph = Glyph()
        glyph.coordinates = GlyphCoordinates(self.points)
        glyph.endPtsOfContours = self.endPts
        glyph.flags = array("B", self.types)
        self.init()

        if components:
            glyph.components = components
            glyph.numberOfContours = -1
        else:
            glyph.numberOfContours = len(glyph.endPtsOfContours)
            glyph.program = ttProgram.Program()
            glyph.program.fromBytecode(b"")

        return glyph
Example #8
0
 def get_tt_glyph(self):
     """Return a special TT Glyph record for the sbix format. It contains
     two dummy contours with one point (bottom left and top right) each."""
     # make dummy contours
     glyph = TTGlyph()
     glyph.program = NoProgram()
     glyph.numberOfContours = 0
     box = self.get_box()
     if box is not None:
         contours = [
             [(box[0], box[1], 1)],
             [(box[2], box[3], 1)],
         ]
         for contour in contours:
             coordinates = []
             flags = []
             for x, y, flag in contour:
                 if not hasattr(glyph, "xMin"):
                     glyph.xMin = x
                     glyph.yMin = y
                     glyph.xMax = x
                     glyph.yMax = y
                 else:
                     glyph.xMin = min(glyph.xMin, x)
                     glyph.yMin = min(glyph.yMin, y)
                     glyph.xMax = max(glyph.xMax, x)
                     glyph.yMax = max(glyph.yMax, y)
                 coordinates.append([x, y])
                 flags.append(flag)
             coordinates = GlyphCoordinates(coordinates)
             flags = array.array("B", flags)
             if not hasattr(glyph, "coordinates"):
                 glyph.coordinates = coordinates
                 glyph.flags = flags
                 glyph.endPtsOfContours = [len(coordinates)-1]
             else:
                 glyph.coordinates.extend(coordinates)
                 glyph.flags.extend(flags)
                 glyph.endPtsOfContours.append(len(glyph.coordinates)-1)
             glyph.numberOfContours += 1
     return glyph
Example #9
0
 def get_tt_glyph(self):
     """Return a special TT Glyph record for the sbix format. It contains
     two dummy contours with one point (bottom left and top right) each."""
     # make dummy contours
     glyph = TTGlyph()
     glyph.program = NoProgram()
     glyph.numberOfContours = 0
     box = self.get_box()
     if box is not None:
         contours = [
             [(box[0], box[1], 1)],
             [(box[2], box[3], 1)],
         ]
         for contour in contours:
             coordinates = []
             flags = []
             for x, y, flag in contour:
                 if not hasattr(glyph, "xMin"):
                     glyph.xMin = x
                     glyph.yMin = y
                     glyph.xMax = x
                     glyph.yMax = y
                 else:
                     glyph.xMin = min(glyph.xMin, x)
                     glyph.yMin = min(glyph.yMin, y)
                     glyph.xMax = max(glyph.xMax, x)
                     glyph.yMax = max(glyph.yMax, y)
                 coordinates.append([x, y])
                 flags.append(flag)
             coordinates = GlyphCoordinates(coordinates)
             flags = array.array("B", flags)
             if not hasattr(glyph, "coordinates"):
                 glyph.coordinates = coordinates
                 glyph.flags = flags
                 glyph.endPtsOfContours = [len(coordinates) - 1]
             else:
                 glyph.coordinates.extend(coordinates)
                 glyph.flags.extend(flags)
                 glyph.endPtsOfContours.append(len(glyph.coordinates) - 1)
             glyph.numberOfContours += 1
     return glyph
Example #10
0
    def glyph(self, componentFlags=0x4):
        """Returns a :py:class:`~._g_l_y_f.Glyph` object representing the glyph."""
        assert self._isClosed(), "Didn't close last contour."

        components = self._buildComponents(componentFlags)

        glyph = Glyph()
        glyph.coordinates = GlyphCoordinates(self.points)
        glyph.coordinates.toInt()
        glyph.endPtsOfContours = self.endPts
        glyph.flags = array("B", self.types)
        self.init()

        if components:
            glyph.components = components
            glyph.numberOfContours = -1
        else:
            glyph.numberOfContours = len(glyph.endPtsOfContours)
            glyph.program = ttProgram.Program()
            glyph.program.fromBytecode(b"")

        return glyph
Example #11
0
    def _newGlyph(self, name, **kwargs):
        import array
        layer = self.naked()
        self._trashPost(layer)

        layer["hmtx"][name] = (0, 0)
        layer.glyphOrder.append(name)
        # newId = layer["maxp"].numGlyphs
        # layer["maxp"].numGlyphs = newId + 1
        if "hdmx" in layer:
            del (layer["hdmx"])  # Obviously this is wrong. XXX
        layer["glyf"][name] = Glyph()  # XXX Only TTF
        layer["glyf"][name].numberOfContours = -1  # Only components right now
        layer["glyf"][name].flags = array.array("B", [])
        layer["glyf"][name].coordinates = GlyphCoordinates([])
        layer["glyf"][name].endPtsOfContours = []
        layer["glyf"][name].program = ttProgram.Program()
        layer["glyf"][name].program.fromBytecode([])
        layer["glyf"][name].xMin = 0
        layer["glyf"][name].yMin = 0
        layer["glyf"][name].xMax = 0
        layer["glyf"][name].yMax = 0
        return self[name]