Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svtools / source / config / fontsubstconfig.cxx
blob6d7323bb0d44cb1474ee3f9ee59a3730d639d8ab
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 <svtools/fontsubstconfig.hxx>
21 #include <com/sun/star/beans/PropertyValue.hpp>
22 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <o3tl/any.hxx>
25 #include <tools/debug.hxx>
26 #include <vcl/outdev.hxx>
27 #include <unotools/configmgr.hxx>
28 #include <unotools/configitem.hxx>
30 using namespace com::sun::star;
31 using namespace com::sun::star::uno;
32 using namespace com::sun::star::beans;
35 constexpr OUStringLiteral cReplacement = u"Replacement";
36 constexpr OUStringLiteral cFontPairs = u"FontPairs";
38 constexpr OUStringLiteral cReplaceFont = u"ReplaceFont";
39 constexpr OUStringLiteral cSubstituteFont= u"SubstituteFont";
40 constexpr OUStringLiteral cOnScreenOnly = u"OnScreenOnly";
41 constexpr OUStringLiteral cAlways = u"Always";
43 namespace svtools
46 bool IsFontSubstitutionsEnabled()
48 bool bIsEnabled = false;
49 Reference<css::container::XHierarchicalNameAccess> xHierarchyAccess = utl::ConfigManager::acquireTree(u"Office.Common/Font/Substitution");
50 Any aVal = xHierarchyAccess->getByHierarchicalName(cReplacement);
52 DBG_ASSERT(aVal.hasValue(), "no value available");
53 if(aVal.hasValue())
54 bIsEnabled = *o3tl::doAccess<bool>(aVal);
55 return bIsEnabled;
58 std::vector<SubstitutionStruct> GetFontSubstitutions()
60 Reference<css::container::XHierarchicalNameAccess> xHierarchyAccess = utl::ConfigManager::acquireTree(u"Office.Common/Font/Substitution");
62 const Sequence<OUString> aNodeNames = utl::ConfigItem::GetNodeNames(xHierarchyAccess, cFontPairs, utl::ConfigNameFormat::LocalPath);
63 Sequence<OUString> aPropNames(aNodeNames.getLength() * 4);
64 OUString* pNames = aPropNames.getArray();
65 sal_Int32 nName = 0;
66 for(const OUString& rNodeName : aNodeNames)
68 OUString sStart = cFontPairs + "/" + rNodeName + "/";
69 pNames[nName++] = sStart + cReplaceFont;
70 pNames[nName++] = sStart + cSubstituteFont;
71 pNames[nName++] = sStart + cAlways;
72 pNames[nName++] = sStart + cOnScreenOnly;
74 Sequence<Any> aNodeValues = utl::ConfigItem::GetProperties(xHierarchyAccess, aPropNames, /*bAllLocales*/false);
75 const Any* pNodeValues = aNodeValues.getConstArray();
76 nName = 0;
77 std::vector<SubstitutionStruct> aSubstArr;
78 for(sal_Int32 nNode = 0; nNode < aNodeNames.getLength(); nNode++)
80 SubstitutionStruct aInsert;
81 pNodeValues[nName++] >>= aInsert.sFont;
82 pNodeValues[nName++] >>= aInsert.sReplaceBy;
83 aInsert.bReplaceAlways = *o3tl::doAccess<bool>(pNodeValues[nName++]);
84 aInsert.bReplaceOnScreenOnly = *o3tl::doAccess<bool>(pNodeValues[nName++]);
85 aSubstArr.push_back(aInsert);
87 return aSubstArr;
90 void SetFontSubstitutions(bool bIsEnabled, std::vector<SubstitutionStruct> const & aSubstArr)
92 Reference<css::container::XHierarchicalNameAccess> xHierarchyAccess = utl::ConfigManager::acquireTree(u"Office.Common/Font/Substitution");
93 utl::ConfigItem::PutProperties(xHierarchyAccess, {cReplacement}, {css::uno::Any(bIsEnabled)}, /*bAllLocales*/false);
95 OUString sNode(cFontPairs);
96 if(aSubstArr.empty())
98 utl::ConfigItem::ClearNodeSet(xHierarchyAccess, sNode);
99 return;
102 Sequence<PropertyValue> aSetValues(4 * aSubstArr.size());
103 PropertyValue* pSetValues = aSetValues.getArray();
104 sal_Int32 nSetValue = 0;
106 const OUString sReplaceFont(cReplaceFont);
107 const OUString sSubstituteFont(cSubstituteFont);
108 const OUString sAlways(cAlways);
109 const OUString sOnScreenOnly(cOnScreenOnly);
111 for(size_t i = 0; i < aSubstArr.size(); i++)
113 OUString sPrefix = sNode + "/_" + OUString::number(i) + "/";
115 const SubstitutionStruct& rSubst = aSubstArr[i];
116 pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sReplaceFont;
117 pSetValues[nSetValue++].Value <<= rSubst.sFont;
118 pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sSubstituteFont;
119 pSetValues[nSetValue++].Value <<= rSubst.sReplaceBy;
120 pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sAlways;
121 pSetValues[nSetValue++].Value <<= rSubst.bReplaceAlways;
122 pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sOnScreenOnly;
123 pSetValues[nSetValue++].Value <<= rSubst.bReplaceOnScreenOnly;
125 utl::ConfigItem::ReplaceSetProperties(xHierarchyAccess, sNode, aSetValues, /*bAllLocales*/false);
128 void ApplyFontSubstitutionsToVcl()
130 OutputDevice::BeginFontSubstitution();
132 // remove old substitutions
133 OutputDevice::RemoveFontsSubstitute();
135 const bool bIsEnabled = IsFontSubstitutionsEnabled();
136 std::vector<SubstitutionStruct> aSubst = GetFontSubstitutions();
138 // read new substitutions
139 if (bIsEnabled)
140 for (const SubstitutionStruct & rSub : aSubst)
142 AddFontSubstituteFlags nFlags = AddFontSubstituteFlags::NONE;
143 if(rSub.bReplaceAlways)
144 nFlags |= AddFontSubstituteFlags::ALWAYS;
145 if(rSub.bReplaceOnScreenOnly)
146 nFlags |= AddFontSubstituteFlags::ScreenOnly;
147 OutputDevice::AddFontSubstitute( rSub.sFont, rSub.sReplaceBy, nFlags );
150 OutputDevice::EndFontSubstitution();
153 } // namespace svtools
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */