Code

fix transforms
authorJohan Engelen <goejendaagh@zonnet.nl>
Sun, 21 Feb 2010 11:39:17 +0000 (12:39 +0100)
committerJohan Engelen <goejendaagh@zonnet.nl>
Sun, 21 Feb 2010 11:39:17 +0000 (12:39 +0100)
src/extension/internal/pdflatex-renderer.cpp
src/extension/internal/pdflatex-renderer.h

index 0d2ffcc94e2a02956b0d3d7253defbcbfbe08894..d21457a8945dbcca38c5e7433c0908fdc7001eb2 100644 (file)
@@ -105,10 +105,11 @@ namespace Internal {
 PDFLaTeXRenderer::PDFLaTeXRenderer(void)
   : _stream(NULL),
     _filename(NULL),
-    _m(Geom::identity()),
     _width(0),
     _height(0)
-{}
+{
+    push_transform(Geom::identity());
+}
 
 PDFLaTeXRenderer::~PDFLaTeXRenderer(void)
 {
@@ -334,10 +335,14 @@ PDFLaTeXRenderer::sp_text_render(SPItem *item)
 {
     SPText *textobj = SP_TEXT (item);
 
+    push_transform(sp_item_i2doc_affine(item));
+
     gchar *str = sp_te_get_string_multiline(item);
-    Geom::Point pos = SP_TEXT(item)->attributes.firstXY();
+    Geom::Point pos = textobj->attributes.firstXY() * transform();
     gchar *alignment = "lb";
 
+    pop_transform();
+
     // write to LaTeX
     Inkscape::SVGOStringStream os;
 
@@ -370,10 +375,10 @@ PDFLaTeXRenderer::sp_root_render(SPItem *item)
 
 //    ctx->pushState();
 //    setStateForItem(ctx, item);
-//    Geom::Matrix tempmat (root->c2p);
-//    ctx->transform(&tempmat);
+    Geom::Matrix tempmat (root->c2p);
+    push_transform(tempmat);
     sp_group_render(item);
-//    ctx->popState();
+    pop_transform();
 }
 
 void
@@ -470,23 +475,22 @@ PDFLaTeXRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem *b
     }
 
     // convert from px to pt
-    d.x0 *= PT_PER_PX;
-    d.x1 *= PT_PER_PX;
-    d.y0 *= PT_PER_PX;
-    d.y1 *= PT_PER_PX;
-
-    _width = d.x1-d.x0;
-    _height = d.y1-d.y0;
+    push_transform( Geom::Scale(PT_PER_PX, PT_PER_PX) );
 
     if (!pageBoundingBox)
     {
         double high = sp_document_height(doc);
-        high *= PT_PER_PX;
 
-        transform( Geom::Translate( -d.x0 * PX_PER_PT,
-                                    (d.y1 - high) * PX_PER_PT ) );
+        push_transform( Geom::Translate( -d.x0,
+                                         -d.y0 ) );
     }
 
+    // flip y-axis
+    push_transform( Geom::Scale(1,-1) * Geom::Translate(0, sp_document_height(doc)) );
+
+    _width = (d.x1-d.x0) * PT_PER_PX;
+    _height = (d.y1-d.y0) * PT_PER_PX;
+
     // write the info to LaTeX
     Inkscape::SVGOStringStream os;
 
@@ -500,12 +504,28 @@ PDFLaTeXRenderer::setupDocument(SPDocument *doc, bool pageBoundingBox, SPItem *b
     return true;
 }
 
+Geom::Matrix const &
+PDFLaTeXRenderer::transform()
+{
+    return _transform_stack.top();
+}
+
 void
-PDFLaTeXRenderer::transform(Geom::Matrix const &transform)
+PDFLaTeXRenderer::push_transform(Geom::Matrix const &tr)
 {
-    _m *= transform;
+    if(_transform_stack.size()){
+        Geom::Matrix tr_top = _transform_stack.top();
+        _transform_stack.push(tr * tr_top);
+    } else {
+        _transform_stack.push(tr);
+    }
 }
 
+void
+PDFLaTeXRenderer::pop_transform()
+{
+    _transform_stack.pop();
+}
 
 /*
 #include "macros.h" // SP_PRINT_*
index 856631f95dc9da8a27d32ceec35c9cbab1718b4d..93c06b1149a75083876bb3740ffd57be0690b0f3 100644 (file)
@@ -26,6 +26,7 @@
 #include <cairo.h>
 
 #include <2geom/matrix.h>
+#include <stack>
 
 class SPClipPath;
 class SPMask;
@@ -58,8 +59,10 @@ protected:
     FILE * _stream;
     gchar * _filename;
 
-    void transform(Geom::Matrix const &transform);
-    Geom::Matrix _m; // the transform for current item
+    void push_transform(Geom::Matrix const &transform);
+    Geom::Matrix const & transform();
+    void pop_transform();
+    std::stack<Geom::Matrix> _transform_stack;
     double _width;
     double _height;