7
* Michael Forbes <miforbes@mbhs.edu>
9
* Copyright (C) 2004 Authors
11
* Released under GNU GPL, read the file 'COPYING' for more information
22
#include "libnr/n-art-bpath.h"
28
#include "latex-pstricks.h"
30
#include <unit-constants.h>
32
#include "extension/system.h"
33
#include "extension/print.h"
41
PrintLatex::PrintLatex (void): _stream(NULL)
45
PrintLatex::~PrintLatex (void)
47
if (_stream) fclose(_stream);
49
/* restore default signal handling for SIGPIPE */
50
#if !defined(_WIN32) && !defined(__WIN32__)
51
(void) signal(SIGPIPE, SIG_DFL);
57
PrintLatex::setup (Inkscape::Extension::Print *mod)
63
PrintLatex::begin (Inkscape::Extension::Print *mod, SPDocument *doc)
65
Inkscape::SVGOStringStream os;
70
fn = mod->get_param_string("destination");
76
gsize bytesWritten = 0;
78
gchar* local_fn = g_filename_from_utf8( fn,
79
-1, &bytesRead, &bytesWritten, &error);
82
/* TODO: Replace the below fprintf's with something that does the right thing whether in
83
* gui or batch mode (e.g. --print=blah). Consider throwing an exception: currently one of
84
* the callers (sp_print_document_to_file, "ret = mod->begin(doc)") wrongly ignores the
88
while (isspace(*fn)) fn += 1;
89
Inkscape::IO::dump_fopen_call(fn, "K");
90
osf = Inkscape::IO::fopen_utf8name(fn, "w+");
92
fprintf(stderr, "inkscape: fopen(%s): %s\n",
102
/* fixme: this is kinda icky */
103
#if !defined(_WIN32) && !defined(__WIN32__)
104
(void) signal(SIGPIPE, SIG_IGN);
108
res = fprintf(_stream, "%%LaTeX with PSTricks extensions\n");
109
/* flush this to test output stream as early as possible */
110
if (fflush(_stream)) {
111
/*g_print("caught error in sp_module_print_plain_begin\n");*/
112
if (ferror(_stream)) {
113
g_print("Error %d on output stream: %s\n", errno,
116
g_print("Printing failed\n");
117
/* fixme: should use pclose() for pipes */
124
// width and height in pt
125
_width = sp_document_width(doc) * PT_PER_PX;
126
_height = sp_document_height(doc) * PT_PER_PX;
130
// printf("Page Bounding Box: %s\n", pageBoundingBox ? "TRUE" : "FALSE");
131
/* if (pageBoundingBox) {
134
d.y1 = ceil(_height);
135
} else */{ // no bounding boxes for now
136
SPItem* doc_item = SP_ITEM(sp_document_root(doc));
137
sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
138
// convert from px to pt
148
os << "%%Creator: " << PACKAGE_STRING << "\n";
149
os << "%%Please note this file requires PSTricks extensions\n";
152
// The point of the following code is (1) to do the thing that's expected by users
153
// who have done File>New>A4_landscape or ...letter_landscape (i.e., rotate
154
// the output), while (2) not messing up users who simply want their output wider
155
// than it is tall (e.g., small figures for inclusion in LaTeX).
156
// The original patch by WQ only had the w>h condition.
158
double w = (d.x1 - d.x0); // width and height of bounding box, in pt
159
double h = (d.y1 - d.y0);
161
(w > 0. && h > 0.) // empty documents fail this sanity check, have w<0, h<0
162
&& (w > h) // implies, but does not prove, the user wanted landscape
163
&& (w > 600) // approximate maximum printable width of an A4
169
os << "\\rotate{90}\n";
172
os << "\\psset{xunit=.5pt,yunit=.5pt,runit=.5pt}\n";
173
// from now on we can output px, but they will be treated as pt
175
os << "\\begin{pspicture}(" << sp_document_width(doc) << "," << sp_document_height(doc) << ")\n";
178
return fprintf(_stream, "%s", os.str().c_str());
182
PrintLatex::finish (Inkscape::Extension::Print *mod)
186
if (!_stream) return 0;
188
res = fprintf(_stream, "\\end{pspicture}\n");
190
/* Flush stream to be sure. */
191
(void) fflush(_stream);
198
unsigned int PrintLatex::comment (Inkscape::Extension::Print * module,
199
const char * comment)
201
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
203
return fprintf(_stream, "%%! %s\n",comment);
207
PrintLatex::fill(Inkscape::Extension::Print *mod,
208
NRBPath const *bpath, NRMatrix const *transform, SPStyle const *style,
209
NRRect const *pbox, NRRect const *dbox, NRRect const *bbox)
211
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
213
if (style->fill.type == SP_PAINT_TYPE_COLOR) {
214
Inkscape::SVGOStringStream os;
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[fillstyle=solid,fillcolor=curcolor]\n{\n";
222
print_bpath(os, bpath->path, transform);
226
fprintf(_stream, "%s", os.str().c_str());
233
PrintLatex::stroke (Inkscape::Extension::Print *mod, const NRBPath *bpath, const NRMatrix *transform, const SPStyle *style,
234
const NRRect *pbox, const NRRect *dbox, const NRRect *bbox)
236
if (!_stream) return 0; // XXX: fixme, returning -1 as unsigned.
238
if (style->stroke.type == SP_PAINT_TYPE_COLOR) {
239
Inkscape::SVGOStringStream os;
242
sp_color_get_rgb_floatv(&style->stroke.value.color, rgb);
243
os << "{\n\\newrgbcolor{curcolor}{" << rgb[0] << " " << rgb[1] << " " << rgb[2] << "}\n";
245
os << "\\pscustom[linewidth=" << style->stroke_width.computed<< ",linecolor=curcolor";
247
if (style->stroke_dasharray_set &&
248
style->stroke_dash.n_dash &&
249
style->stroke_dash.dash) {
251
os << ",linestyle=dashed,dash=";
252
for (i = 0; i < style->stroke_dash.n_dash; i++) {
256
os << style->stroke_dash.dash[i];
262
print_bpath(os, bpath->path, transform);
266
fprintf(_stream, "%s", os.str().c_str());
273
PrintLatex::print_bpath(SVGOStringStream &os, const NArtBpath *bp, const NRMatrix *transform)
276
NR::Matrix tf=*transform;
281
while (bp->code != NR_END) {
284
NR::Point const p1(bp->c(1) * tf);
285
NR::Point const p2(bp->c(2) * tf);
286
NR::Point const p3(bp->c(3) * tf);
287
double const x1 = p1[X], y1 = p1[Y];
288
double const x2 = p2[X], y2 = p2[Y];
289
double const x3 = p3[X], y3 = p3[Y];
294
os << "\\closepath\n";
297
os << "\\moveto(" << x3 << "," << y3 << ")\n";
301
os << "\\closepath\n";
304
os << "\\moveto(" << x3 << "," << y3 << ")\n";
307
os << "\\lineto(" << x3 << "," << y3 << ")\n";
310
os << "\\curveto(" << x1 << "," << y1 << ")("
311
<< x2 << "," << y2 << ")("
312
<< x3 << "," << y3 << ")\n";
320
os << "\\closepath\n";
325
PrintLatex::textToPath(Inkscape::Extension::Print * ext)
327
return ext->get_param_bool("textToPath");
331
PrintLatex::init (void)
333
Inkscape::Extension::Extension * ext;
336
ext = Inkscape::Extension::build_from_mem(
337
"<inkscape-extension>\n"
338
"<name>LaTeX Print</name>\n"
339
"<id>" SP_MODULE_KEY_PRINT_LATEX "</id>\n"
340
"<param name=\"destination\" type=\"string\"></param>\n"
341
"<param name=\"textToPath\" type=\"boolean\">TRUE</param>\n"
343
"</inkscape-extension>", new PrintLatex());
348
} /* namespace Internal */
349
} /* namespace Extension */
350
} /* namespace Inkscape */
355
c-file-style:"stroustrup"
356
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
361
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :