private static void DrawAnnotationBox(System.Drawing.Graphics g, Rectangle destination, string annotationText, AnnotationBox annotationBox) { Font font; Rectangle rect = RectangleUtilities.CalculateSubRectangle(destination, annotationBox.NormalizedRectangle); Rectangle.Inflate(rect, -4, -4); int num = (rect.Height / annotationBox.NumberOfLines) - 1; StringFormat stringFormat = new StringFormat(); if (annotationBox.Truncation == AnnotationBox.TruncationBehaviour.Truncate) { stringFormat.Trimming = StringTrimming.Character; } else { stringFormat.Trimming = StringTrimming.EllipsisCharacter; } if (annotationBox.FitWidth) { stringFormat.Trimming = StringTrimming.None; } if (annotationBox.Justification == AnnotationBox.JustificationBehaviour.Right) { stringFormat.Alignment = StringAlignment.Far; } else if (annotationBox.Justification == AnnotationBox.JustificationBehaviour.Center) { stringFormat.Alignment = StringAlignment.Center; } else { stringFormat.Alignment = StringAlignment.Near; } if (annotationBox.VerticalAlignment == AnnotationBox.VerticalAlignmentBehaviour.Top) { stringFormat.LineAlignment = StringAlignment.Near; } else if (annotationBox.VerticalAlignment == AnnotationBox.VerticalAlignmentBehaviour.Center) { stringFormat.LineAlignment = StringAlignment.Center; } else { stringFormat.LineAlignment = StringAlignment.Far; } stringFormat.FormatFlags = StringFormatFlags.NoClip; if (annotationBox.NumberOfLines == 1) { stringFormat.FormatFlags |= StringFormatFlags.NoWrap; } FontStyle regular = FontStyle.Regular; if (annotationBox.Bold) { regular |= FontStyle.Bold; } if (annotationBox.Italics) { regular |= FontStyle.Italic; } try { font = new Font(annotationBox.Font, (float)num, regular, GraphicsUnit.Pixel); } catch (Exception exception) { Platform.Log(LogLevel.Error, exception); font = new Font(AnnotationBox.DefaultFont, (float)num, FontStyle.Regular, GraphicsUnit.Pixel); } SizeF layoutArea = new SizeF((float)rect.Width, (float)rect.Height); SizeF ef2 = g.MeasureString(annotationText, font, layoutArea, stringFormat); if (annotationBox.FitWidth && (ef2.Width > rect.Width)) { num = (int)Math.Round((double)((((double)(num * rect.Width)) / ((double)ef2.Width)) - 0.5)); try { font = new Font(annotationBox.Font, (float)num, regular, GraphicsUnit.Pixel); } catch (Exception exception2) { Platform.Log(LogLevel.Error, exception2); font = new Font(AnnotationBox.DefaultFont, (float)num, FontStyle.Regular, GraphicsUnit.Pixel); } } SolidBrush brush = new SolidBrush(Color.Black); rect.Offset(1, 1); g.DrawString(annotationText, font, brush, rect, stringFormat); brush.Color = Color.FromName(annotationBox.Color); rect.Offset(-1, -1); g.DrawString(annotationText, font, brush, rect, stringFormat); brush.Dispose(); font.Dispose(); }
/// <summary> /// Draws an <see cref="AnnotationBox"/>. /// </summary> protected override void DrawAnnotationBox(string annotationText, AnnotationBox annotationBox) { // if there's nothing to draw, there's nothing to do. go figure. if (string.IsNullOrEmpty(annotationText)) { return; } Rectangle clientRectangle = RectangleUtilities.CalculateSubRectangle(Surface.ClientRectangle, annotationBox.NormalizedRectangle); //Deflate the client rectangle by 4 pixels to allow some space //between neighbouring rectangles whose borders coincide. Rectangle.Inflate(clientRectangle, -4, -4); int fontSize = (clientRectangle.Height / annotationBox.NumberOfLines) - 1; //don't draw it if it's too small to read, anyway. if (fontSize < MinimumFontSizeInPixels) { return; } StringFormat format = new StringFormat(); if (annotationBox.Truncation == AnnotationBox.TruncationBehaviour.Truncate) { format.Trimming = StringTrimming.Character; } else { format.Trimming = StringTrimming.EllipsisCharacter; } if (annotationBox.FitWidth) { format.Trimming = StringTrimming.None; } if (annotationBox.Justification == AnnotationBox.JustificationBehaviour.Right) { format.Alignment = StringAlignment.Far; } else if (annotationBox.Justification == AnnotationBox.JustificationBehaviour.Center) { format.Alignment = StringAlignment.Center; } else { format.Alignment = StringAlignment.Near; } if (annotationBox.VerticalAlignment == AnnotationBox.VerticalAlignmentBehaviour.Top) { format.LineAlignment = StringAlignment.Near; } else if (annotationBox.VerticalAlignment == AnnotationBox.VerticalAlignmentBehaviour.Center) { format.LineAlignment = StringAlignment.Center; } else { format.LineAlignment = StringAlignment.Far; } //allow p's and q's, etc to extend slightly beyond the bounding rectangle. Only completely visible lines are shown. format.FormatFlags = StringFormatFlags.NoClip; if (annotationBox.NumberOfLines == 1) { format.FormatFlags |= StringFormatFlags.NoWrap; } FontStyle style = FontStyle.Regular; if (annotationBox.Bold) { style |= FontStyle.Bold; } if (annotationBox.Italics) { style |= FontStyle.Italic; } //don't draw it if it's too small to read, anyway. if (fontSize < MinimumFontSizeInPixels) { return; } Font font = _fontFactory.CreateFont(annotationBox.Font, fontSize, style, GraphicsUnit.Pixel, AnnotationBox.DefaultFont); SizeF layoutArea = new SizeF(clientRectangle.Width, clientRectangle.Height); SizeF size = Surface.FinalBuffer.Graphics.MeasureString(annotationText, font, layoutArea, format); if (annotationBox.FitWidth && size.Width > clientRectangle.Width) { fontSize = (int)(Math.Round(fontSize * clientRectangle.Width / (double)size.Width - 0.5)); //don't draw it if it's too small to read, anyway. if (fontSize < MinimumFontSizeInPixels) { return; } font = _fontFactory.CreateFont(annotationBox.Font, fontSize, style, GraphicsUnit.Pixel, AnnotationBox.DefaultFont); } // Draw drop shadow _brush.Color = Color.Black; clientRectangle.Offset(1, 1); Surface.FinalBuffer.Graphics.DrawString( annotationText, font, _brush, clientRectangle, format); _brush.Color = Color.FromName(annotationBox.Color); clientRectangle.Offset(-1, -1); Surface.FinalBuffer.Graphics.DrawString( annotationText, font, _brush, clientRectangle, format); }
/// <summary> /// Draws an annotation box to the specified destination buffer. /// </summary> /// <param name="buffer">The destination buffer.</param> /// <param name="gdiObjectFactory">A factory for GDI objects.</param> /// <param name="annotationText">The annotation text to be drawn.</param> /// <param name="annotationBox">The annotation box to be drawn.</param> /// <param name="dpi">The intended output DPI.</param> public static void DrawAnnotationBox(IGdiBuffer buffer, IGdiObjectFactory gdiObjectFactory, string annotationText, AnnotationBox annotationBox, float dpi = _nominalScreenDpi) { // if there's nothing to draw, there's nothing to do. go figure. if (string.IsNullOrWhiteSpace(annotationText)) { return; } var clientRectangle = RectangleUtilities.CalculateSubRectangle(buffer.Bounds, annotationBox.NormalizedRectangle); //Deflate the client rectangle by 4 pixels to allow some space //between neighbouring rectangles whose borders coincide. Rectangle.Inflate(clientRectangle, -4, -4); var fontSize = (clientRectangle.Height / annotationBox.NumberOfLines) - 1; //don't draw it if it's too small to read, anyway. if (fontSize < MinimumFontSizeInPixels) { return; } var style = FontStyle.Regular; if (annotationBox.Bold) { style |= FontStyle.Bold; } if (annotationBox.Italics) { style |= FontStyle.Italic; } //don't draw it if it's too small to read, anyway. if (fontSize < MinimumFontSizeInPixels) { return; } var fontArgs = new CreateFontArgs(annotationBox.Font, fontSize, style, GraphicsUnit.Pixel) { DefaultFontName = AnnotationBox.DefaultFont }; var font = gdiObjectFactory.CreateFont(fontArgs); var format = gdiObjectFactory.CreateStringFormat(new CreateStringFormatArgs(annotationBox)); var layoutArea = new SizeF(clientRectangle.Width, clientRectangle.Height); var size = buffer.Graphics.MeasureString(annotationText, font, layoutArea, format); if (annotationBox.FitWidth && size.Width > clientRectangle.Width) { fontSize = (int)(Math.Round(fontSize * clientRectangle.Width / (double)size.Width - 0.5)); //don't draw it if it's too small to read, anyway. if (fontSize < MinimumFontSizeInPixels) { return; } font = gdiObjectFactory.CreateFont(fontArgs); } // Draw drop shadow var brush = gdiObjectFactory.CreateBrush(new CreateBrushArgs(Color.Black)); clientRectangle.Offset(1, 1); buffer.Graphics.DrawString( annotationText, font, brush, clientRectangle, format); brush = gdiObjectFactory.CreateBrush(new CreateBrushArgs(annotationBox.Color)); clientRectangle.Offset(-1, -1); buffer.Graphics.DrawString( annotationText, font, brush, clientRectangle, format); }