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