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>
23 #include "libnr/nr-matrix.h"
24 #include "libnr/nr-matrix-ops.h"
25 #include "libnr/nr-matrix-scale-ops.h"
26 #include "libnr/nr-matrix-translate-ops.h"
27 #include "libnr/nr-scale-translate-ops.h"
28 #include "libnr/nr-translate-scale-ops.h"
29 #include <libnr/nr-matrix-fns.h>
30 #include <libnr/n-art-bpath-2geom.h>
32 #include "libnr/n-art-bpath.h"
33 #include "sp-item.h"
36 #include "style.h"
38 #include "latex-pstricks.h"
40 #include <unit-constants.h>
42 #include "extension/system.h"
43 #include "extension/print.h"
45 #include "io/sys.h"
47 namespace Inkscape {
48 namespace Extension {
49 namespace Internal {
51 PrintLatex::PrintLatex (void): _stream(NULL)
52 {
53 }
55 PrintLatex::~PrintLatex (void)
56 {
57 if (_stream) fclose(_stream);
59 /* restore default signal handling for SIGPIPE */
60 #if !defined(_WIN32) && !defined(__WIN32__)
61 (void) signal(SIGPIPE, SIG_DFL);
62 #endif
63 return;
64 }
66 unsigned int
67 PrintLatex::setup (Inkscape::Extension::Print *mod)
68 {
69 return TRUE;
70 }
72 unsigned int
73 PrintLatex::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
74 {
75 Inkscape::SVGOStringStream os;
76 int res;
77 FILE *osf, *osp;
78 const gchar * fn;
80 os.setf(std::ios::fixed);
82 fn = mod->get_param_string("destination");
84 osf = NULL;
85 osp = NULL;
87 gsize bytesRead = 0;
88 gsize bytesWritten = 0;
89 GError* error = NULL;
90 gchar* local_fn = g_filename_from_utf8( fn,
91 -1, &bytesRead, &bytesWritten, &error);
92 fn = local_fn;
94 /* TODO: Replace the below fprintf's with something that does the right thing whether in
95 * gui or batch mode (e.g. --print=blah). Consider throwing an exception: currently one of
96 * the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
97 * return code.
98 */
99 if (fn != NULL) {
100 while (isspace(*fn)) fn += 1;
101 Inkscape::IO::dump_fopen_call(fn, "K");
102 osf = Inkscape::IO::fopen_utf8name(fn, "w+");
103 if (!osf) {
104 fprintf(stderr, "inkscape: fopen(%s): %s\n",
105 fn, strerror(errno));
106 return 0;
107 }
108 _stream = osf;
109 }
111 g_free(local_fn);
113 if (_stream) {
114 /* fixme: this is kinda icky */
115 #if !defined(_WIN32) && !defined(__WIN32__)
116 (void) signal(SIGPIPE, SIG_IGN);
117 #endif
118 }
120 res = fprintf(_stream, "%%LaTeX with PSTricks extensions\n");
121 /* flush this to test output stream as early as possible */
122 if (fflush(_stream)) {
123 /*g_print("caught error in sp_module_print_plain_begin\n");*/
124 if (ferror(_stream)) {
125 g_print("Error %d on output stream: %s\n", errno,
126 g_strerror(errno));
127 }
128 g_print("Printing failed\n");
129 /* fixme: should use pclose() for pipes */
130 fclose(_stream);
131 _stream = NULL;
132 fflush(stdout);
133 return 0;
134 }
136 // width and height in pt
137 _width = sp_document_width(doc) * PT_PER_PX;
138 _height = sp_document_height(doc) * PT_PER_PX;
140 if (res >= 0) {
142 os << "%%Creator: " << PACKAGE_STRING << "\n";
143 os << "%%Please note this file requires PSTricks extensions\n";
145 os << "\\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}\n";
146 // from now on we can output px, but they will be treated as pt
148 os << "\\begin{pspicture}(" << sp_document_width(doc) << "," << sp_document_height(doc) << ")\n";
149 }
151 m_tr_stack.push( NR::scale(1, -1) * NR::translate(0, sp_document_height(doc)));
153 return fprintf(_stream, "%s", os.str().c_str());
154 }
156 unsigned int
157 PrintLatex::finish (Inkscape::Extension::Print *mod)
158 {
159 int res;
161 if (!_stream) return 0;
163 res = fprintf(_stream, "\\end{pspicture}\n");
165 /* Flush stream to be sure. */
166 (void) fflush(_stream);
168 fclose(_stream);
169 _stream = NULL;
170 return 0;
171 }
173 unsigned int
174 PrintLatex::bind(Inkscape::Extension::Print *mod, NR::Matrix const *transform, float opacity)
175 {
176 NR::Matrix tr = *transform;
178 if(m_tr_stack.size()){
179 NR::Matrix tr_top = m_tr_stack.top();
180 m_tr_stack.push(tr * tr_top);
181 }else
182 m_tr_stack.push(tr);
184 return 1;
185 }
187 unsigned int
188 PrintLatex::release(Inkscape::Extension::Print *mod)
189 {
190 m_tr_stack.pop();
191 return 1;
192 }
194 unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
195 const char * comment)
196 {
197 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
199 return fprintf(_stream, "%%! %s\n",comment);
200 }
202 unsigned int
203 PrintLatex::fill(Inkscape::Extension::Print *mod,
204 Geom::PathVector const &pathv, NR::Matrix const *transform, SPStyle const *style,
205 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
206 {
207 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
209 if (style->fill.isColor()) {
210 Inkscape::SVGOStringStream os;
211 float rgb[3];
213 os.setf(std::ios::fixed);
215 sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
216 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
218 os << "\\pscustom[linestyle=none,fillstyle=solid,fillcolor=curcolor]\n{\n";
220 NArtBpath * bpath = BPath_from_2GeomPath(pathv);
221 print_bpath(os, bpath, transform);
222 g_free(bpath);
224 os << "}\n}\n";
226 fprintf(_stream, "%s", os.str().c_str());
227 }
229 return 0;
230 }
232 unsigned int
233 PrintLatex::stroke (Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, const NR::Matrix *transform, const SPStyle *style,
234 const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
235 {
236 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
238 if (style->stroke.isColor()) {
239 Inkscape::SVGOStringStream os;
240 float rgb[3];
241 NR::Matrix tr_stack = m_tr_stack.top();
242 double const scale = expansion(tr_stack);
243 os.setf(std::ios::fixed);
245 sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
246 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
248 os << "\\pscustom[linewidth=" << style->stroke_width.computed*scale<< ",linecolor=curcolor";
250 if (style->stroke_dasharray_set &&
251 style->stroke_dash.n_dash &&
252 style->stroke_dash.dash) {
253 int i;
254 os << ",linestyle=dashed,dash=";
255 for (i = 0; i < style->stroke_dash.n_dash; i++) {
256 if ((i)) {
257 os << " ";
258 }
259 os << style->stroke_dash.dash[i];
260 }
261 }
263 os <<"]\n{\n";
265 NArtBpath * bpath = BPath_from_2GeomPath(pathv);
266 print_bpath(os, bpath, transform);
267 g_free(bpath);
269 os << "}\n}\n";
271 fprintf(_stream, "%s", os.str().c_str());
272 }
274 return 0;
275 }
277 void
278 PrintLatex::print_bpath(SVGOStringStream &os, const NArtBpath *bp, const NR::Matrix *transform)
279 {
280 unsigned int closed;
281 NR::Matrix tf=*transform;
282 NR::Matrix tf_stack=m_tr_stack.top();
284 os << "\\newpath\n";
285 closed = FALSE;
286 while (bp->code != NR_END) {
287 using NR::X;
288 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);
294 NR::Point const p1(bp->c(1) * tf_stack);
295 NR::Point const p2(bp->c(2) * tf_stack);
296 NR::Point const p3(bp->c(3) * tf_stack);
298 double const x1 = p1[X], y1 = p1[Y];
299 double const x2 = p2[X], y2 = p2[Y];
300 double const x3 = p3[X], y3 = p3[Y];
302 switch (bp->code) {
303 case NR_MOVETO:
304 if (closed) {
305 os << "\\closepath\n";
306 }
307 closed = TRUE;
308 os << "\\moveto(" << x3 << "," << y3 << ")\n";
309 break;
310 case NR_MOVETO_OPEN:
311 if (closed) {
312 os << "\\closepath\n";
313 }
314 closed = FALSE;
315 os << "\\moveto(" << x3 << "," << y3 << ")\n";
316 break;
317 case NR_LINETO:
318 os << "\\lineto(" << x3 << "," << y3 << ")\n";
319 break;
320 case NR_CURVETO:
321 os << "\\curveto(" << x1 << "," << y1 << ")("
322 << x2 << "," << y2 << ")("
323 << x3 << "," << y3 << ")\n";
324 break;
325 default:
326 break;
327 }
328 bp += 1;
329 }
330 if (closed) {
331 os << "\\closepath\n";
332 }
333 }
335 bool
336 PrintLatex::textToPath(Inkscape::Extension::Print * ext)
337 {
338 return ext->get_param_bool("textToPath");
339 }
341 #include "clear-n_.h"
343 void
344 PrintLatex::init (void)
345 {
346 Inkscape::Extension::Extension * ext;
348 /* SVG in */
349 ext = Inkscape::Extension::build_from_mem(
350 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
351 "<name>" N_("LaTeX Print") "</name>\n"
352 "<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
353 "<param name=\"destination\" type=\"string\"></param>\n"
354 "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
355 "<print/>\n"
356 "</inkscape-extension>", new PrintLatex());
358 return;
359 }
361 } /* namespace Internal */
362 } /* namespace Extension */
363 } /* namespace Inkscape */
365 /*
366 Local Variables:
367 mode:c++
368 c-file-style:"stroustrup"
369 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
370 indent-tabs-mode:nil
371 fill-column:99
372 End:
373 */
374 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :