How to embed a True Type font( 转载 )
来源:互联网 发布:淘宝店铺首页怎么全屏 编辑:程序博客网 时间:2024/05/01 09:11
Some applications, for reasons of esthetics or a required visual style, will embed certain uncommon fonts so that they are always there when needed regardless of whether the font is actually installed on the destination system.
The secret to this is twofold. First the font needs to be placed in the resources by adding it to the solution and marking it as an embedded resource. Secondly, at runtime, the font is loaded via a stream and stored in a PrivateFontCollection object for later use.
This example uses a font which is unlikely to be installed on your system. Alpha Dance is a free True Type font that is available from the Free Fonts Collection. This font was embedded into the application by adding it to the solution and selecting the "embedded resource" build action in the properties.
Figure 1. The Alpha Dance font file embedded in the solution.
Once the file has been successfully included in the resources you need to provide a PrivateFontCollection object in which to store it and a method by which it's loaded into the collection. The best place to do this is probably the form load override or event handler. The following listing shows the process. Note how the AddMemoryFont method is used. It requires a pointer to the memory in which the font is saved as an array of bytes. In C# we can use the unsafe keyword for convienience but VB must use the capabilities of the Marshal classes unmanaged memory handling. The latter option is of course open to C# programmers who just don't like the unsafe keyword.
PrivateFontCollection pfc = new PrivateFontCollection();
private void Form1_Load(object sender, System.EventArgs e)
{
Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("embedfont.Alphd___.ttf");
byte[] fontdata = new byte[fontStream.Length];
fontStream.Read(fontdata,0,(int)fontStream.Length);
fontStream.Close();
unsafe
{
fixed(byte * pFontData = fontdata)
{
pfc.AddMemoryFont((System.IntPtr)pFontData,fontdata.Length);
}
}
}
Dim pfc As New PrivateFontCollection()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'load the resource
Dim fontStream As Stream = Me.GetType().Assembly.GetManifestResourceStream("embedfont.vb.Alphd___.ttf")
'create an unsafe memory block for the data
Dim data As System.IntPtr = Marshal.AllocCoTaskMem(fontStream.Length)
'create a buffer to read in to
Dim fontdata() As Byte
ReDim fontdata(fontStream.Length)
'fetch the font program from the resource
fontStream.Read(fontdata, 0, fontStream.Length)
'copy the bytes to the unsafe memory block
Marshal.Copy(fontdata, 0, data, fontStream.Length)
'pass the font to the font collection
pfc.AddMemoryFont(data, fontStream.Length)
'close the resource stream
fontStream.Close()
'free the unsafe memory
Marshal.FreeCoTaskMem(data)
End Sub
Fonts may have only certain styles which are available and unfortunately, selecting a font style that doesn't exist will throw an exception. To overcome this the font can be interrogated to see which styles are available and only those provided by the font can be used. The following listing demonstrates how the Alpha Dance font is used by checking the available font styles and showing all those that exist. Note that the underline and strikethrough styles are pseudo styles constructed by the font rendering engine and are not actually provided in glyph form.
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
bool bold=false;
bool regular=false;
bool italic=false;
e.Graphics.PageUnit=GraphicsUnit.Point;
SolidBrush b = new SolidBrush(Color.Black);
float y=5;
System.Drawing.Font fn;
foreach(FontFamily ff in pfc.Families)
{
if(ff.IsStyleAvailable(FontStyle.Regular))
{
regular=true;
fn=new Font(ff,18,FontStyle.Regular);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(ff.IsStyleAvailable(FontStyle.Bold))
{
bold=true;
fn=new Font(ff,18,FontStyle.Bold);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(ff.IsStyleAvailable(FontStyle.Italic))
{
italic=true;
fn=new Font(ff,18,FontStyle.Italic);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(bold && italic)
{
fn=new Font(ff,18,FontStyle.Bold | FontStyle.Italic);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
fn=new Font(ff,18,FontStyle.Underline);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
fn=new Font(ff,18,FontStyle.Strikeout);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
}
b.Dispose();
}
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim bold As Boolean = False
Dim regular As Boolean = False
Dim italic As Boolean = False
e.Graphics.PageUnit = GraphicsUnit.Point
Dim b As New SolidBrush(Color.Black)
Dim y = 5
Dim fn As System.Drawing.Font
Dim ff As FontFamily
For Each ff In pfc.Families
If ff.IsStyleAvailable(FontStyle.Regular) Then
regular = True
fn = New Font(ff, 32, FontStyle.Regular)
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic)
fn.Dispose()
y += 40
End If
If ff.IsStyleAvailable(FontStyle.Bold) Then
bold = True
fn = New Font(ff, 32, FontStyle.Bold)
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic)
fn.Dispose()
y += 40
End If
If ff.IsStyleAvailable(FontStyle.Italic) Then
italic = True
fn = New Font(ff, 32, FontStyle.Italic)
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic)
fn.Dispose()
y += 40
End If
If bold And italic Then
fn = New Font(ff, 32, FontStyle.Bold Or FontStyle.Italic)
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic)
fn.Dispose()
y += 40
End If
fn = New Font(ff, 32, FontStyle.Underline)
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic)
fn.Dispose()
y += 40
fn = New Font(ff, 32, FontStyle.Strikeout)
e.Graphics.DrawString(fn.Name, fn, b, 5, y, StringFormat.GenericTypographic)
fn.Dispose()
Next
b.Dispose()
End Sub
Figure 2 shows the application in action.
Figure 2. The embedded Alpha Dance font.
- How to embed a True Type font(转载)
- How to embed a True Type font( 转载 )
- Loading a true type font with GDI+
- How to embed a manifest in an assembly
- How to embed Excel in a .NET WinForms App
- How to embed an icon in a Qt application
- iphone开发--字体类型设置(how to set the font type)
- 转载:How to create an automation project using MFC and a type library
- 自己绘制True type font字体
- How to Type(DP)
- How a Math Genius Hacked OkCupid to Find True Love
- How To Become A Hacker(转载)
- How to convert a type object from unshared to shared.
- How to embed Lua 5.1 in C++
- C# 实现虚拟打印机 HP Color LaserJet 4500 (2) True Type Font字体显示
- [BF]True to Type一如既往
- How to get a web page content type
- How To Use LoadImage() to Read a BMP File(转载)
- zz怎样得到一个系统盘的全名
- Google排名优化专栏—PageRank是否已成昔日骄子
- 如何提高网站的Google PR值
- 一生中必看的30个故事
- 使用rman复制数据库
- How to embed a True Type font( 转载 )
- 非COM环境下的接口编程--问题,技巧,应用
- vb入门教程-第一章HELLO WORLD
- 使用一个入侵测系统+主动防火墙-->Snort+Guardian (ZT)
- 画渐变函数
- 第五次培训
- 如何遍历DataSet
- [原创]一个可以自动创建多级目录的函数
- 为JEdit设置墙纸(add wallpaper for jedit)