xref: /trunk/main/canvas/source/tools/page.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_canvas.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <boost/bind.hpp>
32*cdf0e10cSrcweir #include "page.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir namespace canvas
35*cdf0e10cSrcweir {
36*cdf0e10cSrcweir     Page::Page( const IRenderModuleSharedPtr &rRenderModule ) :
37*cdf0e10cSrcweir         mpRenderModule(rRenderModule),
38*cdf0e10cSrcweir         mpSurface(rRenderModule->createSurface(::basegfx::B2ISize()))
39*cdf0e10cSrcweir     {
40*cdf0e10cSrcweir     }
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir     void Page::validate()
43*cdf0e10cSrcweir     {
44*cdf0e10cSrcweir         if(!(isValid()))
45*cdf0e10cSrcweir         {
46*cdf0e10cSrcweir             ::std::for_each( mpFragments.begin(),
47*cdf0e10cSrcweir                              mpFragments.end(),
48*cdf0e10cSrcweir                              ::boost::mem_fn(&PageFragment::refresh));
49*cdf0e10cSrcweir         }
50*cdf0e10cSrcweir     }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     bool Page::isValid() const
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         return mpSurface && mpSurface->isValid();
55*cdf0e10cSrcweir     }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     FragmentSharedPtr Page::allocateSpace( const ::basegfx::B2ISize& rSize )
58*cdf0e10cSrcweir     {
59*cdf0e10cSrcweir         SurfaceRect rect(rSize);
60*cdf0e10cSrcweir         if(insert(rect))
61*cdf0e10cSrcweir         {
62*cdf0e10cSrcweir             FragmentSharedPtr pFragment(new PageFragment(rect,this));
63*cdf0e10cSrcweir             mpFragments.push_back(pFragment);
64*cdf0e10cSrcweir             return pFragment;
65*cdf0e10cSrcweir         }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir         return FragmentSharedPtr();
68*cdf0e10cSrcweir     }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     bool Page::nakedFragment( const FragmentSharedPtr& pFragment )
71*cdf0e10cSrcweir     {
72*cdf0e10cSrcweir         SurfaceRect rect(pFragment->getSize());
73*cdf0e10cSrcweir         if(insert(rect))
74*cdf0e10cSrcweir         {
75*cdf0e10cSrcweir             pFragment->setPage(this);
76*cdf0e10cSrcweir             mpFragments.push_back(pFragment);
77*cdf0e10cSrcweir             return true;
78*cdf0e10cSrcweir         }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir         return false;
81*cdf0e10cSrcweir     }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     void Page::free( const FragmentSharedPtr& pFragment )
84*cdf0e10cSrcweir     {
85*cdf0e10cSrcweir         // the fragment passes as argument is no longer
86*cdf0e10cSrcweir         // dedicated to this page. either it is about to
87*cdf0e10cSrcweir         // be relocated to some other page or it will
88*cdf0e10cSrcweir         // currently be deleted. in either case, simply
89*cdf0e10cSrcweir         // remove the reference from our internal storage.
90*cdf0e10cSrcweir         FragmentContainer_t::iterator it(
91*cdf0e10cSrcweir             std::remove(
92*cdf0e10cSrcweir                 mpFragments.begin(),mpFragments.end(),pFragment));
93*cdf0e10cSrcweir         mpFragments.erase(it,mpFragments.end());
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     bool Page::insert( SurfaceRect& r )
97*cdf0e10cSrcweir     {
98*cdf0e10cSrcweir         const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
99*cdf0e10cSrcweir         FragmentContainer_t::const_iterator       it(mpFragments.begin());
100*cdf0e10cSrcweir         while(it != aEnd)
101*cdf0e10cSrcweir         {
102*cdf0e10cSrcweir             const SurfaceRect &rect = (*it)->getRect();
103*cdf0e10cSrcweir             const sal_Int32 x = rect.maPos.getX();
104*cdf0e10cSrcweir             const sal_Int32 y = rect.maPos.getY();
105*cdf0e10cSrcweir             // to avoid interpolation artifacts from other textures,
106*cdf0e10cSrcweir             // one pixel gap between them
107*cdf0e10cSrcweir             const sal_Int32 w = rect.maSize.getX()+1;
108*cdf0e10cSrcweir             const sal_Int32 h = rect.maSize.getY()+1;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir             // probe location to the right
111*cdf0e10cSrcweir             r.maPos.setX(x+w);
112*cdf0e10cSrcweir             r.maPos.setY(y);
113*cdf0e10cSrcweir             if(isValidLocation(r))
114*cdf0e10cSrcweir                 return true;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir             // probe location at bottom
117*cdf0e10cSrcweir             r.maPos.setX(x);
118*cdf0e10cSrcweir             r.maPos.setY(y+h);
119*cdf0e10cSrcweir             if(isValidLocation(r))
120*cdf0e10cSrcweir                 return true;
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir             ++it;
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         r.maPos.setX(0);
126*cdf0e10cSrcweir         r.maPos.setY(0);
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         return isValidLocation(r);
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     bool Page::isValidLocation( const SurfaceRect& r ) const
132*cdf0e10cSrcweir     {
133*cdf0e10cSrcweir         // the rectangle passed as argument has a valid
134*cdf0e10cSrcweir         // location if and only if there's no intersection
135*cdf0e10cSrcweir         // with existing areas.
136*cdf0e10cSrcweir         SurfaceRect aBoundary(mpRenderModule->getPageSize()-basegfx::B2IVector(1,1));
137*cdf0e10cSrcweir         if( !r.inside(aBoundary) )
138*cdf0e10cSrcweir             return false;
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
141*cdf0e10cSrcweir         FragmentContainer_t::const_iterator       it(mpFragments.begin());
142*cdf0e10cSrcweir         while(it != aEnd)
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             if(r.intersection((*it)->getRect()))
145*cdf0e10cSrcweir                 return false;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir             ++it;
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         return true;
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir }
153