AdjustForTextAlignment
(
Double dLabelWidth,
Double dLabelHeight,
VertexLabelPosition eLabelPosition,
FormattedText oFormattedText,
ref Double dDrawX,
ref Double dDrawY,
ref Double dLabelBoundsLeft,
ref Double dLabelBoundsRight
)
{
Debug.Assert(dLabelWidth >= 0);
Debug.Assert(dLabelHeight >= 0);
Debug.Assert(oFormattedText != null);
AssertValid();
Double dHalfLabelHeight = dLabelHeight / 2.0;
Double dHalfLabelWidth = dLabelWidth / 2.0;
Double dMaxTextWidth = oFormattedText.MaxTextWidth;
Double dHalfMaxTextWidth = dMaxTextWidth / 2.0;
// This is the adjustment that needs to be made to the x-coordinate
// passed to DrawingContext.DrawText() to compensate for the way
// centered and right-justified text is positioned.
//
// When wrapping is turned off (FormattedText.MaxTextWidth = 0) and
// FormattedText.TextAlignment = TextAlignment.Center, for example,
// the text gets centered horizontally at the specified drawing point.
// When wrapping is turned on, however (FormattedText.MaxTextWidth
// > 0), the text gets centered horizontally at the point halfway
// between the specified drawing point and the MaxTextWidth value.
Double dAdjustmentForTextAlignmentX = 0;
switch (eLabelPosition)
{
case VertexLabelPosition.TopLeft:
dAdjustmentForTextAlignmentX = -dMaxTextWidth;
dDrawY -= (dLabelHeight + VerticalMargin);
dLabelBoundsLeft = dDrawX - dLabelWidth;
dLabelBoundsRight = dDrawX;
break;
case VertexLabelPosition.TopCenter:
dAdjustmentForTextAlignmentX = -dHalfMaxTextWidth;
dDrawY -= (dLabelHeight + VerticalMargin);
dLabelBoundsLeft = dDrawX - dHalfLabelWidth;
dLabelBoundsRight = dDrawX + dHalfLabelWidth;
break;
case VertexLabelPosition.TopRight:
dDrawY -= (dLabelHeight + VerticalMargin);
dLabelBoundsLeft = dDrawX;
dLabelBoundsRight = dDrawX + dLabelWidth;
break;
case VertexLabelPosition.MiddleLeft:
dAdjustmentForTextAlignmentX = -dMaxTextWidth;
dDrawX -= HorizontalMargin;
dDrawY -= dHalfLabelHeight;
dLabelBoundsLeft = dDrawX - dLabelWidth;
dLabelBoundsRight = dDrawX;
break;
case VertexLabelPosition.MiddleCenter:
dAdjustmentForTextAlignmentX = -dHalfMaxTextWidth;
dDrawY -= dHalfLabelHeight;
dLabelBoundsLeft = dDrawX - dHalfLabelWidth;
dLabelBoundsRight = dDrawX + dHalfLabelWidth;
break;
case VertexLabelPosition.MiddleRight:
dDrawX += HorizontalMargin;
dDrawY -= dHalfLabelHeight;
dLabelBoundsLeft = dDrawX;
dLabelBoundsRight = dDrawX + dLabelWidth;
break;
case VertexLabelPosition.BottomLeft:
dAdjustmentForTextAlignmentX = -dMaxTextWidth;
dDrawY += VerticalMargin;
dLabelBoundsLeft = dDrawX - dLabelWidth;
dLabelBoundsRight = dDrawX;
break;
case VertexLabelPosition.BottomCenter:
dAdjustmentForTextAlignmentX = -dHalfMaxTextWidth;
dDrawY += VerticalMargin;
dLabelBoundsLeft = dDrawX - dHalfLabelWidth;
dLabelBoundsRight = dDrawX + dHalfLabelWidth;
break;
case VertexLabelPosition.BottomRight:
dDrawY += VerticalMargin;
dLabelBoundsLeft = dDrawX;
dLabelBoundsRight = dDrawX + dLabelWidth;
break;
case VertexLabelPosition.Nowhere:
// (This was handled at the top of the method.)
default:
Debug.Assert(false);
break;
}
dDrawX += dAdjustmentForTextAlignmentX;
}