1 #define __SP_LATEX_C__
3 /*
4 * LaTeX Printing
5 *
6 * Author:
7 * Michael Forbes <miforbes@mbhs.edu>
8 *
9 * Copyright (C) 2004 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
19 #include <signal.h>
20 #include <errno.h>
22 #include "libnr/n-art-bpath.h"
23 #include "sp-item.h"
26 #include "style.h"
28 #include "latex-pstricks.h"
30 #include <unit-constants.h>
32 #include "extension/system.h"
33 #include "extension/print.h"
35 #include "io/sys.h"
37 namespace Inkscape {
38 namespace Extension {
39 namespace Internal {
41 PrintLatex::PrintLatex (void): _stream(NULL)
42 {
43 }
45 PrintLatex::~PrintLatex (void)
46 {
47 if (_stream) fclose(_stream);
49 /* restore default signal handling for SIGPIPE */
50 #if !defined(_WIN32) && !defined(__WIN32__)
51 (void) signal(SIGPIPE, SIG_DFL);
52 #endif
53 return;
54 }
56 unsigned int
57 PrintLatex::setup (Inkscape::Extension::Print *mod)
58 {
59 return TRUE;
60 }
62 unsigned int
63 PrintLatex::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
64 {
65 Inkscape::SVGOStringStream os;
66 int res;
67 FILE *osf, *osp;
68 const gchar * fn;
70 os.setf(std::ios::fixed);
72 fn = mod->get_param_string("destination");
74 osf = NULL;
75 osp = NULL;
77 gsize bytesRead = 0;
78 gsize bytesWritten = 0;
79 GError* error = NULL;
80 gchar* local_fn = g_filename_from_utf8( fn,
81 -1, &bytesRead, &bytesWritten, &error);
82 fn = local_fn;
84 /* TODO: Replace the below fprintf's with something that does the right thing whether in
85 * gui or batch mode (e.g. --print=blah). Consider throwing an exception: currently one of
86 * the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
87 * return code.
88 */
89 if (fn != NULL) {
90 while (isspace(*fn)) fn += 1;
91 Inkscape::IO::dump_fopen_call(fn, "K");
92 osf = Inkscape::IO::fopen_utf8name(fn, "w+");
93 if (!osf) {
94 fprintf(stderr, "inkscape: fopen(%s): %s\n",
95 fn, strerror(errno));
96 return 0;
97 }
98 _stream = osf;
99 }
101 g_free(local_fn);
103 if (_stream) {
104 /* fixme: this is kinda icky */
105 #if !defined(_WIN32) && !defined(__WIN32__)
106 (void) signal(SIGPIPE, SIG_IGN);
107 #endif
108 }
110 res = fprintf(_stream, "%%LaTeX with PSTricks extensions\n");
111 /* flush this to test output stream as early as possible */
112 if (fflush(_stream)) {
113 /*g_print("caught error in sp_module_print_plain_begin\n");*/
114 if (ferror(_stream)) {
115 g_print("Error %d on output stream: %s\n", errno,
116 g_strerror(errno));
117 }
118 g_print("Printing failed\n");
119 /* fixme: should use pclose() for pipes */
120 fclose(_stream);
121 _stream = NULL;
122 fflush(stdout);
123 return 0;
124 }
126 // width and height in pt
127 _width = sp_document_width(doc) * PT_PER_PX;
128 _height = sp_document_height(doc) * PT_PER_PX;
130 NRRect d;
131 bool pageLandscape;
132 // printf("Page Bounding Box: %s\n", pageBoundingBox ? "TRUE" : "FALSE");
133 /* if (pageBoundingBox) {
134 d.x0 = d.y0 = 0;
135 d.x1 = ceil(_width);
136 d.y1 = ceil(_height);
137 } else */{ // no bounding boxes for now
138 SPItem* doc_item = SP_ITEM(sp_document_root(doc));
139 sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
140 // convert from px to pt
141 d.x0 *= PT_PER_PX;
142 d.x1 *= PT_PER_PX;
143 d.y0 *= PT_PER_PX;
144 d.y1 *= PT_PER_PX;
145 }
148 if (res >= 0) {
150 os << "%%Creator: " << PACKAGE_STRING << "\n";
151 os << "%%Please note this file requires PSTricks extensions\n";
153 // 2004 Dec 10, BFC:
154 // The point of the following code is (1) to do the thing that's expected by users
155 // who have done File>New>A4_landscape or ...letter_landscape (i.e., rotate
156 // the output), while (2) not messing up users who simply want their output wider
157 // than it is tall (e.g., small figures for inclusion in LaTeX).
158 // The original patch by WQ only had the w>h condition.
159 {
160 double w = (d.x1 - d.x0); // width and height of bounding box, in pt
161 double h = (d.y1 - d.y0);
162 pageLandscape = (
163 (w > 0. && h > 0.) // empty documents fail this sanity check, have w<0, h<0
164 && (w > h) // implies, but does not prove, the user wanted landscape
165 && (w > 600) // approximate maximum printable width of an A4
166 )
167 ? true : false;
168 }
170 if (pageLandscape) {
171 os << "\\rotate{90}\n";
172 }
174 os << "\\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}\n";
175 // from now on we can output px, but they will be treated as pt
177 os << "\\begin{pspicture}(" << sp_document_width(doc) << "," << sp_document_height(doc) << ")\n";
178 }
180 return fprintf(_stream, "%s", os.str().c_str());
181 }
183 unsigned int
184 PrintLatex::finish (Inkscape::Extension::Print *mod)
185 {
186 int res;
188 if (!_stream) return 0;
190 res = fprintf(_stream, "\\end{pspicture}\n");
192 /* Flush stream to be sure. */
193 (void) fflush(_stream);
195 fclose(_stream);
196 _stream = NULL;
197 return 0;
198 }
200 unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
201 const char * comment)
202 {
203 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
205 return fprintf(_stream, "%%! %s\n",comment);
206 }
208 unsigned int
209 PrintLatex::fill(Inkscape::Extension::Print *mod,
210 NRBPath const *bpath, NRMatrix const *transform, SPStyle const *style,
211 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
212 {
213 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
215 if (style->fill.type == SP_PAINT_TYPE_COLOR) {
216 Inkscape::SVGOStringStream os;
217 float rgb[3];
219 os.setf(std::ios::fixed);
221 sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
222 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
224 os << "\\pscustom[fillstyle=solid,fillcolor=curcolor]\n{\n";
226 print_bpath(os, bpath->path, transform);
228 os << "}\n}\n";
230 fprintf(_stream, "%s", os.str().c_str());
231 }
233 return 0;
234 }
236 unsigned int
237 PrintLatex::stroke (Inkscape::Extension::Print *mod, const NRBPath *bpath, const NRMatrix *transform, const SPStyle *style,
238 const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
239 {
240 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
242 if (style->stroke.type == SP_PAINT_TYPE_COLOR) {
243 Inkscape::SVGOStringStream os;
244 float rgb[3];
246 os.setf(std::ios::fixed);
248 sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
249 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
251 os << "\\pscustom[linewidth=" << style->stroke_width.computed<< ",linecolor=curcolor";
253 if (style->stroke_dasharray_set &&
254 style->stroke_dash.n_dash &&
255 style->stroke_dash.dash) {
256 int i;
257 os << ",linestyle=dashed,dash=";
258 for (i = 0; i < style->stroke_dash.n_dash; i++) {
259 if ((i)) {
260 os << " ";
261 }
262 os << style->stroke_dash.dash[i];
263 }
264 }
266 os <<"]\n{\n";
268 print_bpath(os, bpath->path, transform);
270 os << "}\n}\n";
272 fprintf(_stream, "%s", os.str().c_str());
273 }
275 return 0;
276 }
278 void
279 PrintLatex::print_bpath(SVGOStringStream &os, const NArtBpath *bp, const NRMatrix *transform)
280 {
281 unsigned int closed;
282 NR::Matrix tf=*transform;
285 os << "\\newpath\n";
286 closed = FALSE;
287 while (bp->code != NR_END) {
288 using NR::X;
289 using NR::Y;
290 NR::Point const p1(bp->c(1) * tf);
291 NR::Point const p2(bp->c(2) * tf);
292 NR::Point const p3(bp->c(3) * tf);
293 double const x1 = p1[X], y1 = p1[Y];
294 double const x2 = p2[X], y2 = p2[Y];
295 double const x3 = p3[X], y3 = p3[Y];
297 switch (bp->code) {
298 case NR_MOVETO:
299 if (closed) {
300 os << "\\closepath\n";
301 }
302 closed = TRUE;
303 os << "\\moveto(" << x3 << "," << y3 << ")\n";
304 break;
305 case NR_MOVETO_OPEN:
306 if (closed) {
307 os << "\\closepath\n";
308 }
309 closed = FALSE;
310 os << "\\moveto(" << x3 << "," << y3 << ")\n";
311 break;
312 case NR_LINETO:
313 os << "\\lineto(" << x3 << "," << y3 << ")\n";
314 break;
315 case NR_CURVETO:
316 os << "\\curveto(" << x1 << "," << y1 << ")("
317 << x2 << "," << y2 << ")("
318 << x3 << "," << y3 << ")\n";
319 break;
320 default:
321 break;
322 }
323 bp += 1;
324 }
325 if (closed) {
326 os << "\\closepath\n";
327 }
328 }
330 bool
331 PrintLatex::textToPath(Inkscape::Extension::Print * ext)
332 {
333 return ext->get_param_bool("textToPath");
334 }
336 #include "clear-n_.h"
338 void
339 PrintLatex::init (void)
340 {
341 Inkscape::Extension::Extension * ext;
343 /* SVG in */
344 ext = Inkscape::Extension::build_from_mem(
345 "<inkscape-extension>\n"
346 "<name>" N_("LaTeX Print") "</name>\n"
347 "<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
348 "<param name=\"destination\" type=\"string\"></param>\n"
349 "<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"
350 "<print/>\n"
351 "</inkscape-extension>", new PrintLatex());
353 return;
354 }
356 } /* namespace Internal */
357 } /* namespace Extension */
358 } /* namespace Inkscape */
360 /*
361 Local Variables:
362 mode:c++
363 c-file-style:"stroustrup"
364 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
365 indent-tabs-mode:nil
366 fill-column:99
367 End:
368 */
369 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :