Пример #1
0
 /// <summary>
 /// Measures the given text and returns its size.
 /// </summary>
 /// <param name="text">The text to measure</param>
 /// <param name="characterStyle">The text's character style.</param>
 /// <param name="proposedSize">The layout area of the text. Size.Empty means no fitting in area.</param>
 /// <param name="paragraphStyle">The paragraph layout of the text</param>
 /// <returns></returns>
 public static Size MeasureText(string text, ICharacterStyle characterStyle, Size proposedSize, IParagraphStyle paragraphStyle)
 {
     using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
         return(MeasureText(graphics, text, ToolCache.GetFont(characterStyle), proposedSize, ToolCache.GetStringFormat(paragraphStyle)));
 }
Пример #2
0
 /// <summary>
 /// Measures the given text and returns its size.
 /// </summary>
 /// <param name="graphics">Graphics context used for the measuring</param>
 /// <param name="text">The text to measure</param>
 /// <param name="characterStyle">The text's character style</param>
 /// <param name="proposedSize">The layout area of the text. Size.Empty means no fitting in area.</param>
 /// <param name="paragraphStyle">The paragraph layout of the text</param>
 /// <returns></returns>
 public static Size MeasureText(Graphics graphics, string text, ICharacterStyle characterStyle, Size proposedSize, IParagraphStyle paragraphStyle)
 {
     if (paragraphStyle == null)
     {
         throw new ArgumentNullException("paragraphStyle");
     }
     return(MeasureText(graphics, text, ToolCache.GetFont(characterStyle), proposedSize, ToolCache.GetStringFormat(paragraphStyle)));
 }
Пример #3
0
        /// <summary>
        /// Calculates the current text's area within the given caption bounds.
        /// </summary>
        public Rectangle CalculateTextBounds(Rectangle captionBounds, ICharacterStyle characterStyle,
                                             IParagraphStyle paragraphStyle, IDisplayService displayService)
        {
            Rectangle textBounds = Rectangle.Empty;

            Debug.Assert(characterStyle != null);
            Debug.Assert(paragraphStyle != null);

            // measure the text size
            //if (float.IsNaN(dpiY)) dpiY = gfx.DpiY;
            if (displayService != null)
            {
                textBounds.Size = TextMeasurer.MeasureText(displayService.InfoGraphics, string.IsNullOrEmpty(captionText)
                                                                                                                ? "Ig"
                                                                                                                : captionText,
                                                           ToolCache.GetFont(characterStyle), captionBounds.Size, paragraphStyle);
            }
            else
            {
                textBounds.Size = TextMeasurer.MeasureText(string.IsNullOrEmpty(captionText)
                                                                                ? "Ig"
                                                                                : captionText, ToolCache.GetFont(characterStyle), captionBounds.Size,
                                                           paragraphStyle);
            }

            // clip text bounds if too large
            if (textBounds.Width > captionBounds.Width)
            {
                textBounds.Width = captionBounds.Width;
            }
            if (textBounds.Height > captionBounds.Height)
            {
                textBounds.Height = captionBounds.Height;
            }

            // set horizontal alignment
            switch (paragraphStyle.Alignment)
            {
            case ContentAlignment.BottomLeft:
            case ContentAlignment.MiddleLeft:
            case ContentAlignment.TopLeft:
                textBounds.X = captionBounds.X;
                break;

            case ContentAlignment.BottomCenter:
            case ContentAlignment.MiddleCenter:
            case ContentAlignment.TopCenter:
                textBounds.X = captionBounds.X + (int)Math.Round((captionBounds.Width - textBounds.Width) / 2f);
                break;

            case ContentAlignment.BottomRight:
            case ContentAlignment.MiddleRight:
            case ContentAlignment.TopRight:
                textBounds.X = captionBounds.Right - textBounds.Width;
                break;

            default:
                Debug.Assert(false, "Unhandled switch case");
                break;
            }
            // set vertical alignment
            switch (paragraphStyle.Alignment)
            {
            case ContentAlignment.BottomCenter:
            case ContentAlignment.BottomLeft:
            case ContentAlignment.BottomRight:
                textBounds.Y = captionBounds.Bottom - textBounds.Height;
                break;

            case ContentAlignment.MiddleCenter:
            case ContentAlignment.MiddleLeft:
            case ContentAlignment.MiddleRight:
                textBounds.Y = captionBounds.Top + (int)Math.Round((captionBounds.Height - textBounds.Height) / 2f);
                break;

            case ContentAlignment.TopCenter:
            case ContentAlignment.TopLeft:
            case ContentAlignment.TopRight:
                textBounds.Y = captionBounds.Top;
                break;

            default:
                Debug.Assert(false, "Unhandled switch case");
                break;
            }
            return(textBounds);
        }