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 0 : LineJoint lclGetLineJoint( sal_Int32 nToken )
144 : {
145 : OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
146 0 : switch( nToken )
147 : {
148 0 : case XML_round: return LineJoint_ROUND;
149 0 : case XML_bevel: return LineJoint_BEVEL;
150 0 : 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 0 : sal_Int32 lclGetArrowSize( sal_Int32 nToken )
160 : {
161 : OSL_ASSERT((nToken & sal_Int32(0xFFFF0000))==0);
162 0 : switch( nToken )
163 : {
164 0 : case XML_sm: return OOX_ARROWSIZE_SMALL;
165 0 : 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 0 : 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 0 : NamedValue aNamedMarker;
179 :
180 0 : OUStringBuffer aBuffer;
181 0 : sal_Int32 nMarkerWidth = 0;
182 0 : bool bMarkerCenter = false;
183 0 : sal_Int32 nArrowType = rArrowProps.moArrowType.get( XML_none );
184 : OSL_ASSERT((nArrowType & sal_Int32(0xFFFF0000))==0);
185 0 : switch( nArrowType )
186 : {
187 : case XML_triangle:
188 0 : aBuffer.append( "msArrowEnd" );
189 0 : break;
190 : case XML_arrow:
191 0 : aBuffer.append( "msArrowOpenEnd" );
192 0 : break;
193 : case XML_stealth:
194 0 : aBuffer.append( "msArrowStealthEnd" );
195 0 : break;
196 : case XML_diamond:
197 0 : aBuffer.append( "msArrowDiamondEnd" );
198 0 : bMarkerCenter = true;
199 0 : break;
200 : case XML_oval:
201 0 : aBuffer.append( "msArrowOvalEnd" );
202 0 : bMarkerCenter = true;
203 0 : break;
204 : }
205 :
206 0 : if( !aBuffer.isEmpty() )
207 : {
208 0 : sal_Int32 nLength = lclGetArrowSize( rArrowProps.moArrowLength.get( XML_med ) );
209 0 : sal_Int32 nWidth = lclGetArrowSize( rArrowProps.moArrowWidth.get( XML_med ) );
210 :
211 0 : sal_Int32 nNameIndex = nWidth * 3 + nLength + 1;
212 0 : aBuffer.append( ' ' ).append( nNameIndex );
213 0 : OUString aMarkerName = aBuffer.makeStringAndClear();
214 :
215 0 : bool bIsArrow = nArrowType == XML_arrow;
216 0 : double fArrowLength = 1.0;
217 0 : switch( nLength )
218 : {
219 0 : case OOX_ARROWSIZE_SMALL: fArrowLength = (bIsArrow ? 3.5 : 2.0); break;
220 0 : 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 0 : double fArrowWidth = 1.0;
224 0 : switch( nWidth )
225 : {
226 0 : case OOX_ARROWSIZE_SMALL: fArrowWidth = (bIsArrow ? 3.5 : 2.0); break;
227 0 : 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 0 : sal_Int32 nBaseLineWidth = ::std::max< sal_Int32 >( nLineWidth, 70 );
232 0 : 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 0 : 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 0 : ::std::vector< awt::Point > aPoints;
244 : OSL_ASSERT((rArrowProps.moArrowType.get() & sal_Int32(0xFFFF0000))==0);
245 0 : switch( rArrowProps.moArrowType.get() )
246 : {
247 : case XML_triangle:
248 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
249 0 : aPoints.push_back( OOX_ARROW_POINT( 100, 100 ) );
250 0 : aPoints.push_back( OOX_ARROW_POINT( 0, 100 ) );
251 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
252 0 : break;
253 : case XML_arrow:
254 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
255 0 : aPoints.push_back( OOX_ARROW_POINT( 100, 91 ) );
256 0 : aPoints.push_back( OOX_ARROW_POINT( 85, 100 ) );
257 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 36 ) );
258 0 : aPoints.push_back( OOX_ARROW_POINT( 15, 100 ) );
259 0 : aPoints.push_back( OOX_ARROW_POINT( 0, 91 ) );
260 0 : aPoints.push_back( OOX_ARROW_POINT( 50, 0 ) );
261 0 : 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 0 : if( !aPoints.empty() )
296 : {
297 0 : PolyPolygonBezierCoords aMarkerCoords;
298 0 : aMarkerCoords.Coordinates.realloc( 1 );
299 0 : aMarkerCoords.Coordinates[ 0 ] = ContainerHelper::vectorToSequence( aPoints );
300 :
301 0 : ::std::vector< PolygonFlags > aFlags( aPoints.size(), PolygonFlags_NORMAL );
302 0 : aMarkerCoords.Flags.realloc( 1 );
303 0 : aMarkerCoords.Flags[ 0 ] = ContainerHelper::vectorToSequence( aFlags );
304 :
305 0 : aNamedMarker.Name = aMarkerName;
306 0 : aNamedMarker.Value <<= aMarkerCoords;
307 0 : }
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 0 : aNamedMarker.Name = aMarkerName;
315 0 : }
316 : }
317 :
318 : // push the properties (filled aNamedMarker.Name indicates valid marker)
319 0 : if( !aNamedMarker.Name.isEmpty() )
320 : {
321 0 : if( bLineEnd )
322 : {
323 0 : rPropMap.setProperty( SHAPEPROP_LineEnd, aNamedMarker );
324 0 : rPropMap.setProperty( SHAPEPROP_LineEndWidth, nMarkerWidth );
325 0 : rPropMap.setProperty( SHAPEPROP_LineEndCenter, bMarkerCenter );
326 : }
327 : else
328 : {
329 0 : rPropMap.setProperty( SHAPEPROP_LineStart, aNamedMarker );
330 0 : rPropMap.setProperty( SHAPEPROP_LineStartWidth, nMarkerWidth );
331 0 : rPropMap.setProperty( SHAPEPROP_LineStartCenter, bMarkerCenter );
332 : }
333 0 : }
334 0 : }
335 :
336 : } // namespace
337 :
338 :
339 :
340 0 : void LineArrowProperties::assignUsed( const LineArrowProperties& rSourceProps )
341 : {
342 0 : moArrowType.assignIfUsed( rSourceProps.moArrowType );
343 0 : moArrowWidth.assignIfUsed( rSourceProps.moArrowWidth );
344 0 : moArrowLength.assignIfUsed( rSourceProps.moArrowLength );
345 0 : }
346 :
347 :
348 :
349 0 : void LineProperties::assignUsed( const LineProperties& rSourceProps )
350 : {
351 0 : maStartArrow.assignUsed( rSourceProps.maStartArrow );
352 0 : maEndArrow.assignUsed( rSourceProps.maEndArrow );
353 0 : maLineFill.assignUsed( rSourceProps.maLineFill );
354 0 : if( !rSourceProps.maCustomDash.empty() )
355 0 : maCustomDash = rSourceProps.maCustomDash;
356 0 : moLineWidth.assignIfUsed( rSourceProps.moLineWidth );
357 0 : moPresetDash.assignIfUsed( rSourceProps.moPresetDash );
358 0 : moLineCompound.assignIfUsed( rSourceProps.moLineCompound );
359 0 : moLineCap.assignIfUsed( rSourceProps.moLineCap );
360 0 : moLineJoint.assignIfUsed( rSourceProps.moLineJoint );
361 0 : }
362 :
363 0 : 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 0 : if( maLineFill.moFillType.has() )
368 : {
369 : // line style (our core only supports none and solid)
370 0 : 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 0 : sal_Int32 nLineWidth = getLineWidth();
374 :
375 : // create line dash from preset dash token (not for invisible line)
376 0 : if( (eLineStyle != drawing::LineStyle_NONE) && (moPresetDash.differsFrom( XML_solid ) || !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.differsFrom( XML_solid ) )
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 0 : rPropMap.setProperty( SHAPEPROP_LineStyle, eLineStyle );
399 :
400 : // line joint type
401 0 : if( moLineJoint.has() )
402 0 : rPropMap.setProperty( SHAPEPROP_LineJoint, lclGetLineJoint( moLineJoint.get() ) );
403 :
404 : // line width in 1/100mm
405 0 : rPropMap.setProperty( SHAPEPROP_LineWidth, nLineWidth );
406 :
407 : // line color and transparence
408 0 : Color aLineColor = maLineFill.getBestSolidColor();
409 0 : if( aLineColor.isUsed() )
410 : {
411 0 : rPropMap.setProperty( SHAPEPROP_LineColor, aLineColor.getColor( rGraphicHelper, nPhClr ) );
412 0 : if( aLineColor.hasTransparency() )
413 0 : rPropMap.setProperty( SHAPEPROP_LineTransparency, aLineColor.getTransparency() );
414 : }
415 :
416 : // line markers
417 0 : lclPushMarkerProperties( rPropMap, maStartArrow, nLineWidth, false );
418 0 : lclPushMarkerProperties( rPropMap, maEndArrow, nLineWidth, true );
419 : }
420 0 : }
421 :
422 0 : drawing::LineStyle LineProperties::getLineStyle() const
423 : {
424 : // rules to calculate the line style inferred from the code in LineProperties::pushToPropMap
425 0 : return (maLineFill.moFillType.get() == XML_noFill) ?
426 : drawing::LineStyle_NONE :
427 0 : (moPresetDash.differsFrom( XML_solid ) || (!moPresetDash && !maCustomDash.empty())) ?
428 : drawing::LineStyle_DASH :
429 0 : drawing::LineStyle_SOLID;
430 : }
431 :
432 0 : drawing::LineJoint LineProperties::getLineJoint() const
433 : {
434 0 : if( moLineJoint.has() )
435 0 : return lclGetLineJoint( moLineJoint.get() );
436 :
437 0 : return drawing::LineJoint_NONE;
438 : }
439 :
440 0 : sal_Int32 LineProperties::getLineWidth() const
441 : {
442 0 : return convertEmuToHmm( moLineWidth.get( 0 ) );
443 : }
444 :
445 :
446 :
447 : } // namespace drawingml
448 0 : } // namespace oox
449 :
450 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|