#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2017/04/26 20:45:40 +0900> u""" pycairoの動作テスト. 角が丸い四角を描画して、pngで保存。 動作確認環境: Windows10 x64 + Python 2.7.13 32bit + pycairo 1.8.10 参考ページ: rounded rectangle https://www.cairographics.org/samples/rounded_rectangle/ """ import cairo import math def draw_rounder_rectangle(ctx, x, y, w, h, ra): """Set sub path rounded rectangle.""" deg = math.pi / 180.0 ctx.new_sub_path() ctx.arc(x + w - ra, y + ra, ra, -90 * deg, 0 * deg) ctx.arc(x + w - ra, y + h - ra, ra, 0 * deg, 90 * deg) ctx.arc(x + ra, y + h - ra, ra, 90 * deg, 180 * deg) ctx.arc(x + ra, y + ra, ra, 180 * deg, 270 * deg) ctx.close_path() w, h = 640, 480 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) ctx = cairo.Context(surface) ctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL) ctx.set_line_width(6.0) x, y = 24, 24 w, h = 480, 320 radius = 32 draw_rounder_rectangle(ctx, x, y, w, h, radius) ctx.set_source_rgb(0, 0, 1) ctx.fill_preserve() ctx.set_source_rgb(0, 1, 0) ctx.stroke() surface.write_to_png("result.png")