0023098: Cppcheck warnings/errors in QA files
[occt.git] / src / OSD / OSD_FontMgr.cxx
CommitLineData
b311480e 1// Created on: 2008-01-20
2// Created by: Alexander A. BORODIN
3// Copyright (c) 2008-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
7fd59977 20
7fd59977 21// Updated:
22
23#include <OSD_FontMgr.ixx>
24#ifdef WNT
25# include <windows.h>
26# include <stdlib.h>
27#else //WNT
28# include <dirent.h>
29# include <X11/Xlib.h>
30#endif //WNT
31
a6535b1d 32#include <OSD_Environment.hxx>
7fd59977 33#include <NCollection_List.hxx>
34#include <TCollection_HAsciiString.hxx>
35#include <Standard_Stream.hxx>
36
37
38#ifndef WNT
39#include <TCollection_AsciiString.hxx>
40
41#include <NCollection_DefineList.hxx>
42#include <NCollection_List.hxx>
43
44#include <OSD_Path.hxx>
45#include <OSD_FileIterator.hxx>
46#include <OSD_DirectoryIterator.hxx>
47#include <OSD_File.hxx>
48#include <OSD_FileNode.hxx>
49#include <OSD_OpenMode.hxx>
50#include <OSD_Protection.hxx>
51#include <OSD_NListOfSystemFont.hxx>
52
53const Standard_Integer font_service_conf_size = 3;
54static Standard_Character font_service_conf[font_service_conf_size][64] = { {"/etc/X11/fs/config"},
55 {"/usr/X11R6/lib/X11/fs/config"},
56 {"/usr/X11/lib/X11/fs/config"}
57 };
58
59DEFINE_LIST( StringList, NCollection_List, TCollection_HAsciiString );
60
61void find_path_with_font_dir( const TCollection_AsciiString& dir,StringList& dirs )
62{
63 if( !dir.IsEmpty() )
64 {
65 TCollection_AsciiString PathName( dir );
66
67 Standard_Integer rem = PathName.Length();
68
69 if ( PathName.SearchFromEnd("/") == rem )
70 PathName.Remove( rem, 1 );
71
72 Standard_Boolean need_to_append = Standard_True;
73
74 StringList::Iterator it( dirs );
75 for( ; it.More(); it.Next() )
76 {
77 if ( PathName.IsEqual(it.Value().ToCString()) ) {
78 need_to_append = Standard_False;
79 break;
80 }
81 }
82 if ( need_to_append )
83 dirs.Append( PathName );
84
85 OSD_DirectoryIterator osd_dir(PathName,"*");
86 while(osd_dir.More())
87 {
88 OSD_Path path_file;
89 osd_dir.Values().Path( path_file );
90 if( path_file.Name().Length() < 1 )
91 {
92 osd_dir.Next();
93 continue;
94 }
95
96 TCollection_AsciiString full_path_name = PathName + "/" + path_file.Name();
97 rem = full_path_name.Length();
98 if ( full_path_name.SearchFromEnd("/") == rem )
99 full_path_name.Remove( rem, 1 );
100 find_path_with_font_dir( full_path_name, dirs );
101 osd_dir.Next();
102 }
103 }
104}
105
106#endif //WNT
107
108
109Handle(OSD_FontMgr) OSD_FontMgr::GetInstance() {
110
111 static Handle(OSD_FontMgr) _mgr;
112 if ( _mgr.IsNull() )
113 _mgr = new OSD_FontMgr();
114
115 return _mgr;
116
117}
118
119OSD_FontMgr::OSD_FontMgr() {
120
121 InitFontDataBase();
122
123}
124
7fd59977 125void OSD_FontMgr::InitFontDataBase() {
126
127 MyListOfFonts.Clear();
128
129#ifdef WNT
130 //detect font directory
131
a6535b1d
E
132 OSD_Environment env("windir");
133 TCollection_AsciiString windir_str = env.Value();
134 if ( windir_str.IsEmpty() )
135 {
136 return;
137 }
138 Handle(TCollection_HAsciiString) HFontDir = new TCollection_HAsciiString( windir_str );
139 HFontDir->AssignCat( "\\Fonts\\" );
140 #ifdef TRACE
141 cout << "System font directory: " << HFontDir->ToCString() << "\n";
142 #endif TRACE
7fd59977 143
144 //read registry
145 HKEY fonts_hkey;
146 if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
147 TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"),
148 0,
149 KEY_READ,
150 &fonts_hkey )
151 != ERROR_SUCCESS )
152 {
153 return;
154 }
155 Standard_Integer id = 0;
156 Standard_Character buf_name[100];
157 Standard_Byte buf_data[100];
158 DWORD size_name = 100,
159 size_data = 100;
160
161 while ( true )
162 {
163 //detect file name
164 DWORD type;
165 size_name = 100,
166 size_data = 100;
167 OSD_FontAspect aspect;
168 if( RegEnumValue( fonts_hkey,
169 id,
170 buf_name,
171 &size_name,
172 NULL,
173 &type,
174 buf_data,
175 &size_data) == ERROR_NO_MORE_ITEMS ) {
176 break;
177 }
178 Handle(TCollection_HAsciiString) fname =
179 new TCollection_HAsciiString(buf_name);
180 fname->RightAdjust();
181 fname->LeftAdjust();
182 //remove construction like (TrueType....
183 Standard_Integer anIndexTT = fname->SearchFromEnd( new TCollection_HAsciiString( " (" ) );
184 Standard_Boolean aTruncate = Standard_False;
185 if ( anIndexTT > 1 )
186 fname->Trunc( anIndexTT );
187 Standard_Integer anIndex = 0;
188 fname->RightAdjust();
189 if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Bold Italic") ) ) > 0 ) {
190 aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
191 aspect = OSD_FA_BoldItalic;
192 } else if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Bold") ) ) > 0 ) {
193 aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
194 aspect = OSD_FA_Bold;
195 } else if ( ( anIndex = fname->SearchFromEnd( new TCollection_HAsciiString("Italic") ) ) > 0 ) {
196 aTruncate = ( anIndex > 1 ) && ( fname->Value(anIndex - 1 ) == ' ' );
197 aspect = OSD_FA_Italic;
198 } else {
199 aspect = OSD_FA_Regular;
200 }
201 if( aTruncate )
202 fname->Trunc( anIndex - 1 );
203 fname->RightAdjust();
204 Handle(TCollection_HAsciiString) file_path =
205 new TCollection_HAsciiString( (Standard_Character*)buf_data );
206 if ( strchr( (Standard_Character*)buf_data, '\\' ) == NULL ) {
207 file_path->Insert( 1, HFontDir );
208 }
7fd59977 209
210 if( ( ( file_path->Search(".ttf") > 0 ) || ( file_path->Search(".TTF") > 0 ) ||
211 ( file_path->Search(".otf") > 0 ) || ( file_path->Search(".OTF") > 0 ) ||
212 ( file_path->Search(".ttc") > 0 ) || ( file_path->Search(".TTC") > 0 ) ) ){
213 MyListOfFonts.Append( new OSD_SystemFont( fname, aspect, file_path ) );
214#ifdef TRACE
215 cout << "Adding font...\n"
216 << " font name: " << fname->ToCString() << "\n"
217 << " font file: " << file_path->ToCString() << "\n"
218 << " font aspect: ";
219 switch( aspect ) {
220 case OSD_FA_Bold:
221 cout << "OSD_FA_Bold\n";
222 break;
223 case OSD_FA_BoldItalic:
224 cout << "OSD_FA_BoldItalic\n";
225 break;
226 case OSD_FA_Italic:
227 cout << "OSD_FA_Italic\n";
228 break;
229 default:
230 cout << "OSD_FA_Regular\n";
231 break;
232 }
233#endif
234 }
235 id++;
236 }
237 //close registry
238 RegCloseKey( fonts_hkey );
239#endif //WNT
240
241#ifndef WNT
242 StringList dirs;
243 Handle(TCollection_HAsciiString) str = new TCollection_HAsciiString;
244 Display * disp = XOpenDisplay("localhost:0.0");
245
c3d89486 246 if (!disp)
7fd59977 247 {
c3d89486 248 // let the X server find the available connection
249 disp = XOpenDisplay(":0.0");
250 if (!disp)
251 {
252 cout << "Display is NULL!" << endl;
253 return ;
254 }
7fd59977 255 }
256
257 Standard_Integer npaths = 0;
258
259 Standard_Character** fontpath = XGetFontPath(disp, &npaths);
260#ifdef TRACE
261 cout << "NPATHS = " << npaths << endl ;
262#endif
263 for (Standard_Integer i = 0; i < npaths; i++ )
264 {
265#ifdef TRACE
266 cout << "Font Path: " << fontpath[i] << endl;
267#endif
268 if ( fontpath[i][0] == '/' ) {
269 TCollection_AsciiString aFontPath( fontpath[i] );
270 find_path_with_font_dir( aFontPath, dirs );
271 }
272 else
273 {
274 TCollection_AsciiString aFontPath( fontpath[i] );
275 TCollection_AsciiString aCutFontPath;
276 Standard_Integer location = -1 ;
277 location = aFontPath.Location( "/",1,aFontPath.Length() );
278 if( location > 0 )
279 aCutFontPath.AssignCat( aFontPath.SubString(location, aFontPath.Length() ) );
280 find_path_with_font_dir( aCutFontPath, dirs );
281 }
282 }
283 XFreeFontPath(fontpath);
284
285
286 OSD_OpenMode aMode = OSD_ReadOnly;
287 OSD_Protection aProtect( OSD_R, OSD_R, OSD_R, OSD_R );
288
289 for( Standard_Integer j = 0 ; j < font_service_conf_size; j++ )
290 {
291 TCollection_AsciiString fileOfFontServiceName( font_service_conf[j] );
292 OSD_File aFile( fileOfFontServiceName );
293
294 if( aFile.Exists() )
295 aFile.Open( aMode, aProtect );
296
297 if( aFile.IsOpen() )//font service
298 {
299 Standard_Integer aNByte = 256;
300 Standard_Integer aNbyteRead;
301 TCollection_AsciiString aStr( aNByte );//read string with information
302 TCollection_AsciiString aStrCut( aNByte );//cut of string
303 TCollection_AsciiString endStr;//cutting string
304
305 Standard_Boolean read_dirs = Standard_False;
306 Standard_Integer location =- 1; //disposition of necessary literals
307 Standard_Integer begin =- 1; //first left entry in string
308 Standard_Integer end =- 1; //first right entry in string
309 while( !aFile.IsAtEnd() )
310 {
311 aFile.ReadLine( aStr, aNByte, aNbyteRead );//reading 1 lines(256 bytes)
312 location = aStr.Location( "catalogue = ", 1, aStr.Length() );
313 if(location == 0)
314 location = aStr.Location( "catalogue=", 1, aStr.Length() );
315 if(location == 0)
316 location = aStr.Location( "catalogue= ", 1, aStr.Length() );
317 if(location == 0)
318 location = aStr.Location( "catalogue = ", 1, aStr.Length() );
319 if( location > 0 )
320 {
321#ifdef TRACE
322 cout << " Font config find!!" << endl;
323#endif
324 read_dirs = Standard_True;
325 }
326
327 if( read_dirs )
328 {
329 begin = aStr.Location( "/", 1, aStr.Length() );//begin of path name
330 end = aStr.Location( ":", 1, aStr.Length() );//end of path name
331 if( end < 1 )
332 end = aStr.Location( ",", 1, aStr.Length() );//also end of path name
333 end -= 1;
334 if( begin > 0 && end > 0 )
335 {
336 if( ( end - begin ) > 0 )
337 endStr.AssignCat( aStr.SubString ( begin, end ) );//cutting necessary literals for string
338 dirs.Append( TCollection_HAsciiString ( endStr ) );
339 endStr.Clear();
340 }
341 else
342 if( begin > 0 && end == -1 )
343 {
344 //if end of string don't have "," or ":"
345 //it is possible last sentence in block of word
346 endStr.AssignCat( aStr.SubString( begin, aStr.Length() - 1 ) );
347 dirs.Append( TCollection_HAsciiString( endStr ) );
348 endStr.Clear();
349 }
350 }
351
352 }
353 aFile.Close();
354 }
355 }
356
357 if( dirs.Size() > 0 )
358 {
359 //if dirs list contains elements
360 OSD_OpenMode aModeRead = OSD_ReadOnly;
361 OSD_Protection aProtectRead( OSD_R, OSD_R , OSD_R , OSD_R );
362
363 TCollection_AsciiString fileFontsDir;
364 StringList::Iterator it( dirs );
365 for( ; it.More(); it.Next() )
366 {
367 fileFontsDir.AssignCat( it.Value().ToCString() );
368 fileFontsDir.AssignCat( "/fonts.dir" );//append file name in path way
369
370 OSD_File readFile( fileFontsDir );
371 readFile.Open( aModeRead, aProtectRead );
372
373 Standard_Integer aNbyteRead, aNByte = 256;
374 if( readFile.IsOpen ( ) )
375 {
376 TCollection_AsciiString aLine( aNByte );
377 Standard_Integer countOfString = 0 ;
378 while( ! readFile.IsAtEnd() )//return true if EOF
379 {
380 if( countOfString > 1 )
381 {
382 readFile.ReadLine( aLine , aNByte , aNbyteRead );
383 if( ( ( aLine.Search(".pfa") > 0 ) || ( aLine.Search(".PFA") > 0 ) ||
384 ( aLine.Search(".pfb") > 0 ) || ( aLine.Search(".PFB") > 0 ) ||
385 ( aLine.Search(".ttf") > 0 ) || ( aLine.Search(".TTF") > 0 ) ||
386 ( aLine.Search(".otf") > 0 ) || ( aLine.Search(".OTF") > 0 ) ||
387 ( aLine.Search(".ttc") > 0 ) || ( aLine.Search(".TTC") > 0 ) )
388 && ( aLine.Search( "iso8859-1\n" ) > 0 ) )
389 {
390
391 // In current implementation use fonts with ISO-8859-1 coding page.
392 // OCCT not give to manage coding page by means of programm interface.
393 // TODO: make high level interface for
394 // choosing necessary coding page.
395 TCollection_AsciiString aXLFD;
396 Standard_Integer leftXLFD = aLine.SearchFromEnd(" ");
397 Standard_Integer rightXLFD = aLine.Length();
398 if( leftXLFD && rightXLFD )
399 aXLFD.AssignCat(aLine.SubString( leftXLFD + 1, rightXLFD ) );
400
401 TCollection_AsciiString aPath;
402 TCollection_AsciiString aTemp( it.Value().ToCString() );
403 if ( aTemp.SearchFromEnd("/") == aTemp.Length() )
404 {
405 //this branch intend to SUN
406 aPath.AssignCat( aTemp.ToCString() );
407 aPath.AssignCat( aLine.Token( " ", 1 ) );
408 }
409 else {
410 //this branch intend to Linux
411 aPath.AssignCat( aTemp.ToCString( ) );
412 aPath.AssignCat( "/" );
413 aPath.AssignCat( aLine.Token( " ", 1 ) );
414 }
415 MyListOfFonts.Append( new OSD_SystemFont( new TCollection_HAsciiString( aXLFD ),
416 new TCollection_HAsciiString( aPath ) ) );
417 }
418
419 }
420 else
421 readFile.ReadLine( aLine, aNByte, aNbyteRead );
422 countOfString++;
423 }
424 readFile.Close();
425 }
426 fileFontsDir.Clear();
427 }
428 }
429#endif
430}
431
432OSD_NListOfSystemFont OSD_FontMgr::GetAvalableFonts() const
433{
434 return MyListOfFonts;
435}
436
437