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 156 : XFStyleContainer::XFStyleContainer(const rtl::OUString& strStyleNamePrefix)
69 156 : :m_strStyleNamePrefix(strStyleNamePrefix)
70 : {
71 156 : }
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 1508 : XFStyleContainer::~XFStyleContainer()
87 : {
88 754 : std::vector<IXFStyle*>::iterator it;
89 880 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
90 : {
91 126 : IXFStyle *pStyle = *it;
92 126 : if( pStyle )
93 126 : delete pStyle;
94 : }
95 754 : }
96 :
97 :
98 568 : void XFStyleContainer::Reset()
99 : {
100 568 : std::vector<IXFStyle*>::iterator it;
101 :
102 1036 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
103 : {
104 468 : IXFStyle *pStyle = *it;
105 468 : if( pStyle )
106 468 : delete pStyle;
107 : }
108 568 : m_aStyles.clear();
109 568 : }
110 :
111 1330 : IXFStyle* XFStyleContainer::AddStyle(IXFStyle *pStyle)
112 : {
113 1330 : IXFStyle *pConStyle = NULL;
114 1330 : rtl::OUString name;
115 :
116 1330 : if( !pStyle )
117 0 : return NULL;
118 : //no matter we want to delete the style or not,XFFont obejct should be saved first.
119 1330 : ManageStyleFont(pStyle);
120 :
121 1330 : if( pStyle->GetStyleName().isEmpty() )
122 1186 : pConStyle = FindSameStyle(pStyle);
123 :
124 1330 : if( pConStyle )//such a style has exist:
125 : {
126 736 : delete pStyle;
127 736 : return pConStyle;
128 : }
129 : else
130 : {
131 594 : if( pStyle->GetStyleName().isEmpty() )
132 : {
133 450 : name = m_strStyleNamePrefix + Int32ToOUString(m_aStyles.size()+1);
134 450 : pStyle->SetStyleName(name);
135 : }
136 : else
137 : {
138 144 : name = pStyle->GetStyleName();
139 : //for name conflict
140 144 : if(FindStyle( name))
141 : {
142 0 : name = name + Int32ToOUString(m_aStyles.size()+1);
143 0 : pStyle->SetStyleName(name);
144 : }
145 : }
146 :
147 :
148 594 : m_aStyles.push_back(pStyle);
149 : //transform the font object to XFFontFactory
150 :
151 594 : return pStyle;
152 1330 : }
153 : }
154 :
155 1186 : IXFStyle* XFStyleContainer::FindSameStyle(IXFStyle *pStyle)
156 : {
157 1186 : std::vector<IXFStyle*>::iterator it;
158 4636 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
159 : {
160 4186 : IXFStyle *pConStyle = *it;
161 4186 : if( !pConStyle )
162 0 : continue;
163 :
164 4186 : if( pConStyle->Equal(pStyle) )
165 736 : return pConStyle;
166 : }
167 :
168 450 : return NULL;
169 : }
170 :
171 1292 : IXFStyle* XFStyleContainer::FindStyle(rtl::OUString name)
172 : {
173 1292 : std::vector<IXFStyle*>::iterator it;
174 9434 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
175 : {
176 8780 : IXFStyle *pConStyle = *it;
177 8780 : if( !pConStyle )
178 0 : continue;
179 :
180 8780 : if( pConStyle->GetStyleName() == name )
181 638 : return pConStyle;
182 : }
183 :
184 654 : 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 126 : void XFStyleContainer::ToXml(IXFStream *pStrm)
198 : {
199 126 : std::vector<IXFStyle*>::iterator it;
200 :
201 556 : for( it = m_aStyles.begin(); it != m_aStyles.end(); ++it )
202 : {
203 430 : IXFStyle *pStyle = *it;
204 :
205 : assert(pStyle);
206 430 : if( !pStyle )
207 0 : continue;
208 :
209 430 : pStyle->ToXml(pStrm);
210 : }
211 126 : }
212 :
213 :
214 1330 : void XFStyleContainer::ManageStyleFont(IXFStyle *pStyle)
215 : {
216 1330 : XFFont *pStyleFont = NULL;
217 1330 : XFFont *pFactoryFont = NULL;
218 :
219 1330 : if( !pStyle )
220 0 : return;
221 :
222 1330 : if( pStyle->GetStyleFamily() == enumXFStyleText )
223 : {
224 360 : XFTextStyle *pTS = (XFTextStyle*)pStyle;
225 360 : pStyleFont = pTS->GetFont();
226 360 : if( !pStyleFont )
227 0 : return;
228 360 : LwpGlobalMgr* pGlobal = LwpGlobalMgr::GetInstance();
229 360 : XFFontFactory* pFontFactory = pGlobal->GetXFFontFactory();
230 360 : pFactoryFont = pFontFactory->FindSameFont(pStyleFont);
231 : //this font has been exists in the factory:
232 360 : if( pFactoryFont )
233 : {
234 296 : pTS->SetFont(pFactoryFont);
235 296 : if( pStyleFont != pFactoryFont )
236 296 : delete pStyleFont;
237 : }
238 : else
239 : {
240 64 : pFontFactory->AddFont(pStyleFont);
241 : }
242 : }
243 970 : else if( pStyle->GetStyleFamily() == enumXFStylePara )
244 : {
245 520 : XFParaStyle *pPS = (XFParaStyle*)pStyle;
246 520 : pStyleFont = pPS->GetFont();
247 520 : if( !pStyleFont )
248 0 : return;
249 520 : LwpGlobalMgr* pGlobal = LwpGlobalMgr::GetInstance();
250 520 : XFFontFactory* pFontFactory = pGlobal->GetXFFontFactory();
251 520 : pFactoryFont = pFontFactory->FindSameFont(pStyleFont);
252 : //this font has been exists in the factory:
253 520 : if( pFactoryFont )
254 : {
255 456 : pPS->SetFont(pFactoryFont);
256 456 : if( pFactoryFont != pStyleFont )
257 72 : delete pStyleFont;
258 : }
259 : else
260 : {
261 64 : pFontFactory->AddFont(pStyleFont);
262 : }
263 : }
264 : }
265 :
266 318 : bool operator==(XFStyleContainer& b1, XFStyleContainer& b2)
267 : {
268 318 : if( b1.m_strStyleNamePrefix != b2.m_strStyleNamePrefix )
269 0 : return false;
270 318 : if( b1.m_aStyles.size() != b2.m_aStyles.size() )
271 10 : return false;
272 308 : for( size_t i=0; i<b1.m_aStyles.size(); ++i )
273 : {
274 4 : IXFStyle *pS1 = b1.m_aStyles[i];
275 4 : IXFStyle *pS2 = b2.m_aStyles[i];
276 :
277 4 : if( pS1 )
278 : {
279 4 : if( !pS2 )
280 0 : return false;
281 4 : if( !pS1->Equal(pS2) )
282 4 : return false;
283 : }
284 : else
285 : {
286 0 : if( pS2 )
287 0 : return false;
288 : }
289 : }
290 304 : return true;
291 : }
292 :
293 318 : bool operator!=(XFStyleContainer& b1, XFStyleContainer& b2)
294 : {
295 318 : return !(b1==b2);
296 : }
297 :
298 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|