2011-02-17 4 views
3

에뮬레이트 된 장치 디스플레이에 번역 된 텍스트를 표시하는 번역 소프트웨어 추가 기능 (C#, .NET 2.0)을 작업 중입니다. 모든 번역 된 텍스트를 지정된 글꼴 (Windows TTF)로 표시 할 수 있는지 확인해야합니다. 그러나 지원되지 않는 글리프 글꼴 검사 방법을 찾지 못했습니다. 누구나 아이디어가 있습니까?C# : 글꼴에서 지원되지 않는 문자/글리프 확인

감사합니다.

답변

5

당신은 .NET 2.0으로 제한되어 있습니까? .NET 3.0 이상에서는 글꼴 파일을로드 할 수있는 GlyphTypeface 클래스가 있으며 원하는대로 할 수 있다고 생각하는 CharacterToGlyphMap 속성을 노출합니다.

.NET 2.0에서는 PInvoke에 의존해야한다고 생각합니다. 다음과 같이 시도해보십시오.

using System.Drawing; 
using System.Runtime.InteropServices; 

[DllImport("gdi32.dll", EntryPoint = "GetGlyphIndicesW")] 
private static extern uint GetGlyphIndices([In] IntPtr hdc, [In] [MarshalAs(UnmanagedType.LPTStr)] string lpsz, int c, [Out] ushort[] pgi, uint fl); 

[DllImport("gdi32.dll")] 
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); 

private const uint GGI_MARK_NONEXISTING_GLYPHS = 0x01; 

// Create a dummy Graphics object to establish a device context 
private Graphics _graphics = Graphics.FromImage(new Bitmap(1, 1)); 

public bool DoesGlyphExist(char c, Font font) 
{ 
    // Get a device context from the dummy Graphics 
    IntPtr hdc = _graphics.GetHdc(); 
    ushort[] glyphIndices; 

    try { 
    IntPtr hfont = font.ToHfont(); 

    // Load the font into the device context 
    SelectObject(hdc, hfont); 

    string testString = new string(c, 1); 
    glyphIndices = new ushort[testString.Length]; 

    GetGlyphIndices(hdc, testString, testString.Length, glyphIndices, GGI_MARK_NONEXISTING_GLYPHS); 

    } finally { 

    // Clean up our mess 
    _graphics.ReleaseHdc(hdc); 
    } 

    // 0xffff is the value returned for a missing glyph 
    return (glyphIndices[0] != 0xffff); 
} 

private void Test() 
{ 
    Font f = new Font("Courier New", 10); 

    // Glyph for A is found -- returns true 
    System.Diagnostics.Debug.WriteLine(DoesGlyphExist('A', f).ToString()); 

    // Glyph for ಠ is not found -- returns false 
    System.Diagnostics.Debug.WriteLine(DoesGlyphExist((char) 0xca0, f).ToString()); 
} 
+0

답변 해 주셔서 감사합니다. – Marco

+0

예, .NET 2.0으로 제한됩니다. 다른 문제는 코드가 각 분리 문자에 대한 검사를 수행한다는 것입니다. 그러나 아랍어와 같은 일부 언어에서는 문자와 글리프간에 1 대 1의 관계가 없습니다. 일부 문자 모양은 주변 문자에 따라 다르며 일부 문자는 단일 문자 모양에 결합됩니다. 그래서 완전한 문자열을 검사하는 메서드가 필요합니다 ... – Marco

+0

GetGlyphIndices 함수는 문자열을 매개 변수로 사용하므로 원하는 경우 전체 문자열을 전달할 수 있습니다. 나는 당신이 개인적인 특성을 검사하고 있었다는 것을 생각했기 때문에 나는 이것을 이렇게 조작했다. –

관련 문제