File: VisioRender.cpp

package info (click to toggle)
graphviz 2.26.3-5%2Bsqueeze3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 63,044 kB
  • ctags: 25,930
  • sloc: ansic: 212,134; sh: 20,316; cpp: 7,239; makefile: 4,211; yacc: 3,335; xml: 2,450; tcl: 1,900; cs: 1,890; objc: 1,149; perl: 829; lex: 364; awk: 171; python: 41; ruby: 35; php: 26
file content (499 lines) | stat: -rw-r--r-- 16,174 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* $Id: VisioRender.cpp,v 1.8 2009/06/03 01:10:57 ellson Exp $ $Revision: 1.8 $ */
/* vim:set shiftwidth=4 ts=8: */

/**********************************************************
 *      This software is part of the graphviz package      *
 *                http://www.graphviz.org/                 *
 *                                                         *
 *            Copyright (c) 1994-2004 AT&T Corp.           *
 *                and is licensed under the                *
 *            Common Public License, Version 1.0           *
 *                      by AT&T Corp.                      *
 *                                                         *
 *        Information and Software Systems Research        *
 *              AT&T Research, Florham Park NJ             *
 **********************************************************/

#ifdef _MSC_VER
#include <cfloat>
#define isfinite _finite
#endif

#ifdef __GNUC__
#include <cmath>
#endif

#include "VisioRender.h"

#include "const.h"
#include "macros.h"
#include "gvcjob.h"
#include "gvio.h"

namespace Visio
{
	using namespace std;
	
	static const float INCHES_PER_POINT = 1.0 / 72.0;
	static const float ZERO_ADJUST = 0.125;
	
	enum ConLineRouteExt
	{
		LORouteExtDefault = 0,
		LORouteExtStraight = 1,
		LORouteExtNURBS	= 2
	};
	
	enum ShapeRouteStyle
	{
		LORouteDefault = 0,
		LORouteRightAngle = 1,
		LORouteStraight = 2,
		LORouteCenterToCenter = 16
	};
	
	Render::Render():
		_pageId(0),
		_shapeId(0),
		_hyperlinkId(0),
		_inComponent(false),
		_graphics(),
		_texts(),
		_hyperlinks(),
		_nodeIds()
	{
	}

	Render::~Render()
	{
	}

	void Render::BeginGraph(GVJ_t* job)
	{
		gvputs(job, "<VisioDocument xmlns='http://schemas.microsoft.com/visio/2003/core'>\n");
		gvputs(job, "<Pages>\n");
	}

	void Render::EndGraph(GVJ_t* job)
	{
		gvputs(job, "</Pages>\n");
		gvputs(job, "</VisioDocument>\n");
	}

	void Render::BeginPage(GVJ_t* job)
	{
		gvprintf(job, "<Page ID='%d'>\n", ++_pageId);
		
		gvputs(job, "<PageSheet>\n");
		gvputs(job, "<PageProps>\n");
		gvprintf(job, "<PageWidth>%f</PageWidth>\n", job->width * INCHES_PER_POINT);
		gvprintf(job, "<PageHeight>%f</PageHeight>\n", job->height * INCHES_PER_POINT);
		gvputs(job, "</PageProps>\n");
		gvputs(job, "</PageSheet>\n");
		
		gvputs(job, "<Shapes>");
	}

	void Render::EndPage(GVJ_t* job)
	{
		gvputs(job, "</Shapes>\n");
		gvputs(job, "</Page>\n");
	}

	void Render::BeginNode(GVJ_t* job)
	{
		_inComponent = true;
		ClearGraphicsAndTexts();
	}

