From 74e522c0c2bb077db468e42eb11010d85aa28b47 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 10 Jul 2011 22:41:35 +0000 Subject: [PATCH] * Source/GSGhostscriptImageRep.m: Remove hardcoded path to gs executable. First try GSGhostscriptExecutablePath default, then try running "$SHELL -c which gs" to get the path. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@33505 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++ Source/GSGhostscriptImageRep.m | 101 +++++++++++++++++++++++++-------- 2 files changed, 84 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index 27e35c0d0..73f49bb39 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2011-07-10 Eric Wasylishen + + * Source/GSGhostscriptImageRep.m: Remove hardcoded path to gs + executable. First try GSGhostscriptExecutablePath default, then + try running "$SHELL -c which gs" to get the path. + 2011-07-08 Eric Wasylishen * Source/NSView.m (-resizeWithOldSuperviewSize:): Factor out diff --git a/Source/GSGhostscriptImageRep.m b/Source/GSGhostscriptImageRep.m index efc2f8767..8c22d2a52 100644 --- a/Source/GSGhostscriptImageRep.m +++ b/Source/GSGhostscriptImageRep.m @@ -28,9 +28,14 @@ #import #import +#import #import #import +#import +#import +#import #import +#import #import "AppKit/NSImageRep.h" #import "AppKit/NSPasteboard.h" #import "AppKit/NSGraphicsContext.h" @@ -89,6 +94,49 @@ return types; } +- (NSString *) _ghostscriptExecutablePath +{ + NSString *result = nil; + + result = [[NSUserDefaults standardUserDefaults] stringForKey: @"GSGhostscriptExecutablePath"]; + if (result == nil) + { + NS_DURING + { + NSTask *task = [[[NSTask alloc] init] autorelease]; + NSPipe *outputPipe = [NSPipe pipe]; + NSFileHandle *outputHandle = [outputPipe fileHandleForReading]; + NSString *shellLaunchPath = [[[NSProcessInfo processInfo] environment] objectForKey: @"SHELL"]; + NSData *resultData; + + [task setLaunchPath: shellLaunchPath]; + [task setArguments: [NSArray arrayWithObjects: @"-c", @"which gs", nil]]; + [task setStandardOutput: outputPipe]; + [task launch]; + + resultData = [outputHandle readDataToEndOfFile]; + [outputHandle closeFile]; + + if (resultData != nil && [resultData length] > 0) + { + // FIXME: How do we know which encoding the data will be in? + result = [[[NSString alloc] initWithBytes: [resultData bytes] + length: [resultData length] + encoding: NSUTF8StringEncoding] autorelease]; + + result = [result stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + } + } + NS_HANDLER + { + } + NS_ENDHANDLER + } + + return result; +} + - (NSData *) _pngWithGhostscriptData: (NSData *)psData atResolution: (CGFloat)res { NSTask *task = [[[NSTask alloc] init] autorelease]; @@ -96,30 +144,37 @@ NSPipe *outputPipe = [NSPipe pipe]; NSFileHandle *inputHandle = [inputPipe fileHandleForWriting]; NSFileHandle *outputHandle = [outputPipe fileHandleForReading]; - NSData *result; + NSData *result = nil; + NSString *launchPath = [self _ghostscriptExecutablePath]; - // FIXME: Parameterize - [task setLaunchPath: @"/usr/bin/gs"]; - [task setArguments: [NSArray arrayWithObjects: @"-dSAFER", - @"-q", - @"-o", - @"-", // Write output image to stdout - @"-sDEVICE=pngalpha", - [NSString stringWithFormat: @"-r%d", (int)res], - @"-dTextAlphaBits=4", - @"-dGraphicsAlphaBits=4", - @"-dDOINTERPOLATE", - @"-", // Read input from stdin - nil]]; - [task setStandardInput: inputPipe]; - [task setStandardOutput: outputPipe]; - [task launch]; - - [inputHandle writeData: psData]; - [inputHandle closeFile]; - - result = [outputHandle readDataToEndOfFile]; - [outputHandle closeFile]; + NS_DURING + { + [task setLaunchPath: launchPath]; + [task setArguments: [NSArray arrayWithObjects: @"-dSAFER", + @"-q", + @"-o", + @"-", // Write output image to stdout + @"-sDEVICE=pngalpha", + [NSString stringWithFormat: @"-r%d", (int)res], + @"-dTextAlphaBits=4", + @"-dGraphicsAlphaBits=4", + @"-dDOINTERPOLATE", + @"-", // Read input from stdin + nil]]; + [task setStandardInput: inputPipe]; + [task setStandardOutput: outputPipe]; + [task launch]; + + [inputHandle writeData: psData]; + [inputHandle closeFile]; + + result = [outputHandle readDataToEndOfFile]; + [outputHandle closeFile]; + } + NS_HANDLER + { + } + NS_ENDHANDLER return result; }