xref: /aoo41x/main/canvas/source/tools/page.cxx (revision 25ea7f45)
1*25ea7f45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*25ea7f45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*25ea7f45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*25ea7f45SAndrew Rist  * distributed with this work for additional information
6*25ea7f45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*25ea7f45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*25ea7f45SAndrew Rist  * "License"); you may not use this file except in compliance
9*25ea7f45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*25ea7f45SAndrew Rist  *
11*25ea7f45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*25ea7f45SAndrew Rist  *
13*25ea7f45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*25ea7f45SAndrew Rist  * software distributed under the License is distributed on an
15*25ea7f45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*25ea7f45SAndrew Rist  * KIND, either express or implied.  See the License for the
17*25ea7f45SAndrew Rist  * specific language governing permissions and limitations
18*25ea7f45SAndrew Rist  * under the License.
19*25ea7f45SAndrew Rist  *
20*25ea7f45SAndrew Rist  *************************************************************/
21*25ea7f45SAndrew Rist 
22*25ea7f45SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_canvas.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <boost/bind.hpp>
28cdf0e10cSrcweir #include "page.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace canvas
31cdf0e10cSrcweir {
Page(const IRenderModuleSharedPtr & rRenderModule)32cdf0e10cSrcweir 	Page::Page( const IRenderModuleSharedPtr &rRenderModule ) :
33cdf0e10cSrcweir 		mpRenderModule(rRenderModule),
34cdf0e10cSrcweir 		mpSurface(rRenderModule->createSurface(::basegfx::B2ISize()))
35cdf0e10cSrcweir 	{
36cdf0e10cSrcweir 	}
37cdf0e10cSrcweir 
validate()38cdf0e10cSrcweir 	void Page::validate()
39cdf0e10cSrcweir 	{
40cdf0e10cSrcweir 		if(!(isValid()))
41cdf0e10cSrcweir 		{
42cdf0e10cSrcweir 			::std::for_each( mpFragments.begin(),
43cdf0e10cSrcweir 							 mpFragments.end(),
44cdf0e10cSrcweir 							 ::boost::mem_fn(&PageFragment::refresh));
45cdf0e10cSrcweir 		}
46cdf0e10cSrcweir 	}
47cdf0e10cSrcweir 
isValid() const48cdf0e10cSrcweir 	bool Page::isValid() const
49cdf0e10cSrcweir 	{
50cdf0e10cSrcweir 		return mpSurface && mpSurface->isValid();
51cdf0e10cSrcweir 	}
52cdf0e10cSrcweir 
allocateSpace(const::basegfx::B2ISize & rSize)53cdf0e10cSrcweir 	FragmentSharedPtr Page::allocateSpace( const ::basegfx::B2ISize& rSize )
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		SurfaceRect rect(rSize);
56cdf0e10cSrcweir 		if(insert(rect))
57cdf0e10cSrcweir 		{
58cdf0e10cSrcweir 			FragmentSharedPtr pFragment(new PageFragment(rect,this));
59cdf0e10cSrcweir 			mpFragments.push_back(pFragment);
60cdf0e10cSrcweir 			return pFragment;
61cdf0e10cSrcweir 		}
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 		return FragmentSharedPtr();
64cdf0e10cSrcweir 	}
65cdf0e10cSrcweir 
nakedFragment(const FragmentSharedPtr & pFragment)66cdf0e10cSrcweir 	bool Page::nakedFragment( const FragmentSharedPtr& pFragment )
67cdf0e10cSrcweir 	{
68cdf0e10cSrcweir 		SurfaceRect rect(pFragment->getSize());
69cdf0e10cSrcweir 		if(insert(rect))
70cdf0e10cSrcweir 		{
71cdf0e10cSrcweir 			pFragment->setPage(this);
72cdf0e10cSrcweir 			mpFragments.push_back(pFragment);
73cdf0e10cSrcweir 			return true;
74cdf0e10cSrcweir 		}
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 		return false;
77cdf0e10cSrcweir 	}
78cdf0e10cSrcweir 
free(const FragmentSharedPtr & pFragment)79cdf0e10cSrcweir 	void Page::free( const FragmentSharedPtr& pFragment )
80cdf0e10cSrcweir 	{
81cdf0e10cSrcweir 		// the fragment passes as argument is no longer
82cdf0e10cSrcweir 		// dedicated to this page. either it is about to
83cdf0e10cSrcweir 		// be relocated to some other page or it will
84cdf0e10cSrcweir 		// currently be deleted. in either case, simply
85cdf0e10cSrcweir 		// remove the reference from our internal storage.
86cdf0e10cSrcweir 		FragmentContainer_t::iterator it(
87cdf0e10cSrcweir 			std::remove(
88cdf0e10cSrcweir 				mpFragments.begin(),mpFragments.end(),pFragment));
89cdf0e10cSrcweir 		mpFragments.erase(it,mpFragments.end());
90cdf0e10cSrcweir 	}
91cdf0e10cSrcweir 
insert(SurfaceRect & r)92cdf0e10cSrcweir 	bool Page::insert( SurfaceRect& r )
93cdf0e10cSrcweir 	{
94cdf0e10cSrcweir 		const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
95cdf0e10cSrcweir 		FragmentContainer_t::const_iterator       it(mpFragments.begin());
96cdf0e10cSrcweir 		while(it != aEnd)
97cdf0e10cSrcweir 		{
98cdf0e10cSrcweir 			const SurfaceRect &rect = (*it)->getRect();
99cdf0e10cSrcweir             const sal_Int32 x = rect.maPos.getX();
100cdf0e10cSrcweir             const sal_Int32 y = rect.maPos.getY();
101cdf0e10cSrcweir             // to avoid interpolation artifacts from other textures,
102cdf0e10cSrcweir             // one pixel gap between them
103cdf0e10cSrcweir             const sal_Int32 w = rect.maSize.getX()+1;
104cdf0e10cSrcweir             const sal_Int32 h = rect.maSize.getY()+1;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 			// probe location to the right
107cdf0e10cSrcweir 			r.maPos.setX(x+w);
108cdf0e10cSrcweir             r.maPos.setY(y);
109cdf0e10cSrcweir             if(isValidLocation(r))
110cdf0e10cSrcweir 				return true;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 			// probe location at bottom
113cdf0e10cSrcweir             r.maPos.setX(x);
114cdf0e10cSrcweir             r.maPos.setY(y+h);
115cdf0e10cSrcweir             if(isValidLocation(r))
116cdf0e10cSrcweir 				return true;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 			++it;
119cdf0e10cSrcweir 		}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 		r.maPos.setX(0);
122cdf0e10cSrcweir 		r.maPos.setY(0);
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 		return isValidLocation(r);
125cdf0e10cSrcweir 	}
126cdf0e10cSrcweir 
isValidLocation(const SurfaceRect & r) const127cdf0e10cSrcweir 	bool Page::isValidLocation( const SurfaceRect& r ) const
128cdf0e10cSrcweir 	{
129cdf0e10cSrcweir 		// the rectangle passed as argument has a valid
130cdf0e10cSrcweir 		// location if and only if there's no intersection
131cdf0e10cSrcweir 		// with existing areas.
132cdf0e10cSrcweir 		SurfaceRect aBoundary(mpRenderModule->getPageSize()-basegfx::B2IVector(1,1));
133cdf0e10cSrcweir 		if( !r.inside(aBoundary) )
134cdf0e10cSrcweir 			return false;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 		const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
137cdf0e10cSrcweir 		FragmentContainer_t::const_iterator       it(mpFragments.begin());
138cdf0e10cSrcweir 		while(it != aEnd)
139cdf0e10cSrcweir 		{
140cdf0e10cSrcweir             if(r.intersection((*it)->getRect()))
141cdf0e10cSrcweir                 return false;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 			++it;
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 		return true;
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir }
149