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