当前位置:网站首页>Automatically fit single line text into the target rectangle
Automatically fit single line text into the target rectangle
2022-04-23 07:54:00 【Dake mountain man】
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace JohnsonJu.CodeSystem.Helper
{
/// <summary>
/// Automatically fit single line text into the target rectangle
/// </summary>
public class FittingSingleLineWordsInRectF
{
string _words;
Font _fontWords;
RectangleF _destRectF = RectangleF.Empty;
/// <summary>
/// Gets or sets the text to automatically fit
/// </summary>
public string Words
{
get { return this._words; }
set { this._words = value; }
}
/// <summary>
/// Gets or sets the font of the text
/// </summary>
public System.Drawing.Font FontWords
{
get { return this._fontWords; }
set { this._fontWords = value; }
}
/// <summary>
/// Gets or sets the destination rectangle
/// </summary>
public System.Drawing.RectangleF DestRectF
{
get { return this._destRectF; }
set { this._destRectF = value; }
}
/// <summary>
/// Constructors
/// </summary>
public FittingSingleLineWordsInRectF()
{
}
/// <summary>
/// Automatically fit single line text to the constructor in the target rectangle
/// </summary>
/// <param name="words"> One line text </param>
/// <param name="font"> The font of the text </param>
/// <param name="destRectF"> Target rectangle </param>
public FittingSingleLineWordsInRectF(string words, Font font, RectangleF destRectF)
{
this._words = words;
this._fontWords = font;
this._destRectF = destRectF;
}
private GraphicsPath RetrieveWordsPath()
{
return this.RetrieveWordsPath(this._words, this._fontWords);
}
/// <summary>
/// Get text path
/// </summary>
/// <param name="words"> written words </param>
/// <param name="font"> typeface </param>
/// <returns> Graphic path </returns>
private GraphicsPath RetrieveWordsPath(string words, Font font)
{
Rectangle rect = new Rectangle(0, 0, 9999, font.Height);
GraphicsPath gp = new GraphicsPath();
gp.AddString(words, font.FontFamily, (int)font.Style, font.Size, rect, null);
return gp;
}
/// <summary>
/// Get the return graph path
/// </summary>
/// <returns></returns>
public GraphicsPath RetrievePath()
{
return RetrievePath(this._destRectF, this.RetrieveWordsPath());
}
/// <summary>
/// Automatically adapt the drawing path to the target rectangular area
/// </summary>
/// <param name="destRectF"> Target rectangular area </param>
/// <param name="gp"> The original graphics path </param>
/// <returns> Automatically adapted graphics path </returns>
public GraphicsPath RetrievePath(RectangleF destRectF, GraphicsPath gp)
{
if (destRectF == RectangleF.Empty) return null;
PointF[] destPoints = new PointF[]{
new PointF(destRectF.Left, destRectF.Top),
new PointF(destRectF.Right, destRectF.Top),
new PointF(destRectF.Left, destRectF.Bottom),
new PointF(destRectF.Right, destRectF.Bottom)
};
RectangleF srcRect = gp.GetBounds();
gp.Warp(destPoints, srcRect);
GraphicsPath gpNew = (GraphicsPath)gp.Clone();
return gpNew;
}
public RectangleF MeasureTextRectangle(Graphics g, string measureString, Font font)
{
SizeF sizeF = g.MeasureString(measureString, font);
RectangleF rectF = new RectangleF(new PointF(0, 0), sizeF);
return rectF;
}
/// <summary>
/// Measure the rectangular area occupied by single line text
/// </summary>
/// <param name="g"></param>
/// <param name="measureString"></param>
/// <param name="stringFont"></param>
/// <returns></returns>
public RectangleF MeasureCharacterRangesRegions(Graphics g, string measureString, Font stringFont)
{
CharacterRange[] characterRanges ={
new CharacterRange(0, measureString.Length)
};
float width = 99999F;
float height = stringFont.Height;
RectangleF layoutRect = new RectangleF(0, 0, width, height);
StringFormat stringFormat = new StringFormat();
//stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
stringFormat.SetMeasurableCharacterRanges(characterRanges);
// Draw text to Sketchpad
// g.DrawString(measureString, stringFont, Brushes.Black, 0, 0, stringFormat);
// measurement
Region[] stringRegions = new Region[1];
stringRegions = g.MeasureCharacterRanges(measureString, stringFont, layoutRect, stringFormat);
// Draw the rectangle for the first measurement
RectangleF measureRect = stringRegions[0].GetBounds(g);
//g.DrawRectangle( new Pen(Color.Red, 1), Rectangle.Round(measureRect1));
return measureRect;
}
}
}
Calling method :
// rect: Your rectangle ( Area ) text: Your text font: Text font g: Your drawing board (Graphics)
FittingSingleLineWordsInRectF fsw = new FittingSingleLineWordsInRectF();
fsw.DestRectF = new RectangleF(rect.X, rect.Y, rect, rect);
fsw.Words = text;
fsw.FontWords = font;
GraphicsPath gpText =fsw.RetrievePath();
g.FillPath(brushForColorBar, gpText);
版权声明
本文为[Dake mountain man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230625378488.html
边栏推荐
猜你喜欢
第四章 无形资产
Apache Hudi 如何加速传统的批处理模式?
使用flask时代码无报错自动结束,无法保持连接,访问不了url。
ABAP ALV显示金额与导出金额不一致
Houdini > fluid, rigid body export, learning process notes
Dictionary & lt; T1,T2&gt; Sorting problem
【NLP笔记】CRF原理初探
对复杂字典Dictionary&lt;T1,T2&gt;排序问题
基于NLP的软件安全研究(一)
Scrapy modifies the time in the statistics at the end of the crawler as the current system time
随机推荐
Houdini > fluid, rigid body export, learning process notes
Scrapy modifies the time in the statistics at the end of the crawler as the current system time
事件系统(二)多播事件
STO With Billing 跨公司库存转储退货
Encapsulate the debug function of unity
SAP自建表log功能开启
Houdini terrain and fluid solution (simulated debris flow)
Houdini>刚体, 刚体破碎RBD
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
第四章 无形资产
Unity ugui determines the solution of clicking on the UI and 3D objects
【NLP笔记】CRF原理初探
Idea shortcut
How to present your digital portfolio: suggestions from creative recruiters
SAP Query增强开发介绍
Protobuf use
KCD_EXCEL_OLE_TO_INT_CONVERT报错SY-subrc = 2
NodeJS(六) 子进程操作
Electronic builder package error: proxyconnect TCP: Dial TCP: 0: connectex
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods