summaryrefslogtreecommitdiffstats
path: root/xmloff/source/style/bordrhdl.cxx
blob: 4b3a7b666cd24ce958fd912fb69311dcf46d37b6 (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
/* -*- 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 "bordrhdl.hxx"
#include <sax/tools/converter.hxx>
#include <xmloff/xmltoken.hxx>
#include <xmloff/xmluconv.hxx>
#include <xmloff/xmlement.hxx>
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/table/BorderLine2.hpp>
#include <com/sun/star/table/BorderLineStyle.hpp>

#include <limits.h>

using namespace ::com::sun::star;
using namespace ::xmloff::token;

#define DEF_LINE_WIDTH_0        1
#define DEF_LINE_WIDTH_1        35
#define DEF_LINE_WIDTH_2        88

#define SVX_XML_BORDER_WIDTH_THIN 0
#define SVX_XML_BORDER_WIDTH_MIDDLE 1
#define SVX_XML_BORDER_WIDTH_THICK 2

SvXMLEnumMapEntry<sal_uInt16> const pXML_BorderStyles[] =
{
    { XML_NONE,          table::BorderLineStyle::NONE   },
    { XML_HIDDEN,        table::BorderLineStyle::NONE   },
    { XML_SOLID,         table::BorderLineStyle::SOLID  },
    { XML_DOUBLE,        table::BorderLineStyle::DOUBLE },
    { XML_DOUBLE_THIN,   table::BorderLineStyle::DOUBLE_THIN },
    { XML_DOTTED,        table::BorderLineStyle::DOTTED },
    { XML_DASHED,        table::BorderLineStyle::DASHED },
    { XML_GROOVE,        table::BorderLineStyle::ENGRAVED },
    { XML_RIDGE,         table::BorderLineStyle::EMBOSSED },
    { XML_INSET,         table::BorderLineStyle::INSET  },
    { XML_OUTSET,        table::BorderLineStyle::OUTSET },
    { XML_FINE_DASHED,   table::BorderLineStyle::FINE_DASHED },
    { XML_DASH_DOT,      table::BorderLineStyle::DASH_DOT },
    { XML_DASH_DOT_DOT,  table::BorderLineStyle::DASH_DOT_DOT },
    { XML_TOKEN_INVALID, 0 }
};

SvXMLEnumMapEntry<sal_uInt16> const pXML_NamedBorderWidths[] =
{
    { XML_THIN,             SVX_XML_BORDER_WIDTH_THIN   },
    { XML_MIDDLE,           SVX_XML_BORDER_WIDTH_MIDDLE },
    { XML_THICK,            SVX_XML_BORDER_WIDTH_THICK  },
    { XML_TOKEN_INVALID,    0 }
};
// mapping tables to map external xml input to internal box line widths

sal_uInt16 const aBorderWidths[] =
{
    DEF_LINE_WIDTH_0,
    DEF_LINE_WIDTH_1,
    DEF_LINE_WIDTH_2
};

static void lcl_frmitems_setXMLBorderStyle( table::BorderLine2 & rBorderLine, sal_uInt16 nStyle )
{
    sal_Int16 eStyle = -1; // None
    if (nStyle != table::BorderLineStyle::NONE)
        eStyle = sal_Int16( nStyle );

    rBorderLine.LineStyle = eStyle;
}




XMLBorderWidthHdl::~XMLBorderWidthHdl()
{
    // nothing to do
}

bool XMLBorderWidthHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
{
    SvXMLTokenEnumerator aTokenEnum( rStrImpValue );

    sal_Int32 nInWidth, nDistance, nOutWidth;

    std::u16string_view aToken;
    if( !aTokenEnum.getNextToken( aToken ) )
        return false;

    if (!rUnitConverter.convertMeasureToCore( nInWidth, aToken, 0, 500 ))
        return false;

    if( !aTokenEnum.getNextToken( aToken ) )
        return false;

    if (!rUnitConverter.convertMeasureToCore( nDistance, aToken, 0, 500 ))
        return false;

    if( !aTokenEnum.getNextToken( aToken ) )
        return false;

    if (!rUnitConverter.convertMeasureToCore( nOutWidth, aToken, 0, 500 ))
        return false;

    table::BorderLine2 aBorderLine;
    if(!(rValue >>= aBorderLine))
        aBorderLine.Color = 0;

    aBorderLine.InnerLineWidth = sal::static_int_cast< sal_Int16 >(nInWidth);
    aBorderLine.OuterLineWidth = sal::static_int_cast< sal_Int16 >(nOutWidth);
    aBorderLine.LineDistance   = sal::static_int_cast< sal_Int16 >(nDistance);

    rValue <<= aBorderLine;
    return true;
}

bool XMLBorderWidthHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
{
    OUStringBuffer aOut;

    table::BorderLine2 aBorderLine;
    if(!(rValue >>= aBorderLine))
        return false;

    bool bDouble = false;
    switch ( aBorderLine.LineStyle )
    {
        case table::BorderLineStyle::DOUBLE:
        case table::BorderLineStyle::DOUBLE_THIN:
        case table::BorderLineStyle::THINTHICK_SMALLGAP:
        case table::BorderLineStyle::THINTHICK_MEDIUMGAP:
        case table::BorderLineStyle::THINTHICK_LARGEGAP:
        case table::BorderLineStyle::THICKTHIN_SMALLGAP:
        case table::BorderLineStyle::THICKTHIN_MEDIUMGAP:
        case table::BorderLineStyle::THICKTHIN_LARGEGAP:
            bDouble = true;
            break;
        default:
            break;
    }

    if( ( aBorderLine.LineDistance == 0 && aBorderLine.InnerLineWidth == 0 ) || !bDouble )
        return false;

    rUnitConverter.convertMeasureToXML( aOut, aBorderLine.InnerLineWidth );
    aOut.append( ' ' );
    rUnitConverter.convertMeasureToXML( aOut, aBorderLine.LineDistance );
    aOut.append( ' ' );
    rUnitConverter.convertMeasureToXML( aOut, aBorderLine.OuterLineWidth );

    rStrExpValue = aOut.makeStringAndClear();
    return true;
}




XMLBorderHdl::~XMLBorderHdl()
{
    // nothing to do
}

bool XMLBorderHdl::importXML( const OUString& rStrImpValue, uno::Any& rValue, const SvXMLUnitConverter& rUnitConverter ) const
{
    std::u16string_view aToken;
    SvXMLTokenEnumerator aTokens( rStrImpValue );

    bool bHasStyle = false;
    bool bHasWidth = false;
    bool bHasColor = false;

    sal_uInt16 nStyle = USHRT_MAX;
    sal_uInt16 nWidth = 0;
    sal_uInt16 nNamedWidth = USHRT_MAX;
    sal_Int32 nColor = 0;

    sal_Int32 nTemp;
    while( aTokens.getNextToken( aToken ) && !aToken.empty() )
    {
        if( !bHasWidth &&
            SvXMLUnitConverter::convertEnum( nNamedWidth, aToken,
                                        pXML_NamedBorderWidths ) )
        {
            bHasWidth = true;
        }
        else if( !bHasStyle &&
                 SvXMLUnitConverter::convertEnum( nStyle, aToken,
                                              pXML_BorderStyles ) )
        {
            bHasStyle = true;
        }
        else if (!bHasColor && ::sax::Converter::convertColor(nColor, aToken))
        {
            bHasColor = true;
        }
        else if( !bHasWidth &&
                 rUnitConverter.convertMeasureToCore( nTemp, aToken, 0,
                                                 USHRT_MAX ) )
        {
            nWidth = static_cast<sal_uInt16>(nTemp);
            bHasWidth = true;
        }
        else
        {
            // misformed
            return false;
        }
    }

    // if there is no style or a different style than none but no width,
    // then the declaration is not valid.
    if (!bHasStyle || (table::BorderLineStyle::NONE != nStyle && !bHasWidth))
        return false;

    table::BorderLine2 aBorderLine;
    if(!(rValue >>= aBorderLine))
    {
        aBorderLine.Color = 0;
        aBorderLine.InnerLineWidth = 0;
        aBorderLine.OuterLineWidth = 0;
        aBorderLine.LineDistance   = 0;
        aBorderLine.LineWidth      = 0;
    }

    // first of all, delete an empty line
    if (table::BorderLineStyle::NONE == nStyle ||
        (bHasWidth && USHRT_MAX == nNamedWidth && 0 == nWidth) )
    {
        aBorderLine.InnerLineWidth = 0;
        aBorderLine.OuterLineWidth = 0;
        aBorderLine.LineDistance   = 0;
        aBorderLine.LineWidth = 0;
    }
    else
    {
        if( USHRT_MAX != nNamedWidth )
        {
            aBorderLine.LineWidth = aBorderWidths[nNamedWidth];
        }
        else
        {
            aBorderLine.LineWidth = nWidth;
            lcl_frmitems_setXMLBorderStyle( aBorderLine, nStyle );
        }
    }

    // set color
    if( bHasColor )
    {
        aBorderLine.Color = nColor;
    }

    rValue <<= aBorderLine;
    return true;
}

bool XMLBorderHdl::exportXML( OUString& rStrExpValue, const uno::Any& rValue, const SvXMLUnitConverter& /* rUnitConverter */ ) const
{
    OUStringBuffer aOut;

    table::BorderLine2 aBorderLine;
    if(!(rValue >>= aBorderLine))
        return false;

    sal_Int32 nWidth = aBorderLine.LineWidth;

    if( nWidth == 0 )
    {
        aOut.append( GetXMLToken( XML_NONE ) );
    }
    else
    {
        ::sax::Converter::convertMeasure( aOut, nWidth,
               util::MeasureUnit::MM_100TH, util::MeasureUnit::POINT);

        aOut.append( ' ' );

        XMLTokenEnum eStyleToken = XML_SOLID;
        switch ( aBorderLine.LineStyle )
        {
            case table::BorderLineStyle::DASHED:
                eStyleToken = XML_DASHED;
                break;
            case table::BorderLineStyle::DOTTED:
                eStyleToken = XML_DOTTED;
                break;
            case table::BorderLineStyle::DOUBLE:
            case table::BorderLineStyle::THINTHICK_SMALLGAP:
            case table::BorderLineStyle::THINTHICK_MEDIUMGAP:
            case table::BorderLineStyle::THINTHICK_LARGEGAP:
            case table::BorderLineStyle::THICKTHIN_SMALLGAP:
            case table::BorderLineStyle::THICKTHIN_MEDIUMGAP:
            case table::BorderLineStyle::THICKTHIN_LARGEGAP:
                eStyleToken = XML_DOUBLE;
                break;
            case table::BorderLineStyle::EMBOSSED:
                eStyleToken = XML_RIDGE;
                break;
            case table::BorderLineStyle::ENGRAVED:
                eStyleToken = XML_GROOVE;
                break;
            case table::BorderLineStyle::OUTSET:
                eStyleToken = XML_OUTSET;
                break;
            case table::BorderLineStyle::INSET:
                eStyleToken = XML_INSET;
                break;
            case table::BorderLineStyle::FINE_DASHED:
                eStyleToken = XML_FINE_DASHED;
                break;
            case table::BorderLineStyle::DASH_DOT:
                eStyleToken = XML_DASH_DOT;
                break;
            case table::BorderLineStyle::DASH_DOT_DOT:
                eStyleToken = XML_DASH_DOT_DOT;
                break;
            case table::BorderLineStyle::DOUBLE_THIN:
                eStyleToken = XML_DOUBLE_THIN;
                break;
            case table::BorderLineStyle::SOLID:
            default:
                break;
        }
        aOut.append( GetXMLToken( eStyleToken ) );

        aOut.append( ' ' );

        ::sax::Converter::convertColor( aOut, aBorderLine.Color );
    }

    rStrExpValue = aOut.makeStringAndClear();

    return true;
}

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