LibreOffice Module sc (master)  1
xmlfonte.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <scitems.hxx>
21 
22 #include <editeng/eeitem.hxx>
23 
25 #include <editeng/fontitem.hxx>
26 #include <editeng/editeng.hxx>
27 #include <document.hxx>
28 #include <docpool.hxx>
29 #include "xmlexprt.hxx"
30 #include <stlpool.hxx>
31 #include <attrib.hxx>
32 
33 namespace {
34 
35 class ScXMLFontAutoStylePool_Impl: public XMLFontAutoStylePool
36 {
37 private:
38  // #i120077# remember owned pool
39  SfxItemPool* mpEditEnginePool;
40 
41  void AddFontItems(const sal_uInt16* pWhichIds, sal_uInt8 nIdCount, const SfxItemPool* pItemPool, const bool bExportDefaults);
42 
43 public:
44  ScXMLFontAutoStylePool_Impl( ScXMLExport& rExport, bool bEmbedFonts);
45  virtual ~ScXMLFontAutoStylePool_Impl() override;
46 };
47 
48 }
49 
50 void ScXMLFontAutoStylePool_Impl::AddFontItems(const sal_uInt16* pWhichIds, sal_uInt8 nIdCount, const SfxItemPool* pItemPool, const bool bExportDefaults)
51 {
52  for( sal_uInt16 i=0; i < nIdCount; ++i )
53  {
54  sal_uInt16 nWhichId(pWhichIds[i]);
55  if (bExportDefaults)
56  {
57  const SfxPoolItem* pItem = &pItemPool->GetDefaultItem(nWhichId);
58  const SvxFontItem *pFont(static_cast<const SvxFontItem *>(pItem));
59  Add( pFont->GetFamilyName(), pFont->GetStyleName(),
60  pFont->GetFamily(), pFont->GetPitch(),
61  pFont->GetCharSet() );
62  }
63  for (const SfxPoolItem* pItem : pItemPool->GetItemSurrogates( nWhichId ))
64  {
65  const SvxFontItem *pFont(static_cast<const SvxFontItem *>(pItem));
66  Add( pFont->GetFamilyName(), pFont->GetStyleName(),
67  pFont->GetFamily(), pFont->GetPitch(),
68  pFont->GetCharSet() );
69  }
70  }
71 }
72 
73 ScXMLFontAutoStylePool_Impl::ScXMLFontAutoStylePool_Impl(ScXMLExport& rExportP, bool bEmbedFonts)
74  : XMLFontAutoStylePool(rExportP, bEmbedFonts)
75  , mpEditEnginePool(nullptr)
76 {
77  sal_uInt16 const aWhichIds[] { ATTR_FONT, ATTR_CJK_FONT,
78  ATTR_CTL_FONT };
79  sal_uInt16 const aEditWhichIds[] { EE_CHAR_FONTINFO, EE_CHAR_FONTINFO_CJK,
81  sal_uInt16 const aPageWhichIds[] { ATTR_PAGE_HEADERLEFT, ATTR_PAGE_FOOTERLEFT,
83 
84  const SfxItemPool* pItemPool(rExportP.GetDocument()->GetPool());
85  AddFontItems(aWhichIds, 3, pItemPool, true);
86  const SfxItemPool* pEditPool(rExportP.GetDocument()->GetEditPool());
87  AddFontItems(aEditWhichIds, 3, pEditPool, false);
88 
89  std::unique_ptr<SfxStyleSheetIterator> pItr = rExportP.GetDocument()->GetStyleSheetPool()->CreateIterator(SfxStyleFamily::Page);
90 
91  m_bEmbedUsedOnly = rExportP.GetDocument()->IsEmbedUsedFontsOnly();
92  m_bEmbedLatinScript = rExportP.GetDocument()->IsEmbedFontScriptLatin();
93  m_bEmbedAsianScript = rExportP.GetDocument()->IsEmbedFontScriptAsian();
94  m_bEmbedComplexScript = rExportP.GetDocument()->IsEmbedFontScriptComplex();
95 
96  if(!pItr)
97  return;
98 
99  SfxStyleSheetBase* pStyle(pItr->First());
100 
101  if(!pStyle)
102  return;
103 
104  // #i120077# remember the SfxItemPool in member variable before usage. The
105  // local EditEngine will not take over ownership of the pool.
106  mpEditEnginePool = EditEngine::CreatePool();
107  EditEngine aEditEngine(mpEditEnginePool);
108 
109  while (pStyle)
110  {
111  const SfxItemPool& rPagePool(pStyle->GetPool()->GetPool());
112 
113  for (sal_uInt16 nPageWhichId : aPageWhichIds)
114  {
115  for (const SfxPoolItem* pItem : rPagePool.GetItemSurrogates( nPageWhichId ))
116  {
117  const ScPageHFItem* pPageItem = static_cast<const ScPageHFItem*>(pItem);
118  const EditTextObject* pLeftArea(pPageItem->GetLeftArea());
119  if (pLeftArea)
120  {
121  aEditEngine.SetText(*pLeftArea);
122  AddFontItems(aEditWhichIds, 3, mpEditEnginePool, false);
123  }
124  const EditTextObject* pCenterArea(pPageItem->GetCenterArea());
125  if (pCenterArea)
126  {
127  aEditEngine.SetText(*pCenterArea);
128  AddFontItems(aEditWhichIds, 3, mpEditEnginePool, false);
129  }
130  const EditTextObject* pRightArea(pPageItem->GetRightArea());
131  if (pRightArea)
132  {
133  aEditEngine.SetText(*pRightArea);
134  AddFontItems(aEditWhichIds, 3, mpEditEnginePool, false);
135  }
136  }
137  }
138 
139  pStyle = pItr->Next();
140  }
141 }
142 
143 ScXMLFontAutoStylePool_Impl::~ScXMLFontAutoStylePool_Impl()
144 {
145  if(mpEditEnginePool)
146  {
147  // memory leak #i120077#
148  SfxItemPool::Free(mpEditEnginePool);
149  }
150 }
151 
153 {
154  bool blockFontEmbedding = false;
155  // We write font info to both content.xml and styles.xml, but they are both
156  // written by different ScXMLExport instance, and would therefore write each
157  // font file twice without complicated checking for duplicates, so handle
158  // the embedding only in one of them.
159  if(!( getExportFlags() & SvXMLExportFlags::CONTENT ))
160  blockFontEmbedding = true;
161  if (!GetDocument()->IsEmbedFonts())
162  blockFontEmbedding = true;
163  return new ScXMLFontAutoStylePool_Impl( *this, !blockFontEmbedding );
164 }
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual std::unique_ptr< SfxStyleSheetIterator > CreateIterator(SfxStyleFamily, SfxStyleSearchBits nMask=SfxStyleSearchBits::All)
constexpr TypedWhichId< SvxFontItem > EE_CHAR_FONTINFO_CJK(EE_CHAR_START+17)
bool IsEmbedFontScriptLatin() const
Definition: document.hxx:574
const EditTextObject * GetCenterArea() const
Definition: attrib.hxx:186
constexpr TypedWhichId< SvxFontItem > ATTR_FONT(100)
SC_DLLPUBLIC ScDocumentPool * GetPool()
Definition: document.cxx:6054
constexpr TypedWhichId< SvxFontItem > EE_CHAR_FONTINFO_CTL(EE_CHAR_START+18)
bool IsEmbedUsedFontsOnly() const
Definition: document.hxx:573
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_FOOTERLEFT(178)
int i
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_HEADERRIGHT(179)
constexpr TypedWhichId< SvxFontItem > ATTR_CTL_FONT(116)
const SfxPoolItem & GetDefaultItem(sal_uInt16 nWhich) const
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_FOOTERRIGHT(180)
static SfxItemPool * CreatePool()
constexpr TypedWhichId< SvxFontItem > ATTR_CJK_FONT(111)
ScDocument * GetDocument()
Definition: xmlexprt.hxx:240
static void Free(SfxItemPool *pPool)
unsigned char sal_uInt8
const EditTextObject * GetRightArea() const
Definition: attrib.hxx:187
const EditTextObject * GetLeftArea() const
Definition: attrib.hxx:185
bool IsEmbedFontScriptComplex() const
Definition: document.hxx:576
Item2Range GetItemSurrogates(sal_uInt16 nWhich) const
constexpr TypedWhichId< SvxFontItem > EE_CHAR_FONTINFO(EE_CHAR_START+1)
SC_DLLPUBLIC ScStyleSheetPool * GetStyleSheetPool() const
Definition: document.cxx:6059
SC_DLLPUBLIC SfxItemPool * GetEditPool() const
Definition: documen2.cxx:444
bool IsEmbedFontScriptAsian() const
Definition: document.hxx:575
virtual XMLFontAutoStylePool * CreateFontAutoStylePool() override
Definition: xmlfonte.cxx:152
SvXMLExportFlags getExportFlags() const
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_HEADERLEFT(177)