From 1c5c40e220e700f37c72ac520aca7de1cff6f52b Mon Sep 17 00:00:00 2001 From: Kevin Walzer Date: Thu, 28 Sep 2023 13:21:16 +0000 Subject: Replace CGImageRef data with PDF data --- macosx/tkMacOSXImage.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ macosx/tkMacOSXImage.h | 2 ++ macosx/tkMacOSXPrint.c | 46 ++++++++++----------------------------------- 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c index ebbe323..b0c199c 100644 --- a/macosx/tkMacOSXImage.c +++ b/macosx/tkMacOSXImage.c @@ -670,6 +670,57 @@ CreateCGImageFromDrawableRect( } return result; } + +/* + *---------------------------------------------------------------------- + * + * CreatePDFFromDrawableRect + * + * Extract PDF data from a MacOSX drawable. + * + * Results: + * Returns a CFDataRef that can be written to a file. + * + * NOTE: The x,y coordinates should be relative to a coordinate system + * with origin at the top left, as used by XImage and CGImage, not bottom + * left as used by NSView. + * + * Side effects: + * None + * + *---------------------------------------------------------------------- + */ + +CFDataRef +CreatePDFFromDrawableRect( + Drawable drawable, + int x, + int y, + unsigned int width, + unsigned int height) +{ + MacDrawable *mac_drawable = (MacDrawable *)drawable; + NSView *view = TkMacOSXGetNSViewForDrawable(mac_drawable); + if (view == nil) { + TkMacOSXDbgMsg("Invalid source drawable"); + return NULL; + } + NSRect bounds, viewSrcRect; + + /* + * Get the child window area in NSView coordinates + * (origin at bottom left). + */ + + bounds = [view bounds]; + viewSrcRect = NSMakeRect(mac_drawable->xOff + x, + bounds.size.height - height - (mac_drawable->yOff + y), + width, height); + NSData *viewData = [view dataWithPDFInsideRect:viewSrcRect]; + CFDataRef result = (CFDataRef)viewData; + return result; +} + /* *---------------------------------------------------------------------- diff --git a/macosx/tkMacOSXImage.h b/macosx/tkMacOSXImage.h index 1db5acd..7dcb50e 100644 --- a/macosx/tkMacOSXImage.h +++ b/macosx/tkMacOSXImage.h @@ -21,3 +21,5 @@ CGImageRef CreateCGImageFromPixmap(Drawable pixmap); CGImageRef CreateCGImageFromDrawableRect( Drawable drawable, int x, int y, unsigned int width, unsigned int height); +CFDataRef CreatePDFFromDrawableRect( Drawable drawable, + int x, int y, unsigned int width, unsigned int height); diff --git a/macosx/tkMacOSXPrint.c b/macosx/tkMacOSXPrint.c index f13fa90..44a164d 100644 --- a/macosx/tkMacOSXPrint.c +++ b/macosx/tkMacOSXPrint.c @@ -23,6 +23,7 @@ #include #include #include +#include "tkMacOSXPrivate.h" NSString * fileName = nil; CFStringRef urlFile = NULL; @@ -354,19 +355,14 @@ int MakePDF Drawable d = NULL; int x, y; unsigned int width, height; - CGImageRef canvas = NULL; - CGContextRef pdfContext; - CFURLRef url; - CFDataRef boxData = NULL; - CFMutableDictionaryRef imgDictionary = NULL; - CFMutableDictionaryRef pageDictionary = NULL; + CFDataRef pdfData; if (objc != 2) { Tcl_WrongNumArgs(ip, 1, objv, "path?"); return TCL_ERROR; } - /*Get window and render to image.*/ + /*Get window and render to PDF.*/ path = Tk_NameToWindow(ip, Tcl_GetString(objv[1]), Tk_MainWindow(ip)); if (path == NULL) { @@ -380,36 +376,14 @@ int MakePDF height = Tk_Height(path); MacDrawable *mac_drawable = (MacDrawable *)d; - canvas = CreateCGImageFromDrawableRect(mac_drawable, 0, 0, width, height); - CGRect rect = CGRectMake(x + mac_drawable->xOff, y + mac_drawable->yOff, - width, height); - - /*Render image to PDF file.*/ - if (canvas) { - url = CFURLCreateWithFileSystemPath (NULL, CFSTR("/tmp/tk_canvas.pdf"), - kCFURLPOSIXPathStyle, 0); - imgDictionary = CFDictionaryCreateMutable(NULL, 0, - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks); - CFDictionarySetValue(imgDictionary, kCGPDFContextTitle, CFSTR("tk_canvas")); - CFDictionarySetValue(imgDictionary, kCGPDFContextCreator, CFSTR("Tk")); - pdfContext = CGPDFContextCreateWithURL (url, &rect, imgDictionary); - CFRelease(imgDictionary); - CFRelease(url); - pageDictionary = CFDictionaryCreateMutable(NULL, 0, - &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks); - boxData = CFDataCreate(NULL,(const UInt8 *)&rect, sizeof (CGRect)); - CFDictionarySetValue(pageDictionary, kCGPDFContextMediaBox, boxData); - CGPDFContextBeginPage (pdfContext, pageDictionary); - CGContextDrawImage (pdfContext, rect,canvas); - CGImageRelease (canvas); - CGPDFContextEndPage (pdfContext); - CGContextRelease (pdfContext); - CFRelease(pageDictionary); - CFRelease(boxData); - } + TKContentView *view = (TKContentView *)TkMacOSXGetNSViewForDrawable(mac_drawable); + + pdfData = CreatePDFFromDrawableRect(mac_drawable, 0, 0, width, height); + + NSData *viewData = (NSData*)pdfData; + [viewData writeToFile:@"/tmp/tk_canvas.pdf" atomically:YES]; return TCL_OK; + } /* -- cgit v0.12