1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "basebmp/polypolygonrenderer.hxx"
29 
30 #include <algorithm>
31 
32 
33 namespace basebmp
34 {
35 namespace detail
36 {
37     sal_uInt32 setupGlobalEdgeTable( VectorOfVectorOfVertices&      rGET,
38                                      basegfx::B2DPolyPolygon const& rPolyPoly,
39                                      sal_Int32                      nMinY )
40     {
41         sal_Int32 const nNumScanlines( (sal_Int32)rGET.size() );
42 
43         // add all polygons to GET
44         for( sal_uInt32 i(0), nCount(rPolyPoly.count());
45              i<nCount;
46              ++i )
47         {
48             // add all vertices to GET
49             const basegfx::B2DPolygon& rPoly( rPolyPoly.getB2DPolygon(i) );
50             for( sal_uInt32 k(0), nVertices(rPoly.count());
51                  k<nVertices;
52                  ++k )
53             {
54                 const basegfx::B2DPoint& rP1( rPoly.getB2DPoint(k) );
55                 const basegfx::B2DPoint& rP2( rPoly.getB2DPoint( (k + 1) % nVertices ) );
56 
57                 const sal_Int32 nVertexYP1( basegfx::fround(rP1.getY()) );
58                 const sal_Int32 nVertexYP2( basegfx::fround(rP2.getY()) );
59 
60                 // insert only vertices which are not strictly
61                 // horizontal. Strictly horizontal vertices don't add
62                 // any information that is not already present - due
63                 // to their adjacent vertices.
64                 if(nVertexYP1 != nVertexYP2)
65                 {
66                     if( nVertexYP2 < nVertexYP1 )
67                     {
68                         const sal_Int32 nStartScanline(nVertexYP2 - nMinY);
69 
70                         // edge direction is upwards - add with swapped vertices
71                         if( nStartScanline < nNumScanlines )
72                             rGET[ nStartScanline ].push_back( Vertex(rP2, rP1, false) );
73                     }
74                     else
75                     {
76                         const sal_Int32 nStartScanline(nVertexYP1 - nMinY);
77 
78                         if( nStartScanline < nNumScanlines )
79                             rGET[ nStartScanline ].push_back( Vertex(rP1, rP2, true) );
80                     }
81                 }
82             }
83         }
84 
85         // now sort all scanlines individually, with increasing x
86         // coordinates
87         VectorOfVectorOfVertices::iterator       aIter( rGET.begin() );
88         const VectorOfVectorOfVertices::iterator aEnd( rGET.end() );
89         sal_uInt32                               nVertexCount(0);
90         RasterConvertVertexComparator            aComp;
91         while( aIter != aEnd )
92         {
93             std::sort( aIter->begin(),
94                        aIter->end(),
95                        aComp );
96             nVertexCount += aIter->size();
97 
98             ++aIter;
99         }
100 
101         return nVertexCount;
102     }
103 
104     void sortAET( VectorOfVertexPtr& rAETSrc,
105                   VectorOfVertexPtr& rAETDest )
106     {
107         static RasterConvertVertexComparator aComp;
108 
109         rAETDest.clear();
110 
111         // prune AET from ended edges
112         VectorOfVertexPtr::iterator iter( rAETSrc.begin() );
113         VectorOfVertexPtr::iterator const end( rAETSrc.end() );
114         while( iter != end )
115         {
116             if( (*iter)->mnYCounter > 0 )
117                 rAETDest.push_back( *iter );
118             ++iter;
119         }
120 
121         // stable sort is necessary, to avoid segment crossing where
122         // none was intended.
123         std::stable_sort( rAETDest.begin(), rAETDest.end(), aComp );
124     }
125 
126 } // namespace detail
127 } // namespace basebmp
128