1 #define EXTENSION_INTERNAL_PDF_LATEX_RENDERER_CPP
3 /** \file
4 * Rendering LaTeX file (pdf+latex output)
5 *
6 * The idea stems from GNUPlot's epslatex terminal output :-)
7 */
8 /*
9 * Authors:
10 * Johan Engelen <goejendaagh@zonnet.nl>
11 * Miklos Erdelyi <erdelyim@gmail.com>
12 *
13 * Copyright (C) 2006-2010 Authors
14 *
15 * Licensed under GNU GPL
16 */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #ifndef PANGO_ENABLE_BACKEND
23 #define PANGO_ENABLE_BACKEND
24 #endif
26 #ifndef PANGO_ENABLE_ENGINE
27 #define PANGO_ENABLE_ENGINE
28 #endif
31 #include <signal.h>
32 #include <errno.h>
34 #include "libnr/nr-rect.h"
35 #include "libnrtype/Layout-TNG.h"
36 #include <2geom/transforms.h>
37 #include <2geom/pathvector.h>
39 #include <glib/gmem.h>
41 #include <glibmm/i18n.h>
42 #include "display/nr-arena.h"
43 #include "display/nr-arena-item.h"
44 #include "display/nr-arena-group.h"
45 #include "display/curve.h"
46 #include "display/canvas-bpath.h"
47 #include "sp-item.h"
48 #include "sp-item-group.h"
49 #include "style.h"
50 #include "marker.h"
51 #include "sp-linear-gradient.h"
52 #include "sp-radial-gradient.h"
53 #include "sp-root.h"
54 #include "sp-use.h"
55 #include "sp-text.h"
56 #include "sp-flowtext.h"
57 #include "sp-mask.h"
58 #include "sp-clippath.h"
59 #include "text-editing.h"
61 #include <unit-constants.h>
62 #include "helper/png-write.h"
63 #include "helper/pixbuf-ops.h"
65 #include "pdflatex-renderer.h"
66 #include "extension/system.h"
68 #include "io/sys.h"
70 #include <cairo.h>
72 // include support for only the compiled-in surface types
73 #ifdef CAIRO_HAS_PDF_SURFACE
74 #include <cairo-pdf.h>
75 #endif
76 #ifdef CAIRO_HAS_PS_SURFACE
77 #include <cairo-ps.h>
78 #endif
80 //#define TRACE(_args) g_message(_args)
81 #define TRACE(_args)
82 //#define TEST(_args) _args
83 #define TEST(_args)
85 // FIXME: expose these from sp-clippath/mask.cpp
86 struct SPClipPathView {
87 SPClipPathView *next;
88 unsigned int key;
89 NRArenaItem *arenaitem;
90 NRRect bbox;
91 };
93 struct SPMaskView {
94 SPMaskView *next;
95 unsigned int key;
96 NRArenaItem *arenaitem;
97 NRRect bbox;
98 };
100 namespace Inkscape {
101 namespace Extension {
102 namespace Internal {
105 PDFLaTeXRenderer::PDFLaTeXRenderer(void)
106 : _stream(NULL),
107 _filename(NULL),
108 _width(0),
109 _height(0)
110 {
111 push_transform(Geom::identity());
112 }
114 PDFLaTeXRenderer::~PDFLaTeXRenderer(void)
115 {
116 if (_stream) {
117 writePostamble();
119 fclose(_stream);
120 }
122 /* restore default signal handling for SIGPIPE */
123 #if !defined(_WIN32) && !defined(__WIN32__)
124 (void) signal(SIGPIPE, SIG_DFL);
125 #endif
127 if (_filename) {
128 g_free(_filename);
129 }
131 return;
132 }
134 /** This should create the output LaTeX file, and assign it to _stream.
135 * @return Returns true when succesfull
136 */
137 bool
138 PDFLaTeXRenderer::setTargetFile(gchar const *filename) {
139 if (filename != NULL) {
140 while (isspace(*filename)) filename += 1;
142 _filename = g_strdup(filename);
144 gchar *filename_ext = g_strdup_printf("%s.tex", filename);
145 Inkscape::IO::dump_fopen_call(filename_ext, "K");
146 FILE *osf = Inkscape::IO::fopen_utf8name(filename_ext, "w+");
147 if (!osf) {
148 fprintf(stderr, "inkscape: fopen(%s): %s\n",
149 filename_ext, strerror(errno));
150 return false;
151 }
152 _stream = osf;
153 g_free(filename_ext);
154 }
156 if (_stream) {
157 /* fixme: this is kinda icky */
158 #if !defined(_WIN32) && !defined(__WIN32__)
159 (void) signal(SIGPIPE, SIG_IGN);
160 #endif
161 }
163 fprintf(_stream, "%%%% Creator: Inkscape %s, www.inkscape.org\n", PACKAGE_STRING);
164 fprintf(_stream, "%%%% PDF + LaTeX output extension by Johan Engelen, 2010\n");
165 fprintf(_stream, "%%%% Accompanies %s.pdf\n", _filename);
166 /* flush this to test output stream as early as possible */
167 if (fflush(_stream)) {
168 if (ferror(_stream)) {
169 g_print("Error %d on LaTeX file output stream: %s\n", errno,
170 g_strerror(errno));
171 }
172 g_print("Output to LaTeX file failed\n");
173 /* fixme: should use pclose() for pipes */
174 fclose(_stream);
175 _stream = NULL;
176 fflush(stdout);
177 return false;
178 }
180 writePreamble();
182 return true;
183 }
185 static char const preamble[] =
186 "%% To include the image in your LaTeX document, write\n"
187 "%% \\setlength{\\unitlength}{<desired width>}\n"
188 "%% \\input{<filename>.tex}\n"
189 "%% instead of\n"
190 "%% \\includegraphics[width=<desired width>]{<filename>.pdf}\n"
191 "\n"
192 "\\begingroup \n"
193 " \\makeatletter \n"
194 " \\providecommand\\color[2][]{% \n"
195 " \\GenericError{(Inkscape) \\space\\space\\@spaces}{% \n"
196 " Color is used for the text in Inkscape, but the color package color is not loaded. \n"
197 " }{Either use black text in Inkscape or load the package \n"
198 " color.sty in LaTeX.}% \n"
199 " \\renewcommand\\color[2][]{}% \n"
200 " }%% \n"
201 " \\providecommand\\rotatebox[2]{#2}% \n"
202 " \\makeatother \n";
204 static char const postamble[] =
205 " \\end{picture}% \n"
206 "\\endgroup \n";
208 void
209 PDFLaTeXRenderer::writePreamble()
210 {
211 fprintf(_stream, "%s", preamble);
212 }
213 void
214 PDFLaTeXRenderer::writePostamble()
215 {
216 fprintf(_stream, "%s", postamble);
217 }
219 void
220 PDFLaTeXRenderer::sp_group_render(SPItem *item)
221 {
222 SPGroup *group = SP_GROUP(item);
224 GSList *l = g_slist_reverse(group->childList(false));
225 while (l) {
226 SPObject *o = SP_OBJECT (l->data);
227 if (SP_IS_ITEM(o)) {
228 renderItem (SP_ITEM (o));
229 }
230 l = g_slist_remove (l, o);
231 }
232 }
234 void
235 PDFLaTeXRenderer::sp_use_render(SPItem *item)
236 {
237 /*
238 bool translated = false;
239 SPUse *use = SP_USE(item);
241 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
242 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
243 ctx->pushState();
244 ctx->transform(&tp);
245 translated = true;
246 }
248 if (use->child && SP_IS_ITEM(use->child)) {
249 renderItem(SP_ITEM(use->child));
250 }
252 if (translated) {
253 ctx->popState();
254 }
255 */
256 }
258 void
259 PDFLaTeXRenderer::sp_text_render(SPItem *item)
260 {
261 SPText *textobj = SP_TEXT (item);
263 Geom::Matrix i2doc = sp_item_i2doc_affine(item);
264 push_transform(i2doc);
266 gchar *str = sp_te_get_string_multiline(item);
268 // get position and alignment
269 Geom::Point pos;
270 gchar *alignment = NULL;
271 Geom::OptRect bbox = item->getBounds(transform());
272 Geom::Interval bbox_x = (*bbox)[Geom::X];
273 Geom::Interval bbox_y = (*bbox)[Geom::Y];
274 SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item));
275 switch (style->text_anchor.computed) {
276 case SP_CSS_TEXT_ANCHOR_START:
277 pos = Geom::Point( bbox_x.min() , bbox_y.middle() );
278 alignment = "[l]";
279 break;
280 case SP_CSS_TEXT_ANCHOR_END:
281 pos = Geom::Point( bbox_x.max() , bbox_y.middle() );
282 alignment = "[r]";
283 break;
284 case SP_CSS_TEXT_ANCHOR_MIDDLE:
285 default:
286 pos = bbox->midpoint();
287 alignment = "";
288 break;
289 }
291 // get rotation
292 Geom::Matrix wotransl = i2doc.without_translation();
293 double degrees = -180/M_PI * Geom::atan2(wotransl.xAxis());
294 bool has_rotation = !Geom::are_near(degrees,0.);
296 pop_transform();
298 // write to LaTeX
299 Inkscape::SVGOStringStream os;
301 // os << "\\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){\\makebox(0,0)[" << alignment << "]{\\strut{}" << str << "}}%%\n";
302 os << " \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){";
303 os << "\\makebox(0,0)" << alignment << "{";
304 if (has_rotation) {
305 os << "\\rotatebox{" << degrees << "}{";
306 }
307 os << str;
308 if (has_rotation) {
309 os << "}"; // rotatebox end
310 }
311 os << "}"; //makebox end
312 os << "}%\n"; // put end
314 fprintf(_stream, "%s", os.str().c_str());
315 }
317 void
318 PDFLaTeXRenderer::sp_flowtext_render(SPItem *item)
319 {
320 /* SPFlowtext *group = SP_FLOWTEXT(item);
322 // write to LaTeX
323 Inkscape::SVGOStringStream os;
325 os << " \\begin{picture}(" << _width << "," << _height << ")%%\n";
326 os << " \\gplgaddtomacro\\gplbacktext{%%\n";
327 os << " \\csname LTb\\endcsname%%\n";
328 os << "\\put(0,0){\\makebox(0,0)[lb]{\\strut{}Position}}%%\n";
330 fprintf(_stream, "%s", os.str().c_str());
331 */
332 }
334 void
335 PDFLaTeXRenderer::sp_root_render(SPItem *item)
336 {
337 SPRoot *root = SP_ROOT(item);
339 // ctx->pushState();
340 // setStateForItem(ctx, item);
341 Geom::Matrix tempmat (root->c2p);
342 push_transform(tempmat);
343 sp_group_render(item);
344 pop_transform();
345 }
347 void
348 PDFLaTeXRenderer::sp_item_invoke_render(SPItem *item)
349 {
350 // Check item's visibility
351 if (item->isHidden()) {
352 return;
353 }
355 if (SP_IS_ROOT(item)) {
356 TRACE(("root\n"));
357 return sp_root_render(item);
358 } else if (SP_IS_GROUP(item)) {
359 TRACE(("group\n"));
360 return sp_group_render(item);
361 } else if (SP_IS_USE(item)) {
362 TRACE(("use begin---\n"));
363 sp_use_render(item);
364 TRACE(("---use end\n"));
365 } else if (SP_IS_TEXT(item)) {
366 TRACE(("text\n"));
367 return sp_text_render(item);
368 } else if (SP_IS_FLOWTEXT(item)) {
369 TRACE(("flowtext\n"));
370 return sp_flowtext_render(item);
371 }
372 // We are not interested in writing the other SPItem types to LaTeX
373 }
375 void
376 PDFLaTeXRenderer::setStateForItem(SPItem const *item)
377 {
378 /*
379 SPStyle const *style = SP_OBJECT_STYLE(item);
380 ctx->setStateForStyle(style);
382 CairoRenderState *state = ctx->getCurrentState();
383 state->clip_path = item->clip_ref->getObject();
384 state->mask = item->mask_ref->getObject();
385 state->item_transform = Geom::Matrix (item->transform);
387 // If parent_has_userspace is true the parent state's transform
388 // has to be used for the mask's/clippath's context.
389 // This is so because we use the image's/(flow)text's transform for positioning
390 // instead of explicitly specifying it and letting the renderer do the
391 // transformation before rendering the item.
392 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
393 state->parent_has_userspace = TRUE;
394 TRACE(("setStateForItem opacity: %f\n", state->opacity));
395 */
396 }
398 void
399 PDFLaTeXRenderer::renderItem(SPItem *item)
400 {
401 // ctx->pushState();
402 // setStateForItem(ctx, item);
404 // CairoRenderState *state = ctx->getCurrentState();
405 // state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
407 // Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
408 // if (state->need_layer) {
409 // state->merge_opacity = FALSE;
410 // ctx->pushLayer();
411 // }
412 Geom::Matrix tempmat (item->transform);
413 // ctx->transform(&tempmat);
414 sp_item_invoke_render(item);
416 // if (state->need_layer)
417 // ctx->popLayer();
419 // ctx->popState();
420 }
422 bool
423 PDFLaTeXRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem *base)
424 {
425 // The boundingbox calculation here should be exactly the same as the one by CairoRenderer::setupDocument !
427 if (!base)
428 base = SP_ITEM(sp_document_root(doc));
430 NRRect d;
431 if (pageBoundingBox) {
432 d.x0 = d.y0 = 0;
433 d.x1 = ceil(sp_document_width(doc));
434 d.y1 = ceil(sp_document_height(doc));
435 } else {
436 sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
437 }
439 // scale all coordinates, such that the width of the image is 1, this is convenient for scaling the image in LaTeX
440 double scale = 1/(d.x1-d.x0);
441 _width = (d.x1-d.x0) * scale;
442 _height = (d.y1-d.y0) * scale;
443 push_transform( Geom::Scale(scale, scale) );
445 if (!pageBoundingBox)
446 {
447 double high = sp_document_height(doc);
449 push_transform( Geom::Translate( -d.x0,
450 -d.y0 ) );
451 }
453 // flip y-axis
454 push_transform( Geom::Scale(1,-1) * Geom::Translate(0, sp_document_height(doc)) );
456 // write the info to LaTeX
457 Inkscape::SVGOStringStream os;
459 os << " \\begin{picture}(" << _width << "," << _height << ")%\n";
460 // strip pathname, as it is probably desired. Having a specific path in the TeX file is not convenient.
461 gchar *figurefile = g_path_get_basename(_filename);
462 os << " \\put(0,0){\\includegraphics[width=\\unitlength]{" << figurefile << ".pdf}}%\n";
463 g_free(figurefile);
465 fprintf(_stream, "%s", os.str().c_str());
467 return true;
468 }
470 Geom::Matrix const &
471 PDFLaTeXRenderer::transform()
472 {
473 return _transform_stack.top();
474 }
476 void
477 PDFLaTeXRenderer::push_transform(Geom::Matrix const &tr)
478 {
479 if(_transform_stack.size()){
480 Geom::Matrix tr_top = _transform_stack.top();
481 _transform_stack.push(tr * tr_top);
482 } else {
483 _transform_stack.push(tr);
484 }
485 }
487 void
488 PDFLaTeXRenderer::pop_transform()
489 {
490 _transform_stack.pop();
491 }
493 /*
494 #include "macros.h" // SP_PRINT_*
496 // Apply an SVG clip path
497 void
498 PDFLaTeXRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
499 {
500 g_assert( ctx != NULL && ctx->_is_valid );
502 if (cp == NULL)
503 return;
505 CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
506 ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
508 Geom::Matrix saved_ctm;
509 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
510 //SP_PRINT_DRECT("clipd", cp->display->bbox);
511 NRRect clip_bbox(cp->display->bbox);
512 Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
513 t[4] = clip_bbox.x0;
514 t[5] = clip_bbox.y0;
515 t *= ctx->getCurrentState()->transform;
516 ctx->getTransform(&saved_ctm);
517 ctx->setTransform(&t);
518 }
520 TRACE(("BEGIN clip\n"));
521 SPObject *co = SP_OBJECT(cp);
522 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
523 if (SP_IS_ITEM(child)) {
524 SPItem *item = SP_ITEM(child);
526 // combine transform of the item in clippath and the item using clippath:
527 Geom::Matrix tempmat (item->transform);
528 tempmat = tempmat * (ctx->getCurrentState()->item_transform);
530 // render this item in clippath
531 ctx->pushState();
532 ctx->transform(&tempmat);
533 setStateForItem(ctx, item);
534 sp_item_invoke_render(item, ctx);
535 ctx->popState();
536 }
537 }
538 TRACE(("END clip\n"));
540 // do clipping only if this was the first call to applyClipPath
541 if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
542 && saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
543 cairo_clip(ctx->_cr);
545 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
546 ctx->setTransform(&saved_ctm);
548 ctx->setRenderMode(saved_mode);
549 }
551 // Apply an SVG mask
552 void
553 PDFLaTeXRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
554 {
555 g_assert( ctx != NULL && ctx->_is_valid );
557 if (mask == NULL)
558 return;
560 //SP_PRINT_DRECT("maskd", &mask->display->bbox);
561 NRRect mask_bbox(mask->display->bbox);
562 // TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
563 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
564 Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
565 t[4] = mask_bbox.x0;
566 t[5] = mask_bbox.y0;
567 t *= ctx->getCurrentState()->transform;
568 ctx->setTransform(&t);
569 }
571 // Clip mask contents... but...
572 // The mask's bounding box is the "geometric bounding box" which doesn't allow for
573 // filters which extend outside the bounding box. So don't clip.
574 // ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
576 ctx->pushState();
578 TRACE(("BEGIN mask\n"));
579 SPObject *co = SP_OBJECT(mask);
580 for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
581 if (SP_IS_ITEM(child)) {
582 SPItem *item = SP_ITEM(child);
583 renderItem(ctx, item);
584 }
585 }
586 TRACE(("END mask\n"));
588 ctx->popState();
589 }
590 */
592 } /* namespace Internal */
593 } /* namespace Extension */
594 } /* namespace Inkscape */
596 #undef TRACE
598 /* End of GNU GPL code */
600 /*
601 Local Variables:
602 mode:c++
603 c-file-style:"stroustrup"
604 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
605 indent-tabs-mode:nil
606 fill-column:99
607 End:
608 */
609 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :