Kaydet (Commit) 5c1c0a4e authored tarafından Luboš Luňák's avatar Luboš Luňák

function for duplicated code

Change-Id: If9d6a163abb5a1cbd64838ca005b14dcd51c4588
üst 45961203
...@@ -236,6 +236,7 @@ certain functionality. ...@@ -236,6 +236,7 @@ certain functionality.
@li @c vcl.atsui - ATSUI (obsolete) -using code for Mac OS X @li @c vcl.atsui - ATSUI (obsolete) -using code for Mac OS X
@li @c vcl.control @li @c vcl.control
@li @c vcl.coretext - CoreText-using code for Mac OS X and iOS @li @c vcl.coretext - CoreText-using code for Mac OS X and iOS
@li @c vcl.fonts - font-specific code
@li @c vcl.gdi - the GDI part of VCL, devices, bitmaps, etc. @li @c vcl.gdi - the GDI part of VCL, devices, bitmaps, etc.
@li @c vcl.gtk - Gtk+ 2/3 plugin @li @c vcl.gtk - Gtk+ 2/3 plugin
@li @c vcl.layout - Widget layout @li @c vcl.layout - Widget layout
......
...@@ -12,8 +12,11 @@ ...@@ -12,8 +12,11 @@
#include <vcl/dllapi.h> #include <vcl/dllapi.h>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <rtl/ustring.hxx> #include <rtl/ustring.hxx>
#include <tools/fontenum.hxx> #include <tools/fontenum.hxx>
#include <vector>
/** /**
Helper functions for handling embedded fonts in documents. Helper functions for handling embedded fonts in documents.
...@@ -27,6 +30,18 @@ public: ...@@ -27,6 +30,18 @@ public:
*/ */
static OUString fontFileUrl( const OUString& familyName, FontFamily family, FontItalic italic, static OUString fontFileUrl( const OUString& familyName, FontFamily family, FontItalic italic,
FontWeight weight, FontPitch pitch, rtl_TextEncoding encoding ); FontWeight weight, FontPitch pitch, rtl_TextEncoding encoding );
/**
Reads a font from the input stream, saves it to a temporary font file and activates the font.
@param stream stream of font data
@param fontName name of the font (e.g. 'Times New Roman')
@param extra additional text to use for name (e.g. to distinguish regular from bold, italic,...), "?" for unique
@param key key to xor the data with, from the start until the key's length (not repeated)
*/
static bool addEmbeddedFont( com::sun::star::uno::Reference< com::sun::star::io::XInputStream > stream,
const OUString& fontName, const char* extra,
std::vector< unsigned char > key = std::vector< unsigned char >());
/** /**
Returns an URL for a file where to store contents of a given temporary font. Returns an URL for a file where to store contents of a given temporary font.
The file may or not may not exist yet, and will be cleaned up automatically as appropriate. The file may or not may not exist yet, and will be cleaned up automatically as appropriate.
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <outfont.hxx> #include <outfont.hxx>
#include <salgdi.hxx> #include <salgdi.hxx>
using namespace com::sun::star;
using namespace vcl; using namespace vcl;
static void clearDir( const OUString& path ) static void clearDir( const OUString& path )
...@@ -49,6 +50,46 @@ void EmbeddedFontsHelper::clearTemporaryFontFiles() ...@@ -49,6 +50,46 @@ void EmbeddedFontsHelper::clearTemporaryFontFiles()
clearDir( path + "fromsystem/" ); clearDir( path + "fromsystem/" );
} }
bool EmbeddedFontsHelper::addEmbeddedFont( uno::Reference< io::XInputStream > stream, const OUString& fontName,
const char* extra, std::vector< unsigned char > key )
{
OUString fileUrl = EmbeddedFontsHelper::fileUrlForTemporaryFont( fontName, extra );
osl::File file( fileUrl );
switch( file.open( osl_File_OpenFlag_Create | osl_File_OpenFlag_Write ))
{
case osl::File::E_None:
break; // ok
case osl::File::E_EXIST:
return true; // Assume it's already been added correctly.
default:
SAL_WARN( "vcl.fonts", "Cannot open file for temporary font" );
return false;
}
size_t keyPos = 0;
for(;;)
{
uno::Sequence< sal_Int8 > buffer;
int read = stream->readBytes( buffer, 1024 );
for( int pos = 0;
pos < read && keyPos < key.size();
++pos )
buffer[ pos ] ^= key[ keyPos++ ];
sal_uInt64 dummy;
if( read > 0 )
file.write( buffer.getConstArray(), read, dummy );
if( read < 1024 )
break;
}
if( file.close() != osl::File::E_None )
{
SAL_WARN( "vcl.fonts", "Writing temporary font file failed" );
osl::File::remove( fileUrl );
return false;
}
EmbeddedFontsHelper::activateFont( fontName, fileUrl );
return true;
}
OUString EmbeddedFontsHelper::fileUrlForTemporaryFont( const OUString& fontName, const char* extra ) OUString EmbeddedFontsHelper::fileUrlForTemporaryFont( const OUString& fontName, const char* extra )
{ {
OUString path = "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}"; OUString path = "${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}";
......
...@@ -251,35 +251,12 @@ EmbeddedFontHandler::~EmbeddedFontHandler() ...@@ -251,35 +251,12 @@ EmbeddedFontHandler::~EmbeddedFontHandler()
{ {
if( !inputStream.is()) if( !inputStream.is())
return; return;
OUString fileUrl = EmbeddedFontsHelper::fileUrlForTemporaryFont( fontName, style ); std::vector< unsigned char > key( 32 );
osl::File file( fileUrl );
switch( file.open( osl_File_OpenFlag_Create | osl_File_OpenFlag_Write ))
{
case osl::File::E_None:
break; // ok
case osl::File::E_EXIST:
return; // Assume it's already been added correctly.
default:
SAL_WARN( "writerfilter", "Cannot open file for temporary font" );
inputStream->closeInput();
return;
}
if( !fontKey.isEmpty()) if( !fontKey.isEmpty())
{ // unobfuscate { // key for unobfuscating
uno::Sequence< sal_Int8 > buffer;
int read = inputStream->readBytes( buffer, 32 );
if( read < 32 )
{
SAL_WARN( "writerfilter", "Embedded font too small" );
inputStream->closeInput();
file.close();
osl::File::remove( fileUrl );
return;
}
// 1 3 5 7 10 2 5 7 20 2 5 7 9 1 3 5 // 1 3 5 7 10 2 5 7 20 2 5 7 9 1 3 5
// {62E79491-959F-41E9-B76B-6B32631DEA5C} // {62E79491-959F-41E9-B76B-6B32631DEA5C}
static const int pos[ 16 ] = { 35, 33, 31, 29, 27, 25, 22, 20, 17, 15, 12, 10, 7, 5, 3, 1 }; static const int pos[ 16 ] = { 35, 33, 31, 29, 27, 25, 22, 20, 17, 15, 12, 10, 7, 5, 3, 1 };
char key[ 16 ];
for( int i = 0; for( int i = 0;
i < 16; i < 16;
++i ) ++i )
...@@ -290,35 +267,11 @@ EmbeddedFontHandler::~EmbeddedFontHandler() ...@@ -290,35 +267,11 @@ EmbeddedFontHandler::~EmbeddedFontHandler()
assert(( v2 >= '0' && v2 <= '9' ) || ( v2 >= 'A' && v2 <= 'F' )); assert(( v2 >= '0' && v2 <= '9' ) || ( v2 >= 'A' && v2 <= 'F' ));
int val = ( v1 - ( v1 <= '9' ? '0' : 'A' - 10 )) * 16 + v2 - ( v2 <= '9' ? '0' : 'A' - 10 ); int val = ( v1 - ( v1 <= '9' ? '0' : 'A' - 10 )) * 16 + v2 - ( v2 <= '9' ? '0' : 'A' - 10 );
key[ i ] = val; key[ i ] = val;
key[ i + 16 ] = val;
} }
for( int i = 0;
i < 16;
++i )
{
buffer[ i ] ^= key[ i ];
buffer[ i + 16 ] ^= key[ i ];
}
sal_uInt64 dummy;
file.write( buffer.getConstArray(), 32, dummy );
}
for(;;)
{
uno::Sequence< sal_Int8 > buffer;
int read = inputStream->readBytes( buffer, 1024 );
sal_uInt64 dummy;
if( read > 0 )
file.write( buffer.getConstArray(), read, dummy );
if( read < 1024 )
break;
} }
EmbeddedFontsHelper::addEmbeddedFont( inputStream, fontName, style, key );
inputStream->closeInput(); inputStream->closeInput();
if( file.close() != osl::File::E_None )
{
SAL_WARN( "writerfilter", "Writing temporary font file failed" );
osl::File::remove( fileUrl );
return;
}
EmbeddedFontsHelper::activateFont( fontName, fileUrl );
} }
void EmbeddedFontHandler::lcl_attribute( Id name, Value& val ) void EmbeddedFontHandler::lcl_attribute( Id name, Value& val )
......
...@@ -252,38 +252,12 @@ void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const OUString& url ) ...@@ -252,38 +252,12 @@ void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const OUString& url )
if( url.indexOf( '/' ) > -1 ) // TODO what if more levels? if( url.indexOf( '/' ) > -1 ) // TODO what if more levels?
storage.set( storage->openStorageElement( url.copy( 0, url.indexOf( '/' )), storage.set( storage->openStorageElement( url.copy( 0, url.indexOf( '/' )),
::embed::ElementModes::READ ), uno::UNO_QUERY_THROW ); ::embed::ElementModes::READ ), uno::UNO_QUERY_THROW );
OUString fileUrl = EmbeddedFontsHelper::fileUrlForTemporaryFont( fontName, "?" );
osl::File file( fileUrl );
switch( file.open( osl_File_OpenFlag_Create | osl_File_OpenFlag_Write ))
{
case osl::File::E_None:
break; // ok
default:
SAL_WARN( "xmloff", "Cannot open file for temporary font" );
return;
}
uno::Reference< io::XInputStream > inputStream; uno::Reference< io::XInputStream > inputStream;
inputStream.set( storage->openStreamElement( url.copy( url.indexOf( '/' ) + 1 ), ::embed::ElementModes::READ ), inputStream.set( storage->openStreamElement( url.copy( url.indexOf( '/' ) + 1 ), ::embed::ElementModes::READ ),
UNO_QUERY_THROW ); UNO_QUERY_THROW );
for(;;) if( EmbeddedFontsHelper::addEmbeddedFont( inputStream, fontName, "?" ))
{ GetImport().NotifyEmbeddedFontRead();
uno::Sequence< sal_Int8 > buffer;
int read = inputStream->readBytes( buffer, 1024 );
sal_uInt64 dummy;
if( read > 0 )
file.write( buffer.getConstArray(), read, dummy );
if( read < 1024 )
break;
}
inputStream->closeInput(); inputStream->closeInput();
if( file.close() != osl::File::E_None )
{
SAL_WARN( "xmloff", "Writing temporary font file failed" );
osl::File::remove( fileUrl );
return;
}
EmbeddedFontsHelper::activateFont( fontName, fileUrl );
GetImport().NotifyEmbeddedFontRead();
} }
else else
SAL_WARN( "xmloff", "External URL for font file not handled." ); SAL_WARN( "xmloff", "External URL for font file not handled." );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment