resolves tdf#164985 Incorrect bookmarks list in bookmark dialog after
[LibreOffice.git] / sw / source / uibase / config / fontcfg.cxx
bloba17104f4803f46d583608f3f23db890c043a3f63
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <fontcfg.hxx>
21 #include <i18nlangtag/mslangid.hxx>
22 #include <osl/diagnose.h>
23 #include <tools/UnitConversion.hxx>
24 #include <vcl/outdev.hxx>
25 #include <comphelper/configuration.hxx>
26 #include <unotools/lingucfg.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <com/sun/star/i18n/ScriptType.hpp>
30 using namespace utl;
31 using namespace com::sun::star::uno;
33 static LanguageType lcl_LanguageOfType(sal_Int16 nType, LanguageType eWestern, LanguageType eCJK, LanguageType eCTL)
35 return nType < FONT_STANDARD_CJK
36 ? eWestern
37 : nType >= FONT_STANDARD_CTL ? eCTL : eCJK;
40 Sequence<OUString> const & SwStdFontConfig::GetPropertyNames()
42 static Sequence<OUString> aNames {
43 u"DefaultFont/Standard"_ustr, // 0
44 u"DefaultFont/Heading"_ustr, // 1
45 u"DefaultFont/List"_ustr, // 2
46 u"DefaultFont/Caption"_ustr, // 3
47 u"DefaultFont/Index"_ustr, // 4
48 u"DefaultFontCJK/Standard"_ustr, // 5
49 u"DefaultFontCJK/Heading"_ustr, // 6
50 u"DefaultFontCJK/List"_ustr, // 7
51 u"DefaultFontCJK/Caption"_ustr, // 8
52 u"DefaultFontCJK/Index"_ustr, // 9
53 u"DefaultFontCTL/Standard"_ustr, // 10
54 u"DefaultFontCTL/Heading"_ustr, // 11
55 u"DefaultFontCTL/List"_ustr, // 12
56 u"DefaultFontCTL/Caption"_ustr, // 13
57 u"DefaultFontCTL/Index"_ustr, // 14
58 u"DefaultFont/StandardHeight"_ustr, // 15
59 u"DefaultFont/HeadingHeight"_ustr, // 16
60 u"DefaultFont/ListHeight"_ustr, // 17
61 u"DefaultFont/CaptionHeight"_ustr, // 18
62 u"DefaultFont/IndexHeight"_ustr, // 19
63 u"DefaultFontCJK/StandardHeight"_ustr, // 20
64 u"DefaultFontCJK/HeadingHeight"_ustr, // 21
65 u"DefaultFontCJK/ListHeight"_ustr, // 22
66 u"DefaultFontCJK/CaptionHeight"_ustr, // 23
67 u"DefaultFontCJK/IndexHeight"_ustr, // 24
68 u"DefaultFontCTL/StandardHeight"_ustr, // 25
69 u"DefaultFontCTL/HeadingHeight"_ustr, // 26
70 u"DefaultFontCTL/ListHeight"_ustr, // 27
71 u"DefaultFontCTL/CaptionHeight"_ustr, // 28
72 u"DefaultFontCTL/IndexHeight"_ustr // 29
74 return aNames;
77 SwStdFontConfig::SwStdFontConfig() :
78 utl::ConfigItem(u"Office.Writer"_ustr)
80 SvtLinguOptions aLinguOpt;
82 if (!comphelper::IsFuzzing())
83 SvtLinguConfig().GetOptions( aLinguOpt );
85 LanguageType eWestern = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN),
86 eCJK = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN),
87 eCTL = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX);
89 for(sal_Int16 i = 0; i < DEF_FONT_COUNT; i++)
91 m_sDefaultFonts[i] = GetDefaultFor(i,
92 lcl_LanguageOfType(i, eWestern, eCJK, eCTL));
93 m_nDefaultFontHeight[i] = -1;
96 Sequence<OUString> aNames = GetPropertyNames();
97 Sequence<Any> aValues = GetProperties(aNames);
98 const Any* pValues = aValues.getConstArray();
99 OSL_ENSURE(aValues.getLength() == aNames.getLength(), "GetProperties failed");
100 if(aValues.getLength() != aNames.getLength())
101 return;
103 for(int nProp = 0; nProp < aNames.getLength(); nProp++)
105 if(pValues[nProp].hasValue())
107 if( nProp < DEF_FONT_COUNT)
109 OUString sVal;
110 pValues[nProp] >>= sVal;
111 m_sDefaultFonts[nProp] = sVal;
113 else
115 pValues[nProp] >>= m_nDefaultFontHeight[nProp - DEF_FONT_COUNT];
116 m_nDefaultFontHeight[nProp - DEF_FONT_COUNT] = o3tl::toTwips(m_nDefaultFontHeight[nProp - DEF_FONT_COUNT], o3tl::Length::mm100);
122 void SwStdFontConfig::ImplCommit()
124 Sequence<OUString> aNames = GetPropertyNames();
125 Sequence<Any> aValues(aNames.getLength());
126 Any* pValues = aValues.getArray();
127 SvtLinguOptions aLinguOpt;
129 SvtLinguConfig().GetOptions( aLinguOpt );
131 LanguageType eWestern = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN),
132 eCJK = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN),
133 eCTL = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX);
135 for(sal_uInt16 nProp = 0;
136 nProp < sal::static_int_cast< sal_uInt16, sal_Int32 >( aNames.getLength() );
137 nProp++)
139 if( nProp < DEF_FONT_COUNT )
141 if(GetDefaultFor(nProp, lcl_LanguageOfType(nProp, eWestern, eCJK, eCTL)) != m_sDefaultFonts[nProp])
142 pValues[nProp] <<= m_sDefaultFonts[nProp];
144 else
146 if(m_nDefaultFontHeight[nProp - DEF_FONT_COUNT] > 0)
147 pValues[nProp] <<= static_cast<sal_Int32>(convertTwipToMm100(m_nDefaultFontHeight[nProp - DEF_FONT_COUNT]));
150 PutProperties(aNames, aValues);
153 SwStdFontConfig::~SwStdFontConfig()
157 bool SwStdFontConfig::IsFontDefault(sal_uInt16 nFontType) const
159 bool bSame = false;
160 SvtLinguOptions aLinguOpt;
162 if (!comphelper::IsFuzzing())
163 SvtLinguConfig().GetOptions(aLinguOpt);
165 LanguageType eWestern = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN),
166 eCJK = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN),
167 eCTL = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX);
169 OUString sDefFont(GetDefaultFor(FONT_STANDARD, eWestern));
170 OUString sDefFontCJK(GetDefaultFor(FONT_STANDARD_CJK, eCJK));
171 OUString sDefFontCTL(GetDefaultFor(FONT_STANDARD_CTL, eCTL));
172 LanguageType eLang = lcl_LanguageOfType(nFontType, eWestern, eCJK, eCTL);
173 switch( nFontType )
175 case FONT_STANDARD:
176 bSame = m_sDefaultFonts[nFontType] == sDefFont;
177 break;
178 case FONT_STANDARD_CJK:
179 bSame = m_sDefaultFonts[nFontType] == sDefFontCJK;
180 break;
181 case FONT_STANDARD_CTL:
182 bSame = m_sDefaultFonts[nFontType] == sDefFontCTL;
183 break;
184 case FONT_OUTLINE :
185 case FONT_OUTLINE_CJK :
186 case FONT_OUTLINE_CTL :
187 bSame = m_sDefaultFonts[nFontType] ==
188 GetDefaultFor(nFontType, eLang);
189 break;
190 case FONT_LIST :
191 case FONT_CAPTION :
192 case FONT_INDEX :
193 bSame = m_sDefaultFonts[nFontType] == sDefFont &&
194 m_sDefaultFonts[FONT_STANDARD] == sDefFont;
195 break;
196 case FONT_LIST_CJK :
197 case FONT_CAPTION_CJK :
198 case FONT_INDEX_CJK :
200 bool b1 = m_sDefaultFonts[FONT_STANDARD_CJK] == sDefFontCJK;
201 bSame = b1 && m_sDefaultFonts[nFontType] == sDefFontCJK;
203 break;
204 case FONT_LIST_CTL :
205 case FONT_CAPTION_CTL :
206 case FONT_INDEX_CTL :
208 bool b1 = m_sDefaultFonts[FONT_STANDARD_CJK] == sDefFontCTL;
209 bSame = b1 && m_sDefaultFonts[nFontType] == sDefFontCTL;
211 break;
213 return bSame;
216 OUString SwStdFontConfig::GetDefaultFor(sal_uInt16 nFontType, LanguageType eLang)
218 DefaultFontType nFontId;
219 switch( nFontType )
221 case FONT_OUTLINE :
222 nFontId = DefaultFontType::LATIN_HEADING;
223 break;
224 case FONT_OUTLINE_CJK :
225 nFontId = DefaultFontType::CJK_HEADING;
226 break;
227 case FONT_OUTLINE_CTL :
228 nFontId = DefaultFontType::CTL_HEADING;
229 break;
230 case FONT_STANDARD_CJK:
231 case FONT_LIST_CJK :
232 case FONT_CAPTION_CJK :
233 case FONT_INDEX_CJK :
234 nFontId = DefaultFontType::CJK_TEXT;
235 break;
236 case FONT_STANDARD_CTL:
237 case FONT_LIST_CTL :
238 case FONT_CAPTION_CTL :
239 case FONT_INDEX_CTL :
240 nFontId = DefaultFontType::CTL_TEXT;
241 break;
242 default:
243 nFontId = DefaultFontType::LATIN_TEXT;
245 vcl::Font aFont = OutputDevice::GetDefaultFont(nFontId, eLang, GetDefaultFontFlags::OnlyOne);
246 return aFont.GetFamilyName();
249 sal_Int32 SwStdFontConfig::GetDefaultHeightFor(sal_uInt16 nFontType, LanguageType eLang)
251 sal_Int32 nRet = FONTSIZE_DEFAULT;
252 switch( nFontType )
254 case FONT_OUTLINE:
255 case FONT_OUTLINE_CJK:
256 case FONT_OUTLINE_CTL:
257 nRet = FONTSIZE_OUTLINE;
258 break;
259 case FONT_STANDARD_CJK:
260 nRet = FONTSIZE_CJK_DEFAULT;
261 break;
263 if( eLang == LANGUAGE_THAI && nFontType >= FONT_STANDARD_CTL )
265 nRet = nRet * 4 / 3;
267 if( eLang == LANGUAGE_KOREAN )
269 nRet = FONTSIZE_KOREAN_DEFAULT;
271 return nRet;
274 void SwStdFontConfig::ChangeInt( sal_uInt16 nFontType, sal_Int32 nHeight )
276 OSL_ENSURE( nFontType < DEF_FONT_COUNT, "invalid index in SwStdFontConfig::ChangeInt()");
277 if( nFontType >= DEF_FONT_COUNT || m_nDefaultFontHeight[nFontType] == nHeight)
278 return;
280 SvtLinguOptions aLinguOpt;
281 if (!comphelper::IsFuzzing())
282 SvtLinguConfig().GetOptions( aLinguOpt );
284 LanguageType eWestern = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN),
285 eCJK = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN),
286 eCTL = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX);
288 // #i92090# default height value sets back to -1
289 const sal_Int32 nDefaultHeight = GetDefaultHeightFor(nFontType, lcl_LanguageOfType(nFontType, eWestern, eCJK, eCTL));
290 const bool bIsDefaultHeight = nHeight == nDefaultHeight;
291 if( bIsDefaultHeight && m_nDefaultFontHeight[nFontType] > 0 )
293 SetModified();
294 m_nDefaultFontHeight[nFontType] = -1;
296 else if( !bIsDefaultHeight && nHeight != m_nDefaultFontHeight[nFontType] )
298 SetModified();
299 m_nDefaultFontHeight[nFontType] = nHeight;
303 sal_Int32 SwStdFontConfig::GetFontHeight( sal_uInt8 nFont, sal_uInt8 nScriptType, LanguageType eLang )
305 OSL_ENSURE(nFont + FONT_PER_GROUP * nScriptType < DEF_FONT_COUNT, "wrong index in SwStdFontConfig::GetFontHeight()");
306 sal_Int32 nRet = m_nDefaultFontHeight[nFont + FONT_PER_GROUP * nScriptType];
307 if(nRet <= 0)
308 return GetDefaultHeightFor(nFont + FONT_PER_GROUP * nScriptType, eLang);
309 return nRet;
312 void SwStdFontConfig::Notify( const css::uno::Sequence< OUString >& ) {}
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */