Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / oox / source / drawingml / textbodypropertiescontext.cxx
blob46576c069c8ef6bf650f71ab59250ea272c3722c
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/textbodypropertiescontext.hxx>
22 #include <com/sun/star/text/WritingMode.hpp>
23 #include <com/sun/star/text/WritingMode2.hpp>
24 #include <com/sun/star/drawing/TextFitToSizeType.hpp>
25 #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
26 #include <com/sun/star/text/XTextColumns.hpp>
28 #include <drawingml/textbodyproperties.hxx>
29 #include <drawingml/textbody.hxx>
30 #include <drawingml/customshapegeometry.hxx>
31 #include <drawingml/scene3dcontext.hxx>
32 #include <o3tl/unit_conversion.hxx>
33 #include <oox/drawingml/drawingmltypes.hxx>
34 #include <oox/helper/attributelist.hxx>
35 #include <oox/helper/propertymap.hxx>
36 #include <oox/token/namespaces.hxx>
37 #include <oox/token/properties.hxx>
38 #include <oox/token/tokens.hxx>
39 #include <svx/SvxXTextColumns.hxx>
41 using namespace ::oox::core;
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::drawing;
44 using namespace ::com::sun::star::text;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::xml::sax;
48 namespace oox::drawingml {
50 // CT_TextBodyProperties
51 TextBodyPropertiesContext::TextBodyPropertiesContext(ContextHandler2Helper const& rParent,
52 const AttributeList& rAttribs,
53 const ShapePtr& pShapePtr)
54 : TextBodyPropertiesContext(rParent, rAttribs, pShapePtr->getTextBody()->getTextProperties())
56 mpShapePtr = pShapePtr;
59 TextBodyPropertiesContext::TextBodyPropertiesContext( ContextHandler2Helper const & rParent,
60 const AttributeList& rAttribs, TextBodyProperties& rTextBodyProp )
61 : ContextHandler2( rParent )
62 , mrTextBodyProp( rTextBodyProp )
64 // ST_TextWrappingType
65 sal_Int32 nWrappingType = rAttribs.getToken( XML_wrap, XML_square );
66 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextWordWrap, nWrappingType == XML_square );
68 // ST_Coordinate
69 OUString sValue;
70 sal_Int32 aIns[] = { XML_lIns, XML_tIns, XML_rIns, XML_bIns };
71 for( sal_Int32 i = 0; i < sal_Int32(std::size( aIns )); i++)
73 sValue = rAttribs.getStringDefaulted( aIns[i] );
74 if( !sValue.isEmpty() )
75 mrTextBodyProp.moInsets[i] = GetCoordinate( sValue );
78 // bool bCompatLineSpacing = rAttribs.getBool( XML_compatLnSpc, false );
79 // bool bForceAA = rAttribs.getBool( XML_forceAA, false );
80 bool bFromWordArt = rAttribs.getBool(XML_fromWordArt, false);
81 mrTextBodyProp.maPropertyMap.setProperty(PROP_FromWordArt, bFromWordArt);
83 // ST_TextHorzOverflowType
84 mrTextBodyProp.msHorzOverflow = rAttribs.getStringDefaulted(XML_horzOverflow);
85 // ST_TextVertOverflowType
86 if( rAttribs.hasAttribute(XML_vertOverflow) )
88 mrTextBodyProp.moVertOverflow = rAttribs.getToken(XML_vertOverflow);
89 switch( mrTextBodyProp.moVertOverflow.value_or(XML_overflow) )
91 case XML_ellipsis:
92 case XML_clip:
93 mrTextBodyProp.maPropertyMap.setProperty(PROP_TextClipVerticalOverflow, true);
94 break;
95 default:
96 break;
100 // ST_TextColumnCount
101 if (const sal_Int32 nColumns = rAttribs.getInteger(XML_numCol, 0); nColumns > 0)
103 css::uno::Reference<css::text::XTextColumns> xCols(SvxXTextColumns_createInstance(),
104 css::uno::UNO_QUERY_THROW);
105 xCols->setColumnCount(nColumns);
106 css::uno::Reference<css::beans::XPropertySet> xProps(xCols, css::uno::UNO_QUERY_THROW);
107 // ST_PositiveCoordinate32
108 const sal_Int32 nSpacing = o3tl::convert(rAttribs.getInteger(XML_spcCol, 0),
109 o3tl::Length::emu, o3tl::Length::mm100);
110 xProps->setPropertyValue("AutomaticDistance", css::uno::Any(nSpacing));
111 mrTextBodyProp.maPropertyMap.setAnyProperty(PROP_TextColumns, css::uno::Any(xCols));
114 // ST_Angle
115 if (rAttribs.getInteger(XML_rot).has_value())
116 mrTextBodyProp.moTextAreaRotation = rAttribs.getInteger(XML_rot).value();
118 // bool bRtlCol = rAttribs.getBool( XML_rtlCol, false );
119 // ST_PositiveCoordinate
120 // sal_Int32 nSpcCol = rAttribs.getInteger( XML_spcCol, 0 );
121 // bool bSpcFirstLastPara = rAttribs.getBool( XML_spcFirstLastPara, 0 );
123 bool bUpright = rAttribs.getBool(XML_upright, false);
124 if (bUpright)
125 mrTextBodyProp.moUpright = true;
127 // ST_TextVerticalType
128 if( rAttribs.hasAttribute( XML_vert ) ) {
129 mrTextBodyProp.moVert = rAttribs.getToken( XML_vert );
130 sal_Int32 tVert = mrTextBodyProp.moVert.value_or( XML_horz );
131 if (tVert == XML_eaVert)
133 mrTextBodyProp.maPropertyMap.setProperty(PROP_TextWritingMode, WritingMode_TB_RL);
134 mrTextBodyProp.maPropertyMap.setProperty(PROP_WritingMode, text::WritingMode2::TB_RL);
136 else if (tVert == XML_vert)
138 mrTextBodyProp.maPropertyMap.setProperty(PROP_WritingMode, text::WritingMode2::TB_RL90);
140 else if (tVert == XML_mongolianVert)
142 // rendering not yet implemented for shape text, only for frames
143 mrTextBodyProp.maPropertyMap.setProperty(PROP_WritingMode, text::WritingMode2::TB_LR);
145 else if (tVert == XML_vert270)
147 mrTextBodyProp.maPropertyMap.setProperty(PROP_WritingMode, text::WritingMode2::BT_LR);
149 else {
150 bool bRtl = rAttribs.getBool( XML_rtl, false );
151 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextWritingMode,
152 ( bRtl ? WritingMode_RL_TB : WritingMode_LR_TB ));
153 mrTextBodyProp.maPropertyMap.setProperty(PROP_WritingMode,
154 ( bRtl ? text::WritingMode2::RL_TB : text::WritingMode2::LR_TB));
158 // ST_TextAnchoringType
159 mrTextBodyProp.mbAnchorCtr = rAttribs.getBool(XML_anchorCtr, false );
160 if (rAttribs.hasAttribute(XML_anchor))
161 mrTextBodyProp.meVA = GetTextVerticalAdjust( rAttribs.getToken(XML_anchor, XML_t));
162 // else meVA is initialized to TextVerticalAdjust_TOP
164 sal_Int32 tVert = mrTextBodyProp.moVert.value_or(XML_horz);
165 if (tVert == XML_eaVert || tVert == XML_mongolianVert)
167 if (mrTextBodyProp.mbAnchorCtr)
168 mrTextBodyProp.maPropertyMap.setProperty(PROP_TextVerticalAdjust,
169 TextVerticalAdjust_CENTER);
170 else
171 mrTextBodyProp.maPropertyMap.setProperty(PROP_TextVerticalAdjust,
172 TextVerticalAdjust_TOP);
174 if (mrTextBodyProp.meVA == TextVerticalAdjust_CENTER)
175 mrTextBodyProp.maPropertyMap.setProperty(PROP_TextHorizontalAdjust,
176 TextHorizontalAdjust_CENTER);
177 else if (mrTextBodyProp.meVA == TextVerticalAdjust_TOP)
179 mrTextBodyProp.maPropertyMap.setProperty(
180 PROP_TextHorizontalAdjust,
181 tVert == XML_eaVert ? TextHorizontalAdjust_RIGHT : TextHorizontalAdjust_LEFT);
183 else // meVA == TextVerticalAdjust_BOTTOM
185 mrTextBodyProp.maPropertyMap.setProperty(
186 PROP_TextHorizontalAdjust,
187 tVert == XML_eaVert ? TextHorizontalAdjust_LEFT : TextHorizontalAdjust_RIGHT);
190 else
192 if (mrTextBodyProp.mbAnchorCtr)
193 mrTextBodyProp.maPropertyMap.setProperty(PROP_TextHorizontalAdjust,
194 TextHorizontalAdjust_CENTER);
195 else // BLOCK is nearer to rendering in MS Office than LEFT, see tdf#137023
196 mrTextBodyProp.maPropertyMap.setProperty(PROP_TextHorizontalAdjust,
197 TextHorizontalAdjust_BLOCK);
198 mrTextBodyProp.maPropertyMap.setProperty(PROP_TextVerticalAdjust, mrTextBodyProp.meVA);
201 // Push defaults
202 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);
203 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextFitToSize, drawing::TextFitToSizeType_NONE);
206 ContextHandlerRef TextBodyPropertiesContext::onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs )
208 switch( aElementToken )
210 // Sequence
211 case A_TOKEN( prstTxWarp ): // CT_PresetTextShape
212 if( mpShapePtr )
214 const std::optional<OUString> sPrst = rAttribs.getString( XML_prst );
215 if( sPrst.has_value() )
217 mrTextBodyProp.msPrst = sPrst.value();
218 if( mrTextBodyProp.msPrst != "textNoShape" )
219 return new PresetTextShapeContext( *this, rAttribs,
220 *( mpShapePtr->getCustomShapeProperties() ) );
223 break;
225 case A_TOKEN( prot ): // CT_TextProtectionProperty
226 break;
228 // EG_TextAutofit
229 case A_TOKEN( noAutofit ):
230 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false); // CT_TextNoAutofit
231 break;
232 case A_TOKEN( normAutofit ): // CT_TextNormalAutofit
234 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextFitToSize, TextFitToSizeType_AUTOFIT);
235 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);
236 mrTextBodyProp.mnFontScale = rAttribs.getInteger(XML_fontScale, 100000);
237 break;
239 case A_TOKEN( spAutoFit ):
241 const sal_Int32 tVert = mrTextBodyProp.moVert.value_or( XML_horz );
242 if( tVert != XML_vert && tVert != XML_eaVert && tVert != XML_vert270 && tVert != XML_mongolianVert )
243 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, true);
245 break;
247 case A_TOKEN( scene3d ): // CT_Scene3D
249 if(mpShapePtr && mpShapePtr->getServiceName() == "com.sun.star.drawing.CustomShape")
250 return new SceneText3DPropertiesContext( *this, mpShapePtr->getTextBody()->get3DProperties() );
252 break;
255 // EG_Text3D
256 case A_TOKEN( sp3d ): // CT_Shape3D
258 if (mpShapePtr && mpShapePtr->getServiceName() == "com.sun.star.drawing.CustomShape")
260 if (rAttribs.hasAttribute(XML_extrusionH))
261 mpShapePtr->getTextBody()->get3DProperties().mnExtrusionH = rAttribs.getInteger(XML_extrusionH, 0);
262 if (rAttribs.hasAttribute(XML_contourW))
263 mpShapePtr->getTextBody()->get3DProperties().mnContourW = rAttribs.getInteger(XML_contourW, 0);
264 if (rAttribs.hasAttribute(XML_z))
265 mpShapePtr->getTextBody()->get3DProperties().mnShapeZ = rAttribs.getInteger(XML_z, 0);
266 if (rAttribs.hasAttribute(XML_prstMaterial))
267 mpShapePtr->getTextBody()->get3DProperties().mnMaterial = rAttribs.getToken(XML_prstMaterial, XML_none);
268 return new SceneText3DPropertiesContext(*this, mpShapePtr->getTextBody()->get3DProperties());
270 break;
273 case A_TOKEN( flatTx ): // CT_FlatText
275 break;
278 return nullptr;
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */