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