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 <basegfx/point/b2dpoint.hxx>
21 #include <basegfx/polygon/b2dpolygon.hxx>
22 #include <basegfx/polygon/b2dpolypolygon.hxx>
23 #include <o3tl/safeint.hxx>
24 #include <sal/log.hxx>
25 #include "emfppath.hxx"
29 const unsigned char nTopBitInt7
= 0x80;
30 const unsigned char nSignBitInt7
= 0x40;
31 // include the sign bit so if it's negative we get
32 // that "missing" bit pre-set to 1
33 const unsigned char nValueMaskInt7
= 0x7F;
36 namespace emfplushelper
38 // see 2.2.2.21 EmfPlusInteger7
39 // 2.2.2.22 EmfPlusInteger15
40 // and 2.2.2.37 EmfPlusPointR Object
41 static sal_Int16
GetEmfPlusInteger(SvStream
& s
)
46 bool bIsEmfPlusInteger15
= u8
& nTopBitInt7
;
47 bool bNegative
= u8
& nSignBitInt7
;
48 unsigned char val1
= u8
& nValueMaskInt7
;
51 if (!bIsEmfPlusInteger15
)
53 return static_cast<signed char>(val1
);
57 sal_uInt16 nRet
= (val1
<< 8) | u8
;
58 return static_cast<sal_Int16
>(nRet
);
61 EMFPPath::EMFPPath (sal_Int32 _nPoints
, bool bLines
)
63 if (_nPoints
<0 || o3tl::make_unsigned(_nPoints
)>SAL_MAX_INT32
/ (2 * sizeof(float)))
65 _nPoints
= SAL_MAX_INT32
/ (2 * sizeof(float));
69 pPoints
.reset( new float [nPoints
*2] );
72 pPointTypes
.reset( new sal_uInt8
[_nPoints
] );
75 EMFPPath::~EMFPPath ()
79 void EMFPPath::Read (SvStream
& s
, sal_uInt32 pathFlags
)
81 for (int i
= 0; i
< nPoints
; i
++)
83 if (pathFlags
& 0x800)
85 // EMFPlusPointR: points are stored in EMFPlusInteger7 or
86 // EMFPlusInteger15 objects, see section 2.2.2.21/22
87 // If 0x800 bit is set, the 0x4000 bit is undefined and must be ignored
88 sal_Int32 x
= GetEmfPlusInteger(s
);
89 sal_Int32 y
= GetEmfPlusInteger(s
);
91 pPoints
[i
*2 + 1] = y
;
92 SAL_INFO("drawinglayer", "EMF+\t\t\tEmfPlusPointR [x,y]: " << x
<< ", " << y
);
94 else if (pathFlags
& 0x4000)
96 // EMFPlusPoint: stored in signed short 16bit integer format
99 s
.ReadInt16( x
).ReadInt16( y
);
100 SAL_INFO ("drawinglayer", "EMF+\t\t\tEmfPlusPoint [x,y]: " << x
<< "," << y
);
102 pPoints
[i
*2 + 1] = y
;
106 // EMFPlusPointF: stored in Single (float) format
107 s
.ReadFloat( pPoints
[i
*2] ).ReadFloat( pPoints
[i
*2 + 1] );
108 SAL_INFO ("drawinglayer", "EMF+\t EMFPlusPointF [x,y]: " << pPoints
[i
*2] << "," << pPoints
[i
*2 + 1]);
114 for (int i
= 0; i
< nPoints
; i
++)
116 s
.ReadUChar(pPointTypes
[i
]);
117 SAL_INFO("drawinglayer", "EMF+\tpoint type: " << static_cast<int>(pPointTypes
[i
]));
124 ::basegfx::B2DPolyPolygon
& EMFPPath::GetPolygon (EmfPlusHelperData
const & rR
, bool bMapIt
, bool bAddLineToCloseShape
)
126 ::basegfx::B2DPolygon polygon
;
128 int last_normal
= 0, p
= 0;
129 ::basegfx::B2DPoint prev
, mapped
;
130 bool hasPrev
= false;
132 for (int i
= 0; i
< nPoints
; i
++)
134 if (p
&& pPointTypes
&& (pPointTypes
[i
] == 0))
136 aPolygon
.append (polygon
);
143 mapped
= rR
.Map (pPoints
[i
*2], pPoints
[i
*2 + 1]);
145 mapped
= ::basegfx::B2DPoint (pPoints
[i
*2], pPoints
[i
*2 + 1]);
149 if ((pPointTypes
[i
] & 0x07) == 3)
151 if (((i
- last_normal
)% 3) == 1)
153 polygon
.setNextControlPoint (p
- 1, mapped
);
154 SAL_INFO ("drawinglayer", "EMF+\t\tPolygon append next: " << p
- 1 << " mapped: " << mapped
.getX () << "," << mapped
.getY ());
157 else if (((i
- last_normal
) % 3) == 2)
170 polygon
.append (mapped
);
171 SAL_INFO ("drawinglayer", "EMF+\t\tPolygon append point: " << pPoints
[i
*2] << "," << pPoints
[i
*2 + 1] << " mapped: " << mapped
.getX () << ":" << mapped
.getY ());
175 polygon
.setPrevControlPoint (p
, prev
);
176 SAL_INFO ("drawinglayer", "EMF+\t\tPolygon append prev: " << p
<< " mapped: " << prev
.getX () << "," << prev
.getY ());
182 if (pPointTypes
&& (pPointTypes
[i
] & 0x80))
185 polygon
.setClosed (true);
186 aPolygon
.append (polygon
);
187 SAL_INFO ("drawinglayer", "EMF+\t\tClose polygon");
194 // Draw an extra line between the last point and the first point, to close the shape.
195 if (bAddLineToCloseShape
)
197 polygon
.setClosed (true);
200 if (polygon
.count ())
202 aPolygon
.append (polygon
);
204 #if OSL_DEBUG_LEVEL > 1
205 for (unsigned int i
=0; i
<aPolygon
.count(); i
++) {
206 polygon
= aPolygon
.getB2DPolygon(i
);
207 SAL_INFO ("drawinglayer", "EMF+\t\tPolygon: " << i
);
208 for (unsigned int j
=0; j
<polygon
.count(); j
++) {
209 ::basegfx::B2DPoint point
= polygon
.getB2DPoint(j
);
210 SAL_INFO ("drawinglayer", "EMF+\t\t\tPoint: " << point
.getX() << "," << point
.getY());
211 if (polygon
.isPrevControlPointUsed(j
)) {
212 point
= polygon
.getPrevControlPoint(j
);
213 SAL_INFO ("drawinglayer", "EMF+\t\t\tPrev: " << point
.getX() << "," << point
.getY());
215 if (polygon
.isNextControlPointUsed(j
)) {
216 point
= polygon
.getNextControlPoint(j
);
217 SAL_INFO ("drawinglayer", "EMF+\t\t\tNext: " << point
.getX() << "," << point
.getY());
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */