1 // Created on: 2008-01-20
2 // Created by: Alexander A. BORODIN
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Font_FontMgr.ixx>
18 #include <Font_FTLibrary.hxx>
19 #include <OSD_Environment.hxx>
20 #include <NCollection_List.hxx>
21 #include <NCollection_Map.hxx>
22 #include <Standard_Stream.hxx>
23 #include <TCollection_HAsciiString.hxx>
26 #include FT_FREETYPE_H
28 struct Font_FontMgr_FontAliasMapNode
30 const char * EnumName;
31 const char * FontName;
32 Font_FontAspect FontAspect;
35 static const Font_FontMgr_FontAliasMapNode Font_FontMgr_MapOfFontsAliases[] =
40 { "Courier" , "Courier New" , Font_FA_Regular },
41 { "Times-Roman" , "Times New Roman", Font_FA_Regular },
42 { "Times-Bold" , "Times New Roman", Font_FA_Bold },
43 { "Times-Italic" , "Times New Roman", Font_FA_Italic },
44 { "Times-BoldItalic" , "Times New Roman", Font_FA_BoldItalic },
45 { "ZapfChancery-MediumItalic", "Script" , Font_FA_Regular },
46 { "Symbol" , "Symbol" , Font_FA_Regular },
47 { "ZapfDingbats" , "WingDings" , Font_FA_Regular },
48 { "Rock" , "Arial" , Font_FA_Regular },
49 { "Iris" , "Lucida Console" , Font_FA_Regular }
51 #elif defined(__ANDROID__)
53 { "Courier" , "Droid Sans Mono", Font_FA_Regular },
54 { "Times-Roman" , "Droid Serif" , Font_FA_Regular },
55 { "Times-Bold" , "Droid Serif" , Font_FA_Bold },
56 { "Times-Italic" , "Droid Serif" , Font_FA_Italic },
57 { "Times-BoldItalic" , "Droid Serif" , Font_FA_BoldItalic },
58 { "Arial" , "Roboto" , Font_FA_Regular },
62 { "Courier" , "Courier" , Font_FA_Regular },
63 { "Times-Roman" , "Times" , Font_FA_Regular },
64 { "Times-Bold" , "Times" , Font_FA_Bold },
65 { "Times-Italic" , "Times" , Font_FA_Italic },
66 { "Times-BoldItalic" , "Times" , Font_FA_BoldItalic },
67 { "Arial" , "Helvetica" , Font_FA_Regular },
68 { "ZapfChancery-MediumItalic", "-adobe-itc zapf chancery-medium-i-normal--*-*-*-*-*-*-iso8859-1" , Font_FA_Regular },
69 { "Symbol" , "-adobe-symbol-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , Font_FA_Regular },
70 { "ZapfDingbats" , "-adobe-itc zapf dingbats-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , Font_FA_Regular },
71 { "Rock" , "-sgi-rock-medium-r-normal--*-*-*-*-p-*-iso8859-1" , Font_FA_Regular },
72 { "Iris" , "--iris-medium-r-normal--*-*-*-*-m-*-iso8859-1" , Font_FA_Regular }
77 #define NUM_FONT_ENTRIES (int)(sizeof(Font_FontMgr_MapOfFontsAliases)/sizeof(Font_FontMgr_FontAliasMapNode))
85 #pragma comment (lib, "freetype.lib")
91 // list of supported extensions
92 static Standard_CString Font_FontMgr_Extensions[] =
104 #include <OSD_DirectoryIterator.hxx>
105 #include <OSD_FileIterator.hxx>
106 #include <OSD_Path.hxx>
107 #include <OSD_File.hxx>
108 #include <OSD_OpenMode.hxx>
109 #include <OSD_Protection.hxx>
114 // list of supported extensions
115 static Standard_CString Font_FontMgr_Extensions[] =
125 // X11 configuration file in plain text format (obsolete - doesn't exists in modern distributives)
126 static Standard_CString myFontServiceConf[] = {"/etc/X11/fs/config",
127 "/usr/X11R6/lib/X11/fs/config",
128 "/usr/X11/lib/X11/fs/config",
133 // default fonts paths in Mac OS X
134 static Standard_CString myDefaultFontsDirs[] = {"/System/Library/Fonts",
139 // default fonts paths in most Unix systems (Linux and others)
140 static Standard_CString myDefaultFontsDirs[] = {"/system/fonts", // Android
142 "/usr/local/share/fonts",
147 static void addDirsRecursively (const OSD_Path& thePath,
148 NCollection_Map<TCollection_AsciiString>& theDirsMap)
150 TCollection_AsciiString aDirName;
151 thePath.SystemName (aDirName);
152 if (!theDirsMap.Add (aDirName))
157 for (OSD_DirectoryIterator aDirIterator (thePath, "*"); aDirIterator.More(); aDirIterator.Next())
159 OSD_Path aChildDirPath;
160 aDirIterator.Values().Path (aChildDirPath);
162 TCollection_AsciiString aChildDirName;
163 aChildDirPath.SystemName (aChildDirName);
164 if (!aChildDirName.IsEqual (".") && !aChildDirName.IsEqual (".."))
166 aChildDirName = aDirName + "/" + aChildDirName;
167 OSD_Path aPath (aChildDirName);
168 addDirsRecursively (aPath, theDirsMap);
177 // =======================================================================
178 // function : checkFont
180 // =======================================================================
181 static Handle(Font_SystemFont) checkFont (const Handle(Font_FTLibrary)& theFTLib,
182 const Standard_CString theFontPath)
185 FT_Error aFaceError = FT_New_Face (theFTLib->Instance(), theFontPath, 0, &aFontFace);
186 if (aFaceError != FT_Err_Ok)
191 Font_FontAspect anAspect = Font_FA_Regular;
192 if (aFontFace->style_flags == (FT_STYLE_FLAG_ITALIC | FT_STYLE_FLAG_BOLD))
194 anAspect = Font_FA_BoldItalic;
196 else if (aFontFace->style_flags == FT_STYLE_FLAG_ITALIC)
198 anAspect = Font_FA_Italic;
200 else if (aFontFace->style_flags == FT_STYLE_FLAG_BOLD)
202 anAspect = Font_FA_Bold;
205 Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aFontFace->family_name);
206 Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (theFontPath);
207 Handle(Font_SystemFont) aResult = new Font_SystemFont (aFontName, anAspect, aFontPath);
209 FT_Done_Face (aFontFace);
214 // =======================================================================
215 // function : GetInstance
217 // =======================================================================
218 Handle(Font_FontMgr) Font_FontMgr::GetInstance()
220 static Handle(Font_FontMgr) _mgr;
223 _mgr = new Font_FontMgr();
229 // =======================================================================
230 // function : Font_FontMgr
232 // =======================================================================
233 Font_FontMgr::Font_FontMgr()
238 // =======================================================================
239 // function : CheckFont
241 // =======================================================================
242 Handle(Font_SystemFont) Font_FontMgr::CheckFont (Standard_CString theFontPath) const
244 Handle(Font_FTLibrary) aFtLibrary = new Font_FTLibrary();
245 return checkFont (aFtLibrary, theFontPath);
248 // =======================================================================
249 // function : RegisterFont
251 // =======================================================================
252 Standard_Boolean Font_FontMgr::RegisterFont (const Handle(Font_SystemFont)& theFont,
253 const Standard_Boolean theToOverride)
255 if (theFont.IsNull())
257 return Standard_False;
260 for (Font_NListOfSystemFont::Iterator aFontIter (myListOfFonts);
261 aFontIter.More(); aFontIter.Next())
263 if (!aFontIter.Value()->FontName()->IsSameString (theFont->FontName(), Standard_False))
268 if (theFont->FontAspect() != Font_FA_Undefined
269 && aFontIter.Value()->FontAspect() != theFont->FontAspect())
274 if (theFont->FontHeight() == -1 || aFontIter.Value()->FontHeight() == -1
275 || theFont->FontHeight() == aFontIter.Value()->FontHeight())
277 if (theFont->FontPath()->String() == aFontIter.Value()->FontPath()->String())
279 return Standard_True;
281 else if (theToOverride)
283 myListOfFonts.Remove (aFontIter);
287 return Standard_False;
292 myListOfFonts.Append (theFont);
293 return Standard_True;
296 // =======================================================================
297 // function : InitFontDataBase
299 // =======================================================================
300 void Font_FontMgr::InitFontDataBase()
302 myListOfFonts.Clear();
303 Handle(Font_FTLibrary) aFtLibrary;
307 // font directory is placed in "C:\Windows\Fonts\"
308 UINT aStrLength = GetSystemWindowsDirectoryA (NULL, 0);
314 char* aWinDir = new char[aStrLength];
315 GetSystemWindowsDirectoryA (aWinDir, aStrLength);
316 Handle(TCollection_HAsciiString) aFontsDir = new TCollection_HAsciiString (aWinDir);
317 aFontsDir->AssignCat ("\\Fonts\\");
320 // read fonts list from registry
322 if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts",
323 0, KEY_READ, &aFontsKey) != ERROR_SUCCESS)
328 NCollection_Map<TCollection_AsciiString> aSupportedExtensions;
329 for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter)
331 Standard_CString anExt = Font_FontMgr_Extensions[anIter];
332 aSupportedExtensions.Add (TCollection_AsciiString (anExt));
335 aFtLibrary = new Font_FTLibrary();
336 static const DWORD aBufferSize = 256;
337 char aNameBuff[aBufferSize];
338 char aPathBuff[aBufferSize];
339 DWORD aNameSize = aBufferSize;
340 DWORD aPathSize = aBufferSize;
341 for (DWORD anIter = 0;
342 RegEnumValueA (aFontsKey, anIter,
343 aNameBuff, &aNameSize, NULL, NULL,
344 (LPBYTE )aPathBuff, &aPathSize) != ERROR_NO_MORE_ITEMS;
345 ++anIter, aNameSize = aBufferSize, aPathSize = aBufferSize)
347 aPathBuff[(aPathSize < aBufferSize) ? aPathSize : (aBufferSize - 1)] = '\0'; // ensure string is NULL-terminated
349 Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aNameBuff);
350 Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (aPathBuff);
351 if (aFontPath->Search ("\\") == -1)
353 aFontPath->Insert (1, aFontsDir); // make absolute path
356 // check file extension is in list of supported
357 const Standard_Integer anExtensionPosition = aFontPath->SearchFromEnd (".") + 1;
358 if (anExtensionPosition > 0 && anExtensionPosition < aFontPath->Length())
360 Handle(TCollection_HAsciiString) aFontExtension = aFontPath->SubString (anExtensionPosition, aFontPath->Length());
361 aFontExtension->LowerCase();
362 if (aSupportedExtensions.Contains (aFontExtension->String()))
364 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString());
365 if (!aNewFont.IsNull())
367 myListOfFonts.Append (aNewFont);
373 // close registry key
374 RegCloseKey (aFontsKey);
378 NCollection_Map<TCollection_AsciiString> aMapOfFontsDirs;
379 const OSD_Protection aProtectRead (OSD_R, OSD_R, OSD_R, OSD_R);
381 // read fonts directories from font service config file (obsolete)
382 for (Standard_Integer anIter = 0; myFontServiceConf[anIter] != NULL; ++anIter)
384 const TCollection_AsciiString aFileOfFontsPath (myFontServiceConf[anIter]);
385 OSD_File aFile (aFileOfFontsPath);
391 aFile.Open (OSD_ReadOnly, aProtectRead);
397 Standard_Integer aNByte = 256;
398 Standard_Integer aNbyteRead;
399 TCollection_AsciiString aStr; // read string with information
400 while (!aFile.IsAtEnd())
402 Standard_Integer aLocation = -1;
403 Standard_Integer aPathLocation = -1;
405 aFile.ReadLine (aStr, aNByte, aNbyteRead); // reading 1 line (256 bytes)
406 aLocation = aStr.Search ("catalogue=");
409 aLocation = aStr.Search ("catalogue =");
412 aPathLocation = aStr.Search ("/");
413 if (aLocation > 0 && aPathLocation > 0)
415 aStr = aStr.Split (aPathLocation - 1);
416 TCollection_AsciiString aFontPath;
417 Standard_Integer aPathNumber = 1;
420 // Getting directory paths, which can be splitted by "," or ":"
421 aFontPath = aStr.Token (":,", aPathNumber);
422 aFontPath.RightAdjust();
423 if (!aFontPath.IsEmpty())
425 OSD_Path aPath(aFontPath);
426 addDirsRecursively (aPath, aMapOfFontsDirs);
430 while (!aFontPath.IsEmpty());
436 // append default directories
437 for (Standard_Integer anIter = 0; myDefaultFontsDirs[anIter] != NULL; ++anIter)
439 Standard_CString anItem = myDefaultFontsDirs[anIter];
440 TCollection_AsciiString aPathStr (anItem);
441 OSD_Path aPath (aPathStr);
442 addDirsRecursively (aPath, aMapOfFontsDirs);
445 NCollection_Map<TCollection_AsciiString> aSupportedExtensions;
446 for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter)
448 Standard_CString anExt = Font_FontMgr_Extensions[anIter];
449 aSupportedExtensions.Add (TCollection_AsciiString (anExt));
452 aFtLibrary = new Font_FTLibrary();
453 for (NCollection_Map<TCollection_AsciiString>::Iterator anIter (aMapOfFontsDirs);
454 anIter.More(); anIter.Next())
457 OSD_Path aFolderPath (anIter.Value());
458 for (OSD_FileIterator aFileIter (aFolderPath, "*"); aFileIter.More(); aFileIter.Next())
460 OSD_Path aFontFilePath;
461 aFileIter.Values().Path (aFontFilePath);
463 TCollection_AsciiString aFontFileName;
464 aFontFilePath.SystemName (aFontFileName);
465 aFontFileName = anIter.Value() + "/" + aFontFileName;
467 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontFileName.ToCString());
468 if (!aNewFont.IsNull())
470 myListOfFonts.Append (aNewFont);
474 OSD_File aReadFile (anIter.Value() + "/fonts.dir");
475 if (!aReadFile.Exists())
477 continue; // invalid fonts directory
480 aReadFile.Open (OSD_ReadOnly, aProtectRead);
481 if (!aReadFile.IsOpen())
483 continue; // invalid fonts directory
486 Standard_Integer aNbyteRead, aNByte = 256;
487 TCollection_AsciiString aLine (aNByte);
488 Standard_Boolean isFirstLine = Standard_True;
489 const TCollection_AsciiString anEncoding ("iso8859-1\n");
490 while (!aReadFile.IsAtEnd())
492 aReadFile.ReadLine (aLine, aNByte, aNbyteRead);
495 // first line contains the number of fonts in this file
496 // just ignoring it...
497 isFirstLine = Standard_False;
501 Standard_Integer anExtensionPosition = aLine.Search (".") + 1;
502 if (anExtensionPosition == 0)
504 continue; // can't find extension position in the font description
507 Standard_Integer anEndOfFileName = aLine.Location (" ", anExtensionPosition, aLine.Length()) - 1;
508 if (anEndOfFileName < 0 || anEndOfFileName < anExtensionPosition)
510 continue; // font description have empty extension
513 TCollection_AsciiString aFontExtension = aLine.SubString (anExtensionPosition, anEndOfFileName);
514 aFontExtension.LowerCase();
515 if (aSupportedExtensions.Contains (aFontExtension) && (aLine.Search (anEncoding) > 0))
517 // In current implementation use fonts with ISO-8859-1 coding page.
518 // OCCT not give to manage coding page by means of programm interface.
519 // TODO: make high level interface for choosing necessary coding page.
520 Handle(TCollection_HAsciiString) aXLFD =
521 new TCollection_HAsciiString (aLine.SubString (anEndOfFileName + 2, aLine.Length()));
522 Handle(TCollection_HAsciiString) aFontPath =
523 new TCollection_HAsciiString (anIter.Value().ToCString());
524 if (aFontPath->SearchFromEnd ("/") != aFontPath->Length())
526 aFontPath->AssignCat ("/");
528 Handle(TCollection_HAsciiString) aFontFileName =
529 new TCollection_HAsciiString (aLine.SubString (1, anEndOfFileName));
530 aFontPath->AssignCat (aFontFileName);
532 Handle(Font_SystemFont) aNewFontFromXLFD = new Font_SystemFont (aXLFD, aFontPath);
533 Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString());
535 if (aNewFontFromXLFD->IsValid() && !aNewFont.IsNull() &&
536 !aNewFont->IsEqual (aNewFontFromXLFD))
538 myListOfFonts.Append (aNewFont);
539 myListOfFonts.Append (aNewFontFromXLFD);
541 else if (!aNewFont.IsNull())
543 myListOfFonts.Append (aNewFont);
545 else if (aNewFontFromXLFD->IsValid())
547 myListOfFonts.Append (aNewFontFromXLFD);
557 // =======================================================================
558 // function : GetAvailableFonts
560 // =======================================================================
561 const Font_NListOfSystemFont& Font_FontMgr::GetAvailableFonts() const
563 return myListOfFonts;
566 // =======================================================================
567 // function : GetAvailableFontsNames
569 // =======================================================================
570 void Font_FontMgr::GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const
572 theFontsNames.Clear();
573 for (Font_NListOfSystemFont::Iterator anIter(myListOfFonts); anIter.More(); anIter.Next())
575 theFontsNames.Append (anIter.Value()->FontName());
579 // =======================================================================
580 // function : GetFont
582 // =======================================================================
583 Handle(Font_SystemFont) Font_FontMgr::GetFont (const Handle(TCollection_HAsciiString)& theFontName,
584 const Font_FontAspect theFontAspect,
585 const Standard_Integer theFontSize) const
587 if ( (theFontSize < 2 && theFontSize != -1) || theFontName.IsNull())
592 for (Font_NListOfSystemFont::Iterator aFontsIterator (myListOfFonts);
593 aFontsIterator.More(); aFontsIterator.Next())
595 if (!theFontName->IsEmpty() && !aFontsIterator.Value()->FontName()->IsSameString (theFontName, Standard_False))
600 if (theFontAspect != Font_FA_Undefined && aFontsIterator.Value()->FontAspect() != theFontAspect)
605 if (theFontSize == -1 || aFontsIterator.Value()->FontHeight() == -1 ||
606 theFontSize == aFontsIterator.Value()->FontHeight())
608 return aFontsIterator.Value();
615 // =======================================================================
616 // function : FindFont
618 // =======================================================================
619 Handle(Font_SystemFont) Font_FontMgr::FindFont (const Handle(TCollection_HAsciiString)& theFontName,
620 const Font_FontAspect theFontAspect,
621 const Standard_Integer theFontSize) const
623 Handle(TCollection_HAsciiString) aFontName = theFontName;
624 Font_FontAspect aFontAspect = theFontAspect;
625 Standard_Integer aFontSize = theFontSize;
627 Handle(Font_SystemFont) aFont = GetFont (aFontName, aFontAspect, aFontSize);
633 // Trying to use font names mapping
634 for (Standard_Integer anIter = 0; anIter < NUM_FONT_ENTRIES; ++anIter)
636 Handle(TCollection_HAsciiString) aFontAlias =
637 new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].EnumName);
639 if (aFontAlias->IsSameString (aFontName, Standard_False))
641 aFontName = new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].FontName);
642 aFontAspect = Font_FontMgr_MapOfFontsAliases[anIter].FontAspect;
647 // check font family alias with specified font aspect
648 if (theFontAspect != Font_FA_Undefined
649 && theFontAspect != Font_FA_Regular
650 && theFontAspect != aFontAspect)
652 aFont = GetFont (aFontName, theFontAspect, aFontSize);
659 // check font alias with aspect in the name
660 aFont = GetFont (aFontName, aFontAspect, aFontSize);
666 // Requested family name not found -> search for any font family with given aspect and height
667 aFontName = new TCollection_HAsciiString ("");
668 aFont = GetFont (aFontName, aFontAspect, aFontSize);
674 // The last resort: trying to use ANY font available in the system
675 aFontAspect = Font_FA_Undefined;
677 aFont = GetFont (aFontName, aFontAspect, aFontSize);
683 return NULL; // Fonts are not found in the system.