xref: /aoo4110/main/sc/source/filter/xml/xmlfonte.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 #ifdef PRECOMPILED
27 #include "filt_pch.hxx"
28 #endif
29 
30 
31 #include "scitems.hxx"
32 
33 #include <editeng/eeitem.hxx>
34 
35 
36 #include <xmloff/XMLFontAutoStylePool.hxx>
37 #include <editeng/fontitem.hxx>
38 #include <editeng/eeitem.hxx>
39 #include <editeng/editeng.hxx>
40 #include "document.hxx"
41 #include "docpool.hxx"
42 #include "xmlexprt.hxx"
43 #include "stlpool.hxx"
44 #include "attrib.hxx"
45 
46 class ScXMLFontAutoStylePool_Impl: public XMLFontAutoStylePool
47 {
48 private:
49     // #i120077# remember owned pool
50     SfxItemPool*    mpEditEnginePool;
51 
52 	void AddFontItems(sal_uInt16* pWhichIds, sal_uInt8 nIdCount, const SfxItemPool* pItemPool, const sal_Bool bExportDefaults);
53 
54 public:
55 	ScXMLFontAutoStylePool_Impl( ScXMLExport& rExport );
56     virtual ~ScXMLFontAutoStylePool_Impl();
57 };
58 
AddFontItems(sal_uInt16 * pWhichIds,sal_uInt8 nIdCount,const SfxItemPool * pItemPool,const sal_Bool bExportDefaults)59 void ScXMLFontAutoStylePool_Impl::AddFontItems(sal_uInt16* pWhichIds, sal_uInt8 nIdCount, const SfxItemPool* pItemPool, const sal_Bool bExportDefaults)
60 {
61 	const SfxPoolItem* pItem;
62 	for( sal_uInt16 i=0; i < nIdCount; ++i )
63 	{
64 		sal_uInt16 nWhichId(pWhichIds[i]);
65 		if (bExportDefaults && (0 != (pItem = &pItemPool->GetDefaultItem(nWhichId))))
66 		{
67 			const SvxFontItem *pFont((const SvxFontItem *)pItem);
68 			Add( pFont->GetFamilyName(), pFont->GetStyleName(),
69                     sal::static_int_cast<sal_Int16>(pFont->GetFamily()),
70                     sal::static_int_cast<sal_Int16>(pFont->GetPitch()),
71 					pFont->GetCharSet() );
72 		}
73 		sal_uInt32 nItems(pItemPool->GetItemCount2( nWhichId ));
74 		for( sal_uInt32 j = 0; j < nItems; ++j )
75 		{
76 			if( 0 != (pItem = pItemPool->GetItem2( nWhichId, j ) ) )
77 			{
78 				const SvxFontItem *pFont((const SvxFontItem *)pItem);
79 				Add( pFont->GetFamilyName(), pFont->GetStyleName(),
80                      sal::static_int_cast<sal_Int16>(pFont->GetFamily()),
81                      sal::static_int_cast<sal_Int16>(pFont->GetPitch()),
82 					 pFont->GetCharSet() );
83 			}
84 		}
85 	}
86 }
87 
ScXMLFontAutoStylePool_Impl(ScXMLExport & rExportP)88 ScXMLFontAutoStylePool_Impl::ScXMLFontAutoStylePool_Impl(ScXMLExport& rExportP )
89 :   XMLFontAutoStylePool(rExportP),
90     mpEditEnginePool(NULL)
91 {
92 	sal_uInt16 aWhichIds[3] = { ATTR_FONT, ATTR_CJK_FONT,
93 								ATTR_CTL_FONT };
94 	sal_uInt16 aEditWhichIds[3] = { EE_CHAR_FONTINFO, EE_CHAR_FONTINFO_CJK,
95 									EE_CHAR_FONTINFO_CTL };
96 	sal_uInt16 aPageWhichIds[4] = { ATTR_PAGE_HEADERLEFT, ATTR_PAGE_FOOTERLEFT,
97 									ATTR_PAGE_HEADERRIGHT, ATTR_PAGE_FOOTERRIGHT };
98 
99 	const SfxItemPool* pItemPool(rExportP.GetDocument() ? rExportP.GetDocument()->GetPool() : NULL);
100 	AddFontItems(aWhichIds, 3, pItemPool, sal_True);
101 	const SfxItemPool* pEditPool(rExportP.GetDocument()->GetEditPool());
102 	AddFontItems(aEditWhichIds, 3, pEditPool, sal_False);
103 
104 	SfxStyleSheetIteratorPtr pItr;
105 
106     if(rExportP.GetDocument())
107     {
108     	pItr = rExportP.GetDocument()->GetStyleSheetPool()->CreateIterator(SFX_STYLE_FAMILY_PAGE, 0xFFFF);
109     }
110 
111 	if(pItr)
112 	{
113 		SfxStyleSheetBase* pStyle(pItr->First());
114 
115         if(pStyle)
116         {
117             // #i120077# remember the SfxItemPool in member variable before usage. The
118             // local EditEngine will not take over ownership of the pool.
119             mpEditEnginePool = EditEngine::CreatePool();
120 		    EditEngine aEditEngine(mpEditEnginePool);
121 
122 		    while (pStyle)
123 		    {
124 			    const SfxItemPool& rPagePool(pStyle->GetPool().GetPool());
125 
126                 for (sal_uInt8 j = 0; j < 4; ++j)
127 			    {
128 				    sal_uInt16 nPageWhichId(aPageWhichIds[j]);
129 				    sal_uInt32 nPageHFItems(rPagePool.GetItemCount2(nPageWhichId));
130 				    const ScPageHFItem* pPageItem;
131 				    for (sal_uInt32 k = 0; k < nPageHFItems; ++k)
132 				    {
133 					    if (0 != (pPageItem = static_cast<const ScPageHFItem*>(rPagePool.GetItem2(nPageWhichId, k))))
134 					    {
135 						    const EditTextObject* pLeftArea(pPageItem->GetLeftArea());
136 						    if (pLeftArea)
137 						    {
138 							    aEditEngine.SetText(*pLeftArea);
139 							    AddFontItems(aEditWhichIds, 3, mpEditEnginePool, sal_False);
140 						    }
141 						    const EditTextObject* pCenterArea(pPageItem->GetCenterArea());
142 						    if (pCenterArea)
143 						    {
144 							    aEditEngine.SetText(*pCenterArea);
145 							    AddFontItems(aEditWhichIds, 3, mpEditEnginePool, sal_False);
146 						    }
147 						    const EditTextObject* pRightArea(pPageItem->GetRightArea());
148 						    if (pRightArea)
149 						    {
150 							    aEditEngine.SetText(*pRightArea);
151 							    AddFontItems(aEditWhichIds, 3, mpEditEnginePool, sal_False);
152 						    }
153                         }
154 					}
155 				}
156 
157                 pStyle = pItr->Next();
158 			}
159 		}
160 	}
161 }
162 
~ScXMLFontAutoStylePool_Impl()163 ScXMLFontAutoStylePool_Impl::~ScXMLFontAutoStylePool_Impl()
164 {
165     if(mpEditEnginePool)
166     {
167         // memory leak #i120077#
168         SfxItemPool::Free(mpEditEnginePool);
169     }
170 }
171 
CreateFontAutoStylePool()172 XMLFontAutoStylePool* ScXMLExport::CreateFontAutoStylePool()
173 {
174 	return new ScXMLFontAutoStylePool_Impl( *this );
175 }
176 
177 // eof
178