2011-04-09 5 views
10

특정 유니 코드 문자가 있다고 가정 해 봅시다. 嗎, 시스템에 설치된 모든 글꼴을 반복하고이 문자에 대한 글리프가있는 글꼴을 나열하려면 어떻게합니까? 내가 .NET 4.0이 테스트 한특정 문자가 포함 된 글꼴을 확인하는 방법은 무엇입니까?

+0

참조 : [지원되지 않는 문자 확인/글꼴의 글리프 (http://stackoverflow.com/questions/5025740/c-check-for-unsupported-characters-glyphs-in-a-font) – Ani

+0

C#이 아니지만이 python 스크립트는 훌륭하게 작동합니다. http://unix.stackexchange.com/a/268286/26952 –

답변

12

, 당신은 글꼴 & 서체 유형은 일하러 가야 PresentationCore에 대한 참조를 추가해야합니다. Fonts.GetFontFamilies overloads도 확인하십시오.

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Windows.Markup; 
using System.Windows.Media; 

class Program 
{ 
    public static void Main(String[] args) 
    { 
     PrintFamiliesSupprotingChar('a'); 
     Console.ReadLine(); 
     PrintFamiliesSupprotingChar('â'); 
     Console.ReadLine(); 
     PrintFamiliesSupprotingChar('嗎'); 
     Console.ReadLine(); 
    } 

    private static void PrintFamiliesSupprotingChar(char characterToCheck) 
    { 
     int count = 0; 
     ICollection<FontFamily> fontFamilies = Fonts.GetFontFamilies(@"C:\Windows\Fonts\"); 
     ushort glyphIndex; 
     int unicodeValue = Convert.ToUInt16(characterToCheck); 
     GlyphTypeface glyph; 
     string familyName; 

     foreach (FontFamily family in fontFamilies) 
     { 
      var typefaces = family.GetTypefaces(); 
      foreach (Typeface typeface in typefaces) 
      { 
       typeface.TryGetGlyphTypeface(out glyph); 
       if (glyph != null && glyph.CharacterToGlyphMap.TryGetValue(unicodeValue, out glyphIndex)) 
       { 
        family.FamilyNames.TryGetValue(XmlLanguage.GetLanguage("en-us"), out familyName); 
        Console.WriteLine(familyName + " Supports "); 
        count++; 
        break; 
       } 
      } 
     } 
     Console.WriteLine(); 
     Console.WriteLine("Total {0} fonts support {1}", count, characterToCheck); 
    } 
} 
+0

많은 감사합니다. 잘 작동하는 것 같습니다! 물론, 프로덕션 코드에서는 그런 글꼴 폴더를 하드 코딩하지 않을 것입니다. 하나는'char' (또는'string') 대신'int'를 가져 와야하고 그렇지 않으면'char.ConvertToUtf32'를 사용하십시오. 그렇지 않으면 BMP로 제한됩니다. 다시 한 번 감사드립니다! – Timwi

+3

맞아요, 그것은 양질의 코드가 아니며, 더 많은 ** howto **입니다. –

+0

GDI +에서는'new System.Drawing.InstalledFontCollection()'을 만들고'.Families' 속성을 반복하여 (즉, URI 나 폴더 위치가 필요하지 않은 것처럼) 설치된 폰트 목록을 쉽게 쿼리 할 수 ​​있습니다.). WPF PresentationCore 관련 작업을 수행하는 비슷한 방법이 있습니까? – BrainSlugs83

관련 문제