Bug Summary

File:home/maarten/src/libreoffice/core/include/rtl/ref.hxx
Warning:line 192, column 9
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name svdpdf.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -isystem /usr/include/libxml2 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D SVXCORE_DLLIMPLEMENTATION -D SYSTEM_LIBXML -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/epoxy/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/pdfium -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/pdfium/public -D COMPONENT_BUILD -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/i18n -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/common -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/svx/inc -I /home/maarten/src/libreoffice/core/svx/source/inc -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -I /home/maarten/src/libreoffice/core/workdir/SdiTarget/svx/sdi -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx

/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx

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 <svdpdf.hxx>
21
22#include <config_features.h>
23
24#if HAVE_FEATURE_PDFIUM1
25#include <fpdfview.h>
26#include <fpdf_edit.h>
27#include <fpdf_text.h>
28
29#include <tools/UnitConversion.hxx>
30#include <vcl/graph.hxx>
31#include <vcl/vectorgraphicdata.hxx>
32
33#include <math.h>
34#include <editeng/eeitem.hxx>
35#include <editeng/fhgtitem.hxx>
36#include <editeng/wghtitem.hxx>
37#include <editeng/postitem.hxx>
38#include <editeng/udlnitem.hxx>
39#include <editeng/crossedoutitem.hxx>
40#include <editeng/shdditem.hxx>
41#include <svx/xlnclit.hxx>
42#include <svx/xlncapit.hxx>
43#include <svx/xlnwtit.hxx>
44#include <svx/xflclit.hxx>
45#include <editeng/fontitem.hxx>
46#include <editeng/wrlmitem.hxx>
47#include <editeng/contouritem.hxx>
48#include <editeng/colritem.hxx>
49#include <vcl/metric.hxx>
50#include <editeng/charscaleitem.hxx>
51#include <svx/sdtditm.hxx>
52#include <svx/sdtagitm.hxx>
53#include <svx/sdtfsitm.hxx>
54#include <svx/svdmodel.hxx>
55#include <svx/svdpage.hxx>
56#include <svx/svdobj.hxx>
57#include <svx/svdotext.hxx>
58#include <svx/svdorect.hxx>
59#include <svx/svdograf.hxx>
60#include <svx/svdopath.hxx>
61#include <svx/svdetc.hxx>
62#include <svl/itemset.hxx>
63#include <basegfx/polygon/b2dpolygon.hxx>
64#include <tools/helpers.hxx>
65#include <basegfx/matrix/b2dhommatrix.hxx>
66#include <basegfx/matrix/b2dhommatrixtools.hxx>
67#include <svx/xlinjoit.hxx>
68#include <svx/xlndsit.hxx>
69#include <basegfx/polygon/b2dpolygonclipper.hxx>
70#include <svx/xbtmpit.hxx>
71#include <svx/xfillit0.hxx>
72#include <svx/xflbmtit.hxx>
73#include <svx/xflbstit.hxx>
74#include <svx/xlineit0.hxx>
75#include <basegfx/polygon/b2dpolypolygontools.hxx>
76#include <svx/svditer.hxx>
77#include <svx/svdogrp.hxx>
78#include <vcl/dibtools.hxx>
79#include <sal/log.hxx>
80
81namespace
82{
83double sqrt2(double a, double b) { return sqrt(a * a + b * b); }
84
85struct FPDFBitmapDeleter
86{
87 void operator()(FPDF_BITMAP bitmap) { FPDFBitmap_Destroy(bitmap); }
88};
89}
90
91using namespace com::sun::star;
92
93ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, SdrLayerID nLay, const tools::Rectangle& rRect,
94 Graphic const& rGraphic)
95 : maTmpList()
96 , mpVD(VclPtr<VirtualDevice>::Create())
97 , maScaleRect(rRect)
98 , mnMapScalingOfs(0)
99 , mpModel(&rModel)
100 , mnLayer(nLay)
101 , maOldLineColor()
102 , mnLineWidth(0)
103 , maDash(css::drawing::DashStyle_RECT, 0, 0, 0, 0, 0)
104 , mbMov(false)
105 , mbSize(false)
106 , maOfs(0, 0)
107 , mfScaleX(1.0)
108 , mfScaleY(1.0)
109 , maScaleX(1.0)
110 , maScaleY(1.0)
111 , mbFntDirty(true)
112 , mbLastObjWasPolyWithoutLine(false)
113 , mbNoLine(false)
114 , mbNoFill(false)
115 , maClip()
116 , mnPageCount(0)
117 , mdPageHeightPts(0)
118 , mpPDFium(vcl::pdf::PDFiumLibrary::get())
119{
120 mpVD->EnableOutput(false);
121 mpVD->SetLineColor();
122 mpVD->SetFillColor();
123 maOldLineColor.SetRed(mpVD->GetLineColor().GetRed() + 1);
124 mpLineAttr = std::make_unique<SfxItemSet>(rModel.GetItemPool(),
125 svl::Items<XATTR_LINE_FIRST, XATTR_LINE_LAST>{});
126 mpFillAttr = std::make_unique<SfxItemSet>(rModel.GetItemPool(),
127 svl::Items<XATTR_FILL_FIRST, XATTR_FILL_LAST>{});
128 mpTextAttr = std::make_unique<SfxItemSet>(rModel.GetItemPool(),
129 svl::Items<EE_ITEMS_START, EE_ITEMS_END>{});
130 checkClip();
131
132 // Load the buffer using pdfium.
133 auto const& rVectorGraphicData = rGraphic.getVectorGraphicData();
134 auto* pData = rVectorGraphicData->getVectorGraphicDataArray().getConstArray();
135 sal_Int32 nSize = rVectorGraphicData->getVectorGraphicDataArrayLength();
136 mpPdfDocument = mpPDFium->openDocument(pData, nSize);
137 if (!mpPdfDocument)
138 return;
139
140 mnPageCount = mpPdfDocument->getPageCount();
141}
142
143ImpSdrPdfImport::~ImpSdrPdfImport() = default;
1
Calling '~ScopedVclPtr'
144
145void ImpSdrPdfImport::DoObjects(SvdProgressInfo* pProgrInfo, sal_uInt32* pActionsToReport,
146 int nPageIndex)
147{
148 const int nPageCount = mpPdfDocument->getPageCount();
149 if (!(nPageCount > 0 && nPageIndex >= 0 && nPageIndex < nPageCount))
150 return;
151
152 // Render next page.
153 auto pPdfPage = mpPdfDocument->openPage(nPageIndex);
154 if (!pPdfPage)
155 return;
156
157 basegfx::B2DSize dPageSize = mpPdfDocument->getPageSize(nPageIndex);
158
159 const double dPageWidth = dPageSize.getX();
160 const double dPageHeight = dPageSize.getY();
161
162 SetupPageScale(dPageWidth, dPageHeight);
163
164 // Load the page text to extract it when we get text elements.
165 auto pTextPage = pPdfPage->getTextPage();
166
167 const int nPageObjectCount = pPdfPage->getObjectCount();
168 if (pProgrInfo)
169 pProgrInfo->SetActionCount(nPageObjectCount);
170
171 for (int nPageObjectIndex = 0; nPageObjectIndex < nPageObjectCount; ++nPageObjectIndex)
172 {
173 auto pPageObject = pPdfPage->getObject(nPageObjectIndex);
174 ImportPdfObject(pPageObject, pTextPage, nPageObjectIndex);
175 if (pProgrInfo && pActionsToReport)
176 {
177 (*pActionsToReport)++;
178
179 if (*pActionsToReport >= 16)
180 {
181 if (!pProgrInfo->ReportActions(*pActionsToReport))
182 break;
183
184 *pActionsToReport = 0;
185 }
186 }
187 }
188}
189
190void ImpSdrPdfImport::SetupPageScale(const double dPageWidth, const double dPageHeight)
191{
192 mfScaleX = mfScaleY = 1.0;
193
194 // Store the page dimensions in Points.
195 mdPageHeightPts = dPageHeight;
196
197 Size aPageSize(convertPointToMm100(dPageWidth), convertPointToMm100(dPageHeight));
198
199 if (aPageSize.Width() && aPageSize.Height() && (!maScaleRect.IsEmpty()))
200 {
201 maOfs = maScaleRect.TopLeft();
202
203 if (aPageSize.Width() != (maScaleRect.GetWidth() - 1))
204 {
205 mfScaleX = static_cast<double>(maScaleRect.GetWidth() - 1)
206 / static_cast<double>(aPageSize.Width());
207 }
208
209 if (aPageSize.Height() != (maScaleRect.GetHeight() - 1))
210 {
211 mfScaleY = static_cast<double>(maScaleRect.GetHeight() - 1)
212 / static_cast<double>(aPageSize.Height());
213 }
214 }
215
216 mbMov = maOfs.X() != 0 || maOfs.Y() != 0;
217 mbSize = false;
218 maScaleX = Fraction(1, 1);
219 maScaleY = Fraction(1, 1);
220
221 if (aPageSize.Width() != (maScaleRect.GetWidth() - 1))
222 {
223 maScaleX = Fraction(maScaleRect.GetWidth() - 1, aPageSize.Width());
224 mbSize = true;
225 }
226
227 if (aPageSize.Height() != (maScaleRect.GetHeight() - 1))
228 {
229 maScaleY = Fraction(maScaleRect.GetHeight() - 1, aPageSize.Height());
230 mbSize = true;
231 }
232}
233
234size_t ImpSdrPdfImport::DoImport(SdrObjList& rOL, size_t nInsPos, int nPageNumber,
235 SvdProgressInfo* pProgrInfo)
236{
237 sal_uInt32 nActionsToReport(0);
238
239 // execute
240 DoObjects(pProgrInfo, &nActionsToReport, nPageNumber);
241
242 if (pProgrInfo)
243 {
244 pProgrInfo->ReportActions(nActionsToReport);
245 nActionsToReport = 0;
246 }
247
248 // MapMode scaling
249 MapScaling();
250
251 // To calculate the progress meter, we use GetActionSize()*3.
252 // However, maTmpList has a lower entry count limit than GetActionSize(),
253 // so the actions that were assumed were too much have to be re-added.
254 // nActionsToReport = (rMtf.GetActionSize() - maTmpList.size()) * 2;
255
256 // announce all currently unannounced rescales
257 if (pProgrInfo)
258 {
259 pProgrInfo->ReportRescales(nActionsToReport);
260 pProgrInfo->SetInsertCount(maTmpList.size());
261 }
262
263 nActionsToReport = 0;
264
265 // insert all objects cached in aTmpList now into rOL from nInsPos
266 nInsPos = std::min(nInsPos, rOL.GetObjCount());
267
268 for (SdrObject* pObj : maTmpList)
269 {
270 rOL.NbcInsertObject(pObj, nInsPos);
271 nInsPos++;
272
273 if (pProgrInfo)
274 {
275 nActionsToReport++;
276
277 if (nActionsToReport >= 32) // update all 32 actions
278 {
279 pProgrInfo->ReportInserts(nActionsToReport);
280 nActionsToReport = 0;
281 }
282 }
283 }
284
285 // report all remaining inserts for the last time
286 if (pProgrInfo)
287 {
288 pProgrInfo->ReportInserts(nActionsToReport);
289 }
290
291 return maTmpList.size();
292}
293
294void ImpSdrPdfImport::SetAttributes(SdrObject* pObj, bool bForceTextAttr)
295{
296 mbNoLine = false;
297 mbNoFill = false;
298 bool bLine(!bForceTextAttr);
299 bool bFill(!pObj || (pObj->IsClosedObj() && !bForceTextAttr));
300 bool bText(bForceTextAttr || (pObj && pObj->GetOutlinerParaObject()));
301
302 if (bLine)
303 {
304 if (mnLineWidth)
305 {
306 mpLineAttr->Put(XLineWidthItem(mnLineWidth));
307 }
308 else
309 {
310 mpLineAttr->Put(XLineWidthItem(0));
311 }
312
313 maOldLineColor = mpVD->GetLineColor();
314
315 if (mpVD->IsLineColor())
316 {
317 mpLineAttr->Put(XLineStyleItem(drawing::LineStyle_SOLID)); //TODO support dashed lines.
318 mpLineAttr->Put(XLineColorItem(OUString(), mpVD->GetLineColor()));
319 }
320 else
321 {
322 mpLineAttr->Put(XLineStyleItem(drawing::LineStyle_NONE));
323 }
324
325 mpLineAttr->Put(XLineJointItem(css::drawing::LineJoint_NONE));
326
327 // Add LineCap support
328 mpLineAttr->Put(XLineCapItem(gaLineCap));
329
330 if (((maDash.GetDots() && maDash.GetDotLen())
331 || (maDash.GetDashes() && maDash.GetDashLen()))
332 && maDash.GetDistance())
333 {
334 mpLineAttr->Put(XLineDashItem(OUString(), maDash));
335 }
336 else
337 {
338 mpLineAttr->Put(XLineDashItem(OUString(), XDash(css::drawing::DashStyle_RECT)));
339 }
340 }
341 else
342 {
343 mbNoLine = true;
344 }
345
346 if (bFill)
347 {
348 if (mpVD->IsFillColor())
349 {
350 mpFillAttr->Put(XFillStyleItem(drawing::FillStyle_SOLID));
351 mpFillAttr->Put(XFillColorItem(OUString(), mpVD->GetFillColor()));
352 }
353 else
354 {
355 mpFillAttr->Put(XFillStyleItem(drawing::FillStyle_NONE));
356 }
357 }
358 else
359 {
360 mbNoFill = true;
361 }
362
363 if (bText && mbFntDirty)
364 {
365 vcl::Font aFnt(mpVD->GetFont());
366 const sal_uInt32 nHeight(FRound(aFnt.GetFontSize().Height() * mfScaleY));
367
368 mpTextAttr->Put(SvxFontItem(aFnt.GetFamilyType(), aFnt.GetFamilyName(), aFnt.GetStyleName(),
369 aFnt.GetPitch(), aFnt.GetCharSet(), EE_CHAR_FONTINFO));
370 mpTextAttr->Put(SvxFontItem(aFnt.GetFamilyType(), aFnt.GetFamilyName(), aFnt.GetStyleName(),
371 aFnt.GetPitch(), aFnt.GetCharSet(), EE_CHAR_FONTINFO_CJK));
372 mpTextAttr->Put(SvxFontItem(aFnt.GetFamilyType(), aFnt.GetFamilyName(), aFnt.GetStyleName(),
373 aFnt.GetPitch(), aFnt.GetCharSet(), EE_CHAR_FONTINFO_CTL));
374 mpTextAttr->Put(SvxPostureItem(aFnt.GetItalic(), EE_CHAR_ITALIC));
375 mpTextAttr->Put(SvxWeightItem(aFnt.GetWeight(), EE_CHAR_WEIGHT));
376 mpTextAttr->Put(SvxFontHeightItem(nHeight, 100, EE_CHAR_FONTHEIGHT));
377 mpTextAttr->Put(SvxFontHeightItem(nHeight, 100, EE_CHAR_FONTHEIGHT_CJK));
378 mpTextAttr->Put(SvxFontHeightItem(nHeight, 100, EE_CHAR_FONTHEIGHT_CTL));
379 mpTextAttr->Put(SvxCharScaleWidthItem(100, EE_CHAR_FONTWIDTH));
380 mpTextAttr->Put(SvxUnderlineItem(aFnt.GetUnderline(), EE_CHAR_UNDERLINE));
381 mpTextAttr->Put(SvxOverlineItem(aFnt.GetOverline(), EE_CHAR_OVERLINE));
382 mpTextAttr->Put(SvxCrossedOutItem(aFnt.GetStrikeout(), EE_CHAR_STRIKEOUT));
383 mpTextAttr->Put(SvxShadowedItem(aFnt.IsShadow(), EE_CHAR_SHADOW));
384
385 // #i118485# Setting this item leads to problems (written #i118498# for this)
386 // mpTextAttr->Put(SvxAutoKernItem(aFnt.IsKerning(), EE_CHAR_KERNING));
387
388 mpTextAttr->Put(SvxWordLineModeItem(aFnt.IsWordLineMode(), EE_CHAR_WLM));
389 mpTextAttr->Put(SvxContourItem(aFnt.IsOutline(), EE_CHAR_OUTLINE));
390 mpTextAttr->Put(SvxColorItem(mpVD->GetTextColor(), EE_CHAR_COLOR));
391 //... svxfont textitem svditext
392 mbFntDirty = false;
393 }
394
395 if (!pObj)
396 return;
397
398 pObj->SetLayer(mnLayer);
399
400 if (bLine)
401 {
402 pObj->SetMergedItemSet(*mpLineAttr);
403 }
404
405 if (bFill)
406 {
407 pObj->SetMergedItemSet(*mpFillAttr);
408 }
409
410 if (bText)
411 {
412 pObj->SetMergedItemSet(*mpTextAttr);
413 pObj->SetMergedItem(SdrTextHorzAdjustItem(SDRTEXTHORZADJUST_LEFT));
414 }
415}
416
417void ImpSdrPdfImport::InsertObj(SdrObject* pObj, bool bScale)
418{
419 if (bScale && !maScaleRect.IsEmpty())
420 {
421 if (mbSize)
422 {
423 pObj->NbcResize(Point(), maScaleX, maScaleY);
424 }
425
426 if (mbMov)
427 {
428 pObj->NbcMove(Size(maOfs.X(), maOfs.Y()));
429 }
430 }
431
432 if (isClip())
433 {
434 const basegfx::B2DPolyPolygon aPoly(pObj->TakeXorPoly());
435 const basegfx::B2DRange aOldRange(aPoly.getB2DRange());
436 const SdrLayerID aOldLayer(pObj->GetLayer());
437 const SfxItemSet aOldItemSet(pObj->GetMergedItemSet());
438 const SdrGrafObj* pSdrGrafObj = dynamic_cast<SdrGrafObj*>(pObj);
439 const SdrTextObj* pSdrTextObj = dynamic_cast<SdrTextObj*>(pObj);
440
441 if (pSdrTextObj && pSdrTextObj->HasText())
442 {
443 // all text objects are created from ImportText and have no line or fill attributes, so
444 // it is okay to concentrate on the text itself
445 while (true)
446 {
447 const basegfx::B2DPolyPolygon aTextContour(pSdrTextObj->TakeContour());
448 const basegfx::B2DRange aTextRange(aTextContour.getB2DRange());
449 const basegfx::B2DRange aClipRange(maClip.getB2DRange());
450
451 // no overlap -> completely outside
452 if (!aClipRange.overlaps(aTextRange))
453 {
454 SdrObject::Free(pObj);
455 break;
456 }
457
458 // when the clip is a rectangle fast check for inside is possible
459 if (basegfx::utils::isRectangle(maClip) && aClipRange.isInside(aTextRange))
460 {
461 // completely inside ClipRect
462 break;
463 }
464
465 // here text needs to be clipped; to do so, convert to SdrObjects with polygons
466 // and add these recursively. Delete original object, do not add in this run
467 SdrObjectUniquePtr pConverted = pSdrTextObj->ConvertToPolyObj(true, true);
468 SdrObject::Free(pObj);
469
470 if (pConverted)
471 {
472 // recursively add created conversion; per definition this shall not
473 // contain further SdrTextObjs. Visit only non-group objects
474 SdrObjListIter aIter(*pConverted, SdrIterMode::DeepNoGroups);
475
476 // work with clones; the created conversion may contain group objects
477 // and when working with the original objects the loop itself could
478 // break and the cleanup later would be pretty complicated (only delete group
479 // objects, are these empty, ...?)
480 while (aIter.IsMore())
481 {
482 SdrObject* pCandidate = aIter.Next();
483 OSL_ENSURE(pCandidate && dynamic_cast<SdrObjGroup*>(pCandidate) == nullptr,do { if (true && (!(pCandidate && dynamic_cast
<SdrObjGroup*>(pCandidate) == nullptr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "484" ": "), "%s", "SdrObjListIter with SdrIterMode::DeepNoGroups error (!)"
); } } while (false)
484 "SdrObjListIter with SdrIterMode::DeepNoGroups error (!)")do { if (true && (!(pCandidate && dynamic_cast
<SdrObjGroup*>(pCandidate) == nullptr))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "484" ": "), "%s", "SdrObjListIter with SdrIterMode::DeepNoGroups error (!)"
); } } while (false)
;
485 SdrObject* pNewClone(
486 pCandidate->CloneSdrObject(pCandidate->getSdrModelFromSdrObject()));
487
488 if (pNewClone)
489 {
490 InsertObj(pNewClone, false);
491 }
492 else
493 {
494 OSL_ENSURE(false, "SdrObject::Clone() failed (!)")do { if (true && (!(false))) { sal_detail_logFormat((
SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "494" ": "), "%s", "SdrObject::Clone() failed (!)"); } }
while (false)
;
495 }
496 }
497 }
498
499 break;
500 }
501 }
502 else
503 {
504 BitmapEx aBitmapEx;
505
506 if (pSdrGrafObj)
507 {
508 aBitmapEx = pSdrGrafObj->GetGraphic().GetBitmapEx();
509 }
510
511 SdrObject::Free(pObj);
512
513 if (!aOldRange.isEmpty())
514 {
515 // clip against ClipRegion
516 const basegfx::B2DPolyPolygon aNewPoly(basegfx::utils::clipPolyPolygonOnPolyPolygon(
517 aPoly, maClip, true, !aPoly.isClosed()));
518 const basegfx::B2DRange aNewRange(aNewPoly.getB2DRange());
519
520 if (!aNewRange.isEmpty())
521 {
522 pObj = new SdrPathObj(*mpModel, aNewPoly.isClosed() ? OBJ_POLY : OBJ_PLIN,
523 aNewPoly);
524
525 pObj->SetLayer(aOldLayer);
526 pObj->SetMergedItemSet(aOldItemSet);
527
528 if (!!aBitmapEx)
529 {
530 // aNewRange is inside of aOldRange and defines which part of aBitmapEx is used
531 const double fScaleX(aBitmapEx.GetSizePixel().Width()
532 / (aOldRange.getWidth() ? aOldRange.getWidth() : 1.0));
533 const double fScaleY(
534 aBitmapEx.GetSizePixel().Height()
535 / (aOldRange.getHeight() ? aOldRange.getHeight() : 1.0));
536 basegfx::B2DRange aPixel(aNewRange);
537 basegfx::B2DHomMatrix aTrans;
538
539 aTrans.translate(-aOldRange.getMinX(), -aOldRange.getMinY());
540 aTrans.scale(fScaleX, fScaleY);
541 aPixel.transform(aTrans);
542
543 const Size aOrigSizePixel(aBitmapEx.GetSizePixel());
544 const Point aClipTopLeft(
545 basegfx::fround(floor(std::max(0.0, aPixel.getMinX()))),
546 basegfx::fround(floor(std::max(0.0, aPixel.getMinY()))));
547 const Size aClipSize(
548 basegfx::fround(ceil(std::min(
549 static_cast<double>(aOrigSizePixel.Width()), aPixel.getWidth()))),
550 basegfx::fround(
551 ceil(std::min(static_cast<double>(aOrigSizePixel.Height()),
552 aPixel.getHeight()))));
553 const BitmapEx aClippedBitmap(aBitmapEx, aClipTopLeft, aClipSize);
554
555 pObj->SetMergedItem(XFillStyleItem(drawing::FillStyle_BITMAP));
556 pObj->SetMergedItem(XFillBitmapItem(OUString(), Graphic(aClippedBitmap)));
557 pObj->SetMergedItem(XFillBmpTileItem(false));
558 pObj->SetMergedItem(XFillBmpStretchItem(true));
559 }
560 }
561 }
562 }
563 }
564
565 if (!pObj)
566 return;
567
568 // #i111954# check object for visibility
569 // used are SdrPathObj, SdrRectObj, SdrCircObj, SdrGrafObj
570 bool bVisible(false);
571
572 if (pObj->HasLineStyle())
573 {
574 bVisible = true;
575 }
576
577 if (!bVisible && pObj->HasFillStyle())
578 {
579 bVisible = true;
580 }
581
582 if (!bVisible)
583 {
584 SdrTextObj* pTextObj = dynamic_cast<SdrTextObj*>(pObj);
585
586 if (pTextObj && pTextObj->HasText())
587 {
588 bVisible = true;
589 }
590 }
591
592 if (!bVisible)
593 {
594 SdrGrafObj* pGrafObj = dynamic_cast<SdrGrafObj*>(pObj);
595
596 if (pGrafObj)
597 {
598 // this may be refined to check if the graphic really is visible. It
599 // is here to ensure that graphic objects without fill, line and text
600 // get created
601 bVisible = true;
602 }
603 }
604
605 if (!bVisible)
606 {
607 SdrObject::Free(pObj);
608 }
609 else
610 {
611 maTmpList.push_back(pObj);
612
613 if (dynamic_cast<SdrPathObj*>(pObj))
614 {
615 const bool bClosed(pObj->IsClosedObj());
616
617 mbLastObjWasPolyWithoutLine = mbNoLine && bClosed;
618 }
619 else
620 {
621 mbLastObjWasPolyWithoutLine = false;
622 }
623 }
624}
625
626bool ImpSdrPdfImport::CheckLastPolyLineAndFillMerge(const basegfx::B2DPolyPolygon& rPolyPolygon)
627{
628 // #i73407# reformulation to use new B2DPolygon classes
629 if (mbLastObjWasPolyWithoutLine)
630 {
631 SdrObject* pTmpObj = !maTmpList.empty() ? maTmpList[maTmpList.size() - 1] : nullptr;
632 SdrPathObj* pLastPoly = dynamic_cast<SdrPathObj*>(pTmpObj);
633
634 if (pLastPoly)
635 {
636 if (pLastPoly->GetPathPoly() == rPolyPolygon)
637 {
638 SetAttributes(nullptr);
639
640 if (!mbNoLine && mbNoFill)
641 {
642 pLastPoly->SetMergedItemSet(*mpLineAttr);
643
644 return true;
645 }
646 }
647 }
648 }
649
650 return false;
651}
652
653void ImpSdrPdfImport::checkClip()
654{
655 if (mpVD->IsClipRegion())
656 {
657 maClip = mpVD->GetClipRegion().GetAsB2DPolyPolygon();
658
659 if (isClip())
660 {
661 const basegfx::B2DHomMatrix aTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
662 mfScaleX, mfScaleY, maOfs.X(), maOfs.Y()));
663
664 maClip.transform(aTransform);
665 }
666 }
667}
668
669bool ImpSdrPdfImport::isClip() const { return !maClip.getB2DRange().isEmpty(); }
670void ImpSdrPdfImport::ImportPdfObject(
671 std::unique_ptr<vcl::pdf::PDFiumPageObject> const& pPageObject,
672 std::unique_ptr<vcl::pdf::PDFiumTextPage> const& pTextPage, int nPageObjectIndex)
673{
674 if (!pPageObject)
675 return;
676
677 const int nPageObjectType = pPageObject->getType();
678 switch (nPageObjectType)
679 {
680 case FPDF_PAGEOBJ_TEXT1:
681 ImportText(pPageObject, pTextPage, nPageObjectIndex);
682 break;
683 case FPDF_PAGEOBJ_PATH2:
684 ImportPath(pPageObject, nPageObjectIndex);
685 break;
686 case FPDF_PAGEOBJ_IMAGE3:
687 ImportImage(pPageObject, nPageObjectIndex);
688 break;
689 case FPDF_PAGEOBJ_SHADING4:
690 SAL_WARN("sd.filter", "Got page object SHADING: " << nPageObjectIndex)do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Got page object SHADING: "
<< nPageObjectIndex) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "690" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Got page object SHADING: " << nPageObjectIndex
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Got page object SHADING: " << nPageObjectIndex
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "690" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Got page object SHADING: " << nPageObjectIndex
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "690" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Got page object SHADING: " << nPageObjectIndex
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "Got page object SHADING: " << nPageObjectIndex
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "690" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
691 break;
692 case FPDF_PAGEOBJ_FORM5:
693 ImportForm(pPageObject, pTextPage, nPageObjectIndex);
694 break;
695 default:
696 SAL_WARN("sd.filter", "Unknown PDF page object #" << nPageObjectIndexdo { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Unknown PDF page object #"
<< nPageObjectIndex << " of type: " << nPageObjectType
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "697" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Unknown PDF page object #" << nPageObjectIndex
<< " of type: " << nPageObjectType), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Unknown PDF page object #" << nPageObjectIndex <<
" of type: " << nPageObjectType; ::sal::detail::log( (
::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "697" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Unknown PDF page object #" << nPageObjectIndex
<< " of type: " << nPageObjectType) == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "697" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Unknown PDF page object #" << nPageObjectIndex
<< " of type: " << nPageObjectType), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Unknown PDF page object #" << nPageObjectIndex <<
" of type: " << nPageObjectType; ::sal::detail::log( (
::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "697" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
697 << " of type: " << nPageObjectType)do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Unknown PDF page object #"
<< nPageObjectIndex << " of type: " << nPageObjectType
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "697" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Unknown PDF page object #" << nPageObjectIndex
<< " of type: " << nPageObjectType), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Unknown PDF page object #" << nPageObjectIndex <<
" of type: " << nPageObjectType; ::sal::detail::log( (
::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "697" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Unknown PDF page object #" << nPageObjectIndex
<< " of type: " << nPageObjectType) == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "697" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Unknown PDF page object #" << nPageObjectIndex
<< " of type: " << nPageObjectType), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Unknown PDF page object #" << nPageObjectIndex <<
" of type: " << nPageObjectType; ::sal::detail::log( (
::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "697" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
698 break;
699 }
700}
701
702void ImpSdrPdfImport::ImportForm(std::unique_ptr<vcl::pdf::PDFiumPageObject> const& pPageObject,
703 std::unique_ptr<vcl::pdf::PDFiumTextPage> const& pTextPage,
704 int /*nPageObjectIndex*/)
705{
706 // Get the form matrix to perform correct translation/scaling of the form sub-objects.
707 const basegfx::B2DHomMatrix aOldMatrix = maCurrentMatrix;
708
709 maCurrentMatrix = pPageObject->getMatrix();
710
711 const int nCount = pPageObject->getFormObjectCount();
712 for (int nIndex = 0; nIndex < nCount; ++nIndex)
713 {
714 auto pFormObject = pPageObject->getFormObject(nIndex);
715
716 ImportPdfObject(pFormObject, pTextPage, -1);
717 }
718
719 // Restore the old one.
720 maCurrentMatrix = aOldMatrix;
721}
722
723void ImpSdrPdfImport::ImportText(std::unique_ptr<vcl::pdf::PDFiumPageObject> const& pPageObject,
724 std::unique_ptr<vcl::pdf::PDFiumTextPage> const& pTextPage,
725 int /*nPageObjectIndex*/)
726{
727 basegfx::B2DRectangle aTextRect = pPageObject->getBounds();
728 basegfx::B2DHomMatrix aMatrix = pPageObject->getMatrix();
729
730 basegfx::B2DHomMatrix aTextMatrix(maCurrentMatrix);
731
732 aTextRect *= aTextMatrix;
733 const tools::Rectangle aRect = PointsToLogic(aTextRect.getMinX(), aTextRect.getMaxX(),
734 aTextRect.getMinY(), aTextRect.getMaxY());
735
736 OUString sText = pPageObject->getText(pTextPage);
737
738 const double dFontSize = pPageObject->getFontSize();
739 double dFontSizeH = fabs(sqrt2(aMatrix.a(), aMatrix.c()) * dFontSize);
740 double dFontSizeV = fabs(sqrt2(aMatrix.b(), aMatrix.d()) * dFontSize);
741
742 dFontSizeH = convertPointToMm100(dFontSizeH);
743 dFontSizeV = convertPointToMm100(dFontSizeV);
744
745 const Size aFontSize(dFontSizeH, dFontSizeV);
746 vcl::Font aFnt = mpVD->GetFont();
747 if (aFontSize != aFnt.GetFontSize())
748 {
749 aFnt.SetFontSize(aFontSize);
750 mpVD->SetFont(aFnt);
751 mbFntDirty = true;
752 }
753
754 OUString sFontName = pPageObject->getFontName();
755 if (!sFontName.isEmpty() && sFontName != aFnt.GetFamilyName())
756 {
757 aFnt.SetFamilyName(sFontName);
758 mpVD->SetFont(aFnt);
759 mbFntDirty = true;
760 }
761
762 Color aTextColor(COL_TRANSPARENT);
763 bool bFill = false;
764 bool bUse = true;
765 switch (pPageObject->getTextRenderMode())
766 {
767 case FPDF_TEXTRENDERMODE_FILL:
768 case FPDF_TEXTRENDERMODE_FILL_CLIP:
769 case FPDF_TEXTRENDERMODE_FILL_STROKE:
770 case FPDF_TEXTRENDERMODE_FILL_STROKE_CLIP:
771 bFill = true;
772 break;
773 case FPDF_TEXTRENDERMODE_STROKE:
774 case FPDF_TEXTRENDERMODE_STROKE_CLIP:
775 case FPDF_TEXTRENDERMODE_UNKNOWN:
776 break;
777 case FPDF_TEXTRENDERMODE_INVISIBLE:
778 case FPDF_TEXTRENDERMODE_CLIP:
779 bUse = false;
780 break;
781 }
782 if (bUse)
783 {
784 Color aColor = bFill ? pPageObject->getFillColor() : pPageObject->getStrokeColor();
785 if (aColor != COL_TRANSPARENT)
786 aTextColor = aColor.GetRGBColor();
787 }
788
789 if (aTextColor != mpVD->GetTextColor())
790 {
791 mpVD->SetTextColor(aTextColor);
792 mbFntDirty = true;
793 }
794
795 InsertTextObject(aRect.TopLeft(), aRect.GetSize(), sText);
796}
797
798void ImpSdrPdfImport::InsertTextObject(const Point& rPos, const Size& rSize, const OUString& rStr)
799{
800 // calc text box size, add 5% to make it fit safely
801
802 FontMetric aFontMetric(mpVD->GetFontMetric());
803 vcl::Font aFont(mpVD->GetFont());
804 FontAlignTextAlign eAlignment(aFont.GetAlignment());
805
806 // sal_Int32 nTextWidth = static_cast<sal_Int32>(mpVD->GetTextWidth(rStr) * mfScaleX);
807 sal_Int32 nTextHeight = static_cast<sal_Int32>(mpVD->GetTextHeight() * mfScaleY);
808
809 Point aPosition(FRound(rPos.X() * mfScaleX + maOfs.X()),
810 FRound(rPos.Y() * mfScaleY + maOfs.Y()));
811 Size aSize(FRound(rSize.Width() * mfScaleX), FRound(rSize.Height() * mfScaleY));
812
813 if (eAlignment == ALIGN_BASELINE)
814 aPosition.AdjustY(-FRound(aFontMetric.GetAscent() * mfScaleY));
815 else if (eAlignment == ALIGN_BOTTOM)
816 aPosition.AdjustY(-nTextHeight);
817
818 tools::Rectangle aTextRect(aPosition, aSize);
819 SdrRectObj* pText = new SdrRectObj(*mpModel, OBJ_TEXT, aTextRect);
820
821 pText->SetMergedItem(makeSdrTextUpperDistItem(0));
822 pText->SetMergedItem(makeSdrTextLowerDistItem(0));
823 pText->SetMergedItem(makeSdrTextRightDistItem(0));
824 pText->SetMergedItem(makeSdrTextLeftDistItem(0));
825
826 if (aFont.GetAverageFontWidth())
827 {
828 pText->ClearMergedItem(SDRATTR_TEXT_AUTOGROWWIDTH);
829 pText->SetMergedItem(makeSdrTextAutoGrowHeightItem(false));
830 // don't let the margins eat the space needed for the text
831 pText->SetMergedItem(SdrTextFitToSizeTypeItem(drawing::TextFitToSizeType_ALLLINES));
832 }
833 else
834 {
835 pText->SetMergedItem(makeSdrTextAutoGrowWidthItem(true));
836 }
837
838 pText->SetLayer(mnLayer);
839 pText->NbcSetText(rStr);
840 SetAttributes(pText, true);
841 pText->SetSnapRect(aTextRect);
842
843 if (!aFont.IsTransparent())
844 {
845 SfxItemSet aAttr(*mpFillAttr->GetPool(), svl::Items<XATTR_FILL_FIRST, XATTR_FILL_LAST>{});
846 aAttr.Put(XFillStyleItem(drawing::FillStyle_SOLID));
847 aAttr.Put(XFillColorItem(OUString(), aFont.GetFillColor()));
848 pText->SetMergedItemSet(aAttr);
849 }
850 sal_uInt32 nAngle = aFont.GetOrientation();
851 if (nAngle)
852 {
853 nAngle *= 10;
854 double a = nAngle * F_PI18000(3.14159265358979323846/18000.0);
855 double nSin = sin(a);
856 double nCos = cos(a);
857 pText->NbcRotate(aPosition, nAngle, nSin, nCos);
858 }
859 InsertObj(pText, false);
860}
861
862void ImpSdrPdfImport::MapScaling()
863{
864 const size_t nCount(maTmpList.size());
865 const MapMode& rMap = mpVD->GetMapMode();
866 Point aMapOrg(rMap.GetOrigin());
867 bool bMov2(aMapOrg.X() != 0 || aMapOrg.Y() != 0);
868
869 if (bMov2)
870 {
871 for (size_t i = mnMapScalingOfs; i < nCount; i++)
872 {
873 SdrObject* pObj = maTmpList[i];
874
875 pObj->NbcMove(Size(aMapOrg.X(), aMapOrg.Y()));
876 }
877 }
878
879 mnMapScalingOfs = nCount;
880}
881
882void ImpSdrPdfImport::ImportImage(std::unique_ptr<vcl::pdf::PDFiumPageObject> const& pPageObject,
883 int /*nPageObjectIndex*/)
884{
885 std::unique_ptr<std::remove_pointer<FPDF_BITMAP>::type, FPDFBitmapDeleter> bitmap(
886 FPDFImageObj_GetBitmap(pPageObject->getPointer()));
887 if (!bitmap)
888 {
889 SAL_WARN("sd.filter", "Failed to get IMAGE")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Failed to get IMAGE"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "889" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Failed to get IMAGE"), 0); } else { ::
std::ostringstream sal_detail_stream; sal_detail_stream <<
"Failed to get IMAGE"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "889" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Failed to get IMAGE") == 1) { ::sal_detail_log( (
::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "889" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Failed to get IMAGE"), 0); } else { ::
std::ostringstream sal_detail_stream; sal_detail_stream <<
"Failed to get IMAGE"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "889" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
890 return;
891 }
892
893 const int format = FPDFBitmap_GetFormat(bitmap.get());
894 if (format == FPDFBitmap_Unknown0)
895 {
896 SAL_WARN("sd.filter", "Failed to get IMAGE format")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Failed to get IMAGE format"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "896" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Failed to get IMAGE format"), 0); } else
{ ::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Failed to get IMAGE format"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "896" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Failed to get IMAGE format") == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "896" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Failed to get IMAGE format"), 0); } else
{ ::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Failed to get IMAGE format"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "896" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
897 return;
898 }
899
900 const unsigned char* pBuf
901 = static_cast<const unsigned char*>(FPDFBitmap_GetBuffer(bitmap.get()));
902 const int nWidth = FPDFBitmap_GetWidth(bitmap.get());
903 const int nHeight = FPDFBitmap_GetHeight(bitmap.get());
904 const int nStride = FPDFBitmap_GetStride(bitmap.get());
905 BitmapEx aBitmap(Size(nWidth, nHeight), 24);
906
907 switch (format)
908 {
909 case FPDFBitmap_BGR2:
910 ReadRawDIB(aBitmap, pBuf, ScanlineFormat::N24BitTcBgr, nHeight, nStride);
911 break;
912 case FPDFBitmap_BGRx3:
913 ReadRawDIB(aBitmap, pBuf, ScanlineFormat::N32BitTcRgba, nHeight, nStride);
914 break;
915 case FPDFBitmap_BGRA4:
916 ReadRawDIB(aBitmap, pBuf, ScanlineFormat::N32BitTcBgra, nHeight, nStride);
917 break;
918 default:
919 SAL_WARN("sd.filter", "Got IMAGE width: " << nWidth << ", height: " << nHeightdo { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Got IMAGE width: " <<
nWidth << ", height: " << nHeight << ", stride: "
<< nStride << ", format: " << format) == 1
) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Got IMAGE width: " << nWidth <<
", height: " << nHeight << ", stride: " <<
nStride << ", format: " << format), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Got IMAGE width: " << nWidth << ", height: " <<
nHeight << ", stride: " << nStride << ", format: "
<< format; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Got IMAGE width: " << nWidth << ", height: "
<< nHeight << ", stride: " << nStride <<
", format: " << format) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Got IMAGE width: " << nWidth <<
", height: " << nHeight << ", stride: " <<
nStride << ", format: " << format), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Got IMAGE width: " << nWidth << ", height: " <<
nHeight << ", stride: " << nStride << ", format: "
<< format; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
920 << ", stride: " << nStridedo { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Got IMAGE width: " <<
nWidth << ", height: " << nHeight << ", stride: "
<< nStride << ", format: " << format) == 1
) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Got IMAGE width: " << nWidth <<
", height: " << nHeight << ", stride: " <<
nStride << ", format: " << format), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Got IMAGE width: " << nWidth << ", height: " <<
nHeight << ", stride: " << nStride << ", format: "
<< format; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Got IMAGE width: " << nWidth << ", height: "
<< nHeight << ", stride: " << nStride <<
", format: " << format) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Got IMAGE width: " << nWidth <<
", height: " << nHeight << ", stride: " <<
nStride << ", format: " << format), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Got IMAGE width: " << nWidth << ", height: " <<
nHeight << ", stride: " << nStride << ", format: "
<< format; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
921 << ", format: " << format)do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Got IMAGE width: " <<
nWidth << ", height: " << nHeight << ", stride: "
<< nStride << ", format: " << format) == 1
) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Got IMAGE width: " << nWidth <<
", height: " << nHeight << ", stride: " <<
nStride << ", format: " << format), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Got IMAGE width: " << nWidth << ", height: " <<
nHeight << ", stride: " << nStride << ", format: "
<< format; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Got IMAGE width: " << nWidth << ", height: "
<< nHeight << ", stride: " << nStride <<
", format: " << format) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "Got IMAGE width: " << nWidth <<
", height: " << nHeight << ", stride: " <<
nStride << ", format: " << format), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"Got IMAGE width: " << nWidth << ", height: " <<
nHeight << ", stride: " << nStride << ", format: "
<< format; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "921" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
922 break;
923 }
924
925 float left;
926 float bottom;
927 float right;
928 float top;
929 if (!FPDFPageObj_GetBounds(pPageObject->getPointer(), &left, &bottom, &right, &top))
930 {
931 SAL_WARN("sd.filter", "FAILED to get image bounds")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "FAILED to get image bounds"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "931" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "FAILED to get image bounds"), 0); } else
{ ::std::ostringstream sal_detail_stream; sal_detail_stream <<
"FAILED to get image bounds"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "931" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "FAILED to get image bounds") == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "931" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "FAILED to get image bounds"), 0); } else
{ ::std::ostringstream sal_detail_stream; sal_detail_stream <<
"FAILED to get image bounds"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "931" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
932 }
933
934 tools::Rectangle aRect = PointsToLogic(left, right, top, bottom);
935 aRect.AdjustRight(1);
936 aRect.AdjustBottom(1);
937
938 SdrGrafObj* pGraf = new SdrGrafObj(*mpModel, Graphic(aBitmap), aRect);
939
940 // This action is not creating line and fill, set directly, do not use SetAttributes(..)
941 pGraf->SetMergedItem(XLineStyleItem(drawing::LineStyle_NONE));
942 pGraf->SetMergedItem(XFillStyleItem(drawing::FillStyle_NONE));
943 InsertObj(pGraf);
944}
945
946void ImpSdrPdfImport::ImportPath(std::unique_ptr<vcl::pdf::PDFiumPageObject> const& pPageObject,
947 int /*nPageObjectIndex*/)
948{
949 auto aPathMatrix = pPageObject->getMatrix();
950
951 aPathMatrix *= maCurrentMatrix;
952
953 basegfx::B2DPolyPolygon aPolyPoly;
954 basegfx::B2DPolygon aPoly;
955 std::vector<basegfx::B2DPoint> aBezier;
956
957 const int nSegments = pPageObject->getPathSegmentCount();
958 for (int nSegmentIndex = 0; nSegmentIndex < nSegments; ++nSegmentIndex)
959 {
960 auto pPathSegment = pPageObject->getPathSegment(nSegmentIndex);
961 if (pPathSegment != nullptr)
962 {
963 basegfx::B2DPoint aB2DPoint = pPathSegment->getPoint();
964 aB2DPoint *= aPathMatrix;
965
966 const bool bClose = pPathSegment->isClosed();
967 if (bClose)
968 aPoly.setClosed(bClose); // TODO: Review
969
970 Point aPoint = PointsToLogic(aB2DPoint.getX(), aB2DPoint.getY());
971 aB2DPoint.setX(aPoint.X());
972 aB2DPoint.setY(aPoint.Y());
973
974 const int nSegmentType = pPathSegment->getType();
975 switch (nSegmentType)
976 {
977 case FPDF_SEGMENT_LINETO0:
978 aPoly.append(aB2DPoint);
979 break;
980
981 case FPDF_SEGMENT_BEZIERTO1:
982 aBezier.emplace_back(aB2DPoint.getX(), aB2DPoint.getY());
983 if (aBezier.size() == 3)
984 {
985 aPoly.appendBezierSegment(aBezier[0], aBezier[1], aBezier[2]);
986 aBezier.clear();
987 }
988 break;
989
990 case FPDF_SEGMENT_MOVETO2:
991 // New Poly.
992 if (aPoly.count() > 0)
993 {
994 aPolyPoly.append(aPoly, 1);
995 aPoly.clear();
996 }
997
998 aPoly.append(aB2DPoint);
999 break;
1000
1001 case FPDF_SEGMENT_UNKNOWN-1:
1002 default:
1003 SAL_WARN("sd.filter", "Unknown path segment type in PDF: " << nSegmentType)do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN
, "sd.filter")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "Unknown path segment type in PDF: "
<< nSegmentType) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "1003" ": "), ::sal::detail::unwrapStream( ::sal::detail
::StreamStart() << "Unknown path segment type in PDF: "
<< nSegmentType), 0); } else { ::std::ostringstream sal_detail_stream
; sal_detail_stream << "Unknown path segment type in PDF: "
<< nSegmentType; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "1003" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "Unknown path segment type in PDF: " << nSegmentType
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sd.filter"
), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "1003" ": "), ::sal::detail::unwrapStream( ::sal::detail
::StreamStart() << "Unknown path segment type in PDF: "
<< nSegmentType), 0); } else { ::std::ostringstream sal_detail_stream
; sal_detail_stream << "Unknown path segment type in PDF: "
<< nSegmentType; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sd.filter"), ("/home/maarten/src/libreoffice/core/svx/source/svdraw/svdpdf.cxx"
":" "1003" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
1004 break;
1005 }
1006 }
1007 }
1008
1009 if (aBezier.size() == 3)
1010 {
1011 aPoly.appendBezierSegment(aBezier[0], aBezier[1], aBezier[2]);
1012 aBezier.clear();
1013 }
1014
1015 if (aPoly.count() > 0)
1016 {
1017 aPolyPoly.append(aPoly, 1);
1018 aPoly.clear();
1019 }
1020
1021 const basegfx::B2DHomMatrix aTransform(
1022 basegfx::utils::createScaleTranslateB2DHomMatrix(mfScaleX, mfScaleY, maOfs.X(), maOfs.Y()));
1023 aPolyPoly.transform(aTransform);
1024
1025 float fWidth = 1;
1026 FPDFPageObj_GetStrokeWidth(pPageObject->getPointer(), &fWidth);
1027 const double dWidth = 0.5 * fabs(sqrt2(aPathMatrix.a(), aPathMatrix.c()) * fWidth);
1028 mnLineWidth = convertPointToMm100(dWidth);
1029
1030 int nFillMode = FPDF_FILLMODE_ALTERNATE1;
1031 FPDF_BOOL bStroke = 1; // Assume we have to draw, unless told otherwise.
1032 if (FPDFPath_GetDrawMode(pPageObject->getPointer(), &nFillMode, &bStroke))
1033 {
1034 if (nFillMode == FPDF_FILLMODE_ALTERNATE1)
1035 mpVD->SetDrawMode(DrawModeFlags::Default);
1036 else if (nFillMode == FPDF_FILLMODE_WINDING2)
1037 mpVD->SetDrawMode(DrawModeFlags::Default);
1038 else
1039 mpVD->SetDrawMode(DrawModeFlags::NoFill);
1040 }
1041
1042 unsigned int nR;
1043 unsigned int nG;
1044 unsigned int nB;
1045 unsigned int nA;
1046 FPDFPageObj_GetFillColor(pPageObject->getPointer(), &nR, &nG, &nB, &nA);
1047 mpVD->SetFillColor(Color(nR, nG, nB));
1048
1049 if (bStroke)
1050 {
1051 FPDFPageObj_GetStrokeColor(pPageObject->getPointer(), &nR, &nG, &nB, &nA);
1052 mpVD->SetLineColor(Color(nR, nG, nB));
1053 }
1054 else
1055 mpVD->SetLineColor(COL_TRANSPARENT);
1056
1057 if (!mbLastObjWasPolyWithoutLine || !CheckLastPolyLineAndFillMerge(aPolyPoly))
1058 {
1059 SdrPathObj* pPath = new SdrPathObj(*mpModel, OBJ_POLY, aPolyPoly);
1060 SetAttributes(pPath);
1061 InsertObj(pPath, false);
1062 }
1063}
1064
1065Point ImpSdrPdfImport::PointsToLogic(double x, double y) const
1066{
1067 y = correctVertOrigin(y);
1068
1069 Point aPos(convertPointToMm100(x), convertPointToMm100(y));
1070 return aPos;
1071}
1072
1073tools::Rectangle ImpSdrPdfImport::PointsToLogic(double left, double right, double top,
1074 double bottom) const
1075{
1076 top = correctVertOrigin(top);
1077 bottom = correctVertOrigin(bottom);
1078
1079 Point aPos(convertPointToMm100(left), convertPointToMm100(top));
1080 Size aSize(convertPointToMm100(right - left), convertPointToMm100(bottom - top));
1081
1082 return tools::Rectangle(aPos, aSize);
1083}
1084
1085#endif // HAVE_FEATURE_PDFIUM
1086
1087/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx

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
36class VclReferenceBase;
37
38namespace vcl::detail {
39
40template<typename>
41constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; }
42
43template<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 */
56template <class reference_type>
57class 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
66public:
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);
3
Calling copy constructor for 'Reference<VirtualDevice>'
6
Returning from copy constructor for 'Reference<VirtualDevice>'
204 m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-)
7
Calling 'Reference::clear'
14
Returning; memory was released
205 if (aTmp.get()) {
15
Calling 'Reference::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
218template<typename T1, typename T2>
219inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
220 return p1.get() == p2.get();
221}
222
223template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2)
224{
225 return p1.get() == p2;
226}
227
228template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) {
229 return p1.get() == p2;
230}
231
232template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2)
233{
234 return p1 == p2.get();
235}
236
237template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) {
238 return p1 == p2.get();
239}
240
241template<typename T1, typename T2>
242inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
243 return !(p1 == p2);
244}
245
246template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2)
247{
248 return !(p1 == p2);
249}
250
251template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) {
252 return !(p1 == p2);
253}
254
255template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2)
256{
257 return !(p1 == p2);
258}
259
260template<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 */
274template <class reference_type>
275class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type>
276{
277public:
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
290template <class reference_type>
291class ScopedVclPtr : public VclPtr<reference_type>
292{
293public:
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();
2
Calling 'VclPtr::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
378private:
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
387protected:
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
406template <class reference_type>
407class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type>
408{
409public:
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
421private:
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: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

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
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
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)
4
Assuming field 'm_pBody' is non-null
5
Taking true branch
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
7.1
Field 'm_pBody' is non-null
7.1
Field 'm_pBody' is non-null
7.1
Field 'm_pBody' is non-null
7.1
Field 'm_pBody' is non-null
)
8
Taking true branch
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL__null;
180 pOld->release();
9
Calling 'VclReferenceBase::release'
13
Returning; memory was released
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;
16
Use of memory after it is freed
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
277namespace 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*/
286template<typename T>
287struct 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: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

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
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
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)
10
Assuming the condition is true
11
Taking true branch
40 delete this;
12
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif