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 ...
随机推荐
- CSS标签知识
一.内联标签和块标签的区别 内联,内嵌,行内属性标签: 1.默认同行可以继续跟同类型标签: 2.内容撑开宽度 3.不支持宽高 4.不支持上下的margin和padding 5.代码换行被解析 块属性标 ...
- ORACLE 数据库概述以及Oracel数据库的安装、卸载、使用
一:Orcale简介 1.发展史 1978年,Orcale诞生 1982年,Orcale3推出了,它是第一个能够运行在大型机和小型机上的关系型数据库 1997年,Orcale公司推出了基于java语言 ...
- 关于lua垃圾回收是否会执行__gc函数呢?
直接上代码 -- test.lua do local x = setmetatable({},{ __gc = function() print("works") end }) e ...
- 集合框架学习之Guava Collection
开源工具包: Guava : Google Collection Apache:Commons Collecton 1.1 Google Collections Guava:google的工程师利用传 ...
- MIME类型大全
获取MIME:HttpContext.Current.Request.Files[fileKey].ContentLength MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该 ...
- 6款基于SVG的HTML5应用和动画
1.HTML5 SVG 3D蝴蝶飞舞动画 逼真超酷 这次我们要分享的这款HTML5动画简直就是逆天,利用SVG制作的3D蝴蝶飞舞动画,蝴蝶飞舞动画非常逼真,蝴蝶飞舞的路线是利用SVG构造的.另外,动画 ...
- POJ 1384
求猜存钱罐中至少有多少钱.容易知道金币总的重量,接着背包. #include<cstdio> #include<iostream> using namespace std; # ...
- java.lang.ThreadLocal源码分析
ThreadLocal类提供线程本地变量,为变量在每个线程创建一个副本,每个线程可以访问自己内部的副本变量. 比如,有这样一个需求,需要为每个线程创建一个独一无二的标识,这个标识在第一次调用Threa ...
- sql存在一个表而不在另一个表中的数据
(转)A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低 ...
- 版权控制之zend guard 6.0使用教程
zend guard6.0使用教程.doc 一.准备工具 1. ZendGuard-6_0_0 下载地址:http://www.zend.com/en/products/guard/downloads ...