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 "lspachdl.hxx"
21 #include <xmloff/xmltoken.hxx>
22 #include <xmloff/xmluconv.hxx>
23 #include <sax/tools/converter.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/style/LineSpacing.hpp>
27 #include <com/sun/star/style/LineSpacingMode.hpp>
28 
29 using namespace ::com::sun::star;
30 using ::xmloff::token::IsXMLToken;
31 using ::xmloff::token::XML_CASEMAP_NORMAL;
32 
33 
34 // class XMLEscapementPropHdl
35 
36 
~XMLLineHeightHdl()37 XMLLineHeightHdl::~XMLLineHeightHdl()
38 {
39     // nothing to do
40 }
41 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter & rUnitConverter) const42 bool XMLLineHeightHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
43 {
44     style::LineSpacing aLSp;
45     sal_Int32 nTemp = 0;
46 
47     if( -1 != rStrImpValue.indexOf( '%' ) )
48     {
49         aLSp.Mode = style::LineSpacingMode::PROP;
50         if (!::sax::Converter::convertPercent( nTemp, rStrImpValue ))
51             return false;
52         aLSp.Height = sal::static_int_cast< sal_Int16 >(nTemp);
53     }
54     else if( IsXMLToken( rStrImpValue, XML_CASEMAP_NORMAL) )
55     {
56         aLSp.Mode = style::LineSpacingMode::PROP;
57         aLSp.Height = 100;
58     }
59     else
60     {
61         aLSp.Mode = style::LineSpacingMode::FIX;
62         if (!rUnitConverter.convertMeasureToCore(
63                     nTemp, rStrImpValue, 0x0000, 0xffff))
64             return false;
65         aLSp.Height = sal::static_int_cast< sal_Int16 >(nTemp);
66     }
67 
68     rValue <<= aLSp;
69     return true;
70 }
71 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter & rUnitConverter) const72 bool XMLLineHeightHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
73 {
74     OUStringBuffer aOut;
75 
76     style::LineSpacing aLSp;
77     if(!(rValue >>= aLSp))
78         return false;
79 
80     if( style::LineSpacingMode::PROP != aLSp.Mode && style::LineSpacingMode::FIX  != aLSp.Mode )
81         return false;
82 
83     if( style::LineSpacingMode::PROP == aLSp.Mode )
84     {
85         ::sax::Converter::convertPercent( aOut, aLSp.Height );
86     }
87     else
88     {
89         rUnitConverter.convertMeasureToXML( aOut, aLSp.Height );
90     }
91 
92     rStrExpValue = aOut.makeStringAndClear();
93     return !rStrExpValue.isEmpty();
94 }
95 
96 
97 // class XMLLineHeightAtLeastHdl
98 
99 
~XMLLineHeightAtLeastHdl()100 XMLLineHeightAtLeastHdl::~XMLLineHeightAtLeastHdl()
101 {
102     // nothing to do
103 }
104 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter & rUnitConverter) const105 bool XMLLineHeightAtLeastHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
106 {
107     style::LineSpacing aLSp;
108 
109     sal_Int32 nTemp;
110     aLSp.Mode = style::LineSpacingMode::MINIMUM;
111     if (!rUnitConverter.convertMeasureToCore( nTemp, rStrImpValue, 0, 0xffff))
112         return false;
113     aLSp.Height = sal::static_int_cast< sal_Int16 >(nTemp);
114 
115     rValue <<= aLSp;
116     return true;
117 }
118 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter & rUnitConverter) const119 bool XMLLineHeightAtLeastHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
120 {
121     OUStringBuffer aOut;
122 
123     style::LineSpacing aLSp;
124     if(!(rValue >>= aLSp))
125         return false;
126 
127     if( style::LineSpacingMode::MINIMUM != aLSp.Mode )
128         return false;
129 
130     rUnitConverter.convertMeasureToXML( aOut, aLSp.Height );
131 
132     rStrExpValue = aOut.makeStringAndClear();
133     return !rStrExpValue.isEmpty();
134 }
135 
136 
137 // class XMLLineSpacingHdl
138 
139 
~XMLLineSpacingHdl()140 XMLLineSpacingHdl::~XMLLineSpacingHdl()
141 {
142     // nothing to do
143 }
144 
importXML(const OUString & rStrImpValue,uno::Any & rValue,const SvXMLUnitConverter & rUnitConverter) const145 bool XMLLineSpacingHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
146 {
147     style::LineSpacing aLSp;
148     sal_Int32 nTemp;
149 
150     aLSp.Mode = style::LineSpacingMode::LEADING;
151     if (!rUnitConverter.convertMeasureToCore( nTemp, rStrImpValue, 0, 0xffff))
152         return false;
153     aLSp.Height = sal::static_int_cast< sal_Int16 >(nTemp);
154 
155     rValue <<= aLSp;
156     return true;
157 }
158 
exportXML(OUString & rStrExpValue,const uno::Any & rValue,const SvXMLUnitConverter & rUnitConverter) const159 bool XMLLineSpacingHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
160 {
161     OUStringBuffer aOut;
162 
163     style::LineSpacing aLSp;
164     if(!(rValue >>= aLSp))
165         return false;
166 
167     if( style::LineSpacingMode::LEADING != aLSp.Mode )
168         return false;
169 
170     rUnitConverter.convertMeasureToXML( aOut, aLSp.Height );
171 
172     rStrExpValue = aOut.makeStringAndClear();
173     return !rStrExpValue.isEmpty();
174 }
175 
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
177