summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_FontMgr.cxx
blob: 15cfe63adf4964735a547d4ae30ca2b11fe89290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright:     OpenCASCADE

// File:          OSD_FontMgr.cxx
// Created:       20.01.2008
// Author:        Alexander A. BORODIN
// Updated:

#include <OSD_FontMgr.ixx>
#ifdef WNT
# include <windows.h>
# include <stdlib.h>
#endif //WNT

#include <NCollection_List.hxx>
#include <TCollection_HAsciiString.hxx>  
#include <Standard_Stream.hxx>


#ifndef WNT
#include <TCollection_AsciiString.hxx>

#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 <OSD_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"}
                                                                           };

#ifdef X11_FONT_PATH
# define stringify(s) #s
# define tostring(s) stringify(s)
# define X11_FONT_PATH_STR tostring(X11_FONT_PATH)
#else
# define X11_FONT_PATH_STR ""
#endif

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(OSD_FontMgr) OSD_FontMgr::GetInstance() {

  static Handle(OSD_FontMgr) _mgr;
  if ( _mgr.IsNull() )
    _mgr = new OSD_FontMgr();

  return _mgr;

}

OSD_FontMgr::OSD_FontMgr() {

  InitFontDataBase();

}


#ifndef WNT
static void
process_paths_with_font_dir(const Standard_Character* paths, StringList& dirs)
{
  TCollection_AsciiString fontpath(paths);

  while (fontpath.Length() > 0)
  {
    TCollection_AsciiString next_fontpath("");
    Standard_Integer position = fontpath.Search(";");
    if (position >= 0)
    {
      next_fontpath = fontpath.Split(position);
      fontpath.Trunc(fontpath.Length() - 1);
    }
#ifdef TRACE
    cout << "Font Path: " << fontpath << endl;
#endif
    if ( fontpath.Value(1) == '/' ) {
      find_path_with_font_dir( fontpath, dirs );
    }
    else
    {
      TCollection_AsciiString aFontPath( fontpath );
      TCollection_AsciiString aCutFontPath;
      Standard_Integer location = aFontPath.Location( "/",1,aFontPath.Length() );
      if( location > 0 )
        aCutFontPath.AssignCat( aFontPath.SubString(location, aFontPath.Length() ) );
      find_path_with_font_dir( aCutFontPath, dirs );
    }
    fontpath = next_fontpath;
  }
}
#endif

void OSD_FontMgr::InitFontDataBase() {

  MyListOfFonts.Clear();

#ifdef WNT
  //detect font directory

  Standard_Character* windir_var;
  Standard_Size req_size;   
  req_size = strlen( getenv("windir") );

  windir_var = new Standard_Character[req_size + 1];

  strcpy( windir_var, getenv("windir") );

  Standard_Character *font_dir = new Standard_Character[ req_size + strlen("\\Fonts\\") + 1 ]  ;  

  if( !strcpy( font_dir, windir_var ) )
  {
    delete [] windir_var;
    delete [] font_dir;
    return  ;   
  }   
  if( !strcat( font_dir, "\\Fonts\\" ) )
  {
    delete [] windir_var;
    delete [] font_dir;
    return ;  
  }
  Handle(TCollection_HAsciiString) HFontDir = new TCollection_HAsciiString(font_dir);

#ifdef TRACE
  cout << "System font directory: " << font_dir << "\n";
#endif

  //read registry
  HKEY fonts_hkey;
  if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
      TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"),
      0,
      KEY_READ,
      &fonts_hkey )
      != ERROR_SUCCESS ) 
  {
    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;
    OSD_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 = OSD_FA_BoldItalic;
      } else if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Bold") ) ) > 0 ) {
        aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
        aspect = OSD_FA_Bold;
      } else if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Italic") ) ) > 0 ) {
        aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
        aspect = OSD_FA_Italic;
      } else {
        aspect = OSD_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 );
      }
      Handle(TCollection_HAsciiString) HFontDir = new TCollection_HAsciiString(font_dir);

      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 OSD_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 OSD_FA_Bold:
          cout << "OSD_FA_Bold\n";
          break;
        case OSD_FA_BoldItalic:
          cout << "OSD_FA_BoldItalic\n";
          break;
        case OSD_FA_Italic:
          cout << "OSD_FA_Italic\n";
          break;
        default:
          cout << "OSD_FA_Regular\n";
          break;
        }
#endif
      }
  id++; 
  }
  //close registry
  RegCloseKey( fonts_hkey );
#endif //WNT

#ifndef WNT
  StringList dirs;

  Standard_Character* font_path_env = getenv("FONTPATH");
  if (font_path_env != NULL)
    process_paths_with_font_dir( font_path_env, dirs );
  else
    process_paths_with_font_dir( X11_FONT_PATH_STR, dirs );

  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++ )
  {
    TCollection_AsciiString fileOfFontServiceName( font_service_conf[j] );
    OSD_File aFile( fileOfFontServiceName );

    if( aFile.Exists() ) 
      aFile.Open( aMode, aProtect );
  
    if( aFile.IsOpen() )//font service
    {
      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

      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() )
      {
        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 )  
        {
#ifdef TRACE
          cout << " Font config find!!" << endl;
#endif
          read_dirs = Standard_True;
        }
  
        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 );

    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

      OSD_File readFile( fileFontsDir );  
      readFile.Open( aModeRead, aProtectRead );

      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 OSD_SystemFont( new TCollection_HAsciiString( aXLFD ),
                new TCollection_HAsciiString( aPath ) ) );
            }
            
          }
          else
            readFile.ReadLine( aLine, aNByte, aNbyteRead ); 
          countOfString++;
        }
        readFile.Close();
      } 
      fileFontsDir.Clear(); 
    }
  }
#endif
}

OSD_NListOfSystemFont OSD_FontMgr::GetAvalableFonts() const
{
  return MyListOfFonts;
}