-
Notifications
You must be signed in to change notification settings - Fork 976
Expand file tree
/
Copy pathTextAnnotation.cs
More file actions
189 lines (168 loc) · 6.96 KB
/
Copy pathTextAnnotation.cs
File metadata and controls
189 lines (168 loc) · 6.96 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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TextAnnotation.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Represents an annotation that shows text.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#nullable enable
namespace OxyPlot.Annotations
{
using System;
using System.Collections.Generic;
/// <summary>
/// Represents an annotation that shows text.
/// </summary>
public class TextAnnotation : TextualAnnotation
{
/// <summary>
/// The actual bounds of the text.
/// </summary>
private IList<ScreenPoint>? actualBounds;
/// <summary>
/// Initializes a new instance of the <see cref="TextAnnotation" /> class.
/// </summary>
public TextAnnotation()
{
this.Stroke = OxyColors.Black;
this.Background = OxyColors.Undefined;
this.StrokeThickness = 1;
this.TextVerticalAlignment = VerticalAlignment.Bottom;
this.Padding = new OxyThickness(4);
}
/// <summary>
/// Gets or sets the fill color of the background rectangle.
/// </summary>
/// <value>The background.</value>
public OxyColor Background { get; set; }
/// <summary>
/// Gets or sets the position offset (screen coordinates).
/// </summary>
/// <value>The offset.</value>
public ScreenVector Offset { get; set; }
/// <summary>
/// Gets or sets the padding of the background rectangle.
/// </summary>
/// <value>The padding.</value>
public OxyThickness Padding { get; set; }
/// <summary>
/// Gets or sets the stroke color of the background rectangle.
/// </summary>
/// <value>The stroke color.</value>
public OxyColor Stroke { get; set; }
/// <summary>
/// Gets or sets the stroke thickness of the background rectangle.
/// </summary>
/// <value>The stroke thickness.</value>
public double StrokeThickness { get; set; }
/// <inheritdoc/>
public override void Render(IRenderContext rc)
{
if (this.Text == null)
{
throw new InvalidOperationException($"{nameof(this.Text)} must be non-null before rendering.");
}
base.Render(rc);
var position = this.Transform(this.TextPosition) + this.Orientate(this.Offset);
var textSize = rc.MeasureText(this.Text, this.ActualFont, this.ActualFontSize, this.ActualFontWeight);
this.GetActualTextAlignment(out var ha, out var va);
this.actualBounds = GetTextBounds(position, textSize, this.Padding, this.TextRotation, ha, va);
if ((this.TextRotation % 90).Equals(0))
{
var actualRect = new OxyRect(this.actualBounds[0], this.actualBounds[2]);
rc.DrawRectangle(actualRect, this.Background, this.Stroke, this.StrokeThickness, this.EdgeRenderingMode);
}
else
{
rc.DrawPolygon(this.actualBounds, this.Background, this.Stroke, this.StrokeThickness, this.EdgeRenderingMode);
}
rc.DrawMathText(
position,
this.Text,
this.GetSelectableFillColor(this.ActualTextColor),
this.ActualFont,
this.ActualFontSize,
this.ActualFontWeight,
this.TextRotation,
ha,
va);
}
/// <summary>
/// When overridden in a derived class, tests if the plot element is hit by the specified point.
/// </summary>
/// <param name="args">The hit test arguments.</param>
/// <returns>
/// The result of the hit test.
/// </returns>
protected override HitTestResult? HitTestOverride(HitTestArguments args)
{
if (this.actualBounds == null)
{
return null;
}
// Todo: see if performance can be improved by checking rectangle (with rotation and alignment), not polygon
return ScreenPointHelper.IsPointInPolygon(args.Point, this.actualBounds) ? new HitTestResult(this, args.Point) : null;
}
/// <summary>
/// Gets the coordinates of the (rotated) background rectangle.
/// </summary>
/// <param name="position">The position.</param>
/// <param name="size">The size.</param>
/// <param name="padding">The padding.</param>
/// <param name="rotation">The rotation.</param>
/// <param name="horizontalAlignment">The horizontal alignment.</param>
/// <param name="verticalAlignment">The vertical alignment.</param>
/// <returns>The background rectangle coordinates.</returns>
internal static IList<ScreenPoint> GetTextBounds(
ScreenPoint position,
OxySize size,
OxyThickness padding,
double rotation,
HorizontalAlignment horizontalAlignment,
VerticalAlignment verticalAlignment)
{
double left, right, top, bottom;
switch (horizontalAlignment)
{
case HorizontalAlignment.Center:
left = -size.Width * 0.5;
right = -left;
break;
case HorizontalAlignment.Right:
left = -size.Width;
right = 0;
break;
default:
left = 0;
right = size.Width;
break;
}
switch (verticalAlignment)
{
case VerticalAlignment.Middle:
top = -size.Height * 0.5;
bottom = -top;
break;
case VerticalAlignment.Bottom:
top = -size.Height;
bottom = 0;
break;
default:
top = 0;
bottom = size.Height;
break;
}
double cost = Math.Cos(rotation / 180 * Math.PI);
double sint = Math.Sin(rotation / 180 * Math.PI);
var u = new ScreenVector(cost, sint);
var v = new ScreenVector(-sint, cost);
var polygon = new ScreenPoint[4];
polygon[0] = position + (u * (left - padding.Left)) + (v * (top - padding.Top));
polygon[1] = position + (u * (right + padding.Right)) + (v * (top - padding.Top));
polygon[2] = position + (u * (right + padding.Right)) + (v * (bottom + padding.Bottom));
polygon[3] = position + (u * (left - padding.Left)) + (v * (bottom + padding.Bottom));
return polygon;
}
}
}