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 <xfilter/xfstylecont.hxx>
61 #include <xfilter/ixfstyle.hxx>
62 #include <xfilter/xffont.hxx>
63 #include <xfilter/xftextstyle.hxx>
64 #include <xfilter/xfparastyle.hxx>
65 #include <xfilter/xffontfactory.hxx>
66 #include <lwpglobalmgr.hxx>
67
XFStyleContainer(const OUString & strStyleNamePrefix)68 XFStyleContainer::XFStyleContainer(const OUString& strStyleNamePrefix)
69 :m_strStyleNamePrefix(strStyleNamePrefix)
70 {
71 }
72
~XFStyleContainer()73 XFStyleContainer::~XFStyleContainer()
74 {
75 }
76
Reset()77 void XFStyleContainer::Reset()
78 {
79 m_aStyles.clear();
80 }
81
AddStyle(std::unique_ptr<IXFStyle> pStyle)82 IXFStyleRet XFStyleContainer::AddStyle(std::unique_ptr<IXFStyle> pStyle)
83 {
84 IXFStyleRet aRet;
85
86 IXFStyle *pConStyle = nullptr;
87 OUString name;
88
89 if( !pStyle )
90 return aRet;
91 //no matter we want to delete the style or not,XFFont object should be saved first.
92 ManageStyleFont(pStyle.get());
93
94 if( pStyle->GetStyleName().isEmpty() )
95 pConStyle = FindSameStyle(pStyle.get());
96
97 if( pConStyle )//such a style has exist:
98 {
99 aRet.m_pStyle = pConStyle;
100 aRet.m_bOrigDeleted = true;
101 return aRet;
102 }
103 else
104 {
105 if( pStyle->GetStyleName().isEmpty() )
106 {
107 name = m_strStyleNamePrefix + OUString::number(m_aStyles.size()+1);
108 pStyle->SetStyleName(name);
109 }
110 else
111 {
112 name = pStyle->GetStyleName();
113 //for name conflict
114 if(FindStyle( name))
115 {
116 name += OUString::number(m_aStyles.size()+1);
117 pStyle->SetStyleName(name);
118 }
119 }
120
121 //transform the font object to XFFontFactory
122 aRet.m_pStyle = pStyle.get();
123 m_aStyles.push_back(std::move(pStyle));
124 return aRet;
125 }
126 }
127
FindSameStyle(IXFStyle * pStyle)128 IXFStyle* XFStyleContainer::FindSameStyle(IXFStyle *pStyle)
129 {
130 for (auto const& style : m_aStyles)
131 {
132 assert(style);
133 if( style->Equal(pStyle) )
134 return style.get();
135 }
136
137 return nullptr;
138 }
139
FindStyle(const OUString & name)140 IXFStyle* XFStyleContainer::FindStyle(const OUString& name)
141 {
142 for (auto const& style : m_aStyles)
143 {
144 assert(style);
145 if( style->GetStyleName() == name )
146 return style.get();
147 }
148
149 return nullptr;
150 }
151
Item(size_t index) const152 const IXFStyle* XFStyleContainer::Item(size_t index) const
153 {
154 assert(index<m_aStyles.size());
155 if (index < m_aStyles.size())
156 {
157 return m_aStyles[index].get();
158 }
159 return nullptr;
160 }
161
ToXml(IXFStream * pStrm)162 void XFStyleContainer::ToXml(IXFStream *pStrm)
163 {
164 for (auto const& style : m_aStyles)
165 {
166 assert(style);
167 style->ToXml(pStrm);
168 }
169 }
170
ManageStyleFont(IXFStyle * pStyle)171 void XFStyleContainer::ManageStyleFont(IXFStyle *pStyle)
172 {
173 rtl::Reference<XFFont> pStyleFont;
174 rtl::Reference<XFFont> pFactoryFont;
175
176 if( !pStyle )
177 return;
178
179 if( pStyle->GetStyleFamily() == enumXFStyleText )
180 {
181 XFTextStyle *pTS = static_cast<XFTextStyle*>(pStyle);
182 pStyleFont = pTS->GetFont();
183 if( !pStyleFont.is() )
184 return;
185 LwpGlobalMgr* pGlobal = LwpGlobalMgr::GetInstance();
186 XFFontFactory* pFontFactory = pGlobal->GetXFFontFactory();
187 pFactoryFont = pFontFactory->FindSameFont(pStyleFont);
188 //this font has been exists in the factory:
189 if( pFactoryFont.is() )
190 {
191 pTS->SetFont(pFactoryFont);
192 }
193 else
194 {
195 pFontFactory->AddFont(pStyleFont);
196 }
197 }
198 else if( pStyle->GetStyleFamily() == enumXFStylePara )
199 {
200 XFParaStyle *pPS = static_cast<XFParaStyle*>(pStyle);
201 pStyleFont = pPS->GetFont();
202 if( !pStyleFont.is() )
203 return;
204 LwpGlobalMgr* pGlobal = LwpGlobalMgr::GetInstance();
205 XFFontFactory* pFontFactory = pGlobal->GetXFFontFactory();
206 pFactoryFont = pFontFactory->FindSameFont(pStyleFont);
207 //this font has been exists in the factory:
208 if( pFactoryFont.is() )
209 {
210 pPS->SetFont(pFactoryFont);
211 }
212 else
213 {
214 pFontFactory->AddFont(pStyleFont);
215 }
216 }
217 }
218
operator ==(XFStyleContainer & b1,XFStyleContainer & b2)219 bool operator==(XFStyleContainer& b1, XFStyleContainer& b2)
220 {
221 if( b1.m_strStyleNamePrefix != b2.m_strStyleNamePrefix )
222 return false;
223 if( b1.m_aStyles.size() != b2.m_aStyles.size() )
224 return false;
225 for( size_t i=0; i<b1.m_aStyles.size(); ++i )
226 {
227 IXFStyle *pS1 = b1.m_aStyles[i].get();
228 IXFStyle *pS2 = b2.m_aStyles[i].get();
229
230 if( pS1 )
231 {
232 if( !pS2 )
233 return false;
234 if( !pS1->Equal(pS2) )
235 return false;
236 }
237 else
238 {
239 if( pS2 )
240 return false;
241 }
242 }
243 return true;
244 }
245
operator !=(XFStyleContainer & b1,XFStyleContainer & b2)246 bool operator!=(XFStyleContainer& b1, XFStyleContainer& b2)
247 {
248 return !(b1==b2);
249 }
250
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
252