Code

Fixed some further issues with Internal extensions after the introduction of schema...
[inkscape.git] / src / extension / internal / latex-pstricks.cpp
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>
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
147     
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());
156 unsigned int
157 PrintLatex::finish (Inkscape::Extension::Print *mod)
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;
173 unsigned int
174 PrintLatex::bind(Inkscape::Extension::Print *mod, NR::Matrix const *transform, float opacity)
176     NR::Matrix tr = *transform;
177     
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;
187 unsigned int
188 PrintLatex::release(Inkscape::Extension::Print *mod)
190     m_tr_stack.pop();
191     return 1;
194 unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
195                                   const char * comment)
197     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
199     return fprintf(_stream, "%%! %s\n",comment);
202 unsigned int
203 PrintLatex::fill(Inkscape::Extension::Print *mod,
204                  NRBPath const *bpath, NR::Matrix const *transform, SPStyle const *style,
205                  NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
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         print_bpath(os, bpath->path, transform);
222         os << "}\n}\n";
224         fprintf(_stream, "%s", os.str().c_str());
225     }        
227     return 0;
230 unsigned int
231 PrintLatex::stroke (Inkscape::Extension::Print *mod, const NRBPath *bpath, const NR::Matrix *transform, const SPStyle *style,
232                               const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
234     if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
236     if (style->stroke.isColor()) {
237         Inkscape::SVGOStringStream os;
238         float rgb[3];
239         NR::Matrix tr_stack = m_tr_stack.top();
240         double const scale = expansion(tr_stack);
241         os.setf(std::ios::fixed);
243         sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
244         os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
246         os << "\\pscustom[linewidth=" << style->stroke_width.computed*scale<< ",linecolor=curcolor";
248         if (style->stroke_dasharray_set &&
249                 style->stroke_dash.n_dash &&
250                 style->stroke_dash.dash) {
251             int i;
252             os << ",linestyle=dashed,dash=";
253             for (i = 0; i < style->stroke_dash.n_dash; i++) {
254                 if ((i)) {
255                     os << " ";
256                 }
257                 os << style->stroke_dash.dash[i];
258             }
259         }
261         os <<"]\n{\n";
263         print_bpath(os, bpath->path, transform);
265         os << "}\n}\n";
267         fprintf(_stream, "%s", os.str().c_str());
268     }
270     return 0;
273 void
274 PrintLatex::print_bpath(SVGOStringStream &os, const NArtBpath *bp, const NR::Matrix *transform)
276     unsigned int closed;
277     NR::Matrix tf=*transform;
278     NR::Matrix tf_stack=m_tr_stack.top();
280     os << "\\newpath\n";
281     closed = FALSE;
282     while (bp->code != NR_END) {
283         using NR::X;
284         using NR::Y;
286 //        NR::Point const p1(bp->c(1) * tf);
287 //        NR::Point const p2(bp->c(2) * tf);
288 //        NR::Point const p3(bp->c(3) * tf);
290         NR::Point const p1(bp->c(1) * tf_stack);
291         NR::Point const p2(bp->c(2) * tf_stack);
292         NR::Point const p3(bp->c(3) * tf_stack);
294         double const x1 = p1[X], y1 = p1[Y];
295         double const x2 = p2[X], y2 = p2[Y];
296         double const x3 = p3[X], y3 = p3[Y];
297         
298         switch (bp->code) {
299             case NR_MOVETO:
300                 if (closed) {
301                     os << "\\closepath\n";
302                 }
303                 closed = TRUE;
304                 os << "\\moveto(" << x3 << "," << y3 << ")\n";
305                 break;
306             case NR_MOVETO_OPEN:
307                 if (closed) {
308                     os << "\\closepath\n";
309                 }
310                 closed = FALSE;
311                 os << "\\moveto(" << x3 << "," << y3 << ")\n";
312                 break;
313             case NR_LINETO:
314                 os << "\\lineto(" << x3 << "," << y3 << ")\n";
315                 break;
316             case NR_CURVETO:
317                 os << "\\curveto(" << x1 << "," << y1 << ")("
318                    << x2 << "," << y2 << ")("
319                    << x3 << "," << y3 << ")\n";
320                 break;
321             default:
322                 break;
323         }
324         bp += 1;
325     }
326     if (closed) {
327         os << "\\closepath\n";
328     }
331 bool
332 PrintLatex::textToPath(Inkscape::Extension::Print * ext)
334     return ext->get_param_bool("textToPath");
337 #include "clear-n_.h"
339 void
340 PrintLatex::init (void)
342         Inkscape::Extension::Extension * ext;
343         
344         /* SVG in */
345     ext = Inkscape::Extension::build_from_mem(
346                 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
347                         "<name>" N_("LaTeX Print") "</name>\n"
348                         "<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
349                         "<param name=\"destination\" type=\"string\"></param>\n"
350                         "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
351                         "<print/>\n"
352                 "</inkscape-extension>", new PrintLatex());
354         return;
357 }  /* namespace Internal */
358 }  /* namespace Extension */
359 }  /* namespace Inkscape */
361 /*
362   Local Variables:
363   mode:c++
364   c-file-style:"stroustrup"
365   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
366   indent-tabs-mode:nil
367   fill-column:99
368   End:
369 */
370 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :