1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 
31 #include <com/sun/star/awt/FontFamily.hpp>
32 #include <com/sun/star/awt/FontPitch.hpp>
33 
34 #include <rtl/logfile.hxx>
35 
36 #include <xmloff/nmspmap.hxx>
37 #include "xmloff/xmlnmspe.hxx"
38 #include <xmloff/xmltoken.hxx>
39 #include "fonthdl.hxx"
40 #include <xmloff/xmlimp.hxx>
41 #include <xmloff/maptype.hxx>
42 #include <xmloff/XMLFontStylesContext.hxx>
43 
44 
45 using ::rtl::OUString;
46 using ::rtl::OUStringBuffer;
47 
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::xml::sax;
51 using namespace ::com::sun::star::container;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::awt;
55 using namespace ::xmloff::token;
56 
57 
58 #define XML_STYLE_FAMILY_FONT 1
59 
60 enum XMLFontStyleAttrTokens
61 {
62 	XML_TOK_FONT_STYLE_ATTR_FAMILY,
63 	XML_TOK_FONT_STYLE_ATTR_FAMILY_GENERIC,
64 	XML_TOK_FONT_STYLE_ATTR_STYLENAME,
65 	XML_TOK_FONT_STYLE_ATTR_PITCH,
66 	XML_TOK_FONT_STYLE_ATTR_CHARSET,
67 
68 	XML_TOK_FONT_STYLE_ATTR_END=XML_TOK_UNKNOWN
69 };
70 
71 const SvXMLTokenMapEntry* lcl_getFontStyleAttrTokenMap()
72 {
73     static __FAR_DATA SvXMLTokenMapEntry aFontStyleAttrTokenMap[] =
74     {
75 	    { XML_NAMESPACE_SVG, XML_FONT_FAMILY,
76 			    XML_TOK_FONT_STYLE_ATTR_FAMILY },
77 	    { XML_NAMESPACE_STYLE, XML_FONT_FAMILY_GENERIC,
78 			    XML_TOK_FONT_STYLE_ATTR_FAMILY_GENERIC },
79 	    { XML_NAMESPACE_STYLE, XML_FONT_ADORNMENTS,
80 			    XML_TOK_FONT_STYLE_ATTR_STYLENAME },
81 	    { XML_NAMESPACE_STYLE, XML_FONT_PITCH,
82 			    XML_TOK_FONT_STYLE_ATTR_PITCH },
83 	    { XML_NAMESPACE_STYLE, XML_FONT_CHARSET,
84 			    XML_TOK_FONT_STYLE_ATTR_CHARSET },
85 
86 	    XML_TOKEN_MAP_END
87     };
88     return aFontStyleAttrTokenMap;
89 }
90 
91 class XMLFontStyleContext_Impl : public SvXMLStyleContext
92 {
93 	Any aFamilyName;
94 	Any aStyleName;
95 	Any aFamily;
96 	Any aPitch;
97 	Any aEnc;
98 
99 	SvXMLImportContextRef xStyles;
100 
101 	XMLFontStylesContext *GetStyles()
102 	{
103 		return ((XMLFontStylesContext *)&xStyles);
104 	}
105 
106 public:
107 
108 	TYPEINFO();
109 
110 	XMLFontStyleContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
111 			const ::rtl::OUString& rLName,
112 			const ::com::sun::star::uno::Reference<
113 				::com::sun::star::xml::sax::XAttributeList > & xAttrList,
114 			XMLFontStylesContext& rStyles );
115 	virtual ~XMLFontStyleContext_Impl();
116 
117 	void SetAttribute( sal_uInt16 nPrefixKey, const OUString& rLocalName,
118 					   const OUString& rValue );
119 
120 	void FillProperties( ::std::vector< XMLPropertyState > &rProps,
121 						 sal_Int32 nFamilyNameIdx,
122 						 sal_Int32 nStyleNameIdx,
123 						 sal_Int32 nFamilyIdx,
124 						 sal_Int32 nPitchIdx,
125 						 sal_Int32 nCharsetIdx ) const;
126 
127 };
128 
129 TYPEINIT1( XMLFontStyleContext_Impl, SvXMLStyleContext );
130 
131 XMLFontStyleContext_Impl::XMLFontStyleContext_Impl( SvXMLImport& rImport,
132 		sal_uInt16 nPrfx, const OUString& rLName,
133 		const Reference< XAttributeList > & xAttrList,
134 		XMLFontStylesContext& rStyles ) :
135 	SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, XML_STYLE_FAMILY_FONT ),
136 	xStyles( &rStyles )
137 {
138 	OUString sEmpty;
139 	aFamilyName <<= sEmpty;
140 	aStyleName <<= sEmpty;
141 	aFamily <<= (sal_Int16)FontFamily::DONTKNOW;
142 	aPitch <<= (sal_Int16)FontPitch::DONTKNOW;
143 	aEnc <<= (sal_Int16)rStyles.GetDfltCharset();
144 }
145 
146 void XMLFontStyleContext_Impl::SetAttribute( sal_uInt16 nPrefixKey,
147 										const OUString& rLocalName,
148 										const OUString& rValue )
149 {
150 	SvXMLUnitConverter& rUnitConv = GetImport().GetMM100UnitConverter();
151 	const SvXMLTokenMap& rTokenMap = GetStyles()->GetFontStyleAttrTokenMap();
152 	Any aAny;
153 
154 	switch( rTokenMap.Get( nPrefixKey, rLocalName ) )
155 	{
156 	case XML_TOK_FONT_STYLE_ATTR_FAMILY:
157 		if( GetStyles()->GetFamilyNameHdl().importXML( rValue, aAny,
158 														  rUnitConv ) )
159 			aFamilyName = aAny;
160 		break;
161 	case XML_TOK_FONT_STYLE_ATTR_STYLENAME:
162 		aStyleName <<= rValue;
163 		break;
164 	case XML_TOK_FONT_STYLE_ATTR_FAMILY_GENERIC:
165 		if( GetStyles()->GetFamilyHdl().importXML( rValue, aAny,
166 													  rUnitConv ) )
167 			aFamily = aAny;
168 		break;
169 	case XML_TOK_FONT_STYLE_ATTR_PITCH:
170 		if( GetStyles()->GetPitchHdl().importXML( rValue, aAny,
171 													  rUnitConv ) )
172 			aPitch = aAny;
173 		break;
174 	case XML_TOK_FONT_STYLE_ATTR_CHARSET:
175 		if( GetStyles()->GetEncodingHdl().importXML( rValue, aAny,
176 													  rUnitConv ) )
177 			aEnc = aAny;
178 		break;
179 	default:
180 		SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
181 		break;
182 	}
183 }
184 
185 XMLFontStyleContext_Impl::~XMLFontStyleContext_Impl()
186 {
187 }
188 
189 void XMLFontStyleContext_Impl::FillProperties(
190 		::std::vector< XMLPropertyState > &rProps,
191 		sal_Int32 nFamilyNameIdx,
192 		sal_Int32 nStyleNameIdx,
193 		sal_Int32 nFamilyIdx,
194 		sal_Int32 nPitchIdx,
195 		sal_Int32 nCharsetIdx ) const
196 {
197 	if( nFamilyNameIdx != -1 )
198 	{
199 		XMLPropertyState aPropState( nFamilyNameIdx, aFamilyName );
200 		rProps.push_back( aPropState );
201 	}
202 	if( nStyleNameIdx != -1 )
203 	{
204 		XMLPropertyState aPropState( nStyleNameIdx, aStyleName );
205 		rProps.push_back( aPropState );
206 	}
207 	if( nFamilyIdx != -1 )
208 	{
209 		XMLPropertyState aPropState( nFamilyIdx, aFamily );
210 		rProps.push_back( aPropState );
211 	}
212 	if( nPitchIdx != -1 )
213 	{
214 		XMLPropertyState aPropState( nPitchIdx, aPitch );
215 		rProps.push_back( aPropState );
216 	}
217 	if( nCharsetIdx != -1 )
218 	{
219 		XMLPropertyState aPropState( nCharsetIdx, aEnc );
220 		rProps.push_back( aPropState );
221 	}
222 }
223 
224 SvXMLStyleContext *XMLFontStylesContext::CreateStyleChildContext(
225 		sal_uInt16 nPrefix,
226 		const ::rtl::OUString& rLocalName,
227 		const ::com::sun::star::uno::Reference<
228 			::com::sun::star::xml::sax::XAttributeList > & xAttrList )
229 {
230 	SvXMLStyleContext *pStyle;
231 	if( XML_NAMESPACE_STYLE == nPrefix &&
232 		IsXMLToken( rLocalName, XML_FONT_FACE ) )
233 	{
234 		pStyle = new XMLFontStyleContext_Impl( GetImport(), nPrefix,
235 											   rLocalName, xAttrList, *this );
236 	}
237 	else
238 	{
239 		pStyle = SvXMLStylesContext::CreateStyleChildContext( nPrefix,
240 											   rLocalName, xAttrList );
241 	}
242 
243 	return pStyle;
244 }
245 
246 TYPEINIT1( XMLFontStylesContext, SvXMLStylesContext );
247 
248 XMLFontStylesContext::XMLFontStylesContext( SvXMLImport& rImport,
249 		sal_uInt16 nPrfx, const OUString& rLName,
250 		const Reference< XAttributeList > & xAttrList,
251 		rtl_TextEncoding eDfltEnc ) :
252 	SvXMLStylesContext( rImport, nPrfx, rLName, xAttrList ),
253 	pFamilyNameHdl( new XMLFontFamilyNamePropHdl ),
254 	pFamilyHdl( new XMLFontFamilyPropHdl ),
255 	pPitchHdl( new XMLFontPitchPropHdl ),
256 	pEncHdl( new XMLFontEncodingPropHdl ),
257 	pFontStyleAttrTokenMap( new SvXMLTokenMap(lcl_getFontStyleAttrTokenMap()) ),
258 	eDfltEncoding( eDfltEnc )
259 {
260 }
261 
262 XMLFontStylesContext::~XMLFontStylesContext()
263 {
264 	delete pFamilyNameHdl;
265 	delete pFamilyHdl;
266 	delete pPitchHdl;
267 	delete pEncHdl;
268 	delete pFontStyleAttrTokenMap;
269 }
270 
271 sal_Bool XMLFontStylesContext::FillProperties( const OUString& rName,
272 						 ::std::vector< XMLPropertyState > &rProps,
273 						 sal_Int32 nFamilyNameIdx,
274 						 sal_Int32 nStyleNameIdx,
275 						 sal_Int32 nFamilyIdx,
276 						 sal_Int32 nPitchIdx,
277 						 sal_Int32 nCharsetIdx ) const
278 {
279     const SvXMLStyleContext* pStyle = FindStyleChildContext( XML_STYLE_FAMILY_FONT, rName, sal_True );
280 	const XMLFontStyleContext_Impl *pFontStyle = PTR_CAST( XMLFontStyleContext_Impl,pStyle);// use temp var, PTR_CAST is a bad macro, FindStyleChildContext will be called twice
281 	if( pFontStyle )
282 		pFontStyle->FillProperties( rProps, nFamilyNameIdx, nStyleNameIdx,
283 									nFamilyIdx, nPitchIdx, nCharsetIdx );
284 	return 0 != pFontStyle;
285 }
286