Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / oox / source / drawingml / textrun.cxx
blob84e74b2524383deac8b344207d9649775f8cd12b
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/text/ControlCharacter.hpp>
23 #include <com/sun/star/beans/XMultiPropertySet.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/text/XTextField.hpp>
29 #include "oox/helper/helper.hxx"
30 #include "oox/helper/propertyset.hxx"
31 #include "oox/core/xmlfilterbase.hxx"
32 #include <oox/token/properties.hxx>
33 #include "oox/token/tokens.hxx"
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::text;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::frame;
39 using namespace ::com::sun::star::lang;
41 namespace oox { namespace drawingml {
43 TextRun::TextRun() :
44 mbIsLineBreak( false )
48 TextRun::~TextRun()
52 sal_Int32 TextRun::insertAt(
53 const ::oox::core::XmlFilterBase& rFilterBase,
54 const Reference < XText > & xText,
55 const Reference < XTextCursor > &xAt,
56 const TextCharacterProperties& rTextCharacterStyle,
57 float nDefaultCharHeight) const
59 sal_Int32 nCharHeight = 0;
60 try {
61 Reference< XTextRange > xStart( xAt, UNO_QUERY );
62 PropertySet aPropSet( xStart );
64 TextCharacterProperties aTextCharacterProps( rTextCharacterStyle );
65 aTextCharacterProps.assignUsed( maTextCharacterProperties );
66 if ( aTextCharacterProps.moHeight.has() )
67 nCharHeight = aTextCharacterProps.moHeight.get();
68 else
69 // UNO API has the character height as float, DML has it as int, but in hundreds.
70 aTextCharacterProps.moHeight = static_cast<sal_Int32>(nDefaultCharHeight * 100);
71 aTextCharacterProps.pushToPropSet( aPropSet, rFilterBase );
73 if( maTextCharacterProperties.maHyperlinkPropertyMap.empty() )
75 if( mbIsLineBreak )
77 SAL_WARN("oox", "OOX: TextRun::insertAt() insert line break" );
78 xText->insertControlCharacter( xStart, ControlCharacter::LINE_BREAK, false );
80 else
82 OUString aSymbolFontName;
83 sal_Int16 nSymbolFontFamily = 0, nSymbolFontPitch = 0;
85 if ( !aTextCharacterProps.maSymbolFont.getFontData( aSymbolFontName, nSymbolFontPitch, nSymbolFontFamily, rFilterBase ) )
86 xText->insertString( xStart, getText(), false );
87 else if ( !getText().isEmpty() )
89 // #i113673
90 OUString aLatinFontName;
91 sal_Int16 nLatinFontPitch = 0, nLatinFontFamily = 0;
92 bool bLatinOk = aTextCharacterProps.maLatinFont.getFontData( aLatinFontName, nLatinFontPitch, nLatinFontFamily, rFilterBase );
94 sal_Int32 nIndex = 0;
95 while ( true )
97 sal_Int32 nCount = 0;
98 bool bSymbol = ( getText()[ nIndex ] & 0xff00 ) == 0xf000;
99 if ( bSymbol )
103 nCount++;
105 while( ( ( nCount + nIndex ) < getText().getLength() ) && ( ( getText()[ nCount + nIndex ] & 0xff00 ) == 0xf000 ) );
106 aPropSet.setAnyProperty( PROP_CharFontName, Any( aSymbolFontName ) );
107 aPropSet.setAnyProperty( PROP_CharFontPitch, Any( nSymbolFontPitch ) );
108 aPropSet.setAnyProperty( PROP_CharFontFamily, Any( nSymbolFontFamily ) );
110 else
114 nCount++;
116 while( ( ( nCount + nIndex ) < getText().getLength() ) && ( ( getText()[ nCount + nIndex ] & 0xff00 ) != 0xf000 ) );
117 if (bLatinOk)
119 aPropSet.setAnyProperty( PROP_CharFontName, Any( aLatinFontName ) );
120 aPropSet.setAnyProperty( PROP_CharFontPitch, Any( nLatinFontPitch ) );
121 aPropSet.setAnyProperty( PROP_CharFontFamily, Any( nLatinFontFamily ) );
124 OUString aSubString( getText().copy( nIndex, nCount ) );
125 xText->insertString( xStart, aSubString, false );
126 nIndex += nCount;
128 if ( nIndex >= getText().getLength() )
129 break;
131 xStart = xAt;
132 aPropSet = PropertySet( xStart );
133 aTextCharacterProps.pushToPropSet( aPropSet, rFilterBase );
138 else
140 SAL_WARN("oox", "OOX: URL field" );
141 Reference< XMultiServiceFactory > xFactory( rFilterBase.getModel(), UNO_QUERY );
142 Reference< XTextField > xField( xFactory->createInstance( "com.sun.star.text.TextField.URL" ), UNO_QUERY );
143 if( xField.is() )
145 Reference< XTextCursor > xTextFieldCursor = xText->createTextCursor();
146 xTextFieldCursor->gotoEnd( false );
148 PropertySet aFieldProps( xField );
149 aFieldProps.setProperties( maTextCharacterProperties.maHyperlinkPropertyMap );
150 aFieldProps.setProperty( PROP_Representation, getText() );
151 xText->insertTextContent( xStart, xField, false );
153 xTextFieldCursor->gotoEnd( true );
155 aTextCharacterProps.maFillProperties.maFillColor.setSchemeClr( XML_hlink );
156 aTextCharacterProps.maFillProperties.moFillType.set(XML_solidFill);
157 if ( !maTextCharacterProperties.moUnderline.has() )
158 aTextCharacterProps.moUnderline.set( XML_sng );
160 PropertySet aFieldTextPropSet( xTextFieldCursor );
161 aTextCharacterProps.pushToPropSet( aFieldTextPropSet, rFilterBase );
163 oox::core::TextField aTextField;
164 aTextField.xText = xText;
165 aTextField.xTextCursor = xTextFieldCursor;
166 aTextField.xTextField = xField;
167 rFilterBase.getTextFieldStack().push_back( aTextField );
169 else
171 SAL_WARN("oox", "OOX: URL field couldn't be created" );
172 xText->insertString( xStart, getText(), false );
176 catch( const Exception& )
178 SAL_WARN("oox", "OOX: TextRun::insertAt() exception");
181 return nCharHeight;
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */