/// <summary> /// Draws a text primitive to the specified destination buffer. /// </summary> /// <param name="buffer">The destination buffer.</param> /// <param name="gdiObjectFactory">A factory for GDI+ objects.</param> /// <param name="text">The text primitive to be drawn.</param> /// <param name="dpi">The intended output DPI.</param> public static void DrawTextPrimitive(IGdiBuffer buffer, IGdiObjectFactory gdiObjectFactory, InvariantTextPrimitive text, float dpi = _nominalScreenDpi) { text.CoordinateSystem = CoordinateSystem.Destination; try { // We adjust the font size depending on the scale so that it's the same size // irrespective of the zoom var fontSize = CalculateScaledFontPoints(text.SizeInPoints, dpi); var createFontArgs = new CreateFontArgs(text.Font, fontSize, FontStyle.Regular, GraphicsUnit.Point) { DefaultFontName = FontFactory.GenericSansSerif }; var font = gdiObjectFactory.CreateFont(createFontArgs); // Calculate how big the text will be so we can set the bounding box text.Dimensions = buffer.Graphics.MeasureString(text.Text, font); // Draw drop shadow var brush = gdiObjectFactory.CreateBrush(new CreateBrushArgs(Color.Black)); var dropShadowOffset = new SizeF(1, 1); var boundingBoxTopLeft = new PointF(text.BoundingBox.Left, text.BoundingBox.Top); buffer.Graphics.DrawString( text.Text, font, brush, boundingBoxTopLeft + dropShadowOffset); // Draw text brush = gdiObjectFactory.CreateBrush(new CreateBrushArgs(text.Color)); buffer.Graphics.DrawString( text.Text, font, brush, boundingBoxTopLeft); } finally { text.ResetCoordinateSystem(); } }
/// <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); }
/// <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); }