Update ooo320-m1
[ooovba.git] / vcl / unx / source / fontmanager / helper.cxx
blob919ce636a9a77e6965223d31eb63783e5bd98906
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: helper.cxx,v $
10 * $Revision: 1.35 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 #include <cstring>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <limits.h>
39 #include "vcl/helper.hxx"
40 #include "vcl/ppdparser.hxx"
41 #include "tools/string.hxx"
42 #include "tools/urlobj.hxx"
43 #include "osl/file.hxx"
44 #include "osl/process.h"
45 #include "rtl/bootstrap.hxx"
47 using namespace rtl;
49 namespace psp {
51 OUString getOfficePath( enum whichOfficePath ePath )
53 static OUString aNetPath;
54 static OUString aUserPath;
55 static OUString aConfigPath;
56 static OUString aEmpty;
57 static bool bOnce = false;
59 if( ! bOnce )
61 bOnce = true;
62 OUString aIni;
63 Bootstrap::get( OUString( RTL_CONSTASCII_USTRINGPARAM( "BRAND_BASE_DIR" ) ), aIni );
64 aIni += OUString( RTL_CONSTASCII_USTRINGPARAM( "/program/" SAL_CONFIGFILE( "bootstrap" ) ) );
65 Bootstrap aBootstrap( aIni );
66 aBootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "CustomDataUrl" ) ), aConfigPath );
67 aBootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "BaseInstallation" ) ), aNetPath );
68 aBootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "UserInstallation" ) ), aUserPath );
69 OUString aUPath = aUserPath;
71 if( ! aConfigPath.compareToAscii( "file://", 7 ) )
73 OUString aSysPath;
74 if( osl_getSystemPathFromFileURL( aConfigPath.pData, &aSysPath.pData ) == osl_File_E_None )
75 aConfigPath = aSysPath;
77 if( ! aNetPath.compareToAscii( "file://", 7 ) )
79 OUString aSysPath;
80 if( osl_getSystemPathFromFileURL( aNetPath.pData, &aSysPath.pData ) == osl_File_E_None )
81 aNetPath = aSysPath;
83 if( ! aUserPath.compareToAscii( "file://", 7 ) )
85 OUString aSysPath;
86 if( osl_getSystemPathFromFileURL( aUserPath.pData, &aSysPath.pData ) == osl_File_E_None )
87 aUserPath = aSysPath;
89 // ensure user path exists
90 aUPath += OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/psprint" ) );
91 #if OSL_DEBUG_LEVEL > 1
92 oslFileError eErr =
93 #endif
94 osl_createDirectoryPath( aUPath.pData, NULL, NULL );
95 #if OSL_DEBUG_LEVEL > 1
96 fprintf( stderr, "try to create \"%s\" = %d\n", OUStringToOString( aUPath, RTL_TEXTENCODING_UTF8 ).getStr(), eErr );
97 #endif
100 switch( ePath )
102 case ConfigPath: return aConfigPath;
103 case NetPath: return aNetPath;
104 case UserPath: return aUserPath;
106 return aEmpty;
109 static OString getEnvironmentPath( const char* pKey )
111 OString aPath;
113 const char* pValue = getenv( pKey );
114 if( pValue && *pValue )
116 aPath = OString( pValue );
118 return aPath;
121 } // namespace psp
123 void psp::getPrinterPathList( std::list< OUString >& rPathList, const char* pSubDir )
125 rPathList.clear();
126 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
128 OUStringBuffer aPathBuffer( 256 );
130 // append net path
131 aPathBuffer.append( getOfficePath( psp::NetPath ) );
132 if( aPathBuffer.getLength() )
134 aPathBuffer.appendAscii( "/share/psprint" );
135 if( pSubDir )
137 aPathBuffer.append( sal_Unicode('/') );
138 aPathBuffer.appendAscii( pSubDir );
140 rPathList.push_back( aPathBuffer.makeStringAndClear() );
142 // append user path
143 aPathBuffer.append( getOfficePath( psp::UserPath ) );
144 if( aPathBuffer.getLength() )
146 aPathBuffer.appendAscii( "/user/psprint" );
147 if( pSubDir )
149 aPathBuffer.append( sal_Unicode('/') );
150 aPathBuffer.appendAscii( pSubDir );
152 rPathList.push_back( aPathBuffer.makeStringAndClear() );
155 OString aPath( getEnvironmentPath("SAL_PSPRINT") );
156 sal_Int32 nIndex = 0;
159 OString aDir( aPath.getToken( 0, ':', nIndex ) );
160 if( ! aDir.getLength() )
161 continue;
163 if( pSubDir )
165 aDir += "/";
166 aDir += pSubDir;
168 struct stat aStat;
169 if( stat( aDir.getStr(), &aStat ) || ! S_ISDIR( aStat.st_mode ) )
170 continue;
172 rPathList.push_back( OStringToOUString( aDir, aEncoding ) );
173 } while( nIndex != -1 );
175 #ifdef SYSTEM_PPD_DIR
176 if( pSubDir && rtl_str_compare( pSubDir, PRINTER_PPDDIR ) == 0 )
178 rPathList.push_back( rtl::OStringToOUString( rtl::OString( SYSTEM_PPD_DIR ), RTL_TEXTENCODING_UTF8 ) );
180 #endif
182 if( rPathList.empty() )
184 // last resort: next to program file (mainly for setup)
185 OUString aExe;
186 if( osl_getExecutableFile( &aExe.pData ) == osl_Process_E_None )
188 INetURLObject aDir( aExe );
189 aDir.removeSegment();
190 aExe = aDir.GetMainURL( INetURLObject::NO_DECODE );
191 OUString aSysPath;
192 if( osl_getSystemPathFromFileURL( aExe.pData, &aSysPath.pData ) == osl_File_E_None )
194 rPathList.push_back( aSysPath );
200 OUString psp::getFontPath()
202 static OUString aPath;
204 if( ! aPath.getLength() )
206 OUStringBuffer aPathBuffer( 512 );
208 OUString aConfigPath( getOfficePath( psp::ConfigPath ) );
209 OUString aNetPath( getOfficePath( psp::NetPath ) );
210 OUString aUserPath( getOfficePath( psp::UserPath ) );
211 if( aConfigPath.getLength() )
213 // #i53530# Path from CustomDataUrl will completely
214 // replace net and user paths if the path exists
215 aPathBuffer.append(aConfigPath);
216 aPathBuffer.appendAscii("/share/fonts");
217 // check existance of config path
218 struct stat aStat;
219 if( 0 != stat( OUStringToOString( aPathBuffer.makeStringAndClear(), osl_getThreadTextEncoding() ).getStr(), &aStat )
220 || ! S_ISDIR( aStat.st_mode ) )
221 aConfigPath = OUString();
222 else
224 aPathBuffer.append(aConfigPath);
225 aPathBuffer.appendAscii("/share/fonts");
228 if( aConfigPath.getLength() == 0 )
230 if( aNetPath.getLength() )
232 aPathBuffer.append( aNetPath );
233 aPathBuffer.appendAscii( "/share/fonts/truetype;");
234 aPathBuffer.append( aNetPath );
235 aPathBuffer.appendAscii( "/share/fonts/type1;" );
237 if( aUserPath.getLength() )
239 aPathBuffer.append( aUserPath );
240 aPathBuffer.appendAscii( "/user/fonts" );
243 OString aEnvPath( getEnvironmentPath( "SAL_FONTPATH_PRIVATE" ) );
244 if( aEnvPath.getLength() )
246 aPathBuffer.append( sal_Unicode(';') );
247 aPathBuffer.append( OStringToOUString( aEnvPath, osl_getThreadTextEncoding() ) );
250 aPath = aPathBuffer.makeStringAndClear();
251 #if OSL_DEBUG_LEVEL > 1
252 fprintf( stderr, "initializing font path to \"%s\"\n", OUStringToOString( aPath, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
253 #endif
255 return aPath;
258 bool psp::convertPfbToPfa( ::osl::File& rInFile, ::osl::File& rOutFile )
260 static unsigned char hexDigits[] =
262 '0', '1', '2', '3', '4', '5', '6', '7',
263 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
266 bool bSuccess = true;
267 bool bEof = false;
268 unsigned char buffer[256];
269 sal_uInt64 nRead;
270 sal_uInt64 nOrgPos = 0;
271 rInFile.getPos( nOrgPos );
273 while( bSuccess && ! bEof )
275 // read leading bytes
276 bEof = ! rInFile.read( buffer, 6, nRead ) && nRead == 6 ? false : true;
277 unsigned int nType = buffer[ 1 ];
278 unsigned int nBytesToRead = buffer[2] | buffer[3] << 8 | buffer[4] << 16 | buffer[5] << 24;
279 if( buffer[0] != 0x80 ) // test for pfb m_agic number
281 // this migt be a pfa font already
282 sal_uInt64 nWrite = 0;
283 if( ! rInFile.read( buffer+6, 9, nRead ) && nRead == 9 &&
284 ( ! std::strncmp( (char*)buffer, "%!FontType1-", 12 ) ||
285 ! std::strncmp( (char*)buffer, "%!PS-AdobeFont-", 15 ) ) )
287 if( rOutFile.write( buffer, 15, nWrite ) || nWrite != 15 )
288 bSuccess = false;
289 while( bSuccess &&
290 ! rInFile.read( buffer, sizeof( buffer ), nRead ) &&
291 nRead != 0 )
293 if( rOutFile.write( buffer, nRead, nWrite ) ||
294 nWrite != nRead )
295 bSuccess = false;
297 bEof = true;
299 else
300 bSuccess = false;
302 else if( nType == 1 || nType == 2 )
304 unsigned char* pBuffer = new unsigned char[ nBytesToRead+1 ];
306 if( ! rInFile.read( pBuffer, nBytesToRead, nRead ) && nRead == nBytesToRead )
308 if( nType == 1 )
310 // ascii data, convert dos lineends( \r\n ) and
311 // m_ac lineends( \r ) to \n
312 unsigned char * pWriteBuffer = new unsigned char[ nBytesToRead ];
313 unsigned int nBytesToWrite = 0;
314 for( unsigned int i = 0; i < nBytesToRead; i++ )
316 if( pBuffer[i] != '\r' )
317 pWriteBuffer[ nBytesToWrite++ ] = pBuffer[i];
318 else if( pBuffer[ i+1 ] == '\n' )
320 i++;
321 pWriteBuffer[ nBytesToWrite++ ] = '\n';
323 else
324 pWriteBuffer[ nBytesToWrite++ ] = '\n';
326 if( rOutFile.write( pWriteBuffer, nBytesToWrite, nRead ) || nRead != nBytesToWrite )
327 bSuccess = false;
329 delete [] pWriteBuffer;
331 else
333 // binary data
334 unsigned int nBuffer = 0;
335 for( unsigned int i = 0; i < nBytesToRead && bSuccess; i++ )
337 buffer[ nBuffer++ ] = hexDigits[ pBuffer[ i ] >> 4 ];
338 buffer[ nBuffer++ ] = hexDigits[ pBuffer[ i ] & 15 ];
339 if( nBuffer >= 80 )
341 buffer[ nBuffer++ ] = '\n';
342 if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
343 bSuccess = false;
344 nBuffer = 0;
347 if( nBuffer > 0 && bSuccess )
349 buffer[ nBuffer++ ] = '\n';
350 if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
351 bSuccess = false;
355 else
356 bSuccess = false;
358 delete [] pBuffer;
360 else if( nType == 3 )
361 bEof = true;
362 else
363 bSuccess = false;
366 return bSuccess;
369 void psp::normPath( OString& rPath )
371 char buf[PATH_MAX];
373 ByteString aPath( rPath );
375 // double slashes and slash at end are probably
376 // removed by realpath anyway, but since this runs
377 // on many different platforms let's play it safe
378 while( aPath.SearchAndReplace( "//", "/" ) != STRING_NOTFOUND )
380 if( aPath.Len() > 0 && aPath.GetChar( aPath.Len()-1 ) == '/' )
381 aPath.Erase( aPath.Len()-1 );
383 if( ( aPath.Search( "./" ) != STRING_NOTFOUND ||
384 aPath.Search( "~" ) != STRING_NOTFOUND )
385 && realpath( aPath.GetBuffer(), buf ) )
387 rPath = buf;
389 else
391 rPath = aPath;
395 void psp::splitPath( OString& rPath, OString& rDir, OString& rBase )
397 normPath( rPath );
398 sal_Int32 nIndex = rPath.lastIndexOf( '/' );
399 if( nIndex > 0 )
400 rDir = rPath.copy( 0, nIndex );
401 else if( nIndex == 0 ) // root dir
402 rDir = rPath.copy( 0, 1 );
403 if( rPath.getLength() > nIndex+1 )
404 rBase = rPath.copy( nIndex+1 );