	void Render::EndNode(GVJ_t* job)
	{
		_inComponent = false;
		
		unsigned int outerShapeId = 0;
		switch (_graphics.size())
		{
		case 0:
			/* no graphics to render */
			break;
		case 1:
			/* single graphic to render, output as top level shape */
			PrintOuterShape(job, _graphics[0]);
			outerShapeId = _shapeId;
			break;
		default:
			/* multiple graphics to render, output each as a subshape of a group */
				
			/* calculate group bounds */
			boxf outerBounds = _graphics[0]->GetBounds();
			for (Graphics::const_iterator nextGraphic = _graphics.begin() + 1, lastGraphic = _graphics.end();
				 nextGraphic != lastGraphic;
				 ++nextGraphic)
			{
				boxf innerBounds = (*nextGraphic)->GetBounds();
				if (outerBounds.LL.x > innerBounds.LL.x)
					outerBounds.LL.x = innerBounds.LL.x;
				if (outerBounds.LL.y > innerBounds.LL.y)
					outerBounds.LL.y = innerBounds.LL.y;
				if (outerBounds.UR.x < innerBounds.UR.x)
					outerBounds.UR.x = innerBounds.UR.x;
				if (outerBounds.UR.y < innerBounds.UR.y)
					outerBounds.UR.y = innerBounds.UR.y;
			}
			
			gvprintf(job, "<Shape ID='%d' Type='Group'>\n", ++_shapeId);
			outerShapeId = _shapeId;			
			
			gvputs(job, "<XForm>\n");
			gvprintf(job, "<PinX>%f</PinX>\n", (outerBounds.LL.x + outerBounds.UR.x) * 0.5 * INCHES_PER_POINT);
			gvprintf(job, "<PinY>%f</PinY>\n", (outerBounds.LL.y + outerBounds.UR.y) * 0.5 * INCHES_PER_POINT);
			gvprintf(job, "<Width>%f</Width>\n", (outerBounds.UR.x - outerBounds.LL.x) * INCHES_PER_POINT);
			gvprintf(job, "<Height>%f</Height>\n", (outerBounds.UR.y - outerBounds.LL.y) * INCHES_PER_POINT);
			gvputs(job, "</XForm>\n");

			/* output Hyperlink */
			PrintHyperlinks(job);
				
			/* output Para, Char */
			PrintTexts(job);
		
			/* output subshapes */
			gvputs(job, "<Shapes>\n");
			for (Graphics::const_iterator nextGraphic = _graphics.begin(), lastGraphic = _graphics.end(); nextGraphic != lastGraphic; ++nextGraphic)
				PrintInnerShape(job, *nextGraphic, outerShapeId, outerBounds);
			gvputs(job, "</Shapes>\n");
				
			gvputs(job, "</Shape>\n");
			break;
		}
		
		/* save node id for edge logic */
		if (outerShapeId)
			_nodeIds[job->obj->u.n] = outerShapeId;
		ClearGraphicsAndTexts();
	}

	void Render::BeginEdge(GVJ_t* job)
	{
		_inComponent = true;
		ClearGraphicsAndTexts();
	}

	void Render::EndEdge(GVJ_t* job)
	{
		_inComponent = false;
		
		if (_graphics.size() > 0)
		{
			Agedge_t* edge = job->obj->u.e;
			
			/* get previously saved ids for tail and head node; edge type for graph */
			NodeIds::const_iterator beginId = _nodeIds.find(edge->tail);
			NodeIds::const_iterator endId = _nodeIds.find(edge->head);
			
			/* output first connectable shape as an edge shape, all else as regular outer shapes */
			bool firstConnector = true;
			for (Graphics::const_iterator nextGraphic = _graphics.begin(), lastGraphic = _graphics.end(); nextGraphic != lastGraphic; ++nextGraphic)
				if (firstConnector && PrintEdgeShape(job,
					_graphics[0],
					beginId == _nodeIds.end() ? 0 : beginId->second,
					endId == _nodeIds.end() ? 0 : endId->second,
					EDGE_TYPE(edge->head->graph->root)))
					firstConnector = false;
				else
					PrintOuterShape(job, *nextGraphic);

		}
		ClearGraphicsAndTexts();
	}

	void Render::AddEllipse(GVJ_t* job, pointf* A, bool filled)
	{
		AddGraphic(job, Graphic::CreateEllipse(job, A, filled));
	}

	void Render::AddBezier(GVJ_t* job, pointf* A, int n, bool arrow_at_start, bool arrow_at_end, bool filled)
	{
		AddGraphic(job, Graphic::CreateBezier(job, A, n, arrow_at_start, arrow_at_end, filled));
	}

	void Render::AddPolygon(GVJ_t* job, pointf* A, int n, bool filled)
	{
		AddGraphic(job, Graphic::CreatePolygon(job, A, n, filled));
	}

	void Render::AddPolyline(GVJ_t* job, pointf* A, int n)
	{
		AddGraphic(job, Graphic::CreatePolyline(job, A, n));
	}
	
	void Render::AddText(GVJ_t* job, pointf p, textpara_t *para)
	{
		AddText(job, Text::CreateText(job, p, para));
	}

	void Render::AddAnchor(GVJ_t *job, char *url, char *tooltip, char *target, char *id)
	{
		AddHyperlink(job, Hyperlink::CreateHyperlink(job, url, tooltip, target, id));
	}
	
