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