summaryrefslogtreecommitdiff
path: root/filter
diff options
context:
space:
mode:
authorArmin Le Grand <Armin.Le.Grand@cib.de>2018-06-07 09:44:02 +0200
committerArmin Le Grand <Armin.Le.Grand@cib.de>2018-06-13 13:40:04 +0200
commit3087011e2eb810866276e270a6fc61a7e0998fec (patch)
tree961c9b25d00f0258dd0c3eb4850e50b78367f7c8 /filter
parent4a0ca73c31990568bf20a1c3f0906c3a5770a38e (diff)
Replace SVGFilter using SVGIO
Next step is to put more logic into the decision if Draw or Impress should be loaded - if we have a self-exported Impress, import as Impress, else (including all not-self-created SVG Graphics) import to Draw. To do this it is necessary to be able to import to different document formats at all. To do this, add an internal filter type to the filter mechanism (types/registration/...) and decide in the SVG XExtendedFilterDetection::detect from SVGFilter which one to use. Added tooling for SVG detection and more, see SVGFileInfo. This allows to detect for SVG, but also if the creator was LO and if it was Draw or Impress. The document format/filter is choosen accordingly. Corrected the error with <g visibility="hidden"> Slides inisde <g class="SlideGroup"> for import of self-exported Impress documents. No idea why this was written that way, but needs to be fixed to get a visible content at all. Also adapted the final mapping from pt to 100thmm in SvgSvgNode::decomposeSvgNode. Unfortunately (and also for unknown reasons) the self-exported Impress does not write svg:width/height values, thus the adaption from assumed svg-units (px) to 100thmm has to be skipped. Have identified the place in svgio where I can embed Pages/Slides to a helper-Primitive to later be able to 'break' such GraphicObjects to multiple Pages/Slides. I have added a Primitive called PageHierarchyPrimitive2D for this purpose. Change-Id: I38bfef6e7b16479a025fc754e38b4e21a006ad38 Reviewed-on: https://gerrit.libreoffice.org/55434 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Armin Le Grand <Armin.Le.Grand@cib.de>
Diffstat (limited to 'filter')
-rw-r--r--filter/Configuration_filter.mk3
-rwxr-xr-xfilter/source/config/fragments/filters/SVG___Scalable_Vector_Graphics_Draw.xcu30
-rw-r--r--filter/source/config/fragments/types/svg_Scalable_Vector_Graphics.xcu2
-rwxr-xr-xfilter/source/config/fragments/types/svg_Scalable_Vector_Graphics_Draw.xcu29
-rw-r--r--filter/source/svg/svgfilter.cxx260
-rw-r--r--filter/source/svg/svgfilter.hxx3
6 files changed, 262 insertions, 65 deletions
diff --git a/filter/Configuration_filter.mk b/filter/Configuration_filter.mk
index 35d7620f6f70..d7a6322e5564 100644
--- a/filter/Configuration_filter.mk
+++ b/filter/Configuration_filter.mk
@@ -695,6 +695,7 @@ $(eval $(call filter_Configuration_add_types,fcfg_langpack,fcfg_drawgraphics_typ
ppm_Portable_Pixelmap \
psd_Adobe_Photoshop \
ras_Sun_Rasterfile \
+ svg_Scalable_Vector_Graphics_Draw \
svg_Scalable_Vector_Graphics \
svm_StarView_Metafile \
tga_Truevision_TARGA \
@@ -721,6 +722,7 @@ $(eval $(call filter_Configuration_add_filters,fcfg_langpack,fcfg_drawgraphics_f
PPM___Portable_Pixelmap \
PSD___Adobe_Photoshop \
RAS___Sun_Rasterfile \
+ SVG___Scalable_Vector_Graphics_Draw \
SVG___Scalable_Vector_Graphics \
SVM___StarView_Metafile \
TGA___Truevision_TARGA \
@@ -761,6 +763,7 @@ $(eval $(call filter_Configuration_add_types,fcfg_langpack,fcfg_impressgraphics_
png_Portable_Network_Graphic \
ppm_Portable_Pixelmap \
ras_Sun_Rasterfile \
+ svg_Scalable_Vector_Graphics_Draw \
svg_Scalable_Vector_Graphics \
svm_StarView_Metafile \
tif_Tag_Image_File \
diff --git a/filter/source/config/fragments/filters/SVG___Scalable_Vector_Graphics_Draw.xcu b/filter/source/config/fragments/filters/SVG___Scalable_Vector_Graphics_Draw.xcu
new file mode 100755
index 000000000000..eb98d7f940e8
--- /dev/null
+++ b/filter/source/config/fragments/filters/SVG___Scalable_Vector_Graphics_Draw.xcu
@@ -0,0 +1,30 @@
+<!--
+ * 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 .
+-->
+ <node oor:name="SVG - Scalable Vector Graphics Draw" oor:op="replace">
+ <prop oor:name="Flags"><value>IMPORT ALIEN 3RDPARTYFILTER PREFERRED</value></prop>
+ <prop oor:name="UIComponent"/>
+ <prop oor:name="FilterService"><value>com.sun.star.comp.Draw.SVGFilter</value></prop>
+ <prop oor:name="UserData"><value></value></prop>
+ <prop oor:name="UIName">
+ <value xml:lang="en-US">SVG - Scalable Vector Graphics Draw</value>
+ </prop>
+ <prop oor:name="FileFormatVersion"><value>0</value></prop>
+ <prop oor:name="Type"><value>svg_Scalable_Vector_Graphics_Draw</value></prop>
+ <prop oor:name="TemplateName"/>
+ <prop oor:name="DocumentService"><value>com.sun.star.drawing.DrawingDocument</value></prop>
+ </node>
diff --git a/filter/source/config/fragments/types/svg_Scalable_Vector_Graphics.xcu b/filter/source/config/fragments/types/svg_Scalable_Vector_Graphics.xcu
index f811f9bb9c9e..a2015a536873 100644
--- a/filter/source/config/fragments/types/svg_Scalable_Vector_Graphics.xcu
+++ b/filter/source/config/fragments/types/svg_Scalable_Vector_Graphics.xcu
@@ -20,7 +20,7 @@
<prop oor:name="URLPattern"/>
<prop oor:name="Extensions"><value>svg svgz</value></prop>
<prop oor:name="MediaType"><value>image/svg+xml</value></prop>
- <prop oor:name="Preferred"><value>true</value></prop>
+ <prop oor:name="Preferred"><value>false</value></prop>
<prop oor:name="PreferredFilter"><value>SVG - Scalable Vector Graphics</value></prop>
<prop oor:name="UIName">
<value>SVG - Scalable Vector Graphics</value>
diff --git a/filter/source/config/fragments/types/svg_Scalable_Vector_Graphics_Draw.xcu b/filter/source/config/fragments/types/svg_Scalable_Vector_Graphics_Draw.xcu
new file mode 100755
index 000000000000..4b3b9cab58b1
--- /dev/null
+++ b/filter/source/config/fragments/types/svg_Scalable_Vector_Graphics_Draw.xcu
@@ -0,0 +1,29 @@
+<!--
+ * 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 .
+-->
+ <node oor:name="svg_Scalable_Vector_Graphics_Draw" oor:op="replace" >
+ <prop oor:name="DetectService"><value>com.sun.star.comp.Draw.SVGFilter</value></prop>
+ <prop oor:name="URLPattern"/>
+ <prop oor:name="Extensions"><value>svg svgz</value></prop>
+ <prop oor:name="MediaType"><value>image/svg+xml</value></prop>
+ <prop oor:name="Preferred"><value>true</value></prop>
+ <prop oor:name="PreferredFilter"><value>SVG - Scalable Vector Graphics Draw</value></prop>
+ <prop oor:name="UIName">
+ <value>SVG - Scalable Vector Graphics Draw</value>
+ </prop>
+ <prop oor:name="ClipboardFormat"/>
+ </node>
diff --git a/filter/source/svg/svgfilter.cxx b/filter/source/svg/svgfilter.cxx
index 1ec09f65ab05..ddc81b4f89e5 100644
--- a/filter/source/svg/svgfilter.cxx
+++ b/filter/source/svg/svgfilter.cxx
@@ -64,6 +64,7 @@ using namespace ::com::sun::star;
namespace
{
+ static const char constFilterNameDraw[] = "svg_Scalable_Vector_Graphics_Draw";
static const char constFilterName[] = "svg_Scalable_Vector_Graphics";
}
@@ -254,19 +255,22 @@ sal_Bool SAL_CALL SVGFilter::filter( const Sequence< PropertyValue >& rDescripto
// pTargetSdrPage->SetBorder(...) and
// pTargetSdrPage->SetSize(...),
// but ::adaptSizeAndBorderForAllPages
+ // Do use original Size and borders to get as close to original
+ // as possible for better turn-arounds.
pTargetSdrPage->getSdrModelFromSdrPage().adaptSizeAndBorderForAllPages(
Size(
- aGraphicSize.Width() + nAllBorder + nAllBorder,
- aGraphicSize.Height() + nAllBorder + nAllBorder),
+ aGraphicSize.Width(),
+ aGraphicSize.Height()),
nAllBorder,
nAllBorder,
nAllBorder,
nAllBorder);
- // set pos/size at SdrGraphicObj - add offset to PageBorder
+ // set pos/size at SdrGraphicObj - use zero position for
+ // better turn-around results
aNewSdrGrafObj->SetSnapRect(
tools::Rectangle(
- Point(nAllBorder, nAllBorder),
+ Point(0, 0),
aGraphicSize));
// insert to page (owner change of SdrGrafObj)
@@ -472,86 +476,220 @@ void SAL_CALL SVGFilter::setTargetDocument( const Reference< XComponent >& xDoc
mxDstDoc = xDoc;
}
-bool SVGFilter::isStreamGZip(const uno::Reference<io::XInputStream>& xInput)
+// There is already another SVG-Type_Detector, see
+// vcl/source/filter/graphicfilter.cxx ("DOCTYPE svg"),
+// but since these start from different preconditions it is not
+// easy to unify these. For now, use this local helper.
+class SVGFileInfo
{
- uno::Reference<io::XSeekable> xSeek(xInput, uno::UNO_QUERY);
- if(xSeek.is())
- xSeek->seek(0);
-
- uno::Sequence<sal_Int8> aBuffer(2);
- const sal_uInt64 nBytes = xInput->readBytes(aBuffer, 2);
- if (nBytes == 2)
+private:
+ const uno::Reference<io::XInputStream>& mxInput;
+ uno::Sequence< sal_Int8 > mnFirstBytes;
+ sal_Int32 mnFirstBytesSize;
+ sal_uInt64 mnFirstRead;
+ bool mbProcessed;
+ bool mbIsSVG;
+
+ bool impCheckForMagic(
+ const sal_Int8* pMagic,
+ const sal_Int32 nMagicSize)
{
- const sal_Int8* pBuffer = aBuffer.getConstArray();
- if (pBuffer[0] == 0x1F && static_cast<sal_uInt8>(pBuffer[1]) == 0x8B)
- return true;
+ const sal_Int8* pBuffer(mnFirstBytes.getConstArray());
+ return std::search(
+ pBuffer,
+ pBuffer + mnFirstRead,
+ pMagic,
+ pMagic + nMagicSize) != pBuffer + mnFirstRead;
}
- return false;
-}
-bool SVGFilter::isStreamSvg(const uno::Reference<io::XInputStream>& xInput)
-{
- uno::Reference<io::XSeekable> xSeek(xInput, uno::UNO_QUERY);
- if(xSeek.is())
- xSeek->seek(0);
+ void impEnsureProcessed()
+ {
+ if(mbProcessed)
+ {
+ return;
+ }
- const sal_Int32 nLookAhead = 1024;
- uno::Sequence<sal_Int8> aBuffer(nLookAhead);
- const sal_uInt64 nBytes = xInput->readBytes(aBuffer, nLookAhead);
- const sal_Int8* pBuffer = aBuffer.getConstArray();
+ mbProcessed = true;
- sal_Int8 aMagic1[] = {'<', 's', 'v', 'g'};
- sal_Int32 const aMagic1Size = SAL_N_ELEMENTS(aMagic1);
+ if(!mxInput.is())
+ {
+ return;
+ }
- if (std::search(pBuffer, pBuffer + nBytes, aMagic1, aMagic1 + aMagic1Size) != pBuffer + nBytes )
- return true;
+ if(0 == mnFirstBytesSize)
+ {
+ return;
+ }
- sal_Int8 aMagic2[] = {'D', 'O', 'C', 'T', 'Y', 'P', 'E', ' ', 's', 'v', 'g'};
- sal_Int32 const aMagic2Size = SAL_N_ELEMENTS(aMagic2);
+ mnFirstBytes.realloc(mnFirstBytesSize);
- return std::search(pBuffer, pBuffer + nBytes, aMagic2, aMagic2 + aMagic2Size) != pBuffer + nBytes;
-}
+ if(mnFirstBytesSize != mnFirstBytes.getLength())
+ {
+ return;
+ }
-OUString SAL_CALL SVGFilter::detect(Sequence<PropertyValue>& rDescriptor)
-{
- utl::MediaDescriptor aMediaDescriptor(rDescriptor);
- uno::Reference<io::XInputStream> xInput(aMediaDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()], UNO_QUERY);
+ std::unique_ptr< SvStream > aStream(utl::UcbStreamHelper::CreateStream(mxInput, true));
- if (!xInput.is())
- return OUString();
+ if(!aStream.get())
+ {
+ return;
+ }
+
+ const sal_uLong nStreamPos(aStream->Tell());
+ aStream->Seek(STREAM_SEEK_TO_END);
+ const sal_uLong nStreamLen(aStream->Tell() - nStreamPos);
+ aStream->Seek(nStreamPos);
- try {
- if (isStreamGZip(xInput))
+ if(aStream->GetError())
{
- std::unique_ptr<SvStream> aStream(utl::UcbStreamHelper::CreateStream(xInput, true ));
- if(!aStream.get())
- return OUString();
+ return;
+ }
+
+ mnFirstRead = aStream->ReadBytes(
+ &mnFirstBytes[0],
+ std::min(nStreamLen, sal_uLong(mnFirstBytesSize)));
- SvStream* pMemoryStream = new SvMemoryStream;
- uno::Reference<io::XSeekable> xSeek(xInput, uno::UNO_QUERY);
- if (!xSeek.is())
- return OUString();
- xSeek->seek(0);
+ if(aStream->GetError())
+ {
+ return;
+ }
+ // check if it is gzipped -> svgz
+ if(mnFirstBytes[0] == 0x1F && static_cast<sal_uInt8>(mnFirstBytes[1]) == 0x8B)
+ {
ZCodec aCodec;
- aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, false, true);
- aCodec.Decompress(*aStream.get(), *pMemoryStream);
+
+ aCodec.BeginCompression(
+ ZCODEC_DEFAULT_COMPRESSION,
+ false,
+ true);
+ mnFirstRead = aCodec.Read(
+ *aStream,
+ reinterpret_cast< sal_uInt8* >(mnFirstBytes.getArray()),
+ mnFirstBytesSize);
aCodec.EndCompression();
- pMemoryStream->Seek(STREAM_SEEK_TO_BEGIN);
- uno::Reference<io::XInputStream> xDecompressedInput(new utl::OSeekableInputStreamWrapper(pMemoryStream, true));
+ }
+
+ if(!mbIsSVG)
+ {
+ const sal_Int8 aMagic[] = {'<', 's', 'v', 'g'};
+ const sal_Int32 nMagicSize(SAL_N_ELEMENTS(aMagic));
- if (xDecompressedInput.is() && isStreamSvg(xDecompressedInput))
- return OUString(constFilterName);
+ mbIsSVG = impCheckForMagic(aMagic, nMagicSize);
}
- else
+
+ if(!mbIsSVG)
{
- if (isStreamSvg(xInput))
- return OUString(constFilterName);
+ const sal_Int8 aMagic[] = {'D', 'O', 'C', 'T', 'Y', 'P', 'E', ' ', 's', 'v', 'g'};
+ const sal_Int32 nMagicSize(SAL_N_ELEMENTS(aMagic));
+
+ mbIsSVG = impCheckForMagic(aMagic, nMagicSize);
}
- } catch (css::io::IOException & e) {
+
+ return;
+ }
+
+public:
+ SVGFileInfo(
+ const uno::Reference<io::XInputStream>& xInput,
+ sal_Int32 nFirstBytesSize = 4096)
+ : mxInput(xInput),
+ mnFirstBytes(),
+ mnFirstBytesSize(nFirstBytesSize),
+ mnFirstRead(0),
+ mbProcessed(false),
+ mbIsSVG(false)
+ {
+ // For the default buffer size: Use not too big
+ // (not more than 16K), but also not too small
+ // (not less than 1/2K), see comments at
+ // ImpPeekGraphicFormat, SVG section.
+ // I remember these cases and it *can* happen
+ // that SVGs have quite massive comments in their
+ // headings (!)
+ // Limit to plausible sizes, also for security reasons
+ mnFirstBytesSize = std::min(sal_Int32(512), mnFirstBytesSize);
+ mnFirstBytesSize = std::max(sal_Int32(16384), mnFirstBytesSize);
+ }
+
+ bool isSVG()
+ {
+ impEnsureProcessed();
+
+ return mbIsSVG;
+ }
+
+ bool isOwnFormat()
+ {
+ impEnsureProcessed();
+
+ if(mbIsSVG)
+ {
+ // xmlns:ooo
+ const sal_Int8 aMagic[] = {'x', 'm', 'l', 'n', 's', ':', 'o', 'o', 'o'};
+ const sal_Int32 nMagicSize(SAL_N_ELEMENTS(aMagic));
+
+ return impCheckForMagic(aMagic, nMagicSize);
+ }
+
+ return false;
+ }
+
+ bool isImpress()
+ {
+ impEnsureProcessed();
+
+ if(mbIsSVG)
+ {
+ // ooo:meta_slides
+ const sal_Int8 aMagic[] = {'o', 'o', 'o', ':', 'm', 'e', 't', 'a', '_', 's', 'l', 'i', 'd', 'e', 's'};
+ const sal_Int32 nMagicSize(SAL_N_ELEMENTS(aMagic));
+
+ return impCheckForMagic(aMagic, nMagicSize);
+ }
+
+ return false;
+ }
+};
+
+OUString SAL_CALL SVGFilter::detect(Sequence<PropertyValue>& rDescriptor)
+{
+ utl::MediaDescriptor aMediaDescriptor(rDescriptor);
+ uno::Reference<io::XInputStream> xInput(aMediaDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()], UNO_QUERY);
+ OUString aRetval;
+
+ if (!xInput.is())
+ {
+ return aRetval;
+ }
+
+ try
+ {
+ SVGFileInfo aSVGFileInfo(xInput, 2048);
+
+ if(aSVGFileInfo.isSVG())
+ {
+ // We have SVG - set default document format to Draw
+ aRetval = OUString(constFilterNameDraw);
+
+ if(aSVGFileInfo.isOwnFormat())
+ {
+ // it's a file that was written/exported by LO
+ if(aSVGFileInfo.isImpress())
+ {
+ // it was written by Impress export. Set document
+ // format for import to Impress
+ aRetval = OUString(constFilterName);
+ }
+ }
+ }
+ }
+ catch (css::io::IOException & e)
+ {
SAL_WARN("filter.svg", "caught " << e);
}
- return OUString();
+
+ return aRetval;
}
#define SVG_FILTER_IMPL_NAME "com.sun.star.comp.Draw.SVGFilter"
diff --git a/filter/source/svg/svgfilter.hxx b/filter/source/svg/svgfilter.hxx
index 5f117c3310ee..1b5254e25469 100644
--- a/filter/source/svg/svgfilter.hxx
+++ b/filter/source/svg/svgfilter.hxx
@@ -266,9 +266,6 @@ private:
const Reference< XPropertySetInfo > & rxPropSetInfo );
DECL_LINK( CalcFieldHdl, EditFieldInfo*, void );
- static bool isStreamGZip(const css::uno::Reference<css::io::XInputStream>& xInput);
- static bool isStreamSvg(const css::uno::Reference<css::io::XInputStream>& xInput);
-
protected:
// XFilter