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 
48 {
49  { XML_NONE, 0 },
50  { XML_SOLID, 1 },
51  { XML_DOTTED, 2 },
52  { XML_DASHED, 3 },
53  { XML_TOKEN_INVALID, 0 }
54 };
55 
57 {
58  { XML_TOP, VerticalAlignment_TOP },
59  { XML_MIDDLE, VerticalAlignment_MIDDLE },
60  { XML_BOTTOM, VerticalAlignment_BOTTOM },
61  { XML_TOKEN_INVALID, VerticalAlignment(0) }
62 };
63 
65 {
66  text::TextColumn aColumn;
67 
68 public:
69 
70  XMLTextColumnContext_Impl( SvXMLImport& rImport, sal_Int32 nElement,
71  const uno::Reference<
72  xml::sax::XFastAttributeList > & xAttrList );
73 
74  text::TextColumn& getTextColumn() { return aColumn; }
75 };
76 
77 
79  SvXMLImport& rImport, sal_Int32 /*nElement*/,
80  const uno::Reference<
81  xml::sax::XFastAttributeList > & xAttrList ) :
82  SvXMLImportContext( rImport )
83 {
84  aColumn.Width = 0;
85  aColumn.LeftMargin = 0;
86  aColumn.RightMargin = 0;
87 
88  for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
89  {
90  sal_Int32 nVal;
91  switch( aIter.getToken() )
92  {
94  {
95  size_t nPos = aIter.toView().find( '*' );
96  if( nPos != std::string_view::npos && static_cast<sal_Int32>(nPos+1) == aIter.getLength() )
97  {
99  nVal,
100  aIter.toView().substr(0, nPos),
101  0, USHRT_MAX))
102  aColumn.Width = nVal;
103  }
104  }
105  break;
106  case XML_ELEMENT(FO, XML_START_INDENT):
107  case XML_ELEMENT(FO_COMPAT, XML_START_INDENT):
108  if( GetImport().GetMM100UnitConverter().
109  convertMeasureToCore( nVal, aIter.toView() ) )
110  aColumn.LeftMargin = nVal;
111  break;
112  case XML_ELEMENT(FO, XML_END_INDENT):
113  case XML_ELEMENT(FO_COMPAT, XML_END_INDENT):
114  if( GetImport().GetMM100UnitConverter().
115  convertMeasureToCore( nVal, aIter.toView() ) )
116  aColumn.RightMargin = nVal;
117  break;
118  default:
119  XMLOFF_WARN_UNKNOWN("xmloff", aIter);
120  break;
121  }
122  }
123 }
124 
126 {
127  sal_Int32 nWidth;
128  sal_Int32 nColor;
131  VerticalAlignment eVertAlign;
132 
133 public:
134 
135  XMLTextColumnSepContext_Impl( SvXMLImport& rImport, sal_Int32 nElement,
136  const uno::Reference<
137  xml::sax::XFastAttributeList > & xAttrList );
138 
139  sal_Int32 GetWidth() const { return nWidth; }
140  sal_Int32 GetColor() const { return nColor; }
141  sal_Int8 GetHeight() const { return nHeight; }
142  sal_Int8 GetStyle() const { return nStyle; }
143  VerticalAlignment GetVertAlign() const { return eVertAlign; }
144 };
145 
146 
148  SvXMLImport& rImport, sal_Int32 /*nElement*/,
149  const uno::Reference<
150  xml::sax::XFastAttributeList > & xAttrList) :
151  SvXMLImportContext( rImport ),
152  nWidth( 2 ),
153  nColor( 0 ),
154  nHeight( 100 ),
155  nStyle( 1 ),
156  eVertAlign( VerticalAlignment_TOP )
157 {
158  for (auto &aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
159  {
160  sal_Int32 nVal;
161  switch( aIter.getToken() )
162  {
163  case XML_ELEMENT(STYLE, XML_WIDTH):
164  if( GetImport().GetMM100UnitConverter().
165  convertMeasureToCore( nVal, aIter.toView() ) )
166  nWidth = nVal;
167  break;
169  if (::sax::Converter::convertPercent( nVal, aIter.toView() ) &&
170  nVal >=1 && nVal <= 100 )
171  nHeight = static_cast<sal_Int8>(nVal);
172  break;
173  case XML_ELEMENT(STYLE, XML_COLOR):
174  ::sax::Converter::convertColor( nColor, aIter.toView() );
175  break;
179  break;
180  case XML_ELEMENT(STYLE, XML_STYLE):
181  SvXMLUnitConverter::convertEnum( nStyle, aIter.toView(),
183  break;
184  default:
185  XMLOFF_WARN_UNKNOWN("xmloff", aIter);
186  }
187  }
188 }
189 
190 constexpr OUStringLiteral gsSeparatorLineIsOn(u"SeparatorLineIsOn");
191 constexpr OUStringLiteral gsSeparatorLineWidth(u"SeparatorLineWidth");
192 constexpr OUStringLiteral gsSeparatorLineColor(u"SeparatorLineColor");
193 constexpr OUStringLiteral gsSeparatorLineRelativeHeight(u"SeparatorLineRelativeHeight");
194 constexpr OUStringLiteral gsSeparatorLineVerticalAlignment(u"SeparatorLineVerticalAlignment");
195 constexpr OUStringLiteral gsAutomaticDistance(u"AutomaticDistance");
196 constexpr OUStringLiteral gsSeparatorLineStyle(u"SeparatorLineStyle");
197 
199  SvXMLImport& rImport, sal_Int32 nElement,
200  const Reference< xml::sax::XFastAttributeList >& xAttrList,
201  const XMLPropertyState& rProp,
202  ::std::vector< XMLPropertyState > &rProps )
203 : XMLElementPropertyContext( rImport, nElement, rProp, rProps )
204 , nCount( 0 )
205 , bAutomatic( false )
206 , nAutomaticDistance( 0 )
207 {
208  sal_Int32 nVal;
209  for (auto &aIter : sax_fastparser::castToFastAttributeList(xAttrList))
210  {
211  switch(aIter.getToken())
212  {
213  case XML_ELEMENT(FO, XML_COLUMN_COUNT):
214  case XML_ELEMENT(FO_COMPAT, XML_COLUMN_COUNT):
215  if(::sax::Converter::convertNumber( nVal, aIter.toView(), 0, SHRT_MAX ))
216  nCount = static_cast<sal_Int16>(nVal);
217  break;
218  case XML_ELEMENT(FO, XML_COLUMN_GAP):
219  case XML_ELEMENT(FO_COMPAT, XML_COLUMN_GAP):
220  {
222  convertMeasureToCore( nAutomaticDistance, aIter.toView() );
223  break;
224  }
225  default:
226  XMLOFF_WARN_UNKNOWN("xmloff", aIter);
227  }
228  }
229 }
230 
231 css::uno::Reference< css::xml::sax::XFastContextHandler > XMLTextColumnsContext::createFastChildContext(
232  sal_Int32 nElement,
233  const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
234 {
235  if( nElement == XML_ELEMENT(STYLE, XML_COLUMN) )
236  {
238  new XMLTextColumnContext_Impl( GetImport(), nElement, xAttrList )};
239 
240  // add new tabstop to array of tabstops
241  if( !pColumns )
242  pColumns = std::make_unique<XMLTextColumnsArray_Impl>();
243 
244  pColumns->push_back( xColumn );
245 
246  return xColumn;
247  }
248  else if( nElement == XML_ELEMENT(STYLE, XML_COLUMN_SEP) )
249  {
250  mxColumnSep.set(
251  new XMLTextColumnSepContext_Impl( GetImport(), nElement, xAttrList ));
252 
253  return mxColumnSep;
254  }
255  XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
256  return nullptr;
257 }
258 
259 void XMLTextColumnsContext::endFastElement(sal_Int32 nElement )
260 {
261  Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
262  if( !xFactory.is() )
263  return;
264 
265  Reference<XInterface> xIfc = xFactory->createInstance("com.sun.star.text.TextColumns");
266  if( !xIfc.is() )
267  return;
268 
269  Reference< XTextColumns > xColumns( xIfc, UNO_QUERY );
270  if ( 0 == nCount )
271  {
272  // zero columns = no columns -> 1 column
273  xColumns->setColumnCount( 1 );
274  }
275  else if( !bAutomatic && pColumns &&
276  pColumns->size() == static_cast<sal_uInt16>(nCount) )
277  {
278  // if we have column descriptions, one per column, and we don't use
279  // automatic width, then set the column widths
280 
281  sal_Int32 nRelWidth = 0;
282  sal_uInt16 nColumnsWithWidth = 0;
283  sal_Int16 i;
284 
285  for( i = 0; i < nCount; i++ )
286  {
287  const TextColumn& rColumn =
288  (*pColumns)[static_cast<sal_uInt16>(i)]->getTextColumn();
289  if( rColumn.Width > 0 )
290  {
291  nRelWidth += rColumn.Width;
292  nColumnsWithWidth++;
293  }
294  }
295  if( nColumnsWithWidth < nCount )
296  {
297  sal_Int32 nColWidth = 0==nRelWidth
298  ? USHRT_MAX / nCount
299  : nRelWidth / nColumnsWithWidth;
300 
301  for( i=0; i < nCount; i++ )
302  {
303  TextColumn& rColumn =
304  (*pColumns)[static_cast<sal_uInt16>(i)]->getTextColumn();
305  if( rColumn.Width == 0 )
306  {
307  rColumn.Width = nColWidth;
308  nRelWidth += rColumn.Width;
309  if( 0 == --nColumnsWithWidth )
310  break;
311  }
312  }
313  }
314 
315  Sequence< TextColumn > aColumns( static_cast<sal_Int32>(nCount) );
316  TextColumn *pTextColumns = aColumns.getArray();
317  for( i=0; i < nCount; i++ )
318  *pTextColumns++ = (*pColumns)[static_cast<sal_uInt16>(i)]->getTextColumn();
319 
320  xColumns->setColumns( aColumns );
321  }
322  else
323  {
324  // only set column count (and let the columns be distributed
325  // automatically)
326 
327  xColumns->setColumnCount( nCount );
328  }
329 
330  Reference < XPropertySet > xPropSet( xColumns, UNO_QUERY );
331  if( xPropSet.is() )
332  {
333  bool bOn = mxColumnSep != nullptr;
334 
335  xPropSet->setPropertyValue( gsSeparatorLineIsOn, Any(bOn) );
336 
337  if( mxColumnSep.is() )
338  {
339  if( mxColumnSep->GetWidth() )
340  {
341  xPropSet->setPropertyValue( gsSeparatorLineWidth, Any(mxColumnSep->GetWidth()) );
342  }
343  if( mxColumnSep->GetHeight() )
344  {
345  xPropSet->setPropertyValue( gsSeparatorLineRelativeHeight,
346  Any(mxColumnSep->GetHeight()) );
347  }
348  if ( mxColumnSep->GetStyle() )
349  {
350  xPropSet->setPropertyValue( gsSeparatorLineStyle, Any(mxColumnSep->GetStyle()) );
351  }
352 
353  xPropSet->setPropertyValue( gsSeparatorLineColor, Any(mxColumnSep->GetColor()) );
354 
355  xPropSet->setPropertyValue( gsSeparatorLineVerticalAlignment, Any(mxColumnSep->GetVertAlign()) );
356  }
357 
358  // handle 'automatic columns': column distance
359  if( bAutomatic )
360  {
361  xPropSet->setPropertyValue( gsAutomaticDistance, Any(nAutomaticDistance) );
362  }
363  }
364 
365  aProp.maValue <<= xColumns;
366 
367  SetInsert( true );
369 
370 }
371 
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
signed char sal_Int8
constexpr OUStringLiteral gsSeparatorLineVerticalAlignment(u"SeparatorLineVerticalAlignment")
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
constexpr OUStringLiteral gsSeparatorLineStyle(u"SeparatorLineStyle")
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()
int nCount
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:113
XMLTextColumnsContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, const XMLPropertyState &rProp,::std::vector< XMLPropertyState > &rProps)
VerticalAlignment GetVertAlign() const
XMLTextColumnSepContext_Impl(SvXMLImport &rImport, sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList > &xAttrList)
int i
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
constexpr OUStringLiteral gsSeparatorLineRelativeHeight(u"SeparatorLineRelativeHeight")
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
SvXMLEnumMapEntry< VerticalAlignment > const pXML_Sep_Align_Enum[]
static bool convertPercent(sal_Int32 &rValue, std::u16string_view rString)
constexpr OUStringLiteral gsSeparatorLineColor(u"SeparatorLineColor")
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:39
constexpr OUStringLiteral gsSeparatorLineIsOn(u"SeparatorLineIsOn")
css::uno::Any maValue
Definition: maptype.hxx:125
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
XMLTextColumnContext_Impl(SvXMLImport &rImport, sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList > &xAttrList)
constexpr OUStringLiteral gsSeparatorLineWidth(u"SeparatorLineWidth")
Handling of tokens in XML:
constexpr OUStringLiteral gsAutomaticDistance(u"AutomaticDistance")
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:122
static bool convertEnum(EnumT &rEnum, std::u16string_view 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:145
const SvXMLUnitConverter & GetMM100UnitConverter() const
Definition: xmlimp.hxx:400
SvXMLEnumMapEntry< sal_Int8 > const pXML_Sep_Style_Enum[]
Reference< XColumn > xColumn
Reference< XSingleServiceFactory > xFactory
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:119
sal_uInt16 nPos
std::unique_ptr< XMLTextColumnsArray_Impl > pColumns
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)