Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / oox / source / drawingml / themeelementscontext.cxx
blobbdc752b98512957aaa8153cf99368e52b7cc886d
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 <drawingml/themeelementscontext.hxx>
21 #include <drawingml/clrschemecontext.hxx>
22 #include <drawingml/lineproperties.hxx>
23 #include <drawingml/linepropertiescontext.hxx>
24 #include <drawingml/fillproperties.hxx>
25 #include <drawingml/misccontexts.hxx>
26 #include <drawingml/textcharacterproperties.hxx>
27 #include <oox/drawingml/theme.hxx>
28 #include <oox/helper/attributelist.hxx>
29 #include "effectproperties.hxx"
30 #include <drawingml/effectpropertiescontext.hxx>
31 #include <oox/token/namespaces.hxx>
32 #include <oox/token/tokens.hxx>
34 using namespace ::oox::core;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::xml::sax;
38 namespace oox::drawingml {
40 namespace {
42 class FillStyleListContext : public ContextHandler2
44 public:
45 FillStyleListContext(ContextHandler2Helper const & rParent, FillStyleList& rFillStyleList, model::FormatScheme& rFormatScheme);
46 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
48 protected:
49 FillStyleList& mrFillStyleList;
50 model::FormatScheme& mrFormatScheme;
51 virtual model::FillStyle* createAndAddFillStyle()
53 return mrFormatScheme.addFillStyle();
59 FillStyleListContext::FillStyleListContext(ContextHandler2Helper const & rParent, FillStyleList& rFillStyleList, model::FormatScheme& rFormatScheme)
60 : ContextHandler2(rParent)
61 , mrFillStyleList(rFillStyleList)
62 , mrFormatScheme(rFormatScheme)
66 ContextHandlerRef FillStyleListContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
68 switch (nElement)
70 case A_TOKEN( noFill ):
71 case A_TOKEN( solidFill ):
72 case A_TOKEN( gradFill ):
73 case A_TOKEN( blipFill ):
74 case A_TOKEN( pattFill ):
75 case A_TOKEN( grpFill ):
77 mrFillStyleList.push_back(std::make_shared<FillProperties>());
78 model::FillStyle* pFillStyle = createAndAddFillStyle();
79 return FillPropertiesContext::createFillContext(*this, nElement, rAttribs, *mrFillStyleList.back(), pFillStyle);
82 return nullptr;
85 namespace
88 class BackgroundFillStyleListContext : public FillStyleListContext
90 public:
91 BackgroundFillStyleListContext(ContextHandler2Helper const & rParent, FillStyleList& rFillStyleList, model::FormatScheme& rFormatScheme)
92 : FillStyleListContext(rParent, rFillStyleList, rFormatScheme)
95 protected:
96 model::FillStyle* createAndAddFillStyle() override
98 return mrFormatScheme.addBackgroundFillStyle();
102 } // end anonymous ns
104 namespace {
106 class LineStyleListContext : public ContextHandler2
108 public:
109 LineStyleListContext(ContextHandler2Helper const & rParent, LineStyleList& rLineStyleList, model::FormatScheme& rFormatScheme);
110 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
112 private:
113 model::FormatScheme& mrFormatScheme;
114 LineStyleList& mrLineStyleList;
119 LineStyleListContext::LineStyleListContext( ContextHandler2Helper const & rParent, LineStyleList& rLineStyleList, model::FormatScheme& rFormatScheme)
120 : ContextHandler2(rParent)
121 , mrFormatScheme(rFormatScheme)
122 , mrLineStyleList(rLineStyleList)
126 ContextHandlerRef LineStyleListContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
128 switch (nElement)
130 case A_TOKEN(ln):
132 mrLineStyleList.push_back( std::make_shared<LineProperties>( ) );
133 model::LineStyle* pLineStyle = mrFormatScheme.addLineStyle();
134 return new LinePropertiesContext(*this, rAttribs, *mrLineStyleList.back(), pLineStyle);
137 return nullptr;
140 namespace {
142 class EffectStyleListContext : public ContextHandler2
144 public:
145 EffectStyleListContext(ContextHandler2Helper const & rParent, EffectStyleList& rEffectStyleList, model::FormatScheme& rFormatScheme);
146 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
148 private:
149 model::FormatScheme& mrFormatScheme;
150 model::EffectStyle* mpEffectStyle;
151 EffectStyleList& mrEffectStyleList;
156 EffectStyleListContext::EffectStyleListContext( ContextHandler2Helper const & rParent, EffectStyleList& rEffectStyleList, model::FormatScheme& rFormatScheme)
157 : ContextHandler2(rParent)
158 , mrFormatScheme(rFormatScheme)
159 , mpEffectStyle(nullptr)
160 , mrEffectStyleList(rEffectStyleList)
164 ContextHandlerRef EffectStyleListContext::onCreateContext( sal_Int32 nElement, const AttributeList& /*rAttribs*/ )
166 switch( nElement )
168 case A_TOKEN( effectStyle ):
170 mpEffectStyle = mrFormatScheme.addEffectStyle();
171 mrEffectStyleList.push_back( std::make_shared<EffectProperties>( ) );
172 return this;
174 case A_TOKEN( effectLst ): // CT_EffectList
176 if( mrEffectStyleList.back() )
177 return new EffectPropertiesContext(*this, *mrEffectStyleList.back(), mpEffectStyle);
179 break;
181 return nullptr;
184 namespace
187 class FontSchemeContext : public ContextHandler2
189 public:
190 FontSchemeContext(ContextHandler2Helper const & rParent, const AttributeList& rAttribs, FontScheme& rFontScheme,
191 std::map<sal_Int32, std::vector<std::pair<OUString, OUString>>>& rSupplementalFontMap,
192 model::Theme& rTheme);
193 ~FontSchemeContext();
194 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
195 virtual void onEndElement() override;
197 private:
198 FontScheme& mrFontScheme;
199 TextCharacterPropertiesPtr mxCharProps;
200 std::map<sal_Int32, std::vector<std::pair<OUString, OUString>>>& mrSupplementalFontMap;
201 sal_Int32 maCurrentFont = 0;
202 model::Theme& mrTheme;
203 model::FontScheme maFontScheme;
206 } // end anonymous ns
208 FontSchemeContext::FontSchemeContext(ContextHandler2Helper const & rParent, const AttributeList& rAttribs, FontScheme& rFontScheme,
209 std::map<sal_Int32, std::vector<std::pair<OUString, OUString>>>& rSupplementalFontMap,
210 model::Theme& rTheme)
211 : ContextHandler2(rParent)
212 , mrFontScheme(rFontScheme)
213 , mrSupplementalFontMap(rSupplementalFontMap)
214 , mrTheme(rTheme)
215 , maFontScheme(rAttribs.getStringDefaulted(XML_name))
219 FontSchemeContext::~FontSchemeContext()
221 mrTheme.setFontScheme(maFontScheme);
224 namespace
227 void fillThemeFont(model::ThemeFont& rThemeFont, const AttributeList& rAttribs)
229 rThemeFont.maTypeface = rAttribs.getStringDefaulted(XML_typeface);
230 rThemeFont.maPanose = rAttribs.getStringDefaulted(XML_panose);
231 rThemeFont.maCharset = rAttribs.getInteger(XML_charset, WINDOWS_CHARSET_DEFAULT);
232 sal_Int32 nPitchFamily = rAttribs.getInteger(XML_pitchFamily, 0);
233 TextFont::resolvePitch(nPitchFamily, rThemeFont.maPitch, rThemeFont.maFamily);
236 } // end anonymous ns
238 ContextHandlerRef FontSchemeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
240 switch( nElement )
242 case A_TOKEN( majorFont ):
243 mxCharProps = std::make_shared<TextCharacterProperties>();
244 mrFontScheme[ XML_major ] = mxCharProps;
245 maCurrentFont = XML_major;
246 return this;
247 case A_TOKEN( minorFont ):
248 mxCharProps = std::make_shared<TextCharacterProperties>();
249 mrFontScheme[ XML_minor ] = mxCharProps;
250 maCurrentFont = XML_minor;
251 return this;
252 case A_TOKEN( latin ):
254 if (mxCharProps)
255 mxCharProps->maLatinFont.setAttributes(rAttribs);
257 model::ThemeFont aThemeFont;
258 fillThemeFont(aThemeFont, rAttribs);
259 if (maCurrentFont == XML_major)
260 maFontScheme.setMajorLatin(aThemeFont);
261 else if (maCurrentFont == XML_minor)
262 maFontScheme.setMinorLatin(aThemeFont);
264 break;
265 case A_TOKEN( ea ):
267 if( mxCharProps )
268 mxCharProps->maAsianFont.setAttributes( rAttribs );
269 model::ThemeFont aThemeFont;
270 fillThemeFont(aThemeFont, rAttribs);
271 if (maCurrentFont == XML_major)
272 maFontScheme.setMajorAsian(aThemeFont);
273 else if (maCurrentFont == XML_minor)
274 maFontScheme.setMinorAsian(aThemeFont);
276 break;
277 case A_TOKEN( cs ):
279 if( mxCharProps )
280 mxCharProps->maComplexFont.setAttributes( rAttribs );
281 model::ThemeFont aThemeFont;
282 fillThemeFont(aThemeFont, rAttribs);
283 if (maCurrentFont == XML_major)
284 maFontScheme.setMajorComplex(aThemeFont);
285 else if (maCurrentFont == XML_minor)
286 maFontScheme.setMinorComplex(aThemeFont);
288 break;
289 case A_TOKEN(font):
291 OUString aScript = rAttribs.getStringDefaulted(XML_script);
292 OUString aTypeface = rAttribs.getStringDefaulted(XML_typeface);
293 mrSupplementalFontMap[maCurrentFont].emplace_back(std::pair<OUString, OUString>{aScript, aTypeface});
294 if (maCurrentFont == XML_major)
295 maFontScheme.addMajorSupplementalFont({aScript, aTypeface});
296 else if (maCurrentFont == XML_minor)
297 maFontScheme.addMinorSupplementalFont({aScript, aTypeface});
299 break;
301 return nullptr;
304 void FontSchemeContext::onEndElement()
306 switch( getCurrentElement() )
308 case A_TOKEN( majorFont ):
309 case A_TOKEN( minorFont ):
310 mxCharProps.reset();
311 break;
315 ThemeElementsContext::ThemeElementsContext(ContextHandler2Helper const & rParent, Theme& rOoxTheme, model::Theme& rTheme)
316 : ContextHandler2(rParent)
317 , mrOoxTheme(rOoxTheme)
318 , mrTheme(rTheme)
322 ContextHandlerRef ThemeElementsContext::onCreateContext(sal_Int32 nElement, const AttributeList& rAttribs)
324 // CT_BaseStyles
325 switch (nElement)
327 case A_TOKEN( clrScheme ): // CT_ColorScheme
329 OUString aColorSchemeName = rAttribs.getStringDefaulted(XML_name);
330 mrTheme.setColorSet(std::make_shared<model::ColorSet>(aColorSchemeName));
331 if (rAttribs.hasAttribute(XML_name))
332 mrOoxTheme.getClrScheme().SetName(rAttribs.getStringDefaulted(XML_name));
333 return new clrSchemeContext(*this, mrOoxTheme.getClrScheme(), *mrTheme.getColorSet());
335 case A_TOKEN( fontScheme ): // CT_FontScheme
337 if (rAttribs.hasAttribute(XML_name))
338 mrOoxTheme.setFontSchemeName(rAttribs.getStringDefaulted(XML_name));
340 return new FontSchemeContext(*this, rAttribs, mrOoxTheme.getFontScheme(), mrOoxTheme.getSupplementalFontMap(), mrTheme);
343 case A_TOKEN( fmtScheme ): // CT_StyleMatrix
345 if (rAttribs.hasAttribute(XML_name))
346 mrOoxTheme.setFormatSchemeName(rAttribs.getStringDefaulted(XML_name));
347 return this;
350 case A_TOKEN( fillStyleLst ): // CT_FillStyleList
351 return new FillStyleListContext(*this, mrOoxTheme.getFillStyleList(), mrTheme.getFormatScheme());
352 case A_TOKEN( lnStyleLst ): // CT_LineStyleList
353 return new LineStyleListContext(*this, mrOoxTheme.getLineStyleList(), mrTheme.getFormatScheme());
354 case A_TOKEN( effectStyleLst ): // CT_EffectStyleList
355 return new EffectStyleListContext(*this, mrOoxTheme.getEffectStyleList(), mrTheme.getFormatScheme());
356 case A_TOKEN( bgFillStyleLst ): // CT_BackgroundFillStyleList
357 return new BackgroundFillStyleListContext( *this, mrOoxTheme.getBgFillStyleList(), mrTheme.getFormatScheme());
359 return nullptr;
362 } // namespace oox::drawingml
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */