Line data Source code
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/textparagraphpropertiescontext.hxx"
21 :
22 : #include <com/sun/star/text/WritingMode2.hpp>
23 : #include <com/sun/star/awt/FontDescriptor.hpp>
24 : #include <com/sun/star/style/ParagraphAdjust.hpp>
25 :
26 : #include <svx/unopage.hxx>
27 :
28 : #include "drawingml/colorchoicecontext.hxx"
29 : #include "drawingml/textcharacterpropertiescontext.hxx"
30 : #include "oox/drawingml/fillproperties.hxx"
31 : #include "oox/helper/attributelist.hxx"
32 : #include "textspacingcontext.hxx"
33 : #include "texttabstoplistcontext.hxx"
34 :
35 : using namespace ::oox::core;
36 : using ::com::sun::star::awt::FontDescriptor;
37 : using namespace ::com::sun::star::uno;
38 : using namespace ::com::sun::star::xml::sax;
39 : using namespace ::com::sun::star::style;
40 : using namespace ::com::sun::star::text;
41 :
42 : namespace oox { namespace drawingml {
43 :
44 : // CT_TextParagraphProperties
45 6864 : TextParagraphPropertiesContext::TextParagraphPropertiesContext( ContextHandler2Helper& rParent,
46 : const AttributeList& rAttribs,
47 : TextParagraphProperties& rTextParagraphProperties )
48 : : ContextHandler2( rParent )
49 : , mrTextParagraphProperties( rTextParagraphProperties )
50 6864 : , mrBulletList( rTextParagraphProperties.getBulletList() )
51 : {
52 6864 : OUString sValue;
53 :
54 6864 : PropertyMap& rPropertyMap( mrTextParagraphProperties.getTextParagraphPropertyMap() );
55 :
56 : // ST_TextAlignType
57 6864 : rPropertyMap.setProperty( PROP_ParaAdjust, GetParaAdjust( rAttribs.getToken( XML_algn, XML_l ) ));
58 : // TODO see to do the same with RubyAdjust
59 :
60 : // ST_Coordinate32
61 : // sValue = rAttribs.getString( XML_defTabSz ).get(); SJ: we need to be able to set the default tab size for each text object,
62 : // this is possible at the moment only for the whole document.
63 : // sal_Int32 nDefTabSize = ( sValue.getLength() == 0 ? 0 : GetCoordinate( sValue ) );
64 : // TODO
65 :
66 : // bool bEaLineBrk = rAttribs.getBool( XML_eaLnBrk, true );
67 6864 : if ( rAttribs.hasAttribute( XML_latinLnBrk ) )
68 : {
69 2488 : bool bLatinLineBrk = rAttribs.getBool( XML_latinLnBrk, true );
70 2488 : rPropertyMap.setProperty( PROP_ParaIsHyphenation, bLatinLineBrk);
71 : }
72 : // TODO see what to do with Asian hyphenation
73 :
74 : // ST_TextFontAlignType
75 : // TODO
76 : // sal_Int32 nFontAlign = rAttribs.getToken( XML_fontAlgn, XML_base );
77 :
78 6864 : if ( rAttribs.hasAttribute( XML_hangingPunct ) )
79 : {
80 3024 : bool bHangingPunct = rAttribs.getBool( XML_hangingPunct, false );
81 3024 : rPropertyMap.setProperty( PROP_ParaIsHangingPunctuation, bHangingPunct);
82 : }
83 :
84 : // ST_Coordinate
85 6864 : if ( rAttribs.hasAttribute( XML_indent ) )
86 : {
87 1262 : sValue = rAttribs.getString( XML_indent ).get();
88 1262 : mrTextParagraphProperties.getFirstLineIndentation() = boost::optional< sal_Int32 >( sValue.isEmpty() ? 0 : GetCoordinate( sValue ) );
89 : }
90 :
91 : // ST_TextIndentLevelType
92 : // -1 is an invalid value and denote the lack of level
93 6864 : sal_Int32 nLevel = rAttribs.getInteger( XML_lvl, 0 );
94 6864 : if( nLevel > 8 || nLevel < 0 )
95 : {
96 0 : nLevel = 0;
97 : }
98 :
99 6864 : mrTextParagraphProperties.setLevel( static_cast< sal_Int16 >( nLevel ) );
100 :
101 6864 : char name[] = "Outline X";
102 6864 : name[8] = static_cast<char>( '1' + nLevel );
103 13728 : const OUString sStyleNameValue( OUString::createFromAscii( name ) );
104 6864 : mrBulletList.setStyleName( sStyleNameValue );
105 :
106 : // ST_TextMargin
107 : // ParaLeftMargin
108 6864 : if ( rAttribs.hasAttribute( XML_marL ) )
109 : {
110 3418 : sValue = rAttribs.getString( XML_marL ).get();
111 3418 : mrTextParagraphProperties.getParaLeftMargin() = boost::optional< sal_Int32 >( sValue.isEmpty() ? 0 : GetCoordinate( sValue ) );
112 : }
113 :
114 : // ParaRightMargin
115 6864 : if ( rAttribs.hasAttribute( XML_marR ) )
116 : {
117 96 : sValue = rAttribs.getString( XML_marR ).get();
118 96 : sal_Int32 nMarR = sValue.isEmpty() ? 0 : GetCoordinate( sValue ) ;
119 96 : rPropertyMap.setProperty( PROP_ParaRightMargin, nMarR);
120 : }
121 :
122 6864 : if ( rAttribs.hasAttribute( XML_rtl ) )
123 : {
124 3334 : bool bRtl = rAttribs.getBool( XML_rtl, false );
125 3334 : rPropertyMap.setProperty( PROP_WritingMode, ( bRtl ? WritingMode2::RL_TB : WritingMode2::LR_TB ));
126 6864 : }
127 6864 : }
128 :
129 20592 : TextParagraphPropertiesContext::~TextParagraphPropertiesContext()
130 : {
131 6864 : PropertyMap& rPropertyMap( mrTextParagraphProperties.getTextParagraphPropertyMap() );
132 6864 : if ( maLineSpacing.bHasValue )
133 830 : rPropertyMap.setProperty( PROP_ParaLineSpacing, maLineSpacing.toLineSpacing());
134 : else
135 6034 : rPropertyMap.setProperty( PROP_ParaLineSpacing, ::com::sun::star::style::LineSpacing( ::com::sun::star::style::LineSpacingMode::PROP, 100 ));
136 :
137 6864 : ::std::list< TabStop >::size_type nTabCount = maTabList.size();
138 6864 : if( nTabCount != 0 )
139 : {
140 18 : Sequence< TabStop > aSeq( nTabCount );
141 18 : TabStop * aArray = aSeq.getArray();
142 : OSL_ENSURE( aArray != NULL, "sequence array is NULL" );
143 18 : ::std::copy( maTabList.begin(), maTabList.end(), aArray );
144 18 : rPropertyMap.setProperty( PROP_ParaTabStops, aSeq);
145 : }
146 :
147 6864 : if ( mxBlipProps.get() && mxBlipProps->mxGraphic.is() )
148 0 : mrBulletList.setGraphic( mxBlipProps->mxGraphic );
149 :
150 6864 : if( mrBulletList.is() )
151 1526 : rPropertyMap.setProperty( PROP_IsNumbering, sal_True);
152 6864 : sal_Int16 nLevel = mrTextParagraphProperties.getLevel();
153 6864 : rPropertyMap.setProperty( PROP_NumberingLevel, nLevel);
154 6864 : rPropertyMap.setProperty( PROP_NumberingIsNumber, sal_True);
155 :
156 6864 : if( mrTextParagraphProperties.getParaAdjust() )
157 84 : rPropertyMap.setProperty( PROP_ParaAdjust, mrTextParagraphProperties.getParaAdjust().get());
158 13728 : }
159 :
160 12296 : ContextHandlerRef TextParagraphPropertiesContext::onCreateContext( sal_Int32 aElementToken, const AttributeList& rAttribs )
161 : {
162 12296 : Reference< XFastContextHandler > xRet;
163 12296 : switch( aElementToken )
164 : {
165 : case A_TOKEN( lnSpc ): // CT_TextSpacing
166 736 : return new TextSpacingContext( *this, maLineSpacing );
167 : case A_TOKEN( spcBef ): // CT_TextSpacing
168 1848 : return new TextSpacingContext( *this, mrTextParagraphProperties.getParaTopMargin() );
169 : case A_TOKEN( spcAft ): // CT_TextSpacing
170 1234 : return new TextSpacingContext( *this, mrTextParagraphProperties.getParaBottomMargin() );
171 : // EG_TextBulletColor
172 : case A_TOKEN( buClrTx ): // CT_TextBulletColorFollowText ???
173 18 : mrBulletList.mbBulletColorFollowText <<= sal_True;
174 18 : break;
175 : case A_TOKEN( buClr ): // CT_Color
176 124 : return new ColorContext( *this, *mrBulletList.maBulletColorPtr );
177 : // EG_TextBulletSize
178 : case A_TOKEN( buSzTx ): // CT_TextBulletSizeFollowText
179 92 : mrBulletList.setBulletSize(100);
180 92 : break;
181 : case A_TOKEN( buSzPct ): // CT_TextBulletSizePercent
182 220 : mrBulletList.setBulletSize( static_cast<sal_Int16>( GetPercent( rAttribs.getString( XML_val ).get() ) / 1000 ) );
183 220 : break;
184 : case A_TOKEN( buSzPts ): // CT_TextBulletSizePoint
185 0 : mrBulletList.setBulletSize(0);
186 0 : mrBulletList.setFontSize( static_cast<sal_Int16>(GetTextSize( rAttribs.getString( XML_val ).get() ) ) );
187 0 : break;
188 :
189 : // EG_TextBulletTypeface
190 : case A_TOKEN( buFontTx ): // CT_TextBulletTypefaceFollowText
191 62 : mrBulletList.mbBulletFontFollowText <<= sal_True;
192 62 : break;
193 : case A_TOKEN( buFont ): // CT_TextFont
194 926 : mrBulletList.maBulletFont.setAttributes( rAttribs );
195 926 : break;
196 :
197 : // EG_TextBullet
198 : case A_TOKEN( buNone ): // CT_TextNoBullet
199 604 : mrBulletList.setNone();
200 604 : break;
201 : case A_TOKEN( buAutoNum ): // CT_TextAutonumberBullet
202 : {
203 : try {
204 0 : sal_Int32 nType = rAttribs.getToken( XML_type, 0 );
205 0 : sal_Int32 nStartAt = rAttribs.getInteger( XML_startAt, 1 );
206 0 : if( nStartAt > 32767 )
207 : {
208 0 : nStartAt = 32767;
209 : }
210 0 : else if( nStartAt < 1 )
211 : {
212 0 : nStartAt = 1;
213 : }
214 0 : mrBulletList.setStartAt( nStartAt );
215 0 : mrBulletList.setType( nType );
216 : }
217 0 : catch(SAXException& /* e */ )
218 : {
219 : OSL_TRACE("OOX: SAXException in XML_buAutoNum");
220 : }
221 0 : break;
222 : }
223 : case A_TOKEN( buChar ): // CT_TextCharBullet
224 : try {
225 922 : mrBulletList.setBulletChar( rAttribs.getString( XML_char ).get() );
226 : }
227 0 : catch(SAXException& /* e */)
228 : {
229 : OSL_TRACE("OOX: SAXException in XML_buChar");
230 : }
231 922 : break;
232 : case A_TOKEN( buBlip ): // CT_TextBlipBullet
233 : {
234 0 : mxBlipProps.reset( new BlipFillProperties );
235 0 : return new BlipFillContext( *this, rAttribs, *mxBlipProps );
236 : }
237 : case A_TOKEN( tabLst ): // CT_TextTabStopList
238 114 : return new TextTabStopListContext( *this, maTabList );
239 : case A_TOKEN( defRPr ): // CT_TextCharacterProperties
240 4380 : return new TextCharacterPropertiesContext( *this, rAttribs, mrTextParagraphProperties.getTextCharacterProperties() );
241 : case W_TOKEN( jc ):
242 : {
243 126 : OptValue< OUString > oParaAdjust = rAttribs.getString( W_TOKEN(val) );
244 126 : if( oParaAdjust.has() && !oParaAdjust.get().isEmpty() )
245 : {
246 126 : const OUString& sParaAdjust = oParaAdjust.get();
247 126 : if( sParaAdjust == "left" )
248 0 : mrTextParagraphProperties.setParaAdjust(ParagraphAdjust_LEFT);
249 126 : else if ( sParaAdjust == "right" )
250 8 : mrTextParagraphProperties.setParaAdjust(ParagraphAdjust_RIGHT);
251 118 : else if ( sParaAdjust == "center" )
252 72 : mrTextParagraphProperties.setParaAdjust(ParagraphAdjust_CENTER);
253 46 : else if ( sParaAdjust == "both" )
254 4 : mrTextParagraphProperties.setParaAdjust(ParagraphAdjust_BLOCK);
255 126 : }
256 : }
257 126 : break;
258 : case W_TOKEN( spacing ):
259 : {
260 : // Spacing before
261 104 : if( !rAttribs.getBool(W_TOKEN(beforeAutospacing), false) )
262 : {
263 100 : OptValue<sal_Int32> oBefore = rAttribs.getInteger(W_TOKEN(before));
264 100 : if (oBefore.has())
265 : {
266 56 : TextSpacing& rSpacing = mrTextParagraphProperties.getParaTopMargin();
267 56 : rSpacing.nUnit = TextSpacing::POINTS;
268 56 : rSpacing.nValue = TWIPS_TO_MM(oBefore.get());
269 56 : rSpacing.bHasValue = true;
270 : }
271 : else
272 : {
273 44 : OptValue<sal_Int32> oBeforeLines = rAttribs.getInteger(W_TOKEN(beforeLines));
274 44 : if (oBeforeLines.has())
275 : {
276 4 : TextSpacing& rSpacing = mrTextParagraphProperties.getParaTopMargin();
277 4 : rSpacing.nUnit = TextSpacing::PERCENT;
278 4 : rSpacing.nValue = oBeforeLines.get() * MAX_PERCENT / 100;
279 4 : rSpacing.bHasValue = true;
280 : }
281 : }
282 : }
283 :
284 : // Spacing after
285 104 : if( !rAttribs.getBool(W_TOKEN(afterAutospacing), false) )
286 : {
287 100 : OptValue<sal_Int32> oAfter = rAttribs.getInteger(W_TOKEN(after));
288 100 : if (oAfter.has())
289 : {
290 70 : TextSpacing& rSpacing = mrTextParagraphProperties.getParaBottomMargin();
291 70 : rSpacing.nUnit = TextSpacing::POINTS;
292 70 : rSpacing.nValue = TWIPS_TO_MM(oAfter.get());
293 70 : rSpacing.bHasValue = true;
294 : }
295 : else
296 : {
297 30 : OptValue<sal_Int32> oAfterLines = rAttribs.getInteger(W_TOKEN(afterLines));
298 30 : if (oAfterLines.has())
299 : {
300 4 : TextSpacing& rSpacing = mrTextParagraphProperties.getParaBottomMargin();
301 4 : rSpacing.nUnit = TextSpacing::PERCENT;
302 4 : rSpacing.nValue = oAfterLines.get() * MAX_PERCENT / 100;
303 4 : rSpacing.bHasValue = true;
304 : }
305 : }
306 : }
307 :
308 : // Line spacing
309 104 : OptValue<OUString> oLineRule = rAttribs.getString(W_TOKEN(lineRule));
310 104 : OptValue<sal_Int32> oLineSpacing = rAttribs.getInteger(W_TOKEN(line));
311 104 : if (oLineSpacing.has())
312 : {
313 94 : if( !oLineRule.has() || oLineRule.get() == "auto" )
314 : {
315 88 : maLineSpacing.nUnit = TextSpacing::PERCENT;
316 88 : maLineSpacing.nValue = oLineSpacing.get() * MAX_PERCENT / 240;
317 : }
318 : else
319 : {
320 6 : maLineSpacing.nUnit = TextSpacing::POINTS;
321 6 : maLineSpacing.nValue = TWIPS_TO_MM(oLineSpacing.get());
322 : }
323 94 : maLineSpacing.bHasValue = true;
324 104 : }
325 : }
326 104 : break;
327 : default:
328 : SAL_WARN("oox", "TextParagraphPropertiesContext::onCreateContext: unhandled element: " << getBaseToken(aElementToken));
329 : }
330 3860 : return this;
331 : }
332 :
333 : } }
334 :
335 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|