Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / vcl / source / gdi / mapmod.cxx
blobc4d0cf9f555ff3ccf600b6f517ccc296e92a894a
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 <vcl/mapmod.hxx>
22 #include <tools/gen.hxx>
23 #include <tools/fract.hxx>
24 #include <tools/stream.hxx>
25 #include <tools/vcompat.hxx>
26 #include <rtl/instance.hxx>
28 struct MapMode::ImplMapMode
30 MapUnit meUnit;
31 Point maOrigin;
32 // NOTE: these Fraction must NOT have more than 32 bits precision
33 // because ReadFraction / WriteFraction do only 32 bits, so more than
34 // that cannot be stored in MetaFiles!
35 // => call ReduceInaccurate whenever setting these
36 Fraction maScaleX;
37 Fraction maScaleY;
38 bool mbSimple;
40 ImplMapMode();
41 ImplMapMode(const ImplMapMode& rImpMapMode);
43 bool operator==( const ImplMapMode& rImpMapMode ) const;
46 MapMode::ImplMapMode::ImplMapMode() :
47 maOrigin( 0, 0 ),
48 maScaleX( 1, 1 ),
49 maScaleY( 1, 1 )
51 meUnit = MapUnit::MapPixel;
52 mbSimple = true;
55 MapMode::ImplMapMode::ImplMapMode( const ImplMapMode& rImplMapMode ) :
56 meUnit( rImplMapMode.meUnit ),
57 maOrigin( rImplMapMode.maOrigin ),
58 maScaleX( rImplMapMode.maScaleX ),
59 maScaleY( rImplMapMode.maScaleY ),
60 mbSimple( rImplMapMode.mbSimple )
64 bool MapMode::ImplMapMode::operator==( const ImplMapMode& rImpMapMode ) const
66 return meUnit == rImpMapMode.meUnit
67 && maOrigin == rImpMapMode.maOrigin
68 && maScaleX == rImpMapMode.maScaleX
69 && maScaleY == rImpMapMode.maScaleY;
72 namespace
74 struct theGlobalDefault :
75 public rtl::Static< MapMode::ImplType, theGlobalDefault > {};
78 MapMode::MapMode() : mpImplMapMode(theGlobalDefault::get())
82 MapMode::MapMode( const MapMode& rMapMode ) : mpImplMapMode( rMapMode.mpImplMapMode )
86 MapMode::MapMode( MapUnit eUnit ) : mpImplMapMode()
88 mpImplMapMode->meUnit = eUnit;
91 MapMode::MapMode( MapUnit eUnit, const Point& rLogicOrg,
92 const Fraction& rScaleX, const Fraction& rScaleY )
94 mpImplMapMode->meUnit = eUnit;
95 mpImplMapMode->maOrigin = rLogicOrg;
96 mpImplMapMode->maScaleX = rScaleX;
97 mpImplMapMode->maScaleY = rScaleY;
98 mpImplMapMode->maScaleX.ReduceInaccurate(32);
99 mpImplMapMode->maScaleY.ReduceInaccurate(32);
100 mpImplMapMode->mbSimple = false;
103 MapMode::~MapMode()
107 void MapMode::SetMapUnit( MapUnit eUnit )
109 mpImplMapMode->meUnit = eUnit;
112 void MapMode::SetOrigin( const Point& rLogicOrg )
114 mpImplMapMode->maOrigin = rLogicOrg;
115 mpImplMapMode->mbSimple = false;
118 void MapMode::SetScaleX( const Fraction& rScaleX )
120 mpImplMapMode->maScaleX = rScaleX;
121 mpImplMapMode->maScaleX.ReduceInaccurate(32);
122 mpImplMapMode->mbSimple = false;
125 void MapMode::SetScaleY( const Fraction& rScaleY )
127 mpImplMapMode->maScaleY = rScaleY;
128 mpImplMapMode->maScaleY.ReduceInaccurate(32);
129 mpImplMapMode->mbSimple = false;
132 double MapMode::GetUnitMultiplier() const
134 double nMul;
135 switch ( GetMapUnit() )
137 case MapUnit::MapPixel :
138 case MapUnit::MapSysFont :
139 case MapUnit::MapAppFont :
141 case MapUnit::Map100thMM :
142 nMul = 1;
143 break;
144 case MapUnit::Map10thMM :
145 nMul = 10;
146 break;
147 case MapUnit::MapMM :
148 nMul = 100;
149 break;
150 case MapUnit::MapCM :
151 nMul = 1000;
152 break;
153 case MapUnit::Map1000thInch :
154 nMul = 2.54;
155 break;
156 case MapUnit::Map100thInch :
157 nMul = 25.4;
158 break;
159 case MapUnit::Map10thInch :
160 nMul = 254;
161 break;
162 case MapUnit::MapInch :
163 nMul = 2540;
164 break;
165 case MapUnit::MapTwip :
166 nMul = 1.76388889;
167 break;
168 case MapUnit::MapPoint :
169 nMul = 35.27777778;
170 break;
171 default:
172 nMul = 1.0;
173 break;
175 return nMul;
178 MapMode& MapMode::operator=( const MapMode& rMapMode )
180 mpImplMapMode = rMapMode.mpImplMapMode;
181 return *this;
184 MapMode& MapMode::operator=( MapMode&& rMapMode )
186 mpImplMapMode = std::move(rMapMode.mpImplMapMode);
187 return *this;
190 bool MapMode::operator==( const MapMode& rMapMode ) const
192 return mpImplMapMode == rMapMode.mpImplMapMode;
195 bool MapMode::IsDefault() const
197 return mpImplMapMode.same_object(theGlobalDefault::get());
200 SvStream& ReadMapMode( SvStream& rIStm, MapMode& rMapMode )
202 VersionCompat aCompat( rIStm, StreamMode::READ );
203 sal_uInt16 nTmp16;
205 rIStm.ReadUInt16( nTmp16 ); rMapMode.mpImplMapMode->meUnit = (MapUnit) nTmp16;
206 ReadPair( rIStm, rMapMode.mpImplMapMode->maOrigin );
207 ReadFraction( rIStm, rMapMode.mpImplMapMode->maScaleX );
208 ReadFraction( rIStm, rMapMode.mpImplMapMode->maScaleY );
209 rIStm.ReadCharAsBool( rMapMode.mpImplMapMode->mbSimple );
211 return rIStm;
214 SvStream& WriteMapMode( SvStream& rOStm, const MapMode& rMapMode )
216 VersionCompat aCompat( rOStm, StreamMode::WRITE, 1 );
218 rOStm.WriteUInt16( (sal_uInt16)rMapMode.mpImplMapMode->meUnit );
219 WritePair( rOStm, rMapMode.mpImplMapMode->maOrigin );
220 WriteFraction( rOStm, rMapMode.mpImplMapMode->maScaleX );
221 WriteFraction( rOStm, rMapMode.mpImplMapMode->maScaleY );
222 rOStm.WriteBool( rMapMode.mpImplMapMode->mbSimple );
224 return rOStm;
228 MapUnit MapMode::GetMapUnit() const { return mpImplMapMode->meUnit; }
230 const Point& MapMode::GetOrigin() const { return mpImplMapMode->maOrigin; }
232 const Fraction& MapMode::GetScaleX() const { return mpImplMapMode->maScaleX; }
234 const Fraction& MapMode::GetScaleY() const { return mpImplMapMode->maScaleY; }
236 bool MapMode::IsSimple() const { return mpImplMapMode->mbSimple; }
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */