integrate libzmf

Change-Id: I0c7ea5b56ea4ed4839ff38798c0b915aaca81774
Reviewed-on: https://gerrit.libreoffice.org/26574
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: David Tardon <dtardon@redhat.com>
diff --git a/Makefile.fetch b/Makefile.fetch
index 3884f7f..e78ad98 100644
--- a/Makefile.fetch
+++ b/Makefile.fetch
@@ -209,6 +209,7 @@ $(WORKDIR)/download: $(BUILDDIR)/config_host.mk $(SRCDIR)/download.lst $(SRCDIR)
		$(call fetch_Optional,WPS,WPS_TARBALL) \
		$(call fetch_Optional,XSLTML,XSLTML_TARBALL) \
		$(call fetch_Optional,ZLIB,ZLIB_TARBALL) \
		$(call fetch_Optional,ZMF,ZMF_TARBALL) \
	,$(call fetch_Download_item,http://dev-www.libreoffice.org/src,$(item)))
	$(foreach item, \
		$(call fetch_Optional,DBGHELP,DBGHELP_DLL) \
diff --git a/RepositoryExternal.mk b/RepositoryExternal.mk
index 62d3bfd..04546e5 100644
--- a/RepositoryExternal.mk
+++ b/RepositoryExternal.mk
@@ -2001,6 +2001,39 @@ endef
endif # SYSTEM_PAGEMAKER


ifneq ($(SYSTEM_ZMF),)

define gb_LinkTarget__use_zmf
$(call gb_LinkTarget_set_include,$(1),\
	$$(INCLUDE) \
    $(ZMF_CFLAGS) \
)
$(call gb_LinkTarget_add_libs,$(1),$(ZMF_LIBS))

endef
gb_ExternalProject__use_zmf :=

else # !SYSTEM_ZMF

define gb_LinkTarget__use_zmf
$(call gb_LinkTarget_set_include,$(1),\
	-I$(call gb_UnpackedTarball_get_dir,libzmf)/inc \
	$$(INCLUDE) \
)
$(call gb_LinkTarget_add_libs,$(1),\
	$(call gb_UnpackedTarball_get_dir,libzmf)/src/lib/.libs/libzmf-0.0$(gb_StaticLibrary_PLAINEXT) \
)
$(call gb_LinkTarget_use_external_project,$(1),libzmf)

endef
define gb_ExternalProject__use_zmf
$(call gb_ExternalProject_use_external_project,$(1),libzmf)

endef

endif # SYSTEM_ZMF


ifneq ($(SYSTEM_VISIO),)

define gb_LinkTarget__use_visio
diff --git a/config_host.mk.in b/config_host.mk.in
index 620df66..04613e1 100644
--- a/config_host.mk.in
+++ b/config_host.mk.in
@@ -585,6 +585,7 @@ export SYSTEM_WPD=@SYSTEM_WPD@
export SYSTEM_WPG=@SYSTEM_WPG@
export SYSTEM_WPS=@SYSTEM_WPS@
export SYSTEM_ZLIB=@SYSTEM_ZLIB@
export SYSTEM_ZMF=@SYSTEM_ZMF@
export TARFILE_LOCATION=@TARFILE_LOCATION@
export TDE_CFLAGS=$(gb_SPACE)@TDE_CFLAGS@
export TDE_LIBS=$(gb_SPACE)@TDE_LIBS@
@@ -655,6 +656,8 @@ export XRENDER_LIBS=$(gb_SPACE)@XRENDER_LIBS@
export XSLTPROC=@XSLTPROC@
export ZLIB_CFLAGS=$(gb_SPACE)@ZLIB_CFLAGS@
export ZLIB_LIBS=$(gb_SPACE)@ZLIB_LIBS@
export ZMF_CFLAGS=$(gb_SPACE)@ZMF_CFLAGS@
export ZMF_LIBS=$(gb_SPACE)@ZMF_LIBS@

# lang-related stuff
include $(BUILDDIR)/config_$(gb_Side)_lang.mk
diff --git a/configure.ac b/configure.ac
index 590e326..c5e484b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7873,6 +7873,11 @@ dnl ===================================================================
libo_CHECK_SYSTEM_MODULE([libpagemaker],[PAGEMAKER],[libpagemaker-0.0])

dnl ===================================================================
dnl Check for system libzmf
dnl ===================================================================
libo_CHECK_SYSTEM_MODULE([libzmf],[ZMF],[libzmf-0.0])

dnl ===================================================================
dnl Check for system libvisio
dnl ===================================================================
libo_CHECK_SYSTEM_MODULE([libvisio],[VISIO],[libvisio-0.1])
diff --git a/download.lst b/download.lst
index 1993f0f..7741265 100644
--- a/download.lst
+++ b/download.lst
@@ -164,6 +164,8 @@ export WPS_TARBALL := libwps-0.4.$(WPS_VERSION_MICRO).tar.bz2
export XSLTML_TARBALL := a7983f859eafb2677d7ff386a023bc40-xsltml_2.1.2.zip
export ZLIB_MD5SUM := 44d667c142d7cda120332623eab69f40
export ZLIB_TARBALL := zlib-1.2.8.tar.gz
export ZMF_MD5SUM := b64a7780bfef91e0a953f436366b2047
export ZMF_TARBALL := libzmf-0.0.0.tar.bz2

export MOZ_ZIP_INC := $(OS)$(COM)$(if $(filter INTEL,$(CPUNAME)),I,X)inc.zip
export MOZ_ZIP_LIB := $(OS)$(COM)$(if $(filter INTEL,$(CPUNAME)),I,X)lib.zip
diff --git a/external/Module_external.mk b/external/Module_external.mk
index 44280e3..9c014a7e 100644
--- a/external/Module_external.mk
+++ b/external/Module_external.mk
@@ -86,6 +86,7 @@ $(eval $(call gb_Module_add_moduledirs,external,\
	$(call gb_Helper_optional,ORCUS,liborcus) \
	$(call gb_Helper_optional,OWNCLOUD_ANDROID_LIB,owncloud-android-lib) \
	$(call gb_Helper_optional,PAGEMAKER,libpagemaker) \
	$(call gb_Helper_optional,ZMF,libzmf) \
	$(call gb_Helper_optional,POPPLER,poppler) \
	$(call gb_Helper_optional,POSTGRESQL,postgresql) \
	$(call gb_Helper_optional,PYTHON,$(if $(filter $(PYTHON_VERSION_MINOR),3),python33,python3)) \
diff --git a/external/libzmf/ExternalProject_libzmf.mk b/external/libzmf/ExternalProject_libzmf.mk
new file mode 100644
index 0000000..713d55d
--- /dev/null
+++ b/external/libzmf/ExternalProject_libzmf.mk
@@ -0,0 +1,43 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# 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/.
#

$(eval $(call gb_ExternalProject_ExternalProject,libzmf))

$(eval $(call gb_ExternalProject_use_autoconf,libzmf,build))

$(eval $(call gb_ExternalProject_register_targets,libzmf,\
	build \
))

$(eval $(call gb_ExternalProject_use_externals,libzmf,\
	boost_headers \
	revenge \
))

$(call gb_ExternalProject_get_state_target,libzmf,build) :
	$(call gb_ExternalProject_run,build,\
		export PKG_CONFIG="" \
		&& MAKE=$(MAKE) ./configure \
			--with-pic \
			--enable-static \
			--disable-shared \
			--without-docs \
			--disable-tools \
			--disable-debug \
			--disable-werror \
			--disable-weffc \
			$(if $(verbose),--disable-silent-rules,--enable-silent-rules) \
			CXXFLAGS="$(CXXFLAGS) $(CXXFLAGS_CXX11) $(if $(SYSTEM_BOOST),$(BOOST_CPPFLAGS),-I$(call gb_UnpackedTarball_get_dir,boost))" \
			REVENGE_GENERATORS_CFLAGS=' ' REVENGE_GENERATORS_LIBS=' ' REVENGE_STREAM_CFLAGS=' ' REVENGE_STREAM_LIBS=' ' \
			ax_cv_cxx_compile_cxx11=yes \
			$(if $(CROSS_COMPILING),--build=$(BUILD_PLATFORM) --host=$(HOST_PLATFORM)) \
		&& $(MAKE) -C src/lib \
	)

# vim: set noet sw=4 ts=4:
diff --git a/external/libzmf/Makefile b/external/libzmf/Makefile
new file mode 100644
index 0000000..e4968cf8
--- /dev/null
+++ b/external/libzmf/Makefile
@@ -0,0 +1,7 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-

module_directory:=$(dir $(realpath $(firstword $(MAKEFILE_LIST))))

include $(module_directory)/../../solenv/gbuild/partial_build.mk

# vim: set noet sw=4 ts=4:
diff --git a/external/libzmf/Module_libzmf.mk b/external/libzmf/Module_libzmf.mk
new file mode 100644
index 0000000..c024e69
--- /dev/null
+++ b/external/libzmf/Module_libzmf.mk
@@ -0,0 +1,17 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# 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/.
#

$(eval $(call gb_Module_Module,libzmf))

$(eval $(call gb_Module_add_targets,libzmf,\
	ExternalProject_libzmf \
	UnpackedTarball_libzmf \
))

# vim: set noet sw=4 ts=4:
diff --git a/external/libzmf/README b/external/libzmf/README
new file mode 100644
index 0000000..26e05fc
--- /dev/null
+++ b/external/libzmf/README
@@ -0,0 +1,6 @@
libzmf is a library and a set of tools for reading and converting
Zoner Draw and Zebra file formats.

libzmf requires boost and librevenge to build.

The library is available under MPL 2.0+ license.
diff --git a/external/libzmf/UnpackedTarball_libzmf.mk b/external/libzmf/UnpackedTarball_libzmf.mk
new file mode 100644
index 0000000..3246384
--- /dev/null
+++ b/external/libzmf/UnpackedTarball_libzmf.mk
@@ -0,0 +1,24 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# 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/.
#

$(eval $(call gb_UnpackedTarball_UnpackedTarball,libzmf))

$(eval $(call gb_UnpackedTarball_set_tarball,libzmf,$(ZMF_TARBALL)))

$(eval $(call gb_UnpackedTarball_set_patchlevel,libzmf,0))

ifeq ($(COM_IS_CLANG),TRUE)
ifneq ($(filter -fsanitize=%,$(CC)),)
$(eval $(call gb_UnpackedTarball_add_patches,libzmf, \
    external/libzmf/ubsan-visibility.patch \
))
endif
endif

# vim: set noet sw=4 ts=4:
diff --git a/external/libzmf/ubsan-visibility.patch b/external/libzmf/ubsan-visibility.patch
new file mode 100644
index 0000000..90ead2b
--- /dev/null
+++ b/external/libzmf/ubsan-visibility.patch
@@ -0,0 +1,11 @@
--- configure
+++ configure
@@ -16787,7 +16787,7 @@
         { $as_echo "$as_me:${as_lineno-$LINENO}: checking for -fvisibility=hidden compiler flag" >&5
 $as_echo_n "checking for -fvisibility=hidden compiler flag... " >&6; }
         saved_CXXFLAGS="$CXXFLAGS"
-        CXXFLAGS="$CXXFLAGS -fvisibility=hidden"
+        CXXFLAGS="$CXXFLAGS -fvisibility-ms-compat"
         cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
 
diff --git a/filter/Configuration_filter.mk b/filter/Configuration_filter.mk
index a17a00c..ef895de 100644
--- a/filter/Configuration_filter.mk
+++ b/filter/Configuration_filter.mk
@@ -547,6 +547,7 @@ $(eval $(call filter_Configuration_add_types,fcfg_langpack,fcfg_draw_types.xcu,f
	draw_Visio_Document \
	draw_ClarisWorks \
	draw_PageMaker_Document \
	draw_ZMF_Document \
	MWAW_Bitmap \
	MWAW_Drawing \
))
@@ -566,6 +567,7 @@ $(eval $(call filter_Configuration_add_filters,fcfg_langpack,fcfg_draw_filters.x
	FreehandDocument \
	ClarisWorks_Draw \
	PageMakerDocument \
	ZMFDocument \
	MWAW_Bitmap \
	MWAW_Drawing \
))
diff --git a/filter/source/config/fragments/filters/ZMFDocument.xcu b/filter/source/config/fragments/filters/ZMFDocument.xcu
new file mode 100644
index 0000000..b72449d
--- /dev/null
+++ b/filter/source/config/fragments/filters/ZMFDocument.xcu
@@ -0,0 +1,13 @@
    <node oor:name="ZMF Document" oor:op="replace">
        <prop oor:name="Flags"><value>IMPORT ALIEN 3RDPARTYFILTER PREFERRED</value></prop>
        <prop oor:name="UIComponent"/>
        <prop oor:name="FilterService"><value>org.libreoffice.comp.Draw.ZMFImportFilter</value></prop>
        <prop oor:name="UserData"/>
        <prop oor:name="UIName">
            <value xml:lang="en-US">Zoner Callisto/Draw</value>
        </prop>
        <prop oor:name="FileFormatVersion"><value>0</value></prop>
        <prop oor:name="Type"><value>draw_ZMF_Document</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/draw_ZMF_Document.xcu b/filter/source/config/fragments/types/draw_ZMF_Document.xcu
new file mode 100644
index 0000000..ba67d5b
--- /dev/null
+++ b/filter/source/config/fragments/types/draw_ZMF_Document.xcu
@@ -0,0 +1,12 @@
        <node oor:name="draw_ZMF_Document" oor:op="replace" >
            <prop oor:name="DetectService"><value>org.libreoffice.comp.Draw.ZMFImportFilter</value></prop>
            <prop oor:name="URLPattern"/>
            <prop oor:name="Extensions"><value>zmf</value></prop>
            <prop oor:name="MediaType"/>
            <prop oor:name="Preferred"><value>true</value></prop>
            <prop oor:name="PreferredFilter"><value>ZMF Document</value></prop>
            <prop oor:name="UIName">
                <value>Zoner Callisto/Draw</value>
            </prop>
            <prop oor:name="ClipboardFormat"/>
        </node>
diff --git a/writerperfect/Library_wpftdraw.mk b/writerperfect/Library_wpftdraw.mk
index b6927db..cdec029 100644
--- a/writerperfect/Library_wpftdraw.mk
+++ b/writerperfect/Library_wpftdraw.mk
@@ -56,6 +56,7 @@ $(eval $(call gb_Library_use_externals,wpftdraw,\
	wpg \
	wpd \
	zlib \
	zmf \
	lcms2 \
	libxml2 \
	icui18n \
@@ -71,6 +72,7 @@ $(eval $(call gb_Library_add_exception_objects,wpftdraw,\
	writerperfect/source/draw/PageMakerImportFilter \
	writerperfect/source/draw/VisioImportFilter \
	writerperfect/source/draw/WPGImportFilter \
	writerperfect/source/draw/ZMFImportFilter \
))

# vim: set noet sw=4 ts=4:
diff --git a/writerperfect/source/draw/ZMFImportFilter.cxx b/writerperfect/source/draw/ZMFImportFilter.cxx
new file mode 100644
index 0000000..0eabfc1
--- /dev/null
+++ b/writerperfect/source/draw/ZMFImportFilter.cxx
@@ -0,0 +1,69 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ZMFImportFilter: Sets up the filter, and calls OdgExporter
 * to do the actual filtering
 *
 * 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/.
 */

#include <libodfgen/libodfgen.hxx>

#include <libzmf/libzmf.h>

#include <com/sun/star/uno/XComponentContext.hpp>

#include <cppuhelper/supportsservice.hxx>

#include "ZMFImportFilter.hxx"

using com::sun::star::uno::RuntimeException;
using com::sun::star::uno::Sequence;
using com::sun::star::uno::XComponentContext;
using com::sun::star::uno::XInterface;

bool ZMFImportFilter::doImportDocument(librevenge::RVNGInputStream &rInput, OdgGenerator &rGenerator, utl::MediaDescriptor &)
{
    return libzmf::ZMFDocument::parse(&rInput, &rGenerator);
}

bool ZMFImportFilter::doDetectFormat(librevenge::RVNGInputStream &rInput, OUString &rTypeName)
{
    if (libzmf::ZMFDocument::isSupported(&rInput))
    {
        rTypeName = "draw_ZMF_Document";
        return true;
    }

    return false;
}

// XServiceInfo
OUString SAL_CALL ZMFImportFilter::getImplementationName()
throw (RuntimeException, std::exception)
{
    return OUString("org.libreoffice.comp.Draw.ZMFImportFilter");
}

sal_Bool SAL_CALL ZMFImportFilter::supportsService(const OUString &rServiceName)
throw (RuntimeException, std::exception)
{
    return cppu::supportsService(this, rServiceName);
}

Sequence< OUString > SAL_CALL ZMFImportFilter::getSupportedServiceNames()
throw (RuntimeException, std::exception)
{
    return Sequence< OUString >{"com.sun.star.document.ImportFilter", "com.sun.star.document.ExtendedTypeDetection"};
}

extern "C"
SAL_DLLPUBLIC_EXPORT css::uno::XInterface *SAL_CALL
org_libreoffice_comp_Draw_ZMFImportFilter_get_implementation(
    css::uno::XComponentContext *const context,
    const css::uno::Sequence<css::uno::Any> &)
{
    return cppu::acquire(new ZMFImportFilter(context));
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/writerperfect/source/draw/ZMFImportFilter.hxx b/writerperfect/source/draw/ZMFImportFilter.hxx
new file mode 100644
index 0000000..a5eb9f2
--- /dev/null
+++ b/writerperfect/source/draw/ZMFImportFilter.hxx
@@ -0,0 +1,41 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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_WRITERPERFECT_SOURCE_DRAW_ZMFIMPORTFILTER_HXX
#define INCLUDED_WRITERPERFECT_SOURCE_DRAW_ZMFIMPORTFILTER_HXX

#include "ImportFilter.hxx"

#include "DocumentHandlerForOdg.hxx"

/* This component will be instantiated for both import or export. Whether it calls
 * setSourceDocument or setTargetDocument determines which Impl function the filter
 * member calls */
class ZMFImportFilter : public writerperfect::ImportFilter<OdgGenerator>
{
public:
    explicit ZMFImportFilter(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
        : writerperfect::ImportFilter<OdgGenerator>(rxContext)
    {
    }

    // XServiceInfo
    virtual OUString SAL_CALL getImplementationName()
    throw (css::uno::RuntimeException, std::exception) override;
    virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName)
    throw (css::uno::RuntimeException, std::exception) override;
    virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
    throw (css::uno::RuntimeException, std::exception) override;

private:
    virtual bool doDetectFormat(librevenge::RVNGInputStream &rInput, OUString &rTypeName) override;
    virtual bool doImportDocument(librevenge::RVNGInputStream &rInput, OdgGenerator &rGenerator, utl::MediaDescriptor &) override;
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/writerperfect/source/draw/wpftdraw.component b/writerperfect/source/draw/wpftdraw.component
index c153539..c315fa9 100644
--- a/writerperfect/source/draw/wpftdraw.component
+++ b/writerperfect/source/draw/wpftdraw.component
@@ -49,4 +49,9 @@
    <service name="com.sun.star.document.ImportFilter"/>
    <service name="com.sun.star.document.ExtendedTypeDetection"/>
  </implementation>
  <implementation name="org.libreoffice.comp.Draw.ZMFImportFilter"
      constructor="org_libreoffice_comp_Draw_ZMFImportFilter_get_implementation">
    <service name="com.sun.star.document.ImportFilter"/>
    <service name="com.sun.star.document.ExtendedTypeDetection"/>
  </implementation>
</component>