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