nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / style / XMLFontStylesContext.cxx
blobbdd86c93bd7347680a212eee63e6e378fbbd53e1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <xmloff/XMLFontStylesContext.hxx>
21 #include "XMLFontStylesContext_impl.hxx"
23 #include <com/sun/star/awt/FontFamily.hpp>
24 #include <com/sun/star/awt/FontPitch.hpp>
25 #include <com/sun/star/embed/ElementModes.hpp>
26 #include <com/sun/star/embed/XStorage.hpp>
28 #include <comphelper/seqstream.hxx>
30 #include <sal/log.hxx>
32 #include <xmloff/xmlnamespace.hxx>
33 #include <xmloff/xmltoken.hxx>
34 #include "fonthdl.hxx"
35 #include <xmloff/xmlimp.hxx>
36 #include <xmloff/maptype.hxx>
37 #include <xmloff/XMLBase64ImportContext.hxx>
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::xml::sax;
43 using namespace ::com::sun::star::container;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::awt;
47 using namespace ::xmloff::token;
50 #define XML_STYLE_FAMILY_FONT XmlStyleFamily::PAGE_MASTER
52 namespace {
54 enum XMLFontStyleAttrTokens
56 XML_TOK_FONT_STYLE_ATTR_FAMILY,
57 XML_TOK_FONT_STYLE_ATTR_FAMILY_GENERIC,
58 XML_TOK_FONT_STYLE_ATTR_STYLENAME,
59 XML_TOK_FONT_STYLE_ATTR_PITCH,
60 XML_TOK_FONT_STYLE_ATTR_CHARSET,
65 static const SvXMLTokenMapEntry* lcl_getFontStyleAttrTokenMap()
67 static const SvXMLTokenMapEntry aFontStyleAttrTokenMap[] =
69 { XML_NAMESPACE_SVG, XML_FONT_FAMILY,
70 XML_TOK_FONT_STYLE_ATTR_FAMILY },
71 { XML_NAMESPACE_STYLE, XML_FONT_FAMILY_GENERIC,
72 XML_TOK_FONT_STYLE_ATTR_FAMILY_GENERIC },
73 { XML_NAMESPACE_STYLE, XML_FONT_ADORNMENTS,
74 XML_TOK_FONT_STYLE_ATTR_STYLENAME },
75 { XML_NAMESPACE_STYLE, XML_FONT_PITCH,
76 XML_TOK_FONT_STYLE_ATTR_PITCH },
77 { XML_NAMESPACE_STYLE, XML_FONT_CHARSET,
78 XML_TOK_FONT_STYLE_ATTR_CHARSET },
80 XML_TOKEN_MAP_END
82 return aFontStyleAttrTokenMap;
86 XMLFontStyleContextFontFace::XMLFontStyleContextFontFace( SvXMLImport& rImport,
87 XMLFontStylesContext& rStyles ) :
88 SvXMLStyleContext( rImport, XML_STYLE_FAMILY_FONT ),
89 xStyles( &rStyles )
91 aFamilyName <<= OUString();
92 aStyleName <<= OUString();
93 aFamily <<= sal_Int16(awt::FontFamily::DONTKNOW);
94 aPitch <<= sal_Int16(awt::FontPitch::DONTKNOW);
95 aEnc <<= static_cast<sal_Int16>(rStyles.GetDfltCharset());
98 void XMLFontStyleContextFontFace::SetAttribute( sal_uInt16 nPrefixKey,
99 const OUString& rLocalName,
100 const OUString& rValue )
102 SvXMLUnitConverter& rUnitConv = GetImport().GetMM100UnitConverter();
103 const SvXMLTokenMap& rTokenMap = GetStyles()->GetFontStyleAttrTokenMap();
104 Any aAny;
106 switch( rTokenMap.Get( nPrefixKey, rLocalName ) )
108 case XML_TOK_FONT_STYLE_ATTR_FAMILY:
109 if( GetStyles()->GetFamilyNameHdl().importXML( rValue, aAny,
110 rUnitConv ) )
111 aFamilyName = aAny;
112 break;
113 case XML_TOK_FONT_STYLE_ATTR_STYLENAME:
114 aStyleName <<= rValue;
115 break;
116 case XML_TOK_FONT_STYLE_ATTR_FAMILY_GENERIC:
117 if( GetStyles()->GetFamilyHdl().importXML( rValue, aAny,
118 rUnitConv ) )
119 aFamily = aAny;
120 break;
121 case XML_TOK_FONT_STYLE_ATTR_PITCH:
122 if( GetStyles()->GetPitchHdl().importXML( rValue, aAny,
123 rUnitConv ) )
124 aPitch = aAny;
125 break;
126 case XML_TOK_FONT_STYLE_ATTR_CHARSET:
127 if( GetStyles()->GetEncodingHdl().importXML( rValue, aAny,
128 rUnitConv ) )
129 aEnc = aAny;
130 break;
131 default:
132 SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
133 break;
137 XMLFontStyleContextFontFace::~XMLFontStyleContextFontFace()
141 void XMLFontStyleContextFontFace::FillProperties(
142 ::std::vector< XMLPropertyState > &rProps,
143 sal_Int32 nFamilyNameIdx,
144 sal_Int32 nStyleNameIdx,
145 sal_Int32 nFamilyIdx,
146 sal_Int32 nPitchIdx,
147 sal_Int32 nCharsetIdx ) const
149 if( nFamilyNameIdx != -1 )
151 XMLPropertyState aPropState( nFamilyNameIdx, aFamilyName );
152 rProps.push_back( aPropState );
154 if( nStyleNameIdx != -1 )
156 XMLPropertyState aPropState( nStyleNameIdx, aStyleName );
157 rProps.push_back( aPropState );
159 if( nFamilyIdx != -1 )
161 XMLPropertyState aPropState( nFamilyIdx, aFamily );
162 rProps.push_back( aPropState );
164 if( nPitchIdx != -1 )
166 XMLPropertyState aPropState( nPitchIdx, aPitch );
167 rProps.push_back( aPropState );
169 if( nCharsetIdx != -1 )
171 XMLPropertyState aPropState( nCharsetIdx, aEnc );
172 rProps.push_back( aPropState );
176 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFontStyleContextFontFace::createFastChildContext(
177 sal_Int32 nElement,
178 const css::uno::Reference< css::xml::sax::XFastAttributeList > & )
180 if( nElement == XML_ELEMENT(SVG, XML_FONT_FACE_SRC) ||
181 nElement == XML_ELEMENT(SVG_COMPAT, XML_FONT_FACE_SRC) )
182 return new XMLFontStyleContextFontFaceSrc( GetImport(), *this );
183 else
184 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
185 return nullptr;
188 OUString XMLFontStyleContextFontFace::familyName() const
190 OUString ret;
191 aFamilyName >>= ret;
192 return ret;
196 XMLFontStyleContextFontFaceFormat::XMLFontStyleContextFontFaceFormat( SvXMLImport& rImport,
197 XMLFontStyleContextFontFaceUri& _uri )
198 : SvXMLStyleContext( rImport )
199 , uri(_uri)
203 void XMLFontStyleContextFontFaceFormat::SetAttribute( sal_uInt16 nPrefixKey, const OUString& rLocalName,
204 const OUString& rValue )
206 if( nPrefixKey == XML_NAMESPACE_SVG && IsXMLToken( rLocalName, XML_STRING ))
207 uri.SetFormat(rValue);
208 else
209 SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
213 XMLFontStyleContextFontFaceSrc::XMLFontStyleContextFontFaceSrc( SvXMLImport& rImport,
214 const XMLFontStyleContextFontFace& _font )
215 : SvXMLImportContext( rImport )
216 , font( _font )
220 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFontStyleContextFontFaceSrc::createFastChildContext(
221 sal_Int32 nElement,
222 const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/ )
224 if( nElement == XML_ELEMENT(SVG, XML_FONT_FACE_URI) ||
225 nElement == XML_ELEMENT(SVG_COMPAT, XML_FONT_FACE_URI) )
226 return new XMLFontStyleContextFontFaceUri( GetImport(), font );
227 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
228 return nullptr;
232 XMLFontStyleContextFontFaceUri::XMLFontStyleContextFontFaceUri( SvXMLImport& rImport,
233 const XMLFontStyleContextFontFace& _font )
234 : SvXMLStyleContext( rImport )
235 , font( _font )
239 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFontStyleContextFontFaceUri::createFastChildContext(
240 sal_Int32 nElement,
241 const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/ )
243 if( nElement == XML_ELEMENT(SVG, XML_FONT_FACE_FORMAT) )
244 return new XMLFontStyleContextFontFaceFormat( GetImport(), *this );
245 else if( nElement == XML_ELEMENT(OFFICE, XML_BINARY_DATA) )
247 assert(linkPath.isEmpty());
248 if( linkPath.isEmpty() )
250 mxBase64Stream.set( new comphelper::OSequenceOutputStream( maFontData ) );
251 if( mxBase64Stream.is() )
252 return new XMLBase64ImportContext( GetImport(), mxBase64Stream );
255 else
256 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
257 return nullptr;
260 void XMLFontStyleContextFontFaceUri::SetAttribute( sal_uInt16 nPrefixKey, const OUString& rLocalName,
261 const OUString& rValue )
263 if( nPrefixKey == XML_NAMESPACE_XLINK && IsXMLToken( rLocalName, XML_HREF ))
264 linkPath = rValue;
265 else
266 SvXMLStyleContext::SetAttribute( nPrefixKey, rLocalName, rValue );
269 void XMLFontStyleContextFontFaceUri::SetFormat( const OUString& rFormat )
271 format = rFormat;
274 // the CSS2 standard ( http://www.w3.org/TR/2008/REC-CSS2-20080411/fonts.html#referencing )
275 // defines these format strings.
276 const char OPENTYPE_FORMAT[] = "opentype";
277 const char TRUETYPE_FORMAT[] = "truetype";
278 const char EOT_FORMAT[] = "embedded-opentype";
280 void XMLFontStyleContextFontFaceUri::endFastElement(sal_Int32 )
282 if( ( linkPath.getLength() == 0 ) && ( !maFontData.hasElements() ) )
284 SAL_WARN( "xmloff", "svg:font-face-uri tag with no link or base64 data; ignoring." );
285 return;
287 bool eot;
288 // Assume by default that the font is not compressed.
289 if( format.getLength() == 0
290 || format == OPENTYPE_FORMAT
291 || format == TRUETYPE_FORMAT )
293 eot = false;
295 else if( format == EOT_FORMAT )
297 eot = true;
299 else
301 SAL_WARN( "xmloff", "Unknown format of embedded font; assuming TTF." );
302 eot = false;
304 if ( !maFontData.hasElements() )
305 handleEmbeddedFont( linkPath, eot );
306 else
307 handleEmbeddedFont( maFontData, eot );
310 void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const OUString& url, bool eot )
312 if( GetImport().embeddedFontAlreadyProcessed( url ))
314 GetImport().NotifyContainsEmbeddedFont();
315 return;
317 OUString fontName = font.familyName();
318 // If there's any giveMeStreamForThisURL(), then it's well-hidden for me to find it.
319 if( GetImport().IsPackageURL( url ))
321 uno::Reference< embed::XStorage > storage;
322 storage.set( GetImport().GetSourceStorage(), UNO_SET_THROW );
323 if( url.indexOf( '/' ) > -1 ) // TODO what if more levels?
324 storage.set( storage->openStorageElement( url.copy( 0, url.indexOf( '/' )),
325 ::embed::ElementModes::READ ), uno::UNO_SET_THROW );
326 uno::Reference< io::XInputStream > inputStream;
327 inputStream.set( storage->openStreamElement( url.copy( url.indexOf( '/' ) + 1 ), ::embed::ElementModes::READ ),
328 UNO_QUERY_THROW );
329 if (GetImport().addEmbeddedFont(inputStream, fontName, "?", std::vector< unsigned char >(), eot))
330 GetImport().NotifyContainsEmbeddedFont();
331 inputStream->closeInput();
333 else
334 SAL_WARN( "xmloff", "External URL for font file not handled." );
337 void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const ::css::uno::Sequence< sal_Int8 >& rData, const bool eot )
339 const uno::Reference< io::XInputStream > xInput( new comphelper::SequenceInputStream( rData ) );
340 const OUString fontName = font.familyName();
341 if (GetImport().addEmbeddedFont(xInput, fontName, "?", std::vector< unsigned char >(), eot))
342 GetImport().NotifyContainsEmbeddedFont();
343 xInput->closeInput();
346 SvXMLStyleContext *XMLFontStylesContext::CreateStyleChildContext(
347 sal_Int32 nElement,
348 const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList )
350 if( nElement == XML_ELEMENT(STYLE, XML_FONT_FACE) )
352 return new XMLFontStyleContextFontFace( GetImport(), *this );
354 return SvXMLStylesContext::CreateStyleChildContext( nElement, xAttrList );
358 XMLFontStylesContext::XMLFontStylesContext( SvXMLImport& rImport,
359 rtl_TextEncoding eDfltEnc ) :
360 SvXMLStylesContext( rImport ),
361 pFamilyNameHdl( new XMLFontFamilyNamePropHdl ),
362 pFamilyHdl( new XMLFontFamilyPropHdl ),
363 pPitchHdl( new XMLFontPitchPropHdl ),
364 pEncHdl( new XMLFontEncodingPropHdl ),
365 pFontStyleAttrTokenMap( new SvXMLTokenMap(lcl_getFontStyleAttrTokenMap()) ),
366 eDfltEncoding( eDfltEnc )
370 XMLFontStylesContext::~XMLFontStylesContext() {}
372 bool XMLFontStylesContext::FillProperties( const OUString& rName,
373 ::std::vector< XMLPropertyState > &rProps,
374 sal_Int32 nFamilyNameIdx,
375 sal_Int32 nStyleNameIdx,
376 sal_Int32 nFamilyIdx,
377 sal_Int32 nPitchIdx,
378 sal_Int32 nCharsetIdx ) const
380 const SvXMLStyleContext* pStyle = FindStyleChildContext( XML_STYLE_FAMILY_FONT, rName, true );
381 const XMLFontStyleContextFontFace *pFontStyle = dynamic_cast<const XMLFontStyleContextFontFace*>(pStyle);// use temp var, PTR_CAST is a bad macro, FindStyleChildContext will be called twice
382 if( pFontStyle )
383 pFontStyle->FillProperties( rProps, nFamilyNameIdx, nStyleNameIdx,
384 nFamilyIdx, nPitchIdx, nCharsetIdx );
385 return nullptr != pFontStyle;
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */