1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <SfxRedactionHelper.hxx>
11 #include <autoredactdialog.hxx>
13 #include <com/sun/star/beans/XPropertySet.hpp>
14 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
15 #include <com/sun/star/drawing/LineStyle.hpp>
16 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
17 #include <com/sun/star/graphic/XGraphic.hpp>
18 #include <com/sun/star/frame/XLayoutManager.hpp>
20 // For page margin related methods
21 #include <com/sun/star/style/XStyle.hpp>
22 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
23 #include <com/sun/star/text/XPageCursor.hpp>
24 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
25 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
28 #include <i18nutil/searchopt.hxx>
29 #include <com/sun/star/util/SearchAlgorithms.hpp>
30 #include <com/sun/star/util/SearchAlgorithms2.hpp>
31 #include <com/sun/star/util/SearchFlags.hpp>
32 #include <vcl/svapp.hxx>
33 #include <vcl/settings.hxx>
34 #include <i18nlangtag/languagetag.hxx>
35 #include <unotools/textsearch.hxx>
37 #include <sfx2/request.hxx>
38 #include <sfx2/sfxsids.hrc>
39 #include <sfx2/viewfrm.hxx>
41 #include <svl/eitem.hxx>
42 #include <svl/stritem.hxx>
44 #include <svtools/DocumentToGraphicRenderer.hxx>
46 #include <tools/gen.hxx>
47 #include <tools/stream.hxx>
48 #include <tools/diagnose_ex.h>
50 #include <vcl/gdimtf.hxx>
51 #include <vcl/graph.hxx>
52 #include <sal/log.hxx>
54 #include <vcl/wmf.hxx>
55 #include <vcl/metaact.hxx>
56 #include <vcl/outdev.hxx>
57 #include <vcl/vcllayout.hxx>
59 using namespace ::com::sun::star
;
60 using namespace ::com::sun::star::lang
;
61 using namespace ::com::sun::star::uno
;
63 bool SfxRedactionHelper::isRedactMode(const SfxRequest
& rReq
)
65 const SfxItemSet
* pArgs
= rReq
.GetArgs();
68 const SfxBoolItem
* pIsRedactMode
= rReq
.GetArg
<SfxBoolItem
>(SID_IS_REDACT_MODE
);
69 if (pIsRedactMode
&& pIsRedactMode
->GetValue())
76 OUString
SfxRedactionHelper::getStringParam(const SfxRequest
& rReq
, sal_uInt16 nParamId
)
78 OUString sStringParam
;
80 const SfxItemSet
* pArgs
= rReq
.GetArgs();
84 const SfxStringItem
* pStringArg
= rReq
.GetArg
<SfxStringItem
>(nParamId
);
88 sStringParam
= pStringArg
->GetValue();
95 * Roundtrip the gdimetafile to and from WMF
96 * to get rid of the position and size irregularities
97 * We better check the conversion method to see what it
98 * actually does to correct these issues, and do it ourselves.
100 void fixMetaFile(GDIMetaFile
& tmpMtf
)
102 SvMemoryStream
aDestStrm(65535, 65535);
103 ConvertGDIMetaFileToWMF(tmpMtf
, aDestStrm
, nullptr, false);
108 ReadWindowMetafile(aDestStrm
, tmpMtf
);
112 * Sets page margins for a Draw page. Negative values are considered erroneous
114 void setPageMargins(const uno::Reference
<beans::XPropertySet
>& xPageProperySet
,
115 const PageMargins
& aPageMargins
)
117 if (aPageMargins
.nTop
< 0 || aPageMargins
.nBottom
< 0 || aPageMargins
.nLeft
< 0
118 || aPageMargins
.nRight
< 0)
121 xPageProperySet
->setPropertyValue("BorderTop", css::uno::makeAny(aPageMargins
.nTop
));
122 xPageProperySet
->setPropertyValue("BorderBottom", css::uno::makeAny(aPageMargins
.nBottom
));
123 xPageProperySet
->setPropertyValue("BorderLeft", css::uno::makeAny(aPageMargins
.nLeft
));
124 xPageProperySet
->setPropertyValue("BorderRight", css::uno::makeAny(aPageMargins
.nRight
));
127 // #i10613# Extracted from ImplCheckRect::ImplCreate
128 tools::Rectangle
ImplCalcActionBounds(const MetaAction
& rAct
, const OutputDevice
& rOut
,
129 sal_Int32 nStrStartPos
, sal_Int32 nStrEndPos
)
131 tools::Rectangle aActionBounds
;
133 switch (rAct
.GetType())
135 case MetaActionType::TEXTARRAY
:
137 const MetaTextArrayAction
& rTextAct
= static_cast<const MetaTextArrayAction
&>(rAct
);
138 const OUString
aString(rTextAct
.GetText().copy(rTextAct
.GetIndex(), rTextAct
.GetLen()));
140 if (!aString
.isEmpty())
142 // #105987# ImplLayout takes everything in logical coordinates
143 std::unique_ptr
<SalLayout
> pSalLayout1
= rOut
.ImplLayout(
144 aString
, 0, nStrStartPos
, rTextAct
.GetPoint(), 0, rTextAct
.GetDXArray());
145 std::unique_ptr
<SalLayout
> pSalLayout2
= rOut
.ImplLayout(
146 aString
, 0, nStrEndPos
, rTextAct
.GetPoint(), 0, rTextAct
.GetDXArray());
149 tools::Rectangle
aBoundRect2(rOut
.ImplGetTextBoundRect(*pSalLayout2
));
150 aActionBounds
= rOut
.PixelToLogic(aBoundRect2
);
152 if (pSalLayout1
&& nStrStartPos
> 0)
154 tools::Rectangle
aBoundRect1(rOut
.ImplGetTextBoundRect(*pSalLayout1
));
155 aActionBounds
.SetLeft(rOut
.PixelToLogic(aBoundRect1
).Right());
165 if (!aActionBounds
.IsEmpty())
167 // fdo#40421 limit current action's output to clipped area
168 if (rOut
.IsClipRegion())
169 return rOut
.GetClipRegion().GetBoundRect().Intersection(aActionBounds
);
171 return aActionBounds
;
174 return aActionBounds
;
177 } // End of anon namespace
179 void SfxRedactionHelper::getPageMetaFilesFromDoc(std::vector
<GDIMetaFile
>& aMetaFiles
,
180 std::vector
<::Size
>& aPageSizes
, sal_Int32 nPages
,
181 DocumentToGraphicRenderer
& aRenderer
)
183 for (sal_Int32 nPage
= 1; nPage
<= nPages
; ++nPage
)
185 ::Size aDocumentSizePixel
= aRenderer
.getDocumentSizeInPixels(nPage
);
187 ::Point aCalcPageLogicPos
;
188 ::Size aCalcPageContentSize
;
189 ::Size aLogic
= aRenderer
.getDocumentSizeIn100mm(nPage
, &aLogicPos
, &aCalcPageLogicPos
,
190 &aCalcPageContentSize
);
192 aPageSizes
.push_back(aLogic
);
194 Graphic aGraphic
= aRenderer
.renderToGraphic(nPage
, aDocumentSizePixel
, aDocumentSizePixel
,
195 COL_TRANSPARENT
, true);
196 auto& rGDIMetaFile
= const_cast<GDIMetaFile
&>(aGraphic
.GetGDIMetaFile());
198 // Set preferred map unit and size on the metafile, so the Shape size
199 // will be correct in MM.
201 aMapMode
.SetMapUnit(MapUnit::Map100thMM
);
203 rGDIMetaFile
.SetPrefMapMode(aMapMode
);
204 rGDIMetaFile
.SetPrefSize(aLogic
);
206 fixMetaFile(rGDIMetaFile
);
208 aMetaFiles
.push_back(rGDIMetaFile
);
212 void SfxRedactionHelper::addPagesToDraw(
213 const uno::Reference
<XComponent
>& xComponent
, sal_Int32 nPages
,
214 const std::vector
<GDIMetaFile
>& aMetaFiles
, const std::vector
<::Size
>& aPageSizes
,
215 const PageMargins
& aPageMargins
,
216 const std::vector
<std::pair
<RedactionTarget
, OUString
>>& r_aTableTargets
, bool bIsAutoRedact
)
218 // Access the draw pages
219 uno::Reference
<drawing::XDrawPagesSupplier
> xDrawPagesSupplier(xComponent
, uno::UNO_QUERY
);
220 uno::Reference
<drawing::XDrawPages
> xDrawPages
= xDrawPagesSupplier
->getDrawPages();
222 uno::Reference
<css::lang::XMultiServiceFactory
> xFactory(xComponent
, uno::UNO_QUERY
);
224 for (sal_Int32 nPage
= 0; nPage
< nPages
; ++nPage
)
226 GDIMetaFile rGDIMetaFile
= aMetaFiles
[nPage
];
227 Graphic
aGraphic(rGDIMetaFile
);
229 sal_Int32
nPageHeight(aPageSizes
[nPage
].Height());
230 sal_Int32
nPageWidth(aPageSizes
[nPage
].Width());
232 uno::Reference
<graphic::XGraphic
> xGraph
= aGraphic
.GetXGraphic();
233 uno::Reference
<drawing::XDrawPage
> xPage
= xDrawPages
->insertNewByIndex(nPage
);
235 // Set page size & margins
236 uno::Reference
<beans::XPropertySet
> xPageProperySet(xPage
, uno::UNO_QUERY
);
237 xPageProperySet
->setPropertyValue("Height", css::uno::makeAny(nPageHeight
));
238 xPageProperySet
->setPropertyValue("Width", css::uno::makeAny(nPageWidth
));
240 setPageMargins(xPageProperySet
, aPageMargins
);
242 // Create and insert the shape
243 uno::Reference
<drawing::XShape
> xShape(
244 xFactory
->createInstance("com.sun.star.drawing.GraphicObjectShape"), uno::UNO_QUERY
);
245 uno::Reference
<beans::XPropertySet
> xShapeProperySet(xShape
, uno::UNO_QUERY
);
246 xShapeProperySet
->setPropertyValue("Graphic", uno::Any(xGraph
));
247 xShapeProperySet
->setPropertyValue("MoveProtect", uno::Any(true));
248 xShapeProperySet
->setPropertyValue("SizeProtect", uno::Any(true));
252 awt::Size(rGDIMetaFile
.GetPrefSize().Width(), rGDIMetaFile
.GetPrefSize().Height()));
256 if (bIsAutoRedact
&& !r_aTableTargets
.empty())
258 for (const auto& targetPair
: r_aTableTargets
)
260 autoRedactPage(targetPair
.first
, rGDIMetaFile
, xPage
, xComponent
);
265 // Remove the extra page at the beginning
266 uno::Reference
<drawing::XDrawPage
> xPage(xDrawPages
->getByIndex(0), uno::UNO_QUERY_THROW
);
267 xDrawPages
->remove(xPage
);
270 void SfxRedactionHelper::showRedactionToolbar(const SfxViewFrame
* pViewFrame
)
275 Reference
<frame::XFrame
> xFrame
= pViewFrame
->GetFrame().GetFrameInterface();
276 Reference
<css::beans::XPropertySet
> xPropSet(xFrame
, UNO_QUERY
);
277 Reference
<css::frame::XLayoutManager
> xLayoutManager
;
284 Any aValue
= xPropSet
->getPropertyValue("LayoutManager");
285 aValue
>>= xLayoutManager
;
286 xLayoutManager
->createElement("private:resource/toolbar/redactionbar");
287 xLayoutManager
->showElement("private:resource/toolbar/redactionbar");
289 catch (const css::uno::RuntimeException
&)
293 catch (css::uno::Exception
&)
295 TOOLS_WARN_EXCEPTION("sfx.doc", "Exception while trying to show the Redaction Toolbar!");
300 SfxRedactionHelper::getPageMarginsForWriter(const css::uno::Reference
<css::frame::XModel
>& xModel
)
302 PageMargins aPageMargins
= { -1, -1, -1, -1 };
304 Reference
<text::XTextViewCursorSupplier
> xTextViewCursorSupplier(xModel
->getCurrentController(),
306 if (!xTextViewCursorSupplier
.is())
308 SAL_WARN("sfx.doc", "Ref to xTextViewCursorSupplier is null in setPageMargins().");
312 Reference
<text::XPageCursor
> xCursor(xTextViewCursorSupplier
->getViewCursor(), UNO_QUERY
);
314 uno::Reference
<beans::XPropertySet
> xPageProperySet(xCursor
, UNO_QUERY
);
315 OUString sPageStyleName
;
316 Any aValue
= xPageProperySet
->getPropertyValue("PageStyleName");
317 aValue
>>= sPageStyleName
;
319 Reference
<css::style::XStyleFamiliesSupplier
> xStyleFamiliesSupplier(xModel
, UNO_QUERY
);
320 if (!xStyleFamiliesSupplier
.is())
322 SAL_WARN("sfx.doc", "Ref to xStyleFamiliesSupplier is null in setPageMargins().");
325 uno::Reference
<container::XNameAccess
> xStyleFamilies
326 = xStyleFamiliesSupplier
->getStyleFamilies();
328 if (!xStyleFamilies
.is())
331 uno::Reference
<container::XNameAccess
> xPageStyles(xStyleFamilies
->getByName("PageStyles"),
334 if (!xPageStyles
.is())
337 uno::Reference
<css::style::XStyle
> xPageStyle(xPageStyles
->getByName(sPageStyleName
),
340 if (!xPageStyle
.is())
343 uno::Reference
<beans::XPropertySet
> xPageProperties(xPageStyle
, uno::UNO_QUERY
);
345 if (!xPageProperties
.is())
348 xPageProperties
->getPropertyValue("LeftMargin") >>= aPageMargins
.nLeft
;
349 xPageProperties
->getPropertyValue("RightMargin") >>= aPageMargins
.nRight
;
350 xPageProperties
->getPropertyValue("TopMargin") >>= aPageMargins
.nTop
;
351 xPageProperties
->getPropertyValue("BottomMargin") >>= aPageMargins
.nBottom
;
357 SfxRedactionHelper::getPageMarginsForCalc(const css::uno::Reference
<css::frame::XModel
>& xModel
)
359 PageMargins aPageMargins
= { -1, -1, -1, -1 };
360 OUString
sPageStyleName("Default");
362 css::uno::Reference
<css::sheet::XSpreadsheetView
> xSpreadsheetView(
363 xModel
->getCurrentController(), UNO_QUERY
);
365 if (!xSpreadsheetView
.is())
367 SAL_WARN("sfx.doc", "Ref to xSpreadsheetView is null in getPageMarginsForCalc().");
371 uno::Reference
<beans::XPropertySet
> xSheetProperties(xSpreadsheetView
->getActiveSheet(),
374 xSheetProperties
->getPropertyValue("PageStyle") >>= sPageStyleName
;
376 Reference
<css::style::XStyleFamiliesSupplier
> xStyleFamiliesSupplier(xModel
, UNO_QUERY
);
377 if (!xStyleFamiliesSupplier
.is())
379 SAL_WARN("sfx.doc", "Ref to xStyleFamiliesSupplier is null in getPageMarginsForCalc().");
382 uno::Reference
<container::XNameAccess
> xStyleFamilies
383 = xStyleFamiliesSupplier
->getStyleFamilies();
385 if (!xStyleFamilies
.is())
388 uno::Reference
<container::XNameAccess
> xPageStyles(xStyleFamilies
->getByName("PageStyles"),
391 if (!xPageStyles
.is())
394 uno::Reference
<css::style::XStyle
> xPageStyle(xPageStyles
->getByName(sPageStyleName
),
397 if (!xPageStyle
.is())
400 uno::Reference
<beans::XPropertySet
> xPageProperties(xPageStyle
, uno::UNO_QUERY
);
402 if (!xPageProperties
.is())
405 xPageProperties
->getPropertyValue("LeftMargin") >>= aPageMargins
.nLeft
;
406 xPageProperties
->getPropertyValue("RightMargin") >>= aPageMargins
.nRight
;
407 xPageProperties
->getPropertyValue("TopMargin") >>= aPageMargins
.nTop
;
408 xPageProperties
->getPropertyValue("BottomMargin") >>= aPageMargins
.nBottom
;
413 void SfxRedactionHelper::searchInMetaFile(const RedactionTarget
& rRedactionTarget
,
414 const GDIMetaFile
& rMtf
,
415 std::vector
<::tools::Rectangle
>& aRedactionRectangles
,
416 const uno::Reference
<XComponent
>& xComponent
)
419 i18nutil::SearchOptions2 aSearchOptions
;
420 fillSearchOptions(aSearchOptions
, rRedactionTarget
);
422 utl::TextSearch
textSearch(aSearchOptions
);
423 static tools::Long aLastFontHeight
= 0;
425 MetaAction
* pCurrAct
;
427 for (pCurrAct
= const_cast<GDIMetaFile
&>(rMtf
).FirstAction(); pCurrAct
;
428 pCurrAct
= const_cast<GDIMetaFile
&>(rMtf
).NextAction())
430 // Watch for TEXTARRAY actions.
431 // They contain the text of paragraphs.
432 if (pCurrAct
->GetType() == MetaActionType::TEXTARRAY
)
434 MetaTextArrayAction
* pMetaTextArrayAction
= static_cast<MetaTextArrayAction
*>(pCurrAct
);
436 // Search operation takes place here
437 OUString sText
= pMetaTextArrayAction
->GetText();
438 sal_Int32 nStart
= 0;
439 sal_Int32 nEnd
= sText
.getLength();
441 bool bFound
= textSearch
.SearchForward(sText
, &nStart
, &nEnd
);
443 // If found the string, add the corresponding rectangle to the collection
446 OutputDevice
* pOutputDevice
447 = SfxObjectShell::GetShellFromComponent(xComponent
)->GetDocumentRefDev();
448 tools::Rectangle
aNewRect(
449 ImplCalcActionBounds(*pMetaTextArrayAction
, *pOutputDevice
, nStart
, nEnd
));
451 if (!aNewRect
.IsEmpty())
453 // Calculate the difference between current wrong value and value should it be.
454 // Add the difference to current value.
455 // Then increase 10% of the new value to make it look better.
456 aNewRect
.SetTop(aNewRect
.Bottom() - aLastFontHeight
- aLastFontHeight
/ 10);
457 aRedactionRectangles
.push_back(aNewRect
);
460 // Search for the next occurrence
462 nEnd
= sText
.getLength();
463 bFound
= textSearch
.SearchForward(sText
, &nStart
, &nEnd
);
466 else if (pCurrAct
->GetType() == MetaActionType::FONT
)
468 const MetaFontAction
* pFontAct
= static_cast<const MetaFontAction
*>(pCurrAct
);
469 aLastFontHeight
= pFontAct
->GetFont().GetFontSize().getHeight();
474 void SfxRedactionHelper::addRedactionRectToPage(
475 const uno::Reference
<XComponent
>& xComponent
, const uno::Reference
<drawing::XDrawPage
>& xPage
,
476 const std::vector
<::tools::Rectangle
>& aNewRectangles
)
478 if (!xComponent
.is() || !xPage
.is())
481 if (aNewRectangles
.empty())
484 uno::Reference
<css::lang::XMultiServiceFactory
> xFactory(xComponent
, uno::UNO_QUERY
);
486 for (auto const& aNewRectangle
: aNewRectangles
)
488 uno::Reference
<drawing::XShape
> xRectShape(
489 xFactory
->createInstance("com.sun.star.drawing.RectangleShape"), uno::UNO_QUERY
);
490 uno::Reference
<beans::XPropertySet
> xRectShapeProperySet(xRectShape
, uno::UNO_QUERY
);
492 xRectShapeProperySet
->setPropertyValue("Name",
493 uno::Any(OUString("RectangleRedactionShape")));
494 xRectShapeProperySet
->setPropertyValue("FillTransparence",
495 css::uno::makeAny(static_cast<sal_Int16
>(50)));
496 xRectShapeProperySet
->setPropertyValue("FillColor", css::uno::makeAny(COL_GRAY7
));
497 xRectShapeProperySet
->setPropertyValue(
498 "LineStyle", css::uno::makeAny(css::drawing::LineStyle::LineStyle_NONE
));
500 xRectShape
->setSize(awt::Size(aNewRectangle
.GetWidth(), aNewRectangle
.GetHeight()));
501 xRectShape
->setPosition(awt::Point(aNewRectangle
.Left(), aNewRectangle
.Top()));
503 xPage
->add(xRectShape
);
507 void SfxRedactionHelper::autoRedactPage(const RedactionTarget
& rRedactionTarget
,
508 const GDIMetaFile
& rGDIMetaFile
,
509 const uno::Reference
<drawing::XDrawPage
>& xPage
,
510 const uno::Reference
<XComponent
>& xComponent
)
512 if (rRedactionTarget
.sContent
.isEmpty())
515 // Search for the redaction strings, and get the rectangle coordinates
516 std::vector
<::tools::Rectangle
> aRedactionRectangles
;
517 searchInMetaFile(rRedactionTarget
, rGDIMetaFile
, aRedactionRectangles
, xComponent
);
519 // Add the redaction rectangles to the page
520 addRedactionRectToPage(xComponent
, xPage
, aRedactionRectangles
);
525 const LanguageTag
& GetAppLanguageTag() { return Application::GetSettings().GetLanguageTag(); }
528 void SfxRedactionHelper::fillSearchOptions(i18nutil::SearchOptions2
& rSearchOpt
,
529 const RedactionTarget
& rTarget
)
531 if (rTarget
.sType
== RedactionTargetType::REDACTION_TARGET_REGEX
532 || rTarget
.sType
== RedactionTargetType::REDACTION_TARGET_PREDEFINED
)
534 rSearchOpt
.algorithmType
= util::SearchAlgorithms_REGEXP
;
535 rSearchOpt
.AlgorithmType2
= util::SearchAlgorithms2::REGEXP
;
539 rSearchOpt
.algorithmType
= util::SearchAlgorithms_ABSOLUTE
;
540 rSearchOpt
.AlgorithmType2
= util::SearchAlgorithms2::ABSOLUTE
;
543 rSearchOpt
.Locale
= GetAppLanguageTag().getLocale();
544 if (rTarget
.sType
== RedactionTargetType::REDACTION_TARGET_PREDEFINED
)
546 auto nPredefIndex
= rTarget
.sContent
.getToken(0, ';').toUInt32();
547 rSearchOpt
.searchString
= m_aPredefinedTargets
[nPredefIndex
];
550 rSearchOpt
.searchString
= rTarget
.sContent
;
552 rSearchOpt
.replaceString
.clear();
554 if (!rTarget
.bCaseSensitive
&& rTarget
.sType
!= RedactionTargetType::REDACTION_TARGET_REGEX
555 && rTarget
.sType
!= RedactionTargetType::REDACTION_TARGET_PREDEFINED
)
556 rSearchOpt
.transliterateFlags
|= TransliterationFlags::IGNORE_CASE
;
557 if (rTarget
.bWholeWords
)
558 rSearchOpt
.searchFlag
|= util::SearchFlags::NORM_WORD_ONLY
;
561 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */