xref: /aoo42x/main/oox/source/export/shapes.cxx (revision ca5ec200)
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 struct StringCheck
311 {
312     bool operator()( const char* s1, const char* s2 ) const
313     {
314         return strcmp( s1, s2 ) == 0;
315     }
316 };
317 
318 typedef std::hash_map< const char*, const char*, std::hash<const char*>, StringCheck> CustomShapeTypeTranslationHashMap;
319 static CustomShapeTypeTranslationHashMap* pCustomShapeTypeTranslationHashMap = NULL;
320 
321 static const char* lcl_GetPresetGeometry( const char* sShapeType )
322 {
323     const char* sPresetGeo;
324 
325     if( pCustomShapeTypeTranslationHashMap == NULL )
326     {
327         pCustomShapeTypeTranslationHashMap = new CustomShapeTypeTranslationHashMap ();
328         for( unsigned int i = 0; i < sizeof( pCustomShapeTypeTranslationTable )/sizeof( CustomShapeTypeTranslationTable ); i ++ )
329         {
330             (*pCustomShapeTypeTranslationHashMap)[ pCustomShapeTypeTranslationTable[ i ].sOOo ] = pCustomShapeTypeTranslationTable[ i ].sMSO;
331             //DBG(printf("type OOo: %s MSO: %s\n", pCustomShapeTypeTranslationTable[ i ].sOOo, pCustomShapeTypeTranslationTable[ i ].sMSO));
332         }
333     }
334 
335     sPresetGeo = (*pCustomShapeTypeTranslationHashMap)[ sShapeType ];
336 
337     if( sPresetGeo == NULL )
338         sPresetGeo = "rect";
339 
340     return sPresetGeo;
341 }
342 
343 namespace oox { namespace drawingml {
344 
345 #define GETA(propName) \
346     GetProperty( rXPropSet, String( RTL_CONSTASCII_USTRINGPARAM( #propName ) ) )
347 
348 #define GETAD(propName) \
349     ( GetPropertyAndState( rXPropSet, rXPropState, String( RTL_CONSTASCII_USTRINGPARAM( #propName ) ), eState ) && eState == beans::PropertyState_DIRECT_VALUE )
350 
351 #define GET(variable, propName) \
352     if ( GETA(propName) ) \
353         mAny >>= variable;
354 
355 ShapeExport::ShapeExport( sal_Int32 nXmlNamespace, FSHelperPtr pFS, ::oox::core::XmlFilterBase* pFB, DocumentType eDocumentType )
356     : DrawingML( pFS, pFB, eDocumentType )
357     , mnXmlNamespace( nXmlNamespace )
358     , mnShapeIdMax( 1 )
359     , mnPictureIdMax( 1 )
360     , maFraction( 1, 576 )
361     , maMapModeSrc( MAP_100TH_MM )
362     , maMapModeDest( MAP_INCH, Point(), maFraction, maFraction )
363 {
364 }
365 
366 sal_Int32 ShapeExport::GetXmlNamespace() const
367 {
368     return mnXmlNamespace;
369 }
370 
371 ShapeExport& ShapeExport::SetXmlNamespace( sal_Int32 nXmlNamespace )
372 {
373     mnXmlNamespace = nXmlNamespace;
374     return *this;
375 }
376 
377 awt::Size ShapeExport::MapSize( const awt::Size& rSize ) const
378 {
379     Size aRetSize( OutputDevice::LogicToLogic( Size( rSize.Width, rSize.Height ), maMapModeSrc, maMapModeDest ) );
380 
381     if ( !aRetSize.Width() )
382         aRetSize.Width()++;
383     if ( !aRetSize.Height() )
384         aRetSize.Height()++;
385     return awt::Size( aRetSize.Width(), aRetSize.Height() );
386 }
387 
388 sal_Bool ShapeExport::NonEmptyText( Reference< XShape > xShape )
389 {
390     Reference< XSimpleText > xText( xShape, UNO_QUERY );
391 
392     return ( xText.is() && xText->getString().getLength() );
393 }
394 
395 ShapeExport& ShapeExport::WriteBezierShape( Reference< XShape > xShape, sal_Bool bClosed )
396 {
397     DBG(printf("write open bezier shape\n"));
398 
399     FSHelperPtr pFS = GetFS();
400     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
401 
402     PolyPolygon aPolyPolygon = EscherPropertyContainer::GetPolyPolygon( xShape );
403     Rectangle aRect( aPolyPolygon.GetBoundRect() );
404     awt::Size size = MapSize( awt::Size( aRect.GetWidth(), aRect.GetHeight() ) );
405 
406     DBG(printf("poly count %d\nsize: %d x %d", aPolyPolygon.Count(), int( size.Width ), int( size.Height )));
407 
408     // non visual shape properties
409     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
410     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
411                           XML_id, I32S( GetNewShapeID( xShape ) ),
412                           XML_name, IDS( Freeform ),
413                           FSEND );
414     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
415     WriteNonVisualProperties( xShape );
416     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
417 
418     // visual shape properties
419     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
420     WriteTransformation( aRect );
421     WritePolyPolygon( aPolyPolygon );
422     Reference< XPropertySet > xProps( xShape, UNO_QUERY );
423     if( xProps.is() ) {
424         if( bClosed )
425             WriteFill( xProps );
426         WriteOutline( xProps );
427     }
428 
429     pFS->endElementNS( mnXmlNamespace, XML_spPr );
430 
431     // write text
432     WriteTextBox( xShape );
433 
434     pFS->endElementNS( mnXmlNamespace, XML_sp );
435 
436     return *this;
437 }
438 
439 ShapeExport& ShapeExport::WriteClosedBezierShape( Reference< XShape > xShape )
440 {
441     return WriteBezierShape( xShape, TRUE );
442 }
443 
444 ShapeExport& ShapeExport::WriteOpenBezierShape( Reference< XShape > xShape )
445 {
446     return WriteBezierShape( xShape, FALSE );
447 }
448 
449 ShapeExport& ShapeExport::WriteCustomShape( Reference< XShape > xShape )
450 {
451     DBG(printf("write custom shape\n"));
452 
453     Reference< XPropertySet > rXPropSet( xShape, UNO_QUERY );
454     SdrObjCustomShape* pShape = (SdrObjCustomShape*) GetSdrObjectFromXShape( xShape );
455     sal_Bool bIsDefaultObject = EscherPropertyContainer::IsDefaultObject( pShape );
456     sal_Bool bPredefinedHandlesUsed = TRUE;
457     OUString sShapeType;
458     sal_uInt32 nMirrorFlags = 0;
459     MSO_SPT eShapeType = EscherPropertyContainer::GetCustomShapeType( xShape, nMirrorFlags, sShapeType );
460     const char* sPresetShape = lcl_GetPresetGeometry( USS( sShapeType ) );
461     DBG(printf("custom shape type: %s ==> %s\n", USS( sShapeType ), sPresetShape));
462     Sequence< PropertyValue > aGeometrySeq;
463     sal_Int32 nAdjustmentValuesIndex = -1;
464     sal_Int32 nAdjustmentsWhichNeedsToBeConverted = 0;
465 
466     if( GETA( CustomShapeGeometry ) ) {
467         DBG(printf("got custom shape geometry\n"));
468         if( mAny >>= aGeometrySeq ) {
469 
470             DBG(printf("got custom shape geometry sequence\n"));
471             for( int i = 0; i < aGeometrySeq.getLength(); i++ ) {
472                 const PropertyValue& rProp = aGeometrySeq[ i ];
473                 DBG(printf("geometry property: %s\n", USS( rProp.Name )));
474 
475                 if( rProp.Name.equalsAscii( "AdjustmentValues" ))
476                     nAdjustmentValuesIndex = i;
477                 else if( rProp.Name.equalsAscii( "Handles" )) {
478                     if( !bIsDefaultObject )
479                         bPredefinedHandlesUsed = FALSE;
480                     // TODO: update nAdjustmentsWhichNeedsToBeConverted here
481                 }
482             }
483         }
484     }
485 
486     FSHelperPtr pFS = GetFS();
487     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
488 
489     // non visual shape properties
490     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
491     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
492                           XML_id, I32S( GetNewShapeID( xShape ) ),
493                           XML_name, IDS( CustomShape ),
494                           FSEND );
495     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
496     WriteNonVisualProperties( xShape );
497     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
498 
499     // visual shape properties
500     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
501     WriteShapeTransformation( xShape );
502     if( nAdjustmentValuesIndex != -1 )
503         WritePresetShape( sPresetShape, eShapeType, bPredefinedHandlesUsed, nAdjustmentsWhichNeedsToBeConverted, aGeometrySeq[ nAdjustmentValuesIndex ] );
504     else
505         WritePresetShape( sPresetShape );
506     if( rXPropSet.is() )
507     {
508         WriteFill( rXPropSet );
509         WriteOutline( rXPropSet );
510     }
511 
512     pFS->endElementNS( mnXmlNamespace, XML_spPr );
513 
514     // write text
515     WriteTextBox( xShape );
516 
517     pFS->endElementNS( mnXmlNamespace, XML_sp );
518 
519     return *this;
520 }
521 
522 ShapeExport& ShapeExport::WriteEllipseShape( Reference< XShape > xShape )
523 {
524     DBG(printf("write ellipse shape\n"));
525 
526     FSHelperPtr pFS = GetFS();
527 
528     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
529 
530     // TODO: arc, section, cut, connector
531 
532     // non visual shape properties
533     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
534     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
535                           XML_id, I32S( GetNewShapeID( xShape ) ),
536                           XML_name, IDS( Ellipse ),
537                           FSEND );
538     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
539     WriteNonVisualProperties( xShape );
540     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
541 
542     // visual shape properties
543     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
544     WriteShapeTransformation( xShape );
545     WritePresetShape( "ellipse" );
546     Reference< XPropertySet > xProps( xShape, UNO_QUERY );
547     if( xProps.is() )
548     {
549         WriteFill( xProps );
550         WriteOutline( xProps );
551     }
552     pFS->endElementNS( mnXmlNamespace, XML_spPr );
553 
554     // write text
555     WriteTextBox( xShape );
556 
557     pFS->endElementNS( mnXmlNamespace, XML_sp );
558 
559     return *this;
560 }
561 
562 ShapeExport& ShapeExport::WriteFill( Reference< XPropertySet > xPropSet )
563 {
564     FillStyle aFillStyle( FillStyle_NONE );
565     xPropSet->getPropertyValue( S( "FillStyle" ) ) >>= aFillStyle;
566 
567     if( aFillStyle == FillStyle_BITMAP )
568     {
569         //DBG(printf ("FillStyle_BITMAP properties\n"));
570         //DBG(dump_pset(rXPropSet));
571     }
572 
573     if( aFillStyle == FillStyle_NONE ||
574         aFillStyle == FillStyle_HATCH )
575         return *this;
576 
577     switch( aFillStyle )
578     {
579     case ::com::sun::star::drawing::FillStyle_SOLID :
580         WriteSolidFill( xPropSet );
581         break;
582     case ::com::sun::star::drawing::FillStyle_GRADIENT :
583         WriteGradientFill( xPropSet );
584         break;
585     case ::com::sun::star::drawing::FillStyle_BITMAP :
586         WriteBlipFill( xPropSet, S( "FillBitmapURL" ) );
587         break;
588     default:
589         ;
590     }
591 
592     return *this;
593 }
594 
595 ShapeExport& ShapeExport::WriteGraphicObjectShape( Reference< XShape > xShape )
596 {
597     DBG(printf("write graphic object shape\n"));
598 
599     if( NonEmptyText( xShape ) )
600     {
601         WriteTextShape( xShape );
602 
603         //DBG(dump_pset(mXPropSet));
604 
605         return *this;
606     }
607 
608     DBG(printf("graphicObject without text\n"));
609 
610     OUString sGraphicURL;
611     Reference< XPropertySet > xShapeProps( xShape, UNO_QUERY );
612     if( !xShapeProps.is() || !( xShapeProps->getPropertyValue( S( "GraphicURL" ) ) >>= sGraphicURL ) )
613     {
614         DBG(printf("no graphic URL found\n"));
615         return *this;
616     }
617 
618     FSHelperPtr pFS = GetFS();
619 
620     pFS->startElementNS( mnXmlNamespace, XML_pic, FSEND );
621 
622     pFS->startElementNS( mnXmlNamespace, XML_nvPicPr, FSEND );
623 
624     OUString sName, sDescr;
625     bool bHaveName = xShapeProps->getPropertyValue( S( "Name" ) ) >>= sName;
626     bool bHaveDesc = xShapeProps->getPropertyValue( S( "Description" ) ) >>= sDescr;
627 
628     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
629                           XML_id,     I32S( GetNewShapeID( xShape ) ),
630                           XML_name,   bHaveName ? USS( sName ) : (OString("Picture ") + OString::valueOf( mnPictureIdMax++ )).getStr(),
631                           XML_descr,  bHaveDesc ? USS( sDescr ) : NULL,
632                           FSEND );
633     // OOXTODO: //cNvPr children: XML_extLst, XML_hlinkClick, XML_hlinkHover
634 
635     pFS->singleElementNS( mnXmlNamespace, XML_cNvPicPr,
636                           // OOXTODO: XML_preferRelativeSize
637                           FSEND );
638 
639     WriteNonVisualProperties( xShape );
640 
641     pFS->endElementNS( mnXmlNamespace, XML_nvPicPr );
642 
643     pFS->startElementNS( mnXmlNamespace, XML_blipFill, FSEND );
644 
645     WriteBlip( sGraphicURL );
646 
647     bool bStretch = false;
648     if( ( xShapeProps->getPropertyValue( S( "FillBitmapStretch" ) ) >>= bStretch ) && bStretch )
649     {
650         WriteStretch();
651     }
652 
653     pFS->endElementNS( mnXmlNamespace, XML_blipFill );
654 
655     // visual shape properties
656     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
657     WriteShapeTransformation( xShape );
658     WritePresetShape( "rect" );
659     pFS->endElementNS( mnXmlNamespace, XML_spPr );
660 
661     pFS->endElementNS( mnXmlNamespace, XML_pic );
662 
663     return *this;
664 }
665 
666 ShapeExport& ShapeExport::WriteConnectorShape( Reference< XShape > xShape )
667 {
668     sal_Bool bFlipH = false;
669     sal_Bool bFlipV = false;
670 
671     DBG(printf("write connector shape\n"));
672 
673     FSHelperPtr pFS = GetFS();
674 
675     const char* sGeometry = "line";
676     Reference< XPropertySet > rXPropSet( xShape, UNO_QUERY );
677     Reference< XPropertyState > rXPropState( xShape, UNO_QUERY );
678     awt::Point aStartPoint, aEndPoint;
679     Reference< XShape > rXShapeA;
680     Reference< XShape > rXShapeB;
681     PropertyState eState;
682     ConnectorType eConnectorType;
683     if( GETAD( EdgeKind ) ) {
684         mAny >>= eConnectorType;
685 
686         switch( eConnectorType ) {
687             case ConnectorType_CURVE:
688                 sGeometry = "curvedConnector3";
689                 break;
690             case ConnectorType_STANDARD:
691                 sGeometry = "bentConnector3";
692                 break;
693             default:
694             case ConnectorType_LINE:
695             case ConnectorType_LINES:
696                 sGeometry = "straightConnector1";
697                 break;
698         }
699 
700         if( GETAD( EdgeStartPoint ) ) {
701             mAny >>= aStartPoint;
702             if( GETAD( EdgeEndPoint ) ) {
703                 mAny >>= aEndPoint;
704             }
705         }
706         GET( rXShapeA, EdgeStartConnection );
707         GET( rXShapeB, EdgeEndConnection );
708     }
709     EscherConnectorListEntry aConnectorEntry( xShape, aStartPoint, rXShapeA, aEndPoint, rXShapeB );
710 
711     Rectangle aRect( Point( aStartPoint.X, aStartPoint.Y ), Point( aEndPoint.X, aEndPoint.Y ) );
712     if( aRect.getWidth() < 0 ) {
713         bFlipH = TRUE;
714         aRect.setX( aEndPoint.X );
715         aRect.setWidth( aStartPoint.X - aEndPoint.X );
716     }
717 
718     if( aRect.getHeight() < 0 ) {
719         bFlipV = TRUE;
720         aRect.setY( aEndPoint.Y );
721         aRect.setHeight( aStartPoint.Y - aEndPoint.Y );
722     }
723 
724     pFS->startElementNS( mnXmlNamespace, XML_cxnSp, FSEND );
725 
726     // non visual shape properties
727     pFS->startElementNS( mnXmlNamespace, XML_nvCxnSpPr, FSEND );
728     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
729                           XML_id, I32S( GetNewShapeID( xShape ) ),
730                           XML_name, IDS( Line ),
731                           FSEND );
732     // non visual connector shape drawing properties
733     pFS->startElementNS( mnXmlNamespace, XML_cNvCxnSpPr, FSEND );
734     WriteConnectorConnections( aConnectorEntry, GetShapeID( rXShapeA ), GetShapeID( rXShapeB ) );
735     pFS->endElementNS( mnXmlNamespace, XML_cNvCxnSpPr );
736     pFS->singleElementNS( mnXmlNamespace, XML_nvPr, FSEND );
737     pFS->endElementNS( mnXmlNamespace, XML_nvCxnSpPr );
738 
739     // visual shape properties
740     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
741     WriteTransformation( aRect, bFlipH, bFlipV );
742     // TODO: write adjustments (ppt export doesn't work well there either)
743     WritePresetShape( sGeometry );
744     Reference< XPropertySet > xShapeProps( xShape, UNO_QUERY );
745     if( xShapeProps.is() )
746         WriteOutline( xShapeProps );
747     pFS->endElementNS( mnXmlNamespace, XML_spPr );
748 
749     // write text
750     WriteTextBox( xShape );
751 
752     pFS->endElementNS( mnXmlNamespace, XML_cxnSp );
753 
754     return *this;
755 }
756 
757 ShapeExport& ShapeExport::WriteLineShape( Reference< XShape > xShape )
758 {
759     sal_Bool bFlipH = false;
760     sal_Bool bFlipV = false;
761 
762     DBG(printf("write line shape\n"));
763 
764     FSHelperPtr pFS = GetFS();
765 
766     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
767 
768     PolyPolygon aPolyPolygon = EscherPropertyContainer::GetPolyPolygon( xShape );
769     if( aPolyPolygon.Count() == 1 && aPolyPolygon[ 0 ].GetSize() == 2)
770     {
771         const Polygon& rPoly = aPolyPolygon[ 0 ];
772 
773         bFlipH = ( rPoly[ 0 ].X() > rPoly[ 1 ].X() );
774         bFlipV = ( rPoly[ 0 ].Y() > rPoly[ 1 ].Y() );
775     }
776 
777     // non visual shape properties
778     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
779     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
780                           XML_id, I32S( GetNewShapeID( xShape ) ),
781                           XML_name, IDS( Line ),
782                           FSEND );
783     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
784     WriteNonVisualProperties( xShape );
785     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
786 
787     // visual shape properties
788     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
789     WriteShapeTransformation( xShape, bFlipH, bFlipV );
790     WritePresetShape( "line" );
791     Reference< XPropertySet > xShapeProps( xShape, UNO_QUERY );
792     if( xShapeProps.is() )
793         WriteOutline( xShapeProps );
794     pFS->endElementNS( mnXmlNamespace, XML_spPr );
795 
796     // write text
797     WriteTextBox( xShape );
798 
799     pFS->endElementNS( mnXmlNamespace, XML_sp );
800 
801     return *this;
802 }
803 
804 ShapeExport& ShapeExport::WriteNonVisualDrawingProperties( Reference< XShape > xShape, const char* pName )
805 {
806     GetFS()->singleElementNS( mnXmlNamespace, XML_cNvPr,
807                               XML_id, I32S( GetNewShapeID( xShape ) ),
808                               XML_name, pName,
809                               FSEND );
810 
811     return *this;
812 }
813 
814 ShapeExport& ShapeExport::WriteNonVisualProperties( Reference< XShape > )
815 {
816     // Override to generate //nvPr elements.
817     return *this;
818 }
819 
820 ShapeExport& ShapeExport::WriteRectangleShape( Reference< XShape > xShape )
821 {
822     DBG(printf("write rectangle shape\n"));
823 
824     FSHelperPtr pFS = GetFS();
825 
826     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
827 
828     sal_Int32 nRadius = 0;
829 
830     Reference< XPropertySet > xShapeProps( xShape, UNO_QUERY );
831     if( xShapeProps.is() )
832     {
833         xShapeProps->getPropertyValue( S( "CornerRadius" ) ) >>= nRadius;
834     }
835 
836     if( nRadius )
837     {
838         nRadius = MapSize( awt::Size( nRadius, 0 ) ).Width;
839     }
840 
841     // non visual shape properties
842     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
843     pFS->singleElementNS( mnXmlNamespace, XML_cNvPr,
844                           XML_id, I32S( GetNewShapeID( xShape ) ),
845                           XML_name, IDS( Rectangle ),
846                           FSEND );
847     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, FSEND );
848     WriteNonVisualProperties( xShape );
849     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
850 
851     // visual shape properties
852     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
853     WriteShapeTransformation( xShape );
854     WritePresetShape( "rect" );
855     Reference< XPropertySet > xProps( xShape, UNO_QUERY );
856     if( xProps.is() )
857     {
858         WriteFill( xProps );
859         WriteOutline( xProps );
860     }
861     pFS->endElementNS( mnXmlNamespace, XML_spPr );
862 
863     // write text
864     WriteTextBox( xShape );
865 
866     pFS->endElementNS( mnXmlNamespace, XML_sp );
867 
868     return *this;
869 }
870 
871 typedef ShapeExport& (ShapeExport::*ShapeConverter)( Reference< XShape > );
872 typedef std::hash_map< const char*, ShapeConverter, std::hash<const char*>, StringCheck> NameToConvertMapType;
873 
874 static const NameToConvertMapType& lcl_GetConverters()
875 {
876     static bool shape_map_inited = false;
877     static NameToConvertMapType shape_converters;
878     if( shape_map_inited )
879     {
880         return shape_converters;
881     }
882 
883     shape_converters[ "com.sun.star.drawing.ClosedBezierShape" ]        = &ShapeExport::WriteClosedBezierShape;
884     shape_converters[ "com.sun.star.drawing.ConnectorShape" ]           = &ShapeExport::WriteConnectorShape;
885     shape_converters[ "com.sun.star.drawing.CustomShape" ]              = &ShapeExport::WriteCustomShape;
886     shape_converters[ "com.sun.star.drawing.EllipseShape" ]             = &ShapeExport::WriteEllipseShape;
887     shape_converters[ "com.sun.star.drawing.GraphicObjectShape" ]       = &ShapeExport::WriteGraphicObjectShape;
888     shape_converters[ "com.sun.star.drawing.LineShape" ]                = &ShapeExport::WriteLineShape;
889     shape_converters[ "com.sun.star.drawing.OpenBezierShape" ]          = &ShapeExport::WriteOpenBezierShape;
890     shape_converters[ "com.sun.star.drawing.RectangleShape" ]           = &ShapeExport::WriteRectangleShape;
891     shape_converters[ "com.sun.star.drawing.TextShape" ]                = &ShapeExport::WriteTextShape;
892     shape_converters[ "com.sun.star.presentation.DateTimeShape" ]       = &ShapeExport::WriteTextShape;
893     shape_converters[ "com.sun.star.presentation.FooterShape" ]         = &ShapeExport::WriteTextShape;
894     shape_converters[ "com.sun.star.presentation.HeaderShape" ]         = &ShapeExport::WriteTextShape;
895     shape_converters[ "com.sun.star.presentation.NotesShape" ]          = &ShapeExport::WriteTextShape;
896     shape_converters[ "com.sun.star.presentation.OutlinerShape" ]       = &ShapeExport::WriteTextShape;
897     shape_converters[ "com.sun.star.presentation.SlideNumberShape" ]    = &ShapeExport::WriteTextShape;
898     shape_converters[ "com.sun.star.presentation.TitleTextShape" ]      = &ShapeExport::WriteTextShape;
899     shape_map_inited = true;
900 
901     return shape_converters;
902 }
903 
904 ShapeExport& ShapeExport::WriteShape( Reference< XShape > xShape )
905 {
906     OUString sShapeType = xShape->getShapeType();
907     DBG( printf( "write shape: %s\n", USS( sShapeType ) ) );
908     NameToConvertMapType::const_iterator aConverter = lcl_GetConverters().find( USS( sShapeType ) );
909     if( aConverter == lcl_GetConverters().end() )
910     {
911         DBG( printf( "unknown shape\n" ) );
912         return WriteUnknownShape( xShape );
913     }
914     (this->*(aConverter->second))( xShape );
915 
916     return *this;
917 }
918 
919 ShapeExport& ShapeExport::WriteTextBox( Reference< XShape > xShape )
920 {
921     if( NonEmptyText( xShape ) )
922     {
923         FSHelperPtr pFS = GetFS();
924 
925         pFS->startElementNS( mnXmlNamespace, XML_txBody, FSEND );
926         WriteText( xShape );
927         pFS->endElementNS( mnXmlNamespace, XML_txBody );
928     }
929 
930     return *this;
931 }
932 
933 ShapeExport& ShapeExport::WriteTextShape( Reference< XShape > xShape )
934 {
935     FSHelperPtr pFS = GetFS();
936 
937     pFS->startElementNS( mnXmlNamespace, XML_sp, FSEND );
938 
939     // non visual shape properties
940     pFS->startElementNS( mnXmlNamespace, XML_nvSpPr, FSEND );
941     WriteNonVisualDrawingProperties( xShape, IDS( TextShape ) );
942     pFS->singleElementNS( mnXmlNamespace, XML_cNvSpPr, XML_txBox, "1", FSEND );
943     WriteNonVisualProperties( xShape );
944     pFS->endElementNS( mnXmlNamespace, XML_nvSpPr );
945 
946     // visual shape properties
947     pFS->startElementNS( mnXmlNamespace, XML_spPr, FSEND );
948     WriteShapeTransformation( xShape );
949     WritePresetShape( "rect" );
950     WriteBlipFill( Reference< XPropertySet >(xShape, UNO_QUERY ), S( "GraphicURL" ) );
951     pFS->endElementNS( mnXmlNamespace, XML_spPr );
952 
953     WriteTextBox( xShape );
954 
955     pFS->endElementNS( mnXmlNamespace, XML_sp );
956 
957     return *this;
958 }
959 
960 ShapeExport& ShapeExport::WriteUnknownShape( Reference< XShape > )
961 {
962     // Override this method to do something useful.
963     return *this;
964 }
965 
966 size_t ShapeExport::ShapeHash::operator()( const ::com::sun::star::uno::Reference < ::com::sun::star::drawing::XShape > rXShape ) const
967 {
968     return maHashFunction( USS( rXShape->getShapeType() ) );
969 }
970 
971 sal_Int32 ShapeExport::GetNewShapeID( const Reference< XShape > rXShape )
972 {
973     sal_Int32 nID = GetFB()->GetUniqueId();
974 
975     maShapeMap[ rXShape ] = nID;
976 
977     return nID;
978 }
979 
980 sal_Int32 ShapeExport::GetShapeID( const Reference< XShape > rXShape )
981 {
982     ShapeHashMap::const_iterator aIter = maShapeMap.find( rXShape );
983 
984     if( aIter == maShapeMap.end() )
985         return -1;
986 
987     return aIter->second;
988 }
989 
990 } }
991