偶遇需要自己解析 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("&amp;", "&");
t = t.Replace("\t", " ");
return t.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
} }
}

WPF解析TTF 字体的更多相关文章

  1. WPF解析Fnt字体

    偶遇需要再 WPF中加载Fnt字体,此做... using System; using System.Collections.Generic; using System.Drawing; using ...

  2. WPF下如何使用TTF字体

    之前再写代码的时候如果遇到了图标,我都喜欢再资源文件下创建JPG或者PNG来作为图片. 但是随着TTF字体图标的普及,图标类型的的图片越来越多的被放入到TTF中. 这篇也主要是写再WPF下如何使用TT ...

  3. 【WPF】添加自定义字体

    需求:在WPF项目中使用幼圆字体. 步骤: 1.首先要有幼圆TTF字体文件.在C:\Windows\Fonts目录下找,如果系统字体库中没有,就上网下一份,如这里或这里. 2.将字体文件复制到WPF项 ...

  4. WPF使用矢量字体图标(阿里巴巴iconfont)

    原文:WPF使用矢量字体图标(阿里巴巴iconfont) 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/lwwl12/article/details/78 ...

  5. Android自定义TTF字体

    前言: 在Android Design中一个设计手册.在设计手册中有常用的UI图标,图标大小规范等. 其中,有一个TTF字体,以前感觉没什么用.但是我在学习时,常看到有许多开发者使用Google 提供 ...

  6. 【转】cocos2d-x使用第三方的TTF字体库

    步骤一:找一个ttf字体库 步骤二:找到这个ttf字体库的真实名称 打开你的应用 "字体册"(MAC OS系统下),如下图操作): 找到了字体库真实名称,那么修改将其真名作为为此新 ...

  7. Cocos2d-x教程(28)-ttf 字体库的使用

    欢迎增加 Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/37650843 通常为 ...

  8. 小程序使用阿里巴巴TTF字体文件以及图标

    转话地址https://transfonter.org 第一步:下载需要的字体图标 进入阿里图标官网http://iconfont.cn/搜索自己想要的图标,如这里需要一个购物车的图标,流程为: 搜索 ...

  9. android textview使用ttf字体显示图片

    最近在研究一个组件时,发现使用textview显示了一张图片,原以为android原生支持,仔细研究了下,是用ttf字体实现的,记录下 网上的介绍文章很多,这里就不啰嗦了,链接 https://www ...

随机推荐

  1. CSS中的文本属性

    本文总结一下CSS中关于文字的相关属性,最后给出实例. CSS基础文字属性 文字的基础属性主要包括:字体.颜色和文本.除去颜色color的属性外,字体和文本的相关属性可以权威参考: CSS 文本属性( ...

  2. 通过Scrapy抓取QQ空间

    毕业设计题目就是用Scrapy抓取QQ空间的数据,最近毕业设计弄完了,来总结以下: 首先是模拟登录的问题: 由于Tencent对模拟登录比较讨厌,各个防备,而本人能力有限,所以做的最简单的,手动登录后 ...

  3. Part 89 to 91 Talking about pass the parameters in thread

    Part 89   ParameterizedThreadStart delegate Use ParameterizedThreadStart delegate to pass data to th ...

  4. 集合框架学习之Guava Collection

    开源工具包: Guava : Google Collection Apache:Commons Collecton 1.1 Google Collections Guava:google的工程师利用传 ...

  5. S(tuple)类及可选(Optional)类型型

    元组将多个值组合为单个值.元组内的值可以是任意 类型,各元素不必是相同的类型.元组在作为函数返 回值时尤其有用. 1.定义方法1 let http404Error= (404,"Not Fo ...

  6. 【学习笔记】【C语言】数组

    1. 什么是数组 数组,从字面上看,就是一组数据的意思,没错,数组就是用来存储一组数据的 2. 数组的特点 只能存放一种类型的数据,比如int类型的数组.float类型的数组 里面存放的数据称为“元素 ...

  7. EasyUI form ajax submit到MVC后,在IE下提示下载内容的解决办法

    问题描述: 项目环境为,.Net Mvc5+EF6……前端框架使用的是EasyUI v1.4.4. 在视图页面中,使用form的submit方法提交表单数据时,如果是使用IE的话,请求成功后IE会提示 ...

  8. UILabel常用属性

    [super viewDidLoad]; // 实例化UILabel并指定其边框 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0 ...

  9. ADO.NET笔记——读取二进制大对象(BLOB)

    相关知识: 在SQL Server中,一般情况下,每行数据的总长度不能超过8K字节.因此,下列数据类型的长度,也不能超过8K字节:binary,char(),nchar(),varchar(),nva ...

  10. gcc常用选项

    gcc选项:    -c         只编译,不链接成为可执行文件,编译器只是由输入的.c等源代码文件生成.o为后缀的目标文件,通常用于编译不包含主程序的子程序文件.    -std=     指 ...