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