diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2018-07-09 00:47:07 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2018-07-10 16:15:12 +0200 |
commit | 3bc3ddc11fd94877d9c5d2b8313ab53150818236 (patch) | |
tree | 02d505a21705c00f454144b4f8654d46d3b13f73 /xmloff | |
parent | eb6ff07605a55675e7007ac0cb5604fb13a9ddf9 (diff) |
tdf#65353 filter fonts when embedding (unused, font script)
When embedding, the fonts are gathered and added to the ODF
document. This change in addition adds the functionallity to
embed used fonts only and filtering out of font scripts. When
embedding used fonts only, they are filtered depending on if
the style (referencing the font) is in use and in addition if
the font is used in direct formatting.
Change-Id: I1ba0066b1eb34bdde7247eeb63c1be5a91db984d
Reviewed-on: https://gerrit.libreoffice.org/57168
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'xmloff')
-rw-r--r-- | xmloff/source/style/XMLFontAutoStylePool.cxx | 124 | ||||
-rw-r--r-- | xmloff/source/style/impastpl.cxx | 31 | ||||
-rw-r--r-- | xmloff/source/style/impastpl.hxx | 3 | ||||
-rw-r--r-- | xmloff/source/style/xmlaustp.cxx | 5 | ||||
-rw-r--r-- | xmloff/source/text/txtparae.cxx | 5 |
5 files changed, 156 insertions, 12 deletions
diff --git a/xmloff/source/style/XMLFontAutoStylePool.cxx b/xmloff/source/style/XMLFontAutoStylePool.cxx index c410624ee9ce..56bb2211e990 100644 --- a/xmloff/source/style/XMLFontAutoStylePool.cxx +++ b/xmloff/source/style/XMLFontAutoStylePool.cxx @@ -32,8 +32,12 @@ #include <com/sun/star/embed/XTransactedObject.hpp> #include <com/sun/star/lang/XMultiServiceFactory.hpp> #include <com/sun/star/ucb/SimpleFileAccess.hpp> +#include <com/sun/star/style/XStyleFamiliesSupplier.hpp> +#include <com/sun/star/style/XStyle.hpp> +#include <com/sun/star/container/XNameAccess.hpp> #include <XMLBase64Export.hxx> +#include <xmloff/AutoStyleEntry.hxx> #include <comphelper/hash.hxx> using namespace ::com::sun::star; @@ -290,6 +294,93 @@ OUString FontItalicToString(FontItalic eWeight) } +std::unordered_set<OUString> XMLFontAutoStylePool::getUsedFontList() +{ + std::unordered_set<OUString> aReturnSet; + + uno::Reference<style::XStyleFamiliesSupplier> xFamiliesSupp(GetExport().GetModel(), UNO_QUERY); + if (!xFamiliesSupp.is()) + return aReturnSet; + + // Check styles first + uno::Reference<container::XNameAccess> xFamilies(xFamiliesSupp->getStyleFamilies()); + if (xFamilies.is()) + { + for (OUString const & sFamilyName : xFamilies->getElementNames()) + { + uno::Reference<container::XNameAccess> xStyleContainer; + xFamilies->getByName(sFamilyName) >>= xStyleContainer; + + if (xStyleContainer.is()) + { + for (OUString const & rName : xStyleContainer->getElementNames()) + { + uno::Reference<style::XStyle> xStyle; + xStyleContainer->getByName(rName) >>= xStyle; + if (xStyle->isInUse()) + { + uno::Reference<beans::XPropertySet> xPropertySet(xStyle, UNO_QUERY); + if (xPropertySet.is()) + { + uno::Reference<beans::XPropertySetInfo> xInfo(xPropertySet->getPropertySetInfo()); + if (m_bEmbedLatinScript && xInfo->hasPropertyByName("CharFontName")) + { + OUString sCharFontName; + Any aFontAny = xPropertySet->getPropertyValue("CharFontName"); + aFontAny >>= sCharFontName; + if (!sCharFontName.isEmpty()) + aReturnSet.insert(sCharFontName); + } + if (m_bEmbedAsianScript && xInfo->hasPropertyByName("CharFontNameAsian")) + { + OUString sCharFontNameAsian; + Any aFontAny = xPropertySet->getPropertyValue("CharFontNameAsian"); + aFontAny >>= sCharFontNameAsian; + if (!sCharFontNameAsian.isEmpty()) + aReturnSet.insert(sCharFontNameAsian); + } + if (m_bEmbedComplexScript && xInfo->hasPropertyByName("CharFontNameComplex")) + { + OUString sCharFontNameComplex; + Any aFontAny = xPropertySet->getPropertyValue("CharFontNameComplex"); + aFontAny >>= sCharFontNameComplex; + if (!sCharFontNameComplex.isEmpty()) + aReturnSet.insert(sCharFontNameComplex); + } + } + } + } + } + } + } + + // make sure auto-styles are collected + GetExport().GetTextParagraphExport()->collectTextAutoStylesOptimized(false); + + // Check auto-styles for fonts + std::vector<xmloff::AutoStyleEntry> aAutoStyleEntries; + aAutoStyleEntries = GetExport().GetAutoStylePool()->GetAutoStyleEntries(); + for (auto const & rAutoStyleEntry : aAutoStyleEntries) + { + for (auto const & rPair : rAutoStyleEntry.m_aXmlProperties) + { + if (rPair.first == "font-name" || + rPair.first == "font-weight-asian" || + rPair.first == "font-weight-complex") + { + if (rPair.second.has<OUString>()) + { + OUString sFontName = rPair.second.get<OUString>(); + if (!sFontName.isEmpty()) + aReturnSet.insert(sFontName); + } + } + } + } + + return aReturnSet; +} + void XMLFontAutoStylePool::exportXML() { SvXMLElementExport aElem(GetExport(), XML_NAMESPACE_OFFICE, @@ -306,6 +397,10 @@ void XMLFontAutoStylePool::exportXML() std::map<OUString, OUString> fontFilesMap; // our url to document url sal_uInt32 nCount = m_pFontAutoStylePool->size(); + std::unordered_set<OUString> aUsedFontNames; + if (m_bEmbedUsedOnly) + aUsedFontNames = getUsedFontList(); + for (sal_uInt32 i = 0; i < nCount; i++) { const XMLFontAutoStylePoolEntry_Impl* pEntry = (*m_pFontAutoStylePool)[i]; @@ -369,20 +464,25 @@ void XMLFontAutoStylePool::exportXML() if (sFileUrl.isEmpty()) continue; - if (!fontFilesMap.count(sFileUrl)) + // When embeded only is not set or font is used + if (!m_bEmbedUsedOnly || + aUsedFontNames.find(pEntry->GetFamilyName()) != aUsedFontNames.end()) { - const OUString docUrl = bExportFlat ? - lcl_checkFontFile(sFileUrl) : embedFontFile(sFileUrl, pEntry->GetFamilyName()); - if (!docUrl.isEmpty()) - fontFilesMap[sFileUrl] = docUrl; - else - continue; // --> failed to embed + if (!fontFilesMap.count(sFileUrl)) + { + const OUString docUrl = bExportFlat ? + lcl_checkFontFile(sFileUrl) : embedFontFile(sFileUrl, pEntry->GetFamilyName()); + if (!docUrl.isEmpty()) + fontFilesMap[sFileUrl] = docUrl; + else + continue; // --> failed to embed + } + EmbeddedFontInfo aEmbeddedFont; + aEmbeddedFont.aURL = sFileUrl; + aEmbeddedFont.eWeight = aCombinationPair.first; + aEmbeddedFont.eItalic = aCombinationPair.second; + aEmbeddedFonts.push_back(aEmbeddedFont); } - EmbeddedFontInfo aEmbeddedFont; - aEmbeddedFont.aURL = sFileUrl; - aEmbeddedFont.eWeight = aCombinationPair.first; - aEmbeddedFont.eItalic = aCombinationPair.second; - aEmbeddedFonts.push_back(aEmbeddedFont); } if (!aEmbeddedFonts.empty()) { diff --git a/xmloff/source/style/impastpl.cxx b/xmloff/source/style/impastpl.cxx index 69ad5d3e05ce..ed5f0e32eecb 100644 --- a/xmloff/source/style/impastpl.cxx +++ b/xmloff/source/style/impastpl.cxx @@ -591,6 +591,37 @@ OUString SvXMLAutoStylePoolP_Impl::Find( sal_Int32 nFamily, return sName; } +std::vector<xmloff::AutoStyleEntry> SvXMLAutoStylePoolP_Impl::GetAutoStyleEntries() const +{ + std::vector<xmloff::AutoStyleEntry> rReturnVector; + + for (std::unique_ptr<XMLAutoStyleFamily> const & rFamily : m_FamilySet) + { + rtl::Reference<XMLPropertySetMapper> aPropertyMapper = rFamily->mxMapper->getPropertySetMapper(); + sal_Int32 nFamily = rFamily->mnFamily; + for (auto const & rParent : rFamily->m_ParentSet) + { + for (auto const & rProperty : rParent->GetPropertiesList()) + { + rReturnVector.emplace_back(); + xmloff::AutoStyleEntry & rEntry = rReturnVector.back(); + rEntry.m_nFamily = nFamily; + rEntry.m_aParentName = rParent->GetParent(); + rEntry.m_aName = rProperty->GetName(); + for (XMLPropertyState const & rPropertyState : rProperty->GetProperties()) + { + if (rPropertyState.mnIndex >= 0) + { + OUString sXmlName = aPropertyMapper->GetEntryXMLName(rPropertyState.mnIndex); + rEntry.m_aXmlProperties.emplace_back(sXmlName, rPropertyState.maValue); + } + } + } + } + } + return rReturnVector; +} + namespace { struct AutoStylePoolExport diff --git a/xmloff/source/style/impastpl.hxx b/xmloff/source/style/impastpl.hxx index eec145cd19c9..49a93fc2a742 100644 --- a/xmloff/source/style/impastpl.hxx +++ b/xmloff/source/style/impastpl.hxx @@ -33,6 +33,7 @@ #include <xmloff/maptype.hxx> #include <xmloff/xmlexppr.hxx> +#include <xmloff/AutoStyleEntry.hxx> class SvXMLAutoStylePoolP; class XMLAutoStylePoolParent; @@ -174,6 +175,8 @@ public: const SvXMLAutoStylePoolP *pAntiImpl) const; void ClearEntries(); + + std::vector<xmloff::AutoStyleEntry> GetAutoStyleEntries() const; }; #endif diff --git a/xmloff/source/style/xmlaustp.cxx b/xmloff/source/style/xmlaustp.cxx index 863f740b24b8..c5256a49f12b 100644 --- a/xmloff/source/style/xmlaustp.cxx +++ b/xmloff/source/style/xmlaustp.cxx @@ -382,4 +382,9 @@ void SvXMLAutoStylePoolP::ClearEntries() pImpl->ClearEntries(); } +std::vector<xmloff::AutoStyleEntry> SvXMLAutoStylePoolP::GetAutoStyleEntries() const +{ + return pImpl->GetAutoStyleEntries(); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmloff/source/text/txtparae.cxx b/xmloff/source/text/txtparae.cxx index d039682eb856..cd9eba5f801b 100644 --- a/xmloff/source/text/txtparae.cxx +++ b/xmloff/source/text/txtparae.cxx @@ -1173,6 +1173,7 @@ XMLTextParagraphExport::XMLTextParagraphExport( bOpenRuby( false ), mpTextListsHelper( nullptr ), maTextListsHelperStack(), + mbCollected(false), // Implement Title/Description Elements UI (#i73249#) sTitle("Title"), sDescription("Description"), @@ -1432,6 +1433,9 @@ void XMLTextParagraphExport::collectTextAutoStylesOptimized( bool bIsProgress ) { GetExport().GetShapeExport(); // make sure the graphics styles family is added + if (mbCollected) + return; + const bool bAutoStyles = true; const bool bExportContent = false; @@ -1630,6 +1634,7 @@ void XMLTextParagraphExport::collectTextAutoStylesOptimized( bool bIsProgress ) } } } + mbCollected = true; } void XMLTextParagraphExport::exportText( |