LibreOffice Module xmloff (master)  1
XMLGraphicsDefaultStyle.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 
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 
26 #include <tools/color.hxx>
27 
28 #include <xmloff/xmlimp.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/xmltoken.hxx>
31 #include <xmloff/families.hxx>
32 #include <xmloff/xmltypes.hxx>
33 #include <xmloff/maptype.hxx>
34 #include <xmloff/xmlimppr.hxx>
35 #include <xmloff/xmlprmap.hxx>
36 
38 #include <algorithm>
39 
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::xml::sax;
45 
50 
51 
52 XMLGraphicsDefaultStyle::XMLGraphicsDefaultStyle( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const Reference< XAttributeList >& xAttrList, SvXMLStylesContext& rStyles )
53 : XMLPropStyleContext( rImport, nPrfx, rLName, xAttrList, rStyles, XmlStyleFamily::SD_GRAPHICS_ID, true )
54 {
55 }
56 
58 {
59 }
60 
61 SvXMLImportContextRef XMLGraphicsDefaultStyle::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList > & xAttrList )
62 {
63  SvXMLImportContextRef xContext;
64 
65  if( XML_NAMESPACE_STYLE == nPrefix || XML_NAMESPACE_LO_EXT == nPrefix )
66  {
67  sal_uInt32 nFamily = 0;
68  if( IsXMLToken( rLocalName, XML_TEXT_PROPERTIES ) )
69  nFamily = XML_TYPE_PROP_TEXT;
70  else if( IsXMLToken( rLocalName, XML_PARAGRAPH_PROPERTIES ) )
71  nFamily = XML_TYPE_PROP_PARAGRAPH;
72  else if( IsXMLToken( rLocalName, XML_GRAPHIC_PROPERTIES ) )
73  nFamily = XML_TYPE_PROP_GRAPHIC;
74  if( nFamily )
75  {
77  if( xImpPrMap.is() )
78  xContext = new XMLShapePropertySetContext( GetImport(), nPrefix, rLocalName, xAttrList, nFamily, GetProperties(), xImpPrMap );
79  }
80  }
81 
82  if (!xContext)
83  xContext = XMLPropStyleContext::CreateChildContext( nPrefix, rLocalName, xAttrList );
84 
85  return xContext;
86 }
87 
88 namespace {
89 
90 struct XMLPropertyByIndex {
91  sal_Int32 const m_nIndex;
92  explicit XMLPropertyByIndex(sal_Int32 const nIndex) : m_nIndex(nIndex) {}
93  bool operator()(XMLPropertyState const& rProp) {
94  return m_nIndex == rProp.mnIndex;
95  }
96 };
97 
98 }
99 
100 // This method is called for every default style
102 {
103  Reference< XMultiServiceFactory > xFact( GetImport().GetModel(), UNO_QUERY );
104  if( !xFact.is() )
105  return;
106 
107  Reference< XPropertySet > xDefaults( xFact->createInstance( "com.sun.star.drawing.Defaults" ), UNO_QUERY );
108  if( !xDefaults.is() )
109  return;
110  // SJ: #i114750#
111  bool bWordWrapDefault = true; // initializing with correct ODF fo:wrap-option default
112  sal_Int32 nUPD( 0 );
113  sal_Int32 nBuild( 0 );
114  const bool bBuildIdFound = GetImport().getBuildIds( nUPD, nBuild );
115  if ( bBuildIdFound && (
116  ((nUPD >= 600) && (nUPD < 700))
117  ||
118  ((nUPD == 300) && (nBuild <= 9535))
119  ||
120  ((nUPD > 300) && (nUPD <= 330))
121  ) )
122  bWordWrapDefault = false;
123 
124  const OUString sTextWordWrap( "TextWordWrap" );
125  Reference< XPropertySetInfo > xInfo( xDefaults->getPropertySetInfo() );
126  if ( xInfo->hasPropertyByName( sTextWordWrap ) )
127  xDefaults->setPropertyValue( sTextWordWrap, Any( bWordWrapDefault ) );
128 
129  if (GetImport().IsOOoXML()
130  && xInfo->hasPropertyByName("IsFollowingTextFlow"))
131  {
132  // OOo 1.x only supported "true" so that is the more appropriate
133  // default for OOoXML format documents.
134  xDefaults->setPropertyValue("IsFollowingTextFlow", uno::makeAny(true));
135  }
136 
137  // NOTE: the only reason why it's legal to check "==" (not "<") against
138  // arbitrary versions here is that the default value of these attributes
139  // is not defined by ODF, therefore it is implementation-defined
140  // (and we of course must not override any attributes that are actually
141  // in the document, so check for that)
142  bool const bIsAOO4(
143  GetImport().getGeneratorVersion() >= SvXMLImport::AOO_40x
144  && GetImport().getGeneratorVersion() <= SvXMLImport::AOO_4x);
145 
146  // fdo#75872: backward compatibility for pool defaults change
147  if (GetImport().isGeneratorVersionOlderThan(
149  // argh... it turns out that LO has also changed defaults for these
150  // since LO 4.0, and so even the _new_ AOO 4.0+ default needs
151  // special handling since AOO still does _not_ write it into the file
152  || bIsAOO4)
153  {
154  rtl::Reference<XMLPropertySetMapper> const pImpPrMap(
155  GetStyles()->GetImportPropertyMapper(GetFamily())
156  ->getPropertySetMapper());
157  sal_Int32 const nStrokeIndex(
158  pImpPrMap->GetEntryIndex(XML_NAMESPACE_SVG, "stroke-color", 0));
159  if (std::none_of(GetProperties().begin(), GetProperties().end(),
160  XMLPropertyByIndex(nStrokeIndex)))
161  {
162  Color const nStroke(
163  bIsAOO4 ? Color(128, 128, 128) : COL_BLACK);
164  xDefaults->setPropertyValue("LineColor", makeAny(nStroke));
165  }
166  Color const nFillColor( bIsAOO4
167  ? Color(0xCF, 0xE7, 0xF5) : Color(153, 204, 255));
168  sal_Int32 const nFillIndex(
169  pImpPrMap->GetEntryIndex(XML_NAMESPACE_DRAW, "fill-color", 0));
170  if (std::none_of(GetProperties().begin(), GetProperties().end(),
171  XMLPropertyByIndex(nFillIndex)))
172  {
173  xDefaults->setPropertyValue("FillColor", makeAny(nFillColor));
174  }
175  if (xInfo->hasPropertyByName("FillColor2"))
176  {
177  sal_Int32 const nFill2Index(pImpPrMap->GetEntryIndex(
178  XML_NAMESPACE_DRAW, "secondary-fill-color", 0));
179  if (std::none_of(GetProperties().begin(), GetProperties().end(),
180  XMLPropertyByIndex(nFill2Index)))
181  {
182  xDefaults->setPropertyValue("FillColor2", makeAny(sal_Int32(nFillColor)));
183  }
184  }
185  }
186 
187  FillPropertySet( xDefaults );
188 }
189 
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr sal_uInt16 XML_NAMESPACE_LO_EXT
Definition: xmlnmspe.hxx:80
XMLGraphicsDefaultStyle(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, SvXMLStylesContext &rStyles)
XmlStyleFamily GetFamily() const
Definition: xmlstyle.hxx:112
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3422
constexpr sal_uInt16 XML_NAMESPACE_STYLE
Definition: xmlnmspe.hxx:30
constexpr::Color COL_BLACK(0x00, 0x00, 0x00)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
sal_Int32 mnIndex
Definition: maptype.hxx:106
static const sal_uInt16 AOO_40x
Definition: xmlimp.hxx:544
XmlStyleFamily
Definition: families.hxx:47
#define XML_TYPE_PROP_PARAGRAPH
Definition: xmltypes.hxx:99
enumrange< T >::Iterator begin(enumrange< T >)
static const sal_uInt16 AOO_4x
Definition: xmlimp.hxx:550
constexpr sal_uInt16 XML_NAMESPACE_DRAW
Definition: xmlnmspe.hxx:33
virtual rtl::Reference< SvXMLImportPropertyMapper > GetImportPropertyMapper(XmlStyleFamily nFamily) const
Definition: xmlstyle.cxx:580
virtual void FillPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: prstylei.cxx:231
bool getBuildIds(sal_Int32 &rUPD, sal_Int32 &rBuild) const
returns the upd and build id (f.e.
Definition: xmlimp.cxx:1910
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
::std::vector< XMLPropertyState > & GetProperties()
Definition: prstylei.hxx:79
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
Definition: prstylei.cxx:178
#define XML_TYPE_PROP_TEXT
Definition: xmltypes.hxx:98
static const sal_uInt16 LO_42x
Definition: xmlimp.hxx:554
enumrange< T >::Iterator end(enumrange< T >)
virtual ~XMLGraphicsDefaultStyle() override
#define XML_TYPE_PROP_GRAPHIC
Definition: xmltypes.hxx:94
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:104
SvXMLStylesContext * GetStyles()
Definition: prstylei.hxx:78
constexpr sal_uInt16 XML_NAMESPACE_SVG
Definition: xmlnmspe.hxx:40
virtual void SetDefaults() override
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)