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