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 <com/sun/star/style/TabAlign.hpp>
21 #include <sal/log.hxx>
22 #include <xmloff/xmltkmap.hxx>
23 #include <xmloff/namespacemap.hxx>
24 #include <xmloff/xmlnamespace.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <com/sun/star/style/TabStop.hpp>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmluconv.hxx>
29 #include <xmltabi.hxx>
30 
31 
32 using namespace ::com::sun::star;
33 using namespace ::xmloff::token;
34 
35 class SvxXMLTabStopContext_Impl : public SvXMLImportContext
36 {
37 private:
38      style::TabStop aTabStop;
39 
40 public:
41 
42     SvxXMLTabStopContext_Impl( SvXMLImport& rImport, sal_Int32 nElement,
43                                const uno::Reference< xml::sax::XFastAttributeList > & xAttrList );
44 
getTabStop() const45     const style::TabStop& getTabStop() const { return aTabStop; }
46 };
47 
48 
SvxXMLTabStopContext_Impl(SvXMLImport & rImport,sal_Int32,const uno::Reference<xml::sax::XFastAttributeList> & xAttrList)49 SvxXMLTabStopContext_Impl::SvxXMLTabStopContext_Impl(
50                                SvXMLImport& rImport, sal_Int32 /*nElement*/,
51                                const uno::Reference< xml::sax::XFastAttributeList > & xAttrList )
52 : SvXMLImportContext( rImport )
53 {
54     aTabStop.Position = 0;
55     aTabStop.Alignment = style::TabAlign_LEFT;
56     aTabStop.DecimalChar = ',';
57     aTabStop.FillChar = ' ';
58     sal_Unicode cTextFillChar = 0;
59 
60     for (auto &aIter : sax_fastparser::castToFastAttributeList(xAttrList))
61     {
62         sal_Int32 nVal;
63         switch( aIter.getToken() )
64         {
65         case XML_ELEMENT(STYLE, XML_POSITION):
66             if (GetImport().GetMM100UnitConverter().convertMeasureToCore(
67                     nVal, aIter.toView()))
68             {
69                 aTabStop.Position = nVal;
70             }
71             break;
72         case XML_ELEMENT(STYLE, XML_TYPE):
73             if( IsXMLToken( aIter, XML_LEFT ) )
74             {
75                 aTabStop.Alignment = style::TabAlign_LEFT;
76             }
77             else if( IsXMLToken( aIter, XML_RIGHT ) )
78             {
79                 aTabStop.Alignment = style::TabAlign_RIGHT;
80             }
81             else if( IsXMLToken( aIter, XML_CENTER ) )
82             {
83                 aTabStop.Alignment = style::TabAlign_CENTER;
84             }
85             else if( IsXMLToken( aIter, XML_CHAR ) )
86             {
87                 aTabStop.Alignment = style::TabAlign_DECIMAL;
88             }
89             else if( IsXMLToken( aIter, XML_DEFAULT ) )
90             {
91                 aTabStop.Alignment = style::TabAlign_DEFAULT;
92             }
93             break;
94         case XML_ELEMENT(STYLE, XML_CHAR):
95             if( !aIter.isEmpty() )
96                 aTabStop.DecimalChar = aIter.toString()[0];
97             break;
98         case XML_ELEMENT(STYLE, XML_LEADER_STYLE):
99             if( IsXMLToken( aIter, XML_NONE ) )
100                 aTabStop.FillChar = ' ';
101             else if( IsXMLToken( aIter, XML_DOTTED ) )
102                 aTabStop.FillChar = '.';
103             else
104                 aTabStop.FillChar = '_';
105             break;
106         case XML_ELEMENT(STYLE, XML_LEADER_TEXT):
107             if( !aIter.isEmpty() )
108                 cTextFillChar = aIter.toString()[0];
109             break;
110         default:
111             XMLOFF_WARN_UNKNOWN("xmloff", aIter);
112         }
113     }
114 
115     if( cTextFillChar != 0 && aTabStop.FillChar != ' ' )
116         aTabStop.FillChar = cTextFillChar;
117 }
118 
119 
SvxXMLTabStopImportContext(SvXMLImport & rImport,sal_Int32 nElement,const XMLPropertyState & rProp,::std::vector<XMLPropertyState> & rProps)120 SvxXMLTabStopImportContext::SvxXMLTabStopImportContext(
121                                 SvXMLImport& rImport, sal_Int32 nElement,
122                                 const XMLPropertyState& rProp,
123                                  ::std::vector< XMLPropertyState > &rProps )
124 : XMLElementPropertyContext( rImport, nElement, rProp, rProps )
125 {
126 }
127 
createFastChildContext(sal_Int32 nElement,const css::uno::Reference<css::xml::sax::XFastAttributeList> & xAttrList)128 css::uno::Reference< css::xml::sax::XFastContextHandler > SvxXMLTabStopImportContext::createFastChildContext(
129     sal_Int32 nElement,
130     const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
131 {
132     if( nElement == XML_ELEMENT(STYLE, XML_TAB_STOP) )
133     {
134         // create new tabstop import context
135         const rtl::Reference<SvxXMLTabStopContext_Impl> xTabStopContext{
136             new SvxXMLTabStopContext_Impl( GetImport(), nElement, xAttrList )};
137 
138         // add new tabstop to array of tabstops
139         if( !mpTabStops )
140             mpTabStops = std::make_unique<SvxXMLTabStopArray_Impl>();
141 
142         mpTabStops->push_back( xTabStopContext );
143 
144         return xTabStopContext;
145     }
146     else
147         XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
148 
149     return nullptr;
150 }
151 
endFastElement(sal_Int32 nElement)152 void SvxXMLTabStopImportContext::endFastElement(sal_Int32 nElement)
153 {
154     sal_uInt16 nCount = mpTabStops ? mpTabStops->size() : 0;
155     uno::Sequence< style::TabStop> aSeq( nCount );
156 
157     if( mpTabStops )
158     {
159         sal_uInt16 nNewCount = 0;
160 
161         style::TabStop* pTabStops = aSeq.getArray();
162         for( sal_uInt16 i=0; i < nCount; i++ )
163         {
164             SvxXMLTabStopContext_Impl *pTabStopContext = (*mpTabStops)[i].get();
165             const style::TabStop& rTabStop = pTabStopContext->getTabStop();
166             bool bDflt = style::TabAlign_DEFAULT == rTabStop.Alignment;
167             if( !bDflt || 0==i )
168             {
169                 *pTabStops++ = pTabStopContext->getTabStop();
170                 nNewCount++;
171             }
172             if( bDflt && 0==i )
173                 break;
174         }
175 
176         if( nCount != nNewCount )
177             aSeq.realloc( nNewCount );
178     }
179     aProp.maValue <<= aSeq;
180 
181     SetInsert( true );
182     XMLElementPropertyContext::endFastElement(nElement);
183 }
184 
185 
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
187