xref: /AOO41X/main/xmloff/source/style/XMLFontAutoStylePool.cxx (revision 63bba73cc51e0afb45f8a8d578158724bb5afee8)
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_xmloff.hxx"
26 #include <tools/debug.hxx>
27 #include <svl/cntnrsrt.hxx>
28 #include <tools/fontenum.hxx>
29 #include "xmloff/xmlnmspe.hxx"
30 #include <xmloff/xmltoken.hxx>
31 #include <xmloff/xmluconv.hxx>
32 #include "fonthdl.hxx"
33 #include <xmloff/xmlexp.hxx>
34 #include <xmloff/XMLFontAutoStylePool.hxx>
35 
36 
37 using ::rtl::OUString;
38 using ::rtl::OUStringBuffer;
39 
40 using namespace ::com::sun::star::uno;
41 using namespace ::xmloff::token;
42 
XMLFontAutoStylePoolNameCmp_Impl(const OUString & r1,const OUString & r2)43 int XMLFontAutoStylePoolNameCmp_Impl( const OUString& r1,
44                                       const OUString& r2 )
45 {
46     return (int)r1.compareTo( r2 );
47 }
48 
49 DECLARE_CONTAINER_SORT_DEL( XMLFontAutoStylePoolNames_Impl,
50                             OUString )
51 IMPL_CONTAINER_SORT( XMLFontAutoStylePoolNames_Impl,
52                      OUString,
53                      XMLFontAutoStylePoolNameCmp_Impl )
54 
55 class XMLFontAutoStylePoolEntry_Impl
56 {
57     OUString    sName;
58     OUString    sFamilyName;
59     OUString    sStyleName;
60     sal_Int16   nFamily;
61     sal_Int16   nPitch;
62     rtl_TextEncoding eEnc;
63 
64 public:
65 
66     inline XMLFontAutoStylePoolEntry_Impl(
67             const ::rtl::OUString& rName,
68             const ::rtl::OUString& rFamilyName,
69             const ::rtl::OUString& rStyleName,
70             sal_Int16 nFamily,
71             sal_Int16 nPitch,
72             rtl_TextEncoding eEnc );
73 
74     inline XMLFontAutoStylePoolEntry_Impl(
75             const ::rtl::OUString& rFamilyName,
76             const ::rtl::OUString& rStyleName,
77             sal_Int16 nFamily,
78             sal_Int16 nPitch,
79             rtl_TextEncoding eEnc );
80 
GetName() const81     const OUString& GetName() const { return sName; }
GetFamilyName() const82     const OUString& GetFamilyName() const { return sFamilyName; }
GetStyleName() const83     const OUString& GetStyleName() const { return sStyleName; }
GetFamily() const84     sal_Int16 GetFamily() const {   return nFamily; }
GetPitch() const85     sal_Int16 GetPitch() const { return nPitch; }
GetEncoding() const86     rtl_TextEncoding GetEncoding() const { return eEnc; }
87 };
88 
89 
XMLFontAutoStylePoolEntry_Impl(const::rtl::OUString & rName,const::rtl::OUString & rFamilyName,const::rtl::OUString & rStyleName,sal_Int16 nFam,sal_Int16 nP,rtl_TextEncoding eE)90 inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
91         const ::rtl::OUString& rName,
92         const ::rtl::OUString& rFamilyName,
93         const ::rtl::OUString& rStyleName,
94         sal_Int16 nFam,
95         sal_Int16 nP,
96         rtl_TextEncoding eE ) :
97     sName( rName ),
98     sFamilyName( rFamilyName ),
99     sStyleName( rStyleName ),
100     nFamily( nFam ),
101     nPitch( nP ),
102     eEnc( eE )
103 {
104 }
105 
XMLFontAutoStylePoolEntry_Impl(const::rtl::OUString & rFamilyName,const::rtl::OUString & rStyleName,sal_Int16 nFam,sal_Int16 nP,rtl_TextEncoding eE)106 inline XMLFontAutoStylePoolEntry_Impl::XMLFontAutoStylePoolEntry_Impl(
107         const ::rtl::OUString& rFamilyName,
108         const ::rtl::OUString& rStyleName,
109         sal_Int16 nFam,
110         sal_Int16 nP,
111         rtl_TextEncoding eE ) :
112     sFamilyName( rFamilyName ),
113     sStyleName( rStyleName ),
114     nFamily( nFam ),
115     nPitch( nP ),
116     eEnc( eE )
117 {
118 }
XMLFontAutoStylePoolEntryCmp_Impl(const XMLFontAutoStylePoolEntry_Impl & r1,const XMLFontAutoStylePoolEntry_Impl & r2)119 int XMLFontAutoStylePoolEntryCmp_Impl(
120         const XMLFontAutoStylePoolEntry_Impl& r1,
121         const XMLFontAutoStylePoolEntry_Impl& r2 )
122 {
123     sal_Int8 nEnc1(r1.GetEncoding() != RTL_TEXTENCODING_SYMBOL);
124     sal_Int8 nEnc2(r2.GetEncoding() != RTL_TEXTENCODING_SYMBOL);
125     if( nEnc1 != nEnc2 )
126         return nEnc1 - nEnc2;
127     else if( r1.GetPitch() != r2.GetPitch() )
128         return (int)r1.GetPitch() - (int)r2.GetPitch();
129     else if( r1.GetFamily() != r2.GetFamily() )
130         return (int)r1.GetFamily() - (int)r2.GetFamily();
131     else
132     {
133         sal_Int32 nCmp = r1.GetFamilyName().compareTo( r2.GetFamilyName() );
134         if( 0 == nCmp )
135             return (int)r1.GetStyleName().compareTo( r2.GetStyleName() );
136         else
137             return (int)nCmp;
138     }
139 }
140 
141 typedef XMLFontAutoStylePoolEntry_Impl *XMLFontAutoStylePoolEntryPtr;
DECLARE_CONTAINER_SORT_DEL(XMLFontAutoStylePool_Impl,XMLFontAutoStylePoolEntry_Impl)142 DECLARE_CONTAINER_SORT_DEL( XMLFontAutoStylePool_Impl,
143                             XMLFontAutoStylePoolEntry_Impl )
144 IMPL_CONTAINER_SORT( XMLFontAutoStylePool_Impl,
145                      XMLFontAutoStylePoolEntry_Impl,
146                      XMLFontAutoStylePoolEntryCmp_Impl )
147 
148 XMLFontAutoStylePool::XMLFontAutoStylePool( SvXMLExport& rExp ) :
149     rExport( rExp ),
150     pPool( new XMLFontAutoStylePool_Impl( 5, 5 ) ),
151     pNames( new XMLFontAutoStylePoolNames_Impl( 5, 5 ) )
152 {
153 }
154 
~XMLFontAutoStylePool()155 XMLFontAutoStylePool::~XMLFontAutoStylePool()
156 {
157     delete pPool;
158     delete pNames;
159 }
160 
Add(const OUString & rFamilyName,const OUString & rStyleName,sal_Int16 nFamily,sal_Int16 nPitch,rtl_TextEncoding eEnc)161 OUString XMLFontAutoStylePool::Add(
162             const OUString& rFamilyName,
163             const OUString& rStyleName,
164             sal_Int16 nFamily,
165             sal_Int16 nPitch,
166             rtl_TextEncoding eEnc )
167 {
168     OUString sPoolName;
169     XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
170                                          nPitch, eEnc );
171     sal_uLong nPos;
172     if( pPool->Seek_Entry( &aTmp, &nPos ) )
173     {
174         sPoolName = pPool->GetObject( nPos )->GetName();
175     }
176     else
177     {
178         OUString sName;
179         sal_Int32 nLen = rFamilyName.indexOf( sal_Unicode(';'), 0 );
180         if( -1 == nLen )
181         {
182             sName = rFamilyName;
183         }
184         else if( nLen > 0 )
185         {
186             sName = rFamilyName.copy( 0, nLen );
187             sName.trim();
188         }
189 
190         if( !sName.getLength() )
191             sName = OUString::valueOf( sal_Unicode( 'F' ) );
192 
193         if( pNames->Seek_Entry( &sName, 0 ) )
194         {
195             sal_Int32 nCount = 1;
196             OUString sPrefix( sName );
197             sName += OUString::valueOf( nCount );
198             while( pNames->Seek_Entry( &sName, 0 ) )
199             {
200                 sName = sPrefix;
201                 sName += OUString::valueOf( ++nCount );
202             }
203         }
204 
205         XMLFontAutoStylePoolEntry_Impl *pEntry =
206             new XMLFontAutoStylePoolEntry_Impl( sName, rFamilyName, rStyleName,
207                                                 nFamily, nPitch, eEnc );
208         pPool->Insert( pEntry );
209         pNames->Insert( new OUString( sName ) );
210     }
211 
212     return sPoolName;
213 }
214 
Find(const OUString & rFamilyName,const OUString & rStyleName,sal_Int16 nFamily,sal_Int16 nPitch,rtl_TextEncoding eEnc) const215 ::rtl::OUString XMLFontAutoStylePool::Find(
216             const OUString& rFamilyName,
217             const OUString& rStyleName,
218             sal_Int16 nFamily,
219             sal_Int16 nPitch,
220             rtl_TextEncoding eEnc ) const
221 {
222     OUString sName;
223     XMLFontAutoStylePoolEntry_Impl aTmp( rFamilyName, rStyleName, nFamily,
224                                          nPitch, eEnc );
225     sal_uLong nPos;
226     if( pPool->Seek_Entry( &aTmp, &nPos ) )
227     {
228         sName = pPool->GetObject( nPos )->GetName();
229     }
230 
231     return sName;
232 }
233 
234 
exportXML()235 void XMLFontAutoStylePool::exportXML()
236 {
237     SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_OFFICE,
238                               XML_FONT_FACE_DECLS,
239                               sal_True, sal_True );
240     Any aAny;
241     OUString sTmp;
242     XMLFontFamilyNamePropHdl aFamilyNameHdl;
243     XMLFontFamilyPropHdl aFamilyHdl;
244     XMLFontPitchPropHdl aPitchHdl;
245     XMLFontEncodingPropHdl aEncHdl;
246     const SvXMLUnitConverter& rUnitConv = GetExport().GetMM100UnitConverter();
247 
248     sal_uInt32 nCount = pPool->Count();
249     for( sal_uInt32 i=0; i<nCount; i++ )
250     {
251         const XMLFontAutoStylePoolEntry_Impl *pEntry = pPool->GetObject( i );
252 
253         GetExport().AddAttribute( XML_NAMESPACE_STYLE,
254                                   XML_NAME, pEntry->GetName() );
255 
256         aAny <<= pEntry->GetFamilyName();
257         if( aFamilyNameHdl.exportXML( sTmp, aAny, rUnitConv ) )
258             GetExport().AddAttribute( XML_NAMESPACE_SVG,
259                                       XML_FONT_FAMILY, sTmp );
260 
261         const OUString& rStyleName = pEntry->GetStyleName();
262         if( rStyleName.getLength() )
263             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
264                                       XML_FONT_ADORNMENTS,
265                                       rStyleName );
266 
267         aAny <<= (sal_Int16)pEntry->GetFamily();
268         if( aFamilyHdl.exportXML( sTmp, aAny, rUnitConv  ) )
269             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
270                                       XML_FONT_FAMILY_GENERIC, sTmp );
271 
272         aAny <<= (sal_Int16)pEntry->GetPitch();
273         if( aPitchHdl.exportXML( sTmp, aAny, rUnitConv  ) )
274             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
275                                       XML_FONT_PITCH, sTmp );
276 
277         aAny <<= (sal_Int16)pEntry->GetEncoding();
278         if( aEncHdl.exportXML( sTmp, aAny, rUnitConv  ) )
279             GetExport().AddAttribute( XML_NAMESPACE_STYLE,
280                                       XML_FONT_CHARSET, sTmp );
281 
282         SvXMLElementExport aElement( GetExport(), XML_NAMESPACE_STYLE,
283                                   XML_FONT_FACE,
284                                   sal_True, sal_True );
285     }
286 }
287 
288 
289