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