summaryrefslogtreecommitdiffstats
path: root/oox/source/drawingml/linepropertiescontext.cxx
blob: dbcae7caf5c70d2cd425d24021236e451a149db7 (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
/* -*- 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 <drawingml/linepropertiescontext.hxx>
#include <drawingml/misccontexts.hxx>
#include <drawingml/lineproperties.hxx>
#include <oox/helper/attributelist.hxx>
#include <oox/token/namespaces.hxx>
#include <oox/token/tokens.hxx>
#include <docmodel/theme/FormatScheme.hxx>

using namespace ::oox::core;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::xml::sax;

// CT_LineProperties

namespace oox::drawingml {

LinePropertiesContext::LinePropertiesContext( ContextHandler2Helper const & rParent, const AttributeList& rAttribs,
        LineProperties& rLineProperties, model::LineStyle* pLineStyle) noexcept
    : ContextHandler2(rParent)
    , mpLineStyle(pLineStyle)
    , mrLineProperties(rLineProperties)
{
    mrLineProperties.moLineWidth = rAttribs.getInteger( XML_w );
    mrLineProperties.moLineCompound = rAttribs.getToken( XML_cmpd );
    mrLineProperties.moLineCap = rAttribs.getToken( XML_cap );

    if (mpLineStyle)
    {
        mpLineStyle->mnWidth = rAttribs.getInteger(XML_w, 0);

        switch (rAttribs.getToken(XML_cap, XML_TOKEN_INVALID))
        {
            case XML_rnd: mpLineStyle->meCapType = model::CapType::Round; break;
            case XML_sq: mpLineStyle->meCapType = model::CapType::Square; break;
            case XML_flat: mpLineStyle->meCapType = model::CapType::Flat; break;
            default:
                mpLineStyle->meCapType = model::CapType::Unset; break;
        }

        switch (rAttribs.getToken(XML_cmpd, XML_TOKEN_INVALID))
        {
            case XML_sng: mpLineStyle->meCompoundLineType = model::CompoundLineType::Single; break;
            case XML_dbl: mpLineStyle->meCompoundLineType = model::CompoundLineType::Double; break;
            case XML_thickThin: mpLineStyle->meCompoundLineType = model::CompoundLineType::ThickThin_Double; break;
            case XML_thinThick: mpLineStyle->meCompoundLineType = model::CompoundLineType::ThinThick_Double; break;
            case XML_tri: mpLineStyle->meCompoundLineType = model::CompoundLineType::Triple; break;
            default:
                mpLineStyle->meCompoundLineType = model::CompoundLineType::Unset; break;
        }

        switch (rAttribs.getToken(XML_algn, XML_TOKEN_INVALID))
        {
            case XML_ctr: mpLineStyle->mePenAlignment  = model::PenAlignmentType::Center; break;
            case XML_in: mpLineStyle->mePenAlignment  = model::PenAlignmentType::Inset; break;
            default:
                mpLineStyle->mePenAlignment = model::PenAlignmentType::Unset; break;
        }
    }
}

LinePropertiesContext::~LinePropertiesContext()
{
}

ContextHandlerRef LinePropertiesContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
{
    switch( nElement )
    {
        // LineFillPropertiesGroup
        case A_TOKEN( noFill ):
        case A_TOKEN( solidFill ):
        case A_TOKEN( gradFill ):
        case A_TOKEN( pattFill ):
        {
            model::FillStyle* pFillStyle = nullptr;
            if (mpLineStyle)
            {
                pFillStyle = &mpLineStyle->maLineFillStyle;
            }
            return FillPropertiesContext::createFillContext(*this, nElement, rAttribs, mrLineProperties.maLineFill, pFillStyle);
        }
        break;

        // LineDashPropertiesGroup
        case A_TOKEN( prstDash ):  // CT_PresetLineDashProperties
        {
            mrLineProperties.moPresetDash = rAttribs.getToken( XML_val );

            if (mpLineStyle)
            {
                auto& rLineDash = mpLineStyle->maLineDash;
                switch (rAttribs.getToken(XML_val, XML_TOKEN_INVALID))
                {
                    case XML_solid: rLineDash.mePresetType  = model::PresetDashType::Solid; break;
                    case XML_dot: rLineDash.mePresetType  = model::PresetDashType::Dot; break;
                    case XML_dash: rLineDash.mePresetType  = model::PresetDashType::Dash; break;
                    case XML_lgDash: rLineDash.mePresetType  = model::PresetDashType::LargeDash; break;
                    case XML_dashDot: rLineDash.mePresetType  = model::PresetDashType::DashDot; break;
                    case XML_lgDashDot: rLineDash.mePresetType  = model::PresetDashType::LargeDashDot; break;
                    case XML_lgDashDotDot: rLineDash.mePresetType  = model::PresetDashType::LargeDashDotDot; break;
                    case XML_sysDash: rLineDash.mePresetType  = model::PresetDashType::SystemDash; break;
                    case XML_sysDot: rLineDash.mePresetType  = model::PresetDashType::SystemDot; break;
                    case XML_sysDashDot: rLineDash.mePresetType  = model::PresetDashType::SystemDashDot; break;
                    case XML_sysDashDotDot: rLineDash.mePresetType  = model::PresetDashType::SystemDashDotDot; break;
                    default:
                        rLineDash.mePresetType = model::PresetDashType::Unset; break;
                }
            }
        }
        break;
        case A_TOKEN( custDash ):  // CT_DashStopList
            return this;
        case A_TOKEN( ds ):
        {
            // 'a:ds' has 2 attributes : 'd' and 'sp'
            // both are of type 'a:ST_PositivePercentage'
            // according to the specs Office will read percentages formatted with a trailing percent sign
            // or formatted as 1000th of a percent without a trailing percent sign, but only write percentages
            // as 1000th's of a percent without a trailing percent sign.
            // The code below takes care of both scenarios by converting to '1000th of a percent' always
            OUString aStr;
            sal_Int32 nDashLength = 0;
            aStr = rAttribs.getStringDefaulted( XML_d);
            if ( aStr.endsWith("%") )
            {
                // Ends with a '%'
                aStr = aStr.copy(0, aStr.getLength() - 1);
                aStr = aStr.trim();
                nDashLength = aStr.toInt32();

                // Convert to 1000th of a percent
                nDashLength *= 1000;
            }
            else
            {
                nDashLength = rAttribs.getInteger( XML_d, 0 );
            }

            sal_Int32 nSpaceLength = 0;
            aStr = rAttribs.getStringDefaulted( XML_sp);
            if ( aStr.endsWith("%") )
            {
                // Ends with a '%'
                aStr = aStr.copy(0, aStr.getLength() - 1);
                aStr = aStr.trim();
                nSpaceLength = aStr.toInt32();

                // Convert to 1000th of a percent
                nSpaceLength *= 1000;
            }
            else
            {
                nSpaceLength = rAttribs.getInteger( XML_sp, 0 );
            }

            mrLineProperties.maCustomDash.emplace_back( nDashLength, nSpaceLength );

            if (mpLineStyle)
            {
                mpLineStyle->maLineDash.maCustomList.push_back({ nDashLength, nSpaceLength });
            }
        }
        break;

        // LineJoinPropertiesGroup
        case A_TOKEN( round ):
        case A_TOKEN( bevel ):
        case A_TOKEN( miter ):
        {
            sal_Int32 nToken = getBaseToken(nElement);
            mrLineProperties.moLineJoint = nToken;

            if (mpLineStyle)
            {
                switch (nToken)
                {
                    case XML_round: mpLineStyle->maLineJoin.meType = model::LineJoinType::Round; break;
                    case XML_bevel: mpLineStyle->maLineJoin.meType = model::LineJoinType::Bevel; break;
                    case XML_miter: mpLineStyle->maLineJoin.meType = model::LineJoinType::Miter; break;
                    default:
                        mpLineStyle->maLineJoin.meType = model::LineJoinType::Miter;
                }
                if (nToken == XML_miter)
                {
                    sal_Int32 nMiterLimit = rAttribs.getInteger(XML_lim, 0);
                    mpLineStyle->maLineJoin.mnMiterLimit = nMiterLimit;
                }
            }
        }
        break;

        case A_TOKEN( headEnd ):  // CT_LineEndProperties
        case A_TOKEN( tailEnd ):  // CT_LineEndProperties
        {                         // ST_LineEndType
            bool bTailEnd = nElement == A_TOKEN( tailEnd );
            LineArrowProperties& rArrowProps = bTailEnd ? mrLineProperties.maEndArrow : mrLineProperties.maStartArrow;
            rArrowProps.moArrowType = rAttribs.getToken( XML_type );
            rArrowProps.moArrowWidth = rAttribs.getToken( XML_w );
            rArrowProps.moArrowLength = rAttribs.getToken( XML_len );

            if (mpLineStyle)
            {
                model::LineEndType eLineEndType = model::LineEndType::None;
                switch (rAttribs.getToken(XML_type, XML_none))
                {
                    case XML_triangle: eLineEndType = model::LineEndType::Triangle; break;
                    case XML_stealth: eLineEndType = model::LineEndType::Stealth; break;
                    case XML_diamond: eLineEndType = model::LineEndType::Diamond; break;
                    case XML_oval: eLineEndType = model::LineEndType::Oval; break;
                    case XML_arrow: eLineEndType = model::LineEndType::Arrow; break;
                    default:
                    case XML_none: eLineEndType = model::LineEndType::None; break;
                }

                model::LineEndLength eLineEndLength = model::LineEndLength::Unset;
                switch (rAttribs.getToken(XML_len, XML_TOKEN_INVALID))
                {
                    case XML_sm: eLineEndLength = model::LineEndLength::Small; break;
                    case XML_med: eLineEndLength = model::LineEndLength::Medium; break;
                    case XML_lg: eLineEndLength = model::LineEndLength::Large; break;
                    default:
                        break;
                }

                model::LineEndWidth eLineEndWidth = model::LineEndWidth::Unset;
                switch (rAttribs.getToken(XML_w, XML_TOKEN_INVALID))
                {
                    case XML_sm: eLineEndWidth = model::LineEndWidth::Small; break;
                    case XML_med: eLineEndWidth = model::LineEndWidth::Medium; break;
                    case XML_lg: eLineEndWidth = model::LineEndWidth::Large; break;
                    default:
                        break;
                }

                if (nElement == A_TOKEN(tailEnd))
                {
                    mpLineStyle->maTailEnd.meType = eLineEndType;
                    mpLineStyle->maTailEnd.meLength = eLineEndLength;
                    mpLineStyle->maTailEnd.meWidth = eLineEndWidth;
                }
                else if (nElement == A_TOKEN(headEnd))
                {
                    mpLineStyle->maHeadEnd.meType = eLineEndType;
                    mpLineStyle->maHeadEnd.meLength = eLineEndLength;
                    mpLineStyle->maHeadEnd.meWidth = eLineEndWidth;
                }
            }
        }
        break;
    }
    return nullptr;
}

}

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