Code

c058c4ff2cc60cd1522db15664835679da5df37a
[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                                     bool pdflatex)
59 {
60     sp_document_ensure_up_to_date(doc);
62     SPItem *base = NULL;
64     bool pageBoundingBox = true;
65     if (exportId && strcmp(exportId, "")) {
66         // we want to export the given item only
67         base = SP_ITEM(doc->getObjectById(exportId));
68         pageBoundingBox = exportCanvas;
69     }
70     else {
71         // we want to export the entire document from root
72         base = SP_ITEM(sp_document_root(doc));
73         pageBoundingBox = !exportDrawing;
74     }
76     if (!base)
77         return false;
79     /* Create renderer */
80     LaTeXTextRenderer *renderer = new LaTeXTextRenderer(pdflatex);
82     bool ret = renderer->setTargetFile(filename);
83     if (ret) {
84         /* Render document */
85         bool ret = renderer->setupDocument(doc, pageBoundingBox, base);
86         if (ret) {
87             renderer->renderItem(base);
88         }
89     }
91     delete renderer;
93     return ret;
94 }
96 LaTeXTextRenderer::LaTeXTextRenderer(bool pdflatex)
97   : _stream(NULL),
98     _filename(NULL),
99     _pdflatex(pdflatex)
101     push_transform(Geom::identity());
104 LaTeXTextRenderer::~LaTeXTextRenderer(void)
106     if (_stream) {
107         writePostamble();
109         fclose(_stream);
110     }
112     /* restore default signal handling for SIGPIPE */
113 #if !defined(_WIN32) && !defined(__WIN32__)
114     (void) signal(SIGPIPE, SIG_DFL);
115 #endif
117     if (_filename) {
118         g_free(_filename);
119     }
121     return;
124 /** This should create the output LaTeX file, and assign it to _stream.
125  * @return Returns true when succesfull
126  */
127 bool
128 LaTeXTextRenderer::setTargetFile(gchar const *filename) {
129     if (filename != NULL) {
130         while (isspace(*filename)) filename += 1;
132         _filename = g_path_get_basename(filename);
134         gchar *filename_ext = g_strdup_printf("%s.tex", filename);
135         Inkscape::IO::dump_fopen_call(filename_ext, "K");
136         FILE *osf = Inkscape::IO::fopen_utf8name(filename_ext, "w+");
137         if (!osf) {
138             fprintf(stderr, "inkscape: fopen(%s): %s\n",
139                     filename_ext, strerror(errno));
140             return false;
141         }
142         _stream = osf;
143         g_free(filename_ext);
144     }
146     if (_stream) {
147         /* fixme: this is kinda icky */
148 #if !defined(_WIN32) && !defined(__WIN32__)
149         (void) signal(SIGPIPE, SIG_IGN);
150 #endif
151     }
153     fprintf(_stream, "%%%% Creator: Inkscape %s, www.inkscape.org\n", PACKAGE_STRING);
154     fprintf(_stream, "%%%% PDF/EPS/PS + LaTeX output extension by Johan Engelen, 2010\n");
155     fprintf(_stream, "%%%% Accompanies image file '%s' (pdf, eps, ps)\n", _filename);
156     fprintf(_stream, "%%%%\n");
157     /* flush this to test output stream as early as possible */
158     if (fflush(_stream)) {
159         if (ferror(_stream)) {
160             g_print("Error %d on LaTeX file output stream: %s\n", errno,
161                     g_strerror(errno));
162         }
163         g_print("Output to LaTeX file failed\n");
164         /* fixme: should use pclose() for pipes */
165         fclose(_stream);
166         _stream = NULL;
167         fflush(stdout);
168         return false;
169     }
171     writePreamble();
173     return true;
176 static char const preamble[] =
177 "%% To include the image in your LaTeX document, write\n"
178 "%%   \\input{<filename>.tex}\n"
179 "%%  instead of\n"
180 "%%   \\includegraphics{<filename>.pdf}\n"
181 "%% To scale the image, write\n"
182 "%%   \\def{\\svgwidth}{<desired width>}\n"
183 "%%   \\input{<filename>.tex}\n"
184 "%%  instead of\n"
185 "%%   \\includegraphics[width=<desired width>]{<filename>.pdf}\n"
186 "\n"
187 "\\begingroup\n"
188 "  \\makeatletter\n"
189 "  \\providecommand\\color[2][]{%\n"
190 "    \\errmessage{(Inkscape) Color is used for the text in Inkscape, but the package \'color.sty\' is not loaded}\n"
191 "    \\renewcommand\\color[2][]{}%\n"
192 "  }\n"
193 "  \\providecommand\\transparent[1]{%\n"
194 "    \\errmessage{(Inkscape) Transparency is used (non-zero) for the text in Inkscape, but the package \'transparent.sty\' is not loaded}\n"
195 "    \\renewcommand\\transparent[1]{}%\n"
196 "  }\n"
197 "  \\providecommand\\rotatebox[2]{#2}\n";
199 static char const postamble[] =
200 "  \\end{picture}%\n"
201 "\\endgroup\n";
203 void
204 LaTeXTextRenderer::writePreamble()
206     fprintf(_stream, "%s", preamble);
208 void
209 LaTeXTextRenderer::writePostamble()
211     fprintf(_stream, "%s", postamble);
214 void
215 LaTeXTextRenderer::sp_group_render(SPItem *item)
217     SPGroup *group = SP_GROUP(item);
219     GSList *l = g_slist_reverse(group->childList(false));
220     while (l) {
221         SPObject *o = SP_OBJECT (l->data);
222         if (SP_IS_ITEM(o)) {
223             renderItem (SP_ITEM (o));
224         }
225         l = g_slist_remove (l, o);
226     }
229 void
230 LaTeXTextRenderer::sp_use_render(SPItem *item)
232     bool translated = false;
233     SPUse *use = SP_USE(item);
235     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
236         Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
237         push_transform(tp);
238         translated = true;
239     }
241     if (use->child && SP_IS_ITEM(use->child)) {
242         renderItem(SP_ITEM(use->child));
243     }
245     if (translated) {
246         pop_transform();
247     }
250 void
251 LaTeXTextRenderer::sp_text_render(SPItem *item)
253     SPText *textobj = SP_TEXT (item);
254     SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item));
256     gchar *str = sp_te_get_string_multiline(item);
257     if (!str) {
258         return;
259     }
261     // get position and alignment
262     // Align vertically on the baseline of the font (retreived from the anchor point)
263     // Align horizontally on anchorpoint
264     gchar *alignment = NULL;
265     switch (style->text_anchor.computed) {
266     case SP_CSS_TEXT_ANCHOR_START:
267         alignment = "[lb]";
268         break;
269     case SP_CSS_TEXT_ANCHOR_END:
270         alignment = "[rb]";
271         break;
272     case SP_CSS_TEXT_ANCHOR_MIDDLE:
273     default:
274         alignment = "[b]";
275         break;
276     }
277     Geom::Point anchor = textobj->attributes.firstXY() * transform();
278     Geom::Point pos(anchor);
280     // determine color and transparency (for now, use rgb color model as it is most native to Inkscape)
281     bool has_color = false; // if the item has no color set, don't force black color
282     bool has_transparency = false;
283     // TODO: how to handle ICC colors?
284     // give priority to fill color
285     guint32 rgba = 0;
286     float opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
287     if (style->fill.set && style->fill.isColor()) {
288         has_color = true;
289         rgba = style->fill.value.color.toRGBA32(1.);
290         opacity *= SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
291     } else if (style->stroke.set && style->stroke.isColor()) {
292         has_color = true;
293         rgba = style->stroke.value.color.toRGBA32(1.);
294         opacity *= SP_SCALE24_TO_FLOAT(style->stroke_opacity.value);
295     }
296     if (opacity < 1.0) {
297         has_transparency = true;
298     }
300     // get rotation
301     Geom::Matrix i2doc = sp_item_i2doc_affine(item);
302     Geom::Matrix wotransl = i2doc.without_translation();
303     double degrees = -180/M_PI * Geom::atan2(wotransl.xAxis());
304     bool has_rotation = !Geom::are_near(degrees,0.);
306     // write to LaTeX
307     Inkscape::SVGOStringStream os;
308     os.setf(std::ios::fixed); // don't use scientific notation
310     os << "    \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){";
311     if (has_color) {
312         os << "\\color[rgb]{" << SP_RGBA32_R_F(rgba) << "," << SP_RGBA32_G_F(rgba) << "," << SP_RGBA32_B_F(rgba) << "}";
313     }
314     if (_pdflatex && has_transparency) {
315         os << "\\transparent{" << opacity << "}";
316     }
317     if (has_rotation) {
318         os << "\\rotatebox{" << degrees << "}{";
319     }
320     os << "\\makebox(0,0)" << alignment << "{";
321     os << "\\smash{" << str << "}";  // smash the text, to be able to put the makebox coordinates at the baseline
322     if (has_rotation) {
323         os << "}"; // rotatebox end
324     }
325     os << "}"; //makebox end
326     os << "}%\n"; // put end
328     fprintf(_stream, "%s", os.str().c_str());
331 void
332 LaTeXTextRenderer::sp_flowtext_render(SPItem * /*item*/)
334 /*    SPFlowtext *group = SP_FLOWTEXT(item);
336     // write to LaTeX
337     Inkscape::SVGOStringStream os;
338     os.setf(std::ios::fixed); // no scientific notation
340     os << "  \\begin{picture}(" << _width << "," << _height << ")%%\n";
341     os << "    \\gplgaddtomacro\\gplbacktext{%%\n";
342     os << "      \\csname LTb\\endcsname%%\n";
343     os << "\\put(0,0){\\makebox(0,0)[lb]{\\strut{}Position}}%%\n";
345     fprintf(_stream, "%s", os.str().c_str());
346 */
349 void
350 LaTeXTextRenderer::sp_root_render(SPItem *item)
352     SPRoot *root = SP_ROOT(item);
354     push_transform(root->c2p);
355     sp_group_render(item);
356     pop_transform();
359 void
360 LaTeXTextRenderer::sp_item_invoke_render(SPItem *item)
362     // Check item's visibility
363     if (item->isHidden()) {
364         return;
365     }
367     if (SP_IS_ROOT(item)) {
368         return sp_root_render(item);
369     } else if (SP_IS_GROUP(item)) {
370         return sp_group_render(item);
371     } else if (SP_IS_USE(item)) {
372         sp_use_render(item);
373     } else if (SP_IS_TEXT(item)) {
374         return sp_text_render(item);
375     } else if (SP_IS_FLOWTEXT(item)) {
376         return sp_flowtext_render(item);
377     }
378     // We are not interested in writing the other SPItem types to LaTeX
381 void
382 LaTeXTextRenderer::renderItem(SPItem *item)
384     push_transform(item->transform);
385     sp_item_invoke_render(item);
386     pop_transform();
389 bool
390 LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem *base)
392 // The boundingbox calculation here should be exactly the same as the one by CairoRenderer::setupDocument !
394     if (!base)
395         base = SP_ITEM(sp_document_root(doc));
397     Geom::OptRect d;
398     if (pageBoundingBox) {
399         d = Geom::Rect( Geom::Point(0,0),
400                         Geom::Point(sp_document_width(doc), sp_document_height(doc)) );
401     } else {
402         sp_item_invoke_bbox(base, d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
403     }
404     if (!d) {
405         g_message("LaTeXTextRenderer: could not retrieve boundingbox.");
406         return false;
407     }
409     // scale all coordinates, such that the width of the image is 1, this is convenient for scaling the image in LaTeX
410     double scale = 1/(d->width());
411     double _width = d->width() * scale;
412     double _height = d->height() * scale;
413     push_transform( Geom::Scale(scale, scale) );
415     if (!pageBoundingBox)
416     {
417         push_transform( Geom::Translate( - d->min() ) );
418     }
420     // flip y-axis
421     push_transform( Geom::Scale(1,-1) * Geom::Translate(0, sp_document_height(doc)) );
423     // write the info to LaTeX
424     Inkscape::SVGOStringStream os;
425     os.setf(std::ios::fixed); // no scientific notation
427     // scaling of the image when including it in LaTeX
429     os << "  \\ifx\\svgwidth\\undefined\n";
430     os << "    \\setlength{\\unitlength}{" << d->width() * PT_PER_PX << "pt}\n";
431     os << "  \\else\n";
432     os << "    \\setlength{\\unitlength}{\\svgwidth}\n";
433     os << "  \\fi\n";
434     os << "  \\global\\let\\svgwidth\\undefined\n";
435     os << "  \\makeatother\n";
437     os << "  \\begin{picture}(" << _width << "," << _height << ")%\n";
438     // strip pathname, as it is probably desired. Having a specific path in the TeX file is not convenient.
439     os << "    \\put(0,0){\\includegraphics[width=\\unitlength]{" << _filename << "}}%\n";
441     fprintf(_stream, "%s", os.str().c_str());
443     return true;
446 Geom::Matrix const &
447 LaTeXTextRenderer::transform()
449     return _transform_stack.top();
452 void
453 LaTeXTextRenderer::push_transform(Geom::Matrix const &tr)
455     if(_transform_stack.size()){
456         Geom::Matrix tr_top = _transform_stack.top();
457         _transform_stack.push(tr * tr_top);
458     } else {
459         _transform_stack.push(tr);
460     }
463 void
464 LaTeXTextRenderer::pop_transform()
466     _transform_stack.pop();
469 }  /* namespace Internal */
470 }  /* namespace Extension */
471 }  /* namespace Inkscape */
473 /*
474   Local Variables:
475   mode:c++
476   c-file-style:"stroustrup"
477   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
478   indent-tabs-mode:nil
479   fill-column:99
480   End:
481 */
482 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :