xref: /trunk/main/oox/source/export/shapes.cxx (revision fda69661)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/core/xmlfilterbase.hxx"
25 #include "oox/export/shapes.hxx"
26 #include "oox/export/utils.hxx"
27 
28 #include <cstdio>
29 #include <com/sun/star/awt/CharSet.hpp>
30 #include <com/sun/star/awt/FontDescriptor.hpp>
31 #include <com/sun/star/awt/FontSlant.hpp>
32 #include <com/sun/star/awt/FontWeight.hpp>
33 #include <com/sun/star/awt/FontUnderline.hpp>
34 #include <com/sun/star/awt/Gradient.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/beans/XPropertyState.hpp>
37 #include <com/sun/star/container/XEnumerationAccess.hpp>
38 #include <com/sun/star/drawing/FillStyle.hpp>
39 #include <com/sun/star/drawing/BitmapMode.hpp>
40 #include <com/sun/star/drawing/ConnectorType.hpp>
41 #include <com/sun/star/drawing/LineDash.hpp>
42 #include <com/sun/star/drawing/LineJoint.hpp>
43 #include <com/sun/star/drawing/LineStyle.hpp>
44 #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
45 #include <com/sun/star/drawing/TextVerticalAdjust.hpp>
46 #include <com/sun/star/i18n/ScriptType.hpp>
47 #include <com/sun/star/io/XOutputStream.hpp>
48 #include <com/sun/star/style/ParagraphAdjust.hpp>
49 #include <com/sun/star/text/XSimpleText.hpp>
50 #include <com/sun/star/text/XText.hpp>
51 #include <com/sun/star/text/XTextContent.hpp>
52 #include <com/sun/star/text/XTextField.hpp>
53 #include <com/sun/star/text/XTextRange.hpp>
54 #include <tools/stream.hxx>
55 #include <tools/string.hxx>
56 #include <vcl/cvtgrf.hxx>
57 #include <unotools/fontcvt.hxx>
58 #include <vcl/graph.hxx>
59 #include <vcl/outdev.hxx>
60 #include <svtools/grfmgr.hxx>
61 #include <rtl/strbuf.hxx>
62 #include <sfx2/app.hxx>
63 #include <svl/languageoptions.hxx>
64 #include <svx/escherex.hxx>
65 #include <svx/svdoashp.hxx>
66 #include <svx/svxenum.hxx>
67 #include <svx/unoapi.hxx>
68 
69 using namespace ::com::sun::star;
70 using namespace ::com::sun::star::uno;
71 using namespace ::com::sun::star::drawing;
72 using namespace ::com::sun::star::i18n;
73 using ::com::sun::star::beans::PropertyState;
74 using ::com::sun::star::beans::PropertyValue;
75 using ::com::sun::star::beans::XPropertySet;
76 using ::com::sun::star::beans::XPropertyState;
77 using ::com::sun::star::container::XEnumeration;
78 using ::com::sun::star::container::XEnumerationAccess;
79 using ::com::sun::star::container::XIndexAccess;
80 using ::com::sun::star::drawing::FillStyle;
81 using ::com::sun::star::io::XOutputStream;
82 using ::com::sun::star::text::XSimpleText;
83 using ::com::sun::star::text::XText;
84 using ::com::sun::star::text::XTextContent;
85 using ::com::sun::star::text::XTextField;
86 using ::com::sun::star::text::XTextRange;
87 using ::rtl::OString;
88 using ::rtl::OStringBuffer;
89 using ::rtl::OUString;
90 using ::rtl::OUStringBuffer;
91 using ::sax_fastparser::FSHelperPtr;
92 
93 DBG(extern void dump_pset(Reference< XPropertySet > rXPropSet));
94 
95 #define IDS(x) (OString(#x " ") + OString::valueOf( mnShapeIdMax++ )).getStr()
96 
97 struct CustomShapeTypeTranslationTable
98 {
99     const char* sOOo;
100     const char* sMSO;
101 };
102 
103 static const CustomShapeTypeTranslationTable pCustomShapeTypeTranslationTable[] =
104 {
105     // { "non-primitive", mso_sptMin },
106     { "rectangle", "rect" },
107     { "round-rectangle", "roundRect" },
108     { "ellipse", "ellipse" },
109     { "diamond", "diamond" },
110     { "isosceles-triangle", "triangle" },
111     { "right-triangle", "rtTriangle" },
112     { "parallelogram", "parallelogram" },
113     { "trapezoid", "trapezoid" },
114     { "hexagon", "hexagon" },
115     { "octagon", "octagon" },
116     { "cross", "plus" },
117     { "star5", "star5" },
118     { "right-arrow", "rightArrow" },
119     // { "mso-spt14", mso_sptThickArrow },
120     { "pentagon-right", "homePlate" },
121     { "cube", "cube" },
122     // { "mso-spt17", mso_sptBalloon },
123     // { "mso-spt18", mso_sptSeal },
124     { "mso-spt19", "arc" },
125     { "mso-spt20", "line" },
126     { "mso-spt21", "plaque" },
127     { "can", "can" },
128     { "ring", "donut" },
129     { "mso-spt24", "textSimple" },
130     { "mso-spt25", "textOctagon" },
131     { "mso-spt26", "textHexagon" },
132     { "mso-spt27", "textCurve" },
133     { "mso-spt28", "textWave" },
134     { "mso-spt29", "textRing" },
135     { "mso-spt30", "textOnCurve" },
136     { "mso-spt31", "textOnRing" },
137     { "mso-spt32", "straightConnector1" },
138     { "mso-spt33", "bentConnector2" },
139     { "mso-spt34", "bentConnector3" },
140     { "mso-spt35", "bentConnector4" },
141     { "mso-spt36", "bentConnector5" },
142     { "mso-spt37", "curvedConnector2" },
143     { "mso-spt38", "curvedConnector3" },
144     { "mso-spt39", "curvedConnector4" },
145     { "mso-spt40", "curvedConnector5" },
146     { "mso-spt41", "callout1" },
147     { "mso-spt42", "callout2" },
148     { "mso-spt43", "callout3" },
149     { "mso-spt44", "accentCallout1" },
150     { "mso-spt45", "accentCallout2" },
151     { "mso-spt46", "accentCallout3" },
152     { "line-callout-1", "borderCallout1" },
153     { "line-callout-2", "borderCallout2" },
154     { "line-callout-3", "borderCallout3" },
155     { "mso-spt49", "accentBorderCallout90" },
156     { "mso-spt50", "accentBorderCallout1" },
157     { "mso-spt51", "accentBorderCallout2" },
158     { "mso-spt52", "accentBorderCallout3" },
159     { "mso-spt53", "ribbon" },
160     { "mso-spt54", "ribbon2" },
161     { "chevron", "chevron" },
162     { "pentagon", "pentagon" },
163     { "forbidden", "noSmoking" },
164     { "star8", "seal8" },
165     { "mso-spt59", "seal16" },
166     { "mso-spt60", "seal32" },
167     { "rectangular-callout", "wedgeRectCallout" },
168     { "round-rectangular-callout", "wedgeRoundRectCallout" },
169     { "round-callout", "wedgeEllipseCallout" },
170     { "mso-spt64", "wave" },
171     { "paper", "foldedCorner" },
172     { "left-arrow", "leftArrow" },
173     { "down-arrow", "downArrow" },
174     { "up-arrow", "upArrow" },
175     { "left-right-arrow", "leftRightArrow" },
176     { "up-down-arrow", "upDownArrow" },
177     { "mso-spt71", "irregularSeal1" },
178     { "bang", "irregularSeal2" },
179     { "lightning", "lightningBolt" },
180     { "heart", "heart" },
181     { "mso-spt75", "pictureFrame" },
182     { "quad-arrow", "quadArrow" },
183     { "left-arrow-callout", "leftArrowCallout" },
184     { "right-arrow-callout", "rightArrowCallout" },
185     { "up-arrow-callout", "upArrowCallout" },
186     { "down-arrow-callout", "downArrowCallout" },
187     { "left-right-arrow-callout", "leftRightArrowCallout" },
188     { "up-down-arrow-callout", "upDownArrowCallout" },
189     { "quad-arrow-callout", "quadArrowCallout" },
190     { "quad-bevel", "bevel" },
191     { "left-bracket", "leftBracket" },
192     { "right-bracket", "rightBracket" },
193     { "left-brace", "leftBrace" },
194     { "right-brace", "rightBrace" },
195     { "mso-spt89", "leftUpArrow" },
196     { "mso-spt90", "bentUpArrow" },
197     { "mso-spt91", "bentArrow" },
198     { "star24", "seal24" },
199     { "striped-right-arrow", "stripedRightArrow" },
200     { "notched-right-arrow", "notchedRightArrow" },
201     { "block-arc", "blockArc" },
202     { "smiley", "smileyFace" },
203     { "vertical-scroll", "verticalScroll" },
204     { "horizontal-scroll", "horizontalScroll" },
205     { "circular-arrow", "circularArrow" },
206     { "mso-spt100", "pie" }, // looks like MSO_SPT is wrong here
207     { "mso-spt101", "uturnArrow" },
208     { "mso-spt102", "curvedRightArrow" },
209     { "mso-spt103", "curvedLeftArrow" },
210     { "mso-spt104", "curvedUpArrow" },
211     { "mso-spt105", "curvedDownArrow" },
212     { "cloud-callout", "cloudCallout" },
213     { "mso-spt107", "ellipseRibbon" },
214     { "mso-spt108", "ellipseRibbon2" },
215     { "flowchart-process", "flowChartProcess" },
216     { "flowchart-decision", "flowChartDecision" },
217     { "flowchart-data", "flowChartInputOutput" },
218     { "flowchart-predefined-process", "flowChartPredefinedProcess" },
219     { "flowchart-internal-storage", "flowChartInternalStorage" },
220     { "flowchart-document", "flowChartDocument" },
221     { "flowchart-multidocument", "flowChartMultidocument" },
222     { "flowchart-terminator", "flowChartTerminator" },
223     { "flowchart-preparation", "flowChartPreparation" },
224     { "flowchart-manual-input", "flowChartManualInput" },
225     { "flowchart-manual-operation", "flowChartManualOperation" },
226     { "flowchart-connector", "flowChartConnector" },
227     { "flowchart-card", "flowChartPunchedCard" },
228     { "flowchart-punched-tape", "flowChartPunchedTape" },
229     { "flowchart-summing-junction", "flowChartSummingJunction" },
230     { "flowchart-or", "flowChartOr" },
231     { "flowchart-collate", "flowChartCollate" },
232     { "flowchart-sort", "flowChartSort" },
233     { "flowchart-extract", "flowChartExtract" },
234     { "flowchart-merge", "flowChartMerge" },
235     { "mso-spt129", "flowChartOfflineStorage" },
236     { "flowchart-stored-data", "flowChartOnlineStorage" },
237     { "flowchart-sequential-access", "flowChartMagneticTape" },
238     { "flowchart-magnetic-disk", "flowChartMagneticDisk" },
239     { "flowchart-direct-access-storage", "flowChartMagneticDrum" },
240     { "flowchart-display", "flowChartDisplay" },
241     { "flowchart-delay", "flowChartDelay" },
242     { "fontwork-plain-text", "textPlainText" },
243     { "fontwork-stop", "textStop" },
244     { "fontwork-triangle-up", "textTriangle" },
245     { "fontwork-triangle-down", "textTriangleInverted" },
246     { "fontwork-chevron-up", "textChevron" },
247     { "fontwork-chevron-down", "textChevronInverted" },
248     { "mso-spt142", "textRingInside" },
249     { "mso-spt143", "textRingOutside" },
250     { "fontwork-arch-up-curve", "textArchUpCurve" },
251     { "fontwork-arch-down-curve", "textArchDownCurve" },
252     { "fontwork-circle-curve", "textCircleCurve" },
253     { "fontwork-open-circle-curve", "textButtonCurve" },
254     { "fontwork-arch-up-pour", "textArchUpPour" },
255     { "fontwork-arch-down-pour", "textArchDownPour" },
256     { "fontwork-circle-pour", "textCirclePour" },
257     { "fontwork-open-circle-pour", "textButtonPour" },
258     { "fontwork-curve-up", "textCurveUp" },
259     { "fontwork-curve-down", "textCurveDown" },
260     { "fontwork-fade-up-and-right", "textCascadeUp" },
261     { "fontwork-fade-up-and-left", "textCascadeDown" },
262     { "fontwork-wave", "textWave1" },
263     { "mso-spt157", "textWave2" },
264     { "mso-spt158", "textWave3" },
265     { "mso-spt159", "textWave4" },
266     { "fontwork-inflate", "textInflate" },
267     { "mso-spt161", "textDeflate" },
268     { "mso-spt162", "textInflateBottom" },
269     { "mso-spt163", "textDeflateBottom" },
270     { "mso-spt164", "textInflateTop" },
271     { "mso-spt165", "textDeflateTop" },
272     { "mso-spt166", "textDeflateInflate" },
273     { "mso-spt167", "textDeflateInflateDeflate" },
274     { "fontwork-fade-right", "textFadeRight" },
275     { "fontwork-fade-left", "textFadeLeft" },
276     { "fontwork-fade-up", "textFadeUp" },
277     { "fontwork-fade-down", "textFadeDown" },
278     { "fontwork-slant-up", "textSlantUp" },
279     { "fontwork-slant-down", "textSlantDown" },
280     { "mso-spt174", "textCanUp" },
281     { "mso-spt175", "textCanDown" },
282     { "flowchart-alternate-process", "flowChartAlternateProcess" },
283     { "flowchart-off-page-connector", "flowChartOffpageConnector" },
284     { "mso-spt178", "callout90" },
285     { "mso-spt179", "accentCallout90" },
286     { "mso-spt180", "borderCallout90" },
287     { "mso-spt182", "leftRightUpArrow" },
288     { "sun", "sun" },
289     { "moon", "moon" },
290     { "bracket-pair", "bracketPair" },
291     { "brace-pair", "bracePair" },
292     { "star4", "seal4" },
293     { "mso-spt188", "doubleWave" },
294     { "mso-spt189", "actionButtonBlank" },
295     { "mso-spt190", "actionButtonHome" },
296     { "mso-spt191", "actionButtonHelp" },
297     { "mso-spt192", "actionButtonInformation" },
298     { "mso-spt193", "actionButtonForwardNext" },
299     { "mso-spt194", "actionButtonBackPrevious" },
300     { "mso-spt195", "actionButtonEnd" },
301     { "mso-spt196", "actionButtonBeginning" },
302     { "mso-spt197", "actionButtonReturn" },
303     { "mso-spt198", "actionButtonDocument" },
304     { "mso-spt199", "actionButtonSound" },
305     { "mso-spt200", "actionButtonMovie" },
306     { "mso-spt201", "hostControl" },
307     { "mso-spt202", "rect" }
308 };
309 
310 typedef std::hash_map< const char*, const char*, CStringHash, CStringEqual> CustomShapeTypeTranslationHashMap;
311 static CustomShapeTypeTranslationHashMap* pCustomShapeTypeTranslationHashMap = NULL;
312 
lcl_GetPresetGeometry(const char * sShapeType)313 static const char* lcl_GetPresetGeometry( const char* sShapeType )
314 {
315     const char* sPresetGeo;
316 
317     if( pCustomShapeTypeTranslationHashMap == NULL )
318     {
319         pCustomShapeTypeTranslationHashMap = new CustomShapeTypeTranslationHashMap ();
320         for( unsigned int i = 0; i < sizeof( pCustomShapeTypeTranslationTable )/sizeof( CustomShapeTypeTranslationTable ); i ++ )
321         {
322             (*pCustomShapeTypeTranslationHashMap)[ pCustomShapeTypeTranslationTable[ i ].sOOo ] = pCustomShapeTypeTranslationTable[ i ].sMSO;
323             //DBG(printf("type OOo: %s MSO: %s\n", pCustomShapeTypeTranslationTable[ i ].sOOo, pCustomShapeTypeTranslationTable[ i ].sMSO));
324         }
325     }
326 
327     sPresetGeo = (*pCustomShapeTypeTranslationHashMap)[ sShapeType ];
328 
329     if( sPresetGeo == NULL )
330         sPresetGeo = "rect";
331 
332     return sPresetGeo;
333 }
334 
335 namespace oox { namespace drawingml {
336 
337 #define GETA(propName) \
338     GetProperty( rXPropSet, String( RTL_CONSTASCII_USTRINGPARAM( #propName ) ) )
339 
340 #define GETAD(propName) \
341     ( GetPropertyAndState( rXPropSet, rXPropState, String( RTL_CONSTASCII_USTRINGPARAM( #propName ) ), eState ) && eState == beans::PropertyState_DIRECT_VALUE )
342 
343 #define GET(variable, propName) \
344     if ( GETA(propName) ) \
345         mAny >>= variable;
346 
ShapeExport(sal_Int32 nXmlNamespace,FSHelperPtr pFS,::oox::core::XmlFilterBase * pFB,DocumentType eDocumentType)347 ShapeExport::ShapeExport( sal_Int32 nXmlNamespace, FSHelperPtr pFS, ::oox::core::XmlFilterBase* pFB, DocumentType eDocumentType )
348     : DrawingML( pFS, pFB, eDocumentType )
349     , mnXmlNamespace( nXmlNamespace )
350     , mnShapeIdMax( 1 )
351     , mnPictureIdMax( 1 )
352     , maFraction( 1, 576 )
353     , maMapModeSrc( MAP_100TH_MM )
354     , maMapModeDest( MAP_INCH, Point(), maFraction, maFraction )
355 {
356 }
357 
GetXmlNamespace() const358 sal_Int32 ShapeExport::GetXmlNamespace() const
359 {
360     return mnXmlNamespace;
361 }
362 
SetXmlNamespace(sal_Int32 nXmlNamespace)363 ShapeExport& ShapeExport::SetXmlNamespace( sal_Int32 nXmlNamespace )
364 {
365     mnXmlNamespace = nXmlNamespace;
366     return *this;
367 }
368 
MapSize(const awt::Size & rSize) const369 awt::Size ShapeExport::MapSize( const awt::Size& rSize ) const
370 {
371     Size aRetSize( OutputDevice::LogicToLogic( Size( rSize.Width, rSize.Height ), maMapModeSrc, maMapModeDest ) );
372 
373     if ( !aRetSize.Width() )
374         aRetSize.Width()++;
375     if ( !aRetSize.Height() )
376         aRetSize.Height()++;
377     return awt::Size( aRetSize.Width(), aRetSize.Height() );
378 }
379 
NonEmptyText(Reference<XShape> xShape)380 sal_Bool ShapeExport::NonEmptyText( Reference< XShape > xShape )
381 {
382     Reference< XSimpleText > xText( xShape, UNO_QUERY );
383 
384     return ( xText.is() && xText->getString().getLength() );
385 }
386 
WriteBezierShape(Reference<XShape> xShape,sal_Bool bClosed)387 ShapeExport& ShapeExport::WriteBezierShape( Reference< XShape > xShape, sal_Bool bClosed )
388 {
389     DBG(printf("write open bezier shape\n"));
390 
391     FSHelperPtr pFS = GetFS();
392     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
393 
394     PolyPolygon aPolyPolygon = EscherPropertyContainer::GetPolyPolygon( xShape );
395     Rectangle aRect( aPolyPolygon.GetBoundRect() );
396     awt::Size size = MapSize( awt::Size( aRect.GetWidth(), aRect.GetHeight() ) );
397 
398     DBG(printf("poly count %d\nsize: %d x %d", aPolyPolygon.Count(), int( size.Width ), int( size.Height )));
399 
400     // non visual shape properties
401     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
402     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
403                           XML_id, I32S( GetNewShapeID( xShape ) ),
404                           XML_name, IDS( Freeform ),
405                           FSEND );
406     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
407     WriteNonVisualProperties( xShape );
408     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
409 
410     // visual shape properties
411     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
412     WriteTransformation( aRect );
413     WritePolyPolygon( aPolyPolygon );
414     Reference< XPropertySet > xProps( xShape, UNO_QUERY );
415     if( xProps.is() ) {
416         if( bClosed )
417             WriteFill( xProps );
418         WriteOutline( xProps );
419     }
420 
421     pFS->endElementNS( mnXmlNamespace, XML_spPr );
422 
423     // write text
424     WriteTextBox( xShape );
425 
426     pFS->endElementNS( mnXmlNamespace, XML_sp );
427 
428     return *this;
429 }
430 
WriteClosedBezierShape(Reference<XShape> xShape)431 ShapeExport& ShapeExport::WriteClosedBezierShape( Reference< XShape > xShape )
432 {
433     return WriteBezierShape( xShape, TRUE );
434 }
435 
WriteOpenBezierShape(Reference<XShape> xShape)436 ShapeExport& ShapeExport::WriteOpenBezierShape( Reference< XShape > xShape )
437 {
438     return WriteBezierShape( xShape, FALSE );
439 }
440 
WriteCustomShape(Reference<XShape> xShape)441 ShapeExport& ShapeExport::WriteCustomShape( Reference< XShape > xShape )
442 {
443     DBG(printf("write custom shape\n"));
444 
445     Reference< XPropertySet > rXPropSet( xShape, UNO_QUERY );
446     SdrObjCustomShape* pShape = (SdrObjCustomShape*) GetSdrObjectFromXShape( xShape );
447     sal_Bool bIsDefaultObject = EscherPropertyContainer::IsDefaultObject( pShape );
448     sal_Bool bPredefinedHandlesUsed = TRUE;
449     OUString sShapeType;
450     sal_uInt32 nMirrorFlags = 0;
451     MSO_SPT eShapeType = EscherPropertyContainer::GetCustomShapeType( xShape, nMirrorFlags, sShapeType );
452     const char* sPresetShape = lcl_GetPresetGeometry( USS( sShapeType ) );
453     DBG(printf("custom shape type: %s ==> %s\n", USS( sShapeType ), sPresetShape));
454     Sequence< PropertyValue > aGeometrySeq;
455     sal_Int32 nAdjustmentValuesIndex = -1;
456     sal_Int32 nAdjustmentsWhichNeedsToBeConverted = 0;
457 
458     if( GETA( CustomShapeGeometry ) ) {
459         DBG(printf("got custom shape geometry\n"));
460         if( mAny >>= aGeometrySeq ) {
461 
462             DBG(printf("got custom shape geometry sequence\n"));
463             for( int i = 0; i < aGeometrySeq.getLength(); i++ ) {
464                 const PropertyValue& rProp = aGeometrySeq[ i ];
465                 DBG(printf("geometry property: %s\n", USS( rProp.Name )));
466 
467                 if( rProp.Name.equalsAscii( "AdjustmentValues" ))
468                     nAdjustmentValuesIndex = i;
469                 else if( rProp.Name.equalsAscii( "Handles" )) {
470                     if( !bIsDefaultObject )
471                         bPredefinedHandlesUsed = FALSE;
472                     // TODO: update nAdjustmentsWhichNeedsToBeConverted here
473                 }
474             }
475         }
476     }
477 
478     FSHelperPtr pFS = GetFS();
479     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
480 
481     // non visual shape properties
482     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
483     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
484                           XML_id, I32S( GetNewShapeID( xShape ) ),
485                           XML_name, IDS( CustomShape ),
486                           FSEND );
487     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
488     WriteNonVisualProperties( xShape );
489     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
490 
491     // visual shape properties
492     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
493     WriteShapeTransformation( xShape );
494     if( nAdjustmentValuesIndex != -1 )
495         WritePresetShape( sPresetShape, eShapeType, bPredefinedHandlesUsed, nAdjustmentsWhichNeedsToBeConverted, aGeometrySeq[ nAdjustmentValuesIndex ] );
496     else
497         WritePresetShape( sPresetShape );
498     if( rXPropSet.is() )
499     {
500         WriteFill( rXPropSet );
501         WriteOutline( rXPropSet );
502     }
503 
504     pFS->endElementNS( mnXmlNamespace, XML_spPr );
505 
506     // write text
507     WriteTextBox( xShape );
508 
509     pFS->endElementNS( mnXmlNamespace, XML_sp );
510 
511     return *this;
512 }
513 
WriteEllipseShape(Reference<XShape> xShape)514 ShapeExport& ShapeExport::WriteEllipseShape( Reference< XShape > xShape )
515 {
516     DBG(printf("write ellipse shape\n"));
517 
518     FSHelperPtr pFS = GetFS();
519 
520     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
521 
522     // TODO: arc, section, cut, connector
523 
524     // non visual shape properties
525     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
526     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
527                           XML_id, I32S( GetNewShapeID( xShape ) ),
528                           XML_name, IDS( Ellipse ),
529                           FSEND );
530     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
531     WriteNonVisualProperties( xShape );
532     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
533 
534     // visual shape properties
535     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
536     WriteShapeTransformation( xShape );
537     WritePresetShape( "ellipse" );
538     Reference< XPropertySet > xProps( xShape, UNO_QUERY );
539     if( xProps.is() )
540     {
541         WriteFill( xProps );
542         WriteOutline( xProps );
543     }
544     pFS->endElementNS( mnXmlNamespace, XML_spPr );
545 
546     // write text
547     WriteTextBox( xShape );
548 
549     pFS->endElementNS( mnXmlNamespace, XML_sp );
550 
551     return *this;
552 }
553 
WriteFill(Reference<XPropertySet> xPropSet)554 ShapeExport& ShapeExport::WriteFill( Reference< XPropertySet > xPropSet )
555 {
556     FillStyle aFillStyle( FillStyle_NONE );
557     xPropSet->getPropertyValue( S( "FillStyle" ) ) >>= aFillStyle;
558 
559     if( aFillStyle == FillStyle_BITMAP )
560     {
561         //DBG(printf ("FillStyle_BITMAP properties\n"));
562         //DBG(dump_pset(rXPropSet));
563     }
564 
565     if( aFillStyle == FillStyle_NONE ||
566         aFillStyle == FillStyle_HATCH )
567         return *this;
568 
569     switch( aFillStyle )
570     {
571     case ::com::sun::star::drawing::FillStyle_SOLID :
572         WriteSolidFill( xPropSet );
573         break;
574     case ::com::sun::star::drawing::FillStyle_GRADIENT :
575         WriteGradientFill( xPropSet );
576         break;
577     case ::com::sun::star::drawing::FillStyle_BITMAP :
578         WriteBlipFill( xPropSet, S( "FillBitmapURL" ) );
579         break;
580     default:
581         ;
582     }
583 
584     return *this;
585 }
586 
WriteGraphicObjectShape(Reference<XShape> xShape)587 ShapeExport& ShapeExport::WriteGraphicObjectShape( Reference< XShape > xShape )
588 {
589     DBG(printf("write graphic object shape\n"));
590 
591     if( NonEmptyText( xShape ) )
592     {
593         WriteTextShape( xShape );
594 
595         //DBG(dump_pset(mXPropSet));
596 
597         return *this;
598     }
599 
600     DBG(printf("graphicObject without text\n"));
601 
602     OUString sGraphicURL;
603     Reference< XPropertySet > xShapeProps( xShape, UNO_QUERY );
604     if( !xShapeProps.is() || !( xShapeProps->getPropertyValue( S( "GraphicURL" ) ) >>= sGraphicURL ) )
605     {
606         DBG(printf("no graphic URL found\n"));
607         return *this;
608     }
609 
610     FSHelperPtr pFS = GetFS();
611 
612     pFS->startElementNS( mnXmlNamespace, XML_pic, FSEND );
613 
614     pFS->startElementNS( mnXmlNamespace, XML_nvPicPr, FSEND );
615 
616     OUString sName, sDescr;
617     bool bHaveName = xShapeProps->getPropertyValue( S( "Name" ) ) >>= sName;
618     bool bHaveDesc = xShapeProps->getPropertyValue( S( "Description" ) ) >>= sDescr;
619 
620     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
621                           XML_id,     I32S( GetNewShapeID( xShape ) ),
622                           XML_name,   bHaveName ? USS( sName ) : (OString("Picture ") + OString::valueOf( mnPictureIdMax++ )).getStr(),
623                           XML_descr,  bHaveDesc ? USS( sDescr ) : NULL,
624                           FSEND );
625     // OOXTODO: //cNvPr children: XML_extLst, XML_hlinkClick, XML_hlinkHover
626 
627     pFS->singleElementNS( mnXmlNamespace, XML_cNvPicPr,
628                           // OOXTODO: XML_preferRelativeSize
629                           FSEND );
630 
631     WriteNonVisualProperties( xShape );
632 
633     pFS->endElementNS( mnXmlNamespace, XML_nvPicPr );
634 
635     pFS->startElementNS( mnXmlNamespace, XML_blipFill, FSEND );
636 
637     WriteBlip( sGraphicURL );
638 
639     bool bStretch = false;
640     if( ( xShapeProps->getPropertyValue( S( "FillBitmapStretch" ) ) >>= bStretch ) && bStretch )
641     {
642         WriteStretch();
643     }
644 
645     pFS->endElementNS( mnXmlNamespace, XML_blipFill );
646 
647     // visual shape properties
648     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
649     WriteShapeTransformation( xShape );
650     WritePresetShape( "rect" );
651     pFS->endElementNS( mnXmlNamespace, XML_spPr );
652 
653     pFS->endElementNS( mnXmlNamespace, XML_pic );
654 
655     return *this;
656 }
657 
WriteConnectorShape(Reference<XShape> xShape)658 ShapeExport& ShapeExport::WriteConnectorShape( Reference< XShape > xShape )
659 {
660     sal_Bool bFlipH = false;
661     sal_Bool bFlipV = false;
662 
663     DBG(printf("write connector shape\n"));
664 
665     FSHelperPtr pFS = GetFS();
666 
667     const char* sGeometry = "line";
668     Reference< XPropertySet > rXPropSet( xShape, UNO_QUERY );
669     Reference< XPropertyState > rXPropState( xShape, UNO_QUERY );
670     awt::Point aStartPoint, aEndPoint;
671     Reference< XShape > rXShapeA;
672     Reference< XShape > rXShapeB;
673     PropertyState eState;
674     ConnectorType eConnectorType;
675     if( GETAD( EdgeKind ) ) {
676         mAny >>= eConnectorType;
677 
678         switch( eConnectorType ) {
679             case ConnectorType_CURVE:
680                 sGeometry = "curvedConnector3";
681                 break;
682             case ConnectorType_STANDARD:
683                 sGeometry = "bentConnector3";
684                 break;
685             default:
686             case ConnectorType_LINE:
687             case ConnectorType_LINES:
688                 sGeometry = "straightConnector1";
689                 break;
690         }
691 
692         if( GETAD( EdgeStartPoint ) ) {
693             mAny >>= aStartPoint;
694             if( GETAD( EdgeEndPoint ) ) {
695                 mAny >>= aEndPoint;
696             }
697         }
698         GET( rXShapeA, EdgeStartConnection );
699         GET( rXShapeB, EdgeEndConnection );
700     }
701     EscherConnectorListEntry aConnectorEntry( xShape, aStartPoint, rXShapeA, aEndPoint, rXShapeB );
702 
703     Rectangle aRect( Point( aStartPoint.X, aStartPoint.Y ), Point( aEndPoint.X, aEndPoint.Y ) );
704     if( aRect.getWidth() < 0 ) {
705         bFlipH = TRUE;
706         aRect.setX( aEndPoint.X );
707         aRect.setWidth( aStartPoint.X - aEndPoint.X );
708     }
709 
710     if( aRect.getHeight() < 0 ) {
711         bFlipV = TRUE;
712         aRect.setY( aEndPoint.Y );
713         aRect.setHeight( aStartPoint.Y - aEndPoint.Y );
714     }
715 
716     pFS->startElementNS( mnXmlNamespace, XML_cxnSp, FSEND );
717 
718     // non visual shape properties
719     pFS->startElementNS( mnXmlNamespace, XML_nvCxnSpPr, FSEND );
720     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
721                           XML_id, I32S( GetNewShapeID( xShape ) ),
722                           XML_name, IDS( Line ),
723                           FSEND );
724     // non visual connector shape drawing properties
725     pFS->startElementNS( mnXmlNamespace, XML_cNvCxnSpPr, FSEND );
726     WriteConnectorConnections( aConnectorEntry, GetShapeID( rXShapeA ), GetShapeID( rXShapeB ) );
727     pFS->endElementNS( mnXmlNamespace, XML_cNvCxnSpPr );
728     pFS->singleElementNS( mnXmlNamespace, XML_nvPr, FSEND );
729     pFS->endElementNS( mnXmlNamespace, XML_nvCxnSpPr );
730 
731     // visual shape properties
732     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
733     WriteTransformation( aRect, bFlipH, bFlipV );
734     // TODO: write adjustments (ppt export doesn't work well there either)
735     WritePresetShape( sGeometry );
736     Reference< XPropertySet > xShapeProps( xShape, UNO_QUERY );
737     if( xShapeProps.is() )
738         WriteOutline( xShapeProps );
739     pFS->endElementNS( mnXmlNamespace, XML_spPr );
740 
741     // write text
742     WriteTextBox( xShape );
743 
744     pFS->endElementNS( mnXmlNamespace, XML_cxnSp );
745 
746     return *this;
747 }
748 
WriteLineShape(Reference<XShape> xShape)749 ShapeExport& ShapeExport::WriteLineShape( Reference< XShape > xShape )
750 {
751     sal_Bool bFlipH = false;
752     sal_Bool bFlipV = false;
753 
754     DBG(printf("write line shape\n"));
755 
756     FSHelperPtr pFS = GetFS();
757 
758     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
759 
760     PolyPolygon aPolyPolygon = EscherPropertyContainer::GetPolyPolygon( xShape );
761     if( aPolyPolygon.Count() == 1 && aPolyPolygon[ 0 ].GetSize() == 2)
762     {
763         const Polygon& rPoly = aPolyPolygon[ 0 ];
764 
765         bFlipH = ( rPoly[ 0 ].X() > rPoly[ 1 ].X() );
766         bFlipV = ( rPoly[ 0 ].Y() > rPoly[ 1 ].Y() );
767     }
768 
769     // non visual shape properties
770     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
771     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
772                           XML_id, I32S( GetNewShapeID( xShape ) ),
773                           XML_name, IDS( Line ),
774                           FSEND );
775     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
776     WriteNonVisualProperties( xShape );
777     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
778 
779     // visual shape properties
780     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
781     WriteShapeTransformation( xShape, bFlipH, bFlipV );
782     WritePresetShape( "line" );
783     Reference< XPropertySet > xShapeProps( xShape, UNO_QUERY );
784     if( xShapeProps.is() )
785         WriteOutline( xShapeProps );
786     pFS->endElementNS( mnXmlNamespace, XML_spPr );
787 
788     // write text
789     WriteTextBox( xShape );
790 
791     pFS->endElementNS( mnXmlNamespace, XML_sp );
792 
793     return *this;
794 }
795 
WriteNonVisualDrawingProperties(Reference<XShape> xShape,const char * pName)796 ShapeExport& ShapeExport::WriteNonVisualDrawingProperties( Reference< XShape > xShape, const char* pName )
797 {
798     GetFS()->singleElementNS( mnXmlNamespace, XML_cNvPr,
799                               XML_id, I32S( GetNewShapeID( xShape ) ),
800                               XML_name, pName,
801                               FSEND );
802 
803     return *this;
804 }
805 
WriteNonVisualProperties(Reference<XShape>)806 ShapeExport& ShapeExport::WriteNonVisualProperties( Reference< XShape > )
807 {
808     // Override to generate //nvPr elements.
809     return *this;
810 }
811 
WriteRectangleShape(Reference<XShape> xShape)812 ShapeExport& ShapeExport::WriteRectangleShape( Reference< XShape > xShape )
813 {
814     DBG(printf("write rectangle shape\n"));
815 
816     FSHelperPtr pFS = GetFS();
817 
818     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
819 
820     sal_Int32 nRadius = 0;
821 
822     Reference< XPropertySet > xShapeProps( xShape, UNO_QUERY );
823     if( xShapeProps.is() )
824     {
825         xShapeProps->getPropertyValue( S( "CornerRadius" ) ) >>= nRadius;
826     }
827 
828     if( nRadius )
829     {
830         nRadius = MapSize( awt::Size( nRadius, 0 ) ).Width;
831     }
832 
833     // non visual shape properties
834     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
835     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
836                           XML_id, I32S( GetNewShapeID( xShape ) ),
837                           XML_name, IDS( Rectangle ),
838                           FSEND );
839     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
840     WriteNonVisualProperties( xShape );
841     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
842 
843     // visual shape properties
844     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
845     WriteShapeTransformation( xShape );
846     WritePresetShape( "rect" );
847     Reference< XPropertySet > xProps( xShape, UNO_QUERY );
848     if( xProps.is() )
849     {
850         WriteFill( xProps );
851         WriteOutline( xProps );
852     }
853     pFS->endElementNS( mnXmlNamespace, XML_spPr );
854 
855     // write text
856     WriteTextBox( xShape );
857 
858     pFS->endElementNS( mnXmlNamespace, XML_sp );
859 
860     return *this;
861 }
862 
863 typedef ShapeExport& (ShapeExport::*ShapeConverter)( Reference< XShape > );
864 typedef std::hash_map< const char*, ShapeConverter, CStringHash, CStringEqual> NameToConvertMapType;
865 
lcl_GetConverters()866 static const NameToConvertMapType& lcl_GetConverters()
867 {
868     static bool shape_map_inited = false;
869     static NameToConvertMapType shape_converters;
870     if( shape_map_inited )
871     {
872         return shape_converters;
873     }
874 
875     shape_converters[ "com.sun.star.drawing.ClosedBezierShape" ]        = &ShapeExport::WriteClosedBezierShape;
876     shape_converters[ "com.sun.star.drawing.ConnectorShape" ]           = &ShapeExport::WriteConnectorShape;
877     shape_converters[ "com.sun.star.drawing.CustomShape" ]              = &ShapeExport::WriteCustomShape;
878     shape_converters[ "com.sun.star.drawing.EllipseShape" ]             = &ShapeExport::WriteEllipseShape;
879     shape_converters[ "com.sun.star.drawing.GraphicObjectShape" ]       = &ShapeExport::WriteGraphicObjectShape;
880     shape_converters[ "com.sun.star.drawing.LineShape" ]                = &ShapeExport::WriteLineShape;
881     shape_converters[ "com.sun.star.drawing.OpenBezierShape" ]          = &ShapeExport::WriteOpenBezierShape;
882     shape_converters[ "com.sun.star.drawing.RectangleShape" ]           = &ShapeExport::WriteRectangleShape;
883     shape_converters[ "com.sun.star.drawing.TextShape" ]                = &ShapeExport::WriteTextShape;
884     shape_converters[ "com.sun.star.presentation.DateTimeShape" ]       = &ShapeExport::WriteTextShape;
885     shape_converters[ "com.sun.star.presentation.FooterShape" ]         = &ShapeExport::WriteTextShape;
886     shape_converters[ "com.sun.star.presentation.HeaderShape" ]         = &ShapeExport::WriteTextShape;
887     shape_converters[ "com.sun.star.presentation.NotesShape" ]          = &ShapeExport::WriteTextShape;
888     shape_converters[ "com.sun.star.presentation.OutlinerShape" ]       = &ShapeExport::WriteTextShape;
889     shape_converters[ "com.sun.star.presentation.SlideNumberShape" ]    = &ShapeExport::WriteTextShape;
890     shape_converters[ "com.sun.star.presentation.TitleTextShape" ]      = &ShapeExport::WriteTextShape;
891     shape_map_inited = true;
892 
893     return shape_converters;
894 }
895 
WriteShape(Reference<XShape> xShape)896 ShapeExport& ShapeExport::WriteShape( Reference< XShape > xShape )
897 {
898     OUString sShapeType = xShape->getShapeType();
899     DBG( printf( "write shape: %s\n", USS( sShapeType ) ) );
900     NameToConvertMapType::const_iterator aConverter = lcl_GetConverters().find( USS( sShapeType ) );
901     if( aConverter == lcl_GetConverters().end() )
902     {
903         DBG( printf( "unknown shape\n" ) );
904         return WriteUnknownShape( xShape );
905     }
906     (this->*(aConverter->second))( xShape );
907 
908     return *this;
909 }
910 
WriteTextBox(Reference<XShape> xShape)911 ShapeExport& ShapeExport::WriteTextBox( Reference< XShape > xShape )
912 {
913     if( NonEmptyText( xShape ) )
914     {
915         FSHelperPtr pFS = GetFS();
916 
917         pFS->startElementNS( mnXmlNamespace, XML_txBody, FSEND );
918         WriteText( xShape );
919         pFS->endElementNS( mnXmlNamespace, XML_txBody );
920     }
921 
922     return *this;
923 }
924 
WriteTextShape(Reference<XShape> xShape)925 ShapeExport& ShapeExport::WriteTextShape( Reference< XShape > xShape )
926 {
927     FSHelperPtr pFS = GetFS();
928 
929     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
930 
931     // non visual shape properties
932     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
933     WriteNonVisualDrawingProperties( xShape, IDS( TextShape ) );
934     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, XML_txBox, "1", FSEND );
935     WriteNonVisualProperties( xShape );
936     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
937 
938     // visual shape properties
939     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
940     WriteShapeTransformation( xShape );
941     WritePresetShape( "rect" );
942     WriteBlipFill( Reference< XPropertySet >(xShape, UNO_QUERY ), S( "GraphicURL" ) );
943     pFS->endElementNS( mnXmlNamespace, XML_spPr );
944 
945     WriteTextBox( xShape );
946 
947     pFS->endElementNS( mnXmlNamespace, XML_sp );
948 
949     return *this;
950 }
951 
WriteUnknownShape(Reference<XShape>)952 ShapeExport& ShapeExport::WriteUnknownShape( Reference< XShape > )
953 {
954     // Override this method to do something useful.
955     return *this;
956 }
957 
operator ()(const::com::sun::star::uno::Reference<::com::sun::star::drawing::XShape> rXShape) const958 size_t ShapeExport::ShapeHash::operator()( const ::com::sun::star::uno::Reference < ::com::sun::star::drawing::XShape > rXShape ) const
959 {
960     return maHashFunction( USS( rXShape->getShapeType() ) );
961 }
962 
GetNewShapeID(const Reference<XShape> rXShape)963 sal_Int32 ShapeExport::GetNewShapeID( const Reference< XShape > rXShape )
964 {
965     sal_Int32 nID = GetFB()->GetUniqueId();
966 
967     maShapeMap[ rXShape ] = nID;
968 
969     return nID;
970 }
971 
GetShapeID(const Reference<XShape> rXShape)972 sal_Int32 ShapeExport::GetShapeID( const Reference< XShape > rXShape )
973 {
974     ShapeHashMap::const_iterator aIter = maShapeMap.find( rXShape );
975 
976     if( aIter == maShapeMap.end() )
977         return -1;
978 
979     return aIter->second;
980 }
981 
982 } }
983