Changeset 154 for nodebox/trunk/src/DrawingPrimitives.py
- Timestamp:
- 09/20/07 17:33:32 (16 months ago)
- Files:
-
- 1 modified
-
nodebox/trunk/src/DrawingPrimitives.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
nodebox/trunk/src/DrawingPrimitives.py
r148 r154 521 521 522 522 def set(self): 523 self.nsColor.set() 524 525 def _get_nsColor(self): 523 526 if self._ctx._outputmode == RGB: 524 self._rgb.set()527 return self._rgb 525 528 else: 526 self._cmyk.set() 529 return self._cmyk 530 nsColor = property(_get_nsColor) 531 527 532 528 533 def copy(self): … … 927 932 928 933 934 929 935 class Text(Grob, TransformMixin, ColorMixin): 930 936 … … 932 938 kwargs = ('fill', 'font', 'fontsize', 'align', 'lineheight') 933 939 940 __dummy_color = NSColor.blackColor() 941 934 942 def __init__(self, ctx, text, x=0, y=0, width=None, height=None, **kwargs): 935 943 super(Text, self).__init__(ctx) … … 957 965 font = property(_get_font) 958 966 959 def _getLayoutManagerTextContainerTextStorage(self, clr= NSColor.blackColor()):967 def _getLayoutManagerTextContainerTextStorage(self, clr=__dummy_color): 960 968 paraStyle = NSMutableParagraphStyle.alloc().init() 961 969 paraStyle.setAlignment_(self._align) … … 986 994 def _draw(self): 987 995 if self._fillcolor is None: return 988 layoutManager, textContainer, textStorage = self._getLayoutManagerTextContainerTextStorage(self._fillcolor. _cmyk)996 layoutManager, textContainer, textStorage = self._getLayoutManagerTextContainerTextStorage(self._fillcolor.nsColor) 989 997 x,y = self.x, self.y 990 998 glyphRange = layoutManager.glyphRangeForTextContainer_(textContainer) … … 1114 1122 return "Variable(name=%s, type=%s, default=%s, min=%s, max=%s, value=%s)" % (self.name, self.type, self.default, self.min, self.max, self.value) 1115 1123 1124 class _PDFRenderView(NSView): 1125 1126 # This view was created to provide PDF data. 1127 # Strangely enough, the only way to get PDF data from Cocoa is by asking 1128 # dataWithPDFInsideRect_ from a NSView. So, we create one just to get to 1129 # the PDF data. 1130 1131 def initWithCanvas_(self, canvas): 1132 super(_PDFRenderView, self).initWithFrame_( ((0, 0), (canvas.width, canvas.height)) ) 1133 self.canvas = canvas 1134 return self 1135 1136 def drawRect_(self, rect): 1137 self.canvas.draw() 1138 1139 def isOpaque(self): 1140 return False 1141 1142 def isFlipped(self): 1143 return True 1144 1116 1145 class Canvas(Grob): 1117 1146 def __init__(self, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): … … 1160 1189 return image 1161 1190 image = property(_get_image) 1162 1191 1163 1192 def save(self, fname, format=None): 1164 1193 if format is None: 1165 1194 basename, ext = os.path.splitext(fname) 1166 1195 format = ext[1:].lower() # Skip the dot 1167 1168 imgTypes = {"gif": NSGIFFileType, 1169 "jpg": NSJPEGFileType, 1170 "jpeg": NSJPEGFileType, 1171 "png": NSPNGFileType, 1172 "tiff": NSTIFFFileType} 1173 if format not in imgTypes: 1174 raise NodeBoxError, "Filename should end in .tiff, .gif, .jpg or .png" 1175 1176 data = self.image.TIFFRepresentation() 1177 if format != 'tiff': 1178 imgType = imgTypes[format] 1179 rep = NSBitmapImageRep.imageRepWithData_(data) 1180 data = rep.representationUsingType_properties_(imgType, None) 1196 1197 if format == 'pdf': 1198 view = _PDFRenderView.alloc().initWithCanvas_(self) 1199 data = view.dataWithPDFInsideRect_(view.bounds()) 1200 else: 1201 imgTypes = {"gif": NSGIFFileType, 1202 "jpg": NSJPEGFileType, 1203 "jpeg": NSJPEGFileType, 1204 "png": NSPNGFileType, 1205 "tiff": NSTIFFFileType} 1206 if format not in imgTypes: 1207 raise NodeBoxError, "Filename should end in .pdf, .tiff, .gif, .jpg or .png" 1208 1209 data = self.image.TIFFRepresentation() 1210 if format != 'tiff': 1211 imgType = imgTypes[format] 1212 rep = NSBitmapImageRep.imageRepWithData_(data) 1213 data = rep.representationUsingType_properties_(imgType, None) 1181 1214 1182 1215 fname = NSString.stringByExpandingTildeInPath(fname)
