1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/uno/Any.hxx>
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <tools/debug.hxx>
25 #include <vcl/outdev.hxx>
27 #include <boost/ptr_container/ptr_vector.hpp>
30 using namespace com::sun::star
;
31 using namespace com::sun::star::uno
;
32 using namespace com::sun::star::beans
;
35 const sal_Char cReplacement
[] = "Replacement";
36 const sal_Char cFontPairs
[] = "FontPairs";
38 const sal_Char cReplaceFont
[] = "ReplaceFont";
39 const sal_Char cSubstituteFont
[]= "SubstituteFont";
40 const sal_Char cOnScreenOnly
[] = "OnScreenOnly";
41 const sal_Char cAlways
[] = "Always";
43 typedef boost::ptr_vector
<SubstitutionStruct
> SubstitutionStructArr
;
45 struct SvtFontSubstConfig_Impl
47 SubstitutionStructArr aSubstArr
;
50 SvtFontSubstConfig::SvtFontSubstConfig() :
51 ConfigItem(OUString("Office.Common/Font/Substitution")),
52 bIsEnabled(sal_False
),
53 pImpl(new SvtFontSubstConfig_Impl
)
55 Sequence
<OUString
> aNames(1);
56 aNames
.getArray()[0] = cReplacement
;
57 Sequence
<Any
> aValues
= GetProperties(aNames
);
58 DBG_ASSERT(aValues
.getConstArray()[0].hasValue(), "no value available");
59 if(aValues
.getConstArray()[0].hasValue())
60 bIsEnabled
= *(sal_Bool
*)aValues
.getConstArray()[0].getValue();
62 OUString
sPropPrefix(cFontPairs
);
63 Sequence
<OUString
> aNodeNames
= GetNodeNames(sPropPrefix
, CONFIG_NAME_LOCAL_PATH
);
64 const OUString
* pNodeNames
= aNodeNames
.getConstArray();
65 Sequence
<OUString
> aPropNames(aNodeNames
.getLength() * 4);
66 OUString
* pNames
= aPropNames
.getArray();
70 for(nNode
= 0; nNode
< aNodeNames
.getLength(); nNode
++)
72 OUString sStart
= sPropPrefix
+ pNodeNames
[nNode
] + "/";
73 pNames
[nName
] = sStart
; pNames
[nName
++] += cReplaceFont
;
74 pNames
[nName
] = sStart
; pNames
[nName
++] += cSubstituteFont
;
75 pNames
[nName
] = sStart
; pNames
[nName
++] += cAlways
;
76 pNames
[nName
] = sStart
; pNames
[nName
++] += cOnScreenOnly
;
78 Sequence
<Any
> aNodeValues
= GetProperties(aPropNames
);
79 const Any
* pNodeValues
= aNodeValues
.getConstArray();
81 for(nNode
= 0; nNode
< aNodeNames
.getLength(); nNode
++)
83 SubstitutionStruct
* pInsert
= new SubstitutionStruct
;
84 pNodeValues
[nName
++] >>= pInsert
->sFont
;
85 pNodeValues
[nName
++] >>= pInsert
->sReplaceBy
;
86 pInsert
->bReplaceAlways
= *(sal_Bool
*)pNodeValues
[nName
++].getValue();
87 pInsert
->bReplaceOnScreenOnly
= *(sal_Bool
*)pNodeValues
[nName
++].getValue();
88 pImpl
->aSubstArr
.push_back(pInsert
);
92 SvtFontSubstConfig::~SvtFontSubstConfig()
97 void SvtFontSubstConfig::Notify( const com::sun::star::uno::Sequence
< OUString
>& )
101 void SvtFontSubstConfig::Commit()
103 Sequence
<OUString
> aNames(1);
104 aNames
.getArray()[0] = cReplacement
;
105 Sequence
<Any
> aValues(1);
106 aValues
.getArray()[0].setValue(&bIsEnabled
, ::getBooleanCppuType());
107 PutProperties(aNames
, aValues
);
109 OUString
sNode(cFontPairs
);
110 if(pImpl
->aSubstArr
.empty())
114 Sequence
<PropertyValue
> aSetValues(4 * pImpl
->aSubstArr
.size());
115 PropertyValue
* pSetValues
= aSetValues
.getArray();
116 sal_Int32 nSetValue
= 0;
118 const OUString
sReplaceFont(cReplaceFont
);
119 const OUString
sSubstituteFont(cSubstituteFont
);
120 const OUString
sAlways(cAlways
);
121 const OUString
sOnScreenOnly(cOnScreenOnly
);
123 const uno::Type
& rBoolType
= ::getBooleanCppuType();
124 for(size_t i
= 0; i
< pImpl
->aSubstArr
.size(); i
++)
126 OUString sPrefix
= sNode
+ "/_" + OUString::number(i
) + "/";
128 SubstitutionStruct
& pSubst
= pImpl
->aSubstArr
[i
];
129 pSetValues
[nSetValue
].Name
= sPrefix
; pSetValues
[nSetValue
].Name
+= sReplaceFont
;
130 pSetValues
[nSetValue
++].Value
<<= pSubst
.sFont
;
131 pSetValues
[nSetValue
].Name
= sPrefix
; pSetValues
[nSetValue
].Name
+= sSubstituteFont
;
132 pSetValues
[nSetValue
++].Value
<<= pSubst
.sReplaceBy
;
133 pSetValues
[nSetValue
].Name
= sPrefix
; pSetValues
[nSetValue
].Name
+= sAlways
;
134 pSetValues
[nSetValue
++].Value
.setValue(&pSubst
.bReplaceAlways
, rBoolType
);
135 pSetValues
[nSetValue
].Name
= sPrefix
; pSetValues
[nSetValue
].Name
+= sOnScreenOnly
;
136 pSetValues
[nSetValue
++].Value
.setValue(&pSubst
.bReplaceOnScreenOnly
, rBoolType
);
138 ReplaceSetProperties(sNode
, aSetValues
);
142 sal_Int32
SvtFontSubstConfig::SubstitutionCount() const
144 return pImpl
->aSubstArr
.size();
147 void SvtFontSubstConfig::ClearSubstitutions()
149 pImpl
->aSubstArr
.clear();
152 const SubstitutionStruct
* SvtFontSubstConfig::GetSubstitution(sal_Int32 nPos
)
154 sal_Int32 nCount
= static_cast<sal_Int32
>(pImpl
->aSubstArr
.size());
155 DBG_ASSERT(nPos
>= 0 && nPos
< nCount
, "illegal array index");
156 if(nPos
>= 0 && nPos
< nCount
)
157 return &pImpl
->aSubstArr
[nPos
];
161 void SvtFontSubstConfig::AddSubstitution(const SubstitutionStruct
& rToAdd
)
163 pImpl
->aSubstArr
.push_back(new SubstitutionStruct(rToAdd
));
166 void SvtFontSubstConfig::Apply()
168 OutputDevice::BeginFontSubstitution();
170 // remove old substitions
171 sal_uInt16 nOldCount
= OutputDevice::GetFontSubstituteCount();
174 OutputDevice::RemoveFontSubstitute(--nOldCount
);
176 // read new substitutions
177 sal_Int32 nCount
= IsEnabled() ? SubstitutionCount() : 0;
179 for (sal_Int32 i
= 0; i
< nCount
; i
++)
181 sal_uInt16 nFlags
= 0;
182 const SubstitutionStruct
* pSubs
= GetSubstitution(i
);
183 if(pSubs
->bReplaceAlways
)
184 nFlags
|= FONT_SUBSTITUTE_ALWAYS
;
185 if(pSubs
->bReplaceOnScreenOnly
)
186 nFlags
|= FONT_SUBSTITUTE_SCREENONLY
;
187 OutputDevice::AddFontSubstitute( pSubs
->sFont
, pSubs
->sReplaceBy
, nFlags
);
190 OutputDevice::EndFontSubstitution();
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */