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 #ifndef INCLUDED_PDFI_STYLE_HXX
29*cdf0e10cSrcweir #define INCLUDED_PDFI_STYLE_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "pdfihelper.hxx"
32*cdf0e10cSrcweir #include <hash_map>
33*cdf0e10cSrcweir #include <vector>
34*cdf0e10cSrcweir #include <rtl/ustring.hxx>
35*cdf0e10cSrcweir #include <rtl/string.hxx>
36*cdf0e10cSrcweir #include "treevisiting.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir namespace pdfi
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir     struct Element;
41*cdf0e10cSrcweir     struct EmitContext;
42*cdf0e10cSrcweir     struct ElementTreeVisitable;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir     class StyleContainer
45*cdf0e10cSrcweir     {
46*cdf0e10cSrcweir     public:
47*cdf0e10cSrcweir         struct Style
48*cdf0e10cSrcweir         {
49*cdf0e10cSrcweir             rtl::OString             Name;
50*cdf0e10cSrcweir             PropertyMap              Properties;
51*cdf0e10cSrcweir             rtl::OUString            Contents;
52*cdf0e10cSrcweir             Element*                 ContainedElement;
53*cdf0e10cSrcweir             std::vector< Style* >    SubStyles;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir             Style() : ContainedElement( NULL )  {}
56*cdf0e10cSrcweir             Style( const rtl::OString& rName, const PropertyMap& rProps ) :
57*cdf0e10cSrcweir                 Name( rName ),
58*cdf0e10cSrcweir                 Properties( rProps ),
59*cdf0e10cSrcweir                 ContainedElement( NULL )
60*cdf0e10cSrcweir             {}
61*cdf0e10cSrcweir         };
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     private:
64*cdf0e10cSrcweir         struct HashedStyle
65*cdf0e10cSrcweir         {
66*cdf0e10cSrcweir             rtl::OString            Name;
67*cdf0e10cSrcweir             PropertyMap             Properties;
68*cdf0e10cSrcweir             rtl::OUString           Contents;
69*cdf0e10cSrcweir             Element*                ContainedElement;
70*cdf0e10cSrcweir             std::vector<sal_Int32>  SubStyles;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir             bool                    IsSubStyle;
73*cdf0e10cSrcweir             sal_Int32               RefCount;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir             HashedStyle() : ContainedElement( NULL ), IsSubStyle( true ), RefCount( 0 ) {}
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir             HashedStyle( const HashedStyle& rRight ) :
78*cdf0e10cSrcweir                 Name( rRight.Name ),
79*cdf0e10cSrcweir                 Properties( rRight.Properties ),
80*cdf0e10cSrcweir                 Contents( rRight.Contents ),
81*cdf0e10cSrcweir                 ContainedElement( rRight.ContainedElement ),
82*cdf0e10cSrcweir                 SubStyles( rRight.SubStyles ),
83*cdf0e10cSrcweir                 IsSubStyle( rRight.IsSubStyle ),
84*cdf0e10cSrcweir                 RefCount( 0 )
85*cdf0e10cSrcweir             {}
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir             size_t hashCode() const
88*cdf0e10cSrcweir             {
89*cdf0e10cSrcweir                 size_t nRet = size_t(Name.hashCode());
90*cdf0e10cSrcweir                 for( PropertyMap::const_iterator it = Properties.begin();
91*cdf0e10cSrcweir                      it != Properties.end(); ++it )
92*cdf0e10cSrcweir                 {
93*cdf0e10cSrcweir                      nRet ^= size_t(it->first.hashCode());
94*cdf0e10cSrcweir                      nRet ^= size_t(it->second.hashCode());
95*cdf0e10cSrcweir                 }
96*cdf0e10cSrcweir                 nRet = size_t(Contents.hashCode());
97*cdf0e10cSrcweir                 nRet ^= size_t(ContainedElement);
98*cdf0e10cSrcweir                 for( unsigned int n = 0; n < SubStyles.size(); ++n )
99*cdf0e10cSrcweir                      nRet ^= size_t(SubStyles[n]);
100*cdf0e10cSrcweir                 return nRet;
101*cdf0e10cSrcweir             }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir             bool operator==(const HashedStyle& rRight) const
104*cdf0e10cSrcweir             {
105*cdf0e10cSrcweir                 if( Name != rRight.Name                 ||
106*cdf0e10cSrcweir                     Properties != rRight.Properties     ||
107*cdf0e10cSrcweir                     Contents != rRight.Contents         ||
108*cdf0e10cSrcweir                     ContainedElement != rRight.ContainedElement ||
109*cdf0e10cSrcweir                     SubStyles.size() != rRight.SubStyles.size()
110*cdf0e10cSrcweir                     )
111*cdf0e10cSrcweir                     return false;
112*cdf0e10cSrcweir                 for( unsigned int n = 0; n < SubStyles.size(); ++n )
113*cdf0e10cSrcweir                 {
114*cdf0e10cSrcweir                     if( SubStyles[n] != rRight.SubStyles[n] )
115*cdf0e10cSrcweir                         return false;
116*cdf0e10cSrcweir                 }
117*cdf0e10cSrcweir                 return true;
118*cdf0e10cSrcweir             }
119*cdf0e10cSrcweir         };
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         struct StyleHash;
122*cdf0e10cSrcweir         friend struct StyleHash;
123*cdf0e10cSrcweir         struct StyleHash
124*cdf0e10cSrcweir         {
125*cdf0e10cSrcweir             size_t operator()( const StyleContainer::HashedStyle& rStyle ) const
126*cdf0e10cSrcweir             {
127*cdf0e10cSrcweir                 return rStyle.hashCode();
128*cdf0e10cSrcweir             }
129*cdf0e10cSrcweir         };
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         struct StyleIdNameSort;
132*cdf0e10cSrcweir         friend struct StyleIdNameSort;
133*cdf0e10cSrcweir         struct StyleIdNameSort
134*cdf0e10cSrcweir         {
135*cdf0e10cSrcweir             const std::hash_map< sal_Int32, HashedStyle >* m_pMap;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir             StyleIdNameSort( const std::hash_map< sal_Int32, HashedStyle >* pMap ) :
138*cdf0e10cSrcweir                 m_pMap(pMap)
139*cdf0e10cSrcweir             {}
140*cdf0e10cSrcweir             bool operator()( sal_Int32 nLeft, sal_Int32 nRight )
141*cdf0e10cSrcweir             {
142*cdf0e10cSrcweir                 const std::hash_map< sal_Int32, HashedStyle >::const_iterator left_it =
143*cdf0e10cSrcweir                     m_pMap->find( nLeft );
144*cdf0e10cSrcweir                 const std::hash_map< sal_Int32, HashedStyle >::const_iterator right_it =
145*cdf0e10cSrcweir                     m_pMap->find( nRight );
146*cdf0e10cSrcweir                 if( left_it == m_pMap->end() )
147*cdf0e10cSrcweir                     return false;
148*cdf0e10cSrcweir                 else if( right_it == m_pMap->end() )
149*cdf0e10cSrcweir                     return true;
150*cdf0e10cSrcweir                 else
151*cdf0e10cSrcweir                     return left_it->second.Name < right_it->second.Name;
152*cdf0e10cSrcweir             }
153*cdf0e10cSrcweir         };
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         sal_Int32                                               m_nNextId;
156*cdf0e10cSrcweir         std::hash_map< sal_Int32, HashedStyle >                 m_aIdToStyle;
157*cdf0e10cSrcweir         std::hash_map< HashedStyle, sal_Int32, StyleHash >      m_aStyleToId;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         void impl_emitStyle( sal_Int32           nStyleId,
160*cdf0e10cSrcweir                              EmitContext&        rContext,
161*cdf0e10cSrcweir                              ElementTreeVisitor& rContainedElemVisitor );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     public:
164*cdf0e10cSrcweir         StyleContainer();
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir         void emit( EmitContext&        rContext,
167*cdf0e10cSrcweir                    ElementTreeVisitor& rContainedElemVisitor );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         sal_Int32 impl_getStyleId( const Style& rStyle, bool bSubStyle );
170*cdf0e10cSrcweir         sal_Int32 getStyleId( const Style& rStyle )
171*cdf0e10cSrcweir         { return impl_getStyleId( rStyle, false ); }
172*cdf0e10cSrcweir         sal_Int32 getStandardStyleId( const rtl::OString& rFamily );
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         // returns NULL for an invalid style id
175*cdf0e10cSrcweir         const PropertyMap* getProperties( sal_Int32 nStyleId ) const;
176*cdf0e10cSrcweir         sal_Int32 setProperties( sal_Int32 nStyleId, const PropertyMap &rNewProps );
177*cdf0e10cSrcweir         rtl::OUString getStyleName( sal_Int32 nStyle ) const;
178*cdf0e10cSrcweir     };
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir #endif
182