Back to index

texmacs  1.0.7.15
mac_images.mm
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : mac_images.mm
00004 * DESCRIPTION: interface with the MacOSX image conversion facilities
00005 * COPYRIGHT  : (C) 2009  Massimiliano Gubinelli
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "MacOS/mac_images.h"
00013 #include "converter.hpp" // hack: remove as soon as possible
00014 
00015 #include "Cocoa/mac_cocoa.h"
00016 #include "ApplicationServices/ApplicationServices.h"
00017 
00018 static NSString *
00019 to_nsstring_utf8 (string s) {
00020   s= cork_to_utf8 (s);
00021   char *p = as_charp(s);
00022   NSString *nss = [NSString stringWithCString:p encoding:NSUTF8StringEncoding];
00023   tm_delete_array (p);      
00024   return nss;
00025 }
00026 
00027 void mac_image_to_png (url img_file, url png_file, int w, int h) {
00028   // we need to be sure that the Cocoa application infrastructure is initialized 
00029   // (apparently Qt does not do this properly)
00030   NSApplication *NSApp=[NSApplication sharedApplication]; (void) NSApp;
00031   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00032 
00033   NSImage *image = [[NSImage alloc] initWithContentsOfFile: to_nsstring_utf8 ( concretize (img_file) )];
00034   NSSize size = NSMakeSize(w,h);
00035   [image setSize: size];
00036   [image lockFocus];
00037   NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc] initWithFocusedViewRect:
00038                           NSMakeRect(0,0,size.width,size.height)];
00039   [image unlockFocus];
00040   [image release];
00041   NSData *png_data = [bmp representationUsingType: NSPNGFileType properties: nil ];
00042   [png_data writeToURL:[NSURL fileURLWithPath: to_nsstring_utf8 ( concretize (png_file))] atomically: YES];
00043   [bmp release];
00044   [pool release];
00045 } 
00046 
00047 bool mac_image_size (url img_file, int& w, int& h) 
00048 {
00049   string suf= suffix (img_file);
00050   if (suf == "ps" || suf == "eps" || suf == "pdf") return false;
00051 
00052   bool res = false; 
00053   // we need to be sure that the Cocoa application infrastructure is initialized 
00054   // (apparently Qt does not do this properly)
00055   NSApplication *NSApp=[NSApplication sharedApplication]; (void) NSApp;
00056   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00057   
00058   NSImage *image = [[NSImage alloc] initWithContentsOfFile: to_nsstring_utf8 ( concretize (img_file) )];
00059   if (image) {
00060     NSSize size = [image size];
00061     [image release];
00062     //NSLog(@"Probing  image size %f %f.\n", size.width, size.height);
00063     w = size.width;
00064     h = size.height;
00065     res = true;
00066   }
00067   [pool release];
00068   return res;
00069 }
00070 
00071 bool mac_supports (url img_file) {
00072   int w, h;
00073   return mac_image_size (img_file, w, h);
00074 }
00075 
00076 void mac_ps_to_pdf (url ps_file, url pdf_file) 
00077 {
00078   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00079   NSString *inpath = to_nsstring_utf8 ( concretize (ps_file) );
00080   NSString *outpath = to_nsstring_utf8 ( concretize (pdf_file) );
00081   NSURL *inurl = [NSURL fileURLWithPath:inpath];
00082   NSURL *outurl = [NSURL fileURLWithPath: outpath];
00083   
00084   CGPSConverterCallbacks callbacks = {
00085     0, // unsigned int version;
00086     nil, // CGPSConverterBeginDocumentCallback beginDocument;
00087     nil, // CGPSConverterEndDocumentCallback endDocument;
00088     nil, // CGPSConverterBeginPageCallback beginPage;
00089     nil, // CGPSConverterEndPageCallback endPage;
00090     nil, // CGPSConverterProgressCallback noteProgress;
00091     nil, // CGPSConverterMessageCallback noteMessage;
00092     nil  // CGPSConverterReleaseInfoCallback releaseInfo;
00093   };
00094   
00095   CGPSConverterRef converter = CGPSConverterCreate (NULL,&callbacks,NULL);  
00096   CGDataProviderRef provider = CGDataProviderCreateWithURL ((CFURLRef)inurl);
00097   CGDataConsumerRef consumer = CGDataConsumerCreateWithURL ((CFURLRef)outurl);
00098   
00099   BOOL converted = CGPSConverterConvert (converter,provider,consumer,NULL);
00100   
00101   if (converted) {
00102     NSLog(@"Postscript file converted.\n");
00103     //CGDataConsumerRetain(consumer);
00104   } else {
00105     NSLog(@"Converting postscript failed.\n");
00106   }
00107   
00108   CGDataProviderRelease (provider);
00109   CGDataConsumerRelease (consumer);
00110   CFRelease (converter);
00111   
00112   [pool release];
00113 }
00114