Add CreateBoundedText

This commit is contained in:
2025-06-29 21:14:22 -04:00
parent 9fc8e613f1
commit 837bf1a0fc

View File

@@ -507,14 +507,14 @@ namespace MarketData.CNNProcessing
canvas.DrawLine(txSrcPoint, txDstPoint, paint); canvas.DrawLine(txSrcPoint, txDstPoint, paint);
} }
/// <summary> /// <summary>
/// Draw text on the bitmap /// Draw text on the bitmap
/// </summary> /// </summary>
/// <param name="color"></param> /// <param name="color"></param>
/// <param name="strokeWidth"></param> /// <param name="strokeWidth"></param>
/// <param name="srcPoint"></param> /// <param name="srcPoint"></param>
/// <param name="dstPoint"></param> /// <param name="dstPoint"></param>
public void DrawText(String text, SKPoint srcPoint, SKColor color, SKTextAlign align, SKFont font,SKPaintStyle paintStyle=SKPaintStyle.Fill,float strokeWidth = 1) public void DrawText(String text, SKPoint srcPoint, SKColor color, SKTextAlign align, SKFont font,SKPaintStyle paintStyle=SKPaintStyle.Fill,float strokeWidth = 1)
{ {
Validate(); Validate();
SKPoint txSrcPoint = pointMapping.MapPoint(srcPoint); SKPoint txSrcPoint = pointMapping.MapPoint(srcPoint);
@@ -524,36 +524,52 @@ namespace MarketData.CNNProcessing
paint.StrokeWidth = strokeWidth; // Set the desired stroke width paint.StrokeWidth = strokeWidth; // Set the desired stroke width
paint.Color = color; paint.Color = color;
canvas.DrawText(text,srcPoint, align, font, paint); canvas.DrawText(text,srcPoint, align, font, paint);
}
/// <summary>
/// Create a rectangle with the specified text
/// </summary>
/// <param name="text"></param>
/// <param name="textColor"></param>
/// <param name="fillColor"></param>
/// <param name="align"></param>
/// <param name="font"></param>
/// <param name="paintStyle"></param>
/// <param name="strokeWidth"></param>
public void CreateBoundedText(String text, SKColor textColor, SKColor fillColor, SKTextAlign align, SKFont font, SKPaintStyle paintStyle = SKPaintStyle.Fill, float strokeWidth = 1)
{
SKPoint srcPoint = new SKPoint(2, (int)font.Size + 4);
int width = GetTextLength(text, font) + 4;
int height = (int)font.Size * 2;
CreateImage(width, height);
Fill(fillColor);
DrawLine(SKColors.Black, 1, new SKPoint(0, 0), new SKPoint(width - 1, 0)); // bottom left to right
DrawLine(SKColors.Black, 1, new SKPoint(width - 1, 0), new SKPoint(width - 1, height - 1)); // up lefthand side
DrawLine(SKColors.Black, 1, new SKPoint(0, height - 1), new SKPoint(width - 1, height - 1)); // top left to right
DrawLine(SKColors.Black, 1, new SKPoint(0, height - 1), new SKPoint(0, 0)); // left hand side top to bottom
DrawText(text, srcPoint, textColor, align, font);
} }
public void DrawBoundedText(String text, SKPoint srcPoint, SKColor color, SKTextAlign align, SKFont font,SKPaintStyle paintStyle=SKPaintStyle.Fill,float strokeWidth = 1) /// <summary>
/// Gets the length of the text
/// </summary>
/// <param name="text"></param>
/// <param name="font"></param>
/// <param name="paintStyle"></param>
/// <param name="strokeWidth"></param>
/// <returns></returns>
public int GetTextLength(String text, SKFont font, SKPaintStyle paintStyle = SKPaintStyle.Fill, float strokeWidth = 1)
{ {
// Validate();
// SKPoint txSrcPoint = pointMapping.MapPoint(srcPoint);
using SKPaint paint = new SKPaint(); using SKPaint paint = new SKPaint();
paint.Style = paintStyle; paint.Style = paintStyle;
paint.StrokeWidth = strokeWidth; // Set the desired stroke width paint.StrokeWidth = strokeWidth; // Set the desired stroke width
paint.Color = color; paint.Color = SKColors.Transparent;
SKRect rect = new SKRect(0, 0, 0, 0); SKRect rect = new SKRect(0, 0, 0, 0);
float textSize = font.MeasureText(text,out rect,paint); float textLength = font.MeasureText(text, out rect, paint);
CreateImage((int)rect.Width+1,(int)rect.Height+1); return (int)textLength;
SKPoint txSrcPoint = pointMapping.MapPoint(srcPoint); }
using SKCanvas canvas = new SKCanvas(bitmap);
canvas.DrawText(text,srcPoint, align, font, paint);
}
// float textSize = fonts[0].MeasureText(displayText,out rect,SKPaint);
// imageHelper.CreateImage(width, height);
// imageHelper.Fill(SKColors.White);
// imageHelper.DrawLine(SKColors.Black, 1, new SKPoint(0, 0), new SKPoint(width - 1, 0)); // bottom left to right
// imageHelper.DrawLine(SKColors.Black, 1, new SKPoint(width - 1, 0), new SKPoint(width - 1, height - 1)); // up lefthand side
// imageHelper.DrawLine(SKColors.Black, 1, new SKPoint(0, height - 1), new SKPoint(width - 1, height - 1)); // top left to right
// imageHelper.DrawLine(SKColors.Black, 1, new SKPoint(0, height - 1), new SKPoint(0, 0)); // left hand side top to bottom
// imageHelper.DrawText(displayText, new SKPoint(2, 16), SKColors.White, align, fonts[index]);
/// <summary> /// <summary>
/// Draws the path along the line segments /// Draws the path along the line segments
/// </summary> /// </summary>
@@ -569,11 +585,11 @@ namespace MarketData.CNNProcessing
paint.Style = SKPaintStyle.Stroke; paint.Style = SKPaintStyle.Stroke;
paint.StrokeWidth = strokeWidth; // Set the desired stroke width paint.StrokeWidth = strokeWidth; // Set the desired stroke width
paint.Color = SKColors.Black; paint.Color = SKColors.Black;
foreach(LineSegment lineSegment in lineSegments) foreach (LineSegment lineSegment in lineSegments)
{ {
SKPoint txSrcPoint=pointMapping.MapPoint(lineSegment.P1); SKPoint txSrcPoint = pointMapping.MapPoint(lineSegment.P1);
SKPoint txDstPoint=pointMapping.MapPoint(lineSegment.P2); SKPoint txDstPoint = pointMapping.MapPoint(lineSegment.P2);
canvas.DrawLine(txSrcPoint, txDstPoint, paint); canvas.DrawLine(txSrcPoint, txDstPoint, paint);
} }
} }