LibreOffice Module xmloff (master)  1
XMLTextColumnsContext.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 
20 #include <sal/config.h>
21 
22 #include <com/sun/star/frame/XModel.hpp>
23 #include <com/sun/star/text/XTextColumns.hpp>
24 #include <com/sun/star/text/TextColumn.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/style/VerticalAlignment.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <sal/log.hxx>
29 #include <sax/tools/converter.hxx>
30 #include <xmloff/xmltkmap.hxx>
31 #include <xmloff/xmluconv.hxx>
32 #include <xmloff/namespacemap.hxx>
33 #include <xmloff/xmlnamespace.hxx>
34 #include <xmloff/xmlimp.hxx>
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmlement.hxx>
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::text;
43 using namespace ::com::sun::star::style;
44 using namespace ::com::sun::star::beans;
45 using namespace ::xmloff::token;
46 
47 namespace {
48 
50 {
51  XML_TOK_COLUMN_WIDTH,
52  XML_TOK_COLUMN_MARGIN_LEFT,
53  XML_TOK_COLUMN_MARGIN_RIGHT
54 };
55 
57 {
58  XML_TOK_COLUMN_SEP_WIDTH,
59  XML_TOK_COLUMN_SEP_HEIGHT,
60  XML_TOK_COLUMN_SEP_COLOR,
61  XML_TOK_COLUMN_SEP_ALIGN,
62  XML_TOK_COLUMN_SEP_STYLE
63 };
64 
65 }
66 
68 {
69  { XML_NONE, 0 },
70  { XML_SOLID, 1 },
71  { XML_DOTTED, 2 },
72  { XML_DASHED, 3 },
73  { XML_TOKEN_INVALID, 0 }
74 };
75 
77 {
78  { XML_TOP, VerticalAlignment_TOP },
79  { XML_MIDDLE, VerticalAlignment_MIDDLE },
80  { XML_BOTTOM, VerticalAlignment_BOTTOM },
81  { XML_TOKEN_INVALID, VerticalAlignment(0) }
82 };
83 
85 {
86  text::TextColumn aColumn;
87 
88 public:
89 
90  XMLTextColumnContext_Impl( SvXMLImport& rImport, sal_Int32 nElement,
91  const uno::Reference<
92  xml::sax::XFastAttributeList > & xAttrList );
93 
94  text::TextColumn& getTextColumn() { return aColumn; }
95 };
96 
97 
99  SvXMLImport& rImport, sal_Int32 /*nElement*/,
100  const uno::Reference<
101  xml::sax::XFastAttributeList > & xAttrList ) :
102  SvXMLImportContext( rImport )
103 {
104  aColumn.Width = 0;
105  aColumn.LeftMargin = 0;
106  aColumn.RightMargin = 0;
107 
108  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
109  {
110  const OUString sValue = aIter.toString();
111  sal_Int32 nVal;
112  switch( aIter.getToken() )
113  {
115  {
116  sal_Int32 nPos = sValue.indexOf( '*' );
117  if( nPos != -1 && nPos+1 == sValue.getLength() )
118  {
120  nVal,
121  std::u16string_view(sValue).substr(0, nPos),
122  0, USHRT_MAX))
123  aColumn.Width = nVal;
124  }
125  }
126  break;
127  case XML_ELEMENT(FO, XML_START_INDENT):
128  case XML_ELEMENT(FO_COMPAT, XML_START_INDENT):
129  if( GetImport().GetMM100UnitConverter().
130  convertMeasureToCore( nVal, sValue ) )
131  aColumn.LeftMargin = nVal;
132  break;
133  case XML_ELEMENT(FO, XML_END_INDENT):
134  case XML_ELEMENT(FO_COMPAT, XML_END_INDENT):
135  if( GetImport().GetMM100UnitConverter().
136  convertMeasureToCore( nVal, sValue ) )
137  aColumn.RightMargin = nVal;
138  break;
139  default:
140  SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << sValue);
141  break;
142  }
143  }
144 }
145 
147 {
148  sal_Int32 nWidth;
149  sal_Int32 nColor;
152  VerticalAlignment eVertAlign;
153 
154 public:
155 
156  XMLTextColumnSepContext_Impl( SvXMLImport& rImport, sal_Int32 nElement,
157  const uno::Reference<
158  xml::sax::XFastAttributeList > & xAttrList );
159 
160  sal_Int32 GetWidth() const { return nWidth; }
161  sal_Int32 GetColor() const { return nColor; }
162  sal_Int8 GetHeight() const { return nHeight; }
163  sal_Int8 GetStyle() const { return nStyle; }
164  VerticalAlignment GetVertAlign() const { return eVertAlign; }
165 };
166 
167 
169  SvXMLImport& rImport, sal_Int32 /*nElement*/,
170  const uno::Reference<
171  xml::sax::XFastAttributeList > & xAttrList) :
172  SvXMLImportContext( rImport ),
173  nWidth( 2 ),
174  nColor( 0 ),
175  nHeight( 100 ),
176  nStyle( 1 ),
177  eVertAlign( VerticalAlignment_TOP )
178 {
179  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
180  {
181  const OUString sValue = aIter.toString();
182 
183  sal_Int32 nVal;
184  switch( aIter.getToken() )
185  {
186  case XML_ELEMENT(STYLE, XML_WIDTH):
187  if( GetImport().GetMM100UnitConverter().
188  convertMeasureToCore( nVal, sValue ) )
189  nWidth = nVal;
190  break;
192  if (::sax::Converter::convertPercent( nVal, sValue ) &&
193  nVal >=1 && nVal <= 100 )
194  nHeight = static_cast<sal_Int8>(nVal);
195  break;
196  case XML_ELEMENT(STYLE, XML_COLOR):
198  break;
201  pXML_Sep_Align_Enum );
202  break;
203  case XML_ELEMENT(STYLE, XML_STYLE):
205  pXML_Sep_Style_Enum );
206  break;
207  default:
208  SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << sValue);
209  }
210  }
211 }
212 
213 const OUStringLiteral gsSeparatorLineIsOn(u"SeparatorLineIsOn");
214 const OUStringLiteral gsSeparatorLineWidth(u"SeparatorLineWidth");
215 const OUStringLiteral gsSeparatorLineColor(u"SeparatorLineColor");
216 const OUStringLiteral gsSeparatorLineRelativeHeight(u"SeparatorLineRelativeHeight");
217 const OUStringLiteral gsSeparatorLineVerticalAlignment(u"SeparatorLineVerticalAlignment");
218 const OUStringLiteral gsAutomaticDistance(u"AutomaticDistance");
219 const OUStringLiteral gsSeparatorLineStyle(u"SeparatorLineStyle");
220 
222  SvXMLImport& rImport, sal_Int32 nElement,
223  const Reference< xml::sax::XFastAttributeList >& xAttrList,
224  const XMLPropertyState& rProp,
225  ::std::vector< XMLPropertyState > &rProps )
226 : XMLElementPropertyContext( rImport, nElement, rProp, rProps )
227 , nCount( 0 )
228 , bAutomatic( false )
229 , nAutomaticDistance( 0 )
230 {
231  sal_Int32 nVal;
232  for (auto &aIter : sax_fastparser::castToFastAttributeList(xAttrList))
233  {
234  const OUString sValue = aIter.toString();
235  switch(aIter.getToken())
236  {
237  case XML_ELEMENT(FO, XML_COLUMN_COUNT):
238  case XML_ELEMENT(FO_COMPAT, XML_COLUMN_COUNT):
239  if(::sax::Converter::convertNumber( nVal, sValue, 0, SHRT_MAX ))
240  nCount = static_cast<sal_Int16>(nVal);
241  break;
242  case XML_ELEMENT(FO, XML_COLUMN_GAP):
243  case XML_ELEMENT(FO_COMPAT, XML_COLUMN_GAP):
244  {
246  convertMeasureToCore( nAutomaticDistance, sValue );
247  break;
248  }
249  default:
250  SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << sValue);
251  }
252  }
253 }
254 
255 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextColumnsContext::createFastChildContext(
256  sal_Int32 nElement,
257  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
258 {
259  if( nElement == XML_ELEMENT(STYLE, XML_COLUMN) )
260  {
262  new XMLTextColumnContext_Impl( GetImport(), nElement, xAttrList )};
263 
264  // add new tabstop to array of tabstops
265  if( !pColumns )
266  pColumns = std::make_unique<XMLTextColumnsArray_Impl>();
267 
268  pColumns->push_back( xColumn );
269 
270  return xColumn.get();
271  }
272  else if( nElement == XML_ELEMENT(STYLE, XML_COLUMN_SEP) )
273  {
274  mxColumnSep.set(
275  new XMLTextColumnSepContext_Impl( GetImport(), nElement, xAttrList ));
276 
277  return mxColumnSep.get();
278  }
279  SAL_WARN("xmloff", "unknown element " << SvXMLImport::getPrefixAndNameFromToken(nElement));
280  return nullptr;
281 }
282 
283 void XMLTextColumnsContext::endFastElement(sal_Int32 nElement )
284 {
285  Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
286  if( !xFactory.is() )
287  return;
288 
289  Reference<XInterface> xIfc = xFactory->createInstance("com.sun.star.text.TextColumns");
290  if( !xIfc.is() )
291  return;
292 
293  Reference< XTextColumns > xColumns( xIfc, UNO_QUERY );
294  if ( 0 == nCount )
295  {
296  // zero columns = no columns -> 1 column
297  xColumns->setColumnCount( 1 );
298  }
299  else if( !bAutomatic && pColumns &&
300  pColumns->size() == static_cast<sal_uInt16>(nCount) )
301  {
302  // if we have column descriptions, one per column, and we don't use
303  // automatic width, then set the column widths
304 
305  sal_Int32 nRelWidth = 0;
306  sal_uInt16 nColumnsWithWidth = 0;
307  sal_Int16 i;
308 
309  for( i = 0; i < nCount; i++ )
310  {
311  const TextColumn& rColumn =
312  (*pColumns)[static_cast<sal_uInt16>(i)]->getTextColumn();
313  if( rColumn.Width > 0 )
314  {
315  nRelWidth += rColumn.Width;
316  nColumnsWithWidth++;
317  }
318  }
319  if( nColumnsWithWidth < nCount )
320  {
321  sal_Int32 nColWidth = 0==nRelWidth
322  ? USHRT_MAX / nCount
323  : nRelWidth / nColumnsWithWidth;
324 
325  for( i=0; i < nCount; i++ )
326  {
327  TextColumn& rColumn =
328  (*pColumns)[static_cast<sal_uInt16>(i)]->getTextColumn();
329  if( rColumn.Width == 0 )
330  {
331  rColumn.Width = nColWidth;
332  nRelWidth += rColumn.Width;
333  if( 0 == --nColumnsWithWidth )
334  break;
335  }
336  }
337  }
338 
339  Sequence< TextColumn > aColumns( static_cast<sal_Int32>(nCount) );
340  TextColumn *pTextColumns = aColumns.getArray();
341  for( i=0; i < nCount; i++ )
342  *pTextColumns++ = (*pColumns)[static_cast<sal_uInt16>(i)]->getTextColumn();
343 
344  xColumns->setColumns( aColumns );
345  }
346  else
347  {
348  // only set column count (and let the columns be distributed
349  // automatically)
350 
351  xColumns->setColumnCount( nCount );
352  }
353 
354  Reference < XPropertySet > xPropSet( xColumns, UNO_QUERY );
355  if( xPropSet.is() )
356  {
357  bool bOn = mxColumnSep != nullptr;
358 
359  xPropSet->setPropertyValue( gsSeparatorLineIsOn, Any(bOn) );
360 
361  if( mxColumnSep.is() )
362  {
363  if( mxColumnSep->GetWidth() )
364  {
365  xPropSet->setPropertyValue( gsSeparatorLineWidth, Any(mxColumnSep->GetWidth()) );
366  }
367  if( mxColumnSep->GetHeight() )
368  {
369  xPropSet->setPropertyValue( gsSeparatorLineRelativeHeight,
370  Any(mxColumnSep->GetHeight()) );
371  }
372  if ( mxColumnSep->GetStyle() )
373  {
374  xPropSet->setPropertyValue( gsSeparatorLineStyle, Any(mxColumnSep->GetStyle()) );
375  }
376 
377  xPropSet->setPropertyValue( gsSeparatorLineColor, Any(mxColumnSep->GetColor()) );
378 
379  xPropSet->setPropertyValue( gsSeparatorLineVerticalAlignment, Any(mxColumnSep->GetVertAlign()) );
380  }
381 
382  // handle 'automatic columns': column distance
383  if( bAutomatic )
384  {
385  xPropSet->setPropertyValue( gsAutomaticDistance, Any(nAutomaticDistance) );
386  }
387  }
388 
389  aProp.maValue <<= xColumns;
390 
391  SetInsert( true );
393 
394 }
395 
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUStringLiteral gsSeparatorLineColor(u"SeparatorLineColor")
static bool convertEnum(EnumT &rEnum, const OUString &rValue, const SvXMLEnumMapEntry< EnumT > *pMap)
convert string to enum using given enum map, if the enum is not found in the map, this method will re...
Definition: xmluconv.hxx:128
const OUStringLiteral gsSeparatorLineWidth(u"SeparatorLineWidth")
signed char sal_Int8
SvXMLTokenMapAttrs
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
rtl::Reference< XMLTextColumnSepContext_Impl > mxColumnSep
text::TextColumn & getTextColumn()
const OUStringLiteral gsSeparatorLineRelativeHeight(u"SeparatorLineRelativeHeight")
const OUStringLiteral gsSeparatorLineIsOn(u"SeparatorLineIsOn")
int nCount
XMLTextColumnsContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, const XMLPropertyState &rProp,::std::vector< XMLPropertyState > &rProps)
static OUString getPrefixAndNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:2025
VerticalAlignment GetVertAlign() const
XMLTextColumnSepContext_Impl(SvXMLImport &rImport, sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList > &xAttrList)
int i
const OUStringLiteral gsSeparatorLineVerticalAlignment(u"SeparatorLineVerticalAlignment")
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
const OUStringLiteral gsSeparatorLineStyle(u"SeparatorLineStyle")
SvXMLEnumMapEntry< VerticalAlignment > const pXML_Sep_Align_Enum[]
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
Map an XMLTokenEnum to an enum value.
Definition: ximpshap.hxx:40
css::uno::Any maValue
Definition: maptype.hxx:125
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
SvXMLSepTokenMapAttrs
static bool convertColor(sal_Int32 &rColor, const OUString &rValue)
XMLTextColumnContext_Impl(SvXMLImport &rImport, sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList > &xAttrList)
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Handling of tokens in XML:
const OUStringLiteral gsAutomaticDistance(u"AutomaticDistance")
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:122
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlimp.hxx:404
SvXMLEnumMapEntry< sal_Int8 > const pXML_Sep_Style_Enum[]
Reference< XColumn > xColumn
#define SAL_WARN(area, stream)
Reference< XSingleServiceFactory > xFactory
sal_uInt16 nPos
std::unique_ptr< XMLTextColumnsArray_Impl > pColumns
static bool convertPercent(sal_Int32 &rValue, const OUString &rString)
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)