tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / oox / source / drawingml / textrun.cxx
blob1160e36e4cf41fa3089963d6bbb5fd1271877ac7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <drawingml/textrun.hxx>
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>
30 #include <sal/log.hxx>
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>
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;
44 namespace oox::drawingml {
46 TextRun::TextRun() :
47 mbIsLineBreak( false )
51 TextRun::~TextRun()
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
62 sal_Int32 nCharHeight = 0;
63 try {
64 Reference< XTextRange > xStart = xAt;
65 PropertySet aPropSet( xStart );
67 Reference<XPropertyState> xState(xStart, UNO_QUERY);
68 Any aOldFontName = xState->getPropertyDefault(u"CharFontName"_ustr);
69 Any aOldFontPitch = xState->getPropertyDefault(u"CharFontPitch"_ustr);
70 Any aOldFontFamily = xState->getPropertyDefault(u"CharFontFamily"_ustr);
71 Any aOldFontCharSet = xState->getPropertyDefault(u"CharFontCharSet"_ustr);
73 TextCharacterProperties aTextCharacterProps( rTextCharacterStyle );
75 // If no text color specified let's anyway initialize it as default:
76 // this will help to recover after hyperlink
77 if (!aTextCharacterProps.maFillProperties.maFillColor.isUsed())
78 aTextCharacterProps.maFillProperties.moFillType = XML_solidFill;
80 aTextCharacterProps.assignUsed(maTextCharacterProperties);
81 if ( aTextCharacterProps.moHeight.has_value() )
82 nCharHeight = aTextCharacterProps.moHeight.value();
83 else
84 // UNO API has the character height as float, DML has it as int, but in hundreds.
85 aTextCharacterProps.moHeight = static_cast<sal_Int32>(nDefaultCharHeight * 100);
86 aTextCharacterProps.pushToPropSet( aPropSet, rFilterBase );
88 if( maTextCharacterProperties.maHyperlinkPropertyMap.empty() )
90 if( mbIsLineBreak )
92 SAL_WARN("oox", "OOX: TextRun::insertAt() insert line break" );
93 xText->insertControlCharacter( xStart, ControlCharacter::LINE_BREAK, false );
95 else if (!getText().isEmpty())
97 sal_Int32 nIndex = 0;
98 sal_Int32 nMax = getText().getLength();
99 while(true)
101 bool bSymbol = (getText()[nIndex] & 0xff00) == 0xf000;
102 sal_Int32 nCount = 1;
103 while(nIndex + nCount < nMax
104 && ((getText()[nIndex + nCount] & 0xff00) == 0xf000) == bSymbol)
105 ++nCount;
107 OUString aFontName;
108 sal_Int16 nFontFamily = 0, nFontPitch = 0;
109 bool bSymbolEnc(false);
110 bool bReset = false;
112 // Direct formatting for symbols.
113 if (bSymbol && aTextCharacterProps.maSymbolFont.getFontData(aFontName, nFontPitch, nFontFamily, &bSymbolEnc, rFilterBase))
116 aPropSet.setAnyProperty(PROP_CharFontName, Any(aFontName));
117 aPropSet.setAnyProperty(PROP_CharFontPitch, Any(nFontPitch));
118 aPropSet.setAnyProperty(PROP_CharFontFamily, Any(nFontFamily));
119 aPropSet.setAnyProperty(PROP_CharFontCharSet, Any(bSymbolEnc ? css::awt::CharSet::SYMBOL : css::awt::CharSet::DONTKNOW));
120 bReset = true;
123 OUString aSubString(getText().copy(nIndex, nCount));
124 xText->insertString(xStart, aSubString, false);
126 aPropSet = PropertySet(xStart);
127 // Reset to whatever it was.
128 if (bReset)
130 aPropSet.setAnyProperty(PROP_CharFontName, aOldFontName);
131 aPropSet.setAnyProperty(PROP_CharFontPitch, aOldFontPitch);
132 aPropSet.setAnyProperty(PROP_CharFontFamily, aOldFontFamily);
133 aPropSet.setAnyProperty(PROP_CharFontCharSet, aOldFontCharSet);
136 nIndex += nCount;
138 if (nIndex >= nMax)
139 break;
141 aTextCharacterProps.pushToPropSet(aPropSet, rFilterBase);
145 else
147 SAL_WARN("oox", "OOX: URL field" );
148 Reference< XMultiServiceFactory > xFactory( rFilterBase.getModel(), UNO_QUERY );
149 Reference< XTextField > xField( xFactory->createInstance( u"com.sun.star.text.TextField.URL"_ustr ), UNO_QUERY );
150 if( xField.is() )
152 Reference< XTextCursor > xTextFieldCursor = xText->createTextCursor();
153 xTextFieldCursor->gotoEnd( false );
155 PropertySet aFieldProps( xField );
156 aFieldProps.setProperties( maTextCharacterProperties.maHyperlinkPropertyMap );
157 aFieldProps.setProperty( PROP_Representation, getText() );
158 xText->insertTextContent( xStart, xField, false );
160 xTextFieldCursor->gotoEnd( true );
162 if (!maTextCharacterProperties.maHyperlinkPropertyMap.hasProperty(PROP_CharColor))
163 aTextCharacterProps.maFillProperties.maFillColor.setSchemeClr(XML_hlink);
165 aTextCharacterProps.maFillProperties.moFillType = XML_solidFill;
166 if ( !maTextCharacterProperties.moUnderline.has_value() )
167 aTextCharacterProps.moUnderline = XML_sng;
169 PropertySet aFieldTextPropSet( xTextFieldCursor );
170 aTextCharacterProps.pushToPropSet( aFieldTextPropSet, rFilterBase );
172 else
174 SAL_WARN("oox", "OOX: URL field couldn't be created" );
175 xText->insertString( xStart, getText(), false );
179 catch( const Exception& )
181 TOOLS_WARN_EXCEPTION("oox", "OOX: TextRun::insertAt()");
184 return nCharHeight;
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */