diff options
author | Kevin Walzer <kw@codebykevin.com> | 2023-09-27 18:38:02 (GMT) |
---|---|---|
committer | Kevin Walzer <kw@codebykevin.com> | 2023-09-27 18:38:02 (GMT) |
commit | cd76f6f22628ff8a4824a270eb51511d251d537c (patch) | |
tree | 7f4f8a4ab21029c67af117afe81803fc0dad2e30 | |
parent | 12477319c69ecc396d227e770c6c6cdccc898bf9 (diff) | |
download | tk-cd76f6f22628ff8a4824a270eb51511d251d537c.zip tk-cd76f6f22628ff8a4824a270eb51511d251d537c.tar.gz tk-cd76f6f22628ff8a4824a270eb51511d251d537c.tar.bz2 |
Update canvas printing implmentation on macOS to address Apple's removal of PostScript conversion tools from OS
-rw-r--r-- | library/print.tcl | 14 | ||||
-rw-r--r-- | macosx/tkMacOSXImage.c | 11 | ||||
-rw-r--r-- | macosx/tkMacOSXImage.h | 23 | ||||
-rw-r--r-- | macosx/tkMacOSXPrint.c | 89 | ||||
-rw-r--r-- | unix/Makefile.in | 2 | ||||
-rwxr-xr-x | unix/configure | 2 | ||||
-rw-r--r-- | unix/configure.ac | 2 |
7 files changed, 122 insertions, 21 deletions
diff --git a/library/print.tcl b/library/print.tcl index 85383c5..77e509a 100644 --- a/library/print.tcl +++ b/library/print.tcl @@ -957,16 +957,10 @@ proc ::tk::print {w} { tailcall ::tk::print::_print $w } "Canvas,aqua" { - set psfile [::tk::print::makeTempFile tk_canvas.ps] - try { - $w postscript -file $psfile - set printfile [::tk::print::makePDF $psfile tk_canvas.pdf] - ::tk::print::_print $printfile - } finally { - file delete $psfile - } - } - + ::tk::print::_printcanvas $w + set printfile /tmp/tk_canvas.pdf + ::tk::print::_print $printfile + } "Text,win32" { tailcall ::tk::print::_print_data [$w get 1.0 end] 1 {Arial 12} } diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c index db8825c..5ba2f8c 100644 --- a/macosx/tkMacOSXImage.c +++ b/macosx/tkMacOSXImage.c @@ -15,12 +15,13 @@ #include "tkMacOSXPrivate.h" #include "tkMacOSXConstants.h" +#include "tkMacOSXImage.h" #include "tkColor.h" #include "xbytes.h" -static CGImageRef CreateCGImageFromPixmap(Drawable pixmap); -static CGImageRef CreateCGImageFromDrawableRect( Drawable drawable, - int x, int y, unsigned int width, unsigned int height); +//static CGImageRef CreateCGImageFromPixmap(Drawable pixmap); +//static CGImageRef CreateCGImageFromDrawableRect( Drawable drawable, +// int x, int y, unsigned int width, unsigned int height); /* Pixel formats * @@ -628,7 +629,7 @@ int TkpPutRGBAImage( *---------------------------------------------------------------------- */ -static CGImageRef +CGImageRef CreateCGImageFromDrawableRect( Drawable drawable, int x, @@ -690,7 +691,7 @@ CreateCGImageFromDrawableRect( *---------------------------------------------------------------------- */ -static CGImageRef +CGImageRef CreateCGImageFromPixmap( Drawable pixmap) { diff --git a/macosx/tkMacOSXImage.h b/macosx/tkMacOSXImage.h new file mode 100644 index 0000000..1db5acd --- /dev/null +++ b/macosx/tkMacOSXImage.h @@ -0,0 +1,23 @@ +/* + * tkMacOSXImage.h -- + * + * + * The code in this file provides an interface for XImages, and + * implements the nsimage image type. + * + * Copyright (c) 1995-1997 Sun Microsystems, Inc. + * Copyright (c) 2001-2009, Apple Inc. + * Copyright (c) 2005-2009 Daniel A. Steffen <das@users.sourceforge.net> + * Copyright (c) 2017-2021 Marc Culler. + * + * See the file "license.terms" for information on usage and redistribution + * of this file, and for a DISCLAIMER OF ALL WARRANTIES. + */ + +/* + * Function prototypes + */ + +CGImageRef CreateCGImageFromPixmap(Drawable pixmap); +CGImageRef CreateCGImageFromDrawableRect( Drawable drawable, + int x, int y, unsigned int width, unsigned int height); diff --git a/macosx/tkMacOSXPrint.c b/macosx/tkMacOSXPrint.c index 3ebfa29..52f7d88 100644 --- a/macosx/tkMacOSXPrint.c +++ b/macosx/tkMacOSXPrint.c @@ -22,13 +22,15 @@ #include <unistd.h> #include <stdio.h> #include <fcntl.h> +#include <tkMacOSXImage.h> -/* Forward declarations of functions and variables. */ NSString * fileName = nil; CFStringRef urlFile = NULL; int StartPrint(void *clientData, Tcl_Interp * interp, int objc, Tcl_Obj * const objv[]); -OSStatus FinishPrint(NSString *file, int buttonValue); +OSStatus FinishPrint(NSString *file, int buttonValue); +int MakePDF(void *clientData, Tcl_Interp *ip, + int objc, Tcl_Obj *const objv[]); int MacPrint_Init(Tcl_Interp * interp); /* Delegate class for print dialogs. */ @@ -237,7 +239,7 @@ FinishPrint( NSFileManager * fileManager = [NSFileManager defaultManager]; NSError * error = nil; - /* + /* * Is the target file a PDF? If so, copy print file * to output location. */ @@ -326,6 +328,86 @@ FinishPrint( return status; } + +/* + *---------------------------------------------------------------------- + * + * MakePDF-- + * + * Converts a Tk canvas to PDF data. + * + * Results: + * Outputs PDF file. + * + *---------------------------------------------------------------------- + */ + +int MakePDF + (void *clientData, + Tcl_Interp *ip, + int objc, + Tcl_Obj *const objv[]) +{ + Tk_Window path; + 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; + + if (objc != 2) { + Tcl_WrongNumArgs(ip, 1, objv, "path?"); + return TCL_ERROR; + } + + /*Get window and render to image.*/ + + path = Tk_NameToWindow(ip, Tcl_GetString(objv[1]), Tk_MainWindow(ip)); + if (path == NULL) { + return TCL_ERROR; + } + + Tk_MakeWindowExist(path); + Tk_MapWindow(path); + d = Tk_WindowId(path); + width = Tk_Width(path); + 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); + 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); + } + return TCL_OK; +} + /* *---------------------------------------------------------------------- * @@ -342,6 +424,7 @@ FinishPrint( int MacPrint_Init(Tcl_Interp * interp) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Tcl_CreateObjCommand(interp, "::tk::print::_print", StartPrint, NULL, NULL); + Tcl_CreateObjCommand(interp, "::tk::print::_printcanvas", MakePDF, NULL, NULL); [pool release]; return TCL_OK; } diff --git a/unix/Makefile.in b/unix/Makefile.in index a9651ac..9fce557 100644 --- a/unix/Makefile.in +++ b/unix/Makefile.in @@ -1402,7 +1402,7 @@ tkMacOSXFont.o: $(MAC_OSX_DIR)/tkMacOSXFont.c tkMacOSXHLEvents.o: $(MAC_OSX_DIR)/tkMacOSXHLEvents.c $(CC) -c $(CC_SWITCHES) $(MAC_OSX_DIR)/tkMacOSXHLEvents.c -tkMacOSXImage.o: $(MAC_OSX_DIR)/tkMacOSXImage.c +tkMacOSXImage.o: $(MAC_OSX_DIR)/tkMacOSXImage.c $(CC) -c $(CC_SWITCHES) $(MAC_OSX_DIR)/tkMacOSXImage.c tkMacOSXInit.o: $(MAC_OSX_DIR)/tkMacOSXInit.c tkConfig.sh diff --git a/unix/configure b/unix/configure index dd73d5c..065c0bf 100755 --- a/unix/configure +++ b/unix/configure @@ -7652,7 +7652,7 @@ if test $tk_aqua = yes; then printf "%s\n" "#define MAC_OSX_TK 1" >>confdefs.h - LIBS="$LIBS -framework Cocoa -framework Carbon -framework IOKit -framework QuartzCore -framework Security" + LIBS="$LIBS -framework Cocoa -framework Carbon -framework IOKit -framework QuartzCore -framework Security -framework CoreGraphics" if test -d /System/Library/Frameworks/UserNotifications.framework; then LIBS="$LIBS -framework UserNotifications" fi diff --git a/unix/configure.ac b/unix/configure.ac index a81c527..38eda9d 100644 --- a/unix/configure.ac +++ b/unix/configure.ac @@ -284,7 +284,7 @@ fi if test $tk_aqua = yes; then AC_DEFINE(MAC_OSX_TK, 1, [Are we building TkAqua?]) - LIBS="$LIBS -framework Cocoa -framework Carbon -framework IOKit -framework QuartzCore -framework Security" + LIBS="$LIBS -framework Cocoa -framework Carbon -framework IOKit -framework QuartzCore -framework Security -framework CoreGraphics" if test -d /System/Library/Frameworks/UserNotifications.framework; then LIBS="$LIBS -framework UserNotifications" fi |