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