/// <summary> /// Draws an <see cref="InvariantTextPrimitive"/>. /// </summary> protected override void DrawTextPrimitive(InvariantTextPrimitive textPrimitive) { textPrimitive.CoordinateSystem = CoordinateSystem.Destination; // We adjust the font size depending on the scale so that it's the same size // irrespective of the zoom var fontSize = CalculateScaledFontPoints(textPrimitive.SizeInPoints, Dpi); Font font = CreateFont(textPrimitive.Font, fontSize, FontStyle.Regular, GraphicsUnit.Point, _defaultFont); // Calculate how big the text will be so we can set the bounding box textPrimitive.Dimensions = Surface.FinalBuffer.Graphics.MeasureString(textPrimitive.Text, font); // Draw drop shadow _brush.Color = Color.Black; SizeF dropShadowOffset = new SizeF(1, 1); PointF boundingBoxTopLeft = new PointF(textPrimitive.BoundingBox.Left, textPrimitive.BoundingBox.Top); Surface.FinalBuffer.Graphics.DrawString( textPrimitive.Text, font, _brush, boundingBoxTopLeft + dropShadowOffset); // Draw text _brush.Color = textPrimitive.Color; Surface.FinalBuffer.Graphics.DrawString( textPrimitive.Text, font, _brush, boundingBoxTopLeft); font.Dispose(); textPrimitive.ResetCoordinateSystem(); }
/// <summary> /// Draws a text primitive to the specified destination buffer. /// </summary> /// <param name="buffer">The destination buffer.</param> /// <param name="brush">A GDI brush to use for drawing.</param> /// <param name="fontFactory">A GDI font factory to use for drawing.</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, SolidBrush brush, FontFactory fontFactory, 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 font = fontFactory.GetFont(text.Font, fontSize, FontStyle.Regular, GraphicsUnit.Point, FontFactory.GenericSansSerif); // 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 brush.Color = 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.Color = text.Color; buffer.Graphics.DrawString( text.Text, font, brush, boundingBoxTopLeft); } finally { text.ResetCoordinateSystem(); } }