calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / drawinglayer / source / tools / emfppath.cxx
blob4217e36160871d86517825cfe74ce05d42a9b4c7
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/point/b2dpoint.hxx>
21 #include <basegfx/polygon/b2dpolygon.hxx>
22 #include <basegfx/polygon/b2dpolypolygon.hxx>
23 #include <sal/log.hxx>
24 #include "emfppath.hxx"
26 namespace
28 const unsigned char nTopBitInt7 = 0x80;
29 const unsigned char nSignBitInt7 = 0x40;
30 // include the sign bit so if it's negative we get
31 // that "missing" bit pre-set to 1
32 const unsigned char nValueMaskInt7 = 0x7F;
35 namespace emfplushelper
37 // see 2.2.2.21 EmfPlusInteger7
38 // 2.2.2.22 EmfPlusInteger15
39 // and 2.2.2.37 EmfPlusPointR Object
40 static sal_Int16 GetEmfPlusInteger(SvStream& s)
42 unsigned char u8(0);
43 s.ReadUChar(u8);
45 bool bIsEmfPlusInteger15 = u8 & nTopBitInt7;
46 bool bNegative = u8 & nSignBitInt7;
47 unsigned char val1 = u8 & nValueMaskInt7;
48 if (bNegative)
49 val1 |= nTopBitInt7;
50 if (!bIsEmfPlusInteger15)
52 return static_cast<signed char>(val1);
55 s.ReadUChar(u8);
56 sal_uInt16 nRet = (val1 << 8) | u8;
57 return static_cast<sal_Int16>(nRet);
60 EMFPPath::EMFPPath (sal_uInt32 _nPoints, bool bLines)
62 if (_nPoints > SAL_MAX_UINT32 / (2 * sizeof(float)))
64 _nPoints = SAL_MAX_UINT32 / (2 * sizeof(float));
67 nPoints = _nPoints;
68 pPoints.reset( new float [nPoints*2] );
70 if (!bLines)
71 pPointTypes.reset( new sal_uInt8 [_nPoints] );
74 EMFPPath::~EMFPPath ()
78 void EMFPPath::Read (SvStream& s, sal_uInt32 pathFlags)
80 for (sal_uInt32 i = 0; i < nPoints; i ++)
82 if (pathFlags & 0x800)
84 // EMFPlusPointR: points are stored in EMFPlusInteger7 or
85 // EMFPlusInteger15 objects, see section 2.2.2.21/22
86 // If 0x800 bit is set, the 0x4000 bit is undefined and must be ignored
87 sal_Int32 x = GetEmfPlusInteger(s);
88 sal_Int32 y = GetEmfPlusInteger(s);
89 pPoints [i*2] = x;
90 pPoints [i*2 + 1] = y;
91 SAL_INFO("drawinglayer.emf", "EMF+\t\t\t" << i << ". EmfPlusPointR [x,y]: " << x << ", " << y);
93 else if (pathFlags & 0x4000)
95 // EMFPlusPoint: stored in signed short 16bit integer format
96 sal_Int16 x, y;
98 s.ReadInt16( x ).ReadInt16( y );
99 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\t" << i << ". EmfPlusPoint [x,y]: " << x << ", " << y);
100 pPoints [i*2] = x;
101 pPoints [i*2 + 1] = y;
103 else
105 // EMFPlusPointF: stored in Single (float) format
106 s.ReadFloat( pPoints [i*2] ).ReadFloat( pPoints [i*2 + 1] );
107 SAL_INFO("drawinglayer.emf", "EMF+\t" << i << ". EMFPlusPointF [x,y]: " << pPoints [i * 2] << ", " << pPoints [i * 2 + 1]);
111 if (pPointTypes)
113 for (sal_uInt32 i = 0; i < nPoints; i++)
115 s.ReadUChar(pPointTypes[i]);
116 SAL_INFO("drawinglayer.emf", "EMF+\tpoint type: 0x" << std::hex << static_cast<int>(pPointTypes[i]) << std::dec);
120 aPolygon.clear();
123 ::basegfx::B2DPolyPolygon& EMFPPath::GetPolygon (EmfPlusHelperData const & rR, bool bMapIt, bool bAddLineToCloseShape)
125 ::basegfx::B2DPolygon polygon;
126 aPolygon.clear ();
127 sal_uInt32 last_normal = 0, p = 0;
128 ::basegfx::B2DPoint prev, mapped;
129 bool hasPrev = false;
131 for (sal_uInt32 i = 0; i < nPoints; i ++)
133 if (p && pPointTypes && (pPointTypes [i] == 0))
135 aPolygon.append (polygon);
136 last_normal = i;
137 p = 0;
138 polygon.clear ();
141 if (bMapIt)
142 mapped = rR.Map (pPoints [i*2], pPoints [i*2 + 1]);
143 else
144 mapped = ::basegfx::B2DPoint (pPoints [i*2], pPoints [i*2 + 1]);
146 if (pPointTypes)
148 if ((pPointTypes [i] & 0x07) == 3)
150 if (((i - last_normal )% 3) == 1)
152 polygon.setNextControlPoint (p - 1, mapped);
153 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append next: " << p - 1 << " mapped: " << mapped.getX () << "," << mapped.getY ());
154 continue;
156 else if (((i - last_normal) % 3) == 2)
158 prev = mapped;
159 hasPrev = true;
160 continue;
163 else
165 last_normal = i;
169 polygon.append (mapped);
170 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPoint: " << pPoints [i*2] << "," << pPoints [i*2 + 1] << " mapped: " << mapped.getX () << ":" << mapped.getY ());
172 if (hasPrev)
174 polygon.setPrevControlPoint (p, prev);
175 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon append prev: " << p << " mapped: " << prev.getX () << "," << prev.getY ());
176 hasPrev = false;
179 p++;
181 if (pPointTypes && (pPointTypes [i] & 0x80)) // closed polygon
183 polygon.setClosed (true);
184 aPolygon.append (polygon);
185 SAL_INFO ("drawinglayer.emf", "EMF+\t\tClose polygon");
186 last_normal = i + 1;
187 p = 0;
188 polygon.clear ();
192 // Draw an extra line between the last point and the first point, to close the shape.
193 if (bAddLineToCloseShape)
195 polygon.setClosed (true);
198 if (polygon.count ())
200 aPolygon.append (polygon);
202 #if OSL_DEBUG_LEVEL > 1
203 for (unsigned int i=0; i<aPolygon.count(); i++) {
204 polygon = aPolygon.getB2DPolygon(i);
205 SAL_INFO ("drawinglayer.emf", "EMF+\t\tPolygon: " << i);
206 for (unsigned int j=0; j<polygon.count(); j++) {
207 ::basegfx::B2DPoint point = polygon.getB2DPoint(j);
208 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tPoint: " << point.getX() << "," << point.getY());
209 if (polygon.isPrevControlPointUsed(j)) {
210 point = polygon.getPrevControlPoint(j);
211 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tPrev: " << point.getX() << "," << point.getY());
213 if (polygon.isNextControlPointUsed(j)) {
214 point = polygon.getNextControlPoint(j);
215 SAL_INFO ("drawinglayer.emf", "EMF+\t\t\tNext: " << point.getX() << "," << point.getY());
219 #endif
222 return aPolygon;
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */