Repository navigation
Expand file tree
/
Copy pathLineObject.cs
More file actions
212 lines (189 loc) · 7.27 KB
/
Copy pathLineObject.cs
File metadata and controls
212 lines (189 loc) · 7.27 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using FastReport.Utils;
namespace FastReport
{
/// <summary>
/// Represents a line object.
/// </summary>
/// <remarks>
/// Use the <b>Border.Width</b>, <b>Border.Style</b> and <b>Border.Color</b> properties to set
/// the line width, style and color. Set the <see cref="Diagonal"/> property to <b>true</b>
/// if you want to show a diagonal line.
/// </remarks>
public partial class LineObject : ReportComponentBase
{
#region Fields
private bool diagonal;
private CapSettings startCap;
private CapSettings endCap;
#endregion
#region Properties
/// <summary>
/// Gets or sets a value indicating that the line is diagonal.
/// </summary>
/// <remarks>
/// If this property is <b>false</b>, the line can be only horizontal or vertical.
/// </remarks>
[DefaultValue(false)]
[Category("Appearance")]
public bool Diagonal
{
get { return diagonal; }
set { diagonal = value; }
}
/// <summary>
/// Gets or sets the start cap settings.
/// </summary>
[Category("Appearance")]
public CapSettings StartCap
{
get { return startCap; }
set { startCap = value; }
}
/// <summary>
/// Gets or sets the end cap settings.
/// </summary>
[Category("Appearance")]
public CapSettings EndCap
{
get { return endCap; }
set { endCap = value; }
}
#endregion
#region Public Methods
/// <inheritdoc/>
public override void Assign(Base source)
{
base.Assign(source);
LineObject src = source as LineObject;
Diagonal = src.Diagonal;
StartCap.Assign(src.StartCap);
EndCap.Assign(src.EndCap);
}
/// <inheritdoc/>
public override void Draw(FRPaintEventArgs e)
{
IGraphics g = e.Graphics;
// draw marker when inserting a line
if (Width == 0 && Height == 0)
{
g.DrawLine(Pens.Black, AbsLeft * e.ScaleX - 6, AbsTop * e.ScaleY, AbsLeft * e.ScaleX + 6, AbsTop * e.ScaleY);
g.DrawLine(Pens.Black, AbsLeft * e.ScaleX, AbsTop * e.ScaleY - 6, AbsLeft * e.ScaleX, AbsTop * e.ScaleY + 6);
return;
}
Report report = Report;
if (report != null && report.SmoothGraphics)
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.AntiAlias;
}
Pen pen = e.Cache.GetPen(Border.Color, Border.Width * e.ScaleX, Border.DashStyle);
float width = Width;
float height = Height;
if (!Diagonal)
{
if (Math.Abs(width) > Math.Abs(height))
height = 0;
else
width = 0;
}
float x1 = AbsLeft * e.ScaleX;
float y1 = AbsTop * e.ScaleY;
float x2 = (AbsLeft + width) * e.ScaleX;
float y2 = (AbsTop + height) * e.ScaleY;
if (StartCap.Style == CapStyle.None && EndCap.Style == CapStyle.None)
{
g.DrawLine(pen, x1, y1, x2, y2);
}
else
{
// draw line caps manually. It is necessary for correct svg rendering
float angle = (float)(Math.Atan2(x2 - x1, y2 - y1) / Math.PI * 180);
float len = (float)Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
float scale = Border.Width * e.ScaleX;
IGraphicsState state = g.Save();
g.TranslateTransform(x1, y1);
g.RotateTransform(-angle);
float y = 0;
GraphicsPath startCapPath = null;
GraphicsPath endCapPath = null;
float inset = 0;
if (StartCap.Style != CapStyle.None)
{
StartCap.GetCustomCapPath(out startCapPath, out inset);
y += inset * scale;
}
if (EndCap.Style != CapStyle.None)
{
EndCap.GetCustomCapPath(out endCapPath, out inset);
len -= inset * scale;
}
g.DrawLine(pen, 0, y, 0, len);
g.Restore(state);
pen = e.Cache.GetPen(Border.Color, 1, Border.DashStyle);
if (StartCap.Style != CapStyle.None)
{
state = g.Save();
g.TranslateTransform(x1, y1);
g.RotateTransform(180 - angle);
g.ScaleTransform(scale, scale);
g.DrawPath(pen, startCapPath);
g.Restore(state);
}
if (EndCap.Style != CapStyle.None)
{
state = g.Save();
g.TranslateTransform(x2, y2);
g.RotateTransform(-angle);
g.ScaleTransform(scale, scale);
g.DrawPath(pen, endCapPath);
g.Restore(state);
}
}
if (report != null && report.SmoothGraphics && Diagonal)
{
g.InterpolationMode = InterpolationMode.Default;
g.SmoothingMode = SmoothingMode.Default;
}
}
/// <inheritdoc/>
public override List<ValidationError> Validate()
{
List<ValidationError> listError = new List<ValidationError>();
if (Height == 0 && Width == 0)
listError.Add(new ValidationError(Name, ValidationError.ErrorLevel.Error, Res.Get("Messages,Validator,IncorrectSize"), this));
if (Name == "")
listError.Add(new ValidationError(Name, ValidationError.ErrorLevel.Error, Res.Get("Messages,Validator,UnnamedObject"), this));
if (Parent is ReportComponentBase && !Validator.RectContainInOtherRect((Parent as ReportComponentBase).AbsBounds, this.AbsBounds))
listError.Add(new ValidationError(Name, ValidationError.ErrorLevel.Error, Res.Get("Messages,Validator,OutOfBounds"), this));
return listError;
}
/// <inheritdoc/>
public override void Serialize(FRWriter writer)
{
Border.SimpleBorder = true;
base.Serialize(writer);
LineObject c = writer.DiffObject as LineObject;
if (Diagonal != c.Diagonal)
writer.WriteBool("Diagonal", Diagonal);
StartCap.Serialize("StartCap", writer, c.StartCap);
EndCap.Serialize("EndCap", writer, c.EndCap);
}
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="LineObject"/> class with default settings.
/// </summary>
public LineObject()
{
startCap = new CapSettings();
endCap = new CapSettings();
FlagSimpleBorder = true;
FlagUseFill = false;
}
}
}