LibreOffice Module oox (master) 1
textparagraphpropertiescontext.cxx
Go to the documentation of this file.
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
21
22#include <com/sun/star/text/WritingMode2.hpp>
23#include <com/sun/star/style/ParagraphAdjust.hpp>
24#include <com/sun/star/xml/sax/SAXException.hpp>
25#include <com/sun/star/graphic/XGraphic.hpp>
26#include <com/sun/star/awt/Size.hpp>
27#include <com/sun/star/uno/Reference.hxx>
28
29#include <sal/log.hxx>
32
40#include <oox/token/namespaces.hxx>
41#include <oox/token/properties.hxx>
42#include <oox/token/tokens.hxx>
43
44using namespace ::oox::core;
45using namespace ::com::sun::star::uno;
46using namespace ::com::sun::star::xml::sax;
47using namespace ::com::sun::star::style;
48using namespace ::com::sun::star::text;
49using namespace ::com::sun::star::graphic;
50
51namespace oox::drawingml {
52namespace {
53
54double lclGetGraphicAspectRatio( const Reference< XGraphic >& rxGraphic )
55{
56 double fRatio = 1.0;
57 Reference< com::sun::star::beans::XPropertySet > xGraphicPropertySet( rxGraphic, UNO_QUERY_THROW );
58 css::awt::Size aSizeHmm( 0, 0 );
59 xGraphicPropertySet->getPropertyValue( "Size100thMM" ) >>= aSizeHmm;
60
61 if( aSizeHmm.Width > 0 && aSizeHmm.Height > 0)
62 return double(aSizeHmm.Width)/double(aSizeHmm.Height);
63 else
64 {
65 css::awt::Size aSourceSizePixel( 0, 0 );
66 xGraphicPropertySet->getPropertyValue( "SizePixel" ) >>= aSourceSizePixel;
67
68 if( aSourceSizePixel.Width > 0 && aSourceSizePixel.Height > 0 )
69 return double(aSourceSizePixel.Width)/double(aSourceSizePixel.Height);
70 }
71 return fRatio;
72}
73
74} //namespace
75
76// CT_TextParagraphProperties
78 const AttributeList& rAttribs,
79 TextParagraphProperties& rTextParagraphProperties )
80: ContextHandler2( rParent )
81, mrTextParagraphProperties( rTextParagraphProperties )
82, mrBulletList( rTextParagraphProperties.getBulletList() )
83{
84 OUString sValue;
85
87
88 // ST_TextAlignType
89 if ( rAttribs.hasAttribute( XML_algn ) )
90 {
91 mrTextParagraphProperties.getParaAdjust() = GetParaAdjust( rAttribs.getToken( XML_algn, XML_l ) );
92 }
93 // TODO see to do the same with RubyAdjust
94
95 // ST_Coordinate32
96 if ( rAttribs.hasAttribute(XML_defTabSz))
97 {
98 sValue = rAttribs.getStringDefaulted(XML_defTabSz);
99 if(!sValue.isEmpty())
100 {
102 }
103 }
104
105// bool bEaLineBrk = rAttribs.getBool( XML_eaLnBrk, true );
106 if ( rAttribs.hasAttribute( XML_latinLnBrk ) )
107 {
108 bool bLatinLineBrk = rAttribs.getBool( XML_latinLnBrk, true );
109 rPropertyMap.setProperty( PROP_ParaIsHyphenation, bLatinLineBrk);
110 }
111 // TODO see what to do with Asian hyphenation
112
113 // ST_TextFontAlignType
114 // TODO
115// sal_Int32 nFontAlign = rAttribs.getToken( XML_fontAlgn, XML_base );
116
117 if ( rAttribs.hasAttribute( XML_hangingPunct ) )
118 {
119 bool bHangingPunct = rAttribs.getBool( XML_hangingPunct, false );
120 rPropertyMap.setProperty( PROP_ParaIsHangingPunctuation, bHangingPunct);
121 }
122
123 // ST_Coordinate
124 if ( rAttribs.hasAttribute( XML_indent ) )
125 {
126 sValue = rAttribs.getStringDefaulted( XML_indent );
127 mrTextParagraphProperties.getFirstLineIndentation() = std::optional< sal_Int32 >( sValue.isEmpty() ? 0 : GetCoordinate( sValue ) );
128 }
129
130 // ST_TextIndentLevelType
131 // -1 is an invalid value and denote the lack of level
132 sal_Int32 nLevel = rAttribs.getInteger( XML_lvl, 0 );
133 if( nLevel > 8 || nLevel < 0 )
134 {
135 nLevel = 0;
136 }
137
138 mrTextParagraphProperties.setLevel( static_cast< sal_Int16 >( nLevel ) );
139
140 char name[] = "Outline X";
141 name[8] = static_cast<char>( '1' + nLevel );
142 const OUString sStyleNameValue( OUString::createFromAscii( name ) );
143 mrBulletList.setStyleName( sStyleNameValue );
144
145 // ST_TextMargin
146 // ParaLeftMargin
147 if ( rAttribs.hasAttribute( XML_marL ) )
148 {
149 sValue = rAttribs.getStringDefaulted( XML_marL );
150 mrTextParagraphProperties.getParaLeftMargin() = std::optional< sal_Int32 >( sValue.isEmpty() ? 0 : GetCoordinate( sValue ) );
151 }
152
153 // ParaRightMargin
154 if ( rAttribs.hasAttribute( XML_marR ) )
155 {
156 sValue = rAttribs.getStringDefaulted( XML_marR );
157 sal_Int32 nMarR = sValue.isEmpty() ? 0 : GetCoordinate( sValue ) ;
158 rPropertyMap.setProperty( PROP_ParaRightMargin, nMarR);
159 }
160
161 if ( rAttribs.hasAttribute( XML_rtl ) )
162 {
163 bool bRtl = rAttribs.getBool( XML_rtl, false );
164 rPropertyMap.setProperty( PROP_WritingMode, ( bRtl ? WritingMode2::RL_TB : WritingMode2::LR_TB ));
165 }
166}
167
169{
173 else
174 rPropertyMap.setProperty( PROP_ParaLineSpacing, css::style::LineSpacing( css::style::LineSpacingMode::PROP, 100 ));
175
176 ::std::vector< TabStop >::size_type nTabCount = maTabList.size();
177 if( nTabCount != 0 )
178 {
179 Sequence< TabStop > aSeq( nTabCount );
180 TabStop * aArray = aSeq.getArray();
181 OSL_ENSURE( aArray != nullptr, "sequence array is NULL" );
182 ::std::copy( maTabList.begin(), maTabList.end(), aArray );
183 rPropertyMap.setProperty( PROP_ParaTabStops, aSeq);
184 }
185
186 if (mxBlipProps && mxBlipProps->mxFillGraphic.is())
187 {
188 mrBulletList.setGraphic( mxBlipProps->mxFillGraphic );
189 mrBulletList.setBulletAspectRatio( lclGetGraphicAspectRatio(mxBlipProps->mxFillGraphic) );
190 }
191
192 if( mrBulletList.is() )
193 rPropertyMap.setProperty( PROP_IsNumbering, true);
194 sal_Int16 nLevel = mrTextParagraphProperties.getLevel();
195 rPropertyMap.setProperty( PROP_NumberingLevel, nLevel);
196 rPropertyMap.setProperty( PROP_NumberingIsNumber, true);
197
200}
201
203{
204 switch( aElementToken )
205 {
206 case A_TOKEN( lnSpc ): // CT_TextSpacing
208 case A_TOKEN( spcBef ): // CT_TextSpacing
210 case A_TOKEN( spcAft ): // CT_TextSpacing
212 // EG_TextBulletColor
213 case A_TOKEN( buClrTx ): // CT_TextBulletColorFollowText ???
215 break;
216 case A_TOKEN( buClr ): // CT_Color
217 return new ColorContext( *this, *mrBulletList.maBulletColorPtr );
218 // EG_TextBulletSize
219 case A_TOKEN( buSzTx ): // CT_TextBulletSizeFollowText
221 break;
222 case A_TOKEN( buSzPct ): // CT_TextBulletSizePercent
223 mrBulletList.setBulletSize( std::lround( GetPercent( rAttribs.getStringDefaulted( XML_val ) ) / 1000.f ) );
224 break;
225 case A_TOKEN( buSzPts ): // CT_TextBulletSizePoint
227 mrBulletList.setFontSize( static_cast<sal_Int16>(GetTextSize( rAttribs.getStringDefaulted( XML_val ) ) ) );
228 break;
229
230 // EG_TextBulletTypeface
231 case A_TOKEN( buFontTx ): // CT_TextBulletTypefaceFollowText
233 break;
234 case A_TOKEN( buFont ): // CT_TextFont
236 break;
237
238 // EG_TextBullet
239 case A_TOKEN( buNone ): // CT_TextNoBullet
241 break;
242 case A_TOKEN( buAutoNum ): // CT_TextAutonumberBullet
243 {
244 try {
245 sal_Int32 nType = rAttribs.getToken( XML_type, 0 );
246 sal_Int32 nStartAt = rAttribs.getInteger( XML_startAt, 1 );
247 if( nStartAt > 32767 )
248 {
249 nStartAt = 32767;
250 }
251 else if( nStartAt < 1 )
252 {
253 nStartAt = 1;
254 }
255 mrBulletList.setStartAt( nStartAt );
257 }
258 catch(SAXException& /* e */ )
259 {
260 TOOLS_WARN_EXCEPTION("oox", "OOX: SAXException in XML_buAutoNum");
261 }
262 break;
263 }
264 case A_TOKEN( buChar ): // CT_TextCharBullet
265 try {
266
267 mrBulletList.setBulletChar( rAttribs.getStringDefaulted( XML_char ) );
269 }
270 catch(SAXException& /* e */)
271 {
272 TOOLS_WARN_EXCEPTION("oox", "OOX: SAXException in XML_buChar");
273 }
274 break;
275 case A_TOKEN( buBlip ): // CT_TextBlipBullet
276 {
277 mxBlipProps = std::make_shared<BlipFillProperties>();
278 return new BlipFillContext(*this, rAttribs, *mxBlipProps, nullptr);
279 }
280 case A_TOKEN( tabLst ): // CT_TextTabStopList
281 return new TextTabStopListContext( *this, maTabList );
282 case A_TOKEN( defRPr ): // CT_TextCharacterProperties
284 case W_TOKEN( jc ):
285 {
286 std::optional< OUString > oParaAdjust = rAttribs.getString( W_TOKEN(val) );
287 if( oParaAdjust.has_value() && !oParaAdjust.value().isEmpty() )
288 {
289 const OUString& sParaAdjust = oParaAdjust.value();
290 if( sParaAdjust == "left" )
291 mrTextParagraphProperties.setParaAdjust(ParagraphAdjust_LEFT);
292 else if ( sParaAdjust == "right" )
293 mrTextParagraphProperties.setParaAdjust(ParagraphAdjust_RIGHT);
294 else if ( sParaAdjust == "center" )
295 mrTextParagraphProperties.setParaAdjust(ParagraphAdjust_CENTER);
296 else if ( sParaAdjust == "both" )
297 mrTextParagraphProperties.setParaAdjust(ParagraphAdjust_BLOCK);
298 }
299 }
300 break;
301 case W_TOKEN( spacing ):
302 {
303 // Spacing before
304 if( !rAttribs.getBool(W_TOKEN(beforeAutospacing), false) )
305 {
306 std::optional<sal_Int32> oBefore = rAttribs.getInteger(W_TOKEN(before));
307 if (oBefore.has_value())
308 {
311 rSpacing.nValue = convertTwipToMm100(oBefore.value());
312 rSpacing.bHasValue = true;
313 }
314 else
315 {
316 std::optional<sal_Int32> oBeforeLines = rAttribs.getInteger(W_TOKEN(beforeLines));
317 if (oBeforeLines.has_value())
318 {
321 rSpacing.nValue = oBeforeLines.value() * MAX_PERCENT / 100;
322 rSpacing.bHasValue = true;
323 }
324 }
325 }
326
327 // Spacing after
328 if( !rAttribs.getBool(W_TOKEN(afterAutospacing), false) )
329 {
330 std::optional<sal_Int32> oAfter = rAttribs.getInteger(W_TOKEN(after));
331 if (oAfter.has_value())
332 {
335 rSpacing.nValue = convertTwipToMm100(oAfter.value());
336 rSpacing.bHasValue = true;
337 }
338 else
339 {
340 std::optional<sal_Int32> oAfterLines = rAttribs.getInteger(W_TOKEN(afterLines));
341 if (oAfterLines.has_value())
342 {
345 rSpacing.nValue = oAfterLines.value() * MAX_PERCENT / 100;
346 rSpacing.bHasValue = true;
347 }
348 }
349 }
350
351 // Line spacing
352 std::optional<OUString> oLineRule = rAttribs.getString(W_TOKEN(lineRule));
353 std::optional<sal_Int32> oLineSpacing = rAttribs.getInteger(W_TOKEN(line));
354 if (oLineSpacing.has_value())
355 {
357 if( !oLineRule.has_value() || oLineRule.value() == "auto" )
358 {
359 rLineSpacing.nUnit = TextSpacing::Unit::Percent;
360 rLineSpacing.nValue = oLineSpacing.value() * MAX_PERCENT / 240;
361 }
362 else
363 {
364 rLineSpacing.nUnit = TextSpacing::Unit::Points;
365 rLineSpacing.nValue = convertTwipToMm100(oLineSpacing.value());
366 }
367 rLineSpacing.bHasValue = true;
368 }
369 }
370 break;
371 default:
372 SAL_WARN("oox", "TextParagraphPropertiesContext::onCreateContext: unhandled element: " << getBaseToken(aElementToken));
373 }
374 return this;
375}
376
377}
378
379/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr auto convertTwipToMm100(N n)
Provides access to attribute values of an element.
OUString getStringDefaulted(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute, returns an empty string if attribute not present...
bool hasAttribute(sal_Int32 nAttrToken) const
Returns true, if the specified attribute is present.
std::optional< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
Returns the 32-bit signed integer value of the specified attribute (decimal).
std::optional< OUString > getString(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute.
std::optional< bool > getBool(sal_Int32 nAttrToken) const
Returns the boolean value of the specified attribute.
std::optional< sal_Int32 > getToken(sal_Int32 nAttrToken) const
Returns the token identifier of the value of the specified attribute.
A helper that maps property identifiers to property values.
Definition: propertymap.hxx:52
bool setProperty(sal_Int32 nPropId, Type &&rValue)
Sets the specified property to the passed value.
Definition: propertymap.hxx:72
Context handler that imports the a:blipFill element.
void setBulletAspectRatio(double nAspectRatio)
void setStartAt(sal_Int32 nStartAt)
void setBulletChar(const OUString &sChar)
void setGraphic(css::uno::Reference< css::graphic::XGraphic > const &rXGraphic)
void setStyleName(const OUString &rStyleName)
::oox::drawingml::TextFont maBulletFont
std::shared_ptr< ::oox::drawingml::Color > maBulletColorPtr
Context handler for elements that contain a color value element (a:scrgbClr, a:srgbClr,...
void setAttributes(const AttributeList &rAttribs)
Sets attributes from the passed attribute list.
Definition: textfont.cxx:57
TextParagraphPropertiesContext(::oox::core::ContextHandler2Helper const &rParent, const ::oox::AttributeList &rAttributes, TextParagraphProperties &rTextParagraphProperties)
virtual ::oox::core::ContextHandlerRef onCreateContext(::sal_Int32 Element, const ::oox::AttributeList &rAttribs) override
std::optional< sal_Int32 > & getParaLeftMargin()
std::optional< sal_Int32 > & getFirstLineIndentation()
std::optional< sal_Int32 > & getDefaultTabSize()
std::optional< css::style::ParagraphAdjust > & getParaAdjust()
TextCharacterProperties & getTextCharacterProperties()
void setParaAdjust(css::style::ParagraphAdjust nParaAdjust)
carries a CT_TextSpacing
Definition: textspacing.hxx:32
css::style::LineSpacing toLineSpacing() const
Definition: textspacing.hxx:43
#define TOOLS_WARN_EXCEPTION(area, stream)
const char * name
Sequence< sal_Int8 > aSeq
#define SAL_WARN(area, stream)
line
sal_Int32 GetCoordinate(sal_Int32 nValue)
converts EMUs into 1/100th mmm
ParagraphAdjust GetParaAdjust(sal_Int32 nAlign)
converts a paragraph align to a ParaAdjust
const sal_Int32 MAX_PERCENT
sal_Int32 GetPercent(std::u16string_view sValue)
converts a ST_Percentage % string into 1/1000th of %
float GetTextSize(std::u16string_view sValue)
converts the ST_TextFontSize to point
XML_type
QPRO_FUNC_TYPE nType
constexpr OUStringLiteral PROP_WritingMode
constexpr OUStringLiteral PROP_ParaIsHyphenation
constexpr OUStringLiteral PROP_ParaTabStops
constexpr OUStringLiteral PROP_ParaRightMargin
constexpr OUStringLiteral PROP_ParaLineSpacing
constexpr OUStringLiteral PROP_ParaIsHangingPunctuation
constexpr OUStringLiteral PROP_ParaAdjust