Line data Source code
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 "oox/drawingml/lineproperties.hxx"
21 : #include <vector>
22 : #include <rtl/ustrbuf.hxx>
23 : #include <com/sun/star/beans/NamedValue.hpp>
24 : #include <com/sun/star/container/XNameContainer.hpp>
25 : #include <com/sun/star/drawing/FlagSequence.hpp>
26 : #include <com/sun/star/drawing/LineDash.hpp>
27 : #include <com/sun/star/drawing/LineJoint.hpp>
28 : #include <com/sun/star/drawing/LineStyle.hpp>
29 : #include <com/sun/star/drawing/PointSequence.hpp>
30 : #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
31 : #include "oox/drawingml/drawingmltypes.hxx"
32 : #include "oox/drawingml/shapepropertymap.hxx"
33 : #include "oox/helper/containerhelper.hxx"
34 : #include "oox/helper/graphichelper.hxx"
35 : #include "oox/token/tokens.hxx"
36 :
37 : using namespace ::com::sun::star;
38 : using namespace ::com::sun::star::beans;
39 : using namespace ::com::sun::star::drawing;
40 :
41 : using ::com::sun::star::uno::Any;
42 : using ::com::sun::star::uno::Reference;
43 : using ::com::sun::star::container::XNameContainer;
44 :
45 : namespace oox {
46 : namespace drawingml {
47 :
48 : // ============================================================================
49 :
50 : namespace {
51 :
52 0 : void lclSetDashData( LineDash& orLineDash, sal_Int16 nDots, sal_Int32 nDotLen,
53 : sal_Int16 nDashes, sal_Int32 nDashLen, sal_Int32 nDistance )
54 : {
55 0 : orLineDash.Dots = nDots;
56 0 : orLineDash.DotLen = nDotLen;
57 0 : orLineDash.Dashes = nDashes;
58 0 : orLineDash.DashLen = nDashLen;
59 0 : orLineDash.Distance = nDistance;
60 0 : }
61 :
62 : /** Converts the specified preset dash to API dash.
63 :
64 : Line length and dot length are set relative to line width and have to be
65 : multiplied by the actual line width after this function.
66 : */
67 0 : void lclConvertPresetDash( LineDash& orLineDash, sal_Int32 nPresetDash )
68 : {
69 0 : switch( nPresetDash )
70 : {
71 0 : case XML_dot: lclSetDashData( orLineDash, 1, 1, 0, 0, 3 ); break;
72 0 : case XML_dash: lclSetDashData( orLineDash, 0, 0, 1, 4, 3 ); break;
73 0 : case XML_dashDot: lclSetDashData( orLineDash, 1, 1, 1, 4, 3 ); break;
74 :
75 0 : case XML_lgDash: lclSetDashData( orLineDash, 0, 0, 1, 8, 3 ); break;
76 0 : case XML_lgDashDot: lclSetDashData( orLineDash, 1, 1, 1, 8, 3 ); break;
77 0 : case XML_lgDashDotDot: lclSetDashData( orLineDash, 2, 1, 1, 8, 3 ); break;
78 :
79 0 : case XML_sysDot: lclSetDashData( orLineDash, 1, 1, 0, 0, 1 ); break;
80 0 : case XML_sysDash: lclSetDashData( orLineDash, 0, 0, 1, 3, 1 ); break;
81 0 : case XML_sysDashDot: lclSetDashData( orLineDash, 1, 1, 1, 3, 1 ); break;
82 0 : case XML_sysDashDotDot: lclSetDashData( orLineDash, 2, 1, 1, 3, 1 ); break;
83 :
84 : default:
85 : OSL_FAIL( "lclConvertPresetDash - unsupported preset dash" );
86 0 : lclSetDashData( orLineDash, 0, 0, 1, 4, 3 );
87 : }
88 0 : }
89 :
90 : /** Converts the passed custom dash to API dash.
91 :
92 : Line length and dot length are set relative to line width and have to be
93 : multiplied by the actual line width after this function.
94 : */
95 0 : void lclConvertCustomDash( LineDash& orLineDash, const LineProperties::DashStopVector& rCustomDash )
96 : {
97 0 : if( rCustomDash.empty() )
98 : {
99 : OSL_FAIL( "lclConvertCustomDash - unexpected empty custom dash" );
100 0 : lclSetDashData( orLineDash, 0, 0, 1, 4, 3 );
101 0 : return;
102 : }
103 :
104 : // count dashes and dots (stops equal or less than 2 are assumed to be dots)
105 0 : sal_Int16 nDots = 0;
106 0 : sal_Int32 nDotLen = 0;
107 0 : sal_Int16 nDashes = 0;
108 0 : sal_Int32 nDashLen = 0;
109 0 : sal_Int32 nDistance = 0;
110 0 : for( LineProperties::DashStopVector::const_iterator aIt = rCustomDash.begin(), aEnd = rCustomDash.end(); aIt != aEnd; ++aIt )
111 : {
112 0 : if( aIt->first <= 2 )
113 : {
114 0 : ++nDots;
115 0 : nDotLen += aIt->first;
116 : }
117 : else
118 : {
119 0 : ++nDashes;
120 0 : nDashLen += aIt->first;
121 : }
122 0 : nDistance += aIt->second;
123 : }
124 0 : orLineDash.DotLen = (nDots > 0) ? ::std::max< sal_Int32 >( nDotLen / nDots, 1 ) : 0;
125 0 : orLineDash.Dots = nDots;
126 0 : orLineDash.DashLen = (nDashes > 0) ? ::std::max< sal_Int32 >( nDashLen / nDashes, 1 ) : 0;
127 0 : orLineDash.Dashes = nDashes;
128 0 : orLineDash.Distance = ::std::max< sal_Int32 >( nDistance / rCustomDash.size(), 1 );
129 : }
130 :
131 0 : DashStyle lclGetDashStyle( sal_Int32 nToken )
132 : {
133 : OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
134 0 : switch( nToken )
135 : {
136 0 : case XML_rnd: return DashStyle_ROUNDRELATIVE;
137 0 : case XML_sq: return DashStyle_RECTRELATIVE;
138 0 : case XML_flat: return DashStyle_RECT;
139 : }
140 0 : return DashStyle_ROUNDRELATIVE;
141 : }
142 :
143 22 : LineJoint lclGetLineJoint( sal_Int32 nToken )
144 : {
145 : OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
146 22 : switch( nToken )
147 : {
148 8 : case XML_round: return LineJoint_ROUND;
149 0 : case XML_bevel: return LineJoint_BEVEL;
150 14 : case XML_miter: return LineJoint_MITER;
151 : }
152 0 : return LineJoint_ROUND;
153 : }
154 :
155 : const sal_Int32 OOX_ARROWSIZE_SMALL = 0;
156 : const sal_Int32 OOX_ARROWSIZE_MEDIUM = 1;
157 : const sal_Int32 OOX_ARROWSIZE_LARGE = 2;
158 :
159 16 : sal_Int32 lclGetArrowSize( sal_Int32 nToken )
160 : {
161 : OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
162 16 : switch( nToken )
163 : {
164 0 : case XML_sm: return OOX_ARROWSIZE_SMALL;
165 16 : case XML_med: return OOX_ARROWSIZE_MEDIUM;
166 0 : case XML_lg: return OOX_ARROWSIZE_LARGE;
167 : }
168 0 : return OOX_ARROWSIZE_MEDIUM;
169 : }
170 :
171 : // ----------------------------------------------------------------------------
172 :
173 120 : void lclPushMarkerProperties( ShapePropertyMap& rPropMap,
174 : const LineArrowProperties& rArrowProps, sal_Int32 nLineWidth, bool bLineEnd )
175 : {
176 : /* Store the marker polygon and the marker name in a single value, to be
177 : able to pass both to the ShapePropertyMap::setProperty() function. */
178 120 : NamedValue aNamedMarker;
179 :
180 120 : OUStringBuffer aBuffer;
181 120 : sal_Int32 nMarkerWidth = 0;
182 120 : bool bMarkerCenter = false;
183 120 : sal_Int32 nArrowType = rArrowProps.moArrowType.get( XML_none );
184 : OSL_ASSERT((nArrowType & sal_Int32(0xFFFF0000))==0);
185 120 : switch( nArrowType )
186 : {
187 : case XML_triangle:
188 4 : aBuffer.append( CREATE_OUSTRING( "msArrowEnd" ) );
189 4 : break;
190 : case XML_arrow:
191 4 : aBuffer.append( CREATE_OUSTRING( "msArrowOpenEnd" ) );
192 4 : break;
193 : case XML_stealth:
194 0 : aBuffer.append( CREATE_OUSTRING( "msArrowStealthEnd" ) );
195 0 : break;
196 : case XML_diamond:
197 0 : aBuffer.append( CREATE_OUSTRING( "msArrowDiamondEnd" ) );
198 0 : bMarkerCenter = true;
199 0 : break;
200 : case XML_oval:
201 0 : aBuffer.append( CREATE_OUSTRING( "msArrowOvalEnd" ) );
202 0 : bMarkerCenter = true;
203 0 : break;
204 : }
205 :
206 120 : if( aBuffer.getLength() > 0 )
207 : {
208 8 : sal_Int32 nLength = lclGetArrowSize( rArrowProps.moArrowLength.get( XML_med ) );
209 8 : sal_Int32 nWidth = lclGetArrowSize( rArrowProps.moArrowWidth.get( XML_med ) );
210 :
211 8 : sal_Int32 nNameIndex = nWidth * 3 + nLength + 1;
212 8 : aBuffer.append( sal_Unicode( ' ' ) ).append( nNameIndex );
213 8 : OUString aMarkerName = aBuffer.makeStringAndClear();
214 :
215 8 : bool bIsArrow = nArrowType == XML_arrow;
216 8 : double fArrowLength = 1.0;
217 8 : switch( nLength )
218 : {
219 0 : case OOX_ARROWSIZE_SMALL: fArrowLength = (bIsArrow ? 3.5 : 2.0); break;
220 8 : case OOX_ARROWSIZE_MEDIUM: fArrowLength = (bIsArrow ? 4.5 : 3.0); break;
221 0 : case OOX_ARROWSIZE_LARGE: fArrowLength = (bIsArrow ? 6.0 : 5.0); break;
222 : }
223 8 : double fArrowWidth = 1.0;
224 8 : switch( nWidth )
225 : {
226 0 : case OOX_ARROWSIZE_SMALL: fArrowWidth = (bIsArrow ? 3.5 : 2.0); break;
227 8 : case OOX_ARROWSIZE_MEDIUM: fArrowWidth = (bIsArrow ? 4.5 : 3.0); break;
228 0 : case OOX_ARROWSIZE_LARGE: fArrowWidth = (bIsArrow ? 6.0 : 5.0); break;
229 : }
230 : // set arrow width relative to line width
231 8 : sal_Int32 nBaseLineWidth = ::std::max< sal_Int32 >( nLineWidth, 70 );
232 8 : nMarkerWidth = static_cast< sal_Int32 >( fArrowWidth * nBaseLineWidth );
233 :
234 : /* Test if the marker already exists in the marker table, do not
235 : create it again in this case. If markers are inserted explicitly
236 : instead by their name, the polygon will be created always.
237 : TODO: this can be optimized by using a map. */
238 8 : if( !rPropMap.hasNamedLineMarkerInTable( aMarkerName ) )
239 : {
240 : // pass X and Y as percentage to OOX_ARROW_POINT
241 : #define OOX_ARROW_POINT( x, y ) awt::Point( static_cast< sal_Int32 >( fArrowWidth * x ), static_cast< sal_Int32 >( fArrowLength * y ) )
242 :
243 3 : ::std::vector< awt::Point > aPoints;
244 : OSL_ASSERT((rArrowProps.moArrowType.get() & sal_Int32(0xFFFF0000))==0);
245 3 : switch( rArrowProps.moArrowType.get() )
246 : {
247 : case XML_triangle:
248 2 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
249 2 : aPoints.push_back( OOX_ARROW_POINT( 100, 100 ) );
250 2 : aPoints.push_back( OOX_ARROW_POINT( 0, 100 ) );
251 2 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
252 2 : break;
253 : case XML_arrow:
254 1 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
255 1 : aPoints.push_back( OOX_ARROW_POINT( 100, 91 ) );
256 1 : aPoints.push_back( OOX_ARROW_POINT( 85, 100 ) );
257 1 : aPoints.push_back( OOX_ARROW_POINT( 50, 36 ) );
258 1 : aPoints.push_back( OOX_ARROW_POINT( 15, 100 ) );
259 1 : aPoints.push_back( OOX_ARROW_POINT( 0, 91 ) );
260 1 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
261 1 : break;
262 : case XML_stealth:
263 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
264 0 : aPoints.push_back( OOX_ARROW_POINT( 100, 100 ) );
265 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 60 ) );
266 0 : aPoints.push_back( OOX_ARROW_POINT( 0, 100 ) );
267 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
268 0 : break;
269 : case XML_diamond:
270 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
271 0 : aPoints.push_back( OOX_ARROW_POINT( 100, 50 ) );
272 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 100 ) );
273 0 : aPoints.push_back( OOX_ARROW_POINT( 0, 50 ) );
274 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
275 0 : break;
276 : case XML_oval:
277 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
278 0 : aPoints.push_back( OOX_ARROW_POINT( 75, 7 ) );
279 0 : aPoints.push_back( OOX_ARROW_POINT( 93, 25 ) );
280 0 : aPoints.push_back( OOX_ARROW_POINT( 100, 50 ) );
281 0 : aPoints.push_back( OOX_ARROW_POINT( 93, 75 ) );
282 0 : aPoints.push_back( OOX_ARROW_POINT( 75, 93 ) );
283 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 100 ) );
284 0 : aPoints.push_back( OOX_ARROW_POINT( 25, 93 ) );
285 0 : aPoints.push_back( OOX_ARROW_POINT( 7, 75 ) );
286 0 : aPoints.push_back( OOX_ARROW_POINT( 0, 50 ) );
287 0 : aPoints.push_back( OOX_ARROW_POINT( 7, 25 ) );
288 0 : aPoints.push_back( OOX_ARROW_POINT( 25, 7 ) );
289 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
290 0 : break;
291 : }
292 : #undef OOX_ARROW_POINT
293 :
294 : OSL_ENSURE( !aPoints.empty(), "lclPushMarkerProperties - missing arrow coordinates" );
295 3 : if( !aPoints.empty() )
296 : {
297 3 : PolyPolygonBezierCoords aMarkerCoords;
298 3 : aMarkerCoords.Coordinates.realloc( 1 );
299 3 : aMarkerCoords.Coordinates[ 0 ] = ContainerHelper::vectorToSequence( aPoints );
300 :
301 3 : ::std::vector< PolygonFlags > aFlags( aPoints.size(), PolygonFlags_NORMAL );
302 3 : aMarkerCoords.Flags.realloc( 1 );
303 3 : aMarkerCoords.Flags[ 0 ] = ContainerHelper::vectorToSequence( aFlags );
304 :
305 3 : aNamedMarker.Name = aMarkerName;
306 3 : aNamedMarker.Value <<= aMarkerCoords;
307 3 : }
308 : }
309 : else
310 : {
311 : /* Named marker object exists already in the marker table, pass
312 : its name only. This will set the name as property value, but
313 : does not create a new object in the marker table. */
314 5 : aNamedMarker.Name = aMarkerName;
315 8 : }
316 : }
317 :
318 : // push the properties (filled aNamedMarker.Name indicates valid marker)
319 120 : if( !aNamedMarker.Name.isEmpty() )
320 : {
321 8 : if( bLineEnd )
322 : {
323 7 : rPropMap.setProperty( SHAPEPROP_LineEnd, aNamedMarker );
324 7 : rPropMap.setProperty( SHAPEPROP_LineEndWidth, nMarkerWidth );
325 7 : rPropMap.setProperty( SHAPEPROP_LineEndCenter, bMarkerCenter );
326 : }
327 : else
328 : {
329 1 : rPropMap.setProperty( SHAPEPROP_LineStart, aNamedMarker );
330 1 : rPropMap.setProperty( SHAPEPROP_LineStartWidth, nMarkerWidth );
331 1 : rPropMap.setProperty( SHAPEPROP_LineStartCenter, bMarkerCenter );
332 : }
333 120 : }
334 120 : }
335 :
336 : } // namespace
337 :
338 : // ============================================================================
339 :
340 98 : void LineArrowProperties::assignUsed( const LineArrowProperties& rSourceProps )
341 : {
342 98 : moArrowType.assignIfUsed( rSourceProps.moArrowType );
343 98 : moArrowWidth.assignIfUsed( rSourceProps.moArrowWidth );
344 98 : moArrowLength.assignIfUsed( rSourceProps.moArrowLength );
345 98 : }
346 :
347 : // ============================================================================
348 :
349 49 : void LineProperties::assignUsed( const LineProperties& rSourceProps )
350 : {
351 49 : maStartArrow.assignUsed( rSourceProps.maStartArrow );
352 49 : maEndArrow.assignUsed( rSourceProps.maEndArrow );
353 49 : maLineFill.assignUsed( rSourceProps.maLineFill );
354 49 : if( !rSourceProps.maCustomDash.empty() )
355 0 : maCustomDash = rSourceProps.maCustomDash;
356 49 : moLineWidth.assignIfUsed( rSourceProps.moLineWidth );
357 49 : moPresetDash.assignIfUsed( rSourceProps.moPresetDash );
358 49 : moLineCompound.assignIfUsed( rSourceProps.moLineCompound );
359 49 : moLineCap.assignIfUsed( rSourceProps.moLineCap );
360 49 : moLineJoint.assignIfUsed( rSourceProps.moLineJoint );
361 49 : }
362 :
363 60 : void LineProperties::pushToPropMap( ShapePropertyMap& rPropMap,
364 : const GraphicHelper& rGraphicHelper, sal_Int32 nPhClr ) const
365 : {
366 : // line fill type must exist, otherwise ignore other properties
367 60 : if( maLineFill.moFillType.has() )
368 : {
369 : // line style (our core only supports none and solid)
370 60 : drawing::LineStyle eLineStyle = (maLineFill.moFillType.get() == XML_noFill) ? drawing::LineStyle_NONE : drawing::LineStyle_SOLID;
371 :
372 : // convert line width from EMUs to 1/100mm
373 60 : sal_Int32 nLineWidth = convertEmuToHmm( moLineWidth.get( 0 ) );
374 :
375 : // create line dash from preset dash token (not for invisible line)
376 60 : if( (eLineStyle != drawing::LineStyle_NONE) && (moPresetDash.differsFrom( XML_solid ) || (!moPresetDash && !maCustomDash.empty())) )
377 : {
378 0 : LineDash aLineDash;
379 0 : aLineDash.Style = lclGetDashStyle( moLineCap.get( XML_rnd ) );
380 :
381 : // convert preset dash or custom dash
382 0 : if( moPresetDash.has() )
383 0 : lclConvertPresetDash( aLineDash, moPresetDash.get() );
384 : else
385 0 : lclConvertCustomDash( aLineDash, maCustomDash );
386 :
387 : // convert relative dash/dot length to absolute length
388 0 : sal_Int32 nBaseLineWidth = ::std::max< sal_Int32 >( nLineWidth, 35 );
389 0 : aLineDash.DotLen *= nBaseLineWidth;
390 0 : aLineDash.DashLen *= nBaseLineWidth;
391 0 : aLineDash.Distance *= nBaseLineWidth;
392 :
393 0 : if( rPropMap.setProperty( SHAPEPROP_LineDash, aLineDash ) )
394 0 : eLineStyle = drawing::LineStyle_DASH;
395 : }
396 :
397 : // set final line style property
398 60 : rPropMap.setProperty( SHAPEPROP_LineStyle, eLineStyle );
399 :
400 : // line joint type
401 60 : if( moLineJoint.has() )
402 22 : rPropMap.setProperty( SHAPEPROP_LineJoint, lclGetLineJoint( moLineJoint.get() ) );
403 :
404 : // line width in 1/100mm
405 60 : rPropMap.setProperty( SHAPEPROP_LineWidth, nLineWidth );
406 :
407 : // line color and transparence
408 60 : Color aLineColor = maLineFill.getBestSolidColor();
409 60 : if( aLineColor.isUsed() )
410 : {
411 20 : rPropMap.setProperty( SHAPEPROP_LineColor, aLineColor.getColor( rGraphicHelper, nPhClr ) );
412 20 : if( aLineColor.hasTransparency() )
413 0 : rPropMap.setProperty( SHAPEPROP_LineTransparency, aLineColor.getTransparency() );
414 : }
415 :
416 : // line markers
417 60 : lclPushMarkerProperties( rPropMap, maStartArrow, nLineWidth, false );
418 60 : lclPushMarkerProperties( rPropMap, maEndArrow, nLineWidth, true );
419 : }
420 60 : }
421 :
422 : // ============================================================================
423 :
424 : } // namespace drawingml
425 51 : } // namespace oox
426 :
427 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|