Show
Ignore:
Timestamp:
09/20/07 17:33:32 (16 months ago)
Author:
fdb
Message:

- Colors are now correct in Text also
- PDF rendering using canvas.save
- Added for headless rendering (through console.py)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • nodebox/trunk/src/DrawingPrimitives.py

    r148 r154  
    521521 
    522522    def set(self): 
     523        self.nsColor.set() 
     524     
     525    def _get_nsColor(self): 
    523526        if self._ctx._outputmode == RGB: 
    524             self._rgb.set() 
     527            return self._rgb 
    525528        else: 
    526             self._cmyk.set() 
     529            return self._cmyk 
     530    nsColor = property(_get_nsColor) 
     531         
    527532 
    528533    def copy(self): 
     
    927932 
    928933 
     934 
    929935class Text(Grob, TransformMixin, ColorMixin): 
    930936 
     
    932938    kwargs = ('fill', 'font', 'fontsize', 'align', 'lineheight') 
    933939 
     940    __dummy_color = NSColor.blackColor() 
     941     
    934942    def __init__(self, ctx, text, x=0, y=0, width=None, height=None, **kwargs): 
    935943        super(Text, self).__init__(ctx) 
     
    957965    font = property(_get_font) 
    958966 
    959     def _getLayoutManagerTextContainerTextStorage(self, clr=NSColor.blackColor()): 
     967    def _getLayoutManagerTextContainerTextStorage(self, clr=__dummy_color): 
    960968        paraStyle = NSMutableParagraphStyle.alloc().init() 
    961969        paraStyle.setAlignment_(self._align) 
     
    986994    def _draw(self): 
    987995        if self._fillcolor is None: return 
    988         layoutManager, textContainer, textStorage = self._getLayoutManagerTextContainerTextStorage(self._fillcolor._cmyk) 
     996        layoutManager, textContainer, textStorage = self._getLayoutManagerTextContainerTextStorage(self._fillcolor.nsColor) 
    989997        x,y = self.x, self.y 
    990998        glyphRange = layoutManager.glyphRangeForTextContainer_(textContainer) 
     
    11141122        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) 
    11151123 
     1124class _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 
    11161145class Canvas(Grob): 
    11171146    def __init__(self, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT): 
     
    11601189        return image 
    11611190    image = property(_get_image) 
    1162  
     1191     
    11631192    def save(self, fname, format=None): 
    11641193        if format is None: 
    11651194            basename, ext = os.path.splitext(fname) 
    11661195            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) 
    11811214             
    11821215        fname = NSString.stringByExpandingTildeInPath(fname)