From 642f6386a1a73c13ed3997f54a737385e8894992 Mon Sep 17 00:00:00 2001 From: Mihai Varga Date: Mon, 17 Aug 2015 18:49:40 +0300 Subject: [PATCH 095/398] lok::Document getStyles method This method returns a JSON mapping of style families to a list of styles from the corresponding family. Will be used to know and apply styles in tiledrendering. Change-Id: I0aa395c40b9573920ade44255f97c077475ae5f1 (cherry picked from commit c5a516bd1bf0216ee39f31322369f6bffdf464eb) --- desktop/source/lib/init.cxx | 34 ++++++++++++++++++++++++++++++ include/LibreOfficeKit/LibreOfficeKit.h | 3 +++ include/LibreOfficeKit/LibreOfficeKit.hxx | 8 +++++++ libreofficekit/qa/unit/tiledrendering.cxx | 35 +++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index abd8ca0b640b..51302d1f8466 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -35,11 +35,13 @@ #include #include +#include #include #include #include #include #include +#include #include #include @@ -233,6 +235,7 @@ static void doc_setGraphicSelection (LibreOfficeKitDocument* pThis, int nX, int nY); static void doc_resetSelection (LibreOfficeKitDocument* pThis); +static char* doc_getStyles(LibreOfficeKitDocument* pThis); struct LibLODocument_Impl : public _LibreOfficeKitDocument { @@ -267,6 +270,7 @@ struct LibLODocument_Impl : public _LibreOfficeKitDocument m_pDocumentClass->getTextSelection = doc_getTextSelection; m_pDocumentClass->setGraphicSelection = doc_setGraphicSelection; m_pDocumentClass->resetSelection = doc_resetSelection; + m_pDocumentClass->getStyles = doc_getStyles; gDocumentClass = m_pDocumentClass; } @@ -864,6 +868,36 @@ static void doc_resetSelection(LibreOfficeKitDocument* pThis) pDoc->resetSelection(); } +static char* doc_getStyles(LibreOfficeKitDocument* pThis) +{ + LibLODocument_Impl* pDocument = static_cast(pThis); + + boost::property_tree::ptree aTree; + uno::Reference xStyleFamiliesSupplier(pDocument->mxComponent, uno::UNO_QUERY); + uno::Reference xStyleFamilies(xStyleFamiliesSupplier->getStyleFamilies(), uno::UNO_QUERY); + uno::Sequence aStyleFamilies = xStyleFamilies->getElementNames(); + + for (sal_Int32 nStyleFam = 0; nStyleFam < aStyleFamilies.getLength(); ++nStyleFam) + { + boost::property_tree::ptree aChildren; + OUString sStyleFam = aStyleFamilies[nStyleFam]; + uno::Reference xStyleFamily(xStyleFamilies->getByName(sStyleFam), uno::UNO_QUERY); + uno::Sequence aStyles = xStyleFamily->getElementNames(); + for (sal_Int32 nInd = 0; nInd < aStyles.getLength(); ++nInd) + { + boost::property_tree::ptree aChild; + aChild.put("", aStyles[nInd]); + aChildren.push_back(std::make_pair("", aChild)); + } + aTree.add_child(sStyleFam.toUtf8().getStr(), aChildren); + } + std::stringstream aStream; + boost::property_tree::write_json(aStream, aTree); + char* pJson = static_cast(malloc(aStream.str().size() + 1)); + strcpy(pJson, aStream.str().c_str()); + pJson[aStream.str().size()] = '\0'; + return pJson; +} static char* lo_getError (LibreOfficeKit *pThis) { LibLibreOffice_Impl* pLib = static_cast(pThis); diff --git a/include/LibreOfficeKit/LibreOfficeKit.h b/include/LibreOfficeKit/LibreOfficeKit.h index e3b485052444..af7155c4db67 100644 --- a/include/LibreOfficeKit/LibreOfficeKit.h +++ b/include/LibreOfficeKit/LibreOfficeKit.h @@ -159,6 +159,9 @@ struct _LibreOfficeKitDocumentClass /// @see lok::Document::resetSelection void (*resetSelection) (LibreOfficeKitDocument* pThis); + + /// @see lok::Document:getStyles + char* (*getStyles) (LibreOfficeKitDocument* pThis); #endif // LOK_USE_UNSTABLE_API }; diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx index 816ade5649b2..c526bda95593 100644 --- a/include/LibreOfficeKit/LibreOfficeKit.hxx +++ b/include/LibreOfficeKit/LibreOfficeKit.hxx @@ -246,6 +246,14 @@ public: { mpDoc->pClass->resetSelection(mpDoc); } + + /** + * Returns a json map, {"familyName1" : ["list of style names in the family1"], etc.} + */ + inline char* getStyles() + { + return mpDoc->pClass->getStyles(mpDoc); + } #endif // LOK_USE_UNSTABLE_API }; diff --git a/libreofficekit/qa/unit/tiledrendering.cxx b/libreofficekit/qa/unit/tiledrendering.cxx index fb2f7416ba34..a0f4bcb5b8eb 100644 --- a/libreofficekit/qa/unit/tiledrendering.cxx +++ b/libreofficekit/qa/unit/tiledrendering.cxx @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -67,6 +68,7 @@ public: void testDocumentTypes( Office* pOffice ); void testImpressSlideNames( Office* pOffice ); void testCalcSheetNames( Office* pOffice ); + void testGetStyles( Office* pOffice ); #if 0 void testOverlay( Office* pOffice ); #endif @@ -93,6 +95,7 @@ void TiledRenderingTest::runAllTests() testDocumentTypes( pOffice.get() ); testImpressSlideNames( pOffice.get() ); testCalcSheetNames( pOffice.get() ); + testGetStyles( pOffice.get() ); #if 0 testOverlay( pOffice.get() ); #endif @@ -181,6 +184,38 @@ void TiledRenderingTest::testCalcSheetNames( Office* pOffice ) CPPUNIT_ASSERT( strcmp( pDocument->getPartName( 2 ), "Sheet3" ) == 0 ); } +void TiledRenderingTest::testGetStyles( Office* pOffice ) +{ + const string sDocPath = m_sSrcRoot + "/libreofficekit/qa/data/blank_text.odt"; + const string sLockFile = m_sSrcRoot +"/libreofficekit/qa/data/.~lock.blank_text.odt#"; + + // FIXME: LOK will fail when trying to open a locked file + remove( sLockFile.c_str() ); + + scoped_ptr< Document> pDocument( pOffice->documentLoad( sDocPath.c_str() ) ); + + boost::property_tree::ptree aTree; + char* pJSON = pDocument->getStyles(); + std::stringstream aStream(pJSON); + boost::property_tree::read_json(aStream, aTree); + CPPUNIT_ASSERT( aTree.size() > 0 ); + + for (const std::pair& rPair : aTree) + { + CPPUNIT_ASSERT( rPair.second.size() > 0); + if (rPair.first != "CharacterStyles" && + rPair.first != "ParagraphStyles" && + rPair.first != "FrameStyles" && + rPair.first != "PageStyles" && + rPair.first != "NumberingStyles" && + rPair.first != "CellStyles" && + rPair.first != "ShapeStyles") + { + CPPUNIT_FAIL("Unknown style family: " + rPair.first); + } + } +} + #if 0 static void dumpRGBABitmap( const OUString& rPath, const unsigned char* pBuffer, const int nWidth, const int nHeight ) -- 2.12.0