d44652419dbf7b0670c18ee9c8a324d429e504b8
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());
100 }
102 LaTeXTextRenderer::~LaTeXTextRenderer(void)
103 {
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;
120 }
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;
172 }
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()
202 {
203 fprintf(_stream, "%s", preamble);
204 }
205 void
206 LaTeXTextRenderer::writePostamble()
207 {
208 fprintf(_stream, "%s", postamble);
209 }
211 void
212 LaTeXTextRenderer::sp_group_render(SPItem *item)
213 {
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 }
224 }
226 void
227 LaTeXTextRenderer::sp_use_render(SPItem *item)
228 {
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 }
245 }
247 void
248 LaTeXTextRenderer::sp_text_render(SPItem *item)
249 {
250 SPText *textobj = SP_TEXT (item);
251 SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item));
253 gchar *str = sp_te_get_string_multiline(item);
255 // get position and alignment
256 // Align vertically on the baseline of the font (retreived from the anchor point)
257 // Align horizontally on boundingbox
258 Geom::Coord pos_x;
259 gchar *alignment = NULL;
260 Geom::OptRect bbox = item->getBounds(transform());
261 Geom::Interval bbox_x = (*bbox)[Geom::X];
262 switch (style->text_anchor.computed) {
263 case SP_CSS_TEXT_ANCHOR_START:
264 pos_x = bbox_x.min();
265 alignment = "[lb]";
266 break;
267 case SP_CSS_TEXT_ANCHOR_END:
268 pos_x = bbox_x.max();
269 alignment = "[rb]";
270 break;
271 case SP_CSS_TEXT_ANCHOR_MIDDLE:
272 default:
273 pos_x = bbox_x.middle();
274 alignment = "[b]";
275 break;
276 }
277 Geom::Point anchor = textobj->attributes.firstXY() * transform();
278 // If we want to align horizontally on bbox: Geom::Point pos(pos_x, anchor[Geom::Y]);
279 Geom::Point pos(anchor[Geom::X], anchor[Geom::Y]);
281 // determine color (for now, use rgb color model as it is most native to Inkscape)
282 bool has_color = false; // if the item has no color set, don't force black color
283 // TODO: how to handle ICC colors?
284 // give priority to fill color
285 guint32 rgba = 0;
286 if (style->fill.set && style->fill.isColor()) {
287 has_color = true;
288 rgba = style->fill.value.color.toRGBA32(1.);
289 } else if (style->stroke.set && style->stroke.isColor()) {
290 has_color = true;
291 rgba = style->stroke.value.color.toRGBA32(1.);
292 }
295 // get rotation
296 Geom::Matrix i2doc = sp_item_i2doc_affine(item);
297 Geom::Matrix wotransl = i2doc.without_translation();
298 double degrees = -180/M_PI * Geom::atan2(wotransl.xAxis());
299 bool has_rotation = !Geom::are_near(degrees,0.);
301 // write to LaTeX
302 Inkscape::SVGOStringStream os;
303 os.setf(std::ios::fixed); // don't use scientific notation
305 os << " \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){";
306 if (has_color) {
307 os << "\\color[rgb]{" << SP_RGBA32_R_F(rgba) << "," << SP_RGBA32_G_F(rgba) << "," << SP_RGBA32_B_F(rgba) << "}";
308 }
309 if (has_rotation) {
310 os << "\\rotatebox{" << degrees << "}{";
311 }
312 os << "\\makebox(0,0)" << alignment << "{";
313 os << "\\smash{" << str << "}"; // smash the text, to be able to put the makebox coordinates at the baseline
314 if (has_rotation) {
315 os << "}"; // rotatebox end
316 }
317 os << "}"; //makebox end
318 os << "}%\n"; // put end
320 fprintf(_stream, "%s", os.str().c_str());
321 }
323 void
324 LaTeXTextRenderer::sp_flowtext_render(SPItem *item)
325 {
326 /* SPFlowtext *group = SP_FLOWTEXT(item);
328 // write to LaTeX
329 Inkscape::SVGOStringStream os;
330 os.setf(std::ios::fixed); // no scientific notation
332 os << " \\begin{picture}(" << _width << "," << _height << ")%%\n";
333 os << " \\gplgaddtomacro\\gplbacktext{%%\n";
334 os << " \\csname LTb\\endcsname%%\n";
335 os << "\\put(0,0){\\makebox(0,0)[lb]{\\strut{}Position}}%%\n";
337 fprintf(_stream, "%s", os.str().c_str());
338 */
339 }
341 void
342 LaTeXTextRenderer::sp_root_render(SPItem *item)
343 {
344 SPRoot *root = SP_ROOT(item);
346 push_transform(root->c2p);
347 sp_group_render(item);
348 pop_transform();
349 }
351 void
352 LaTeXTextRenderer::sp_item_invoke_render(SPItem *item)
353 {
354 // Check item's visibility
355 if (item->isHidden()) {
356 return;
357 }
359 if (SP_IS_ROOT(item)) {
360 return sp_root_render(item);
361 } else if (SP_IS_GROUP(item)) {
362 return sp_group_render(item);
363 } else if (SP_IS_USE(item)) {
364 sp_use_render(item);
365 } else if (SP_IS_TEXT(item)) {
366 return sp_text_render(item);
367 } else if (SP_IS_FLOWTEXT(item)) {
368 return sp_flowtext_render(item);
369 }
370 // We are not interested in writing the other SPItem types to LaTeX
371 }
373 void
374 LaTeXTextRenderer::renderItem(SPItem *item)
375 {
376 push_transform(item->transform);
377 sp_item_invoke_render(item);
378 pop_transform();
379 }
381 bool
382 LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem *base)
383 {
384 // The boundingbox calculation here should be exactly the same as the one by CairoRenderer::setupDocument !
386 if (!base)
387 base = SP_ITEM(sp_document_root(doc));
389 Geom::OptRect d;
390 if (pageBoundingBox) {
391 d = Geom::Rect( Geom::Point(0,0),
392 Geom::Point(sp_document_width(doc), sp_document_height(doc)) );
393 } else {
394 sp_item_invoke_bbox(base, d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
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 double high = sp_document_height(doc);
407 push_transform( Geom::Translate( - d->min() ) );
408 }
410 // flip y-axis
411 push_transform( Geom::Scale(1,-1) * Geom::Translate(0, sp_document_height(doc)) );
413 // write the info to LaTeX
414 Inkscape::SVGOStringStream os;
415 os.setf(std::ios::fixed); // no scientific notation
417 // scaling of the image when including it in LaTeX
419 os << " \\ifx \\svgwidth \\@empty\n";
420 os << " \\setlength{\\unitlength}{" << d->width() * PT_PER_PX << "pt}\n";
421 os << " \\else\n";
422 os << " \\setlength{\\unitlength}{\\svgwidth}\n";
423 os << " \\fi\n";
424 os << " \\global\\let\\svgwidth\\@empty\n";
425 os << " \\makeatother\n";
427 os << " \\begin{picture}(" << _width << "," << _height << ")%\n";
428 // strip pathname, as it is probably desired. Having a specific path in the TeX file is not convenient.
429 os << " \\put(0,0){\\includegraphics[width=\\unitlength]{" << _filename << "}}%\n";
431 fprintf(_stream, "%s", os.str().c_str());
433 return true;
434 }
436 Geom::Matrix const &
437 LaTeXTextRenderer::transform()
438 {
439 return _transform_stack.top();
440 }
442 void
443 LaTeXTextRenderer::push_transform(Geom::Matrix const &tr)
444 {
445 if(_transform_stack.size()){
446 Geom::Matrix tr_top = _transform_stack.top();
447 _transform_stack.push(tr * tr_top);
448 } else {
449 _transform_stack.push(tr);
450 }
451 }
453 void
454 LaTeXTextRenderer::pop_transform()
455 {
456 _transform_stack.pop();
457 }
459 } /* namespace Internal */
460 } /* namespace Extension */
461 } /* namespace Inkscape */
463 /*
464 Local Variables:
465 mode:c++
466 c-file-style:"stroustrup"
467 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
468 indent-tabs-mode:nil
469 fill-column:99
470 End:
471 */
472 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :