editeng/source/misc/txtrange.cxx      |    3 +--
 filter/source/svg/svgwriter.cxx       |   13 +++++++------
 include/tools/poly.hxx                |   11 +++++++++++
 sw/source/core/docnode/ndnotxt.cxx    |   11 +++--------
 sw/source/filter/ww8/writerhelper.cxx |    9 +++------
 tools/inc/poly.h                      |    9 +++++++++
 tools/source/generic/poly2.cxx        |   20 ++++++++++++++++++++
 vcl/source/filter/imet/ios2met.cxx    |    4 ++--
 vcl/source/filter/wmf/emfwr.cxx       |   13 +++++--------
 vcl/source/filter/wmf/wmfwr.cxx       |   20 +++++++++-----------
 10 files changed, 70 insertions(+), 43 deletions(-)

New commits:
commit cfe6ce1656754776568be5b30217efc08aa70dc0
Author:     Yuki Joou <yukij...@kemonomimi.gay>
AuthorDate: Wed Jun 12 00:30:31 2024 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Sun Jul 14 12:24:14 2024 +0200

    tdf#145538 - related: Make tools::PolyPolygon iterable
    
    This patch allows tools::PolyPolygon to be iterated using c++11's
    fancy range-based for-loops. It also upgrades a bunch of old-style
    for-loops iterating over polypolygons to the new style to make code
    more readable.
    
    Note that not all for-loops iterating over polypolygons have been
    upgraded, as I didn't find a way to automatically find those. I just
    did a few I found manually.
    
    Change-Id: Ifa8d0d44e6f0aa28fdcfd87d70058b12f48c1349
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168730
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>
    Tested-by: Jenkins

diff --git a/editeng/source/misc/txtrange.cxx b/editeng/source/misc/txtrange.cxx
index 2f02a1150f91..02e452209876 100644
--- a/editeng/source/misc/txtrange.cxx
+++ b/editeng/source/misc/txtrange.cxx
@@ -305,9 +305,8 @@ void SvxBoundArgs::Calc( const tools::PolyPolygon& rPoly )
 {
     sal_uInt16 nCount;
     nAct = 0;
-    for( sal_uInt16 i = 0; i < rPoly.Count(); ++i )
+    for( auto const& rPol : rPoly )
     {
-        const tools::Polygon& rPol = rPoly[ i ];
         nCount = rPol.GetSize();
         if( nCount )
         {
diff --git a/filter/source/svg/svgwriter.cxx b/filter/source/svg/svgwriter.cxx
index 57a039f32a18..d9446a886f8a 100644
--- a/filter/source/svg/svgwriter.cxx
+++ b/filter/source/svg/svgwriter.cxx
@@ -1882,9 +1882,9 @@ tools::PolyPolygon& SVGActionWriter::ImplMap( const 
tools::PolyPolygon& rPolyPol
 
     rDstPolyPoly = tools::PolyPolygon();
 
-    for( sal_uInt16 i = 0, nCount = rPolyPoly.Count(); i < nCount; ++i )
+    for( auto const& poly : rPolyPoly )
     {
-        rDstPolyPoly.Insert( ImplMap( rPolyPoly[ i ], aPoly ) );
+        rDstPolyPoly.Insert( ImplMap( poly, aPoly ) );
     }
 
     return rDstPolyPoly;
@@ -1898,9 +1898,10 @@ OUString SVGActionWriter::GetPathString( const 
tools::PolyPolygon& rPolyPoly, bo
     static constexpr OUString   aComma( u","_ustr );
     Point                      aPolyPoint;
 
-    for( tools::Long i = 0, nCount = rPolyPoly.Count(); i < nCount; i++ )
+
+    for( auto rPolyIter = rPolyPoly.begin(); rPolyIter != rPolyPoly.end(); 
++rPolyIter )
     {
-        const tools::Polygon&  rPoly = rPolyPoly[ static_cast<sal_uInt16>(i) ];
+        auto const& rPoly = *rPolyIter;
         sal_uInt16 n = 1, nSize = rPoly.GetSize();
 
         if( nSize > 1 )
@@ -1950,10 +1951,10 @@ OUString SVGActionWriter::GetPathString( const 
tools::PolyPolygon& rPolyPoly, bo
                 }
             }
 
-            if(bClose)
+            if( bClose )
                 aPathData.append(" Z");
 
-            if( i < ( nCount - 1 ) )
+            if( rPolyIter != rPolyPoly.end() )
                 aPathData.append(aBlank);
         }
     }
diff --git a/include/tools/poly.hxx b/include/tools/poly.hxx
index 74e95141c710..95d39d8df5ab 100644
--- a/include/tools/poly.hxx
+++ b/include/tools/poly.hxx
@@ -197,6 +197,10 @@ private:
     TOOLS_DLLPRIVATE void  ImplDoOperation( const tools::PolyPolygon& 
rPolyPoly, tools::PolyPolygon& rResult, PolyClipOp nOperation ) const;
 
 public:
+
+    typedef std::vector<tools::Polygon>::iterator iterator;
+    typedef std::vector<tools::Polygon>::const_iterator const_iterator;
+
                         explicit PolyPolygon( sal_uInt16 nInitSize = 16 );
                         explicit PolyPolygon( const tools::Polygon& rPoly );
                         explicit PolyPolygon( const tools::Rectangle& );
@@ -264,6 +268,13 @@ public:
     // constructor to convert from ::basegfx::B2DPolyPolygon
      // #i76339# made explicit
      explicit PolyPolygon(const ::basegfx::B2DPolyPolygon& rPolyPolygon);
+
+
+    iterator begin();
+    iterator end();
+
+    const_iterator begin() const;
+    const_iterator end() const;
 };
 
 template< typename charT, typename traits >
diff --git a/sw/source/core/docnode/ndnotxt.cxx 
b/sw/source/core/docnode/ndnotxt.cxx
index e4641de22325..d044f4e90dd3 100644
--- a/sw/source/core/docnode/ndnotxt.cxx
+++ b/sw/source/core/docnode/ndnotxt.cxx
@@ -126,11 +126,9 @@ const tools::PolyPolygon *SwNoTextNode::HasContour() const
             OutputDevice* pOutDev =
                 (bPixelGrf || m_bPixelContour) ? 
Application::GetDefaultDevice()
                                              : nullptr;
-            sal_uInt16 nPolyCount = m_pContour->Count();
-            for( sal_uInt16 j=0; j<nPolyCount; j++ )
-            {
-                tools::Polygon& rPoly = (*m_pContour)[j];
 
+            for ( auto& rPoly : *m_pContour )
+            {
                 sal_uInt16 nCount = rPoly.GetSize();
                 for( sal_uInt16 i=0 ; i<nCount; i++ )
                 {
@@ -192,11 +190,8 @@ bool SwNoTextNode::GetContourAPI( tools::PolyPolygon 
&rContour ) const
         if( aGrfMap.GetMapUnit() != MapUnit::MapPixel &&
             aGrfMap != aContourMap )
         {
-            sal_uInt16 nPolyCount = rContour.Count();
-            for( sal_uInt16 j=0; j<nPolyCount; j++ )
+            for( auto& rPoly : rContour )
             {
-                tools::Polygon& rPoly = rContour[j];
-
                 sal_uInt16 nCount = rPoly.GetSize();
                 for( sal_uInt16 i=0 ; i<nCount; i++ )
                 {
diff --git a/sw/source/filter/ww8/writerhelper.cxx 
b/sw/source/filter/ww8/writerhelper.cxx
index a13aae10d8c1..1e21525ce679 100644
--- a/sw/source/filter/ww8/writerhelper.cxx
+++ b/sw/source/filter/ww8/writerhelper.cxx
@@ -597,11 +597,10 @@ namespace sw
                 // for reducing to a single polygon will just need more power 
and
                 // cannot create more correct results.
                 sal_uInt32 nPointCount(0);
-                sal_uInt16 a;
 
-                for(a = 0; a < rPolyPoly.Count(); a++)
+                for( auto const& rPoly : rPolyPoly )
                 {
-                    nPointCount += 
static_cast<sal_uInt32>(rPolyPoly[a].GetSize());
+                    nPointCount += static_cast<sal_uInt32>(rPoly.GetSize());
                 }
 
                 if(nPointCount > 0x0000ffff)
@@ -613,10 +612,8 @@ namespace sw
                 tools::Polygon 
aRetval(o3tl::narrowing<sal_uInt16>(nPointCount));
                 sal_uInt32 nAppendIndex(0);
 
-                for(a = 0; a < rPolyPoly.Count(); a++)
+                for( auto const& rCandidate : rPolyPoly )
                 {
-                    const tools::Polygon& rCandidate = rPolyPoly[a];
-
                     for(sal_uInt16 b(0); nAppendIndex <= nPointCount && b < 
rCandidate.GetSize(); b++)
                     {
                         aRetval[o3tl::narrowing<sal_uInt16>(nAppendIndex++)] = 
rCandidate[b];
diff --git a/tools/inc/poly.h b/tools/inc/poly.h
index 772e854e31fb..4a3139570aac 100644
--- a/tools/inc/poly.h
+++ b/tools/inc/poly.h
@@ -57,6 +57,9 @@ struct ImplPolyPolygon
 {
     std::vector<tools::Polygon> mvPolyAry;
 
+    typedef std::vector<tools::Polygon>::iterator iterator;
+    typedef std::vector<tools::Polygon>::const_iterator const_iterator;
+
     ImplPolyPolygon( sal_uInt16 nInitSize )
     {
        if ( !nInitSize )
@@ -78,6 +81,12 @@ struct ImplPolyPolygon
     {
         return mvPolyAry == other.mvPolyAry;
     }
+
+    iterator begin() { return mvPolyAry.begin(); }
+    iterator end() { return mvPolyAry.end(); }
+
+    const_iterator begin() const { return mvPolyAry.begin(); }
+    const_iterator end() const { return mvPolyAry.end(); }
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/tools/source/generic/poly2.cxx b/tools/source/generic/poly2.cxx
index a00f64f41b18..24099817ce9e 100644
--- a/tools/source/generic/poly2.cxx
+++ b/tools/source/generic/poly2.cxx
@@ -495,6 +495,26 @@ PolyPolygon::PolyPolygon(const basegfx::B2DPolyPolygon& 
rPolyPolygon)
 {
 }
 
+PolyPolygon::iterator PolyPolygon::begin()
+{
+    return mpImplPolyPolygon->begin();
+}
+
+PolyPolygon::iterator PolyPolygon::end()
+{
+    return mpImplPolyPolygon->end();
+}
+
+PolyPolygon::const_iterator PolyPolygon::begin() const
+{
+    return mpImplPolyPolygon->begin();
+}
+
+PolyPolygon::const_iterator PolyPolygon::end() const
+{
+    return mpImplPolyPolygon->end();
+}
+
 } /* namespace tools */
 
 ImplPolyPolygon::ImplPolyPolygon(const basegfx::B2DPolyPolygon& rPolyPolygon)
diff --git a/vcl/source/filter/imet/ios2met.cxx 
b/vcl/source/filter/imet/ios2met.cxx
index ff6a54db253d..1d1bcfa1c8d6 100644
--- a/vcl/source/filter/imet/ios2met.cxx
+++ b/vcl/source/filter/imet/ios2met.cxx
@@ -552,8 +552,8 @@ void OS2METReader::DrawPolyPolygon( const 
tools::PolyPolygon& rPolyPolygon )
         pVirDev->SetLineColor( COL_TRANSPARENT );
         pVirDev->DrawPolyPolygon( rPolyPolygon );
         pVirDev->Pop();
-        for ( sal_uInt16 i = 0; i < rPolyPolygon.Count(); i++ )
-            pVirDev->DrawPolyLine( rPolyPolygon.GetObject( i ), aLineInfo );
+        for ( auto const& rPoly : rPolyPolygon )
+            pVirDev->DrawPolyLine( rPoly, aLineInfo );
     }
     else
         pVirDev->DrawPolyPolygon( rPolyPolygon );
diff --git a/vcl/source/filter/wmf/emfwr.cxx b/vcl/source/filter/wmf/emfwr.cxx
index 92a2320369c1..c9c08d590174 100644
--- a/vcl/source/filter/wmf/emfwr.cxx
+++ b/vcl/source/filter/wmf/emfwr.cxx
@@ -698,13 +698,11 @@ void EMFWriter::ImplWritePolyPolygonRecord( const 
tools::PolyPolygon& rPolyPoly
                 ImplWriteRect( rPolyPoly.GetBoundRect() );
                 m_rStm.WriteUInt32( nPolyCount ).WriteUInt32( nTotalPoints );
 
-                for( i = 0; i < nPolyCount; i++ )
-                    m_rStm.WriteUInt32( rPolyPoly[ i ].GetSize() );
+                for( auto const& rPoly : rPolyPoly )
+                    m_rStm.WriteUInt32( rPoly.GetSize() );
 
-                for( i = 0; i < nPolyCount; i++ )
+                for( auto const& rPoly : rPolyPoly )
                 {
-                    const tools::Polygon& rPoly = rPolyPoly[ i ];
-
                     for( n = 0; n < rPoly.GetSize(); n++ )
                         ImplWritePoint( rPoly[ n ] );
                 }
@@ -723,11 +721,10 @@ void EMFWriter::ImplWritePath( const tools::PolyPolygon& 
rPolyPoly, bool bClosed
     ImplBeginRecord( WIN_EMR_BEGINPATH );
     ImplEndRecord();
 
-    sal_uInt16 i, n, o, nPolyCount = rPolyPoly.Count();
-    for ( i = 0; i < nPolyCount; i++ )
+    sal_uInt16 n, o;
+    for ( auto const& rPoly : rPolyPoly )
     {
         n = 0;
-        const tools::Polygon& rPoly = rPolyPoly[ i ];
         while ( n < rPoly.GetSize() )
         {
             if( n == 0 )
diff --git a/vcl/source/filter/wmf/wmfwr.cxx b/vcl/source/filter/wmf/wmfwr.cxx
index e89180073cba..cf4dfd3509fc 100644
--- a/vcl/source/filter/wmf/wmfwr.cxx
+++ b/vcl/source/filter/wmf/wmfwr.cxx
@@ -647,27 +647,25 @@ void WMFWriter::WMFRecord_PolyLine(const tools::Polygon & 
rPoly)
 
 void WMFWriter::WMFRecord_PolyPolygon(const tools::PolyPolygon & rPolyPoly)
 {
-    const tools::Polygon * pPoly;
-    sal_uInt16 nCount,nSize,i,j;
+    sal_uInt16 nCount,nSize;
 
     nCount=rPolyPoly.Count();
     tools::PolyPolygon aSimplePolyPoly( rPolyPoly );
-    for ( i = 0; i < nCount; i++ )
+    for ( auto& rSimplePolyPolyItm : aSimplePolyPoly )
     {
-        if ( aSimplePolyPoly[ i ].HasFlags() )
+        if ( rSimplePolyPolyItm.HasFlags() )
         {
             tools::Polygon aSimplePoly;
-            aSimplePolyPoly[ i ].AdaptiveSubdivide( aSimplePoly );
-            aSimplePolyPoly[ i ] = std::move(aSimplePoly);
+            rSimplePolyPolyItm.AdaptiveSubdivide( aSimplePoly );
+            rSimplePolyPolyItm = std::move(aSimplePoly);
         }
     }
     WriteRecordHeader(0,W_META_POLYPOLYGON);
     pWMF->WriteUInt16( nCount );
-    for (i=0; i<nCount; i++) pWMF->WriteUInt16( 
aSimplePolyPoly.GetObject(i).GetSize() );
-    for (i=0; i<nCount; i++) {
-        pPoly=&(aSimplePolyPoly.GetObject(i));
-        nSize=pPoly->GetSize();
-        for (j=0; j<nSize; j++) WritePointXY(pPoly->GetPoint(j));
+    for ( auto const& aPoly : aSimplePolyPoly ) pWMF->WriteUInt16( 
aPoly.GetSize() );
+    for ( auto const& aPoly : aSimplePolyPoly ) {
+        nSize = aPoly.GetSize();
+        for ( sal_uInt16 j = 0; j < nSize; j++ ) WritePointXY( aPoly.GetPoint( 
j ) );
     }
     UpdateRecordHeader();
 }

Reply via email to