| 1 | // Created on: 2008-01-20 |
| 2 | // Created by: Alexander A. BORODIN |
| 3 | // Copyright (c) 2008-2014 OPEN CASCADE SAS |
| 4 | // |
| 5 | // This file is part of Open CASCADE Technology software library. |
| 6 | // |
| 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. |
| 12 | // |
| 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
| 14 | // commercial license or contractual agreement. |
| 15 | |
| 16 | |
| 17 | #include <Font_FontMgr.hxx> |
| 18 | #include <Font_FTLibrary.hxx> |
| 19 | #include <Font_SystemFont.hxx> |
| 20 | #include <NCollection_List.hxx> |
| 21 | #include <NCollection_Map.hxx> |
| 22 | #include <OSD_Environment.hxx> |
| 23 | #include <Standard_Stream.hxx> |
| 24 | #include <Standard_Type.hxx> |
| 25 | #include <TCollection_HAsciiString.hxx> |
| 26 | |
| 27 | #include <ft2build.h> |
| 28 | #include FT_FREETYPE_H |
| 29 | IMPLEMENT_STANDARD_RTTIEXT(Font_FontMgr,MMgt_TShared) |
| 30 | |
| 31 | struct Font_FontMgr_FontAliasMapNode |
| 32 | { |
| 33 | const char * EnumName; |
| 34 | const char * FontName; |
| 35 | Font_FontAspect FontAspect; |
| 36 | }; |
| 37 | |
| 38 | static const Font_FontMgr_FontAliasMapNode Font_FontMgr_MapOfFontsAliases[] = |
| 39 | { |
| 40 | |
| 41 | #if defined(_WIN32) || defined(__APPLE__) |
| 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 | |
| 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 | |
| 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 }, |
| 70 | { "Arial" , "Helvetica" , Font_FA_Regular }, |
| 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 | |
| 80 | #define NUM_FONT_ENTRIES (int)(sizeof(Font_FontMgr_MapOfFontsAliases)/sizeof(Font_FontMgr_FontAliasMapNode)) |
| 81 | |
| 82 | #if defined(_WIN32) |
| 83 | |
| 84 | #include <windows.h> |
| 85 | #include <stdlib.h> |
| 86 | |
| 87 | #ifdef _MSC_VER |
| 88 | #pragma comment (lib, "freetype.lib") |
| 89 | #endif |
| 90 | |
| 91 | namespace |
| 92 | { |
| 93 | |
| 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 |
| 106 | |
| 107 | #include <OSD_DirectoryIterator.hxx> |
| 108 | #include <OSD_FileIterator.hxx> |
| 109 | #include <OSD_Path.hxx> |
| 110 | #include <OSD_File.hxx> |
| 111 | #include <OSD_OpenMode.hxx> |
| 112 | #include <OSD_Protection.hxx> |
| 113 | |
| 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", |
| 125 | #ifdef __APPLE__ |
| 126 | // Datafork TrueType (OS X), obsolete |
| 127 | //"dfont", |
| 128 | #endif |
| 129 | NULL |
| 130 | }; |
| 131 | |
| 132 | #if !defined(__ANDROID__) && !defined(__APPLE__) |
| 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 | }; |
| 139 | #endif |
| 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) |
| 149 | static Standard_CString myDefaultFontsDirs[] = {"/system/fonts", // Android |
| 150 | "/usr/share/fonts", |
| 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) |
| 158 | { |
| 159 | TCollection_AsciiString aDirName; |
| 160 | thePath.SystemName (aDirName); |
| 161 | if (!theDirsMap.Add (aDirName)) |
| 162 | { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | for (OSD_DirectoryIterator aDirIterator (thePath, "*"); aDirIterator.More(); aDirIterator.Next()) |
| 167 | { |
| 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 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | } // anonymous namespace |
| 183 | |
| 184 | #endif |
| 185 | |
| 186 | // ======================================================================= |
| 187 | // function : checkFont |
| 188 | // purpose : |
| 189 | // ======================================================================= |
| 190 | static Handle(Font_SystemFont) checkFont (const Handle(Font_FTLibrary)& theFTLib, |
| 191 | const Standard_CString theFontPath) |
| 192 | { |
| 193 | FT_Face aFontFace; |
| 194 | FT_Error aFaceError = FT_New_Face (theFTLib->Instance(), theFontPath, 0, &aFontFace); |
| 195 | if (aFaceError != FT_Err_Ok) |
| 196 | { |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 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 | |
| 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 | } |
| 222 | |
| 223 | FT_Done_Face (aFontFace); |
| 224 | |
| 225 | return aResult; |
| 226 | } |
| 227 | |
| 228 | // ======================================================================= |
| 229 | // function : GetInstance |
| 230 | // purpose : |
| 231 | // ======================================================================= |
| 232 | Handle(Font_FontMgr) Font_FontMgr::GetInstance() |
| 233 | { |
| 234 | static Handle(Font_FontMgr) _mgr; |
| 235 | if (_mgr.IsNull()) |
| 236 | { |
| 237 | _mgr = new Font_FontMgr(); |
| 238 | } |
| 239 | |
| 240 | return _mgr; |
| 241 | } |
| 242 | |
| 243 | // ======================================================================= |
| 244 | // function : Font_FontMgr |
| 245 | // purpose : |
| 246 | // ======================================================================= |
| 247 | Font_FontMgr::Font_FontMgr() |
| 248 | { |
| 249 | InitFontDataBase(); |
| 250 | } |
| 251 | |
| 252 | // ======================================================================= |
| 253 | // function : CheckFont |
| 254 | // purpose : |
| 255 | // ======================================================================= |
| 256 | Handle(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 | // ======================================================================= |
| 266 | Standard_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 | // ======================================================================= |
| 311 | // function : InitFontDataBase |
| 312 | // purpose : |
| 313 | // ======================================================================= |
| 314 | void Font_FontMgr::InitFontDataBase() |
| 315 | { |
| 316 | myListOfFonts.Clear(); |
| 317 | Handle(Font_FTLibrary) aFtLibrary; |
| 318 | |
| 319 | #if defined(OCCT_UWP) |
| 320 | // system font files are not accessible |
| 321 | (void )aFtLibrary; |
| 322 | #elif defined(_WIN32) |
| 323 | |
| 324 | // font directory is placed in "C:\Windows\Fonts\" |
| 325 | UINT aStrLength = GetSystemWindowsDirectoryA (NULL, 0); |
| 326 | if (aStrLength == 0) |
| 327 | { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | char* aWinDir = new char[aStrLength]; |
| 332 | GetSystemWindowsDirectoryA (aWinDir, aStrLength); |
| 333 | Handle(TCollection_HAsciiString) aFontsDir = new TCollection_HAsciiString (aWinDir); |
| 334 | aFontsDir->AssignCat ("\\Fonts\\"); |
| 335 | delete[] aWinDir; |
| 336 | |
| 337 | // read fonts list from registry |
| 338 | HKEY aFontsKey; |
| 339 | if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", |
| 340 | 0, KEY_READ, &aFontsKey) != ERROR_SUCCESS) |
| 341 | { |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | NCollection_Map<TCollection_AsciiString> aSupportedExtensions; |
| 346 | for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter) |
| 347 | { |
| 348 | Standard_CString anExt = Font_FontMgr_Extensions[anIter]; |
| 349 | aSupportedExtensions.Add (TCollection_AsciiString (anExt)); |
| 350 | } |
| 351 | |
| 352 | aFtLibrary = new Font_FTLibrary(); |
| 353 | static const DWORD aBufferSize = 256; |
| 354 | char aNameBuff[aBufferSize]; |
| 355 | char aPathBuff[aBufferSize]; |
| 356 | DWORD aNameSize = aBufferSize; |
| 357 | DWORD aPathSize = aBufferSize; |
| 358 | for (DWORD anIter = 0; |
| 359 | RegEnumValueA (aFontsKey, anIter, |
| 360 | aNameBuff, &aNameSize, NULL, NULL, |
| 361 | (LPBYTE )aPathBuff, &aPathSize) != ERROR_NO_MORE_ITEMS; |
| 362 | ++anIter, aNameSize = aBufferSize, aPathSize = aBufferSize) |
| 363 | { |
| 364 | aPathBuff[(aPathSize < aBufferSize) ? aPathSize : (aBufferSize - 1)] = '\0'; // ensure string is NULL-terminated |
| 365 | |
| 366 | Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aNameBuff); |
| 367 | Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (aPathBuff); |
| 368 | if (aFontPath->Search ("\\") == -1) |
| 369 | { |
| 370 | aFontPath->Insert (1, aFontsDir); // make absolute path |
| 371 | } |
| 372 | |
| 373 | // check file extension is in list of supported |
| 374 | const Standard_Integer anExtensionPosition = aFontPath->SearchFromEnd (".") + 1; |
| 375 | if (anExtensionPosition > 0 && anExtensionPosition < aFontPath->Length()) |
| 376 | { |
| 377 | Handle(TCollection_HAsciiString) aFontExtension = aFontPath->SubString (anExtensionPosition, aFontPath->Length()); |
| 378 | aFontExtension->LowerCase(); |
| 379 | if (aSupportedExtensions.Contains (aFontExtension->String())) |
| 380 | { |
| 381 | Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString()); |
| 382 | if (!aNewFont.IsNull()) |
| 383 | { |
| 384 | myListOfFonts.Append (aNewFont); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // close registry key |
| 391 | RegCloseKey (aFontsKey); |
| 392 | |
| 393 | #else |
| 394 | |
| 395 | NCollection_Map<TCollection_AsciiString> aMapOfFontsDirs; |
| 396 | #if !defined(__ANDROID__) && !defined(__APPLE__) |
| 397 | const OSD_Protection aProtectRead (OSD_R, OSD_R, OSD_R, OSD_R); |
| 398 | |
| 399 | // read fonts directories from font service config file (obsolete) |
| 400 | for (Standard_Integer anIter = 0; myFontServiceConf[anIter] != NULL; ++anIter) |
| 401 | { |
| 402 | const TCollection_AsciiString aFileOfFontsPath (myFontServiceConf[anIter]); |
| 403 | OSD_File aFile (aFileOfFontsPath); |
| 404 | if (!aFile.Exists()) |
| 405 | { |
| 406 | continue; |
| 407 | } |
| 408 | |
| 409 | aFile.Open (OSD_ReadOnly, aProtectRead); |
| 410 | if (!aFile.IsOpen()) |
| 411 | { |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | Standard_Integer aNByte = 256; |
| 416 | Standard_Integer aNbyteRead; |
| 417 | TCollection_AsciiString aStr; // read string with information |
| 418 | while (!aFile.IsAtEnd()) |
| 419 | { |
| 420 | Standard_Integer aLocation = -1; |
| 421 | Standard_Integer aPathLocation = -1; |
| 422 | |
| 423 | aFile.ReadLine (aStr, aNByte, aNbyteRead); // reading 1 line (256 bytes) |
| 424 | aLocation = aStr.Search ("catalogue="); |
| 425 | if (aLocation < 0) |
| 426 | { |
| 427 | aLocation = aStr.Search ("catalogue ="); |
| 428 | } |
| 429 | |
| 430 | aPathLocation = aStr.Search ("/"); |
| 431 | if (aLocation > 0 && aPathLocation > 0) |
| 432 | { |
| 433 | aStr = aStr.Split (aPathLocation - 1); |
| 434 | TCollection_AsciiString aFontPath; |
| 435 | Standard_Integer aPathNumber = 1; |
| 436 | do |
| 437 | { |
| 438 | // Getting directory paths, which can be splitted by "," or ":" |
| 439 | aFontPath = aStr.Token (":,", aPathNumber); |
| 440 | aFontPath.RightAdjust(); |
| 441 | if (!aFontPath.IsEmpty()) |
| 442 | { |
| 443 | OSD_Path aPath(aFontPath); |
| 444 | addDirsRecursively (aPath, aMapOfFontsDirs); |
| 445 | } |
| 446 | aPathNumber++; |
| 447 | } |
| 448 | while (!aFontPath.IsEmpty()); |
| 449 | } |
| 450 | } |
| 451 | aFile.Close(); |
| 452 | } |
| 453 | #endif |
| 454 | |
| 455 | // append default directories |
| 456 | for (Standard_Integer anIter = 0; myDefaultFontsDirs[anIter] != NULL; ++anIter) |
| 457 | { |
| 458 | Standard_CString anItem = myDefaultFontsDirs[anIter]; |
| 459 | TCollection_AsciiString aPathStr (anItem); |
| 460 | OSD_Path aPath (aPathStr); |
| 461 | addDirsRecursively (aPath, aMapOfFontsDirs); |
| 462 | } |
| 463 | |
| 464 | NCollection_Map<TCollection_AsciiString> aSupportedExtensions; |
| 465 | for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter) |
| 466 | { |
| 467 | Standard_CString anExt = Font_FontMgr_Extensions[anIter]; |
| 468 | aSupportedExtensions.Add (TCollection_AsciiString (anExt)); |
| 469 | } |
| 470 | |
| 471 | aFtLibrary = new Font_FTLibrary(); |
| 472 | for (NCollection_Map<TCollection_AsciiString>::Iterator anIter (aMapOfFontsDirs); |
| 473 | anIter.More(); anIter.Next()) |
| 474 | { |
| 475 | #if defined(__ANDROID__) || defined(__APPLE__) |
| 476 | OSD_Path aFolderPath (anIter.Value()); |
| 477 | for (OSD_FileIterator aFileIter (aFolderPath, "*"); aFileIter.More(); aFileIter.Next()) |
| 478 | { |
| 479 | OSD_Path aFontFilePath; |
| 480 | aFileIter.Values().Path (aFontFilePath); |
| 481 | |
| 482 | TCollection_AsciiString aFontFileName; |
| 483 | aFontFilePath.SystemName (aFontFileName); |
| 484 | aFontFileName = anIter.Value() + "/" + aFontFileName; |
| 485 | |
| 486 | Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontFileName.ToCString()); |
| 487 | if (!aNewFont.IsNull()) |
| 488 | { |
| 489 | myListOfFonts.Append (aNewFont); |
| 490 | } |
| 491 | } |
| 492 | #else |
| 493 | OSD_File aReadFile (anIter.Value() + "/fonts.dir"); |
| 494 | if (!aReadFile.Exists()) |
| 495 | { |
| 496 | continue; // invalid fonts directory |
| 497 | } |
| 498 | |
| 499 | aReadFile.Open (OSD_ReadOnly, aProtectRead); |
| 500 | if (!aReadFile.IsOpen()) |
| 501 | { |
| 502 | continue; // invalid fonts directory |
| 503 | } |
| 504 | |
| 505 | Standard_Integer aNbyteRead, aNByte = 256; |
| 506 | TCollection_AsciiString aLine (aNByte); |
| 507 | Standard_Boolean isFirstLine = Standard_True; |
| 508 | const TCollection_AsciiString anEncoding ("iso8859-1\n"); |
| 509 | while (!aReadFile.IsAtEnd()) |
| 510 | { |
| 511 | aReadFile.ReadLine (aLine, aNByte, aNbyteRead); |
| 512 | if (isFirstLine) |
| 513 | { |
| 514 | // first line contains the number of fonts in this file |
| 515 | // just ignoring it... |
| 516 | isFirstLine = Standard_False; |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | Standard_Integer anExtensionPosition = aLine.Search (".") + 1; |
| 521 | if (anExtensionPosition == 0) |
| 522 | { |
| 523 | continue; // can't find extension position in the font description |
| 524 | } |
| 525 | |
| 526 | Standard_Integer anEndOfFileName = aLine.Location (" ", anExtensionPosition, aLine.Length()) - 1; |
| 527 | if (anEndOfFileName < 0 || anEndOfFileName < anExtensionPosition) |
| 528 | { |
| 529 | continue; // font description have empty extension |
| 530 | } |
| 531 | |
| 532 | TCollection_AsciiString aFontExtension = aLine.SubString (anExtensionPosition, anEndOfFileName); |
| 533 | aFontExtension.LowerCase(); |
| 534 | if (aSupportedExtensions.Contains (aFontExtension) && (aLine.Search (anEncoding) > 0)) |
| 535 | { |
| 536 | // In current implementation use fonts with ISO-8859-1 coding page. |
| 537 | // OCCT not give to manage coding page by means of programm interface. |
| 538 | // TODO: make high level interface for choosing necessary coding page. |
| 539 | Handle(TCollection_HAsciiString) aXLFD = |
| 540 | new TCollection_HAsciiString (aLine.SubString (anEndOfFileName + 2, aLine.Length())); |
| 541 | Handle(TCollection_HAsciiString) aFontPath = |
| 542 | new TCollection_HAsciiString (anIter.Value().ToCString()); |
| 543 | if (aFontPath->SearchFromEnd ("/") != aFontPath->Length()) |
| 544 | { |
| 545 | aFontPath->AssignCat ("/"); |
| 546 | } |
| 547 | Handle(TCollection_HAsciiString) aFontFileName = |
| 548 | new TCollection_HAsciiString (aLine.SubString (1, anEndOfFileName)); |
| 549 | aFontPath->AssignCat (aFontFileName); |
| 550 | |
| 551 | Handle(Font_SystemFont) aNewFontFromXLFD = new Font_SystemFont (aXLFD, aFontPath); |
| 552 | Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString()); |
| 553 | |
| 554 | if (aNewFontFromXLFD->IsValid() && !aNewFont.IsNull() && |
| 555 | !aNewFont->IsEqual (aNewFontFromXLFD)) |
| 556 | { |
| 557 | myListOfFonts.Append (aNewFont); |
| 558 | myListOfFonts.Append (aNewFontFromXLFD); |
| 559 | } |
| 560 | else if (!aNewFont.IsNull()) |
| 561 | { |
| 562 | myListOfFonts.Append (aNewFont); |
| 563 | } |
| 564 | else if (aNewFontFromXLFD->IsValid()) |
| 565 | { |
| 566 | myListOfFonts.Append (aNewFontFromXLFD); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | aReadFile.Close(); |
| 571 | #endif |
| 572 | } |
| 573 | #endif |
| 574 | } |
| 575 | |
| 576 | // ======================================================================= |
| 577 | // function : GetAvailableFonts |
| 578 | // purpose : |
| 579 | // ======================================================================= |
| 580 | const Font_NListOfSystemFont& Font_FontMgr::GetAvailableFonts() const |
| 581 | { |
| 582 | return myListOfFonts; |
| 583 | } |
| 584 | |
| 585 | // ======================================================================= |
| 586 | // function : GetAvailableFontsNames |
| 587 | // purpose : |
| 588 | // ======================================================================= |
| 589 | void Font_FontMgr::GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const |
| 590 | { |
| 591 | theFontsNames.Clear(); |
| 592 | for (Font_NListOfSystemFont::Iterator anIter(myListOfFonts); anIter.More(); anIter.Next()) |
| 593 | { |
| 594 | theFontsNames.Append (anIter.Value()->FontName()); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | // ======================================================================= |
| 599 | // function : GetFont |
| 600 | // purpose : |
| 601 | // ======================================================================= |
| 602 | Handle(Font_SystemFont) Font_FontMgr::GetFont (const Handle(TCollection_HAsciiString)& theFontName, |
| 603 | const Font_FontAspect theFontAspect, |
| 604 | const Standard_Integer theFontSize) const |
| 605 | { |
| 606 | if ( (theFontSize < 2 && theFontSize != -1) || theFontName.IsNull()) |
| 607 | { |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | for (Font_NListOfSystemFont::Iterator aFontsIterator (myListOfFonts); |
| 612 | aFontsIterator.More(); aFontsIterator.Next()) |
| 613 | { |
| 614 | if (!theFontName->IsEmpty() && !aFontsIterator.Value()->FontName()->IsSameString (theFontName, Standard_False)) |
| 615 | { |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | if (theFontAspect != Font_FA_Undefined && aFontsIterator.Value()->FontAspect() != theFontAspect) |
| 620 | { |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | if (theFontSize == -1 || aFontsIterator.Value()->FontHeight() == -1 || |
| 625 | theFontSize == aFontsIterator.Value()->FontHeight()) |
| 626 | { |
| 627 | return aFontsIterator.Value(); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | return NULL; |
| 632 | } |
| 633 | |
| 634 | // ======================================================================= |
| 635 | // function : FindFont |
| 636 | // purpose : |
| 637 | // ======================================================================= |
| 638 | Handle(Font_SystemFont) Font_FontMgr::FindFont (const Handle(TCollection_HAsciiString)& theFontName, |
| 639 | const Font_FontAspect theFontAspect, |
| 640 | const Standard_Integer theFontSize) const |
| 641 | { |
| 642 | Handle(TCollection_HAsciiString) aFontName = theFontName; |
| 643 | Font_FontAspect aFontAspect = theFontAspect; |
| 644 | Standard_Integer aFontSize = theFontSize; |
| 645 | |
| 646 | Handle(Font_SystemFont) aFont = GetFont (aFontName, aFontAspect, aFontSize); |
| 647 | if (!aFont.IsNull()) |
| 648 | { |
| 649 | return aFont; |
| 650 | } |
| 651 | |
| 652 | // Trying to use font names mapping |
| 653 | for (Standard_Integer anIter = 0; anIter < NUM_FONT_ENTRIES; ++anIter) |
| 654 | { |
| 655 | Handle(TCollection_HAsciiString) aFontAlias = |
| 656 | new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].EnumName); |
| 657 | |
| 658 | if (aFontAlias->IsSameString (aFontName, Standard_False)) |
| 659 | { |
| 660 | aFontName = new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].FontName); |
| 661 | aFontAspect = Font_FontMgr_MapOfFontsAliases[anIter].FontAspect; |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // check font family alias with specified font aspect |
| 667 | if (theFontAspect != Font_FA_Undefined |
| 668 | && theFontAspect != Font_FA_Regular |
| 669 | && theFontAspect != aFontAspect) |
| 670 | { |
| 671 | aFont = GetFont (aFontName, theFontAspect, aFontSize); |
| 672 | if (!aFont.IsNull()) |
| 673 | { |
| 674 | return aFont; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // check font alias with aspect in the name |
| 679 | aFont = GetFont (aFontName, aFontAspect, aFontSize); |
| 680 | if (!aFont.IsNull()) |
| 681 | { |
| 682 | return aFont; |
| 683 | } |
| 684 | |
| 685 | // Requested family name not found -> search for any font family with given aspect and height |
| 686 | aFontName = new TCollection_HAsciiString (""); |
| 687 | aFont = GetFont (aFontName, aFontAspect, aFontSize); |
| 688 | if (!aFont.IsNull()) |
| 689 | { |
| 690 | return aFont; |
| 691 | } |
| 692 | |
| 693 | // The last resort: trying to use ANY font available in the system |
| 694 | aFontAspect = Font_FA_Undefined; |
| 695 | aFontSize = -1; |
| 696 | aFont = GetFont (aFontName, aFontAspect, aFontSize); |
| 697 | if (!aFont.IsNull()) |
| 698 | { |
| 699 | return aFont; |
| 700 | } |
| 701 | |
| 702 | return NULL; // Fonts are not found in the system. |
| 703 | } |