pdflatex-renderer.cpp revision 9fc8e85e3459185c416e667372cd4b38c723c7ab
/** \file
* Rendering LaTeX file (pdf+latex output)
*/
/*
* Authors:
* Johan Engelen <goejendaagh@zonnet.nl>
* Miklos Erdelyi <erdelyim@gmail.com>
*
* Copyright (C) 2006-2010 Authors
*
* Most of the pre- and postamble is copied from GNUPlot's epslatex terminal output :-)
*
* Licensed under GNU GPL
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifndef PANGO_ENABLE_BACKEND
#define PANGO_ENABLE_BACKEND
#endif
#ifndef PANGO_ENABLE_ENGINE
#define PANGO_ENABLE_ENGINE
#endif
#include <signal.h>
#include <errno.h>
#include "libnrtype/Layout-TNG.h"
#include "display/nr-arena.h"
#include "display/nr-arena-item.h"
#include "display/nr-arena-group.h"
#include "display/canvas-bpath.h"
#include "sp-item.h"
#include "sp-item-group.h"
#include "style.h"
#include "marker.h"
#include "sp-linear-gradient.h"
#include "sp-radial-gradient.h"
#include "sp-root.h"
#include "sp-use.h"
#include "sp-text.h"
#include "sp-flowtext.h"
#include "sp-mask.h"
#include "sp-clippath.h"
#include <unit-constants.h>
#include "helper/png-write.h"
#include "helper/pixbuf-ops.h"
#include "pdflatex-renderer.h"
#include <cairo.h>
// include support for only the compiled-in surface types
#ifdef CAIRO_HAS_PDF_SURFACE
#include <cairo-pdf.h>
#endif
#ifdef CAIRO_HAS_PS_SURFACE
#include <cairo-ps.h>
#endif
//#define TRACE(_args) g_printf _args
//#define TEST(_args) _args
// FIXME: expose these from sp-clippath/mask.cpp
struct SPClipPathView {
unsigned int key;
};
struct SPMaskView {
unsigned int key;
};
namespace Inkscape {
namespace Extension {
namespace Internal {
PDFLaTeXRenderer::PDFLaTeXRenderer(void)
_width(0),
_height(0)
{}
PDFLaTeXRenderer::~PDFLaTeXRenderer(void)
{
if (_stream) {
}
/* restore default signal handling for SIGPIPE */
#endif
if (_filename) {
}
return;
}
/** This should create the output LaTeX file, and assign it to _stream.
* @return Returns true when succesfull
*/
bool
if (!osf) {
return false;
}
}
if (_stream) {
/* fixme: this is kinda icky */
#endif
}
/* flush this to test output stream as early as possible */
g_strerror(errno));
}
g_print("Output to LaTeX file failed\n");
/* fixme: should use pclose() for pipes */
return false;
}
return true;
}
/* Most of this preamble is copied from GNUPlot's epslatex terminal output :-) */
static char const preamble[] =
"\\begingroup \n"
" \\makeatletter \n"
" \\providecommand\\color[2][]{%% \n"
" \\GenericError{(gnuplot) \\space\\space\\space\\@spaces}{%% \n"
" Package color not loaded in conjunction with \n"
" terminal option `colourtext'%% \n"
" }{See the gnuplot documentation for explanation.%% \n"
" }{Either use 'blacktext' in gnuplot or load the package \n"
" color.sty in LaTeX.}%% \n"
" \\renewcommand\\color[2][]{}%% \n"
" }%% \n"
" \\providecommand\\includegraphics[2][]{%% \n"
" \\GenericError{(gnuplot) \\space\\space\\space\\@spaces}{%% \n"
" Package graphicx or graphics not loaded%% \n"
" }{See the gnuplot documentation for explanation.%% \n"
" }{The gnuplot epslatex terminal needs graphicx.sty or graphics.sty.}%% \n"
" \\renewcommand\\includegraphics[2][]{}%% \n"
" }%% \n"
" \\providecommand\\rotatebox[2]{#2}%% \n"
" \\@ifundefined{ifGPcolor}{%% \n"
" \\newif\\ifGPcolor \n"
" \\GPcolorfalse \n"
" }{}%% \n"
" \\@ifundefined{ifGPblacktext}{%% \n"
" \\newif\\ifGPblacktext \n"
" \\GPblacktexttrue \n"
" }{}%% \n"
" %% define a \\g@addto@macro without @ in the name: \n"
" \\let\\gplgaddtomacro\\g@addto@macro \n"
" %% define empty templates for all commands taking text: \n"
" \\gdef\\gplbacktext{}%% \n"
" \\gdef\\gplfronttext{}%% \n"
" \\makeatother \n"
" \\ifGPblacktext \n"
" %% no textcolor at all \n"
" \\def\\colorrgb#1{}%% \n"
" \\def\\colorgray#1{}%% \n"
" \\else \n"
" %% gray or color? \n"
" \\ifGPcolor \n"
" \\def\\colorrgb#1{\\color[rgb]{#1}}%% \n"
" \\def\\colorgray#1{\\color[gray]{#1}}%% \n"
" \\expandafter\\def\\csname LTw\\endcsname{\\color{white}}%% \n"
" \\expandafter\\def\\csname LTb\\endcsname{\\color{black}}%% \n"
" \\expandafter\\def\\csname LTa\\endcsname{\\color{black}}%% \n"
" \\expandafter\\def\\csname LT0\\endcsname{\\color[rgb]{1,0,0}}%% \n"
" \\expandafter\\def\\csname LT1\\endcsname{\\color[rgb]{0,1,0}}%% \n"
" \\expandafter\\def\\csname LT2\\endcsname{\\color[rgb]{0,0,1}}%% \n"
" \\expandafter\\def\\csname LT3\\endcsname{\\color[rgb]{1,0,1}}%% \n"
" \\expandafter\\def\\csname LT4\\endcsname{\\color[rgb]{0,1,1}}%% \n"
" \\expandafter\\def\\csname LT5\\endcsname{\\color[rgb]{1,1,0}}%% \n"
" \\expandafter\\def\\csname LT6\\endcsname{\\color[rgb]{0,0,0}}%% \n"
" \\expandafter\\def\\csname LT7\\endcsname{\\color[rgb]{1,0.3,0}}%% \n"
" \\expandafter\\def\\csname LT8\\endcsname{\\color[rgb]{0.5,0.5,0.5}}%% \n"
" \\else \n"
" %% gray \n"
" \\def\\colorrgb#1{\\color{black}}%% \n"
" \\def\\colorgray#1{\\color[gray]{#1}}%% \n"
" \\expandafter\\def\\csname LTw\\endcsname{\\color{white}}% \n"
" \\expandafter\\def\\csname LTb\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LTa\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT0\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT1\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT2\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT3\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT4\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT5\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT6\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT7\\endcsname{\\color{black}}% \n"
" \\expandafter\\def\\csname LT8\\endcsname{\\color{black}}% \n"
" \\fi \n"
" \\fi \n"
" \\setlength{\\unitlength}{0.0500bp}%% \n";
static char const postamble1[] =
" }%% \n"
" \\gplgaddtomacro\\gplfronttext{% \n"
" }%% \n"
" \\gplbacktext \n";
static char const postamble2[] =
" \\gplfronttext \n"
" \\end{picture}% \n"
"\\endgroup \n";
void
{
}
void
{
// TODO: strip path from filename on Windows
}
void
{
while (l) {
if (SP_IS_ITEM(o)) {
renderItem (SP_ITEM (o));
}
l = g_slist_remove (l, o);
}
}
void
{
/*
bool translated = false;
SPUse *use = SP_USE(item);
ctx->pushState();
ctx->transform(&tp);
translated = true;
}
if (use->child && SP_IS_ITEM(use->child)) {
renderItem(SP_ITEM(use->child));
}
if (translated) {
ctx->popState();
}
*/
}
void
{
/*
implement
*/
}
void
{
/*
implement
*/
}
void
{
/*
if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
ctx->pushState();
setStateForItem(ctx, item);
Geom::Matrix tempmat (root->c2p);
ctx->transform(&tempmat);
sp_group_render(item, ctx);
ctx->popState();
*/
}
void
{
// Check item's visibility
return;
}
if (SP_IS_ROOT(item)) {
TRACE(("root\n"));
return sp_root_render(item);
} else if (SP_IS_GROUP(item)) {
TRACE(("group\n"));
return sp_group_render(item);
TRACE(("use begin---\n"));
TRACE(("---use end\n"));
} else if (SP_IS_TEXT(item)) {
TRACE(("text\n"));
return sp_text_render(item);
} else if (SP_IS_FLOWTEXT(item)) {
TRACE(("flowtext\n"));
return sp_flowtext_render(item);
}
// We are not interested in writing the other SPItem types to LaTeX
}
void
{
/*
SPStyle const *style = SP_OBJECT_STYLE(item);
ctx->setStateForStyle(style);
CairoRenderState *state = ctx->getCurrentState();
state->clip_path = item->clip_ref->getObject();
state->mask = item->mask_ref->getObject();
state->item_transform = Geom::Matrix (item->transform);
// If parent_has_userspace is true the parent state's transform
// This is so because we use the image's/(flow)text's transform for positioning
// instead of explicitly specifying it and letting the renderer do the
// transformation before rendering the item.
if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
state->parent_has_userspace = TRUE;
TRACE(("setStateForItem opacity: %f\n", state->opacity));
*/
}
void
{
/*
ctx->pushState();
setStateForItem(ctx, item);
CairoRenderState *state = ctx->getCurrentState();
state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
// Draw item on a temporary surface so a mask, clip path, or opacity can be applied to it.
if (state->need_layer) {
state->merge_opacity = FALSE;
ctx->pushLayer();
}
Geom::Matrix tempmat (item->transform);
ctx->transform(&tempmat);
sp_item_invoke_render(item, ctx);
if (state->need_layer)
ctx->popLayer();
ctx->popState();
*/
}
bool
{
// The boundingbox calculation here should be exactly the same as the one by CairoRenderer::setupDocument !
if (!base)
NRRect d;
if (pageBoundingBox) {
} else {
}
// convert from px to pt
if (!pageBoundingBox)
{
}
// write the info to LaTeX:
return true;
}
void
{
}
/*
#include "macros.h" // SP_PRINT_*
// Apply an SVG clip path
void
PDFLaTeXRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
{
g_assert( ctx != NULL && ctx->_is_valid );
if (cp == NULL)
return;
CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
Geom::Matrix saved_ctm;
if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
//SP_PRINT_DRECT("clipd", cp->display->bbox);
NRRect clip_bbox(cp->display->bbox);
Geom::Matrix t(Geom::Scale(clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0));
t[4] = clip_bbox.x0;
t[5] = clip_bbox.y0;
t *= ctx->getCurrentState()->transform;
ctx->getTransform(&saved_ctm);
ctx->setTransform(&t);
}
TRACE(("BEGIN clip\n"));
SPObject *co = SP_OBJECT(cp);
for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
if (SP_IS_ITEM(child)) {
SPItem *item = SP_ITEM(child);
// combine transform of the item in clippath and the item using clippath:
Geom::Matrix tempmat (item->transform);
tempmat = tempmat * (ctx->getCurrentState()->item_transform);
// render this item in clippath
ctx->pushState();
ctx->transform(&tempmat);
setStateForItem(ctx, item);
sp_item_invoke_render(item, ctx);
ctx->popState();
}
}
TRACE(("END clip\n"));
// do clipping only if this was the first call to applyClipPath
if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
&& saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
cairo_clip(ctx->_cr);
if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
ctx->setTransform(&saved_ctm);
ctx->setRenderMode(saved_mode);
}
// Apply an SVG mask
void
PDFLaTeXRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
{
g_assert( ctx != NULL && ctx->_is_valid );
if (mask == NULL)
return;
//SP_PRINT_DRECT("maskd", &mask->display->bbox);
NRRect mask_bbox(mask->display->bbox);
// TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
Geom::Matrix t(Geom::Scale(mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0));
t[4] = mask_bbox.x0;
t[5] = mask_bbox.y0;
t *= ctx->getCurrentState()->transform;
ctx->setTransform(&t);
}
// Clip mask contents... but...
// The mask's bounding box is the "geometric bounding box" which doesn't allow for
// filters which extend outside the bounding box. So don't clip.
// ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
ctx->pushState();
TRACE(("BEGIN mask\n"));
SPObject *co = SP_OBJECT(mask);
for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
if (SP_IS_ITEM(child)) {
SPItem *item = SP_ITEM(child);
renderItem(ctx, item);
}
}
TRACE(("END mask\n"));
ctx->popState();
}
*/
} /* namespace Internal */
} /* namespace Extension */
} /* namespace Inkscape */
/* End of GNU GPL code */
/*
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 :