Line data Source code
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 :
20 : #include <svtools/fontsubstconfig.hxx>
21 : #include <com/sun/star/beans/PropertyValue.hpp>
22 : #include <com/sun/star/uno/Any.hxx>
23 : #include <com/sun/star/uno/Sequence.hxx>
24 : #include <tools/debug.hxx>
25 : #include <vcl/outdev.hxx>
26 : #include <rtl/logfile.hxx>
27 :
28 : #include <boost/ptr_container/ptr_vector.hpp>
29 :
30 : using namespace utl;
31 : using namespace com::sun::star;
32 : using namespace com::sun::star::uno;
33 : using namespace com::sun::star::beans;
34 :
35 : using ::rtl::OUString;
36 :
37 : #define C2U(cChar) OUString::createFromAscii(cChar)
38 :
39 : const sal_Char cReplacement[] = "Replacement";
40 : const sal_Char cFontPairs[] = "FontPairs";
41 :
42 : const sal_Char cReplaceFont[] = "ReplaceFont";
43 : const sal_Char cSubstituteFont[]= "SubstituteFont";
44 : const sal_Char cOnScreenOnly[] = "OnScreenOnly";
45 : const sal_Char cAlways[] = "Always";
46 :
47 : typedef boost::ptr_vector<SubstitutionStruct> SubstitutionStructArr;
48 :
49 0 : struct SvtFontSubstConfig_Impl
50 : {
51 : SubstitutionStructArr aSubstArr;
52 : };
53 :
54 0 : SvtFontSubstConfig::SvtFontSubstConfig() :
55 : ConfigItem(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/Font/Substitution"))),
56 : bIsEnabled(sal_False),
57 0 : pImpl(new SvtFontSubstConfig_Impl)
58 : {
59 : RTL_LOGFILE_CONTEXT(aLog, "svtools SvtFontSubstConfig::SvtFontSubstConfig()");
60 :
61 0 : Sequence<OUString> aNames(1);
62 0 : aNames.getArray()[0] = C2U(cReplacement);
63 0 : Sequence<Any> aValues = GetProperties(aNames);
64 : DBG_ASSERT(aValues.getConstArray()[0].hasValue(), "no value available");
65 0 : if(aValues.getConstArray()[0].hasValue())
66 0 : bIsEnabled = *(sal_Bool*)aValues.getConstArray()[0].getValue();
67 :
68 0 : OUString sPropPrefix(C2U(cFontPairs));
69 0 : Sequence<OUString> aNodeNames = GetNodeNames(sPropPrefix, CONFIG_NAME_LOCAL_PATH);
70 0 : const OUString* pNodeNames = aNodeNames.getConstArray();
71 0 : Sequence<OUString> aPropNames(aNodeNames.getLength() * 4);
72 0 : OUString* pNames = aPropNames.getArray();
73 0 : sal_Int32 nName = 0;
74 0 : sPropPrefix += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
75 : sal_Int32 nNode;
76 0 : for(nNode = 0; nNode < aNodeNames.getLength(); nNode++)
77 : {
78 0 : OUString sStart(sPropPrefix);
79 0 : sStart += pNodeNames[nNode];
80 0 : sStart += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/"));
81 0 : pNames[nName] = sStart; pNames[nName++] += C2U(cReplaceFont);
82 0 : pNames[nName] = sStart; pNames[nName++] += C2U(cSubstituteFont);
83 0 : pNames[nName] = sStart; pNames[nName++] += C2U(cAlways);
84 0 : pNames[nName] = sStart; pNames[nName++] += C2U(cOnScreenOnly);
85 0 : }
86 0 : Sequence<Any> aNodeValues = GetProperties(aPropNames);
87 0 : const Any* pNodeValues = aNodeValues.getConstArray();
88 0 : nName = 0;
89 0 : for(nNode = 0; nNode < aNodeNames.getLength(); nNode++)
90 : {
91 0 : SubstitutionStruct* pInsert = new SubstitutionStruct;
92 0 : pNodeValues[nName++] >>= pInsert->sFont;
93 0 : pNodeValues[nName++] >>= pInsert->sReplaceBy;
94 0 : pInsert->bReplaceAlways = *(sal_Bool*)pNodeValues[nName++].getValue();
95 0 : pInsert->bReplaceOnScreenOnly = *(sal_Bool*)pNodeValues[nName++].getValue();
96 0 : pImpl->aSubstArr.push_back(pInsert);
97 0 : }
98 0 : }
99 :
100 0 : SvtFontSubstConfig::~SvtFontSubstConfig()
101 : {
102 0 : delete pImpl;
103 0 : }
104 :
105 0 : void SvtFontSubstConfig::Notify( const com::sun::star::uno::Sequence< rtl::OUString >& )
106 : {
107 0 : }
108 :
109 0 : void SvtFontSubstConfig::Commit()
110 : {
111 0 : Sequence<OUString> aNames(1);
112 0 : aNames.getArray()[0] = C2U(cReplacement);
113 0 : Sequence<Any> aValues(1);
114 0 : aValues.getArray()[0].setValue(&bIsEnabled, ::getBooleanCppuType());
115 0 : PutProperties(aNames, aValues);
116 :
117 0 : OUString sNode(C2U(cFontPairs));
118 0 : if(pImpl->aSubstArr.empty())
119 0 : ClearNodeSet(sNode);
120 : else
121 : {
122 0 : Sequence<PropertyValue> aSetValues(4 * pImpl->aSubstArr.size());
123 0 : PropertyValue* pSetValues = aSetValues.getArray();
124 0 : sal_Int32 nSetValue = 0;
125 :
126 0 : const OUString sReplaceFont(C2U(cReplaceFont));
127 0 : const OUString sSubstituteFont(C2U(cSubstituteFont));
128 0 : const OUString sAlways(C2U(cAlways));
129 0 : const OUString sOnScreenOnly(C2U(cOnScreenOnly));
130 :
131 0 : const uno::Type& rBoolType = ::getBooleanCppuType();
132 0 : for(size_t i = 0; i < pImpl->aSubstArr.size(); i++)
133 : {
134 0 : OUString sPrefix(sNode);
135 0 : sPrefix += C2U("/_");
136 0 : sPrefix += OUString::valueOf((sal_Int32)i);
137 0 : sPrefix += C2U("/");
138 :
139 0 : SubstitutionStruct& pSubst = pImpl->aSubstArr[i];
140 0 : pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sReplaceFont;
141 0 : pSetValues[nSetValue++].Value <<= pSubst.sFont;
142 0 : pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sSubstituteFont;
143 0 : pSetValues[nSetValue++].Value <<= pSubst.sReplaceBy;
144 0 : pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sAlways;
145 0 : pSetValues[nSetValue++].Value.setValue(&pSubst.bReplaceAlways, rBoolType);
146 0 : pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sOnScreenOnly;
147 0 : pSetValues[nSetValue++].Value.setValue(&pSubst.bReplaceOnScreenOnly, rBoolType);
148 0 : }
149 0 : ReplaceSetProperties(sNode, aSetValues);
150 0 : }
151 0 : }
152 :
153 0 : sal_Int32 SvtFontSubstConfig::SubstitutionCount() const
154 : {
155 0 : return pImpl->aSubstArr.size();
156 : }
157 :
158 0 : void SvtFontSubstConfig::ClearSubstitutions()
159 : {
160 0 : pImpl->aSubstArr.clear();
161 0 : }
162 :
163 0 : const SubstitutionStruct* SvtFontSubstConfig::GetSubstitution(sal_Int32 nPos)
164 : {
165 0 : sal_Int32 nCount = static_cast<sal_Int32>(pImpl->aSubstArr.size());
166 : DBG_ASSERT(nPos >= 0 && nPos < nCount, "illegal array index");
167 0 : if(nPos >= 0 && nPos < nCount)
168 0 : return &pImpl->aSubstArr[nPos];
169 0 : return NULL;
170 : }
171 :
172 0 : void SvtFontSubstConfig::AddSubstitution(const SubstitutionStruct& rToAdd)
173 : {
174 0 : pImpl->aSubstArr.push_back(new SubstitutionStruct(rToAdd));
175 0 : }
176 :
177 0 : void SvtFontSubstConfig::Apply()
178 : {
179 0 : OutputDevice::BeginFontSubstitution();
180 :
181 : // remove old substitions
182 0 : sal_uInt16 nOldCount = OutputDevice::GetFontSubstituteCount();
183 :
184 0 : while (nOldCount)
185 0 : OutputDevice::RemoveFontSubstitute(--nOldCount);
186 :
187 : // read new substitutions
188 0 : sal_Int32 nCount = IsEnabled() ? SubstitutionCount() : 0;
189 :
190 0 : for (sal_Int32 i = 0; i < nCount; i++)
191 : {
192 0 : sal_uInt16 nFlags = 0;
193 0 : const SubstitutionStruct* pSubs = GetSubstitution(i);
194 0 : if(pSubs->bReplaceAlways)
195 0 : nFlags |= FONT_SUBSTITUTE_ALWAYS;
196 0 : if(pSubs->bReplaceOnScreenOnly)
197 0 : nFlags |= FONT_SUBSTITUTE_SCREENONLY;
198 0 : OutputDevice::AddFontSubstitute( String(pSubs->sFont), String(pSubs->sReplaceBy), nFlags );
199 : }
200 :
201 0 : OutputDevice::EndFontSubstitution();
202 0 : }
203 :
204 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|