/// <summary> /// Gets rectangle of the arrow text /// </summary> /// <param name="g">GraphicsPath</param> /// <param name="style">Style of the arrow</param> /// <param name="textStyle">Style of the arrow text</param> /// <param name="points">Arrow's point collection</param> /// <param name="textColor">Text color</param> /// <param name="segmentCount">Arrow segments count</param> /// <param name="text">Arrow's text itself</param> /// <param name="textFont">Arrow's text font</param> /// <param name="rect">Bounding rect</param> /// <param name="a">Text's rotation angle to be returned</param> /// <returns>Arrow text's rectangle</returns> private RectangleF getTextRect(Graphics g , ArrowStyle style, ArrowTextStyle textStyle, PointCollection points , Color textColor ,int segmentCount , string text , Font textFont , RectangleF rect , ref float a ) { // DrawString crashes with a too small PageScale if (g.PageScale < 0.01) return RectangleF.Empty; System.Drawing.Brush brText = new System.Drawing.SolidBrush(textColor); StringFormat sf = StringFormat.GenericDefault; sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Far; float x =0 , y = 0; a = 0; ArrowUtils au = new ArrowUtils(style,points ); System.Drawing.SizeF textSize = g.MeasureString(text, textFont); if (textStyle == ArrowTextStyle.Rotate) { // Find the segment near which the text will be drawn int pt = points.Count / 2 - 1; int jump = 1; int ptBest = pt; float r = 0, rBest = 0; if (style != ArrowStyle.Bezier) { if (style == ArrowStyle.Cascading || (style == ArrowStyle.Polyline && segmentCount % 2 == 1)) { do { MindFusion.Geometry.Geometry2D.Convert.DekartToPolar( points[pt], points[pt + 1], ref a, ref r); if (r > rBest) { rBest = r; ptBest = pt; } pt = pt + jump; jump = -Math.Sign(jump) * (Math.Abs(jump) + 1); if(pt < 0 || pt >= points.Count - 1) break; } while(textSize.Width > r); pt = ptBest; x = (points[pt].X + points[pt + 1].X) / 2; y = (points[pt].Y + points[pt + 1].Y) / 2; } else { pt = pt + 1; x = points[pt].X; y = points[pt].Y; } } else { if(segmentCount % 2 == 1) { pt = (pt + 1) / 4; PointF ptMid = ArrowUtils.GetBezierPt(points, pt, 0.5f); x = ptMid.X; y = ptMid.Y; } else { pt = (pt + 1) / 4; PointF ptMid = points[(pt + 1) * 3]; x = ptMid.X; y = ptMid.Y; } } // Find out the angle if (style != ArrowStyle.Bezier) { if( style == ArrowStyle.Cascading || (style == ArrowStyle.Polyline && segmentCount % 2 == 1)) { PointF pt1 = points[pt]; PointF pt2 = points[pt + 1]; MindFusion.Geometry.Geometry2D.Convert.DekartToPolar( pt1, pt2, ref a, ref r); } else { float r1 = 0, a1 = 0; MindFusion.Geometry.Geometry2D.Convert.DekartToPolar( points[pt - 1], points[pt], ref a, ref r); MindFusion.Geometry.Geometry2D.Convert.DekartToPolar( points[pt], points[pt + 1], ref a1, ref r1); a = (a + a1) / 2; } } else { if (segmentCount % 2 == 1) { PointF pt1 = ArrowUtils.GetBezierPt(points, pt, 0.45f); PointF pt2 = ArrowUtils.GetBezierPt(points, pt, 0.55f); MindFusion.Geometry.Geometry2D.Convert.DekartToPolar( pt1, pt2, ref a, ref r); } else { PointF pt1 = ArrowUtils.GetBezierPt(points, pt, 0.95f); PointF pt2 = ArrowUtils.GetBezierPt(points, pt + 1, 0.05f); MindFusion.Geometry.Geometry2D.Convert.DekartToPolar( pt1, pt2, ref a, ref r); } } a = 180 - a; if (a > 90 && a < 270) a -= 180; } else if (textStyle == ArrowTextStyle.Center) { if(points.Count % 2 == 1) { x = points[points.Count / 2].X; y = points[points.Count / 2].Y; } else { x = points[points.Count / 2 - 1].X + points[points.Count / 2].X; y = points[points.Count / 2 - 1].Y + points[points.Count / 2].Y; x /= 2; y /= 2; } //g.DrawString(text, textFont, brText, new PointF(x, y), sf); } else if (textStyle == ArrowTextStyle.OverLongestSegment) { // find the center point of longest segment int longest = au.getLongestSegment(); PointF center = au.getSegmentCenter(longest); // see how much space the text requires StringFormat cf = m_FlowChart.TextFormat; SizeF size = g.MeasureString(text, textFont); size.Width += ArrowUtils.getMillimeter(m_FlowChart.MeasureUnit); size.Height += ArrowUtils.getMillimeter(m_FlowChart.MeasureUnit); } brText.Dispose(); RectangleF rc = new RectangleF(x - textSize.Width/2 ,y - textSize.Height/2,textSize.Width, textSize.Height); return rc; }
private RectangleF getTextRect(Graphics g, ArrowStyle style, ArrowTextStyle textStyle, PointCollection points, Color textColor, int segmentCount, string text, Font textFont, RectangleF rect, ref float a) { // DrawString crashes with a too small PageScale if (g.PageScale < 0.01) { return(RectangleF.Empty); } System.Drawing.Brush brText = new System.Drawing.SolidBrush(textColor); StringFormat sf = StringFormat.GenericDefault; sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Far; float x = 0, y = 0; a = 0; ArrowUtils au = new ArrowUtils(style, points); System.Drawing.SizeF textSize = g.MeasureString(text, textFont); if (textStyle == ArrowTextStyle.Rotate) { // Find the segment near which the text will be drawn int pt = points.Count / 2 - 1; int jump = 1; int ptBest = pt; float r = 0, rBest = 0; if (style != ArrowStyle.Bezier) { if (style == ArrowStyle.Cascading || (style == ArrowStyle.Polyline && segmentCount % 2 == 1)) { do { MindFusion.Diagramming.Export.Convert.DekartToPolar( points[pt], points[pt + 1], ref a, ref r); if (r > rBest) { rBest = r; ptBest = pt; } pt = pt + jump; jump = -Math.Sign(jump) * (Math.Abs(jump) + 1); if (pt < 0 || pt >= points.Count - 1) { break; } }while(textSize.Width > r); pt = ptBest; x = (points[pt].X + points[pt + 1].X) / 2; y = (points[pt].Y + points[pt + 1].Y) / 2; } else { pt = pt + 1; x = points[pt].X; y = points[pt].Y; } } else { if (segmentCount % 2 == 1) { pt = (pt + 1) / 4; PointF ptMid = ArrowUtils.GetBezierPt(points, pt, 0.5f); x = ptMid.X; y = ptMid.Y; } else { pt = (pt + 1) / 4; PointF ptMid = points[(pt + 1) * 3]; x = ptMid.X; y = ptMid.Y; } } // Find out the angle if (style != ArrowStyle.Bezier) { if (style == ArrowStyle.Cascading || (style == ArrowStyle.Polyline && segmentCount % 2 == 1)) { PointF pt1 = points[pt]; PointF pt2 = points[pt + 1]; MindFusion.Diagramming.Export.Convert.DekartToPolar( pt1, pt2, ref a, ref r); } else { float r1 = 0, a1 = 0; MindFusion.Diagramming.Export.Convert.DekartToPolar( points[pt - 1], points[pt], ref a, ref r); MindFusion.Diagramming.Export.Convert.DekartToPolar( points[pt], points[pt + 1], ref a1, ref r1); a = (a + a1) / 2; } } else { if (segmentCount % 2 == 1) { PointF pt1 = ArrowUtils.GetBezierPt(points, pt, 0.45f); PointF pt2 = ArrowUtils.GetBezierPt(points, pt, 0.55f); MindFusion.Diagramming.Export.Convert.DekartToPolar( pt1, pt2, ref a, ref r); } else { PointF pt1 = ArrowUtils.GetBezierPt(points, pt, 0.95f); PointF pt2 = ArrowUtils.GetBezierPt(points, pt + 1, 0.05f); MindFusion.Diagramming.Export.Convert.DekartToPolar( pt1, pt2, ref a, ref r); } } a = 180 - a; if (a > 90 && a < 270) { a -= 180; } // Draw the text /* * * * GraphicsState state = g.Save(); * * g.TranslateTransform(x, y); * g.RotateTransform(a); * g.TranslateTransform(-x, -y); * g.DrawString(text, textFont, brText, new PointF(x, y), sf); * * g.Restore(state); */ } else if (textStyle == ArrowTextStyle.Center) { if (points.Count % 2 == 1) { x = points[points.Count / 2].X; y = points[points.Count / 2].Y; } else { x = points[points.Count / 2 - 1].X + points[points.Count / 2].X; y = points[points.Count / 2 - 1].Y + points[points.Count / 2].Y; x /= 2; y /= 2; } //g.DrawString(text, textFont, brText, new PointF(x, y), sf); } else if (textStyle == ArrowTextStyle.OverLongestSegment) { // find the center point of longest segment int longest = au.getLongestSegment(); PointF center = au.getSegmentCenter(longest); // see how much space the text requires StringFormat cf = new StringFormat(); cf.Alignment = StringAlignment.Center; cf.LineAlignment = StringAlignment.Center; SizeF size = g.MeasureString(text, textFont); size.Width += ArrowUtils.getMillimeter(pChart.MeasureUnit); size.Height += ArrowUtils.getMillimeter(pChart.MeasureUnit); // fill using the background brush /* * * * System.Drawing.Brush brush = pChart.BackBrush.CreateGDIBrush(rect); * g.FillRectangle(brush, center.X - size.Width / 2, * center.Y - size.Height /2, size.Width, size.Height); * brush.Dispose(); * * // draw the text * g.DrawString(text, textFont, brText, center, cf); */ } brText.Dispose(); RectangleF rc = new RectangleF(x - textSize.Width / 2, y - textSize.Height / 2, textSize.Width, textSize.Height); return(rc); }