Ejemplo n.º 1
0
    def getPolygon(self):
        (x1, y1), (x2, y2) = self.a, self.b
        ax = ay = bx = by = 0
        width = sqrt((x2-x1)**2 + (y2-y1)**2)
        circ = Point(x1, y1).buffer(width, quadsegs=32)

        if self.weight > 0:
            innerCirc = Point(x1, y1).buffer(width - (self.weight * 2),
                                             quadsegs=32)
            circ = circ.difference(innerCirc)

        if self.semiA and self.semiB:
            (ax, ay), (bx, by) = self.semiA, self.semiB

            clipPoly = Polygon(((x1, y1),
                                (ax, ay),
                                (bx, by)))

            if self.swapClip:
                circ = circ.intersection(clipPoly)
            else:
                circ = circ.difference(clipPoly)

        # Done, if we don't want serifs
        if self.serif == 0 or not (self.semiA and self.semiB):
            return [circ]

        serifPolys = []
        serifWeight = self.weight * 0.618 + max((capHeight / 90.0) - 1.0, 0.0)
        ss = capHeight / 15.0 + (self.weight * capHeight / 200.0)

        topAngle = atan2(by - y1, bx - x1)
        bottomAngle = atan2(ay - y1, ax - x1)

        if self.serif == 1 or self.serif == 2:
            serifPolys = [Line((x1 + (width-self.weight)*cos(topAngle),
                                y1 - (width-self.weight)*sin(topAngle) + ss +
                                    self.weight / 2.0),
                               (x1 + (width-self.weight)*cos(topAngle),
                                y1 - (width-self.weight)*sin(topAngle) - ss +
                                    self.weight / 2.0),
                               serifWeight)]
        if self.serif == 2:
            serifPolys += [Line((x1 + (width-self.weight)*cos(bottomAngle),
                                 y1 - (width-self.weight)*sin(bottomAngle) +
                                     ss - self.weight / 2.0),
                                (x1 + (width-self.weight)*cos(bottomAngle),
                                 y1 - (width-self.weight)*sin(bottomAngle) -
                                     ss - self.weight / 2.0),
                                serifWeight)]

        return mergeSubPolys([circ, serifPolys])
