当前位置:网站首页>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
边栏推荐
猜你喜欢

Houdini terrain and fluid solution (simulated debris flow)

Use of command line parameter passing library argparse

H5 local storage data sessionstorage, localstorage

Unity C single case mode learning review notes

Houdini>刚体, 刚体破碎RBD

MySQL in window10 version does not work after setting remote access permission

C # control the camera, rotate and drag the observation script (similar to scenes observation mode)

Houdini>建筑道路可变,学习过程笔记

【Unity VFX】VFX特效入门笔记-火花制作

C#控制相机,旋转,拖拽观察脚本(类似Scenes观察方式)
随机推荐
Encapsulate the debug function of unity
Idea shortcut
基于NLP的软件安全研究(二)
C SVG path parser of xamarin version
SVG中Path Data数据简化及文件夹所有文件批量导出为图片
Houdini地形与流体解算(模拟泥石流)
Houdini > variable building roads, learning process notes
Weblux file upload and download
Mongodb 启动警告信息处理
如何展示您的数字作品集:来自创意招聘人员的建议
ABAP ALV显示金额与导出金额不一致
webflux文件上传下载
NodeJS(二)同步读取文件和异步读取文件
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
RGB颜色转HEX进制与单位换算
Event system (II) multicast events
Mongodb starts warning information processing
IDEA快捷键
MySQL8.0 安装/卸载 教程【Window10版】
Houdini流体>>粒子流体导出到unity笔记