Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/oox/source/drawingml/textrun.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 <drawingml/textrun.hxx>
21
22
#include <com/sun/star/awt/CharSet.hpp>
23
#include <com/sun/star/frame/XModel.hpp>
24
#include <com/sun/star/text/ControlCharacter.hpp>
25
#include <com/sun/star/beans/XPropertySet.hpp>
26
#include <com/sun/star/beans/XPropertyState.hpp>
27
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
28
#include <com/sun/star/text/XTextField.hpp>
29
30
#include <sal/log.hxx>
31
32
#include <oox/helper/helper.hxx>
33
#include <oox/helper/propertyset.hxx>
34
#include <oox/core/xmlfilterbase.hxx>
35
#include <oox/token/properties.hxx>
36
#include <oox/token/tokens.hxx>
37
#include <comphelper/diagnose_ex.hxx>
38
39
using namespace ::com::sun::star::uno;
40
using namespace ::com::sun::star::text;
41
using namespace ::com::sun::star::beans;
42
using namespace ::com::sun::star::lang;
43
44
namespace oox::drawingml {
45
46
TextRun::TextRun() :
47
139k
    mbIsLineBreak( false )
48
139k
{
49
139k
}
50
51
TextRun::~TextRun()
52
139k
{
53
139k
}
54
55
sal_Int32 TextRun::insertAt(
56
        const ::oox::core::XmlFilterBase& rFilterBase,
57
        const Reference < XText > & xText,
58
        const Reference < XTextCursor > &xAt,
59
        const TextCharacterProperties& rTextCharacterStyle,
60
        float nDefaultCharHeight) const
61
49.3k
{
62
49.3k
    sal_Int32 nCharHeight = 0;
63
49.3k
    try {
64
49.3k
        Reference< XTextRange > xStart = xAt;
65
49.3k
        PropertySet aPropSet( xStart );
66
67
49.3k
        Reference<XPropertyState> xState(xStart, UNO_QUERY);
68
49.3k
        Any aOldFontName = xState->getPropertyDefault(u"CharFontName"_ustr);
69
49.3k
        Any aOldFontPitch = xState->getPropertyDefault(u"CharFontPitch"_ustr);
70
49.3k
        Any aOldFontFamily = xState->getPropertyDefault(u"CharFontFamily"_ustr);
71
49.3k
        Any aOldFontCharSet = xState->getPropertyDefault(u"CharFontCharSet"_ustr);
72
73
49.3k
        TextCharacterProperties aTextCharacterProps( rTextCharacterStyle );
74
75
        // If no text color specified let's anyway initialize it as default:
76
        // this will help to recover after hyperlink
77
49.3k
        if (!aTextCharacterProps.maFillProperties.maFillColor.isUsed())
78
30.9k
            aTextCharacterProps.maFillProperties.moFillType = XML_solidFill;
79
80
49.3k
        aTextCharacterProps.assignUsed(maTextCharacterProperties);
81
49.3k
        if ( aTextCharacterProps.moHeight.has_value() )
82
33.6k
            nCharHeight = aTextCharacterProps.moHeight.value();
83
15.6k
        else
84
            // UNO API has the character height as float, DML has it as int, but in hundreds.
85
15.6k
            aTextCharacterProps.moHeight = static_cast<sal_Int32>(nDefaultCharHeight * 100);
86
49.3k
        aTextCharacterProps.pushToPropSet( aPropSet, rFilterBase );
87
88
49.3k
        if( maTextCharacterProperties.maHyperlinkPropertyMap.empty() )
89
49.1k
        {
90
49.1k
            if( mbIsLineBreak )
91
73
            {
92
73
                SAL_WARN("oox",  "OOX: TextRun::insertAt() insert line break" );
93
73
                xText->insertControlCharacter( xStart, ControlCharacter::LINE_BREAK, false );
94
73
            }
95
49.1k
            else if (!getText().isEmpty())
96
48.9k
            {
97
48.9k
                sal_Int32 nIndex = 0;
98
48.9k
                sal_Int32 nMax = getText().getLength();
99
48.9k
                while(true)
100
48.9k
                {
101
48.9k
                    bool bSymbol = (getText()[nIndex] & 0xff00) == 0xf000;
102
48.9k
                    sal_Int32 nCount = 1;
103
847k
                    while(nIndex + nCount < nMax
104
798k
                            && ((getText()[nIndex + nCount] & 0xff00) == 0xf000) == bSymbol)
105
798k
                        ++nCount;
106
107
48.9k
                    OUString aFontName;
108
48.9k
                    sal_Int16 nFontFamily = 0, nFontPitch = 0;
109
48.9k
                    bool bSymbolEnc(false);
110
48.9k
                    bool bReset = false;
111
112
                    // Direct formatting for symbols.
113
48.9k
                    if (bSymbol && aTextCharacterProps.maSymbolFont.getFontData(aFontName, nFontPitch, nFontFamily, &bSymbolEnc, rFilterBase))
114
115
31
                    {
116
31
                        aPropSet.setAnyProperty(PROP_CharFontName, Any(aFontName));
117
31
                        aPropSet.setAnyProperty(PROP_CharFontPitch, Any(nFontPitch));
118
31
                        aPropSet.setAnyProperty(PROP_CharFontFamily, Any(nFontFamily));
119
31
                        aPropSet.setAnyProperty(PROP_CharFontCharSet, Any(bSymbolEnc ? css::awt::CharSet::SYMBOL : css::awt::CharSet::DONTKNOW));
120
31
                        bReset = true;
121
31
                    }
122
123
48.9k
                    OUString aSubString(getText().copy(nIndex, nCount));
124
48.9k
                    xText->insertString(xStart, aSubString, false);
125
126
48.9k
                    aPropSet = PropertySet(xStart);
127
                    // Reset to whatever it was.
128
48.9k
                    if (bReset)
129
31
                    {
130
31
                        aPropSet.setAnyProperty(PROP_CharFontName, aOldFontName);
131
31
                        aPropSet.setAnyProperty(PROP_CharFontPitch, aOldFontPitch);
132
31
                        aPropSet.setAnyProperty(PROP_CharFontFamily, aOldFontFamily);
133
31
                        aPropSet.setAnyProperty(PROP_CharFontCharSet, aOldFontCharSet);
134
31
                    }
135
136
48.9k
                    nIndex += nCount;
137
138
48.9k
                    if (nIndex >= nMax)
139
48.9k
                        break;
140
141
0
                    aTextCharacterProps.pushToPropSet(aPropSet, rFilterBase);
142
0
                }
143
48.9k
            }
144
49.1k
        }
145
188
        else
146
188
        {
147
188
            SAL_WARN("oox",  "OOX: URL field" );
148
188
            Reference< XMultiServiceFactory > xFactory( rFilterBase.getModel(), UNO_QUERY );
149
188
            Reference< XTextField > xField( xFactory->createInstance( u"com.sun.star.text.TextField.URL"_ustr ), UNO_QUERY );
150
188
            if( xField.is() )
151
188
            {
152
188
                Reference< XTextCursor > xTextFieldCursor = xText->createTextCursor();
153
188
                xTextFieldCursor->gotoEnd( false );
154
155
188
                PropertySet aFieldProps( xField );
156
188
                aFieldProps.setProperties( maTextCharacterProperties.maHyperlinkPropertyMap );
157
188
                aFieldProps.setProperty( PROP_Representation, getText() );
158
188
                xText->insertTextContent( xStart, xField, false );
159
160
188
                xTextFieldCursor->gotoEnd( true );
161
162
188
                if (!maTextCharacterProperties.maHyperlinkPropertyMap.hasProperty(PROP_CharColor))
163
188
                    aTextCharacterProps.maFillProperties.maFillColor.setSchemeClr(XML_hlink);
164
165
188
                aTextCharacterProps.maFillProperties.moFillType = XML_solidFill;
166
188
                if ( !maTextCharacterProperties.moUnderline.has_value() )
167
188
                    aTextCharacterProps.moUnderline = XML_sng;
168
169
188
                PropertySet aFieldTextPropSet( xTextFieldCursor );
170
188
                aTextCharacterProps.pushToPropSet( aFieldTextPropSet, rFilterBase );
171
188
            }
172
0
            else
173
0
            {
174
0
                SAL_WARN("oox",  "OOX: URL field couldn't be created" );
175
0
                xText->insertString( xStart, getText(), false );
176
0
            }
177
188
        }
178
49.3k
    }
179
49.3k
    catch( const Exception&  )
180
49.3k
    {
181
0
        TOOLS_WARN_EXCEPTION("oox", "OOX: TextRun::insertAt()");
182
0
    }
183
184
49.3k
    return nCharHeight;
185
49.3k
}
186
187
}
188
189
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */