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/textbodypropertiescontext.hxx>
21 
22 #include <com/sun/star/text/WritingMode.hpp>
23 #include <com/sun/star/drawing/TextFitToSizeType.hpp>
24 #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
25 #include <com/sun/star/text/XTextColumns.hpp>
26 #include <drawingml/textbodyproperties.hxx>
27 #include <drawingml/textbody.hxx>
28 #include <drawingml/customshapegeometry.hxx>
29 #include <drawingml/scene3dcontext.hxx>
30 #include <o3tl/unit_conversion.hxx>
31 #include <oox/drawingml/drawingmltypes.hxx>
32 #include <oox/helper/attributelist.hxx>
33 #include <oox/helper/propertymap.hxx>
34 #include <oox/token/namespaces.hxx>
35 #include <oox/token/properties.hxx>
36 #include <oox/token/tokens.hxx>
37 #include <svx/SvxXTextColumns.hxx>
38 
39 using namespace ::oox::core;
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::drawing;
42 using namespace ::com::sun::star::text;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::xml::sax;
45 
46 namespace oox::drawingml {
47 
48 // CT_TextBodyProperties
TextBodyPropertiesContext(ContextHandler2Helper const & rParent,const AttributeList & rAttribs,const ShapePtr & pShapePtr)49 TextBodyPropertiesContext::TextBodyPropertiesContext(ContextHandler2Helper const& rParent,
50                                                      const AttributeList& rAttribs,
51                                                      const ShapePtr& pShapePtr)
52     : TextBodyPropertiesContext(rParent, rAttribs, pShapePtr->getTextBody()->getTextProperties())
53 {
54     mpShapePtr = pShapePtr;
55 }
56 
TextBodyPropertiesContext(ContextHandler2Helper const & rParent,const AttributeList & rAttribs,TextBodyProperties & rTextBodyProp)57 TextBodyPropertiesContext::TextBodyPropertiesContext( ContextHandler2Helper const & rParent,
58     const AttributeList& rAttribs, TextBodyProperties& rTextBodyProp )
59 : ContextHandler2( rParent )
60 , mrTextBodyProp( rTextBodyProp )
61 {
62     // ST_TextWrappingType
63     sal_Int32 nWrappingType = rAttribs.getToken( XML_wrap, XML_square );
64     mrTextBodyProp.maPropertyMap.setProperty( PROP_TextWordWrap, nWrappingType == XML_square );
65 
66     // ST_Coordinate
67     OUString sValue;
68     sal_Int32 aIns[] = { XML_lIns, XML_tIns, XML_rIns, XML_bIns };
69     for( sal_Int32 i = 0; i < sal_Int32(SAL_N_ELEMENTS( aIns )); i++)
70     {
71         sValue = rAttribs.getString( aIns[i] ).get();
72         if( !sValue.isEmpty() )
73             mrTextBodyProp.moInsets[i] = GetCoordinate( sValue );
74     }
75 
76     mrTextBodyProp.mbAnchorCtr = rAttribs.getBool( XML_anchorCtr, false );
77     if( mrTextBodyProp.mbAnchorCtr )
78         mrTextBodyProp.maPropertyMap.setProperty( PROP_TextHorizontalAdjust, TextHorizontalAdjust_CENTER );
79 
80 //   bool bCompatLineSpacing = rAttribs.getBool( XML_compatLnSpc, false );
81 //   bool bForceAA = rAttribs.getBool( XML_forceAA, false );
82     bool bFromWordArt = rAttribs.getBool(XML_fromWordArt, false);
83     mrTextBodyProp.maPropertyMap.setProperty(PROP_FromWordArt, bFromWordArt);
84 
85   // ST_TextHorzOverflowType
86     mrTextBodyProp.msHorzOverflow = rAttribs.getString(XML_horzOverflow, "");
87     // ST_TextVertOverflowType
88     mrTextBodyProp.msVertOverflow = rAttribs.getString(XML_vertOverflow, "");
89 
90     // ST_TextColumnCount
91     if (const sal_Int32 nColumns = rAttribs.getInteger(XML_numCol, 0); nColumns > 0)
92     {
93         css::uno::Reference<css::text::XTextColumns> xCols(SvxXTextColumns_createInstance(),
94                                                            css::uno::UNO_QUERY_THROW);
95         xCols->setColumnCount(nColumns);
96         css::uno::Reference<css::beans::XPropertySet> xProps(xCols, css::uno::UNO_QUERY_THROW);
97         // ST_PositiveCoordinate32
98         const sal_Int32 nSpacing = o3tl::convert(rAttribs.getInteger(XML_spcCol, 0),
99                                                  o3tl::Length::emu, o3tl::Length::mm100);
100         xProps->setPropertyValue("AutomaticDistance", css::uno::Any(nSpacing));
101         mrTextBodyProp.maPropertyMap.setAnyProperty(PROP_TextColumns, css::uno::Any(xCols));
102     }
103 
104     // ST_Angle
105     mrTextBodyProp.moRotation = rAttribs.getInteger( XML_rot );
106 
107 //   bool bRtlCol = rAttribs.getBool( XML_rtlCol, false );
108     // ST_PositiveCoordinate
109 //   sal_Int32 nSpcCol = rAttribs.getInteger( XML_spcCol, 0 );
110 //   bool bSpcFirstLastPara = rAttribs.getBool( XML_spcFirstLastPara, 0 );
111 
112     bool bUpright = rAttribs.getBool(XML_upright, false);
113     if (bUpright)
114         mrTextBodyProp.moUpright = true;
115 
116     // ST_TextVerticalType
117     if( rAttribs.hasAttribute( XML_vert ) ) {
118         mrTextBodyProp.moVert = rAttribs.getToken( XML_vert );
119         sal_Int32 tVert = mrTextBodyProp.moVert.get( XML_horz );
120         if (tVert == XML_vert || tVert == XML_eaVert || tVert == XML_mongolianVert)
121             mrTextBodyProp.moRotation = 5400000;
122         else if (tVert == XML_vert270)
123             mrTextBodyProp.moRotation = 5400000 * 3;
124         else {
125             bool bRtl = rAttribs.getBool( XML_rtl, false );
126             mrTextBodyProp.maPropertyMap.setProperty( PROP_TextWritingMode,
127                 ( bRtl ? WritingMode_RL_TB : WritingMode_LR_TB ));
128         }
129     }
130 
131     // ST_TextAnchoringType
132     if( rAttribs.hasAttribute( XML_anchor ) )
133     {
134         mrTextBodyProp.meVA = GetTextVerticalAdjust( rAttribs.getToken( XML_anchor, XML_t ) );
135         mrTextBodyProp.maPropertyMap.setProperty( PROP_TextVerticalAdjust, mrTextBodyProp.meVA);
136     }
137 
138     // Push defaults
139     mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);
140     mrTextBodyProp.maPropertyMap.setProperty( PROP_TextFitToSize, drawing::TextFitToSizeType_NONE);
141 }
142 
onCreateContext(sal_Int32 aElementToken,const AttributeList & rAttribs)143 ContextHandlerRef TextBodyPropertiesContext::onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs )
144 {
145     switch( aElementToken )
146     {
147             // Sequence
148             case A_TOKEN( prstTxWarp ):     // CT_PresetTextShape
149                 if( mpShapePtr )
150                 {
151                     const OptValue<OUString> sPrst = rAttribs.getString( XML_prst );
152                     if( sPrst.has() )
153                     {
154                         mrTextBodyProp.msPrst = sPrst.get();
155                         if( mrTextBodyProp.msPrst != "textNoShape" )
156                             return new PresetTextShapeContext( *this, rAttribs,
157                                                            *( mpShapePtr->getCustomShapeProperties() ) );
158                     }
159                 }
160                 break;
161 
162             case A_TOKEN( prot ):           // CT_TextProtectionProperty
163                 break;
164 
165             // EG_TextAutofit
166             case A_TOKEN( noAutofit ):
167                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);   // CT_TextNoAutofit
168                 break;
169             case A_TOKEN( normAutofit ):    // CT_TextNormalAutofit
170             {
171                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextFitToSize, TextFitToSizeType_AUTOFIT);
172                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);
173                 mrTextBodyProp.mnFontScale = rAttribs.getInteger(XML_fontScale, 100000);
174                 break;
175             }
176             case A_TOKEN( spAutoFit ):
177                 {
178                     const sal_Int32 tVert = mrTextBodyProp.moVert.get( XML_horz );
179                     if( tVert != XML_vert && tVert != XML_eaVert && tVert != XML_vert270 && tVert != XML_mongolianVert )
180                         mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, true);
181                 }
182                 break;
183 
184             case A_TOKEN( scene3d ):        // CT_Scene3D
185             {
186                 if(mpShapePtr && mpShapePtr->getServiceName() == "com.sun.star.drawing.CustomShape")
187                     return new SceneText3DPropertiesContext( *this, mpShapePtr->getTextBody()->get3DProperties() );
188 
189                 break;
190             }
191 
192             // EG_Text3D
193             case A_TOKEN( sp3d ):           // CT_Shape3D
194             {
195                 if (mpShapePtr && mpShapePtr->getServiceName() == "com.sun.star.drawing.CustomShape")
196                 {
197                     if (rAttribs.hasAttribute(XML_extrusionH))
198                         mpShapePtr->getTextBody()->get3DProperties().mnExtrusionH = rAttribs.getInteger(XML_extrusionH, 0);
199                     if (rAttribs.hasAttribute(XML_contourW))
200                         mpShapePtr->getTextBody()->get3DProperties().mnContourW = rAttribs.getInteger(XML_contourW, 0);
201                     if (rAttribs.hasAttribute(XML_z))
202                         mpShapePtr->getTextBody()->get3DProperties().mnShapeZ = rAttribs.getInteger(XML_z, 0);
203                     if (rAttribs.hasAttribute(XML_prstMaterial))
204                         mpShapePtr->getTextBody()->get3DProperties().mnMaterial = rAttribs.getToken(XML_prstMaterial, XML_none);
205                     return new SceneText3DPropertiesContext(*this, mpShapePtr->getTextBody()->get3DProperties());
206                 }
207                 break;
208             }
209 
210             case A_TOKEN( flatTx ):         // CT_FlatText
211 
212                 break;
213     }
214 
215     return nullptr;
216 }
217 
218 }
219 
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
221