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 <2geom/pathvector.h>
31 #include <2geom/sbasis-to-bezier.h>
32 #include <2geom/bezier-curve.h>
33 #include <2geom/hvlinesegment.h>
34 #include "helper/geom-curves.h"
36 #include "sp-item.h"
38 #include "style.h"
40 #include "latex-pstricks.h"
42 #include <unit-constants.h>
44 #include "extension/system.h"
45 #include "extension/print.h"
47 #include "io/sys.h"
49 namespace Inkscape {
50 namespace Extension {
51 namespace Internal {
53 PrintLatex::PrintLatex (void): _stream(NULL)
54 {
55 }
57 PrintLatex::~PrintLatex (void)
58 {
59 if (_stream) fclose(_stream);
61 /* restore default signal handling for SIGPIPE */
62 #if !defined(_WIN32) && !defined(__WIN32__)
63 (void) signal(SIGPIPE, SIG_DFL);
64 #endif
65 return;
66 }
68 unsigned int
69 PrintLatex::setup (Inkscape::Extension::Print *mod)
70 {
71 return TRUE;
72 }
74 unsigned int
75 PrintLatex::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
76 {
77 Inkscape::SVGOStringStream os;
78 int res;
79 FILE *osf, *osp;
80 const gchar * fn;
82 os.setf(std::ios::fixed);
84 fn = mod->get_param_string("destination");
86 osf = NULL;
87 osp = NULL;
89 gsize bytesRead = 0;
90 gsize bytesWritten = 0;
91 GError* error = NULL;
92 gchar* local_fn = g_filename_from_utf8( fn,
93 -1, &bytesRead, &bytesWritten, &error);
94 fn = local_fn;
96 /* TODO: Replace the below fprintf's with something that does the right thing whether in
97 * gui or batch mode (e.g. --print=blah). Consider throwing an exception: currently one of
98 * the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
99 * return code.
100 */
101 if (fn != NULL) {
102 while (isspace(*fn)) fn += 1;
103 Inkscape::IO::dump_fopen_call(fn, "K");
104 osf = Inkscape::IO::fopen_utf8name(fn, "w+");
105 if (!osf) {
106 fprintf(stderr, "inkscape: fopen(%s): %s\n",
107 fn, strerror(errno));
108 return 0;
109 }
110 _stream = osf;
111 }
113 g_free(local_fn);
115 if (_stream) {
116 /* fixme: this is kinda icky */
117 #if !defined(_WIN32) && !defined(__WIN32__)
118 (void) signal(SIGPIPE, SIG_IGN);
119 #endif
120 }
122 res = fprintf(_stream, "%%LaTeX with PSTricks extensions\n");
123 /* flush this to test output stream as early as possible */
124 if (fflush(_stream)) {
125 /*g_print("caught error in sp_module_print_plain_begin\n");*/
126 if (ferror(_stream)) {
127 g_print("Error %d on output stream: %s\n", errno,
128 g_strerror(errno));
129 }
130 g_print("Printing failed\n");
131 /* fixme: should use pclose() for pipes */
132 fclose(_stream);
133 _stream = NULL;
134 fflush(stdout);
135 return 0;
136 }
138 // width and height in pt
139 _width = sp_document_width(doc) * PT_PER_PX;
140 _height = sp_document_height(doc) * PT_PER_PX;
142 if (res >= 0) {
144 os << "%%Creator: " << PACKAGE_STRING << "\n";
145 os << "%%Please note this file requires PSTricks extensions\n";
147 os << "\\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}\n";
148 // from now on we can output px, but they will be treated as pt
150 os << "\\begin{pspicture}(" << sp_document_width(doc) << "," << sp_document_height(doc) << ")\n";
151 }
153 m_tr_stack.push( Geom::Scale(1, -1) * Geom::Translate(0, sp_document_height(doc)));
155 return fprintf(_stream, "%s", os.str().c_str());
156 }
158 unsigned int
159 PrintLatex::finish (Inkscape::Extension::Print *mod)
160 {
161 int res;
163 if (!_stream) return 0;
165 res = fprintf(_stream, "\\end{pspicture}\n");
167 /* Flush stream to be sure. */
168 (void) fflush(_stream);
170 fclose(_stream);
171 _stream = NULL;
172 return 0;
173 }
175 unsigned int
176 PrintLatex::bind(Inkscape::Extension::Print *mod, Geom::Matrix const *transform, float opacity)
177 {
178 Geom::Matrix tr = *transform;
180 if(m_tr_stack.size()){
181 Geom::Matrix tr_top = m_tr_stack.top();
182 m_tr_stack.push(tr * tr_top);
183 }else
184 m_tr_stack.push(tr);
186 return 1;
187 }
189 unsigned int
190 PrintLatex::release(Inkscape::Extension::Print *mod)
191 {
192 m_tr_stack.pop();
193 return 1;
194 }
196 unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
197 const char * comment)
198 {
199 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
201 return fprintf(_stream, "%%! %s\n",comment);
202 }
204 unsigned int
205 PrintLatex::fill(Inkscape::Extension::Print *mod,
206 Geom::PathVector const &pathv, Geom::Matrix const *transform, SPStyle const *style,
207 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
208 {
209 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
211 if (style->fill.isColor()) {
212 Inkscape::SVGOStringStream os;
213 float rgb[3];
215 os.setf(std::ios::fixed);
217 sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
218 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
220 os << "\\pscustom[linestyle=none,fillstyle=solid,fillcolor=curcolor]\n{\n";
222 print_pathvector(os, pathv, transform);
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 Geom::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 Geom::Matrix tr_stack = m_tr_stack.top();
242 double const scale = tr_stack.descrim();
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 print_pathvector(os, pathv, transform);
267 os << "}\n}\n";
269 fprintf(_stream, "%s", os.str().c_str());
270 }
272 return 0;
273 }
275 // FIXME: why is 'transform' argument not used?
276 void
277 PrintLatex::print_pathvector(SVGOStringStream &os, Geom::PathVector const &pathv_in, const Geom::Matrix * /*transform*/)
278 {
279 if (pathv_in.empty())
280 return;
282 // Geom::Matrix tf=*transform; // why was this here?
283 Geom::Matrix tf_stack=m_tr_stack.top(); // and why is transform argument not used?
284 Geom::PathVector pathv = pathv_in * tf_stack; // generates new path, which is a bit slow, but this doesn't have to be performance optimized
286 os << "\\newpath\n";
288 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
290 os << "\\moveto(" << it->initialPoint()[Geom::X] << "," << it->initialPoint()[Geom::Y] << ")\n";
292 for(Geom::Path::const_iterator cit = it->begin(); cit != it->end_open(); ++cit) {
293 print_2geomcurve(os, *cit);
294 }
296 if (it->closed()) {
297 os << "\\closepath\n";
298 }
300 }
301 }
303 void
304 PrintLatex::print_2geomcurve(SVGOStringStream &os, Geom::Curve const & c )
305 {
306 using Geom::X;
307 using Geom::Y;
309 if( is_straight_curve(c) )
310 {
311 os << "\\lineto(" << c.finalPoint()[X] << "," << c.finalPoint()[Y] << ")\n";
312 }
313 else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
314 std::vector<Geom::Point> points = cubic_bezier->points();
315 os << "\\curveto(" << points[1][X] << "," << points[1][Y] << ")("
316 << points[2][X] << "," << points[2][Y] << ")("
317 << points[3][X] << "," << points[3][Y] << ")\n";
318 }
319 else {
320 //this case handles sbasis as well as all other curve types
321 Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
323 for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
324 print_2geomcurve(os, *iter);
325 }
326 }
327 }
329 bool
330 PrintLatex::textToPath(Inkscape::Extension::Print * ext)
331 {
332 return ext->get_param_bool("textToPath");
333 }
335 #include "clear-n_.h"
337 void
338 PrintLatex::init (void)
339 {
340 Inkscape::Extension::Extension * ext;
342 /* SVG in */
343 ext = Inkscape::Extension::build_from_mem(
344 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
345 "<name>" N_("LaTeX Print") "</name>\n"
346 "<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
347 "<param name=\"destination\" type=\"string\"></param>\n"
348 "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
349 "<print/>\n"
350 "</inkscape-extension>", new PrintLatex());
352 return;
353 }
355 } /* namespace Internal */
356 } /* namespace Extension */
357 } /* namespace Inkscape */
359 /*
360 Local Variables:
361 mode:c++
362 c-file-style:"stroustrup"
363 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
364 indent-tabs-mode:nil
365 fill-column:99
366 End:
367 */
368 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :