Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / tools / page.cxx
blob519e5add72a8253a4e8a434e927e2b8095936839
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <boost/mem_fn.hpp>
21 #include "page.hxx"
23 namespace canvas
25 Page::Page( const IRenderModuleSharedPtr &rRenderModule ) :
26 mpRenderModule(rRenderModule),
27 mpSurface(rRenderModule->createSurface(::basegfx::B2ISize()))
31 void Page::validate()
33 if(!(isValid()))
35 ::std::for_each( mpFragments.begin(),
36 mpFragments.end(),
37 ::boost::mem_fn(&PageFragment::refresh));
41 bool Page::isValid() const
43 return mpSurface && mpSurface->isValid();
46 FragmentSharedPtr Page::allocateSpace( const ::basegfx::B2ISize& rSize )
48 SurfaceRect rect(rSize);
49 if(insert(rect))
51 FragmentSharedPtr pFragment(new PageFragment(rect,this));
52 mpFragments.push_back(pFragment);
53 return pFragment;
56 return FragmentSharedPtr();
59 bool Page::nakedFragment( const FragmentSharedPtr& pFragment )
61 SurfaceRect rect(pFragment->getSize());
62 if(insert(rect))
64 pFragment->setPage(this);
65 mpFragments.push_back(pFragment);
66 return true;
69 return false;
72 void Page::free( const FragmentSharedPtr& pFragment )
74 // the fragment passes as argument is no longer
75 // dedicated to this page. either it is about to
76 // be relocated to some other page or it will
77 // currently be deleted. in either case, simply
78 // remove the reference from our internal storage.
79 FragmentContainer_t::iterator it(
80 std::remove(
81 mpFragments.begin(),mpFragments.end(),pFragment));
82 mpFragments.erase(it,mpFragments.end());
85 bool Page::insert( SurfaceRect& r )
87 const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
88 FragmentContainer_t::const_iterator it(mpFragments.begin());
89 while(it != aEnd)
91 const SurfaceRect &rect = (*it)->getRect();
92 const sal_Int32 x = rect.maPos.getX();
93 const sal_Int32 y = rect.maPos.getY();
94 // to avoid interpolation artifacts from other textures,
95 // one pixel gap between them
96 const sal_Int32 w = rect.maSize.getX()+1;
97 const sal_Int32 h = rect.maSize.getY()+1;
99 // probe location to the right
100 r.maPos.setX(x+w);
101 r.maPos.setY(y);
102 if(isValidLocation(r))
103 return true;
105 // probe location at bottom
106 r.maPos.setX(x);
107 r.maPos.setY(y+h);
108 if(isValidLocation(r))
109 return true;
111 ++it;
114 r.maPos.setX(0);
115 r.maPos.setY(0);
117 return isValidLocation(r);
120 bool Page::isValidLocation( const SurfaceRect& r ) const
122 // the rectangle passed as argument has a valid
123 // location if and only if there's no intersection
124 // with existing areas.
125 SurfaceRect aBoundary(mpRenderModule->getPageSize()-basegfx::B2IVector(1,1));
126 if( !r.inside(aBoundary) )
127 return false;
129 const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
130 FragmentContainer_t::const_iterator it(mpFragments.begin());
131 while(it != aEnd)
133 if(r.intersection((*it)->getRect()))
134 return false;
136 ++it;
139 return true;
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */