1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-09 13:22:24 +03:00

0023415: OSD_FontMgr can't idenify aspect for fonts with names dependant on system locale.

Added function DetectFontsAspects to Font_FontMgr class. This function uses workaround from OpenGl_FontMgr with FreeType for detecting font aspect.
Removed font name parsing from Font_FontMgr::InitFontDataBase(). Now the font name and font style we get through the FreeType.
Fixed Unix part of Font_FontMgr::InitFontDataBase() method. Font name and font style now detected through the FreeType.
Remarks fix. Added recursive default font directories scanning .
Fixed adding fonts folders recursively from configuration files.
Moved fonts aliases map from OpenGl_Display_1 to Font_FontMgr.
Moved fonts name definition from Graphic3d_NameOfFont.hxx to Font_NameOfFont.hxx.
Added new methods to Font_FontMgr: GetAvailableFontsNames, GetFont and FindFont.
Modified Font_SystemFont creation from XLFD. Added method IsEqual to Font_SystemFont.
Modified methods OpenGl_Display::FindFont, OpenGl_FontMgr::request_font in accordance to the new functionality of the Font_FontMgr.
OpenGl_FontMgr now stores only generated fonts instead of duplication of available fonts list.
Removed method OpenGl_FontMgr::requestFontList. Its function now performs Font_FontMgr::GetAvailableFontsNames.
Documentation was fixed
Adjusting testing cases for current state of OCCT
This commit is contained in:
dbv
2012-12-07 13:42:37 +04:00
parent cb9292ed80
commit aff395a36d
29 changed files with 809 additions and 840 deletions

1
src/Font/EXTERNLIB Normal file
View File

@@ -0,0 +1 @@
CSF_FREETYPE

View File

@@ -1 +1,3 @@
EXTERNLIB
Font_NListOfSystemFont.hxx
Font_NameOfFont.hxx

View File

@@ -21,7 +21,8 @@ package Font
uses Standard ,
Quantity ,
TCollection,
OSD
OSD,
TColStd
is
enumeration FontAspect is FA_Undefined, FA_Regular, FA_Bold, FA_Italic, FA_BoldItalic;

View File

@@ -18,27 +18,78 @@
-- and conditions governing the rights and limitations under the License.
class FontMgr from Font inherits TShared from MMgt
---Purpose: Structure for store of Font System Information
---Purpose: Collects and provides information about available fonts in system.
---On Windows it gets information about available fonts from registry value
---"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts". If the description
---of the font does not contain the full path to it, FontMgr looks for it in the
---default fonts directory. (WinAPI function GetSystemWindowsDirectory is used
---to get the path of the shared Windows directory on a multi-user system.)
---On Linux and Mac OS X platforms for getting the directories with fonts, at first
---it checks X11 configuration files, which can be located at "/etc/X11/fs/config",
---"/usr/X11R6/lib/X11/fs/config" or "/usr/X11/lib/X11/fs/config". Then it adds
---default directories(for Linux: "/usr/share/fonts" and "/usr/local/share/fonts",
---for Mac OS X: "/System/Library/Fonts" and "/Library/Fonts"). After that FontMgr
---looks for the "fonts.dir" file in each stored directory. This file contain
---fonts description in XLFD (X Logical Font Description) format.
---On all platforms (Windows, Linux, Mac OS X) we use FreeType library for getting
---font name and aspect.
---On Linux and Mac OS X in cases when the font description obtained from FreeType
---does not match the description in XLFD, FontMgr stores such fonts as two different
---fonts (font management feature on Unix systems).
uses SystemFont,
uses FontAspect,
SystemFont,
NListOfSystemFont,
Path from OSD,
AsciiString from TCollection
AsciiString from TCollection,
HAsciiString from TCollection,
SequenceOfHAsciiString from TColStd
is
GetInstance(myclass) returns FontMgr;
---Level: Public
GetAvalableFonts(me) returns NListOfSystemFont;
GetAvailableFonts(me) returns NListOfSystemFont;
---C++: return const &
GetAvailableFontsNames(me;
theFontsNames: out SequenceOfHAsciiString);
---Purpose: Returns sequence of available fonts names
---Level: Public
GetFont (me;
theFontName : HAsciiString;
theFontAspect : FontAspect;
theFontSize : Integer) returns SystemFont;
---Purpose: Returns font that match given parameters.
--- If theFontName is empty string returned font can have any FontName.
--- If theFontAspect is Font_FA_Undefined returned font can have any FontAspect.
--- If theFontSize is "-1" returned font can have any FontSize.
---Level: Public
--- Private methods
FindFont (me;
theFontName : HAsciiString;
theFontAspect : FontAspect;
theFontSize : Integer) returns SystemFont;
---Purpose: Tries to find font by given parameters.
--- If the specified font is not found tries to use font names mapping.
--- If the requested family name not found -> search for any font family
--- with given aspect and height. If the font is still not found, returns
--- any font available in the system. Returns NULL in case when the fonts
--- are not found in the system.
---Level: Public
--- Private methods
Create returns FontMgr is private;
---Purpose: Creates empty font object
---Level: Private
InitFontDataBase(me:mutable) is private;
InitFontDataBase(me:mutable) is private;
---Purpose: Collects available fonts paths.
---Level: Private
fields
MyListOfFonts: NListOfSystemFont;
myListOfFonts : NListOfSystemFont;
end FontMgr;

View File

