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