	void Render::ClearGraphicsAndTexts()
	{
		/* clear graphics */
		for (Graphics::iterator nextGraphic = _graphics.begin(), lastGraphic = _graphics.end(); nextGraphic != lastGraphic; ++nextGraphic)
			delete *nextGraphic;
		_graphics.clear();
		
		/* clear texts */
		for (Texts::iterator nextText = _texts.begin(), lastText = _texts.end(); nextText != lastText; ++nextText)
			delete *nextText;
		_texts.clear();

		/* clear hyperlinks */
		for (Hyperlinks::iterator nextHyperlink = _hyperlinks.begin(), lastHyperlink = _hyperlinks.end(); nextHyperlink != lastHyperlink; ++nextHyperlink)
			delete *nextHyperlink;
		_hyperlinks.clear();
	}
	
	void Render::AddGraphic(GVJ_t* job, const Graphic* graphic)
	{
		if (_inComponent)
			/* if in component, accumulate for end node/edge */
			_graphics.push_back(graphic);
		else
			/* if outside, output immediately */
			PrintOuterShape(job, graphic);		
	}

	void Render::AddText(GVJ_t* job, const Text* text)
	{
		/* if in component, accumulate for end node/edge */
		if (_inComponent)
			_texts.push_back(text);
	}

	void Render::AddHyperlink(GVJ_t* job, const Hyperlink* hyperlink)
	{
		/* if in component, accumulate for end node/edge */
		if (_inComponent)
			_hyperlinks.push_back(hyperlink);
	}
	
	void Render::PrintOuterShape(GVJ_t* job, const Graphic* graphic)
	{
		boxf bounds = graphic->GetBounds();
		
		gvprintf(job, "<Shape ID='%d' Type='Shape'>\n", ++_shapeId);
		
		gvputs(job, "<XForm>\n");
		gvprintf(job, "<PinX>%f</PinX>\n", (bounds.LL.x + bounds.UR.x) * 0.5 * INCHES_PER_POINT);
		gvprintf(job, "<PinY>%f</PinY>\n", (bounds.LL.y + bounds.UR.y) * 0.5 * INCHES_PER_POINT);
		gvprintf(job, "<Width>%f</Width>\n", (bounds.UR.x - bounds.LL.x) * INCHES_PER_POINT);
		gvprintf(job, "<Height>%f</Height>\n", (bounds.UR.y - bounds.LL.y) * INCHES_PER_POINT);
		gvputs(job, "</XForm>\n");
		
		gvputs(job, "<Misc>\n");
		gvputs(job, "<ObjType>1</ObjType>\n");
		gvputs(job, "</Misc>\n");
		
		/* output Hyperlink */
		PrintHyperlinks(job);

		/* output Para, Char, Text */
		PrintTexts(job);
		
		/* output Line, Fill, Geom */
		graphic->Print(job, bounds.LL, bounds.UR, true);
		
		gvputs(job, "</Shape>\n");
	}
	
	void Render::PrintInnerShape(GVJ_t* job, const Graphic* graphic, unsigned int outerId, boxf outerBounds)
	{
		boxf innerBounds = graphic->GetBounds();
		
		/* compute scale. if infinite, scale by 0 instead */
		double xscale = 1.0 / (outerBounds.UR.x - outerBounds.LL.x);
		double yscale = 1.0 / (outerBounds.UR.y - outerBounds.LL.y);
		if (isfinite(xscale) == 0)
			xscale = 0.0;
		if (isfinite(yscale) == 0)
			yscale = 0.0;
		
		gvprintf(job, "<Shape ID='%d' Type='Shape'>\n", ++_shapeId);
		
		/* inner XForm is based on width or height of outer Shape */
		gvputs(job, "<XForm>\n");
		gvprintf(job, "<PinX F='Sheet.%d!Width*%f' />\n", outerId, (((innerBounds.LL.x + innerBounds.UR.x) * 0.5) - outerBounds.LL.x) * xscale);
		gvprintf(job, "<PinY F='Sheet.%d!Height*%f' />\n", outerId, (((innerBounds.LL.y + innerBounds.UR.y) * 0.5) - outerBounds.LL.y) * yscale);
		gvprintf(job, "<Width F='Sheet.%d!Width*%f' />\n", outerId, (innerBounds.UR.x - innerBounds.LL.x) * xscale);
		gvprintf(job, "<Height F='Sheet.%d!Height*%f' />\n", outerId, (innerBounds.UR.y - innerBounds.LL.y) * yscale);
		gvputs(job, "</XForm>\n");
		
		gvputs(job, "<Misc>\n");
		gvputs(job, "<ObjType>1</ObjType>\n");
		gvputs(job, "</Misc>\n");
		
		/* output Line, Fill, Geom */
		graphic->Print(job, innerBounds.LL, innerBounds.UR, true);

		gvputs(job, "</Shape>\n");
	}
	
