/// <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; }
/// <summary> /// Calculates the caption path in the untransformed state. /// </summary> /// <param name="layoutX">X coordinate of the layout rectangle</param> /// <param name="layoutY">Y coordinate of the layout rectangle</param> /// <param name="layoutW">Width of the layout rectangle</param> /// <param name="layoutH">Height of the layout rectangle</param> /// <param name="characterStyle">Character style of the caption</param> /// <param name="paragraphStyle">Paragraph style of the caption</param> /// <returns></returns> public bool CalculatePath(int layoutX, int layoutY, int layoutW, int layoutH, ICharacterStyle characterStyle, IParagraphStyle paragraphStyle) { if (characterStyle == null) throw new ArgumentNullException("charStyle"); if (paragraphStyle == null) throw new ArgumentNullException("paragraphStyle"); if (string.IsNullOrEmpty(captionText)) return true; else if (textPath != null /*&& layoutW > 0 && layoutH > 0*/) { // Collect objects for calculating text layout Font font = ToolCache.GetFont(characterStyle); StringFormat formatter = ToolCache.GetStringFormat(paragraphStyle); Rectangle textBounds = Rectangle.Empty; textBounds.X = layoutX + paragraphStyle.Padding.Left; textBounds.Y = layoutY + paragraphStyle.Padding.Top; textBounds.Width = Math.Max(1, layoutW - paragraphStyle.Padding.Horizontal); textBounds.Height = Math.Max(1, layoutH - paragraphStyle.Padding.Vertical); // Create text path textPath.Reset(); textPath.StartFigure(); textPath.AddString(PathText, font.FontFamily, (int)font.Style, characterStyle.Size, textBounds, formatter); textPath.CloseFigure(); #if DEBUG_DIAGNOSTICS if (textPath.PointCount == 0 && PathText.Trim() != string.Empty) { Size textSize = TextMeasurer.MeasureText(PathText, font, textBounds.Size, paragraphStyle); Debug.Print("Failed to create TextPath - please check if the caption bounds are too small for the text."); } #endif return true; } return false; }
/// <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))); }
/// <override></override> public override void DrawThumbnail(Image image, int margin, Color transparentColor) { AutoSize = false; Text = "ab"; Size textSize = Size.Empty; // Create a ParameterStyle without padding ParagraphStyle paragraphStyle = new ParagraphStyle(); paragraphStyle.Alignment = ContentAlignment.TopLeft; paragraphStyle.Padding = new TextPadding(0); paragraphStyle.Trimming = StringTrimming.None; paragraphStyle.WordWrap = false; ParagraphStyle = paragraphStyle; textSize = TextMeasurer.MeasureText(Text, ToolCache.GetFont(CharacterStyle), textSize, ParagraphStyle); Width = textSize.Width; Height = textSize.Height; // If the linestyle is transparent, modify margin in order to improve the text's readability int marginCorrection = (LineStyle.ColorStyle.Transparency == 100) ? 3 : 0; base.DrawThumbnail(image, margin - marginCorrection, transparentColor); }
/// <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))); }