Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/style/XMLFontStylesContext.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#include <xmloff/XMLFontStylesContext.hxx>
21
#include "XMLFontStylesContext_impl.hxx"
22
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>
27
28
#include <comphelper/seqstream.hxx>
29
30
#include <sal/log.hxx>
31
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>
38
39
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;
46
47
48
78.0k
#define XML_STYLE_FAMILY_FONT XmlStyleFamily::PAGE_MASTER
49
50
XMLFontStyleContextFontFace::XMLFontStyleContextFontFace( SvXMLImport& rImport,
51
        XMLFontStylesContext& rStyles ) :
52
26.5k
    SvXMLStyleContext( rImport, XML_STYLE_FAMILY_FONT ),
53
26.5k
    xStyles( &rStyles )
54
26.5k
{
55
26.5k
    aFamilyName <<= OUString();
56
26.5k
    aStyleName <<= OUString();
57
26.5k
    aFamily <<= sal_Int16(awt::FontFamily::DONTKNOW);
58
26.5k
    aPitch <<= sal_Int16(awt::FontPitch::DONTKNOW);
59
26.5k
    aEnc <<= static_cast<sal_Int16>(rStyles.GetDfltCharset());
60
26.5k
}
61
62
void XMLFontStyleContextFontFace::SetAttribute( sal_Int32 nElement,
63
                                        const OUString& rValue )
64
89.1k
{
65
89.1k
    SvXMLUnitConverter& rUnitConv = GetImport().GetMM100UnitConverter();
66
89.1k
    Any aAny;
67
68
89.1k
    switch(nElement)
69
89.1k
    {
70
0
    case XML_ELEMENT(SVG, XML_FONT_FAMILY):
71
20.1k
    case XML_ELEMENT(SVG_COMPAT, XML_FONT_FAMILY):
72
20.1k
        if( GetStyles()->GetFamilyNameHdl().importXML( rValue, aAny,
73
20.1k
                                                          rUnitConv ) )
74
20.1k
            aFamilyName = aAny;
75
20.1k
        break;
76
3.88k
    case XML_ELEMENT(STYLE, XML_FONT_ADORNMENTS):
77
3.88k
        aStyleName <<= rValue;
78
3.88k
        break;
79
16.1k
    case XML_ELEMENT(STYLE, XML_FONT_FAMILY_GENERIC):
80
16.1k
        if( GetStyles()->GetFamilyHdl().importXML( rValue, aAny,
81
16.1k
                                                      rUnitConv ) )
82
16.0k
            aFamily = aAny;
83
16.1k
        break;
84
21.8k
    case XML_ELEMENT(STYLE, XML_FONT_PITCH):
85
21.8k
        if( GetStyles()->GetPitchHdl().importXML( rValue, aAny,
86
21.8k
                                                      rUnitConv ) )
87
20.6k
            aPitch = aAny;
88
21.8k
        break;
89
1.17k
    case XML_ELEMENT(STYLE, XML_FONT_CHARSET):
90
1.17k
        if( GetStyles()->GetEncodingHdl().importXML( rValue, aAny,
91
1.17k
                                                      rUnitConv ) )
92
1.17k
            aEnc = std::move(aAny);
93
1.17k
        break;
94
26.0k
    default:
95
26.0k
        SvXMLStyleContext::SetAttribute( nElement, rValue );
96
26.0k
        break;
97
89.1k
    }
98
89.1k
}
99
100
XMLFontStyleContextFontFace::~XMLFontStyleContextFontFace()
101
26.5k
{
102
26.5k
}
103
104
void XMLFontStyleContextFontFace::FillProperties(
105
        ::std::vector< XMLPropertyState > &rProps,
106
        sal_Int32 nFamilyNameIdx,
107
        sal_Int32 nStyleNameIdx,
108
        sal_Int32 nFamilyIdx,
109
        sal_Int32 nPitchIdx,
110
        sal_Int32 nCharsetIdx ) const
