xref: /AOO41X/main/oox/source/drawingml/drawingmltypes.cxx (revision ca5ec2004b000a7d9aaa8381be8ac2853e3b1dc7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/drawingml/drawingmltypes.hxx"
25 #include <com/sun/star/awt/FontUnderline.hpp>
26 #include <com/sun/star/awt/FontStrikeout.hpp>
27 #include <com/sun/star/style/CaseMap.hpp>
28 #include <com/sun/star/style/ParagraphAdjust.hpp>
29 #include <sax/tools/converter.hxx>
30 #include "oox/token/tokens.hxx"
31 
32 using ::rtl::OUString;
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::xml::sax::XFastAttributeList;
35 using namespace ::com::sun::star::awt;
36 using namespace ::com::sun::star::drawing;
37 using namespace ::com::sun::star::geometry;
38 using namespace ::com::sun::star::style;
39 
40 namespace oox {
41 namespace drawingml {
42 
43 // ============================================================================
44 
45 /** converts an emu string into 1/100th mmm but constrain as per ST_TextMargin
46  * see 5.1.12.73
47  */
GetTextMargin(const OUString & sValue)48 sal_Int32 GetTextMargin( const OUString& sValue )
49 {
50     sal_Int32 nRet = 0;
51     if( !::sax::Converter::convertNumber( nRet, sValue ) )
52         nRet = 0;
53     else if( nRet < 0 )
54         nRet = 0;
55     else if( nRet > 51206400 )
56         nRet = 51206400;
57 
58     nRet /= 360;
59     return nRet;
60 }
61 
62 /** converts EMUs into 1/100th mmm */
GetCoordinate(sal_Int32 nValue)63 sal_Int32 GetCoordinate( sal_Int32 nValue )
64 {
65     return (nValue + 180) / 360;
66 }
67 
68 /** converts an emu string into 1/100th mmm */
GetCoordinate(const OUString & sValue)69 sal_Int32 GetCoordinate( const OUString& sValue )
70 {
71     sal_Int32 nRet = 0;
72     if( !::sax::Converter::convertNumber( nRet, sValue ) )
73         nRet = 0;
74     return GetCoordinate( nRet );
75 }
76 
77 /** converts a ST_Percentage % string into 1/1000th of % */
GetPercent(const OUString & sValue)78 sal_Int32 GetPercent( const OUString& sValue )
79 {
80     sal_Int32 nRet = 0;
81     if( !::sax::Converter::convertNumber( nRet, sValue ) )
82         nRet = 0;
83 
84     return nRet;
85 }
86 
GetPositiveFixedPercentage(const OUString & sValue)87 double GetPositiveFixedPercentage( const OUString& sValue )
88 {
89     double fPercent = sValue.toFloat() / 100000.;
90     return fPercent;
91 }
92 
93 // --------------------------------------------------------------------
94 
95 /** converts the attributes from an CT_Point2D into an awt Point with 1/100thmm */
GetPoint2D(const Reference<XFastAttributeList> & xAttribs)96 Point GetPoint2D( const Reference< XFastAttributeList >& xAttribs )
97 {
98     return Point( GetCoordinate( xAttribs->getOptionalValue( XML_x ) ), GetCoordinate( xAttribs->getOptionalValue( XML_y ) ) );
99 }
100 
101 /** converts the attributes from an CT_TLPoint into an awt Point with 1/1000% */
GetPointPercent(const Reference<XFastAttributeList> & xAttribs)102 Point GetPointPercent( const Reference< XFastAttributeList >& xAttribs )
103 {
104     return Point( GetPercent( xAttribs->getOptionalValue( XML_x ) ), GetCoordinate( xAttribs->getOptionalValue( XML_y ) ) );
105 }
106 
107 // --------------------------------------------------------------------
108 
109 /** converts the ST_TextFontSize to point */
GetTextSize(const OUString & sValue)110 float GetTextSize( const OUString& sValue )
111 {
112     float fRet = 0;
113     sal_Int32 nRet;
114     if( ::sax::Converter::convertNumber( nRet, sValue ) )
115         fRet = static_cast< float >( static_cast< double >( nRet ) / 100.0 );
116     return fRet;
117 }
118 
119 
120 /** converts the ST_TextSpacingPoint to 1/100mm */
GetTextSpacingPoint(const OUString & sValue)121 sal_Int32 GetTextSpacingPoint( const OUString& sValue )
122 {
123     sal_Int32 nRet;
124     if( ::sax::Converter::convertNumber( nRet, sValue ) )
125         nRet = GetTextSpacingPoint( nRet );
126     return nRet;
127 }
128 
GetTextSpacingPoint(const sal_Int32 nValue)129 sal_Int32 GetTextSpacingPoint( const sal_Int32 nValue )
130 {
131     return ( nValue * 254 + 360 ) / 720;
132 }
133 
GetTextVerticalAdjust(sal_Int32 nToken)134 TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
135 {
136     TextVerticalAdjust rVal = TextVerticalAdjust_TOP;
137 
138     switch( nToken ) {
139     case XML_b:
140         rVal = TextVerticalAdjust_BOTTOM;
141         break;
142     case XML_ctr:
143         rVal = TextVerticalAdjust_CENTER;
144         break;
145     }
146 
147     return rVal;
148 }
149 
GetFontHeight(sal_Int32 nHeight)150 float GetFontHeight( sal_Int32 nHeight )
151 {
152     // convert 1/100 points to points
153     return static_cast< float >( nHeight / 100.0 );
154 }
155 
GetFontUnderline(sal_Int32 nToken)156 sal_Int16 GetFontUnderline( sal_Int32 nToken )
157 {
158     switch( nToken )
159     {
160         case XML_none:              return FontUnderline::NONE;
161         case XML_dash:              return FontUnderline::DASH;
162         case XML_dashHeavy:         return FontUnderline::BOLDDASH;
163         case XML_dashLong:          return FontUnderline::LONGDASH;
164         case XML_dashLongHeavy:     return FontUnderline::BOLDLONGDASH;
165         case XML_dbl:               return FontUnderline::DOUBLE;
166         case XML_dotDash:           return FontUnderline::DASHDOT;
167         case XML_dotDashHeavy:      return FontUnderline::BOLDDASHDOT;
168         case XML_dotDotDash:        return FontUnderline::DASHDOTDOT;
169         case XML_dotDotDashHeavy:   return FontUnderline::BOLDDASHDOTDOT;
170         case XML_dotted:            return FontUnderline::DOTTED;
171         case XML_dottedHeavy:       return FontUnderline::BOLDDOTTED;
172         case XML_heavy:             return FontUnderline::BOLD;
173         case XML_sng:               return FontUnderline::SINGLE;
174         case XML_wavy:              return FontUnderline::WAVE;
175         case XML_wavyDbl:           return FontUnderline::DOUBLEWAVE;
176         case XML_wavyHeavy:         return FontUnderline::BOLDWAVE;
177 //        case XML_words:             // TODO
178     }
179     return FontUnderline::DONTKNOW;
180 }
181 
GetFontStrikeout(sal_Int32 nToken)182 sal_Int16 GetFontStrikeout( sal_Int32 nToken )
183 {
184     switch( nToken )
185     {
186         case XML_dblStrike: return FontStrikeout::DOUBLE;
187         case XML_noStrike:  return FontStrikeout::NONE;
188         case XML_sngStrike: return FontStrikeout::SINGLE;
189     }
190     return FontStrikeout::DONTKNOW;
191 }
192 
GetCaseMap(sal_Int32 nToken)193 sal_Int16 GetCaseMap( sal_Int32 nToken )
194 {
195     switch( nToken )
196     {
197         case XML_all:   return CaseMap::UPPERCASE;
198         case XML_small: return CaseMap::SMALLCAPS;
199     }
200     return CaseMap::NONE;
201 }
202 
203 /** converts a paragraph align to a ParaAdjust */
GetParaAdjust(sal_Int32 nAlign)204 sal_Int16 GetParaAdjust( sal_Int32 nAlign )
205 {
206     sal_Int16 nEnum;
207     switch( nAlign )
208     {
209     case XML_ctr:
210         nEnum = ParagraphAdjust_CENTER;
211         break;
212     case XML_just:
213     case XML_justLow:
214         nEnum = ParagraphAdjust_BLOCK;
215         break;
216     case XML_r:
217         nEnum = ParagraphAdjust_RIGHT;
218         break;
219     case XML_thaiDist:
220     case XML_dist:
221         nEnum = ParagraphAdjust_STRETCH;
222         break;
223     case XML_l:
224     default:
225         nEnum = ParagraphAdjust_LEFT;
226         break;
227     }
228     return nEnum;
229 }
230 
231 
GetTabAlign(sal_Int32 aToken)232 TabAlign GetTabAlign( sal_Int32 aToken )
233 {
234     TabAlign nEnum;
235     switch( aToken )
236     {
237     case XML_ctr:
238         nEnum = TabAlign_CENTER;
239         break;
240     case XML_dec:
241         nEnum = TabAlign_DECIMAL;
242         break;
243     case XML_l:
244         nEnum = TabAlign_LEFT;
245         break;
246     case XML_r:
247         nEnum = TabAlign_RIGHT;
248         break;
249     default:
250         nEnum = TabAlign_DEFAULT;
251         break;
252     }
253     return nEnum;
254 }
255 
256 // --------------------------------------------------------------------
257 
258 /** converts the attributes from a CT_RelativeRect to an IntegerRectangle2D */
GetRelativeRect(const Reference<XFastAttributeList> & xAttribs)259 IntegerRectangle2D GetRelativeRect( const Reference< XFastAttributeList >& xAttribs )
260 {
261     IntegerRectangle2D r;
262 
263     r.X1 = xAttribs->getOptionalValue( XML_l ).toInt32();
264     r.Y1 = xAttribs->getOptionalValue( XML_t ).toInt32();
265     r.X2 = xAttribs->getOptionalValue( XML_r ).toInt32();
266     r.Y2 = xAttribs->getOptionalValue( XML_b ).toInt32();
267 
268     return r;
269 }
270 
271 // ============================================================================
272 
273 /** converts the attributes from an CT_Size2D into an awt Size with 1/100thmm */
GetSize2D(const Reference<XFastAttributeList> & xAttribs)274 Size GetSize2D( const Reference< XFastAttributeList >& xAttribs )
275 {
276     return Size( GetCoordinate( xAttribs->getOptionalValue( XML_cx ) ), GetCoordinate( xAttribs->getOptionalValue( XML_cy ) ) );
277 }
278 
GetIndexRange(const Reference<XFastAttributeList> & xAttributes)279 IndexRange GetIndexRange( const Reference< XFastAttributeList >& xAttributes )
280 {
281     IndexRange range;
282     range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
283     range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
284     return range;
285 }
286 
287 // ============================================================================
288 
289 } // namespace drawingml
290 } // namespace oox
291 
292