Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / tools / zoomtools.cxx
blob57d88de6fe3ca86f91355de1f5516782dd6a2e5e
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/.
8 */
10 #include <basegfx/tools/zoomtools.hxx>
12 namespace basegfx
14 namespace zoomtools
17 /** 2^(1/6) as the default step
19 This ensures (unless the rounding is used) that 6 steps lead
20 to double / half zoom level.
22 const double ZOOM_FACTOR = 1.12246205;
24 /**
25 * Round a value against a specified multiple. Values below half
26 * of the multiple are rounded down and all others are rounded up.
28 * @param nCurrent current value
29 * @param nMultiple multiple against which the current value is rounded
31 static long roundMultiple(long nCurrent, int nMultiple)
33 // round zoom to a multiple of nMultiple
34 return (( nCurrent + nMultiple / 2 ) - ( nCurrent + nMultiple / 2 ) % nMultiple);
37 /**
38 * Convert geometric progression results into more common values by
39 * rounding them against certain multiples depending on the size.
40 * Beginning with 50 the multiple is 5, with 100, 10, and so on.
42 * @param nCurrent current zoom factor
44 static long roundZoom(double nCurrent)
46 // convert nCurrent properly to int
47 long nNew = nCurrent + 0.5;
49 // round to more common numbers above 50
50 if (nNew > 1000) {
51 nNew = roundMultiple(nNew, 100);
52 } else if ( nNew > 500 ) {
53 nNew = roundMultiple(nNew, 50);
54 } else if ( nNew > 100 ) {
55 nNew = roundMultiple(nNew, 10);
56 } else if ( nNew > 50 ) {
57 nNew = roundMultiple(nNew, 5);
60 return nNew;
63 /**
64 * Make sure that a certain step isn't skipped during the zooming
65 * progress.
67 * @param nCurrent current zoom factor
68 * @param nPrevious previous zoom factor
69 * @param nStep step which shouldn't be skipped
71 static long enforceStep(long nCurrent, long nPrevious, int nStep)
73 if ((( nCurrent > nStep ) && ( nPrevious < nStep ))
74 || (( nCurrent < nStep ) && ( nPrevious > nStep )))
75 return nStep;
76 else
77 return nCurrent;
80 /**
81 * Increasing the zoom level.
83 * @param nCurrent current zoom factor
85 long zoomIn(long nCurrent)
87 long nNew = roundZoom( nCurrent * ZOOM_FACTOR );
88 // make sure some values are not skipped
89 nNew = enforceStep(nNew, nCurrent, 200);
90 nNew = enforceStep(nNew, nCurrent, 100);
91 nNew = enforceStep(nNew, nCurrent, 75);
92 nNew = enforceStep(nNew, nCurrent, 50);
93 nNew = enforceStep(nNew, nCurrent, 25);
94 return nNew;
97 /**
98 * Decreasing the zoom level.
100 * @param nCurrent current zoom factor
102 long zoomOut(long nCurrent)
104 long nNew = roundZoom( nCurrent / ZOOM_FACTOR );
105 // make sure some values are not skipped
106 nNew = enforceStep(nNew, nCurrent, 200);
107 nNew = enforceStep(nNew, nCurrent, 100);
108 nNew = enforceStep(nNew, nCurrent, 75);
109 nNew = enforceStep(nNew, nCurrent, 50);
110 nNew = enforceStep(nNew, nCurrent, 25);
111 return nNew;
113 } // namespace zoomtools
114 } // namespace basegfx
116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */