Use SvXMLUnitConverter's base64 encoder

SVGActionWriter uses FastString class to encode an image to a base64 string.
But there are several more generic encoders. We can use one of them.
This commit is contained in:
Takeshi Kurosawa 2011-02-02 12:39:36 +09:00 committed by Thorsten Behrens
parent 69a3a12a54
commit e95db00960
2 changed files with 14 additions and 158 deletions

View file

@ -68,19 +68,6 @@ static const char aXMLAttrHeight[] = "height";
static const char aXMLAttrPoints[] = "points"; static const char aXMLAttrPoints[] = "points";
static const char aXMLAttrXLinkHRef[] = "xlink:href"; static const char aXMLAttrXLinkHRef[] = "xlink:href";
static const sal_Unicode pBase64[] =
{
//0 1 2 3 4 5 6 7
'A','B','C','D','E','F','G','H', // 0
'I','J','K','L','M','N','O','P', // 1
'Q','R','S','T','U','V','W','X', // 2
'Y','Z','a','b','c','d','e','f', // 3
'g','h','i','j','k','l','m','n', // 4
'o','p','q','r','s','t','u','v', // 5
'w','x','y','z','0','1','2','3', // 6
'4','5','6','7','8','9','+','/' // 7
};
// -------------- // --------------
// - FastString - // - FastString -
// -------------- // --------------
@ -98,64 +85,6 @@ FastString::FastString( sal_uInt32 nInitLen, sal_uInt32 nIncrement ) :
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
FastString::FastString( sal_Char* pBufferForBase64Encoding, sal_uInt32 nBufLen ) :
mnBufInc( 2048 ),
mnPartPos( 0 )
{
DBG_ASSERT( pBufferForBase64Encoding && nBufLen, "invalid arguments" );
const sal_uInt32 nQuadCount = nBufLen / 3;
const sal_uInt32 nRest = nBufLen % 3;
if( nQuadCount || nRest )
{
mnBufLen = mnCurLen = ( ( nQuadCount + ( nRest ? 1 : 0 ) ) << 2 );
mpBuffer = new sal_Unicode[ mnBufLen * sizeof( sal_Unicode ) ];
sal_Char* pTmpSrc = pBufferForBase64Encoding;
sal_Unicode* pTmpDst = mpBuffer;
for( sal_uInt32 i = 0; i < nQuadCount; i++ )
{
const sal_Int32 nA = *pTmpSrc++;
const sal_Int32 nB = *pTmpSrc++;
const sal_Int32 nC = *pTmpSrc++;
*pTmpDst++ = pBase64[ ( nA >> 2 ) & 0x3f ];
*pTmpDst++ = pBase64[ ( ( nA << 4 ) & 0x30 ) + ( ( nB >> 4 ) & 0xf ) ];
*pTmpDst++ = pBase64[ ( ( nB << 2 ) & 0x3c ) + ( ( nC >> 6 ) & 0x3 ) ];
*pTmpDst++ = pBase64[ nC & 0x3f ];
}
if( 1 == nRest )
{
const sal_Int32 nA = *pTmpSrc;
*pTmpDst++ = pBase64[ ( nA >> 2 ) & 0x3f ];
*pTmpDst++ = pBase64[ ( nA << 4 ) & 0x30 ];
*pTmpDst++ = '=';
*pTmpDst = '=';
}
else if( 2 == nRest )
{
const sal_Int32 nA = *pTmpSrc++;
const sal_Int32 nB = *pTmpSrc;
*pTmpDst++ = pBase64[ ( nA >> 2 ) & 0x3f ];
*pTmpDst++ = pBase64[ ( ( nA << 4 ) & 0x30 ) + ( ( nB >> 4 ) & 0xf ) ];
*pTmpDst++ = pBase64[ ( nB << 2 ) & 0x3c ];
*pTmpDst = '=';
}
}
else
{
mpBuffer = new sal_Unicode[ ( mnBufLen = 1 ) * sizeof( sal_Unicode ) ];
mnCurLen = 0;
}
}
// -----------------------------------------------------------------------------
FastString::~FastString() FastString::~FastString()
{ {
delete[] mpBuffer; delete[] mpBuffer;
@ -198,39 +127,6 @@ const NMSP_RTL::OUString& FastString::GetString() const
return maString; return maString;
} }
// -----------------------------------------------------------------------------
sal_Bool FastString::GetFirstPartString( const sal_uInt32 nPartLen, NMSP_RTL::OUString& rPartString )
{
const sal_uInt32 nLength = Min( mnCurLen, nPartLen );
mnPartPos = 0;
if( nLength )
{
rPartString = NMSP_RTL::OUString( mpBuffer, nLength );
mnPartPos = nLength;
}
return( rPartString.getLength() > 0 );
}
// -----------------------------------------------------------------------------
sal_Bool FastString::GetNextPartString( const sal_uInt32 nPartLen, NMSP_RTL::OUString& rPartString )
{
if( mnPartPos < mnCurLen )
{
const sal_uInt32 nLength = Min( mnCurLen - mnPartPos, nPartLen );
rPartString = NMSP_RTL::OUString( mpBuffer + mnPartPos, nLength );
mnPartPos += nLength;
}
else
rPartString = NMSP_RTL::OUString();
return( rPartString.getLength() > 0 );
}
// ---------------------- // ----------------------
// - SVGAttributeWriter - // - SVGAttributeWriter -
// ---------------------- // ----------------------
@ -1108,57 +1004,19 @@ void SVGActionWriter::ImplWriteBmp( const BitmapEx& rBmpEx,
{ {
const Point aPt( ImplMap( rPt ) ); const Point aPt( ImplMap( rPt ) );
const Size aSz( ImplMap( rSz ) ); const Size aSz( ImplMap( rSz ) );
FastString aImageData( (sal_Char*) aOStm.GetData(), aOStm.Tell() ); Sequence< sal_Int8 > aSeq( (sal_Int8*) aOStm.GetData(), aOStm.Tell() );
REF( NMSP_SAX::XExtendedDocumentHandler ) xExtDocHandler( mrExport.GetDocHandler(), NMSP_UNO::UNO_QUERY ); NMSP_RTL::OUStringBuffer aBuffer;
aBuffer.appendAscii( "data:image/png;base64," );
SvXMLUnitConverter::encodeBase64( aBuffer, aSeq );
mrExport.AddAttribute( XML_NAMESPACE_NONE, aXMLAttrX, GetValueString( aPt.X() ) );
mrExport.AddAttribute( XML_NAMESPACE_NONE, aXMLAttrY, GetValueString( aPt.Y() ) );
mrExport.AddAttribute( XML_NAMESPACE_NONE, aXMLAttrWidth, GetValueString( aSz.Width() ) );
mrExport.AddAttribute( XML_NAMESPACE_NONE, aXMLAttrHeight, GetValueString( aSz.Height() ) );
mrExport.AddAttribute( XML_NAMESPACE_NONE, aXMLAttrXLinkHRef, aBuffer.makeStringAndClear() );
if( xExtDocHandler.is() )
{ {
static const sal_uInt32 nPartLen = 64; SvXMLElementExport aElem( mrExport, XML_NAMESPACE_NONE, aXMLElemImage, TRUE, TRUE );
const NMSP_RTL::OUString aSpace( ' ' );
const NMSP_RTL::OUString aLineFeed( NMSP_RTL::OUString::valueOf( (sal_Unicode) 0x0a ) );
NMSP_RTL::OUString aString;
NMSP_RTL::OUString aImageString;
aString = aLineFeed;
aString += B2UCONST( "<" );
aString += NMSP_RTL::OUString::createFromAscii( aXMLElemImage );
aString += aSpace;
aString += NMSP_RTL::OUString::createFromAscii( aXMLAttrX );
aString += B2UCONST( "=\"" );
aString += GetValueString( aPt.X() );
aString += B2UCONST( "\" " );
aString += NMSP_RTL::OUString::createFromAscii( aXMLAttrY );
aString += B2UCONST( "=\"" );
aString += GetValueString( aPt.Y() );
aString += B2UCONST( "\" " );
aString += NMSP_RTL::OUString::createFromAscii( aXMLAttrWidth );
aString += B2UCONST( "=\"" );
aString += GetValueString( aSz.Width() );
aString += B2UCONST( "\" " );
aString += NMSP_RTL::OUString::createFromAscii( aXMLAttrHeight );
aString += B2UCONST( "=\"" );
aString += GetValueString( aSz.Height() );
aString += B2UCONST( "\" " );
aString += NMSP_RTL::OUString::createFromAscii( aXMLAttrXLinkHRef );
aString += B2UCONST( "=\"data:image/png;base64," );
if( aImageData.GetFirstPartString( nPartLen, aImageString ) )
{
xExtDocHandler->unknown( aString += aImageString );
while( aImageData.GetNextPartString( nPartLen, aImageString ) )
{
xExtDocHandler->unknown( aLineFeed );
xExtDocHandler->unknown( aImageString );
}
}
xExtDocHandler->unknown( B2UCONST( "\"/>" ) );
} }
} }
} }

View file

@ -44,6 +44,7 @@
#include <vcl/cvtgrf.hxx> #include <vcl/cvtgrf.hxx>
#include <xmloff/xmlexp.hxx> #include <xmloff/xmlexp.hxx>
#include <xmloff/nmspmap.hxx> #include <xmloff/nmspmap.hxx>
#include <xmloff/xmluconv.hxx>
#include <com/sun/star/uno/Reference.h> #include <com/sun/star/uno/Reference.h>
#include <com/sun/star/uno/RuntimeException.hpp> #include <com/sun/star/uno/RuntimeException.hpp>
@ -102,14 +103,11 @@ private:
public: public:
FastString( sal_uInt32 nInitLen = 2048, sal_uInt32 nIncrement = 2048 ); FastString( sal_uInt32 nInitLen = 2048, sal_uInt32 nIncrement = 2048 );
FastString( sal_Char* pBufferForBase64Encoding, sal_uInt32 nBufLen );
~FastString(); ~FastString();
FastString& operator+=( const ::rtl::OUString& rStr ); FastString& operator+=( const ::rtl::OUString& rStr );
const ::rtl::OUString& GetString() const; const ::rtl::OUString& GetString() const;
sal_Bool GetFirstPartString( const sal_uInt32 nPartLen, ::rtl::OUString& rPartString );
sal_Bool GetNextPartString( const sal_uInt32 nPartLen, ::rtl::OUString& rPartString );
sal_uInt32 GetLength() const { return mnCurLen; } sal_uInt32 GetLength() const { return mnCurLen; }
void Clear() { mnCurLen = 0, maString = ::rtl::OUString(); } void Clear() { mnCurLen = 0, maString = ::rtl::OUString(); }