Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <boost/mem_fn.hpp>
21 : #include "page.hxx"
22 :
23 : namespace canvas
24 : {
25 0 : Page::Page( const IRenderModuleSharedPtr &rRenderModule ) :
26 : mpRenderModule(rRenderModule),
27 0 : mpSurface(rRenderModule->createSurface(::basegfx::B2ISize()))
28 : {
29 0 : }
30 :
31 0 : void Page::validate()
32 : {
33 0 : if(!(isValid()))
34 : {
35 : ::std::for_each( mpFragments.begin(),
36 : mpFragments.end(),
37 0 : ::boost::mem_fn(&PageFragment::refresh));
38 : }
39 0 : }
40 :
41 0 : bool Page::isValid() const
42 : {
43 0 : return mpSurface && mpSurface->isValid();
44 : }
45 :
46 0 : FragmentSharedPtr Page::allocateSpace( const ::basegfx::B2ISize& rSize )
47 : {
48 0 : SurfaceRect rect(rSize);
49 0 : if(insert(rect))
50 : {
51 0 : FragmentSharedPtr pFragment(new PageFragment(rect,this));
52 0 : mpFragments.push_back(pFragment);
53 0 : return pFragment;
54 : }
55 :
56 0 : return FragmentSharedPtr();
57 : }
58 :
59 0 : bool Page::nakedFragment( const FragmentSharedPtr& pFragment )
60 : {
61 0 : SurfaceRect rect(pFragment->getSize());
62 0 : if(insert(rect))
63 : {
64 0 : pFragment->setPage(this);
65 0 : mpFragments.push_back(pFragment);
66 0 : return true;
67 : }
68 :
69 0 : return false;
70 : }
71 :
72 0 : void Page::free( const FragmentSharedPtr& pFragment )
73 : {
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 0 : mpFragments.begin(),mpFragments.end(),pFragment));
82 0 : mpFragments.erase(it,mpFragments.end());
83 0 : }
84 :
85 0 : bool Page::insert( SurfaceRect& r )
86 : {
87 0 : const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
88 0 : FragmentContainer_t::const_iterator it(mpFragments.begin());
89 0 : while(it != aEnd)
90 : {
91 0 : const SurfaceRect &rect = (*it)->getRect();
92 0 : const sal_Int32 x = rect.maPos.getX();
93 0 : const sal_Int32 y = rect.maPos.getY();
94 : // to avoid interpolation artifacts from other textures,
95 : // one pixel gap between them
96 0 : const sal_Int32 w = rect.maSize.getX()+1;
97 0 : const sal_Int32 h = rect.maSize.getY()+1;
98 :
99 : // probe location to the right
100 0 : r.maPos.setX(x+w);
101 0 : r.maPos.setY(y);
102 0 : if(isValidLocation(r))
103 0 : return true;
104 :
105 : // probe location at bottom
106 0 : r.maPos.setX(x);
107 0 : r.maPos.setY(y+h);
108 0 : if(isValidLocation(r))
109 0 : return true;
110 :
111 0 : ++it;
112 : }
113 :
114 0 : r.maPos.setX(0);
115 0 : r.maPos.setY(0);
116 :
117 0 : return isValidLocation(r);
118 : }
119 :
120 0 : bool Page::isValidLocation( const SurfaceRect& r ) const
121 : {
122 : // the rectangle passed as argument has a valid
123 : // location if and only if there's no intersection
124 : // with existing areas.
125 0 : SurfaceRect aBoundary(mpRenderModule->getPageSize()-basegfx::B2IVector(1,1));
126 0 : if( !r.inside(aBoundary) )
127 0 : return false;
128 :
129 0 : const FragmentContainer_t::const_iterator aEnd(mpFragments.end());
130 0 : FragmentContainer_t::const_iterator it(mpFragments.begin());
131 0 : while(it != aEnd)
132 : {
133 0 : if(r.intersection((*it)->getRect()))
134 0 : return false;
135 :
136 0 : ++it;
137 : }
138 :
139 0 : return true;
140 : }
141 : }
142 :
143 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|