Bump version to 24.04.3.4
[LibreOffice.git] / xmloff / source / draw / XMLGraphicsDefaultStyle.cxx
blobde6b50a1471ecdb2dc33d2b92336b6d965c79a78
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <xmloff/XMLGraphicsDefaultStyle.hxx>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <tools/color.hxx>
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>
37 #include <XMLShapePropertySetContext.hxx>
38 #include <algorithm>
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;
46 using ::xmloff::token::XML_TEXT_PROPERTIES;
47 using ::xmloff::token::XML_GRAPHIC_PROPERTIES;
48 using ::xmloff::token::XML_PARAGRAPH_PROPERTIES;
51 XMLGraphicsDefaultStyle::XMLGraphicsDefaultStyle( SvXMLImport& rImport, SvXMLStylesContext& rStyles )
52 : XMLPropStyleContext( rImport, rStyles, XmlStyleFamily::SD_GRAPHICS_ID, true )
56 XMLGraphicsDefaultStyle::~XMLGraphicsDefaultStyle()
60 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLGraphicsDefaultStyle::createFastChildContext(
61 sal_Int32 nElement,
62 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
64 if( IsTokenInNamespace(nElement, XML_NAMESPACE_STYLE) ||
65 IsTokenInNamespace(nElement, XML_NAMESPACE_LO_EXT) )
67 sal_Int32 nLocalName = nElement & TOKEN_MASK;
68 sal_uInt32 nFamily = 0;
69 if( nLocalName == XML_TEXT_PROPERTIES )
70 nFamily = XML_TYPE_PROP_TEXT;
71 else if( nLocalName == XML_PARAGRAPH_PROPERTIES )
72 nFamily = XML_TYPE_PROP_PARAGRAPH;
73 else if( nLocalName == XML_GRAPHIC_PROPERTIES )
74 nFamily = XML_TYPE_PROP_GRAPHIC;
75 if( nFamily )
77 rtl::Reference < SvXMLImportPropertyMapper > xImpPrMap = GetStyles()->GetImportPropertyMapper( GetFamily() );
78 if( xImpPrMap.is() )
79 return new XMLShapePropertySetContext( GetImport(), nElement, xAttrList, nFamily, GetProperties(), xImpPrMap );
83 return XMLPropStyleContext::createFastChildContext( nElement, xAttrList );
86 namespace {
88 struct XMLPropertyByIndex {
89 sal_Int32 const m_nIndex;
90 explicit XMLPropertyByIndex(sal_Int32 const nIndex) : m_nIndex(nIndex) {}
91 bool operator()(XMLPropertyState const& rProp) {
92 return m_nIndex == rProp.mnIndex;
98 // This method is called for every default style
99 void XMLGraphicsDefaultStyle::SetDefaults()
101 Reference< XMultiServiceFactory > xFact( GetImport().GetModel(), UNO_QUERY );
102 if( !xFact.is() )
103 return;
105 Reference< XPropertySet > xDefaults( xFact->createInstance( "com.sun.star.drawing.Defaults" ), UNO_QUERY );
106 if( !xDefaults.is() )
107 return;
108 // SJ: #i114750#
109 bool bWordWrapDefault = true; // initializing with correct ODF fo:wrap-option default
110 sal_Int32 nUPD( 0 );
111 sal_Int32 nBuild( 0 );
112 const bool bBuildIdFound = GetImport().getBuildIds( nUPD, nBuild );
113 if ( bBuildIdFound && (
114 ((nUPD >= 600) && (nUPD < 700))
116 ((nUPD == 300) && (nBuild <= 9535))
118 ((nUPD > 300) && (nUPD <= 330))
120 bWordWrapDefault = false;
122 static constexpr OUString sTextWordWrap( u"TextWordWrap"_ustr );
123 Reference< XPropertySetInfo > xInfo( xDefaults->getPropertySetInfo() );
124 if ( xInfo->hasPropertyByName( sTextWordWrap ) )
125 xDefaults->setPropertyValue( sTextWordWrap, Any( bWordWrapDefault ) );
127 if (GetImport().IsOOoXML()
128 && xInfo->hasPropertyByName("IsFollowingTextFlow"))
130 // OOo 1.x only supported "true" so that is the more appropriate
131 // default for OOoXML format documents.
132 xDefaults->setPropertyValue("IsFollowingTextFlow", uno::Any(true));
135 // NOTE: the only reason why it's legal to check "==" (not "<") against
136 // arbitrary versions here is that the default value of these attributes
137 // is not defined by ODF, therefore it is implementation-defined
138 // (and we of course must not override any attributes that are actually
139 // in the document, so check for that)
140 bool const bIsAOO4(
141 GetImport().getGeneratorVersion() >= SvXMLImport::AOO_40x
142 && GetImport().getGeneratorVersion() <= SvXMLImport::AOO_4x);
144 // fdo#75872: backward compatibility for pool defaults change
145 if (GetImport().isGeneratorVersionOlderThan(
146 SvXMLImport::AOO_40x, SvXMLImport::LO_42x)
147 // argh... it turns out that LO has also changed defaults for these
148 // since LO 4.0, and so even the _new_ AOO 4.0+ default needs
149 // special handling since AOO still does _not_ write it into the file
150 || bIsAOO4)
152 rtl::Reference<XMLPropertySetMapper> const pImpPrMap(
153 GetStyles()->GetImportPropertyMapper(GetFamily())
154 ->getPropertySetMapper());
155 sal_Int32 const nStrokeIndex(
156 pImpPrMap->GetEntryIndex(XML_NAMESPACE_SVG, u"stroke-color", 0));
157 if (std::none_of(GetProperties().begin(), GetProperties().end(),
158 XMLPropertyByIndex(nStrokeIndex)))
160 Color const nStroke(
161 bIsAOO4 ? Color(128, 128, 128) : COL_BLACK);
162 xDefaults->setPropertyValue("LineColor", Any(nStroke));
164 Color const nFillColor( bIsAOO4
165 ? Color(0xCF, 0xE7, 0xF5) : Color(153, 204, 255));
166 sal_Int32 const nFillIndex(
167 pImpPrMap->GetEntryIndex(XML_NAMESPACE_DRAW, u"fill-color", 0));
168 if (std::none_of(GetProperties().begin(), GetProperties().end(),
169 XMLPropertyByIndex(nFillIndex)))
171 xDefaults->setPropertyValue("FillColor", Any(nFillColor));
173 if (xInfo->hasPropertyByName("FillColor2"))
175 sal_Int32 const nFill2Index(pImpPrMap->GetEntryIndex(
176 XML_NAMESPACE_DRAW, u"secondary-fill-color", 0));
177 if (std::none_of(GetProperties().begin(), GetProperties().end(),
178 XMLPropertyByIndex(nFill2Index)))
180 xDefaults->setPropertyValue("FillColor2", Any(sal_Int32(nFillColor)));
185 FillPropertySet( xDefaults );
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */