• 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
  • 相关阅读:
    Sql Server系列:分区表操作
    [bug]WCF 内存入口检查失败 Memory gates checking failed
    由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面。
    如何设置IIS程序池的回收时间,才能最大程度的减少对用户的影响?
    IIS 之 在IIS7、IIS7.5中应用程序池最优配置方案
    使用nginx搭建高可用,高并发的wcf集群
    NPOI读写Excel
    用 IIS 实现请求转发
    模型验证组件 FluentValidation
    C#中的 正则表达式
  • 原文地址:https://www.cnblogs.com/jiailiuyan/p/4838478.html
Copyright © 2020-2023  润新知