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>
22#include "page.hxx"
23
24namespace canvas
25{
26 Page::Page( const std::shared_ptr<IRenderModule> &rRenderModule ) :
27 mpRenderModule(rRenderModule),
28 mpSurface(rRenderModule->createSurface(::basegfx::B2IVector()))
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.getWidth() + 1;
95 const sal_Int32 h = rect.maSize.getHeight() + 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 basegfx::B2ISize aSize(mpRenderModule->getPageSize().getX(), mpRenderModule->getPageSize().getY());
122 SurfaceRect aBoundary(aSize);
123 if( !r.inside(aBoundary) )
124 return false;
125
126 for( const auto& pFragment : mpFragments )
127 {
128 if( r.intersection( pFragment->getRect() ) )
129 return false;
130 }
131
132 return true;
133 }
134}
135
136/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
TYPE getWidth() const
TYPE getHeight() const
TYPE getX() const
void setY(TYPE fY)
TYPE getY() const
void setX(TYPE fX)
std::shared_ptr< ISurface > mpSurface
Definition: page.hxx:57
bool isValidLocation(const SurfaceRect &r) const
Definition: page.cxx:116
std::shared_ptr< IRenderModule > mpRenderModule
Definition: page.hxx:56
void free(const FragmentSharedPtr &pFragment)
Definition: page.cxx:72
FragmentContainer_t mpFragments
Definition: page.hxx:58
bool insert(SurfaceRect &r)
Definition: page.cxx:85
FragmentSharedPtr allocateSpace(const ::basegfx::B2ISize &rSize)
Definition: page.cxx:46
Page(const std::shared_ptr< IRenderModule > &rRenderModule)
Definition: page.cxx:26
void validate()
Definition: page.cxx:32
bool isValid() const
Definition: page.cxx:41
bool nakedFragment(const FragmentSharedPtr &pFragment)
Definition: page.cxx:59
float y
Definition: dx_9rm.cxx:190
float x
Definition: dx_9rm.cxx:190
sal::systools::COMReference< surface_type > mpSurface
std::shared_ptr< PageFragment > FragmentSharedPtr
Definition: page.hxx:35
sal_Int32 h
sal_Int32 w
This implements some equivalent to basegfx::B2IBox, but instead of two BasicBox ranges,...
Definition: surfacerect.hxx:38
::basegfx::B2ISize maSize
Definition: surfacerect.hxx:40
bool inside(const SurfaceRect &r) const
Definition: surfacerect.hxx:77
bool intersection(const SurfaceRect &r) const
returns true if the passed rect intersects this one.
Definition: surfacerect.hxx:62
::basegfx::B2IPoint maPos
Definition: surfacerect.hxx:39