111
35.0k
{
112
35.0k
    if( nFamilyNameIdx != -1 )
113
35.0k
    {
114
35.0k
        XMLPropertyState aPropState( nFamilyNameIdx, aFamilyName );
115
35.0k
        rProps.push_back( aPropState );
116
35.0k
    }
117
35.0k
    if( nStyleNameIdx != -1 )
118
35.0k
    {
119
35.0k
        XMLPropertyState aPropState( nStyleNameIdx, aStyleName );
120
35.0k
        rProps.push_back( aPropState );
121
35.0k
    }
122
35.0k
    if( nFamilyIdx != -1 )
123
35.0k
    {
124
35.0k
        XMLPropertyState aPropState( nFamilyIdx, aFamily );
125
35.0k
        rProps.push_back( aPropState );
126
35.0k
    }
127
35.0k
    if( nPitchIdx != -1 )
128
35.0k
    {
129
35.0k
        XMLPropertyState aPropState( nPitchIdx, aPitch );
130
35.0k
        rProps.push_back( aPropState );
131
35.0k
    }
132
35.0k
    if( nCharsetIdx != -1 )
133
35.0k
    {
134
35.0k
        XMLPropertyState aPropState( nCharsetIdx, aEnc );
135
35.0k
        rProps.push_back( aPropState );
136
35.0k
    }
137
35.0k
}
138
139
css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFontStyleContextFontFace::createFastChildContext(
140
        sal_Int32 nElement,
141
        const css::uno::Reference< css::xml::sax::XFastAttributeList > &  )
142
401
{
143
401
    if( nElement == XML_ELEMENT(SVG, XML_FONT_FACE_SRC) ||
144
401
        nElement == XML_ELEMENT(SVG_COMPAT, XML_FONT_FACE_SRC) )
145
0
        return new XMLFontStyleContextFontFaceSrc( GetImport(), *this );
146
401
    else
147
401
        XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
148
401
    return nullptr;
149
401
}
150
151
OUString XMLFontStyleContextFontFace::familyName() const
152
0
{
153
0
    OUString ret;
154
0
    aFamilyName >>= ret;
155
0
    return ret;
156
0
}
157
158
OUString XMLFontStyleContextFontFace::styleName() const
159
0
{
160
0
    OUString ret;
161
0
    aStyleName >>= ret;
162
0
    return ret;
163
0
}
164
165
166
XMLFontStyleContextFontFaceFormat::XMLFontStyleContextFontFaceFormat( SvXMLImport& rImport,
167
        XMLFontStyleContextFontFaceUri& _uri )
168
0
    : SvXMLStyleContext( rImport )
169
0
    , uri(_uri)
170
0
{
171
0
}
172
173
void XMLFontStyleContextFontFaceFormat::SetAttribute( sal_Int32 nElement,
174
    const OUString& rValue )
175
0
{
176
0
    if( nElement == XML_ELEMENT(SVG, XML_STRING) || nElement == XML_ELEMENT(SVG_COMPAT, XML_STRING))
177
0
        uri.SetFormat(rValue);
178
0
    else
179
0
        SvXMLStyleContext::SetAttribute( nElement, rValue );
180
0
}
181
182
183
XMLFontStyleContextFontFaceSrc::XMLFontStyleContextFontFaceSrc( SvXMLImport& rImport,
184
        const XMLFontStyleContextFontFace& _font )
185
0
    : SvXMLImportContext( rImport )
186
0
    , font( _font )
187
0
{
188
0
}
189
190
css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFontStyleContextFontFaceSrc::createFastChildContext(
191
        sal_Int32 nElement,
192
        const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/ )
193
0
{
194
0
    if( nElement == XML_ELEMENT(SVG, XML_FONT_FACE_URI) ||
195
0
        nElement == XML_ELEMENT(SVG_COMPAT, XML_FONT_FACE_URI) )
196
0
        return new XMLFontStyleContextFontFaceUri( GetImport(), font );
197
0
    XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
198
0
    return nullptr;
199
0
}
200
201
202
XMLFontStyleContextFontFaceUri::XMLFontStyleContextFontFaceUri( SvXMLImport& rImport,
203
        const XMLFontStyleContextFontFace& _font )
204
0
    : SvXMLStyleContext( rImport )
205
0
    , font( _font )
206
0
{
207
0
}
208
209
css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFontStyleContextFontFaceUri::createFastChildContext(
210
        sal_Int32 nElement,
211
        const css::uno::Reference< css::xml::sax::XFastAttributeList > & /*xAttrList*/ )
212
0
{
213
0
    if( nElement == XML_ELEMENT(SVG, XML_FONT_FACE_FORMAT) )
214
0
        return new XMLFontStyleContextFontFaceFormat( GetImport(), *this );
215
0
    else if( nElement == XML_ELEMENT(OFFICE, XML_BINARY_DATA) )
216
0
    {
217
0
        assert(linkPath.isEmpty());
218
0
        if( linkPath.isEmpty() )
219
0
        {
220
0
            mxBase64Stream.set( new comphelper::OSequenceOutputStream( maFontData ) );
221
0
            if( mxBase64Stream.is() )
222
0
                return new XMLBase64ImportContext( GetImport(), mxBase64Stream );
223
0
        }
224
0
    }
225
0
    else
226
0
        XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
227
0
    return nullptr;
228
0
}
229
230
void XMLFontStyleContextFontFaceUri::SetAttribute( sal_Int32 nElement,
231
    const OUString& rValue )
232
0
{
233
0
    if( nElement == XML_ELEMENT(XLINK, XML_HREF) )
234
0
        linkPath = rValue;
235
0
    else
236
0
        SvXMLStyleContext::SetAttribute( nElement, rValue );
237
0
}
238
239
void XMLFontStyleContextFontFaceUri::SetFormat( const OUString& rFormat )
240
0
{
241
0
    format = rFormat;
242
0
}
243
244
// the CSS2 standard ( http://www.w3.org/TR/2008/REC-CSS2-20080411/fonts.html#referencing )
245
// defines these format strings.
246
const char OPENTYPE_FORMAT[] = "opentype";
247
const char TRUETYPE_FORMAT[] = "truetype";
248
const char EOT_FORMAT[]      = "embedded-opentype";
249
250
void XMLFontStyleContextFontFaceUri::endFastElement(sal_Int32 )
251
0
{
252
0
    if( ( linkPath.getLength() == 0 ) && ( !maFontData.hasElements() ) )
253
0
    {
254
0
        SAL_WARN( "xmloff", "svg:font-face-uri tag with no link or base64 data; ignoring." );
255
0
        return;
256
0
    }
257
0
    bool eot;
258
    // Assume by default that the font is not compressed.
259
0
    if( format.getLength() == 0
260
0
        || format == OPENTYPE_FORMAT
261
0
        || format == TRUETYPE_FORMAT )
262
0
    {
263
0
        eot = false;
264
0
    }
265
0
    else if( format == EOT_FORMAT )
266
0
    {
267
0
        eot = true;
268
0
    }
269
0
    else
270
0
    {
271
0
        SAL_WARN( "xmloff", "Unknown format of embedded font; assuming TTF." );
272
0
        eot = false;
273
0
    }
274
0
    if ( !maFontData.hasElements() )
275
0
        handleEmbeddedFont( linkPath, eot );
276
0
    else
277
0
        handleEmbeddedFont( maFontData, eot );
278
0
}
279
280
void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const OUString& url, bool eot )
281
0
{
282
0
    if( GetImport().embeddedFontAlreadyProcessed( url ))
283
0
    {
284
0
        GetImport().NotifyContainsEmbeddedFont();
285
0
        return;
286
0
    }
287
0
    OUString fontName = font.familyName();
288
    // If there's any giveMeStreamForThisURL(), then it's well-hidden for me to find it.
289
0
    if( GetImport().IsPackageURL( url ))
290
0
    {
291
0
        uno::Reference< embed::XStorage > storage;
292
0
        storage.set( GetImport().GetSourceStorage(), UNO_SET_THROW );
293
0
        if( url.indexOf( '/' ) > -1 ) // TODO what if more levels?
294
0
            storage.set( storage->openStorageElement( url.copy( 0, url.indexOf( '/' )),
295
0
                ::embed::ElementModes::READ ), uno::UNO_SET_THROW );
296
0
        uno::Reference< io::XInputStream > inputStream;
297
0
        inputStream.set( storage->openStreamElement( url.copy( url.indexOf( '/' ) + 1 ), ::embed::ElementModes::READ ),
298
0
            UNO_QUERY_THROW );
299
0
        if (GetImport().addEmbeddedFont(inputStream, fontName, font.styleName(), std::vector< unsigned char >(), eot))
300
0
            GetImport().NotifyContainsEmbeddedFont();
301
0
        inputStream->closeInput();
302
0
    }
303
0
    else
304
0
        SAL_WARN( "xmloff", "External URL for font file not handled." );
305
0
}
306
307
void XMLFontStyleContextFontFaceUri::handleEmbeddedFont( const ::css::uno::Sequence< sal_Int8 >& rData, const bool eot )
308
0
{
309
0
    const uno::Reference< io::XInputStream > xInput( new comphelper::SequenceInputStream( rData ) );
310
0
    const OUString fontName = font.familyName();
311
0
    if (GetImport().addEmbeddedFont(xInput, fontName, font.styleName(), std::vector< unsigned char >(), eot))
312
0
        GetImport().NotifyContainsEmbeddedFont();
313
0
    xInput->closeInput();
314
0
}
315
316
SvXMLStyleContext *XMLFontStylesContext::CreateStyleChildContext(
317
        sal_Int32 nElement,
318
        const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList )
