1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "unx/fontcache.hxx"
#include "impfont.hxx"
#include "unx/fontmanager.hxx"
#include <vcl/svapp.hxx>
#include <vcl/sysdata.hxx>
#include <vcl/vclenum.hxx>
#include <vcl/wrkwin.hxx>
#include "fontinstance.hxx"
#include "sallayout.hxx"
#include <i18nlangtag/languagetag.hxx>
#include <i18nutil/unicode.hxx>
#include <rtl/strbuf.hxx>
#include <unicode/uchar.h>
#include <unicode/uscript.h>
using namespace psp;
#include <fontconfig/fontconfig.h>
#include <ft2build.h>
#include <fontconfig/fcfreetype.h>
#if ENABLE_DBUS
#include <dbus/dbus-glib.h>
#endif
#include <cstdio>
#include <cstdarg>
#include <unotools/atom.hxx>
#include <unotools/configmgr.hxx>
#include "osl/module.h"
#include "osl/thread.h"
#include "osl/process.h"
#include "rtl/ustrbuf.hxx"
#include <utility>
#include <algorithm>
using namespace osl;
namespace
{
typedef std::pair<FcChar8*, FcChar8*> lang_and_element;
}
class FontCfgWrapper
{
FcFontSet* m_pOutlineSet;
void addFontSet( FcSetName );
FontCfgWrapper();
~FontCfgWrapper();
public:
static FontCfgWrapper& get();
static void release();
FcFontSet* getFontSet();
void clear();
public:
FcResult LocalizedElementFromPattern(FcPattern* pPattern, FcChar8 **family,
const char *elementtype, const char *elementlangtype);
//to-do, make private and add some cleaner accessor methods
std::unordered_map< OString, OString, OStringHash > m_aFontNameToLocalized;
std::unordered_map< OString, OString, OStringHash > m_aLocalizedToCanonical;
private:
void cacheLocalizedFontNames(const FcChar8 *origfontname, const FcChar8 *bestfontname, const std::vector< lang_and_element > &lang_and_elements);
LanguageTag* m_pLanguageTag;
};
FontCfgWrapper::FontCfgWrapper()
:
m_pOutlineSet( nullptr ),
m_pLanguageTag( nullptr )
{
FcInit();
}
void FontCfgWrapper::addFontSet( FcSetName eSetName )
{
/*
add only acceptable outlined fonts to our config,
for future fontconfig use
*/
FcFontSet* pOrig = FcConfigGetFonts( FcConfigGetCurrent(), eSetName );
if( !pOrig )
return;
// filter the font sets to remove obsolete faces
for( int i = 0; i < pOrig->nfont; ++i )
{
FcPattern* pPattern = pOrig->fonts[i];
// #i115131# ignore non-outline fonts
FcBool bOutline = FcFalse;
FcResult eOutRes = FcPatternGetBool( pPattern, FC_OUTLINE, 0, &bOutline );
if( (eOutRes != FcResultMatch) || (bOutline == FcFalse) )
continue;
if (SalLayout::UseCommonLayout())
{
// Ignore Type 1 fonts; CommonSalLayout does not support them.
FcChar8* pFormat = nullptr;
FcResult eFormatRes = FcPatternGetString(pPattern, FC_FONTFORMAT, 0, &pFormat);
if ((eFormatRes == FcResultMatch) &&
(strcmp(reinterpret_cast<char*>(pFormat), "Type 1") == 0))
{
continue;
}
}
FcPatternReference( pPattern );
FcFontSetAdd( m_pOutlineSet, pPattern );
}
// TODO?: FcFontSetDestroy( pOrig );
}
namespace
{
int compareFontNames(const FcPattern *a, const FcPattern *b)
{
FcChar8 *pNameA=nullptr, *pNameB=nullptr;
bool bHaveA = FcPatternGetString(a, FC_FAMILY, 0, &pNameA) == FcResultMatch;
bool bHaveB = FcPatternGetString(b, FC_FAMILY, 0, &pNameB) == FcResultMatch;
if (bHaveA && bHaveB)
return strcmp(reinterpret_cast<const char*>(pNameA), reinterpret_cast<const char*>(pNameB));
return int(bHaveA) - int(bHaveB);
}
//Sort fonts so that fonts with the same family name are side-by-side, with
//those with higher version numbers first
class SortFont : public ::std::binary_function< const FcPattern*, const FcPattern*, bool >
{
public:
bool operator()(const FcPattern *a, const FcPattern *b)
{
int comp = compareFontNames(a, b);
if (comp != 0)
return comp < 0;
int nVersionA=0, nVersionB=0;
bool bHaveA = FcPatternGetInteger(a, FC_FONTVERSION, 0, &nVersionA) == FcResultMatch;
bool bHaveB = FcPatternGetInteger(b, FC_FONTVERSION, 0, &nVersionB) == FcResultMatch;
if (bHaveA && bHaveB)
return nVersionA > nVersionB;
return bHaveA > bHaveB;
}
};
//See fdo#30729 for where an old opensymbol installed system-wide can
//clobber the new opensymbol installed locally
//See if this font is a duplicate with equal attributes which has already been
//inserted, or if it an older version of an inserted fonts. Depends on FcFontSet
//on being sorted with SortFont
bool isPreviouslyDuplicateOrObsoleted(FcFontSet *pFSet, int i)
{
const FcPattern *a = pFSet->fonts[i];
FcPattern* pTestPatternA = FcPatternDuplicate(a);
FcPatternDel(pTestPatternA, FC_FILE);
FcPatternDel(pTestPatternA, FC_CHARSET);
FcPatternDel(pTestPatternA, FC_CAPABILITY);
FcPatternDel(pTestPatternA, FC_FONTVERSION);
FcPatternDel(pTestPatternA, FC_LANG);
bool bIsDup(false);
// fdo#66715: loop for case of several font files for same font
for (int j = i - 1; 0 <= j && !bIsDup; --j)
{
const FcPattern *b = pFSet->fonts[j];
if (compareFontNames(a, b) != 0)
break;
FcPattern* pTestPatternB = FcPatternDuplicate(b);
FcPatternDel(pTestPatternB, FC_FILE);
FcPatternDel(pTestPatternB, FC_CHARSET);
FcPatternDel(pTestPatternB, FC_CAPABILITY);
FcPatternDel(pTestPatternB, FC_FONTVERSION);
FcPatternDel(pTestPatternB, FC_LANG);
bIsDup = FcPatternEqual(pTestPatternA, pTestPatternB);
FcPatternDestroy(pTestPatternB);
}
FcPatternDestroy(pTestPatternA);
return bIsDup;
}
}
FcFontSet* FontCfgWrapper::getFontSet()
{
if( !m_pOutlineSet )
{
m_pOutlineSet = FcFontSetCreate();
addFontSet( FcSetSystem );
if( FcGetVersion() > 20400 ) // #i85462# prevent crashes
addFontSet( FcSetApplication );
::std::sort(m_pOutlineSet->fonts,m_pOutlineSet->fonts+m_pOutlineSet->nfont,SortFont());
}
return m_pOutlineSet;
}
FontCfgWrapper::~FontCfgWrapper()
{
clear();
//To-Do: get gtk vclplug smoketest to pass
//FcFini();
}
static FontCfgWrapper* pOneInstance = nullptr;
FontCfgWrapper& FontCfgWrapper::get()
{
if( ! pOneInstance )
pOneInstance = new FontCfgWrapper();
return *pOneInstance;
}
void FontCfgWrapper::release()
{
if( pOneInstance )
{
delete pOneInstance;
pOneInstance = nullptr;
}
}
namespace
{
FcChar8* bestname(const std::vector<lang_and_element> &elements, const LanguageTag & rLangTag);
FcChar8* bestname(const std::vector<lang_and_element> &elements, const LanguageTag & rLangTag)
{
FcChar8* candidate = elements.begin()->second;
/* FIXME-BCP47: once fontconfig supports language tags this
* language-territory stuff needs to be changed! */
SAL_INFO_IF( !rLangTag.isIsoLocale(), "i18n", "localizedsorter::bestname - not an ISO locale");
OString sLangMatch(OUStringToOString(rLangTag.getLanguage().toAsciiLowerCase(), RTL_TEXTENCODING_UTF8));
OString sFullMatch = sLangMatch;
sFullMatch += OString('-');
sFullMatch += OUStringToOString(rLangTag.getCountry().toAsciiLowerCase(), RTL_TEXTENCODING_UTF8);
std::vector<lang_and_element>::const_iterator aEnd = elements.end();
bool alreadyclosematch = false;
bool found_fallback_englishname = false;
for( std::vector<lang_and_element>::const_iterator aIter = elements.begin(); aIter != aEnd; ++aIter )
{
const char *pLang = reinterpret_cast<const char*>(aIter->first);
if( rtl_str_compare( pLang, sFullMatch.getStr() ) == 0)
{
// both language and country match
candidate = aIter->second;
break;
}
else if( alreadyclosematch )
{
// current candidate matches lang of lang-TERRITORY
// override candidate only if there is a full match
continue;
}
else if( rtl_str_compare( pLang, sLangMatch.getStr()) == 0)
{
// just the language matches
candidate = aIter->second;
alreadyclosematch = true;
}
else if( found_fallback_englishname )
{
// already found an english fallback, don't override candidate
// unless there is a better language match
continue;
}
else if( rtl_str_compare( pLang, "en") == 0)
{
// select a fallback candidate of the first english element
// name
candidate = aIter->second;
found_fallback_englishname = true;
}
}
return candidate;
}
}
//Set up maps to quickly map between a fonts best UI name and all the rest of its names, and vice versa
void FontCfgWrapper::cacheLocalizedFontNames(const FcChar8 *origfontname, const FcChar8 *bestfontname,
const std::vector< lang_and_element > &lang_and_elements)
{
std::vector<lang_and_element>::const_iterator aEnd = lang_and_elements.end();
for (std::vector<lang_and_element>::const_iterator aIter = lang_and_elements.begin(); aIter != aEnd; ++aIter)
{
const char *candidate = reinterpret_cast<const char*>(aIter->second);
if (rtl_str_compare(candidate, reinterpret_cast<const char*>(bestfontname)) != 0)
m_aFontNameToLocalized[OString(candidate)] = OString(reinterpret_cast<const char*>(bestfontname));
}
if (rtl_str_compare(reinterpret_cast<const char*>(origfontname), reinterpret_cast<const char*>(bestfontname)) != 0)
m_aLocalizedToCanonical[OString(reinterpret_cast<const char*>(bestfontname))] = OString(reinterpret_cast<const char*>(origfontname));
}
FcResult FontCfgWrapper::LocalizedElementFromPattern(FcPattern* pPattern, FcChar8 **element,
const char *elementtype, const char *elementlangtype)
{ /* e. g.: ^ FC_FAMILY ^ FC_FAMILYLANG */
FcChar8 *origelement;
FcResult eElementRes = FcPatternGetString( pPattern, elementtype, 0, &origelement );
*element = origelement;
if( eElementRes == FcResultMatch)
{
FcChar8* elementlang = nullptr;
if (FcPatternGetString( pPattern, elementlangtype, 0, &elementlang ) == FcResultMatch)
{
std::vector< lang_and_element > lang_and_elements;
lang_and_elements.push_back(lang_and_element(elementlang, *element));
int k = 1;
while (true)
{
if (FcPatternGetString( pPattern, elementlangtype, k, &elementlang ) != FcResultMatch)
break;
if (FcPatternGetString( pPattern, elementtype, k, element ) != FcResultMatch)
break;
lang_and_elements.push_back(lang_and_element(elementlang, *element));
++k;
}
//possible to-do, sort by UILocale instead of process locale
if (!m_pLanguageTag)
{
rtl_Locale* pLoc = nullptr;
osl_getProcessLocale(&pLoc);
m_pLanguageTag = new LanguageTag(*pLoc);
}
*element = bestname(lang_and_elements, *m_pLanguageTag);
//if this element is a fontname, map the other names to this best-name
if (rtl_str_compare(elementtype, FC_FAMILY) == 0)
cacheLocalizedFontNames(origelement, *element, lang_and_elements);
}
}
return eElementRes;
}
void FontCfgWrapper::clear()
{
m_aFontNameToLocalized.clear();
m_aLocalizedToCanonical.clear();
if( m_pOutlineSet )
{
FcFontSetDestroy( m_pOutlineSet );
m_pOutlineSet = nullptr;
}
delete m_pLanguageTag;
m_pLanguageTag = nullptr;
}
/*
* PrintFontManager::initFontconfig
*/
void PrintFontManager::initFontconfig()
{
FontCfgWrapper& rWrapper = FontCfgWrapper::get();
rWrapper.clear();
}
namespace
{
FontWeight convertWeight(int weight)
{
// set weight
if( weight <= FC_WEIGHT_THIN )
return WEIGHT_THIN;
else if( weight <= FC_WEIGHT_ULTRALIGHT )
return WEIGHT_ULTRALIGHT;
else if( weight <= FC_WEIGHT_LIGHT )
return WEIGHT_LIGHT;
else if( weight <= FC_WEIGHT_BOOK )
return WEIGHT_SEMILIGHT;
else if( weight <= FC_WEIGHT_NORMAL )
return WEIGHT_NORMAL;
else if( weight <= FC_WEIGHT_MEDIUM )
return WEIGHT_MEDIUM;
else if( weight <= FC_WEIGHT_SEMIBOLD )
return WEIGHT_SEMIBOLD;
else if( weight <= FC_WEIGHT_BOLD )
return WEIGHT_BOLD;
else if( weight <= FC_WEIGHT_ULTRABOLD )
return WEIGHT_ULTRABOLD;
return WEIGHT_BLACK;
}
FontItalic convertSlant(int slant)
{
// set italic
if( slant == FC_SLANT_ITALIC )
return ITALIC_NORMAL;
else if( slant == FC_SLANT_OBLIQUE )
return ITALIC_OBLIQUE;
return ITALIC_NONE;
}
FontPitch convertSpacing(int spacing)
{
// set pitch
if( spacing == FC_MONO || spacing == FC_CHARCELL )
return PITCH_FIXED;
return PITCH_VARIABLE;
}
// translation: fontconfig enum -> vcl enum
FontWidth convertWidth(int width)
{
if (width == FC_WIDTH_ULTRACONDENSED)
return WIDTH_ULTRA_CONDENSED;
else if (width == FC_WIDTH_EXTRACONDENSED)
return WIDTH_EXTRA_CONDENSED;
else if (width == FC_WIDTH_CONDENSED)
return WIDTH_CONDENSED;
else if (width == FC_WIDTH_SEMICONDENSED)
return WIDTH_SEMI_CONDENSED;
else if (width == FC_WIDTH_SEMIEXPANDED)
return WIDTH_SEMI_EXPANDED;
else if (width == FC_WIDTH_EXPANDED)
return WIDTH_EXPANDED;
else if (width == FC_WIDTH_EXTRAEXPANDED)
return WIDTH_EXTRA_EXPANDED;
else if (width == FC_WIDTH_ULTRAEXPANDED)
return WIDTH_ULTRA_EXPANDED;
return WIDTH_NORMAL;
}
}
//FontConfig doesn't come with a way to remove an element from a FontSet as far
//as I can see
static void lcl_FcFontSetRemove(FcFontSet* pFSet, int i)
{
FcPatternDestroy(pFSet->fonts[i]);
int nTail = pFSet->nfont - (i + 1);
--pFSet->nfont;
if (!nTail)
return;
memmove(pFSet->fonts + i, pFSet->fonts + i + 1, nTail*sizeof(FcPattern*));
}
void PrintFontManager::countFontconfigFonts( std::unordered_map<OString, int, OStringHash>& o_rVisitedPaths )
{
#if OSL_DEBUG_LEVEL > 1
int nFonts = 0;
#endif
FontCfgWrapper& rWrapper = FontCfgWrapper::get();
FcFontSet* pFSet = rWrapper.getFontSet();
const bool bMinimalFontset = utl::ConfigManager::IsAvoidConfig();
if( pFSet )
{
#if OSL_DEBUG_LEVEL > 1
fprintf( stderr, "found %d entries in fontconfig fontset\n", pFSet->nfont );
#endif
for( int i = 0; i < pFSet->nfont; i++ )
{
FcChar8* file = nullptr;
FcChar8* family = nullptr;
FcChar8* style = nullptr;
FcChar8* format = nullptr;
int slant = 0;
int weight = 0;
int width = 0;
int spacing = 0;
int nCollectionEntry = -1;
FcBool outline = false;
FcResult eFileRes = FcPatternGetString(pFSet->fonts[i], FC_FILE, 0, &file);
FcResult eFamilyRes = rWrapper.LocalizedElementFromPattern( pFSet->fonts[i], &family, FC_FAMILY, FC_FAMILYLANG );
if (bMinimalFontset && strncmp(reinterpret_cast<char*>(family), "Liberation", strlen("Liberation")))
continue;
FcResult eStyleRes = rWrapper.LocalizedElementFromPattern( pFSet->fonts[i], &style, FC_STYLE, FC_STYLELANG );
FcResult eSlantRes = FcPatternGetInteger(pFSet->fonts[i], FC_SLANT, 0, &slant);
FcResult eWeightRes = FcPatternGetInteger(pFSet->fonts[i], FC_WEIGHT, 0, &weight);
FcResult eWidthRes = FcPatternGetInteger(pFSet->fonts[i], FC_WIDTH, 0, &width);
FcResult eSpacRes = FcPatternGetInteger(pFSet->fonts[i], FC_SPACING, 0, &spacing);
FcResult eOutRes = FcPatternGetBool(pFSet->fonts[i], FC_OUTLINE, 0, &outline);
FcResult eIndexRes = FcPatternGetInteger(pFSet->fonts[i], FC_INDEX, 0, &nCollectionEntry);
FcResult eFormatRes = FcPatternGetString(pFSet->fonts[i], FC_FONTFORMAT, 0, &format);
if( eFileRes != FcResultMatch || eFamilyRes != FcResultMatch || eOutRes != FcResultMatch )
continue;
#if (OSL_DEBUG_LEVEL > 2)
fprintf( stderr, "found font \"%s\" in file %s\n"
" weight = %d, slant = %d, style = \"%s\"\n"
" width = %d, spacing = %d, outline = %d, format %s\n"
, family, file
, eWeightRes == FcResultMatch ? weight : -1
, eSpacRes == FcResultMatch ? slant : -1
, eStyleRes == FcResultMatch ? (const char*) style : "<nil>"
, eWeightRes == FcResultMatch ? width : -1
, eSpacRes == FcResultMatch ? spacing : -1
, eOutRes == FcResultMatch ? outline : -1
, eFormatRes == FcResultMatch ? (const char*)format : "<unknown>"
);
#endif
// OSL_ASSERT(eOutRes != FcResultMatch || outline);
// only outline fonts are usable to psprint anyway
if( eOutRes == FcResultMatch && ! outline )
continue;
if (isPreviouslyDuplicateOrObsoleted(pFSet, i))
{
#if OSL_DEBUG_LEVEL > 2
fprintf(stderr, "Ditching %s as duplicate/obsolete\n", file);
#endif
continue;
}
// see if this font is already cached
// update attributes
std::list< PrintFont* > aFonts;
OString aDir, aBase, aOrgPath( reinterpret_cast<char*>(file) );
splitPath( aOrgPath, aDir, aBase );
o_rVisitedPaths[aDir] = 1;
int nDirID = getDirectoryAtom( aDir, true );
if( ! m_pFontCache->getFontCacheFile( nDirID, aBase, aFonts ) )
{
#if OSL_DEBUG_LEVEL > 2
fprintf( stderr, "file %s not cached\n", aBase.getStr() );
#endif
// not known, analyze font file to get attributes
// not described by fontconfig (e.g. alias names, PSName)
if (eFormatRes != FcResultMatch)
format = nullptr;
analyzeFontFile( nDirID, aBase, aFonts, reinterpret_cast<char*>(format) );
#if OSL_DEBUG_LEVEL > 1
if( aFonts.empty() )
fprintf( stderr, "Warning: file \"%s\" is unusable to psprint\n", aOrgPath.getStr() );
#endif
}
if( aFonts.empty() )
{
//remove font, reuse index
//we want to remove unusable fonts here, in case there is a usable font
//which duplicates the properties of the unusable one
//not removing the unusable font will risk the usable font being rejected
//as a duplicate by isPreviouslyDuplicateOrObsoleted
lcl_FcFontSetRemove(pFSet, i--);
continue;
}
int nFamilyName = m_pAtoms->getAtom( ATOM_FAMILYNAME, OStringToOUString( OString( reinterpret_cast<char*>(family) ), RTL_TEXTENCODING_UTF8 ) );
PrintFont* pUpdate = aFonts.front();
std::list<PrintFont*>::const_iterator second_font = aFonts.begin();
++second_font;
if( second_font != aFonts.end() ) // more than one font
{
// a collection entry, get the correct index
if( eIndexRes == FcResultMatch && nCollectionEntry != -1 )
{
for( std::list< PrintFont* >::iterator it = aFonts.begin(); it != aFonts.end(); ++it )
{
if( (*it)->m_eType == fonttype::TrueType &&
static_cast<TrueTypeFontFile*>(*it)->m_nCollectionEntry == nCollectionEntry )
{
pUpdate = *it;
break;
}
}
// update collection entry
// additional entries will be created in the cache
// if this is a new index (that is if the loop above
// ran to the end of the list)
if( pUpdate->m_eType == fonttype::TrueType ) // sanity check, this should always be the case here
static_cast<TrueTypeFontFile*>(pUpdate)->m_nCollectionEntry = nCollectionEntry;
}
else
{
#if OSL_DEBUG_LEVEL > 1
fprintf( stderr, "multiple fonts for file, but no index in fontconfig pattern ! (index res = %d collection entry = %d\nfile will not be used\n", eIndexRes, nCollectionEntry );
#endif
// we have found more than one font in this file
// but fontconfig will not tell us which index is meant
// -> something is in disorder, do not use this font
pUpdate = nullptr;
}
}
if( pUpdate )
{
// set family name
if( pUpdate->m_nFamilyName != nFamilyName )
{
}
if( eWeightRes == FcResultMatch )
pUpdate->m_eWeight = convertWeight(weight);
if( eWidthRes == FcResultMatch )
pUpdate->m_eWidth = convertWidth(width);
if( eSpacRes == FcResultMatch )
pUpdate->m_ePitch = convertSpacing(spacing);
if( eSlantRes == FcResultMatch )
pUpdate->m_eItalic = convertSlant(slant);
if( eStyleRes == FcResultMatch )
{
pUpdate->m_aStyleName = OStringToOUString( OString( reinterpret_cast<char*>(style) ), RTL_TEXTENCODING_UTF8 );
}
// update font cache
m_pFontCache->updateFontCacheEntry( pUpdate, false );
// sort into known fonts
fontID aFont = m_nNextFontID++;
m_aFonts[ aFont ] = pUpdate;
m_aFontFileToFontID[ aBase ].insert( aFont );
#if OSL_DEBUG_LEVEL > 1
nFonts++;
#endif
#if OSL_DEBUG_LEVEL > 2
fprintf( stderr, "inserted font %s as fontID %d\n", family, aFont );
#endif
}
// clean up the fonts we did not put into the list
for( std::list< PrintFont* >::iterator it = aFonts.begin(); it != aFonts.end(); ++it )
{
if( *it != pUpdate )
{
m_pFontCache->updateFontCacheEntry( *it, false ); // prepare a cache entry for a collection item
delete *it;
}
}
}
}
// how does one get rid of the config ?
#if OSL_DEBUG_LEVEL > 1
fprintf( stderr, "inserted %d fonts from fontconfig\n", nFonts );
#endif
}
void PrintFontManager::deinitFontconfig()
{
FontCfgWrapper::release();
}
bool PrintFontManager::addFontconfigDir( const OString& rDirName )
{
// workaround for a stability problems in older FC versions
// when handling application specific fonts
const int nVersion = FcGetVersion();
if( nVersion <= 20400 )
return false;
const char* pDirName = rDirName.getStr();
bool bDirOk = (FcConfigAppFontAddDir(FcConfigGetCurrent(), reinterpret_cast<FcChar8 const *>(pDirName) ) == FcTrue);
#if OSL_DEBUG_LEVEL > 1
fprintf( stderr, "FcConfigAppFontAddDir( \"%s\") => %d\n", pDirName, bDirOk );
#endif
if( !bDirOk )
return false;
// load dir-specific fc-config file too if available
const OString aConfFileName = rDirName + "/fc_local.conf";
FILE* pCfgFile = fopen( aConfFileName.getStr(), "rb" );
if( pCfgFile )
{
fclose( pCfgFile);
bool bCfgOk = FcConfigParseAndLoad(FcConfigGetCurrent(),
reinterpret_cast<FcChar8 const *>(aConfFileName.getStr()), FcTrue);
if( !bCfgOk )
fprintf( stderr, "FcConfigParseAndLoad( \"%s\") => %d\n", aConfFileName.getStr(), bCfgOk );
}
return true;
}
static void addtopattern(FcPattern *pPattern,
FontItalic eItalic, FontWeight eWeight, FontWidth eWidth, FontPitch ePitch)
{
if( eItalic != ITALIC_DONTKNOW )
{
int nSlant = FC_SLANT_ROMAN;
switch( eItalic )
{
case ITALIC_NORMAL:
nSlant = FC_SLANT_ITALIC;
break;
case ITALIC_OBLIQUE:
nSlant = FC_SLANT_OBLIQUE;
break;
default:
break;
}
FcPatternAddInteger(pPattern, FC_SLANT, nSlant);
}
if( eWeight != WEIGHT_DONTKNOW )
{
int nWeight = FC_WEIGHT_NORMAL;
switch( eWeight )
{
case WEIGHT_THIN: nWeight = FC_WEIGHT_THIN;break;
case WEIGHT_ULTRALIGHT: nWeight = FC_WEIGHT_ULTRALIGHT;break;
case WEIGHT_LIGHT: nWeight = FC_WEIGHT_LIGHT;break;
case WEIGHT_SEMILIGHT: nWeight = FC_WEIGHT_BOOK;break;
case WEIGHT_NORMAL: nWeight = FC_WEIGHT_NORMAL;break;
case WEIGHT_MEDIUM: nWeight = FC_WEIGHT_MEDIUM;break;
case WEIGHT_SEMIBOLD: nWeight = FC_WEIGHT_SEMIBOLD;break;
case WEIGHT_BOLD: nWeight = FC_WEIGHT_BOLD;break;
case WEIGHT_ULTRABOLD: nWeight = FC_WEIGHT_ULTRABOLD;break;
case WEIGHT_BLACK: nWeight = FC_WEIGHT_BLACK;break;
default:
break;
}
FcPatternAddInteger(pPattern, FC_WEIGHT, nWeight);
}
if( eWidth != WIDTH_DONTKNOW )
{
int nWidth = FC_WIDTH_NORMAL;
switch( eWidth )
{
case WIDTH_ULTRA_CONDENSED: nWidth = FC_WIDTH_ULTRACONDENSED;break;
case WIDTH_EXTRA_CONDENSED: nWidth = FC_WIDTH_EXTRACONDENSED;break;
case WIDTH_CONDENSED: nWidth = FC_WIDTH_CONDENSED;break;
case WIDTH_SEMI_CONDENSED: nWidth = FC_WIDTH_SEMICONDENSED;break;
case WIDTH_NORMAL: nWidth = FC_WIDTH_NORMAL;break;
case WIDTH_SEMI_EXPANDED: nWidth = FC_WIDTH_SEMIEXPANDED;break;
case WIDTH_EXPANDED: nWidth = FC_WIDTH_EXPANDED;break;
case WIDTH_EXTRA_EXPANDED: nWidth = FC_WIDTH_EXTRAEXPANDED;break;
case WIDTH_ULTRA_EXPANDED: nWidth = FC_WIDTH_ULTRAEXPANDED;break;
default:
break;
}
FcPatternAddInteger(pPattern, FC_WIDTH, nWidth);
}
if( ePitch != PITCH_DONTKNOW )
{
int nSpacing = FC_PROPORTIONAL;
switch( ePitch )
{
case PITCH_FIXED: nSpacing = FC_MONO;break;
case PITCH_VARIABLE: nSpacing = FC_PROPORTIONAL;break;
default:
break;
}
FcPatternAddInteger(pPattern, FC_SPACING, nSpacing);
if (nSpacing == FC_MONO)
FcPatternAddString(pPattern, FC_FAMILY, reinterpret_cast<FcChar8 const *>("monospace"));
}
}
namespace
{
//Someday fontconfig will hopefully use bcp47, see fdo#19869
//In the meantime try something that will fit to workaround fdo#35118
OString mapToFontConfigLangTag(const LanguageTag &rLangTag)
{
#if defined(FC_VERSION) && (FC_VERSION >= 20492)
std::shared_ptr<FcStrSet> xLangSet(FcGetLangs(), FcStrSetDestroy);
OString sLangAttrib;
sLangAttrib = OUStringToOString(rLangTag.getBcp47(), RTL_TEXTENCODING_UTF8).toAsciiLowerCase();
if (FcStrSetMember(xLangSet.get(), reinterpret_cast<const FcChar8*>(sLangAttrib.getStr())))
{
return sLangAttrib;
}
sLangAttrib = OUStringToOString(rLangTag.getLanguageAndScript(), RTL_TEXTENCODING_UTF8).toAsciiLowerCase();
if (FcStrSetMember(xLangSet.get(), reinterpret_cast<const FcChar8*>(sLangAttrib.getStr())))
{
return sLangAttrib;
}
OString sLang = OUStringToOString(rLangTag.getLanguage(), RTL_TEXTENCODING_UTF8).toAsciiLowerCase();
OString sRegion = OUStringToOString(rLangTag.getCountry(), RTL_TEXTENCODING_UTF8).toAsciiLowerCase();
if (!sRegion.isEmpty())
{
sLangAttrib = sLang + OString('-') + sRegion;
if (FcStrSetMember(xLangSet.get(), reinterpret_cast<const FcChar8*>(sLangAttrib.getStr())))
{
return sLangAttrib;
}
}
if (FcStrSetMember(xLangSet.get(), reinterpret_cast<const FcChar8*>(sLang.getStr())))
{
return sLang;
}
return OString();
#else
OString sLangAttrib = OUStringToOString(rLangTag.getLanguageAndScript(), RTL_TEXTENCODING_UTF8).toAsciiLowerCase();
if (sLangAttrib.equalsIgnoreAsciiCase("pa-in"))
sLangAttrib = "pa";
return sLangAttrib;
#endif
}
//returns true if the given code-point couldn't possibly be in rLangTag.
bool isImpossibleCodePointForLang(const LanguageTag &rLangTag, sal_uInt32 currentChar)
{
//a non-default script is set, lets believe it
if (rLangTag.hasScript())
return false;
int32_t script = u_getIntPropertyValue(currentChar, UCHAR_SCRIPT);
UScriptCode eScript = static_cast<UScriptCode>(script);
bool bIsImpossible = false;
OUString sLang = rLangTag.getLanguage();
switch (eScript)
{
//http://en.wiktionary.org/wiki/Category:Oriya_script_languages
case USCRIPT_ORIYA:
bIsImpossible =
sLang != "or" &&
sLang != "kxv";
break;
//http://en.wiktionary.org/wiki/Category:Telugu_script_languages
case USCRIPT_TELUGU:
bIsImpossible =
sLang != "te" &&
sLang != "gon" &&
sLang != "kfc";
break;
//http://en.wiktionary.org/wiki/Category:Bengali_script_languages
case USCRIPT_BENGALI:
bIsImpossible =
sLang != "bn" &&
sLang != "as" &&
sLang != "bpy" &&
sLang != "ctg" &&
sLang != "sa";
break;
default:
break;
}
SAL_WARN_IF(bIsImpossible, "vcl", "In glyph fallback throwing away the language property of "
<< sLang << " because the detected script for '0x"
<< OUString::number(currentChar, 16)
<< "' is " << uscript_getName(eScript)
<< " and that language doesn't make sense. Autodetecting instead.");
return bIsImpossible;
}
OUString getExemplarLangTagForCodePoint(sal_uInt32 currentChar)
{
int32_t script = u_getIntPropertyValue(currentChar, UCHAR_SCRIPT);
UScriptCode eScript = static_cast<UScriptCode>(script);
OStringBuffer aBuf(unicode::getExemplarLanguageForUScriptCode(eScript));
if (const char* pScriptCode = uscript_getShortName(eScript))
aBuf.append('-').append(pScriptCode);
return OStringToOUString(aBuf.makeStringAndClear(), RTL_TEXTENCODING_UTF8);
}
#if ENABLE_DBUS
guint get_xid_for_dbus()
{
const vcl::Window *pTopWindow = Application::IsHeadlessModeEnabled() ? nullptr : Application::GetActiveTopWindow();
const SystemEnvData* pEnvData = pTopWindow ? pTopWindow->GetSystemData() : nullptr;
return pEnvData ? pEnvData->aWindow : 0;
}
#endif
}
#if ENABLE_DBUS
IMPL_LINK_NOARG(PrintFontManager, autoInstallFontLangSupport, Timer *, void)
{
guint xid = get_xid_for_dbus();
if (!xid)
return;
GError *error = nullptr;
/* get the DBUS session connection */
DBusGConnection *session_connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
if (error != nullptr)
{
g_debug ("DBUS cannot connect : %s", error->message);
g_error_free (error);
return;
}
/* get the proxy with gnome-session-manager */
DBusGProxy *proxy = dbus_g_proxy_new_for_name(session_connection,
"org.freedesktop.PackageKit",
"/org/freedesktop/PackageKit",
"org.freedesktop.PackageKit.Modify");
if (proxy == nullptr)
{
g_debug("Could not get DBUS proxy: org.freedesktop.PackageKit");
return;
}
gchar **fonts = static_cast<gchar**>(g_malloc((m_aCurrentRequests.size() + 1) * sizeof(gchar*)));
gchar **font = fonts;
for (std::vector<OString>::const_iterator aI = m_aCurrentRequests.begin(); aI != m_aCurrentRequests.end(); ++aI)
*font++ = const_cast<gchar*>(aI->getStr());
*font = nullptr;
gboolean res = dbus_g_proxy_call(proxy, "InstallFontconfigResources", &error,
G_TYPE_UINT, xid, /* xid */
G_TYPE_STRV, fonts, /* data */
G_TYPE_STRING, "hide-finished", /* interaction */
G_TYPE_INVALID,
G_TYPE_INVALID);
/* check the return value */
if (!res)
g_debug("InstallFontconfigResources method failed");
/* check the error value */
if (error != nullptr)
{
g_debug("InstallFontconfigResources problem : %s", error->message);
g_error_free(error);
}
g_free(fonts);
g_object_unref(G_OBJECT (proxy));
m_aCurrentRequests.clear();
}
#endif
void PrintFontManager::Substitute( FontSelectPattern &rPattern, OUString& rMissingCodes )
{
FontCfgWrapper& rWrapper = FontCfgWrapper::get();
// build pattern argument for fontconfig query
FcPattern* pPattern = FcPatternCreate();
// Prefer scalable fonts
FcPatternAddBool(pPattern, FC_SCALABLE, FcTrue);
const OString aTargetName = OUStringToOString( rPattern.maTargetName, RTL_TEXTENCODING_UTF8 );
const FcChar8* pTargetNameUtf8 = reinterpret_cast<FcChar8 const *>(aTargetName.getStr());
FcPatternAddString(pPattern, FC_FAMILY, pTargetNameUtf8);
LanguageTag aLangTag(rPattern.meLanguage);
OString aLangAttrib = mapToFontConfigLangTag(aLangTag);
// Add required Unicode characters, if any
if ( !rMissingCodes.isEmpty() )
{
FcCharSet *unicodes = FcCharSetCreate();
for( sal_Int32 nStrIndex = 0; nStrIndex < rMissingCodes.getLength(); )
{
// also handle unicode surrogates
const sal_uInt32 nCode = rMissingCodes.iterateCodePoints( &nStrIndex );
FcCharSetAddChar( unicodes, nCode );
//if the codepoint is impossible for this lang tag, then clear it
//and autodetect something useful
if (!aLangAttrib.isEmpty() && isImpossibleCodePointForLang(aLangTag, nCode))
aLangAttrib.clear();
//#i105784#/rhbz#527719 improve selection of fallback font
if (aLangAttrib.isEmpty())
{
aLangTag.reset(getExemplarLangTagForCodePoint(nCode));
aLangAttrib = mapToFontConfigLangTag(aLangTag);
}
}
FcPatternAddCharSet(pPattern, FC_CHARSET, unicodes);
FcCharSetDestroy(unicodes);
}
if (!aLangAttrib.isEmpty())
FcPatternAddString(pPattern, FC_LANG, reinterpret_cast<FcChar8 const *>(aLangAttrib.getStr()));
addtopattern(pPattern, rPattern.GetItalic(), rPattern.GetWeight(),
rPattern.GetWidthType(), rPattern.GetPitch());
// query fontconfig for a substitute
FcConfigSubstitute(FcConfigGetCurrent(), pPattern, FcMatchPattern);
FcDefaultSubstitute(pPattern);
// process the result of the fontconfig query
FcResult eResult = FcResultNoMatch;
FcFontSet* pFontSet = rWrapper.getFontSet();
FcPattern* pResult = FcFontSetMatch(FcConfigGetCurrent(), &pFontSet, 1, pPattern, &eResult);
FcPatternDestroy( pPattern );
FcFontSet* pSet = nullptr;
if( pResult )
{
pSet = FcFontSetCreate();
// info: destroying the pSet destroys pResult implicitly
// since pResult was "added" to pSet
FcFontSetAdd( pSet, pResult );
}
if( pSet )
{
if( pSet->nfont > 0 )
{
bool bRet = false;
//extract the closest match
FcChar8* file = nullptr;
FcResult eFileRes = FcPatternGetString(pSet->fonts[0], FC_FILE, 0, &file);
int nCollectionEntry = 0;
FcResult eIndexRes = FcPatternGetInteger(pSet->fonts[0], FC_INDEX, 0, &nCollectionEntry);
if (eIndexRes != FcResultMatch)
nCollectionEntry = 0;
if( eFileRes == FcResultMatch )
{
OString aDir, aBase, aOrgPath( reinterpret_cast<char*>(file) );
splitPath( aOrgPath, aDir, aBase );
int nDirID = getDirectoryAtom( aDir, true );
fontID aFont = findFontFileID( nDirID, aBase, nCollectionEntry );
if( aFont > 0 )
{
FastPrintFontInfo aInfo;
bRet = getFontFastInfo( aFont, aInfo );
rPattern.maSearchName = aInfo.m_aFamilyName;
}
}
SAL_WARN_IF(!bRet, "vcl", "no FC_FILE found, falling back to name search");
if (!bRet)
{
FcChar8* family = nullptr;
FcResult eFamilyRes = FcPatternGetString( pSet->fonts[0], FC_FAMILY, 0, &family );
// get the family name
if( eFamilyRes == FcResultMatch )
{
OString sFamily(reinterpret_cast<char*>(family));
std::unordered_map< OString, OString, OStringHash >::const_iterator aI =
rWrapper.m_aFontNameToLocalized.find(sFamily);
if (aI != rWrapper.m_aFontNameToLocalized.end())
sFamily = aI->second;
rPattern.maSearchName = OStringToOUString( sFamily, RTL_TEXTENCODING_UTF8 );
bRet = true;
}
}
if (bRet)
{
int val = 0;
if (FcResultMatch == FcPatternGetInteger(pSet->fonts[0], FC_WEIGHT, 0, &val))
rPattern.SetWeight( convertWeight(val) );
if (FcResultMatch == FcPatternGetInteger(pSet->fonts[0], FC_SLANT, 0, &val))
rPattern.SetItalic( convertSlant(val) );
if (FcResultMatch == FcPatternGetInteger(pSet->fonts[0], FC_SPACING, 0, &val))
rPattern.SetPitch ( convertSpacing(val) );
if (FcResultMatch == FcPatternGetInteger(pSet->fonts[0], FC_WIDTH, 0, &val))
rPattern.SetWidthType ( convertWidth(val) );
FcBool bEmbolden;
if (FcResultMatch == FcPatternGetBool(pSet->fonts[0], FC_EMBOLDEN, 0, &bEmbolden))
rPattern.mbEmbolden = bEmbolden;
FcMatrix *pMatrix = nullptr;
if (FcResultMatch == FcPatternGetMatrix(pSet->fonts[0], FC_MATRIX, 0, &pMatrix))
{
rPattern.maItalicMatrix.xx = pMatrix->xx;
rPattern.maItalicMatrix.xy = pMatrix->xy;
rPattern.maItalicMatrix.yx = pMatrix->yx;
rPattern.maItalicMatrix.yy = pMatrix->yy;
}
}
// update rMissingCodes by removing resolved unicodes
if( !rMissingCodes.isEmpty() )
{
std::unique_ptr<sal_uInt32[]> const pRemainingCodes(new sal_uInt32[rMissingCodes.getLength()]);
int nRemainingLen = 0;
FcCharSet* unicodes;
if (!FcPatternGetCharSet(pSet->fonts[0], FC_CHARSET, 0, &unicodes))
{
for( sal_Int32 nStrIndex = 0; nStrIndex < rMissingCodes.getLength(); )
{
// also handle unicode surrogates
const sal_uInt32 nCode = rMissingCodes.iterateCodePoints( &nStrIndex );
if (FcCharSetHasChar(unicodes, nCode) != FcTrue)
pRemainingCodes[ nRemainingLen++ ] = nCode;
}
}
OUString sStillMissing(pRemainingCodes.get(), nRemainingLen);
#if ENABLE_DBUS
if (get_xid_for_dbus())
{
if (sStillMissing == rMissingCodes) //replaced nothing
{
//It'd be better if we could ask packagekit using the
//missing codepoints or some such rather than using
//"language" as a proxy to how fontconfig considers
//scripts to default to a given language.
for (sal_Int32 i = 0; i < nRemainingLen; ++i)
{
LanguageTag aOurTag(getExemplarLangTagForCodePoint(pRemainingCodes[i]));
OString sTag = OUStringToOString(aOurTag.getBcp47(), RTL_TEXTENCODING_UTF8);
if (m_aPreviousLangSupportRequests.find(sTag) != m_aPreviousLangSupportRequests.end())
continue;
m_aPreviousLangSupportRequests.insert(sTag);
sTag = mapToFontConfigLangTag(aOurTag);
if (!sTag.isEmpty() && m_aPreviousLangSupportRequests.find(sTag) == m_aPreviousLangSupportRequests.end())
{
OString sReq = OString(":lang=") + sTag;
m_aCurrentRequests.push_back(sReq);
m_aPreviousLangSupportRequests.insert(sTag);
}
}
}
if (!m_aCurrentRequests.empty())
{
m_aFontInstallerTimer.Stop();
m_aFontInstallerTimer.Start();
}
}
#endif
rMissingCodes = sStillMissing;
}
}
FcFontSetDestroy( pSet );
}
}
FontConfigFontOptions::~FontConfigFontOptions()
{
FcPatternDestroy(mpPattern);
}
FcPattern *FontConfigFontOptions::GetPattern() const
{
return mpPattern;
}
void FontConfigFontOptions::SyncPattern(const OString& rFileName, int nIndex, bool bEmbolden)
{
FcPatternDel(mpPattern, FC_FILE);
FcPatternAddString(mpPattern, FC_FILE, reinterpret_cast<FcChar8 const *>(rFileName.getStr()));
FcPatternDel(mpPattern, FC_INDEX);
FcPatternAddInteger(mpPattern, FC_INDEX, nIndex);
FcPatternDel(mpPattern, FC_EMBOLDEN);
FcPatternAddBool(mpPattern, FC_EMBOLDEN, bEmbolden ? FcTrue : FcFalse);
}
FontConfigFontOptions* PrintFontManager::getFontOptions(const FastPrintFontInfo& rInfo, int nSize)
{
FontCfgWrapper& rWrapper = FontCfgWrapper::get();
FontConfigFontOptions* pOptions = nullptr;
FcConfig* pConfig = FcConfigGetCurrent();
FcPattern* pPattern = FcPatternCreate();
OString sFamily = OUStringToOString( rInfo.m_aFamilyName, RTL_TEXTENCODING_UTF8 );
std::unordered_map< OString, OString, OStringHash >::const_iterator aI = rWrapper.m_aLocalizedToCanonical.find(sFamily);
if (aI != rWrapper.m_aLocalizedToCanonical.end())
sFamily = aI->second;
if( !sFamily.isEmpty() )
FcPatternAddString(pPattern, FC_FAMILY, reinterpret_cast<FcChar8 const *>(sFamily.getStr()));
addtopattern(pPattern, rInfo.m_eItalic, rInfo.m_eWeight, rInfo.m_eWidth, rInfo.m_ePitch);
FcPatternAddDouble(pPattern, FC_PIXEL_SIZE, nSize);
FcBool embitmap = true, antialias = true, autohint = true, hinting = true;
int hintstyle = FC_HINT_FULL;
FcConfigSubstitute(pConfig, pPattern, FcMatchPattern);
FontConfigFontOptions::cairo_font_options_substitute(pPattern);
FcDefaultSubstitute(pPattern);
FcResult eResult = FcResultNoMatch;
FcFontSet* pFontSet = rWrapper.getFontSet();
FcPattern* pResult = FcFontSetMatch( pConfig, &pFontSet, 1, pPattern, &eResult );
if( pResult )
{
FcResult eEmbeddedBitmap = FcPatternGetBool(pResult,
FC_EMBEDDED_BITMAP, 0, &embitmap);
FcResult eAntialias = FcPatternGetBool(pResult,
FC_ANTIALIAS, 0, &antialias);
FcResult eAutoHint = FcPatternGetBool(pResult,
FC_AUTOHINT, 0, &autohint);
FcResult eHinting = FcPatternGetBool(pResult,
FC_HINTING, 0, &hinting);
(void) FcPatternGetInteger(pResult,
FC_HINT_STYLE, 0, &hintstyle);
pOptions = new FontConfigFontOptions(pResult);
if( eEmbeddedBitmap == FcResultMatch )
pOptions->meEmbeddedBitmap = embitmap ? EMBEDDEDBITMAP_TRUE : EMBEDDEDBITMAP_FALSE;
if( eAntialias == FcResultMatch )
pOptions->meAntiAlias = antialias ? ANTIALIAS_TRUE : ANTIALIAS_FALSE;
if( eAutoHint == FcResultMatch )
pOptions->meAutoHint = autohint ? FontAutoHint::Yes : FontAutoHint::No;
if( eHinting == FcResultMatch )
pOptions->meHinting = hinting ? FontHinting::Yes : FontHinting::No;
switch (hintstyle)
{
case FC_HINT_NONE: pOptions->meHintStyle = FontHintStyle::NONE; break;
case FC_HINT_SLIGHT: pOptions->meHintStyle = FontHintStyle::Slight; break;
case FC_HINT_MEDIUM: pOptions->meHintStyle = FontHintStyle::Medium; break;
default: // fall through
case FC_HINT_FULL: pOptions->meHintStyle = FontHintStyle::Full; break;
}
}
// cleanup
FcPatternDestroy( pPattern );
return pOptions;
}
void PrintFontManager::matchFont( FastPrintFontInfo& rInfo, const css::lang::Locale& rLocale )
{
FontCfgWrapper& rWrapper = FontCfgWrapper::get();
FcConfig* pConfig = FcConfigGetCurrent();
FcPattern* pPattern = FcPatternCreate();
// populate pattern with font characteristics
const LanguageTag aLangTag(rLocale);
const OString aLangAttrib = mapToFontConfigLangTag(aLangTag);
if (!aLangAttrib.isEmpty())
FcPatternAddString(pPattern, FC_LANG, reinterpret_cast<FcChar8 const *>(aLangAttrib.getStr()));
OString aFamily = OUStringToOString( rInfo.m_aFamilyName, RTL_TEXTENCODING_UTF8 );
if( !aFamily.isEmpty() )
FcPatternAddString(pPattern, FC_FAMILY, reinterpret_cast<FcChar8 const *>(aFamily.getStr()));
addtopattern(pPattern, rInfo.m_eItalic, rInfo.m_eWeight, rInfo.m_eWidth, rInfo.m_ePitch);
FcConfigSubstitute(pConfig, pPattern, FcMatchPattern);
FcDefaultSubstitute(pPattern);
FcResult eResult = FcResultNoMatch;
FcFontSet *pFontSet = rWrapper.getFontSet();
FcPattern* pResult = FcFontSetMatch(pConfig, &pFontSet, 1, pPattern, &eResult);
if( pResult )
{
FcFontSet* pSet = FcFontSetCreate();
FcFontSetAdd( pSet, pResult );
if( pSet->nfont > 0 )
{
//extract the closest match
FcChar8* file = nullptr;
FcResult eFileRes = FcPatternGetString(pSet->fonts[0], FC_FILE, 0, &file);
int nCollectionEntry = 0;
FcResult eIndexRes = FcPatternGetInteger(pSet->fonts[0], FC_INDEX, 0, &nCollectionEntry);
if (eIndexRes != FcResultMatch)
nCollectionEntry = 0;
if( eFileRes == FcResultMatch )
{
OString aDir, aBase, aOrgPath( reinterpret_cast<char*>(file) );
splitPath( aOrgPath, aDir, aBase );
int nDirID = getDirectoryAtom( aDir, true );
fontID aFont = findFontFileID( nDirID, aBase, nCollectionEntry );
if( aFont > 0 )
getFontFastInfo( aFont, rInfo );
}
}
// info: destroying the pSet destroys pResult implicitly
// since pResult was "added" to pSet
FcFontSetDestroy( pSet );
}
// cleanup
FcPatternDestroy( pPattern );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|