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 :
21 : #include <functional>
22 : #include <boost/bind.hpp>
23 :
24 : #include <com/sun/star/awt/Point.hpp>
25 : #include <com/sun/star/awt/Size.hpp>
26 : #include <com/sun/star/xml/dom/XDocument.hpp>
27 : #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
28 : #include <rtl/ustrbuf.hxx>
29 : #include <editeng/unoprnms.hxx>
30 : #include "oox/drawingml/textbody.hxx"
31 : #include "oox/drawingml/textparagraph.hxx"
32 : #include "oox/drawingml/textrun.hxx"
33 : #include "oox/drawingml/diagram/diagram.hxx"
34 : #include "oox/drawingml/fillproperties.hxx"
35 : #include "oox/ppt/pptshapegroupcontext.hxx"
36 : #include "oox/ppt/pptshape.hxx"
37 :
38 : #include "diagramlayoutatoms.hxx"
39 : #include "diagramfragmenthandler.hxx"
40 :
41 : #include <iostream>
42 : #include <fstream>
43 :
44 : using namespace ::com::sun::star;
45 :
46 : namespace oox { namespace drawingml {
47 :
48 : namespace dgm {
49 :
50 0 : void Connection::dump()
51 : {
52 : OSL_TRACE("dgm: cnx modelId %s, srcId %s, dstId %s, parTransId %s, presId %s, sibTransId %s, srcOrd %d, dstOrd %d",
53 : OUSTRING_TO_CSTR( msModelId ),
54 : OUSTRING_TO_CSTR( msSourceId ),
55 : OUSTRING_TO_CSTR( msDestId ),
56 : OUSTRING_TO_CSTR( msParTransId ),
57 : OUSTRING_TO_CSTR( msPresId ),
58 : OUSTRING_TO_CSTR( msSibTransId ),
59 : mnSourceOrder,
60 : mnDestOrder );
61 0 : }
62 :
63 0 : void Point::dump()
64 : {
65 : OSL_TRACE( "dgm: pt text %x, cnxId %s, modelId %s, type %d",
66 : mpShape.get(),
67 : OUSTRING_TO_CSTR( msCnxId ),
68 : OUSTRING_TO_CSTR( msModelId ),
69 : mnType );
70 0 : }
71 :
72 : } // dgm namespace
73 :
74 0 : DiagramData::DiagramData()
75 0 : : mpFillProperties( new FillProperties )
76 : {
77 0 : }
78 :
79 0 : void DiagramData::dump()
80 : {
81 : OSL_TRACE("Dgm: DiagramData # of cnx: %zu", maConnections.size() );
82 : std::for_each( maConnections.begin(), maConnections.end(),
83 0 : boost::bind( &dgm::Connection::dump, _1 ) );
84 : OSL_TRACE("Dgm: DiagramData # of pt: %zu", maPoints.size() );
85 : std::for_each( maPoints.begin(), maPoints.end(),
86 0 : boost::bind( &dgm::Point::dump, _1 ) );
87 0 : }
88 :
89 :
90 0 : void Diagram::setData( const DiagramDataPtr & pData)
91 : {
92 0 : mpData = pData;
93 0 : }
94 :
95 :
96 0 : void Diagram::setLayout( const DiagramLayoutPtr & pLayout)
97 : {
98 0 : mpLayout = pLayout;
99 0 : }
100 :
101 : #if OSL_DEBUG_LEVEL > 1
102 : OString normalizeDotName( const OUString& rStr )
103 : {
104 : OUStringBuffer aBuf;
105 : aBuf.append('N');
106 :
107 : const sal_Int32 nLen(rStr.getLength());
108 : sal_Int32 nCurrIndex(0);
109 : while( nCurrIndex < nLen )
110 : {
111 : const sal_Int32 aChar=rStr.iterateCodePoints(&nCurrIndex);
112 : if( aChar != '-' && aChar != '{' && aChar != '}' )
113 : aBuf.append((sal_Unicode)aChar);
114 : }
115 :
116 : return OUStringToOString(aBuf.makeStringAndClear(),
117 : RTL_TEXTENCODING_UTF8);
118 : }
119 : #endif
120 :
121 0 : static sal_Int32 calcDepth( const OUString& rNodeName,
122 : const dgm::Connections& rCnx )
123 : {
124 : // find length of longest path in 'isChild' graph, ending with rNodeName
125 0 : dgm::Connections::const_iterator aCurrCxn( rCnx.begin() );
126 0 : const dgm::Connections::const_iterator aEndCxn( rCnx.end() );
127 0 : while( aCurrCxn != aEndCxn )
128 : {
129 0 : if( !aCurrCxn->msParTransId.isEmpty() &&
130 0 : !aCurrCxn->msSibTransId.isEmpty() &&
131 0 : !aCurrCxn->msSourceId.isEmpty() &&
132 0 : !aCurrCxn->msDestId.isEmpty() &&
133 0 : aCurrCxn->mnType != XML_presOf &&
134 0 : aCurrCxn->mnType != XML_presParOf &&
135 0 : rNodeName == aCurrCxn->msDestId )
136 : {
137 0 : return calcDepth(aCurrCxn->msSourceId,
138 0 : rCnx) + 1;
139 : }
140 0 : ++aCurrCxn;
141 : }
142 :
143 0 : return 0;
144 : }
145 :
146 :
147 0 : void Diagram::build( )
148 : {
149 : // build name-object maps
150 :
151 :
152 : #if OSL_DEBUG_LEVEL > 1
153 : std::ofstream output("/tmp/tree.dot");
154 :
155 : output << "digraph datatree {" << std::endl;
156 : #endif
157 :
158 0 : dgm::Points::iterator aCurrPoint( getData()->getPoints( ).begin() );
159 0 : dgm::Points::iterator aEndPoint( getData()->getPoints( ).end() );
160 0 : while( aCurrPoint != aEndPoint )
161 : {
162 : #if OSL_DEBUG_LEVEL > 1
163 : output << "\t"
164 : << normalizeDotName(aCurrPoint->msModelId).getStr()
165 : << "[";
166 :
167 : if( !aCurrPoint->msPresentationLayoutName.isEmpty() )
168 : output << "label=\""
169 : << OUStringToOString(
170 : aCurrPoint->msPresentationLayoutName,
171 : RTL_TEXTENCODING_UTF8).getStr() << "\", ";
172 : else
173 : output << "label=\""
174 : << OUStringToOString(
175 : aCurrPoint->msModelId,
176 : RTL_TEXTENCODING_UTF8).getStr() << "\", ";
177 :
178 : switch( aCurrPoint->mnType )
179 : {
180 : case XML_doc: output << "style=filled, color=red"; break;
181 : case XML_asst: output << "style=filled, color=green"; break;
182 : default:
183 : case XML_node: output << "style=filled, color=blue"; break;
184 : case XML_pres: output << "style=filled, color=yellow"; break;
185 : case XML_parTrans: output << "color=grey"; break;
186 : case XML_sibTrans: output << " "; break;
187 : }
188 :
189 : output << "];" << std::endl;
190 :
191 : // does currpoint have any text set?
192 : if( aCurrPoint->mpShape &&
193 : aCurrPoint->mpShape->getTextBody() &&
194 : !aCurrPoint->mpShape->getTextBody()->getParagraphs().empty() &&
195 : !aCurrPoint->mpShape->getTextBody()->getParagraphs().front()->getRuns().empty() )
196 : {
197 : static sal_Int32 nCount=0;
198 :
199 : output << "\t"
200 : << "textNode" << nCount
201 : << " ["
202 : << "label=\""
203 : << OUStringToOString(
204 : aCurrPoint->mpShape->getTextBody()->getParagraphs().front()->getRuns().front()->getText(),
205 : RTL_TEXTENCODING_UTF8).getStr()
206 : << "\"" << "];" << std::endl;
207 : output << "\t"
208 : << normalizeDotName(aCurrPoint->msModelId).getStr()
209 : << " -> "
210 : << "textNode" << nCount++
211 : << ";" << std::endl;
212 : }
213 : #endif
214 :
215 0 : const bool bInserted1=getData()->getPointNameMap().insert(
216 0 : std::make_pair(aCurrPoint->msModelId,&(*aCurrPoint))).second;
217 : (void)bInserted1;
218 :
219 : OSL_ENSURE(bInserted1,"Diagram::build(): non-unique point model id");
220 :
221 0 : if( !aCurrPoint->msPresentationLayoutName.isEmpty() )
222 : {
223 : DiagramData::PointsNameMap::value_type::second_type& rVec=
224 0 : getData()->getPointsPresNameMap()[aCurrPoint->msPresentationLayoutName];
225 0 : rVec.push_back(&(*aCurrPoint));
226 : }
227 0 : ++aCurrPoint;
228 : }
229 :
230 0 : dgm::Connections::const_iterator aCurrCxn( getData()->getConnections( ).begin() );
231 0 : const dgm::Connections::const_iterator aEndCxn( getData()->getConnections( ).end() );
232 0 : while( aCurrCxn != aEndCxn )
233 : {
234 : #if OSL_DEBUG_LEVEL > 1
235 : if( !aCurrCxn->msParTransId.isEmpty() ||
236 : !aCurrCxn->msSibTransId.isEmpty() )
237 : {
238 : if( !aCurrCxn->msSourceId.isEmpty() ||
239 : !aCurrCxn->msDestId.isEmpty() )
240 : {
241 : output << "\t"
242 : << normalizeDotName(aCurrCxn->msSourceId).getStr()
243 : << " -> "
244 : << normalizeDotName(aCurrCxn->msParTransId).getStr()
245 : << " -> "
246 : << normalizeDotName(aCurrCxn->msSibTransId).getStr()
247 : << " -> "
248 : << normalizeDotName(aCurrCxn->msDestId).getStr()
249 : << " [style=dotted,"
250 : << ((aCurrCxn->mnType == XML_presOf) ? " color=red, " : ((aCurrCxn->mnType == XML_presParOf) ? " color=green, " : " "))
251 : << "label=\""
252 : << OUStringToOString(aCurrCxn->msModelId,
253 : RTL_TEXTENCODING_UTF8 ).getStr()
254 : << "\"];" << std::endl;
255 : }
256 : else
257 : {
258 : output << "\t"
259 : << normalizeDotName(aCurrCxn->msParTransId).getStr()
260 : << " -> "
261 : << normalizeDotName(aCurrCxn->msSibTransId).getStr()
262 : << " ["
263 : << ((aCurrCxn->mnType == XML_presOf) ? " color=red, " : ((aCurrCxn->mnType == XML_presParOf) ? " color=green, " : " "))
264 : << "label=\""
265 : << OUStringToOString(aCurrCxn->msModelId,
266 : RTL_TEXTENCODING_UTF8 ).getStr()
267 : << "\"];" << std::endl;
268 : }
269 : }
270 : else if( !aCurrCxn->msSourceId.isEmpty() ||
271 : !aCurrCxn->msDestId.isEmpty() )
272 : output << "\t"
273 : << normalizeDotName(aCurrCxn->msSourceId).getStr()
274 : << " -> "
275 : << normalizeDotName(aCurrCxn->msDestId).getStr()
276 : << " [label=\""
277 : << OUStringToOString(aCurrCxn->msModelId,
278 : RTL_TEXTENCODING_UTF8 ).getStr()
279 : << ((aCurrCxn->mnType == XML_presOf) ? "\", color=red]" : ((aCurrCxn->mnType == XML_presParOf) ? "\", color=green]" : "\"]"))
280 : << ";" << std::endl;
281 : #endif
282 :
283 0 : const bool bInserted1=getData()->getConnectionNameMap().insert(
284 0 : std::make_pair(aCurrCxn->msModelId,&(*aCurrCxn))).second;
285 : (void)bInserted1;
286 :
287 : OSL_ENSURE(bInserted1,"Diagram::build(): non-unique connection model id");
288 :
289 0 : if( aCurrCxn->mnType == XML_presOf )
290 : {
291 0 : DiagramData::StringMap::value_type::second_type& rVec=getData()->getPresOfNameMap()[aCurrCxn->msDestId];
292 : rVec.push_back(
293 : std::make_pair(
294 0 : aCurrCxn->msSourceId,sal_Int32(0)));
295 : }
296 :
297 0 : ++aCurrCxn;
298 : }
299 :
300 : // assign outline levels
301 0 : DiagramData::StringMap::iterator aPresOfIter=getData()->getPresOfNameMap().begin();
302 0 : const DiagramData::StringMap::iterator aPresOfEnd=getData()->getPresOfNameMap().end();
303 0 : while( aPresOfIter != aPresOfEnd )
304 : {
305 0 : DiagramData::StringMap::value_type::second_type::iterator aPresOfNodeIterCalcLevel=aPresOfIter->second.begin();
306 0 : const DiagramData::StringMap::value_type::second_type::iterator aPresOfNodeEnd=aPresOfIter->second.end();
307 0 : while(aPresOfNodeIterCalcLevel != aPresOfNodeEnd)
308 : {
309 0 : const sal_Int32 nDepth=calcDepth(aPresOfNodeIterCalcLevel->first,
310 0 : getData()->getConnections());
311 0 : aPresOfNodeIterCalcLevel->second = nDepth != 0 ? nDepth : -1;
312 0 : ++aPresOfNodeIterCalcLevel;
313 : }
314 :
315 0 : ++aPresOfIter;
316 : }
317 :
318 : #if OSL_DEBUG_LEVEL > 1
319 : output << "}" << std::endl;
320 : #endif
321 0 : }
322 :
323 :
324 0 : void Diagram::addTo( const ShapePtr & pParentShape )
325 : {
326 : // collect data, init maps
327 0 : build( );
328 :
329 : // create Shape hierarchy
330 0 : ShapeCreationVisitor aCreationVisitor(pParentShape, *this);
331 0 : if( mpLayout->getNode() )
332 0 : mpLayout->getNode()->accept( aCreationVisitor );
333 :
334 0 : pParentShape->setDiagramDoms( getDomsAsPropertyValues() );
335 0 : }
336 :
337 0 : uno::Sequence<beans::PropertyValue> Diagram::getDomsAsPropertyValues() const
338 : {
339 0 : sal_Int32 length = maMainDomMap.size();
340 :
341 0 : if ( 0 < maDataRelsMap.getLength() )
342 0 : ++length;
343 :
344 0 : uno::Sequence<beans::PropertyValue> aValue(length);
345 0 : beans::PropertyValue* pValue = aValue.getArray();
346 0 : for (DiagramDomMap::const_iterator i = maMainDomMap.begin();
347 0 : i != maMainDomMap.end();
348 : ++i)
349 : {
350 0 : pValue[0].Name = i->first;
351 0 : pValue[0].Value = uno::makeAny(i->second);
352 0 : ++pValue;
353 : }
354 :
355 0 : if ( 0 < maDataRelsMap.getLength() )
356 : {
357 0 : pValue[0].Name = OUString("OOXDiagramDataRels");
358 0 : pValue[0].Value = uno::makeAny ( maDataRelsMap );
359 0 : ++pValue;
360 : }
361 :
362 0 : return aValue;
363 : }
364 :
365 0 : uno::Reference<xml::dom::XDocument> loadFragment(
366 : core::XmlFilterBase& rFilter,
367 : const OUString& rFragmentPath )
368 : {
369 : // load diagramming fragments into DOM representation, that later
370 : // gets serialized back to SAX events and parsed
371 0 : return rFilter.importFragment( rFragmentPath );
372 : }
373 :
374 0 : uno::Reference<xml::dom::XDocument> loadFragment(
375 : core::XmlFilterBase& rFilter,
376 : const rtl::Reference< core::FragmentHandler >& rxHandler )
377 : {
378 0 : return loadFragment( rFilter, rxHandler->getFragmentPath() );
379 : }
380 :
381 0 : void importFragment( core::XmlFilterBase& rFilter,
382 : const uno::Reference<xml::dom::XDocument>& rXDom,
383 : const char* pDocName,
384 : const DiagramPtr& pDiagram,
385 : const rtl::Reference< core::FragmentHandler >& rxHandler )
386 : {
387 0 : DiagramDomMap& rMainDomMap = pDiagram->getDomMap();
388 0 : rMainDomMap[OUString::createFromAscii(pDocName)] = rXDom;
389 :
390 : uno::Reference<xml::sax::XFastSAXSerializable> xSerializer(
391 0 : rXDom, uno::UNO_QUERY_THROW);
392 :
393 : // now serialize DOM tree into internal data structures
394 0 : rFilter.importFragment( rxHandler, xSerializer );
395 0 : }
396 :
397 0 : void loadDiagram( ShapePtr& pShape,
398 : core::XmlFilterBase& rFilter,
399 : const OUString& rDataModelPath,
400 : const OUString& rLayoutPath,
401 : const OUString& rQStylePath,
402 : const OUString& rColorStylePath )
403 : {
404 0 : DiagramPtr pDiagram( new Diagram() );
405 :
406 0 : DiagramDataPtr pData( new DiagramData() );
407 0 : pDiagram->setData( pData );
408 :
409 0 : DiagramLayoutPtr pLayout( new DiagramLayout() );
410 0 : pDiagram->setLayout( pLayout );
411 :
412 : // data
413 0 : if( !rDataModelPath.isEmpty() )
414 : {
415 : rtl::Reference< core::FragmentHandler > xRefDataModel(
416 0 : new DiagramDataFragmentHandler( rFilter, rDataModelPath, pData ));
417 :
418 : importFragment(rFilter,
419 : loadFragment(rFilter,xRefDataModel),
420 : "OOXData",
421 : pDiagram,
422 0 : xRefDataModel);
423 :
424 0 : pDiagram->getDataRelsMap() = pShape->resolveRelationshipsOfTypeFromOfficeDoc( rFilter,
425 0 : xRefDataModel->getFragmentPath(), "image" );
426 :
427 :
428 : // Pass the info to pShape
429 0 : for( ::std::vector<OUString>::const_iterator aIt = pData->getExtDrawings().begin(), aEnd = pData->getExtDrawings().end();
430 : aIt != aEnd; ++aIt )
431 0 : pShape->addExtDrawingRelId( *aIt );
432 : }
433 :
434 : // extLst is present, lets bet on that and ignore the rest of the data from here
435 0 : if( !pData->getExtDrawings().size() )
436 : {
437 : // layout
438 0 : if( !rLayoutPath.isEmpty() )
439 : {
440 : rtl::Reference< core::FragmentHandler > xRefLayout(
441 0 : new DiagramLayoutFragmentHandler( rFilter, rLayoutPath, pLayout ));
442 :
443 : importFragment(rFilter,
444 : loadFragment(rFilter,xRefLayout),
445 : "OOXLayout",
446 : pDiagram,
447 0 : xRefLayout);
448 : }
449 :
450 : // style
451 0 : if( !rQStylePath.isEmpty() )
452 : {
453 : rtl::Reference< core::FragmentHandler > xRefQStyle(
454 0 : new DiagramQStylesFragmentHandler( rFilter, rQStylePath, pDiagram->getStyles() ));
455 :
456 : importFragment(rFilter,
457 : loadFragment(rFilter,xRefQStyle),
458 : "OOXStyle",
459 : pDiagram,
460 0 : xRefQStyle);
461 : }
462 :
463 : // colors
464 0 : if( !rColorStylePath.isEmpty() )
465 : {
466 : rtl::Reference< core::FragmentHandler > xRefColorStyle(
467 0 : new ColorFragmentHandler( rFilter, rColorStylePath, pDiagram->getColors() ));
468 :
469 : importFragment(rFilter,
470 : loadFragment(rFilter,xRefColorStyle),
471 : "OOXColor",
472 : pDiagram,
473 0 : xRefColorStyle);
474 : }
475 : } else {
476 : // We still want to add the XDocuments to the DiagramDomMap
477 0 : DiagramDomMap& rMainDomMap = pDiagram->getDomMap();
478 0 : rMainDomMap[OUString("OOXLayout")] = loadFragment(rFilter,rLayoutPath);
479 0 : rMainDomMap[OUString("OOXStyle")] = loadFragment(rFilter,rQStylePath);
480 0 : rMainDomMap[OUString("OOXColor")] = loadFragment(rFilter,rColorStylePath);
481 : }
482 :
483 : // diagram loaded. now lump together & attach to shape
484 0 : pDiagram->addTo(pShape);
485 0 : }
486 :
487 0 : void loadDiagram( const ShapePtr& pShape,
488 : core::XmlFilterBase& rFilter,
489 : const uno::Reference<xml::dom::XDocument>& rXDataModelDom,
490 : const uno::Reference<xml::dom::XDocument>& rXLayoutDom,
491 : const uno::Reference<xml::dom::XDocument>& rXQStyleDom,
492 : const uno::Reference<xml::dom::XDocument>& rXColorStyleDom )
493 : {
494 0 : DiagramPtr pDiagram( new Diagram() );
495 :
496 0 : DiagramDataPtr pData( new DiagramData() );
497 0 : pDiagram->setData( pData );
498 :
499 0 : DiagramLayoutPtr pLayout( new DiagramLayout() );
500 0 : pDiagram->setLayout( pLayout );
501 :
502 0 : OUString aEmpty;
503 :
504 : // data
505 0 : if( rXDataModelDom.is() )
506 : importFragment(rFilter,
507 : rXDataModelDom,
508 : "OOXData",
509 : pDiagram,
510 0 : new DiagramDataFragmentHandler( rFilter, aEmpty, pData ));
511 :
512 : // layout
513 0 : if( rXLayoutDom.is() )
514 : importFragment(rFilter,
515 : rXLayoutDom,
516 : "OOXLayout",
517 : pDiagram,
518 0 : new DiagramLayoutFragmentHandler( rFilter, aEmpty, pLayout ));
519 :
520 : // style
521 0 : if( rXQStyleDom.is() )
522 : importFragment(rFilter,
523 : rXQStyleDom,
524 : "OOXStyle",
525 : pDiagram,
526 0 : new DiagramQStylesFragmentHandler( rFilter, aEmpty, pDiagram->getStyles() ));
527 :
528 : // colors
529 0 : if( rXColorStyleDom.is() )
530 : importFragment(rFilter,
531 : rXColorStyleDom,
532 : "OOXColor",
533 : pDiagram,
534 0 : new ColorFragmentHandler( rFilter, aEmpty, pDiagram->getColors() ));
535 :
536 : // diagram loaded. now lump together & attach to shape
537 0 : pDiagram->addTo(pShape);
538 0 : }
539 :
540 0 : } }
541 :
542 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|