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/drawing/XEnhancedCustomShapeDefaulter.hpp>
26 #include <com/sun/star/drawing/CircleKind.hpp>
27 #include <drawingml/textbodyproperties.hxx>
28 #include <drawingml/textbody.hxx>
29 #include <drawingml/customshapegeometry.hxx>
30 #include <drawingml/scene3dcontext.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/EnhancedCustomShapeGeometry.hxx>
38 #include <comphelper/propertysequence.hxx>
39 #include <comphelper/propertyvalue.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <comphelper/sequence.hxx>
42 
43 using namespace ::oox::core;
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::drawing;
46 using namespace ::com::sun::star::text;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::xml::sax;
49 
50 namespace oox::drawingml {
51 
52 // CT_TextBodyProperties
53 TextBodyPropertiesContext::TextBodyPropertiesContext(ContextHandler2Helper const& rParent,
54                                                      const AttributeList& rAttribs,
55                                                      const ShapePtr& pShapePtr)
56     : TextBodyPropertiesContext(rParent, rAttribs, pShapePtr->getTextBody()->getTextProperties())
57 {
58     mpShapePtr = pShapePtr;
59 }
60 
61 TextBodyPropertiesContext::TextBodyPropertiesContext( ContextHandler2Helper const & rParent,
62     const AttributeList& rAttribs, TextBodyProperties& rTextBodyProp )
63 : ContextHandler2( rParent )
64 , mrTextBodyProp( rTextBodyProp )
65 {
66     // ST_TextWrappingType
67     sal_Int32 nWrappingType = rAttribs.getToken( XML_wrap, XML_square );
68     mrTextBodyProp.maPropertyMap.setProperty( PROP_TextWordWrap, nWrappingType == XML_square );
69 
70     // ST_Coordinate
71     OUString sValue;
72     sal_Int32 aIns[] = { XML_lIns, XML_tIns, XML_rIns, XML_bIns };
73     for( sal_Int32 i = 0; i < sal_Int32(SAL_N_ELEMENTS( aIns )); i++)
74     {
75         sValue = rAttribs.getString( aIns[i] ).get();
76         if( !sValue.isEmpty() )
77             mrTextBodyProp.moInsets[i] = GetCoordinate( sValue );
78     }
79 
80     mrTextBodyProp.mbAnchorCtr = rAttribs.getBool( XML_anchorCtr, false );
81     if( mrTextBodyProp.mbAnchorCtr )
82         mrTextBodyProp.maPropertyMap.setProperty( PROP_TextHorizontalAdjust, TextHorizontalAdjust_CENTER );
83 
84 //   bool bCompatLineSpacing = rAttribs.getBool( XML_compatLnSpc, false );
85 //   bool bForceAA = rAttribs.getBool( XML_forceAA, false );
86     bool bFromWordArt = rAttribs.getBool(XML_fromWordArt, false);
87     mrTextBodyProp.maPropertyMap.setProperty(PROP_FromWordArt, bFromWordArt);
88 
89   // ST_TextHorzOverflowType
90 //   sal_Int32 nHorzOverflow = rAttribs.getToken( XML_horzOverflow, XML_overflow );
91     // ST_TextVertOverflowType
92 //   sal_Int32 nVertOverflow =  rAttribs.getToken( XML_vertOverflow, XML_overflow );
93 
94     // ST_TextColumnCount
95     mrTextBodyProp.mnNumCol = rAttribs.getInteger( XML_numCol, 1 );
96 
97     // ST_Angle
98     mrTextBodyProp.moRotation = rAttribs.getInteger( XML_rot );
99 
100 //   bool bRtlCol = rAttribs.getBool( XML_rtlCol, false );
101     // ST_PositiveCoordinate
102 //   sal_Int32 nSpcCol = rAttribs.getInteger( XML_spcCol, 0 );
103 //   bool bSpcFirstLastPara = rAttribs.getBool( XML_spcFirstLastPara, 0 );
104 //   bool bUpRight = rAttribs.getBool( XML_upright, 0 );
105 
106     // ST_TextVerticalType
107     if( rAttribs.hasAttribute( XML_vert ) ) {
108         mrTextBodyProp.moVert = rAttribs.getToken( XML_vert );
109         sal_Int32 tVert = mrTextBodyProp.moVert.get( XML_horz );
110         if (tVert == XML_vert || tVert == XML_eaVert || tVert == XML_mongolianVert)
111             mrTextBodyProp.moRotation = 5400000;
112         else if (tVert == XML_vert270)
113             mrTextBodyProp.moRotation = 5400000 * 3;
114         else {
115             bool bRtl = rAttribs.getBool( XML_rtl, false );
116             mrTextBodyProp.maPropertyMap.setProperty( PROP_TextWritingMode,
117                 ( bRtl ? WritingMode_RL_TB : WritingMode_LR_TB ));
118         }
119     }
120 
121     // ST_TextAnchoringType
122     if( rAttribs.hasAttribute( XML_anchor ) )
123     {
124         mrTextBodyProp.meVA = GetTextVerticalAdjust( rAttribs.getToken( XML_anchor, XML_t ) );
125         mrTextBodyProp.maPropertyMap.setProperty( PROP_TextVerticalAdjust, mrTextBodyProp.meVA);
126     }
127 
128     // Push defaults
129     mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);
130     mrTextBodyProp.maPropertyMap.setProperty( PROP_TextFitToSize, drawing::TextFitToSizeType_NONE);
131 }
132 
133 ContextHandlerRef TextBodyPropertiesContext::onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs )
134 {
135     switch( aElementToken )
136     {
137             // Sequence
138             case A_TOKEN( prstTxWarp ):     // CT_PresetTextShape
139                 if( mpShapePtr )
140                 {
141                     const OptValue<OUString> sPrst = rAttribs.getString( XML_prst );
142                     if( sPrst.has() )
143                     {
144                         mrTextBodyProp.msPrst = sPrst.get();
145                         if( mrTextBodyProp.msPrst != "textNoShape" )
146                             return new PresetTextShapeContext( *this, rAttribs,
147                                                            *( mpShapePtr->getCustomShapeProperties() ) );
148                     }
149                 }
150                 break;
151 
152             case A_TOKEN( prot ):           // CT_TextProtectionProperty
153                 break;
154 
155             // EG_TextAutofit
156             case A_TOKEN( noAutofit ):
157                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);   // CT_TextNoAutofit
158                 break;
159             case A_TOKEN( normAutofit ):    // CT_TextNormalAutofit
160             {
161                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextFitToSize, TextFitToSizeType_AUTOFIT);
162                 mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, false);
163                 mrTextBodyProp.mnFontScale = rAttribs.getInteger(XML_fontScale, 100000);
164                 break;
165             }
166             case A_TOKEN( spAutoFit ):
167                 {
168                     const sal_Int32 tVert = mrTextBodyProp.moVert.get( XML_horz );
169                     if( tVert != XML_vert && tVert != XML_eaVert && tVert != XML_vert270 && tVert != XML_mongolianVert )
170                         mrTextBodyProp.maPropertyMap.setProperty( PROP_TextAutoGrowHeight, true);
171                 }
172                 break;
173 
174             case A_TOKEN( scene3d ):        // CT_Scene3D
175             {
176                 if(mpShapePtr && mpShapePtr->getServiceName() == "com.sun.star.drawing.CustomShape")
177                     return new Scene3DPropertiesContext( *this, mpShapePtr->get3DProperties() );
178 
179                 break;
180             }
181 
182             // EG_Text3D
183             case A_TOKEN( sp3d ):           // CT_Shape3D
184             case A_TOKEN( flatTx ):         // CT_FlatText
185 
186                 break;
187     }
188 
189     return nullptr;
190 }
191 
192 }
193 
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
195