curved.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:neferset 作者: andburn 项目源码 文件源码
def draw(self, context):
        context.save()
        # reduce the font size, until its <= the curve length
        # TODO could use some estimate to speed this up
        size_step = 1
        size = self.size
        while True:
            path, extents, xheight = text_path(context, self.font, size, self.text)
            if extents.width > self.curve.length:
                size -= size_step
            else:
                break

        # use the height to adjust the curve so that text centered on curve vertically
        self.curve.offset(0, xheight / 2)

        width = extents.width
        # Centre text horizontally when shorter than curve length
        length = self.curve.length
        if width < length:
            r = width / float(length)
            half = r / 2
            minr = 0.5 - half
            maxr = 0.5 + half
            rng = (minr, maxr)
        else:
            rng = (0, 1)

        context.new_path()
        for ptype, pts in path:
            if ptype == cairo.PATH_MOVE_TO:
                x, y = self._fit(width, pts[0], pts[1], rng)
                context.move_to(x, y)
            elif ptype == cairo.PATH_LINE_TO:
                x, y = self._fit(width, pts[0], pts[1], rng)
                context.line_to(x,y)
            elif ptype == cairo.PATH_CURVE_TO:
                x, y = self._fit(width, pts[0], pts[1], rng)
                u, v = self._fit(width, pts[2], pts[3], rng)
                s, t = self._fit(width, pts[4], pts[5], rng)
                context.curve_to(x, y, u, v, s, t)
            elif ptype == cairo.PATH_CLOSE_PATH:
                context.close_path()

        if self.outline:
            context.set_source_rgb(*self.outline)
            context.set_line_cap(cairo.LINE_CAP_ROUND)
            context.set_line_join(cairo.LINE_JOIN_ROUND)
            context.set_line_width(6)
            context.stroke_preserve()
        context.set_source_rgb(*self.color)
        context.fill()
        context.restore()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号