319
33.5k
{
320
33.5k
    if( nElement == XML_ELEMENT(STYLE, XML_FONT_FACE) )
321
26.5k
    {
322
26.5k
        return new XMLFontStyleContextFontFace( GetImport(), *this );
323
26.5k
    }
324
7.00k
    return SvXMLStylesContext::CreateStyleChildContext( nElement, xAttrList );
325
33.5k
}
326
327
328
XMLFontStylesContext::XMLFontStylesContext( SvXMLImport& rImport,
329
        rtl_TextEncoding eDfltEnc ) :
330
9.93k
    SvXMLStylesContext( rImport ),
331
9.93k
    m_pFamilyNameHdl( new XMLFontFamilyNamePropHdl ),
332
9.93k
    m_pFamilyHdl( new XMLFontFamilyPropHdl ),
333
9.93k
    m_pPitchHdl( new XMLFontPitchPropHdl ),
334
9.93k
    m_pEncHdl( new XMLFontEncodingPropHdl ),
335
9.93k
    m_eDefaultEncoding( eDfltEnc )
336
9.93k
{
337
9.93k
}
338
339
9.93k
XMLFontStylesContext::~XMLFontStylesContext() {}
340
341
bool XMLFontStylesContext::FillProperties( const OUString& rName,
342
                         ::std::vector< XMLPropertyState > &rProps,
343
                         sal_Int32 nFamilyNameIdx,
344
                         sal_Int32 nStyleNameIdx,
345
                         sal_Int32 nFamilyIdx,
346
                         sal_Int32 nPitchIdx,
347
                         sal_Int32 nCharsetIdx ) const
348
51.5k
{
349
51.5k
    const SvXMLStyleContext* pStyle = FindStyleChildContext( XML_STYLE_FAMILY_FONT, rName, true );
350
51.5k
    const XMLFontStyleContextFontFace *pFontStyle = dynamic_cast<const XMLFontStyleContextFontFace*>(pStyle);// use temp var, PTR_CAST is a bad macro, FindStyleChildContext will be called twice
351
51.5k
    if( pFontStyle )
352
35.0k
        pFontStyle->FillProperties( rProps, nFamilyNameIdx, nStyleNameIdx,
353
35.0k
                                    nFamilyIdx, nPitchIdx, nCharsetIdx );
354
51.5k
    return nullptr != pFontStyle;
355
51.5k
}
356
357
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */