
{"id":175,"date":"2006-11-04T17:25:33","date_gmt":"2006-11-04T17:25:33","guid":{"rendered":"http:\/\/www.starcoder.com\/wordpress\/2006\/11\/04\/171\/"},"modified":"2021-10-30T20:00:12","modified_gmt":"2021-10-30T20:00:12","slug":"171","status":"publish","type":"post","link":"https:\/\/www.starcoder.com\/wordpress\/2006\/11\/171\/","title":{"rendered":""},"content":{"rendered":"<p>I ran across a problem in the latest betas of Seasonality where the graphs would show up blank when printing.  It took me awhile to fix this issue, so I thought I would post my solution here in the hopes that it might save someone else time in the future.  Here&#8217;s a screenshot of print output before the fix:<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/static_links\/printing_before.jpg\" \/><\/p>\n<p>The problem is that the Seasonality graphs are drawn using Core Graphics routines instead of the higher-level Quartz routines.  I did this to squeeze a little more speed out of the drawing code, and also gained more customization with transparency settings and layered drawing.  Basically, I grab a CGGraphicsRef using [[NSGraphicsContext currentContext] graphicsPort], and use it to draw all the other elements.<\/p>\n<p>From what I could find online, it seemed that the problem was that while printing, the context isn&#8217;t referenced until later than it is when drawing it to screen, so the context doesn&#8217;t stick around long enough for the printing code to pick up on it and draw.  So either I could find a way to retain the graphics context long enough for the printing to complete, or I could find a new way to draw my view for the printer.  I didn&#8217;t want to retain a graphics context object because I think that makes the code a little messy, so I tried to find a new way to draw my view.<\/p>\n<p>The first thing that came to mind was to not use all the Core Graphics routines to draw the graphs when printing.  That would be a very large code change, and it&#8217;s a lot of extra code that isn&#8217;t used too frequently.  The better solution I found was to draw everything to an NSImage first, and then draw the NSImage to the printer.  Here&#8217;s some code&#8230;<\/p>\n<pre>- (void)drawRect:(NSRect)rect {\n   NSImage *lockedImage;\n   BOOL printing = ![NSGraphicsContext currentContextDrawingToScreen];\n   if (printing) {\n      lockedImage = [[NSImage alloc] initWithSize:rect.size];\n      [lockedImage lockFocus];\n   }\n\n   \/\/ Get our graphics context.\n   CGContextRef cgContext = [[NSGraphicsContext currentContext] graphicsPort];\n\n   \/\/ Drawing code here...\n\n   if (printing) {\n      [lockedImage unlockFocus];\n      [lockedImage drawInRect:rect\n                     fromRect:NSMakeRect(0, 0, rect.size.width, rect.size.height)\n                    operation:NSCompositeCopy\n                     fraction:1.0];\n      [lockedImage release];\n   }\n}\n<\/pre>\n<p>Here&#8217;s a screenshot of the output after the fix:<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/static_links\/printing_after.jpg\" \/><\/p>\n<p>This solution has a small disadvantage, you don&#8217;t get the ability to print using the printer&#8217;s full resolution.  Since Seasonality users don&#8217;t print too often, this tradeoff is okay, but it might be an issue to consider for other applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I ran across a problem in the latest betas of Seasonality where the graphs would show up blank when printing. It took me awhile to fix this issue, so I thought I would post my solution here in the hopes that it might save someone else time in the future. Here&#8217;s a screenshot of print [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-175","post","type-post","status-publish","format-standard","hentry","category-general","post-preview"],"_links":{"self":[{"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":1,"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":617,"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/posts\/175\/revisions\/617"}],"wp:attachment":[{"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.starcoder.com\/wordpress\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}