コード例 #1
0
    def drawShapeWithRectInGlyph(self, shape, rect, glyph):
        # draw the shape into the glyph
        # tell the glyph something is going to happen (undo is going to be prepared)
        glyph.prepareUndo("Drawing Shapes")

        # get the pen to draw with
        pen = glyph.getPointPen()
        if glyph.preferredSegmentType == "qcurve" and not self.shouldReverse:
            pen = ReverseContourPointPen(pen)
        elif self.shouldReverse:
            pen = ReverseContourPointPen(pen)

        x, y, w, h = rect

        # draw with the pen a rect in the glyph
        if shape == "rect":
            pen.beginPath()
            pen.addPoint(_roundPoint(x, y), "line")
            pen.addPoint(_roundPoint(x + w, y), "line")
            pen.addPoint(_roundPoint(x + w, y + h), "line")
            pen.addPoint(_roundPoint(x, y + h), "line")

            pen.endPath()

        # draw with the pen an oval in the glyph
        elif shape == "oval":

            hw = w / 2.
            hh = h / 2.

            r = .55
            segmentType = glyph.preferredSegmentType
            if glyph.preferredSegmentType == "qcurve":
                r = .42

            pen.beginPath()
            pen.addPoint(_roundPoint(x + hw, y), segmentType, True)
            pen.addPoint(_roundPoint(x + hw + hw * r, y))
            pen.addPoint(_roundPoint(x + w, y + hh - hh * r))

            pen.addPoint(_roundPoint(x + w, y + hh), segmentType, True)
            pen.addPoint(_roundPoint(x + w, y + hh + hh * r))
            pen.addPoint(_roundPoint(x + hw + hw * r, y + h))

            pen.addPoint(_roundPoint(x + hw, y + h), segmentType, True)
            pen.addPoint(_roundPoint(x + hw - hw * r, y + h))
            pen.addPoint(_roundPoint(x, y + hh + hh * r))

            pen.addPoint(_roundPoint(x, y + hh), segmentType, True)
            pen.addPoint(_roundPoint(x, y + hh - hh * r))
            pen.addPoint(_roundPoint(x + hw - hw * r, y))

            pen.endPath()

        # tell the glyph you are done with your actions so it can handle the undo properly
        glyph.performUndo()
        glyph.changed()
コード例 #2
0
def convert(glyph, maxDistance, minLength, useArcLength):
    originalNumPoints = 0
    nbPoints = 0
    conts = []
    for contour in glyph:
        conts.append([])
        cmds = conts[-1]
        p0 = getFirstOnPoint(contour)
        nseg = len(contour)
        for s in range(nseg):
            seg = contour[s]
            if seg.type == 'line':
                originalNumPoints += 1
                p1 = seg.points[0]
                cmds.append((lineto, p1))
                nbPoints += 1
                p0 = p1
            elif seg.type == 'qcurve':
                #print("Should not have quadratic segment in here. Skipping.",)
                p0 = seg.points[-1]
            elif seg.type == 'curve':
                originalNumPoints += 3
                p1, p2, p3 = seg.points
                pt0 = Point(p0.x, p0.y)
                pt1 = Point(p1.x, p1.y)
                pt2 = Point(p2.x, p2.y)
                pt3 = Point(p3.x, p3.y)
                qsegs = []
                inputCubic = (pt0, pt1, pt2, pt3)
                for cubic in splitCubicOnInflection(inputCubic, minLength):
                    qsegs = qsegs + adaptiveSmoothCubicSplit(
                        cubic, maxDistance, minLength, useArcLength)
                nbQSegMinusOne = len(qsegs) - 1
                smooth = True
                for i, qseg in enumerate(qsegs):
                    # We have to split the quad segment because Robofont does not (seem to) support
                    # ON-OFF-ON quadratic bezier curves. If ever Robofont can handle this,
                    # then it would suffice to write something like:
                    #	(a0, a1, a2) = qseg
                    #	cmds.append((curveto, (a1, a2)))

                    if i == nbQSegMinusOne: smooth = seg.smooth
                    q1, q2 = qseg
                    cmds.append((curveto, (q1[1], q2[1], q2[2], smooth)))
                    #ql, qr = splitQuadratic(0.5, qseg)
                    #cmds.append((curveto, (ql[1], qr[1], qr[2], smooth)))
                    nbPoints += 3
                p0 = p3
            else:
                #print("Unknown segment type: "+seg.type+". Skipping.",)
                p0 = seg.points[-1]
    glyph.clearContours()
    glyph.preferredSegmentStyle = 'qcurve'
    pen = ReverseContourPointPen(glyph.getPointPen())
    for cmds in conts:
        if cmds == []: continue
        pen.beginPath()
        for action, args in cmds:
            action(pen, args)
        pen.endPath()
    # Now, we make sure that each contour starts with a ON control point
    for contour in glyph:
        contour.setStartSegment(0)
    glyph.changed()
    return originalNumPoints, nbPoints
コード例 #3
0
def convert(glyph, maxDistance, minLength, useArcLength):
	originalNumPoints = 0
	nbPoints = 0
	conts = []
	for contour in glyph:
		conts.append([])
		cmds = conts[-1]
		p0 = getFirstOnPoint(contour)
		nseg = len(contour)
		for s in range(nseg):
			seg = contour[s]
			if seg.type == 'line':
				originalNumPoints += 1
				p1 = seg.points[0]
				cmds.append((lineto, p1))
				nbPoints += 1
				p0 = p1
			elif seg.type == 'qcurve':
				#print("Should not have quadratic segment in here. Skipping.",)
				p0 = seg.points[-1]
			elif seg.type == 'curve':
				originalNumPoints += 3
				p1, p2, p3 = seg.points
				pt0 = Point(p0.x, p0.y)
				pt1 = Point(p1.x, p1.y)
				pt2 = Point(p2.x, p2.y)
				pt3 = Point(p3.x, p3.y)
				qsegs = []
				inputCubic = (pt0, pt1, pt2, pt3)
				for cubic in splitCubicOnInflection(inputCubic, minLength):
					qsegs = qsegs + adaptiveSmoothCubicSplit(cubic, maxDistance, minLength, useArcLength)
				nbQSegMinusOne = len(qsegs) - 1
				smooth = True
				for i, qseg in enumerate(qsegs):
					# We have to split the quad segment because Robofont does not (seem to) support
					# ON-OFF-ON quadratic bezier curves. If ever Robofont can handle this,
					# then it would suffice to write something like:
					#	(a0, a1, a2) = qseg
					#	cmds.append((curveto, (a1, a2)))

					if i == nbQSegMinusOne: smooth = seg.smooth
					q1, q2 = qseg
					cmds.append((curveto, (q1[1], q2[1], q2[2], smooth)))
					#ql, qr = splitQuadratic(0.5, qseg)
					#cmds.append((curveto, (ql[1], qr[1], qr[2], smooth)))
					nbPoints += 3
				p0 = p3
			else:
				#print("Unknown segment type: "+seg.type+". Skipping.",)
				p0 = seg.points[-1]
	glyph.clearContours()
	glyph.preferredSegmentStyle = 'qcurve'
	pen = ReverseContourPointPen(glyph.getPointPen())
	for cmds in conts:
		if cmds == []: continue
		pen.beginPath()
		for action, args in cmds:
			action(pen, args)
		pen.endPath()
	# Now, we make sure that each contour starts with a ON control point
	for contour in glyph:
		contour.setStartSegment(0)
	glyph.changed()
	return originalNumPoints, nbPoints