0027819: Visualization - provide a possibility to redefine SelectMgr_SelectableObject...
[occt.git] / src / Font / Font_FontMgr.cxx
CommitLineData
b311480e 1// Created on: 2008-01-20
2// Created by: Alexander A. BORODIN
973c2be1 3// Copyright (c) 2008-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16
42cf5bc1 17#include <Font_FontMgr.hxx>
725ef85e 18#include <Font_FTLibrary.hxx>
42cf5bc1 19#include <Font_SystemFont.hxx>
7fd59977 20#include <NCollection_List.hxx>
aff395a3 21#include <NCollection_Map.hxx>
42cf5bc1 22#include <OSD_Environment.hxx>
7fd59977 23#include <Standard_Stream.hxx>
42cf5bc1 24#include <Standard_Type.hxx>
aff395a3 25#include <TCollection_HAsciiString.hxx>
7fd59977 26
aff395a3 27#include <ft2build.h>
28#include FT_FREETYPE_H
92efcf78 29IMPLEMENT_STANDARD_RTTIEXT(Font_FontMgr,MMgt_TShared)
30
aff395a3 31struct Font_FontMgr_FontAliasMapNode
32{
33 const char * EnumName;
34 const char * FontName;
35 Font_FontAspect FontAspect;
36};
7fd59977 37
aff395a3 38static const Font_FontMgr_FontAliasMapNode Font_FontMgr_MapOfFontsAliases[] =
39{
40
264abd72 41#if defined(_WIN32) || defined(__APPLE__)
aff395a3 42
43 { "Courier" , "Courier New" , Font_FA_Regular },
44 { "Times-Roman" , "Times New Roman", Font_FA_Regular },
45 { "Times-Bold" , "Times New Roman", Font_FA_Bold },
46 { "Times-Italic" , "Times New Roman", Font_FA_Italic },
47 { "Times-BoldItalic" , "Times New Roman", Font_FA_BoldItalic },
48 { "ZapfChancery-MediumItalic", "Script" , Font_FA_Regular },
49 { "Symbol" , "Symbol" , Font_FA_Regular },
50 { "ZapfDingbats" , "WingDings" , Font_FA_Regular },
51 { "Rock" , "Arial" , Font_FA_Regular },
52 { "Iris" , "Lucida Console" , Font_FA_Regular }
53
65360da3 54#elif defined(__ANDROID__)
55
56 { "Courier" , "Droid Sans Mono", Font_FA_Regular },
57 { "Times-Roman" , "Droid Serif" , Font_FA_Regular },
58 { "Times-Bold" , "Droid Serif" , Font_FA_Bold },
59 { "Times-Italic" , "Droid Serif" , Font_FA_Italic },
60 { "Times-BoldItalic" , "Droid Serif" , Font_FA_BoldItalic },
61 { "Arial" , "Roboto" , Font_FA_Regular },
62
aff395a3 63#else //X11
64
65 { "Courier" , "Courier" , Font_FA_Regular },
66 { "Times-Roman" , "Times" , Font_FA_Regular },
67 { "Times-Bold" , "Times" , Font_FA_Bold },
68 { "Times-Italic" , "Times" , Font_FA_Italic },
69 { "Times-BoldItalic" , "Times" , Font_FA_BoldItalic },
65360da3 70 { "Arial" , "Helvetica" , Font_FA_Regular },
aff395a3 71 { "ZapfChancery-MediumItalic", "-adobe-itc zapf chancery-medium-i-normal--*-*-*-*-*-*-iso8859-1" , Font_FA_Regular },
72 { "Symbol" , "-adobe-symbol-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , Font_FA_Regular },
73 { "ZapfDingbats" , "-adobe-itc zapf dingbats-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , Font_FA_Regular },
74 { "Rock" , "-sgi-rock-medium-r-normal--*-*-*-*-p-*-iso8859-1" , Font_FA_Regular },
75 { "Iris" , "--iris-medium-r-normal--*-*-*-*-m-*-iso8859-1" , Font_FA_Regular }
76#endif
77
78};
79
566f8441 80#define NUM_FONT_ENTRIES (int)(sizeof(Font_FontMgr_MapOfFontsAliases)/sizeof(Font_FontMgr_FontAliasMapNode))
aff395a3 81
65360da3 82#if defined(_WIN32)
7fd59977 83
aff395a3 84 #include <windows.h>
85 #include <stdlib.h>
86
87 #ifdef _MSC_VER
88 #pragma comment (lib, "freetype.lib")
89 #endif
90
91 namespace
7fd59977 92 {
7fd59977 93
aff395a3 94 // list of supported extensions
95 static Standard_CString Font_FontMgr_Extensions[] =
96 {
97 "ttf",
98 "otf",
99 "ttc",
100 NULL
101 };
102
103 };
104
105#else
7fd59977 106
aff395a3 107 #include <OSD_DirectoryIterator.hxx>
65360da3 108 #include <OSD_FileIterator.hxx>
aff395a3 109 #include <OSD_Path.hxx>
110 #include <OSD_File.hxx>
111 #include <OSD_OpenMode.hxx>
112 #include <OSD_Protection.hxx>
7fd59977 113
aff395a3 114 namespace
115 {
116
117 // list of supported extensions
118 static Standard_CString Font_FontMgr_Extensions[] =
119 {
120 "ttf",
121 "otf",
122 "ttc",
123 "pfa",
124 "pfb",
264abd72 125 #ifdef __APPLE__
126 // Datafork TrueType (OS X), obsolete
127 //"dfont",
128 #endif
aff395a3 129 NULL
130 };
131
264abd72 132 #if !defined(__ANDROID__) && !defined(__APPLE__)
aff395a3 133 // X11 configuration file in plain text format (obsolete - doesn't exists in modern distributives)
134 static Standard_CString myFontServiceConf[] = {"/etc/X11/fs/config",
135 "/usr/X11R6/lib/X11/fs/config",
136 "/usr/X11/lib/X11/fs/config",
137 NULL
138 };
264abd72 139 #endif
aff395a3 140
141 #ifdef __APPLE__
142 // default fonts paths in Mac OS X
143 static Standard_CString myDefaultFontsDirs[] = {"/System/Library/Fonts",
144 "/Library/Fonts",
145 NULL
146 };
147 #else
148 // default fonts paths in most Unix systems (Linux and others)
65360da3 149 static Standard_CString myDefaultFontsDirs[] = {"/system/fonts", // Android
150 "/usr/share/fonts",
aff395a3 151 "/usr/local/share/fonts",
152 NULL
153 };
154 #endif
155
156 static void addDirsRecursively (const OSD_Path& thePath,
157 NCollection_Map<TCollection_AsciiString>& theDirsMap)
7fd59977 158 {
aff395a3 159 TCollection_AsciiString aDirName;
160 thePath.SystemName (aDirName);
161 if (!theDirsMap.Add (aDirName))
162 {
163 return;
7fd59977 164 }
7fd59977 165
aff395a3 166 for (OSD_DirectoryIterator aDirIterator (thePath, "*"); aDirIterator.More(); aDirIterator.Next())
7fd59977 167 {
aff395a3 168 OSD_Path aChildDirPath;
169 aDirIterator.Values().Path (aChildDirPath);
170
171 TCollection_AsciiString aChildDirName;
172 aChildDirPath.SystemName (aChildDirName);
173 if (!aChildDirName.IsEqual (".") && !aChildDirName.IsEqual (".."))
174 {
175 aChildDirName = aDirName + "/" + aChildDirName;
176 OSD_Path aPath (aChildDirName);
177 addDirsRecursively (aPath, theDirsMap);
178 }
7fd59977 179 }
7fd59977 180 }
aff395a3 181
68858c7d 182 } // anonymous namespace
aff395a3 183
184#endif
185
186// =======================================================================
187// function : checkFont
188// purpose :
189// =======================================================================
725ef85e 190static Handle(Font_SystemFont) checkFont (const Handle(Font_FTLibrary)& theFTLib,
191 const Standard_CString theFontPath)
aff395a3 192{
193 FT_Face aFontFace;
725ef85e 194 FT_Error aFaceError = FT_New_Face (theFTLib->Instance(), theFontPath, 0, &aFontFace);
aff395a3 195 if (aFaceError != FT_Err_Ok)
196 {
197 return NULL;
7fd59977 198 }
7fd59977 199
aff395a3 200 Font_FontAspect anAspect = Font_FA_Regular;
201 if (aFontFace->style_flags == (FT_STYLE_FLAG_ITALIC | FT_STYLE_FLAG_BOLD))
202 {
203 anAspect = Font_FA_BoldItalic;
204 }
205 else if (aFontFace->style_flags == FT_STYLE_FLAG_ITALIC)
206 {
207 anAspect = Font_FA_Italic;
208 }
209 else if (aFontFace->style_flags == FT_STYLE_FLAG_BOLD)
210 {
211 anAspect = Font_FA_Bold;
212 }
213
508643cf 214 Handle(Font_SystemFont) aResult;
215 if (aFontFace->family_name != NULL // skip broken fonts (error in FreeType?)
216 && FT_Select_Charmap (aFontFace, ft_encoding_unicode) == 0) // Font_FTFont supports only UNICODE fonts
217 {
218 Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aFontFace->family_name);
219 Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (theFontPath);
220 aResult = new Font_SystemFont (aFontName, anAspect, aFontPath);
221 }
7fd59977 222
aff395a3 223 FT_Done_Face (aFontFace);
7fd59977 224
aff395a3 225 return aResult;
226}
7fd59977 227
aff395a3 228// =======================================================================
229// function : GetInstance
230// purpose :
231// =======================================================================
232Handle(Font_FontMgr) Font_FontMgr::GetInstance()
233{
eeaaaefb 234 static Handle(Font_FontMgr) _mgr;
aff395a3 235 if (_mgr.IsNull())
236 {
eeaaaefb 237 _mgr = new Font_FontMgr();
aff395a3 238 }
7fd59977 239
240 return _mgr;
7fd59977 241}
242
aff395a3 243// =======================================================================
244// function : Font_FontMgr
245// purpose :
246// =======================================================================
247Font_FontMgr::Font_FontMgr()
248{
7fd59977 249 InitFontDataBase();
7fd59977 250}
251
aff395a3 252// =======================================================================
725ef85e 253// function : CheckFont
254// purpose :
255// =======================================================================
256Handle(Font_SystemFont) Font_FontMgr::CheckFont (Standard_CString theFontPath) const
257{
258 Handle(Font_FTLibrary) aFtLibrary = new Font_FTLibrary();
259 return checkFont (aFtLibrary, theFontPath);
260}
261
262// =======================================================================
263// function : RegisterFont
264// purpose :
265// =======================================================================
266Standard_Boolean Font_FontMgr::RegisterFont (const Handle(Font_SystemFont)& theFont,
267 const Standard_Boolean theToOverride)
268{
269 if (theFont.IsNull())
270 {
271 return Standard_False;
272 }
273
274 for (Font_NListOfSystemFont::Iterator aFontIter (myListOfFonts);
275 aFontIter.More(); aFontIter.Next())
276 {
277 if (!aFontIter.Value()->FontName()->IsSameString (theFont->FontName(), Standard_False))
278 {
279 continue;
280 }
281
282 if (theFont->FontAspect() != Font_FA_Undefined
283 && aFontIter.Value()->FontAspect() != theFont->FontAspect())
284 {
285 continue;
286 }
287
288 if (theFont->FontHeight() == -1 || aFontIter.Value()->FontHeight() == -1
289 || theFont->FontHeight() == aFontIter.Value()->FontHeight())
290 {
291 if (theFont->FontPath()->String() == aFontIter.Value()->FontPath()->String())
292 {
293 return Standard_True;
294 }
295 else if (theToOverride)
296 {
297 myListOfFonts.Remove (aFontIter);
298 }
299 else
300 {
301 return Standard_False;
302 }
303 }
304 }
305
306 myListOfFonts.Append (theFont);
307 return Standard_True;
308}
309
310// =======================================================================
aff395a3 311// function : InitFontDataBase
312// purpose :
313// =======================================================================
314void Font_FontMgr::InitFontDataBase()
315{
316 myListOfFonts.Clear();
725ef85e 317 Handle(Font_FTLibrary) aFtLibrary;
7fd59977 318
725ef85e 319#if defined(_WIN32)
7fd59977 320
aff395a3 321 // font directory is placed in "C:\Windows\Fonts\"
322 UINT aStrLength = GetSystemWindowsDirectoryA (NULL, 0);
323 if (aStrLength == 0)
a6535b1d 324 {
aff395a3 325 return;
a6535b1d 326 }
aff395a3 327
328 char* aWinDir = new char[aStrLength];
329 GetSystemWindowsDirectoryA (aWinDir, aStrLength);
330 Handle(TCollection_HAsciiString) aFontsDir = new TCollection_HAsciiString (aWinDir);
331 aFontsDir->AssignCat ("\\Fonts\\");
332 delete[] aWinDir;
333
334 // read fonts list from registry
335 HKEY aFontsKey;
336 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts",
337 0, KEY_READ, &aFontsKey) != ERROR_SUCCESS)
7fd59977 338 {
339 return;
7fd59977 340 }
7fd59977 341
aff395a3 342 NCollection_Map<TCollection_AsciiString> aSupportedExtensions;
343 for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter)
7fd59977 344 {
aff395a3 345 Standard_CString anExt = Font_FontMgr_Extensions[anIter];
346 aSupportedExtensions.Add (TCollection_AsciiString (anExt));
7fd59977 347 }
348
725ef85e 349 aFtLibrary = new Font_FTLibrary();
aff395a3 350 static const DWORD aBufferSize = 256;
351 char aNameBuff[aBufferSize];
352 char aPathBuff[aBufferSize];
353 DWORD aNameSize = aBufferSize;
354 DWORD aPathSize = aBufferSize;
355 for (DWORD anIter = 0;
356 RegEnumValueA (aFontsKey, anIter,
357 aNameBuff, &aNameSize, NULL, NULL,
358 (LPBYTE )aPathBuff, &aPathSize) != ERROR_NO_MORE_ITEMS;
359 ++anIter, aNameSize = aBufferSize, aPathSize = aBufferSize)
360 {
361 aPathBuff[(aPathSize < aBufferSize) ? aPathSize : (aBufferSize - 1)] = '\0'; // ensure string is NULL-terminated
7fd59977 362
aff395a3 363 Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aNameBuff);
364 Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (aPathBuff);
365 if (aFontPath->Search ("\\") == -1)
366 {
367 aFontPath->Insert (1, aFontsDir); // make absolute path
7fd59977 368 }
aff395a3 369
370 // check file extension is in list of supported
371 const Standard_Integer anExtensionPosition = aFontPath->SearchFromEnd (".") + 1;
372 if (anExtensionPosition > 0 && anExtensionPosition < aFontPath->Length())
7fd59977 373 {
aff395a3 374 Handle(TCollection_HAsciiString) aFontExtension = aFontPath->SubString (anExtensionPosition, aFontPath->Length());
375 aFontExtension->LowerCase();
376 if (aSupportedExtensions.Contains (aFontExtension->String()))
377 {
378 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString());
379 if (!aNewFont.IsNull())
380 {
381 myListOfFonts.Append (aNewFont);
382 }
383 }
7fd59977 384 }
385 }
7fd59977 386
aff395a3 387 // close registry key
388 RegCloseKey (aFontsKey);
389
390#else
7fd59977 391
aff395a3 392 NCollection_Map<TCollection_AsciiString> aMapOfFontsDirs;
264abd72 393#if !defined(__ANDROID__) && !defined(__APPLE__)
aff395a3 394 const OSD_Protection aProtectRead (OSD_R, OSD_R, OSD_R, OSD_R);
7fd59977 395
aff395a3 396 // read fonts directories from font service config file (obsolete)
397 for (Standard_Integer anIter = 0; myFontServiceConf[anIter] != NULL; ++anIter)
7fd59977 398 {
aff395a3 399 const TCollection_AsciiString aFileOfFontsPath (myFontServiceConf[anIter]);
400 OSD_File aFile (aFileOfFontsPath);
401 if (!aFile.Exists())
402 {
403 continue;
404 }
7fd59977 405
aff395a3 406 aFile.Open (OSD_ReadOnly, aProtectRead);
407 if (!aFile.IsOpen())
408 {
409 continue;
410 }
411
412 Standard_Integer aNByte = 256;
413 Standard_Integer aNbyteRead;
414 TCollection_AsciiString aStr; // read string with information
415 while (!aFile.IsAtEnd())
7fd59977 416 {
aff395a3 417 Standard_Integer aLocation = -1;
418 Standard_Integer aPathLocation = -1;
419
420 aFile.ReadLine (aStr, aNByte, aNbyteRead); // reading 1 line (256 bytes)
421 aLocation = aStr.Search ("catalogue=");
422 if (aLocation < 0)
423 {
424 aLocation = aStr.Search ("catalogue =");
425 }
426
427 aPathLocation = aStr.Search ("/");
428 if (aLocation > 0 && aPathLocation > 0)
7fd59977 429 {
aff395a3 430 aStr = aStr.Split (aPathLocation - 1);
431 TCollection_AsciiString aFontPath;
432 Standard_Integer aPathNumber = 1;
433 do
7fd59977 434 {
aff395a3 435 // Getting directory paths, which can be splitted by "," or ":"
436 aFontPath = aStr.Token (":,", aPathNumber);
437 aFontPath.RightAdjust();
438 if (!aFontPath.IsEmpty())
7fd59977 439 {
aff395a3 440 OSD_Path aPath(aFontPath);
441 addDirsRecursively (aPath, aMapOfFontsDirs);
7fd59977 442 }
aff395a3 443 aPathNumber++;
444 }
445 while (!aFontPath.IsEmpty());
7fd59977 446 }
7fd59977 447 }
aff395a3 448 aFile.Close();
7fd59977 449 }
264abd72 450#endif
7fd59977 451
aff395a3 452 // append default directories
453 for (Standard_Integer anIter = 0; myDefaultFontsDirs[anIter] != NULL; ++anIter)
454 {
455 Standard_CString anItem = myDefaultFontsDirs[anIter];
456 TCollection_AsciiString aPathStr (anItem);
457 OSD_Path aPath (aPathStr);
458 addDirsRecursively (aPath, aMapOfFontsDirs);
459 }
460
461 NCollection_Map<TCollection_AsciiString> aSupportedExtensions;
462 for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter)
463 {
464 Standard_CString anExt = Font_FontMgr_Extensions[anIter];
465 aSupportedExtensions.Add (TCollection_AsciiString (anExt));
466 }
467
725ef85e 468 aFtLibrary = new Font_FTLibrary();
aff395a3 469 for (NCollection_Map<TCollection_AsciiString>::Iterator anIter (aMapOfFontsDirs);
470 anIter.More(); anIter.Next())
471 {
264abd72 472 #if defined(__ANDROID__) || defined(__APPLE__)
65360da3 473 OSD_Path aFolderPath (anIter.Value());
474 for (OSD_FileIterator aFileIter (aFolderPath, "*"); aFileIter.More(); aFileIter.Next())
475 {
476 OSD_Path aFontFilePath;
477 aFileIter.Values().Path (aFontFilePath);
478
479 TCollection_AsciiString aFontFileName;
480 aFontFilePath.SystemName (aFontFileName);
481 aFontFileName = anIter.Value() + "/" + aFontFileName;
482
483 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontFileName.ToCString());
484 if (!aNewFont.IsNull())
485 {
486 myListOfFonts.Append (aNewFont);
487 }
488 }
489 #else
aff395a3 490 OSD_File aReadFile (anIter.Value() + "/fonts.dir");
491 if (!aReadFile.Exists())
492 {
493 continue; // invalid fonts directory
494 }
495
496 aReadFile.Open (OSD_ReadOnly, aProtectRead);
497 if (!aReadFile.IsOpen())
498 {
499 continue; // invalid fonts directory
500 }
501
502 Standard_Integer aNbyteRead, aNByte = 256;
503 TCollection_AsciiString aLine (aNByte);
504 Standard_Boolean isFirstLine = Standard_True;
505 const TCollection_AsciiString anEncoding ("iso8859-1\n");
506 while (!aReadFile.IsAtEnd())
507 {
508 aReadFile.ReadLine (aLine, aNByte, aNbyteRead);
509 if (isFirstLine)
510 {
511 // first line contains the number of fonts in this file
512 // just ignoring it...
513 isFirstLine = Standard_False;
514 continue;
515 }
7fd59977 516
aff395a3 517 Standard_Integer anExtensionPosition = aLine.Search (".") + 1;
518 if (anExtensionPosition == 0)
519 {
520 continue; // can't find extension position in the font description
521 }
7fd59977 522
aff395a3 523 Standard_Integer anEndOfFileName = aLine.Location (" ", anExtensionPosition, aLine.Length()) - 1;
524 if (anEndOfFileName < 0 || anEndOfFileName < anExtensionPosition)
525 {
526 continue; // font description have empty extension
527 }
7fd59977 528
aff395a3 529 TCollection_AsciiString aFontExtension = aLine.SubString (anExtensionPosition, anEndOfFileName);
530 aFontExtension.LowerCase();
531 if (aSupportedExtensions.Contains (aFontExtension) && (aLine.Search (anEncoding) > 0))
7fd59977 532 {
aff395a3 533 // In current implementation use fonts with ISO-8859-1 coding page.
534 // OCCT not give to manage coding page by means of programm interface.
535 // TODO: make high level interface for choosing necessary coding page.
536 Handle(TCollection_HAsciiString) aXLFD =
537 new TCollection_HAsciiString (aLine.SubString (anEndOfFileName + 2, aLine.Length()));
538 Handle(TCollection_HAsciiString) aFontPath =
539 new TCollection_HAsciiString (anIter.Value().ToCString());
540 if (aFontPath->SearchFromEnd ("/") != aFontPath->Length())
7fd59977 541 {
aff395a3 542 aFontPath->AssignCat ("/");
7fd59977 543 }
aff395a3 544 Handle(TCollection_HAsciiString) aFontFileName =
545 new TCollection_HAsciiString (aLine.SubString (1, anEndOfFileName));
546 aFontPath->AssignCat (aFontFileName);
547
548 Handle(Font_SystemFont) aNewFontFromXLFD = new Font_SystemFont (aXLFD, aFontPath);
549 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString());
550
551 if (aNewFontFromXLFD->IsValid() && !aNewFont.IsNull() &&
552 !aNewFont->IsEqual (aNewFontFromXLFD))
553 {
554 myListOfFonts.Append (aNewFont);
555 myListOfFonts.Append (aNewFontFromXLFD);
556 }
557 else if (!aNewFont.IsNull())
558 {
559 myListOfFonts.Append (aNewFont);
560 }
561 else if (aNewFontFromXLFD->IsValid())
562 {
563 myListOfFonts.Append (aNewFontFromXLFD);
564 }
aff395a3 565 }
7fd59977 566 }
aff395a3 567 aReadFile.Close();
65360da3 568 #endif
7fd59977 569 }
570#endif
aff395a3 571}
572
573// =======================================================================
574// function : GetAvailableFonts
575// purpose :
576// =======================================================================
577const Font_NListOfSystemFont& Font_FontMgr::GetAvailableFonts() const
578{
579 return myListOfFonts;
580}
581
b514beda 582// =======================================================================
583// function : GetAvailableFontsNames
584// purpose :
585// =======================================================================
aff395a3 586void Font_FontMgr::GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const
587{
588 theFontsNames.Clear();
589 for (Font_NListOfSystemFont::Iterator anIter(myListOfFonts); anIter.More(); anIter.Next())
590 {
591 theFontsNames.Append (anIter.Value()->FontName());
592 }
7fd59977 593}
594
b514beda 595// =======================================================================
596// function : GetFont
597// purpose :
598// =======================================================================
aff395a3 599Handle(Font_SystemFont) Font_FontMgr::GetFont (const Handle(TCollection_HAsciiString)& theFontName,
b514beda 600 const Font_FontAspect theFontAspect,
aff395a3 601 const Standard_Integer theFontSize) const
7fd59977 602{
aff395a3 603 if ( (theFontSize < 2 && theFontSize != -1) || theFontName.IsNull())
604 {
65360da3 605 return NULL;
aff395a3 606 }
607
725ef85e 608 for (Font_NListOfSystemFont::Iterator aFontsIterator (myListOfFonts);
609 aFontsIterator.More(); aFontsIterator.Next())
aff395a3 610 {
611 if (!theFontName->IsEmpty() && !aFontsIterator.Value()->FontName()->IsSameString (theFontName, Standard_False))
612 {
613 continue;
614 }
615
616 if (theFontAspect != Font_FA_Undefined && aFontsIterator.Value()->FontAspect() != theFontAspect)
617 {
618 continue;
619 }
620
621 if (theFontSize == -1 || aFontsIterator.Value()->FontHeight() == -1 ||
622 theFontSize == aFontsIterator.Value()->FontHeight())
623 {
624 return aFontsIterator.Value();
625 }
626 }
627
628 return NULL;
7fd59977 629}
630
b514beda 631// =======================================================================
632// function : FindFont
633// purpose :
634// =======================================================================
aff395a3 635Handle(Font_SystemFont) Font_FontMgr::FindFont (const Handle(TCollection_HAsciiString)& theFontName,
b514beda 636 const Font_FontAspect theFontAspect,
aff395a3 637 const Standard_Integer theFontSize) const
638{
639 Handle(TCollection_HAsciiString) aFontName = theFontName;
640 Font_FontAspect aFontAspect = theFontAspect;
b514beda 641 Standard_Integer aFontSize = theFontSize;
aff395a3 642
643 Handle(Font_SystemFont) aFont = GetFont (aFontName, aFontAspect, aFontSize);
aff395a3 644 if (!aFont.IsNull())
645 {
646 return aFont;
647 }
648
649 // Trying to use font names mapping
650 for (Standard_Integer anIter = 0; anIter < NUM_FONT_ENTRIES; ++anIter)
651 {
652 Handle(TCollection_HAsciiString) aFontAlias =
653 new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].EnumName);
7fd59977 654
aff395a3 655 if (aFontAlias->IsSameString (aFontName, Standard_False))
656 {
657 aFontName = new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].FontName);
658 aFontAspect = Font_FontMgr_MapOfFontsAliases[anIter].FontAspect;
659 break;
660 }
661 }
662
b514beda 663 // check font family alias with specified font aspect
664 if (theFontAspect != Font_FA_Undefined
665 && theFontAspect != Font_FA_Regular
666 && theFontAspect != aFontAspect)
667 {
668 aFont = GetFont (aFontName, theFontAspect, aFontSize);
669 if (!aFont.IsNull())
670 {
671 return aFont;
672 }
673 }
aff395a3 674
b514beda 675 // check font alias with aspect in the name
676 aFont = GetFont (aFontName, aFontAspect, aFontSize);
aff395a3 677 if (!aFont.IsNull())
678 {
679 return aFont;
680 }
681
682 // Requested family name not found -> search for any font family with given aspect and height
683 aFontName = new TCollection_HAsciiString ("");
684 aFont = GetFont (aFontName, aFontAspect, aFontSize);
aff395a3 685 if (!aFont.IsNull())
686 {
687 return aFont;
688 }
689
690 // The last resort: trying to use ANY font available in the system
691 aFontAspect = Font_FA_Undefined;
692 aFontSize = -1;
693 aFont = GetFont (aFontName, aFontAspect, aFontSize);
aff395a3 694 if (!aFont.IsNull())
695 {
696 return aFont;
697 }
698
699 return NULL; // Fonts are not found in the system.
700}