Update ooo320-m1
[ooovba.git] / xmloff / source / style / XMLFontAutoStylePool.cxx
blobf45fb244b805dc34f286b542a86f48f0d0cf1f83
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLFontAutoStylePool.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include <tools/debug.hxx>
34 #include <svtools/cntnrsrt.hxx>
35 #include <vcl/vclenum.hxx>
36 #include "xmlnmspe.hxx"
37 #include <xmloff/xmltoken.hxx>
38 #include <xmloff/xmluconv.hxx>
39 #ifndef _XMLOFF_PROPERTYHANDLER_FONTTYPES_HXX
40 #include "fonthdl.hxx"
41 #endif
42 #include <xmloff/xmlexp.hxx>
43 #include <xmloff/XMLFontAutoStylePool.hxx>
45 using ::rtl::OUString;
46 using ::rtl::OUStringBuffer;
48 using namespace ::com::sun::star::uno;
49 using namespace ::xmloff::token;
51 int XMLFontAutoStylePoolNameCmp_Impl( const OUString& r1,
52 const OUString& r2 )
54 return (int)r1.compareTo( r2 );
57 DECLARE_CONTAINER_SORT_DEL( XMLFontAutoStylePoolNames_Impl,
58 OUString )
59 IMPL_CONTAINER_SORT( XMLFontAutoStylePoolNames_Impl,
60 OUString,
61 XMLFontAutoStylePoolNameCmp_Impl )
63 class XMLFontAutoStylePoolEntry_Impl
65 OUString sName;
66 OUString sFamilyName;
67 OUString sStyleName;
68 sal_Int16 nFamily;
69 sal_Int16 nPitch;
70 rtl_TextEncoding eEnc;
72 public:
74 inline XMLFontAutoStylePoolEntry_Impl(
75 const ::rtl::OUString& rName,
76 const ::rtl::OUString& rFamilyName,
77 const ::rtl::OUString& rStyleName,
78 sal_Int16 nFamily,
79 sal_Int16 nPitch,
80 rtl_TextEncoding eEnc );
82 inline XMLFontAutoStylePoolEntry_Impl(
83 const ::rtl::OUString& rFamilyName,
84 const ::rtl::OUString& rStyleName,
85 sal_Int16 nFamily,
86 sal_Int16 nPitch,
87 rtl_TextEncoding eEnc );
89 const OUString& GetName() const { return sName; }
90 const OUString& GetFamilyName() const { return sFamilyName; }
91 const OUString& GetStyleName() const { return sStyleName; }
92 sal_Int16 GetFamily() const { return nFamily; }
93 sal_Int16 GetPitch() const { return nPitch; }
94 rtl_TextEncoding GetEncoding() const { return eEnc; }
98 inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
99 const ::rtl::OUString& rName,
100 const ::rtl::OUString& rFamilyName,
101 const ::rtl::OUString& rStyleName,
102 sal_Int16 nFam,
103 sal_Int16 nP,
104 rtl_TextEncoding eE ) :
105 sName( rName ),
106 sFamilyName( rFamilyName ),
107 sStyleName( rStyleName ),
108 nFamily( nFam ),
109 nPitch( nP ),
110 eEnc( eE )
114 inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
115 const ::rtl::OUString& rFamilyName,
116 const ::rtl::OUString& rStyleName,
117 sal_Int16 nFam,
118 sal_Int16 nP,
119 rtl_TextEncoding eE ) :
120 sFamilyName( rFamilyName ),
121 sStyleName( rStyleName ),
122 nFamily( nFam ),
123 nPitch( nP ),
124 eEnc( eE )
127 int XMLFontAutoStylePoolEntryCmp_Impl(
128 const XMLFontAutoStylePoolEntry_Impl& r1,
129 const XMLFontAutoStylePoolEntry_Impl& r2 )
131 sal_Int8 nEnc1(r1.GetEncoding() != RTL_TEXTENCODING_SYMBOL);
132 sal_Int8 nEnc2(r2.GetEncoding() != RTL_TEXTENCODING_SYMBOL);
133 if( nEnc1 != nEnc2 )
134 return nEnc1 - nEnc2;
135 else if( r1.GetPitch() != r2.GetPitch() )
136 return (int)r1.GetPitch() - (int)r2.GetPitch();
137 else if( r1.GetFamily() != r2.GetFamily() )
138 return (int)r1.GetFamily() - (int)r2.GetFamily();
139 else
141 sal_Int32 nCmp = r1.GetFamilyName().compareTo( r2.GetFamilyName() );
142 if( 0 == nCmp )
143 return (int)r1.GetStyleName().compareTo( r2.GetStyleName() );
144 else
145 return (int)nCmp;
149 typedef XMLFontAutoStylePoolEntry_Impl *XMLFontAutoStylePoolEntryPtr;
150 DECLARE_CONTAINER_SORT_DEL( XMLFontAutoStylePool_Impl,
151 XMLFontAutoStylePoolEntry_Impl )
152 IMPL_CONTAINER_SORT( XMLFontAutoStylePool_Impl,
153 XMLFontAutoStylePoolEntry_Impl,
154 XMLFontAutoStylePoolEntryCmp_Impl )
156 XMLFontAutoStylePool::XMLFontAutoStylePool( SvXMLExport& rExp ) :
157 rExport( rExp ),
158 pPool( new XMLFontAutoStylePool_Impl( 5, 5 ) ),
159 pNames( new XMLFontAutoStylePoolNames_Impl( 5, 5 ) )
163 XMLFontAutoStylePool::~XMLFontAutoStylePool()
165 delete pPool;
166 delete pNames;
169 OUString XMLFontAutoStylePool::Add(
170 const OUString& rFamilyName,
171 const OUString& rStyleName,
172 sal_Int16 nFamily,
173 sal_Int16 nPitch,
174 rtl_TextEncoding eEnc )
176 OUString sPoolName;
177 XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
178 nPitch, eEnc );
179 ULONG nPos;
180 if( pPool->Seek_Entry( &aTmp, &nPos ) )
182 sPoolName = pPool->GetObject( nPos )->GetName();
184 else
186 OUString sName;
187 sal_Int32 nLen = rFamilyName.indexOf( sal_Unicode(';'), 0 );
188 if( -1 == nLen )
190 sName = rFamilyName;
192 else if( nLen > 0 )
194 sName = rFamilyName.copy( 0, nLen );
195 sName.trim();
198 if( !sName.getLength() )
199 sName = OUString::valueOf( sal_Unicode( 'F' ) );
201 if( pNames->Seek_Entry( &sName, 0 ) )
203 sal_Int32 nCount = 1;
204 OUString sPrefix( sName );
205 sName += OUString::valueOf( nCount );
206 while( pNames->Seek_Entry( &sName, 0 ) )
208 sName = sPrefix;
209 sName += OUString::valueOf( ++nCount );
213 XMLFontAutoStylePoolEntry_Impl *pEntry =
214 new XMLFontAutoStylePoolEntry_Impl( sName, rFamilyName, rStyleName,
215 nFamily, nPitch, eEnc );
216 pPool->Insert( pEntry );
217 pNames->Insert( new OUString( sName ) );
220 return sPoolName;
223 ::rtl::OUString XMLFontAutoStylePool::Find(
224 const OUString& rFamilyName,
225 const OUString& rStyleName,
226 sal_Int16 nFamily,
227 sal_Int16 nPitch,
228 rtl_TextEncoding eEnc ) const
230 OUString sName;
231 XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
232 nPitch, eEnc );
233 ULONG nPos;
234 if( pPool->Seek_Entry( &aTmp, &nPos ) )
236 sName = pPool->GetObject( nPos )->GetName();
239 return sName;
243 void XMLFontAutoStylePool::exportXML()
245 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_OFFICE,
246 XML_FONT_FACE_DECLS,
247 sal_True, sal_True );
248 Any aAny;
249 OUString sTmp;
250 XMLFontFamilyNamePropHdl aFamilyNameHdl;
251 XMLFontFamilyPropHdl aFamilyHdl;
252 XMLFontPitchPropHdl aPitchHdl;
253 XMLFontEncodingPropHdl aEncHdl;
254 const SvXMLUnitConverter& rUnitConv = GetExport().GetMM100UnitConverter();
256 sal_uInt32 nCount = pPool->Count();
257 for( sal_uInt32 i=0; i<nCount; i++ )
259 const XMLFontAutoStylePoolEntry_Impl *pEntry = pPool->GetObject( i );
261 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
262 XML_NAME, pEntry->GetName() );
264 aAny <<= pEntry->GetFamilyName();
265 if( aFamilyNameHdl.exportXML( sTmp, aAny, rUnitConv ) )
266 GetExport().AddAttribute( XML_NAMESPACE_SVG,
267 XML_FONT_FAMILY, sTmp );
269 const OUString& rStyleName = pEntry->GetStyleName();
270 if( rStyleName.getLength() )
271 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
272 XML_FONT_ADORNMENTS,
273 rStyleName );
275 aAny <<= (sal_Int16)pEntry->GetFamily();
276 if( aFamilyHdl.exportXML( sTmp, aAny, rUnitConv ) )
277 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
278 XML_FONT_FAMILY_GENERIC, sTmp );
280 aAny <<= (sal_Int16)pEntry->GetPitch();
281 if( aPitchHdl.exportXML( sTmp, aAny, rUnitConv ) )
282 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
283 XML_FONT_PITCH, sTmp );
285 aAny <<= (sal_Int16)pEntry->GetEncoding();
286 if( aEncHdl.exportXML( sTmp, aAny, rUnitConv ) )
287 GetExport().AddAttribute( XML_NAMESPACE_STYLE,
288 XML_FONT_CHARSET, sTmp );
290 SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE,
291 XML_FONT_FACE,
292 sal_True, sal_True );