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

Julien Jassaud sojastar07 at gmail.com
Thu Nov 4 19:19:48 PDT 2010


Matt, Laurent,

Thanks for pointing me to this very obvious mistake ! It works perfectly now.

Julien

Le 5 nov. 2010 à 05:23, Laurent Sansonetti a écrit :

> 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
> 
> _______________________________________________
> 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/20101105/0cbe29d6/attachment.html>


More information about the MacRuby-devel mailing list