PdfiumViewer组件扩展(Pdfium.Net.Free)--创建字符子集

项目地址:

Pdfium.Net:https://github.com/1000374/Pdfium.Net.Free

PdfiumViewer:https://github.com/1000374/PdfiumViewer

Pdfium.Net.Free 支持

  • .NETFramework 4.0

  • .NETFramework 4.5

  • .NETStandard 2.0

  • .Net8.0

可以和PdfiumViewer.Free共同使用预览、编辑pdf,也可以直接引用Pdfium.Net.Free 操作pdf,Pdfium.Net.Free封装了现有Pdfium的函数,实现了部分操作pdf的功能,部分功能等待后续~~

Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--加载字体 草稿

接上篇,怎么创建字符子集呢?获取字符集内的字形符号需要引用wpf下PresentationCore.dll,根据比对传入字符和字符集中的字形获取对应的字形字节,从而生成新的字符集,

如需要生成ttf文件,则直接byte[]保存成ttf即可

  /// <summary>
  /// create SubCharacterSet
  /// </summary>
  /// <param name="fontPath">font path</param>
  /// <param name="sourceText"></param>
  /// <returns></returns>
  public static byte[] CreateSubSet(this string fontPath, string sourceText)
  {
      if (!File.Exists(fontPath))
          throw new ArgumentException($"{fontPath} not find");
      var glyphTypeface = new GlyphTypeface(new Uri(fontPath, UriKind.RelativeOrAbsolute));
      var Index = new List<ushort>();
      var sourceTextBytes = Encoding.Unicode.GetBytes(sourceText);
      var sourceTextChars = Encoding.Unicode.GetChars(sourceTextBytes);
      for (var charPos = 0; charPos <= (sourceTextChars.Length - 1); charPos++)
      {
          var sourceTextCharVal = (int)sourceTextChars[charPos];
          var glyphIndex = glyphTypeface.CharacterToGlyphMap[sourceTextCharVal];
          if (!Index.Contains(glyphIndex))
              Index.Add(glyphIndex);
      }
      return glyphTypeface.ComputeSubset(Index);
  }

  

 

posted @ 2024-01-29 18:02  小树禾小央  阅读(40)  评论(0编辑  收藏  举报