xref: /AOO41X/main/oox/source/drawingml/textparagraphpropertiescontext.cxx (revision bbdaa03c027b99dfd94777720229473c094bb8b9)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/drawingml/textparagraphpropertiescontext.hxx"
25 
26 #include <com/sun/star/text/WritingMode2.hpp>
27 #include <com/sun/star/awt/FontDescriptor.hpp>
28 
29 #include "oox/drawingml/colorchoicecontext.hxx"
30 #include "oox/drawingml/textcharacterpropertiescontext.hxx"
31 #include "oox/drawingml/fillproperties.hxx"
32 #include "oox/helper/attributelist.hxx"
33 #include "textspacingcontext.hxx"
34 #include "texttabstoplistcontext.hxx"
35 
36 using ::rtl::OUString;
37 using namespace ::oox::core;
38 using ::com::sun::star::awt::FontDescriptor;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::xml::sax;
41 using namespace ::com::sun::star::style;
42 using namespace ::com::sun::star::text;
43 
44 namespace oox { namespace drawingml {
45 
46 // CT_TextParagraphProperties
TextParagraphPropertiesContext(ContextHandler & rParent,const Reference<XFastAttributeList> & xAttribs,TextParagraphProperties & rTextParagraphProperties)47 TextParagraphPropertiesContext::TextParagraphPropertiesContext( ContextHandler& rParent,
48                                                                 const Reference< XFastAttributeList >& xAttribs,
49                                                                 TextParagraphProperties& rTextParagraphProperties )
50 : ContextHandler( rParent )
51 , mrTextParagraphProperties( rTextParagraphProperties )
52 , mrSpaceBefore( rTextParagraphProperties.getParaTopMargin() )
53 , mrSpaceAfter( rTextParagraphProperties.getParaBottomMargin() )
54 , mrBulletList( rTextParagraphProperties.getBulletList() )
55 {
56     OUString sValue;
57     AttributeList attribs( xAttribs );
58 
59     PropertyMap& rPropertyMap( mrTextParagraphProperties.getTextParagraphPropertyMap() );
60 
61     // ST_TextAlignType
62     if ( xAttribs->hasAttribute( XML_algn ) )
63     {
64         sal_Int32 nAlign = xAttribs->getOptionalValueToken( XML_algn, XML_l );
65         rPropertyMap[ PROP_ParaAdjust ] <<= GetParaAdjust( nAlign );
66     }
67 //  OSL_TRACE( "OOX: para adjust %d", GetParaAdjust( nAlign ));
68     // TODO see to do the same with RubyAdjust
69 
70     // ST_Coordinate32
71 //  sValue = xAttribs->getOptionalValue( XML_defTabSz );    SJ: we need to be able to set the default tab size for each text object,
72 //                                                          this is possible at the moment only for the whole document.
73 //  sal_Int32 nDefTabSize = ( sValue.getLength() == 0 ? 0 : GetCoordinate(  sValue ) );
74     // TODO
75 
76 //  bool bEaLineBrk = attribs.getBool( XML_eaLnBrk, true );
77     if ( xAttribs->hasAttribute( XML_latinLnBrk ) )
78     {
79         bool bLatinLineBrk = attribs.getBool( XML_latinLnBrk, true );
80         rPropertyMap[ PROP_ParaIsHyphenation ] <<= bLatinLineBrk;
81     }
82     // TODO see what to do with Asian hyphenation
83 
84     // ST_TextFontAlignType
85     // TODO
86 //  sal_Int32 nFontAlign = xAttribs->getOptionalValueToken( XML_fontAlgn, XML_base );
87 
88     if ( xAttribs->hasAttribute( XML_hangingPunct ) )
89     {
90         bool bHangingPunct = attribs.getBool( XML_hangingPunct, false );
91         rPropertyMap[ PROP_ParaIsHangingPunctuation ] <<= bHangingPunct;
92     }
93 
94   // ST_Coordinate
95     if ( xAttribs->hasAttribute( XML_indent ) )
96     {
97         sValue = xAttribs->getOptionalValue( XML_indent );
98         mrTextParagraphProperties.getFirstLineIndentation() = boost::optional< sal_Int32 >( sValue.getLength() == 0 ? 0 : GetCoordinate( sValue ) );
99     }
100 
101   // ST_TextIndentLevelType
102     // -1 is an invalid value and denote the lack of level
103     sal_Int32 nLevel = attribs.getInteger( XML_lvl, 0 );
104     if( nLevel > 8 || nLevel < 0 )
105     {
106         nLevel = 0;
107     }
108 
109     mrTextParagraphProperties.setLevel( static_cast< sal_Int16 >( nLevel ) );
110 
111     char name[] = "Outline X";
112     name[8] = static_cast<char>( '1' + nLevel );
113     const OUString sStyleNameValue( rtl::OUString::createFromAscii( name ) );
114     mrBulletList.setStyleName( sStyleNameValue );
115 
116     // ST_TextMargin
117     // ParaLeftMargin
118     if ( xAttribs->hasAttribute( XML_marL ) )
119     {
120         sValue = xAttribs->getOptionalValue( XML_marL );
121         mrTextParagraphProperties.getParaLeftMargin() = boost::optional< sal_Int32 >( sValue.getLength() == 0 ? 0 : GetCoordinate( sValue ) );
122     }
123 
124     // ParaRightMargin
125     if ( xAttribs->hasAttribute( XML_marR ) )
126     {
127         sValue = xAttribs->getOptionalValue( XML_marR );
128         sal_Int32 nMarR  = ( sValue.getLength() == 0 ? 0 : GetCoordinate( sValue ) );
129         rPropertyMap[ PROP_ParaRightMargin ] <<= nMarR;
130     }
131 
132     if ( xAttribs->hasAttribute( XML_rtl ) )
133     {
134         bool bRtl = attribs.getBool( XML_rtl, false );
135         rPropertyMap[ PROP_WritingMode ] <<= ( bRtl ? WritingMode2::RL_TB : WritingMode2::LR_TB );
136     }
137 }
138 
139 
140 
~TextParagraphPropertiesContext()141 TextParagraphPropertiesContext::~TextParagraphPropertiesContext()
142 {
143     PropertyMap& rPropertyMap( mrTextParagraphProperties.getTextParagraphPropertyMap() );
144     if ( maLineSpacing.bHasValue )
145         rPropertyMap[ PROP_ParaLineSpacing ] <<= maLineSpacing.toLineSpacing();
146 
147     ::std::list< TabStop >::size_type nTabCount = maTabList.size();
148     if( nTabCount != 0 )
149     {
150         Sequence< TabStop > aSeq( nTabCount );
151         TabStop * aArray = aSeq.getArray();
152         OSL_ENSURE( aArray != NULL, "sequence array is NULL" );
153         ::std::copy( maTabList.begin(), maTabList.end(), aArray );
154         rPropertyMap[ PROP_ParaTabStops ] <<= aSeq;
155     }
156 
157     if ( mxBlipProps.get() && mxBlipProps->mxGraphic.is() )
158         mrBulletList.setGraphic( mxBlipProps->mxGraphic );
159 
160     if( mrBulletList.is() )
161         rPropertyMap[ PROP_IsNumbering ] <<= sal_True;
162     sal_Int16 nLevel = mrTextParagraphProperties.getLevel();
163     rPropertyMap[ PROP_NumberingLevel ] <<= nLevel;
164     rPropertyMap[ PROP_NumberingIsNumber ] <<= sal_True;
165 }
166 
167 // --------------------------------------------------------------------
168 
endFastElement(sal_Int32)169 void TextParagraphPropertiesContext::endFastElement( sal_Int32 ) throw (SAXException, RuntimeException)
170 {
171 }
172 
173 
174 
175 // --------------------------------------------------------------------
176 
createFastChildContext(sal_Int32 aElementToken,const Reference<XFastAttributeList> & rXAttributes)177 Reference< XFastContextHandler > TextParagraphPropertiesContext::createFastChildContext( sal_Int32 aElementToken, const Reference< XFastAttributeList >& rXAttributes ) throw (SAXException, RuntimeException)
178 {
179     AttributeList aAttribs( rXAttributes );
180     Reference< XFastContextHandler > xRet;
181     switch( aElementToken )
182     {
183         case A_TOKEN( lnSpc ):          // CT_TextSpacing
184             xRet.set( new TextSpacingContext( *this, maLineSpacing ) );
185             break;
186         case A_TOKEN( spcBef ):         // CT_TextSpacing
187             xRet.set( new TextSpacingContext( *this, mrSpaceBefore ) );
188             break;
189         case A_TOKEN( spcAft ):         // CT_TextSpacing
190             xRet.set( new TextSpacingContext( *this, mrSpaceAfter ) );
191             break;
192 
193         // EG_TextBulletColor
194         case A_TOKEN( buClrTx ):        // CT_TextBulletColorFollowText ???
195             mrBulletList.mbBulletColorFollowText <<= sal_True;
196             break;
197         case A_TOKEN( buClr ):          // CT_Color
198             xRet.set( new ColorContext( *this, *mrBulletList.maBulletColorPtr ) );
199             break;
200 
201         // EG_TextBulletSize
202         case A_TOKEN( buSzTx ):         // CT_TextBulletSizeFollowText
203             mrBulletList.setBulletSize(100);
204             break;
205         case A_TOKEN( buSzPct ):        // CT_TextBulletSizePercent
206             mrBulletList.setBulletSize( static_cast<sal_Int16>( GetPercent( rXAttributes->getOptionalValue( XML_val ) ) / 1000 ) );
207             break;
208         case A_TOKEN( buSzPts ):        // CT_TextBulletSizePoint
209             mrBulletList.setBulletSize(0);
210             mrBulletList.setFontSize( static_cast<sal_Int16>(GetTextSize( rXAttributes->getOptionalValue( XML_val ) ) ) );
211             break;
212 
213         // EG_TextBulletTypeface
214         case A_TOKEN( buFontTx ):       // CT_TextBulletTypefaceFollowText
215             mrBulletList.mbBulletFontFollowText <<= sal_True;
216             break;
217         case A_TOKEN( buFont ):         // CT_TextFont
218             mrBulletList.maBulletFont.setAttributes( aAttribs );
219             break;
220 
221         // EG_TextBullet
222         case A_TOKEN( buNone ):         // CT_TextNoBullet
223             mrBulletList.setNone();
224             break;
225         case A_TOKEN( buAutoNum ):      // CT_TextAutonumberBullet
226         {
227             AttributeList attribs( rXAttributes );
228             try {
229                 sal_Int32 nType = rXAttributes->getValueToken( XML_type );
230                 sal_Int32 nStartAt = attribs.getInteger( XML_startAt, 1 );
231                 if( nStartAt > 32767 )
232                 {
233                     nStartAt = 32767;
234                 }
235                 else if( nStartAt < 1 )
236                 {
237                     nStartAt = 1;
238                 }
239                 mrBulletList.setStartAt( nStartAt );
240                 mrBulletList.setType( nType );
241             }
242             catch(SAXException& /* e */ )
243             {
244                 OSL_TRACE("OOX: SAXException in XML_buAutoNum");
245             }
246             break;
247         }
248         case A_TOKEN( buChar ):         // CT_TextCharBullet
249             try {
250                 mrBulletList.setBulletChar( rXAttributes->getValue( XML_char ) );
251             }
252             catch(SAXException& /* e */)
253             {
254                 OSL_TRACE("OOX: SAXException in XML_buChar");
255             }
256             break;
257         case A_TOKEN( buBlip ):         // CT_TextBlipBullet
258             {
259                 mxBlipProps.reset( new BlipFillProperties );
260                 xRet.set( new BlipFillContext( *this, rXAttributes, *mxBlipProps ) );
261             }
262             break;
263 
264         case A_TOKEN( tabLst ):         // CT_TextTabStopList
265             xRet.set( new TextTabStopListContext( *this, maTabList ) );
266             break;
267         case A_TOKEN( defRPr ):         // CT_TextCharacterProperties
268             xRet.set( new TextCharacterPropertiesContext( *this, rXAttributes, mrTextParagraphProperties.getTextCharacterProperties() ) );
269             break;
270     }
271     if ( !xRet.is() )
272         xRet.set( this );
273     return xRet;
274 }
275 
276 // --------------------------------------------------------------------
277 
278 } }
279 
280