Ejemplo n.º 1
0
        DrawLabel
        (
            DrawingContext drawingContext,
            GraphDrawingContext graphDrawingContext,
            VertexDrawingHistory vertexDrawingHistory,
            VertexLabelPosition labelPosition,
            FormattedText formattedText,
            Color formattedTextColor,
            Boolean drawBackground
        )
        {
            Debug.Assert(drawingContext != null);
            Debug.Assert(graphDrawingContext != null);
            Debug.Assert(vertexDrawingHistory != null);
            Debug.Assert(formattedText != null);
            AssertValid();

            if (labelPosition == VertexLabelPosition.Nowhere)
            {
                return;
            }

            // The alignment needs to be set before the width and height of the
            // FormattedText are obtained.

            SetTextAlignment(formattedText, labelPosition);

            // You can't use FormattedText.Width to get the width of the actual
            // text when text wrapping is enabled (FormattedText.MaxTextWidth > 0).
            // Instead, use a method that takes wrapping into account.

            Double dLabelWidth = WpfGraphicsUtil.GetFormattedTextSize(
                formattedText).Width;

            Double dLabelHeight = formattedText.Height;

            // This is the point where the label will be drawn using
            // DrawingContext.Draw().  It initially assumes a text height of zero,
            // a margin of zero, and no adjustment for alignment (see
            // dAdjustmentForTextAlignmentX below), but this will be modified
            // appropriately within the switch statement below.

            Point  oDraw  = vertexDrawingHistory.GetLabelLocation(labelPosition);
            Double dDrawX = oDraw.X;
            Double dDrawY = oDraw.Y;

            // These are the left and right bounds of the rectangle where the text
            // will actually appear.

            Double dLabelBoundsLeft  = 0;
            Double dLabelBoundsRight = 0;

            AdjustForTextAlignment(dLabelWidth, dLabelHeight, labelPosition,
                                   formattedText, ref dDrawX, ref dDrawY, ref dLabelBoundsLeft,
                                   ref dLabelBoundsRight);

            // Don't let the text exceed the bounds of the graph rectangle.

            dDrawX = StayWithinHorizontalBounds(
                dDrawX, graphDrawingContext, dLabelBoundsLeft, dLabelBoundsRight);

            Double dLabelBoundsTop    = dDrawY;
            Double dLabelBoundsBottom = dDrawY + dLabelHeight;

            dDrawY = StayWithinVerticalBounds(
                dDrawY, graphDrawingContext, dLabelBoundsTop, dLabelBoundsBottom);

            if (drawBackground)
            {
                dLabelBoundsLeft = StayWithinHorizontalBounds(
                    dLabelBoundsLeft, graphDrawingContext,
                    dLabelBoundsLeft, dLabelBoundsRight);

                DrawLabelBackground(drawingContext, graphDrawingContext,
                                    formattedText, formattedTextColor, m_btBackgroundAlpha,
                                    new Point(dLabelBoundsLeft, dDrawY));
            }

            drawingContext.DrawText(formattedText, new Point(dDrawX, dDrawY));
        }
Ejemplo n.º 2
0
        DrawGroupLabel
        (
            DrawingContext oDrawingContext,
            GraphDrawingContext oGraphDrawingContext,
            GroupInfo oGroupInfo
        )
        {
            Debug.Assert(oDrawingContext != null);
            Debug.Assert(oGraphDrawingContext != null);
            Debug.Assert(oGroupInfo != null);
            Debug.Assert(m_eLabelPosition != VertexLabelPosition.Nowhere);
            AssertValid();

            String sLabel = oGroupInfo.Label;
            Rect   oGroupRectangle;

            if (
                String.IsNullOrEmpty(sLabel)
                ||
                !TryGetGroupRectangle(oGroupInfo, out oGroupRectangle)
                )
            {
                return;
            }

            FormattedText oFormattedText =
                m_oFormattedTextManager.CreateFormattedText(sLabel,
                                                            m_oLabelTextColor, m_dLabelScale);

            oFormattedText.MaxTextWidth  = oGroupRectangle.Width;
            oFormattedText.MaxTextHeight = oGroupRectangle.Height;

            // The alignment needs to be set before the width and height of the
            // FormattedText are obtained.

            SetTextAlignment(oFormattedText, m_eLabelPosition);

            // You can't use FormattedText.Width to get the width of the actual
            // text when text wrapping is enabled (FormattedText.MaxTextWidth > 0).
            // Instead, use a method that takes wrapping into account.

            Double dLabelWidth =
                WpfGraphicsUtil.GetFormattedTextSize(oFormattedText).Width;

            Double dLabelHeight = oFormattedText.Height;

            Double dGroupRectangleWidth  = oGroupRectangle.Width;
            Double dGroupRectangleHeight = oGroupRectangle.Height;
            Double dMaxTextWidth         = oFormattedText.MaxTextWidth;

            Double dTextOffsetXForCenter =
                (dGroupRectangleWidth - dMaxTextWidth) / 2.0;

            Double dTextOffsetYForMiddle =
                (dGroupRectangleHeight - dLabelHeight) / 2.0;

            Double dTextOffsetYForBottom =
                dGroupRectangleHeight - dLabelHeight - LabelVerticalMargin;

            Point  oTextOrigin  = oGroupRectangle.Location;
            Double dTextOffsetX = 0;
            Double dTextOffsetY = 0;

            switch (m_eLabelPosition)
            {
            case VertexLabelPosition.TopLeft:

                dTextOffsetX = LabelHorizontalMargin;
                dTextOffsetY = LabelVerticalMargin;
                break;

            case VertexLabelPosition.TopCenter:

                dTextOffsetX = dTextOffsetXForCenter;
                dTextOffsetY = LabelVerticalMargin;

                break;

            case VertexLabelPosition.TopRight:

                dTextOffsetX = -LabelHorizontalMargin;
                dTextOffsetY = LabelVerticalMargin;

                break;

            case VertexLabelPosition.MiddleLeft:

                dTextOffsetX = LabelHorizontalMargin;
                dTextOffsetY = dTextOffsetYForMiddle;

                break;

            case VertexLabelPosition.MiddleCenter:

                dTextOffsetX = dTextOffsetXForCenter;
                dTextOffsetY = dTextOffsetYForMiddle;

                break;

            case VertexLabelPosition.MiddleRight:

                dTextOffsetX = -LabelHorizontalMargin;
                dTextOffsetY = dTextOffsetYForMiddle;

                break;

            case VertexLabelPosition.BottomLeft:

                dTextOffsetX = LabelHorizontalMargin;
                dTextOffsetY = dTextOffsetYForBottom;

                break;

            case VertexLabelPosition.BottomCenter:


                dTextOffsetX = dTextOffsetXForCenter;
                dTextOffsetY = dTextOffsetYForBottom;

                break;

            case VertexLabelPosition.BottomRight:

                dTextOffsetX = -LabelHorizontalMargin;
                dTextOffsetY = dTextOffsetYForBottom;

                break;

            default:

                Debug.Assert(false);
                break;
            }

            oTextOrigin.Offset(dTextOffsetX, dTextOffsetY);

            DrawLabelBackground(oDrawingContext, oGraphDrawingContext,
                                oFormattedText, m_oLabelTextColor, LabelBackgroundAlpha,
                                oTextOrigin);

            oDrawingContext.DrawText(oFormattedText, oTextOrigin);
        }