tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / oox / source / drawingml / themeelementscontext.cxx
blob537a24441a852b302d4ffe238eb9e856f889ffd3
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 <oox/drawingml/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;
37 namespace oox::drawingml {
39 namespace {
41 class FillStyleListContext : public ContextHandler2
43 public:
44 FillStyleListContext(ContextHandler2Helper const & rParent, FillStyleList& rFillStyleList, model::FormatScheme& rFormatScheme);
45 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
47 protected:
48 FillStyleList& mrFillStyleList;
49 model::FormatScheme& mrFormatScheme;
50 virtual model::FillStyle* createAndAddFillStyle()
52 return mrFormatScheme.addFillStyle();
58 FillStyleListContext::FillStyleListContext(ContextHandler2Helper const & rParent, FillStyleList& rFillStyleList, model::FormatScheme& rFormatScheme)
59 : ContextHandler2(rParent)
60 , mrFillStyleList(rFillStyleList)
61 , mrFormatScheme(rFormatScheme)
65 ContextHandlerRef FillStyleListContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
67 switch (nElement)
69 case A_TOKEN( noFill ):
70 case A_TOKEN( solidFill ):
71 case A_TOKEN( gradFill ):
72 case A_TOKEN( blipFill ):
73 case A_TOKEN( pattFill ):
74 case A_TOKEN( grpFill ):
76 mrFillStyleList.push_back(std::make_shared<FillProperties>());
77 model::FillStyle* pFillStyle = createAndAddFillStyle();
78 return FillPropertiesContext::createFillContext(*this, nElement, rAttribs, *mrFillStyleList.back(), pFillStyle);
81 return nullptr;
84 namespace
87 class BackgroundFillStyleListContext : public FillStyleListContext
89 public:
90 BackgroundFillStyleListContext(ContextHandler2Helper const & rParent, FillStyleList& rFillStyleList, model::FormatScheme& rFormatScheme)
91 : FillStyleListContext(rParent, rFillStyleList, rFormatScheme)
94 protected:
95 model::FillStyle* createAndAddFillStyle() override
97 return mrFormatScheme.addBackgroundFillStyle();
101 } // end anonymous ns
103 namespace {
105 class LineStyleListContext : public ContextHandler2
107 public:
108 LineStyleListContext(ContextHandler2Helper const & rParent, LineStyleList& rLineStyleList, model::FormatScheme& rFormatScheme);
109 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
111 private:
112 model::FormatScheme& mrFormatScheme;
113 LineStyleList& mrLineStyleList;
118 LineStyleListContext::LineStyleListContext( ContextHandler2Helper const & rParent, LineStyleList& rLineStyleList, model::FormatScheme& rFormatScheme)
119 : ContextHandler2(rParent)
120 , mrFormatScheme(rFormatScheme)
121 , mrLineStyleList(rLineStyleList)
125 ContextHandlerRef LineStyleListContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
127 switch (nElement)
129 case A_TOKEN(ln):
131 mrLineStyleList.push_back( std::make_shared<LineProperties>( ) );
132 model::LineStyle* pLineStyle = mrFormatScheme.addLineStyle();
133 return new LinePropertiesContext(*this, rAttribs, *mrLineStyleList.back(), pLineStyle);
136 return nullptr;
139 namespace {
141 class EffectStyleListContext : public ContextHandler2
143 public:
144 EffectStyleListContext(ContextHandler2Helper const & rParent, EffectStyleList& rEffectStyleList, model::FormatScheme& rFormatScheme);
145 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
147 private:
148 model::FormatScheme& mrFormatScheme;
149 model::EffectStyle* mpEffectStyle;
150 EffectStyleList& mrEffectStyleList;
155 EffectStyleListContext::EffectStyleListContext( ContextHandler2Helper const & rParent, EffectStyleList& rEffectStyleList, model::FormatScheme& rFormatScheme)
156 : ContextHandler2(rParent)
157 , mrFormatScheme(rFormatScheme)
158 , mpEffectStyle(nullptr)
159 , mrEffectStyleList(rEffectStyleList)
163 ContextHandlerRef EffectStyleListContext::onCreateContext( sal_Int32 nElement, const AttributeList& /*rAttribs*/ )
165 switch( nElement )
167 case A_TOKEN( effectStyle ):
169 mpEffectStyle = mrFormatScheme.addEffectStyle();
170 mrEffectStyleList.push_back( std::make_shared<EffectProperties>( ) );
171 return this;
173 case A_TOKEN( effectLst ): // CT_EffectList
175 if( mrEffectStyleList.back() )
176 return new EffectPropertiesContext(*this, *mrEffectStyleList.back(), mpEffectStyle);
178 break;
180 return nullptr;
183 namespace
186 class FontSchemeContext : public ContextHandler2
188 public:
189 FontSchemeContext(ContextHandler2Helper const & rParent, const AttributeList& rAttribs, FontScheme& rFontScheme,
190 std::map<sal_Int32, std::vector<std::pair<OUString, OUString>>>& rSupplementalFontMap,
191 model::Theme& rTheme);
192 ~FontSchemeContext();
193 virtual ContextHandlerRef onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs ) override;
194 virtual void onEndElement() override;
196 private:
197 FontScheme& mrFontScheme;
198 TextCharacterPropertiesPtr mxCharProps;
199 std::map<sal_Int32, std::vector<std::pair<OUString, OUString>>>& mrSupplementalFontMap;
200 sal_Int32 maCurrentFont = 0;
201 model::Theme& mrTheme;
202 model::FontScheme maFontScheme;
205 } // end anonymous ns
207 FontSchemeContext::FontSchemeContext(ContextHandler2Helper const & rParent, const AttributeList& rAttribs, FontScheme& rFontScheme,
208 std::map<sal_Int32, std::vector<std::pair<OUString, OUString>>>& rSupplementalFontMap,
209 model::Theme& rTheme)
210 : ContextHandler2(rParent)
211 , mrFontScheme(rFontScheme)
212 , mrSupplementalFontMap(rSupplementalFontMap)
213 , mrTheme(rTheme)
214 , maFontScheme(rAttribs.getStringDefaulted(XML_name))
218 FontSchemeContext::~FontSchemeContext()
220 mrTheme.setFontScheme(maFontScheme);
223 namespace
226 void fillThemeFont(model::ThemeFont& rThemeFont, const AttributeList& rAttribs)
228 rThemeFont.maTypeface = rAttribs.getStringDefaulted(XML_typeface);
229 rThemeFont.maPanose = rAttribs.getStringDefaulted(XML_panose);
230 rThemeFont.maCharset = rAttribs.getInteger(XML_charset, WINDOWS_CHARSET_DEFAULT);
231 sal_Int32 nPitchFamily = rAttribs.getInteger(XML_pitchFamily, 0);
232 TextFont::resolvePitch(nPitchFamily, rThemeFont.maPitch, rThemeFont.maFamily);
235 } // end anonymous ns
237 ContextHandlerRef FontSchemeContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
239 switch( nElement )
241 case A_TOKEN( majorFont ):
242 mxCharProps = std::make_shared<TextCharacterProperties>();
243 mrFontScheme[ XML_major ] = mxCharProps;
244 maCurrentFont = XML_major;
245 return this;
246 case A_TOKEN( minorFont ):
247 mxCharProps = std::make_shared<TextCharacterProperties>();
248 mrFontScheme[ XML_minor ] = mxCharProps;
249 maCurrentFont = XML_minor;
250 return this;
251 case A_TOKEN( latin ):
253 if (mxCharProps)
254 mxCharProps->maLatinFont.setAttributes(rAttribs);
256 model::ThemeFont aThemeFont;
257 fillThemeFont(aThemeFont, rAttribs);
258 if (maCurrentFont == XML_major)
259 maFontScheme.setMajorLatin(aThemeFont);
260 else if (maCurrentFont == XML_minor)
261 maFontScheme.setMinorLatin(aThemeFont);
263 break;
264 case A_TOKEN( ea ):
266 if( mxCharProps )
267 mxCharProps->maAsianFont.setAttributes( rAttribs );
268 model::ThemeFont aThemeFont;
269 fillThemeFont(aThemeFont, rAttribs);
270 if (maCurrentFont == XML_major)
271 maFontScheme.setMajorAsian(aThemeFont);
272 else if (maCurrentFont == XML_minor)
273 maFontScheme.setMinorAsian(aThemeFont);
275 break;
276 case A_TOKEN( cs ):
278 if( mxCharProps )
279 mxCharProps->maComplexFont.setAttributes( rAttribs );
280 model::ThemeFont aThemeFont;
281 fillThemeFont(aThemeFont, rAttribs);
282 if (maCurrentFont == XML_major)
283 maFontScheme.setMajorComplex(aThemeFont);
284 else if (maCurrentFont == XML_minor)
285 maFontScheme.setMinorComplex(aThemeFont);
287 break;
288 case A_TOKEN(font):
290 OUString aScript = rAttribs.getStringDefaulted(XML_script);
291 OUString aTypeface = rAttribs.getStringDefaulted(XML_typeface);
292 mrSupplementalFontMap[maCurrentFont].emplace_back(std::pair<OUString, OUString>{aScript, aTypeface});
293 if (maCurrentFont == XML_major)
294 maFontScheme.addMajorSupplementalFont({aScript, aTypeface});
295 else if (maCurrentFont == XML_minor)
296 maFontScheme.addMinorSupplementalFont({aScript, aTypeface});
298 break;
300 return nullptr;
303 void FontSchemeContext::onEndElement()
305 switch( getCurrentElement() )
307 case A_TOKEN( majorFont ):
308 case A_TOKEN( minorFont ):
309 mxCharProps.reset();
310 break;
314 ThemeElementsContext::ThemeElementsContext(ContextHandler2Helper const & rParent, Theme& rOoxTheme, model::Theme& rTheme)
315 : ContextHandler2(rParent)
316 , mrOoxTheme(rOoxTheme)
317 , mrTheme(rTheme)
321 ContextHandlerRef ThemeElementsContext::onCreateContext(sal_Int32 nElement, const AttributeList& rAttribs)
323 // CT_BaseStyles
324 switch (nElement)
326 case A_TOKEN( clrScheme ): // CT_ColorScheme
328 OUString aColorSchemeName = rAttribs.getStringDefaulted(XML_name);
329 mrTheme.setColorSet(std::make_shared<model::ColorSet>(aColorSchemeName));
330 if (rAttribs.hasAttribute(XML_name))
331 mrOoxTheme.getClrScheme().SetName(rAttribs.getStringDefaulted(XML_name));
332 return new clrSchemeContext(*this, mrOoxTheme.getClrScheme(), *mrTheme.getColorSet());
334 case A_TOKEN( fontScheme ): // CT_FontScheme
336 if (rAttribs.hasAttribute(XML_name))
337 mrOoxTheme.setFontSchemeName(rAttribs.getStringDefaulted(XML_name));
339 return new FontSchemeContext(*this, rAttribs, mrOoxTheme.getFontScheme(), mrOoxTheme.getSupplementalFontMap(), mrTheme);
342 case A_TOKEN( fmtScheme ): // CT_StyleMatrix
344 if (rAttribs.hasAttribute(XML_name))
345 mrOoxTheme.setFormatSchemeName(rAttribs.getStringDefaulted(XML_name));
346 return this;
349 case A_TOKEN( fillStyleLst ): // CT_FillStyleList
350 return new FillStyleListContext(*this, mrOoxTheme.getFillStyleList(), mrTheme.getFormatScheme());
351 case A_TOKEN( lnStyleLst ): // CT_LineStyleList
352 return new LineStyleListContext(*this, mrOoxTheme.getLineStyleList(), mrTheme.getFormatScheme());
353 case A_TOKEN( effectStyleLst ): // CT_EffectStyleList
354 return new EffectStyleListContext(*this, mrOoxTheme.getEffectStyleList(), mrTheme.getFormatScheme());
355 case A_TOKEN( bgFillStyleLst ): // CT_BackgroundFillStyleList
356 return new BackgroundFillStyleListContext( *this, mrOoxTheme.getBgFillStyleList(), mrTheme.getFormatScheme());
358 return nullptr;
361 } // namespace oox::drawingml
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */