1 /*
2 * LaTeX Printing
3 *
4 * Author:
5 * Michael Forbes <miforbes@mbhs.edu>
6 * Abhishek Sharma
7 *
8 * Copyright (C) 2004 Authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
18 #include <signal.h>
19 #include <errno.h>
21 #include <2geom/pathvector.h>
22 #include <2geom/sbasis-to-bezier.h>
23 #include <2geom/bezier-curve.h>
24 #include <2geom/hvlinesegment.h>
25 #include "helper/geom-curves.h"
27 #include "sp-item.h"
29 #include "style.h"
31 #include "latex-pstricks.h"
33 #include <unit-constants.h>
35 #include "extension/system.h"
36 #include "extension/print.h"
38 #include "io/sys.h"
40 namespace Inkscape {
41 namespace Extension {
42 namespace Internal {
44 PrintLatex::PrintLatex (void): _stream(NULL)
45 {
46 }
48 PrintLatex::~PrintLatex (void)
49 {
50 if (_stream) fclose(_stream);
52 /* restore default signal handling for SIGPIPE */
53 #if !defined(_WIN32) && !defined(__WIN32__)
54 (void) signal(SIGPIPE, SIG_DFL);
55 #endif
56 return;
57 }
59 unsigned int
60 PrintLatex::setup (Inkscape::Extension::Print *mod)
61 {
62 return TRUE;
63 }
65 unsigned int
66 PrintLatex::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
67 {
68 Inkscape::SVGOStringStream os;
69 int res;
70 FILE *osf, *osp;
71 const gchar * fn;
73 os.setf(std::ios::fixed);
75 fn = mod->get_param_string("destination");
77 osf = NULL;
78 osp = NULL;
80 gsize bytesRead = 0;
81 gsize bytesWritten = 0;
82 GError* error = NULL;
83 gchar* local_fn = g_filename_from_utf8( fn,
84 -1, &bytesRead, &bytesWritten, &error);
85 fn = local_fn;
87 /* TODO: Replace the below fprintf's with something that does the right thing whether in
88 * gui or batch mode (e.g. --print=blah). Consider throwing an exception: currently one of
89 * the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
90 * return code.
91 */
92 if (fn != NULL) {
93 while (isspace(*fn)) fn += 1;
94 Inkscape::IO::dump_fopen_call(fn, "K");
95 osf = Inkscape::IO::fopen_utf8name(fn, "w+");
96 if (!osf) {
97 fprintf(stderr, "inkscape: fopen(%s): %s\n",
98 fn, strerror(errno));
99 return 0;
100 }
101 _stream = osf;
102 }
104 g_free(local_fn);
106 if (_stream) {
107 /* fixme: this is kinda icky */
108 #if !defined(_WIN32) && !defined(__WIN32__)
109 (void) signal(SIGPIPE, SIG_IGN);
110 #endif
111 }
113 res = fprintf(_stream, "%%LaTeX with PSTricks extensions\n");
114 /* flush this to test output stream as early as possible */
115 if (fflush(_stream)) {
116 /*g_print("caught error in sp_module_print_plain_begin\n");*/
117 if (ferror(_stream)) {
118 g_print("Error %d on output stream: %s\n", errno,
119 g_strerror(errno));
120 }
121 g_print("Printing failed\n");
122 /* fixme: should use pclose() for pipes */
123 fclose(_stream);
124 _stream = NULL;
125 fflush(stdout);
126 return 0;
127 }
129 // width and height in pt
130 _width = doc->getWidth() * PT_PER_PX;
131 _height = doc->getHeight() * PT_PER_PX;
133 if (res >= 0) {
135 os << "%%Creator: " << PACKAGE_STRING << "\n";
136 os << "%%Please note this file requires PSTricks extensions\n";
138 os << "\\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}\n";
139 // from now on we can output px, but they will be treated as pt
141 os << "\\begin{pspicture}(" << doc->getWidth() << "," << doc->getHeight() << ")\n";
142 }
144 m_tr_stack.push( Geom::Scale(1, -1) * Geom::Translate(0, doc->getHeight()));
146 return fprintf(_stream, "%s", os.str().c_str());
147 }
149 unsigned int
150 PrintLatex::finish (Inkscape::Extension::Print *mod)
151 {
152 int res;
154 if (!_stream) return 0;
156 res = fprintf(_stream, "\\end{pspicture}\n");
158 /* Flush stream to be sure. */
159 (void) fflush(_stream);
161 fclose(_stream);
162 _stream = NULL;
163 return 0;
164 }
166 unsigned int
167 PrintLatex::bind(Inkscape::Extension::Print *mod, Geom::Matrix const *transform, float opacity)
168 {
169 Geom::Matrix tr = *transform;
171 if(m_tr_stack.size()){
172 Geom::Matrix tr_top = m_tr_stack.top();
173 m_tr_stack.push(tr * tr_top);
174 }else
175 m_tr_stack.push(tr);
177 return 1;
178 }
180 unsigned int
181 PrintLatex::release(Inkscape::Extension::Print *mod)
182 {
183 m_tr_stack.pop();
184 return 1;
185 }
187 unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
188 const char * comment)
189 {
190 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
192 return fprintf(_stream, "%%! %s\n",comment);
193 }
195 unsigned int
196 PrintLatex::fill(Inkscape::Extension::Print *mod,
197 Geom::PathVector const &pathv, Geom::Matrix const *transform, SPStyle const *style,
198 NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
199 {
200 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
202 if (style->fill.isColor()) {
203 Inkscape::SVGOStringStream os;
204 float rgb[3];
205 float fill_opacity;
207 os.setf(std::ios::fixed);
209 fill_opacity=SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
210 sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
211 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
212 os << "\\pscustom[linestyle=none,fillstyle=solid,fillcolor=curcolor";
213 if (fill_opacity!=1.0) {
214 os << ",opacity="<<fill_opacity;
215 }
217 os << "]\n{\n";
219 print_pathvector(os, pathv, transform);
221 os << "}\n}\n";
223 fprintf(_stream, "%s", os.str().c_str());
224 }
226 return 0;
227 }
229 unsigned int
230 PrintLatex::stroke (Inkscape::Extension::Print *mod, Geom::PathVector const &pathv, const Geom::Matrix *transform, const SPStyle *style,
231 const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
232 {
233 if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
235 if (style->stroke.isColor()) {
236 Inkscape::SVGOStringStream os;
237 float rgb[3];
238 float stroke_opacity;
239 Geom::Matrix tr_stack = m_tr_stack.top();
240 double const scale = tr_stack.descrim();
241 os.setf(std::ios::fixed);
243 stroke_opacity=SP_SCALE24_TO_FLOAT(style->stroke_opacity.value);
244 sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
245 os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
247 os << "\\pscustom[linewidth=" << style->stroke_width.computed*scale<< ",linecolor=curcolor";
249 if (stroke_opacity!=1.0) {
250 os<<",strokeopacity="<<stroke_opacity;
251 }
253 if (style->stroke_dasharray_set &&
254 style->stroke_dash.n_dash &&
255 style->stroke_dash.dash) {
256 int i;
257 os << ",linestyle=dashed,dash=";
258 for (i = 0; i < style->stroke_dash.n_dash; i++) {
259 if ((i)) {
260 os << " ";
261 }
262 os << style->stroke_dash.dash[i];
263 }
264 }
266 os <<"]\n{\n";
268 print_pathvector(os, pathv, transform);
270 os << "}\n}\n";
272 fprintf(_stream, "%s", os.str().c_str());
273 }
275 return 0;
276 }
278 // FIXME: why is 'transform' argument not used?
279 void
280 PrintLatex::print_pathvector(SVGOStringStream &os, Geom::PathVector const &pathv_in, const Geom::Matrix * /*transform*/)
281 {
282 if (pathv_in.empty())
283 return;
285 // Geom::Matrix tf=*transform; // why was this here?
286 Geom::Matrix tf_stack=m_tr_stack.top(); // and why is transform argument not used?
287 Geom::PathVector pathv = pathv_in * tf_stack; // generates new path, which is a bit slow, but this doesn't have to be performance optimized
289 os << "\\newpath\n";
291 for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) {
293 os << "\\moveto(" << it->initialPoint()[Geom::X] << "," << it->initialPoint()[Geom::Y] << ")\n";
295 for(Geom::Path::const_iterator cit = it->begin(); cit != it->end_open(); ++cit) {
296 print_2geomcurve(os, *cit);
297 }
299 if (it->closed()) {
300 os << "\\closepath\n";
301 }
303 }
304 }
306 void
307 PrintLatex::print_2geomcurve(SVGOStringStream &os, Geom::Curve const & c )
308 {
309 using Geom::X;
310 using Geom::Y;
312 if( is_straight_curve(c) )
313 {
314 os << "\\lineto(" << c.finalPoint()[X] << "," << c.finalPoint()[Y] << ")\n";
315 }
316 else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c)) {
317 std::vector<Geom::Point> points = cubic_bezier->points();
318 os << "\\curveto(" << points[1][X] << "," << points[1][Y] << ")("
319 << points[2][X] << "," << points[2][Y] << ")("
320 << points[3][X] << "," << points[3][Y] << ")\n";
321 }
322 else {
323 //this case handles sbasis as well as all other curve types
324 Geom::Path sbasis_path = Geom::cubicbezierpath_from_sbasis(c.toSBasis(), 0.1);
326 for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter) {
327 print_2geomcurve(os, *iter);
328 }
329 }
330 }
332 bool
333 PrintLatex::textToPath(Inkscape::Extension::Print * ext)
334 {
335 return ext->get_param_bool("textToPath");
336 }
338 #include "clear-n_.h"
340 void
341 PrintLatex::init (void)
342 {
343 Inkscape::Extension::Extension * ext;
345 /* SVG in */
346 ext = Inkscape::Extension::build_from_mem(
347 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
348 "<name>" N_("LaTeX Print") "</name>\n"
349 "<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
350 "<param name=\"destination\" type=\"string\"></param>\n"
351 "<param name=\"textToPath\" type=\"boolean\">true</param>\n"
352 "<print/>\n"
353 "</inkscape-extension>", new PrintLatex());
355 return;
356 }
358 } /* namespace Internal */
359 } /* namespace Extension */
360 } /* namespace Inkscape */
362 /*
363 Local Variables:
364 mode:c++
365 c-file-style:"stroustrup"
366 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
367 indent-tabs-mode:nil
368 fill-column:99
369 End:
370 */
371 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :