我正在开发一个 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()
谢谢,它