zoukankan      html  css  js  c++  java
  • 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 > 0 && this.GlyphTypeface != null)
                    {
                        DrawingGroup dg = new DrawingGroup();
                        double width = 1;
                        double height = 1;
                        foreach (var text in strs)
                        {
                            var glyphIndexes = new ushort[text.Length];
                            var advanceWidths = new double[text.Length];
                            for (int n = 0; 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, 0, false, 1.0, glyphIndexes, new Point(0, 0), 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(0, 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("	", "   ");
                return t.Split(new string[] { "
    " }, StringSplitOptions.None).ToList();
            }
    
        }
    }
    View Code
  • 相关阅读:
    ios8消息快捷处理——暂无输入框
    animateWithDuration 动画的速度选择
    对网页进行修改js代码
    如何对网页进行长截图
    centos快速配置yum源
    No module named 'Crypto.PublicKey' 完美解决办法
    Virtualenv 环境配置
    Python逻辑运算符的本质
    Django 使用Contenttype组件创建多关联数据库表
    Django Rest Framework url注册器组件 | 响应器组件 | 分页器组件
  • 原文地址:https://www.cnblogs.com/jiailiuyan/p/4838478.html
Copyright © 2011-2022 走看看