Ejemplo n.º 2
0
    def getPolygon(self):
        ((x1, y1), (x2, y2)) = (self.a, self.b)

        angle = atan2(abs(y2 - y1), abs(x2 - x1))
        realAngle = atan2(y2 - y1, x2 - x1)
        xOff = yOff = 0.0

        endDirection = angle > pi / 4.0

        if self.swapAngle:
            endDirection = not endDirection

        if endDirection:
            if self.shift and "left" in self.shift:
                x1 -= self.delta
                x2 -= self.delta
            elif self.shift and "right" in self.shift:
                x1 += self.delta
                x2 += self.delta
        else:
            if self.shift and "up" in self.shift:
                y1 -= self.delta
                y2 -= self.delta
            elif self.shift and "down" in self.shift:
                y1 += self.delta
                y2 += self.delta

        dx = x1-x2
        dy = y1-y2
        dist = sqrt(dx*dx + dy*dy)
        dx /= dist
        dy /= dist
        rx1 = x1 + (self.delta)*dy
        ry1 = y1 - (self.delta)*dx
        rx2 = x1 - (self.delta)*dy
        ry2 = y1 + (self.delta)*dx

        rx3 = x2 + (self.delta)*dy
        ry3 = y2 - (self.delta)*dx
        rx4 = x2 - (self.delta)*dy
        ry4 = y2 + (self.delta)*dx

        linePoly = Polygon(((rx1, ry1), (rx2, ry2),
                            (rx4, ry4), (rx3, ry3)))

        xInf = yInf = 0
        if endDirection:
            xInf = 200
        else:
            yInf = 200

        if (not self.noclip) or (self.noclip == 2):
            clipPoly = None

            if self.noclip == 2:
                clipPoly = Polygon(((x1 - xInf, y1 - yInf),
                                    (x1 - xInf, y2 + 200),
                                    (x2 + 200, y2 + 200),
                                    (x2 + 200, y1 - yInf)))
            else:
                clipPoly = Polygon(((x1 - xInf, y1 - yInf),
                                    (x1 - xInf, y2 + yInf),
                                    (x2 + xInf, y2 + yInf),
                                    (x2 + xInf, y1 - yInf)))

            clipPoly = linePoly.intersection(clipPoly)

            if type(clipPoly) is not GeometryCollection:
                linePoly = clipPoly

        # Done, unless we want serifs
        if not drawSerifs:
            return linePoly

        serifPolys = []
        serifWeight = self.delta * 0.618
        ss = capHeight / 15.0
        angleShift = cos(realAngle) * (self.delta * -1.2)

        if self.serif == 1 or self.serif == 2:
            if self.shift and "down" in self.shift:
                serifPolys = [Line((x2 - serifWeight, y2 - self.delta),
                                   (x2 - serifWeight, y2 + self.delta + ss),
                                   serifWeight)]
            elif self.shift and "up" in self.shift:
                serifPolys = [Line((x2 - serifWeight, y2 + self.delta),
                                   (x2 - serifWeight, y2 - self.delta - ss),
                                   serifWeight)]
            else:
                serifPolys = [Line((x2 - serifWeight, y2 + self.delta + ss),
                                   (x2 - serifWeight, y2 - self.delta - ss),
                                   serifWeight)]
        if self.serif == 2:
            if self.shift and "down" in self.shift:
                serifPolys += [Line((x1 + serifWeight, y1 - self.delta),
                                    (x1 + serifWeight, y1 + self.delta + ss),
                                    serifWeight)]
            elif self.shift and "up" in self.shift:
                serifPolys += [Line((x1 + serifWeight, y1 + self.delta),
                                    (x1 + serifWeight, y1 - self.delta - ss),
                                    serifWeight)]
            else:
                serifPolys += [Line((x1 + serifWeight, y1 + self.delta + ss),
                                    (x1 + serifWeight, y1 - self.delta - ss),
                                    serifWeight)]
        if self.serif == 3 or self.serif == 4 or self.serif == 5:
            if self.shift and "down" in self.shift:
                serifPolys = [Line((x2 + serifWeight + ss + angleShift,
                                    y2 + serifWeight),
                                   (x2 - serifWeight - ss + angleShift,
                                    y2 + serifWeight),
                                   serifWeight)]
            elif self.shift and "up" in self.shift:
                serifPolys = [Line((x2 + serifWeight + ss + angleShift,
                                    y2 - serifWeight),
                                   (x2 - serifWeight - ss + angleShift,
                                    y2 - serifWeight),
                                   serifWeight)]
            else:
                serifPolys = [Line((x2 + serifWeight + ss + angleShift,
                                    y2 - serifWeight),
                                   (x2 - serifWeight - ss + angleShift,
                                    y2 - serifWeight),
                                   serifWeight)]
        if self.serif == 4:
            if self.shift and "down" in self.shift:
                serifPolys += [Line((x1 + serifWeight + ss - angleShift,
                                     y1 - serifWeight),
                                    (x1 - serifWeight - ss - angleShift,
                                     y1 - serifWeight),
                                    serifWeight)]
            elif self.shift and "up" in self.shift:
                serifPolys += [Line((x1 + serifWeight + ss - angleShift,
                                     y1 + serifWeight),
                                    (x1 - serifWeight - ss - angleShift,
                                     y1 + serifWeight),
                                    serifWeight)]
            else:
                serifPolys += [Line((x1 + serifWeight + ss - angleShift,
                                     y1 + serifWeight),
                                    (x1 - serifWeight - ss - angleShift,
                                     y1 + serifWeight),
                                    serifWeight)]
        if self.serif == 5 or self.serif == 6:
            if self.shift and "down" in self.shift:
                serifPolys += [Line((x1,
                                     y1 - serifWeight),
                                    (x1 - serifWeight - ss - angleShift,
                                     y1 - serifWeight),
                                    serifWeight)]
            elif self.shift and "up" in self.shift:
                serifPolys += [Line((x1,
                                     y1 + serifWeight),
                                    (x1 - serifWeight - ss - angleShift,
                                     y1 + serifWeight),
                                    serifWeight)]
            else:
                serifPolys += [Line((x1,
                                     y1 + serifWeight),
                                    (x1 - serifWeight - ss - angleShift,
                                     y1 + serifWeight),
                                    serifWeight)]
        return mergeSubPolys([linePoly, serifPolys])
Ejemplo n.º 3
0
    def layoutGlyphs(self):
        allGlyphs = []
        wordGlyphs = []

        xloc = self.x
        yloc = self.y

        metrics = Glyph(0, 0, capHeight=self.size)

        for i in range(len(self.glyphs)):
            a = self.glyphs[i]

            if i + 1 < len(self.glyphs):
                b = self.glyphs[i + 1]
            else:
                b = None

            if isinstance(a, LineBreak):
                if b and not isinstance(b, LineBreak):
                    bGlyphBounds = mergeSubPolys([b]).bounds
                    b.x = self.x
                    xloc = b.x - bGlyphBounds[0]
                else:
                    xloc = self.x

                yloc += metrics.capHeight() + a.leading

                allGlyphs += wordGlyphs
                wordGlyphs = []
                continue

            glyphBounds = mergeSubPolys([a]).bounds
            a.x = xloc
            a.y = yloc
            xShift = glyphBounds[2] - glyphBounds[0]

            if isinstance(b, Glyph):
                xShift += (kernGlyphs(a.char, b.char, a.weight(),
                                      capHeight=a.capHeight()) + a.tracking)

                if a.outlined:
                    xShift += (a.capHeight() / 15.0)

            xloc += xShift

            if isinstance(a, spaceGlyph):
                if len(wordGlyphs):
                    if xloc > self.width:
                        xloc = self.x
                        yloc += metrics.capHeight() + self.leading

                    allGlyphs += wordGlyphs
                    wordGlyphs = []
            else:
                wordGlyphs += [a]

        allGlyphs += wordGlyphs

        for g in allGlyphs:
            g.serifed = g.willBeSerifed

        return allGlyphs