/// <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; }
/// <ToBeCompleted></ToBeCompleted> public virtual bool GetCaptionTextBounds(int index, out Point topLeft, out Point topRight, out Point bottomRight, out Point bottomLeft) { if (index != 0) { throw new IndexOutOfRangeException("index"); } bool result; Rectangle captionBounds = Rectangle.Empty; CalcCaptionBounds(index, out captionBounds); Rectangle textBounds = Rectangle.Empty; if (caption != null) { textBounds = caption.CalculateTextBounds(captionBounds, CharacterStyle, ParagraphStyle, DisplayService); result = true; } else { // Calculate placeholder bounds textBounds.Size = TextMeasurer.MeasureText("Iq", CharacterStyle, captionBounds.Size, ParagraphStyle); textBounds.X = (int)Math.Round(captionBounds.X + (captionBounds.Width / 2f) - textBounds.Width / 2f); textBounds.Y = (int)Math.Round(captionBounds.Y + (captionBounds.Height / 2f) - textBounds.Height / 2f); result = false; } Geometry.TransformRectangle(Center, Angle, textBounds, out topLeft, out topRight, out bottomRight, out bottomLeft); return(result); }
/// <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); }
private void FitShapeToText() { System.Diagnostics.Debug.Assert(CharacterStyle != null); System.Diagnostics.Debug.Assert(ParagraphStyle != null); if (!string.IsNullOrEmpty(Text) && CharacterStyle != null && ParagraphStyle != null) { Size textSize = TextMeasurer.MeasureText(Text, CharacterStyle, Size.Empty, ParagraphStyle); Width = textSize.Width + ParagraphStyle.Padding.Horizontal; Height = textSize.Height + ParagraphStyle.Padding.Vertical; } }
/// <override></override> protected override void CalcCaptionBounds(int index, out Rectangle captionBounds) { captionBounds = Rectangle.Empty; if (autoSize) { captionBounds.Size = TextMeasurer.MeasureText(Text, CharacterStyle, Size.Empty, ParagraphStyle); captionBounds.Width += ParagraphStyle.Padding.Horizontal; captionBounds.Height += ParagraphStyle.Padding.Vertical; captionBounds.X = (int)Math.Round(-captionBounds.Width / 2f); captionBounds.Y = (int)Math.Round(-captionBounds.Height / 2f); } else { base.CalcCaptionBounds(index, out captionBounds); } }