LibreOffice Module svtools (master) 1
fontsubstconfig.cxx
Go to the documentation of this file.
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
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>
29
30using namespace com::sun::star;
31using namespace com::sun::star::uno;
32using namespace com::sun::star::beans;
33
34
35constexpr OUStringLiteral cReplacement = u"Replacement";
36constexpr OUStringLiteral cFontPairs = u"FontPairs";
37
38constexpr OUStringLiteral cReplaceFont = u"ReplaceFont";
39constexpr OUStringLiteral cSubstituteFont= u"SubstituteFont";
40constexpr OUStringLiteral cOnScreenOnly = u"OnScreenOnly";
41constexpr OUStringLiteral cAlways = u"Always";
42
43namespace svtools
44{
45
47{
48 bool bIsEnabled = false;
49 Reference<css::container::XHierarchicalNameAccess> xHierarchyAccess = utl::ConfigManager::acquireTree(u"Office.Common/Font/Substitution");
50 Any aVal = xHierarchyAccess->getByHierarchicalName(cReplacement);
51
52 DBG_ASSERT(aVal.hasValue(), "no value available");
53 if(aVal.hasValue())
54 bIsEnabled = *o3tl::doAccess<bool>(aVal);
55 return bIsEnabled;
56}
57
58std::vector<SubstitutionStruct> GetFontSubstitutions()
59{
60 Reference<css::container::XHierarchicalNameAccess> xHierarchyAccess = utl::ConfigManager::acquireTree(u"Office.Common/Font/Substitution");
61
63 Sequence<OUString> aPropNames(aNodeNames.getLength() * 4);
64 OUString* pNames = aPropNames.getArray();
65 sal_Int32 nName = 0;
66 for(const OUString& rNodeName : aNodeNames)
67 {
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;
73 }
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++)
79 {
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);
86 }
87 return aSubstArr;
88}
89
90void SetFontSubstitutions(bool bIsEnabled, std::vector<SubstitutionStruct> const & aSubstArr)
91{
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);
94
95 OUString sNode(cFontPairs);
96 if(aSubstArr.empty())
97 {
98 utl::ConfigItem::ClearNodeSet(xHierarchyAccess, sNode);
99 return;
100 }
101
102 Sequence<PropertyValue> aSetValues(4 * aSubstArr.size());
103 PropertyValue* pSetValues = aSetValues.getArray();
104 sal_Int32 nSetValue = 0;
105
106 const OUString sReplaceFont(cReplaceFont);
107 const OUString sSubstituteFont(cSubstituteFont);
108 const OUString sAlways(cAlways);
109 const OUString sOnScreenOnly(cOnScreenOnly);
110
111 for(size_t i = 0; i < aSubstArr.size(); i++)
112 {
113 OUString sPrefix = sNode + "/_" + OUString::number(i) + "/";
114
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;
124 }
125 utl::ConfigItem::ReplaceSetProperties(xHierarchyAccess, sNode, aSetValues, /*bAllLocales*/false);
126}
127
129{
131
132 // remove old substitutions
134
136 std::vector<SubstitutionStruct> aSubst = GetFontSubstitutions();
137
138 // read new substitutions
139 if (bIsEnabled)
140 for (const SubstitutionStruct & rSub : aSubst)
141 {
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 );
148 }
149
151}
152
153} // namespace svtools
154
155/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AddFontSubstituteFlags
static void EndFontSubstitution()
static void AddFontSubstitute(const OUString &rFontName, const OUString &rReplaceFontName, AddFontSubstituteFlags nFlags)
static void BeginFontSubstitution()
static void RemoveFontsSubstitute()
static bool PutProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues, bool bAllLocales)
static bool ClearNodeSet(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const OUString &rNode)
static css::uno::Sequence< css::uno::Any > GetProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, bool bAllLocales)
static bool ReplaceSetProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const OUString &rNode, const css::uno::Sequence< css::beans::PropertyValue > &rValues, bool bAllLocales)
static css::uno::Sequence< OUString > GetNodeNames(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const OUString &rNode, ConfigNameFormat eFormat)
static SAL_DLLPRIVATE css::uno::Reference< css::container::XHierarchicalNameAccess > acquireTree(utl::ConfigItem const &item)
#define DBG_ASSERT(sCon, aError)
float u
constexpr OUStringLiteral cOnScreenOnly
constexpr OUStringLiteral cReplaceFont
constexpr OUStringLiteral cAlways
constexpr OUStringLiteral cReplacement
constexpr OUStringLiteral cFontPairs
constexpr OUStringLiteral cSubstituteFont
OUString sPrefix
int i
bool IsFontSubstitutionsEnabled()
void SetFontSubstitutions(bool bIsEnabled, std::vector< SubstitutionStruct > const &aSubstArr)
void ApplyFontSubstitutionsToVcl()
std::vector< SubstitutionStruct > GetFontSubstitutions()
const PropertyStruct aPropNames[]
bool bIsEnabled