Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / vcl / generic / fontmanager / helper.cxx
blob9a3296b7bbf17ed951ce8e206349aa66c6d5ca61
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <cstring>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <limits.h>
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;
35 using ::rtl::OUString;
36 using ::rtl::OUStringBuffer;
37 using ::rtl::OString;
38 using ::rtl::OStringToOUString;
39 using ::rtl::OUStringToOString;
41 namespace psp {
43 OUString getOfficePath( enum whichOfficePath ePath )
45 static OUString aInstallationRootPath;
46 static OUString aUserPath;
47 static OUString aConfigPath;
48 static OUString aEmpty;
49 static bool bOnce = false;
51 if( ! bOnce )
53 bOnce = true;
54 OUString aIni;
55 Bootstrap::get( OUString( RTL_CONSTASCII_USTRINGPARAM( "BRAND_BASE_DIR" ) ), aInstallationRootPath );
56 aIni = aInstallationRootPath + OUString( RTL_CONSTASCII_USTRINGPARAM( "/program/" SAL_CONFIGFILE( "bootstrap" ) ) );
57 Bootstrap aBootstrap( aIni );
58 aBootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "CustomDataUrl" ) ), aConfigPath );
59 aBootstrap.getFrom( OUString( RTL_CONSTASCII_USTRINGPARAM( "UserInstallation" ) ), aUserPath );
60 OUString aUPath = aUserPath;
62 if( ! aConfigPath.compareToAscii( "file://", 7 ) )
64 OUString aSysPath;
65 if( osl_getSystemPathFromFileURL( aConfigPath.pData, &aSysPath.pData ) == osl_File_E_None )
66 aConfigPath = aSysPath;
68 if( ! aInstallationRootPath.compareToAscii( "file://", 7 ) )
70 OUString aSysPath;
71 if( osl_getSystemPathFromFileURL( aInstallationRootPath.pData, &aSysPath.pData ) == osl_File_E_None )
72 aInstallationRootPath = aSysPath;
74 if( ! aUserPath.compareToAscii( "file://", 7 ) )
76 OUString aSysPath;
77 if( osl_getSystemPathFromFileURL( aUserPath.pData, &aSysPath.pData ) == osl_File_E_None )
78 aUserPath = aSysPath;
80 // ensure user path exists
81 aUPath += OUString( RTL_CONSTASCII_USTRINGPARAM( "/user/psprint" ) );
82 #if OSL_DEBUG_LEVEL > 1
83 oslFileError eErr =
84 #endif
85 osl_createDirectoryPath( aUPath.pData, NULL, NULL );
86 #if OSL_DEBUG_LEVEL > 1
87 fprintf( stderr, "try to create \"%s\" = %d\n", OUStringToOString( aUPath, RTL_TEXTENCODING_UTF8 ).getStr(), eErr );
88 #endif
91 switch( ePath )
93 case ConfigPath: return aConfigPath;
94 case InstallationRootPath: return aInstallationRootPath;
95 case UserPath: return aUserPath;
97 return aEmpty;
100 static OString getEnvironmentPath( const char* pKey )
102 OString aPath;
104 const char* pValue = getenv( pKey );
105 if( pValue && *pValue )
107 aPath = OString( pValue );
109 return aPath;
112 } // namespace psp
114 void psp::getPrinterPathList( std::list< OUString >& rPathList, const char* pSubDir )
116 rPathList.clear();
117 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
119 OUStringBuffer aPathBuffer( 256 );
121 // append net path
122 aPathBuffer.append( getOfficePath( psp::InstallationRootPath ) );
123 if( aPathBuffer.getLength() )
125 aPathBuffer.appendAscii( "/share/psprint" );
126 if( pSubDir )
128 aPathBuffer.append( sal_Unicode('/') );
129 aPathBuffer.appendAscii( pSubDir );
131 rPathList.push_back( aPathBuffer.makeStringAndClear() );
133 // append user path
134 aPathBuffer.append( getOfficePath( psp::UserPath ) );
135 if( aPathBuffer.getLength() )
137 aPathBuffer.appendAscii( "/user/psprint" );
138 if( pSubDir )
140 aPathBuffer.append( sal_Unicode('/') );
141 aPathBuffer.appendAscii( pSubDir );
143 rPathList.push_back( aPathBuffer.makeStringAndClear() );
146 OString aPath( getEnvironmentPath("SAL_PSPRINT") );
147 sal_Int32 nIndex = 0;
150 OString aDir( aPath.getToken( 0, ':', nIndex ) );
151 if( aDir.isEmpty() )
152 continue;
154 if( pSubDir )
156 aDir += "/";
157 aDir += pSubDir;
159 struct stat aStat;
160 if( stat( aDir.getStr(), &aStat ) || ! S_ISDIR( aStat.st_mode ) )
161 continue;
163 rPathList.push_back( OStringToOUString( aDir, aEncoding ) );
164 } while( nIndex != -1 );
166 #ifdef SYSTEM_PPD_DIR
167 if( pSubDir && rtl_str_compare( pSubDir, PRINTER_PPDDIR ) == 0 )
169 rPathList.push_back( rtl::OStringToOUString( rtl::OString( SYSTEM_PPD_DIR ), RTL_TEXTENCODING_UTF8 ) );
171 #endif
173 if( rPathList.empty() )
175 // last resort: next to program file (mainly for setup)
176 OUString aExe;
177 if( osl_getExecutableFile( &aExe.pData ) == osl_Process_E_None )
179 INetURLObject aDir( aExe );
180 aDir.removeSegment();
181 aExe = aDir.GetMainURL( INetURLObject::NO_DECODE );
182 OUString aSysPath;
183 if( osl_getSystemPathFromFileURL( aExe.pData, &aSysPath.pData ) == osl_File_E_None )
185 rPathList.push_back( aSysPath );
191 OUString psp::getFontPath()
193 static OUString aPath;
195 if (aPath.isEmpty())
197 OUStringBuffer aPathBuffer( 512 );
199 OUString aConfigPath( getOfficePath( psp::ConfigPath ) );
200 OUString aInstallationRootPath( getOfficePath( psp::InstallationRootPath ) );
201 OUString aUserPath( getOfficePath( psp::UserPath ) );
202 if( !aConfigPath.isEmpty() )
204 // #i53530# Path from CustomDataUrl will completely
205 // replace net and user paths if the path exists
206 aPathBuffer.append(aConfigPath);
207 aPathBuffer.appendAscii("/share/fonts");
208 // check existance of config path
209 struct stat aStat;
210 if( 0 != stat( OUStringToOString( aPathBuffer.makeStringAndClear(), osl_getThreadTextEncoding() ).getStr(), &aStat )
211 || ! S_ISDIR( aStat.st_mode ) )
212 aConfigPath = OUString();
213 else
215 aPathBuffer.append(aConfigPath);
216 aPathBuffer.appendAscii("/share/fonts");
219 if( aConfigPath.isEmpty() )
221 if( !aInstallationRootPath.isEmpty() )
223 aPathBuffer.append( aInstallationRootPath );
224 aPathBuffer.appendAscii( "/share/fonts/truetype;");
225 aPathBuffer.append( aInstallationRootPath );
226 aPathBuffer.appendAscii( "/share/fonts/type1;" );
228 if( !aUserPath.isEmpty() )
230 aPathBuffer.append( aUserPath );
231 aPathBuffer.appendAscii( "/user/fonts" );
235 aPath = aPathBuffer.makeStringAndClear();
236 #if OSL_DEBUG_LEVEL > 1
237 fprintf( stderr, "initializing font path to \"%s\"\n", OUStringToOString( aPath, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
238 #endif
240 return aPath;
243 bool psp::convertPfbToPfa( ::osl::File& rInFile, ::osl::File& rOutFile )
245 static unsigned char hexDigits[] =
247 '0', '1', '2', '3', '4', '5', '6', '7',
248 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
251 bool bSuccess = true;
252 bool bEof = false;
253 unsigned char buffer[256];
254 sal_uInt64 nRead;
255 sal_uInt64 nOrgPos = 0;
256 rInFile.getPos( nOrgPos );
258 while( bSuccess && ! bEof )
260 // read leading bytes
261 bEof = ! rInFile.read( buffer, 6, nRead ) && nRead == 6 ? false : true;
262 unsigned int nType = buffer[ 1 ];
263 unsigned int nBytesToRead = buffer[2] | buffer[3] << 8 | buffer[4] << 16 | buffer[5] << 24;
264 if( buffer[0] != 0x80 ) // test for pfb m_agic number
266 // this migt be a pfa font already
267 if( ! rInFile.read( buffer+6, 9, nRead ) && nRead == 9 &&
268 ( ! std::strncmp( (char*)buffer, "%!FontType1-", 12 ) ||
269 ! std::strncmp( (char*)buffer, "%!PS-AdobeFont-", 15 ) ) )
271 sal_uInt64 nWrite = 0;
272 if( rOutFile.write( buffer, 15, nWrite ) || nWrite != 15 )
273 bSuccess = false;
274 while( bSuccess &&
275 ! rInFile.read( buffer, sizeof( buffer ), nRead ) &&
276 nRead != 0 )
278 if( rOutFile.write( buffer, nRead, nWrite ) ||
279 nWrite != nRead )
280 bSuccess = false;
282 bEof = true;
284 else
285 bSuccess = false;
287 else if( nType == 1 || nType == 2 )
289 unsigned char* pBuffer = new unsigned char[ nBytesToRead+1 ];
291 if( ! rInFile.read( pBuffer, nBytesToRead, nRead ) && nRead == nBytesToRead )
293 if( nType == 1 )
295 // ascii data, convert dos lineends( \r\n ) and
296 // m_ac lineends( \r ) to \n
297 unsigned char * pWriteBuffer = new unsigned char[ nBytesToRead ];
298 unsigned int nBytesToWrite = 0;
299 for( unsigned int i = 0; i < nBytesToRead; i++ )
301 if( pBuffer[i] != '\r' )
302 pWriteBuffer[ nBytesToWrite++ ] = pBuffer[i];
303 else if( pBuffer[ i+1 ] == '\n' )
305 i++;
306 pWriteBuffer[ nBytesToWrite++ ] = '\n';
308 else
309 pWriteBuffer[ nBytesToWrite++ ] = '\n';
311 if( rOutFile.write( pWriteBuffer, nBytesToWrite, nRead ) || nRead != nBytesToWrite )
312 bSuccess = false;
314 delete [] pWriteBuffer;
316 else
318 // binary data
319 unsigned int nBuffer = 0;
320 for( unsigned int i = 0; i < nBytesToRead && bSuccess; i++ )
322 buffer[ nBuffer++ ] = hexDigits[ pBuffer[ i ] >> 4 ];
323 buffer[ nBuffer++ ] = hexDigits[ pBuffer[ i ] & 15 ];
324 if( nBuffer >= 80 )
326 buffer[ nBuffer++ ] = '\n';
327 if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
328 bSuccess = false;
329 nBuffer = 0;
332 if( nBuffer > 0 && bSuccess )
334 buffer[ nBuffer++ ] = '\n';
335 if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
336 bSuccess = false;
340 else
341 bSuccess = false;
343 delete [] pBuffer;
345 else if( nType == 3 )
346 bEof = true;
347 else
348 bSuccess = false;
351 return bSuccess;
354 void psp::normPath( OString& rPath )
356 char buf[PATH_MAX];
358 // double slashes and slash at end are probably
359 // removed by realpath anyway, but since this runs
360 // on many different platforms let's play it safe
361 rtl::OString aPath = rPath.replaceAll("//", "/");
363 if( !aPath.isEmpty() && aPath[aPath.getLength()-1] == '/' )
364 aPath = aPath.copy(0, aPath.getLength()-1);
366 if( ( aPath.indexOfL(RTL_CONSTASCII_STRINGPARAM("./")) != -1 ||
367 aPath.indexOf( '~' ) != -1 )
368 && realpath( aPath.getStr(), buf ) )
370 rPath = buf;
372 else
374 rPath = aPath;
378 void psp::splitPath( OString& rPath, OString& rDir, OString& rBase )
380 normPath( rPath );
381 sal_Int32 nIndex = rPath.lastIndexOf( '/' );
382 if( nIndex > 0 )
383 rDir = rPath.copy( 0, nIndex );
384 else if( nIndex == 0 ) // root dir
385 rDir = rPath.copy( 0, 1 );
386 if( rPath.getLength() > nIndex+1 )
387 rBase = rPath.copy( nIndex+1 );
391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */