DrawLabel
(
IEdge oEdge,
DrawingContext oDrawingContext,
GraphDrawingContext oGraphDrawingContext,
Boolean bDrawAsSelected,
Color oEdgeColor,
VisibilityKeyValue eVisibility,
Point oEdgeEndpoint1,
Point oEdgeEndpoint2,
PathGeometry oBezierCurve,
Point oBezierControlPoint,
String sLabel
)
{
Debug.Assert(oEdge != null);
Debug.Assert(oDrawingContext != null);
Debug.Assert(oGraphDrawingContext != null);
Debug.Assert(sLabel != null);
AssertValid();
if (sLabel.Length == 0)
{
return;
}
sLabel = TruncateLabel(sLabel);
Double dFontSize = GetLabelFontSize(oEdge);
Color oLabelTextColor = GetLabelTextColor(oEdge, bDrawAsSelected,
oEdgeColor, eVisibility);
FormattedText oFormattedText =
m_oFormattedTextManager.CreateFormattedText(sLabel,
oLabelTextColor, dFontSize, m_dGraphScale);
oFormattedText.Trimming = TextTrimming.CharacterEllipsis;
// Don't let the FormattedText class break long lines when drawing
// Bezier curves. DrawBezierLabel(), which draws the label
// character-by-character, is unable to tell where such line breaks
// occur, because FormattedText doesn't expose this information.
//
// For consistency, do the same when drawing straight edges.
oFormattedText.MaxLineCount = sLabel.Count(c => c == '\n') + 1;
// The ends of the label text are between one and two "buffer units"
// from the ends of the edge. The buffer unit is the width of an
// arbitrary character.
Double dBufferWidth =
m_oFormattedTextManager.CreateFormattedText("i", oLabelTextColor,
dFontSize, m_dGraphScale).Width;
Double dEdgeLength = WpfGraphicsUtil.GetDistanceBetweenPoints(
oEdgeEndpoint1, oEdgeEndpoint2);
Double dEdgeLengthMinusBuffers = dEdgeLength - 2 * dBufferWidth;
if (dEdgeLengthMinusBuffers <= 0)
{
return;
}
// Determine where to draw the label text.
Double dTextWidth = oFormattedText.Width;
Double dLabelOriginAsFractionOfEdgeLength;
if (dTextWidth > dEdgeLengthMinusBuffers)
{
// The label text should start one buffer unit from the first
// endpoint, and terminate with ellipses approximately one buffer
// length from the second endpoint.
dLabelOriginAsFractionOfEdgeLength = dBufferWidth / dEdgeLength;
oFormattedText.MaxTextWidth = dEdgeLengthMinusBuffers;
}
else
{
// The label should be centered along the edge's length.
dLabelOriginAsFractionOfEdgeLength =
( (dEdgeLength - dTextWidth) / 2.0 ) / dEdgeLength;
}
// Note: Don't make the translucent rectangle any more opaque than the
// edge, which might be translucent itself.
Color oTranslucentRectangleColor = WpfGraphicsUtil.SetWpfColorAlpha(
oGraphDrawingContext.BackColor,
Math.Min(LabelBackgroundAlpha, oEdgeColor.A)
);
if (this.ShouldDrawBezierCurve)
{
DrawBezierLabel(oDrawingContext, oGraphDrawingContext,
oFormattedText, oEdgeEndpoint1, oEdgeEndpoint2, oBezierCurve,
dLabelOriginAsFractionOfEdgeLength, dEdgeLength,
dBufferWidth, oLabelTextColor, oTranslucentRectangleColor,
dFontSize);
}
else
{
DrawStraightLabel(oDrawingContext, oGraphDrawingContext,
oFormattedText, oEdgeEndpoint1, oEdgeEndpoint2,
dLabelOriginAsFractionOfEdgeLength, dEdgeLength, dBufferWidth,
oTranslucentRectangleColor);
}
}