1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19*63bba73cSAndrew Rist  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include <tools/debug.hxx>
27cdf0e10cSrcweir #include <svl/cntnrsrt.hxx>
28cdf0e10cSrcweir #include <tools/fontenum.hxx>
29cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
30cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
31cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
32cdf0e10cSrcweir #include "fonthdl.hxx"
33cdf0e10cSrcweir #include <xmloff/xmlexp.hxx>
34cdf0e10cSrcweir #include <xmloff/XMLFontAutoStylePool.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using ::rtl::OUString;
38cdf0e10cSrcweir using ::rtl::OUStringBuffer;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41cdf0e10cSrcweir using namespace ::xmloff::token;
42cdf0e10cSrcweir 
XMLFontAutoStylePoolNameCmp_Impl(const OUString & r1,const OUString & r2)43cdf0e10cSrcweir int XMLFontAutoStylePoolNameCmp_Impl( const OUString& r1,
44cdf0e10cSrcweir 									  const OUString& r2 )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	return (int)r1.compareTo( r2 );
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir DECLARE_CONTAINER_SORT_DEL( XMLFontAutoStylePoolNames_Impl,
50cdf0e10cSrcweir 							OUString )
51cdf0e10cSrcweir IMPL_CONTAINER_SORT( XMLFontAutoStylePoolNames_Impl,
52cdf0e10cSrcweir 					 OUString,
53cdf0e10cSrcweir 				     XMLFontAutoStylePoolNameCmp_Impl )
54cdf0e10cSrcweir 
55cdf0e10cSrcweir class XMLFontAutoStylePoolEntry_Impl
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	OUString	sName;
58cdf0e10cSrcweir 	OUString	sFamilyName;
59cdf0e10cSrcweir 	OUString	sStyleName;
60cdf0e10cSrcweir 	sal_Int16	nFamily;
61cdf0e10cSrcweir 	sal_Int16	nPitch;
62cdf0e10cSrcweir 	rtl_TextEncoding eEnc;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir public:
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	inline XMLFontAutoStylePoolEntry_Impl(
67cdf0e10cSrcweir 			const ::rtl::OUString& rName,
68cdf0e10cSrcweir 			const ::rtl::OUString& rFamilyName,
69cdf0e10cSrcweir 			const ::rtl::OUString& rStyleName,
70cdf0e10cSrcweir 			sal_Int16 nFamily,
71cdf0e10cSrcweir 			sal_Int16 nPitch,
72cdf0e10cSrcweir 			rtl_TextEncoding eEnc );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	inline XMLFontAutoStylePoolEntry_Impl(
75cdf0e10cSrcweir 			const ::rtl::OUString& rFamilyName,
76cdf0e10cSrcweir 			const ::rtl::OUString& rStyleName,
77cdf0e10cSrcweir 			sal_Int16 nFamily,
78cdf0e10cSrcweir 			sal_Int16 nPitch,
79cdf0e10cSrcweir 			rtl_TextEncoding eEnc );
80cdf0e10cSrcweir 
GetName() const81cdf0e10cSrcweir 	const OUString&	GetName() const { return sName; }
GetFamilyName() const82cdf0e10cSrcweir 	const OUString&	GetFamilyName() const { return sFamilyName; }
GetStyleName() const83cdf0e10cSrcweir 	const OUString&	GetStyleName() const { return sStyleName; }
GetFamily() const84cdf0e10cSrcweir 	sal_Int16 GetFamily() const {	return nFamily; }
GetPitch() const85cdf0e10cSrcweir 	sal_Int16 GetPitch() const { return nPitch; }
GetEncoding() const86cdf0e10cSrcweir 	rtl_TextEncoding GetEncoding() const { return eEnc; }
87cdf0e10cSrcweir };
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 
XMLFontAutoStylePoolEntry_Impl(const::rtl::OUString & rName,const::rtl::OUString & rFamilyName,const::rtl::OUString & rStyleName,sal_Int16 nFam,sal_Int16 nP,rtl_TextEncoding eE)90cdf0e10cSrcweir inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
91cdf0e10cSrcweir 		const ::rtl::OUString& rName,
92cdf0e10cSrcweir 		const ::rtl::OUString& rFamilyName,
93cdf0e10cSrcweir 		const ::rtl::OUString& rStyleName,
94cdf0e10cSrcweir 		sal_Int16 nFam,
95cdf0e10cSrcweir 		sal_Int16 nP,
96cdf0e10cSrcweir 		rtl_TextEncoding eE ) :
97cdf0e10cSrcweir 	sName( rName ),
98cdf0e10cSrcweir 	sFamilyName( rFamilyName ),
99cdf0e10cSrcweir 	sStyleName( rStyleName ),
100cdf0e10cSrcweir 	nFamily( nFam ),
101cdf0e10cSrcweir 	nPitch( nP ),
102cdf0e10cSrcweir 	eEnc( eE )
103cdf0e10cSrcweir {
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
XMLFontAutoStylePoolEntry_Impl(const::rtl::OUString & rFamilyName,const::rtl::OUString & rStyleName,sal_Int16 nFam,sal_Int16 nP,rtl_TextEncoding eE)106cdf0e10cSrcweir inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
107cdf0e10cSrcweir 		const ::rtl::OUString& rFamilyName,
108cdf0e10cSrcweir 		const ::rtl::OUString& rStyleName,
109cdf0e10cSrcweir 		sal_Int16 nFam,
110cdf0e10cSrcweir 		sal_Int16 nP,
111cdf0e10cSrcweir 		rtl_TextEncoding eE ) :
112cdf0e10cSrcweir 	sFamilyName( rFamilyName ),
113cdf0e10cSrcweir 	sStyleName( rStyleName ),
114cdf0e10cSrcweir 	nFamily( nFam ),
115cdf0e10cSrcweir 	nPitch( nP ),
116cdf0e10cSrcweir 	eEnc( eE )
117cdf0e10cSrcweir {
118cdf0e10cSrcweir }
XMLFontAutoStylePoolEntryCmp_Impl(const XMLFontAutoStylePoolEntry_Impl & r1,const XMLFontAutoStylePoolEntry_Impl & r2)119cdf0e10cSrcweir int XMLFontAutoStylePoolEntryCmp_Impl(
120cdf0e10cSrcweir 		const XMLFontAutoStylePoolEntry_Impl& r1,
121cdf0e10cSrcweir 		const XMLFontAutoStylePoolEntry_Impl& r2 )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	sal_Int8 nEnc1(r1.GetEncoding() != RTL_TEXTENCODING_SYMBOL);
124cdf0e10cSrcweir 	sal_Int8 nEnc2(r2.GetEncoding() != RTL_TEXTENCODING_SYMBOL);
125cdf0e10cSrcweir 	if( nEnc1 != nEnc2 )
126cdf0e10cSrcweir 		return nEnc1 - nEnc2;
127cdf0e10cSrcweir 	else if( r1.GetPitch() != r2.GetPitch() )
128cdf0e10cSrcweir 		return (int)r1.GetPitch() - (int)r2.GetPitch();
129cdf0e10cSrcweir 	else if( r1.GetFamily() != r2.GetFamily() )
130cdf0e10cSrcweir 		return (int)r1.GetFamily() - (int)r2.GetFamily();
131cdf0e10cSrcweir 	else
132cdf0e10cSrcweir 	{
133cdf0e10cSrcweir 		sal_Int32 nCmp = r1.GetFamilyName().compareTo( r2.GetFamilyName() );
134cdf0e10cSrcweir 		if( 0 == nCmp )
135cdf0e10cSrcweir 			return (int)r1.GetStyleName().compareTo( r2.GetStyleName() );
136cdf0e10cSrcweir 		else
137cdf0e10cSrcweir 			return (int)nCmp;
138cdf0e10cSrcweir 	}
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir typedef XMLFontAutoStylePoolEntry_Impl *XMLFontAutoStylePoolEntryPtr;
DECLARE_CONTAINER_SORT_DEL(XMLFontAutoStylePool_Impl,XMLFontAutoStylePoolEntry_Impl)142cdf0e10cSrcweir DECLARE_CONTAINER_SORT_DEL( XMLFontAutoStylePool_Impl,
143cdf0e10cSrcweir 							XMLFontAutoStylePoolEntry_Impl )
144cdf0e10cSrcweir IMPL_CONTAINER_SORT( XMLFontAutoStylePool_Impl,
145cdf0e10cSrcweir 					 XMLFontAutoStylePoolEntry_Impl,
146cdf0e10cSrcweir 					 XMLFontAutoStylePoolEntryCmp_Impl )
147cdf0e10cSrcweir 
148cdf0e10cSrcweir XMLFontAutoStylePool::XMLFontAutoStylePool( SvXMLExport& rExp ) :
149cdf0e10cSrcweir 	rExport( rExp ),
150cdf0e10cSrcweir 	pPool( new XMLFontAutoStylePool_Impl( 5, 5 ) ),
151cdf0e10cSrcweir 	pNames( new XMLFontAutoStylePoolNames_Impl( 5, 5 ) )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
~XMLFontAutoStylePool()155cdf0e10cSrcweir XMLFontAutoStylePool::~XMLFontAutoStylePool()
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	delete pPool;
158cdf0e10cSrcweir 	delete pNames;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
Add(const OUString & rFamilyName,const OUString & rStyleName,sal_Int16 nFamily,sal_Int16 nPitch,rtl_TextEncoding eEnc)161cdf0e10cSrcweir OUString XMLFontAutoStylePool::Add(
162cdf0e10cSrcweir 			const OUString& rFamilyName,
163cdf0e10cSrcweir 			const OUString& rStyleName,
164cdf0e10cSrcweir 			sal_Int16 nFamily,
165cdf0e10cSrcweir 			sal_Int16 nPitch,
166cdf0e10cSrcweir 			rtl_TextEncoding eEnc )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	OUString sPoolName;
169cdf0e10cSrcweir 	XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
170cdf0e10cSrcweir 									 	 nPitch, eEnc );
171cdf0e10cSrcweir 	sal_uLong nPos;
172cdf0e10cSrcweir 	if( pPool->Seek_Entry( &aTmp, &nPos ) )
173cdf0e10cSrcweir 	{
174cdf0e10cSrcweir 		sPoolName = pPool->GetObject( nPos )->GetName();
175cdf0e10cSrcweir 	}
176cdf0e10cSrcweir 	else
177cdf0e10cSrcweir 	{
178cdf0e10cSrcweir 		OUString sName;
179cdf0e10cSrcweir 		sal_Int32 nLen = rFamilyName.indexOf( sal_Unicode(';'), 0 );
180cdf0e10cSrcweir 		if( -1 == nLen )
181cdf0e10cSrcweir 		{
182cdf0e10cSrcweir 			sName = rFamilyName;
183cdf0e10cSrcweir 		}
184cdf0e10cSrcweir 		else if( nLen > 0 )
185cdf0e10cSrcweir 		{
186cdf0e10cSrcweir 			sName = rFamilyName.copy( 0, nLen );
187cdf0e10cSrcweir 			sName.trim();
188cdf0e10cSrcweir 		}
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 		if( !sName.getLength() )
191cdf0e10cSrcweir 			sName = OUString::valueOf( sal_Unicode( 'F' ) );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 		if( pNames->Seek_Entry( &sName, 0 ) )
194cdf0e10cSrcweir 		{
195cdf0e10cSrcweir 			sal_Int32 nCount = 1;
196cdf0e10cSrcweir 			OUString sPrefix( sName );
197cdf0e10cSrcweir 			sName += OUString::valueOf( nCount );
198cdf0e10cSrcweir 			while( pNames->Seek_Entry( &sName, 0 ) )
199cdf0e10cSrcweir 			{
200cdf0e10cSrcweir 				sName = sPrefix;
201cdf0e10cSrcweir 				sName += OUString::valueOf( ++nCount );
202cdf0e10cSrcweir 			}
203cdf0e10cSrcweir 		}
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 		XMLFontAutoStylePoolEntry_Impl *pEntry =
206cdf0e10cSrcweir 			new XMLFontAutoStylePoolEntry_Impl( sName, rFamilyName, rStyleName,
207cdf0e10cSrcweir 												nFamily, nPitch, eEnc );
208cdf0e10cSrcweir 		pPool->Insert( pEntry );
209cdf0e10cSrcweir 		pNames->Insert( new OUString( sName ) );
210cdf0e10cSrcweir 	}
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 	return sPoolName;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
Find(const OUString & rFamilyName,const OUString & rStyleName,sal_Int16 nFamily,sal_Int16 nPitch,rtl_TextEncoding eEnc) const215cdf0e10cSrcweir ::rtl::OUString XMLFontAutoStylePool::Find(
216cdf0e10cSrcweir 			const OUString& rFamilyName,
217cdf0e10cSrcweir 			const OUString& rStyleName,
218cdf0e10cSrcweir 			sal_Int16 nFamily,
219cdf0e10cSrcweir 			sal_Int16 nPitch,
220cdf0e10cSrcweir 			rtl_TextEncoding eEnc ) const
221cdf0e10cSrcweir {
222cdf0e10cSrcweir 	OUString sName;
223cdf0e10cSrcweir 	XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
224cdf0e10cSrcweir 									 	 nPitch, eEnc );
225cdf0e10cSrcweir 	sal_uLong nPos;
226cdf0e10cSrcweir 	if( pPool->Seek_Entry( &aTmp, &nPos ) )
227cdf0e10cSrcweir 	{
228cdf0e10cSrcweir 		sName = pPool->GetObject( nPos )->GetName();
229cdf0e10cSrcweir 	}
230cdf0e10cSrcweir 
231cdf0e10cSrcweir 	return sName;
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
exportXML()235cdf0e10cSrcweir void XMLFontAutoStylePool::exportXML()
236cdf0e10cSrcweir {
237cdf0e10cSrcweir 	SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_OFFICE,
238cdf0e10cSrcweir 							  XML_FONT_FACE_DECLS,
239cdf0e10cSrcweir 							  sal_True, sal_True );
240cdf0e10cSrcweir 	Any aAny;
241cdf0e10cSrcweir 	OUString sTmp;
242cdf0e10cSrcweir 	XMLFontFamilyNamePropHdl aFamilyNameHdl;
243cdf0e10cSrcweir 	XMLFontFamilyPropHdl aFamilyHdl;
244cdf0e10cSrcweir 	XMLFontPitchPropHdl aPitchHdl;
245cdf0e10cSrcweir 	XMLFontEncodingPropHdl aEncHdl;
246cdf0e10cSrcweir 	const SvXMLUnitConverter& rUnitConv = GetExport().GetMM100UnitConverter();
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	sal_uInt32 nCount = pPool->Count();
249cdf0e10cSrcweir 	for( sal_uInt32 i=0; i<nCount; i++ )
250cdf0e10cSrcweir 	{
251cdf0e10cSrcweir 		const XMLFontAutoStylePoolEntry_Impl *pEntry = pPool->GetObject( i );
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 		GetExport().AddAttribute( XML_NAMESPACE_STYLE,
254cdf0e10cSrcweir 								  XML_NAME, pEntry->GetName() );
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 		aAny <<= pEntry->GetFamilyName();
257cdf0e10cSrcweir 		if( aFamilyNameHdl.exportXML( sTmp, aAny, rUnitConv ) )
258cdf0e10cSrcweir 			GetExport().AddAttribute( XML_NAMESPACE_SVG,
259cdf0e10cSrcweir 									  XML_FONT_FAMILY, sTmp );
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 		const OUString& rStyleName = pEntry->GetStyleName();
262cdf0e10cSrcweir 		if( rStyleName.getLength() )
263cdf0e10cSrcweir 			GetExport().AddAttribute( XML_NAMESPACE_STYLE,
264cdf0e10cSrcweir 									  XML_FONT_ADORNMENTS,
265cdf0e10cSrcweir 									  rStyleName );
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 		aAny <<= (sal_Int16)pEntry->GetFamily();
268cdf0e10cSrcweir 		if( aFamilyHdl.exportXML( sTmp, aAny, rUnitConv  ) )
269cdf0e10cSrcweir 			GetExport().AddAttribute( XML_NAMESPACE_STYLE,
270cdf0e10cSrcweir 									  XML_FONT_FAMILY_GENERIC, sTmp );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 		aAny <<= (sal_Int16)pEntry->GetPitch();
273cdf0e10cSrcweir 		if( aPitchHdl.exportXML( sTmp, aAny, rUnitConv  ) )
274cdf0e10cSrcweir 			GetExport().AddAttribute( XML_NAMESPACE_STYLE,
275cdf0e10cSrcweir 									  XML_FONT_PITCH, sTmp );
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 		aAny <<= (sal_Int16)pEntry->GetEncoding();
278cdf0e10cSrcweir 		if( aEncHdl.exportXML( sTmp, aAny, rUnitConv  ) )
279cdf0e10cSrcweir 			GetExport().AddAttribute( XML_NAMESPACE_STYLE,
280cdf0e10cSrcweir 									  XML_FONT_CHARSET, sTmp );
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 		SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE,
283cdf0e10cSrcweir 								  XML_FONT_FACE,
284cdf0e10cSrcweir 								  sal_True, sal_True );
285cdf0e10cSrcweir 	}
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 
289