WPF解析TTF 字体
偶遇需要自己解析 TTF 字体并显示,此做。。。
using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging; namespace Helper.JilyData
{ public class TTFInfo
{
public FileInfo TTFFileInfo { get; private set; } private GlyphTypeface GlyphTypeface { get; set; } public Size Bounds { get; private set; } public string ErrorMessage { get; private set; } public TTFInfo(string file)
{
this.TTFFileInfo = new FileInfo(file);
this.GlyphTypeface = new GlyphTypeface(new Uri(file, UriKind.Absolute));
} public TTFInfo(FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch)
{
this.TTFFileInfo = null;
Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
GlyphTypeface glyphTypeface = null;
if (typeface.TryGetGlyphTypeface(out glyphTypeface))
{
this.GlyphTypeface = glyphTypeface;
} if (this.GlyphTypeface == null)
{
var missfont = Application.Current.MainWindow.FontFamily;
typeface = new Typeface(missfont, fontStyle, fontWeight, fontStretch);
if (typeface.TryGetGlyphTypeface(out glyphTypeface))
{
this.GlyphTypeface = glyphTypeface;
} this.ErrorMessage = "当前系统无此字体";
}
} public ImageSource GetStrImage(string str, double fontsize, Brush foreBrush = null)
{
if (str != null)
{
var strs = ConverterText(str);
if (str.Length > && this.GlyphTypeface != null)
{
DrawingGroup dg = new DrawingGroup();
double width = ;
double height = ;
foreach (var text in strs)
{
var glyphIndexes = new ushort[text.Length];
var advanceWidths = new double[text.Length];
for (int n = ; n < text.Length; n++)
{
var glyphIndex = this.GlyphTypeface.CharacterToGlyphMap[text[n]];
glyphIndexes[n] = glyphIndex;
advanceWidths[n] = this.GlyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
} var gr = new GlyphRun(this.GlyphTypeface, , false, 1.0, glyphIndexes, new Point(, ), advanceWidths, null, null, null, null, null, null);
var glyphRunDrawing = new GlyphRunDrawing(foreBrush ?? Brushes.White, gr);
var w = glyphRunDrawing.Bounds.Width * fontsize;
var h = glyphRunDrawing.Bounds.Height * fontsize;
ImageDrawing dring = new ImageDrawing(new DrawingImage(glyphRunDrawing), new Rect(new Point(, height), new Size(w, h)));
dg.Children.Add(dring); width += w;
height += h;
}
Bounds = new Size(width, height);
return new DrawingImage(dg);
}
} return null;
} public List<string> ConverterText(string text)
{
var t = text.Replace("&", "&");
t = t.Replace("\t", " ");
return t.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
} }
}
WPF解析TTF 字体的更多相关文章
- WPF解析Fnt字体
偶遇需要再 WPF中加载Fnt字体,此做... using System; using System.Collections.Generic; using System.Drawing; using ...
- WPF下如何使用TTF字体
之前再写代码的时候如果遇到了图标,我都喜欢再资源文件下创建JPG或者PNG来作为图片. 但是随着TTF字体图标的普及,图标类型的的图片越来越多的被放入到TTF中. 这篇也主要是写再WPF下如何使用TT ...
- 【WPF】添加自定义字体
需求:在WPF项目中使用幼圆字体. 步骤: 1.首先要有幼圆TTF字体文件.在C:\Windows\Fonts目录下找,如果系统字体库中没有,就上网下一份,如这里或这里. 2.将字体文件复制到WPF项 ...
- WPF使用矢量字体图标(阿里巴巴iconfont)
原文:WPF使用矢量字体图标(阿里巴巴iconfont) 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/lwwl12/article/details/78 ...
- Android自定义TTF字体
前言: 在Android Design中一个设计手册.在设计手册中有常用的UI图标,图标大小规范等. 其中,有一个TTF字体,以前感觉没什么用.但是我在学习时,常看到有许多开发者使用Google 提供 ...
- 【转】cocos2d-x使用第三方的TTF字体库
步骤一:找一个ttf字体库 步骤二:找到这个ttf字体库的真实名称 打开你的应用 "字体册"(MAC OS系统下),如下图操作): 找到了字体库真实名称,那么修改将其真名作为为此新 ...
- Cocos2d-x教程(28)-ttf 字体库的使用
欢迎增加 Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/37650843 通常为 ...
- 小程序使用阿里巴巴TTF字体文件以及图标
转话地址https://transfonter.org 第一步:下载需要的字体图标 进入阿里图标官网http://iconfont.cn/搜索自己想要的图标,如这里需要一个购物车的图标,流程为: 搜索 ...
- android textview使用ttf字体显示图片
最近在研究一个组件时,发现使用textview显示了一张图片,原以为android原生支持,仔细研究了下,是用ttf字体实现的,记录下 网上的介绍文章很多,这里就不啰嗦了,链接 https://www ...
随机推荐
- 使用SSIS创建同步数据库数据任务
国外相关的文章:http://blog.dxuf.com/sql-tutorial/use-ssis-to-create-the-synchronization-database-data-task. ...
- 路由器之VPN应用与配置指南
应用背景 近日,公司需要在外人员通过直接访问连接到公司内网,实现办公等一系列操作,这个时候就需要通过配置路由器VPN实现该需求了. 无线企业路由器可以帮助中小型企业搭建高性价比.稳定的企业办公网络,灵 ...
- 【学习笔记】【C语言】位运算
1. & 按位与 1> 功能 只有对应的两个二进位均为1时,结果位才为1,否则为0. 2> 举例: 比如9&5,其实就是1001&101=1,因此9&5=1 ...
- ASP.NET MVC自定义路由 - 实现IRouteConstraint限制控制器名(转载)
自定义约束前 namespace MvcApplication2 { public class RouteConfig { public static void RegisterRoutes(Rout ...
- (转)如何构建高性能,稳定SOA应用之-负载均衡-Decoupled Invocation(一)
当我们在为一个软件设计架构的时候,我们不仅仅要确保所做出来的架构要满足系统的业务需求,更加要确保做出来的架构要满足可维护性,安全,稳定性的非业务行的需求. 另外一个非常重要的非功能性需求就是性能.性能 ...
- 杭电ACM2061--Treasure the new start, freshmen!
http://acm.hdu.edu.cn/showproblem.php?pid=2061 这题很简单.注意换行. <span style="font-size:18px;" ...
- 配置PostgreSQL Streaming Replication集群
运行环境: Primary: 192.168.0.11 Standby: 192.168.0.21, 192.168.0.22 OS: CentOS 6.2 PostgreSQL: 9.1.2 版本以 ...
- 探索VS中C++多态实现原理
引言 最近把<深度探索c++对象模型>读了几遍,收获甚大.明白了很多以前知其然却不知其所以然的姿势.比如构造函数与拷贝构造函数什么时候被编译器合成,虚函数.实例函数.类函数的区别等等.在此 ...
- oracle连接和执行流程总结
参考关于oracle连接及一个事务的完整流程分析的资料,做整理如下 参考资料: http://blog.csdn.net/wyzxg/archive/2010/08/16/5815335.aspx h ...
- Windows phone(1)-ApplicationBar(应用栏)
在手机APP应用当中,我们往往会想某些操作能够进行快速访问或者能够持续显示用户信息的情况,比如像做泡泡堂游戏右边区域玩家信息 在wp7,wp8中为我们提供了ApplicationBar这样的控件来创建 ...