[MacRuby-devel] Creating a pdf : Cocoa to MacRuby translation

Laurent Sansonetti lsansonetti at apple.com
Thu Nov 4 13:23:55 PDT 2010


Salut Julien,

Looks like you forgot to set the value of the pointer. CGPDFContextCreateWithURL() was therefore called with a pointer to a zero rect.

Try this:

	page_rectangle         = NSMakeRect(0,0,100,100)
	page_rectangle_pointer = Pointer.new_with_type(NSRect.type)    # Using NSRect.type is better than pasting the whole signature here
	page_rectangle_pointer[0] = page_rectangle

Laurent

On Nov 4, 2010, at 3:12 AM, Julien Jassaud wrote:

> Hello to every one,
> 
> I am trying to programmatically create a pdf. I have this quite simple code in Cocoa that works perfectly :
> 
> #import <Cocoa/Cocoa.h>
> #import <Foundation/Foundation.h>
> 
> int main (int argc, const char * argv[]) {
> 
>   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
> 
>   CGContextRef pdfContext;
>   CFStringRef path;
>   CFURLRef url;
>   CGRect pageRect;
> 
>   path = CFStringCreateWithCString(NULL, "./my_pdf.pdf", kCFStringEncodingUTF8);
>   url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
>   CFRelease (path);
> 
>   pageRect = NSMakeRect(0,0,100,100);
> 
>   pdfContext = CGPDFContextCreateWithURL(url, &pageRect, nil);
>   CFRelease(url);
> 
>   CGPDFContextBeginPage(pdfContext, nil);
> 
>   CGContextSetCMYKFillColor(pdfContext, 1.0, 0.0, 0.0, 0.0, 1.0);
>   CGContextFillRect(pdfContext, NSMakeRect(30, 30, 40, 40));
> 
>   CGPDFContextEndPage(pdfContext);
> 
>   CGPDFContextClose(pdfContext);
> 
>   [pool drain];
>   return 0;
> 
> }
> 
> 
> After running this program I get a pdf in perfect working order. I tried to port this little example to MacRuby like so :
> 
> framework 'Cocoa'
> 
> url = CFURLCreateWithFileSystemPath(nil, './my_pfd.pdf', KCFURLPOSIXPathStyle, 0)
> 
> page_rectangle         = NSMakeRect(0,0,100,100)
> page_rectangle_pointer = Pointer.new_with_type('{CGRect={CGPoint=dd}{CGSize=dd}}')
> 
> pdf_context = CGPDFContextCreateWithURL(url, page_rectangle_pointer, nil)
> 
> CGPDFContextBeginPage(pdf_context, nil)
> 
> CGContextSetCMYKFillColor(pdf_context, 1.0, 0.0, 0.0, 0.0, 1.0)
> CGContextFillRect(pdf_context, NSMakeRect(30, 30, 40, 40))
> 
> CGPDFContextEndPage(pdf_context)
> CGPDFContextClose(pdf_context)
> 
> This do produces a  pdf but it is completely empty. Also, MacRuby complains when called with the -w option :
> 
> $ macruby -w cocoapdf.rb
> unknown: warning: instance variable __octype__ not initialized
> 
> 
> Does anybody have an idea what I am doing wrong ? 
> 
> Thanks,
> Julien Jassaud
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20101104/925bd97a/attachment.html>


More information about the MacRuby-devel mailing list