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 <drawingml/linepropertiescontext.hxx>
21 #include <oox/drawingml/drawingmltypes.hxx>
22 #include <drawingml/misccontexts.hxx>
23 #include <drawingml/lineproperties.hxx>
24 #include <oox/helper/attributelist.hxx>
25 #include <oox/token/namespaces.hxx>
26 #include <oox/token/tokens.hxx>
27 
28 using namespace ::oox::core;
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::xml::sax;
31 
32 // CT_LineProperties
33 
34 namespace oox { namespace drawingml {
35 
LinePropertiesContext(ContextHandler2Helper const & rParent,const AttributeList & rAttribs,LineProperties & rLineProperties)36 LinePropertiesContext::LinePropertiesContext( ContextHandler2Helper const & rParent, const AttributeList& rAttribs,
37     LineProperties& rLineProperties ) throw()
38 : ContextHandler2( rParent )
39 , mrLineProperties( rLineProperties )
40 {
41     mrLineProperties.moLineWidth = rAttribs.getInteger( XML_w );
42     mrLineProperties.moLineCompound = rAttribs.getToken( XML_cmpd );
43     mrLineProperties.moLineCap = rAttribs.getToken( XML_cap );
44 }
45 
~LinePropertiesContext()46 LinePropertiesContext::~LinePropertiesContext()
47 {
48 }
49 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)50 ContextHandlerRef LinePropertiesContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
51 {
52     switch( nElement )
53     {
54         // LineFillPropertiesGroup
55         case A_TOKEN( noFill ):
56         case A_TOKEN( solidFill ):
57         case A_TOKEN( gradFill ):
58         case A_TOKEN( pattFill ):
59             return FillPropertiesContext::createFillContext( *this, nElement, rAttribs, mrLineProperties.maLineFill );
60         break;
61 
62         // LineDashPropertiesGroup
63         case A_TOKEN( prstDash ):  // CT_PresetLineDashProperties
64             mrLineProperties.moPresetDash = rAttribs.getToken( XML_val );
65         break;
66         case A_TOKEN( custDash ):  // CT_DashStopList
67             return this;
68         break;
69         case A_TOKEN( ds ):
70         {
71             // 'a:ds' has 2 attributes : 'd' and 'sp'
72             // both are of type 'a:ST_PositivePercentage'
73             // according to the specs Office will read percentages formatted with a trailing percent sign
74             // or formatted as 1000th of a percent without a trailing percent sign, but only write percentages
75             // as 1000th's of a percent without a trailing percent sign.
76             // The code below takes care of both scenarios by converting to '1000th of a percent' always
77             OUString aStr;
78             sal_Int32 nDash = 0;
79             aStr = rAttribs.getString( XML_d, "" );
80             if ( aStr.endsWith("%") )
81             {
82                 // Ends with a '%'
83                 aStr = aStr.copy(0, aStr.getLength() - 1);
84                 aStr = aStr.trim();
85                 nDash = aStr.toInt32();
86 
87                 // Convert to 1000th of a percent
88                 nDash *= 1000;
89             }
90             else
91             {
92                 nDash = rAttribs.getInteger( XML_d, 0 );
93             }
94 
95             sal_Int32 nSp = 0;
96             aStr = rAttribs.getString( XML_sp, "" );
97             if ( aStr.endsWith("%") )
98             {
99                 // Ends with a '%'
100                 aStr = aStr.copy(0, aStr.getLength() - 1);
101                 aStr = aStr.trim();
102                 nSp = aStr.toInt32();
103 
104                 // Convert to 1000th of a percent
105                 nSp *= 1000;
106             }
107             else
108             {
109                 nSp = rAttribs.getInteger( XML_sp, 0 );
110             }
111 
112             mrLineProperties.maCustomDash.emplace_back( nDash, nSp );
113         }
114         break;
115 
116         // LineJoinPropertiesGroup
117         case A_TOKEN( round ):
118         case A_TOKEN( bevel ):
119         case A_TOKEN( miter ):
120             mrLineProperties.moLineJoint = getBaseToken( nElement );
121         break;
122 
123         case A_TOKEN( headEnd ):  // CT_LineEndProperties
124         case A_TOKEN( tailEnd ):  // CT_LineEndProperties
125         {                         // ST_LineEndType
126             bool bTailEnd = nElement == A_TOKEN( tailEnd );
127             LineArrowProperties& rArrowProps = bTailEnd ? mrLineProperties.maEndArrow : mrLineProperties.maStartArrow;
128             rArrowProps.moArrowType = rAttribs.getToken( XML_type );
129             rArrowProps.moArrowWidth = rAttribs.getToken( XML_w );
130             rArrowProps.moArrowLength = rAttribs.getToken( XML_len );
131         }
132         break;
133     }
134     return nullptr;
135 }
136 
137 } }
138 
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
140