당신은 .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());
}
답변 해 주셔서 감사합니다. – Marco
예, .NET 2.0으로 제한됩니다. 다른 문제는 코드가 각 분리 문자에 대한 검사를 수행한다는 것입니다. 그러나 아랍어와 같은 일부 언어에서는 문자와 글리프간에 1 대 1의 관계가 없습니다. 일부 문자 모양은 주변 문자에 따라 다르며 일부 문자는 단일 문자 모양에 결합됩니다. 그래서 완전한 문자열을 검사하는 메서드가 필요합니다 ... – Marco
GetGlyphIndices 함수는 문자열을 매개 변수로 사용하므로 원하는 경우 전체 문자열을 전달할 수 있습니다. 나는 당신이 개인적인 특성을 검사하고 있었다는 것을 생각했기 때문에 나는 이것을 이렇게 조작했다. –