File: | home/maarten/src/libreoffice/core/include/rtl/ref.hxx |
Warning: | line 192, column 9 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | ||||||||
2 | /* | ||||||||
3 | * This file is part of the LibreOffice project. | ||||||||
4 | * | ||||||||
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/. | ||||||||
8 | * | ||||||||
9 | * This file incorporates work covered by the following license notice: | ||||||||
10 | * | ||||||||
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 . | ||||||||
18 | */ | ||||||||
19 | |||||||||
20 | #include <sal/log.hxx> | ||||||||
21 | #include <vcl/bitmapaccess.hxx> | ||||||||
22 | #include <tools/link.hxx> | ||||||||
23 | #include <tools/poly.hxx> | ||||||||
24 | #include <tools/helpers.hxx> | ||||||||
25 | #include <vcl/gdimtf.hxx> | ||||||||
26 | #include <vcl/metaact.hxx> | ||||||||
27 | #include <vcl/virdev.hxx> | ||||||||
28 | #include "impvect.hxx" | ||||||||
29 | #include <array> | ||||||||
30 | #include <memory> | ||||||||
31 | |||||||||
32 | #define VECT_POLY_MAX8192 8192 | ||||||||
33 | |||||||||
34 | #define VECT_FREE_INDEX0 0 | ||||||||
35 | #define VECT_CONT_INDEX1 1 | ||||||||
36 | #define VECT_DONE_INDEX2 2 | ||||||||
37 | |||||||||
38 | #define VECT_POLY_INLINE_INNER1UL 1UL | ||||||||
39 | #define VECT_POLY_INLINE_OUTER2UL 2UL | ||||||||
40 | #define VECT_POLY_OUTLINE_INNER4UL 4UL | ||||||||
41 | #define VECT_POLY_OUTLINE_OUTER8UL 8UL | ||||||||
42 | |||||||||
43 | static void VECT_MAP( const std::unique_ptr<long []> & pMapIn, const std::unique_ptr<long []>& pMapOut, long nVal ) | ||||||||
44 | { | ||||||||
45 | pMapIn[nVal] = (nVal * 4) + 1; | ||||||||
46 | pMapOut[nVal] = pMapIn[nVal] + 5; | ||||||||
47 | } | ||||||||
48 | static constexpr long BACK_MAP( long _def_nVal ) | ||||||||
49 | { | ||||||||
50 | return ((_def_nVal + 2) >> 2) - 1; | ||||||||
51 | } | ||||||||
52 | static void VECT_PROGRESS( const Link<long, void>* pProgress, long _def_nVal ) | ||||||||
53 | { | ||||||||
54 | if(pProgress) | ||||||||
55 | pProgress->Call(_def_nVal); | ||||||||
56 | } | ||||||||
57 | |||||||||
58 | namespace { | ||||||||
59 | |||||||||
60 | class ImplVectMap; | ||||||||
61 | class ImplChain; | ||||||||
62 | |||||||||
63 | } | ||||||||
64 | |||||||||
65 | namespace ImplVectorizer | ||||||||
66 | { | ||||||||
67 | static ImplVectMap* ImplExpand( BitmapReadAccess* pRAcc, const Color& rColor ); | ||||||||
68 | static void ImplCalculate( ImplVectMap* pMap, tools::PolyPolygon& rPolyPoly, sal_uInt8 cReduce ); | ||||||||
69 | static bool ImplGetChain( ImplVectMap* pMap, const Point& rStartPt, ImplChain& rChain ); | ||||||||
70 | static bool ImplIsUp( ImplVectMap const * pMap, long nY, long nX ); | ||||||||
71 | static void ImplLimitPolyPoly( tools::PolyPolygon& rPolyPoly ); | ||||||||
72 | } | ||||||||
73 | |||||||||
74 | namespace { | ||||||||
75 | |||||||||
76 | struct ChainMove { long nDX; long nDY; }; | ||||||||
77 | |||||||||
78 | } | ||||||||
79 | |||||||||
80 | const ChainMove aImplMove[ 8 ] = { | ||||||||
81 | { 1, 0 }, | ||||||||
82 | { 0, -1 }, | ||||||||
83 | { -1, 0 }, | ||||||||
84 | { 0, 1 }, | ||||||||
85 | { 1, -1 }, | ||||||||
86 | { -1, -1 }, | ||||||||
87 | { -1, 1 }, | ||||||||
88 | { 1, 1 } | ||||||||
89 | }; | ||||||||
90 | |||||||||
91 | const ChainMove aImplMoveInner[ 8 ] = { | ||||||||
92 | { 0, 1 }, | ||||||||
93 | { 1, 0 }, | ||||||||
94 | { 0, -1 }, | ||||||||
95 | { -1, 0 }, | ||||||||
96 | { 0, 1 }, | ||||||||
97 | { 1, 0 }, | ||||||||
98 | { 0, -1 }, | ||||||||
99 | { -1, 0 } | ||||||||
100 | }; | ||||||||
101 | |||||||||
102 | const ChainMove aImplMoveOuter[ 8 ] = { | ||||||||
103 | { 0, -1 }, | ||||||||
104 | { -1, 0 }, | ||||||||
105 | { 0, 1 }, | ||||||||
106 | { 1, 0 }, | ||||||||
107 | { -1, 0 }, | ||||||||
108 | { 0, 1 }, | ||||||||
109 | { 1, 0 }, | ||||||||
110 | { 0, -1 } | ||||||||
111 | }; | ||||||||
112 | |||||||||
113 | namespace { | ||||||||
114 | |||||||||
115 | struct ImplColorSet | ||||||||
116 | { | ||||||||
117 | BitmapColor maColor; | ||||||||
118 | sal_uInt16 mnIndex = 0; | ||||||||
119 | bool mbSet = false; | ||||||||
120 | }; | ||||||||
121 | |||||||||
122 | } | ||||||||
123 | |||||||||
124 | static bool ImplColorSetCmpFnc( const ImplColorSet& lhs, const ImplColorSet& rhs) | ||||||||
125 | { | ||||||||
126 | if( lhs.mbSet && rhs.mbSet ) | ||||||||
127 | { | ||||||||
128 | const sal_uInt8 cLum1 = lhs.maColor.GetLuminance(); | ||||||||
129 | const sal_uInt8 cLum2 = rhs.maColor.GetLuminance(); | ||||||||
130 | return cLum1 < cLum2; | ||||||||
131 | } | ||||||||
132 | return lhs.mbSet > rhs.mbSet; | ||||||||
133 | } | ||||||||
134 | |||||||||
135 | namespace { | ||||||||
136 | |||||||||
137 | class ImplPointArray | ||||||||
138 | { | ||||||||
139 | std::unique_ptr<Point[]> mpArray; | ||||||||
140 | sal_uLong mnSize; | ||||||||
141 | sal_uLong mnRealSize; | ||||||||
142 | |||||||||
143 | public: | ||||||||
144 | |||||||||
145 | ImplPointArray(); | ||||||||
146 | |||||||||
147 | void ImplSetSize( sal_uLong nSize ); | ||||||||
148 | sal_uLong ImplGetRealSize() const { return mnRealSize; } | ||||||||
149 | void ImplSetRealSize( sal_uLong nRealSize ) { mnRealSize = nRealSize; } | ||||||||
150 | void ImplCreatePoly( tools::Polygon& rPoly ) const; | ||||||||
151 | |||||||||
152 | inline Point& operator[]( sal_uLong nPos ); | ||||||||
153 | inline const Point& operator[]( sal_uLong nPos ) const; | ||||||||
154 | |||||||||
155 | }; | ||||||||
156 | |||||||||
157 | } | ||||||||
158 | |||||||||
159 | ImplPointArray::ImplPointArray() : | ||||||||
160 | mnSize ( 0 ), | ||||||||
161 | mnRealSize ( 0 ) | ||||||||
162 | |||||||||
163 | { | ||||||||
164 | } | ||||||||
165 | |||||||||
166 | void ImplPointArray::ImplSetSize( sal_uLong nSize ) | ||||||||
167 | { | ||||||||
168 | const sal_uLong nTotal = nSize * sizeof( Point ); | ||||||||
169 | |||||||||
170 | mnSize = nSize; | ||||||||
171 | mnRealSize = 0; | ||||||||
172 | |||||||||
173 | mpArray = std::make_unique<Point[]>( nTotal ); | ||||||||
174 | } | ||||||||
175 | |||||||||
176 | inline Point& ImplPointArray::operator[]( sal_uLong nPos ) | ||||||||
177 | { | ||||||||
178 | SAL_WARN_IF( nPos >= mnSize, "vcl", "ImplPointArray::operator[]: nPos out of range!" )do { if (true && (nPos >= mnSize)) { switch (sal_detail_log_report (::SAL_DETAIL_LOG_LEVEL_WARN, "vcl")) { case SAL_DETAIL_LOG_ACTION_IGNORE : break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail ::getResult( ::sal::detail::StreamStart() << "ImplPointArray::operator[]: nPos out of range!" ) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl" ), ("/home/maarten/src/libreoffice/core/vcl/source/gdi/impvect.cxx" ":" "178" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "ImplPointArray::operator[]: nPos out of range!" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "ImplPointArray::operator[]: nPos out of range!"; :: sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl"), ("/home/maarten/src/libreoffice/core/vcl/source/gdi/impvect.cxx" ":" "178" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "ImplPointArray::operator[]: nPos out of range!") == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl" ), ("/home/maarten/src/libreoffice/core/vcl/source/gdi/impvect.cxx" ":" "178" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "ImplPointArray::operator[]: nPos out of range!" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "ImplPointArray::operator[]: nPos out of range!"; :: sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl"), ("/home/maarten/src/libreoffice/core/vcl/source/gdi/impvect.cxx" ":" "178" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); | ||||||||
179 | return mpArray[ nPos ]; | ||||||||
180 | } | ||||||||
181 | |||||||||
182 | inline const Point& ImplPointArray::operator[]( sal_uLong nPos ) const | ||||||||
183 | { | ||||||||
184 | SAL_WARN_IF( nPos >= mnSize, "vcl", "ImplPointArray::operator[]: nPos out of range!" )do { if (true && (nPos >= mnSize)) { switch (sal_detail_log_report (::SAL_DETAIL_LOG_LEVEL_WARN, "vcl")) { case SAL_DETAIL_LOG_ACTION_IGNORE : break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail ::getResult( ::sal::detail::StreamStart() << "ImplPointArray::operator[]: nPos out of range!" ) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl" ), ("/home/maarten/src/libreoffice/core/vcl/source/gdi/impvect.cxx" ":" "184" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "ImplPointArray::operator[]: nPos out of range!" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "ImplPointArray::operator[]: nPos out of range!"; :: sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl"), ("/home/maarten/src/libreoffice/core/vcl/source/gdi/impvect.cxx" ":" "184" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "ImplPointArray::operator[]: nPos out of range!") == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl" ), ("/home/maarten/src/libreoffice/core/vcl/source/gdi/impvect.cxx" ":" "184" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "ImplPointArray::operator[]: nPos out of range!" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "ImplPointArray::operator[]: nPos out of range!"; :: sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl"), ("/home/maarten/src/libreoffice/core/vcl/source/gdi/impvect.cxx" ":" "184" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); | ||||||||
185 | return mpArray[ nPos ]; | ||||||||
186 | } | ||||||||
187 | |||||||||
188 | void ImplPointArray::ImplCreatePoly( tools::Polygon& rPoly ) const | ||||||||
189 | { | ||||||||
190 | rPoly = tools::Polygon( sal::static_int_cast<sal_uInt16>(mnRealSize), mpArray.get() ); | ||||||||
191 | } | ||||||||
192 | |||||||||
193 | namespace { | ||||||||
194 | |||||||||
195 | class ImplVectMap | ||||||||
196 | { | ||||||||
197 | private: | ||||||||
198 | |||||||||
199 | Scanline mpBuf; | ||||||||
200 | Scanline* mpScan; | ||||||||
201 | long mnWidth; | ||||||||
202 | long mnHeight; | ||||||||
203 | |||||||||
204 | public: | ||||||||
205 | |||||||||
206 | ImplVectMap( long nWidth, long nHeight ); | ||||||||
207 | ~ImplVectMap(); | ||||||||
208 | |||||||||
209 | long Width() const { return mnWidth; } | ||||||||
210 | long Height() const { return mnHeight; } | ||||||||
211 | |||||||||
212 | inline void Set( long nY, long nX, sal_uInt8 cVal ); | ||||||||
213 | inline sal_uInt8 Get( long nY, long nX ) const; | ||||||||
214 | |||||||||
215 | inline bool IsFree( long nY, long nX ) const; | ||||||||
216 | inline bool IsCont( long nY, long nX ) const; | ||||||||
217 | inline bool IsDone( long nY, long nX ) const; | ||||||||
218 | |||||||||
219 | }; | ||||||||
220 | |||||||||
221 | } | ||||||||
222 | |||||||||
223 | ImplVectMap::ImplVectMap( long nWidth, long nHeight ) : | ||||||||
224 | mpBuf ( static_cast<Scanline>(rtl_allocateZeroMemory(nWidth * nHeight)) ), | ||||||||
225 | mpScan ( static_cast<Scanline*>(std::malloc(nHeight * sizeof(Scanline))) ), | ||||||||
226 | mnWidth ( nWidth ), | ||||||||
227 | mnHeight( nHeight ) | ||||||||
228 | { | ||||||||
229 | const long nWidthAl = ( nWidth >> 2 ) + 1; | ||||||||
230 | Scanline pTmp = mpBuf; | ||||||||
231 | |||||||||
232 | for( long nY = 0; nY < nHeight; pTmp += nWidthAl ) | ||||||||
233 | mpScan[ nY++ ] = pTmp; | ||||||||
234 | } | ||||||||
235 | |||||||||
236 | ImplVectMap::~ImplVectMap() | ||||||||
237 | { | ||||||||
238 | std::free( mpBuf ); | ||||||||
239 | std::free( mpScan ); | ||||||||
240 | } | ||||||||
241 | |||||||||
242 | inline void ImplVectMap::Set( long nY, long nX, sal_uInt8 cVal ) | ||||||||
243 | { | ||||||||
244 | const sal_uInt8 cShift = sal::static_int_cast<sal_uInt8>(6 - ( ( nX & 3 ) << 1 )); | ||||||||
245 | auto & rPixel = mpScan[ nY ][ nX >> 2 ]; | ||||||||
246 | rPixel = (rPixel & ~( 3 << cShift ) ) | ( cVal << cShift ); | ||||||||
247 | } | ||||||||
248 | |||||||||
249 | inline sal_uInt8 ImplVectMap::Get( long nY, long nX ) const | ||||||||
250 | { | ||||||||
251 | return sal::static_int_cast<sal_uInt8>( ( ( mpScan[ nY ][ nX >> 2 ] ) >> ( 6 - ( ( nX & 3 ) << 1 ) ) ) & 3 ); | ||||||||
252 | } | ||||||||
253 | |||||||||
254 | inline bool ImplVectMap::IsFree( long nY, long nX ) const | ||||||||
255 | { | ||||||||
256 | return( VECT_FREE_INDEX0 == Get( nY, nX ) ); | ||||||||
257 | } | ||||||||
258 | |||||||||
259 | inline bool ImplVectMap::IsCont( long nY, long nX ) const | ||||||||
260 | { | ||||||||
261 | return( VECT_CONT_INDEX1 == Get( nY, nX ) ); | ||||||||
262 | } | ||||||||
263 | |||||||||
264 | inline bool ImplVectMap::IsDone( long nY, long nX ) const | ||||||||
265 | { | ||||||||
266 | return( VECT_DONE_INDEX2 == Get( nY, nX ) ); | ||||||||
267 | } | ||||||||
268 | |||||||||
269 | namespace { | ||||||||
270 | |||||||||
271 | class ImplChain | ||||||||
272 | { | ||||||||
273 | private: | ||||||||
274 | |||||||||
275 | tools::Polygon maPoly; | ||||||||
276 | Point maStartPt; | ||||||||
277 | sal_uLong mnArraySize; | ||||||||
278 | sal_uLong mnCount; | ||||||||
279 | std::unique_ptr<sal_uInt8[]> | ||||||||
280 | mpCodes; | ||||||||
281 | |||||||||
282 | void ImplGetSpace(); | ||||||||
283 | |||||||||
284 | void ImplPostProcess( const ImplPointArray& rArr ); | ||||||||
285 | |||||||||
286 | ImplChain(const ImplChain&) = delete; | ||||||||
287 | ImplChain& operator=(const ImplChain&) = delete; | ||||||||
288 | |||||||||
289 | public: | ||||||||
290 | |||||||||
291 | ImplChain(); | ||||||||
292 | |||||||||
293 | void ImplBeginAdd( const Point& rStartPt ); | ||||||||
294 | inline void ImplAdd( sal_uInt8 nCode ); | ||||||||
295 | void ImplEndAdd( sal_uLong nTypeFlag ); | ||||||||
296 | |||||||||
297 | const tools::Polygon& ImplGetPoly() const { return maPoly; } | ||||||||
298 | }; | ||||||||
299 | |||||||||
300 | } | ||||||||
301 | |||||||||
302 | ImplChain::ImplChain() : | ||||||||
303 | mnArraySize ( 1024 ), | ||||||||
304 | mnCount ( 0 ), | ||||||||
305 | mpCodes ( new sal_uInt8[mnArraySize] ) | ||||||||
306 | { | ||||||||
307 | } | ||||||||
308 | |||||||||
309 | void ImplChain::ImplGetSpace() | ||||||||
310 | { | ||||||||
311 | const sal_uLong nOldArraySize = mnArraySize; | ||||||||
312 | sal_uInt8* pNewCodes; | ||||||||
313 | |||||||||
314 | mnArraySize = mnArraySize << 1; | ||||||||
315 | pNewCodes = new sal_uInt8[ mnArraySize ]; | ||||||||
316 | memcpy( pNewCodes, mpCodes.get(), nOldArraySize ); | ||||||||
317 | mpCodes.reset( pNewCodes ); | ||||||||
318 | } | ||||||||
319 | |||||||||
320 | void ImplChain::ImplBeginAdd( const Point& rStartPt ) | ||||||||
321 | { | ||||||||
322 | maPoly = tools::Polygon(); | ||||||||
323 | maStartPt = rStartPt; | ||||||||
324 | mnCount = 0; | ||||||||
325 | } | ||||||||
326 | |||||||||
327 | inline void ImplChain::ImplAdd( sal_uInt8 nCode ) | ||||||||
328 | { | ||||||||
329 | if( mnCount == mnArraySize ) | ||||||||
330 | ImplGetSpace(); | ||||||||
331 | |||||||||
332 | mpCodes[ mnCount++ ] = nCode; | ||||||||
333 | } | ||||||||
334 | |||||||||
335 | void ImplChain::ImplEndAdd( sal_uLong nFlag ) | ||||||||
336 | { | ||||||||
337 | if( mnCount ) | ||||||||
338 | { | ||||||||
339 | ImplPointArray aArr; | ||||||||
340 | |||||||||
341 | if( nFlag & VECT_POLY_INLINE_INNER1UL ) | ||||||||
342 | { | ||||||||
343 | long nFirstX, nFirstY; | ||||||||
344 | long nLastX, nLastY; | ||||||||
345 | |||||||||
346 | nFirstX = nLastX = maStartPt.X(); | ||||||||
347 | nFirstY = nLastY = maStartPt.Y(); | ||||||||
348 | aArr.ImplSetSize( mnCount << 1 ); | ||||||||
349 | |||||||||
350 | sal_uInt16 nPolyPos; | ||||||||
351 | sal_uLong i; | ||||||||
352 | for( i = 0, nPolyPos = 0; i < ( mnCount - 1 ); i++ ) | ||||||||
353 | { | ||||||||
354 | const sal_uInt8 cMove = mpCodes[ i ]; | ||||||||
355 | const sal_uInt8 cNextMove = mpCodes[ i + 1 ]; | ||||||||
356 | const ChainMove& rMove = aImplMove[ cMove ]; | ||||||||
357 | const ChainMove& rMoveInner = aImplMoveInner[ cMove ]; | ||||||||
358 | // Point& rPt = aArr[ nPolyPos ]; | ||||||||
359 | bool bDone = true; | ||||||||
360 | |||||||||
361 | nLastX += rMove.nDX; | ||||||||
362 | nLastY += rMove.nDY; | ||||||||
363 | |||||||||
364 | if( cMove < 4 ) | ||||||||
365 | { | ||||||||
366 | if( ( cMove == 0 && cNextMove == 3 ) || | ||||||||
367 | ( cMove == 3 && cNextMove == 2 ) || | ||||||||
368 | ( cMove == 2 && cNextMove == 1 ) || | ||||||||
369 | ( cMove == 1 && cNextMove == 0 ) ) | ||||||||
370 | { | ||||||||
371 | } | ||||||||
372 | else if( cMove == 2 && cNextMove == 3 ) | ||||||||
373 | { | ||||||||
374 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
375 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
376 | |||||||||
377 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
378 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
379 | |||||||||
380 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
381 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
382 | } | ||||||||
383 | else if( cMove == 3 && cNextMove == 0 ) | ||||||||
384 | { | ||||||||
385 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
386 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
387 | |||||||||
388 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
389 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
390 | |||||||||
391 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
392 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
393 | } | ||||||||
394 | else if( cMove == 0 && cNextMove == 1 ) | ||||||||
395 | { | ||||||||
396 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
397 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
398 | |||||||||
399 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
400 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
401 | |||||||||
402 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
403 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
404 | } | ||||||||
405 | else if( cMove == 1 && cNextMove == 2 ) | ||||||||
406 | { | ||||||||
407 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
408 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
409 | |||||||||
410 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
411 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
412 | |||||||||
413 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
414 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
415 | } | ||||||||
416 | else | ||||||||
417 | bDone = false; | ||||||||
418 | } | ||||||||
419 | else if( cMove == 7 && cNextMove == 0 ) | ||||||||
420 | { | ||||||||
421 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
422 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
423 | |||||||||
424 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
425 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
426 | } | ||||||||
427 | else if( cMove == 4 && cNextMove == 1 ) | ||||||||
428 | { | ||||||||
429 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
430 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
431 | |||||||||
432 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
433 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
434 | } | ||||||||
435 | else | ||||||||
436 | bDone = false; | ||||||||
437 | |||||||||
438 | if( !bDone ) | ||||||||
439 | { | ||||||||
440 | aArr[ nPolyPos ].setX( nLastX + rMoveInner.nDX ); | ||||||||
441 | aArr[ nPolyPos++ ].setY( nLastY + rMoveInner.nDY ); | ||||||||
442 | } | ||||||||
443 | } | ||||||||
444 | |||||||||
445 | aArr[ nPolyPos ].setX( nFirstX + 1 ); | ||||||||
446 | aArr[ nPolyPos++ ].setY( nFirstY + 1 ); | ||||||||
447 | aArr.ImplSetRealSize( nPolyPos ); | ||||||||
448 | } | ||||||||
449 | else if( nFlag & VECT_POLY_INLINE_OUTER2UL ) | ||||||||
450 | { | ||||||||
451 | long nFirstX, nFirstY; | ||||||||
452 | long nLastX, nLastY; | ||||||||
453 | |||||||||
454 | nFirstX = nLastX = maStartPt.X(); | ||||||||
455 | nFirstY = nLastY = maStartPt.Y(); | ||||||||
456 | aArr.ImplSetSize( mnCount << 1 ); | ||||||||
457 | |||||||||
458 | sal_uInt16 nPolyPos; | ||||||||
459 | sal_uLong i; | ||||||||
460 | for( i = 0, nPolyPos = 0; i < ( mnCount - 1 ); i++ ) | ||||||||
461 | { | ||||||||
462 | const sal_uInt8 cMove = mpCodes[ i ]; | ||||||||
463 | const sal_uInt8 cNextMove = mpCodes[ i + 1 ]; | ||||||||
464 | const ChainMove& rMove = aImplMove[ cMove ]; | ||||||||
465 | const ChainMove& rMoveOuter = aImplMoveOuter[ cMove ]; | ||||||||
466 | // Point& rPt = aArr[ nPolyPos ]; | ||||||||
467 | bool bDone = true; | ||||||||
468 | |||||||||
469 | nLastX += rMove.nDX; | ||||||||
470 | nLastY += rMove.nDY; | ||||||||
471 | |||||||||
472 | if( cMove < 4 ) | ||||||||
473 | { | ||||||||
474 | if( ( cMove == 0 && cNextMove == 1 ) || | ||||||||
475 | ( cMove == 1 && cNextMove == 2 ) || | ||||||||
476 | ( cMove == 2 && cNextMove == 3 ) || | ||||||||
477 | ( cMove == 3 && cNextMove == 0 ) ) | ||||||||
478 | { | ||||||||
479 | } | ||||||||
480 | else if( cMove == 0 && cNextMove == 3 ) | ||||||||
481 | { | ||||||||
482 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
483 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
484 | |||||||||
485 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
486 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
487 | |||||||||
488 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
489 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
490 | } | ||||||||
491 | else if( cMove == 3 && cNextMove == 2 ) | ||||||||
492 | { | ||||||||
493 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
494 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
495 | |||||||||
496 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
497 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
498 | |||||||||
499 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
500 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
501 | } | ||||||||
502 | else if( cMove == 2 && cNextMove == 1 ) | ||||||||
503 | { | ||||||||
504 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
505 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
506 | |||||||||
507 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
508 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
509 | |||||||||
510 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
511 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
512 | } | ||||||||
513 | else if( cMove == 1 && cNextMove == 0 ) | ||||||||
514 | { | ||||||||
515 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
516 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
517 | |||||||||
518 | aArr[ nPolyPos ].setX( nLastX - 1 ); | ||||||||
519 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
520 | |||||||||
521 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
522 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
523 | } | ||||||||
524 | else | ||||||||
525 | bDone = false; | ||||||||
526 | } | ||||||||
527 | else if( cMove == 7 && cNextMove == 3 ) | ||||||||
528 | { | ||||||||
529 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
530 | aArr[ nPolyPos++ ].setY( nLastY - 1 ); | ||||||||
531 | |||||||||
532 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
533 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
534 | } | ||||||||
535 | else if( cMove == 6 && cNextMove == 2 ) | ||||||||
536 | { | ||||||||
537 | aArr[ nPolyPos ].setX( nLastX + 1 ); | ||||||||
538 | aArr[ nPolyPos++ ].setY( nLastY ); | ||||||||
539 | |||||||||
540 | aArr[ nPolyPos ].setX( nLastX ); | ||||||||
541 | aArr[ nPolyPos++ ].setY( nLastY + 1 ); | ||||||||
542 | } | ||||||||
543 | else | ||||||||
544 | bDone = false; | ||||||||
545 | |||||||||
546 | if( !bDone ) | ||||||||
547 | { | ||||||||
548 | aArr[ nPolyPos ].setX( nLastX + rMoveOuter.nDX ); | ||||||||
549 | aArr[ nPolyPos++ ].setY( nLastY + rMoveOuter.nDY ); | ||||||||
550 | } | ||||||||
551 | } | ||||||||
552 | |||||||||
553 | aArr[ nPolyPos ].setX( nFirstX - 1 ); | ||||||||
554 | aArr[ nPolyPos++ ].setY( nFirstY - 1 ); | ||||||||
555 | aArr.ImplSetRealSize( nPolyPos ); | ||||||||
556 | } | ||||||||
557 | else | ||||||||
558 | { | ||||||||
559 | long nLastX = maStartPt.X(), nLastY = maStartPt.Y(); | ||||||||
560 | |||||||||
561 | aArr.ImplSetSize( mnCount + 1 ); | ||||||||
562 | aArr[ 0 ] = Point( nLastX, nLastY ); | ||||||||
563 | |||||||||
564 | for( sal_uLong i = 0; i < mnCount; ) | ||||||||
565 | { | ||||||||
566 | const ChainMove& rMove = aImplMove[ mpCodes[ i ] ]; | ||||||||
567 | nLastX += rMove.nDX; | ||||||||
568 | nLastY += rMove.nDY; | ||||||||
569 | aArr[ ++i ] = Point( nLastX, nLastY ); | ||||||||
570 | } | ||||||||
571 | |||||||||
572 | aArr.ImplSetRealSize( mnCount + 1 ); | ||||||||
573 | } | ||||||||
574 | |||||||||
575 | ImplPostProcess( aArr ); | ||||||||
576 | } | ||||||||
577 | else | ||||||||
578 | maPoly.SetSize( 0 ); | ||||||||
579 | } | ||||||||
580 | |||||||||
581 | void ImplChain::ImplPostProcess( const ImplPointArray& rArr ) | ||||||||
582 | { | ||||||||
583 | ImplPointArray aNewArr1; | ||||||||
584 | ImplPointArray aNewArr2; | ||||||||
585 | Point* pLast; | ||||||||
586 | Point* pLeast; | ||||||||
587 | sal_uLong nNewPos; | ||||||||
588 | sal_uLong nCount = rArr.ImplGetRealSize(); | ||||||||
589 | sal_uLong n; | ||||||||
590 | |||||||||
591 | // pass 1 | ||||||||
592 | aNewArr1.ImplSetSize( nCount ); | ||||||||
593 | pLast = &( aNewArr1[ 0 ] ); | ||||||||
594 | pLast->setX( BACK_MAP( rArr[ 0 ].X() ) ); | ||||||||
595 | pLast->setY( BACK_MAP( rArr[ 0 ].Y() ) ); | ||||||||
596 | |||||||||
597 | for( n = nNewPos = 1; n < nCount; ) | ||||||||
598 | { | ||||||||
599 | const Point& rPt = rArr[ n++ ]; | ||||||||
600 | const long nX = BACK_MAP( rPt.X() ); | ||||||||
601 | const long nY = BACK_MAP( rPt.Y() ); | ||||||||
602 | |||||||||
603 | if( nX != pLast->X() || nY != pLast->Y() ) | ||||||||
604 | { | ||||||||
605 | pLast = pLeast = &( aNewArr1[ nNewPos++ ] ); | ||||||||
606 | pLeast->setX( nX ); | ||||||||
607 | pLeast->setY( nY ); | ||||||||
608 | } | ||||||||
609 | } | ||||||||
610 | |||||||||
611 | nCount = nNewPos; | ||||||||
612 | aNewArr1.ImplSetRealSize( nCount ); | ||||||||
613 | |||||||||
614 | // pass 2 | ||||||||
615 | aNewArr2.ImplSetSize( nCount ); | ||||||||
616 | pLast = &( aNewArr2[ 0 ] ); | ||||||||
617 | *pLast = aNewArr1[ 0 ]; | ||||||||
618 | |||||||||
619 | for( n = nNewPos = 1; n < nCount; ) | ||||||||
620 | { | ||||||||
621 | pLeast = &( aNewArr1[ n++ ] ); | ||||||||
622 | |||||||||
623 | if( pLeast->X() == pLast->X() ) | ||||||||
624 | { | ||||||||
625 | while( n < nCount && aNewArr1[ n ].X() == pLast->X() ) | ||||||||
626 | pLeast = &( aNewArr1[ n++ ] ); | ||||||||
627 | } | ||||||||
628 | else if( pLeast->Y() == pLast->Y() ) | ||||||||
629 | { | ||||||||
630 | while( n < nCount && aNewArr1[ n ].Y() == pLast->Y() ) | ||||||||
631 | pLeast = &( aNewArr1[ n++ ] ); | ||||||||
632 | } | ||||||||
633 | |||||||||
634 | pLast = pLeast; | ||||||||
635 | aNewArr2[ nNewPos++ ] = *pLast; | ||||||||
636 | } | ||||||||
637 | |||||||||
638 | aNewArr2.ImplSetRealSize( nNewPos ); | ||||||||
639 | aNewArr2.ImplCreatePoly( maPoly ); | ||||||||
640 | } | ||||||||
641 | |||||||||
642 | namespace ImplVectorizer { | ||||||||
643 | |||||||||
644 | bool ImplVectorize( const Bitmap& rColorBmp, GDIMetaFile& rMtf, | ||||||||
645 | sal_uInt8 cReduce, const Link<long,void>* pProgress ) | ||||||||
646 | { | ||||||||
647 | bool bRet = false; | ||||||||
648 | |||||||||
649 | VECT_PROGRESS( pProgress, 0 ); | ||||||||
650 | |||||||||
651 | std::unique_ptr<Bitmap> xBmp(new Bitmap( rColorBmp )); | ||||||||
652 | Bitmap::ScopedReadAccess pRAcc(*xBmp); | ||||||||
653 | |||||||||
654 | if( pRAcc ) | ||||||||
| |||||||||
655 | { | ||||||||
656 | tools::PolyPolygon aPolyPoly; | ||||||||
657 | double fPercent = 0.0; | ||||||||
658 | double fPercentStep_2 = 0.0; | ||||||||
659 | const long nWidth = pRAcc->Width(); | ||||||||
660 | const long nHeight = pRAcc->Height(); | ||||||||
661 | const sal_uInt16 nColorCount = pRAcc->GetPaletteEntryCount(); | ||||||||
662 | sal_uInt16 n; | ||||||||
663 | std::array<ImplColorSet, 256> aColorSet; | ||||||||
664 | |||||||||
665 | rMtf.Clear(); | ||||||||
666 | |||||||||
667 | // get used palette colors and sort them from light to dark colors | ||||||||
668 | for( n = 0; n < nColorCount; n++ ) | ||||||||
669 | { | ||||||||
670 | aColorSet[ n ].mnIndex = n; | ||||||||
671 | aColorSet[ n ].maColor = pRAcc->GetPaletteColor( n ); | ||||||||
672 | } | ||||||||
673 | |||||||||
674 | for( long nY = 0; nY < nHeight; nY++ ) | ||||||||
675 | { | ||||||||
676 | Scanline pScanlineRead = pRAcc->GetScanline( nY ); | ||||||||
677 | for( long nX = 0; nX < nWidth; nX++ ) | ||||||||
678 | aColorSet[ pRAcc->GetIndexFromData( pScanlineRead, nX ) ].mbSet = true; | ||||||||
679 | } | ||||||||
680 | |||||||||
681 | std::sort( aColorSet.begin(), aColorSet.end(), ImplColorSetCmpFnc ); | ||||||||
682 | |||||||||
683 | for( n = 0; n < 256; n++ ) | ||||||||
684 | if( !aColorSet[ n ].mbSet ) | ||||||||
685 | break; | ||||||||
686 | |||||||||
687 | if( n
| ||||||||
688 | fPercentStep_2 = 45.0 / n; | ||||||||
689 | |||||||||
690 | fPercent += 10.0; | ||||||||
691 | VECT_PROGRESS( pProgress, FRound( fPercent ) ); | ||||||||
692 | |||||||||
693 | for( sal_uInt16 i = 0; i < n; i++ ) | ||||||||
694 | { | ||||||||
695 | const BitmapColor aBmpCol( pRAcc->GetPaletteColor( aColorSet[ i ].mnIndex ) ); | ||||||||
696 | const Color aFindColor( aBmpCol.GetRed(), aBmpCol.GetGreen(), aBmpCol.GetBlue() ); | ||||||||
697 | std::unique_ptr<ImplVectMap> xMap(ImplExpand( pRAcc.get(), aFindColor )); | ||||||||
698 | |||||||||
699 | fPercent += fPercentStep_2; | ||||||||
700 | VECT_PROGRESS( pProgress, FRound( fPercent ) ); | ||||||||
701 | |||||||||
702 | if( xMap ) | ||||||||
703 | { | ||||||||
704 | aPolyPoly.Clear(); | ||||||||
705 | ImplCalculate( xMap.get(), aPolyPoly, cReduce ); | ||||||||
706 | xMap.reset(); | ||||||||
707 | |||||||||
708 | if( aPolyPoly.Count() ) | ||||||||
709 | { | ||||||||
710 | ImplLimitPolyPoly( aPolyPoly ); | ||||||||
711 | |||||||||
712 | aPolyPoly.Optimize( PolyOptimizeFlags::EDGES ); | ||||||||
713 | |||||||||
714 | if( aPolyPoly.Count() ) | ||||||||
715 | { | ||||||||
716 | rMtf.AddAction( new MetaLineColorAction( aFindColor, true ) ); | ||||||||
717 | rMtf.AddAction( new MetaFillColorAction( aFindColor, true ) ); | ||||||||
718 | rMtf.AddAction( new MetaPolyPolygonAction( aPolyPoly ) ); | ||||||||
719 | } | ||||||||
720 | } | ||||||||
721 | } | ||||||||
722 | |||||||||
723 | fPercent += fPercentStep_2; | ||||||||
724 | VECT_PROGRESS( pProgress, FRound( fPercent ) ); | ||||||||
725 | } | ||||||||
726 | |||||||||
727 | if( rMtf.GetActionSize() ) | ||||||||
728 | { | ||||||||
729 | MapMode aMap( MapUnit::Map100thMM ); | ||||||||
730 | ScopedVclPtrInstance< VirtualDevice > aVDev; | ||||||||
731 | const Size aLogSize1( aVDev->PixelToLogic( Size( 1, 1 ), aMap ) ); | ||||||||
732 | |||||||||
733 | rMtf.SetPrefMapMode( aMap ); | ||||||||
734 | rMtf.SetPrefSize( Size( nWidth + 2, nHeight + 2 ) ); | ||||||||
735 | rMtf.Move( 1, 1 ); | ||||||||
736 | rMtf.Scale( aLogSize1.Width(), aLogSize1.Height() ); | ||||||||
737 | bRet = true; | ||||||||
738 | } | ||||||||
739 | } | ||||||||
740 | |||||||||
741 | pRAcc.reset(); | ||||||||
742 | xBmp.reset(); | ||||||||
743 | VECT_PROGRESS( pProgress, 100 ); | ||||||||
744 | |||||||||
745 | return bRet; | ||||||||
746 | } | ||||||||
747 | |||||||||
748 | void ImplLimitPolyPoly( tools::PolyPolygon& rPolyPoly ) | ||||||||
749 | { | ||||||||
750 | if( rPolyPoly.Count() <= VECT_POLY_MAX8192 ) | ||||||||
751 | return; | ||||||||
752 | |||||||||
753 | tools::PolyPolygon aNewPolyPoly; | ||||||||
754 | long nReduce = 0; | ||||||||
755 | sal_uInt16 nNewCount; | ||||||||
756 | |||||||||
757 | do | ||||||||
758 | { | ||||||||
759 | aNewPolyPoly.Clear(); | ||||||||
760 | nReduce++; | ||||||||
761 | |||||||||
762 | for( sal_uInt16 i = 0, nCount = rPolyPoly.Count(); i < nCount; i++ ) | ||||||||
763 | { | ||||||||
764 | const tools::Rectangle aBound( rPolyPoly[ i ].GetBoundRect() ); | ||||||||
765 | |||||||||
766 | if( aBound.GetWidth() > nReduce && aBound.GetHeight() > nReduce ) | ||||||||
767 | { | ||||||||
768 | if( rPolyPoly[ i ].GetSize() ) | ||||||||
769 | aNewPolyPoly.Insert( rPolyPoly[ i ] ); | ||||||||
770 | } | ||||||||
771 | } | ||||||||
772 | |||||||||
773 | nNewCount = aNewPolyPoly.Count(); | ||||||||
774 | } | ||||||||
775 | while( nNewCount > VECT_POLY_MAX8192 ); | ||||||||
776 | |||||||||
777 | rPolyPoly = aNewPolyPoly; | ||||||||
778 | } | ||||||||
779 | |||||||||
780 | ImplVectMap* ImplExpand( BitmapReadAccess* pRAcc, const Color& rColor ) | ||||||||
781 | { | ||||||||
782 | ImplVectMap* pMap = nullptr; | ||||||||
783 | |||||||||
784 | if( pRAcc && pRAcc->Width() && pRAcc->Height() ) | ||||||||
785 | { | ||||||||
786 | const long nOldWidth = pRAcc->Width(); | ||||||||
787 | const long nOldHeight = pRAcc->Height(); | ||||||||
788 | const long nNewWidth = ( nOldWidth << 2 ) + 4; | ||||||||
789 | const long nNewHeight = ( nOldHeight << 2 ) + 4; | ||||||||
790 | const BitmapColor aTest( pRAcc->GetBestMatchingColor( rColor ) ); | ||||||||
791 | std::unique_ptr<long[]> pMapIn(new long[ std::max( nOldWidth, nOldHeight ) ]); | ||||||||
792 | std::unique_ptr<long[]> pMapOut(new long[ std::max( nOldWidth, nOldHeight ) ]); | ||||||||
793 | long nX, nY, nTmpX, nTmpY; | ||||||||
794 | |||||||||
795 | pMap = new ImplVectMap( nNewWidth, nNewHeight ); | ||||||||
796 | |||||||||
797 | for( nX = 0; nX < nOldWidth; nX++ ) | ||||||||
798 | VECT_MAP( pMapIn, pMapOut, nX ); | ||||||||
799 | |||||||||
800 | for( nY = 0, nTmpY = 5; nY < nOldHeight; nY++, nTmpY += 4 ) | ||||||||
801 | { | ||||||||
802 | Scanline pScanlineRead = pRAcc->GetScanline( nY ); | ||||||||
803 | for( nX = 0; nX < nOldWidth; ) | ||||||||
804 | { | ||||||||
805 | if( pRAcc->GetPixelFromData( pScanlineRead, nX ) == aTest ) | ||||||||
806 | { | ||||||||
807 | nTmpX = pMapIn[ nX++ ]; | ||||||||
808 | nTmpY -= 3; | ||||||||
809 | |||||||||
810 | pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
811 | pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
812 | pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
813 | pMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
814 | |||||||||
815 | while( nX < nOldWidth && pRAcc->GetPixelFromData( pScanlineRead, nX ) == aTest ) | ||||||||
816 | nX++; | ||||||||
817 | |||||||||
818 | nTmpX = pMapOut[ nX - 1 ]; | ||||||||
819 | nTmpY -= 3; | ||||||||
820 | |||||||||
821 | pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
822 | pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
823 | pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
824 | pMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
825 | } | ||||||||
826 | else | ||||||||
827 | nX++; | ||||||||
828 | } | ||||||||
829 | } | ||||||||
830 | |||||||||
831 | for( nY = 0; nY < nOldHeight; nY++ ) | ||||||||
832 | VECT_MAP( pMapIn, pMapOut, nY ); | ||||||||
833 | |||||||||
834 | for( nX = 0, nTmpX = 5; nX < nOldWidth; nX++, nTmpX += 4 ) | ||||||||
835 | { | ||||||||
836 | for( nY = 0; nY < nOldHeight; ) | ||||||||
837 | { | ||||||||
838 | if( pRAcc->GetPixel( nY, nX ) == aTest ) | ||||||||
839 | { | ||||||||
840 | nTmpX -= 3; | ||||||||
841 | nTmpY = pMapIn[ nY++ ]; | ||||||||
842 | |||||||||
843 | pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX1 ); | ||||||||
844 | pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX1 ); | ||||||||
845 | pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX1 ); | ||||||||
846 | pMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
847 | |||||||||
848 | while( nY < nOldHeight && pRAcc->GetPixel( nY, nX ) == aTest ) | ||||||||
849 | nY++; | ||||||||
850 | |||||||||
851 | nTmpX -= 3; | ||||||||
852 | nTmpY = pMapOut[ nY - 1 ]; | ||||||||
853 | |||||||||
854 | pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX1 ); | ||||||||
855 | pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX1 ); | ||||||||
856 | pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX1 ); | ||||||||
857 | pMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX1 ); | ||||||||
858 | } | ||||||||
859 | else | ||||||||
860 | nY++; | ||||||||
861 | } | ||||||||
862 | } | ||||||||
863 | } | ||||||||
864 | |||||||||
865 | return pMap; | ||||||||
866 | } | ||||||||
867 | |||||||||
868 | void ImplCalculate( ImplVectMap* pMap, tools::PolyPolygon& rPolyPoly, sal_uInt8 cReduce ) | ||||||||
869 | { | ||||||||
870 | const long nWidth = pMap->Width(), nHeight= pMap->Height(); | ||||||||
871 | |||||||||
872 | for( long nY = 0; nY < nHeight; nY++ ) | ||||||||
873 | { | ||||||||
874 | long nX = 0; | ||||||||
875 | bool bInner = true; | ||||||||
876 | |||||||||
877 | while( nX < nWidth ) | ||||||||
878 | { | ||||||||
879 | // skip free | ||||||||
880 | while( ( nX < nWidth ) && pMap->IsFree( nY, nX ) ) | ||||||||
881 | nX++; | ||||||||
882 | |||||||||
883 | if( nX == nWidth ) | ||||||||
884 | break; | ||||||||
885 | |||||||||
886 | if( pMap->IsCont( nY, nX ) ) | ||||||||
887 | { | ||||||||
888 | // new contour | ||||||||
889 | ImplChain aChain; | ||||||||
890 | const Point aStartPt( nX++, nY ); | ||||||||
891 | |||||||||
892 | // get chain code | ||||||||
893 | aChain.ImplBeginAdd( aStartPt ); | ||||||||
894 | ImplGetChain( pMap, aStartPt, aChain ); | ||||||||
895 | |||||||||
896 | aChain.ImplEndAdd( bInner ? VECT_POLY_OUTLINE_INNER4UL : VECT_POLY_OUTLINE_OUTER8UL ); | ||||||||
897 | |||||||||
898 | const tools::Polygon& rPoly = aChain.ImplGetPoly(); | ||||||||
899 | |||||||||
900 | if( rPoly.GetSize() > 2 ) | ||||||||
901 | { | ||||||||
902 | if( cReduce ) | ||||||||
903 | { | ||||||||
904 | const tools::Rectangle aBound( rPoly.GetBoundRect() ); | ||||||||
905 | |||||||||
906 | if( aBound.GetWidth() > cReduce && aBound.GetHeight() > cReduce ) | ||||||||
907 | rPolyPoly.Insert( rPoly ); | ||||||||
908 | } | ||||||||
909 | else | ||||||||
910 | rPolyPoly.Insert( rPoly ); | ||||||||
911 | } | ||||||||
912 | |||||||||
913 | // skip rest of detected contour | ||||||||
914 | while( pMap->IsCont( nY, nX ) ) | ||||||||
915 | nX++; | ||||||||
916 | } | ||||||||
917 | else | ||||||||
918 | { | ||||||||
919 | // process done segment | ||||||||
920 | const long nStartSegX = nX++; | ||||||||
921 | |||||||||
922 | while( pMap->IsDone( nY, nX ) ) | ||||||||
923 | nX++; | ||||||||
924 | |||||||||
925 | if( ( ( nX - nStartSegX ) == 1 ) || ( ImplIsUp( pMap, nY, nStartSegX ) != ImplIsUp( pMap, nY, nX - 1 ) ) ) | ||||||||
926 | bInner = !bInner; | ||||||||
927 | } | ||||||||
928 | } | ||||||||
929 | } | ||||||||
930 | } | ||||||||
931 | |||||||||
932 | bool ImplGetChain( ImplVectMap* pMap, const Point& rStartPt, ImplChain& rChain ) | ||||||||
933 | { | ||||||||
934 | long nActX = rStartPt.X(); | ||||||||
935 | long nActY = rStartPt.Y(); | ||||||||
936 | sal_uLong nFound; | ||||||||
937 | sal_uLong nLastDir = 0; | ||||||||
938 | sal_uLong nDir; | ||||||||
939 | |||||||||
940 | do | ||||||||
941 | { | ||||||||
942 | nFound = 0; | ||||||||
943 | |||||||||
944 | // first try last direction | ||||||||
945 | long nTryX = nActX + aImplMove[ nLastDir ].nDX; | ||||||||
946 | long nTryY = nActY + aImplMove[ nLastDir ].nDY; | ||||||||
947 | |||||||||
948 | if( pMap->IsCont( nTryY, nTryX ) ) | ||||||||
949 | { | ||||||||
950 | rChain.ImplAdd( static_cast<sal_uInt8>(nLastDir) ); | ||||||||
951 | nActY = nTryY; | ||||||||
952 | nActX = nTryX; | ||||||||
953 | pMap->Set( nActY, nActX, VECT_DONE_INDEX2 ); | ||||||||
954 | nFound = 1; | ||||||||
955 | } | ||||||||
956 | else | ||||||||
957 | { | ||||||||
958 | // try other directions | ||||||||
959 | for( nDir = 0; nDir < 8; nDir++ ) | ||||||||
960 | { | ||||||||
961 | // we already tried nLastDir | ||||||||
962 | if( nDir != nLastDir ) | ||||||||
963 | { | ||||||||
964 | nTryX = nActX + aImplMove[ nDir ].nDX; | ||||||||
965 | nTryY = nActY + aImplMove[ nDir ].nDY; | ||||||||
966 | |||||||||
967 | if( pMap->IsCont( nTryY, nTryX ) ) | ||||||||
968 | { | ||||||||
969 | rChain.ImplAdd( static_cast<sal_uInt8>(nDir) ); | ||||||||
970 | nActY = nTryY; | ||||||||
971 | nActX = nTryX; | ||||||||
972 | pMap->Set( nActY, nActX, VECT_DONE_INDEX2 ); | ||||||||
973 | nFound = 1; | ||||||||
974 | nLastDir = nDir; | ||||||||
975 | break; | ||||||||
976 | } | ||||||||
977 | } | ||||||||
978 | } | ||||||||
979 | } | ||||||||
980 | } | ||||||||
981 | while( nFound ); | ||||||||
982 | |||||||||
983 | return true; | ||||||||
984 | } | ||||||||
985 | |||||||||
986 | bool ImplIsUp( ImplVectMap const * pMap, long nY, long nX ) | ||||||||
987 | { | ||||||||
988 | if( pMap->IsDone( nY - 1, nX ) ) | ||||||||
989 | return true; | ||||||||
990 | else if( pMap->IsDone( nY + 1, nX ) ) | ||||||||
991 | return false; | ||||||||
992 | else if( pMap->IsDone( nY - 1, nX - 1 ) || pMap->IsDone( nY - 1, nX + 1 ) ) | ||||||||
993 | return true; | ||||||||
994 | else | ||||||||
995 | return false; | ||||||||
996 | } | ||||||||
997 | |||||||||
998 | } | ||||||||
999 | |||||||||
1000 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | /* |
3 | * This file is part of the LibreOffice project. |
4 | * |
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/. |
8 | * |
9 | * This file incorporates work covered by the following license notice: |
10 | * |
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 . |
18 | */ |
19 | |
20 | #ifndef INCLUDED_VCL_PTR_HXX |
21 | #define INCLUDED_VCL_PTR_HXX |
22 | |
23 | #include <sal/config.h> |
24 | |
25 | #include <rtl/ref.hxx> |
26 | |
27 | #include <utility> |
28 | #include <type_traits> |
29 | |
30 | #ifdef DBG_UTIL |
31 | #ifndef _WIN32 |
32 | #include <vcl/vclmain.hxx> |
33 | #endif |
34 | #endif |
35 | |
36 | class VclReferenceBase; |
37 | |
38 | namespace vcl::detail { |
39 | |
40 | template<typename> |
41 | constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; } |
42 | |
43 | template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase( |
44 | int (*)[sizeof(T)]) |
45 | { return std::is_base_of<VclReferenceBase, T>::value; } |
46 | |
47 | } // namespace vcl::detail |
48 | |
49 | /** |
50 | * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses. |
51 | * |
52 | * For more details on the design please see vcl/README.lifecycle |
53 | * |
54 | * @param reference_type must be a subclass of vcl::Window |
55 | */ |
56 | template <class reference_type> |
57 | class VclPtr |
58 | { |
59 | static_assert( |
60 | vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>( |
61 | nullptr), |
62 | "template argument type must be derived from VclReferenceBase"); |
63 | |
64 | ::rtl::Reference<reference_type> m_rInnerRef; |
65 | |
66 | public: |
67 | /** Constructor... |
68 | */ |
69 | VclPtr() |
70 | : m_rInnerRef() |
71 | {} |
72 | |
73 | /** Constructor... |
74 | */ |
75 | VclPtr (reference_type * pBody) |
76 | : m_rInnerRef(pBody) |
77 | {} |
78 | |
79 | /** Constructor... that doesn't take a ref. |
80 | */ |
81 | VclPtr (reference_type * pBody, __sal_NoAcquire) |
82 | : m_rInnerRef(pBody, SAL_NO_ACQUIRE) |
83 | {} |
84 | |
85 | /** Up-casting conversion constructor: Copies interface reference. |
86 | |
87 | Does not work for up-casts to ambiguous bases. For the special case of |
88 | up-casting to Reference< XInterface >, see the corresponding conversion |
89 | operator. |
90 | |
91 | @param rRef another reference |
92 | */ |
93 | template< class derived_type > |
94 | VclPtr( |
95 | const VclPtr< derived_type > & rRef, |
96 | typename std::enable_if< |
97 | std::is_base_of<reference_type, derived_type>::value, int>::type |
98 | = 0 ) |
99 | : m_rInnerRef( static_cast<reference_type*>(rRef) ) |
100 | { |
101 | } |
102 | |
103 | #if defined(DBG_UTIL) && !defined(_WIN32) |
104 | virtual ~VclPtr() |
105 | { |
106 | assert(m_rInnerRef.get() == nullptr || vclmain::isAlive())(static_cast <bool> (m_rInnerRef.get() == nullptr || vclmain ::isAlive()) ? void (0) : __assert_fail ("m_rInnerRef.get() == nullptr || vclmain::isAlive()" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 106, __extension__ __PRETTY_FUNCTION__)); |
107 | // We can be one of the intermediate counts, but if we are the last |
108 | // VclPtr keeping this object alive, then something forgot to call dispose(). |
109 | assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1)(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef ->isDisposed() || m_rInnerRef->getRefCount() > 1) && "someone forgot to call dispose()") ? void (0) : __assert_fail ("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\"" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 110, __extension__ __PRETTY_FUNCTION__)) |
110 | && "someone forgot to call dispose()")(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef ->isDisposed() || m_rInnerRef->getRefCount() > 1) && "someone forgot to call dispose()") ? void (0) : __assert_fail ("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\"" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 110, __extension__ __PRETTY_FUNCTION__)); |
111 | } |
112 | VclPtr(VclPtr const &) = default; |
113 | VclPtr(VclPtr &&) = default; |
114 | VclPtr & operator =(VclPtr const &) = default; |
115 | VclPtr & operator =(VclPtr &&) = default; |
116 | #endif |
117 | |
118 | /** |
119 | * A construction helper for VclPtr. Since VclPtr types are created |
120 | * with a reference-count of one - to help fit into the existing |
121 | * code-flow; this helps us to construct them easily. |
122 | * |
123 | * For more details on the design please see vcl/README.lifecycle |
124 | * |
125 | * @tparam reference_type must be a subclass of vcl::Window |
126 | */ |
127 | template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg) |
128 | { |
129 | return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ); |
130 | } |
131 | |
132 | /** Probably most common used: handle->someBodyOp(). |
133 | */ |
134 | reference_type * operator->() const |
135 | { |
136 | return m_rInnerRef.get(); |
137 | } |
138 | |
139 | /** Get the body. Can be used instead of operator->(). |
140 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() |
141 | are the same. |
142 | */ |
143 | reference_type * get() const |
144 | { |
145 | return m_rInnerRef.get(); |
146 | } |
147 | |
148 | void set(reference_type *pBody) |
149 | { |
150 | m_rInnerRef.set(pBody); |
151 | } |
152 | |
153 | void reset(reference_type *pBody) |
154 | { |
155 | m_rInnerRef.set(pBody); |
156 | } |
157 | |
158 | /** Up-casting copy assignment operator. |
159 | |
160 | Does not work for up-casts to ambiguous bases. |
161 | |
162 | @param rRef another reference |
163 | */ |
164 | template<typename derived_type> |
165 | typename std::enable_if< |
166 | std::is_base_of<reference_type, derived_type>::value, |
167 | VclPtr &>::type |
168 | operator =(VclPtr<derived_type> const & rRef) |
169 | { |
170 | m_rInnerRef.set(rRef.get()); |
171 | return *this; |
172 | } |
173 | |
174 | VclPtr & operator =(reference_type * pBody) |
175 | { |
176 | m_rInnerRef.set(pBody); |
177 | return *this; |
178 | } |
179 | |
180 | operator reference_type * () const |
181 | { |
182 | return m_rInnerRef.get(); |
183 | } |
184 | |
185 | explicit operator bool () const |
186 | { |
187 | return m_rInnerRef.get() != nullptr; |
188 | } |
189 | |
190 | void clear() |
191 | { |
192 | m_rInnerRef.clear(); |
193 | } |
194 | |
195 | void reset() |
196 | { |
197 | m_rInnerRef.clear(); |
198 | } |
199 | |
200 | void disposeAndClear() |
201 | { |
202 | // hold it alive for the lifetime of this method |
203 | ::rtl::Reference<reference_type> aTmp(m_rInnerRef); |
204 | m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-) |
205 | if (aTmp.get()) { |
206 | aTmp->disposeOnce(); |
207 | } |
208 | } |
209 | |
210 | /** Needed to place VclPtr's into STL collection. |
211 | */ |
212 | bool operator< (const VclPtr<reference_type> & handle) const |
213 | { |
214 | return (m_rInnerRef < handle.m_rInnerRef); |
215 | } |
216 | }; // class VclPtr |
217 | |
218 | template<typename T1, typename T2> |
219 | inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
220 | return p1.get() == p2.get(); |
221 | } |
222 | |
223 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2) |
224 | { |
225 | return p1.get() == p2; |
226 | } |
227 | |
228 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) { |
229 | return p1.get() == p2; |
230 | } |
231 | |
232 | template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2) |
233 | { |
234 | return p1 == p2.get(); |
235 | } |
236 | |
237 | template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) { |
238 | return p1 == p2.get(); |
239 | } |
240 | |
241 | template<typename T1, typename T2> |
242 | inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
243 | return !(p1 == p2); |
244 | } |
245 | |
246 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2) |
247 | { |
248 | return !(p1 == p2); |
249 | } |
250 | |
251 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) { |
252 | return !(p1 == p2); |
253 | } |
254 | |
255 | template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2) |
256 | { |
257 | return !(p1 == p2); |
258 | } |
259 | |
260 | template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) { |
261 | return !(p1 == p2); |
262 | } |
263 | |
264 | /** |
265 | * A construction helper for a temporary VclPtr. Since VclPtr types |
266 | * are created with a reference-count of one - to help fit into |
267 | * the existing code-flow; this helps us to construct them easily. |
268 | * see also VclPtr::Create and ScopedVclPtr |
269 | * |
270 | * For more details on the design please see vcl/README.lifecycle |
271 | * |
272 | * @param reference_type must be a subclass of vcl::Window |
273 | */ |
274 | template <class reference_type> |
275 | class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type> |
276 | { |
277 | public: |
278 | template<typename... Arg> VclPtrInstance(Arg &&... arg) |
279 | : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
280 | { |
281 | } |
282 | |
283 | /** |
284 | * Override and disallow this, to prevent people accidentally calling it and actually |
285 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
286 | */ |
287 | template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete; |
288 | }; |
289 | |
290 | template <class reference_type> |
291 | class ScopedVclPtr : public VclPtr<reference_type> |
292 | { |
293 | public: |
294 | /** Constructor... |
295 | */ |
296 | ScopedVclPtr() |
297 | : VclPtr<reference_type>() |
298 | {} |
299 | |
300 | /** Constructor |
301 | */ |
302 | ScopedVclPtr (reference_type * pBody) |
303 | : VclPtr<reference_type>(pBody) |
304 | {} |
305 | |
306 | /** Copy constructor... |
307 | */ |
308 | ScopedVclPtr (const VclPtr<reference_type> & handle) |
309 | : VclPtr<reference_type>(handle) |
310 | {} |
311 | |
312 | /** |
313 | Assignment that releases the last reference. |
314 | */ |
315 | void disposeAndReset(reference_type *pBody) |
316 | { |
317 | if (pBody != this->get()) { |
318 | VclPtr<reference_type>::disposeAndClear(); |
319 | VclPtr<reference_type>::set(pBody); |
320 | } |
321 | } |
322 | |
323 | /** |
324 | Assignment that releases the last reference. |
325 | */ |
326 | ScopedVclPtr<reference_type>& operator = (reference_type * pBody) |
327 | { |
328 | disposeAndReset(pBody); |
329 | return *this; |
330 | } |
331 | |
332 | /** Up-casting conversion constructor: Copies interface reference. |
333 | |
334 | Does not work for up-casts to ambiguous bases. For the special case of |
335 | up-casting to Reference< XInterface >, see the corresponding conversion |
336 | operator. |
337 | |
338 | @param rRef another reference |
339 | */ |
340 | template< class derived_type > |
341 | ScopedVclPtr( |
342 | const VclPtr< derived_type > & rRef, |
343 | typename std::enable_if< |
344 | std::is_base_of<reference_type, derived_type>::value, int>::type |
345 | = 0 ) |
346 | : VclPtr<reference_type>( rRef ) |
347 | { |
348 | } |
349 | |
350 | /** Up-casting assignment operator. |
351 | |
352 | Does not work for up-casts to ambiguous bases. |
353 | |
354 | @param rRef another VclPtr |
355 | */ |
356 | template<typename derived_type> |
357 | typename std::enable_if< |
358 | std::is_base_of<reference_type, derived_type>::value, |
359 | ScopedVclPtr &>::type |
360 | operator =(VclPtr<derived_type> const & rRef) |
361 | { |
362 | disposeAndReset(rRef.get()); |
363 | return *this; |
364 | } |
365 | |
366 | /** |
367 | * Override and disallow this, to prevent people accidentally calling it and actually |
368 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
369 | */ |
370 | template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete; |
371 | |
372 | ~ScopedVclPtr() |
373 | { |
374 | VclPtr<reference_type>::disposeAndClear(); |
375 | assert(VclPtr<reference_type>::get() == nullptr)(static_cast <bool> (VclPtr<reference_type>::get( ) == nullptr) ? void (0) : __assert_fail ("VclPtr<reference_type>::get() == nullptr" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 375, __extension__ __PRETTY_FUNCTION__)); // make sure there are no lingering references |
376 | } |
377 | |
378 | private: |
379 | // Most likely we don't want this default copy-constructor. |
380 | ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete; |
381 | // And certainly we don't want a default assignment operator. |
382 | ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete; |
383 | // And disallow reset as that doesn't call disposeAndClear on the original reference |
384 | void reset() = delete; |
385 | void reset(reference_type *pBody) = delete; |
386 | |
387 | protected: |
388 | ScopedVclPtr (reference_type * pBody, __sal_NoAcquire) |
389 | : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE) |
390 | {} |
391 | }; |
392 | |
393 | /** |
394 | * A construction helper for ScopedVclPtr. Since VclPtr types are created |
395 | * with a reference-count of one - to help fit into the existing |
396 | * code-flow; this helps us to construct them easily. |
397 | * |
398 | * For more details on the design please see vcl/README.lifecycle |
399 | * |
400 | * @param reference_type must be a subclass of vcl::Window |
401 | */ |
402 | #if defined _MSC_VER |
403 | #pragma warning(push) |
404 | #pragma warning(disable: 4521) // " multiple copy constructors specified" |
405 | #endif |
406 | template <class reference_type> |
407 | class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type> |
408 | { |
409 | public: |
410 | template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg) |
411 | : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
412 | { |
413 | } |
414 | |
415 | /** |
416 | * Override and disallow this, to prevent people accidentally calling it and actually |
417 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
418 | */ |
419 | template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete; |
420 | |
421 | private: |
422 | // Prevent the above perfect forwarding ctor from hijacking (accidental) |
423 | // attempts at ScopedVclPtrInstance copy construction (where the hijacking |
424 | // would typically lead to somewhat obscure error messages); both non-const |
425 | // and const variants are needed here, as the ScopedVclPtr base class has a |
426 | // const--variant copy ctor, so the implicitly declared copy ctor for |
427 | // ScopedVclPtrInstance would also be the const variant, so non-const copy |
428 | // construction attempts would be hijacked by the perfect forwarding ctor; |
429 | // but if we only declared a non-const variant here, the const variant would |
430 | // no longer be implicitly declared (as there would already be an explicitly |
431 | // declared copy ctor), so const copy construction attempts would then be |
432 | // hijacked by the perfect forwarding ctor: |
433 | ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete; |
434 | ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete; |
435 | }; |
436 | #if defined _MSC_VER |
437 | #pragma warning(pop) |
438 | #endif |
439 | |
440 | #endif // INCLUDED_VCL_PTR_HXX |
441 | |
442 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | ||||||||
2 | /* | ||||||||
3 | * This file is part of the LibreOffice project. | ||||||||
4 | * | ||||||||
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/. | ||||||||
8 | * | ||||||||
9 | * This file incorporates work covered by the following license notice: | ||||||||
10 | * | ||||||||
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 . | ||||||||
18 | */ | ||||||||
19 | |||||||||
20 | #ifndef INCLUDED_RTL_REF_HXX | ||||||||
21 | #define INCLUDED_RTL_REF_HXX | ||||||||
22 | |||||||||
23 | #include "sal/config.h" | ||||||||
24 | |||||||||
25 | #include <cassert> | ||||||||
26 | #include <cstddef> | ||||||||
27 | #include <functional> | ||||||||
28 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
29 | #include <type_traits> | ||||||||
30 | #endif | ||||||||
31 | |||||||||
32 | #include "sal/types.h" | ||||||||
33 | |||||||||
34 | namespace rtl | ||||||||
35 | { | ||||||||
36 | |||||||||
37 | /** Template reference class for reference type. | ||||||||
38 | */ | ||||||||
39 | template <class reference_type> | ||||||||
40 | class Reference | ||||||||
41 | { | ||||||||
42 | /** The <b>reference_type</b> body pointer. | ||||||||
43 | */ | ||||||||
44 | reference_type * m_pBody; | ||||||||
45 | |||||||||
46 | |||||||||
47 | public: | ||||||||
48 | /** Constructor... | ||||||||
49 | */ | ||||||||
50 | Reference() | ||||||||
51 | : m_pBody (NULL__null) | ||||||||
52 | {} | ||||||||
53 | |||||||||
54 | |||||||||
55 | /** Constructor... | ||||||||
56 | */ | ||||||||
57 | Reference (reference_type * pBody, __sal_NoAcquire) | ||||||||
58 | : m_pBody (pBody) | ||||||||
59 | { | ||||||||
60 | } | ||||||||
61 | |||||||||
62 | /** Constructor... | ||||||||
63 | */ | ||||||||
64 | Reference (reference_type * pBody) | ||||||||
65 | : m_pBody (pBody) | ||||||||
66 | { | ||||||||
67 | if (m_pBody) | ||||||||
68 | m_pBody->acquire(); | ||||||||
69 | } | ||||||||
70 | |||||||||
71 | /** Copy constructor... | ||||||||
72 | */ | ||||||||
73 | Reference (const Reference<reference_type> & handle) | ||||||||
74 | : m_pBody (handle.m_pBody) | ||||||||
75 | { | ||||||||
76 | if (m_pBody) | ||||||||
77 | m_pBody->acquire(); | ||||||||
78 | } | ||||||||
79 | |||||||||
80 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
81 | /** Move constructor... | ||||||||
82 | */ | ||||||||
83 | Reference (Reference<reference_type> && handle) noexcept | ||||||||
84 | : m_pBody (handle.m_pBody) | ||||||||
85 | { | ||||||||
86 | handle.m_pBody = nullptr; | ||||||||
87 | } | ||||||||
88 | #endif | ||||||||
89 | |||||||||
90 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
91 | /** Up-casting conversion constructor: Copies interface reference. | ||||||||
92 | |||||||||
93 | Does not work for up-casts to ambiguous bases. | ||||||||
94 | |||||||||
95 | @param rRef another reference | ||||||||
96 | */ | ||||||||
97 | template< class derived_type > | ||||||||
98 | inline Reference( | ||||||||
99 | const Reference< derived_type > & rRef, | ||||||||
100 | std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 ) | ||||||||
101 | : m_pBody (rRef.get()) | ||||||||
102 | { | ||||||||
103 | if (m_pBody) | ||||||||
104 | m_pBody->acquire(); | ||||||||
105 | } | ||||||||
106 | #endif | ||||||||
107 | |||||||||
108 | /** Destructor... | ||||||||
109 | */ | ||||||||
110 | ~Reference() COVERITY_NOEXCEPT_FALSE | ||||||||
111 | { | ||||||||
112 | if (m_pBody) | ||||||||
113 | m_pBody->release(); | ||||||||
114 | } | ||||||||
115 | |||||||||
116 | /** Set... | ||||||||
117 | Similar to assignment. | ||||||||
118 | */ | ||||||||
119 | Reference<reference_type> & | ||||||||
120 | SAL_CALL set (reference_type * pBody) | ||||||||
121 | { | ||||||||
122 | if (pBody) | ||||||||
123 | pBody->acquire(); | ||||||||
124 | reference_type * const pOld = m_pBody; | ||||||||
125 | m_pBody = pBody; | ||||||||
126 | if (pOld) | ||||||||
127 | pOld->release(); | ||||||||
128 | return *this; | ||||||||
129 | } | ||||||||
130 | |||||||||
131 | /** Assignment. | ||||||||
132 | Unbinds this instance from its body (if bound) and | ||||||||
133 | bind it to the body represented by the handle. | ||||||||
134 | */ | ||||||||
135 | Reference<reference_type> & | ||||||||
136 | SAL_CALL operator= (const Reference<reference_type> & handle) | ||||||||
137 | { | ||||||||
138 | return set( handle.m_pBody ); | ||||||||
139 | } | ||||||||
140 | |||||||||
141 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
142 | /** Assignment. | ||||||||
143 | * Unbinds this instance from its body (if bound), | ||||||||
144 | * bind it to the body represented by the handle, and | ||||||||
145 | * set the body represented by the handle to nullptr. | ||||||||
146 | */ | ||||||||
147 | Reference<reference_type> & | ||||||||
148 | operator= (Reference<reference_type> && handle) | ||||||||
149 | { | ||||||||
150 | // self-movement guts ourself | ||||||||
151 | if (m_pBody) | ||||||||
152 | m_pBody->release(); | ||||||||
153 | m_pBody = handle.m_pBody; | ||||||||
154 | handle.m_pBody = nullptr; | ||||||||
155 | return *this; | ||||||||
156 | } | ||||||||
157 | #endif | ||||||||
158 | |||||||||
159 | /** Assignment... | ||||||||
160 | */ | ||||||||
161 | Reference<reference_type> & | ||||||||
162 | SAL_CALL operator= (reference_type * pBody) | ||||||||
163 | { | ||||||||
164 | return set( pBody ); | ||||||||
165 | } | ||||||||
166 | |||||||||
167 | /** Unbind the body from this handle. | ||||||||
168 | Note that for a handle representing a large body, | ||||||||
169 | "handle.clear().set(new body());" _might_ | ||||||||
170 | perform a little bit better than "handle.set(new body());", | ||||||||
171 | since in the second case two large objects exist in memory | ||||||||
172 | (the old body and the new body). | ||||||||
173 | */ | ||||||||
174 | Reference<reference_type> & SAL_CALL clear() | ||||||||
175 | { | ||||||||
176 | if (m_pBody
| ||||||||
177 | { | ||||||||
178 | reference_type * const pOld = m_pBody; | ||||||||
179 | m_pBody = NULL__null; | ||||||||
180 | pOld->release(); | ||||||||
181 | } | ||||||||
182 | return *this; | ||||||||
183 | } | ||||||||
184 | |||||||||
185 | |||||||||
186 | /** Get the body. Can be used instead of operator->(). | ||||||||
187 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() | ||||||||
188 | are the same. | ||||||||
189 | */ | ||||||||
190 | reference_type * SAL_CALL get() const | ||||||||
191 | { | ||||||||
192 | return m_pBody; | ||||||||
| |||||||||
193 | } | ||||||||
194 | |||||||||
195 | |||||||||
196 | /** Probably most common used: handle->someBodyOp(). | ||||||||
197 | */ | ||||||||
198 | reference_type * SAL_CALL operator->() const | ||||||||
199 | { | ||||||||
200 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 200, __extension__ __PRETTY_FUNCTION__)); | ||||||||
201 | return m_pBody; | ||||||||
202 | } | ||||||||
203 | |||||||||
204 | |||||||||
205 | /** Allows (*handle).someBodyOp(). | ||||||||
206 | */ | ||||||||
207 | reference_type & SAL_CALL operator*() const | ||||||||
208 | { | ||||||||
209 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 209, __extension__ __PRETTY_FUNCTION__)); | ||||||||
210 | return *m_pBody; | ||||||||
211 | } | ||||||||
212 | |||||||||
213 | |||||||||
214 | /** Returns True if the handle does point to a valid body. | ||||||||
215 | */ | ||||||||
216 | bool SAL_CALL is() const | ||||||||
217 | { | ||||||||
218 | return (m_pBody != NULL__null); | ||||||||
219 | } | ||||||||
220 | |||||||||
221 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
222 | /** Returns True if the handle does point to a valid body. | ||||||||
223 | */ | ||||||||
224 | explicit operator bool() const | ||||||||
225 | { | ||||||||
226 | return is(); | ||||||||
227 | } | ||||||||
228 | #endif | ||||||||
229 | |||||||||
230 | /** Returns True if this points to pBody. | ||||||||
231 | */ | ||||||||
232 | bool SAL_CALL operator== (const reference_type * pBody) const | ||||||||
233 | { | ||||||||
234 | return (m_pBody == pBody); | ||||||||
235 | } | ||||||||
236 | |||||||||
237 | |||||||||
238 | /** Returns True if handle points to the same body. | ||||||||
239 | */ | ||||||||
240 | bool | ||||||||
241 | SAL_CALL operator== (const Reference<reference_type> & handle) const | ||||||||
242 | { | ||||||||
243 | return (m_pBody == handle.m_pBody); | ||||||||
244 | } | ||||||||
245 | |||||||||
246 | |||||||||
247 | /** Needed to place References into STL collection. | ||||||||
248 | */ | ||||||||
249 | bool | ||||||||
250 | SAL_CALL operator!= (const Reference<reference_type> & handle) const | ||||||||
251 | { | ||||||||
252 | return (m_pBody != handle.m_pBody); | ||||||||
253 | } | ||||||||
254 | |||||||||
255 | |||||||||
256 | /** Needed to place References into STL collection. | ||||||||
257 | */ | ||||||||
258 | bool | ||||||||
259 | SAL_CALL operator< (const Reference<reference_type> & handle) const | ||||||||
260 | { | ||||||||
261 | return (m_pBody < handle.m_pBody); | ||||||||
262 | } | ||||||||
263 | |||||||||
264 | |||||||||
265 | /** Needed to place References into STL collection. | ||||||||
266 | */ | ||||||||
267 | bool | ||||||||
268 | SAL_CALL operator> (const Reference<reference_type> & handle) const | ||||||||
269 | { | ||||||||
270 | return (m_pBody > handle.m_pBody); | ||||||||
271 | } | ||||||||
272 | }; | ||||||||
273 | |||||||||
274 | } // namespace rtl | ||||||||
275 | |||||||||
276 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
277 | namespace std | ||||||||
278 | { | ||||||||
279 | |||||||||
280 | /// @cond INTERNAL | ||||||||
281 | /** | ||||||||
282 | Make rtl::Reference hashable by default for use in STL containers. | ||||||||
283 | |||||||||
284 | @since LibreOffice 6.3 | ||||||||
285 | */ | ||||||||
286 | template<typename T> | ||||||||
287 | struct hash<::rtl::Reference<T>> | ||||||||
288 | { | ||||||||
289 | std::size_t operator()(::rtl::Reference<T> const & s) const | ||||||||
290 | { return std::size_t(s.get()); } | ||||||||
291 | }; | ||||||||
292 | /// @endcond | ||||||||
293 | |||||||||
294 | } | ||||||||
295 | |||||||||
296 | #endif | ||||||||
297 | |||||||||
298 | #endif /* ! INCLUDED_RTL_REF_HXX */ | ||||||||
299 | |||||||||
300 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | /* |
3 | * This file is part of the LibreOffice project. |
4 | * |
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/. |
8 | * |
9 | * This file incorporates work covered by the following license notice: |
10 | * |
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 . |
18 | */ |
19 | #ifndef INCLUDED_VCL_Reference_HXX |
20 | #define INCLUDED_VCL_Reference_HXX |
21 | |
22 | #include <vcl/dllapi.h> |
23 | #include <osl/interlck.h> |
24 | |
25 | class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase |
26 | { |
27 | mutable oslInterlockedCount mnRefCnt; |
28 | |
29 | template<typename T> friend class VclPtr; |
30 | |
31 | public: |
32 | void acquire() const |
33 | { |
34 | osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1); |
35 | } |
36 | |
37 | void release() const |
38 | { |
39 | if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0) |
40 | delete this; |
41 | } |
42 | #ifdef DBG_UTIL |
43 | #ifndef _WIN32 |
44 | sal_Int32 getRefCount() const { return mnRefCnt; } |
45 | #endif |
46 | #endif |
47 | |
48 | |
49 | private: |
50 | VclReferenceBase(const VclReferenceBase&) = delete; |
51 | VclReferenceBase& operator=(const VclReferenceBase&) = delete; |
52 | |
53 | bool mbDisposed : 1; |
54 | |
55 | protected: |
56 | VclReferenceBase(); |
57 | protected: |
58 | virtual ~VclReferenceBase(); |
59 | |
60 | protected: |
61 | virtual void dispose(); |
62 | |
63 | public: |
64 | void disposeOnce(); |
65 | bool isDisposed() const { return mbDisposed; } |
66 | |
67 | }; |
68 | #endif |