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