	bool Render::PrintEdgeShape(GVJ_t* job, const Graphic* graphic, unsigned int beginId, unsigned int endId, int edgeType)
	{
		if (const Connection* connection = graphic->GetConnection())
		{
			pointf first = connection->GetFirst();
			pointf last = connection->GetLast();
			
			bool zeroWidth = first.x == last.x;
			bool zeroHeight = first.y == last.y;

			gvprintf(job, "<Shape ID='%d' Type='Shape'>\n", ++_shapeId);
			
			/* XForm depends on XForm1D */
			gvputs(job, "<XForm>\n");
			gvputs(job, "<PinX F='GUARD((BeginX+EndX)/2)'/>\n");
			gvputs(job, "<PinY F='GUARD((BeginY+EndY)/2)'/>\n");
			if (zeroWidth)
				gvprintf(job, "<Width F='GUARD(%f)'/>\n", 2 * ZERO_ADJUST);	/* if vertical line, expand width to 0.25 inches */
			else
				gvputs(job, "<Width F='GUARD(EndX-BeginX)'/>\n");
			if (zeroHeight)
				gvprintf(job, "<Height F='GUARD(%f)'/>\n", 2 * ZERO_ADJUST);	/* if horizontal line, expand height to 0.25 inches */
			else
				gvputs(job, "<Height F='GUARD(EndY-BeginY)'/>\n");
			gvputs(job, "<Angle F='GUARD(0DA)'/>\n");
			gvputs(job, "</XForm>\n");
				
			/* XForm1D walking glue makes connector attach to its nodes */
			gvputs(job, "<XForm1D>\n");
			gvprintf(job, "<BeginX F='_WALKGLUE(BegTrigger,EndTrigger,WalkPreference)'>%f</BeginX>\n", first.x * INCHES_PER_POINT);
			gvprintf(job, "<BeginY F='_WALKGLUE(BegTrigger,EndTrigger,WalkPreference)'>%f</BeginY>\n", first.y * INCHES_PER_POINT);
			gvprintf(job, "<EndX F='_WALKGLUE(EndTrigger,BegTrigger,WalkPreference)'>%f</EndX>\n", last.x * INCHES_PER_POINT);
			gvprintf(job, "<EndY F='_WALKGLUE(EndTrigger,BegTrigger,WalkPreference)'>%f</EndY>\n", last.y * INCHES_PER_POINT);
			gvputs(job, "</XForm1D>\n");
			
			gvputs(job, "<Protection>\n");
			gvputs(job, "<LockHeight>1</LockHeight>\n");
			gvputs(job, "<LockCalcWH>1</LockCalcWH>\n");
			gvputs(job, "</Protection>\n");
			
			gvputs(job, "<Misc>\n");
			gvputs(job, "<NoAlignBox>1</NoAlignBox>\n");
			gvputs(job, "<DynFeedback>2</DynFeedback>\n");
			gvputs(job, "<GlueType>2</GlueType>\n");
			if (beginId && endId)
			{
				gvprintf(job, "<BegTrigger F='_XFTRIGGER(Sheet.%d!EventXFMod)'/>\n", beginId);
				gvprintf(job, "<EndTrigger F='_XFTRIGGER(Sheet.%d!EventXFMod)'/>\n", endId);
			}
			gvputs(job, "<ObjType>2</ObjType>\n");
			gvputs(job, "</Misc>\n");
			
			gvputs(job, "<Layout>\n");
			gvprintf(job, "<ShapeRouteStyle>%d</ShapeRouteStyle>\n", edgeType == ET_LINE ? LORouteCenterToCenter : LORouteRightAngle);
			gvputs(job, "<ConFixedCode>6</ConFixedCode>\n");
			gvprintf(job, "<ConLineRouteExt>%d</ConLineRouteExt>\n", edgeType == ET_LINE || edgeType == ET_PLINE ? LORouteExtStraight : LORouteExtNURBS);
			gvputs(job, "<ShapeSplittable>1</ShapeSplittable>\n");
			gvputs(job, "</Layout>\n");
			
			/* output Hyperlink */
			PrintHyperlinks(job);

			/* TextXForm depends on custom control */
			gvputs(job, "<TextXForm>\n");
			gvputs(job, "<TxtPinX F='SETATREF(Controls.TextPosition)'/>\n");
			gvputs(job, "<TxtPinY F='SETATREF(Controls.TextPosition.Y)'/>\n");
			gvputs(job, "<TxtWidth F='MAX(TEXTWIDTH(TheText),5*Char.Size)'/>\n");
			gvputs(job, "<TxtHeight F='TEXTHEIGHT(TheText,TxtWidth)'/>\n");
			gvputs(job, "</TextXForm>\n");

			if (zeroWidth)
			{
				first.x -= ZERO_ADJUST;
				last.x += ZERO_ADJUST;
			}
			if (zeroHeight)
			{
				first.y -= ZERO_ADJUST;
				last.y += ZERO_ADJUST;
			}
			
			/* compute center to attach text to. if text has been rendered, use overall bounding box center; if not, use the path center */
			pointf textCenter;
			if (_texts.size() > 0)
			{
				boxf outerTextBounds = _texts[0]->GetBounds();
				
				for (Texts::const_iterator nextText = _texts.begin() + 1, lastText = _texts.end();
					 nextText != lastText;
					 ++nextText)
				{
					boxf innerTextBounds = (*nextText)->GetBounds();
					if (outerTextBounds.LL.x > innerTextBounds.LL.x)
						outerTextBounds.LL.x = innerTextBounds.LL.x;
					if (outerTextBounds.LL.y > innerTextBounds.LL.y)
						outerTextBounds.LL.y = innerTextBounds.LL.y;
					if (outerTextBounds.UR.x < innerTextBounds.UR.x)
						outerTextBounds.UR.x = innerTextBounds.UR.x;
					if (outerTextBounds.UR.y < innerTextBounds.UR.y)
						outerTextBounds.UR.y = innerTextBounds.UR.y;
				}
				textCenter.x = (outerTextBounds.LL.x + outerTextBounds.UR.x) / 2.0;
				textCenter.y = (outerTextBounds.LL.y + outerTextBounds.UR.y) / 2.0;
			}
			else
				textCenter = connection->GetCenter();
			
			/* Control for positioning text */
			gvputs(job, "<Control NameU='TextPosition'>\n");
			gvprintf(job, "<X>%f</X>\n", (textCenter.x - first.x) * INCHES_PER_POINT);
			gvprintf(job, "<Y>%f</Y>\n", (textCenter.y - first.y) * INCHES_PER_POINT);
			gvputs(job, "<XDyn F='Controls.TextPosition'/>\n");
			gvputs(job, "<YDyn F='Controls.TextPosition.Y'/>\n");
			gvputs(job, "<XCon F='IF(OR(STRSAME(SHAPETEXT(TheText),&quot;&quot;),HideText),5,0)'>5</XCon>\n");
			gvputs(job, "<YCon>0</YCon>\n");
			gvputs(job, "</Control>\n");
				
			/* output Para, Char, Text */
			PrintTexts(job);
			
			/* output Line, Fill, Geom */
			graphic->Print(job, first, last, edgeType != ET_LINE && edgeType != ET_PLINE);
			
			gvputs(job, "</Shape>\n");
			return true;
		}
		else
			return false;
	}
	
	void Render::PrintTexts(GVJ_t* job)
	{
		if (_texts.size() > 0)
		{
			/* output Para, Char */
			for (Texts::iterator nextText = _texts.begin(), lastText = _texts.end(); nextText != lastText; ++nextText)
				(*nextText)->Print(job);
			
			/* output Text. each run references above Para + Char */
			gvputs(job, "<Text>");
			for (unsigned int index = 0, count = _texts.size(); index < count; ++index)
				(_texts[index])->PrintRun(job, index);
			gvputs(job, "</Text>");
		}
	}
	
	void Render::PrintHyperlinks(GVJ_t* job)
	{
		if (_hyperlinks.size() > 0)
		{
			_hyperlinks[0]->Print(job, ++_hyperlinkId, true);
			for (Hyperlinks::iterator nextHyperlink = _hyperlinks.begin() + 1, lastHyperlink = _hyperlinks.end(); nextHyperlink != lastHyperlink; ++nextHyperlink)
				(*nextHyperlink)->Print(job, ++_hyperlinkId, false);
		}
	}

}