@@ -18,417 +18,564 @@
// and conditions governing the rights and limitations under the License.
#include <Font_FontMgr.ixx>
#ifdef WNT
# include <windows.h>
# include <stdlib.h>
#else //WNT
# include <dirent.h>
# include <X11/Xlib.h>
#endif //WNT
#include <OSD_Environment.hxx>
#include <NCollection_List.hxx>
#include <TCollection_HAsciiString.hxx>
#include <NCollection_Map.hxx>
#include <Standard_Stream.hxx>
#include <TCollection_HAsciiString.hxx>
#include <ft2build.h>
#include FT_FREETYPE_H
#ifndef WNT
#include <TCollection_AsciiString.hxx>
struct Font_FontMgr_FontAliasMapNode
{
const char * EnumName;
const char * FontName;
Font_FontAspect FontAspect;
};
#include <NCollection_DefineList.hxx>
#include <NCollection_List.hxx>
#include <OSD_Path.hxx>
#include <OSD_FileIterator.hxx>
#include <OSD_DirectoryIterator.hxx>
#include <OSD_File.hxx>
#include <OSD_FileNode.hxx>
#include <OSD_OpenMode.hxx>
#include <OSD_Protection.hxx>
#include <Font_NListOfSystemFont.hxx>
const Standard_Integer font_service_conf_size = 3;
static Standard_Character font_service_conf[font_service_conf_size][64] = { {"/etc/X11/fs/config"},
{"/usr/X11R6/lib/X11/fs/config"},
{"/usr/X11/lib/X11/fs/config"}
};
DEFINE_LIST( StringList, NCollection_List, TCollection_HAsciiString );
void find_path_with_font_dir( const TCollection_AsciiString& dir,StringList& dirs )
{
if( !dir.IsEmpty() )
{
TCollection_AsciiString PathName( dir );
Standard_Integer rem = PathName.Length();
if ( PathName.SearchFromEnd("/") == rem )
PathName.Remove( rem, 1 );
Standard_Boolean need_to_append = Standard_True;
StringList::Iterator it( dirs );
for( ; it.More(); it.Next() )
{
if ( PathName.IsEqual(it.Value().ToCString()) ) {
need_to_append = Standard_False;
break;
}
}
if ( need_to_append )
dirs.Append( PathName );
OSD_DirectoryIterator osd_dir(PathName,"*");
while(osd_dir.More())
{
OSD_Path path_file;
osd_dir.Values().Path( path_file );
if( path_file.Name().Length() < 1 )
{
osd_dir.Next();
continue;
}
TCollection_AsciiString full_path_name = PathName + "/" + path_file.Name();
rem = full_path_name.Length();
if ( full_path_name.SearchFromEnd("/") == rem )
full_path_name.Remove( rem, 1 );
find_path_with_font_dir( full_path_name, dirs );
osd_dir.Next();
}
}
}
#endif //WNT
Handle(Font_FontMgr) Font_FontMgr::GetInstance() {
static Handle(Font_FontMgr) _mgr;
if ( _mgr.IsNull() )
_mgr = new Font_FontMgr();
return _mgr;
}
Font_FontMgr::Font_FontMgr() {
InitFontDataBase();
}
void Font_FontMgr::InitFontDataBase() {
MyListOfFonts.Clear();
static const Font_FontMgr_FontAliasMapNode Font_FontMgr_MapOfFontsAliases[] =
{
#ifdef WNT
//detect font directory
OSD_Environment env("windir");
TCollection_AsciiString windir_str = env.Value();
if ( windir_str.IsEmpty() )
{ "Courier" , "Courier New" , Font_FA_Regular },
{ "Times-Roman" , "Times New Roman", Font_FA_Regular },
{ "Times-Bold" , "Times New Roman", Font_FA_Bold },
{ "Times-Italic" , "Times New Roman", Font_FA_Italic },
{ "Times-BoldItalic" , "Times New Roman", Font_FA_BoldItalic },
{ "ZapfChancery-MediumItalic", "Script" , Font_FA_Regular },
{ "Symbol" , "Symbol" , Font_FA_Regular },
{ "ZapfDingbats" , "WingDings" , Font_FA_Regular },
{ "Rock" , "Arial" , Font_FA_Regular },
{ "Iris" , "Lucida Console" , Font_FA_Regular }
#else //X11
{ "Courier" , "Courier" , Font_FA_Regular },
{ "Times-Roman" , "Times" , Font_FA_Regular },
{ "Times-Bold" , "Times" , Font_FA_Bold },
{ "Times-Italic" , "Times" , Font_FA_Italic },
{ "Times-BoldItalic" , "Times" , Font_FA_BoldItalic },
{ "Arial" , "Helvetica" , Font_FA_Regular },
{ "ZapfChancery-MediumItalic", "-adobe-itc zapf chancery-medium-i-normal--*-*-*-*-*-*-iso8859-1" , Font_FA_Regular },
{ "Symbol" , "-adobe-symbol-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , Font_FA_Regular },
{ "ZapfDingbats" , "-adobe-itc zapf dingbats-medium-r-normal--*-*-*-*-*-*-adobe-fontspecific" , Font_FA_Regular },
{ "Rock" , "-sgi-rock-medium-r-normal--*-*-*-*-p-*-iso8859-1" , Font_FA_Regular },
{ "Iris" , "--iris-medium-r-normal--*-*-*-*-m-*-iso8859-1" , Font_FA_Regular }
#endif
};
#define NUM_FONT_ENTRIES (sizeof(Font_FontMgr_MapOfFontsAliases)/sizeof(Font_FontMgr_FontAliasMapNode))
#if (defined(_WIN32) || defined(__WIN32__))
#include <windows.h>
#include <stdlib.h>
#ifdef _MSC_VER
#pragma comment (lib, "freetype.lib")
#endif
namespace
{
return;
}
Handle(TCollection_HAsciiString) HFontDir = new TCollection_HAsciiString( windir_str );
HFontDir->AssignCat( "\\Fonts\\" );
#ifdef TRACE
cout << "System font directory: " << HFontDir->ToCString() << "\n";
#endif TRACE
//read registry
HKEY fonts_hkey;
if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"),
0,
KEY_READ,
&fonts_hkey )
!= ERROR_SUCCESS )
// list of supported extensions
static Standard_CString Font_FontMgr_Extensions[] =
{
"ttf",
"otf",
"ttc",
NULL
};
};
#else
#include <OSD_DirectoryIterator.hxx>
#include <OSD_Path.hxx>
#include <OSD_File.hxx>
#include <OSD_OpenMode.hxx>
#include <OSD_Protection.hxx>
namespace
{
// list of supported extensions
static Standard_CString Font_FontMgr_Extensions[] =
{
"ttf",
"otf",
"ttc",
"pfa",
"pfb",
NULL
};
// X11 configuration file in plain text format (obsolete - doesn't exists in modern distributives)
static Standard_CString myFontServiceConf[] = {"/etc/X11/fs/config",
"/usr/X11R6/lib/X11/fs/config",
"/usr/X11/lib/X11/fs/config",
NULL
};
#ifdef __APPLE__
// default fonts paths in Mac OS X
static Standard_CString myDefaultFontsDirs[] = {"/System/Library/Fonts",
"/Library/Fonts",
NULL
};
#else
// default fonts paths in most Unix systems (Linux and others)
static Standard_CString myDefaultFontsDirs[] = {"/usr/share/fonts",
"/usr/local/share/fonts",
NULL
};
#endif
static void addDirsRecursively (const OSD_Path& thePath,
NCollection_Map<TCollection_AsciiString>& theDirsMap)
{
TCollection_AsciiString aDirName;
thePath.SystemName (aDirName);
if (!theDirsMap.Add (aDirName))
{
return;
}
for (OSD_DirectoryIterator aDirIterator (thePath, "*"); aDirIterator.More(); aDirIterator.Next())
{
OSD_Path aChildDirPath;
aDirIterator.Values().Path (aChildDirPath);
TCollection_AsciiString aChildDirName;
aChildDirPath.SystemName (aChildDirName);
if (!aChildDirName.IsEqual (".") && !aChildDirName.IsEqual (".."))
{
aChildDirName = aDirName + "/" + aChildDirName;
OSD_Path aPath (aChildDirName);
addDirsRecursively (aPath, theDirsMap);
}
}
}
};
#endif
// =======================================================================
// function : checkFont
// purpose :
// =======================================================================
static Handle(Font_SystemFont) checkFont (FT_Library theFTLib,
const Standard_CString theFontPath)
{
FT_Face aFontFace;
FT_Error aFaceError = FT_New_Face (theFTLib, theFontPath, 0, &aFontFace);
if (aFaceError != FT_Err_Ok)
{
return NULL;
}
Font_FontAspect anAspect = Font_FA_Regular;
if (aFontFace->style_flags == (FT_STYLE_FLAG_ITALIC | FT_STYLE_FLAG_BOLD))
{
anAspect = Font_FA_BoldItalic;
}
else if (aFontFace->style_flags == FT_STYLE_FLAG_ITALIC)
{
anAspect = Font_FA_Italic;
}
else if (aFontFace->style_flags == FT_STYLE_FLAG_BOLD)
{
anAspect = Font_FA_Bold;
}
Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aFontFace->family_name);
Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (theFontPath);
Handle(Font_SystemFont) aResult = new Font_SystemFont (aFontName, anAspect, aFontPath);
FT_Done_Face (aFontFace);
return aResult;
}
// =======================================================================
// function : GetInstance
// purpose :
// =======================================================================
Handle(Font_FontMgr) Font_FontMgr::GetInstance()
{
static Handle(Font_FontMgr) _mgr;
if (_mgr.IsNull())
{
_mgr = new Font_FontMgr();
}
return _mgr;
}
// =======================================================================
// function : Font_FontMgr
// purpose :
// =======================================================================
Font_FontMgr::Font_FontMgr()
{
InitFontDataBase();
}
// =======================================================================
// function : InitFontDataBase
// purpose :
// =======================================================================
void Font_FontMgr::InitFontDataBase()
{
myListOfFonts.Clear();
FT_Library aFtLibrary = NULL;
#if (defined(_WIN32) || defined(__WIN32__))
// font directory is placed in "C:\Windows\Fonts\"
UINT aStrLength = GetSystemWindowsDirectoryA (NULL, 0);
if (aStrLength == 0)
{
return;
}
Standard_Integer id = 0;
Standard_Character buf_name[100];
Standard_Byte buf_data[100];
DWORD size_name = 100,
size_data = 100;
while ( true )
{
//detect file name
DWORD type;
size_name = 100,
size_data = 100;
Font_FontAspect aspect;
if( RegEnumValue( fonts_hkey,
id,
buf_name,
&size_name,
NULL,
&type,
buf_data,
&size_data) == ERROR_NO_MORE_ITEMS ) {
break;
}
Handle(TCollection_HAsciiString) fname =
new TCollection_HAsciiString(buf_name);
fname->RightAdjust();
fname->LeftAdjust();
//remove construction like (TrueType....
Standard_Integer anIndexTT = fname->SearchFromEnd( new TCollection_HAsciiString( " (" ) );
Standard_Boolean aTruncate = Standard_False;
if ( anIndexTT > 1 )
fname->Trunc( anIndexTT );
Standard_Integer anIndex = 0;
fname->RightAdjust();
if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Bold Italic") ) ) > 0 ) {
aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
aspect = Font_FA_BoldItalic;
} else if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Bold") ) ) > 0 ) {
aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
aspect = Font_FA_Bold;
} else if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Italic") ) ) > 0 ) {
aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
aspect = Font_FA_Italic;
} else {
aspect = Font_FA_Regular;
}
if( aTruncate )
fname->Trunc( anIndex - 1 );
fname->RightAdjust();
Handle(TCollection_HAsciiString) file_path =
new TCollection_HAsciiString( (Standard_Character*)buf_data );
if ( strchr( (Standard_Character*)buf_data, '\\' ) == NULL ) {
file_path->Insert( 1, HFontDir );
}
if( ( ( file_path->Search(".ttf") > 0 ) || ( file_path->Search(".TTF") > 0 ) ||
( file_path->Search(".otf") > 0 ) || ( file_path->Search(".OTF") > 0 ) ||
( file_path->Search(".ttc") > 0 ) || ( file_path->Search(".TTC") > 0 ) ) ){
MyListOfFonts.Append( new Font_SystemFont( fname, aspect, file_path ) );
#ifdef TRACE
cout << "Adding font...\n"
<< " font name: " << fname->ToCString() << "\n"
<< " font file: " << file_path->ToCString() << "\n"
<< " font aspect: ";
switch( aspect ) {
case Font_FA_Bold:
cout << "Font_FA_Bold\n";
break;
case Font_FA_BoldItalic:
cout << "Font_FA_BoldItalic\n";
break;
case Font_FA_Italic:
cout << "Font_FA_Italic\n";
break;
default:
cout << "Font_FA_Regular\n";
break;
}
#endif
}
id++;
}
//close registry
RegCloseKey( fonts_hkey );
#endif //WNT
#ifndef WNT
StringList dirs;
Handle(TCollection_HAsciiString) str = new TCollection_HAsciiString;
Display * disp = XOpenDisplay("localhost:0.0");
if (!disp)
{
// let the X server find the available connection
disp = XOpenDisplay(":0.0");
if (!disp)
{
cout << "Display is NULL!" << endl;
return ;
}
}
Standard_Integer npaths = 0;
char* aWinDir = new char[aStrLength];
GetSystemWindowsDirectoryA (aWinDir, aStrLength);
Handle(TCollection_HAsciiString) aFontsDir = new TCollection_HAsciiString (aWinDir);
aFontsDir->AssignCat ("\\Fonts\\");
delete[] aWinDir;
Standard_Character** fontpath = XGetFontPath(disp, &npaths);
#ifdef TRACE
cout << "NPATHS = " << npaths << endl ;
#endif
for (Standard_Integer i = 0; i < npaths; i++ )
{
#ifdef TRACE
cout << "Font Path: " << fontpath[i] << endl;
#endif
if ( fontpath[i][0] == '/' ) {
TCollection_AsciiString aFontPath( fontpath[i] );
find_path_with_font_dir( aFontPath, dirs );
}
else
{
TCollection_AsciiString aFontPath( fontpath[i] );
TCollection_AsciiString aCutFontPath;
Standard_Integer location = -1 ;
location = aFontPath.Location( "/",1,aFontPath.Length() );
if( location > 0 )
aCutFontPath.AssignCat( aFontPath.SubString(location, aFontPath.Length() ) );
find_path_with_font_dir( aCutFontPath, dirs );
}
}
XFreeFontPath(fontpath);
OSD_OpenMode aMode = OSD_ReadOnly;
OSD_Protection aProtect( OSD_R, OSD_R, OSD_R, OSD_R );
for( Standard_Integer j = 0 ; j < font_service_conf_size; j++ )
// read fonts list from registry
HKEY aFontsKey;
if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts",
0, KEY_READ, &aFontsKey) != ERROR_SUCCESS)
{
TCollection_AsciiString fileOfFontServiceName( font_service_conf[j] );
OSD_File aFile( fileOfFontServiceName );
return;
}
if( aFile.Exists() )
aFile.Open( aMode, aProtect );
if( aFile.IsOpen() )//font service
NCollection_Map<TCollection_AsciiString> aSupportedExtensions;
for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter)
{
Standard_CString anExt = Font_FontMgr_Extensions[anIter];
aSupportedExtensions.Add (TCollection_AsciiString (anExt));
}
FT_Init_FreeType (&aFtLibrary);
static const DWORD aBufferSize = 256;
char aNameBuff[aBufferSize];
char aPathBuff[aBufferSize];
DWORD aNameSize = aBufferSize;
DWORD aPathSize = aBufferSize;
for (DWORD anIter = 0;
RegEnumValueA (aFontsKey, anIter,
aNameBuff, &aNameSize, NULL, NULL,
(LPBYTE )aPathBuff, &aPathSize) != ERROR_NO_MORE_ITEMS;
++anIter, aNameSize = aBufferSize, aPathSize = aBufferSize)
{
aPathBuff[(aPathSize < aBufferSize) ? aPathSize : (aBufferSize - 1)] = '\0'; // ensure string is NULL-terminated
Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (aNameBuff);
Handle(TCollection_HAsciiString) aFontPath = new TCollection_HAsciiString (aPathBuff);
if (aFontPath->Search ("\\") == -1)
{
Standard_Integer aNByte = 256;
Standard_Integer aNbyteRead;
TCollection_AsciiString aStr( aNByte );//read string with information
TCollection_AsciiString aStrCut( aNByte );//cut of string
TCollection_AsciiString endStr;//cutting string
aFontPath->Insert (1, aFontsDir); // make absolute path
}
Standard_Boolean read_dirs = Standard_False;
Standard_Integer location =- 1; //disposition of necessary literals
Standard_Integer begin =- 1; //first left entry in string
Standard_Integer end =- 1; //first right entry in string
while( !aFile.IsAtEnd() )
// check file extension is in list of supported
const Standard_Integer anExtensionPosition = aFontPath->SearchFromEnd (".") + 1;
if (anExtensionPosition > 0 && anExtensionPosition < aFontPath->Length())
{
Handle(TCollection_HAsciiString) aFontExtension = aFontPath->SubString (anExtensionPosition, aFontPath->Length());
aFontExtension->LowerCase();
if (aSupportedExtensions.Contains (aFontExtension->String()))
{
aFile.ReadLine( aStr, aNByte, aNbyteRead );//reading 1 lines(256 bytes)
location = aStr.Location( "catalogue = ", 1, aStr.Length() );
if(location == 0)
location = aStr.Location( "catalogue=", 1, aStr.Length() );
if(location == 0)
location = aStr.Location( "catalogue= ", 1, aStr.Length() );
if(location == 0)
location = aStr.Location( "catalogue = ", 1, aStr.Length() );
if( location > 0 )
Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString());
if (!aNewFont.IsNull())
{
#ifdef TRACE
cout << " Font config find!!" << endl;
#endif
read_dirs = Standard_True;
myListOfFonts.Append (aNewFont);
}
if( read_dirs )
{
begin = aStr.Location( "/", 1, aStr.Length() );//begin of path name
end = aStr.Location( ":", 1, aStr.Length() );//end of path name
if( end < 1 )
end = aStr.Location( ",", 1, aStr.Length() );//also end of path name
end -= 1;
if( begin > 0 && end > 0 )
{
if( ( end - begin ) > 0 )
endStr.AssignCat( aStr.SubString ( begin, end ) );//cutting necessary literals for string
dirs.Append( TCollection_HAsciiString ( endStr ) );
endStr.Clear();
}
else
if( begin > 0 && end == -1 )
{
//if end of string don't have "," or ":"
//it is possible last sentence in block of word
endStr.AssignCat( aStr.SubString( begin, aStr.Length() - 1 ) );
dirs.Append( TCollection_HAsciiString( endStr ) );
endStr.Clear();
}
}
}
aFile.Close();
}
}
if( dirs.Size() > 0 )
{
//if dirs list contains elements
OSD_OpenMode aModeRead = OSD_ReadOnly;
OSD_Protection aProtectRead( OSD_R, OSD_R , OSD_R , OSD_R );
// close registry key
RegCloseKey (aFontsKey);
TCollection_AsciiString fileFontsDir;
StringList::Iterator it( dirs );
for( ; it.More(); it.Next() )
{
fileFontsDir.AssignCat( it.Value().ToCString() );
fileFontsDir.AssignCat( "/fonts.dir" );//append file name in path way
#else
OSD_File readFile( fileFontsDir );
readFile.Open( aModeRead, aProtectRead );
NCollection_Map<TCollection_AsciiString> aMapOfFontsDirs;
const OSD_Protection aProtectRead (OSD_R, OSD_R, OSD_R, OSD_R);
Standard_Integer aNbyteRead, aNByte = 256;
if( readFile.IsOpen ( ) )
{
TCollection_AsciiString aLine( aNByte );
Standard_Integer countOfString = 0 ;
while( ! readFile.IsAtEnd() )//return true if EOF
{
if( countOfString > 1 )
{
readFile.ReadLine( aLine , aNByte , aNbyteRead );
if( ( ( aLine.Search(".pfa") > 0 ) || ( aLine.Search(".PFA") > 0 ) ||
( aLine.Search(".pfb") > 0 ) || ( aLine.Search(".PFB") > 0 ) ||
( aLine.Search(".ttf") > 0 ) || ( aLine.Search(".TTF") > 0 ) ||
( aLine.Search(".otf") > 0 ) || ( aLine.Search(".OTF") > 0 ) ||
( aLine.Search(".ttc") > 0 ) || ( aLine.Search(".TTC") > 0 ) )
&& ( aLine.Search( "iso8859-1\n" ) > 0 ) )
{
// In current implementation use fonts with ISO-8859-1 coding page.
// OCCT not give to manage coding page by means of programm interface.
// TODO: make high level interface for
// choosing necessary coding page.
TCollection_AsciiString aXLFD;
Standard_Integer leftXLFD = aLine.SearchFromEnd(" ");
Standard_Integer rightXLFD = aLine.Length();
if( leftXLFD && rightXLFD )
aXLFD.AssignCat(aLine.SubString( leftXLFD + 1, rightXLFD ) );
TCollection_AsciiString aPath;
TCollection_AsciiString aTemp( it.Value().ToCString() );
if ( aTemp.SearchFromEnd("/") == aTemp.Length() )
{
//this branch intend to SUN
aPath.AssignCat( aTemp.ToCString() );
aPath.AssignCat( aLine.Token( " ", 1 ) );
}
else {
//this branch intend to Linux
aPath.AssignCat( aTemp.ToCString( ) );
aPath.AssignCat( "/" );
aPath.AssignCat( aLine.Token( " ", 1 ) );
}
MyListOfFonts.Append( new Font_SystemFont( new TCollection_HAsciiString( aXLFD ),
new TCollection_HAsciiString( aPath ) ) );
}
}
else
readFile.ReadLine( aLine, aNByte, aNbyteRead );
countOfString++;
}
readFile.Close();
}
fileFontsDir.Clear();
// read fonts directories from font service config file (obsolete)
for (Standard_Integer anIter = 0; myFontServiceConf[anIter] != NULL; ++anIter)
{
const TCollection_AsciiString aFileOfFontsPath (myFontServiceConf[anIter]);
OSD_File aFile (aFileOfFontsPath);
if (!aFile.Exists())
{
continue;
}
aFile.Open (OSD_ReadOnly, aProtectRead);
if (!aFile.IsOpen())
{
continue;
}
Standard_Integer aNByte = 256;
Standard_Integer aNbyteRead;
TCollection_AsciiString aStr; // read string with information
while (!aFile.IsAtEnd())
{
Standard_Integer aLocation = -1;
Standard_Integer aPathLocation = -1;
aFile.ReadLine (aStr, aNByte, aNbyteRead); // reading 1 line (256 bytes)
aLocation = aStr.Search ("catalogue=");
if (aLocation < 0)
{
aLocation = aStr.Search ("catalogue =");
}
aPathLocation = aStr.Search ("/");
if (aLocation > 0 && aPathLocation > 0)
{
aStr = aStr.Split (aPathLocation - 1);
TCollection_AsciiString aFontPath;
Standard_Integer aPathNumber = 1;
do
{
// Getting directory paths, which can be splitted by "," or ":"
aFontPath = aStr.Token (":,", aPathNumber);
aFontPath.RightAdjust();
if (!aFontPath.IsEmpty())
{
OSD_Path aPath(aFontPath);
addDirsRecursively (aPath, aMapOfFontsDirs);
}
aPathNumber++;
}
while (!aFontPath.IsEmpty());
}
}
aFile.Close();
}
// append default directories
for (Standard_Integer anIter = 0; myDefaultFontsDirs[anIter] != NULL; ++anIter)
{
Standard_CString anItem = myDefaultFontsDirs[anIter];
TCollection_AsciiString aPathStr (anItem);
OSD_Path aPath (aPathStr);
addDirsRecursively (aPath, aMapOfFontsDirs);
}
NCollection_Map<TCollection_AsciiString> aSupportedExtensions;
for (Standard_Integer anIter = 0; Font_FontMgr_Extensions[anIter] != NULL; ++anIter)
{
Standard_CString anExt = Font_FontMgr_Extensions[anIter];
aSupportedExtensions.Add (TCollection_AsciiString (anExt));
}
FT_Init_FreeType (&aFtLibrary);
for (NCollection_Map<TCollection_AsciiString>::Iterator anIter (aMapOfFontsDirs);
anIter.More(); anIter.Next())
{
OSD_File aReadFile (anIter.Value() + "/fonts.dir");
if (!aReadFile.Exists())
{
continue; // invalid fonts directory
}
aReadFile.Open (OSD_ReadOnly, aProtectRead);
if (!aReadFile.IsOpen())
{
continue; // invalid fonts directory
}
Standard_Integer aNbyteRead, aNByte = 256;
TCollection_AsciiString aLine (aNByte);
Standard_Boolean isFirstLine = Standard_True;
const TCollection_AsciiString anEncoding ("iso8859-1\n");
while (!aReadFile.IsAtEnd())
{
aReadFile.ReadLine (aLine, aNByte, aNbyteRead);
if (isFirstLine)
{
// first line contains the number of fonts in this file
// just ignoring it...
isFirstLine = Standard_False;
continue;
}
Standard_Integer anExtensionPosition = aLine.Search (".") + 1;
if (anExtensionPosition == 0)
{
continue; // can't find extension position in the font description
}
Standard_Integer anEndOfFileName = aLine.Location (" ", anExtensionPosition, aLine.Length()) - 1;
if (anEndOfFileName < 0 || anEndOfFileName < anExtensionPosition)
{
continue; // font description have empty extension
}
TCollection_AsciiString aFontExtension = aLine.SubString (anExtensionPosition, anEndOfFileName);
aFontExtension.LowerCase();
if (aSupportedExtensions.Contains (aFontExtension) && (aLine.Search (anEncoding) > 0))
{
// In current implementation use fonts with ISO-8859-1 coding page.
// OCCT not give to manage coding page by means of programm interface.
// TODO: make high level interface for choosing necessary coding page.
Handle(TCollection_HAsciiString) aXLFD =
new TCollection_HAsciiString (aLine.SubString (anEndOfFileName + 2, aLine.Length()));
Handle(TCollection_HAsciiString) aFontPath =
new TCollection_HAsciiString (anIter.Value().ToCString());
if (aFontPath->SearchFromEnd ("/") != aFontPath->Length())
{
aFontPath->AssignCat ("/");
}
Handle(TCollection_HAsciiString) aFontFileName =
new TCollection_HAsciiString (aLine.SubString (1, anEndOfFileName));
aFontPath->AssignCat (aFontFileName);
Handle(Font_SystemFont) aNewFontFromXLFD = new Font_SystemFont (aXLFD, aFontPath);
Handle(Font_SystemFont) aNewFont = checkFont (aFtLibrary, aFontPath->ToCString());
if (aNewFontFromXLFD->IsValid() && !aNewFont.IsNull() &&
!aNewFont->IsEqual (aNewFontFromXLFD))
{
myListOfFonts.Append (aNewFont);
myListOfFonts.Append (aNewFontFromXLFD);
}
else if (!aNewFont.IsNull())
{
myListOfFonts.Append (aNewFont);
}
else if (aNewFontFromXLFD->IsValid())
{
myListOfFonts.Append (aNewFontFromXLFD);
}
}
}
aReadFile.Close();
}
#endif
FT_Done_FreeType (aFtLibrary);
}
Font_NListOfSystemFont Font_FontMgr::GetAvalableFonts() const
// =======================================================================
// function : GetAvailableFonts
// purpose :
// =======================================================================
const Font_NListOfSystemFont& Font_FontMgr::GetAvailableFonts() const
{
return MyListOfFonts;
return myListOfFonts;
}
void Font_FontMgr::GetAvailableFontsNames (TColStd_SequenceOfHAsciiString& theFontsNames) const
{
theFontsNames.Clear();
for (Font_NListOfSystemFont::Iterator anIter(myListOfFonts); anIter.More(); anIter.Next())
{
theFontsNames.Append (anIter.Value()->FontName());
}
}
Handle(Font_SystemFont) Font_FontMgr::GetFont (const Handle(TCollection_HAsciiString)& theFontName,
const Font_FontAspect theFontAspect,
const Standard_Integer theFontSize) const
{
if ( (theFontSize < 2 && theFontSize != -1) || theFontName.IsNull())
{
return NULL;
}
Font_NListOfSystemFont::Iterator aFontsIterator (myListOfFonts);
for (; aFontsIterator.More(); aFontsIterator.Next())
{
if (!theFontName->IsEmpty() && !aFontsIterator.Value()->FontName()->IsSameString (theFontName, Standard_False))
{
continue;
}
if (theFontAspect != Font_FA_Undefined && aFontsIterator.Value()->FontAspect() != theFontAspect)
{
continue;
}
if (theFontSize == -1 || aFontsIterator.Value()->FontHeight() == -1 ||
theFontSize == aFontsIterator.Value()->FontHeight())
{
return aFontsIterator.Value();
}
}
return NULL;
}
Handle(Font_SystemFont) Font_FontMgr::FindFont (const Handle(TCollection_HAsciiString)& theFontName,
const Font_FontAspect theFontAspect,
const Standard_Integer theFontSize) const
{
Handle(TCollection_HAsciiString) aFontName = theFontName;
Font_FontAspect aFontAspect = theFontAspect;
Standard_Integer aFontSize = theFontSize;
Handle(Font_SystemFont) aFont = GetFont (aFontName, aFontAspect, aFontSize);
if (!aFont.IsNull())
{
return aFont;
}
// Trying to use font names mapping
for (Standard_Integer anIter = 0; anIter < NUM_FONT_ENTRIES; ++anIter)
{
Handle(TCollection_HAsciiString) aFontAlias =
new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].EnumName);
if (aFontAlias->IsSameString (aFontName, Standard_False))
{
aFontName = new TCollection_HAsciiString (Font_FontMgr_MapOfFontsAliases[anIter].FontName);
aFontAspect = Font_FontMgr_MapOfFontsAliases[anIter].FontAspect;
break;
}
}
aFont = GetFont (aFontName, aFontAspect, aFontSize);
if (!aFont.IsNull())
{
return aFont;
}
// Requested family name not found -> search for any font family with given aspect and height
aFontName = new TCollection_HAsciiString ("");
aFont = GetFont (aFontName, aFontAspect, aFontSize);
if (!aFont.IsNull())
{
return aFont;
}
// The last resort: trying to use ANY font available in the system
aFontAspect = Font_FA_Undefined;
aFontSize = -1;
aFont = GetFont (aFontName, aFontAspect, aFontSize);
if (!aFont.IsNull())
{
return aFont;
}
return NULL; // Fonts are not found in the system.
}

View File

@@ -23,4 +23,10 @@
DEFINE_LIST (Font_NListOfSystemFont,
NCollection_List,
Handle(Font_SystemFont))
#endif
inline Standard_Boolean IsEqual (const Handle(Font_SystemFont)& theFirstFont,
const Handle(Font_SystemFont)& theSecondFont)
{
return theFirstFont->IsEqual (theSecondFont);
}
#endif

View File

@@ -0,0 +1,38 @@
// Created on: 2009-05-10
// Created by: Paul V. SUPRYATKIN
// Copyright (c) 2009-2012 OPEN CASCADE SAS
//
// The content of this file is subject to the Open CASCADE Technology Public
// License Version 6.5 (the "License"). You may not use the content of this file
// except in compliance with the License. Please obtain a copy of the License
// at http://www.opencascade.org and read it completely before using this file.
//
// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
//
// The Original Code and all software distributed under the License is
// distributed on an "AS IS" basis, without warranty of any kind, and the
// Initial Developer hereby disclaims all such warranties, including without
// limitation, any warranties of merchantability, fitness for a particular
// purpose or non-infringement. Please see the License for the specific terms
// and conditions governing the rights and limitations under the License.
#define Font_NOF_ASCII_MONO "Courier"
#define Font_NOF_ASCII_SIMPLEX "Times-Roman"
#define Font_NOF_ASCII_COMPLEX "Times-Roman"
#define Font_NOF_ASCII_DUPLEX "Times-Bold"
#define Font_NOF_ASCII_TRIPLEX "Times-Bold"
#define Font_NOF_ASCII_ITALIC_COMPLEX "Times-Italic"
#define Font_NOF_ASCII_ITALIC_TRIPLEX "Times-BoldItalic"
#define Font_NOF_ASCII_SCRIPT_SIMPLEX "ZapfChancery-MediumItalic"
#define Font_NOF_ASCII_SCRIPT_COMPLEX "ZapfChancery-MediumItalic"
#define Font_NOF_GREEK_MONO "Symbol"
#define Font_NOF_GREEK_SIMPLEX "Symbol"
#define Font_NOF_GREEK_COMPLEX "Symbol"
#define Font_NOF_SYMBOL_MONO "ZapfDingbats"
#define Font_NOF_SYMBOL_SIMPLEX "ZapfDingbats"
#define Font_NOF_CARTOGRAPHIC_SIMPLEX "Rock"
#define Font_NOF_KANJI_MONO "Iris"
#define Font_NOF_KATAKANA_MONO "Iris"

View File

@@ -28,16 +28,17 @@ is
---Purpose: Creates empty font object
---Level: Public
Create (FontName : HAsciiString;
Aspect : FontAspect;
FilePath : HAsciiString ) returns SystemFont;
Create (theFontName : HAsciiString;
theFontAspect : FontAspect;
theFilePath : HAsciiString ) returns SystemFont;
---Purpose: Creates Font object initialized with <FontName> as name
--- <FontAspect>.... TODO
---Level: Public
Create (XLFD : HAsciiString;
FilePath : HAsciiString ) returns SystemFont;
---Purpose: TODO
Create (theXLFD : HAsciiString;
theFilePath : HAsciiString ) returns SystemFont;
---Purpose: Creates Font object and initialize class fields with
--- values taken from XLFD (X Logical Font Description)
---Level: Public
FontName (me) returns HAsciiString;
@@ -58,7 +59,12 @@ is
--- Level: Public
IsValid (me) returns Boolean;
IsEqual (me;
theOtherFont: SystemFont) returns Boolean;
--- Purpose: Return true if the FontName, FontAspect and FontSize are the same.
--- Level: Public
fields
MyFontName: HAsciiString; --Font family name
MyFontAspect: FontAspect;

View File

@@ -45,48 +45,46 @@ MyVerification(Standard_True)
}
Font_SystemFont::Font_SystemFont( const Handle(TCollection_HAsciiString)& XLFD,
const Handle(TCollection_HAsciiString)& FilePath) :
MyFilePath(FilePath),
MyFontAspect(Font_FA_Undefined)
Font_SystemFont::Font_SystemFont (const Handle(TCollection_HAsciiString)& theXLFD,
const Handle(TCollection_HAsciiString)& theFilePath) :
MyFilePath(theFilePath),
MyFontAspect(Font_FA_Regular)
{
MyVerification = Standard_True;
if ( XLFD.IsNull() )
if (theXLFD.IsNull())
{
MyVerification=Standard_False;
printf("NULL XLFD handler \n");
MyVerification = Standard_False; // empty font description handler
}
if ( XLFD->IsEmpty() )
if (theXLFD->IsEmpty())
{
MyVerification=Standard_False;
printf("EMPTY XLFD handler \n");
MyVerification = Standard_False; // empty font description
}
if(MyVerification)
if (MyVerification)
{
MyFontName = XLFD->Token( "-", 2 );
TCollection_AsciiString str( XLFD->ToCString() );
MyFontName = theXLFD->Token ("-", 2);
TCollection_AsciiString aXLFD (theXLFD->ToCString());
if ( str.Search( "-0-0-0-0-" ) >=0 )
MyFaceSize = -1;
// Getting font size for fixed size fonts
if (aXLFD.Search ("-0-0-0-0-") >= 0)
MyFaceSize = -1; // Scalable font
else
//TODO catch exeption
MyFaceSize = str.Token( "-", 7 ).IntegerValue();
MyFaceSize = aXLFD.Token ("-", 7).IntegerValue();
//detect aspect
if ( str.Token("-", 3).IsEqual( "bold" ) )
MyFontAspect = Font_FA_Bold;
else if ( str.Token("-", 3).IsEqual( "medium" ) ||
str.Token("-", 3).IsEqual( "normal" ) )
MyFontAspect = Font_FA_Regular;
if ( MyFontAspect != Font_FA_Undefined &&
( str.Token("-",4 ).IsEqual( "i" ) || str.Token("-",4 ).IsEqual( "o" ) ) )
// Detect font aspect
if (aXLFD.Token ("-", 3).IsEqual ("bold") &&
(aXLFD.Token ("-", 4).IsEqual ("i") || aXLFD.Token ("-", 4).IsEqual ("o")))
{
if ( MyFontAspect == Font_FA_Bold )
MyFontAspect = Font_FA_BoldItalic;
else
MyFontAspect = Font_FA_Italic;
MyFontAspect = Font_FA_BoldItalic;
}
else if (aXLFD.Token ("-", 3).IsEqual ("bold"))
{
MyFontAspect = Font_FA_Bold;
}
else if (aXLFD.Token ("-", 4).IsEqual ("i") || aXLFD.Token ("-", 4).IsEqual ("o"))
{
MyFontAspect = Font_FA_Italic;
}
}
}
@@ -120,3 +118,23 @@ Font_FontAspect Font_SystemFont::FontAspect() const{
Standard_Integer Font_SystemFont::FontHeight() const {
return MyFaceSize;
}
Standard_Boolean Font_SystemFont::IsEqual(const Handle(Font_SystemFont)& theOtherFont) const
{
if (!MyFontName->IsSameString (theOtherFont->FontName(), Standard_False))
{
return Standard_False;
}
if (MyFontAspect != theOtherFont->FontAspect())
{
return Standard_False;
}
if (MyFaceSize != theOtherFont->FontHeight())
{
return Standard_False;
}
return Standard_True;
}