Bump version to 4.1-6
[LibreOffice.git] / vcl / generic / fontmanager / helper.cxx
blob33db29819c798affe84531fc215d523a527cbf46
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;
36 namespace psp {
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;
46 if( ! bOnce )
48 bOnce = true;
49 OUString aIni;
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://" ) )
59 OUString aSysPath;
60 if( osl_getSystemPathFromFileURL( aConfigPath.pData, &aSysPath.pData ) == osl_File_E_None )
61 aConfigPath = aSysPath;
63 if( aInstallationRootPath.startsWith( "file://" ) )
65 OUString aSysPath;
66 if( osl_getSystemPathFromFileURL( aInstallationRootPath.pData, &aSysPath.pData ) == osl_File_E_None )
67 aInstallationRootPath = aSysPath;
69 if( aUserPath.startsWith( "file://" ) )
71 OUString aSysPath;
72 if( osl_getSystemPathFromFileURL( aUserPath.pData, &aSysPath.pData ) == osl_File_E_None )
73 aUserPath = aSysPath;
75 // ensure user path exists
76 aUPath += "/user/psprint";
77 #if OSL_DEBUG_LEVEL > 1
78 oslFileError eErr =
79 #endif
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 );
83 #endif
86 switch( ePath )
88 case ConfigPath: return aConfigPath;
89 case InstallationRootPath: return aInstallationRootPath;
90 case UserPath: return aUserPath;
92 return aEmpty;
95 static OString getEnvironmentPath( const char* pKey )
97 OString aPath;
99 const char* pValue = getenv( pKey );
100 if( pValue && *pValue )
102 aPath = OString( pValue );
104 return aPath;
107 } // namespace psp
109 void psp::getPrinterPathList( std::list< OUString >& rPathList, const char* pSubDir )
111 rPathList.clear();
112 rtl_TextEncoding aEncoding = osl_getThreadTextEncoding();
114 OUStringBuffer aPathBuffer( 256 );
116 // append net path
117 aPathBuffer.append( getOfficePath( psp::InstallationRootPath ) );
118 if( aPathBuffer.getLength() )
120 aPathBuffer.appendAscii( "/share/psprint" );
121 if( pSubDir )
123 aPathBuffer.append( sal_Unicode('/') );
124 aPathBuffer.appendAscii( pSubDir );
126 rPathList.push_back( aPathBuffer.makeStringAndClear() );
128 // append user path
129 aPathBuffer.append( getOfficePath( psp::UserPath ) );
130 if( aPathBuffer.getLength() )
132 aPathBuffer.appendAscii( "/user/psprint" );
133 if( pSubDir )
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 ) );
146 if( aDir.isEmpty() )
147 continue;
149 if( pSubDir )
151 aDir += "/";
152 aDir += pSubDir;
154 struct stat aStat;
155 if( stat( aDir.getStr(), &aStat ) || ! S_ISDIR( aStat.st_mode ) )
156 continue;
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 ) );
166 #endif
168 if( rPathList.empty() )
170 // last resort: next to program file (mainly for setup)
171 OUString aExe;
172 if( osl_getExecutableFile( &aExe.pData ) == osl_Process_E_None )
174 INetURLObject aDir( aExe );
175 aDir.removeSegment();
176 aExe = aDir.GetMainURL( INetURLObject::NO_DECODE );
177 OUString aSysPath;
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;
190 if (aPath.isEmpty())
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
204 struct stat aStat;
205 if( 0 != stat( OUStringToOString( aPathBuffer.makeStringAndClear(), osl_getThreadTextEncoding() ).getStr(), &aStat )
206 || ! S_ISDIR( aStat.st_mode ) )
207 aConfigPath = OUString();
208 else
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() );
233 #endif
235 return aPath;
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;
247 bool bEof = false;
248 unsigned char buffer[256];
249 sal_uInt64 nRead;
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));
257 if( bEof )
258 break;
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 )
270 bSuccess = false;
271 while( bSuccess &&
272 ! rInFile.read( buffer, sizeof( buffer ), nRead ) &&
273 nRead != 0 )
275 if( rOutFile.write( buffer, nRead, nWrite ) ||
276 nWrite != nRead )
277 bSuccess = false;
279 bEof = true;
281 else
282 bSuccess = false;
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 )
290 if( nType == 1 )
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' )
302 i++;
303 pWriteBuffer[ nBytesToWrite++ ] = '\n';
305 else
306 pWriteBuffer[ nBytesToWrite++ ] = '\n';
308 if( rOutFile.write( pWriteBuffer, nBytesToWrite, nRead ) || nRead != nBytesToWrite )
309 bSuccess = false;
311 delete [] pWriteBuffer;
313 else
315 // binary data
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 ];
321 if( nBuffer >= 80 )
323 buffer[ nBuffer++ ] = '\n';
324 if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
325 bSuccess = false;
326 nBuffer = 0;
329 if( nBuffer > 0 && bSuccess )
331 buffer[ nBuffer++ ] = '\n';
332 if( rOutFile.write( buffer, nBuffer, nRead ) || nRead != nBuffer )
333 bSuccess = false;
337 else
338 bSuccess = false;
340 delete [] pBuffer;
342 else if( nType == 3 )
343 bEof = true;
344 else
345 bSuccess = false;
348 return bSuccess;
351 void psp::normPath( OString& rPath )
353 char buf[PATH_MAX];
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 ) )
367 rPath = buf;
369 else
371 rPath = aPath;
375 void psp::splitPath( OString& rPath, OString& rDir, OString& rBase )
377 normPath( rPath );
378 sal_Int32 nIndex = rPath.lastIndexOf( '/' );
379 if( nIndex > 0 )
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: */