/// <summary> /// Returns a polygon that defines the bounding box of /// the text, taking into account alignment and rotation parameters. /// </summary> /// <param name="g"> /// A graphic device object to be drawn into. This is normally e.Graphics from the /// PaintEventArgs argument to the Paint() method. /// </param> /// <param name="text">A string value containing the text to be /// displayed. This can be multiple lines, separated by newline ('\n') /// characters</param> /// <param name="x">The X location to display the text, in screen /// coordinates, relative to the horizontal (<see cref="AlignH"/>) /// alignment parameter <paramref name="alignH"/></param> /// <param name="y">The Y location to display the text, in screen /// coordinates, relative to the vertical (<see cref="AlignV"/> /// alignment parameter <paramref name="alignV"/></param> /// <param name="alignH">A horizontal alignment parameter specified /// using the <see cref="AlignH"/> enum type</param> /// <param name="alignV">A vertical alignment parameter specified /// using the <see cref="AlignV"/> enum type</param> /// <param name="scaleFactor"> /// The scaling factor to be used for rendering objects. This is calculated and /// passed down by the parent <see cref="GraphPane"/> object using the /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust /// font sizes, etc. according to the actual size of the graph. /// </param> /// <param name="layoutArea">The limiting area (<see cref="SizeF"/>) into which the text /// must fit. The actual rectangle may be smaller than this, but the text will be wrapped /// to accomodate the area.</param> /// <returns>A polygon of 4 points defining the area of this text</returns> public PointF[] GetBox( Graphics g, string text, float x, float y, AlignH alignH, AlignV alignV, float scaleFactor, SizeF layoutArea ) { // make sure the font size is properly scaled Remake( scaleFactor, this.Size, ref _scaledSize, ref _font ); // Get the width and height of the text SizeF sizeF; if ( layoutArea.IsEmpty ) sizeF = g.MeasureStringCache(text, _font); else sizeF = g.MeasureStringCache(text, _font, layoutArea); // Create a bounding box rectangle for the text RectangleF rect = new RectangleF( new PointF( -sizeF.Width / 2.0F, 0.0F ), sizeF ); Matrix matrix = new Matrix(); SetupMatrix( matrix, x, y, sizeF, alignH, alignV, _angle ); PointF[] pts = new PointF[4]; pts[0] = new PointF( rect.Left, rect.Top ); pts[1] = new PointF( rect.Right, rect.Top ); pts[2] = new PointF( rect.Right, rect.Bottom ); pts[3] = new PointF( rect.Left, rect.Bottom ); matrix.TransformPoints( pts ); return pts; }
/// <summary> /// Render the specified <paramref name="text"/> to the specifed /// <see cref="Graphics"/> device. The text, border, and fill options /// will be rendered as required. This special case method will show the /// specified text as a power of 10, using the <see cref="Default.SuperSize"/> /// and <see cref="Default.SuperShift"/>. /// </summary> /// <param name="g"> /// A graphic device object to be drawn into. This is normally e.Graphics from the /// PaintEventArgs argument to the Paint() method. /// </param> /// <param name="pane"> /// A reference to the <see cref="ZedGraph.GraphPane"/> object that is the parent or /// owner of this object. /// </param> /// <param name="text">A string value containing the text to be /// displayed. This can be multiple lines, separated by newline ('\n') /// characters</param> /// <param name="x">The X location to display the text, in screen /// coordinates, relative to the horizontal (<see cref="AlignH"/>) /// alignment parameter <paramref name="alignH"/></param> /// <param name="y">The Y location to display the text, in screen /// coordinates, relative to the vertical (<see cref="AlignV"/> /// alignment parameter <paramref name="alignV"/></param> /// <param name="alignH">A horizontal alignment parameter specified /// using the <see cref="AlignH"/> enum type</param> /// <param name="alignV">A vertical alignment parameter specified /// using the <see cref="AlignV"/> enum type</param> /// <param name="scaleFactor"> /// The scaling factor to be used for rendering objects. This is calculated and /// passed down by the parent <see cref="GraphPane"/> object using the /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust /// font sizes, etc. according to the actual size of the graph. /// </param> public void DrawTenPower( Graphics g, GraphPane pane, string text, float x, float y, AlignH alignH, AlignV alignV, float scaleFactor ) { SmoothingMode sModeSave = g.SmoothingMode; TextRenderingHint sHintSave = g.TextRenderingHint; if ( _isAntiAlias ) { g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = TextRenderingHint.AntiAlias; } // make sure the font size is properly scaled Remake( scaleFactor, this.Size, ref _scaledSize, ref _font ); float scaledSuperSize = _scaledSize * Default.SuperSize; Remake( scaleFactor, this.Size * Default.SuperSize, ref scaledSuperSize, ref _superScriptFont ); // Get the width and height of the text SizeF size10 = g.MeasureStringCache("10", _font); SizeF sizeText = g.MeasureStringCache(text, _superScriptFont); SizeF totSize = new SizeF( size10.Width + sizeText.Width, size10.Height + sizeText.Height * Default.SuperShift ); float charWidth = g.MeasureStringCache("x", _superScriptFont).Width; // Save the old transform matrix for later restoration Matrix saveMatrix = g.Transform; g.Transform = SetupMatrix( g.Transform, x, y, totSize, alignH, alignV, _angle ); // make a center justified StringFormat alignment // for drawing the text StringFormat strFormat = new StringFormat(); strFormat.Alignment = _stringAlignment; // Create a rectangle representing the border around the // text. Note that, while the text is drawn based on the // TopCenter position, the rectangle is drawn based on // the TopLeft position. Therefore, move the rectangle // width/2 to the left to align it properly RectangleF rectF = new RectangleF( -totSize.Width / 2.0F, 0.0F, totSize.Width, totSize.Height ); // If the background is to be filled, fill it _fill.Draw( g, rectF ); // Draw the border around the text if required _border.Draw( g, pane, scaleFactor, rectF ); // make a solid brush for rendering the font itself using ( SolidBrush brush = new SolidBrush( _fontColor ) ) { // Draw the actual text. Note that the coordinate system // is set up such that 0,0 is at the location where the // CenterTop of the text needs to be. g.DrawString( "10", _font, brush, ( -totSize.Width + size10.Width ) / 2.0F, sizeText.Height * Default.SuperShift, strFormat ); g.DrawString( text, _superScriptFont, brush, ( totSize.Width - sizeText.Width - charWidth ) / 2.0F, 0.0F, strFormat ); } // Restore the transform matrix back to original g.Transform = saveMatrix; g.SmoothingMode = sModeSave; g.TextRenderingHint = sHintSave; }
/// <summary> /// Determines if the specified screen point lies within the bounding box of /// the text, taking into account alignment and rotation parameters. /// </summary> /// <param name="pt">The screen point, in pixel units</param> /// <param name="g"> /// A graphic device object to be drawn into. This is normally e.Graphics from the /// PaintEventArgs argument to the Paint() method. /// </param> /// <param name="text">A string value containing the text to be /// displayed. This can be multiple lines, separated by newline ('\n') /// characters</param> /// <param name="x">The X location to display the text, in screen /// coordinates, relative to the horizontal (<see cref="AlignH"/>) /// alignment parameter <paramref name="alignH"/></param> /// <param name="y">The Y location to display the text, in screen /// coordinates, relative to the vertical (<see cref="AlignV"/> /// alignment parameter <paramref name="alignV"/></param> /// <param name="alignH">A horizontal alignment parameter specified /// using the <see cref="AlignH"/> enum type</param> /// <param name="alignV">A vertical alignment parameter specified /// using the <see cref="AlignV"/> enum type</param> /// <param name="scaleFactor"> /// The scaling factor to be used for rendering objects. This is calculated and /// passed down by the parent <see cref="GraphPane"/> object using the /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust /// font sizes, etc. according to the actual size of the graph. /// </param> /// <param name="layoutArea">The limiting area (<see cref="SizeF"/>) into which the text /// must fit. The actual rectangle may be smaller than this, but the text will be wrapped /// to accomodate the area.</param> /// <returns>true if the point lies within the bounding box, false otherwise</returns> public bool PointInBox( PointF pt, Graphics g, string text, float x, float y, AlignH alignH, AlignV alignV, float scaleFactor, SizeF layoutArea ) { // make sure the font size is properly scaled Remake( scaleFactor, this.Size, ref _scaledSize, ref _font ); // Get the width and height of the text SizeF sizeF; if ( layoutArea.IsEmpty ) sizeF = g.MeasureStringCache(text, _font); else sizeF = g.MeasureStringCache(text, _font, layoutArea); // Create a bounding box rectangle for the text RectangleF rect = new RectangleF( new PointF( -sizeF.Width / 2.0F, 0.0F ), sizeF ); // Build a transform matrix that inverts that drawing transform // in this manner, the point is brought back to the box, rather // than vice-versa. This allows the container check to be a simple // RectangleF.Contains, since the rectangle won't be rotated. Matrix matrix = GetMatrix( x, y, sizeF, alignH, alignV, _angle ); PointF[] pts = new PointF[1]; pts[0] = pt; matrix.TransformPoints( pts ); return rect.Contains( pts[0] ); }
/// <summary> /// Get a <see cref="SizeF"/> struct representing the width and height /// of the bounding box for the specified text string, based on the scaled font size. /// </summary> /// <remarks> /// This special case method will show the specified string as a power of 10, /// superscripted and downsized according to the /// <see cref="Default.SuperSize"/> and <see cref="Default.SuperShift"/>. /// This routine differs from <see cref="MeasureString(Graphics,string,float)"/> in that it takes into /// account the rotation angle of the font, and gives the dimensions of the /// bounding box that encloses the text at the specified angle. /// </remarks> /// <param name="g"> /// A graphic device object to be drawn into. This is normally e.Graphics from the /// PaintEventArgs argument to the Paint() method. /// </param> /// <param name="text">The text string for which the width is to be calculated /// </param> /// <param name="scaleFactor"> /// The scaling factor to be used for rendering objects. This is calculated and /// passed down by the parent <see cref="GraphPane"/> object using the /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust /// font sizes, etc. according to the actual size of the graph. /// </param> /// <returns>The scaled text dimensions, in pixels, in the form of /// a <see cref="SizeF"/> struct</returns> public SizeF BoundingBoxTenPower( Graphics g, string text, float scaleFactor ) { //Remake( scaleFactor, this.Size, ref this.scaledSize, ref this.font ); float scaledSuperSize = _scaledSize * Default.SuperSize; Remake( scaleFactor, this.Size * Default.SuperSize, ref scaledSuperSize, ref _superScriptFont ); // Get the width and height of the text SizeF size10 = MeasureString( g, "10", scaleFactor ); SizeF sizeText = g.MeasureStringCache(text, _superScriptFont); if ( _isDropShadow ) { sizeText.Width += (float)( Math.Cos( _dropShadowAngle ) * _dropShadowOffset * _superScriptFont.Height ); sizeText.Height += (float)( Math.Sin( _dropShadowAngle ) * _dropShadowOffset * _superScriptFont.Height ); } SizeF totSize = new SizeF( size10.Width + sizeText.Width, size10.Height + sizeText.Height * Default.SuperShift ); float cs = (float)Math.Abs( Math.Cos( _angle * Math.PI / 180.0 ) ); float sn = (float)Math.Abs( Math.Sin( _angle * Math.PI / 180.0 ) ); SizeF s2 = new SizeF( totSize.Width * cs + totSize.Height * sn, totSize.Width * sn + totSize.Height * cs ); return s2; }
/// <summary> /// Get a <see cref="SizeF"/> struct representing the width and height /// of the specified text string, based on the scaled font size, and using /// the specified <see cref="SizeF"/> as an outer limit. /// </summary> /// <remarks> /// This method will allow the text to wrap as necessary to fit the /// <see paramref="layoutArea"/>. /// </remarks> /// <param name="g"> /// A graphic device object to be drawn into. This is normally e.Graphics from the /// PaintEventArgs argument to the Paint() method. /// </param> /// <param name="text">The text string for which the width is to be calculated /// </param> /// <param name="scaleFactor"> /// The scaling factor to be used for rendering objects. This is calculated and /// passed down by the parent <see cref="GraphPane"/> object using the /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust /// font sizes, etc. according to the actual size of the graph. /// </param> /// <param name="layoutArea">The limiting area (<see cref="SizeF"/>) into which the text /// must fit. The actual rectangle may be smaller than this, but the text will be wrapped /// to accomodate the area.</param> /// <returns>The scaled text dimensions, in pixels, in the form of /// a <see cref="SizeF"/> struct</returns> public SizeF MeasureString( Graphics g, string text, float scaleFactor, SizeF layoutArea ) { Remake( scaleFactor, this.Size, ref _scaledSize, ref _font ); SizeF size = g.MeasureStringCache( text, _font, layoutArea ); if ( _isDropShadow ) { size.Width += (float)( Math.Cos( _dropShadowAngle ) * _dropShadowOffset * _font.Height ); size.Height += (float)( Math.Sin( _dropShadowAngle ) * _dropShadowOffset * _font.Height ); } return size; }
/// <summary> /// Get the total width of the specified text string /// </summary> /// <param name="g"> /// A graphic device object to be drawn into. This is normally e.Graphics from the /// PaintEventArgs argument to the Paint() method. /// </param> /// <param name="text">The text string for which the width is to be calculated /// </param> /// <param name="scaleFactor"> /// The scaling factor to be used for rendering objects. This is calculated and /// passed down by the parent <see cref="GraphPane"/> object using the /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust /// font sizes, etc. according to the actual size of the graph. /// </param> /// <returns>The scaled text width, in pixels</returns> public float GetWidth( Graphics g, string text, float scaleFactor ) { Remake( scaleFactor, this.Size, ref _scaledSize, ref _font ); float width = g.MeasureStringCache(text, _font).Width; if ( _isDropShadow ) width += (float)( Math.Cos( _dropShadowAngle ) * _dropShadowOffset * _font.Height ); return width; }
/// <summary> /// Get the average character width of the scaled font. The average width is /// based on the character 'x' /// </summary> /// <param name="g"> /// A graphic device object to be drawn into. This is normally e.Graphics from the /// PaintEventArgs argument to the Paint() method. /// </param> /// <param name="scaleFactor"> /// The scaling factor to be used for rendering objects. This is calculated and /// passed down by the parent <see cref="GraphPane"/> object using the /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust /// font sizes, etc. according to the actual size of the graph. /// </param> /// <returns>The scaled font width, in pixels</returns> public float GetWidth( Graphics g, float scaleFactor ) { Remake( scaleFactor, this.Size, ref _scaledSize, ref _font ); return g.MeasureStringCache("x", _font).Width; }