Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/draw/XMLGraphicsDefaultStyle.cxx
Line
Count
Source
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
20
#include <xmloff/XMLGraphicsDefaultStyle.hxx>
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
37
#include <XMLShapePropertySetContext.hxx>
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
46
using ::xmloff::token::XML_TEXT_PROPERTIES;
47
using ::xmloff::token::XML_GRAPHIC_PROPERTIES;
48
using ::xmloff::token::XML_PARAGRAPH_PROPERTIES;
49
50
51
XMLGraphicsDefaultStyle::XMLGraphicsDefaultStyle( SvXMLImport& rImport, SvXMLStylesContext& rStyles )
52
3.68k
: XMLPropStyleContext( rImport, rStyles, XmlStyleFamily::SD_GRAPHICS_ID, true )
53
3.68k
{
54
3.68k
}
55
56
XMLGraphicsDefaultStyle::~XMLGraphicsDefaultStyle()
57
3.68k
{
58
3.68k
}
59
60
css::uno::Reference< css::xml::sax::XFastContextHandler > XMLGraphicsDefaultStyle::createFastChildContext(
61
    sal_Int32 nElement,
62
    const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
63
11.4k
{
64
11.4k
    if( IsTokenInNamespace(nElement, XML_NAMESPACE_STYLE) ||
65
614
        IsTokenInNamespace(nElement, XML_NAMESPACE_LO_EXT) )
66
10.8k
    {
67
10.8k
        sal_Int32 nLocalName = nElement & TOKEN_MASK;
68
10.8k
        sal_uInt32 nFamily = 0;
69
10.8k
        if( nLocalName == XML_TEXT_PROPERTIES )
70
3.59k
            nFamily = XML_TYPE_PROP_TEXT;
71
7.21k
        else if( nLocalName == XML_PARAGRAPH_PROPERTIES )
72
3.63k
            nFamily = XML_TYPE_PROP_PARAGRAPH;
73
3.57k
        else if( nLocalName == XML_GRAPHIC_PROPERTIES )
74
3.12k
            nFamily = XML_TYPE_PROP_GRAPHIC;
75
10.8k
        if( nFamily )
76
10.3k
        {
77
10.3k
            SvXMLImportPropertyMapper* pImpPrMap = GetStyles()->GetImportPropertyMapper( GetFamily() );
78
10.3k
            if( pImpPrMap )
79
10.3k
                return new XMLShapePropertySetContext( GetImport(), nElement, xAttrList, nFamily, GetProperties(), pImpPrMap );
80
10.3k
        }
81
10.8k
    }
82
83
1.06k
    return XMLPropStyleContext::createFastChildContext( nElement, xAttrList );
84
11.4k
}
85
86
namespace {
87
88
struct XMLPropertyByIndex {
89
    sal_Int32 const m_nIndex;
90
1.50k
    explicit XMLPropertyByIndex(sal_Int32 const nIndex) : m_nIndex(nIndex) {}
91
13.3k
    bool operator()(XMLPropertyState const& rProp) {
92
13.3k
        return m_nIndex == rProp.mnIndex;
93
13.3k
    }
94
};
95
96
}
97
98
// This method is called for every default style
99
void XMLGraphicsDefaultStyle::SetDefaults()
100
2.65k
{
101
2.65k
    Reference< XMultiServiceFactory > xFact( GetImport().GetModel(), UNO_QUERY );
102
2.65k
    if( !xFact.is() )
103
0
        return;
104
105
2.65k
    Reference< XPropertySet > xDefaults( xFact->createInstance( u"com.sun.star.drawing.Defaults"_ustr ), UNO_QUERY );
106
2.65k
    if( !xDefaults.is() )
107
0
        return;
108
                                            // SJ: #i114750#
109
2.65k
    bool bWordWrapDefault = true;   // initializing with correct ODF fo:wrap-option default
110
2.65k
    sal_Int32 nUPD( 0 );
111
2.65k
    sal_Int32 nBuild( 0 );
112
2.65k
    const bool bBuildIdFound = GetImport().getBuildIds( nUPD, nBuild );
113
2.65k
    if ( bBuildIdFound && (
114
94
        ((nUPD >= 600) &&  (nUPD < 700))
115
94
        ||
116
94
        ((nUPD == 300) && (nBuild <= 9535))
117
94
        ||
118
94
        ((nUPD > 300) && (nUPD <= 330))
119
94
    ) )
120
28
        bWordWrapDefault = false;
121
122
2.65k
    static constexpr OUString sTextWordWrap( u"TextWordWrap"_ustr );
123
2.65k
    Reference< XPropertySetInfo > xInfo( xDefaults->getPropertySetInfo() );
124
2.65k
    if ( xInfo->hasPropertyByName( sTextWordWrap ) )
125
2.65k
        xDefaults->setPropertyValue( sTextWordWrap, Any( bWordWrapDefault ) );
126
127
2.65k
    if (GetImport().IsOOoXML()
128
0
        && xInfo->hasPropertyByName(u"IsFollowingTextFlow"_ustr))
129
0
    {
130
        // OOo 1.x only supported "true" so that is the more appropriate
131
        // default for OOoXML format documents.
132
0
        xDefaults->setPropertyValue(u"IsFollowingTextFlow"_ustr, uno::Any(true));
133
0
    }
134
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
2.65k
    bool const bIsAOO4(
141
2.65k
           GetImport().getGeneratorVersion() >= SvXMLImport::AOO_40x
142
2.65k
        && GetImport().getGeneratorVersion() <= SvXMLImport::AOO_4x);
143
144
    // fdo#75872: backward compatibility for pool defaults change
145
2.65k
    if (GetImport().isGeneratorVersionOlderThan(
146
2.65k
                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
1.90k
        || bIsAOO4)
151
752
    {
152
752
        rtl::Reference<XMLPropertySetMapper> const pImpPrMap(
153
752
            GetStyles()->GetImportPropertyMapper(GetFamily())
154
752
                ->getPropertySetMapper());
155
752
        sal_Int32 const nStrokeIndex(
156
752
            pImpPrMap->GetEntryIndex(XML_NAMESPACE_SVG, u"stroke-color", 0));
157
752
        if (std::none_of(GetProperties().begin(), GetProperties().end(),
158
752
                         XMLPropertyByIndex(nStrokeIndex)))
159
292
        {
160
292
            Color const nStroke(
161
292
                    bIsAOO4 ? Color(128, 128, 128) : COL_BLACK);
162
292
            xDefaults->setPropertyValue(u"LineColor"_ustr, Any(nStroke));
163
292
        }
164
752
        Color const nFillColor( bIsAOO4
165
752
            ? Color(0xCF, 0xE7, 0xF5) : Color(153, 204, 255));
166
752
        sal_Int32 const nFillIndex(
167
752
            pImpPrMap->GetEntryIndex(XML_NAMESPACE_DRAW, u"fill-color", 0));
168
752
        if (std::none_of(GetProperties().begin(), GetProperties().end(),
169
752
                         XMLPropertyByIndex(nFillIndex)))
170
140
        {
171
140
            xDefaults->setPropertyValue(u"FillColor"_ustr, Any(nFillColor));
172
140
        }
173
752
        if (xInfo->hasPropertyByName(u"FillColor2"_ustr))
174
0
        {
175
0
            sal_Int32 const nFill2Index(pImpPrMap->GetEntryIndex(
176
0
                        XML_NAMESPACE_DRAW, u"secondary-fill-color", 0));
177
0
            if (std::none_of(GetProperties().begin(), GetProperties().end(),
178
0
                             XMLPropertyByIndex(nFill2Index)))
179
0
            {
180
0
                xDefaults->setPropertyValue(u"FillColor2"_ustr, Any(sal_Int32(nFillColor)));
181
0
            }
182
0
        }
183
752
    }
184
185
2.65k
    FillPropertySet( xDefaults );
186
2.65k
}
187
188
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */