[PATCH 2/3] Refactoring drawing of simple polygons in enhwmf.cxx

Marc-Andre Laverdiere marc-andre at atc.tcs.com
Mon Sep 19 23:55:01 PDT 2011


---
 svtools/source/filter/wmf/enhwmf.cxx |  112 ++++++++++++++--------------------
 svtools/source/filter/wmf/winmtf.hxx |    7 ++
 2 files changed, 54 insertions(+), 65 deletions(-)

diff --git a/svtools/source/filter/wmf/enhwmf.cxx b/svtools/source/filter/wmf/enhwmf.cxx
index 618f13d..9d80efb 100644
--- a/svtools/source/filter/wmf/enhwmf.cxx
+++ b/svtools/source/filter/wmf/enhwmf.cxx
@@ -31,7 +31,7 @@
 
 #include "winmtf.hxx"
 #include <osl/endian.h>
-#include <vector>
+#include <boost/bind.hpp>
 
 using namespace std;
 //=========================== GDI-Array ===================================
@@ -350,6 +350,38 @@ void EnhWMFReader::ReadGDIComment()
  * nPoints: number of points
  * pWMF: the stream containings the polygons
  * */
+template <class T, class Drawer>
+void EnhWMFReader::ReadAndDrawPolygon(Drawer drawer, const sal_Bool skipFirst)
+{
+    sal_uInt16 nPoints(0), nStartIndex(0);
+    pWMF->SeekRel( 16 );
+    *pWMF >> nPoints;
+    if (skipFirst)
+    {
+        nPoints ++;
+        nStartIndex ++;
+    }
+
+    Polygon aPolygon(nPoints);
+    for (sal_uInt16 i = nStartIndex ; i < nPoints && pWMF->good(); i++ )
+    {
+        T nX, nY;
+        *pWMF >> nX >> nY;
+        if (pWMF->good())
+            break;
+        aPolygon[ i ] = Point( nX, nY );
+    }
+    drawer(pOut, aPolygon, skipFirst, bRecordPath);
+}
+
+
+/**
+ * Reads polygons from the stream.
+ * The <class T> parameter is for the type of the points
+ * nStartIndex: which is the starting index in the polygon of the first point read
+ * nPoints: number of points
+ * pWMF: the stream containings the polygons
+ * */
 template <class T>
 Polygon EnhWMFReader::ReadPolygon(sal_uInt16 nStartIndex, sal_uInt16 nPoints)
 {
@@ -479,46 +511,21 @@ sal_Bool EnhWMFReader::ReadEnhWMF()
         switch( nRecType )
         {
             case EMR_POLYBEZIERTO :
-                bFlag = sal_True;
+                ReadAndDrawPolygon<sal_Int32>(boost::bind(&WinMtfOutput::DrawPolyBezier, _1, _2, _3, _4), sal_True);
+            break;
             case EMR_POLYBEZIER :
-            {
-                pWMF->SeekRel( 16 );
-                *pWMF >> nPoints;
-                sal_uInt16 i = 0;
-                if ( bFlag )
-                {
-                    i++;
-                    nPoints++;
-                }
-                Polygon aPoly = ReadPolygon<sal_Int32>(i, nPoints);
-                pOut->DrawPolyBezier( aPoly, bFlag, bRecordPath );
-            }
+                ReadAndDrawPolygon<sal_Int32>(boost::bind(&WinMtfOutput::DrawPolyBezier, _1, _2, _3, _4), sal_False);
             break;
 
             case EMR_POLYGON :
-            {
-                pWMF->SeekRel( 16 );
-                *pWMF >> nPoints;
-                Polygon aPoly = ReadPolygon<sal_Int32>(0, nPoints);
-                pOut->DrawPolygon( aPoly, bRecordPath );
-            }
+                ReadAndDrawPolygon<sal_Int32>(boost::bind(&WinMtfOutput::DrawPolygon, _1, _2, _3, _4), sal_False);
             break;
 
             case EMR_POLYLINETO :
-                bFlag = sal_True;
+                ReadAndDrawPolygon<sal_Int32>(boost::bind(&WinMtfOutput::DrawPolyLine, _1, _2, _3, _4), sal_True);
+            break;
             case EMR_POLYLINE :
-            {
-                pWMF->SeekRel( 0x10 );
-                *pWMF >> nPoints;
-                sal_uInt16 i = 0;
-                if ( bFlag )
-                {
-                    i++;
-                    nPoints++;
-                }
-                Polygon aPolygon = ReadPolygon<sal_Int32>(i, nPoints);
-                pOut->DrawPolyLine( aPolygon, bFlag, bRecordPath );
-            }
+                ReadAndDrawPolygon<sal_Int32>(boost::bind(&WinMtfOutput::DrawPolyLine, _1, _2, _3, _4), sal_False);
             break;
 
             case EMR_POLYPOLYLINE :
@@ -1227,46 +1234,21 @@ sal_Bool EnhWMFReader::ReadEnhWMF()
             break;
 
             case EMR_POLYBEZIERTO16 :
-                bFlag = sal_True;
+                ReadAndDrawPolygon<sal_Int16>(boost::bind(&WinMtfOutput::DrawPolyBezier, _1, _2, _3, _4), sal_True);
+                break;
             case EMR_POLYBEZIER16 :
-            {
-                pWMF->SeekRel( 16 );
-                *pWMF >> nPoints;
-                sal_uInt16 i = 0;
-                if ( bFlag )
-                {
-                    i++;
-                    nPoints++;
-                }
-                Polygon aPoly = ReadPolygon<sal_Int16>(i, nPoints);
-                pOut->DrawPolyBezier( aPoly, bFlag, bRecordPath );  // Line( aPoly, bFlag );
-            }
+                ReadAndDrawPolygon<sal_Int16>(boost::bind(&WinMtfOutput::DrawPolyBezier, _1, _2, _3, _4), sal_False);
             break;
 
             case EMR_POLYGON16 :
-            {
-                pWMF->SeekRel( 16 );
-                *pWMF >> nPoints;
-                Polygon aPoly = ReadPolygon<sal_Int16>(0, nPoints);
-                pOut->DrawPolygon( aPoly, bRecordPath );
-            }
+                ReadAndDrawPolygon<sal_Int16>(boost::bind(&WinMtfOutput::DrawPolygon, _1, _2, _3, _4), sal_False);
             break;
 
             case EMR_POLYLINETO16 :
-                bFlag = sal_True;
+                ReadAndDrawPolygon<sal_Int16>(boost::bind(&WinMtfOutput::DrawPolyLine, _1, _2, _3, _4), sal_True);
+                break;
             case EMR_POLYLINE16 :
-            {
-                pWMF->SeekRel( 16 );
-                *pWMF >> nPoints;
-                sal_uInt16 i = 0;
-                if ( bFlag )
-                {
-                    i++;
-                    nPoints++;
-                }
-                Polygon aPoly = ReadPolygon<sal_Int16>(i, nPoints);
-                pOut->DrawPolyLine( aPoly, bFlag, bRecordPath );
-            }
+                ReadAndDrawPolygon<sal_Int16>(boost::bind(&WinMtfOutput::DrawPolyLine, _1, _2, _3, _4), sal_False);
             break;
 
             case EMR_POLYPOLYLINE16 :
diff --git a/svtools/source/filter/wmf/winmtf.hxx b/svtools/source/filter/wmf/winmtf.hxx
index c6561f6..07ddb7e 100644
--- a/svtools/source/filter/wmf/winmtf.hxx
+++ b/svtools/source/filter/wmf/winmtf.hxx
@@ -738,6 +738,11 @@ public:
                             const Point& rEndAngle
                         );
     void                DrawPolygon( Polygon& rPolygon, sal_Bool bRecordPath = sal_False );
+    void                DrawPolygon( Polygon& rPolygon, sal_Bool bDrawTo, sal_Bool bRecordPath)
+                        { //only for the template compatibility
+                            bDrawTo = bDrawTo; //to avoid complaints about unused parameter
+                            DrawPolygon(rPolygon, bRecordPath);
+                        }
     void                DrawPolyPolygon( PolyPolygon& rPolyPolygon, sal_Bool bRecordPath = sal_False );
     void                DrawPolyLine(
                             Polygon& rPolygon,
@@ -839,6 +844,8 @@ public:
 private:
     template <class T> void ReadAndDrawPolyPolygon();
     template <class T> Polygon ReadPolygon(sal_uInt16 nStartIndex, sal_uInt16 nPoints);
+//    template <class T, void * O> void ReadAndDrawPolygon(const sal_uInt16 nPoints, const sal_Bool skipFirst);
+    template <class T, class Drawer> void ReadAndDrawPolygon(Drawer drawer, const sal_Bool skipFirst);
 };
 
 //============================ WMFReader ==================================
-- 
1.7.6


--------------040808010506090009050800--


More information about the LibreOffice mailing list