Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef INCLUDED_PDFI_STYLE_HXX
30 : : #define INCLUDED_PDFI_STYLE_HXX
31 : :
32 : : #include "pdfihelper.hxx"
33 : : #include <boost/unordered_map.hpp>
34 : : #include <vector>
35 : : #include <rtl/ustring.hxx>
36 : : #include <rtl/string.hxx>
37 : : #include "treevisiting.hxx"
38 : :
39 : : namespace pdfi
40 : : {
41 : : struct Element;
42 : : struct EmitContext;
43 : : struct ElementTreeVisitable;
44 : :
45 [ + - ]: 6 : class StyleContainer
46 : : {
47 : : public:
48 [ + - ]: 474 : struct Style
49 : : {
50 : : rtl::OString Name;
51 : : PropertyMap Properties;
52 : : rtl::OUString Contents;
53 : : Element* ContainedElement;
54 : : std::vector< Style* > SubStyles;
55 : :
56 : : Style() : ContainedElement( NULL ) {}
57 : 474 : Style( const rtl::OString& rName, const PropertyMap& rProps ) :
58 : : Name( rName ),
59 : : Properties( rProps ),
60 [ + - ][ + - ]: 474 : ContainedElement( NULL )
61 : 474 : {}
62 : : };
63 : :
64 : : private:
65 [ + - ][ # # ]: 1026 : struct HashedStyle
66 : : {
67 : : rtl::OString Name;
68 : : PropertyMap Properties;
69 : : rtl::OUString Contents;
70 : : Element* ContainedElement;
71 : : std::vector<sal_Int32> SubStyles;
72 : :
73 : : bool IsSubStyle;
74 : : sal_Int32 RefCount;
75 : :
76 [ + - ][ + - ]: 603 : HashedStyle() : ContainedElement( NULL ), IsSubStyle( true ), RefCount( 0 ) {}
77 : :
78 : 282 : HashedStyle( const HashedStyle& rRight ) :
79 : : Name( rRight.Name ),
80 : : Properties( rRight.Properties ),
81 : : Contents( rRight.Contents ),
82 : : ContainedElement( rRight.ContainedElement ),
83 : : SubStyles( rRight.SubStyles ),
84 : : IsSubStyle( rRight.IsSubStyle ),
85 [ + - ][ + - ]: 282 : RefCount( 0 )
86 : 282 : {}
87 : :
88 : 597 : size_t hashCode() const
89 : : {
90 : 597 : size_t nRet = size_t(Name.hashCode());
91 [ + + ][ + - ]: 4698 : for( PropertyMap::const_iterator it = Properties.begin();
92 [ + - ]: 2349 : it != Properties.end(); ++it )
93 : : {
94 [ + - ]: 1752 : nRet ^= size_t(it->first.hashCode());
95 [ + - ]: 1752 : nRet ^= size_t(it->second.hashCode());
96 : : }
97 : 597 : nRet = size_t(Contents.hashCode());
98 : 597 : nRet ^= size_t(ContainedElement);
99 [ + + ]: 849 : for( unsigned int n = 0; n < SubStyles.size(); ++n )
100 : 252 : nRet ^= size_t(SubStyles[n]);
101 : 597 : return nRet;
102 : : }
103 : :
104 : 2019 : bool operator==(const HashedStyle& rRight) const
105 : : {
106 [ + + + + : 3270 : if( Name != rRight.Name ||
+ - ]
[ + - - + ]
[ + + ]
107 : 609 : Properties != rRight.Properties ||
108 : 321 : Contents != rRight.Contents ||
109 : : ContainedElement != rRight.ContainedElement ||
110 : 321 : SubStyles.size() != rRight.SubStyles.size()
111 : : )
112 : 1698 : return false;
113 [ + + ]: 447 : for( unsigned int n = 0; n < SubStyles.size(); ++n )
114 : : {
115 [ - + ]: 126 : if( SubStyles[n] != rRight.SubStyles[n] )
116 : 0 : return false;
117 : : }
118 : 2019 : return true;
119 : : }
120 : : };
121 : :
122 : : struct StyleHash;
123 : : friend struct StyleHash;
124 : : struct StyleHash
125 : : {
126 : 597 : size_t operator()( const StyleContainer::HashedStyle& rStyle ) const
127 : : {
128 : 597 : return rStyle.hashCode();
129 : : }
130 : : };
131 : :
132 : : struct StyleIdNameSort;
133 : : friend struct StyleIdNameSort;
134 : : struct StyleIdNameSort
135 : : {
136 : : const boost::unordered_map< sal_Int32, HashedStyle >* m_pMap;
137 : :
138 : 15 : StyleIdNameSort( const boost::unordered_map< sal_Int32, HashedStyle >* pMap ) :
139 : 15 : m_pMap(pMap)
140 : 15 : {}
141 : 165 : bool operator()( sal_Int32 nLeft, sal_Int32 nRight )
142 : : {
143 : : const boost::unordered_map< sal_Int32, HashedStyle >::const_iterator left_it =
144 [ + - ]: 165 : m_pMap->find( nLeft );
145 : : const boost::unordered_map< sal_Int32, HashedStyle >::const_iterator right_it =
146 [ + - ]: 165 : m_pMap->find( nRight );
147 [ + - ][ - + ]: 165 : if( left_it == m_pMap->end() )
148 : 0 : return false;
149 [ + - ][ - + ]: 165 : else if( right_it == m_pMap->end() )
150 : 0 : return true;
151 : : else
152 [ + - ][ + - ]: 165 : return left_it->second.Name < right_it->second.Name;
153 : : }
154 : : };
155 : :
156 : : sal_Int32 m_nNextId;
157 : : boost::unordered_map< sal_Int32, HashedStyle > m_aIdToStyle;
158 : : boost::unordered_map< HashedStyle, sal_Int32, StyleHash > m_aStyleToId;
159 : :
160 : : void impl_emitStyle( sal_Int32 nStyleId,
161 : : EmitContext& rContext,
162 : : ElementTreeVisitor& rContainedElemVisitor );
163 : :
164 : : public:
165 : : StyleContainer();
166 : :
167 : : void emit( EmitContext& rContext,
168 : : ElementTreeVisitor& rContainedElemVisitor );
169 : :
170 : : sal_Int32 impl_getStyleId( const Style& rStyle, bool bSubStyle );
171 : 261 : sal_Int32 getStyleId( const Style& rStyle )
172 : 261 : { return impl_getStyleId( rStyle, false ); }
173 : : sal_Int32 getStandardStyleId( const rtl::OString& rFamily );
174 : :
175 : : // returns NULL for an invalid style id
176 : : const PropertyMap* getProperties( sal_Int32 nStyleId ) const;
177 : : sal_Int32 setProperties( sal_Int32 nStyleId, const PropertyMap &rNewProps );
178 : : rtl::OUString getStyleName( sal_Int32 nStyle ) const;
179 : : };
180 : : }
181 : :
182 : : #endif
183 : :
184 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|