1fdf1d7fd04b5c0fb4bb2cb06002d573328d1935
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 "sp-rect.h"
40 #include "text-editing.h"
42 #include <unit-constants.h>
44 #include "extension/system.h"
46 #include "io/sys.h"
48 namespace Inkscape {
49 namespace Extension {
50 namespace Internal {
52 /**
53 * This method is called by the PDF, EPS and PS output extensions.
54 * @param filename This should be the filename without extension to which the tex code should be written. Output goes to <filename>.tex.
55 */
56 bool
57 latex_render_document_text_to_file( SPDocument *doc, gchar const *filename,
58 const gchar * const exportId, bool exportDrawing, bool exportCanvas,
59 bool pdflatex)
60 {
61 sp_document_ensure_up_to_date(doc);
63 SPItem *base = NULL;
65 bool pageBoundingBox = true;
66 if (exportId && strcmp(exportId, "")) {
67 // we want to export the given item only
68 base = SP_ITEM(doc->getObjectById(exportId));
69 pageBoundingBox = exportCanvas;
70 }
71 else {
72 // we want to export the entire document from root
73 base = SP_ITEM(sp_document_root(doc));
74 pageBoundingBox = !exportDrawing;
75 }
77 if (!base)
78 return false;
80 /* Create renderer */
81 LaTeXTextRenderer *renderer = new LaTeXTextRenderer(pdflatex);
83 bool ret = renderer->setTargetFile(filename);
84 if (ret) {
85 /* Render document */
86 bool ret = renderer->setupDocument(doc, pageBoundingBox, base);
87 if (ret) {
88 renderer->renderItem(base);
89 }
90 }
92 delete renderer;
94 return ret;
95 }
97 LaTeXTextRenderer::LaTeXTextRenderer(bool pdflatex)
98 : _stream(NULL),
99 _filename(NULL),
100 _pdflatex(pdflatex)
101 {
102 push_transform(Geom::identity());
103 }
105 LaTeXTextRenderer::~LaTeXTextRenderer(void)
106 {
107 if (_stream) {
108 writePostamble();
110 fclose(_stream);
111 }
113 /* restore default signal handling for SIGPIPE */
114 #if !defined(_WIN32) && !defined(__WIN32__)
115 (void) signal(SIGPIPE, SIG_DFL);
116 #endif
118 if (_filename) {
119 g_free(_filename);
120 }
122 return;
123 }
125 /** This should create the output LaTeX file, and assign it to _stream.
126 * @return Returns true when succesfull
127 */
128 bool
129 LaTeXTextRenderer::setTargetFile(gchar const *filename) {
130 if (filename != NULL) {
131 while (isspace(*filename)) filename += 1;
133 _filename = g_path_get_basename(filename);
135 gchar *filename_ext = g_strdup_printf("%s.tex", filename);
136 Inkscape::IO::dump_fopen_call(filename_ext, "K");
137 FILE *osf = Inkscape::IO::fopen_utf8name(filename_ext, "w+");
138 if (!osf) {
139 fprintf(stderr, "inkscape: fopen(%s): %s\n",
140 filename_ext, strerror(errno));
141 return false;
142 }
143 _stream = osf;
144 g_free(filename_ext);
145 }
147 if (_stream) {
148 /* fixme: this is kinda icky */
149 #if !defined(_WIN32) && !defined(__WIN32__)
150 (void) signal(SIGPIPE, SIG_IGN);
151 #endif
152 }
154 fprintf(_stream, "%%%% Creator: Inkscape %s, www.inkscape.org\n", PACKAGE_STRING);
155 fprintf(_stream, "%%%% PDF/EPS/PS + LaTeX output extension by Johan Engelen, 2010\n");
156 fprintf(_stream, "%%%% Accompanies image file '%s' (pdf, eps, ps)\n", _filename);
157 fprintf(_stream, "%%%%\n");
158 /* flush this to test output stream as early as possible */
159 if (fflush(_stream)) {
160 if (ferror(_stream)) {
161 g_print("Error %d on LaTeX file output stream: %s\n", errno,
162 g_strerror(errno));
163 }
164 g_print("Output to LaTeX file failed\n");
165 /* fixme: should use pclose() for pipes */
166 fclose(_stream);
167 _stream = NULL;
168 fflush(stdout);
169 return false;
170 }
172 writePreamble();
174 return true;
175 }
177 static char const preamble[] =
178 "%% To include the image in your LaTeX document, write\n"
179 "%% \\input{<filename>.tex}\n"
180 "%% instead of\n"
181 "%% \\includegraphics{<filename>.pdf}\n"
182 "%% To scale the image, write\n"
183 "%% \\def{\\svgwidth}{<desired width>}\n"
184 "%% \\input{<filename>.tex}\n"
185 "%% instead of\n"
186 "%% \\includegraphics[width=<desired width>]{<filename>.pdf}\n"
187 "\n"
188 "\\begingroup\n"
189 " \\makeatletter\n"
190 " \\providecommand\\color[2][]{%\n"
191 " \\errmessage{(Inkscape) Color is used for the text in Inkscape, but the package \'color.sty\' is not loaded}\n"
192 " \\renewcommand\\color[2][]{}%\n"
193 " }\n"
194 " \\providecommand\\transparent[1]{%\n"
195 " \\errmessage{(Inkscape) Transparency is used (non-zero) for the text in Inkscape, but the package \'transparent.sty\' is not loaded}\n"
196 " \\renewcommand\\transparent[1]{}%\n"
197 " }\n"
198 " \\providecommand\\rotatebox[2]{#2}\n";
200 static char const postamble[] =
201 " \\end{picture}%\n"
202 "\\endgroup\n";
204 void
205 LaTeXTextRenderer::writePreamble()
206 {
207 fprintf(_stream, "%s", preamble);
208 }
209 void
210 LaTeXTextRenderer::writePostamble()
211 {
212 fprintf(_stream, "%s", postamble);
213 }
215 void
216 LaTeXTextRenderer::sp_group_render(SPItem *item)
217 {
218 SPGroup *group = SP_GROUP(item);
220 GSList *l = g_slist_reverse(group->childList(false));
221 while (l) {
222 SPObject *o = SP_OBJECT (l->data);
223 if (SP_IS_ITEM(o)) {
224 renderItem (SP_ITEM (o));
225 }
226 l = g_slist_remove (l, o);
227 }
228 }
230 void
231 LaTeXTextRenderer::sp_use_render(SPItem *item)
232 {
233 bool translated = false;
234 SPUse *use = SP_USE(item);
236 if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
237 Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
238 push_transform(tp);
239 translated = true;
240 }
242 if (use->child && SP_IS_ITEM(use->child)) {
243 renderItem(SP_ITEM(use->child));
244 }
246 if (translated) {
247 pop_transform();
248 }
249 }
251 void
252 LaTeXTextRenderer::sp_text_render(SPItem *item)
253 {
254 SPText *textobj = SP_TEXT (item);
255 SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item));
257 gchar *str = sp_te_get_string_multiline(item);
258 if (!str) {
259 return;
260 }
262 // get position and alignment
263 // Align vertically on the baseline of the font (retreived from the anchor point)
264 // Align horizontally on anchorpoint
265 gchar const *alignment = NULL;
266 switch (style->text_anchor.computed) {
267 case SP_CSS_TEXT_ANCHOR_START:
268 alignment = "[lb]";
269 break;
270 case SP_CSS_TEXT_ANCHOR_END:
271 alignment = "[rb]";
272 break;
273 case SP_CSS_TEXT_ANCHOR_MIDDLE:
274 default:
275 alignment = "[b]";
276 break;
277 }
278 Geom::Point anchor = textobj->attributes.firstXY() * transform();
279 Geom::Point pos(anchor);
281 // determine color and transparency (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 bool has_transparency = false;
284 // TODO: how to handle ICC colors?
285 // give priority to fill color
286 guint32 rgba = 0;
287 float opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
288 if (style->fill.set && style->fill.isColor()) {
289 has_color = true;
290 rgba = style->fill.value.color.toRGBA32(1.);
291 opacity *= SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
292 } else if (style->stroke.set && style->stroke.isColor()) {
293 has_color = true;
294 rgba = style->stroke.value.color.toRGBA32(1.);
295 opacity *= SP_SCALE24_TO_FLOAT(style->stroke_opacity.value);
296 }
297 if (opacity < 1.0) {
298 has_transparency = true;
299 }
301 // get rotation
302 Geom::Matrix i2doc = sp_item_i2doc_affine(item);
303 Geom::Matrix wotransl = i2doc.without_translation();
304 double degrees = -180/M_PI * Geom::atan2(wotransl.xAxis());
305 bool has_rotation = !Geom::are_near(degrees,0.);
307 // write to LaTeX
308 Inkscape::SVGOStringStream os;
309 os.setf(std::ios::fixed); // don't use scientific notation
311 os << " \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){";
312 if (has_color) {
313 os << "\\color[rgb]{" << SP_RGBA32_R_F(rgba) << "," << SP_RGBA32_G_F(rgba) << "," << SP_RGBA32_B_F(rgba) << "}";
314 }
315 if (_pdflatex && has_transparency) {
316 os << "\\transparent{" << opacity << "}";
317 }
318 if (has_rotation) {
319 os << "\\rotatebox{" << degrees << "}{";
320 }
321 os << "\\makebox(0,0)" << alignment << "{";
322 os << "\\smash{" << str << "}"; // smash the text, to be able to put the makebox coordinates at the baseline
323 if (has_rotation) {
324 os << "}"; // rotatebox end
325 }
326 os << "}"; //makebox end
327 os << "}%\n"; // put end
329 fprintf(_stream, "%s", os.str().c_str());
330 }
332 void
333 LaTeXTextRenderer::sp_flowtext_render(SPItem * item)
334 {
335 /*
336 Flowtext is possible by using a minipage! :)
337 Flowing in rectangle is possible, not in arb shape.
338 */
340 SPFlowtext *flowtext = SP_FLOWTEXT(item);
341 SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item));
343 gchar *strtext = sp_te_get_string_multiline(item);
344 if (!strtext) {
345 return;
346 }
347 // replace carriage return with double slash
348 gchar ** splitstr = g_strsplit(strtext, "\n", -1);
349 gchar *str = g_strjoinv("\\\\ ", splitstr);
350 g_free(strtext);
351 g_strfreev(splitstr);
353 if (!flowtext->has_internal_frame()) {
354 // has_internal_frame includes a check that frame is a SPRect
355 g_warning("LaTeX export: non-rectangular flowed text shapes are not supported, skipping text.");
356 return; // don't know how to handle non-rect frames yet. is quite uncommon for latex users i think
357 }
359 SPRect *frame = SP_RECT(flowtext->get_frame(NULL));
360 Geom::Rect framebox = sp_rect_get_rect(frame) * transform();
362 // get position and alignment
363 // Align on topleft corner.
364 gchar const *alignment = "[lt]";
365 gchar const *justification = "";
366 switch (flowtext->layout.paragraphAlignment(flowtext->layout.begin())) {
367 case Inkscape::Text::Layout::LEFT:
368 justification = "\\raggedright";
369 break;
370 case Inkscape::Text::Layout::RIGHT:
371 justification = "\\raggedleft";
372 break;
373 case Inkscape::Text::Layout::CENTER:
374 justification = "\\centering";
375 case Inkscape::Text::Layout::FULL:
376 default:
377 // no need to add LaTeX code for standard justified output :)
378 break;
379 }
380 Geom::Point pos(framebox.corner(3)); //topleft corner
382 // determine color and transparency (for now, use rgb color model as it is most native to Inkscape)
383 bool has_color = false; // if the item has no color set, don't force black color
384 bool has_transparency = false;
385 // TODO: how to handle ICC colors?
386 // give priority to fill color
387 guint32 rgba = 0;
388 float opacity = SP_SCALE24_TO_FLOAT(style->opacity.value);
389 if (style->fill.set && style->fill.isColor()) {
390 has_color = true;
391 rgba = style->fill.value.color.toRGBA32(1.);
392 opacity *= SP_SCALE24_TO_FLOAT(style->fill_opacity.value);
393 } else if (style->stroke.set && style->stroke.isColor()) {
394 has_color = true;
395 rgba = style->stroke.value.color.toRGBA32(1.);
396 opacity *= SP_SCALE24_TO_FLOAT(style->stroke_opacity.value);
397 }
398 if (opacity < 1.0) {
399 has_transparency = true;
400 }
402 // get rotation
403 Geom::Matrix i2doc = sp_item_i2doc_affine(item);
404 Geom::Matrix wotransl = i2doc.without_translation();
405 double degrees = -180/M_PI * Geom::atan2(wotransl.xAxis());
406 bool has_rotation = !Geom::are_near(degrees,0.);
408 // write to LaTeX
409 Inkscape::SVGOStringStream os;
410 os.setf(std::ios::fixed); // don't use scientific notation
412 os << " \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){";
413 if (has_color) {
414 os << "\\color[rgb]{" << SP_RGBA32_R_F(rgba) << "," << SP_RGBA32_G_F(rgba) << "," << SP_RGBA32_B_F(rgba) << "}";
415 }
416 if (_pdflatex && has_transparency) {
417 os << "\\transparent{" << opacity << "}";
418 }
419 if (has_rotation) {
420 os << "\\rotatebox{" << degrees << "}{";
421 }
422 os << "\\makebox(0,0)" << alignment << "{";
423 os << "\\begin{minipage}{" << framebox.width() << "\\unitlength}";
424 os << justification;
425 os << str;
426 os << "\\end{minipage}";
427 if (has_rotation) {
428 os << "}"; // rotatebox end
429 }
430 os << "}"; //makebox end
431 os << "}%\n"; // put end
433 fprintf(_stream, "%s", os.str().c_str());
434 }
436 void
437 LaTeXTextRenderer::sp_root_render(SPItem *item)
438 {
439 SPRoot *root = SP_ROOT(item);
441 push_transform(root->c2p);
442 sp_group_render(item);
443 pop_transform();
444 }
446 void
447 LaTeXTextRenderer::sp_item_invoke_render(SPItem *item)
448 {
449 // Check item's visibility
450 if (item->isHidden()) {
451 return;
452 }
454 if (SP_IS_ROOT(item)) {
455 return sp_root_render(item);
456 } else if (SP_IS_GROUP(item)) {
457 return sp_group_render(item);
458 } else if (SP_IS_USE(item)) {
459 sp_use_render(item);
460 } else if (SP_IS_TEXT(item)) {
461 return sp_text_render(item);
462 } else if (SP_IS_FLOWTEXT(item)) {
463 return sp_flowtext_render(item);
464 }
465 // We are not interested in writing the other SPItem types to LaTeX
466 }
468 void
469 LaTeXTextRenderer::renderItem(SPItem *item)
470 {
471 push_transform(item->transform);
472 sp_item_invoke_render(item);
473 pop_transform();
474 }
476 bool
477 LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem *base)
478 {
479 // The boundingbox calculation here should be exactly the same as the one by CairoRenderer::setupDocument !
481 if (!base)
482 base = SP_ITEM(sp_document_root(doc));
484 Geom::OptRect d;
485 if (pageBoundingBox) {
486 d = Geom::Rect( Geom::Point(0,0),
487 Geom::Point(sp_document_width(doc), sp_document_height(doc)) );
488 } else {
489 sp_item_invoke_bbox(base, d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX);
490 }
491 if (!d) {
492 g_message("LaTeXTextRenderer: could not retrieve boundingbox.");
493 return false;
494 }
496 // scale all coordinates, such that the width of the image is 1, this is convenient for scaling the image in LaTeX
497 double scale = 1/(d->width());
498 double _width = d->width() * scale;
499 double _height = d->height() * scale;
500 push_transform( Geom::Scale(scale, scale) );
502 if (!pageBoundingBox)
503 {
504 Geom::Translate t(-d->min()[Geom::X], d->max()[Geom::Y] - sp_document_height(doc));
505 push_transform( t );
506 }
508 // flip y-axis
509 push_transform( Geom::Scale(1,-1) * Geom::Translate(0, sp_document_height(doc)) );
511 // write the info to LaTeX
512 Inkscape::SVGOStringStream os;
513 os.setf(std::ios::fixed); // no scientific notation
515 // scaling of the image when including it in LaTeX
517 os << " \\ifx\\svgwidth\\undefined\n";
518 os << " \\setlength{\\unitlength}{" << d->width() * PT_PER_PX << "pt}\n";
519 os << " \\else\n";
520 os << " \\setlength{\\unitlength}{\\svgwidth}\n";
521 os << " \\fi\n";
522 os << " \\global\\let\\svgwidth\\undefined\n";
523 os << " \\makeatother\n";
525 os << " \\begin{picture}(" << _width << "," << _height << ")%\n";
526 // strip pathname, as it is probably desired. Having a specific path in the TeX file is not convenient.
527 os << " \\put(0,0){\\includegraphics[width=\\unitlength]{" << _filename << "}}%\n";
529 fprintf(_stream, "%s", os.str().c_str());
531 return true;
532 }
534 Geom::Matrix const &
535 LaTeXTextRenderer::transform()
536 {
537 return _transform_stack.top();
538 }
540 void
541 LaTeXTextRenderer::push_transform(Geom::Matrix const &tr)
542 {
543 if(_transform_stack.size()){
544 Geom::Matrix tr_top = _transform_stack.top();
545 _transform_stack.push(tr * tr_top);
546 } else {
547 _transform_stack.push(tr);
548 }
549 }
551 void
552 LaTeXTextRenderer::pop_transform()
553 {
554 _transform_stack.pop();
555 }
557 } /* namespace Internal */
558 } /* namespace Extension */
559 } /* namespace Inkscape */
561 /*
562 Local Variables:
563 mode:c++
564 c-file-style:"stroustrup"
565 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
566 indent-tabs-mode:nil
567 fill-column:99
568 End:
569 */
570 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :