1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
47 namespace oox::drawingml
{
49 // CT_TextBodyProperties
50 TextBodyPropertiesContext::TextBodyPropertiesContext(ContextHandler2Helper
const& rParent
,
51 const AttributeList
& rAttribs
,
52 const ShapePtr
& pShapePtr
)
53 : TextBodyPropertiesContext(rParent
, rAttribs
, pShapePtr
->getTextBody()->getTextProperties())
55 mpShapePtr
= pShapePtr
;
58 TextBodyPropertiesContext::TextBodyPropertiesContext( ContextHandler2Helper
const & rParent
,
59 const AttributeList
& rAttribs
, TextBodyProperties
& rTextBodyProp
)
60 : ContextHandler2( rParent
)
61 , mrTextBodyProp( rTextBodyProp
)
63 // ST_TextWrappingType
64 sal_Int32 nWrappingType
= rAttribs
.getToken( XML_wrap
, XML_square
);
65 mrTextBodyProp
.maPropertyMap
.setProperty( PROP_TextWordWrap
, nWrappingType
== XML_square
);
69 sal_Int32 aIns
[] = { XML_lIns
, XML_tIns
, XML_rIns
, XML_bIns
};
70 for( sal_Int32 i
= 0; i
< sal_Int32(std::size( aIns
)); i
++)
72 sValue
= rAttribs
.getStringDefaulted( aIns
[i
] );
73 if( !sValue
.isEmpty() )
74 mrTextBodyProp
.moInsets
[i
] = GetCoordinate( sValue
);
77 // bool bCompatLineSpacing = rAttribs.getBool( XML_compatLnSpc, false );
78 // bool bForceAA = rAttribs.getBool( XML_forceAA, false );
79 bool bFromWordArt
= rAttribs
.getBool(XML_fromWordArt
, false);
80 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_FromWordArt
, bFromWordArt
);
82 // ST_TextHorzOverflowType
83 mrTextBodyProp
.msHorzOverflow
= rAttribs
.getStringDefaulted(XML_horzOverflow
);
84 // ST_TextVertOverflowType
85 if( rAttribs
.hasAttribute(XML_vertOverflow
) )
87 mrTextBodyProp
.moVertOverflow
= rAttribs
.getToken(XML_vertOverflow
);
88 switch( mrTextBodyProp
.moVertOverflow
.value_or(XML_overflow
) )
92 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextClipVerticalOverflow
, true);
100 if (const sal_Int32 nColumns
= rAttribs
.getInteger(XML_numCol
, 0); nColumns
> 0)
102 css::uno::Reference
<css::text::XTextColumns
> xCols(SvxXTextColumns_createInstance(),
103 css::uno::UNO_QUERY_THROW
);
104 xCols
->setColumnCount(nColumns
);
105 css::uno::Reference
<css::beans::XPropertySet
> xProps(xCols
, css::uno::UNO_QUERY_THROW
);
106 // ST_PositiveCoordinate32
107 const sal_Int32 nSpacing
= o3tl::convert(rAttribs
.getInteger(XML_spcCol
, 0),
108 o3tl::Length::emu
, o3tl::Length::mm100
);
109 xProps
->setPropertyValue(u
"AutomaticDistance"_ustr
, css::uno::Any(nSpacing
));
110 mrTextBodyProp
.maPropertyMap
.setAnyProperty(PROP_TextColumns
, css::uno::Any(xCols
));
114 if (rAttribs
.getInteger(XML_rot
).has_value())
115 mrTextBodyProp
.moTextAreaRotation
= rAttribs
.getInteger(XML_rot
).value();
117 // bool bRtlCol = rAttribs.getBool( XML_rtlCol, false );
118 // ST_PositiveCoordinate
119 // sal_Int32 nSpcCol = rAttribs.getInteger( XML_spcCol, 0 );
120 // bool bSpcFirstLastPara = rAttribs.getBool( XML_spcFirstLastPara, 0 );
122 bool bUpright
= rAttribs
.getBool(XML_upright
, false);
124 mrTextBodyProp
.moUpright
= true;
126 // ST_TextVerticalType
127 if( rAttribs
.hasAttribute( XML_vert
) ) {
128 mrTextBodyProp
.moVert
= rAttribs
.getToken( XML_vert
);
129 sal_Int32 tVert
= mrTextBodyProp
.moVert
.value_or( XML_horz
);
130 if (tVert
== XML_eaVert
)
132 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextWritingMode
, WritingMode_TB_RL
);
133 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_WritingMode
, text::WritingMode2::TB_RL
);
135 else if (tVert
== XML_vert
)
137 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_WritingMode
, text::WritingMode2::TB_RL90
);
139 else if (tVert
== XML_mongolianVert
)
141 // rendering not yet implemented for shape text, only for frames
142 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_WritingMode
, text::WritingMode2::TB_LR
);
144 else if (tVert
== XML_vert270
)
146 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_WritingMode
, text::WritingMode2::BT_LR
);
148 else if (tVert
== XML_wordArtVert
) // what about XML_wordArtVertRtl ?
150 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_WritingMode
, text::WritingMode2::STACKED
);
154 bool bRtl
= rAttribs
.getBool( XML_rtl
, false );
155 mrTextBodyProp
.maPropertyMap
.setProperty( PROP_TextWritingMode
,
156 ( bRtl
? WritingMode_RL_TB
: WritingMode_LR_TB
));
157 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_WritingMode
,
158 ( bRtl
? text::WritingMode2::RL_TB
: text::WritingMode2::LR_TB
));
162 // ST_TextAnchoringType
163 mrTextBodyProp
.mbAnchorCtr
= rAttribs
.getBool(XML_anchorCtr
, false );
164 if (rAttribs
.hasAttribute(XML_anchor
))
165 mrTextBodyProp
.meVA
= GetTextVerticalAdjust( rAttribs
.getToken(XML_anchor
, XML_t
));
166 // else meVA is initialized to TextVerticalAdjust_TOP
168 sal_Int32 tVert
= mrTextBodyProp
.moVert
.value_or(XML_horz
);
169 if (tVert
== XML_eaVert
|| tVert
== XML_mongolianVert
)
171 if (mrTextBodyProp
.mbAnchorCtr
)
172 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextVerticalAdjust
,
173 TextVerticalAdjust_CENTER
);
175 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextVerticalAdjust
,
176 TextVerticalAdjust_TOP
);
178 if (mrTextBodyProp
.meVA
== TextVerticalAdjust_CENTER
)
179 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextHorizontalAdjust
,
180 TextHorizontalAdjust_CENTER
);
181 else if (mrTextBodyProp
.meVA
== TextVerticalAdjust_TOP
)
183 mrTextBodyProp
.maPropertyMap
.setProperty(
184 PROP_TextHorizontalAdjust
,
185 tVert
== XML_eaVert
? TextHorizontalAdjust_RIGHT
: TextHorizontalAdjust_LEFT
);
187 else // meVA == TextVerticalAdjust_BOTTOM
189 mrTextBodyProp
.maPropertyMap
.setProperty(
190 PROP_TextHorizontalAdjust
,
191 tVert
== XML_eaVert
? TextHorizontalAdjust_LEFT
: TextHorizontalAdjust_RIGHT
);
196 if (mrTextBodyProp
.mbAnchorCtr
)
197 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextHorizontalAdjust
,
198 TextHorizontalAdjust_CENTER
);
199 else // BLOCK is nearer to rendering in MS Office than LEFT, see tdf#137023
200 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextHorizontalAdjust
,
201 TextHorizontalAdjust_BLOCK
);
202 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextVerticalAdjust
, mrTextBodyProp
.meVA
);
206 mrTextBodyProp
.maPropertyMap
.setProperty( PROP_TextAutoGrowHeight
, false);
207 mrTextBodyProp
.maPropertyMap
.setProperty( PROP_TextFitToSize
, drawing::TextFitToSizeType_NONE
);
210 ContextHandlerRef
TextBodyPropertiesContext::onCreateContext( sal_Int32 aElementToken
, const AttributeList
& rAttribs
)
212 switch( aElementToken
)
215 case A_TOKEN( prstTxWarp
): // CT_PresetTextShape
218 const std::optional
<OUString
> sPrst
= rAttribs
.getString( XML_prst
);
219 if( sPrst
.has_value() )
221 mrTextBodyProp
.msPrst
= sPrst
.value();
222 if( mrTextBodyProp
.msPrst
!= "textNoShape" )
223 return new PresetTextShapeContext( *this, rAttribs
,
224 *( mpShapePtr
->getCustomShapeProperties() ) );
229 case A_TOKEN( prot
): // CT_TextProtectionProperty
233 case A_TOKEN( noAutofit
):
234 mrTextBodyProp
.maPropertyMap
.setProperty( PROP_TextAutoGrowHeight
, false); // CT_TextNoAutofit
236 case A_TOKEN( normAutofit
): // CT_TextNormalAutofit
238 mrTextBodyProp
.maPropertyMap
.setProperty( PROP_TextFitToSize
, TextFitToSizeType_AUTOFIT
);
239 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextAutoGrowHeight
, false);
240 mrTextBodyProp
.mnFontScale
= rAttribs
.getInteger(XML_fontScale
, 100000);
241 mrTextBodyProp
.mnSpacingScale
= rAttribs
.getInteger(XML_lnSpcReduction
, 100000);
242 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextFitToSizeFontScale
, mrTextBodyProp
.mnFontScale
/ 100000.0);
243 mrTextBodyProp
.maPropertyMap
.setProperty(PROP_TextFitToSizeSpacingScale
, 1.0 - mrTextBodyProp
.mnSpacingScale
/ 100000.0);
246 case A_TOKEN( spAutoFit
):
248 const sal_Int32 tVert
= mrTextBodyProp
.moVert
.value_or( XML_horz
);
249 if( tVert
!= XML_vert
&& tVert
!= XML_eaVert
&& tVert
!= XML_vert270
&& tVert
!= XML_mongolianVert
)
250 mrTextBodyProp
.maPropertyMap
.setProperty( PROP_TextAutoGrowHeight
, true);
254 case A_TOKEN( scene3d
): // CT_Scene3D
256 if(mpShapePtr
&& mpShapePtr
->getServiceName() == "com.sun.star.drawing.CustomShape")
257 return new SceneText3DPropertiesContext( *this, mpShapePtr
->getTextBody()->get3DProperties() );
263 case A_TOKEN( sp3d
): // CT_Shape3D
265 if (mpShapePtr
&& mpShapePtr
->getServiceName() == "com.sun.star.drawing.CustomShape")
267 if (rAttribs
.hasAttribute(XML_extrusionH
))
268 mpShapePtr
->getTextBody()->get3DProperties().mnExtrusionH
= rAttribs
.getInteger(XML_extrusionH
, 0);
269 if (rAttribs
.hasAttribute(XML_contourW
))
270 mpShapePtr
->getTextBody()->get3DProperties().mnContourW
= rAttribs
.getInteger(XML_contourW
, 0);
271 if (rAttribs
.hasAttribute(XML_z
))
272 mpShapePtr
->getTextBody()->get3DProperties().mnShapeZ
= rAttribs
.getInteger(XML_z
, 0);
273 if (rAttribs
.hasAttribute(XML_prstMaterial
))
274 mpShapePtr
->getTextBody()->get3DProperties().mnMaterial
= rAttribs
.getToken(XML_prstMaterial
, XML_none
);
275 return new SceneText3DPropertiesContext(*this, mpShapePtr
->getTextBody()->get3DProperties());
280 case A_TOKEN( flatTx
): // CT_FlatText
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */