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>
30#include <xmloff/xmltkmap.hxx>
31#include <xmloff/xmluconv.hxx>
34#include <xmloff/xmlimp.hxx>
35#include <xmloff/xmltoken.hxx>
36#include <xmloff/xmlement.hxx>
38
39using namespace ::com::sun::star;
40using namespace ::com::sun::star::uno;
41using namespace ::com::sun::star::lang;
42using namespace ::com::sun::star::text;
43using namespace ::com::sun::star::style;
44using namespace ::com::sun::star::beans;
45using namespace ::xmloff::token;
46
48{
49 { XML_NONE, 0 },
50 { XML_SOLID, 1 },
51 { XML_DOTTED, 2 },
52 { XML_DASHED, 3 },
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
68public:
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;
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
133public:
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 {
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;
174 ::sax::Converter::convertColor( nColor, aIter.toView() );
175 break;
179 break;
183 break;
184 default:
185 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
186 }
187 }
188}
189
190constexpr OUStringLiteral gsSeparatorLineIsOn(u"SeparatorLineIsOn");
191constexpr OUStringLiteral gsSeparatorLineWidth(u"SeparatorLineWidth");
192constexpr OUStringLiteral gsSeparatorLineColor(u"SeparatorLineColor");
193constexpr OUStringLiteral gsSeparatorLineRelativeHeight(u"SeparatorLineRelativeHeight");
194constexpr OUStringLiteral gsSeparatorLineVerticalAlignment(u"SeparatorLineVerticalAlignment");
195constexpr OUStringLiteral gsAutomaticDistance(u"AutomaticDistance");
196constexpr 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 {
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 {
221 bAutomatic = GetImport().GetMM100UnitConverter().
222 convertMeasureToCore( nAutomaticDistance, aIter.toView() );
223 break;
224 }
225 default:
226 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
227 }
228 }
229}
230
231css::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 maColumns.push_back( xColumn );
242
243 return xColumn;
244 }
245 else if( nElement == XML_ELEMENT(STYLE, XML_COLUMN_SEP) )
246 {
247 mxColumnSep.set(
248 new XMLTextColumnSepContext_Impl( GetImport(), nElement, xAttrList ));
249
250 return mxColumnSep;
251 }
252 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
253 return nullptr;
254}
255
257{
258 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),UNO_QUERY);
259 if( !xFactory.is() )
260 return;
261
262 Reference<XInterface> xIfc = xFactory->createInstance("com.sun.star.text.TextColumns");
263 if( !xIfc.is() )
264 return;
265
266 Reference< XTextColumns > xColumns( xIfc, UNO_QUERY );
267 if ( 0 == nCount )
268 {
269 // zero columns = no columns -> 1 column
270 xColumns->setColumnCount( 1 );
271 }
272 else if( !bAutomatic &&
273 maColumns.size() == static_cast<sal_uInt16>(nCount) )
274 {
275 // if we have column descriptions, one per column, and we don't use
276 // automatic width, then set the column widths
277
278 sal_Int32 nRelWidth = 0;
279 sal_uInt16 nColumnsWithWidth = 0;
280 sal_Int16 i;
281
282 for( i = 0; i < nCount; i++ )
283 {
284 const TextColumn& rColumn =
285 maColumns[static_cast<sal_uInt16>(i)]->getTextColumn();
286 if( rColumn.Width > 0 )
287 {
288 nRelWidth += rColumn.Width;
289 nColumnsWithWidth++;
290 }
291 }
292 if( nColumnsWithWidth < nCount )
293 {
294 sal_Int32 nColWidth = 0==nRelWidth
295 ? USHRT_MAX / nCount
296 : nRelWidth / nColumnsWithWidth;
297
298 for( i=0; i < nCount; i++ )
299 {
300 TextColumn& rColumn =
301 maColumns[static_cast<sal_uInt16>(i)]->getTextColumn();
302 if( rColumn.Width == 0 )
303 {
304 rColumn.Width = nColWidth;
305 nRelWidth += rColumn.Width;
306 if( 0 == --nColumnsWithWidth )
307 break;
308 }
309 }
310 }
311
312 Sequence< TextColumn > aColumns( static_cast<sal_Int32>(nCount) );
313 TextColumn *pTextColumns = aColumns.getArray();
314 for( i=0; i < nCount; i++ )
315 *pTextColumns++ = maColumns[static_cast<sal_uInt16>(i)]->getTextColumn();
316
317 xColumns->setColumns( aColumns );
318 }
319 else
320 {
321 // only set column count (and let the columns be distributed
322 // automatically)
323
324 xColumns->setColumnCount( nCount );
325 }
326
327 Reference < XPropertySet > xPropSet( xColumns, UNO_QUERY );
328 if( xPropSet.is() )
329 {
330 bool bOn = mxColumnSep != nullptr;
331
332 xPropSet->setPropertyValue( gsSeparatorLineIsOn, Any(bOn) );
333
334 if( mxColumnSep.is() )
335 {
336 if( mxColumnSep->GetWidth() )
337 {
338 xPropSet->setPropertyValue( gsSeparatorLineWidth, Any(mxColumnSep->GetWidth()) );
339 }
340 if( mxColumnSep->GetHeight() )
341 {
342 xPropSet->setPropertyValue( gsSeparatorLineRelativeHeight,
343 Any(mxColumnSep->GetHeight()) );
344 }
345 if ( mxColumnSep->GetStyle() )
346 {
347 xPropSet->setPropertyValue( gsSeparatorLineStyle, Any(mxColumnSep->GetStyle()) );
348 }
349
350 xPropSet->setPropertyValue( gsSeparatorLineColor, Any(mxColumnSep->GetColor()) );
351
352 xPropSet->setPropertyValue( gsSeparatorLineVerticalAlignment, Any(mxColumnSep->GetVertAlign()) );
353 }
354
355 // handle 'automatic columns': column distance
356 if( bAutomatic )
357 {
358 xPropSet->setPropertyValue( gsAutomaticDistance, Any(nAutomaticDistance) );
359 }
360 }
361
362 aProp.maValue <<= xColumns;
363
364 SetInsert( true );
366
367}
368
369/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral gsSeparatorLineVerticalAlignment(u"SeparatorLineVerticalAlignment")
constexpr OUStringLiteral gsSeparatorLineStyle(u"SeparatorLineStyle")
SvXMLEnumMapEntry< sal_Int8 > const pXML_Sep_Style_Enum[]
constexpr OUStringLiteral gsSeparatorLineWidth(u"SeparatorLineWidth")
constexpr OUStringLiteral gsSeparatorLineColor(u"SeparatorLineColor")
constexpr OUStringLiteral gsSeparatorLineIsOn(u"SeparatorLineIsOn")
constexpr OUStringLiteral gsSeparatorLineRelativeHeight(u"SeparatorLineRelativeHeight")
constexpr OUStringLiteral gsAutomaticDistance(u"AutomaticDistance")
SvXMLEnumMapEntry< VerticalAlignment > const pXML_Sep_Align_Enum[]
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
friend class SvXMLImport
Definition: xmlictxt.hxx:49
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
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)
text::TextColumn & getTextColumn()
VerticalAlignment GetVertAlign() const
XMLTextColumnSepContext_Impl(SvXMLImport &rImport, sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList > &xAttrList)
XMLTextColumnsContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, const XMLPropertyState &rProp, ::std::vector< XMLPropertyState > &rProps)
std::vector< rtl::Reference< XMLTextColumnContext_Impl > > maColumns
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 ...
rtl::Reference< XMLTextColumnSepContext_Impl > mxColumnSep
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
static bool convertPercent(sal_Int32 &rValue, std::u16string_view rString)
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
int nCount
Reference< XSingleServiceFactory > xFactory
Reference< XColumn > xColumn
sal_uInt16 nPos
int i
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
Smart struct to transport an Any with an index to the appropriate property-name.
Definition: maptype.hxx:140
css::uno::Any maValue
Definition: maptype.hxx:142
signed char sal_Int8
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:120
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97