私は Python+Gtk+Cairo アプリに取り組んでいます。私のアプリケーションは、非常に複雑な (そして高価な) 描画を行う必要があります。
パフォーマンス上の理由から、最初に ImageSurface (画面外) に描画し、次に DrawingArea オブジェクトに ImageSurface コンテンツを描画します。
注意:図面を表示するには、図面を拡大/縮小する必要があります。そうしないと、大きな図面が窓から落ちてしまいます。
残念ながら、何かがうまくいかないことがあります。ご覧のとおり、ImageSurface のコンテンツ全体をペイントすることはできません。「標準的な」 Context.set_source_surface(imageSurface, xCoord, yCoord) Context.paint() を使用する
私はこの間違った出力しか得ません:
これは私が達成したい結果です:
イメージサーフェス全体を強制的にペイントするにはどうすればよいですか?
以下のサンプルコード:
import gtk
import cairo
def main():
w = gtk.Window()
w.add(RubberTest())
w.show_all()
gtk.main()
class RubberTest(gtk.DrawingArea):
def __init__(self, model=None):
gtk.DrawingArea.__init__(self)
self.set_size_request(800, 500)
self.connect("expose-event", self._expose, self.create_basic_image())
self.add_events(gtk.gdk.EXPOSURE_MASK
| gtk.gdk.BUTTON_PRESS_MASK
| gtk.gdk.BUTTON_RELEASE_MASK
| gtk.gdk.POINTER_MOTION_MASK)
def create_basic_image(self):
img = cairo.ImageSurface(cairo.FORMAT_ARGB32, 24, 24)
cr = cairo.Context(img)
# due to large coordinates, the drawing fall out of the window
self.points = [(10000, 10000), (790000, 10000), (790000, 490000), (10000, 490000)]
xMax, yMax, xMin, yMin = max([c[0] for c in self.points]), max([c[1] for c in self.points]), min([c[0] for c in self.points]), min([c[1] for c in self.points])
maxRectWidth = xMax - xMin
maxRectHeight = yMax - yMin
w = 800
h = 500
width_ratio = float(w) / maxRectWidth
height_ratio = float(h) / maxRectHeight
# scale factor
scaleFactor = min(height_ratio, width_ratio)
cr.set_line_width(4)
cr.set_source_rgb(1, 0, 0)
cr.save()
# scale + translate
cr.scale(scaleFactor, scaleFactor)
cr.translate(-xMin, -yMin)
for i in range(0, len(self.points)):
currPoint = self.points[i]
currX = float(currPoint[0])
currY = float(currPoint[1])
nextIndex = i + 1
if (nextIndex == len(self.points)):
nextIndex = 0
nextPoint = self.points[nextIndex]
nextX = nextPoint[0]
nextY = nextPoint[1]
cr.move_to(currX, currY)
cr.line_to(nextX, nextY)
cr.restore()
cr.close_path()
cr.stroke_preserve()
return img
def _expose(self, sender, event, img):
cr = self.window.cairo_create()
cr.set_source_surface(img, 0, 0)
cr.paint()
return True
if __name__ == '__main__':
main()
ありがとう、IT