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