LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / canvas / source / tools / page.cxx
blob3537fa0b687396cdd78f4c262ce5f089c9f3b730
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 <sal/config.h>
22 #include "page.hxx"
24 namespace canvas
26 Page::Page( const std::shared_ptr<IRenderModule> &rRenderModule ) :
27 mpRenderModule(rRenderModule),
28 mpSurface(rRenderModule->createSurface(::basegfx::B2ISize()))
32 void Page::validate()
34 if(!(isValid()))
36 for( const auto& rFragmentPtr : mpFragments )
37 rFragmentPtr->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 = std::make_shared<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 for( const auto& pFragment : mpFragments )
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;
97 // probe location to the right
98 r.maPos.setX(x+w);
99 r.maPos.setY(y);
100 if(isValidLocation(r))
101 return true;
103 // probe location at bottom
104 r.maPos.setX(x);
105 r.maPos.setY(y+h);
106 if(isValidLocation(r))
107 return true;
110 r.maPos.setX(0);
111 r.maPos.setY(0);
113 return isValidLocation(r);
116 bool Page::isValidLocation( const SurfaceRect& r ) const
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;
125 for( const auto& pFragment : mpFragments )
127 if( r.intersection( pFragment->getRect() ) )
128 return false;
131 return true;
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */