summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatarina Behrens <Katarina.Behrens@cib.de>2017-09-04 14:14:39 +0200
committerSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>2017-11-28 11:54:17 +0100
commit082cdbc991a2e31556bab53a5b8214cb89138354 (patch)
tree44eb08e6298928643455ada1435f7ca9383c331d
parent913f77dc5af13d5ceabf5c9fac2a870ebb7ccdea (diff)
tdf#109202: Convert images to desired format in (f)odt filter
Pass down mimetype to SvXMLGraphicHelper Change-Id: I9c81c06d2a1d6168704440094081e99d0bcbbff9 Reviewed-on: https://gerrit.libreoffice.org/41893 Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de> Tested-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
-rw-r--r--comphelper/Library_comphelper.mk1
-rw-r--r--comphelper/source/misc/graphicmimetype.cxx51
-rw-r--r--include/comphelper/graphicmimetype.hxx27
-rw-r--r--include/svx/xmlgrhlp.hxx12
-rw-r--r--include/xmloff/xmlexp.hxx3
-rw-r--r--svx/source/xml/xmlgrhlp.cxx83
-rw-r--r--sw/source/filter/xml/xmlexp.cxx2
-rw-r--r--xmloff/source/core/xmlexp.cxx4
-rw-r--r--xmloff/source/text/txtparae.cxx13
9 files changed, 145 insertions, 51 deletions
diff --git a/comphelper/Library_comphelper.mk b/comphelper/Library_comphelper.mk
index 6f8b9d07a83a..e997bd5e6f32 100644
--- a/comphelper/Library_comphelper.mk
+++ b/comphelper/Library_comphelper.mk
@@ -106,6 +106,7 @@ $(eval $(call gb_Library_add_exception_objects,comphelper,\
comphelper/source/misc/evtmethodhelper \
comphelper/source/misc/fileurl \
comphelper/source/misc/getexpandeduri \
+ comphelper/source/misc/graphicmimetype \
comphelper/source/misc/hash \
comphelper/source/misc/instancelocker \
comphelper/source/misc/interaction \
diff --git a/comphelper/source/misc/graphicmimetype.cxx b/comphelper/source/misc/graphicmimetype.cxx
new file mode 100644
index 000000000000..f8eeb3e92de5
--- /dev/null
+++ b/comphelper/source/misc/graphicmimetype.cxx
@@ -0,0 +1,51 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <comphelper/graphicmimetype.hxx>
+
+namespace comphelper
+{
+OUString GraphicMimeTypeHelper::GetMimeTypeForExtension(const OString& rExt)
+{
+ struct XMLGraphicMimeTypeMapper
+ {
+ const char* pExt;
+ const char* pMimeType;
+ };
+
+ static const XMLGraphicMimeTypeMapper aMapper[]
+ = { { "gif", "image/gif" }, { "png", "image/png" }, { "jpg", "image/jpeg" },
+ { "tif", "image/tiff" }, { "svg", "image/svg+xml" }, { "pdf", "application/pdf" },
+ { "wmf", "image/x-wmf" }, { "eps", "image/x-eps" }, { "bmp", "image/bmp" },
+ { "pct", "image/x-pict" } };
+
+ OUString aMimeType;
+
+ long const nCount = SAL_N_ELEMENTS(aMapper);
+ for (long i = 0; (i < nCount) && aMimeType.isEmpty(); ++i)
+ {
+ if (rExt == aMapper[i].pExt)
+ aMimeType = OUString(aMapper[i].pMimeType, strlen(aMapper[i].pMimeType),
+ RTL_TEXTENCODING_ASCII_US);
+ }
+
+ return aMimeType;
+}
+}
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/comphelper/graphicmimetype.hxx b/include/comphelper/graphicmimetype.hxx
new file mode 100644
index 000000000000..3f2b5308b2d6
--- /dev/null
+++ b/include/comphelper/graphicmimetype.hxx
@@ -0,0 +1,27 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef INCLUDED_COMPHELPER_GMH_HXX
+#define INCLUDED_COMPHELPER_GMH_HXX
+
+#include <comphelper/comphelperdllapi.h>
+#include <rtl/ustring.hxx>
+
+namespace comphelper
+{
+class COMPHELPER_DLLPUBLIC GraphicMimeTypeHelper
+{
+public:
+ static OUString GetMimeTypeForExtension(const OString& rExt);
+};
+}
+
+#endif // INCLUDED_COMPHELPER_GMH_HXX
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/svx/xmlgrhlp.hxx b/include/svx/xmlgrhlp.hxx
index 0c6eaa1d2124..9aff143b3b04 100644
--- a/include/svx/xmlgrhlp.hxx
+++ b/include/svx/xmlgrhlp.hxx
@@ -59,6 +59,7 @@ class SVX_DLLPUBLIC SvXMLGraphicHelper final : public cppu::WeakComponentImplHel
GraphicOutputStreamVector maGrfStms;
URLSet maURLSet;
SvXMLGraphicHelperMode meCreateMode;
+ OUString maOutputMimeType;
bool mbDirect;
SVX_DLLPRIVATE static bool ImplGetStreamNames( const OUString& rURLStr,
@@ -82,7 +83,8 @@ class SVX_DLLPUBLIC SvXMLGraphicHelper final : public cppu::WeakComponentImplHel
virtual ~SvXMLGraphicHelper() override;
void Init( const css::uno::Reference < css::embed::XStorage >& xXMLStorage,
SvXMLGraphicHelperMode eCreateMode,
- bool bDirect );
+ bool bDirect,
+ const OUString& rGraphicMimeType = OUString() );
virtual void SAL_CALL disposing() override;
@@ -91,8 +93,12 @@ public:
static rtl::Reference<SvXMLGraphicHelper> Create( const css::uno::Reference < css::embed::XStorage >& rXMLStorage,
SvXMLGraphicHelperMode eCreateMode,
- bool bDirect = true );
- static rtl::Reference<SvXMLGraphicHelper> Create( SvXMLGraphicHelperMode eCreateMode );
+ bool bDirect = true,
+ const OUString& rGraphicMimeType = OUString() );
+ static rtl::Reference<SvXMLGraphicHelper> Create( SvXMLGraphicHelperMode eCreateMode,
+ const OUString& rMimeType = OUString() );
+
+public:
// XGraphicObjectResolver
virtual OUString SAL_CALL resolveGraphicObjectURL( const OUString& aURL ) override;
diff --git a/include/xmloff/xmlexp.hxx b/include/xmloff/xmlexp.hxx
index f981929b3fe7..474d718d5a41 100644
--- a/include/xmloff/xmlexp.hxx
+++ b/include/xmloff/xmlexp.hxx
@@ -554,6 +554,9 @@ public:
/// set null date from model to unit converter, if not already done
bool SetNullDateOnUnitConverter();
+
+ /// Get clamped mimetype for image export (empty if none)
+ OUString GetImageFilterName() const;
};
inline rtl::Reference< XMLTextParagraphExport > const & SvXMLExport::GetTextParagraphExport()
diff --git a/svx/source/xml/xmlgrhlp.cxx b/svx/source/xml/xmlgrhlp.cxx
index bfc7414b5211..ff7e4043d519 100644
--- a/svx/source/xml/xmlgrhlp.cxx
+++ b/svx/source/xml/xmlgrhlp.cxx
@@ -27,6 +27,7 @@
#include <com/sun/star/io/NotConnectedException.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
+#include <comphelper/graphicmimetype.hxx>
#include <cppuhelper/compbase.hxx>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
@@ -89,14 +90,15 @@ private:
public:
- explicit SvXMLGraphicInputStream( const OUString& rGraphicId );
+ explicit SvXMLGraphicInputStream( const OUString& rGraphicId, const OUString& rMimeType );
SvXMLGraphicInputStream(const SvXMLGraphicInputStream&) = delete;
SvXMLGraphicInputStream& operator=(const SvXMLGraphicInputStream&) = delete;
bool Exists() const { return mxStmWrapper.is(); }
};
-SvXMLGraphicInputStream::SvXMLGraphicInputStream( const OUString& rGraphicId )
+
+SvXMLGraphicInputStream::SvXMLGraphicInputStream( const OUString& rGraphicId, const OUString& rMimeType )
{
GraphicObject aGrfObject( OUStringToOString(rGraphicId, RTL_TEXTENCODING_ASCII_US) );
@@ -114,30 +116,43 @@ SvXMLGraphicInputStream::SvXMLGraphicInputStream( const OUString& rGraphicId )
if( aGfxLink.GetDataSize() && aGfxLink.GetData() )
{
- pStm->WriteBytes(aGfxLink.GetData(), aGfxLink.GetDataSize());
- bRet = ( pStm->GetError() == ERRCODE_NONE );
+ if ( rMimeType.isEmpty() )
+ {
+ pStm->WriteBytes(aGfxLink.GetData(), aGfxLink.GetDataSize());
+ bRet = ( pStm->GetError() == ERRCODE_NONE );
+ }
+ else
+ {
+ GraphicFilter &rFilter = GraphicFilter::GetGraphicFilter();
+ bRet = ( rFilter.ExportGraphic( aGraphic, "", *pStm, rFilter.GetExportFormatNumberForMediaType( rMimeType ) ) == ERRCODE_NONE );
+ }
}
else
{
if( aGraphic.GetType() == GraphicType::Bitmap )
{
GraphicFilter &rFilter = GraphicFilter::GetGraphicFilter();
- OUString aFormat;
+ OUString aFormat=rMimeType;
if( aGraphic.IsAnimated() )
- aFormat = "gif";
- else
- aFormat = "png";
+ aFormat = "image/gif";
+ else if( aFormat.isEmpty() )
+ aFormat = "image/png";
- bRet = ( rFilter.ExportGraphic( aGraphic, "", *pStm, rFilter.GetExportFormatNumberForShortName( aFormat ) ) == ERRCODE_NONE );
+ bRet = ( rFilter.ExportGraphic( aGraphic, "", *pStm, rFilter.GetExportFormatNumberForMediaType( aFormat ) ) == ERRCODE_NONE );
}
- else if( aGraphic.GetType() == GraphicType::GdiMetafile )
+ else if( rMimeType.isEmpty() && aGraphic.GetType() == GraphicType::GdiMetafile )
{
pStm->SetVersion( SOFFICE_FILEFORMAT_8 );
pStm->SetCompressMode( SvStreamCompressFlags::ZBITMAP );
const_cast<GDIMetaFile&>( aGraphic.GetGDIMetaFile() ).Write( *pStm );
bRet = ( pStm->GetError() == ERRCODE_NONE );
}
+ else if( !rMimeType.isEmpty() )
+ {
+ GraphicFilter &rFilter = GraphicFilter::GetGraphicFilter();
+ bRet = ( rFilter.ExportGraphic( aGraphic, "", *pStm, rFilter.GetExportFormatNumberForMediaType( rMimeType ) ) == ERRCODE_NONE );
+ }
}
if( bRet )
@@ -450,40 +465,14 @@ SvxGraphicHelperStream_Impl SvXMLGraphicHelper::ImplGetGraphicStream( const OUSt
OUString SvXMLGraphicHelper::ImplGetGraphicMimeType( const OUString& rFileName )
{
- struct XMLGraphicMimeTypeMapper
- {
- const char* pExt;
- const char* pMimeType;
- };
-
- static const XMLGraphicMimeTypeMapper aMapper[] =
- {
- { "gif", "image/gif" },
- { "png", "image/png" },
- { "jpg", "image/jpeg" },
- { "tif", "image/tiff" },
- { "svg", "image/svg+xml" },
- { "pdf", "application/pdf" },
- { "wmf", "image/x-wmf" },
- { "eps", "image/x-eps" },
- { "bmp", "image/bmp" },
- { "pct", "image/x-pict" }
- };
-
- OUString aMimeType;
-
if( ( rFileName.getLength() >= 4 ) && ( rFileName[ rFileName.getLength() - 4 ] == '.' ) )
{
const OString aExt(OUStringToOString(rFileName.copy(rFileName.getLength() - 3),
RTL_TEXTENCODING_ASCII_US));
-
- long const nCount = SAL_N_ELEMENTS(aMapper);
- for( long i = 0; ( i < nCount ) && aMimeType.isEmpty(); ++i )
- if( aExt == aMapper[ i ].pExt )
- aMimeType = OUString( aMapper[ i ].pMimeType, strlen( aMapper[ i ].pMimeType ), RTL_TEXTENCODING_ASCII_US );
+ return comphelper::GraphicMimeTypeHelper::GetMimeTypeForExtension( aExt );
}
- return aMimeType;
+ return OUString();
}
Graphic SvXMLGraphicHelper::ImplReadGraphic( const OUString& rPictureStorageName,
@@ -766,29 +755,32 @@ void SvXMLGraphicHelper::ImplInsertGraphicURL( const OUString& rURLStr, sal_uInt
void SvXMLGraphicHelper::Init( const uno::Reference < embed::XStorage >& rXMLStorage,
SvXMLGraphicHelperMode eCreateMode,
- bool bDirect )
+ bool bDirect,
+ const OUString& rGraphicMimeType )
{
mxRootStorage = rXMLStorage;
meCreateMode = eCreateMode;
+ maOutputMimeType = rGraphicMimeType;
mbDirect = meCreateMode != SvXMLGraphicHelperMode::Read || bDirect;
}
rtl::Reference<SvXMLGraphicHelper> SvXMLGraphicHelper::Create( const uno::Reference < embed::XStorage >& rXMLStorage,
SvXMLGraphicHelperMode eCreateMode,
- bool bDirect )
+ bool bDirect,
+ const OUString& rGraphicMimeType )
{
rtl::Reference<SvXMLGraphicHelper> pThis = new SvXMLGraphicHelper;
-
- pThis->Init( rXMLStorage, eCreateMode, bDirect );
+ pThis->Init( rXMLStorage, eCreateMode, bDirect, rGraphicMimeType );
return pThis;
}
-rtl::Reference<SvXMLGraphicHelper> SvXMLGraphicHelper::Create( SvXMLGraphicHelperMode eCreateMode )
+rtl::Reference<SvXMLGraphicHelper> SvXMLGraphicHelper::Create( SvXMLGraphicHelperMode eCreateMode,
+ const OUString& rGraphicMimeType )
{
rtl::Reference<SvXMLGraphicHelper> pThis = new SvXMLGraphicHelper;
- pThis->Init( nullptr, eCreateMode, false );
+ pThis->Init( nullptr, eCreateMode, false, rGraphicMimeType );
return pThis;
}
@@ -846,7 +838,8 @@ Reference< XInputStream > SAL_CALL SvXMLGraphicHelper::getInputStream( const OUS
if( ( SvXMLGraphicHelperMode::Write == meCreateMode ) &&
ImplGetStreamNames( rURL, aPictureStorageName, aGraphicId ) )
{
- SvXMLGraphicInputStream* pInputStream = new SvXMLGraphicInputStream( aGraphicId );
+ OUString sMimeType = comphelper::GraphicMimeTypeHelper::GetMimeTypeForExtension( OUStringToOString( maOutputMimeType, RTL_TEXTENCODING_ASCII_US ) );
+ SvXMLGraphicInputStream* pInputStream = new SvXMLGraphicInputStream( aGraphicId, sMimeType );
if( pInputStream->Exists() )
xRet = pInputStream;
diff --git a/sw/source/filter/xml/xmlexp.cxx b/sw/source/filter/xml/xmlexp.cxx
index ac6fbe5f5f6a..7d6d88940005 100644
--- a/sw/source/filter/xml/xmlexp.cxx
+++ b/sw/source/filter/xml/xmlexp.cxx
@@ -241,7 +241,7 @@ ErrCode SwXMLExport::exportDoc( enum XMLTokenEnum eClass )
rtl::Reference<SvXMLGraphicHelper> xGraphicResolver;
if( !GetGraphicResolver().is() )
{
- xGraphicResolver = SvXMLGraphicHelper::Create( SvXMLGraphicHelperMode::Write );
+ xGraphicResolver = SvXMLGraphicHelper::Create( SvXMLGraphicHelperMode::Write, GetImageFilterName() );
SetGraphicResolver( xGraphicResolver.get() );
}
diff --git a/xmloff/source/core/xmlexp.cxx b/xmloff/source/core/xmlexp.cxx
index 33cd48a9982c..f8970a58f2c2 100644
--- a/xmloff/source/core/xmlexp.cxx
+++ b/xmloff/source/core/xmlexp.cxx
@@ -2451,6 +2451,10 @@ bool SvXMLExport::SetNullDateOnUnitConverter()
return mpImpl->mbNullDateInitialized;
}
+OUString SvXMLExport::GetImageFilterName() const
+{
+ return msImgFilterName;
+}
void SvXMLElementExport::StartElement(
const sal_uInt16 nPrefixKey,
diff --git a/xmloff/source/text/txtparae.cxx b/xmloff/source/text/txtparae.cxx
index ccbb11b4d2c2..4ab345e599c3 100644
--- a/xmloff/source/text/txtparae.cxx
+++ b/xmloff/source/text/txtparae.cxx
@@ -118,6 +118,7 @@
#include <algorithm>
#include <iterator>
#include <comphelper/processfactory.hxx>
+#include <comphelper/graphicmimetype.hxx>
using namespace ::std;
using namespace ::com::sun::star;
@@ -3096,8 +3097,16 @@ void XMLTextParagraphExport::_exportTextGraphic(
sGrfFilter );
// Add mimetype to make it easier for readers to get the base64 image type right, tdf#109202
- OUString aSourceMimeType = getMimeType(sOrigURL);
- if ( !aSourceMimeType.isEmpty() )
+ // do we have a hard export image filter set? then that's our mimetype
+ OUString aSourceMimeType = GetExport().GetImageFilterName();
+ // otherwise determine mimetype from graphic
+ if ( aSourceMimeType.isEmpty() )
+ aSourceMimeType = getMimeType(sOrigURL);
+ else // !aSourceMimeType.isEmpty()
+ {
+ const OString aExt( OUStringToOString( aSourceMimeType, RTL_TEXTENCODING_ASCII_US ) );
+ aSourceMimeType = comphelper::GraphicMimeTypeHelper::GetMimeTypeForExtension( aExt );
+ }
GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, "mime-type", aSourceMimeType);
{