desktop lok, export options: allow passing through a JSON string as-is

setFormatSpecificFilterData() sets useful defaults, but the PDF export
code has the (sane) behavior of preferring FilterData over
FilterOptions, so in case we explicitly got a JSON string in
FilterOptions to in fact set FilterData, then leave FilterData empty.

Change-Id: I20e8094bf431782fe0f5d68e3ec630e1274e30c7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128970
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
This commit is contained in:
Miklos Vajna 2022-01-26 08:38:59 +01:00
parent 822b551f6c
commit d0451dcf96
3 changed files with 35 additions and 1 deletions

BIN
desktop/qa/data/3page.odg Normal file

Binary file not shown.

View file

@ -62,6 +62,7 @@
#include <cppunit/TestAssert.h>
#include <vcl/BitmapTools.hxx>
#include <vcl/pngwrite.hxx>
#include <vcl/filter/PDFiumLibrary.hxx>
#if USE_TLS_NSS
#include <nss.h>
@ -159,6 +160,7 @@ public:
void testSearchAllNotificationsCalc();
void testPaintTile();
void testSaveAs();
void testSaveAsJsonOptions();
void testSaveAsCalc();
void testPasteWriter();
void testPasteWriterJPEG();
@ -225,6 +227,7 @@ public:
CPPUNIT_TEST(testSearchAllNotificationsCalc);
CPPUNIT_TEST(testPaintTile);
CPPUNIT_TEST(testSaveAs);
CPPUNIT_TEST(testSaveAsJsonOptions);
CPPUNIT_TEST(testSaveAsCalc);
CPPUNIT_TEST(testPasteWriter);
CPPUNIT_TEST(testPasteWriterJPEG);
@ -675,6 +678,32 @@ void DesktopLOKTest::testSaveAs()
CPPUNIT_ASSERT(pDocument->pClass->saveAs(pDocument, aTempFile.GetURL().toUtf8().getStr(), "png", nullptr));
}
void DesktopLOKTest::testSaveAsJsonOptions()
{
// Given a document with 3 pages:
LibLODocument_Impl* pDocument = loadDoc("3page.odg");
// When exporting that document to PDF, skipping the first page:
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
OString aOptions("{\"PageRange\":{\"type\":\"string\",\"value\":\"2-\"}}");
CPPUNIT_ASSERT(pDocument->pClass->saveAs(pDocument, aTempFile.GetURL().toUtf8().getStr(), "pdf", aOptions.getStr()));
// Then make sure the resulting PDF has 2 pages:
SvFileStream aFile(aTempFile.GetURL(), StreamMode::READ);
SvMemoryStream aMemory;
aMemory.WriteStream(aFile);
std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();
std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument
= pPDFium->openDocument(aMemory.GetData(), aMemory.GetSize());
CPPUNIT_ASSERT(pPdfDocument);
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 2
// - Actual : 3
// i.e. FilterOptions was ignored.
CPPUNIT_ASSERT_EQUAL(2, pPdfDocument->getPageCount());
}
void DesktopLOKTest::testSaveAsCalc()
{
LibLODocument_Impl* pDocument = loadDoc("search.ods");

View file

@ -2897,7 +2897,12 @@ static int doc_saveAs(LibreOfficeKitDocument* pThis, const char* sUrl, const cha
comphelper::SequenceAsHashMap aFilterDataMap;
setFormatSpecificFilterData(sFormat, aFilterDataMap);
// If filter options is JSON string, then make sure aFilterDataMap stays empty, otherwise we
// would ignore the filter options.
if (!aFilterOptions.startsWith("{"))
{
setFormatSpecificFilterData(sFormat, aFilterDataMap);
}
if (!watermarkText.isEmpty())
aFilterDataMap["TiledWatermark"] <<= watermarkText;