forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxGraphViewImageReader.cs
More file actions
190 lines (164 loc) · 5.64 KB
/
mxGraphViewImageReader.cs
File metadata and controls
190 lines (164 loc) · 5.64 KB
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
// $Id: mxGraphViewImageReader.cs,v 1.1 2012/11/15 13:26:49 gaudenz Exp $
// Copyright (c) 2007-2008, Gaudenz Alder
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace com.mxgraph
{
/// <summary>
/// A converter that renders display XML data onto a GDI canvas.
/// </summary>
public class mxGraphViewImageReader : mxGraphViewReader
{
/// <summary>
/// Specifies the background color.
/// </summary>
protected Color? background;
/// <summary>
/// Specifies the border size. Default is 0.
/// </summary>
protected int border;
/// <summary>
/// Default is true.
/// </summary>
protected bool antiAlias;
/// <summary>
/// Default is true.
/// </summary>
protected bool cropping;
/// <summary>
/// Specifies the optional clipping rectangle.
/// </summary>
protected mxRectangle clip;
/// <summary>
/// Constructs a new GDI reader for the given display XML reader.
/// </summary>
/// <param name="reader"></param>
public mxGraphViewImageReader(XmlReader reader) : this(reader, null) { }
/// <summary>
/// Constructs a new GDI reader for the given display XML reader.
/// </summary>
public mxGraphViewImageReader(XmlReader reader, Color? background) : this(reader, background, 0) { }
/// <summary>
/// Constructs a new GDI reader for the given display XML reader.
/// </summary>
public mxGraphViewImageReader(XmlReader reader, Color? background, int border)
: this(reader, background, border, true) { }
/// <summary>
/// Constructs a new GDI reader for the given display XML reader.
/// </summary>
public mxGraphViewImageReader(XmlReader reader, Color? background, int border, bool antiAlias)
: this(reader, background, border, antiAlias, true) { }
/// <summary>
/// Constructs a new GDI reader for the given display XML reader.
/// </summary>
public mxGraphViewImageReader(XmlReader reader, Color? background, int border, bool antiAlias, bool cropping)
{
Background = background;
Border = border;
AntiAlias = antiAlias;
Cropping = cropping;
Read(reader);
}
/// <summary>
/// Accessors for the background property.
/// </summary>
public Color? Background
{
get { return background; }
set { background = value; }
}
/// <summary>
/// Accessors for the border property.
/// </summary>
public int Border
{
get { return border; }
set { border = value; }
}
/// <summary>
/// Accessors for the background property.
/// </summary>
public bool AntiAlias
{
get { return antiAlias; }
set { antiAlias = value; }
}
/// <summary>
/// Accessors for the cropping property.
/// </summary>
public bool Cropping
{
get { return cropping; }
set { cropping = value; }
}
/// <summary>
/// Accessors for the clip property.
/// </summary>
public mxRectangle Clip
{
get { return clip; }
set { clip = value; }
}
/* (non-Dotnetdoc)
* see com.mxgraph.mxGraphViewReader.CreateCanvas()
*/
override public mxICanvas CreateCanvas(Dictionary<string, Object> attrs)
{
int width = 0;
int height = 0;
int dx = 0;
int dy = 0;
mxRectangle tmp = Clip;
if (tmp != null)
{
dx -= (int)tmp.X;
dy -= (int)tmp.Y;
width = (int)tmp.Width;
height = (int)tmp.Height;
}
else
{
int x = (int)Math.Round(mxUtils.GetDouble(attrs, "x"));
int y = (int)Math.Round(mxUtils.GetDouble(attrs, "y"));
width = (int)(Math.Round(mxUtils.GetDouble(attrs, "width")))
+ border + 3;
height = (int)(Math.Round(mxUtils.GetDouble(attrs, "height")))
+ border + 3;
if (cropping)
{
dx = -x + 3;
dy = -y + 3;
}
else
{
width += x;
height += y;
}
}
mxImageCanvas canvas = new mxImageCanvas(new mxGdiCanvas(),
width, height, Background, AntiAlias);
canvas.Translate = new Point(dx, dy);
return canvas;
}
/// <summary>
/// Creates the image for the given display XML reader. For a given XmlReader,
/// use the following code to create the view reader:
/// new mxGraphViewImageReader(xmlReader, background, border, antiAlias);
/// </summary>
/// <param name="viewReader">Reader that contains the display XML.</param>
/// <returns>Returns an image representing the display XML reader.</returns>
public static Image Convert(mxGraphViewImageReader viewReader)
{
if (viewReader.Canvas is mxImageCanvas)
{
return ((mxImageCanvas) viewReader.Canvas).Destroy();
}
return null;
}
}
}