Permalink
Browse files

full Retina support, output now PNG, @2x.PNG and TIFF

  • Loading branch information...
1 parent 8280555 commit 58e9907544c09eb2581474ed6b0f45dca34d0a2f @ByteProject ByteProject committed Oct 5, 2012
Showing with 221 additions and 25 deletions.
  1. +23 −7 AnsiLove/Classes/ALAnsiGenerator.h
  2. +198 −18 AnsiLove/Classes/ALAnsiGenerator.m
@@ -13,12 +13,28 @@
@interface ALAnsiGenerator : NSObject
-// class methods
-+ (void)createPNGFromAnsiSource:(NSString *)inputFile
- outputFile:(NSString *)outputFile
- font:(NSString *)font
- bits:(NSString *)bits
- iceColors:(NSString *)iceColors
- columns:(NSString *)columns;
+// Creates single PNG image from ANSi source.
++ (void)ansiFileToPNG:(NSString *)inputFile
+ outputFile:(NSString *)outputFile
+ font:(NSString *)font
+ bits:(NSString *)bits
+ iceColors:(NSString *)iceColors
+ columns:(NSString *)columns;
+
+// Creates regular and @2x Retina PNG image from ANSi source.
++ (void)ansiFileToRetinaPNG:(NSString *)inputFile
+ outputFile:(NSString *)outputFile
+ font:(NSString *)font
+ bits:(NSString *)bits
+ iceColors:(NSString *)iceColors
+ columns:(NSString *)columns;
+
+// Creates single TIFF image from ANSi source, all sizes embedded.
++ (void)ansiFileToRetinaTIFF:(NSString *)inputFile
+ outputFile:(NSString *)outputFile
+ font:(NSString *)font
+ bits:(NSString *)bits
+ iceColors:(NSString *)iceColors
+ columns:(NSString *)columns;
@end
@@ -19,12 +19,12 @@ - (id)init
return self;
}
-+ (void)createPNGFromAnsiSource:(NSString *)inputFile
- outputFile:(NSString *)outputFile
- font:(NSString *)font
- bits:(NSString *)bits
- iceColors:(NSString *)iceColors
- columns:(NSString *)columns
++ (void)ansiFileToPNG:(NSString *)inputFile
+ outputFile:(NSString *)outputFile
+ font:(NSString *)font
+ bits:(NSString *)bits
+ iceColors:(NSString *)iceColors
+ columns:(NSString *)columns;
{
if (inputFile == nil || inputFile == @"") {
// No inputfile? This means we can't do anything. Get the hell outta here.
@@ -33,18 +33,18 @@ + (void)createPNGFromAnsiSource:(NSString *)inputFile
if (outputFile == nil || outputFile == @"") {
// In case the user provided no output file / path, just use the file name and
- // path from the inputFile value but add .png as suffix.
- outputFile = [[NSString alloc] initWithFormat:@"%@.png", inputFile];
+ // path from the inputFile value. AnsiLove/C adds PNG suffix automatically.
+ outputFile = inputFile;
}
// We need to resolve any tilde in path before passing this argument to NSTask.
inputFile = [inputFile stringByExpandingTildeInPath];
outputFile = [outputFile stringByExpandingTildeInPath];
// Initialize the arguments array.
- NSMutableArray *arguments = [[NSMutableArray alloc] init];
+ NSMutableArray *arguments = [NSMutableArray new];
- // option flag for generating output in AnsiLove/C
+ // Option flag for generating a single, regular-sized PNG with AnsiLove/C.
NSString *optionFlag = @"-o";
// Add the three most necessary arguments to the array.
@@ -68,19 +68,199 @@ + (void)createPNGFromAnsiSource:(NSString *)inputFile
}
// Finally start the task with the flags we gathered.
- NSPipe *pipe;
- pipe = [NSPipe pipe];
+ NSPipe *libPipe;
+ libPipe = [NSPipe pipe];
- NSTask *task = [[NSTask alloc] init];
- [task setLaunchPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"ansilove" ofType:nil]];
- [task setArguments:arguments];
- [task setStandardOutput:pipe];
+ NSTask *libTask = [NSTask new];
+ [libTask setLaunchPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"ansilove" ofType:nil]];
+ [libTask setArguments:arguments];
+ [libTask setStandardOutput:libPipe];
// Ready for launch in 3... 2... 1...
- [task launch];
+ [libTask launch];
// Stay in limbo until rendering is finished.
- [task waitUntilExit];
+ [libTask waitUntilExit];
+
+ // Post a notification so any listener will know rendering has finished now.
+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
+ [nc postNotificationName:@"AnsiLoveFinishedRendering" object:self];
+}
+
++ (void)ansiFileToRetinaPNG:(NSString *)inputFile
+ outputFile:(NSString *)outputFile
+ font:(NSString *)font
+ bits:(NSString *)bits
+ iceColors:(NSString *)iceColors
+ columns:(NSString *)columns
+{
+ if (inputFile == nil || inputFile == @"") {
+ // No inputfile? This means we can't do anything. Get the hell outta here.
+ return;
+ }
+
+ if (outputFile == nil || outputFile == @"") {
+ // In case the user provided no output file / path, just use the file name and
+ // path from the inputFile value. AnsiLove/C adds PNG suffix automatically.
+ outputFile = inputFile;
+ }
+
+ // We need to resolve any tilde in path before passing this argument to NSTask.
+ inputFile = [inputFile stringByExpandingTildeInPath];
+ outputFile = [outputFile stringByExpandingTildeInPath];
+
+ // Initialize the arguments array.
+ NSMutableArray *arguments = [NSMutableArray new];
+
+ // Option flag for generating additional @2x.PNG output with AnsiLove/C.
+ NSString *optionFlag = @"-or";
+
+ // Add the three most necessary arguments to the array.
+ [arguments addObject:inputFile];
+ [arguments addObject:optionFlag];
+ [arguments addObject:outputFile];
+
+ // The following if statements check if a string is nil or empty. That way we can
+ // be sure only strings with proper contents will be added to the arguments array.
+ if (font && ![font isEqualToString:@""]) {
+ [arguments addObject:font];
+ }
+ if (bits && ![bits isEqualToString:@""]) {
+ [arguments addObject:bits];
+ }
+ if (iceColors && ([iceColors isEqualToString:@"1"] || [iceColors isEqualToString:@"0"])) {
+ [arguments addObject:iceColors];
+ }
+ if (columns && ![columns isEqualToString:@""]) {
+ [arguments addObject:columns];
+ }
+
+ // Finally start the task with the flags we gathered.
+ NSPipe *libPipe;
+ libPipe = [NSPipe pipe];
+
+ NSTask *libTask = [NSTask new];
+ [libTask setLaunchPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"ansilove" ofType:nil]];
+ [libTask setArguments:arguments];
+ [libTask setStandardOutput:libPipe];
+
+ // Ready for launch in 3... 2... 1...
+ [libTask launch];
+
+ // Stay in limbo until rendering is finished.
+ [libTask waitUntilExit];
+
+ // Post a notification so any listener will know rendering has finished now.
+ NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
+ [nc postNotificationName:@"AnsiLoveFinishedRendering" object:self];
+}
+
++ (void)ansiFileToRetinaTIFF:(NSString *)inputFile
+ outputFile:(NSString *)outputFile
+ font:(NSString *)font
+ bits:(NSString *)bits
+ iceColors:(NSString *)iceColors
+ columns:(NSString *)columns
+{
+ if (inputFile == nil || inputFile == @"") {
+ // No inputfile? This means we can't do anything. Get the hell outta here.
+ return;
+ }
+
+ if (outputFile == nil || outputFile == @"") {
+ // In case the user provided no output file / path, just use the file name and
+ // path from the inputFile value. AnsiLove/C adds PNG suffix automatically.
+ outputFile = inputFile;
+ }
+
+ // We need to resolve any tilde in path before passing this argument to NSTask.
+ inputFile = [inputFile stringByExpandingTildeInPath];
+ outputFile = [outputFile stringByExpandingTildeInPath];
+
+ // Initialize the arguments array.
+ NSMutableArray *arguments = [NSMutableArray new];
+
+ // Option flag for generating additional @2x.PNG output with AnsiLove/C.
+ NSString *optionFlag = @"-or";
+
+ // Add the three most necessary arguments to the array.
+ [arguments addObject:inputFile];
+ [arguments addObject:optionFlag];
+ [arguments addObject:outputFile];
+
+ // The following if statements check if a string is nil or empty. That way we can
+ // be sure only strings with proper contents will be added to the arguments array.
+ if (font && ![font isEqualToString:@""]) {
+ [arguments addObject:font];
+ }
+ if (bits && ![bits isEqualToString:@""]) {
+ [arguments addObject:bits];
+ }
+ if (iceColors && ([iceColors isEqualToString:@"1"] || [iceColors isEqualToString:@"0"])) {
+ [arguments addObject:iceColors];
+ }
+ if (columns && ![columns isEqualToString:@""]) {
+ [arguments addObject:columns];
+ }
+
+ // Finally start the task with the flags we gathered.
+ NSPipe *libPipe;
+ libPipe = [NSPipe pipe];
+
+ NSTask *libTask = [NSTask new];
+ [libTask setLaunchPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"ansilove" ofType:nil]];
+ [libTask setArguments:arguments];
+ [libTask setStandardOutput:libPipe];
+
+ // Ready for launch in 3... 2... 1...
+ [libTask launch];
+
+ // Stay in limbo until rendering is finished.
+ [libTask waitUntilExit];
+
+ // Generate path / suffix strings for all files involved.
+ NSString *outputPNG = [[NSString alloc] initWithFormat:@"%@.png", outputFile];
+ NSString *retinaOutputPNG = [[NSString alloc] initWithFormat:@"%@@2x.png", outputFile];
+ NSString *retinaOutputTIFF = [[NSString alloc] initWithFormat:@"%@.tiff", outputFile];
+
+ // We use tiffutil, there are two more strings needed for this purpose.
+ NSString *dpiFlag = @"-cathidpicheck";
+ NSString *outFlag = @"-out";
+
+ // Create an array for flags we pass.
+ NSMutableArray *tiffutilFlags = [NSMutableArray new];
+
+ // This will be the hell of an argument array.
+ [tiffutilFlags addObject:dpiFlag];
+ [tiffutilFlags addObject:outputPNG];
+ [tiffutilFlags addObject:retinaOutputPNG];
+ [tiffutilFlags addObject:outFlag];
+ [tiffutilFlags addObject:retinaOutputTIFF];
+
+ // Note that tiffutil is outside any sandboxed environment.
+ NSPipe *extPipe;
+ extPipe = [NSPipe pipe];
+
+ NSTask *extTask = [NSTask new];
+ [extTask setLaunchPath:@"/usr/bin/tiffutil"];
+ [extTask setArguments:tiffutilFlags];
+ [extTask setStandardOutput:extPipe];
+
+ // OMG lasergun, phew phew!
+ [extTask launch];
+
+ // Time stands still.
+ [extTask waitUntilExit];
+
+ // Remove temporary PNG files.
+ NSFileManager *fileManager = [NSFileManager defaultManager];
+
+ if ([fileManager fileExistsAtPath:outputPNG]) {
+ [fileManager removeItemAtPath:outputPNG error:nil];
+ }
+ if ([fileManager fileExistsAtPath:retinaOutputPNG]) {
+ [fileManager removeItemAtPath:retinaOutputPNG error:nil];
+ }
// Post a notification so any listener will know rendering has finished now.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

0 comments on commit 58e9907

Please sign in to comment.