DrawStraightLabel
(
DrawingContext oDrawingContext,
GraphDrawingContext oGraphDrawingContext,
FormattedText oFormattedText,
Point oEdgeEndpoint1,
Point oEdgeEndpoint2,
Double dLabelOriginAsFractionOfEdgeLength,
Double dEdgeLength,
Double dBufferWidth,
Color oTranslucentRectangleColor
)
{
Debug.Assert(oDrawingContext != null);
Debug.Assert(oGraphDrawingContext != null);
Debug.Assert(oFormattedText != null);
Debug.Assert(dLabelOriginAsFractionOfEdgeLength >= 0);
Debug.Assert(dEdgeLength >= 0);
Debug.Assert(dBufferWidth >= 0);
AssertValid();
if (oEdgeEndpoint2.X < oEdgeEndpoint1.X)
{
// Don't let text be drawn upside-down.
WpfGraphicsUtil.SwapPoints(ref oEdgeEndpoint1, ref oEdgeEndpoint2);
}
// To avoid trigonometric calculations, use a RotateTransform to make
// the edge look as if it is horizontal, with oEdgeEndpoint2 to the
// right of oEdgeEndpoint1.
Double dEdgeAngleDegrees = MathUtil.RadiansToDegrees(
WpfGraphicsUtil.GetAngleBetweenPointsRadians(
oEdgeEndpoint1, oEdgeEndpoint2) );
RotateTransform oRotateTransform = new RotateTransform(
dEdgeAngleDegrees, oEdgeEndpoint1.X, oEdgeEndpoint1.Y);
oRotateTransform.Angle = -dEdgeAngleDegrees;
oDrawingContext.PushTransform(oRotateTransform);
Double dTextWidth = oFormattedText.Width;
Double dEdgeLengthMinusBuffers = dEdgeLength - 2 * dBufferWidth;
Point oLabelOrigin = oEdgeEndpoint1;
// The text should be vertically centered on the edge.
oLabelOrigin.Offset(dLabelOriginAsFractionOfEdgeLength * dEdgeLength,
-oFormattedText.Height / 2.0);
// Determine where to draw a translucent rectangle behind the text.
// The translucent rectangle serves to obscure, but not completely
// hide, the underlying edge.
Rect oTranslucentRectangle;
if (dTextWidth > dEdgeLengthMinusBuffers)
{
// The translucent rectangle should be the same width as the text.
oTranslucentRectangle = new Rect( oLabelOrigin,
new Size(dEdgeLengthMinusBuffers, oFormattedText.Height) );
}
else
{
// The label is centered along the edge's length.
// The translucent rectangle should extend between zero and one
// buffer units beyond the ends of the text. This provides a
// margin between the text and the unobscured edge, if there is
// enough space.
oTranslucentRectangle = new Rect( oLabelOrigin,
new Size(
Math.Min(dTextWidth + 2 * dBufferWidth,
dEdgeLengthMinusBuffers),
oFormattedText.Height)
);
oTranslucentRectangle.Offset(-dBufferWidth, 0);
}
DrawTranslucentRectangle(oDrawingContext, oTranslucentRectangle,
oTranslucentRectangleColor);
oDrawingContext.DrawText(oFormattedText, oLabelOrigin);
oDrawingContext.Pop();
}