Temporally removing test case M1 from boolean/gdml_private group according to OCC26303.
[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
206 Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aFontFace->family_name);
207 Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (theFontPath);
208 Handle(Font_SystemFont) aResult = new Font_SystemFont (aFontName, anAspect, aFontPath);
7fd59977 209
aff395a3 210 FT_Done_Face (aFontFace);
7fd59977 211
aff395a3 212 return aResult;
213}
7fd59977 214
aff395a3 215// =======================================================================
216// function : GetInstance
217// purpose :
218// =======================================================================
219Handle(Font_FontMgr) Font_FontMgr::GetInstance()
220{
eeaaaefb 221 static Handle(Font_FontMgr) _mgr;
aff395a3 222 if (_mgr.IsNull())
223 {
eeaaaefb 224 _mgr = new Font_FontMgr();
aff395a3 225 }
7fd59977 226
227 return _mgr;
7fd59977 228}
229
aff395a3 230// =======================================================================
231// function : Font_FontMgr
232// purpose :
233// =======================================================================
234Font_FontMgr::Font_FontMgr()
235{
7fd59977 236 InitFontDataBase();
7fd59977 237}
238
aff395a3 239// =======================================================================
725ef85e 240// function : CheckFont
241// purpose :
242// =======================================================================
243Handle(Font_SystemFont) Font_FontMgr::CheckFont (Standard_CString theFontPath) const
244{
245 Handle(Font_FTLibrary) aFtLibrary = new Font_FTLibrary();
246 return checkFont (aFtLibrary, theFontPath);
247}
248
249// =======================================================================
250// function : RegisterFont
251// purpose :
252// =======================================================================
253Standard_Boolean Font_FontMgr::RegisterFont (const Handle(Font_SystemFont)& theFont,
254 const Standard_Boolean theToOverride)
255{
256 if (theFont.IsNull())
257 {
258 return Standard_False;
259 }
260
261 for (Font_NListOfSystemFont::Iterator aFontIter (myListOfFonts);
262 aFontIter.More(); aFontIter.Next())
263 {
264 if (!aFontIter.Value()->FontName()->IsSameString (theFont->FontName(), Standard_False))
265 {
266 continue;
267 }
268
269 if (theFont->FontAspect() != Font_FA_Undefined
270 && aFontIter.Value()->FontAspect() != theFont->FontAspect())
271 {
272 continue;
273 }
274
275 if (theFont->FontHeight() == -1 || aFontIter.Value()->FontHeight() == -1
276 || theFont->FontHeight() == aFontIter.Value()->FontHeight())
277 {
278 if (theFont->FontPath()->String() == aFontIter.Value()->FontPath()->String())
279 {
280 return Standard_True;
281 }
282 else if (theToOverride)
283 {
284 myListOfFonts.Remove (aFontIter);
285 }
286 else
287 {
288 return Standard_False;
289 }
290 }
291 }
292
293 myListOfFonts.Append (theFont);
294 return Standard_True;
295}
296
297// =======================================================================
aff395a3 298// function : InitFontDataBase
299// purpose :
300// =======================================================================
301void Font_FontMgr::InitFontDataBase()
302{
303 myListOfFonts.Clear();
725ef85e 304 Handle(Font_FTLibrary) aFtLibrary;
7fd59977 305
725ef85e 306#if defined(_WIN32)
7fd59977 307
aff395a3 308 // font directory is placed in "C:\Windows\Fonts\"
309 UINT aStrLength = GetSystemWindowsDirectoryA (NULL, 0);
310 if (aStrLength == 0)
a6535b1d 311 {
aff395a3 312 return;
a6535b1d 313 }
aff395a3 314
315 char* aWinDir = new char[aStrLength];
316 GetSystemWindowsDirectoryA (aWinDir, aStrLength);
317 Handle(TCollection_HAsciiString) aFontsDir = new TCollection_HAsciiString (aWinDir);
318 aFontsDir->AssignCat ("\\Fonts\\");
319 delete[] aWinDir;
320
321 // read fonts list from registry
322 HKEY aFontsKey;
323 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts",
324 0, KEY_READ, &aFontsKey) != ERROR_SUCCESS)
7fd59977 325 {
326 return;
7fd59977 327 }
7fd59977 328
aff395a3 329 NCollection_Map<TCollection_AsciiString> aSupportedExtensions;
330 for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter)
7fd59977 331 {
aff395a3 332 Standard_CString anExt = Font_FontMgr_Extensions[anIter];
333 aSupportedExtensions.Add (TCollection_AsciiString (anExt));
7fd59977 334 }
335
725ef85e 336 aFtLibrary = new Font_FTLibrary();
aff395a3 337 static const DWORD aBufferSize = 256;
338 char aNameBuff[aBufferSize];
339 char aPathBuff[aBufferSize];
340 DWORD aNameSize = aBufferSize;
341 DWORD aPathSize = aBufferSize;
342 for (DWORD anIter = 0;
343 RegEnumValueA (aFontsKey, anIter,
344 aNameBuff, &aNameSize, NULL, NULL,
345 (LPBYTE )aPathBuff, &aPathSize) != ERROR_NO_MORE_ITEMS;
346 ++anIter, aNameSize = aBufferSize, aPathSize = aBufferSize)
347 {
348 aPathBuff[(aPathSize < aBufferSize) ? aPathSize : (aBufferSize - 1)] = '\0'; // ensure string is NULL-terminated
7fd59977 349
aff395a3 350 Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aNameBuff);
351 Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (aPathBuff);
352 if (aFontPath->Search ("\\") == -1)
353 {
354 aFontPath->Insert (1, aFontsDir); // make absolute path
7fd59977 355 }
aff395a3 356
357 // check file extension is in list of supported
358 const Standard_Integer anExtensionPosition = aFontPath->SearchFromEnd (".") + 1;
359 if (anExtensionPosition > 0 && anExtensionPosition < aFontPath->Length())
7fd59977 360 {
aff395a3 361 Handle(TCollection_HAsciiString) aFontExtension = aFontPath->SubString (anExtensionPosition, aFontPath->Length());
362 aFontExtension->LowerCase();
363 if (aSupportedExtensions.Contains (aFontExtension->String()))
364 {
365 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString());
366 if (!aNewFont.IsNull())
367 {
368 myListOfFonts.Append (aNewFont);
369 }
370 }
7fd59977 371 }
372 }
7fd59977 373
aff395a3 374 // close registry key
375 RegCloseKey (aFontsKey);
376
377#else
7fd59977 378
aff395a3 379 NCollection_Map<TCollection_AsciiString> aMapOfFontsDirs;
380 const OSD_Protection aProtectRead (OSD_R, OSD_R, OSD_R, OSD_R);
7fd59977 381
aff395a3 382 // read fonts directories from font service config file (obsolete)
383 for (Standard_Integer anIter = 0; myFontServiceConf[anIter] != NULL; ++anIter)
7fd59977 384 {
aff395a3 385 const TCollection_AsciiString aFileOfFontsPath (myFontServiceConf[anIter]);
386 OSD_File aFile (aFileOfFontsPath);
387 if (!aFile.Exists())
388 {
389 continue;
390 }
7fd59977 391
aff395a3 392 aFile.Open (OSD_ReadOnly, aProtectRead);
393 if (!aFile.IsOpen())
394 {
395 continue;
396 }
397
398 Standard_Integer aNByte = 256;
399 Standard_Integer aNbyteRead;
400 TCollection_AsciiString aStr; // read string with information
401 while (!aFile.IsAtEnd())
7fd59977 402 {
aff395a3 403 Standard_Integer aLocation = -1;
404 Standard_Integer aPathLocation = -1;
405
406 aFile.ReadLine (aStr, aNByte, aNbyteRead); // reading 1 line (256 bytes)
407 aLocation = aStr.Search ("catalogue=");
408 if (aLocation < 0)
409 {
410 aLocation = aStr.Search ("catalogue =");
411 }
412
413 aPathLocation = aStr.Search ("/");
414 if (aLocation > 0 && aPathLocation > 0)
7fd59977 415 {
aff395a3 416 aStr = aStr.Split (aPathLocation - 1);
417 TCollection_AsciiString aFontPath;
418 Standard_Integer aPathNumber = 1;
419 do
7fd59977 420 {
aff395a3 421 // Getting directory paths, which can be splitted by "," or ":"
422 aFontPath = aStr.Token (":,", aPathNumber);
423 aFontPath.RightAdjust();
424 if (!aFontPath.IsEmpty())
7fd59977 425 {
aff395a3 426 OSD_Path aPath(aFontPath);
427 addDirsRecursively (aPath, aMapOfFontsDirs);
7fd59977 428 }
aff395a3 429 aPathNumber++;
430 }
431 while (!aFontPath.IsEmpty());
7fd59977 432 }
7fd59977 433 }
aff395a3 434 aFile.Close();
7fd59977 435 }
436
aff395a3 437 // append default directories
438 for (Standard_Integer anIter = 0; myDefaultFontsDirs[anIter] != NULL; ++anIter)
439 {
440 Standard_CString anItem = myDefaultFontsDirs[anIter];
441 TCollection_AsciiString aPathStr (anItem);
442 OSD_Path aPath (aPathStr);
443 addDirsRecursively (aPath, aMapOfFontsDirs);
444 }
445
446 NCollection_Map<TCollection_AsciiString> aSupportedExtensions;
447 for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter)
448 {
449 Standard_CString anExt = Font_FontMgr_Extensions[anIter];
450 aSupportedExtensions.Add (TCollection_AsciiString (anExt));
451 }
452
725ef85e 453 aFtLibrary = new Font_FTLibrary();
aff395a3 454 for (NCollection_Map<TCollection_AsciiString>::Iterator anIter (aMapOfFontsDirs);
455 anIter.More(); anIter.Next())
456 {
65360da3 457 #ifdef __ANDROID__
458 OSD_Path aFolderPath (anIter.Value());
459 for (OSD_FileIterator aFileIter (aFolderPath, "*"); aFileIter.More(); aFileIter.Next())
460 {
461 OSD_Path aFontFilePath;
462 aFileIter.Values().Path (aFontFilePath);
463
464 TCollection_AsciiString aFontFileName;
465 aFontFilePath.SystemName (aFontFileName);
466 aFontFileName = anIter.Value() + "/" + aFontFileName;
467
468 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontFileName.ToCString());
469 if (!aNewFont.IsNull())
470 {
471 myListOfFonts.Append (aNewFont);
472 }
473 }
474 #else
aff395a3 475 OSD_File aReadFile (anIter.Value() + "/fonts.dir");
476 if (!aReadFile.Exists())
477 {
478 continue; // invalid fonts directory
479 }
480
481 aReadFile.Open (OSD_ReadOnly, aProtectRead);
482 if (!aReadFile.IsOpen())
483 {
484 continue; // invalid fonts directory
485 }
486
487 Standard_Integer aNbyteRead, aNByte = 256;
488 TCollection_AsciiString aLine (aNByte);
489 Standard_Boolean isFirstLine = Standard_True;
490 const TCollection_AsciiString anEncoding ("iso8859-1\n");
491 while (!aReadFile.IsAtEnd())
492 {
493 aReadFile.ReadLine (aLine, aNByte, aNbyteRead);
494 if (isFirstLine)
495 {
496 // first line contains the number of fonts in this file
497 // just ignoring it...
498 isFirstLine = Standard_False;
499 continue;
500 }
7fd59977 501
aff395a3 502 Standard_Integer anExtensionPosition = aLine.Search (".") + 1;
503 if (anExtensionPosition == 0)
504 {
505 continue; // can't find extension position in the font description
506 }
7fd59977 507
aff395a3 508 Standard_Integer anEndOfFileName = aLine.Location (" ", anExtensionPosition, aLine.Length()) - 1;
509 if (anEndOfFileName < 0 || anEndOfFileName < anExtensionPosition)
510 {
511 continue; // font description have empty extension
512 }
7fd59977 513
aff395a3 514 TCollection_AsciiString aFontExtension = aLine.SubString (anExtensionPosition, anEndOfFileName);
515 aFontExtension.LowerCase();
516 if (aSupportedExtensions.Contains (aFontExtension) && (aLine.Search (anEncoding) > 0))
7fd59977 517 {
aff395a3 518 // In current implementation use fonts with ISO-8859-1 coding page.
519 // OCCT not give to manage coding page by means of programm interface.
520 // TODO: make high level interface for choosing necessary coding page.
521 Handle(TCollection_HAsciiString) aXLFD =
522 new TCollection_HAsciiString (aLine.SubString (anEndOfFileName + 2, aLine.Length()));
523 Handle(TCollection_HAsciiString) aFontPath =
524 new TCollection_HAsciiString (anIter.Value().ToCString());
525 if (aFontPath->SearchFromEnd ("/") != aFontPath->Length())
7fd59977 526 {
aff395a3 527 aFontPath->AssignCat ("/");
7fd59977 528 }
aff395a3 529 Handle(TCollection_HAsciiString) aFontFileName =
530 new TCollection_HAsciiString (aLine.SubString (1, anEndOfFileName));
531 aFontPath->AssignCat (aFontFileName);
532
533 Handle(Font_SystemFont) aNewFontFromXLFD = new Font_SystemFont (aXLFD, aFontPath);
534 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString());
535
536 if (aNewFontFromXLFD->IsValid() && !aNewFont.IsNull() &&
537 !aNewFont->IsEqual (aNewFontFromXLFD))
538 {
539 myListOfFonts.Append (aNewFont);
540 myListOfFonts.Append (aNewFontFromXLFD);
541 }
542 else if (!aNewFont.IsNull())
543 {
544 myListOfFonts.Append (aNewFont);
545 }
546 else if (aNewFontFromXLFD->IsValid())
547 {
548 myListOfFonts.Append (aNewFontFromXLFD);
549 }
aff395a3 550 }
7fd59977 551 }
aff395a3 552 aReadFile.Close();
65360da3 553 #endif
7fd59977 554 }
555#endif
aff395a3 556}
557
558// =======================================================================
559// function : GetAvailableFonts
560// purpose :
561// =======================================================================
562const Font_NListOfSystemFont& Font_FontMgr::GetAvailableFonts() const
563{
564 return myListOfFonts;
565}
566
b514beda 567// =======================================================================
568// function : GetAvailableFontsNames
569// purpose :
570// =======================================================================
aff395a3 571void Font_FontMgr::GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const
572{
573 theFontsNames.Clear();
574 for (Font_NListOfSystemFont::Iterator anIter(myListOfFonts); anIter.More(); anIter.Next())
575 {
576 theFontsNames.Append (anIter.Value()->FontName());
577 }
7fd59977 578}
579
b514beda 580// =======================================================================
581// function : GetFont
582// purpose :
583// =======================================================================
aff395a3 584Handle(Font_SystemFont) Font_FontMgr::GetFont (const Handle(TCollection_HAsciiString)& theFontName,
b514beda 585 const Font_FontAspect theFontAspect,
aff395a3 586 const Standard_Integer theFontSize) const
7fd59977 587{
aff395a3 588 if ( (theFontSize < 2 && theFontSize != -1) || theFontName.IsNull())
589 {
65360da3 590 return NULL;
aff395a3 591 }
592
725ef85e 593 for (Font_NListOfSystemFont::Iterator aFontsIterator (myListOfFonts);
594 aFontsIterator.More(); aFontsIterator.Next())
aff395a3 595 {
596 if (!theFontName->IsEmpty() && !aFontsIterator.Value()->FontName()->IsSameString (theFontName, Standard_False))
597 {
598 continue;
599 }
600
601 if (theFontAspect != Font_FA_Undefined && aFontsIterator.Value()->FontAspect() != theFontAspect)
602 {
603 continue;
604 }
605
606 if (theFontSize == -1 || aFontsIterator.Value()->FontHeight() == -1 ||
607 theFontSize == aFontsIterator.Value()->FontHeight())
608 {
609 return aFontsIterator.Value();
610 }
611 }
612
613 return NULL;
7fd59977 614}
615
b514beda 616// =======================================================================
617// function : FindFont
618// purpose :
619// =======================================================================
aff395a3 620Handle(Font_SystemFont) Font_FontMgr::FindFont (const Handle(TCollection_HAsciiString)& theFontName,
b514beda 621 const Font_FontAspect theFontAspect,
aff395a3 622 const Standard_Integer theFontSize) const
623{
624 Handle(TCollection_HAsciiString) aFontName = theFontName;
625 Font_FontAspect aFontAspect = theFontAspect;
b514beda 626 Standard_Integer aFontSize = theFontSize;
aff395a3 627
628 Handle(Font_SystemFont) aFont = GetFont (aFontName, aFontAspect, aFontSize);
aff395a3 629 if (!aFont.IsNull())
630 {
631 return aFont;
632 }
633
634 // Trying to use font names mapping
635 for (Standard_Integer anIter = 0; anIter < NUM_FONT_ENTRIES; ++anIter)
636 {
637 Handle(TCollection_HAsciiString) aFontAlias =
638 new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].EnumName);
7fd59977 639
aff395a3 640 if (aFontAlias->IsSameString (aFontName, Standard_False))
641 {
642 aFontName = new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].FontName);
643 aFontAspect = Font_FontMgr_MapOfFontsAliases[anIter].FontAspect;
644 break;
645 }
646 }
647
b514beda 648 // check font family alias with specified font aspect
649 if (theFontAspect != Font_FA_Undefined
650 && theFontAspect != Font_FA_Regular
651 && theFontAspect != aFontAspect)
652 {
653 aFont = GetFont (aFontName, theFontAspect, aFontSize);
654 if (!aFont.IsNull())
655 {
656 return aFont;
657 }
658 }
aff395a3 659
b514beda 660 // check font alias with aspect in the name
661 aFont = GetFont (aFontName, aFontAspect, aFontSize);
aff395a3 662 if (!aFont.IsNull())
663 {
664 return aFont;
665 }
666
667 // Requested family name not found -> search for any font family with given aspect and height
668 aFontName = new TCollection_HAsciiString ("");
669 aFont = GetFont (aFontName, aFontAspect, aFontSize);
aff395a3 670 if (!aFont.IsNull())
671 {
672 return aFont;
673 }
674
675 // The last resort: trying to use ANY font available in the system
676 aFontAspect = Font_FA_Undefined;
677 aFontSize = -1;
678 aFont = GetFont (aFontName, aFontAspect, aFontSize);
aff395a3 679 if (!aFont.IsNull())
680 {
681 return aFont;
682 }
683
684 return NULL; // Fonts are not found in the system.
685}