def getImage(self): image = NSImage.alloc().initWithSize_((self.width, self.height)) image.setFlipped_(True) image.lockFocus() context = NSGraphicsContext.currentContext() bodyRect = ((0, 0), (self.width, self.height - self.headerHeight)) headerRect = ((0, -self.height + self.headerHeight), (self.width, self.headerHeight)) # draw a background color cellBackgroundColor.set() NSRectFill(((0, 0), (self.width, self.height))) # background context.saveGraphicsState() bodyTransform = NSAffineTransform.transform() bodyTransform.translateXBy_yBy_(0, self.height - self.headerHeight) bodyTransform.scaleXBy_yBy_(1.0, -1.0) bodyTransform.concat() self.drawCellBackground(bodyRect) context.restoreGraphicsState() # glyph if self.shouldDrawMetrics: self.drawCellHorizontalMetrics(bodyRect) self.drawCellVerticalMetrics(bodyRect) context.saveGraphicsState() NSBezierPath.clipRect_( ((0, 0), (self.width, self.height - self.headerHeight))) glyphTransform = NSAffineTransform.transform() glyphTransform.translateXBy_yBy_(self.xOffset, self.yOffset) glyphTransform.scaleBy_(self.scale) glyphTransform.concat() self.drawCellGlyph() context.restoreGraphicsState() # foreground context.saveGraphicsState() bodyTransform.concat() self.drawCellForeground(bodyRect) context.restoreGraphicsState() # header if self.shouldDrawHeader: context.saveGraphicsState() headerTransform = NSAffineTransform.transform() headerTransform.translateXBy_yBy_(0, self.headerHeight) headerTransform.scaleXBy_yBy_(1.0, -1.0) headerTransform.concat() self.drawCellHeaderBackground(headerRect) self.drawCellHeaderText(headerRect) context.restoreGraphicsState() # done image.unlockFocus() return image
def getImage(self): image = NSImage.alloc().initWithSize_((self.width, self.height)) image.setFlipped_(True) image.lockFocus() context = NSGraphicsContext.currentContext() bodyRect = ((0, 0), (self.width, self.height - self.headerHeight)) headerRect = ((0, -self.height + self.headerHeight), (self.width, self.headerHeight)) # draw a background color cellBackgroundColor.set() NSRectFill(((0, 0), (self.width, self.height))) # background context.saveGraphicsState() bodyTransform = NSAffineTransform.transform() bodyTransform.translateXBy_yBy_(0, self.height - self.headerHeight) bodyTransform.scaleXBy_yBy_(1.0, -1.0) bodyTransform.concat() self.drawCellBackground(bodyRect) context.restoreGraphicsState() # glyph if self.shouldDrawMetrics: self.drawCellHorizontalMetrics(bodyRect) self.drawCellVerticalMetrics(bodyRect) context.saveGraphicsState() NSBezierPath.clipRect_(((0, 0), (self.width, self.height - self.headerHeight))) glyphTransform = NSAffineTransform.transform() glyphTransform.translateXBy_yBy_(self.xOffset, self.yOffset) glyphTransform.scaleBy_(self.scale) glyphTransform.concat() self.drawCellGlyph() context.restoreGraphicsState() # foreground context.saveGraphicsState() bodyTransform.concat() self.drawCellForeground(bodyRect) context.restoreGraphicsState() # header if self.shouldDrawHeader: context.saveGraphicsState() headerTransform = NSAffineTransform.transform() headerTransform.translateXBy_yBy_(0, self.headerHeight) headerTransform.scaleXBy_yBy_(1.0, -1.0) headerTransform.concat() self.drawCellHeaderBackground(headerRect) self.drawCellHeaderText(headerRect) context.restoreGraphicsState() # done image.unlockFocus() return image
class Canvas(GCanvas): def __init__(self): self._ns_path = NSBezierPath.bezierPath() self._ns_path.setWindingRule_(NSEvenOddWindingRule) self._stack = [] ctx = NSGraphicsContext.currentContext() ctx.setCompositingOperation_(NSCompositeSourceOver) GCanvas.__init__(self) self._printing = not ctx.isDrawingToScreen() self.initgraphics() def get_pencolor(self): return self._pencolor def set_pencolor(self, c): self._pencolor = c def get_fillcolor(self): return self._fillcolor def set_fillcolor(self, c): self._fillcolor = c def get_textcolor(self): return self._textcolor def set_textcolor(self, c): self._textcolor = c def get_backcolor(self): return self._backcolor def set_backcolor(self, c): self._backcolor = c def get_pensize(self): return self._pensize def set_pensize(self, d): self._pensize = d self._ns_path.setLineWidth_(d) def get_font(self): return self._font def set_font(self, f): self._font = f def get_current_point(self): return self._ns_path.currentPoint() def newpath(self): self._ns_path.removeAllPoints() def moveto(self, x, y): self._ns_path.moveToPoint_((x, y)) def rmoveto(self, dx, dy): self._ns_path.relativeMoveToPoint_((dx, dy)) def lineto(self, x, y): self._ns_path.lineToPoint_((x, y)) def rlineto(self, dx, dy): self._ns_path.relativeLineToPoint_((dx, dy)) def curveto(self, cp1, cp2, ep): self._ns_path.curveToPoint_controlPoint1_controlPoint2_(ep, cp1, *cp2) def rcurveto(self, cp1, cp2, ep): self._ns_path.relativeCurveToPoint_controlPoint1_controlPoint2_( ep, cp1, cp2) def arc(self, c, r, a0, a1): self._ns_path.appendBezierPathWithArcWithCenter_radius_startAngle_endAngle_( c, r, a0, a1) def rect(self, rect): self._ns_path.appendBezierPathWithRect_(_ns_rect(rect)) def oval(self, rect): self._ns_path.appendBezierPathWithOvalInRect(_ns_rect(rect)) def lines(self, points): # Due to a memory leak in PyObjC 2.3, we need to be very careful # about the type of object that we pass to appendBezierPathWithPoints_count_. # If 'points' is a numpy array, we convert it to an array.array of type 'f', # else we fall back on iterating over the points in Python. # ns = self._ns_path # ns.moveToPoint_(points[0]) # ns.appendBezierPathWithPoints_count_(points, len(points)) try: p = points.flat except AttributeError: GCanvas.lines(self, points) else: a = array('f', p) ns = self._ns_path ns.moveToPoint_(points[0]) ns.appendBezierPathWithPoints_count_(a, len(points)) def poly(self, points): # ns = self._ns_path # ns.moveToPoint_(points[0]) # ns.appendBezierPathWithPoints_count_(points, len(points)) # ns.closePath() self.lines(points) self.closepath() def closepath(self): self._ns_path.closePath() def clip(self): ns = self._ns_path ns.addClip() def rectclip(self, (l, t, r, b)): ns_rect = NSMakeRect(l, t, r - l, b - t) NSBezierPath.clipRect_(ns_rect)
class Canvas(GCanvas): def __init__(self): self._ns_path = NSBezierPath.bezierPath() self._ns_path.setWindingRule_(NSEvenOddWindingRule) self._stack = [] ctx = NSGraphicsContext.currentContext() ctx.setCompositingOperation_(NSCompositeSourceOver) GCanvas.__init__(self) self._printing = not ctx.isDrawingToScreen() self.initgraphics() self.transformstack = [[]] def get_pencolor(self): return self._pencolor def set_pencolor(self, c): self._pencolor = c def get_fillcolor(self): return self._fillcolor def set_fillcolor(self, c): self._fillcolor = c def get_textcolor(self): return self._textcolor def set_textcolor(self, c): self._textcolor = c def get_backcolor(self): return self._backcolor def set_backcolor(self, c): self._backcolor = c def get_pensize(self): return self._pensize def set_pensize(self, d): self._pensize = d self._ns_path.setLineWidth_(d) def get_font(self): return self._font def set_font(self, f): self._font = f def get_current_point(self): return self._ns_path.currentPoint() def newpath(self): self._ns_path.removeAllPoints() self.minx = sys.maxint self.miny = sys.maxint self.maxx = -sys.maxint / 2 self.maxy = -sys.maxint / 2 #for i in range(len(self.transformstack)): #j = self.transformstack.pop() #transforms = {"translate":self.translate,"rotate":self.rotate,"scale":self.scale} #transforms[j[0]](*j[1:]) def moveto(self, x, y): x, y = self._transform(x, y) self.minx = min(self.minx, x) self.miny = min(self.miny, y) self.maxx = max(self.maxx, x) self.maxy = max(self.maxy, y) self._ns_path.moveToPoint_((x, y)) def rmoveto(self, dx, dy): self._ns_path.relativeMoveToPoint_((dx, dy)) def lineto(self, x, y): x, y = self._transform(x, y) self.minx = min(self.minx, x) self.miny = min(self.miny, y) self.maxx = max(self.maxx, x) self.maxy = max(self.maxy, y) self._ns_path.lineToPoint_((x, y)) def rlineto(self, dx, dy): self._ns_path.relativeLineToPoint_((dx, dy)) def curveto(self, cp1, cp2, ep): cp1 = self._transform(*cp1) cp2 = self._transform(*cp2) ep = self._transform(*ep) self.minx = min(self.minx, cp1[0], cp2[0], ep[0]) self.miny = min(self.miny, cp1[1], cp2[1], ep[1]) self.maxx = max(self.maxx, cp1[0], cp2[0], ep[0]) self.maxy = max(self.maxy, cp1[1], cp2[1], ep[1]) self._ns_path.curveToPoint_controlPoint1_controlPoint2_(ep, cp1, cp2) def rcurveto(self, cp1, cp2, ep): self._ns_path.relativeCurveToPoint_controlPoint1_controlPoint2_( ep, cp1, cp2) def arc(self, c, r, a0, a1): c = self._transform(*c) self._ns_path.appendBezierPathWithArcWithCenter_radius_startAngle_endAngle_( c, r, a0, a1) def rect(self, rect): try: rect = self._transform(rect[0], rect[1]) + self._transform( rect[2], rect[3]) except: print "Error here - line 113" self._ns_path.appendBezierPathWithRect_(_ns_rect(rect)) def oval(self, rect): rect = (self._transform(*rect[0]), self._transform(*rect[1])) self._ns_path.appendBezierPathWithOvalInRect(_ns_rect(rect)) def lines(self, points): # Due to a memory leak in PyObjC 2.3, we need to be very careful # about the type of object that we pass to appendBezierPathWithPoints_count_. # If 'points' is a numpy array, we convert it to an array.array of type 'f', # else we fall back on iterating over the points in Python. # ns = self._ns_path # ns.moveToPoint_(points[0]) # ns.appendBezierPathWithPoints_count_(points, len(points)) try: p = points.flat except AttributeError: GCanvas.lines(self, points) else: a = array('f', p) ns = self._ns_path ns.moveToPoint_(points[0]) ns.appendBezierPathWithPoints_count_(a, len(points)) def poly(self, points): # ns = self._ns_path # ns.moveToPoint_(points[0]) # ns.appendBezierPathWithPoints_count_(points, len(points)) # ns.closePath() self.lines(points) self.closepath() def closepath(self): self._ns_path.closePath() def clip(self): ns = self._ns_path ns.addClip() def rectclip(self, (l, t, r, b)): ns_rect = NSMakeRect(l, t, r - l, b - t) NSBezierPath.clipRect_(ns_rect)