From bde8ed1df082d20ca7bc31f9ede94419cde16b11 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Mon, 22 Feb 2010 20:33:37 +0100 Subject: - change cmdline option to --export-latex. - change source file names to reflect that it is "generic" latex renderer - make latex export work for EPS and PS aswell (bzr r9101.1.13) --- src/extension/internal/latex-text-renderer.cpp | 648 +++++++++++++++++++++++++ 1 file changed, 648 insertions(+) create mode 100644 src/extension/internal/latex-text-renderer.cpp (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp new file mode 100644 index 000000000..83bb7c52d --- /dev/null +++ b/src/extension/internal/latex-text-renderer.cpp @@ -0,0 +1,648 @@ +#define EXTENSION_INTERNAL_PDF_LATEX_RENDERER_CPP + +/** \file + * Rendering LaTeX file (pdf+latex output) + * + * The idea stems from GNUPlot's epslatex terminal output :-) + */ +/* + * Authors: + * Johan Engelen + * Miklos Erdelyi + * + * Copyright (C) 2006-2010 Authors + * + * 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 +#include + +#include "libnr/nr-rect.h" +#include "libnrtype/Layout-TNG.h" +#include <2geom/transforms.h> +#include <2geom/pathvector.h> + +#include + +#include +#include "display/nr-arena.h" +#include "display/nr-arena-item.h" +#include "display/nr-arena-group.h" +#include "display/curve.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 "text-editing.h" + +#include +#include "helper/png-write.h" +#include "helper/pixbuf-ops.h" + +#include "latex-text-renderer.h" +#include "extension/system.h" + +#include "io/sys.h" + +#include + +// include support for only the compiled-in surface types +#ifdef CAIRO_HAS_PDF_SURFACE +#include +#endif +#ifdef CAIRO_HAS_PS_SURFACE +#include +#endif + +//#define TRACE(_args) g_message(_args) +#define TRACE(_args) +//#define TEST(_args) _args +#define TEST(_args) + +// FIXME: expose these from sp-clippath/mask.cpp +struct SPClipPathView { + SPClipPathView *next; + unsigned int key; + NRArenaItem *arenaitem; + NRRect bbox; +}; + +struct SPMaskView { + SPMaskView *next; + unsigned int key; + NRArenaItem *arenaitem; + NRRect bbox; +}; + + +namespace Inkscape { +namespace Extension { +namespace Internal { + +bool +latex_render_document_text_to_file( SPDocument *doc, gchar const *filename, + const gchar * const exportId, bool exportDrawing, bool exportCanvas) +{ + 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(); + + 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(void) + : _stream(NULL), + _filename(NULL), + _width(0), + _height(0) +{ + 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, "%%%%"); + /* 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" +"%% \\setlength{\\unitlength}{}\n" +"%% \\input{.tex}\n" +"%% instead of\n" +"%% \\includegraphics[width=]{.pdf}\n" +"\n" +"\\begingroup \n" +" \\makeatletter \n" +" \\providecommand\\color[2][]{% \n" +" \\GenericError{(Inkscape) \\space\\space\\@spaces}{% \n" +" Color is used for the text in Inkscape, but the color package color is not loaded. \n" +" }{Either use black text in Inkscape or load the package \n" +" color.sty in LaTeX.}% \n" +" \\renewcommand\\color[2][]{}% \n" +" }%% \n" +" \\providecommand\\rotatebox[2]{#2}% \n" +" \\makeatother \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)); + 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 +LaTeXTextRenderer::sp_text_render(SPItem *item) +{ + SPText *textobj = SP_TEXT (item); + + Geom::Matrix i2doc = sp_item_i2doc_affine(item); + push_transform(i2doc); + + gchar *str = sp_te_get_string_multiline(item); + + // get position and alignment + Geom::Point pos; + gchar *alignment = NULL; + Geom::OptRect bbox = item->getBounds(transform()); + Geom::Interval bbox_x = (*bbox)[Geom::X]; + Geom::Interval bbox_y = (*bbox)[Geom::Y]; + SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item)); + switch (style->text_anchor.computed) { + case SP_CSS_TEXT_ANCHOR_START: + pos = Geom::Point( bbox_x.min() , bbox_y.middle() ); + alignment = "[l]"; + break; + case SP_CSS_TEXT_ANCHOR_END: + pos = Geom::Point( bbox_x.max() , bbox_y.middle() ); + alignment = "[r]"; + break; + case SP_CSS_TEXT_ANCHOR_MIDDLE: + default: + pos = bbox->midpoint(); + alignment = ""; + break; + } + + // get rotation + Geom::Matrix wotransl = i2doc.without_translation(); + double degrees = -180/M_PI * Geom::atan2(wotransl.xAxis()); + bool has_rotation = !Geom::are_near(degrees,0.); + + pop_transform(); + + // write to LaTeX + Inkscape::SVGOStringStream os; + +// os << "\\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){\\makebox(0,0)[" << alignment << "]{\\strut{}" << str << "}}%%\n"; + os << " \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){"; + os << "\\makebox(0,0)" << alignment << "{"; + if (has_rotation) { + os << "\\rotatebox{" << degrees << "}{"; + } + os << str; + 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) +{ +/* SPFlowtext *group = SP_FLOWTEXT(item); + + // write to LaTeX + Inkscape::SVGOStringStream os; + + os << " \\begin{picture}(" << _width << "," << _height << ")%%\n"; + os << " \\gplgaddtomacro\\gplbacktext{%%\n"; + os << " \\csname LTb\\endcsname%%\n"; + os << "\\put(0,0){\\makebox(0,0)[lb]{\\strut{}Position}}%%\n"; + + fprintf(_stream, "%s", os.str().c_str()); +*/ +} + +void +LaTeXTextRenderer::sp_root_render(SPItem *item) +{ + SPRoot *root = SP_ROOT(item); + +// ctx->pushState(); +// setStateForItem(ctx, item); + Geom::Matrix tempmat (root->c2p); + push_transform(tempmat); + 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)) { + TRACE(("root\n")); + return sp_root_render(item); + } else if (SP_IS_GROUP(item)) { + TRACE(("group\n")); + return sp_group_render(item); + } else if (SP_IS_USE(item)) { + TRACE(("use begin---\n")); + sp_use_render(item); + 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 +LaTeXTextRenderer::setStateForItem(SPItem const *item) +{ +/* + 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 + // has to be used for the mask's/clippath's context. + // 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 +LaTeXTextRenderer::renderItem(SPItem *item) +{ +// 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); + +// if (state->need_layer) +// ctx->popLayer(); + +// ctx->popState(); +} + +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)); + + NRRect d; + if (pageBoundingBox) { + d.x0 = d.y0 = 0; + d.x1 = ceil(sp_document_width(doc)); + d.y1 = ceil(sp_document_height(doc)); + } else { + sp_item_invoke_bbox(base, &d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX); + } + + // 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.x1-d.x0); + _width = (d.x1-d.x0) * scale; + _height = (d.y1-d.y0) * scale; + push_transform( Geom::Scale(scale, scale) ); + + if (!pageBoundingBox) + { + double high = sp_document_height(doc); + + push_transform( Geom::Translate( -d.x0, + -d.y0 ) ); + } + + // 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 << " \\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(); +} + +/* +#include "macros.h" // SP_PRINT_* + +// Apply an SVG clip path +void +LaTeXTextRenderer::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 +LaTeXTextRenderer::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 */ + +#undef TRACE + +/* 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 : -- cgit v1.2.3 From 3c2f9385f7e3fa38fcd785d989edd90a167b1f8a Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Mon, 22 Feb 2010 21:17:55 +0100 Subject: clean up code (bzr r9101.1.14) --- src/extension/internal/latex-text-renderer.cpp | 231 ++----------------------- 1 file changed, 11 insertions(+), 220 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 83bb7c52d..159595a6d 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -1,7 +1,7 @@ -#define EXTENSION_INTERNAL_PDF_LATEX_RENDERER_CPP +#define EXTENSION_INTERNAL_LATEX_TEXT_RENDERER_CPP /** \file - * Rendering LaTeX file (pdf+latex output) + * Rendering LaTeX file (pdf/eps/ps+latex output) * * The idea stems from GNUPlot's epslatex terminal output :-) */ @@ -19,85 +19,30 @@ # include "config.h" #endif -#ifndef PANGO_ENABLE_BACKEND -#define PANGO_ENABLE_BACKEND -#endif - -#ifndef PANGO_ENABLE_ENGINE -#define PANGO_ENABLE_ENGINE -#endif - +#include "latex-text-renderer.h" #include #include -#include "libnr/nr-rect.h" #include "libnrtype/Layout-TNG.h" #include <2geom/transforms.h> -#include <2geom/pathvector.h> - -#include #include -#include "display/nr-arena.h" -#include "display/nr-arena-item.h" -#include "display/nr-arena-group.h" -#include "display/curve.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 "text-editing.h" #include -#include "helper/png-write.h" -#include "helper/pixbuf-ops.h" -#include "latex-text-renderer.h" #include "extension/system.h" #include "io/sys.h" -#include - -// include support for only the compiled-in surface types -#ifdef CAIRO_HAS_PDF_SURFACE -#include -#endif -#ifdef CAIRO_HAS_PS_SURFACE -#include -#endif - -//#define TRACE(_args) g_message(_args) -#define TRACE(_args) -//#define TEST(_args) _args -#define TEST(_args) - -// FIXME: expose these from sp-clippath/mask.cpp -struct SPClipPathView { - SPClipPathView *next; - unsigned int key; - NRArenaItem *arenaitem; - NRRect bbox; -}; - -struct SPMaskView { - SPMaskView *next; - unsigned int key; - NRArenaItem *arenaitem; - NRRect bbox; -}; - - namespace Inkscape { namespace Extension { namespace Internal { @@ -144,9 +89,7 @@ latex_render_document_text_to_file( SPDocument *doc, gchar const *filename, LaTeXTextRenderer::LaTeXTextRenderer(void) : _stream(NULL), - _filename(NULL), - _width(0), - _height(0) + _filename(NULL) { push_transform(Geom::identity()); } @@ -281,8 +224,7 @@ LaTeXTextRenderer::sp_use_render(SPItem *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)); - ctx->pushState(); - ctx->transform(&tp); + push_transform(tp); translated = true; } @@ -291,7 +233,7 @@ LaTeXTextRenderer::sp_use_render(SPItem *item) } if (translated) { - ctx->popState(); + pop_transform(); } */ } @@ -339,7 +281,6 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) // write to LaTeX Inkscape::SVGOStringStream os; -// os << "\\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){\\makebox(0,0)[" << alignment << "]{\\strut{}" << str << "}}%%\n"; os << " \\put(" << pos[Geom::X] << "," << pos[Geom::Y] << "){"; os << "\\makebox(0,0)" << alignment << "{"; if (has_rotation) { @@ -377,8 +318,6 @@ LaTeXTextRenderer::sp_root_render(SPItem *item) { SPRoot *root = SP_ROOT(item); -// ctx->pushState(); -// setStateForItem(ctx, item); Geom::Matrix tempmat (root->c2p); push_transform(tempmat); sp_group_render(item); @@ -394,70 +333,26 @@ LaTeXTextRenderer::sp_item_invoke_render(SPItem *item) } 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); } else if (SP_IS_USE(item)) { - TRACE(("use begin---\n")); sp_use_render(item); - 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 -LaTeXTextRenderer::setStateForItem(SPItem const *item) -{ -/* - 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 - // has to be used for the mask's/clippath's context. - // 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 LaTeXTextRenderer::renderItem(SPItem *item) { -// 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); +// push_transform(item->transform); sp_item_invoke_render(item); -// if (state->need_layer) -// ctx->popLayer(); - -// ctx->popState(); +// pop_transform(); } bool @@ -479,16 +374,15 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * // 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.x1-d.x0); - _width = (d.x1-d.x0) * scale; - _height = (d.y1-d.y0) * scale; + double _width = (d.x1-d.x0) * scale; + double _height = (d.y1-d.y0) * scale; push_transform( Geom::Scale(scale, scale) ); if (!pageBoundingBox) { double high = sp_document_height(doc); - push_transform( Geom::Translate( -d.x0, - -d.y0 ) ); + push_transform( Geom::Translate(-d.x0, -d.y0) ); } // flip y-axis @@ -529,113 +423,10 @@ LaTeXTextRenderer::pop_transform() _transform_stack.pop(); } -/* -#include "macros.h" // SP_PRINT_* - -// Apply an SVG clip path -void -LaTeXTextRenderer::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 -LaTeXTextRenderer::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 */ -#undef TRACE - -/* End of GNU GPL code */ - /* Local Variables: mode:c++ -- cgit v1.2.3 From ee162daf2703adeba5ffc58574c0a6eb356583e9 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Mon, 22 Feb 2010 21:39:08 +0100 Subject: - proper transformations handling - 2geomify some stuff (bzr r9101.1.15) --- src/extension/internal/latex-text-renderer.cpp | 36 ++++++++++++-------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 159595a6d..7290bda56 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -26,6 +26,7 @@ #include "libnrtype/Layout-TNG.h" #include <2geom/transforms.h> +#include <2geom/rect.h> #include #include "sp-item.h" @@ -47,6 +48,10 @@ 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 .tex. + */ bool latex_render_document_text_to_file( SPDocument *doc, gchar const *filename, const gchar * const exportId, bool exportDrawing, bool exportCanvas) @@ -243,9 +248,6 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) { SPText *textobj = SP_TEXT (item); - Geom::Matrix i2doc = sp_item_i2doc_affine(item); - push_transform(i2doc); - gchar *str = sp_te_get_string_multiline(item); // get position and alignment @@ -272,12 +274,11 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) } // 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.); - pop_transform(); - // write to LaTeX Inkscape::SVGOStringStream os; @@ -318,8 +319,7 @@ LaTeXTextRenderer::sp_root_render(SPItem *item) { SPRoot *root = SP_ROOT(item); - Geom::Matrix tempmat (root->c2p); - push_transform(tempmat); + push_transform(root->c2p); sp_group_render(item); pop_transform(); } @@ -349,10 +349,9 @@ LaTeXTextRenderer::sp_item_invoke_render(SPItem *item) void LaTeXTextRenderer::renderItem(SPItem *item) { -// push_transform(item->transform); + push_transform(item->transform); sp_item_invoke_render(item); - -// pop_transform(); + pop_transform(); } bool @@ -363,26 +362,25 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * if (!base) base = SP_ITEM(sp_document_root(doc)); - NRRect d; + Geom::OptRect d; if (pageBoundingBox) { - d.x0 = d.y0 = 0; - d.x1 = ceil(sp_document_width(doc)); - d.y1 = ceil(sp_document_height(doc)); + 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); + sp_item_invoke_bbox(base, d, sp_item_i2d_affine(base), TRUE, SPItem::RENDERING_BBOX); } // 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.x1-d.x0); - double _width = (d.x1-d.x0) * scale; - double _height = (d.y1-d.y0) * scale; + double scale = 1/(d->width()); + double _width = d->width() * scale; + double _height = d->height() * scale; push_transform( Geom::Scale(scale, scale) ); if (!pageBoundingBox) { double high = sp_document_height(doc); - push_transform( Geom::Translate(-d.x0, -d.y0) ); + push_transform( Geom::Translate( - d->min() ) ); } // flip y-axis -- cgit v1.2.3 From 5c391ba25dc2a08f6eeba290847c970e0a1a1433 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Mon, 22 Feb 2010 22:52:22 +0100 Subject: also output color to tex. ICC colors do not work yet. (bzr r9101.1.16) --- src/extension/internal/latex-text-renderer.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 7290bda56..e07b17fb8 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -247,6 +247,7 @@ 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); @@ -256,7 +257,6 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) Geom::OptRect bbox = item->getBounds(transform()); Geom::Interval bbox_x = (*bbox)[Geom::X]; Geom::Interval bbox_y = (*bbox)[Geom::Y]; - SPStyle *style = SP_OBJECT_STYLE (SP_OBJECT(item)); switch (style->text_anchor.computed) { case SP_CSS_TEXT_ANCHOR_START: pos = Geom::Point( bbox_x.min() , bbox_y.middle() ); @@ -273,6 +273,20 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) break; } + // determine color (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 + // TODO: how to handle ICC colors? + // give priority to fill color + guint32 rgba = 0; + if (style->fill.set && style->fill.isColor()) { + has_color = true; + rgba = style->fill.value.color.toRGBA32(1.); + } else if (style->stroke.set && style->stroke.isColor()) { + has_color = true; + rgba = style->stroke.value.color.toRGBA32(1.); + } + + // get rotation Geom::Matrix i2doc = sp_item_i2doc_affine(item); Geom::Matrix wotransl = i2doc.without_translation(); @@ -283,6 +297,9 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) Inkscape::SVGOStringStream os; 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) << "}"; + } os << "\\makebox(0,0)" << alignment << "{"; if (has_rotation) { os << "\\rotatebox{" << degrees << "}{"; -- cgit v1.2.3 From 5ee3de5e8e6315a57cac1f34c9ceb5ef100c162a Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Wed, 24 Feb 2010 22:07:31 +0100 Subject: output clones to latex too (bzr r9101.1.17) --- src/extension/internal/latex-text-renderer.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index e07b17fb8..6c09a7396 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -223,7 +223,6 @@ LaTeXTextRenderer::sp_group_render(SPItem *item) void LaTeXTextRenderer::sp_use_render(SPItem *item) { -/* bool translated = false; SPUse *use = SP_USE(item); @@ -240,7 +239,6 @@ LaTeXTextRenderer::sp_use_render(SPItem *item) if (translated) { pop_transform(); } -*/ } void -- cgit v1.2.3 From 8d7774019aa0bd74ac3fbc194c907643a2c21cab Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Wed, 24 Feb 2010 22:19:52 +0100 Subject: add original width of picture to be able to *not* specify width in latex doc. still work to be done. (bzr r9101.1.18) --- src/extension/internal/latex-text-renderer.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 6c09a7396..6a57f71e6 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -404,6 +404,10 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * // write the info to LaTeX Inkscape::SVGOStringStream os; + // also write original width to LaTeX + // TODO: add \ifdef statements to be able to choose between specifying width or not to specify it! + os << " %\\setlength{\\unitlength}{" << d->width() * PT_PER_PX << "pt}\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"; -- cgit v1.2.3 From a29676e2e739e0b508c3eabbf6bf40b61a70dc38 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Wed, 24 Feb 2010 22:27:16 +0100 Subject: fix bug: don't output scientific notation numbers to latex (bzr r9101.1.19) --- src/extension/internal/latex-text-renderer.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 6a57f71e6..7e7e94801 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -293,6 +293,7 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) // 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) { @@ -319,6 +320,7 @@ LaTeXTextRenderer::sp_flowtext_render(SPItem *item) // write to LaTeX Inkscape::SVGOStringStream os; + os.setf(std::ios::fixed); // no scientific notation os << " \\begin{picture}(" << _width << "," << _height << ")%%\n"; os << " \\gplgaddtomacro\\gplbacktext{%%\n"; @@ -403,6 +405,7 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * // write the info to LaTeX Inkscape::SVGOStringStream os; + os.setf(std::ios::fixed); // no scientific notation // also write original width to LaTeX // TODO: add \ifdef statements to be able to choose between specifying width or not to specify it! -- cgit v1.2.3 From ce260704f96c6aad4042fb141e778fe7fe351a31 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Thu, 25 Feb 2010 00:02:25 +0100 Subject: - align on text anchor - provide better scaling option. default, the image is not scaled. image can be scaled by \def\svgwidth{...} (bzr r9101.1.20) --- src/extension/internal/latex-text-renderer.cpp | 49 ++++++++++++++++---------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 7e7e94801..d44652419 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -151,7 +151,7 @@ LaTeXTextRenderer::setTargetFile(gchar const *filename) { 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, "%%%%"); + fprintf(_stream, "%%%%\n"); /* flush this to test output stream as early as possible */ if (fflush(_stream)) { if (ferror(_stream)) { @@ -173,9 +173,13 @@ LaTeXTextRenderer::setTargetFile(gchar const *filename) { static char const preamble[] = "%% To include the image in your LaTeX document, write\n" -"%% \\setlength{\\unitlength}{}\n" "%% \\input{.tex}\n" -"%% instead of\n" +"%% instead of\n" +"%% \\includegraphics{.pdf}\n" +"%% To scale the image, write\n" +"%% \\def{\\svgwidth}{}\n" +"%% \\input{.tex}\n" +"%% instead of\n" "%% \\includegraphics[width=]{.pdf}\n" "\n" "\\begingroup \n" @@ -187,8 +191,7 @@ static char const preamble[] = " color.sty in LaTeX.}% \n" " \\renewcommand\\color[2][]{}% \n" " }%% \n" -" \\providecommand\\rotatebox[2]{#2}% \n" -" \\makeatother \n"; +" \\providecommand\\rotatebox[2]{#2}% \n"; static char const postamble[] = " \\end{picture}% \n" @@ -250,26 +253,30 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) gchar *str = sp_te_get_string_multiline(item); // get position and alignment - Geom::Point pos; + // Align vertically on the baseline of the font (retreived from the anchor point) + // Align horizontally on boundingbox + Geom::Coord pos_x; gchar *alignment = NULL; Geom::OptRect bbox = item->getBounds(transform()); Geom::Interval bbox_x = (*bbox)[Geom::X]; - Geom::Interval bbox_y = (*bbox)[Geom::Y]; switch (style->text_anchor.computed) { case SP_CSS_TEXT_ANCHOR_START: - pos = Geom::Point( bbox_x.min() , bbox_y.middle() ); - alignment = "[l]"; + pos_x = bbox_x.min(); + alignment = "[lb]"; break; case SP_CSS_TEXT_ANCHOR_END: - pos = Geom::Point( bbox_x.max() , bbox_y.middle() ); - alignment = "[r]"; + pos_x = bbox_x.max(); + alignment = "[rb]"; break; case SP_CSS_TEXT_ANCHOR_MIDDLE: default: - pos = bbox->midpoint(); - alignment = ""; + pos_x = bbox_x.middle(); + alignment = "[b]"; break; } + Geom::Point anchor = textobj->attributes.firstXY() * transform(); + // If we want to align horizontally on bbox: Geom::Point pos(pos_x, anchor[Geom::Y]); + Geom::Point pos(anchor[Geom::X], anchor[Geom::Y]); // determine color (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 @@ -299,11 +306,11 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) if (has_color) { os << "\\color[rgb]{" << SP_RGBA32_R_F(rgba) << "," << SP_RGBA32_G_F(rgba) << "," << SP_RGBA32_B_F(rgba) << "}"; } - os << "\\makebox(0,0)" << alignment << "{"; if (has_rotation) { os << "\\rotatebox{" << degrees << "}{"; } - os << str; + 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 } @@ -407,9 +414,15 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * Inkscape::SVGOStringStream os; os.setf(std::ios::fixed); // no scientific notation - // also write original width to LaTeX - // TODO: add \ifdef statements to be able to choose between specifying width or not to specify it! - os << " %\\setlength{\\unitlength}{" << d->width() * PT_PER_PX << "pt}\n"; + // scaling of the image when including it in LaTeX + + os << " \\ifx \\svgwidth \\@empty\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\\@empty\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. -- cgit v1.2.3 From db2cebbe4b243b26276f9fe2e9dd6213473e6ce9 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Thu, 25 Feb 2010 00:15:29 +0100 Subject: remove whitespace (bzr r9101.1.21) --- src/extension/internal/latex-text-renderer.cpp | 36 +++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index d44652419..825291e8c 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -53,7 +53,7 @@ namespace Internal { * @param filename This should be the filename without extension to which the tex code should be written. Output goes to .tex. */ bool -latex_render_document_text_to_file( SPDocument *doc, gchar const *filename, +latex_render_document_text_to_file( SPDocument *doc, gchar const *filename, const gchar * const exportId, bool exportDrawing, bool exportCanvas) { sp_document_ensure_up_to_date(doc); @@ -126,7 +126,7 @@ 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); @@ -182,20 +182,20 @@ static char const preamble[] = "%% instead of\n" "%% \\includegraphics[width=]{.pdf}\n" "\n" -"\\begingroup \n" -" \\makeatletter \n" -" \\providecommand\\color[2][]{% \n" -" \\GenericError{(Inkscape) \\space\\space\\@spaces}{% \n" -" Color is used for the text in Inkscape, but the color package color is not loaded. \n" -" }{Either use black text in Inkscape or load the package \n" -" color.sty in LaTeX.}% \n" -" \\renewcommand\\color[2][]{}% \n" -" }%% \n" -" \\providecommand\\rotatebox[2]{#2}% \n"; +"\\begingroup\n" +" \\makeatletter\n" +" \\providecommand\\color[2][]{%\n" +" \\GenericError{(Inkscape) \\space\\space\\@spaces}{%\n" +" Color is used for the text in Inkscape, but the color package color is not loaded.\n" +" }{Either use black text in Inkscape or load the package\n" +" color.sty in LaTeX.}%\n" +" \\renewcommand\\color[2][]{}%\n" +" }\n" +" \\providecommand\\rotatebox[2]{#2}\n"; static char const postamble[] = -" \\end{picture}% \n" -"\\endgroup \n"; +" \\end{picture}\n" +"\\endgroup\n"; void LaTeXTextRenderer::writePreamble() @@ -283,10 +283,10 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) // TODO: how to handle ICC colors? // give priority to fill color guint32 rgba = 0; - if (style->fill.set && style->fill.isColor()) { + if (style->fill.set && style->fill.isColor()) { has_color = true; rgba = style->fill.value.color.toRGBA32(1.); - } else if (style->stroke.set && style->stroke.isColor()) { + } else if (style->stroke.set && style->stroke.isColor()) { has_color = true; rgba = style->stroke.value.color.toRGBA32(1.); } @@ -415,12 +415,12 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * os.setf(std::ios::fixed); // no scientific notation // scaling of the image when including it in LaTeX - + os << " \\ifx \\svgwidth \\@empty\n"; os << " \\setlength{\\unitlength}{" << d->width() * PT_PER_PX << "pt}\n"; os << " \\else\n"; os << " \\setlength{\\unitlength}{\\svgwidth}\n"; - os << " \\fi\n"; + os << " \\fi\n"; os << " \\global\\let\\svgwidth\\@empty\n"; os << " \\makeatother\n"; -- cgit v1.2.3 From 1a16023189984da1735d6b2ea640af683443a136 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Thu, 25 Feb 2010 20:03:16 +0100 Subject: fix 2 bugs (bzr r9101.1.22) --- src/extension/internal/latex-text-renderer.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 825291e8c..78b17a000 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -251,32 +251,28 @@ LaTeXTextRenderer::sp_text_render(SPItem *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 boundingbox - Geom::Coord pos_x; + // Align horizontally on anchorpoint gchar *alignment = NULL; - Geom::OptRect bbox = item->getBounds(transform()); - Geom::Interval bbox_x = (*bbox)[Geom::X]; switch (style->text_anchor.computed) { case SP_CSS_TEXT_ANCHOR_START: - pos_x = bbox_x.min(); alignment = "[lb]"; break; case SP_CSS_TEXT_ANCHOR_END: - pos_x = bbox_x.max(); alignment = "[rb]"; break; case SP_CSS_TEXT_ANCHOR_MIDDLE: default: - pos_x = bbox_x.middle(); alignment = "[b]"; break; } Geom::Point anchor = textobj->attributes.firstXY() * transform(); - // If we want to align horizontally on bbox: Geom::Point pos(pos_x, anchor[Geom::Y]); - Geom::Point pos(anchor[Geom::X], anchor[Geom::Y]); + Geom::Point pos(anchor); // determine color (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 @@ -393,6 +389,10 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * } 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()); -- cgit v1.2.3 From 1532813ff2c49d332d43d90ac8642b1983ef9f9b Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Thu, 25 Feb 2010 21:34:33 +0100 Subject: prevent extra vertical space after image (bzr r9101.1.23) --- src/extension/internal/latex-text-renderer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 78b17a000..94e1d9893 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -194,7 +194,7 @@ static char const preamble[] = " \\providecommand\\rotatebox[2]{#2}\n"; static char const postamble[] = -" \\end{picture}\n" +" \\end{picture}%\n" "\\endgroup\n"; void -- cgit v1.2.3 From b5a872ce37707cefd7c1202118d964e35300a3dc Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Thu, 25 Feb 2010 22:43:22 +0100 Subject: fix warnings (bzr r9112) --- src/extension/internal/latex-text-renderer.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 94e1d9893..df0bfb8cb 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -317,7 +317,7 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) } void -LaTeXTextRenderer::sp_flowtext_render(SPItem *item) +LaTeXTextRenderer::sp_flowtext_render(SPItem * /*item*/) { /* SPFlowtext *group = SP_FLOWTEXT(item); @@ -402,8 +402,6 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * if (!pageBoundingBox) { - double high = sp_document_height(doc); - push_transform( Geom::Translate( - d->min() ) ); } -- cgit v1.2.3 From f0a5ede33f900b7c1aacf703914a8be8458b24df Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Fri, 26 Feb 2010 22:15:03 +0100 Subject: fix bug in latex export, when svgwidth has not been defined at all (bzr r9114) --- src/extension/internal/latex-text-renderer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index df0bfb8cb..07f3d5a6d 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -414,12 +414,12 @@ LaTeXTextRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem * // scaling of the image when including it in LaTeX - os << " \\ifx \\svgwidth \\@empty\n"; + 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\\@empty\n"; + os << " \\global\\let\\svgwidth\\undefined\n"; os << " \\makeatother\n"; os << " \\begin{picture}(" << _width << "," << _height << ")%\n"; -- cgit v1.2.3 From 45db5ae23a22758d0d72851534e60fc38fa5ff5d Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Sat, 27 Feb 2010 00:29:26 +0100 Subject: latex export: add transparency export for pdf (bzr r9115) --- src/extension/internal/latex-text-renderer.cpp | 32 ++++++++++++++++++-------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index 07f3d5a6d..c058c4ff2 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -54,7 +54,8 @@ namespace Internal { */ bool latex_render_document_text_to_file( SPDocument *doc, gchar const *filename, - const gchar * const exportId, bool exportDrawing, bool exportCanvas) + const gchar * const exportId, bool exportDrawing, bool exportCanvas, + bool pdflatex) { sp_document_ensure_up_to_date(doc); @@ -76,7 +77,7 @@ latex_render_document_text_to_file( SPDocument *doc, gchar const *filename, return false; /* Create renderer */ - LaTeXTextRenderer *renderer = new LaTeXTextRenderer(); + LaTeXTextRenderer *renderer = new LaTeXTextRenderer(pdflatex); bool ret = renderer->setTargetFile(filename); if (ret) { @@ -92,9 +93,10 @@ latex_render_document_text_to_file( SPDocument *doc, gchar const *filename, return ret; } -LaTeXTextRenderer::LaTeXTextRenderer(void) +LaTeXTextRenderer::LaTeXTextRenderer(bool pdflatex) : _stream(NULL), - _filename(NULL) + _filename(NULL), + _pdflatex(pdflatex) { push_transform(Geom::identity()); } @@ -185,12 +187,13 @@ static char const preamble[] = "\\begingroup\n" " \\makeatletter\n" " \\providecommand\\color[2][]{%\n" -" \\GenericError{(Inkscape) \\space\\space\\@spaces}{%\n" -" Color is used for the text in Inkscape, but the color package color is not loaded.\n" -" }{Either use black text in Inkscape or load the package\n" -" color.sty in LaTeX.}%\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[] = @@ -274,19 +277,25 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) Geom::Point anchor = textobj->attributes.firstXY() * transform(); Geom::Point pos(anchor); - // determine color (for now, use rgb color model as it is most native to Inkscape) + // 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); @@ -302,6 +311,9 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) 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 << "}{"; } -- cgit v1.2.3 From 6ab9d600bc54afd257dfc4f136f01f191770f858 Mon Sep 17 00:00:00 2001 From: "Johan B. C. Engelen" Date: Mon, 1 Mar 2010 00:18:58 +0100 Subject: pdf+latex: add support for flowed text! (bzr r9122) --- src/extension/internal/latex-text-renderer.cpp | 105 ++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 9 deletions(-) (limited to 'src/extension/internal/latex-text-renderer.cpp') diff --git a/src/extension/internal/latex-text-renderer.cpp b/src/extension/internal/latex-text-renderer.cpp index c058c4ff2..28bba1beb 100644 --- a/src/extension/internal/latex-text-renderer.cpp +++ b/src/extension/internal/latex-text-renderer.cpp @@ -36,6 +36,7 @@ #include "sp-use.h" #include "sp-text.h" #include "sp-flowtext.h" +#include "sp-rect.h" #include "text-editing.h" #include @@ -261,7 +262,7 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) // get position and alignment // Align vertically on the baseline of the font (retreived from the anchor point) // Align horizontally on anchorpoint - gchar *alignment = NULL; + gchar const *alignment = NULL; switch (style->text_anchor.computed) { case SP_CSS_TEXT_ANCHOR_START: alignment = "[lb]"; @@ -329,21 +330,107 @@ LaTeXTextRenderer::sp_text_render(SPItem *item) } void -LaTeXTextRenderer::sp_flowtext_render(SPItem * /*item*/) +LaTeXTextRenderer::sp_flowtext_render(SPItem * item) { -/* SPFlowtext *group = SP_FLOWTEXT(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); + + if (!flowtext->has_internal_frame()) { + // has_internal_frame includes a check that frame is a SPRect + 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(flowtext->get_frame(NULL)); + 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); // no scientific notation + os.setf(std::ios::fixed); // don't use scientific notation - os << " \\begin{picture}(" << _width << "," << _height << ")%%\n"; - os << " \\gplgaddtomacro\\gplbacktext{%%\n"; - os << " \\csname LTb\\endcsname%%\n"; - os << "\\put(0,0){\\makebox(0,0)[lb]{\\strut{}Position}}%%\n"; + 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 -- cgit v1.2.3