LibreOffice Module canvas (master)  1
page.cxx
Go to the documentation of this file.
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 <sal/config.h>
21 
22 #include "page.hxx"
23 
24 namespace canvas
25 {
26  Page::Page( const std::shared_ptr<IRenderModule> &rRenderModule ) :
27  mpRenderModule(rRenderModule),
28  mpSurface(rRenderModule->createSurface(::basegfx::B2ISize()))
29  {
30  }
31 
33  {
34  if(!(isValid()))
35  {
36  for( const auto& rFragmentPtr : mpFragments )
37  rFragmentPtr->refresh();
38  }
39  }
40 
41  bool Page::isValid() const
42  {
43  return mpSurface && mpSurface->isValid();
44  }
45 
46  FragmentSharedPtr Page::allocateSpace( const ::basegfx::B2ISize& rSize )
47  {
48  SurfaceRect rect(rSize);
49  if(insert(rect))
50  {
51  FragmentSharedPtr pFragment = std::make_shared<PageFragment>(rect,this);
52  mpFragments.push_back(pFragment);
53  return pFragment;
54  }
55 
56  return FragmentSharedPtr();
57  }
58 
59  bool Page::nakedFragment( const FragmentSharedPtr& pFragment )
60  {
61  SurfaceRect rect(pFragment->getSize());
62  if(insert(rect))
63  {
64  pFragment->setPage(this);
65  mpFragments.push_back(pFragment);
66  return true;
67  }
68 
69  return false;
70  }
71 
72  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  mpFragments.begin(),mpFragments.end(),pFragment));
82  mpFragments.erase(it,mpFragments.end());
83  }
84 
86  {
87  for( const auto& pFragment : mpFragments )
88  {
89  const SurfaceRect &rect = pFragment->getRect();
90  const sal_Int32 x = rect.maPos.getX();
91  const sal_Int32 y = rect.maPos.getY();
92  // to avoid interpolation artifacts from other textures,
93  // one pixel gap between them
94  const sal_Int32 w = rect.maSize.getX()+1;
95  const sal_Int32 h = rect.maSize.getY()+1;
96 
97  // probe location to the right
98  r.maPos.setX(x+w);
99  r.maPos.setY(y);
100  if(isValidLocation(r))
101  return true;
102 
103  // probe location at bottom
104  r.maPos.setX(x);
105  r.maPos.setY(y+h);
106  if(isValidLocation(r))
107  return true;
108  }
109 
110  r.maPos.setX(0);
111  r.maPos.setY(0);
112 
113  return isValidLocation(r);
114  }
115 
116  bool Page::isValidLocation( const SurfaceRect& r ) const
117  {
118  // the rectangle passed as argument has a valid
119  // location if and only if there's no intersection
120  // with existing areas.
121  SurfaceRect aBoundary(mpRenderModule->getPageSize());
122  if( !r.inside(aBoundary) )
123  return false;
124 
125  for( const auto& pFragment : mpFragments )
126  {
127  if( r.intersection( pFragment->getRect() ) )
128  return false;
129  }
130 
131  return true;
132  }
133 }
134 
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool nakedFragment(const FragmentSharedPtr &pFragment)
Definition: page.cxx:59
B2IVector B2ISize
void setY(sal_Int32 fY)
FragmentSharedPtr allocateSpace(const ::basegfx::B2ISize &rSize)
Definition: page.cxx:46
float x
Definition: dx_9rm.cxx:190
bool intersection(const SurfaceRect &r) const
returns true if the passed rect intersects this one.
Definition: surfacerect.hxx:62
bool inside(const SurfaceRect &r) const
Definition: surfacerect.hxx:77
bool insert(SurfaceRect &r)
Definition: page.cxx:85
float y
Definition: dx_9rm.cxx:190
FragmentContainer_t mpFragments
Definition: page.hxx:57
void free(const FragmentSharedPtr &pFragment)
Definition: page.cxx:72
sal_Int32 getX() const
sal_Int32 w
bool isValid() const
Definition: page.cxx:41
bool isValidLocation(const SurfaceRect &r) const
Definition: page.cxx:116
sal_Int32 getY() const
Page(const std::shared_ptr< IRenderModule > &rRenderModule)
Definition: page.cxx:26
std::shared_ptr< ISurface > mpSurface
Definition: page.hxx:56
::basegfx::B2IPoint maPos
Definition: surfacerect.hxx:39
::basegfx::B2ISize maSize
Definition: surfacerect.hxx:40
void setX(sal_Int32 fX)
std::shared_ptr< IRenderModule > mpRenderModule
Definition: page.hxx:55
std::shared_ptr< PageFragment > FragmentSharedPtr
Definition: page.hxx:34
sal_Int32 h
This implements some equivalent to basegfx::B2IBox, but instead of two BasicBox ranges, it uses a position and a size.
Definition: surfacerect.hxx:37
COMReference< surface_type > mpSurface
void validate()
Definition: page.cxx:32