1/*
2    This file is part of the LaTeX service for GNUstep
3    (http://www.roard.com/latexservice)
4    Copyright (C) 2003 Nicolas Roard (nicolas@roard.com)
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <Foundation/Foundation.h>
22#include <AppKit/AppKit.h>
23
24@interface LaTeXService : NSObject
25- (void) textToRender: (NSPasteboard*)bp
26	userData: (NSString*)ud
27	   error: (NSString**)err;
28@end
29
30@implementation LaTeXService
31
32- (void) textToRender: (NSPasteboard*)pb
33	userData: (NSString*)ud
34	   error: (NSString**)err
35{
36  NSString	*equation;
37  NSArray	*types;
38  NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
39
40  *err = nil;
41  types = [pb types];
42  if (![types containsObject: NSStringPboardType])
43    {
44      *err = @"No string type supplied on pasteboard";
45      return;
46    }
47
48  equation = [pb stringForType: NSStringPboardType];
49  if (equation == nil)
50    {
51      *err = @"No string value supplied on pasteboard";
52      return;
53    }
54  NSString* out = [equation uppercaseString];
55  types = [NSArray arrayWithObjects: NSStringPboardType, NSTIFFPboardType,nil];
56  [pb declareTypes: types owner: nil];
57  [pb setString: equation forType: NSStringPboardType];
58  NSMutableString* content = [NSMutableString stringWithString: @"\\documentclass{article}\n"];
59  [content appendString: @"\\usepackage[latin1]{inputenc}\n"];
60  [content appendString: @"\\batchmode\n"];
61  [content appendString: @"\\begin{document}\n"];
62  [content appendString: @"\\pagestyle{empty}\n"];
63  [content appendString: equation];
64  [content appendString: @"\n\\end{document}"];
65  BOOL ret = [content writeToFile: @"temp.tex" atomically: YES];
66
67  if (ret)
68  {
69  	NSTask* taskLaTeX = [NSTask launchedTaskWithLaunchPath: @"/usr/bin/tex"
70			arguments: [NSArray arrayWithObjects: @"-progname=latex",@"temp.tex",nil]];
71	[taskLaTeX waitUntilExit];
72  	NSTask* taskDVI2PNG = [NSTask launchedTaskWithLaunchPath: @"/usr/local/bin/dvipng"
73			arguments: [NSArray arrayWithObjects: @"-otemp.png",@"-Ttight",@"temp.dvi",nil]];
74	[taskDVI2PNG waitUntilExit];
75	NSTask* taskPNG2TIFF = [NSTask launchedTaskWithLaunchPath: @"/usr/bin/convert"
76			arguments: [NSArray arrayWithObjects: @"temp.png",@"temp.tiff",nil]];
77	[taskPNG2TIFF waitUntilExit];
78        NSImage* equationImage = [[NSImage alloc] initWithContentsOfFile: @"temp.tiff"];
79	[pb setData: [equationImage TIFFRepresentation] forType: NSTIFFPboardType];
80	[equationImage release];
81  }
82}
83@end
84
85int main (int argc, char** argv, char **env)
86{
87  NSAutoreleasePool *pool;
88  LaTeXService *server;
89
90#ifdef GS_PASS_ARGUMENTS
91  [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
92#endif
93  pool = [NSAutoreleasePool new];
94  server = [LaTeXService new];
95
96  if (server == nil)
97    {
98      NSLog(@"Unable to create server object.\n");
99      exit(EXIT_FAILURE);
100    }
101
102  NSRegisterServicesProvider(server, @"LaTeXService");
103
104  [[NSRunLoop currentRunLoop] run];
105
106  exit(EXIT_SUCCESS);
107}
108
109
110