2016-12-05 12 views
1

Windows 10(UWP)ストアアプリケーションで、サポートされている文字を特定のフォントで列挙または一覧表示する方法があるかどうかを確認しようとしています。私は 'inspect'する必要があるフォントは、アプリケーションパッケージにローカルに格納されています)。UWP/Cで特定のフォントでサポートされている文字/グリフを列挙する#

Win32アプリケーションではsimilar answersがありますが、UWPアプリケーションではこれまで見たことはありません。

ご協力いただきましてありがとうございます。

答えて

1

新しい回答 - フォントはIDWriteFontFace::GetGlyphIndices methodを使用してのDirectWriteを経由して、特定のUnicode文字/コードへのグリフをマップするかどうかの確認カスタムフォントファイル

あなたがチェックすることができます。 DirectWrite用のC#ラッパーは、SharpDxライブラリの一部として、具体的にはSharpDX.Direct2D1 package on nugetです。

私はSharpDx Directwrite example on githubから例を挙げました。私はUWPアプリケーションがファイルシステムの大部分にアクセスすることが許可されていないので、ファイルセレクタダイアログを使用してファイルを開きました。あなたのアプリでフォントを置くこともできます。私は、テスト用のフリーフォント「Aileron」と「Grundschrift」をダウンロードしました。私はちょうどあなたがそれほど難しくないことを示すためにそれを本当に速く振ったが、コードは私が確信しているベストプラクティスに従わない。まず、SharpDx Directwriteカスタムフォントの例(ResourceFontFileStream.cs,ResourceFontLoader.cs、およびResourceFontFileEnumerator.cs)から次の3つのクラスを追加します。プロジェクトの名前空間に名前空間を変更します。あなたのテストのメインページで

public ResourceFontLoader(Factory factory, List<Stream> fontfiles) 
{ 
    _factory = factory; 
    var AnyFontsLoaded = false; 
    foreach (var filename in fontfiles) 
    { 
     try 
     { 
      using (filename) 
      { 
       var fontBytes = Utilities.ReadStream(filename); 
       var stream = new DataStream(fontBytes.Length, true, true); 
       stream.Write(fontBytes, 0, fontBytes.Length); 
       stream.Position = 0; 
       _fontStreams.Add(new ResourceFontFileStream(stream)); 
       AnyFontsLoaded = true; 
      } 
     } 
     catch (System.Exception) 
     { 
      // Handle all file exceptions how you see fit 
      throw; 
     } 
    } 
    if (AnyFontsLoaded) 
    { 
     // Build a Key storage that stores the index of the font 
     _keyStream = new DataStream(sizeof(int) * _fontStreams.Count, true, true); 
     for (int i = 0; i < _fontStreams.Count; i++) 
      _keyStream.Write((int)i); 
     _keyStream.Position = 0; 

     // Register the 
     _factory.RegisterFontFileLoader(this); 
     _factory.RegisterFontCollectionLoader(this); 
    } 
} 

ResourceFontLoader.cs変更これにコンストラクタで

public sealed partial class MainPage : Page 
{ 
    public ResourceFontLoader CurrentResourceFontLoader { get; set; } 
    public FontCollection CurrentFontCollection { get; set; } 
    public SharpDX.DirectWrite.Factory FactoryDWrite { get; private set; } 
    public List<Stream> customFontStreams { get; set; } 
    public List<string> FontFamilyNames { get; set; } 
    public MainPage() 
    { 
     customFontStreams = new List<Stream>(); 
     this.InitializeComponent(); 
    } 

    async Task LoadCustomFonts() 
    { 
     await Task.Run(() => 
     { 
      // Font Families 
      FontFamilyNames = new List<string> { "Aileron", "Grundschrift" }; 
      // Character codes to check for: 
      int[] codes = { 0x41, 0x6f, 0x7c, 0xc2aa, 0xD7A3 }; 
      FactoryDWrite = new SharpDX.DirectWrite.Factory(); 
      CurrentResourceFontLoader = new ResourceFontLoader(FactoryDWrite, customFontStreams); 
      CurrentFontCollection = new FontCollection(FactoryDWrite, CurrentResourceFontLoader, CurrentResourceFontLoader.Key); 

      foreach (var fontfamilyname in FontFamilyNames) 
      { 
       int familyIndex; 
       CurrentFontCollection.FindFamilyName(fontfamilyname, out familyIndex); 

       using (var fontFamily = CurrentFontCollection.GetFontFamily(familyIndex)) 
       { 
        var font = fontFamily.GetFont(0); 

        using (var fontface = new FontFace(font)) 
        { 
         var results = fontface.GetGlyphIndices(codes); 
         for (int i = 0; i < codes.Length - 1; i++) 
         { 
          if (results[i] > 0) 
          { 
           Debug.WriteLine("Contains the unicode character " + codes[i]); 

          } 
          else 
          { 
           Debug.WriteLine("Does not contain the unicode character " + codes[i]); 
          } 
         } 
        } 
       } 

      } 
     }); 

    } 
    private async void button_Click(object sender, RoutedEventArgs e) 
    { 
     FileOpenPicker openPicker = new FileOpenPicker(); 
     openPicker.ViewMode = PickerViewMode.List; 
     openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; 
     openPicker.FileTypeFilter.Add(".otf"); 
     openPicker.FileTypeFilter.Add(".ttf"); 
     IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync(); 
     if (files.Count > 0) 
     { 


      // Application now has read/write access to the picked file(s) 
      foreach (StorageFile file in files) 
      { 
       customFontStreams.Add(await file.OpenStreamForReadAsync()); 
      } 
      await LoadCustomFonts(); 
     } 
    } 
} 

オリジナル回答 - チェックシステムフォント:

SharpDX.Direct2D1パッケージを使用した例フォントに特定の文字が含まれているかどうかを確認する:

var fontName = "Segoe UI"; 

using (var factory = new Factory(FactoryType.Shared)) 
{ 
    using (var fontCollection = factory.GetSystemFontCollection(true)) 
    { 
     int familyIndex; 
     fontCollection.FindFamilyName(fontName, out familyIndex); 

     using (var fontFamily = fontCollection.GetFontFamily(familyIndex)) 
     { 
      var font = fontFamily.GetFont(0); 

      using (var fontface = new FontFace(font)) 
      { 
       int[] codes = { 0x41, 0x6f, 0x7c, 0xc2aa, 0xD7A3 }; 

       var results = fontface.GetGlyphIndices(codes); 
       for (int i = 0; i < codes.Length - 1; i++) 
       { 
        if (results[i] > 0) 
        { 
         Debug.WriteLine("Contains the unicode character " + codes[i]); 
        } 
        else 
        { 
         Debug.WriteLine("Does not contain the unicode character " + codes[i]); 
        } 
       } 
      } 
     } 

    } 
} 
+0

これは有望そうです - 私の唯一の問題は、私が作業しているフォントファイルはシステムフォントではなく、ローカルに保存されていることです。 /Fonts/AnonymousPro-regular.ttf#Anonymous Proのようなフォントファイルに対してこれが動作するかどうかは任意です。 – erickfiveten

+1

SharpDx githubにはカスタムフォントを使用した例があります。 [見てください](https://github.com/sharpdx/SharpDX-Samples/tree/master/Desktop/DirectWrite/CustomFont) –

+0

コードを精査した後、私はまだ動作するカスタムフォントを取得する運がなかったSharpDxで。私は乞食がチョーサーになることはできないと知っていますが、より簡単な解決策を望んでいます... – erickfiveten

関連する問題