没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:李显亮|2020-09-21 09:47:11.320|阅读 642 次
概述:Aspose最新推出专用的API——Aspose.Font,用于处理和渲染包括TrueType,CFF,OpenType和Type1在内的流行字体类型。本文将介绍如何使用C#呈现TrueType和Type1字体的文本。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
在数字排版中,字体定义了用于字符外观的特定样式。通常,在文档和网页中使用字体来样式化文本。每种字体都在一个文件中进行了描述,该文件包含有关字符的大小,粗细,样式和编码的信息。
对于此类情况,Aspose提供了专用的API——Aspose.Font,用于处理和渲染包括TrueType,CFF,OpenType和Type1在内的流行字体类型。
在上一篇文章中,您已经了解了如何使用Aspose.Font for .NET API来以编程方式加载和保存CFF,TrueType和Type1字体。在本文中,将学习如何使用C#呈现TrueType和Type1字体的文本。
为了实现文本呈现,.NET的Aspose.Font提供了IGlyphOutlinePainter接口来绘制字形。以下步骤演示了如何在IGlyphOutlinePainter中实现这些方法。
class GlyphOutlinePainter : IGlyphOutlinePainter
{
private System.Drawing.Drawing2D.GraphicsPath _path;
private System.Drawing.PointF _currentPoint;
public GlyphOutlinePainter(System.Drawing.Drawing2D.GraphicsPath path)
{
_path = path;
}
public void MoveTo(MoveTo moveTo)
{
_path.CloseFigure();
_currentPoint.X = (float)moveTo.X;
_currentPoint.Y = (float)moveTo.Y;
}
public void LineTo(LineTo lineTo)
{
float x = (float)lineTo.X;
float y = (float)lineTo.Y;
_path.AddLine(_currentPoint.X, _currentPoint.Y, x, y);
_currentPoint.X = x;
_currentPoint.Y = y;
}
public void CurveTo(CurveTo curveTo)
{
float x3 = (float)curveTo.X3;
float y3 = (float)curveTo.Y3;
_path.AddBezier(
_currentPoint.X,
_currentPoint.Y,
(float)curveTo.X1,
(float)curveTo.Y1,
(float)curveTo.X2,
(float)curveTo.Y2,
x3,
y3);
_currentPoint.X = x3;
_currentPoint.Y = y3;
}
public void ClosePath()
{
_path.CloseFigure();
}
}
static void DrawText(string text, IFont font, double fontSize,
Brush backgroundBrush, Brush textBrush, string outFile)
{
//Get glyph identifiers for every symbol in text line
GlyphId[] gids = new GlyphId[text.Length];
for (int i = 0; i < text.Length; i++)
gids[i] = font.Encoding.DecodeToGid(text[i]);
// set common drawing settings
double dpi = 300;
double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
// prepare output bitmap
Bitmap outBitmap = new Bitmap(960, 720);
outBitmap.SetResolution((float)dpi, (float)dpi);
Graphics outGraphics = Graphics.FromImage(outBitmap);
outGraphics.FillRectangle(backgroundBrush, 0, 0, outBitmap.Width, outBitmap.Height);
outGraphics.SmoothingMode = SmoothingMode.HighQuality;
//declare coordinate variables and previous gid
GlyphId previousGid = null;
double glyphXCoordinate = 0;
double glyphYCoordinate = fontSize * resolutionCorrection;
//loop which paints every glyph in gids
foreach (GlyphId gid in gids)
{
// if the font contains the gid
if (gid != null)
{
Glyph glyph = font.GlyphAccessor.GetGlyphById(gid);
if (glyph == null)
continue;
// path that accepts drawing instructions
GraphicsPath path = new GraphicsPath();
// Create IGlyphOutlinePainter implementation
GlyphOutlinePainter outlinePainter = new GlyphOutlinePainter(path);
// Create the renderer
Aspose.Font.Renderers.IGlyphRenderer renderer = new
Aspose.Font.Renderers.GlyphOutlineRenderer(outlinePainter);
// get common glyph properties
double kerning = 0;
// get kerning value
if (previousGid != null)
{
kerning = (font.Metrics.GetKerningValue(previousGid, gid) /
glyph.SourceResolution) * fontSize * resolutionCorrection;
kerning += FontWidthToImageWith(font.Metrics.GetGlyphWidth(previousGid),
glyph.SourceResolution, fontSize);
}
// glyph positioning - increase glyph X coordinate according to kerning distance
glyphXCoordinate += kerning;
// Glyph placement matrix
TransformationMatrix glyphMatrix =
new TransformationMatrix(
new double[]
{
fontSize*resolutionCorrection,
0,
0,
// negative because of bitmap coordinate system begins from the top
- fontSize*resolutionCorrection,
glyphXCoordinate,
glyphYCoordinate
});
// render current glyph
renderer.RenderGlyph(font, gid, glyphMatrix);
// fill the path
path.FillMode = FillMode.Winding;
outGraphics.FillPath(textBrush, path);
}
//set current gid as previous to get correct kerning for next glyph
previousGid = gid;
}
//Save results
outBitmap.Save(outFile);
}
static double FontWidthToImageWith(double width, int fontSourceResulution, double fontSize, double dpi = 300)
{
double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
return (width / fontSourceResulution) * fontSize * resolutionCorrection;
}
下面的代码示例演示如何使用上述实现通过C#使用TrueType字体呈现文本。
string dataDir = RunExamples.GetDataDir_Data();
string fileName1 = dataDir + "Montserrat-Bold.ttf"; //Font file name with full path
FontDefinition fd1 = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName1)));
TtfFont ttfFont1 = Aspose.Font.Font.Open(fd1) as TtfFont;
string fileName2 = dataDir + "Lora-Bold.ttf"; //Font file name with full path
FontDefinition fd2 = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName2)));
TtfFont ttfFont2 = Aspose.Font.Font.Open(fd2) as TtfFont;
DrawText("Hello world", ttfFont1, 14, Brushes.White, Brushes.Black, dataDir + "hello1_montserrat_out.jpg");
DrawText("Hello world", ttfFont2, 14, Brushes.Yellow, Brushes.Red, dataDir + "hello2_lora_out.jpg");
下面的代码示例演示如何使用C#以Type1字体呈现文本。
string fileName = dataDir + "courier.pfb"; //Font file name with full path
FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
DrawText("Hello world", font, 14, Brushes.White, Brushes.Black, dataDir + "hello1_type1_out.jpg");
DrawText("Hello world", font, 14, Brushes.Yellow, Brushes.Red, dataDir + "hello2_type1_out.jpg");
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn




注意: Cogent DataHub 软件 v11 包含一些新功能,您的目标操作系统可能不支持这些功能。
本教程主要为大家介绍如何使用DevExpress WinForms数据网格控件进行数据排序的基础知识,欢迎下载最新版组件体验!
在使用Parasoft C/C++test执行BugDetective数据流分析时,可能会遇到用户自定义的资源API,那在这种情况下,若要判断是否存在资源问题,如资源泄露等,则需要手动配置测试配置。
大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
相关产品
灵活且易于使用的库可用于不同的字体文件,用于以渲染任何所需的字形或文本。
半岛外围网上直营相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营