1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <o3tl/unit_conversion.hxx>
23 #include <vcl/outdev.hxx>
25 #include <drawutil.hxx>
26 #include <document.hxx>
27 #include <viewdata.hxx>
29 void ScDrawUtil::CalcScale( const ScDocument
& rDoc
, SCTAB nTab
,
30 SCCOL nStartCol
, SCROW nStartRow
, SCCOL nEndCol
, SCROW nEndRow
,
31 const OutputDevice
* pDev
,
32 const Fraction
& rZoomX
, const Fraction
& rZoomY
,
33 double nPPTX
, double nPPTY
,
34 Fraction
& rScaleX
, Fraction
& rScaleY
)
36 tools::Long nPixelX
= 0;
37 tools::Long nTwipsX
= 0;
38 tools::Long nPixelY
= 0;
39 tools::Long nTwipsY
= 0;
40 for (SCCOL i
=nStartCol
; i
<nEndCol
; i
++)
42 sal_uInt16 nWidth
= rDoc
.GetColWidth(i
,nTab
);
43 nTwipsX
+= static_cast<tools::Long
>(nWidth
);
44 nPixelX
+= ScViewData::ToPixel( nWidth
, nPPTX
);
47 for (SCROW nRow
= nStartRow
; nRow
<= nEndRow
-1; ++nRow
)
49 SCROW nLastRow
= nRow
;
50 if (rDoc
.RowHidden(nRow
, nTab
, nullptr, &nLastRow
))
56 sal_uInt16 nHeight
= rDoc
.GetRowHeight(nRow
, nTab
);
57 nTwipsY
+= static_cast<tools::Long
>(nHeight
);
58 nPixelY
+= ScViewData::ToPixel(nHeight
, nPPTY
);
61 MapMode
aHMMMode( MapUnit::Map100thMM
, Point(), rZoomX
, rZoomY
);
62 Point aPixelLog
= pDev
->PixelToLogic( Point( nPixelX
,nPixelY
), aHMMMode
);
64 // Fraction(double) ctor can be used here (and avoid overflows of PixelLog * Zoom)
65 // because ReduceInaccurate is called later anyway.
67 if ( aPixelLog
.X() && nTwipsX
)
68 rScaleX
= Fraction( static_cast<double>(aPixelLog
.X()) *
69 static_cast<double>(rZoomX
.GetNumerator()) /
70 o3tl::convert
<double>(nTwipsX
, o3tl::Length::twip
, o3tl::Length::mm100
) /
71 static_cast<double>(rZoomX
.GetDenominator()) );
73 rScaleX
= Fraction( 1, 1 );
75 if ( aPixelLog
.Y() && nTwipsY
)
76 rScaleY
= Fraction( static_cast<double>(aPixelLog
.Y()) *
77 static_cast<double>(rZoomY
.GetNumerator()) /
78 o3tl::convert
<double>(nTwipsY
, o3tl::Length::twip
, o3tl::Length::mm100
) /
79 static_cast<double>(rZoomY
.GetDenominator()) );
81 rScaleY
= Fraction( 1, 1 );
83 // 25 bits of accuracy are needed to always hit the right part of
84 // cells in the last rows (was 17 before 1M rows).
85 rScaleX
.ReduceInaccurate( 25 );
86 rScaleY
.ReduceInaccurate( 25 );
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */