偶遇需要自己解析 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的核心机制. HTML文档样式的来源 1.浏览器默认样式,每 ...

  2. CSS之transition(动画)

    Transform字面上就是变形,改变的意思.在CSS中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动translate以及矩阵变形matrix. 下面我们一 ...

  3. JavaScript之菱形打印

    很高兴来到博客园!迈入这座知识的殿堂,实是幸运.这是我的第一篇博客,开启丰富有趣的学习之旅,同时,我希望和大家一起学习一起进步,Let‘s go! <!DOCTYPE html PUBLIC & ...

  4. 【Linux C中文函数手册】之 目录操作函数

    目录操作函数 1)closedir 关闭目录 相关函数: opendir表头文件: #include<sys/types.h> #include<dirent.h>定义函数: ...

  5. DCL,DDL,DML,DQL

    DCL(Data Control Language)是数据库控制语言. 是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句. 在默认状态下,只有sysadmi ...

  6. c#中sqlhelper类的编写(二)

    上一篇文章讲了简易版的SqlHelper类的编写,我们在这里就上一篇文章末尾提出的问题写出解决方案. sql语句注入攻击已经是众所周知的了.我们如何在C#中保护自己的数据库不被这样的方式攻击呢? 不用 ...

  7. Tomcat提示Null component

    Tomcat提示“严重: Null component Catalina:type=JspMonitor,name=jsp,WebModule=//localhost/,J2EEApplication ...

  8. QT 多线程程序设计【转】

    QT通过三种形式提供了对线程的支持.它们分别是,一.平台无关的线程类,二.线程安全的事件投递,三.跨线程的信号-槽连接.这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势.多线程编 ...

  9. Base64加密

    实际开发中可能需要使用到可解密的加密方式,例如客户端记住用户的密码,客户端不能记住明文密码,那就需要对明文密码进行加密,然后在表单提交之后先对密码进行解密,在进行MD5加密和数据库中的密码进行比较实现 ...

  10. 【风马一族_Android】让app上传到Android市场的网站介绍

    豌豆荚  开发者中心 http://open.wandoujia.com/account/info China app http://www.chinaapp.org