Code

fix bug: don't output scientific notation numbers to latex
[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;
129         
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, "%%%%");
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 "%%   \\setlength{\\unitlength}{<desired width>}\n"
177 "%%   \\input{<filename>.tex}\n"
178 "%% instead of\n"
179 "%%   \\includegraphics[width=<desired width>]{<filename>.pdf}\n"
180 "\n"
181 "\\begingroup                                                                              \n"
182 "  \\makeatletter                                                                          \n"
183 "  \\providecommand\\color[2][]{%                                                          \n"
184 "    \\GenericError{(Inkscape) \\space\\space\\@spaces}{%                                  \n"
185 "      Color is used for the text in Inkscape, but the color package color is not loaded.  \n"
186 "    }{Either use black text in Inkscape or load the package                               \n"
187 "      color.sty in LaTeX.}%                                                               \n"
188 "    \\renewcommand\\color[2][]{}%                                                         \n"
189 "  }%%                                                                                     \n"
190 "  \\providecommand\\rotatebox[2]{#2}%                                                     \n"
191 "  \\makeatother                                                                           \n";
193 static char const postamble[] =
194 "  \\end{picture}%                                                                          \n"
195 "\\endgroup                                                                                 \n";
197 void
198 LaTeXTextRenderer::writePreamble()
200     fprintf(_stream, "%s", preamble);
202 void
203 LaTeXTextRenderer::writePostamble()
205     fprintf(_stream, "%s", postamble);
208 void
209 LaTeXTextRenderer::sp_group_render(SPItem *item)
211     SPGroup *group = SP_GROUP(item);
213     GSList *l = g_slist_reverse(group->childList(false));
214     while (l) {
215         SPObject *o = SP_OBJECT (l->data);
216         if (SP_IS_ITEM(o)) {
217             renderItem (SP_ITEM (o));
218         }
219         l = g_slist_remove (l, o);
220     }
223 void
224 LaTeXTextRenderer::sp_use_render(SPItem *item)
226     bool translated = false;
227     SPUse *use = SP_USE(item);
229     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
230         Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
231         push_transform(tp);
232         translated = true;
233     }
235     if (use->child && SP_IS_ITEM(use->child)) {
236         renderItem(SP_ITEM(use->child));
237     }
239     if (translated) {
240         pop_transform();
241     }
244 void
245 LaTeXTextRenderer::sp_text_render(SPItem *item)
247     SPText *textobj = SP_TEXT (item);
248     SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item));
250     gchar *str = sp_te_get_string_multiline(item);
252     // get position and alignment
253     Geom::Point pos;
254     gchar *alignment = NULL;
255     Geom::OptRect bbox = item->getBounds(transform());
256     Geom::Interval bbox_x = (*bbox)[Geom::X];
257     Geom::Interval bbox_y = (*bbox)[Geom::Y];
258     switch (style->text_anchor.computed) {
259     case SP_CSS_TEXT_ANCHOR_START:
260         pos = Geom::Point( bbox_x.min() , bbox_y.middle() );
261         alignment = "[l]";
262         break;
263     case SP_CSS_TEXT_ANCHOR_END:
264         pos = Geom::Point( bbox_x.max() , bbox_y.middle() );
265         alignment = "[r]";
266         break;
267     case SP_CSS_TEXT_ANCHOR_MIDDLE:
268     default:
269         pos = bbox->midpoint();
270         alignment = "";
271         break;
272     }
274     // determine color (for now, use rgb color model as it is most native to Inkscape)
275     bool has_color = false; // if the item has no color set, don't force black color
276     // TODO: how to handle ICC colors?
277     // give priority to fill color
278     guint32 rgba = 0;
279     if (style->fill.set && style->fill.isColor()) { 
280         has_color = true;
281         rgba = style->fill.value.color.toRGBA32(1.);
282     } else if (style->stroke.set && style->stroke.isColor()) { 
283         has_color = true;
284         rgba = style->stroke.value.color.toRGBA32(1.);
285     }
288     // get rotation
289     Geom::Matrix i2doc = sp_item_i2doc_affine(item);
290     Geom::Matrix wotransl = i2doc.without_translation();
291     double degrees = -180/M_PI * Geom::atan2(wotransl.xAxis());
292     bool has_rotation = !Geom::are_near(degrees,0.);
294     // write to LaTeX
295     Inkscape::SVGOStringStream os;
296     os.setf(std::ios::fixed); // don't use scientific notation
298     os << "    \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){";
299     if (has_color) {
300         os << "\\color[rgb]{" << SP_RGBA32_R_F(rgba) << "," << SP_RGBA32_G_F(rgba) << "," << SP_RGBA32_B_F(rgba) << "}";
301     }
302     os << "\\makebox(0,0)" << alignment << "{";
303     if (has_rotation) {
304         os << "\\rotatebox{" << degrees << "}{";
305     }
306     os <<   str;
307     if (has_rotation) {
308         os << "}"; // rotatebox end
309     }
310     os << "}"; //makebox end
311     os << "}%\n"; // put end
313     fprintf(_stream, "%s", os.str().c_str());
316 void
317 LaTeXTextRenderer::sp_flowtext_render(SPItem *item)
319 /*    SPFlowtext *group = SP_FLOWTEXT(item);
321     // write to LaTeX
322     Inkscape::SVGOStringStream os;
323     os.setf(std::ios::fixed); // no scientific notation
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 */
334 void
335 LaTeXTextRenderer::sp_root_render(SPItem *item)
337     SPRoot *root = SP_ROOT(item);
339     push_transform(root->c2p);
340     sp_group_render(item);
341     pop_transform();
344 void
345 LaTeXTextRenderer::sp_item_invoke_render(SPItem *item)
347     // Check item's visibility
348     if (item->isHidden()) {
349         return;
350     }
352     if (SP_IS_ROOT(item)) {
353         return sp_root_render(item);
354     } else if (SP_IS_GROUP(item)) {
355         return sp_group_render(item);
356     } else if (SP_IS_USE(item)) {
357         sp_use_render(item);
358     } else if (SP_IS_TEXT(item)) {
359         return sp_text_render(item);
360     } else if (SP_IS_FLOWTEXT(item)) {
361         return sp_flowtext_render(item);
362     }
363     // We are not interested in writing the other SPItem types to LaTeX
366 void
367 LaTeXTextRenderer::renderItem(SPItem *item)
369     push_transform(item->transform);
370     sp_item_invoke_render(item);
371     pop_transform();
374 bool
375 LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem *base)
377 // The boundingbox calculation here should be exactly the same as the one by CairoRenderer::setupDocument !
379     if (!base)
380         base = SP_ITEM(sp_document_root(doc));
382     Geom::OptRect d;
383     if (pageBoundingBox) {
384         d = Geom::Rect( Geom::Point(0,0),
385                         Geom::Point(sp_document_width(doc), sp_document_height(doc)) );
386     } else {
387         sp_item_invoke_bbox(base, d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
388     }
390     // scale all coordinates, such that the width of the image is 1, this is convenient for scaling the image in LaTeX
391     double scale = 1/(d->width());
392     double _width = d->width() * scale;
393     double _height = d->height() * scale;
394     push_transform( Geom::Scale(scale, scale) );
396     if (!pageBoundingBox)
397     {
398         double high = sp_document_height(doc);
400         push_transform( Geom::Translate( - d->min() ) );
401     }
403     // flip y-axis
404     push_transform( Geom::Scale(1,-1) * Geom::Translate(0, sp_document_height(doc)) );
406     // write the info to LaTeX
407     Inkscape::SVGOStringStream os;
408     os.setf(std::ios::fixed); // no scientific notation
410     // also write original width to LaTeX
411     // TODO: add \ifdef statements to be able to choose between specifying width or not to specify it!
412     os << "  %\\setlength{\\unitlength}{" << d->width() * PT_PER_PX << "pt}\n";
414     os << "  \\begin{picture}(" << _width << "," << _height << ")%\n";
415     // strip pathname, as it is probably desired. Having a specific path in the TeX file is not convenient.
416     os << "    \\put(0,0){\\includegraphics[width=\\unitlength]{" << _filename << "}}%\n";
418     fprintf(_stream, "%s", os.str().c_str());
420     return true;
423 Geom::Matrix const &
424 LaTeXTextRenderer::transform()
426     return _transform_stack.top();
429 void
430 LaTeXTextRenderer::push_transform(Geom::Matrix const &tr)
432     if(_transform_stack.size()){
433         Geom::Matrix tr_top = _transform_stack.top();
434         _transform_stack.push(tr * tr_top);
435     } else {
436         _transform_stack.push(tr);
437     }
440 void
441 LaTeXTextRenderer::pop_transform()
443     _transform_stack.pop();
446 }  /* namespace Internal */
447 }  /* namespace Extension */
448 }  /* namespace Inkscape */
450 /*
451   Local Variables:
452   mode:c++
453   c-file-style:"stroustrup"
454   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
455   indent-tabs-mode:nil
456   fill-column:99
457   End:
458 */
459 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :