偶遇需要自己解析 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. 深入浅出ExtJS 第六章 布局

       6.1 布局的用途 6.1 布局的用途 //决定把什么东西放到什么位置; var vieport = new Ext.Viewport({ layout:'border', //使用Border ...

  2. MFC中改变控件的大小和位置

    用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...

  3. tslib 移植问题与解决方法

    问题一.执行脚本.提示出错,错误有"cann't exec aclocal" ,错误提示最多的是关于aclocal的问题,查资料显示这个文件是automake必备一个文件,好吧,那 ...

  4. 分享10款激发灵感的最新HTML5/CSS3应用

    1.HTML5/CSS3实现iOS Path菜单 菜单动画很酷 Path菜单相信大家都不陌生吧,它在iOS中非常流行,今天我们要分享的菜单就是利用HTML5和CSS3技术来模拟实现这款iOS Path ...

  5. Map排序(按key/按value)

    package com.abc.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collecti ...

  6. BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19226   Accepted: 8775 Description One ...

  7. ListView Web 服务器控件概述(MSDN)

    1: "折叠"图像"展开"图像"复制"图像"复制悬停"图像 全部折叠全部展开 代码:全部 代码:多个 代码:Visual ...

  8. argularJS学习笔记-增删改

    <!doctype html> <html lang="en" ng-app> <head> <meta charset="UT ...

  9. CentOS 6.X安装LAMP最高版本环境

    #------------CentOS .X安装LAMP最高版本环境------------------ #! /bin/sh   #安装Apache yum install httpd -y #.关 ...

  10. 【Qt】QSettings介绍【转】

    简介 QSettings类提供了持久的跨平台应用程序设置. 用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表,OS X和iOS的属性列表文件中. ...