LibreOffice Module sw (master)  1
fontcfg.cxx
Go to the documentation of this file.
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 <fontcfg.hxx>
21 #include <i18nlangtag/mslangid.hxx>
22 #include <osl/diagnose.h>
23 #include <vcl/outdev.hxx>
24 #include <unotools/configmgr.hxx>
25 #include <unotools/lingucfg.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <com/sun/star/i18n/ScriptType.hpp>
28 
29 using namespace utl;
30 using namespace com::sun::star::uno;
31 
32 static LanguageType lcl_LanguageOfType(sal_Int16 nType, LanguageType eWestern, LanguageType eCJK, LanguageType eCTL)
33 {
34  return nType < FONT_STANDARD_CJK
35  ? eWestern
36  : nType >= FONT_STANDARD_CTL ? eCTL : eCJK;
37 }
38 
40 {
41  static Sequence<OUString> aNames {
42  "DefaultFont/Standard", // 0
43  "DefaultFont/Heading", // 1
44  "DefaultFont/List", // 2
45  "DefaultFont/Caption", // 3
46  "DefaultFont/Index", // 4
47  "DefaultFontCJK/Standard", // 5
48  "DefaultFontCJK/Heading", // 6
49  "DefaultFontCJK/List", // 7
50  "DefaultFontCJK/Caption", // 8
51  "DefaultFontCJK/Index", // 9
52  "DefaultFontCTL/Standard", // 10
53  "DefaultFontCTL/Heading", // 11
54  "DefaultFontCTL/List", // 12
55  "DefaultFontCTL/Caption", // 13
56  "DefaultFontCTL/Index", // 14
57  "DefaultFont/StandardHeight", // 15
58  "DefaultFont/HeadingHeight", // 16
59  "DefaultFont/ListHeight", // 17
60  "DefaultFont/CaptionHeight", // 18
61  "DefaultFont/IndexHeight", // 19
62  "DefaultFontCJK/StandardHeight", // 20
63  "DefaultFontCJK/HeadingHeight", // 21
64  "DefaultFontCJK/ListHeight", // 22
65  "DefaultFontCJK/CaptionHeight", // 23
66  "DefaultFontCJK/IndexHeight", // 24
67  "DefaultFontCTL/StandardHeight", // 25
68  "DefaultFontCTL/HeadingHeight", // 26
69  "DefaultFontCTL/ListHeight", // 27
70  "DefaultFontCTL/CaptionHeight", // 28
71  "DefaultFontCTL/IndexHeight" // 29
72  };
73  return aNames;
74 }
75 
77  utl::ConfigItem("Office.Writer")
78 {
79  SvtLinguOptions aLinguOpt;
80 
82  SvtLinguConfig().GetOptions( aLinguOpt );
83 
84  LanguageType eWestern = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN),
85  eCJK = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN),
86  eCTL = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX);
87 
88  for(sal_Int16 i = 0; i < DEF_FONT_COUNT; i++)
89  {
91  lcl_LanguageOfType(i, eWestern, eCJK, eCTL));
92  nDefaultFontHeight[i] = -1;
93  }
94 
96  Sequence<Any> aValues = GetProperties(aNames);
97  const Any* pValues = aValues.getConstArray();
98  OSL_ENSURE(aValues.getLength() == aNames.getLength(), "GetProperties failed");
99  if(aValues.getLength() == aNames.getLength())
100  {
101  for(int nProp = 0; nProp < aNames.getLength(); nProp++)
102  {
103  if(pValues[nProp].hasValue())
104  {
105  if( nProp < DEF_FONT_COUNT)
106  {
107  OUString sVal;
108  pValues[nProp] >>= sVal;
109  sDefaultFonts[nProp] = sVal;
110  }
111  else
112  {
113  pValues[nProp] >>= nDefaultFontHeight[nProp - DEF_FONT_COUNT];
114  nDefaultFontHeight[nProp - DEF_FONT_COUNT] = convertMm100ToTwip(nDefaultFontHeight[nProp - DEF_FONT_COUNT]);
115  }
116  }
117  }
118  }
119 }
120 
122 {
124  Sequence<Any> aValues(aNames.getLength());
125  Any* pValues = aValues.getArray();
126  SvtLinguOptions aLinguOpt;
127 
128  SvtLinguConfig().GetOptions( aLinguOpt );
129 
130  LanguageType eWestern = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN),
131  eCJK = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN),
132  eCTL = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX);
133 
134  for(sal_uInt16 nProp = 0;
135  nProp < sal::static_int_cast< sal_uInt16, sal_Int32 >( aNames.getLength() );
136  nProp++)
137  {
138  if( nProp < DEF_FONT_COUNT )
139  {
140  if(GetDefaultFor(nProp, lcl_LanguageOfType(nProp, eWestern, eCJK, eCTL)) != sDefaultFonts[nProp])
141  pValues[nProp] <<= sDefaultFonts[nProp];
142  }
143  else
144  {
145  if(nDefaultFontHeight[nProp - DEF_FONT_COUNT] > 0)
146  pValues[nProp] <<= static_cast<sal_Int32>(convertTwipToMm100(nDefaultFontHeight[nProp - DEF_FONT_COUNT]));
147  }
148  }
149  PutProperties(aNames, aValues);
150 }
151 
153 {
154 }
155 
156 bool SwStdFontConfig::IsFontDefault(sal_uInt16 nFontType) const
157 {
158  bool bSame = false;
159  SvtLinguOptions aLinguOpt;
160 
162  SvtLinguConfig().GetOptions(aLinguOpt);
163 
164  LanguageType eWestern = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN),
165  eCJK = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN),
166  eCTL = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX);
167 
168  OUString sDefFont(GetDefaultFor(FONT_STANDARD, eWestern));
169  OUString sDefFontCJK(GetDefaultFor(FONT_STANDARD_CJK, eCJK));
170  OUString sDefFontCTL(GetDefaultFor(FONT_STANDARD_CTL, eCTL));
171  LanguageType eLang = lcl_LanguageOfType(nFontType, eWestern, eCJK, eCTL);
172  switch( nFontType )
173  {
174  case FONT_STANDARD:
175  bSame = sDefaultFonts[nFontType] == sDefFont;
176  break;
177  case FONT_STANDARD_CJK:
178  bSame = sDefaultFonts[nFontType] == sDefFontCJK;
179  break;
180  case FONT_STANDARD_CTL:
181  bSame = sDefaultFonts[nFontType] == sDefFontCTL;
182  break;
183  case FONT_OUTLINE :
184  case FONT_OUTLINE_CJK :
185  case FONT_OUTLINE_CTL :
186  bSame = sDefaultFonts[nFontType] ==
187  GetDefaultFor(nFontType, eLang);
188  break;
189  case FONT_LIST :
190  case FONT_CAPTION :
191  case FONT_INDEX :
192  bSame = sDefaultFonts[nFontType] == sDefFont &&
193  sDefaultFonts[FONT_STANDARD] == sDefFont;
194  break;
195  case FONT_LIST_CJK :
196  case FONT_CAPTION_CJK :
197  case FONT_INDEX_CJK :
198  {
199  bool b1 = sDefaultFonts[FONT_STANDARD_CJK] == sDefFontCJK;
200  bSame = b1 && sDefaultFonts[nFontType] == sDefFontCJK;
201  }
202  break;
203  case FONT_LIST_CTL :
204  case FONT_CAPTION_CTL :
205  case FONT_INDEX_CTL :
206  {
207  bool b1 = sDefaultFonts[FONT_STANDARD_CJK] == sDefFontCTL;
208  bSame = b1 && sDefaultFonts[nFontType] == sDefFontCTL;
209  }
210  break;
211  }
212  return bSame;
213 }
214 
215 OUString SwStdFontConfig::GetDefaultFor(sal_uInt16 nFontType, LanguageType eLang)
216 {
217  DefaultFontType nFontId;
218  switch( nFontType )
219  {
220  case FONT_OUTLINE :
221  nFontId = DefaultFontType::LATIN_HEADING;
222  break;
223  case FONT_OUTLINE_CJK :
224  nFontId = DefaultFontType::CJK_HEADING;
225  break;
226  case FONT_OUTLINE_CTL :
227  nFontId = DefaultFontType::CTL_HEADING;
228  break;
229  case FONT_STANDARD_CJK:
230  case FONT_LIST_CJK :
231  case FONT_CAPTION_CJK :
232  case FONT_INDEX_CJK :
233  nFontId = DefaultFontType::CJK_TEXT;
234  break;
235  case FONT_STANDARD_CTL:
236  case FONT_LIST_CTL :
237  case FONT_CAPTION_CTL :
238  case FONT_INDEX_CTL :
239  nFontId = DefaultFontType::CTL_TEXT;
240  break;
241  default:
242  nFontId = DefaultFontType::LATIN_TEXT;
243  }
244  vcl::Font aFont = OutputDevice::GetDefaultFont(nFontId, eLang, GetDefaultFontFlags::OnlyOne);
245  return aFont.GetFamilyName();
246 }
247 
248 sal_Int32 SwStdFontConfig::GetDefaultHeightFor(sal_uInt16 nFontType, LanguageType eLang)
249 {
250  sal_Int32 nRet = FONTSIZE_DEFAULT;
251  switch( nFontType )
252  {
253  case FONT_OUTLINE:
254  case FONT_OUTLINE_CJK:
255  case FONT_OUTLINE_CTL:
256  nRet = FONTSIZE_OUTLINE;
257  break;
258  case FONT_STANDARD_CJK:
259  nRet = FONTSIZE_CJK_DEFAULT;
260  break;
261  }
262  if( eLang == LANGUAGE_THAI && nFontType >= FONT_STANDARD_CTL )
263  {
264  nRet = nRet * 4 / 3;
265  }
266  return nRet;
267 }
268 
269 void SwStdFontConfig::ChangeInt( sal_uInt16 nFontType, sal_Int32 nHeight )
270 {
271  OSL_ENSURE( nFontType < DEF_FONT_COUNT, "invalid index in SwStdFontConfig::ChangeInt()");
272  if( nFontType < DEF_FONT_COUNT && nDefaultFontHeight[nFontType] != nHeight)
273  {
274  SvtLinguOptions aLinguOpt;
276  SvtLinguConfig().GetOptions( aLinguOpt );
277 
278  LanguageType eWestern = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage, css::i18n::ScriptType::LATIN),
279  eCJK = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CJK, css::i18n::ScriptType::ASIAN),
280  eCTL = MsLangId::resolveSystemLanguageByScriptType(aLinguOpt.nDefaultLanguage_CTL, css::i18n::ScriptType::COMPLEX);
281 
282  // #i92090# default height value sets back to -1
283  const sal_Int32 nDefaultHeight = GetDefaultHeightFor(nFontType, lcl_LanguageOfType(nFontType, eWestern, eCJK, eCTL));
284  const bool bIsDefaultHeight = nHeight == nDefaultHeight;
285  if( bIsDefaultHeight && nDefaultFontHeight[nFontType] > 0 )
286  {
287  SetModified();
288  nDefaultFontHeight[nFontType] = -1;
289  }
290  else if( !bIsDefaultHeight && nHeight != nDefaultFontHeight[nFontType] )
291  {
292  SetModified();
293  nDefaultFontHeight[nFontType] = nHeight;
294  }
295  }
296 }
297 
298 sal_Int32 SwStdFontConfig::GetFontHeight( sal_uInt8 nFont, sal_uInt8 nScriptType, LanguageType eLang )
299 {
300  OSL_ENSURE(nFont + FONT_PER_GROUP * nScriptType < DEF_FONT_COUNT, "wrong index in SwStdFontConfig::GetFontHeight()");
301  sal_Int32 nRet = nDefaultFontHeight[nFont + FONT_PER_GROUP * nScriptType];
302  if(nRet <= 0)
303  return GetDefaultHeightFor(nFont + FONT_PER_GROUP * nScriptType, eLang);
304  return nRet;
305 }
306 
307 void SwStdFontConfig::Notify( const css::uno::Sequence< OUString >& ) {}
308 
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static vcl::Font GetDefaultFont(DefaultFontType nType, LanguageType eLang, GetDefaultFontFlags nFlags, const OutputDevice *pOutDev=nullptr)
#define FONTSIZE_OUTLINE
Definition: fontcfg.hxx:53
#define FONT_STANDARD
Definition: fontcfg.hxx:27
virtual void Notify(const css::uno::Sequence< OUString > &aPropertyNames) override
Definition: fontcfg.cxx:307
const OUString & GetFamilyName() const
#define FONT_OUTLINE
Definition: fontcfg.hxx:28
#define FONT_STANDARD_CJK
Definition: fontcfg.hxx:32
#define FONT_LIST_CTL
Definition: fontcfg.hxx:39
#define FONT_OUTLINE_CJK
Definition: fontcfg.hxx:33
#define FONT_LIST_CJK
Definition: fontcfg.hxx:34
static SAL_DLLPRIVATE css::uno::Sequence< OUString > const & GetPropertyNames()
Definition: fontcfg.cxx:39
LanguageType nDefaultLanguage_CJK
#define FONT_LIST
Definition: fontcfg.hxx:29
static LanguageType resolveSystemLanguageByScriptType(LanguageType nLang, sal_Int16 nType)
#define FONT_INDEX_CTL
Definition: fontcfg.hxx:41
sal_Int32 GetFontHeight(sal_uInt8 nFont, sal_uInt8 nScriptType, LanguageType eLang)
Definition: fontcfg.cxx:298
#define FONT_CAPTION
Definition: fontcfg.hxx:30
#define FONTSIZE_DEFAULT
Definition: fontcfg.hxx:51
#define FONT_OUTLINE_CTL
Definition: fontcfg.hxx:38
#define DEF_FONT_COUNT
Definition: fontcfg.hxx:42
#define FONTSIZE_CJK_DEFAULT
Definition: fontcfg.hxx:52
static bool IsFuzzing()
#define FONT_PER_GROUP
Definition: fontcfg.hxx:44
static OUString GetDefaultFor(sal_uInt16 nFontType, LanguageType eLang)
Definition: fontcfg.cxx:215
#define FONT_INDEX_CJK
Definition: fontcfg.hxx:36
int i
bool IsFontDefault(sal_uInt16 nFontType) const
Definition: fontcfg.cxx:156
bool PutProperties(const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues)
void GetOptions(SvtLinguOptions &rOptions) const
static LanguageType lcl_LanguageOfType(sal_Int16 nType, LanguageType eWestern, LanguageType eCJK, LanguageType eCTL)
Definition: fontcfg.cxx:32
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
#define FONT_INDEX
Definition: fontcfg.hxx:31
virtual void ImplCommit() override
Definition: fontcfg.cxx:121
#define LANGUAGE_THAI
const PropertyValue * pValues
virtual ~SwStdFontConfig() override
Definition: fontcfg.cxx:152
constexpr sal_Int64 convertTwipToMm100(sal_Int64 n)
unsigned char sal_uInt8
LanguageType nDefaultLanguage
constexpr sal_Int64 convertMm100ToTwip(sal_Int64 n)
#define FONT_STANDARD_CTL
Definition: fontcfg.hxx:37
DefaultFontType
OUString sDefaultFonts[DEF_FONT_COUNT]
Definition: fontcfg.hxx:57
#define FONT_CAPTION_CTL
Definition: fontcfg.hxx:40
#define FONT_CAPTION_CJK
Definition: fontcfg.hxx:35
void ChangeInt(sal_uInt16 nFontType, sal_Int32 nHeight)
Definition: fontcfg.cxx:269
static sal_Int32 GetDefaultHeightFor(sal_uInt16 nFontType, LanguageType eLang)
Definition: fontcfg.cxx:248
LanguageType nDefaultLanguage_CTL
sal_Int32 nDefaultFontHeight[DEF_FONT_COUNT]
Definition: fontcfg.hxx:58