• 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
  • 相关阅读:
    一、NHibernate 使用配置与实现简单查询
    NHibernate学习步骤
    sql2008转成2005的解决办法
    二、使用NHibernate对数据库实现增、删、改、查操作
    FusionCharts 点击事件 新页面
    [分享]NHibernate视频教程
    FusionCharts Free使用文档教程第五章FusionCharts Free使用dataXML加载数据
    职业规划
    网页生成pdf文件
    FusionCharts Free使用文档教程第四章FusionCharts Free使用JavaScript加载图形
  • 原文地址:https://www.cnblogs.com/jiailiuyan/p/4838478.html
Copyright © 2020-2023  润新知