tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / basegfx / source / tools / tools.cxx
blob4a7f2c962ab36257e06c7aa6a682b366dc9937c9
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 <basegfx/utils/tools.hxx>
21 #include <basegfx/range/b2drange.hxx>
23 #include <algorithm>
25 namespace basegfx::utils
27 namespace
29 double distance( const double& nX,
30 const double& nY,
31 const ::basegfx::B2DVector& rNormal,
32 const double& nC )
34 return nX*rNormal.getX() + nY*rNormal.getY() - nC;
37 void moveLineOutsideRect( ::basegfx::B2DPoint& io_rStart,
38 ::basegfx::B2DPoint& io_rEnd,
39 const ::basegfx::B2DVector& rMoveDirection,
40 const ::basegfx::B2DRange& rFitTarget )
42 // calc c for normal line form equation n x - c = 0
43 const double nC( rMoveDirection.scalar( io_rStart ) );
45 // calc maximum orthogonal distance for all four bound
46 // rect corners to the line
47 const double nMaxDistance( std::max(
48 0.0,
49 std::max(
50 distance(rFitTarget.getMinX(),
51 rFitTarget.getMinY(),
52 rMoveDirection,
53 nC),
54 std::max(
55 distance(rFitTarget.getMinX(),
56 rFitTarget.getMaxY(),
57 rMoveDirection,
58 nC),
59 std::max(
60 distance(rFitTarget.getMaxX(),
61 rFitTarget.getMinY(),
62 rMoveDirection,
63 nC),
64 distance(rFitTarget.getMaxX(),
65 rFitTarget.getMaxY(),
66 rMoveDirection,
67 nC) ) ) ) ) );
69 // now move line points, such that the bound rect
70 // points are all either 'on' or on the negative side
71 // of the half-plane
72 io_rStart += nMaxDistance*rMoveDirection;
73 io_rEnd += nMaxDistance*rMoveDirection;
77 void infiniteLineFromParallelogram( ::basegfx::B2DPoint& io_rLeftTop,
78 ::basegfx::B2DPoint& io_rLeftBottom,
79 ::basegfx::B2DPoint& io_rRightTop,
80 ::basegfx::B2DPoint& io_rRightBottom,
81 const ::basegfx::B2DRange& rFitTarget )
83 // For the top and bottom border line of the
84 // parallelogram, we determine the distance to all four
85 // corner points of the bound rect (tl, tr, bl, br). When
86 // using the unit normal form for lines (n x - c = 0), and
87 // choosing n to point 'outwards' the parallelogram, then
88 // all bound rect corner points having positive distance
89 // to the line lie outside the extended gradient rect, and
90 // thus, the corresponding border line must be moved the
91 // maximum distance outwards.
93 // don't use the top and bottom border line direction, and
94 // calculate the normal from them. Instead, use the
95 // vertical lines (lt - lb or rt - rb), as they more
96 // faithfully represent the direction of the
97 // to-be-generated infinite line
98 ::basegfx::B2DVector aDirectionVertical( io_rLeftTop - io_rLeftBottom );
99 aDirectionVertical.normalize();
101 const ::basegfx::B2DVector aNormalTop( aDirectionVertical );
102 const ::basegfx::B2DVector aNormalBottom( -aDirectionVertical );
104 // now extend parallelogram, such that the bound rect
105 // point are included
106 moveLineOutsideRect( io_rLeftTop, io_rRightTop, aNormalTop, rFitTarget );
107 moveLineOutsideRect( io_rLeftBottom, io_rRightBottom, aNormalBottom, rFitTarget );
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */