xref: /trunk/main/sc/source/ui/view/drawutil.cxx (revision b3f79822)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 
31 #include <vcl/outdev.hxx>
32 
33 #include "drawutil.hxx"
34 #include "document.hxx"
35 #include "global.hxx"
36 #include "viewdata.hxx"
37 
38 // STATIC DATA -----------------------------------------------------------
39 
40 // -----------------------------------------------------------------------
41 
42 
MakeFraction(long nA,long nB)43 inline Fraction MakeFraction( long nA, long nB )
44 {
45 	return ( nA && nB ) ? Fraction(nA,nB) : Fraction(1,1);
46 }
47 
CalcScale(ScDocument * pDoc,SCTAB nTab,SCCOL nStartCol,SCROW nStartRow,SCCOL nEndCol,SCROW nEndRow,OutputDevice * pDev,const Fraction & rZoomX,const Fraction & rZoomY,double nPPTX,double nPPTY,Fraction & rScaleX,Fraction & rScaleY)48 void ScDrawUtil::CalcScale( ScDocument* pDoc, SCTAB nTab,
49 							SCCOL nStartCol, SCROW nStartRow, SCCOL nEndCol, SCROW nEndRow,
50 							OutputDevice* pDev,
51 							const Fraction& rZoomX, const Fraction& rZoomY,
52 							double nPPTX, double nPPTY,
53 							Fraction& rScaleX, Fraction& rScaleY )
54 {
55 	long nPixelX = 0;
56 	long nTwipsX = 0;
57 	long nPixelY = 0;
58 	long nTwipsY = 0;
59 	for (SCCOL i=nStartCol; i<nEndCol; i++)
60 	{
61 		sal_uInt16 nWidth = pDoc->GetColWidth(i,nTab);
62 		nTwipsX += (long) nWidth;
63 		nPixelX += ScViewData::ToPixel( nWidth, nPPTX );
64 	}
65 
66     for (SCROW nRow = nStartRow; nRow <= nEndRow-1; ++nRow)
67 	{
68         SCROW nLastRow = nRow;
69         if (pDoc->RowHidden(nRow, nTab, NULL, &nLastRow))
70         {
71             nRow = nLastRow;
72             continue;
73         }
74 
75         sal_uInt16 nHeight = pDoc->GetRowHeight(nRow, nTab);
76         nTwipsY += static_cast<long>(nHeight);
77         nPixelY += ScViewData::ToPixel(nHeight, nPPTY);
78 	}
79 
80     // #i116848# To get a large-enough number for PixelToLogic, multiply the integer values
81     // instead of using a larger number of rows
82     if ( nTwipsY )
83     {
84         long nMultiply = 2000000 / nTwipsY;
85         if ( nMultiply > 1 )
86         {
87             nTwipsY *= nMultiply;
88             nPixelY *= nMultiply;
89         }
90     }
91 
92 	MapMode aHMMMode( MAP_100TH_MM, Point(), rZoomX, rZoomY );
93 	Point aPixelLog = pDev->PixelToLogic( Point( nPixelX,nPixelY ), aHMMMode );
94 
95 	//	Fraction(double) ctor can be used here (and avoid overflows of PixelLog * Zoom)
96 	//	because ReduceInaccurate is called later anyway.
97 
98 	if ( aPixelLog.X() && nTwipsX )
99 		rScaleX = Fraction( ((double)aPixelLog.X()) *
100 							((double)rZoomX.GetNumerator()) /
101 							((double)nTwipsX) /
102 							((double)HMM_PER_TWIPS) /
103 							((double)rZoomX.GetDenominator()) );
104 	else
105 		rScaleX = Fraction( 1, 1 );
106 
107 	if ( aPixelLog.Y() && nTwipsY )
108 		rScaleY = Fraction( ((double)aPixelLog.Y()) *
109 							((double)rZoomY.GetNumerator()) /
110 							((double)nTwipsY) /
111 							((double)HMM_PER_TWIPS) /
112 							((double)rZoomY.GetDenominator()) );
113 	else
114 		rScaleY = Fraction( 1, 1 );
115 
116     //	25 bits of accuracy are needed to always hit the right part of
117     //	cells in the last rows (was 17 before 1M rows).
118 	rScaleX.ReduceInaccurate( 25 );
119 	rScaleY.ReduceInaccurate( 25 );
120 }
121 
122 
123 
124 
125