1 // --------------------------------------------------------------------
2 // iperender
3 // --------------------------------------------------------------------
4 /*
5 
6     This file is part of the extensible drawing editor Ipe.
7     Copyright (c) 1993-2020 Otfried Cheong
8 
9     Ipe is free software; you can redistribute it and/or modify it
10     under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 3 of the License, or
12     (at your option) any later version.
13 
14     As a special exception, you have permission to link Ipe with the
15     CGAL library and distribute executables, as long as you follow the
16     requirements of the Gnu General Public License in regard to all of
17     the software in the executable aside from CGAL.
18 
19     Ipe is distributed in the hope that it will be useful, but WITHOUT
20     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
22     License for more details.
23 
24     You should have received a copy of the GNU General Public License
25     along with Ipe; if not, you can find it at
26     "http://www.gnu.org/copyleft/gpl.html", or write to the Free
27     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 
29 */
30 
31 #include "ipedoc.h"
32 #include "ipethumbs.h"
33 
34 #include <cstdio>
35 #include <cstdlib>
36 #include <cstring>
37 
38 using ipe::Document;
39 using ipe::Page;
40 using ipe::Thumbnail;
41 
42 // --------------------------------------------------------------------
43 
renderPage(Thumbnail::TargetFormat fm,const char * src,const char * dst,const char * pageSpec,const char * viewSpec,double zoom,bool transparent,bool nocrop)44 static int renderPage(Thumbnail::TargetFormat fm,
45 		      const char *src, const char *dst,
46 		      const char *pageSpec, const char *viewSpec, double zoom,
47 		      bool transparent, bool nocrop)
48 {
49   Document *doc = Document::loadWithErrorReport(src);
50 
51   if (!doc)
52     return 1;
53 
54   int pageIdx = pageSpec ? doc->findPage(pageSpec) : 0;
55   if (pageIdx < 0) {
56     fprintf(stderr, "Incorrect -page specification.\n");
57     delete doc;
58     return 1;
59   }
60 
61   const Page *page = doc->page(pageIdx);
62 
63   int viewIdx = viewSpec ? page->findView(viewSpec) : 0;
64   if (viewIdx < 0) {
65     fprintf(stderr, "Incorrect -view specification.\n");
66     delete doc;
67     return 1;
68   }
69 
70   if (doc->runLatex(src)) {
71     delete doc;
72     return 1;
73   }
74 
75   Thumbnail tn(doc, 0);
76   tn.setTransparent(transparent);
77   tn.setNoCrop(nocrop);
78   if (!tn.saveRender(fm, dst, page, viewIdx, zoom))
79     fprintf(stderr, "Failure to render page.\n");
80   delete doc;
81   return 0;
82 }
83 
84 // --------------------------------------------------------------------
85 
usage()86 static void usage()
87 {
88   fprintf(stderr, "Usage: iperender [ -png ");
89 #ifdef CAIRO_HAS_PS_SURFACE
90   fprintf(stderr, "| -eps ");
91 #endif
92 #ifdef CAIRO_HAS_PDF_SURFACE
93   fprintf(stderr, "| -pdf ");
94 #endif
95 #ifdef CAIRO_HAS_SVG_SURFACE
96   fprintf(stderr, "| -svg ");
97 #endif
98   fprintf(stderr, "] "
99 	  "[ -page <page> ] [ -view <view> ] [ -resolution <dpi> ] "
100 	  "[ -transparent ] [ -nocrop ] "
101 	  "infile outfile\n"
102 	  "Iperender saves a single page of the Ipe document in some formats.\n"
103 	  " -page       : page to save (default 1).\n"
104 	  " -view       : view to save (default 1).\n"
105 	  " -resolution : resolution for png format (default 72.0 ppi).\n"
106 	  " -transparent: use transparent background in png format.\n"
107 	  " -nocrop     : do not crop page.\n"
108 	  "<page> can be a page number or a page name.\n"
109 	  );
110   exit(1);
111 }
112 
main(int argc,char * argv[])113 int main(int argc, char *argv[])
114 {
115   ipe::Platform::initLib(ipe::IPELIB_VERSION);
116 
117   // ensure at least three arguments (handles -help as well :-)
118   if (argc < 4)
119     usage();
120 
121   Thumbnail::TargetFormat fm = Thumbnail::EPNG;
122   if (!strcmp(argv[1], "-png"))
123     fm = Thumbnail::EPNG;
124 #ifdef CAIRO_HAS_PS_SURFACE
125   else if (!strcmp(argv[1], "-eps"))
126     fm = Thumbnail::EPS;
127 #endif
128 #ifdef CAIRO_HAS_PDF_SURFACE
129   else if (!strcmp(argv[1], "-pdf"))
130     fm = Thumbnail::EPDF;
131 #endif
132 #ifdef CAIRO_HAS_SVG_SURFACE
133   else if (!strcmp(argv[1], "-svg"))
134     fm = Thumbnail::ESVG;
135 #endif
136   else
137     usage();
138 
139   const char *page = nullptr;
140   const char *view = nullptr;
141   double dpi = 72.0;
142   bool transparent = false;
143   bool nocrop = false;
144 
145   int i = 2;
146   while (i < argc - 2) {
147     if (!strcmp(argv[i], "-page")) {
148       if (i + 1 == argc)
149 	usage();
150       page = argv[i+1];
151       i += 2;
152     } else if (!strcmp(argv[i], "-view")) {
153       if (i + 1 == argc)
154 	usage();
155       view = argv[i+1];
156       i += 2;
157     } else if (!strcmp(argv[i], "-resolution")) {
158       if (i + 1 == argc)
159 	usage();
160       dpi = ipe::Lex(ipe::String(argv[i+1])).getDouble();
161       i += 2;
162     } else if (!strcmp(argv[i], "-transparent")) {
163       transparent = true;
164       ++i;
165     } else if (!strcmp(argv[i], "-nocrop")) {
166       nocrop = true;
167       ++i;
168     } else
169       usage();
170   }
171 
172   // remaining arguments must be two filenames
173   const char *src = argv[i];
174   const char *dst = argv[i+1];
175 
176   return renderPage(fm, src, dst, page, view, dpi / 72.0,
177 		    transparent, nocrop);
178 }
179 
180 // --------------------------------------------------------------------
181