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/Sequence.hxx>
23 #include <o3tl/any.hxx>
24 #include <tools/debug.hxx>
25 #include <vcl/outdev.hxx>
30 using namespace com::sun::star
;
31 using namespace com::sun::star::uno
;
32 using namespace com::sun::star::beans
;
35 const char cReplacement
[] = "Replacement";
36 const char cFontPairs
[] = "FontPairs";
38 const char cReplaceFont
[] = "ReplaceFont";
39 const char cSubstituteFont
[]= "SubstituteFont";
40 const char cOnScreenOnly
[] = "OnScreenOnly";
41 const char cAlways
[] = "Always";
43 typedef std::vector
<SubstitutionStruct
> SubstitutionStructArr
;
45 struct SvtFontSubstConfig_Impl
47 SubstitutionStructArr aSubstArr
;
50 SvtFontSubstConfig::SvtFontSubstConfig() :
51 ConfigItem("Office.Common/Font/Substitution"),
53 pImpl(new SvtFontSubstConfig_Impl
)
55 Sequence
<OUString
> aNames
{ cReplacement
};
56 Sequence
<Any
> aValues
= GetProperties(aNames
);
57 DBG_ASSERT(aValues
.getConstArray()[0].hasValue(), "no value available");
58 if(aValues
.getConstArray()[0].hasValue())
59 bIsEnabled
= *o3tl::doAccess
<bool>(aValues
.getConstArray()[0]);
61 OUString
sPropPrefix(cFontPairs
);
62 const Sequence
<OUString
> aNodeNames
= GetNodeNames(sPropPrefix
, ConfigNameFormat::LocalPath
);
63 Sequence
<OUString
> aPropNames(aNodeNames
.getLength() * 4);
64 OUString
* pNames
= aPropNames
.getArray();
67 for(const OUString
& rNodeName
: aNodeNames
)
69 OUString sStart
= sPropPrefix
+ rNodeName
+ "/";
70 pNames
[nName
++] = sStart
+ cReplaceFont
;
71 pNames
[nName
++] = sStart
+ cSubstituteFont
;
72 pNames
[nName
++] = sStart
+ cAlways
;
73 pNames
[nName
++] = sStart
+ cOnScreenOnly
;
75 Sequence
<Any
> aNodeValues
= GetProperties(aPropNames
);
76 const Any
* pNodeValues
= aNodeValues
.getConstArray();
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 pImpl
->aSubstArr
.push_back(aInsert
);
89 SvtFontSubstConfig::~SvtFontSubstConfig()
93 void SvtFontSubstConfig::Notify( const css::uno::Sequence
< OUString
>& )
97 void SvtFontSubstConfig::ImplCommit()
99 PutProperties({cReplacement
}, {css::uno::Any(bIsEnabled
)});
101 OUString
sNode(cFontPairs
);
102 if(pImpl
->aSubstArr
.empty())
106 Sequence
<PropertyValue
> aSetValues(4 * pImpl
->aSubstArr
.size());
107 PropertyValue
* pSetValues
= aSetValues
.getArray();
108 sal_Int32 nSetValue
= 0;
110 const OUString
sReplaceFont(cReplaceFont
);
111 const OUString
sSubstituteFont(cSubstituteFont
);
112 const OUString
sAlways(cAlways
);
113 const OUString
sOnScreenOnly(cOnScreenOnly
);
115 for(size_t i
= 0; i
< pImpl
->aSubstArr
.size(); i
++)
117 OUString sPrefix
= sNode
+ "/_" + OUString::number(i
) + "/";
119 SubstitutionStruct
& rSubst
= pImpl
->aSubstArr
[i
];
120 pSetValues
[nSetValue
].Name
= sPrefix
; pSetValues
[nSetValue
].Name
+= sReplaceFont
;
121 pSetValues
[nSetValue
++].Value
<<= rSubst
.sFont
;
122 pSetValues
[nSetValue
].Name
= sPrefix
; pSetValues
[nSetValue
].Name
+= sSubstituteFont
;
123 pSetValues
[nSetValue
++].Value
<<= rSubst
.sReplaceBy
;
124 pSetValues
[nSetValue
].Name
= sPrefix
; pSetValues
[nSetValue
].Name
+= sAlways
;
125 pSetValues
[nSetValue
++].Value
<<= rSubst
.bReplaceAlways
;
126 pSetValues
[nSetValue
].Name
= sPrefix
; pSetValues
[nSetValue
].Name
+= sOnScreenOnly
;
127 pSetValues
[nSetValue
++].Value
<<= rSubst
.bReplaceOnScreenOnly
;
129 ReplaceSetProperties(sNode
, aSetValues
);
133 sal_Int32
SvtFontSubstConfig::SubstitutionCount() const
135 return pImpl
->aSubstArr
.size();
138 void SvtFontSubstConfig::ClearSubstitutions()
140 pImpl
->aSubstArr
.clear();
143 const SubstitutionStruct
* SvtFontSubstConfig::GetSubstitution(sal_Int32 nPos
)
145 sal_Int32 nCount
= static_cast<sal_Int32
>(pImpl
->aSubstArr
.size());
146 DBG_ASSERT(nPos
>= 0 && nPos
< nCount
, "illegal array index");
147 if(nPos
>= 0 && nPos
< nCount
)
148 return &pImpl
->aSubstArr
[nPos
];
152 void SvtFontSubstConfig::AddSubstitution(const SubstitutionStruct
& rToAdd
)
154 pImpl
->aSubstArr
.push_back(rToAdd
);
157 void SvtFontSubstConfig::Apply()
159 OutputDevice::BeginFontSubstitution();
161 // remove old substitutions
162 OutputDevice::RemoveFontsSubstitute();
164 // read new substitutions
165 sal_Int32 nCount
= IsEnabled() ? SubstitutionCount() : 0;
167 for (sal_Int32 i
= 0; i
< nCount
; i
++)
169 AddFontSubstituteFlags nFlags
= AddFontSubstituteFlags::NONE
;
170 const SubstitutionStruct
* pSubs
= GetSubstitution(i
);
171 if(pSubs
->bReplaceAlways
)
172 nFlags
|= AddFontSubstituteFlags::ALWAYS
;
173 if(pSubs
->bReplaceOnScreenOnly
)
174 nFlags
|= AddFontSubstituteFlags::ScreenOnly
;
175 OutputDevice::AddFontSubstitute( pSubs
->sFont
, pSubs
->sReplaceBy
, nFlags
);
178 OutputDevice::EndFontSubstitution();
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */