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