1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
26 #include "vcl/helper.hxx"
27 #include "vcl/ppdparser.hxx"
28 #include "tools/string.hxx"
29 #include "tools/urlobj.hxx"
30 #include "osl/file.hxx"
31 #include "osl/process.h"
32 #include "rtl/bootstrap.hxx"
34 using ::rtl::Bootstrap
;
38 OUString
getOfficePath( enum whichOfficePath ePath
)
40 static OUString aInstallationRootPath
;
41 static OUString aUserPath
;
42 static OUString aConfigPath
;
43 static OUString aEmpty
;
44 static bool bOnce
= false;
50 Bootstrap::get( "BRAND_BASE_DIR", aInstallationRootPath
);
51 aIni
= aInstallationRootPath
+ "/program/" + SAL_CONFIGFILE( "bootstrap" );
52 Bootstrap
aBootstrap( aIni
);
53 aBootstrap
.getFrom( "CustomDataUrl", aConfigPath
);
54 aBootstrap
.getFrom( "UserInstallation", aUserPath
);
55 OUString aUPath
= aUserPath
;
57 if( aConfigPath
.startsWith( "file://" ) )
60 if( osl_getSystemPathFromFileURL( aConfigPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
61 aConfigPath
= aSysPath
;
63 if( aInstallationRootPath
.startsWith( "file://" ) )
66 if( osl_getSystemPathFromFileURL( aInstallationRootPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
67 aInstallationRootPath
= aSysPath
;
69 if( aUserPath
.startsWith( "file://" ) )
72 if( osl_getSystemPathFromFileURL( aUserPath
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
75 // ensure user path exists
76 aUPath
+= "/user/psprint";
77 #if OSL_DEBUG_LEVEL > 1
80 osl_createDirectoryPath( aUPath
.pData
, NULL
, NULL
);
81 #if OSL_DEBUG_LEVEL > 1
82 fprintf( stderr
, "try to create \"%s\" = %d\n", OUStringToOString( aUPath
, RTL_TEXTENCODING_UTF8
).getStr(), eErr
);
88 case ConfigPath
: return aConfigPath
;
89 case InstallationRootPath
: return aInstallationRootPath
;
90 case UserPath
: return aUserPath
;
95 static OString
getEnvironmentPath( const char* pKey
)
99 const char* pValue
= getenv( pKey
);
100 if( pValue
&& *pValue
)
102 aPath
= OString( pValue
);
109 void psp::getPrinterPathList( std::list
< OUString
>& rPathList
, const char* pSubDir
)
112 rtl_TextEncoding aEncoding
= osl_getThreadTextEncoding();
114 OUStringBuffer
aPathBuffer( 256 );
117 aPathBuffer
.append( getOfficePath( psp::InstallationRootPath
) );
118 if( aPathBuffer
.getLength() )
120 aPathBuffer
.appendAscii( "/share/psprint" );
123 aPathBuffer
.append( sal_Unicode('/') );
124 aPathBuffer
.appendAscii( pSubDir
);
126 rPathList
.push_back( aPathBuffer
.makeStringAndClear() );
129 aPathBuffer
.append( getOfficePath( psp::UserPath
) );
130 if( aPathBuffer
.getLength() )
132 aPathBuffer
.appendAscii( "/user/psprint" );
135 aPathBuffer
.append( sal_Unicode('/') );
136 aPathBuffer
.appendAscii( pSubDir
);
138 rPathList
.push_back( aPathBuffer
.makeStringAndClear() );
141 OString
aPath( getEnvironmentPath("SAL_PSPRINT") );
142 sal_Int32 nIndex
= 0;
145 OString
aDir( aPath
.getToken( 0, ':', nIndex
) );
155 if( stat( aDir
.getStr(), &aStat
) || ! S_ISDIR( aStat
.st_mode
) )
158 rPathList
.push_back( OStringToOUString( aDir
, aEncoding
) );
159 } while( nIndex
!= -1 );
161 #ifdef SYSTEM_PPD_DIR
162 if( pSubDir
&& rtl_str_compare( pSubDir
, PRINTER_PPDDIR
) == 0 )
164 rPathList
.push_back( OStringToOUString( OString( SYSTEM_PPD_DIR
), RTL_TEXTENCODING_UTF8
) );
168 if( rPathList
.empty() )
170 // last resort: next to program file (mainly for setup)
172 if( osl_getExecutableFile( &aExe
.pData
) == osl_Process_E_None
)
174 INetURLObject
aDir( aExe
);
175 aDir
.removeSegment();
176 aExe
= aDir
.GetMainURL( INetURLObject::NO_DECODE
);
178 if( osl_getSystemPathFromFileURL( aExe
.pData
, &aSysPath
.pData
) == osl_File_E_None
)
180 rPathList
.push_back( aSysPath
);
186 OUString
psp::getFontPath()
188 static OUString aPath
;
192 OUStringBuffer
aPathBuffer( 512 );
194 OUString
aConfigPath( getOfficePath( psp::ConfigPath
) );
195 OUString
aInstallationRootPath( getOfficePath( psp::InstallationRootPath
) );
196 OUString
aUserPath( getOfficePath( psp::UserPath
) );
197 if( !aConfigPath
.isEmpty() )
199 // #i53530# Path from CustomDataUrl will completely
200 // replace net and user paths if the path exists
201 aPathBuffer
.append(aConfigPath
);
202 aPathBuffer
.appendAscii("/share/fonts");
203 // check existance of config path
205 if( 0 != stat( OUStringToOString( aPathBuffer
.makeStringAndClear(), osl_getThreadTextEncoding() ).getStr(), &aStat
)
206 || ! S_ISDIR( aStat
.st_mode
) )
207 aConfigPath
= OUString();
210 aPathBuffer
.append(aConfigPath
);
211 aPathBuffer
.appendAscii("/share/fonts");
214 if( aConfigPath
.isEmpty() )
216 if( !aInstallationRootPath
.isEmpty() )
218 aPathBuffer
.append( aInstallationRootPath
);
219 aPathBuffer
.appendAscii( "/share/fonts/truetype;");
220 aPathBuffer
.append( aInstallationRootPath
);
221 aPathBuffer
.appendAscii( "/share/fonts/type1;" );
223 if( !aUserPath
.isEmpty() )
225 aPathBuffer
.append( aUserPath
);
226 aPathBuffer
.appendAscii( "/user/fonts" );
230 aPath
= aPathBuffer
.makeStringAndClear();
231 #if OSL_DEBUG_LEVEL > 1
232 fprintf( stderr
, "initializing font path to \"%s\"\n", OUStringToOString( aPath
, RTL_TEXTENCODING_ISO_8859_1
).getStr() );
238 bool psp::convertPfbToPfa( ::osl::File
& rInFile
, ::osl::File
& rOutFile
)
240 static const unsigned char hexDigits
[] =
242 '0', '1', '2', '3', '4', '5', '6', '7',
243 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
246 bool bSuccess
= true;
248 unsigned char buffer
[256];
250 sal_uInt64 nOrgPos
= 0;
251 rInFile
.getPos( nOrgPos
);
253 while( bSuccess
&& ! bEof
)
255 // read leading bytes
256 bEof
= ((0 != rInFile
.read( buffer
, 6, nRead
)) || (nRead
!= 6));
259 unsigned int nType
= buffer
[ 1 ];
260 unsigned int nBytesToRead
= buffer
[2] | buffer
[3] << 8 | buffer
[4] << 16 | buffer
[5] << 24;
261 if( buffer
[0] != 0x80 ) // test for pfb magic number
263 // this migt be a pfa font already
264 if( ! rInFile
.read( buffer
+6, 9, nRead
) && nRead
== 9 &&
265 ( ! std::strncmp( (char*)buffer
, "%!FontType1-", 12 ) ||
266 ! std::strncmp( (char*)buffer
, "%!PS-AdobeFont-", 15 ) ) )
268 sal_uInt64 nWrite
= 0;
269 if( rOutFile
.write( buffer
, 15, nWrite
) || nWrite
!= 15 )
272 ! rInFile
.read( buffer
, sizeof( buffer
), nRead
) &&
275 if( rOutFile
.write( buffer
, nRead
, nWrite
) ||
284 else if( nType
== 1 || nType
== 2 )
286 unsigned char* pBuffer
= new unsigned char[ nBytesToRead
+1 ];
288 if( ! rInFile
.read( pBuffer
, nBytesToRead
, nRead
) && nRead
== nBytesToRead
)
292 // ascii data, convert dos lineends( \r\n ) and
293 // m_ac lineends( \r ) to \n
294 unsigned char * pWriteBuffer
= new unsigned char[ nBytesToRead
];
295 unsigned int nBytesToWrite
= 0;
296 for( unsigned int i
= 0; i
< nBytesToRead
; i
++ )
298 if( pBuffer
[i
] != '\r' )
299 pWriteBuffer
[ nBytesToWrite
++ ] = pBuffer
[i
];
300 else if( pBuffer
[ i
+1 ] == '\n' )
303 pWriteBuffer
[ nBytesToWrite
++ ] = '\n';
306 pWriteBuffer
[ nBytesToWrite
++ ] = '\n';
308 if( rOutFile
.write( pWriteBuffer
, nBytesToWrite
, nRead
) || nRead
!= nBytesToWrite
)
311 delete [] pWriteBuffer
;
316 unsigned int nBuffer
= 0;
317 for( unsigned int i
= 0; i
< nBytesToRead
&& bSuccess
; i
++ )
319 buffer
[ nBuffer
++ ] = hexDigits
[ pBuffer
[ i
] >> 4 ];
320 buffer
[ nBuffer
++ ] = hexDigits
[ pBuffer
[ i
] & 15 ];
323 buffer
[ nBuffer
++ ] = '\n';
324 if( rOutFile
.write( buffer
, nBuffer
, nRead
) || nRead
!= nBuffer
)
329 if( nBuffer
> 0 && bSuccess
)
331 buffer
[ nBuffer
++ ] = '\n';
332 if( rOutFile
.write( buffer
, nBuffer
, nRead
) || nRead
!= nBuffer
)
342 else if( nType
== 3 )
351 void psp::normPath( OString
& rPath
)
355 // double slashes and slash at end are probably
356 // removed by realpath anyway, but since this runs
357 // on many different platforms let's play it safe
358 OString aPath
= rPath
.replaceAll("//", "/");
360 if( !aPath
.isEmpty() && aPath
[aPath
.getLength()-1] == '/' )
361 aPath
= aPath
.copy(0, aPath
.getLength()-1);
363 if( ( aPath
.indexOf("./") != -1 ||
364 aPath
.indexOf( '~' ) != -1 )
365 && realpath( aPath
.getStr(), buf
) )
375 void psp::splitPath( OString
& rPath
, OString
& rDir
, OString
& rBase
)
378 sal_Int32 nIndex
= rPath
.lastIndexOf( '/' );
380 rDir
= rPath
.copy( 0, nIndex
);
381 else if( nIndex
== 0 ) // root dir
382 rDir
= rPath
.copy( 0, 1 );
383 if( rPath
.getLength() > nIndex
+1 )
384 rBase
= rPath
.copy( nIndex
+1 );
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */