Code

fix bug in latex export, when svgwidth has not been defined at all
[inkscape.git] / src / extension / internal / latex-text-renderer.cpp
1 #define EXTENSION_INTERNAL_LATEX_TEXT_RENDERER_CPP
3 /** \file
4  * Rendering LaTeX file (pdf/eps/ps+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 #include "latex-text-renderer.h"
24 #include <signal.h>
25 #include <errno.h>
27 #include "libnrtype/Layout-TNG.h"
28 #include <2geom/transforms.h>
29 #include <2geom/rect.h>
31 #include <glibmm/i18n.h>
32 #include "sp-item.h"
33 #include "sp-item-group.h"
34 #include "style.h"
35 #include "sp-root.h"
36 #include "sp-use.h"
37 #include "sp-text.h"
38 #include "sp-flowtext.h"
39 #include "text-editing.h"
41 #include <unit-constants.h>
43 #include "extension/system.h"
45 #include "io/sys.h"
47 namespace Inkscape {
48 namespace Extension {
49 namespace Internal {
51 /**
52  * This method is called by the PDF, EPS and PS output extensions.
53  * @param filename This should be the filename without extension to which the tex code should be written. Output goes to <filename>.tex.
54  */
55 bool
56 latex_render_document_text_to_file( SPDocument *doc, gchar const *filename,
57                                     const gchar * const exportId, bool exportDrawing, bool exportCanvas)
58 {
59     sp_document_ensure_up_to_date(doc);
61     SPItem *base = NULL;
63     bool pageBoundingBox = true;
64     if (exportId && strcmp(exportId, "")) {
65         // we want to export the given item only
66         base = SP_ITEM(doc->getObjectById(exportId));
67         pageBoundingBox = exportCanvas;
68     }
69     else {
70         // we want to export the entire document from root
71         base = SP_ITEM(sp_document_root(doc));
72         pageBoundingBox = !exportDrawing;
73     }
75     if (!base)
76         return false;
78     /* Create renderer */
79     LaTeXTextRenderer *renderer = new LaTeXTextRenderer();
81     bool ret = renderer->setTargetFile(filename);
82     if (ret) {
83         /* Render document */
84         bool ret = renderer->setupDocument(doc, pageBoundingBox, base);
85         if (ret) {
86             renderer->renderItem(base);
87         }
88     }
90     delete renderer;
92     return ret;
93 }
95 LaTeXTextRenderer::LaTeXTextRenderer(void)
96   : _stream(NULL),
97     _filename(NULL)
98 {
99     push_transform(Geom::identity());
102 LaTeXTextRenderer::~LaTeXTextRenderer(void)
104     if (_stream) {
105         writePostamble();
107         fclose(_stream);
108     }
110     /* restore default signal handling for SIGPIPE */
111 #if !defined(_WIN32) && !defined(__WIN32__)
112     (void) signal(SIGPIPE, SIG_DFL);
113 #endif
115     if (_filename) {
116         g_free(_filename);
117     }
119     return;
122 /** This should create the output LaTeX file, and assign it to _stream.
123  * @return Returns true when succesfull
124  */
125 bool
126 LaTeXTextRenderer::setTargetFile(gchar const *filename) {
127     if (filename != NULL) {
128         while (isspace(*filename)) filename += 1;
130         _filename = g_path_get_basename(filename);
132         gchar *filename_ext = g_strdup_printf("%s.tex", filename);
133         Inkscape::IO::dump_fopen_call(filename_ext, "K");
134         FILE *osf = Inkscape::IO::fopen_utf8name(filename_ext, "w+");
135         if (!osf) {
136             fprintf(stderr, "inkscape: fopen(%s): %s\n",
137                     filename_ext, strerror(errno));
138             return false;
139         }
140         _stream = osf;
141         g_free(filename_ext);
142     }
144     if (_stream) {
145         /* fixme: this is kinda icky */
146 #if !defined(_WIN32) && !defined(__WIN32__)
147         (void) signal(SIGPIPE, SIG_IGN);
148 #endif
149     }
151     fprintf(_stream, "%%%% Creator: Inkscape %s, www.inkscape.org\n", PACKAGE_STRING);
152     fprintf(_stream, "%%%% PDF/EPS/PS + LaTeX output extension by Johan Engelen, 2010\n");
153     fprintf(_stream, "%%%% Accompanies image file '%s' (pdf, eps, ps)\n", _filename);
154     fprintf(_stream, "%%%%\n");
155     /* flush this to test output stream as early as possible */
156     if (fflush(_stream)) {
157         if (ferror(_stream)) {
158             g_print("Error %d on LaTeX file output stream: %s\n", errno,
159                     g_strerror(errno));
160         }
161         g_print("Output to LaTeX file failed\n");
162         /* fixme: should use pclose() for pipes */
163         fclose(_stream);
164         _stream = NULL;
165         fflush(stdout);
166         return false;
167     }
169     writePreamble();
171     return true;
174 static char const preamble[] =
175 "%% To include the image in your LaTeX document, write\n"
176 "%%   \\input{<filename>.tex}\n"
177 "%%  instead of\n"
178 "%%   \\includegraphics{<filename>.pdf}\n"
179 "%% To scale the image, write\n"
180 "%%   \\def{\\svgwidth}{<desired width>}\n"
181 "%%   \\input{<filename>.tex}\n"
182 "%%  instead of\n"
183 "%%   \\includegraphics[width=<desired width>]{<filename>.pdf}\n"
184 "\n"
185 "\\begingroup\n"
186 "  \\makeatletter\n"
187 "  \\providecommand\\color[2][]{%\n"
188 "    \\GenericError{(Inkscape) \\space\\space\\@spaces}{%\n"
189 "      Color is used for the text in Inkscape, but the color package color is not loaded.\n"
190 "    }{Either use black text in Inkscape or load the package\n"
191 "      color.sty in LaTeX.}%\n"
192 "    \\renewcommand\\color[2][]{}%\n"
193 "  }\n"
194 "  \\providecommand\\rotatebox[2]{#2}\n";
196 static char const postamble[] =
197 "  \\end{picture}%\n"
198 "\\endgroup\n";
200 void
201 LaTeXTextRenderer::writePreamble()
203     fprintf(_stream, "%s", preamble);
205 void
206 LaTeXTextRenderer::writePostamble()
208     fprintf(_stream, "%s", postamble);
211 void
212 LaTeXTextRenderer::sp_group_render(SPItem *item)
214     SPGroup *group = SP_GROUP(item);
216     GSList *l = g_slist_reverse(group->childList(false));
217     while (l) {
218         SPObject *o = SP_OBJECT (l->data);
219         if (SP_IS_ITEM(o)) {
220             renderItem (SP_ITEM (o));
221         }
222         l = g_slist_remove (l, o);
223     }
226 void
227 LaTeXTextRenderer::sp_use_render(SPItem *item)
229     bool translated = false;
230     SPUse *use = SP_USE(item);
232     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
233         Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
234         push_transform(tp);
235         translated = true;
236     }
238     if (use->child && SP_IS_ITEM(use->child)) {
239         renderItem(SP_ITEM(use->child));
240     }
242     if (translated) {
243         pop_transform();
244     }
247 void
248 LaTeXTextRenderer::sp_text_render(SPItem *item)
250     SPText *textobj = SP_TEXT (item);
251     SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item));
253     gchar *str = sp_te_get_string_multiline(item);
254     if (!str) {
255         return;
256     }
258     // get position and alignment
259     // Align vertically on the baseline of the font (retreived from the anchor point)
260     // Align horizontally on anchorpoint
261     gchar *alignment = NULL;
262     switch (style->text_anchor.computed) {
263     case SP_CSS_TEXT_ANCHOR_START:
264         alignment = "[lb]";
265         break;
266     case SP_CSS_TEXT_ANCHOR_END:
267         alignment = "[rb]";
268         break;
269     case SP_CSS_TEXT_ANCHOR_MIDDLE:
270     default:
271         alignment = "[b]";
272         break;
273     }
274     Geom::Point anchor = textobj->attributes.firstXY() * transform();
275     Geom::Point pos(anchor);
277     // determine color (for now, use rgb color model as it is most native to Inkscape)
278     bool has_color = false; // if the item has no color set, don't force black color
279     // TODO: how to handle ICC colors?
280     // give priority to fill color
281     guint32 rgba = 0;
282     if (style->fill.set && style->fill.isColor()) {
283         has_color = true;
284         rgba = style->fill.value.color.toRGBA32(1.);
285     } else if (style->stroke.set && style->stroke.isColor()) {
286         has_color = true;
287         rgba = style->stroke.value.color.toRGBA32(1.);
288     }
291     // get rotation
292     Geom::Matrix i2doc = sp_item_i2doc_affine(item);
293     Geom::Matrix wotransl = i2doc.without_translation();
294     double degrees = -180/M_PI * Geom::atan2(wotransl.xAxis());
295     bool has_rotation = !Geom::are_near(degrees,0.);
297     // write to LaTeX
298     Inkscape::SVGOStringStream os;
299     os.setf(std::ios::fixed); // don't use scientific notation
301     os << "    \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){";
302     if (has_color) {
303         os << "\\color[rgb]{" << SP_RGBA32_R_F(rgba) << "," << SP_RGBA32_G_F(rgba) << "," << SP_RGBA32_B_F(rgba) << "}";
304     }
305     if (has_rotation) {
306         os << "\\rotatebox{" << degrees << "}{";
307     }
308     os << "\\makebox(0,0)" << alignment << "{";
309     os << "\\smash{" << str << "}";  // smash the text, to be able to put the makebox coordinates at the baseline
310     if (has_rotation) {
311         os << "}"; // rotatebox end
312     }
313     os << "}"; //makebox end
314     os << "}%\n"; // put end
316     fprintf(_stream, "%s", os.str().c_str());
319 void
320 LaTeXTextRenderer::sp_flowtext_render(SPItem * /*item*/)
322 /*    SPFlowtext *group = SP_FLOWTEXT(item);
324     // write to LaTeX
325     Inkscape::SVGOStringStream os;
326     os.setf(std::ios::fixed); // no scientific notation
328     os << "  \\begin{picture}(" << _width << "," << _height << ")%%\n";
329     os << "    \\gplgaddtomacro\\gplbacktext{%%\n";
330     os << "      \\csname LTb\\endcsname%%\n";
331     os << "\\put(0,0){\\makebox(0,0)[lb]{\\strut{}Position}}%%\n";
333     fprintf(_stream, "%s", os.str().c_str());
334 */
337 void
338 LaTeXTextRenderer::sp_root_render(SPItem *item)
340     SPRoot *root = SP_ROOT(item);
342     push_transform(root->c2p);
343     sp_group_render(item);
344     pop_transform();
347 void
348 LaTeXTextRenderer::sp_item_invoke_render(SPItem *item)
350     // Check item's visibility
351     if (item->isHidden()) {
352         return;
353     }
355     if (SP_IS_ROOT(item)) {
356         return sp_root_render(item);
357     } else if (SP_IS_GROUP(item)) {
358         return sp_group_render(item);
359     } else if (SP_IS_USE(item)) {
360         sp_use_render(item);
361     } else if (SP_IS_TEXT(item)) {
362         return sp_text_render(item);
363     } else if (SP_IS_FLOWTEXT(item)) {
364         return sp_flowtext_render(item);
365     }
366     // We are not interested in writing the other SPItem types to LaTeX
369 void
370 LaTeXTextRenderer::renderItem(SPItem *item)
372     push_transform(item->transform);
373     sp_item_invoke_render(item);
374     pop_transform();
377 bool
378 LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem *base)
380 // The boundingbox calculation here should be exactly the same as the one by CairoRenderer::setupDocument !
382     if (!base)
383         base = SP_ITEM(sp_document_root(doc));
385     Geom::OptRect d;
386     if (pageBoundingBox) {
387         d = Geom::Rect( Geom::Point(0,0),
388                         Geom::Point(sp_document_width(doc), sp_document_height(doc)) );
389     } else {
390         sp_item_invoke_bbox(base, d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
391     }
392     if (!d) {
393         g_message("LaTeXTextRenderer: could not retrieve boundingbox.");
394         return false;
395     }
397     // scale all coordinates, such that the width of the image is 1, this is convenient for scaling the image in LaTeX
398     double scale = 1/(d->width());
399     double _width = d->width() * scale;
400     double _height = d->height() * scale;
401     push_transform( Geom::Scale(scale, scale) );
403     if (!pageBoundingBox)
404     {
405         push_transform( Geom::Translate( - d->min() ) );
406     }
408     // flip y-axis
409     push_transform( Geom::Scale(1,-1) * Geom::Translate(0, sp_document_height(doc)) );
411     // write the info to LaTeX
412     Inkscape::SVGOStringStream os;
413     os.setf(std::ios::fixed); // no scientific notation
415     // scaling of the image when including it in LaTeX
417     os << "  \\ifx\\svgwidth\\undefined\n";
418     os << "    \\setlength{\\unitlength}{" << d->width() * PT_PER_PX << "pt}\n";
419     os << "  \\else\n";
420     os << "    \\setlength{\\unitlength}{\\svgwidth}\n";
421     os << "  \\fi\n";
422     os << "  \\global\\let\\svgwidth\\undefined\n";
423     os << "  \\makeatother\n";
425     os << "  \\begin{picture}(" << _width << "," << _height << ")%\n";
426     // strip pathname, as it is probably desired. Having a specific path in the TeX file is not convenient.
427     os << "    \\put(0,0){\\includegraphics[width=\\unitlength]{" << _filename << "}}%\n";
429     fprintf(_stream, "%s", os.str().c_str());
431     return true;
434 Geom::Matrix const &
435 LaTeXTextRenderer::transform()
437     return _transform_stack.top();
440 void
441 LaTeXTextRenderer::push_transform(Geom::Matrix const &tr)
443     if(_transform_stack.size()){
444         Geom::Matrix tr_top = _transform_stack.top();
445         _transform_stack.push(tr * tr_top);
446     } else {
447         _transform_stack.push(tr);
448     }
451 void
452 LaTeXTextRenderer::pop_transform()
454     _transform_stack.pop();
457 }  /* namespace Internal */
458 }  /* namespace Extension */
459 }  /* namespace Inkscape */
461 /*
462   Local Variables:
463   mode:c++
464   c-file-style:"stroustrup"
465   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
466   indent-tabs-mode:nil
467   fill-column:99
468   End:
469 */
470 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :