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::awt
;
45 using namespace ::xmloff::token
;
48 #define XML_STYLE_FAMILY_FONT XmlStyleFamily::PAGE_MASTER
50 XMLFontStyleContextFontFace::XMLFontStyleContextFontFace( SvXMLImport
& rImport
,
51 XMLFontStylesContext
& rStyles
) :
52 SvXMLStyleContext( rImport
, XML_STYLE_FAMILY_FONT
),
55 aFamilyName
<<= OUString();
56 aStyleName
<<= OUString();
57 aFamily
<<= sal_Int16(awt::FontFamily::DONTKNOW
);
58 aPitch
<<= sal_Int16(awt::FontPitch::DONTKNOW
);
59 aEnc
<<= static_cast<sal_Int16
>(rStyles
.GetDfltCharset());
62 void XMLFontStyleContextFontFace::SetAttribute( sal_Int32 nElement
,
63 const OUString
& rValue
)
65 SvXMLUnitConverter
& rUnitConv
= GetImport().GetMM100UnitConverter();
70 case XML_ELEMENT(SVG
, XML_FONT_FAMILY
):
71 case XML_ELEMENT(SVG_COMPAT
, XML_FONT_FAMILY
):
72 if( GetStyles()->GetFamilyNameHdl().importXML( rValue
, aAny
,
76 case XML_ELEMENT(STYLE
, XML_FONT_ADORNMENTS
):
77 aStyleName
<<= rValue
;
79 case XML_ELEMENT(STYLE
, XML_FONT_FAMILY_GENERIC
):
80 if( GetStyles()->GetFamilyHdl().importXML( rValue
, aAny
,
84 case XML_ELEMENT(STYLE
, XML_FONT_PITCH
):
85 if( GetStyles()->GetPitchHdl().importXML( rValue
, aAny
,
89 case XML_ELEMENT(STYLE
, XML_FONT_CHARSET
):
90 if( GetStyles()->GetEncodingHdl().importXML( rValue
, aAny
,
92 aEnc
= std::move(aAny
);
95 SvXMLStyleContext::SetAttribute( nElement
, rValue
);
100 XMLFontStyleContextFontFace::~XMLFontStyleContextFontFace()
104 void XMLFontStyleContextFontFace::FillProperties(
105 ::std::vector
< XMLPropertyState
> &rProps
,
106 sal_Int32 nFamilyNameIdx
,
107 sal_Int32 nStyleNameIdx
,
108 sal_Int32 nFamilyIdx
,
110 sal_Int32 nCharsetIdx
) const
112 if( nFamilyNameIdx
!= -1 )
114 XMLPropertyState
aPropState( nFamilyNameIdx
, aFamilyName
);
115 rProps
.push_back( aPropState
);
117 if( nStyleNameIdx
!= -1 )
119 XMLPropertyState
aPropState( nStyleNameIdx
, aStyleName
);
120 rProps
.push_back( aPropState
);
122 if( nFamilyIdx
!= -1 )
124 XMLPropertyState
aPropState( nFamilyIdx
, aFamily
);
125 rProps
.push_back( aPropState
);
127 if( nPitchIdx
!= -1 )
129 XMLPropertyState
aPropState( nPitchIdx
, aPitch
);
130 rProps
.push_back( aPropState
);
132 if( nCharsetIdx
!= -1 )
134 XMLPropertyState
aPropState( nCharsetIdx
, aEnc
);
135 rProps
.push_back( aPropState
);
139 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLFontStyleContextFontFace::createFastChildContext(
141 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & )
143 if( nElement
== XML_ELEMENT(SVG
, XML_FONT_FACE_SRC
) ||
144 nElement
== XML_ELEMENT(SVG_COMPAT
, XML_FONT_FACE_SRC
) )
145 return new XMLFontStyleContextFontFaceSrc( GetImport(), *this );
147 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
151 OUString
XMLFontStyleContextFontFace::familyName() const
159 XMLFontStyleContextFontFaceFormat::XMLFontStyleContextFontFaceFormat( SvXMLImport
& rImport
,
160 XMLFontStyleContextFontFaceUri
& _uri
)
161 : SvXMLStyleContext( rImport
)
166 void XMLFontStyleContextFontFaceFormat::SetAttribute( sal_Int32 nElement
,
167 const OUString
& rValue
)
169 if( nElement
== XML_ELEMENT(SVG
, XML_STRING
) || nElement
== XML_ELEMENT(SVG_COMPAT
, XML_STRING
))
170 uri
.SetFormat(rValue
);
172 SvXMLStyleContext::SetAttribute( nElement
, rValue
);
176 XMLFontStyleContextFontFaceSrc::XMLFontStyleContextFontFaceSrc( SvXMLImport
& rImport
,
177 const XMLFontStyleContextFontFace
& _font
)
178 : SvXMLImportContext( rImport
)
183 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLFontStyleContextFontFaceSrc::createFastChildContext(
185 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & /*xAttrList*/ )
187 if( nElement
== XML_ELEMENT(SVG
, XML_FONT_FACE_URI
) ||
188 nElement
== XML_ELEMENT(SVG_COMPAT
, XML_FONT_FACE_URI
) )
189 return new XMLFontStyleContextFontFaceUri( GetImport(), font
);
190 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
195 XMLFontStyleContextFontFaceUri::XMLFontStyleContextFontFaceUri( SvXMLImport
& rImport
,
196 const XMLFontStyleContextFontFace
& _font
)
197 : SvXMLStyleContext( rImport
)
202 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLFontStyleContextFontFaceUri::createFastChildContext(
204 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & /*xAttrList*/ )
206 if( nElement
== XML_ELEMENT(SVG
, XML_FONT_FACE_FORMAT
) )
207 return new XMLFontStyleContextFontFaceFormat( GetImport(), *this );
208 else if( nElement
== XML_ELEMENT(OFFICE
, XML_BINARY_DATA
) )
210 assert(linkPath
.isEmpty());
211 if( linkPath
.isEmpty() )
213 mxBase64Stream
.set( new comphelper::OSequenceOutputStream( maFontData
) );
214 if( mxBase64Stream
.is() )
215 return new XMLBase64ImportContext( GetImport(), mxBase64Stream
);
219 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
223 void XMLFontStyleContextFontFaceUri::SetAttribute( sal_Int32 nElement
,
224 const OUString
& rValue
)
226 if( nElement
== XML_ELEMENT(XLINK
, XML_HREF
) )
229 SvXMLStyleContext::SetAttribute( nElement
, rValue
);
232 void XMLFontStyleContextFontFaceUri::SetFormat( const OUString
& rFormat
)
237 // the CSS2 standard ( http://www.w3.org/TR/2008/REC-CSS2-20080411/fonts.html#referencing )
238 // defines these format strings.
239 const char OPENTYPE_FORMAT
[] = "opentype";
240 const char TRUETYPE_FORMAT
[] = "truetype";
241 const char EOT_FORMAT
[] = "embedded-opentype";
243 void XMLFontStyleContextFontFaceUri::endFastElement(sal_Int32
)
245 if( ( linkPath
.getLength() == 0 ) && ( !maFontData
.hasElements() ) )
247 SAL_WARN( "xmloff", "svg:font-face-uri tag with no link or base64 data; ignoring." );
251 // Assume by default that the font is not compressed.
252 if( format
.getLength() == 0
253 || format
== OPENTYPE_FORMAT
254 || format
== TRUETYPE_FORMAT
)
258 else if( format
== EOT_FORMAT
)
264 SAL_WARN( "xmloff", "Unknown format of embedded font; assuming TTF." );
267 if ( !maFontData
.hasElements() )
268 handleEmbeddedFont( linkPath
, eot
);
270 handleEmbeddedFont( maFontData
, eot
);
273 void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const OUString
& url
, bool eot
)
275 if( GetImport().embeddedFontAlreadyProcessed( url
))
277 GetImport().NotifyContainsEmbeddedFont();
280 OUString fontName
= font
.familyName();
281 // If there's any giveMeStreamForThisURL(), then it's well-hidden for me to find it.
282 if( GetImport().IsPackageURL( url
))
284 uno::Reference
< embed::XStorage
> storage
;
285 storage
.set( GetImport().GetSourceStorage(), UNO_SET_THROW
);
286 if( url
.indexOf( '/' ) > -1 ) // TODO what if more levels?
287 storage
.set( storage
->openStorageElement( url
.copy( 0, url
.indexOf( '/' )),
288 ::embed::ElementModes::READ
), uno::UNO_SET_THROW
);
289 uno::Reference
< io::XInputStream
> inputStream
;
290 inputStream
.set( storage
->openStreamElement( url
.copy( url
.indexOf( '/' ) + 1 ), ::embed::ElementModes::READ
),
292 if (GetImport().addEmbeddedFont(inputStream
, fontName
, u
"?", std::vector
< unsigned char >(), eot
))
293 GetImport().NotifyContainsEmbeddedFont();
294 inputStream
->closeInput();
297 SAL_WARN( "xmloff", "External URL for font file not handled." );
300 void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const ::css::uno::Sequence
< sal_Int8
>& rData
, const bool eot
)
302 const uno::Reference
< io::XInputStream
> xInput( new comphelper::SequenceInputStream( rData
) );
303 const OUString fontName
= font
.familyName();
304 if (GetImport().addEmbeddedFont(xInput
, fontName
, u
"?", std::vector
< unsigned char >(), eot
))
305 GetImport().NotifyContainsEmbeddedFont();
306 xInput
->closeInput();
309 SvXMLStyleContext
*XMLFontStylesContext::CreateStyleChildContext(
311 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & xAttrList
)
313 if( nElement
== XML_ELEMENT(STYLE
, XML_FONT_FACE
) )
315 return new XMLFontStyleContextFontFace( GetImport(), *this );
317 return SvXMLStylesContext::CreateStyleChildContext( nElement
, xAttrList
);
321 XMLFontStylesContext::XMLFontStylesContext( SvXMLImport
& rImport
,
322 rtl_TextEncoding eDfltEnc
) :
323 SvXMLStylesContext( rImport
),
324 m_pFamilyNameHdl( new XMLFontFamilyNamePropHdl
),
325 m_pFamilyHdl( new XMLFontFamilyPropHdl
),
326 m_pPitchHdl( new XMLFontPitchPropHdl
),
327 m_pEncHdl( new XMLFontEncodingPropHdl
),
328 m_eDefaultEncoding( eDfltEnc
)
332 XMLFontStylesContext::~XMLFontStylesContext() {}
334 bool XMLFontStylesContext::FillProperties( const OUString
& rName
,
335 ::std::vector
< XMLPropertyState
> &rProps
,
336 sal_Int32 nFamilyNameIdx
,
337 sal_Int32 nStyleNameIdx
,
338 sal_Int32 nFamilyIdx
,
340 sal_Int32 nCharsetIdx
) const
342 const SvXMLStyleContext
* pStyle
= FindStyleChildContext( XML_STYLE_FAMILY_FONT
, rName
, true );
343 const XMLFontStyleContextFontFace
*pFontStyle
= dynamic_cast<const XMLFontStyleContextFontFace
*>(pStyle
);// use temp var, PTR_CAST is a bad macro, FindStyleChildContext will be called twice
345 pFontStyle
->FillProperties( rProps
, nFamilyNameIdx
, nStyleNameIdx
,
346 nFamilyIdx
, nPitchIdx
, nCharsetIdx
);
347 return nullptr != pFontStyle
;
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */