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