我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用cairo.LINE_CAP_ROUND。
def draw(self, ctx, width, height): self.tc += 0.2 self.tc %= 2 * math.pi for i in range(-4, 5): ctx.set_source_rgb(0.2, 0.5, 1) ctx.set_line_width(6) ctx.set_line_cap(cairo.LINE_CAP_ROUND) if i % 2 == 0: ctx.move_to(width / 2 + i * 10, height / 2 + 3 - 8 * math.sin(self.tc + i)) ctx.line_to(width / 2 + i * 10, height / 2 - 3 + 8 * math.sin(self.tc + i)) else: ctx.set_source_rgb(0.2, 0.7, 1) ctx.move_to(width / 2 + i * 10, height / 2 + 3 - 8 * math.cos(self.tc - i)) ctx.line_to(width / 2 + i * 10, height / 2 - 3 + 8 * math.cos(self.tc - i)) ctx.stroke()
def cairo_line_cap(line_cap): if line_cap == 'round': return cairo.LINE_CAP_ROUND elif line_cap == 'square': return cairo.LINE_CAP_SQUARE else: return cairo.LINE_CAP_BUTT
def create_normal_surfaces(self, context, vis_width, vis_height, star_width): rgba1 = context.get_border_color(Gtk.StateFlags.NORMAL) rgba0 = context.get_color(Gtk.StateFlags.ACTIVE) lin = cairo.LinearGradient(0, 0, 0, vis_height) lin.add_color_stop_rgb(0, rgba0.red, rgba0.green, rgba0.blue) lin.add_color_stop_rgb(1, rgba1.red, rgba1.green, rgba1.blue) # paint full full_surf = cairo.ImageSurface( cairo.FORMAT_ARGB32, vis_width, vis_height) cr = cairo.Context(full_surf) cr.set_source(lin) cr.set_line_width(1) if self.rounded: cr.set_line_join(cairo.LINE_CAP_ROUND) for i in range(self.n_stars): x = 1 + i * (star_width + self.spacing) self.layout(cr, x + 1, 1, star_width - 2, vis_height - 2) cr.stroke_preserve() cr.fill() del cr # paint empty empty_surf = cairo.ImageSurface( cairo.FORMAT_ARGB32, vis_width, vis_height) cr = cairo.Context(empty_surf) cr.set_source(lin) cr.set_line_width(1) if self.rounded: cr.set_line_join(cairo.LINE_CAP_ROUND) for i in range(self.n_stars): x = 1 + i * (star_width + self.spacing) self.layout(cr, x + 1, 1, star_width - 2, vis_height - 2) cr.stroke() del cr return full_surf, empty_surf
def text_block(ctx, obj, text, font, lang="en-US", debug=False): ctx.save() lyt = PangoCairo.create_layout(ctx) pg_ctx = lyt.get_context() pg_ctx.set_language(Pango.Language.from_string(lang)) fo = cairo.FontOptions() fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL) PangoCairo.context_set_font_options(pg_ctx, fo) font_family = font.family if not font.replace else font.replace pg_font = Pango.FontDescription("{} {}px".format(font_family, font.size)) lyt.set_font_description(pg_font) lyt.set_markup(text, -1) # force length calculation lyt.set_height(obj.height * Pango.SCALE * 1.5) # TODO what? lyt.set_width(obj.width * Pango.SCALE) lyt.set_alignment(Pango.Alignment.CENTER) #PangoCairo.update_layout(ctx, lyt) pg_size = lyt.get_pixel_size() ink, logical = lyt.get_pixel_extents() while ink.height > obj.height and pg_font.get_size() > 0: pg_font.set_size(pg_font.get_size() - Pango.SCALE) lyt.set_font_description(pg_font) ink, logical = lyt.get_pixel_extents() #x = obj["x"] - pext.x - pext.width / 2 #y = obj["y"] - pext.y - pext.height / 2 x = (obj.x + obj.width / 2) - ((ink.x + ink.width / 2)) y = (obj.y + obj.height / 2) - ((ink.y + ink.height / 2)) #x = obj["x"] #y = obj["y"] if debug: print("x,y: %s, %s" % (x, y)) ctx.translate(x, y) PangoCairo.update_layout(ctx, lyt) PangoCairo.layout_path(ctx, lyt) ctx.set_line_width(9.0) ctx.set_line_cap(cairo.LINE_CAP_ROUND) ctx.set_line_join(cairo.LINE_JOIN_ROUND) ctx.set_source_rgb(*font.color) PangoCairo.show_layout(ctx, lyt) ctx.new_path() ctx.restore() if debug: rectangle(ctx, ink.x, ink.y, ink.width, ink.height, True, 2.0, (0, 1, 0)) rectangle(ctx, obj.x, obj.y, obj.width, obj.height, True, 2.0, (0.8, 0.8, 0))
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()