Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/style/fonthdl.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 <sal/config.h>
21
22
#include <string_view>
23
24
#include "fonthdl.hxx"
25
26
#include <sax/tools/converter.hxx>
27
28
#include <xmloff/xmltoken.hxx>
29
#include <xmloff/xmluconv.hxx>
30
#include <xmloff/xmlement.hxx>
31
#include <rtl/ustrbuf.hxx>
32
#include <com/sun/star/uno/Any.hxx>
33
#include <tools/fontenum.hxx>
34
35
using namespace ::com::sun::star;
36
using namespace ::xmloff::token;
37
38
static const SvXMLEnumMapEntry<FontFamily>* lcl_getFontFamilyGenericMapping()
39
19.5k
{
40
19.5k
    static SvXMLEnumMapEntry<FontFamily> const aFontFamilyGenericMapping[] =
41
19.5k
    {
42
19.5k
        { XML_DECORATIVE,       FAMILY_DECORATIVE },
43
44
19.5k
        { XML_MODERN,           FAMILY_MODERN   },
45
19.5k
        { XML_ROMAN,            FAMILY_ROMAN    },
46
19.5k
        { XML_SCRIPT,           FAMILY_SCRIPT   },
47
19.5k
        { XML_SWISS,            FAMILY_SWISS    },
48
19.5k
        { XML_SYSTEM,           FAMILY_SYSTEM   },
49
19.5k
        { XML_TOKEN_INVALID,    FontFamily(0)   }
50
19.5k
    };
51
19.5k
    return aFontFamilyGenericMapping;
52
19.5k
}
53
54
SvXMLEnumMapEntry<FontPitch> const aFontPitchMapping[] =
55
{
56
    { XML_FIXED,            PITCH_FIXED     },
57
    { XML_VARIABLE,         PITCH_VARIABLE  },
58
    { XML_TOKEN_INVALID,    FontPitch(0)    }
59
};
60
61
62
XMLFontFamilyNamePropHdl::~XMLFontFamilyNamePropHdl()
63
101k
{
64
    // Nothing to do
65
101k
}
66
67
bool XMLFontFamilyNamePropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
68
31.7k
{
69
31.7k
    bool bRet = false;
70
31.7k
    OUStringBuffer sValue;
71
31.7k
    sal_Int32 nPos = 0;
72
73
31.7k
    do
74
54.1k
    {
75
54.1k
        sal_Int32 nFirst = nPos;
76
54.1k
        nPos = ::sax::Converter::indexOfComma( rStrImpValue, nPos );
77
54.1k
        sal_Int32 nLast = (-1 == nPos ? rStrImpValue.getLength() - 1 : nPos - 1);
78
79
        // skip trailing blanks
80
54.1k
        while( nLast > nFirst && ' ' == rStrImpValue[nLast] )
81
1
            nLast--;
82
83
        // skip leading blanks
84
54.1k
        while(nFirst <= nLast && ' ' == rStrImpValue[nFirst])
85
5
            nFirst++;
86
87
        // remove quotes
88
54.1k
        sal_Unicode c = nFirst > nLast ? 0 : rStrImpValue[nFirst];
89
54.1k
        if( nFirst < nLast && ('\'' == c || '\"' == c) && rStrImpValue[nLast] == c )
90
15.4k
        {
91
15.4k
            nFirst++;
92
15.4k
            nLast--;
93
15.4k
        }
94
95
54.1k
        if( nFirst <= nLast )
96
54.0k
        {
97
54.0k
            if( !sValue.isEmpty() )
98
22.2k
                sValue.append(';');
99
100
54.0k
            sValue.append(rStrImpValue.subView(nFirst, nLast-nFirst+1));
101
54.0k
        }
102
103
54.1k
        if( -1 != nPos )
104
22.4k
            nPos++;
105
54.1k
    }
106
54.1k
    while( -1 != nPos );
107
108
31.7k
    if (!sValue.isEmpty())
109
31.7k
    {
110
31.7k
        rValue <<= sValue.makeStringAndClear();
111
31.7k
        bRet = true;
112
31.7k
    }
113
114
31.7k
    return bRet;
115
31.7k
}
116
117
bool XMLFontFamilyNamePropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
118
2.35k
{
119
2.35k
    bool bRet = false;
120
2.35k
    OUString aStrFamilyName;
121
122
2.35k
    if( rValue >>= aStrFamilyName )
123
2.35k
    {
124
2.35k
        OUStringBuffer sValue( aStrFamilyName.getLength() + 2 );
125
2.35k
        sal_Int32 nPos = 0;
126
2.35k
        do
127
2.35k
        {
128
2.35k
            sal_Int32 nFirst = nPos;
129
2.35k
            nPos = aStrFamilyName.indexOf( ';', nPos );
130
2.35k
            sal_Int32 nLast = (-1 == nPos ? aStrFamilyName.getLength() : nPos);
131
132
            // Set position to the character behind the ';', so we won't
133
            // forget this.
134
2.35k
            if( -1 != nPos )
135
0
                nPos++;
136
137
            // If the property value was empty, we stop now.
138
            // If there is a ';' at the first position, the empty name
139
            // at the start will be removed.
140
2.35k
            if( 0 == nLast )
141
0
                continue;
142
143
            // nFirst and nLast now denote the first and last character of
144
            // one font name.
145
2.35k
            nLast--;
146
147
            // skip trailing blanks
148
2.35k
            while(  nLast > nFirst && ' ' == aStrFamilyName[nLast] )
149
0
                nLast--;
150
151
            // skip leading blanks
152
2.35k
            while( nFirst <= nLast && ' ' == aStrFamilyName[nFirst] )
153
0
                nFirst++;
154
155
2.35k
            if( nFirst <= nLast )
156
2.35k
            {
157
2.35k
                if( !sValue.isEmpty() )
158
0
                    sValue.append( ", " );
159
2.35k
                sal_Int32 nLen = nLast-nFirst+1;
160
2.35k
                std::u16string_view sFamily( aStrFamilyName.subView( nFirst, nLen ) );
161
2.35k
                bool bQuote = false;
162
24.8k
                for( sal_Int32 i=0; i < nLen; i++ )
163
23.2k
                {
164
23.2k
                    sal_Unicode c = sFamily[i];
165
23.2k
                    if( ' ' == c || ',' == c )
166
788
                    {
167
788
                        bQuote = true;
168
788
                        break;
169
788
                    }
170
23.2k
                }
171
2.35k
                if( bQuote )
172
788
                    sValue.append( '\'' );
173
2.35k
                sValue.append( sFamily );
174
2.35k
                if( bQuote )
175
788
                    sValue.append( '\'' );
176
2.35k
            }
177
2.35k
        }
178
2.35k
        while( -1 != nPos );
179
180
2.35k
        rStrExpValue = sValue.makeStringAndClear();
181
182
2.35k
        bRet = true;
183
2.35k
    }
184
185
2.35k
    return bRet;
186
2.35k
}
187
188
189
XMLFontFamilyPropHdl::~XMLFontFamilyPropHdl()
190
101k
{
191
    // Nothing to do
192
101k
}
193
194
bool XMLFontFamilyPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
195
18.8k
{
196
18.8k
    FontFamily eNewFamily;
197
18.8k
    bool bRet = SvXMLUnitConverter::convertEnum( eNewFamily, rStrImpValue, lcl_getFontFamilyGenericMapping() );
198
18.8k
    if( bRet )
199
18.7k
        rValue <<= static_cast<sal_Int16>(eNewFamily);
200
201
18.8k
    return bRet;
202
18.8k
}
203
204
bool XMLFontFamilyPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
205
2.35k
{
206
2.35k
    bool bRet = false;
207
2.35k
    OUStringBuffer aOut;
208
209
2.35k
    sal_Int16 nFamily = sal_Int16();
210
2.35k
    if( rValue >>= nFamily )
211
2.35k
    {
212
2.35k
        FontFamily eFamily = static_cast<FontFamily>(nFamily);
213
2.35k
        if( eFamily != FAMILY_DONTKNOW )
214
750
            bRet = SvXMLUnitConverter::convertEnum( aOut, eFamily, lcl_getFontFamilyGenericMapping() );
215
2.35k
    }
216
217
2.35k
    rStrExpValue = aOut.makeStringAndClear();
218
219
2.35k
    return bRet;
220
2.35k
}
221
222
223
XMLFontEncodingPropHdl::~XMLFontEncodingPropHdl()
224
101k
{
225
    // Nothing to do
226
101k
}
227
228
bool XMLFontEncodingPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
229
2.68k
{
230
2.68k
    if( IsXMLToken( rStrImpValue, XML_X_SYMBOL ) )
231
2.64k
        rValue <<= sal_Int16(RTL_TEXTENCODING_SYMBOL);
232
233
2.68k
    return true;
234
2.68k
}
235
236
bool XMLFontEncodingPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
237
2.35k
{
238
2.35k
    bool bRet = false;
239
2.35k
    sal_Int16 nSet = sal_Int16();
240
241
2.35k
    if( rValue >>= nSet )
242
2.35k
    {
243
2.35k
        if( static_cast<rtl_TextEncoding>(nSet) == RTL_TEXTENCODING_SYMBOL )
244
0
        {
245
0
            rStrExpValue = GetXMLToken(XML_X_SYMBOL);
246
0
            bRet = true;
247
0
        }
248
2.35k
    }
249
250
2.35k
    return bRet;
251
2.35k
}
252
253
254
XMLFontPitchPropHdl::~XMLFontPitchPropHdl()
255
101k
{
256
    // Nothing to do
257
101k
}
258
259
bool XMLFontPitchPropHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& ) const
260
25.1k
{
261
25.1k
    FontPitch eNewPitch;
262
25.1k
    bool bRet = SvXMLUnitConverter::convertEnum( eNewPitch, rStrImpValue, aFontPitchMapping );
263
25.1k
    if( bRet )
264
23.9k
        rValue <<= static_cast<sal_Int16>(eNewPitch);
265
266
25.1k
    return bRet;
267
25.1k
}
268
269
bool XMLFontPitchPropHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& ) const
270
2.35k
{
271
2.35k
    bool bRet = false;
272
2.35k
    sal_Int16 nPitch = sal_Int16();
273
274
2.35k
    FontPitch ePitch = PITCH_DONTKNOW;
275
2.35k
    if( rValue >>= nPitch )
276
2.35k
        ePitch =  static_cast<FontPitch>(nPitch);
277
278
2.35k
    if( PITCH_DONTKNOW != ePitch )
279
827
    {
280
827
        OUStringBuffer aOut;
281
827
        bRet = SvXMLUnitConverter::convertEnum( aOut, ePitch, aFontPitchMapping, XML_FIXED );
282
827
        rStrExpValue = aOut.makeStringAndClear();
283
827
    }
284
285
2.35k
    return bRet;
286
2.35k
}
287
288
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */