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];
206 float fill_opacity;
208 os.setf(std::ios::fixed);
210 fill_opacity=SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
211 sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
212 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
213 os << "\\pscustom[linestyle=none,fillstyle=solid,fillcolor=curcolor";
214 if (fill_opacity!=1.0) {
215 os << ",opacity="<<fill_opacity;
216 }
218 os << "]\n{\n";
220 print_pathvector(os, pathv, transform);
222 os << "}\n}\n";
224 fprintf(_stream, "%s", os.str().c_str());
225 }
227 return 0;
228 }
230 unsigned int
231 PrintLatex::stroke (Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, const Geom::Matrix *transform, const SPStyle *style,
232 const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
233 {
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 float stroke_opacity;
240 Geom::Matrix tr_stack = m_tr_stack.top();
241 double const scale = tr_stack.descrim();
242 os.setf(std::ios::fixed);
244 stroke_opacity=SP_SCALE24_TO_FLOAT(style->stroke_opacity.value);
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 (stroke_opacity!=1.0) {
251 os<<",strokeopacity="<<stroke_opacity;
252 }
254 if (style->stroke_dasharray_set &&
255 style->stroke_dash.n_dash &&
256 style->stroke_dash.dash) {
257 int i;
258 os << ",linestyle=dashed,dash=";
259 for (i = 0; i < style->stroke_dash.n_dash; i++) {
260 if ((i)) {
261 os << " ";
262 }
263 os << style->stroke_dash.dash[i];
264 }
265 }
267 os <<"]\n{\n";
269 print_pathvector(os, pathv, transform);
271 os << "}\n}\n";
273 fprintf(_stream, "%s", os.str().c_str());
274 }
276 return 0;
277 }
279 // FIXME: why is 'transform' argument not used?
280 void
281 PrintLatex::print_pathvector(SVGOStringStream &os, Geom::PathVector const &pathv_in, const Geom::Matrix * /*transform*/)
282 {
283 if (pathv_in.empty())
284 return;
286 // Geom::Matrix tf=*transform; // why was this here?
287 Geom::Matrix tf_stack=m_tr_stack.top(); // and why is transform argument not used?
288 Geom::PathVector pathv = pathv_in * tf_stack; // generates new path, which is a bit slow, but this doesn't have to be performance optimized
290 os << "\\newpath\n";
292 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
294 os << "\\moveto(" << it->initialPoint()[Geom::X] << "," << it->initialPoint()[Geom::Y] << ")\n";
296 for(Geom::Path::const_iterator cit = it->begin(); cit != it->end_open(); ++cit) {
297 print_2geomcurve(os, *cit);
298 }
300 if (it->closed()) {
301 os << "\\closepath\n";
302 }
304 }
305 }
307 void
308 PrintLatex::print_2geomcurve(SVGOStringStream &os, Geom::Curve const & c )
309 {
310 using Geom::X;
311 using Geom::Y;
313 if( is_straight_curve(c) )
314 {
315 os << "\\lineto(" << c.finalPoint()[X] << "," << c.finalPoint()[Y] << ")\n";
316 }
317 else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
318 std::vector<Geom::Point> points = cubic_bezier->points();
319 os << "\\curveto(" << points[1][X] << "," << points[1][Y] << ")("
320 << points[2][X] << "," << points[2][Y] << ")("
321 << points[3][X] << "," << points[3][Y] << ")\n";
322 }
323 else {
324 //this case handles sbasis as well as all other curve types
325 Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
327 for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
328 print_2geomcurve(os, *iter);
329 }
330 }
331 }
333 bool
334 PrintLatex::textToPath(Inkscape::Extension::Print * ext)
335 {
336 return ext->get_param_bool("textToPath");
337 }
339 #include "clear-n_.h"
341 void
342 PrintLatex::init (void)
343 {
344 Inkscape::Extension::Extension * ext;
346 /* SVG in */
347 ext = Inkscape::Extension::build_from_mem(
348 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
349 "<name>" N_("LaTeX Print") "</name>\n"
350 "<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
351 "<param name=\"destination\" type=\"string\"></param>\n"
352 "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
353 "<print/>\n"
354 "</inkscape-extension>", new PrintLatex());
356 return;
357 }
359 } /* namespace Internal */
360 } /* namespace Extension */
361 } /* namespace Inkscape */
363 /*
364 Local Variables:
365 mode:c++
366 c-file-style:"stroustrup"
367 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
368 indent-tabs-mode:nil
369 fill-column:99
370 End:
371 */
372 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :