diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /docmodel/source/theme | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'docmodel/source/theme')
-rw-r--r-- | docmodel/source/theme/ColorSet.cxx | 74 | ||||
-rw-r--r-- | docmodel/source/theme/Theme.cxx | 157 |
2 files changed, 231 insertions, 0 deletions
diff --git a/docmodel/source/theme/ColorSet.cxx b/docmodel/source/theme/ColorSet.cxx new file mode 100644 index 0000000000..df7cf18f61 --- /dev/null +++ b/docmodel/source/theme/ColorSet.cxx @@ -0,0 +1,74 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include <docmodel/theme/ColorSet.hxx> +#include <sstream> +#include <utility> +#include <libxml/xmlwriter.h> +#include <sal/log.hxx> + +namespace model +{ +ColorSet::ColorSet(OUString const& rName) + : maName(rName) +{ +} + +void ColorSet::add(model::ThemeColorType eType, Color aColorData) +{ + if (eType == model::ThemeColorType::Unknown) + return; + maColors[sal_Int16(eType)] = aColorData; +} + +Color ColorSet::getColor(model::ThemeColorType eType) const +{ + if (eType == model::ThemeColorType::Unknown) + { + SAL_WARN("svx", "ColorSet::getColor with ThemeColorType::Unknown"); + return COL_AUTO; + } + return maColors[size_t(eType)]; +} + +Color ColorSet::resolveColor(model::ComplexColor const& rComplexColor) const +{ + auto eThemeType = rComplexColor.getThemeColorType(); + if (eThemeType == model::ThemeColorType::Unknown) + { + SAL_WARN("svx", "ColorSet::resolveColor with ThemeColorType::Unknown"); + return COL_AUTO; + } + Color aColor = getColor(eThemeType); + return rComplexColor.applyTransformations(aColor); +} + +void ColorSet::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + (void)xmlTextWriterStartElement(pWriter, BAD_CAST("ColorSet")); + (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this); + (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("maName"), + BAD_CAST(maName.toUtf8().getStr())); + + for (const auto& rColor : maColors) + { + (void)xmlTextWriterStartElement(pWriter, BAD_CAST("Color")); + std::stringstream ss; + ss << rColor; + (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("value"), BAD_CAST(ss.str().c_str())); + (void)xmlTextWriterEndElement(pWriter); + } + + (void)xmlTextWriterEndElement(pWriter); +} + +} // end of namespace svx + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/docmodel/source/theme/Theme.cxx b/docmodel/source/theme/Theme.cxx new file mode 100644 index 0000000000..d4095eeb3e --- /dev/null +++ b/docmodel/source/theme/Theme.cxx @@ -0,0 +1,157 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include <docmodel/theme/Theme.hxx> + +#include <utility> +#include <libxml/xmlwriter.h> +#include <comphelper/sequenceashashmap.hxx> +#include <comphelper/sequence.hxx> +#include <sal/log.hxx> +#include <sal/types.h> +#include <o3tl/enumrange.hxx> +#include <com/sun/star/util/Color.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> + +using namespace com::sun::star; + +namespace model +{ +Theme::Theme() = default; + +Theme::Theme(OUString const& rName) + : maName(rName) +{ +} + +Theme::Theme(Theme const& rTheme) + : maName(rTheme.maName) + , mpColorSet(new ColorSet(*rTheme.getColorSet())) + , maFontScheme(rTheme.maFontScheme) +{ +} + +void Theme::SetName(const OUString& rName) { maName = rName; } + +const OUString& Theme::GetName() const { return maName; } + +void Theme::dumpAsXml(xmlTextWriterPtr pWriter) const +{ + (void)xmlTextWriterStartElement(pWriter, BAD_CAST("Theme")); + (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this); + (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("maName"), + BAD_CAST(maName.toUtf8().getStr())); + + if (mpColorSet) + { + mpColorSet->dumpAsXml(pWriter); + } + + (void)xmlTextWriterEndElement(pWriter); +} + +void Theme::ToAny(uno::Any& rVal) const +{ + comphelper::SequenceAsHashMap aMap; + aMap["Name"] <<= maName; + + if (mpColorSet) + { + std::vector<util::Color> aColorScheme; + for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>()) + { + if (eThemeColorType != model::ThemeColorType::Unknown) + { + Color aColor = mpColorSet->getColor(eThemeColorType); + aColorScheme.push_back(sal_Int32(aColor)); + } + } + + aMap["ColorSchemeName"] <<= mpColorSet->getName(); + aMap["ColorScheme"] <<= comphelper::containerToSequence(aColorScheme); + } + + rVal <<= aMap.getAsConstPropertyValueList(); +} + +std::unique_ptr<Theme> Theme::FromAny(const uno::Any& rVal) +{ + comphelper::SequenceAsHashMap aMap(rVal); + std::unique_ptr<Theme> pTheme; + std::shared_ptr<model::ColorSet> pColorSet; + + auto it = aMap.find("Name"); + if (it != aMap.end()) + { + OUString aName; + it->second >>= aName; + pTheme = std::make_unique<Theme>(aName); + } + + it = aMap.find("ColorSchemeName"); + if (it != aMap.end() && pTheme) + { + OUString aName; + it->second >>= aName; + pColorSet = std::make_shared<model::ColorSet>(aName); + pTheme->setColorSet(pColorSet); + } + + it = aMap.find("ColorScheme"); + if (it != aMap.end() && pColorSet) + { + uno::Sequence<util::Color> aColors; + it->second >>= aColors; + + SAL_WARN_IF(aColors.size() > 12, "svx", + "Theme::FromAny: number of colors greater than max theme colors supported"); + + for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>()) + { + if (eThemeColorType != model::ThemeColorType::Unknown) + { + size_t nIndex(static_cast<sal_Int16>(eThemeColorType)); + if (nIndex < aColors.size()) + { + Color aColor(ColorTransparency, aColors[nIndex]); + pColorSet->add(eThemeColorType, aColor); + } + } + } + } + + return pTheme; +} + +std::vector<Color> Theme::GetColors() const +{ + if (!mpColorSet) + return {}; + + std::vector<Color> aColors; + for (auto eThemeColorType : o3tl::enumrange<model::ThemeColorType>()) + { + if (eThemeColorType != model::ThemeColorType::Unknown) + aColors.push_back(mpColorSet->getColor(eThemeColorType)); + } + return aColors; +} + +Color Theme::GetColor(model::ThemeColorType eType) const +{ + if (!mpColorSet) + return {}; + + return mpColorSet->getColor(eType); +} + +} // end of namespace model + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |