1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sdext.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "style.hxx"
32*cdf0e10cSrcweir #include "genericelements.hxx"
33*cdf0e10cSrcweir #include "xmlemitter.hxx"
34*cdf0e10cSrcweir #include "pdfiprocessor.hxx"
35*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <algorithm>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace rtl;
40*cdf0e10cSrcweir using namespace pdfi;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #define USTR(x) rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir StyleContainer::StyleContainer() :
45*cdf0e10cSrcweir     m_nNextId( 1 )
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir sal_Int32 StyleContainer::impl_getStyleId( const Style& rStyle, bool bSubStyle )
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     sal_Int32 nRet = -1;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     // construct HashedStyle to find or insert
54*cdf0e10cSrcweir     HashedStyle aSearchStyle;
55*cdf0e10cSrcweir     aSearchStyle.Name                   = rStyle.Name;
56*cdf0e10cSrcweir     aSearchStyle.Properties             = rStyle.Properties;
57*cdf0e10cSrcweir     aSearchStyle.Contents               = rStyle.Contents;
58*cdf0e10cSrcweir     aSearchStyle.ContainedElement       = rStyle.ContainedElement;
59*cdf0e10cSrcweir     for( unsigned int n = 0; n < rStyle.SubStyles.size(); ++n )
60*cdf0e10cSrcweir         aSearchStyle.SubStyles.push_back( impl_getStyleId( *rStyle.SubStyles[n], true ) );
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     std::hash_map< HashedStyle, sal_Int32, StyleHash >::iterator it =
63*cdf0e10cSrcweir         m_aStyleToId.find( aSearchStyle );
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     if( it != m_aStyleToId.end() )
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir         nRet = it->second;
68*cdf0e10cSrcweir         HashedStyle& rFound = m_aIdToStyle[ nRet ];
69*cdf0e10cSrcweir         // increase refcount on this style
70*cdf0e10cSrcweir         rFound.RefCount++;
71*cdf0e10cSrcweir         if( ! bSubStyle )
72*cdf0e10cSrcweir             rFound.IsSubStyle = false;
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir     else
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         nRet = m_nNextId++;
77*cdf0e10cSrcweir         // create new style
78*cdf0e10cSrcweir         HashedStyle& rNew = m_aIdToStyle[ nRet ];
79*cdf0e10cSrcweir         rNew = aSearchStyle;
80*cdf0e10cSrcweir         rNew.RefCount           = 1;
81*cdf0e10cSrcweir         rNew.IsSubStyle         = bSubStyle;
82*cdf0e10cSrcweir         // fill the style hash to find the id
83*cdf0e10cSrcweir         m_aStyleToId[ rNew ] = nRet;
84*cdf0e10cSrcweir     }
85*cdf0e10cSrcweir     return nRet;
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir sal_Int32 StyleContainer::getStandardStyleId( const rtl::OString& rName )
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir     PropertyMap aProps;
91*cdf0e10cSrcweir     aProps[ USTR( "style:family" ) ] = rtl::OStringToOUString( rName, RTL_TEXTENCODING_UTF8 );
92*cdf0e10cSrcweir     aProps[ USTR( "style:name" ) ] = USTR( "standard" );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     Style aStyle( "style:style", aProps );
95*cdf0e10cSrcweir     return getStyleId( aStyle );
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir const PropertyMap* StyleContainer::getProperties( sal_Int32 nStyleId ) const
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir     std::hash_map< sal_Int32, HashedStyle >::const_iterator it =
101*cdf0e10cSrcweir         m_aIdToStyle.find( nStyleId );
102*cdf0e10cSrcweir     return it != m_aIdToStyle.end() ? &(it->second.Properties) : NULL;
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir sal_Int32 StyleContainer::setProperties( sal_Int32 nStyleId, const PropertyMap& rNewProps )
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     sal_Int32 nRet = -1;
108*cdf0e10cSrcweir     std::hash_map< sal_Int32, HashedStyle >::iterator it =
109*cdf0e10cSrcweir         m_aIdToStyle.find( nStyleId );
110*cdf0e10cSrcweir     if( it != m_aIdToStyle.end() )
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         if( it->second.RefCount == 1 )
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             nRet = it->first;
115*cdf0e10cSrcweir             // erase old hash to id mapping
116*cdf0e10cSrcweir             m_aStyleToId.erase( it->second );
117*cdf0e10cSrcweir             // change properties
118*cdf0e10cSrcweir             it->second.Properties = rNewProps;
119*cdf0e10cSrcweir             // fill in new hash to id mapping
120*cdf0e10cSrcweir             m_aStyleToId[ it->second ] = nRet;
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir         else
123*cdf0e10cSrcweir         {
124*cdf0e10cSrcweir             // decrease refcound on old instance
125*cdf0e10cSrcweir             it->second.RefCount--;
126*cdf0e10cSrcweir             // acquire new HashedStyle
127*cdf0e10cSrcweir             HashedStyle aSearchStyle;
128*cdf0e10cSrcweir             aSearchStyle.Name                   = it->second.Name;
129*cdf0e10cSrcweir             aSearchStyle.Properties             = rNewProps;
130*cdf0e10cSrcweir             aSearchStyle.Contents               = it->second.Contents;
131*cdf0e10cSrcweir             aSearchStyle.ContainedElement       = it->second.ContainedElement;
132*cdf0e10cSrcweir             aSearchStyle.SubStyles              = it->second.SubStyles;
133*cdf0e10cSrcweir             aSearchStyle.IsSubStyle             = it->second.IsSubStyle;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir             // find out whether this new style already exists
136*cdf0e10cSrcweir             std::hash_map< HashedStyle, sal_Int32, StyleHash >::iterator new_it =
137*cdf0e10cSrcweir                 m_aStyleToId.find( aSearchStyle );
138*cdf0e10cSrcweir             if( new_it != m_aStyleToId.end() )
139*cdf0e10cSrcweir             {
140*cdf0e10cSrcweir                 nRet = new_it->second;
141*cdf0e10cSrcweir                 m_aIdToStyle[ nRet ].RefCount++;
142*cdf0e10cSrcweir             }
143*cdf0e10cSrcweir             else
144*cdf0e10cSrcweir             {
145*cdf0e10cSrcweir                 nRet = m_nNextId++;
146*cdf0e10cSrcweir                 // create new style with new id
147*cdf0e10cSrcweir                 HashedStyle& rNew = m_aIdToStyle[ nRet ];
148*cdf0e10cSrcweir                 rNew = aSearchStyle;
149*cdf0e10cSrcweir                 rNew.RefCount = 1;
150*cdf0e10cSrcweir                 // fill style to id hash
151*cdf0e10cSrcweir                 m_aStyleToId[ aSearchStyle ] = nRet;
152*cdf0e10cSrcweir             }
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir     return nRet;
156*cdf0e10cSrcweir }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir OUString StyleContainer::getStyleName( sal_Int32 nStyle ) const
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir     OUStringBuffer aRet( 64 );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     std::hash_map< sal_Int32, HashedStyle >::const_iterator style_it =
163*cdf0e10cSrcweir         m_aIdToStyle.find( nStyle );
164*cdf0e10cSrcweir     if( style_it != m_aIdToStyle.end() )
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         const HashedStyle& rStyle = style_it->second;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         PropertyMap::const_iterator name_it = rStyle.Properties.find( USTR("style:name") );
169*cdf0e10cSrcweir         if( name_it != rStyle.Properties.end() )
170*cdf0e10cSrcweir             aRet.append( name_it->second );
171*cdf0e10cSrcweir         else
172*cdf0e10cSrcweir         {
173*cdf0e10cSrcweir             PropertyMap::const_iterator fam_it = rStyle.Properties.find( USTR("style:family" ) );
174*cdf0e10cSrcweir             OUString aStyleName;
175*cdf0e10cSrcweir             if( fam_it != rStyle.Properties.end() )
176*cdf0e10cSrcweir             {
177*cdf0e10cSrcweir                 aStyleName = fam_it->second;
178*cdf0e10cSrcweir             }
179*cdf0e10cSrcweir             else
180*cdf0e10cSrcweir                 aStyleName = OStringToOUString( rStyle.Name, RTL_TEXTENCODING_ASCII_US );
181*cdf0e10cSrcweir             sal_Int32 nIndex = aStyleName.lastIndexOf( ':' );
182*cdf0e10cSrcweir             aRet.append( aStyleName.copy( nIndex+1 ) );
183*cdf0e10cSrcweir             aRet.append( nStyle );
184*cdf0e10cSrcweir         }
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir     else
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         aRet.appendAscii( "invalid style id " );
189*cdf0e10cSrcweir         aRet.append( nStyle );
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     return aRet.makeStringAndClear();
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir void StyleContainer::impl_emitStyle( sal_Int32           nStyleId,
196*cdf0e10cSrcweir                                      EmitContext&        rContext,
197*cdf0e10cSrcweir                                      ElementTreeVisitor& rContainedElemVisitor )
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir     std::hash_map< sal_Int32, HashedStyle >::const_iterator it = m_aIdToStyle.find( nStyleId );
200*cdf0e10cSrcweir     if( it != m_aIdToStyle.end() )
201*cdf0e10cSrcweir     {
202*cdf0e10cSrcweir         const HashedStyle& rStyle = it->second;
203*cdf0e10cSrcweir             PropertyMap aProps( rStyle.Properties );
204*cdf0e10cSrcweir         if( !rStyle.IsSubStyle )
205*cdf0e10cSrcweir             aProps[ USTR( "style:name" ) ] = getStyleName( nStyleId );
206*cdf0e10cSrcweir         rContext.rEmitter.beginTag( rStyle.Name.getStr(), aProps );
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         for( unsigned int n = 0; n < rStyle.SubStyles.size(); ++n )
209*cdf0e10cSrcweir             impl_emitStyle( rStyle.SubStyles[n], rContext, rContainedElemVisitor );
210*cdf0e10cSrcweir         if( rStyle.Contents )
211*cdf0e10cSrcweir             rContext.rEmitter.write( rStyle.Contents );
212*cdf0e10cSrcweir         if( rStyle.ContainedElement )
213*cdf0e10cSrcweir             rStyle.ContainedElement->visitedBy( rContainedElemVisitor,
214*cdf0e10cSrcweir                                                 std::list<Element*>::iterator() );
215*cdf0e10cSrcweir         rContext.rEmitter.endTag( rStyle.Name.getStr() );
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir void StyleContainer::emit( EmitContext&        rContext,
220*cdf0e10cSrcweir                            ElementTreeVisitor& rContainedElemVisitor )
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir     std::vector< sal_Int32 > aMasterPageSection, aAutomaticStyleSection, aOfficeStyleSection;
223*cdf0e10cSrcweir     for( std::hash_map< sal_Int32, HashedStyle >::iterator it = m_aIdToStyle.begin();
224*cdf0e10cSrcweir          it != m_aIdToStyle.end(); ++it )
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         if( ! it->second.IsSubStyle )
227*cdf0e10cSrcweir         {
228*cdf0e10cSrcweir             if( it->second.Name.equals( "style:master-page" ) )
229*cdf0e10cSrcweir                 aMasterPageSection.push_back( it->first );
230*cdf0e10cSrcweir             else if( getStyleName( it->first ).equalsAscii( "standard" ) )
231*cdf0e10cSrcweir                 aOfficeStyleSection.push_back( it->first );
232*cdf0e10cSrcweir             else
233*cdf0e10cSrcweir                 aAutomaticStyleSection.push_back( it->first );
234*cdf0e10cSrcweir         }
235*cdf0e10cSrcweir     }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     if( ! aMasterPageSection.empty() )
238*cdf0e10cSrcweir         std::stable_sort( aMasterPageSection.begin(), aMasterPageSection.end(), StyleIdNameSort(&m_aIdToStyle) );
239*cdf0e10cSrcweir     if( ! aAutomaticStyleSection.empty() )
240*cdf0e10cSrcweir         std::stable_sort( aAutomaticStyleSection.begin(), aAutomaticStyleSection.end(), StyleIdNameSort(&m_aIdToStyle) );
241*cdf0e10cSrcweir     if( ! aOfficeStyleSection.empty() )
242*cdf0e10cSrcweir         std::stable_sort( aOfficeStyleSection.begin(), aOfficeStyleSection.end(), StyleIdNameSort(&m_aIdToStyle) );
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     int n = 0, nElements = 0;
245*cdf0e10cSrcweir     rContext.rEmitter.beginTag( "office:styles", PropertyMap() );
246*cdf0e10cSrcweir     for( n = 0, nElements = aOfficeStyleSection.size(); n < nElements; n++ )
247*cdf0e10cSrcweir         impl_emitStyle( aOfficeStyleSection[n], rContext, rContainedElemVisitor );
248*cdf0e10cSrcweir     rContext.rEmitter.endTag( "office:styles" );
249*cdf0e10cSrcweir     rContext.rEmitter.beginTag( "office:automatic-styles", PropertyMap() );
250*cdf0e10cSrcweir     for( n = 0, nElements = aAutomaticStyleSection.size(); n < nElements; n++ )
251*cdf0e10cSrcweir         impl_emitStyle( aAutomaticStyleSection[n], rContext, rContainedElemVisitor );
252*cdf0e10cSrcweir     rContext.rEmitter.endTag( "office:automatic-styles" );
253*cdf0e10cSrcweir     rContext.rEmitter.beginTag( "office:master-styles", PropertyMap() );
254*cdf0e10cSrcweir     for( n = 0, nElements = aMasterPageSection.size(); n < nElements; n++ )
255*cdf0e10cSrcweir         impl_emitStyle( aMasterPageSection[n], rContext, rContainedElemVisitor );
256*cdf0e10cSrcweir     rContext.rEmitter.endTag( "office:master-styles" );
257*cdf0e10cSrcweir }
258