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