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