LibreOffice Module xmloff (master) 1
PageMasterImportContext.cxx
Go to the documentation of this file.
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
21#include <xmloff/xmlimppr.hxx>
23#include <xmloff/xmlprmap.hxx>
24#include <xmloff/xmltoken.hxx>
25#include "PageMasterPropHdl.hxx"
29#include <com/sun/star/frame/XModel.hpp>
30#include <com/sun/star/lang/XMultiServiceFactory.hpp>
31#include <osl/diagnose.h>
32
33//
34#include <com/sun/star/beans/XPropertySet.hpp>
35#include <com/sun/star/beans/XPropertySetInfo.hpp>
36#include <com/sun/star/drawing/FillStyle.hpp>
37#include <com/sun/star/drawing/BitmapMode.hpp>
38#include <xmloff/xmlerror.hxx>
40
41using namespace ::com::sun::star;
42using namespace ::xmloff::token;
43using namespace ::com::sun::star::uno;
44using namespace ::com::sun::star::lang;
45
46//
47using namespace ::com::sun::star::beans;
48
49void PageStyleContext::SetAttribute( sal_Int32 nElement,
50 const OUString& rValue )
51{
52 if( nElement == XML_ELEMENT(STYLE, XML_PAGE_USAGE) )
53 {
54 sPageUsage = rValue;
55 }
56 else
57 {
58 XMLPropStyleContext::SetAttribute( nElement, rValue );
59 }
60}
61
62
64 SvXMLStylesContext& rStyles,
65 bool bDefaultStyle) :
66 XMLPropStyleContext( rImport, rStyles, XmlStyleFamily::PAGE_MASTER, bDefaultStyle),
67 m_bIsFillStyleAlreadyConverted(false) //
68{
69}
70
72{
73}
74
75css::uno::Reference< css::xml::sax::XFastContextHandler > PageStyleContext::createFastChildContext(
76 sal_Int32 nElement,
77 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
78{
79 if( nElement == XML_ELEMENT(STYLE, XML_HEADER_STYLE) ||
80 nElement == XML_ELEMENT(STYLE, XML_FOOTER_STYLE) )
81 {
82 bool bHeader = nElement == XML_ELEMENT(STYLE, XML_HEADER_STYLE);
85 if( xImpPrMap.is() )
86 {
87 const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
88 sal_Int32 nFlag;
89 if (bHeader)
90 nFlag = CTF_PM_HEADERFLAG;
91 else
92 nFlag = CTF_PM_FOOTERFLAG;
93 sal_Int32 nStartIndex (-1);
94 sal_Int32 nEndIndex (-1);
95 bool bFirst(false);
96 bool bEnd(false);
97 sal_Int32 nIndex = 0;
98 while ( nIndex < rMapper->GetEntryCount() && !bEnd)
99 {
100 if ((rMapper->GetEntryContextId( nIndex ) & CTF_PM_FLAGMASK) == nFlag)
101 {
102 if (!bFirst)
103 {
104 bFirst = true;
105 nStartIndex = nIndex;
106 }
107 }
108 else if (bFirst)
109 {
110 bEnd = true;
111 nEndIndex = nIndex;
112 }
113 nIndex++;
114 }
115 if (!bEnd)
116 nEndIndex = nIndex;
118 GetProperties(), xImpPrMap, nStartIndex, nEndIndex, bHeader);
119 }
120 }
121
123 {
126 if( xImpPrMap.is() )
127 {
128 const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
129 sal_Int32 nEndIndex (-1);
130 bool bEnd(false);
131 sal_Int32 nIndex = 0;
132 sal_Int16 nContextID;
133 while ( nIndex < rMapper->GetEntryCount() && !bEnd)
134 {
135 nContextID = rMapper->GetEntryContextId( nIndex );
136 if (nContextID && ((nContextID & CTF_PM_FLAGMASK) != XML_PM_CTF_START))
137 {
138 nEndIndex = nIndex;
139 bEnd = true;
140 }
141 nIndex++;
142 }
143 if (!bEnd)
144 nEndIndex = nIndex;
145 return new PagePropertySetContext( GetImport(), nElement,
146 xAttrList,
149 xImpPrMap, 0, nEndIndex, Page);
150 }
151 }
152
153 return XMLPropStyleContext::createFastChildContext(nElement, xAttrList);
154}
155
156void PageStyleContext::FillPropertySet(const uno::Reference<beans::XPropertySet > &)
157{
158 assert(false); // don't call this virtual, call function below
159}
160
162 const uno::Reference<beans::XPropertySet> & xPropSet,
163 XMLPropStyleContext *const pDrawingPageStyle)
164{
165 // need to filter out old fill definitions when the new ones are used. The new
166 // ones are used when a FillStyle is defined
168 {
169 static constexpr OUStringLiteral s_FillStyle(u"FillStyle");
170 static constexpr OUStringLiteral s_HeaderFillStyle(u"HeaderFillStyle");
171 static constexpr OUStringLiteral s_FooterFillStyle(u"FooterFillStyle");
172
173 // note: the function must only check by property name, not any id/flag!
175 || (pDrawingPageStyle && pDrawingPageStyle->doNewDrawingLayerFillStyleDefinitionsExist(s_FillStyle)))
176 {
178 }
179
180 if(doNewDrawingLayerFillStyleDefinitionsExist(s_HeaderFillStyle))
181 {
183 }
184
185 if(doNewDrawingLayerFillStyleDefinitionsExist(s_FooterFillStyle))
186 {
188 }
189
191 }
192
193 // do not use XMLPropStyleContext::FillPropertySet, we need to handle this ourselves since
194 // we have properties which use the MID_FLAG_NO_PROPERTY_IMPORT flag since they need some special
195 // handling
197
198 if(xImpPrMap.is())
199 {
200 // properties that need special handling because they need the used name to be translated first
201 struct ContextID_Index_Pair aContextIDs[] =
202 {
203 { CTF_PM_FILLGRADIENTNAME, -1, drawing::FillStyle::FillStyle_GRADIENT },
204 { CTF_PM_FILLTRANSNAME, -1, drawing::FillStyle::FillStyle_MAKE_FIXED_SIZE },
205 { CTF_PM_FILLHATCHNAME, -1, drawing::FillStyle::FillStyle_HATCH },
206 { CTF_PM_FILLBITMAPNAME, -1, drawing::FillStyle::FillStyle_BITMAP },
207
208 // also need to special handling for header entries
209 { CTF_PM_HEADERFILLGRADIENTNAME, -1, drawing::FillStyle::FillStyle_GRADIENT },
210 { CTF_PM_HEADERFILLTRANSNAME, -1, drawing::FillStyle::FillStyle_MAKE_FIXED_SIZE },
211 { CTF_PM_HEADERFILLHATCHNAME, -1, drawing::FillStyle::FillStyle_HATCH },
212 { CTF_PM_HEADERFILLBITMAPNAME, -1, drawing::FillStyle::FillStyle_BITMAP },
213
214 // also need to special handling for footer entries
215 { CTF_PM_FOOTERFILLGRADIENTNAME, -1, drawing::FillStyle::FillStyle_GRADIENT },
216 { CTF_PM_FOOTERFILLTRANSNAME, -1, drawing::FillStyle::FillStyle_MAKE_FIXED_SIZE },
217 { CTF_PM_FOOTERFILLHATCHNAME, -1, drawing::FillStyle::FillStyle_HATCH },
218 { CTF_PM_FOOTERFILLBITMAPNAME, -1, drawing::FillStyle::FillStyle_BITMAP },
219
220 {-1, -1, drawing::FillStyle::FillStyle_GRADIENT}
221 };
222
223 // the style families associated with the same index modulo 4
224 static const XmlStyleFamily aFamilies[] =
225 {
230 };
231
232 // Fill PropertySet, but let it handle special properties not itself
233 xImpPrMap->FillPropertySet(GetProperties(), xPropSet, aContextIDs);
234
235 // get property set mapper
236 const rtl::Reference< XMLPropertySetMapper >& rMapper = xImpPrMap->getPropertySetMapper();
237 Reference<XPropertySetInfo> const xInfo(xPropSet->getPropertySetInfo());
238
239 // don't look at the attributes, look at the property, could
240 // theoretically be inherited and we don't want to delete erroneously
241 drawing::FillStyle fillStyle{drawing::FillStyle_NONE};
242 drawing::FillStyle fillStyleHeader{drawing::FillStyle_NONE};
243 drawing::FillStyle fillStyleFooter{drawing::FillStyle_NONE};
244 if (xInfo->hasPropertyByName("FillStyle")) // SwXTextDefaults lacks it?
245 {
246 xPropSet->getPropertyValue("FillStyle") >>= fillStyle;
247 xPropSet->getPropertyValue("HeaderFillStyle") >>= fillStyleHeader;
248 xPropSet->getPropertyValue("FooterFillStyle") >>= fillStyleFooter;
249 }
250
251 // handle special attributes which have MID_FLAG_NO_PROPERTY_IMPORT set
252 for(sal_uInt16 i = 0; aContextIDs[i].nContextID != -1; i++)
253 {
254 sal_Int32 nIndex = aContextIDs[i].nIndex;
255
256 if(nIndex != -1)
257 {
258 drawing::FillStyle const* pFillStyle(nullptr);
259 switch(aContextIDs[i].nContextID)
260 {
265 pFillStyle = &fillStyle;
266 [[fallthrough]];
271 if (!pFillStyle) { pFillStyle = &fillStyleHeader; }
272 [[fallthrough]];
277 {
278 if (!pFillStyle) { pFillStyle = &fillStyleFooter; }
279 struct XMLPropertyState& rState = GetProperties()[nIndex];
280 OUString sStyleName;
281 rState.maValue >>= sStyleName;
282
283 if (aContextIDs[i].nExpectedFillStyle != drawing::FillStyle::FillStyle_MAKE_FIXED_SIZE
284 && aContextIDs[i].nExpectedFillStyle != *pFillStyle)
285 {
286 SAL_INFO("xmloff.style", "PageStyleContext: dropping fill named item: " << sStyleName);
287 break; // ignore it, it's not used
288 }
289 // translate the used name from ODF intern to the name used in the Model
290 sStyleName = GetImport().GetStyleDisplayName(aFamilies[i%4], sStyleName);
291
292 try
293 {
294 // set property
295 const OUString& rPropertyName = rMapper->GetEntryAPIName(rState.mnIndex);
296
297 if(xInfo->hasPropertyByName(rPropertyName))
298 {
299 xPropSet->setPropertyValue(rPropertyName,Any(sStyleName));
300 }
301 }
302 catch(css::lang::IllegalArgumentException& e)
303 {
304 Sequence<OUString> aSeq { sStyleName };
305 GetImport().SetError(
307 aSeq,e.Message,nullptr);
308 }
309 break;
310 }
311 }
312 }
313 }
314 }
315 else
316 {
317 OSL_ENSURE(xImpPrMap.is(), "Got no SvXMLImportPropertyMapper (!)");
318 }
319
320 // pDrawingPageStyle overrides this
321 if (pDrawingPageStyle)
322 {
323 pDrawingPageStyle->FillPropertySet(xPropSet);
324 }
325 // horrible heuristic to guess BackgroundFullSize for Writer < 7.0
326 else if (!IsDefaultStyle() // ignore pool default, only fix existing styles
327 && (GetImport().isGeneratorVersionOlderThan(SvXMLImport::AOO_4x, SvXMLImport::LO_7x)
328 // also for AOO 4.x, assume there won't ever be a 4.2
329 || GetImport().getGeneratorVersion() == SvXMLImport::AOO_4x))
330 {
331 bool isFullSize(true); // default is current LO default
332 drawing::FillStyle fillStyle{drawing::FillStyle_NONE};
333 xPropSet->getPropertyValue("FillStyle") >>= fillStyle;
334 if (GetImport().isGeneratorVersionOlderThan(SvXMLImport::AOO_4x, SvXMLImport::LO_63x)
335 // also for AOO 4.x, assume there won't ever be a 4.2
336 || GetImport().getGeneratorVersion() == SvXMLImport::AOO_4x)
337 {
338 // before LO 6.3, always inside the margins (but ignore it if NONE)
339 if (fillStyle != drawing::FillStyle_NONE)
340 {
341 isFullSize = false;
342 }
343 }
344 else
345 {
346 // LO 6.3/6.4: guess depending on fill style/bitmap mode
347 // this should work even if the document doesn't contain fill style
348 // but only old background attributes
349 // (can't use the aContextIDs stuff above because that requires
350 // re-routing through handleSpecialItem())
351 switch (fillStyle)
352 {
353 case drawing::FillStyle_NONE:
354 break;
355 case drawing::FillStyle_SOLID:
356 case drawing::FillStyle_GRADIENT:
357 case drawing::FillStyle_HATCH:
358 isFullSize = true;
359 break;
360 case drawing::FillStyle_BITMAP:
361 {
362 drawing::BitmapMode bitmapMode{};
363 xPropSet->getPropertyValue("FillBitmapMode") >>= bitmapMode;
364 switch (bitmapMode)
365 {
366 case drawing::BitmapMode_REPEAT:
367 isFullSize = true;
368 break;
369 case drawing::BitmapMode_STRETCH:
370 case drawing::BitmapMode_NO_REPEAT:
371 isFullSize = false;
372 break;
373 default:
374 assert(false);
375 }
376 }
377 break;
378 default:
379 assert(false);
380 }
381 }
382 // set it explicitly if it's not the default
383 if (!isFullSize)
384 {
385 SAL_INFO("xmloff.style", "FillPropertySet_PageStyle: Heuristically resetting BackgroundFullSize");
386 xPropSet->setPropertyValue("BackgroundFullSize", uno::Any(isFullSize));
387 }
388 }
389
390 // old code, replaced by above stuff
391 // XMLPropStyleContext::FillPropertySet(rPropSet);
392
393 if (!sPageUsage.isEmpty())
394 {
395 uno::Any aPageUsage;
396 XMLPMPropHdl_PageStyleLayout aPageUsageHdl;
397 if (aPageUsageHdl.importXML(sPageUsage, aPageUsage, GetImport().GetMM100UnitConverter()))
398 xPropSet->setPropertyValue("PageStyleLayout", aPageUsage);
399 }
400}
401
403{
404 { CTF_PM_FILLGRADIENTNAME, -1, drawing::FillStyle::FillStyle_GRADIENT },
405 { CTF_PM_FILLTRANSNAME, -1, drawing::FillStyle::FillStyle_MAKE_FIXED_SIZE },
406 { CTF_PM_FILLHATCHNAME, -1, drawing::FillStyle::FillStyle_HATCH },
407 { CTF_PM_FILLBITMAPNAME, -1, drawing::FillStyle::FillStyle_BITMAP },
408
409 {-1, -1, drawing::FillStyle::FillStyle_MAKE_FIXED_SIZE}
410};
411
413{
418};
419
420// text grid enhancement for better CJK support
421//set default page layout style
423{
424 Reference < XMultiServiceFactory > xFactory ( GetImport().GetModel(), UNO_QUERY);
425 if (xFactory.is())
426 {
427 Reference < XInterface > xInt = xFactory->createInstance( "com.sun.star.text.Defaults" );
428 Reference < beans::XPropertySet > xProperties ( xInt, UNO_QUERY );
429 if ( xProperties.is() )
430 FillPropertySet_PageStyle(xProperties, nullptr);
431 }
432}
433
434/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XmlStyleFamily const g_MasterPageFamilies[]
ContextID_Index_Pair const g_MasterPageContextIDs[]
#define CTF_PM_FILLTRANSNAME
#define CTF_PM_HEADERFILLHATCHNAME
#define CTF_PM_FILLHATCHNAME
#define CTF_PM_HEADERFLAG
#define CTF_PM_FLAGMASK
#define CTF_PM_FOOTERFILLHATCHNAME
#define CTF_PM_HEADERFILLGRADIENTNAME
#define CTF_PM_FILLBITMAPNAME
#define CTF_PM_HEADERFILLTRANSNAME
#define CTF_PM_FOOTERFILLTRANSNAME
#define CTF_PM_FOOTERFILLBITMAPNAME
#define CTF_PM_HEADERFILLBITMAPNAME
#define CTF_PM_FOOTERFILLGRADIENTNAME
#define CTF_PM_FILLGRADIENTNAME
#define CTF_PM_FOOTERFLAG
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
void FillPropertySet_PageStyle(const css::uno::Reference< css::beans::XPropertySet > &rPropSet, XMLPropStyleContext *pDrawingPageStyle)
virtual void SetDefaults() override
virtual ~PageStyleContext() override
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet) override
PageStyleContext(SvXMLImport &rImport, SvXMLStylesContext &rStyles, bool bDefaultStyle)
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
XmlStyleFamily GetFamily() const
Definition: xmlstyle.hxx:85
bool IsDefaultStyle() const
Definition: xmlstyle.hxx:110
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
Definition: xmlstyle.cxx:522
virtual bool importXML(const OUString &rStrImpValue, css::uno::Any &rValue, const SvXMLUnitConverter &rUnitConverter) const override
Imports the given value according to the XML-data-type corresponding to the derived class.
bool doNewDrawingLayerFillStyleDefinitionsExist(std::u16string_view rFillStyleTag) const
Definition: prstylei.cxx:543
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:222
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
Definition: prstylei.cxx:175
::std::vector< XMLPropertyState > & GetProperties()
Definition: prstylei.hxx:79
void deactivateOldFillStyleDefinitions(const OldFillStyleDefinitionSet &rHashSetOfTags)
Definition: prstylei.cxx:589
static const OldFillStyleDefinitionSet & getHeaderSet()
Definition: prstylei.cxx:165
static const OldFillStyleDefinitionSet & getStandardSet()
Definition: prstylei.cxx:160
static const OldFillStyleDefinitionSet & getFooterSet()
Definition: prstylei.cxx:170
SvXMLStylesContext * GetStyles()
Definition: prstylei.hxx:78
virtual void SetAttribute(sal_Int32 nElement, const OUString &rValue) override
Definition: prstylei.cxx:57
#define XML_PM_CTF_START
Definition: contextid.hxx:30
float u
Reference< XSingleServiceFactory > xFactory
XmlStyleFamily
Definition: families.hxx:50
sal_Int32 nIndex
Sequence< sal_Int8 > aSeq
#define SAL_INFO(area, stream)
enum SAL_DLLPUBLIC_RTTI FillStyle
Definition: xmlimppr.hxx:42
int i
Handling of tokens in XML:
@ XML_PAGE_LAYOUT_PROPERTIES
Definition: xmltoken.hxx:2631
This struct is used as an optional parameter to the static _FillPropertySet() methods.
Definition: xmlimppr.hxx:58
sal_Int16 nContextID
Definition: xmlimppr.hxx:59
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:140
css::uno::Any maValue
Definition: maptype.hxx:142
sal_Int32 mnIndex
Definition: maptype.hxx:141
Page
#define XMLERROR_STYLE_PROP_VALUE
Definition: xmlerror.hxx:68
#define XMLERROR_FLAG_WARNING
Definition: xmlerror.hxx:35
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
#define XML_TYPE_PROP_PAGE_LAYOUT
Definition: xmltypes.hxx:96