summaryrefslogtreecommitdiffstats
path: root/oox/source/drawingml/drawingmltypes.cxx
blob: fc2f28d902b77a6f25a6e5b371f824455c855903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <oox/drawingml/drawingmltypes.hxx>
#include <com/sun/star/awt/FontUnderline.hpp>
#include <com/sun/star/awt/FontStrikeout.hpp>
#include <com/sun/star/drawing/Hatch.hpp>
#include <com/sun/star/style/CaseMap.hpp>
#include <com/sun/star/xml/sax/XFastAttributeList.hpp>

#include <o3tl/string_view.hxx>
#include <osl/diagnose.h>
#include <sax/tools/converter.hxx>
#include <oox/token/tokens.hxx>

using ::com::sun::star::uno::Reference;
using ::com::sun::star::xml::sax::XFastAttributeList;
using namespace ::com::sun::star;
using namespace ::com::sun::star::drawing;
using namespace ::com::sun::star::geometry;
using namespace ::com::sun::star::style;

namespace oox::drawingml {

/** converts EMUs into 1/100th mmm */
sal_Int32 GetCoordinate( sal_Int32 nValue )
{
    return o3tl::convert(nValue, o3tl::Length::emu, o3tl::Length::mm100);
}

/** converts an emu string into 1/100th mmm */
sal_Int32 GetCoordinate( std::u16string_view sValue )
{
    sal_Int32 nRet = 0;
    if( !::sax::Converter::convertNumber( nRet, sValue ) )
        nRet = 0;
    return GetCoordinate( nRet );
}

/** converts 1/100mm to EMU */
sal_Int32 GetPointFromCoordinate( sal_Int32 nValue )
{
    return o3tl::convert(nValue, o3tl::Length::mm100, o3tl::Length::emu);
}

/** converts a ST_Percentage % string into 1/1000th of % */
sal_Int32 GetPercent( std::u16string_view sValue )
{
    sal_Int32 nRet = 0;
    if( !::sax::Converter::convertNumber( nRet, sValue ) )
        nRet = 0;

    return nRet;
}

double GetPositiveFixedPercentage( const OUString& sValue )
{
    double fPercent = sValue.toFloat() / 100000.;
    return fPercent;
}

/** converts the attributes from a CT_TLPoint into an awt Point with 1/1000% */
awt::Point GetPointPercent( const Reference< XFastAttributeList >& xAttribs )
{
    return awt::Point(GetPercent(xAttribs->getOptionalValue(XML_x)), GetPercent(xAttribs->getOptionalValue(XML_y)));
}

/** converts the ST_TextFontSize to point */
float GetTextSize( std::u16string_view sValue )
{
    float fRet = 0;
    sal_Int32 nRet;
    if( ::sax::Converter::convertNumber( nRet, sValue ) )
        fRet = static_cast< float >( static_cast< double >( nRet ) / 100.0 );
    return fRet;
}

/** converts the ST_TextSpacingPoint to 1/100mm */
sal_Int32 GetTextSpacingPoint( std::u16string_view sValue )
{
    sal_Int32 nRet;
    if( ::sax::Converter::convertNumber( nRet, sValue, (SAL_MIN_INT32 + 360) / 254, (SAL_MAX_INT32 - 360) / 254 ) )
        nRet = GetTextSpacingPoint( nRet );
    return nRet;
}

sal_Int32 GetTextSpacingPoint(sal_Int32 nValue)
{
    if (nValue > 0)
        nValue = (nValue * 254 + 360);
    else if (nValue < 0)
        nValue = (nValue * 254 - 360);
    return nValue / 720;
}

float GetFontHeight( sal_Int32 nHeight )
{
    // convert 1/100 points to points
    return static_cast< float >( nHeight / 100.0 );
}

sal_Int16 GetFontUnderline( sal_Int32 nToken )
{
    OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
    switch( nToken )
    {
        case XML_none:              return awt::FontUnderline::NONE;
        case XML_dash:              return awt::FontUnderline::DASH;
        case XML_dashHeavy:         return awt::FontUnderline::BOLDDASH;
        case XML_dashLong:          return awt::FontUnderline::LONGDASH;
        case XML_dashLongHeavy:     return awt::FontUnderline::BOLDLONGDASH;
        case XML_dbl:               return awt::FontUnderline::DOUBLE;
        case XML_dotDash:           return awt::FontUnderline::DASHDOT;
        case XML_dotDashHeavy:      return awt::FontUnderline::BOLDDASHDOT;
        case XML_dotDotDash:        return awt::FontUnderline::DASHDOTDOT;
        case XML_dotDotDashHeavy:   return awt::FontUnderline::BOLDDASHDOTDOT;
        case XML_dotted:            return awt::FontUnderline::DOTTED;
        case XML_dottedHeavy:       return awt::FontUnderline::BOLDDOTTED;
        case XML_heavy:             return awt::FontUnderline::BOLD;
        case XML_sng:               return awt::FontUnderline::SINGLE;
        case XML_wavy:              return awt::FontUnderline::WAVE;
        case XML_wavyDbl:           return awt::FontUnderline::DOUBLEWAVE;
        case XML_wavyHeavy:         return awt::FontUnderline::BOLDWAVE;
//        case XML_words:             // TODO
    }
    return awt::FontUnderline::DONTKNOW;
}

sal_Int16 GetFontStrikeout( sal_Int32 nToken )
{
    OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
    switch( nToken )
    {
        case XML_dblStrike: return awt::FontStrikeout::DOUBLE;
        case XML_noStrike:  return awt::FontStrikeout::NONE;
        case XML_sngStrike: return awt::FontStrikeout::SINGLE;
    }
    return awt::FontStrikeout::DONTKNOW;
}

sal_Int16 GetCaseMap( sal_Int32 nToken )
{
    switch( nToken )
    {
        case XML_all:   return CaseMap::UPPERCASE;
        case XML_small: return CaseMap::SMALLCAPS;
    }
    return CaseMap::NONE;
}

/** converts a paragraph align to a ParaAdjust */
ParagraphAdjust GetParaAdjust( sal_Int32 nAlign )
{
    OSL_ASSERT((nAlign & sal_Int32(0xFFFF0000))==0);
    ParagraphAdjust nEnum;
    switch( nAlign )
    {
    case XML_ctr:
        nEnum = ParagraphAdjust_CENTER;
        break;
    case XML_just:
    case XML_justLow:
        nEnum = ParagraphAdjust_BLOCK;
        break;
    case XML_r:
        nEnum = ParagraphAdjust_RIGHT;
        break;
    case XML_thaiDist:
    case XML_dist:
        nEnum = ParagraphAdjust_STRETCH;
        break;
    case XML_l:
    default:
        nEnum = ParagraphAdjust_LEFT;
        break;
    }
    return nEnum;
}

TextVerticalAdjust GetTextVerticalAdjust( sal_Int32 nToken )
{
    TextVerticalAdjust aVertAdjust;
    switch( nToken )
    {
    case XML_b:
        aVertAdjust = TextVerticalAdjust_BOTTOM;
        break;
    case XML_dist:
    case XML_just:
    case XML_ctr:
        aVertAdjust = TextVerticalAdjust_CENTER;
        break;
    case XML_t:
    default:
        aVertAdjust = TextVerticalAdjust_TOP;
        break;
    }
    return aVertAdjust;
}

const char* GetTextVerticalAdjust( TextVerticalAdjust eAdjust )
{
    const char* sVerticalAdjust = nullptr;
    switch( eAdjust )
    {
        case TextVerticalAdjust_BOTTOM:
            sVerticalAdjust = "b";
            break;
        case TextVerticalAdjust_CENTER:
            sVerticalAdjust = "ctr";
            break;
        case TextVerticalAdjust_TOP:
        default:
            sVerticalAdjust = "t";
            break;
    }
    return sVerticalAdjust;
}

TabAlign GetTabAlign( sal_Int32 aToken )
{
    OSL_ASSERT((aToken & sal_Int32(0xFFFF0000))==0);
    TabAlign nEnum;
    switch( aToken )
    {
    case XML_ctr:
        nEnum = TabAlign_CENTER;
        break;
    case XML_dec:
        nEnum = TabAlign_DECIMAL;
        break;
    case XML_l:
        nEnum = TabAlign_LEFT;
        break;
    case XML_r:
        nEnum = TabAlign_RIGHT;
        break;
    default:
        nEnum = TabAlign_DEFAULT;
        break;
    }
    return nEnum;
}

const char* GetHatchPattern( const drawing::Hatch& rHatch )
{
    const char* sPattern = nullptr;
    const sal_Int32 nAngle = rHatch.Angle > 1800 ? rHatch.Angle - 1800 : rHatch.Angle;
    // Angle ~ 0° (horizontal)
    if( (nAngle >= 0 && nAngle < 225) || nAngle >= 1575 )
    {
        switch( rHatch.Style )
        {
            case drawing::HatchStyle_SINGLE:
            {
                if( rHatch.Distance < 75 )
                    sPattern = "ltHorz";
                else
                    sPattern = "horz";

                break;
            }
            case drawing::HatchStyle_DOUBLE:
            case drawing::HatchStyle_TRIPLE:
            {
                if( rHatch.Distance < 75 )
                    sPattern = "smGrid";
                else
                    sPattern = "lgGrid";

                break;
            }
            default: break;
        }
    }
    // Angle ~ 45° (upward diagonal)
    else if( nAngle < 675 )
    {
        switch( rHatch.Style )
        {
            case drawing::HatchStyle_SINGLE:
            {
                if( rHatch.Distance < 75 )
                    sPattern = "ltUpDiag";
                else
                    sPattern = "wdUpDiag";

                break;
            }
            case drawing::HatchStyle_DOUBLE:
            case drawing::HatchStyle_TRIPLE:
            {
                if( rHatch.Distance < 75 )
                    sPattern = "smCheck";
                else
                    sPattern = "openDmnd";

                break;
            }
            default: break;
        }
    }
    // Angle ~ 90° (vertical)
    else if( nAngle < 1125 )
    {
        switch( rHatch.Style )
        {
            case drawing::HatchStyle_SINGLE:
            {
                // dkVert is imported as Distance = 25, ltVert as Distance = 50, export them accordingly.
                if( rHatch.Distance < 50 )
                    sPattern = "dkVert";
                else if( rHatch.Distance < 75 )
                    sPattern = "ltVert";
                else
                    sPattern = "vert";

                break;
            }
            case drawing::HatchStyle_DOUBLE:
            case drawing::HatchStyle_TRIPLE:
            {
                if( rHatch.Distance < 75 )
                    sPattern = "smGrid";
                else
                    sPattern = "lgGrid";

                break;
            }
            default: break;
        }
    }
    // Angle ~ 135° (downward diagonal)
    else if( nAngle < 1575 )
    {
        switch( rHatch.Style )
        {
            case drawing::HatchStyle_SINGLE:
            {
                if( rHatch.Distance < 75 )
                    sPattern = "ltDnDiag";
                else
                    sPattern = "wdDnDiag";

                break;
            }
            case drawing::HatchStyle_DOUBLE:
            case drawing::HatchStyle_TRIPLE:
            {
                if( rHatch.Distance < 75 )
                    sPattern = "smCheck";
                else
                    sPattern = "openDmnd";

                break;
            }
            default: break;
        }
    }
    return sPattern;
}

namespace
{
// ISO/IEC-29500 Part 1 ST_Percentage, and [MS-OI29500] 2.1.1324
sal_Int32 GetST_Percentage(std::u16string_view s)
{
    if (o3tl::ends_with(s, u"%"))
        return std::round(o3tl::toDouble(s) * 1000);
    return o3tl::toInt32(s);
}
}

/** converts the attributes from a CT_RelativeRect to an IntegerRectangle2D */
IntegerRectangle2D GetRelativeRect( const Reference< XFastAttributeList >& xAttribs )
{
    IntegerRectangle2D r;

    r.X1 = GetST_Percentage(xAttribs->getOptionalValue( XML_l ));
    r.Y1 = GetST_Percentage(xAttribs->getOptionalValue( XML_t ));
    r.X2 = GetST_Percentage(xAttribs->getOptionalValue( XML_r ));
    r.Y2 = GetST_Percentage(xAttribs->getOptionalValue( XML_b ));

    return r;
}

/** converts the attributes from a CT_Size2D into an awt Size with 1/100thmm */
awt::Size GetSize2D( const Reference< XFastAttributeList >& xAttribs )
{
    return awt::Size( GetCoordinate( xAttribs->getOptionalValue( XML_cx ) ), GetCoordinate( xAttribs->getOptionalValue( XML_cy ) ) );
}

IndexRange GetIndexRange( const Reference< XFastAttributeList >& xAttributes )
{
    IndexRange range;
    range.start = xAttributes->getOptionalValue( XML_st ).toInt32();
    range.end = xAttributes->getOptionalValue( XML_end ).toInt32();
    return range;
}

} // namespace oox::drawingml

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */