Esempio n. 1
0
def _load_otglyph(g, ttfont, cmap):
    glyph = Glyph()
    glyph._name = g

    if g in cmap:
        glyph._unicodes = list(cmap[g])
    else:
        glyph._unicodes = []
    glyph._contours = []

    _load_ttcategory(glyph, ttfont, g)

    ttglyph = ttfont.getGlyphSet()[g]
    pen = RecordingPen()
    ttglyph.draw(pen)
    contours = pen.value
    lastcontour = []
    startPt = (0, 0)
    lastPt = (0, 0)
    index = 0
    for c in contours:
        if c[0] == "moveTo":
            startPt = c[1][0]
        elif c[0] == "closePath":
            if startPt != lastPt:
                lastcontour.append(_load_point(startPt, segmentType="line"))
            contour = Contour()
            contour._points = lastcontour
            contour._correct_direction()
            glyph._contours.append(contour)
            lastcontour = []
        elif c[0] == "curveTo":
            lastcontour.append(_load_point(c[1][0], segmentType="offcurve"))
            lastcontour.append(_load_point(c[1][1], segmentType="offcurve"))
            lastcontour.append(_load_point(c[1][2], segmentType="curve"))
            lastPt = c[1][2]
        elif c[0] == "lineTo":
            lastcontour.append(_load_point(c[1][0], segmentType="line"))
            lastPt = c[1][0]
        elif c[0] == "qCurveTo":
            self.raiseNotImplementedError()

    glyph._width = ttfont["hmtx"][g][0]
    glyph._leftMargin = ttfont["hmtx"][g][1]
    glyph._height = ttfont["hhea"].ascent
    if glyph.bounds:
        glyph._rightMargin = glyph._width - glyph.bounds[2]

    # CFF glyphs do not have components as such

    return glyph
Esempio n. 2
0
def _load_contour(ttglyph, index):
    endPt = ttglyph.endPtsOfContours[index]
    if index > 0:
        startPt = ttglyph.endPtsOfContours[index - 1] + 1
    else:
        startPt = 0
    points = []
    for j in range(startPt, endPt + 1):
        coords = (ttglyph.coordinates[j][0], ttglyph.coordinates[j][1])
        flags = ttglyph.flags[j] == 1
        t = "offcurve"
        if flags == 1:
            if (j == startPt and ttglyph.flags[endPt]
                    == 1) or (j != startPt and points[-1].type != "offcurve"):
                t = "line"
            else:
                t = "qcurve"
        else:
            if len(points) > 1 and points[-1].type == "offcurve":
                # Double offcurve. Insert implicit oncurve.
                prevpoint = points[-1]
                intermediate = Point()
                intermediate.x = (coords[0] + prevpoint.x) / 2
                intermediate.y = (coords[1] + prevpoint.y) / 2
                intermediate.smooth = False  # XXX
                intermediate.type = "qcurve"
                points.append(intermediate)
        p = Point()
        p.x, p.y = coords
        p.type = t
        p.smooth = False  # for testing
        points.append(p)
    c = Contour()
    c._points = points
    c._correct_direction()
    return c
Esempio n. 3
0
def _load_contour(glyph, segments):
    contour = Contour()
    contour._glyph = glyph
    contour._points = []

    for s in segments:
        if s[-1] == "x":  # sx?
            s = s[:-3]

        if s[-1] == "s":
            smooth = True
            s = s[:-2]
        else:
            smooth = False
        if s[-1] == "c":  # Close
            s = s[:-2]
        points = s.split("  ")
        for ix, position in enumerate(points):
            p = Point()
            p._contour = contour
            p.x, p.y = [float(pos) for pos in position.split(" ")]
            if len(points) == 1:
                p.type = "line"
            elif ix == 2:
                p.type = "curve"
            else:
                p.type = "offcurve"
            if ix == len(points) - 1 and smooth:
                p.smooth = True
            else:
                p.smooth = False
            contour._points.append(p)
        if len(contour._points) and contour._points[-1].type == "offcurve":
            contour._points[0].type = "curve"

    contour._correct_direction()
    return contour
Esempio n. 4
0
def _load_gspath(gspath, glyph):
    contour = Contour()
    contour._glyph = glyph
    contour._points = [_load_gspoint(p, contour) for p in gspath.nodes]
    contour._clockwise = gspath.direction == 1
    return contour
Esempio n. 5
0
def _load_dccontour(dccontour, glyph):
    contour = Contour()
    contour._glyph = glyph
    contour._points = [_load_dcpoint(p, contour) for p in dccontour]
    contour.clockwise = dccontour.clockwise
    return contour