Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * The Contents of this file are made available subject to the terms of
5 : : * either of the following licenses
6 : : *
7 : : * - GNU Lesser General Public License Version 2.1
8 : : * - Sun Industry Standards Source License Version 1.1
9 : : *
10 : : * Sun Microsystems Inc., October, 2000
11 : : *
12 : : * GNU Lesser General Public License Version 2.1
13 : : * =============================================
14 : : * Copyright 2000 by Sun Microsystems, Inc.
15 : : * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 : : *
17 : : * This library is free software; you can redistribute it and/or
18 : : * modify it under the terms of the GNU Lesser General Public
19 : : * License version 2.1, as published by the Free Software Foundation.
20 : : *
21 : : * This library is distributed in the hope that it will be useful,
22 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : : * Lesser General Public License for more details.
25 : : *
26 : : * You should have received a copy of the GNU Lesser General Public
27 : : * License along with this library; if not, write to the Free Software
28 : : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 : : * MA 02111-1307 USA
30 : : *
31 : : *
32 : : * Sun Industry Standards Source License Version 1.1
33 : : * =================================================
34 : : * The contents of this file are subject to the Sun Industry Standards
35 : : * Source License Version 1.1 (the "License"); You may not use this file
36 : : * except in compliance with the License. You may obtain a copy of the
37 : : * License at http://www.openoffice.org/license.html.
38 : : *
39 : : * Software provided under this License is provided on an "AS IS" basis,
40 : : * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 : : * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 : : * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 : : * See the License for the specific provisions governing your rights and
44 : : * obligations concerning the Software.
45 : : *
46 : : * The Initial Developer of the Original Code is: IBM Corporation
47 : : *
48 : : * Copyright: 2008 by IBM Corporation
49 : : *
50 : : * All Rights Reserved.
51 : : *
52 : : * Contributor(s): _______________________________________
53 : : *
54 : : *
55 : : ************************************************************************/
56 : : /*************************************************************************
57 : : * @file
58 : : * Font object to serial to xml filter.
59 : : ************************************************************************/
60 : : #include "xfstylecont.hxx"
61 : : #include "ixfstyle.hxx"
62 : : #include "xffont.hxx"
63 : : #include "xftextstyle.hxx"
64 : : #include "xfparastyle.hxx"
65 : : #include "xffontfactory.hxx"
66 : : #include "../lwpglobalmgr.hxx"
67 : :
68 : 234 : XFStyleContainer::XFStyleContainer(const rtl::OUString& strStyleNamePrefix)
69 [ + - ]: 234 : :m_strStyleNamePrefix(strStyleNamePrefix)
70 : : {
71 : 234 : }
72 : :
73 : 0 : XFStyleContainer::XFStyleContainer(const XFStyleContainer& other):IXFObject(other),
74 [ # # ]: 0 : m_strStyleNamePrefix(other.m_strStyleNamePrefix)
75 : : {
76 [ # # ]: 0 : this->m_aStyles = other.m_aStyles;
77 : 0 : }
78 : :
79 : 0 : XFStyleContainer& XFStyleContainer::operator=(const XFStyleContainer& other)
80 : : {
81 : 0 : this->m_strStyleNamePrefix = other.m_strStyleNamePrefix;
82 : 0 : this->m_aStyles = other.m_aStyles;
83 : 0 : return *this;
84 : : }
85 : :
86 : 1131 : XFStyleContainer::~XFStyleContainer()
87 : : {
88 : 1131 : std::vector<IXFStyle*>::iterator it;
89 [ + - ][ + + ]: 1320 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
90 : : {
91 : 189 : IXFStyle *pStyle = *it;
92 [ + - ]: 189 : if( pStyle )
93 [ + - ][ + - ]: 189 : delete pStyle;
94 : : }
95 [ - + ]: 1131 : }
96 : :
97 : :
98 : 852 : void XFStyleContainer::Reset()
99 : : {
100 : 852 : std::vector<IXFStyle*>::iterator it;
101 : :
102 [ + - ][ + + ]: 1554 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
103 : : {
104 : 702 : IXFStyle *pStyle = *it;
105 [ + - ]: 702 : if( pStyle )
106 [ + - ][ + - ]: 702 : delete pStyle;
107 : : }
108 : 852 : m_aStyles.clear();
109 : 852 : }
110 : :
111 : 1995 : IXFStyle* XFStyleContainer::AddStyle(IXFStyle *pStyle)
112 : : {
113 : 1995 : IXFStyle *pConStyle = NULL;
114 : 1995 : rtl::OUString name;
115 : :
116 [ - + ]: 1995 : if( !pStyle )
117 : 0 : return NULL;
118 : : //no matter we want to delete the style or not,XFFont obejct should be saved first.
119 [ + - ]: 1995 : ManageStyleFont(pStyle);
120 : :
121 [ + - ][ + + ]: 1995 : if( pStyle->GetStyleName().isEmpty() )
122 [ + - ]: 1779 : pConStyle = FindSameStyle(pStyle);
123 : :
124 [ + + ]: 1995 : if( pConStyle )//such a style has exist:
125 : : {
126 [ + - ][ + - ]: 1104 : delete pStyle;
127 : 1104 : return pConStyle;
128 : : }
129 : : else
130 : : {
131 [ + - ][ + + ]: 891 : if( pStyle->GetStyleName().isEmpty() )
132 : : {
133 [ + - ]: 675 : name = m_strStyleNamePrefix + Int32ToOUString(m_aStyles.size()+1);
134 [ + - ]: 675 : pStyle->SetStyleName(name);
135 : : }
136 : : else
137 : : {
138 [ + - ]: 216 : name = pStyle->GetStyleName();
139 : : //for name conflict
140 [ - + ][ + - ]: 216 : if(FindStyle( name))
141 : : {
142 [ # # ]: 0 : name = name + Int32ToOUString(m_aStyles.size()+1);
143 [ # # ]: 0 : pStyle->SetStyleName(name);
144 : : }
145 : : }
146 : :
147 : :
148 [ + - ]: 891 : m_aStyles.push_back(pStyle);
149 : : //transform the font object to XFFontFactory
150 : :
151 : 891 : return pStyle;
152 : 1995 : }
153 : : }
154 : :
155 : 1779 : IXFStyle* XFStyleContainer::FindSameStyle(IXFStyle *pStyle)
156 : : {
157 : 1779 : std::vector<IXFStyle*>::iterator it;
158 [ + - ][ + + ]: 6954 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
159 : : {
160 : 6279 : IXFStyle *pConStyle = *it;
161 [ - + ]: 6279 : if( !pConStyle )
162 : 0 : continue;
163 : :
164 [ + - ][ + + ]: 6279 : if( pConStyle->Equal(pStyle) )
165 : 1104 : return pConStyle;
166 : : }
167 : :
168 : 1779 : return NULL;
169 : : }
170 : :
171 : 1938 : IXFStyle* XFStyleContainer::FindStyle(rtl::OUString name)
172 : : {
173 : 1938 : std::vector<IXFStyle*>::iterator it;
174 [ + - ][ + + ]: 14151 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
175 : : {
176 : 13170 : IXFStyle *pConStyle = *it;
177 [ - + ]: 13170 : if( !pConStyle )
178 : 0 : continue;
179 : :
180 [ + - ][ + + ]: 13170 : if( pConStyle->GetStyleName() == name )
181 : 957 : return pConStyle;
182 : : }
183 : :
184 : 1938 : return NULL;
185 : : }
186 : :
187 : 0 : const IXFStyle* XFStyleContainer::Item(size_t index) const
188 : : {
189 : : assert(index<m_aStyles.size());
190 [ # # ]: 0 : if (index < m_aStyles.size())
191 : : {
192 : 0 : return m_aStyles[index];
193 : : }
194 : 0 : return NULL;
195 : : }
196 : :
197 : 189 : void XFStyleContainer::ToXml(IXFStream *pStrm)
198 : : {
199 : 189 : std::vector<IXFStyle*>::iterator it;
200 : :
201 [ + - ][ + + ]: 834 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
202 : : {
203 : 645 : IXFStyle *pStyle = *it;
204 : :
205 : : assert(pStyle);
206 [ - + ]: 645 : if( !pStyle )
207 : 0 : continue;
208 : :
209 [ + - ]: 645 : pStyle->ToXml(pStrm);
210 : : }
211 : 189 : }
212 : :
213 : :
214 : 1995 : void XFStyleContainer::ManageStyleFont(IXFStyle *pStyle)
215 : : {
216 : 1995 : XFFont *pStyleFont = NULL;
217 : 1995 : XFFont *pFactoryFont = NULL;
218 : :
219 [ - + ]: 1995 : if( !pStyle )
220 : 0 : return;
221 : :
222 [ + + ]: 1995 : if( pStyle->GetStyleFamily() == enumXFStyleText )
223 : : {
224 : 540 : XFTextStyle *pTS = (XFTextStyle*)pStyle;
225 : 540 : pStyleFont = pTS->GetFont();
226 [ - + ]: 540 : if( !pStyleFont )
227 : 0 : return;
228 : 540 : LwpGlobalMgr* pGlobal = LwpGlobalMgr::GetInstance();
229 : 540 : XFFontFactory* pFontFactory = pGlobal->GetXFFontFactory();
230 : 540 : pFactoryFont = pFontFactory->FindSameFont(pStyleFont);
231 : : //this font has been exists in the factory:
232 [ + + ]: 540 : if( pFactoryFont )
233 : : {
234 : 444 : pTS->SetFont(pFactoryFont);
235 [ + - ]: 444 : if( pStyleFont != pFactoryFont )
236 [ + - ]: 444 : delete pStyleFont;
237 : : }
238 : : else
239 : : {
240 : 96 : pFontFactory->AddFont(pStyleFont);
241 : : }
242 : : }
243 [ + + ]: 1455 : else if( pStyle->GetStyleFamily() == enumXFStylePara )
244 : : {
245 : 780 : XFParaStyle *pPS = (XFParaStyle*)pStyle;
246 : 780 : pStyleFont = pPS->GetFont();
247 [ - + ]: 780 : if( !pStyleFont )
248 : 0 : return;
249 : 780 : LwpGlobalMgr* pGlobal = LwpGlobalMgr::GetInstance();
250 : 780 : XFFontFactory* pFontFactory = pGlobal->GetXFFontFactory();
251 : 780 : pFactoryFont = pFontFactory->FindSameFont(pStyleFont);
252 : : //this font has been exists in the factory:
253 [ + + ]: 780 : if( pFactoryFont )
254 : : {
255 : 684 : pPS->SetFont(pFactoryFont);
256 [ + + ]: 684 : if( pFactoryFont != pStyleFont )
257 [ + - ]: 108 : delete pStyleFont;
258 : : }
259 : : else
260 : : {
261 : 1995 : pFontFactory->AddFont(pStyleFont);
262 : : }
263 : : }
264 : : }
265 : :
266 : 477 : bool operator==(XFStyleContainer& b1, XFStyleContainer& b2)
267 : : {
268 [ - + ]: 477 : if( b1.m_strStyleNamePrefix != b2.m_strStyleNamePrefix )
269 : 0 : return false;
270 [ + + ]: 477 : if( b1.m_aStyles.size() != b2.m_aStyles.size() )
271 : 15 : return false;
272 [ + + ]: 462 : for( size_t i=0; i<b1.m_aStyles.size(); ++i )
273 : : {
274 : 6 : IXFStyle *pS1 = b1.m_aStyles[i];
275 : 6 : IXFStyle *pS2 = b2.m_aStyles[i];
276 : :
277 [ + - ]: 6 : if( pS1 )
278 : : {
279 [ - + ]: 6 : if( !pS2 )
280 : 0 : return false;
281 [ + - ]: 6 : if( !pS1->Equal(pS2) )
282 : 6 : return false;
283 : : }
284 : : else
285 : : {
286 [ # # ]: 0 : if( pS2 )
287 : 0 : return false;
288 : : }
289 : : }
290 : 477 : return true;
291 : : }
292 : :
293 : 477 : bool operator!=(XFStyleContainer& b1, XFStyleContainer& b2)
294 : : {
295 : 477 : return !(b1==b2);
296 : : }
297 : :
298 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|