From 895fabaf6268e5473e3da893789f737d7557b24b Mon Sep 17 00:00:00 2001 From: Johan Engelen Date: Sat, 27 Feb 2010 00:29:26 +0100 Subject: [PATCH] latex export: add transparency export for pdf --- src/extension/internal/cairo-ps-out.cpp | 4 +-- .../internal/cairo-renderer-pdf-out.cpp | 2 +- .../internal/latex-text-renderer.cpp | 32 +++++++++++++------ src/extension/internal/latex-text-renderer.h | 8 +++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/extension/internal/cairo-ps-out.cpp b/src/extension/internal/cairo-ps-out.cpp index 6f22dbdc7..61760e9d9 100644 --- a/src/extension/internal/cairo-ps-out.cpp +++ b/src/extension/internal/cairo-ps-out.cpp @@ -199,7 +199,7 @@ CairoPsOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar con //strip filename of ".ps", do not add ".tex" here. gsize n = g_str_has_suffix(filename, ".ps") ? strlen(filename)-3 : strlen(filename); tex_filename = g_strndup(filename, n); - ret = latex_render_document_text_to_file(doc, tex_filename, new_exportId, new_areaDrawing, new_areaPage); + ret = latex_render_document_text_to_file(doc, tex_filename, new_exportId, new_areaDrawing, new_areaPage, false); g_free(tex_filename); if (!ret) @@ -287,7 +287,7 @@ CairoEpsOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar co //strip filename of ".eps", do not add ".tex" here. gsize n = g_str_has_suffix(filename, ".eps") ? strlen(filename)-4 : strlen(filename); tex_filename = g_strndup(filename, n); - ret = latex_render_document_text_to_file(doc, tex_filename, new_exportId, new_areaDrawing, new_areaPage); + ret = latex_render_document_text_to_file(doc, tex_filename, new_exportId, new_areaDrawing, new_areaPage, false); g_free(tex_filename); if (!ret) diff --git a/src/extension/internal/cairo-renderer-pdf-out.cpp b/src/extension/internal/cairo-renderer-pdf-out.cpp index 1dcfbdf1d..808590e04 100644 --- a/src/extension/internal/cairo-renderer-pdf-out.cpp +++ b/src/extension/internal/cairo-renderer-pdf-out.cpp @@ -217,7 +217,7 @@ CairoRendererPdfOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, //strip filename of ".pdf", do not add ".tex" here. gsize n = g_str_has_suffix(filename, ".pdf") ? strlen(filename)-4 : strlen(filename); tex_filename = g_strndup(filename, n); - ret = latex_render_document_text_to_file(doc, tex_filename, new_exportId, new_exportDrawing, new_exportCanvas); + ret = latex_render_document_text_to_file(doc, tex_filename, new_exportId, new_exportDrawing, new_exportCanvas, true); g_free(tex_filename); if (!ret) 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 << "}{"; } diff --git a/src/extension/internal/latex-text-renderer.h b/src/extension/internal/latex-text-renderer.h index 69f539314..b5d4bfac1 100644 --- a/src/extension/internal/latex-text-renderer.h +++ b/src/extension/internal/latex-text-renderer.h @@ -27,11 +27,13 @@ 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); +bool latex_render_document_text_to_file(SPDocument *doc, gchar const *filename, + const gchar * const exportId, bool exportDrawing, bool exportCanvas, + bool pdflatex); class LaTeXTextRenderer { public: - LaTeXTextRenderer(); + LaTeXTextRenderer(bool pdflatex); virtual ~LaTeXTextRenderer(); bool setTargetFile(gchar const *filename); @@ -47,6 +49,8 @@ protected: FILE * _stream; gchar * _filename; + bool _pdflatex; /** true if ouputting for pdfLaTeX*/ + void push_transform(Geom::Matrix const &transform); Geom::Matrix const & transform(); void pop_transform(); -- 2.30.2