1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 XMLFontStyleContextFontFace::XMLFontStyleContextFontFace( SvXMLImport
& rImport
,
53 XMLFontStylesContext
& rStyles
) :
54 SvXMLStyleContext( rImport
, XML_STYLE_FAMILY_FONT
),
57 aFamilyName
<<= OUString();
58 aStyleName
<<= OUString();
59 aFamily
<<= sal_Int16(awt::FontFamily::DONTKNOW
);
60 aPitch
<<= sal_Int16(awt::FontPitch::DONTKNOW
);
61 aEnc
<<= static_cast<sal_Int16
>(rStyles
.GetDfltCharset());
64 void XMLFontStyleContextFontFace::SetAttribute( sal_Int32 nElement
,
65 const OUString
& rValue
)
67 SvXMLUnitConverter
& rUnitConv
= GetImport().GetMM100UnitConverter();
72 case XML_ELEMENT(SVG
, XML_FONT_FAMILY
):
73 case XML_ELEMENT(SVG_COMPAT
, XML_FONT_FAMILY
):
74 if( GetStyles()->GetFamilyNameHdl().importXML( rValue
, aAny
,
78 case XML_ELEMENT(STYLE
, XML_FONT_ADORNMENTS
):
79 aStyleName
<<= rValue
;
81 case XML_ELEMENT(STYLE
, XML_FONT_FAMILY_GENERIC
):
82 if( GetStyles()->GetFamilyHdl().importXML( rValue
, aAny
,
86 case XML_ELEMENT(STYLE
, XML_FONT_PITCH
):
87 if( GetStyles()->GetPitchHdl().importXML( rValue
, aAny
,
91 case XML_ELEMENT(STYLE
, XML_FONT_CHARSET
):
92 if( GetStyles()->GetEncodingHdl().importXML( rValue
, aAny
,
97 SvXMLStyleContext::SetAttribute( nElement
, rValue
);
102 XMLFontStyleContextFontFace::~XMLFontStyleContextFontFace()
106 void XMLFontStyleContextFontFace::FillProperties(
107 ::std::vector
< XMLPropertyState
> &rProps
,
108 sal_Int32 nFamilyNameIdx
,
109 sal_Int32 nStyleNameIdx
,
110 sal_Int32 nFamilyIdx
,
112 sal_Int32 nCharsetIdx
) const
114 if( nFamilyNameIdx
!= -1 )
116 XMLPropertyState
aPropState( nFamilyNameIdx
, aFamilyName
);
117 rProps
.push_back( aPropState
);
119 if( nStyleNameIdx
!= -1 )
121 XMLPropertyState
aPropState( nStyleNameIdx
, aStyleName
);
122 rProps
.push_back( aPropState
);
124 if( nFamilyIdx
!= -1 )
126 XMLPropertyState
aPropState( nFamilyIdx
, aFamily
);
127 rProps
.push_back( aPropState
);
129 if( nPitchIdx
!= -1 )
131 XMLPropertyState
aPropState( nPitchIdx
, aPitch
);
132 rProps
.push_back( aPropState
);
134 if( nCharsetIdx
!= -1 )
136 XMLPropertyState
aPropState( nCharsetIdx
, aEnc
);
137 rProps
.push_back( aPropState
);
141 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLFontStyleContextFontFace::createFastChildContext(
143 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & )
145 if( nElement
== XML_ELEMENT(SVG
, XML_FONT_FACE_SRC
) ||
146 nElement
== XML_ELEMENT(SVG_COMPAT
, XML_FONT_FACE_SRC
) )
147 return new XMLFontStyleContextFontFaceSrc( GetImport(), *this );
149 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
153 OUString
XMLFontStyleContextFontFace::familyName() const
161 XMLFontStyleContextFontFaceFormat::XMLFontStyleContextFontFaceFormat( SvXMLImport
& rImport
,
162 XMLFontStyleContextFontFaceUri
& _uri
)
163 : SvXMLStyleContext( rImport
)
168 void XMLFontStyleContextFontFaceFormat::SetAttribute( sal_Int32 nElement
,
169 const OUString
& rValue
)
171 if( nElement
== XML_ELEMENT(SVG
, XML_STRING
) || nElement
== XML_ELEMENT(SVG_COMPAT
, XML_STRING
))
172 uri
.SetFormat(rValue
);
174 SvXMLStyleContext::SetAttribute( nElement
, rValue
);
178 XMLFontStyleContextFontFaceSrc::XMLFontStyleContextFontFaceSrc( SvXMLImport
& rImport
,
179 const XMLFontStyleContextFontFace
& _font
)
180 : SvXMLImportContext( rImport
)
185 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLFontStyleContextFontFaceSrc::createFastChildContext(
187 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & /*xAttrList*/ )
189 if( nElement
== XML_ELEMENT(SVG
, XML_FONT_FACE_URI
) ||
190 nElement
== XML_ELEMENT(SVG_COMPAT
, XML_FONT_FACE_URI
) )
191 return new XMLFontStyleContextFontFaceUri( GetImport(), font
);
192 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
197 XMLFontStyleContextFontFaceUri::XMLFontStyleContextFontFaceUri( SvXMLImport
& rImport
,
198 const XMLFontStyleContextFontFace
& _font
)
199 : SvXMLStyleContext( rImport
)
204 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLFontStyleContextFontFaceUri::createFastChildContext(
206 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & /*xAttrList*/ )
208 if( nElement
== XML_ELEMENT(SVG
, XML_FONT_FACE_FORMAT
) )
209 return new XMLFontStyleContextFontFaceFormat( GetImport(), *this );
210 else if( nElement
== XML_ELEMENT(OFFICE
, XML_BINARY_DATA
) )
212 assert(linkPath
.isEmpty());
213 if( linkPath
.isEmpty() )
215 mxBase64Stream
.set( new comphelper::OSequenceOutputStream( maFontData
) );
216 if( mxBase64Stream
.is() )
217 return new XMLBase64ImportContext( GetImport(), mxBase64Stream
);
221 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
225 void XMLFontStyleContextFontFaceUri::SetAttribute( sal_Int32 nElement
,
226 const OUString
& rValue
)
228 if( nElement
== XML_ELEMENT(XLINK
, XML_HREF
) )
231 SvXMLStyleContext::SetAttribute( nElement
, rValue
);
234 void XMLFontStyleContextFontFaceUri::SetFormat( const OUString
& rFormat
)
239 // the CSS2 standard ( http://www.w3.org/TR/2008/REC-CSS2-20080411/fonts.html#referencing )
240 // defines these format strings.
241 const char OPENTYPE_FORMAT
[] = "opentype";
242 const char TRUETYPE_FORMAT
[] = "truetype";
243 const char EOT_FORMAT
[] = "embedded-opentype";
245 void XMLFontStyleContextFontFaceUri::endFastElement(sal_Int32
)
247 if( ( linkPath
.getLength() == 0 ) && ( !maFontData
.hasElements() ) )
249 SAL_WARN( "xmloff", "svg:font-face-uri tag with no link or base64 data; ignoring." );
253 // Assume by default that the font is not compressed.
254 if( format
.getLength() == 0
255 || format
== OPENTYPE_FORMAT
256 || format
== TRUETYPE_FORMAT
)
260 else if( format
== EOT_FORMAT
)
266 SAL_WARN( "xmloff", "Unknown format of embedded font; assuming TTF." );
269 if ( !maFontData
.hasElements() )
270 handleEmbeddedFont( linkPath
, eot
);
272 handleEmbeddedFont( maFontData
, eot
);
275 void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const OUString
& url
, bool eot
)
277 if( GetImport().embeddedFontAlreadyProcessed( url
))
279 GetImport().NotifyContainsEmbeddedFont();
282 OUString fontName
= font
.familyName();
283 // If there's any giveMeStreamForThisURL(), then it's well-hidden for me to find it.
284 if( GetImport().IsPackageURL( url
))
286 uno::Reference
< embed::XStorage
> storage
;
287 storage
.set( GetImport().GetSourceStorage(), UNO_SET_THROW
);
288 if( url
.indexOf( '/' ) > -1 ) // TODO what if more levels?
289 storage
.set( storage
->openStorageElement( url
.copy( 0, url
.indexOf( '/' )),
290 ::embed::ElementModes::READ
), uno::UNO_SET_THROW
);
291 uno::Reference
< io::XInputStream
> inputStream
;
292 inputStream
.set( storage
->openStreamElement( url
.copy( url
.indexOf( '/' ) + 1 ), ::embed::ElementModes::READ
),
294 if (GetImport().addEmbeddedFont(inputStream
, fontName
, "?", std::vector
< unsigned char >(), eot
))
295 GetImport().NotifyContainsEmbeddedFont();
296 inputStream
->closeInput();
299 SAL_WARN( "xmloff", "External URL for font file not handled." );
302 void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const ::css::uno::Sequence
< sal_Int8
>& rData
, const bool eot
)
304 const uno::Reference
< io::XInputStream
> xInput( new comphelper::SequenceInputStream( rData
) );
305 const OUString fontName
= font
.familyName();
306 if (GetImport().addEmbeddedFont(xInput
, fontName
, "?", std::vector
< unsigned char >(), eot
))
307 GetImport().NotifyContainsEmbeddedFont();
308 xInput
->closeInput();
311 SvXMLStyleContext
*XMLFontStylesContext::CreateStyleChildContext(
313 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & xAttrList
)
315 if( nElement
== XML_ELEMENT(STYLE
, XML_FONT_FACE
) )
317 return new XMLFontStyleContextFontFace( GetImport(), *this );
319 return SvXMLStylesContext::CreateStyleChildContext( nElement
, xAttrList
);
323 XMLFontStylesContext::XMLFontStylesContext( SvXMLImport
& rImport
,
324 rtl_TextEncoding eDfltEnc
) :
325 SvXMLStylesContext( rImport
),
326 m_pFamilyNameHdl( new XMLFontFamilyNamePropHdl
),
327 m_pFamilyHdl( new XMLFontFamilyPropHdl
),
328 m_pPitchHdl( new XMLFontPitchPropHdl
),
329 m_pEncHdl( new XMLFontEncodingPropHdl
),
330 m_eDefaultEncoding( eDfltEnc
)
334 XMLFontStylesContext::~XMLFontStylesContext() {}
336 bool XMLFontStylesContext::FillProperties( const OUString
& rName
,
337 ::std::vector
< XMLPropertyState
> &rProps
,
338 sal_Int32 nFamilyNameIdx
,
339 sal_Int32 nStyleNameIdx
,
340 sal_Int32 nFamilyIdx
,
342 sal_Int32 nCharsetIdx
) const
344 const SvXMLStyleContext
* pStyle
= FindStyleChildContext( XML_STYLE_FAMILY_FONT
, rName
, true );
345 const XMLFontStyleContextFontFace
*pFontStyle
= dynamic_cast<const XMLFontStyleContextFontFace
*>(pStyle
);// use temp var, PTR_CAST is a bad macro, FindStyleChildContext will be called twice
347 pFontStyle
->FillProperties( rProps
, nFamilyNameIdx
, nStyleNameIdx
,
348 nFamilyIdx
, nPitchIdx
, nCharsetIdx
);
349 return nullptr != pFontStyle
;
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */