xref: /AOO41X/main/svtools/source/config/fontsubstconfig.cxx (revision 5900e8ec128faec89519683efce668ccd8cc6084)
1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5900e8ecSAndrew Rist  * distributed with this work for additional information
6*5900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*5900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*5900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist  * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5900e8ecSAndrew Rist  * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*5900e8ecSAndrew Rist  *************************************************************/
21*5900e8ecSAndrew Rist 
22*5900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <svtools/fontsubstconfig.hxx>
28cdf0e10cSrcweir #include <svl/svarray.hxx>
29cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
30cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
31cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
32cdf0e10cSrcweir #include <tools/debug.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <vcl/outdev.hxx>
35cdf0e10cSrcweir #include <rtl/logfile.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace utl;
38cdf0e10cSrcweir using namespace rtl;
39cdf0e10cSrcweir using namespace com::sun::star;
40cdf0e10cSrcweir using namespace com::sun::star::uno;
41cdf0e10cSrcweir using namespace com::sun::star::beans;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #define C2U(cChar) OUString::createFromAscii(cChar)
44cdf0e10cSrcweir 
45cdf0e10cSrcweir const sal_Char cReplacement[] = "Replacement";
46cdf0e10cSrcweir const sal_Char cFontPairs[] = "FontPairs";
47cdf0e10cSrcweir 
48cdf0e10cSrcweir const sal_Char cReplaceFont[] 	= "ReplaceFont";
49cdf0e10cSrcweir const sal_Char cSubstituteFont[]= "SubstituteFont";
50cdf0e10cSrcweir const sal_Char cOnScreenOnly[] 	= "OnScreenOnly";
51cdf0e10cSrcweir const sal_Char cAlways[] 		= "Always";
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //-----------------------------------------------------------------------------
54cdf0e10cSrcweir typedef SubstitutionStruct* SubstitutionStructPtr;
55cdf0e10cSrcweir SV_DECL_PTRARR_DEL(SubstitutionStructArr, SubstitutionStructPtr, 2, 2)
56cdf0e10cSrcweir SV_IMPL_PTRARR(SubstitutionStructArr, SubstitutionStructPtr);
57cdf0e10cSrcweir //-----------------------------------------------------------------------------
58cdf0e10cSrcweir struct SvtFontSubstConfig_Impl
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	SubstitutionStructArr	aSubstArr;
61cdf0e10cSrcweir };
62cdf0e10cSrcweir /* -----------------------------18.01.01 12:04--------------------------------
63cdf0e10cSrcweir 
64cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
SvtFontSubstConfig()65cdf0e10cSrcweir SvtFontSubstConfig::SvtFontSubstConfig() :
66cdf0e10cSrcweir 	ConfigItem(C2U("Office.Common/Font/Substitution")),
67cdf0e10cSrcweir 	bIsEnabled(sal_False),
68cdf0e10cSrcweir 	pImpl(new SvtFontSubstConfig_Impl)
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT(aLog, "svtools SvtFontSubstConfig::SvtFontSubstConfig()");
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	Sequence<OUString> aNames(1);
73cdf0e10cSrcweir 	aNames.getArray()[0] = C2U(cReplacement);
74cdf0e10cSrcweir 	Sequence<Any> aValues = GetProperties(aNames);
75cdf0e10cSrcweir 	DBG_ASSERT(aValues.getConstArray()[0].hasValue(), "no value available");
76cdf0e10cSrcweir 	if(aValues.getConstArray()[0].hasValue())
77cdf0e10cSrcweir 		bIsEnabled = *(sal_Bool*)aValues.getConstArray()[0].getValue();
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	OUString sPropPrefix(C2U(cFontPairs));
80cdf0e10cSrcweir     Sequence<OUString> aNodeNames = GetNodeNames(sPropPrefix, CONFIG_NAME_LOCAL_PATH);
81cdf0e10cSrcweir 	const OUString* pNodeNames = aNodeNames.getConstArray();
82cdf0e10cSrcweir 	Sequence<OUString> aPropNames(aNodeNames.getLength() * 4);
83cdf0e10cSrcweir 	OUString* pNames = aPropNames.getArray();
84cdf0e10cSrcweir 	sal_Int32 nName = 0;
85cdf0e10cSrcweir 	sPropPrefix += C2U("/");
86cdf0e10cSrcweir 	sal_Int32 nNode;
87cdf0e10cSrcweir 	for(nNode = 0; nNode < aNodeNames.getLength(); nNode++)
88cdf0e10cSrcweir 	{
89cdf0e10cSrcweir 		OUString sStart(sPropPrefix);
90cdf0e10cSrcweir 		sStart += pNodeNames[nNode];
91cdf0e10cSrcweir 		sStart += C2U("/");
92cdf0e10cSrcweir 		pNames[nName] = sStart; 	pNames[nName++] += C2U(cReplaceFont);
93cdf0e10cSrcweir 		pNames[nName] = sStart; 	pNames[nName++] += C2U(cSubstituteFont);
94cdf0e10cSrcweir 		pNames[nName] = sStart; 	pNames[nName++] += C2U(cAlways);
95cdf0e10cSrcweir 		pNames[nName] = sStart; 	pNames[nName++] += C2U(cOnScreenOnly);
96cdf0e10cSrcweir 	}
97cdf0e10cSrcweir 	Sequence<Any> aNodeValues = GetProperties(aPropNames);
98cdf0e10cSrcweir 	const Any* pNodeValues = aNodeValues.getConstArray();
99cdf0e10cSrcweir 	nName = 0;
100cdf0e10cSrcweir 	for(nNode = 0; nNode < aNodeNames.getLength(); nNode++)
101cdf0e10cSrcweir 	{
102cdf0e10cSrcweir 		SubstitutionStructPtr pInsert = new SubstitutionStruct;
103cdf0e10cSrcweir 		pNodeValues[nName++] >>= pInsert->sFont;
104cdf0e10cSrcweir 		pNodeValues[nName++] >>= pInsert->sReplaceBy;
105cdf0e10cSrcweir 		pInsert->bReplaceAlways = *(sal_Bool*)pNodeValues[nName++].getValue();
106cdf0e10cSrcweir 		pInsert->bReplaceOnScreenOnly = *(sal_Bool*)pNodeValues[nName++].getValue();
107cdf0e10cSrcweir 		pImpl->aSubstArr.Insert(pInsert, pImpl->aSubstArr.Count());
108cdf0e10cSrcweir 	}
109cdf0e10cSrcweir }
110cdf0e10cSrcweir /* -----------------------------18.01.01 12:06--------------------------------
111cdf0e10cSrcweir 
112cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
~SvtFontSubstConfig()113cdf0e10cSrcweir SvtFontSubstConfig::~SvtFontSubstConfig()
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	delete pImpl;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir /*-- 18.01.01 12:08:00---------------------------------------------------
118cdf0e10cSrcweir 
119cdf0e10cSrcweir   -----------------------------------------------------------------------*/
Notify(const com::sun::star::uno::Sequence<rtl::OUString> &)120cdf0e10cSrcweir void SvtFontSubstConfig::Notify( const com::sun::star::uno::Sequence< rtl::OUString >& )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir }
123cdf0e10cSrcweir 
Commit()124cdf0e10cSrcweir void SvtFontSubstConfig::Commit()
125cdf0e10cSrcweir {
126cdf0e10cSrcweir 	Sequence<OUString> aNames(1);
127cdf0e10cSrcweir 	aNames.getArray()[0] = C2U(cReplacement);
128cdf0e10cSrcweir 	Sequence<Any> aValues(1);
129cdf0e10cSrcweir 	aValues.getArray()[0].setValue(&bIsEnabled, ::getBooleanCppuType());
130cdf0e10cSrcweir 	PutProperties(aNames, aValues);
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	OUString sNode(C2U(cFontPairs));
133cdf0e10cSrcweir 	if(!pImpl->aSubstArr.Count())
134cdf0e10cSrcweir 		ClearNodeSet(sNode);
135cdf0e10cSrcweir 	else
136cdf0e10cSrcweir 	{
137cdf0e10cSrcweir 		Sequence<PropertyValue> aSetValues(4 * pImpl->aSubstArr.Count());
138cdf0e10cSrcweir 		PropertyValue* pSetValues = aSetValues.getArray();
139cdf0e10cSrcweir 		sal_Int32 nSetValue = 0;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 		const OUString sReplaceFont(C2U(cReplaceFont));
142cdf0e10cSrcweir 		const OUString sSubstituteFont(C2U(cSubstituteFont));
143cdf0e10cSrcweir 		const OUString sAlways(C2U(cAlways));
144cdf0e10cSrcweir 		const OUString sOnScreenOnly(C2U(cOnScreenOnly));
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 		const uno::Type& rBoolType = ::getBooleanCppuType();
147cdf0e10cSrcweir 		for(sal_uInt16 i = 0; i < pImpl->aSubstArr.Count(); i++)
148cdf0e10cSrcweir 		{
149cdf0e10cSrcweir 			OUString sPrefix(sNode);
150cdf0e10cSrcweir 			sPrefix += C2U("/_");
151cdf0e10cSrcweir 			sPrefix += OUString::valueOf((sal_Int32)i);
152cdf0e10cSrcweir 			sPrefix += C2U("/");
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 			SubstitutionStructPtr pSubst = pImpl->aSubstArr[i];
155cdf0e10cSrcweir 			pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sReplaceFont;
156cdf0e10cSrcweir 			pSetValues[nSetValue++].Value <<= pSubst->sFont;
157cdf0e10cSrcweir 			pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sSubstituteFont;
158cdf0e10cSrcweir 			pSetValues[nSetValue++].Value <<= pSubst->sReplaceBy;
159cdf0e10cSrcweir 			pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sAlways;
160cdf0e10cSrcweir 			pSetValues[nSetValue++].Value.setValue(&pSubst->bReplaceAlways, rBoolType);
161cdf0e10cSrcweir 			pSetValues[nSetValue].Name = sPrefix; pSetValues[nSetValue].Name += sOnScreenOnly;
162cdf0e10cSrcweir 			pSetValues[nSetValue++].Value.setValue(&pSubst->bReplaceOnScreenOnly, rBoolType);
163cdf0e10cSrcweir 		}
164cdf0e10cSrcweir 		ReplaceSetProperties(sNode, aSetValues);
165cdf0e10cSrcweir 	}
166cdf0e10cSrcweir }
167cdf0e10cSrcweir /*-- 18.01.01 12:08:00---------------------------------------------------
168cdf0e10cSrcweir 
169cdf0e10cSrcweir   -----------------------------------------------------------------------*/
SubstitutionCount() const170cdf0e10cSrcweir sal_Int32 SvtFontSubstConfig::SubstitutionCount() const
171cdf0e10cSrcweir {
172cdf0e10cSrcweir 	return pImpl->aSubstArr.Count();
173cdf0e10cSrcweir }
174cdf0e10cSrcweir /*-- 18.01.01 12:08:00---------------------------------------------------
175cdf0e10cSrcweir 
176cdf0e10cSrcweir   -----------------------------------------------------------------------*/
ClearSubstitutions()177cdf0e10cSrcweir void SvtFontSubstConfig::ClearSubstitutions()
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	pImpl->aSubstArr.DeleteAndDestroy(0, pImpl->aSubstArr.Count());
180cdf0e10cSrcweir }
181cdf0e10cSrcweir /*-- 18.01.01 12:08:00---------------------------------------------------
182cdf0e10cSrcweir 
183cdf0e10cSrcweir   -----------------------------------------------------------------------*/
GetSubstitution(sal_Int32 nPos)184cdf0e10cSrcweir const SubstitutionStruct* SvtFontSubstConfig::GetSubstitution(sal_Int32 nPos)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	DBG_ASSERT(nPos >= 0 && nPos < pImpl->aSubstArr.Count(), "illegal array index");
187cdf0e10cSrcweir 	if(nPos >= 0 && nPos < pImpl->aSubstArr.Count())
188cdf0e10cSrcweir 		return pImpl->aSubstArr[(sal_uInt16)nPos];
189cdf0e10cSrcweir 	return 0;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir /*-- 18.01.01 12:08:01---------------------------------------------------
192cdf0e10cSrcweir 
193cdf0e10cSrcweir   -----------------------------------------------------------------------*/
AddSubstitution(const SubstitutionStruct & rToAdd)194cdf0e10cSrcweir void SvtFontSubstConfig::AddSubstitution(const SubstitutionStruct& rToAdd)
195cdf0e10cSrcweir {
196cdf0e10cSrcweir 	SubstitutionStructPtr pInsert = new SubstitutionStruct(rToAdd);
197cdf0e10cSrcweir 	pImpl->aSubstArr.Insert(pInsert, pImpl->aSubstArr.Count());
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
Apply()200cdf0e10cSrcweir void SvtFontSubstConfig::Apply()
201cdf0e10cSrcweir {
202cdf0e10cSrcweir 	OutputDevice::BeginFontSubstitution();
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 	// Alte Substitution entfernen
205cdf0e10cSrcweir 	sal_uInt16 nOldCount = OutputDevice::GetFontSubstituteCount();
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 	while (nOldCount)
208cdf0e10cSrcweir 		OutputDevice::RemoveFontSubstitute(--nOldCount);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	// Neue Substitution einlesen
211cdf0e10cSrcweir     sal_Int32 nCount = IsEnabled() ? SubstitutionCount() : 0;
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	for (sal_Int32  i = 0; i < nCount; i++)
214cdf0e10cSrcweir 	{
215cdf0e10cSrcweir 	    sal_uInt16 nFlags = 0;
216cdf0e10cSrcweir 		const SubstitutionStruct* pSubs = GetSubstitution(i);
217cdf0e10cSrcweir 		if(pSubs->bReplaceAlways)
218cdf0e10cSrcweir 			nFlags |= FONT_SUBSTITUTE_ALWAYS;
219cdf0e10cSrcweir 		if(pSubs->bReplaceOnScreenOnly)
220cdf0e10cSrcweir 			nFlags |= FONT_SUBSTITUTE_SCREENONLY;
221cdf0e10cSrcweir 		OutputDevice::AddFontSubstitute( String(pSubs->sFont), String(pSubs->sReplaceBy), nFlags );
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 	OutputDevice::EndFontSubstitution();
225cdf0e10cSrcweir }
226