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 <2geom/pathvector.h>
23 #include <2geom/sbasis-to-bezier.h>
24 #include <2geom/bezier-curve.h>
25 #include <2geom/hvlinesegment.h>
26 #include "helper/geom-curves.h"
28 #include "sp-item.h"
30 #include "style.h"
32 #include "latex-pstricks.h"
34 #include <unit-constants.h>
36 #include "extension/system.h"
37 #include "extension/print.h"
39 #include "io/sys.h"
41 namespace Inkscape {
42 namespace Extension {
43 namespace Internal {
45 PrintLatex::PrintLatex (void): _stream(NULL)
46 {
47 }
49 PrintLatex::~PrintLatex (void)
50 {
51 if (_stream) fclose(_stream);
53 /* restore default signal handling for SIGPIPE */
54 #if !defined(_WIN32) && !defined(__WIN32__)
55 (void) signal(SIGPIPE, SIG_DFL);
56 #endif
57 return;
58 }
60 unsigned int
61 PrintLatex::setup (Inkscape::Extension::Print *mod)
62 {
63 return TRUE;
64 }
66 unsigned int
67 PrintLatex::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
68 {
69 Inkscape::SVGOStringStream os;
70 int res;
71 FILE *osf, *osp;
72 const gchar * fn;
74 os.setf(std::ios::fixed);
76 fn = mod->get_param_string("destination");
78 osf = NULL;
79 osp = NULL;
81 gsize bytesRead = 0;
82 gsize bytesWritten = 0;
83 GError* error = NULL;
84 gchar* local_fn = g_filename_from_utf8( fn,
85 -1, &bytesRead, &bytesWritten, &error);
86 fn = local_fn;
88 /* TODO: Replace the below fprintf's with something that does the right thing whether in
89 * gui or batch mode (e.g. --print=blah). Consider throwing an exception: currently one of
90 * the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
91 * return code.
92 */
93 if (fn != NULL) {
94 while (isspace(*fn)) fn += 1;
95 Inkscape::IO::dump_fopen_call(fn, "K");
96 osf = Inkscape::IO::fopen_utf8name(fn, "w+");
97 if (!osf) {
98 fprintf(stderr, "inkscape: fopen(%s): %s\n",
99 fn, strerror(errno));
100 return 0;
101 }
102 _stream = osf;
103 }
105 g_free(local_fn);
107 if (_stream) {
108 /* fixme: this is kinda icky */
109 #if !defined(_WIN32) && !defined(__WIN32__)
110 (void) signal(SIGPIPE, SIG_IGN);
111 #endif
112 }
114 res = fprintf(_stream, "%%LaTeX with PSTricks extensions\n");
115 /* flush this to test output stream as early as possible */
116 if (fflush(_stream)) {
117 /*g_print("caught error in sp_module_print_plain_begin\n");*/
118 if (ferror(_stream)) {
119 g_print("Error %d on output stream: %s\n", errno,
120 g_strerror(errno));
121 }
122 g_print("Printing failed\n");
123 /* fixme: should use pclose() for pipes */
124 fclose(_stream);
125 _stream = NULL;
126 fflush(stdout);
127 return 0;
128 }
130 // width and height in pt
131 _width = sp_document_width(doc) * PT_PER_PX;
132 _height = sp_document_height(doc) * PT_PER_PX;
134 if (res >= 0) {
136 os << "%%Creator: " << PACKAGE_STRING << "\n";
137 os << "%%Please note this file requires PSTricks extensions\n";
139 os << "\\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}\n";
140 // from now on we can output px, but they will be treated as pt
142 os << "\\begin{pspicture}(" << sp_document_width(doc) << "," << sp_document_height(doc) << ")\n";
143 }
145 m_tr_stack.push( Geom::Scale(1, -1) * Geom::Translate(0, sp_document_height(doc)));
147 return fprintf(_stream, "%s", os.str().c_str());
148 }
150 unsigned int
151 PrintLatex::finish (Inkscape::Extension::Print *mod)
152 {
153 int res;
155 if (!_stream) return 0;
157 res = fprintf(_stream, "\\end{pspicture}\n");
159 /* Flush stream to be sure. */
160 (void) fflush(_stream);
162 fclose(_stream);
163 _stream = NULL;
164 return 0;
165 }
167 unsigned int
168 PrintLatex::bind(Inkscape::Extension::Print *mod, Geom::Matrix const *transform, float opacity)
169 {
170 Geom::Matrix tr = *transform;
172 if(m_tr_stack.size()){
173 Geom::Matrix tr_top = m_tr_stack.top();
174 m_tr_stack.push(tr * tr_top);
175 }else
176 m_tr_stack.push(tr);
178 return 1;
179 }
181 unsigned int
182 PrintLatex::release(Inkscape::Extension::Print *mod)
183 {
184 m_tr_stack.pop();
185 return 1;
186 }
188 unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
189 const char * comment)
190 {
191 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
193 return fprintf(_stream, "%%! %s\n",comment);
194 }
196 unsigned int
197 PrintLatex::fill(Inkscape::Extension::Print *mod,
198 Geom::PathVector const &pathv, Geom::Matrix const *transform, SPStyle const *style,
199 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
200 {
201 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
203 if (style->fill.isColor()) {
204 Inkscape::SVGOStringStream os;
205 float rgb[3];
207 os.setf(std::ios::fixed);
209 sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
210 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
212 os << "\\pscustom[linestyle=none,fillstyle=solid,fillcolor=curcolor]\n{\n";
214 print_pathvector(os, pathv, transform);
216 os << "}\n}\n";
218 fprintf(_stream, "%s", os.str().c_str());
219 }
221 return 0;
222 }
224 unsigned int
225 PrintLatex::stroke (Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, const Geom::Matrix *transform, const SPStyle *style,
226 const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
227 {
228 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
230 if (style->stroke.isColor()) {
231 Inkscape::SVGOStringStream os;
232 float rgb[3];
233 Geom::Matrix tr_stack = m_tr_stack.top();
234 double const scale = tr_stack.descrim();
235 os.setf(std::ios::fixed);
237 sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
238 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
240 os << "\\pscustom[linewidth=" << style->stroke_width.computed*scale<< ",linecolor=curcolor";
242 if (style->stroke_dasharray_set &&
243 style->stroke_dash.n_dash &&
244 style->stroke_dash.dash) {
245 int i;
246 os << ",linestyle=dashed,dash=";
247 for (i = 0; i < style->stroke_dash.n_dash; i++) {
248 if ((i)) {
249 os << " ";
250 }
251 os << style->stroke_dash.dash[i];
252 }
253 }
255 os <<"]\n{\n";
257 print_pathvector(os, pathv, transform);
259 os << "}\n}\n";
261 fprintf(_stream, "%s", os.str().c_str());
262 }
264 return 0;
265 }
267 // FIXME: why is 'transform' argument not used?
268 void
269 PrintLatex::print_pathvector(SVGOStringStream &os, Geom::PathVector const &pathv_in, const Geom::Matrix * /*transform*/)
270 {
271 if (pathv_in.empty())
272 return;
274 // Geom::Matrix tf=*transform; // why was this here?
275 Geom::Matrix tf_stack=m_tr_stack.top(); // and why is transform argument not used?
276 Geom::PathVector pathv = pathv_in * tf_stack; // generates new path, which is a bit slow, but this doesn't have to be performance optimized
278 os << "\\newpath\n";
280 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
282 os << "\\moveto(" << it->initialPoint()[Geom::X] << "," << it->initialPoint()[Geom::Y] << ")\n";
284 for(Geom::Path::const_iterator cit = it->begin(); cit != it->end_open(); ++cit) {
285 print_2geomcurve(os, *cit);
286 }
288 if (it->closed()) {
289 os << "\\closepath\n";
290 }
292 }
293 }
295 void
296 PrintLatex::print_2geomcurve(SVGOStringStream &os, Geom::Curve const & c )
297 {
298 using Geom::X;
299 using Geom::Y;
301 if( is_straight_curve(c) )
302 {
303 os << "\\lineto(" << c.finalPoint()[X] << "," << c.finalPoint()[Y] << ")\n";
304 }
305 else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
306 std::vector<Geom::Point> points = cubic_bezier->points();
307 os << "\\curveto(" << points[1][X] << "," << points[1][Y] << ")("
308 << points[2][X] << "," << points[2][Y] << ")("
309 << points[3][X] << "," << points[3][Y] << ")\n";
310 }
311 else {
312 //this case handles sbasis as well as all other curve types
313 Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
315 for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
316 print_2geomcurve(os, *iter);
317 }
318 }
319 }
321 bool
322 PrintLatex::textToPath(Inkscape::Extension::Print * ext)
323 {
324 return ext->get_param_bool("textToPath");
325 }
327 #include "clear-n_.h"
329 void
330 PrintLatex::init (void)
331 {
332 Inkscape::Extension::Extension * ext;
334 /* SVG in */
335 ext = Inkscape::Extension::build_from_mem(
336 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
337 "<name>" N_("LaTeX Print") "</name>\n"
338 "<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
339 "<param name=\"destination\" type=\"string\"></param>\n"
340 "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
341 "<print/>\n"
342 "</inkscape-extension>", new PrintLatex());
344 return;
345 }
347 } /* namespace Internal */
348 } /* namespace Extension */
349 } /* namespace Inkscape */
351 /*
352 Local Variables:
353 mode:c++
354 c-file-style:"stroustrup"
355 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
356 indent-tabs-mode:nil
357 fill-column:99
358 End:
359 */
360 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :