1 // Copyright: OpenCASCADE
3 // File: OSD_FontMgr.cxx
5 // Author: Alexander A. BORODIN
8 #include <OSD_FontMgr.ixx>
14 # include <X11/Xlib.h>
17 #include <NCollection_List.hxx>
18 #include <TCollection_HAsciiString.hxx>
19 #include <Standard_Stream.hxx>
23 #include <TCollection_AsciiString.hxx>
25 #include <NCollection_DefineList.hxx>
26 #include <NCollection_List.hxx>
28 #include <OSD_Path.hxx>
29 #include <OSD_FileIterator.hxx>
30 #include <OSD_DirectoryIterator.hxx>
31 #include <OSD_File.hxx>
32 #include <OSD_FileNode.hxx>
33 #include <OSD_OpenMode.hxx>
34 #include <OSD_Protection.hxx>
35 #include <OSD_NListOfSystemFont.hxx>
37 const Standard_Integer font_service_conf_size = 3;
38 static Standard_Character font_service_conf[font_service_conf_size][64] = { {"/etc/X11/fs/config"},
39 {"/usr/X11R6/lib/X11/fs/config"},
40 {"/usr/X11/lib/X11/fs/config"}
43 DEFINE_LIST( StringList, NCollection_List, TCollection_HAsciiString );
45 void find_path_with_font_dir( const TCollection_AsciiString& dir,StringList& dirs )
49 TCollection_AsciiString PathName( dir );
51 Standard_Integer rem = PathName.Length();
53 if ( PathName.SearchFromEnd("/") == rem )
54 PathName.Remove( rem, 1 );
56 Standard_Boolean need_to_append = Standard_True;
58 StringList::Iterator it( dirs );
59 for( ; it.More(); it.Next() )
61 if ( PathName.IsEqual(it.Value().ToCString()) ) {
62 need_to_append = Standard_False;
67 dirs.Append( PathName );
69 OSD_DirectoryIterator osd_dir(PathName,"*");
73 osd_dir.Values().Path( path_file );
74 if( path_file.Name().Length() < 1 )
80 TCollection_AsciiString full_path_name = PathName + "/" + path_file.Name();
81 rem = full_path_name.Length();
82 if ( full_path_name.SearchFromEnd("/") == rem )
83 full_path_name.Remove( rem, 1 );
84 find_path_with_font_dir( full_path_name, dirs );
93 Handle(OSD_FontMgr) OSD_FontMgr::GetInstance() {
95 static Handle(OSD_FontMgr) _mgr;
97 _mgr = new OSD_FontMgr();
103 OSD_FontMgr::OSD_FontMgr() {
110 void OSD_FontMgr::InitFontDataBase() {
112 MyListOfFonts.Clear();
115 //detect font directory
117 Standard_Character* windir_var;
118 Standard_Size req_size;
119 req_size = strlen( getenv("windir") );
121 windir_var = new Standard_Character[req_size];
123 strcpy( windir_var, getenv("windir") );
125 Standard_Character *font_dir = new Standard_Character[ req_size + strlen("\\Fonts\\") + 1 ] ;
127 if( !strcpy( font_dir, windir_var ) )
129 if( !strcat( font_dir, "\\Fonts\\" ) )
132 Handle(TCollection_HAsciiString) HFontDir = new TCollection_HAsciiString(font_dir);
135 cout << "System font directory: " << font_dir << "\n";
140 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
141 TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"),
149 Standard_Integer id = 0;
150 Standard_Character buf_name[100];
151 Standard_Byte buf_data[100];
152 DWORD size_name = 100,
161 OSD_FontAspect aspect;
162 if( RegEnumValue( fonts_hkey,
169 &size_data) == ERROR_NO_MORE_ITEMS ) {
172 Handle(TCollection_HAsciiString) fname =
173 new TCollection_HAsciiString(buf_name);
174 fname->RightAdjust();
176 //remove construction like (TrueType....
177 Standard_Integer anIndexTT = fname->SearchFromEnd( new TCollection_HAsciiString( " (" ) );
178 Standard_Boolean aTruncate = Standard_False;
180 fname->Trunc( anIndexTT );
181 Standard_Integer anIndex = 0;
182 fname->RightAdjust();
183 if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Bold Italic") ) ) > 0 ) {
184 aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
185 aspect = OSD_FA_BoldItalic;
186 } else if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Bold") ) ) > 0 ) {
187 aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
188 aspect = OSD_FA_Bold;
189 } else if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Italic") ) ) > 0 ) {
190 aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
191 aspect = OSD_FA_Italic;
193 aspect = OSD_FA_Regular;
196 fname->Trunc( anIndex - 1 );
197 fname->RightAdjust();
198 Handle(TCollection_HAsciiString) file_path =
199 new TCollection_HAsciiString( (Standard_Character*)buf_data );
200 if ( strchr( (Standard_Character*)buf_data, '\\' ) == NULL ) {
201 file_path->Insert( 1, HFontDir );
203 Handle(TCollection_HAsciiString) HFontDir = new TCollection_HAsciiString(font_dir);
205 if( ( ( file_path->Search(".ttf") > 0 ) || ( file_path->Search(".TTF") > 0 ) ||
206 ( file_path->Search(".otf") > 0 ) || ( file_path->Search(".OTF") > 0 ) ||
207 ( file_path->Search(".ttc") > 0 ) || ( file_path->Search(".TTC") > 0 ) ) ){
208 MyListOfFonts.Append( new OSD_SystemFont( fname, aspect, file_path ) );
210 cout << "Adding font...\n"
211 << " font name: " << fname->ToCString() << "\n"
212 << " font file: " << file_path->ToCString() << "\n"
216 cout << "OSD_FA_Bold\n";
218 case OSD_FA_BoldItalic:
219 cout << "OSD_FA_BoldItalic\n";
222 cout << "OSD_FA_Italic\n";
225 cout << "OSD_FA_Regular\n";
233 RegCloseKey( fonts_hkey );
238 Handle(TCollection_HAsciiString) str = new TCollection_HAsciiString;
239 Display * disp = XOpenDisplay("localhost:0.0");
243 cout << "Display is NULL!" << endl;
247 Standard_Integer npaths = 0;
249 Standard_Character** fontpath = XGetFontPath(disp, &npaths);
251 cout << "NPATHS = " << npaths << endl ;
253 for (Standard_Integer i = 0; i < npaths; i++ )
256 cout << "Font Path: " << fontpath[i] << endl;
258 if ( fontpath[i][0] == '/' ) {
259 TCollection_AsciiString aFontPath( fontpath[i] );
260 find_path_with_font_dir( aFontPath, dirs );
264 TCollection_AsciiString aFontPath( fontpath[i] );
265 TCollection_AsciiString aCutFontPath;
266 Standard_Integer location = -1 ;
267 location = aFontPath.Location( "/",1,aFontPath.Length() );
269 aCutFontPath.AssignCat( aFontPath.SubString(location, aFontPath.Length() ) );
270 find_path_with_font_dir( aCutFontPath, dirs );
273 XFreeFontPath(fontpath);
276 OSD_OpenMode aMode = OSD_ReadOnly;
277 OSD_Protection aProtect( OSD_R, OSD_R, OSD_R, OSD_R );
279 for( Standard_Integer j = 0 ; j < font_service_conf_size; j++ )
281 TCollection_AsciiString fileOfFontServiceName( font_service_conf[j] );
282 OSD_File aFile( fileOfFontServiceName );
285 aFile.Open( aMode, aProtect );
287 if( aFile.IsOpen() )//font service
289 Standard_Integer aNByte = 256;
290 Standard_Integer aNbyteRead;
291 TCollection_AsciiString aStr( aNByte );//read string with information
292 TCollection_AsciiString aStrCut( aNByte );//cut of string
293 TCollection_AsciiString endStr;//cutting string
295 Standard_Boolean read_dirs = Standard_False;
296 Standard_Integer location =- 1; //disposition of necessary literals
297 Standard_Integer begin =- 1; //first left entry in string
298 Standard_Integer end =- 1; //first right entry in string
299 while( !aFile.IsAtEnd() )
301 aFile.ReadLine( aStr, aNByte, aNbyteRead );//reading 1 lines(256 bytes)
302 location = aStr.Location( "catalogue = ", 1, aStr.Length() );
304 location = aStr.Location( "catalogue=", 1, aStr.Length() );
306 location = aStr.Location( "catalogue= ", 1, aStr.Length() );
308 location = aStr.Location( "catalogue = ", 1, aStr.Length() );
312 cout << " Font config find!!" << endl;
314 read_dirs = Standard_True;
319 begin = aStr.Location( "/", 1, aStr.Length() );//begin of path name
320 end = aStr.Location( ":", 1, aStr.Length() );//end of path name
322 end = aStr.Location( ",", 1, aStr.Length() );//also end of path name
324 if( begin > 0 && end > 0 )
326 if( ( end - begin ) > 0 )
327 endStr.AssignCat( aStr.SubString ( begin, end ) );//cutting necessary literals for string
328 dirs.Append( TCollection_HAsciiString ( endStr ) );
332 if( begin > 0 && end == -1 )
334 //if end of string don't have "," or ":"
335 //it is possible last sentence in block of word
336 endStr.AssignCat( aStr.SubString( begin, aStr.Length() - 1 ) );
337 dirs.Append( TCollection_HAsciiString( endStr ) );
347 if( dirs.Size() > 0 )
349 //if dirs list contains elements
350 OSD_OpenMode aModeRead = OSD_ReadOnly;
351 OSD_Protection aProtectRead( OSD_R, OSD_R , OSD_R , OSD_R );
353 TCollection_AsciiString fileFontsDir;
354 StringList::Iterator it( dirs );
355 for( ; it.More(); it.Next() )
357 fileFontsDir.AssignCat( it.Value().ToCString() );
358 fileFontsDir.AssignCat( "/fonts.dir" );//append file name in path way
360 OSD_File readFile( fileFontsDir );
361 readFile.Open( aModeRead, aProtectRead );
363 Standard_Integer aNbyteRead, aNByte = 256;
364 if( readFile.IsOpen ( ) )
366 TCollection_AsciiString aLine( aNByte );
367 Standard_Integer countOfString = 0 ;
368 while( ! readFile.IsAtEnd() )//return true if EOF
370 if( countOfString > 1 )
372 readFile.ReadLine( aLine , aNByte , aNbyteRead );
373 if( ( ( aLine.Search(".pfa") > 0 ) || ( aLine.Search(".PFA") > 0 ) ||
374 ( aLine.Search(".pfb") > 0 ) || ( aLine.Search(".PFB") > 0 ) ||
375 ( aLine.Search(".ttf") > 0 ) || ( aLine.Search(".TTF") > 0 ) ||
376 ( aLine.Search(".otf") > 0 ) || ( aLine.Search(".OTF") > 0 ) ||
377 ( aLine.Search(".ttc") > 0 ) || ( aLine.Search(".TTC") > 0 ) )
378 && ( aLine.Search( "iso8859-1\n" ) > 0 ) )
381 // In current implementation use fonts with ISO-8859-1 coding page.
382 // OCCT not give to manage coding page by means of programm interface.
383 // TODO: make high level interface for
384 // choosing necessary coding page.
385 TCollection_AsciiString aXLFD;
386 Standard_Integer leftXLFD = aLine.SearchFromEnd(" ");
387 Standard_Integer rightXLFD = aLine.Length();
388 if( leftXLFD && rightXLFD )
389 aXLFD.AssignCat(aLine.SubString( leftXLFD + 1, rightXLFD ) );
391 TCollection_AsciiString aPath;
392 TCollection_AsciiString aTemp( it.Value().ToCString() );
393 if ( aTemp.SearchFromEnd("/") == aTemp.Length() )
395 //this branch intend to SUN
396 aPath.AssignCat( aTemp.ToCString() );
397 aPath.AssignCat( aLine.Token( " ", 1 ) );
400 //this branch intend to Linux
401 aPath.AssignCat( aTemp.ToCString( ) );
402 aPath.AssignCat( "/" );
403 aPath.AssignCat( aLine.Token( " ", 1 ) );
405 MyListOfFonts.Append( new OSD_SystemFont( new TCollection_HAsciiString( aXLFD ),
406 new TCollection_HAsciiString( aPath ) ) );
411 readFile.ReadLine( aLine, aNByte, aNbyteRead );
416 fileFontsDir.Clear();
422 OSD_NListOfSystemFont OSD_FontMgr::GetAvalableFonts() const
424 return MyListOfFonts;