Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "style.hxx"
22 : #include "genericelements.hxx"
23 : #include "xmlemitter.hxx"
24 : #include "pdfiprocessor.hxx"
25 : #include <rtl/ustrbuf.hxx>
26 :
27 : #include <algorithm>
28 :
29 : using namespace pdfi;
30 :
31 :
32 4 : StyleContainer::StyleContainer() :
33 4 : m_nNextId( 1 )
34 : {
35 4 : }
36 :
37 736 : sal_Int32 StyleContainer::impl_getStyleId( const Style& rStyle, bool bSubStyle )
38 : {
39 736 : sal_Int32 nRet = -1;
40 :
41 : // construct HashedStyle to find or insert
42 736 : HashedStyle aSearchStyle;
43 736 : aSearchStyle.Name = rStyle.Name;
44 736 : aSearchStyle.Properties = rStyle.Properties;
45 736 : aSearchStyle.Contents = rStyle.Contents;
46 736 : aSearchStyle.ContainedElement = rStyle.ContainedElement;
47 1085 : for( size_t n = 0; n < rStyle.SubStyles.size(); ++n )
48 349 : aSearchStyle.SubStyles.push_back( impl_getStyleId( *rStyle.SubStyles[n], true ) );
49 :
50 : std::unordered_map< HashedStyle, sal_Int32, StyleHash >::iterator it =
51 736 : m_aStyleToId.find( aSearchStyle );
52 :
53 736 : if( it != m_aStyleToId.end() )
54 : {
55 653 : nRet = it->second;
56 653 : HashedStyle& rFound = m_aIdToStyle[ nRet ];
57 : // increase refcount on this style
58 653 : rFound.RefCount++;
59 653 : if( ! bSubStyle )
60 339 : rFound.IsSubStyle = false;
61 : }
62 : else
63 : {
64 83 : nRet = m_nNextId++;
65 : // create new style
66 83 : HashedStyle& rNew = m_aIdToStyle[ nRet ];
67 83 : rNew = aSearchStyle;
68 83 : rNew.RefCount = 1;
69 83 : rNew.IsSubStyle = bSubStyle;
70 : // fill the style hash to find the id
71 83 : m_aStyleToId[ rNew ] = nRet;
72 : }
73 736 : return nRet;
74 : }
75 :
76 31 : sal_Int32 StyleContainer::getStandardStyleId( const OString& rName )
77 : {
78 31 : PropertyMap aProps;
79 31 : aProps[ "style:family" ] = OStringToOUString( rName, RTL_TEXTENCODING_UTF8 );
80 31 : aProps[ "style:name" ] = "standard";
81 :
82 62 : Style aStyle( "style:style", aProps );
83 62 : return getStyleId( aStyle );
84 : }
85 :
86 0 : const PropertyMap* StyleContainer::getProperties( sal_Int32 nStyleId ) const
87 : {
88 : std::unordered_map< sal_Int32, HashedStyle >::const_iterator it =
89 0 : m_aIdToStyle.find( nStyleId );
90 0 : return it != m_aIdToStyle.end() ? &(it->second.Properties) : NULL;
91 : }
92 :
93 0 : sal_Int32 StyleContainer::setProperties( sal_Int32 nStyleId, const PropertyMap& rNewProps )
94 : {
95 0 : sal_Int32 nRet = -1;
96 : std::unordered_map< sal_Int32, HashedStyle >::iterator it =
97 0 : m_aIdToStyle.find( nStyleId );
98 0 : if( it != m_aIdToStyle.end() )
99 : {
100 0 : if( it->second.RefCount == 1 )
101 : {
102 0 : nRet = it->first;
103 : // erase old hash to id mapping
104 0 : m_aStyleToId.erase( it->second );
105 : // change properties
106 0 : it->second.Properties = rNewProps;
107 : // fill in new hash to id mapping
108 0 : m_aStyleToId[ it->second ] = nRet;
109 : }
110 : else
111 : {
112 : // decrease refcound on old instance
113 0 : it->second.RefCount--;
114 : // acquire new HashedStyle
115 0 : HashedStyle aSearchStyle;
116 0 : aSearchStyle.Name = it->second.Name;
117 0 : aSearchStyle.Properties = rNewProps;
118 0 : aSearchStyle.Contents = it->second.Contents;
119 0 : aSearchStyle.ContainedElement = it->second.ContainedElement;
120 0 : aSearchStyle.SubStyles = it->second.SubStyles;
121 0 : aSearchStyle.IsSubStyle = it->second.IsSubStyle;
122 :
123 : // find out whether this new style already exists
124 : std::unordered_map< HashedStyle, sal_Int32, StyleHash >::iterator new_it =
125 0 : m_aStyleToId.find( aSearchStyle );
126 0 : if( new_it != m_aStyleToId.end() )
127 : {
128 0 : nRet = new_it->second;
129 0 : m_aIdToStyle[ nRet ].RefCount++;
130 : }
131 : else
132 : {
133 0 : nRet = m_nNextId++;
134 : // create new style with new id
135 0 : HashedStyle& rNew = m_aIdToStyle[ nRet ];
136 0 : rNew = aSearchStyle;
137 0 : rNew.RefCount = 1;
138 : // fill style to id hash
139 0 : m_aStyleToId[ aSearchStyle ] = nRet;
140 0 : }
141 : }
142 : }
143 0 : return nRet;
144 : }
145 :
146 463 : OUString StyleContainer::getStyleName( sal_Int32 nStyle ) const
147 : {
148 463 : OUStringBuffer aRet( 64 );
149 :
150 : std::unordered_map< sal_Int32, HashedStyle >::const_iterator style_it =
151 463 : m_aIdToStyle.find( nStyle );
152 463 : if( style_it != m_aIdToStyle.end() )
153 : {
154 463 : const HashedStyle& rStyle = style_it->second;
155 :
156 463 : PropertyMap::const_iterator name_it = rStyle.Properties.find( "style:name" );
157 463 : if( name_it != rStyle.Properties.end() )
158 12 : aRet.append( name_it->second );
159 : else
160 : {
161 451 : PropertyMap::const_iterator fam_it = rStyle.Properties.find( "style:family" );
162 451 : OUString aStyleName;
163 451 : if( fam_it != rStyle.Properties.end() )
164 : {
165 425 : aStyleName = fam_it->second;
166 : }
167 : else
168 26 : aStyleName = OStringToOUString( rStyle.Name, RTL_TEXTENCODING_ASCII_US );
169 451 : sal_Int32 nIndex = aStyleName.lastIndexOf( ':' );
170 451 : aRet.append( aStyleName.copy( nIndex+1 ) );
171 451 : aRet.append( nStyle );
172 : }
173 : }
174 : else
175 : {
176 0 : aRet.appendAscii( "invalid style id " );
177 0 : aRet.append( nStyle );
178 : }
179 :
180 463 : return aRet.makeStringAndClear();
181 : }
182 :
183 83 : void StyleContainer::impl_emitStyle( sal_Int32 nStyleId,
184 : EmitContext& rContext,
185 : ElementTreeVisitor& rContainedElemVisitor )
186 : {
187 83 : std::unordered_map< sal_Int32, HashedStyle >::const_iterator it = m_aIdToStyle.find( nStyleId );
188 83 : if( it != m_aIdToStyle.end() )
189 : {
190 83 : const HashedStyle& rStyle = it->second;
191 83 : PropertyMap aProps( rStyle.Properties );
192 83 : if( !rStyle.IsSubStyle )
193 48 : aProps[ "style:name" ] = getStyleName( nStyleId );
194 83 : if (rStyle.Name == "draw:stroke-dash")
195 2 : aProps[ "draw:name" ] = aProps[ "style:name" ];
196 83 : rContext.rEmitter.beginTag( rStyle.Name.getStr(), aProps );
197 :
198 118 : for( size_t n = 0; n < rStyle.SubStyles.size(); ++n )
199 35 : impl_emitStyle( rStyle.SubStyles[n], rContext, rContainedElemVisitor );
200 83 : if( !rStyle.Contents.isEmpty() )
201 0 : rContext.rEmitter.write( rStyle.Contents );
202 83 : if( rStyle.ContainedElement )
203 : rStyle.ContainedElement->visitedBy( rContainedElemVisitor,
204 0 : std::list<Element*>::iterator() );
205 83 : rContext.rEmitter.endTag( rStyle.Name.getStr() );
206 : }
207 83 : }
208 :
209 4 : void StyleContainer::emit( EmitContext& rContext,
210 : ElementTreeVisitor& rContainedElemVisitor )
211 : {
212 8 : std::vector< sal_Int32 > aMasterPageSection, aAutomaticStyleSection, aOfficeStyleSection;
213 261 : for( std::unordered_map< sal_Int32, HashedStyle >::iterator it = m_aIdToStyle.begin();
214 174 : it != m_aIdToStyle.end(); ++it )
215 : {
216 83 : if( ! it->second.IsSubStyle )
217 : {
218 48 : if( it->second.Name.equals( "style:master-page" ) )
219 4 : aMasterPageSection.push_back( it->first );
220 44 : else if( getStyleName( it->first ) == "standard" )
221 6 : aOfficeStyleSection.push_back( it->first );
222 : else
223 38 : aAutomaticStyleSection.push_back( it->first );
224 : }
225 : }
226 :
227 4 : if( ! aMasterPageSection.empty() )
228 4 : std::stable_sort( aMasterPageSection.begin(), aMasterPageSection.end(), StyleIdNameSort(&m_aIdToStyle) );
229 4 : if( ! aAutomaticStyleSection.empty() )
230 4 : std::stable_sort( aAutomaticStyleSection.begin(), aAutomaticStyleSection.end(), StyleIdNameSort(&m_aIdToStyle) );
231 4 : if( ! aOfficeStyleSection.empty() )
232 3 : std::stable_sort( aOfficeStyleSection.begin(), aOfficeStyleSection.end(), StyleIdNameSort(&m_aIdToStyle) );
233 :
234 4 : int n = 0, nElements = 0;
235 4 : rContext.rEmitter.beginTag( "office:styles", PropertyMap() );
236 10 : for( n = 0, nElements = aOfficeStyleSection.size(); n < nElements; n++ )
237 6 : impl_emitStyle( aOfficeStyleSection[n], rContext, rContainedElemVisitor );
238 4 : rContext.rEmitter.endTag( "office:styles" );
239 4 : rContext.rEmitter.beginTag( "office:automatic-styles", PropertyMap() );
240 42 : for( n = 0, nElements = aAutomaticStyleSection.size(); n < nElements; n++ )
241 38 : impl_emitStyle( aAutomaticStyleSection[n], rContext, rContainedElemVisitor );
242 4 : rContext.rEmitter.endTag( "office:automatic-styles" );
243 4 : rContext.rEmitter.beginTag( "office:master-styles", PropertyMap() );
244 8 : for( n = 0, nElements = aMasterPageSection.size(); n < nElements; n++ )
245 4 : impl_emitStyle( aMasterPageSection[n], rContext, rContainedElemVisitor );
246 8 : rContext.rEmitter.endTag( "office:master-styles" );
247 4 : }
248 :
249 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|