import ctypes
from ctypes import wintypes
# Load the necessary Windows DLL
gdi32 = ctypes.WinDLL("gdi32")
user32 = ctypes.WinDLL("user32")
# Define necessary constants
GGO_METRICS = 0
GGO_BITMAP = 1
GGO_NATIVE = 2
GGO_BEZIER = 3
# Define necessary structures
class MAT2(ctypes.Structure):
_fields_ = [
("eM11", ctypes.c_long),
("eM12", ctypes.c_long),
("eM21", ctypes.c_long),
("eM22", ctypes.c_long)
]
# Initialize MAT2 structure for identity transformation
identity_mat2 = MAT2(0x10000, 0, 0, 0x10000)
# Define necessary functions from gdi32
GetGlyphIndicesW = gdi32.GetGlyphIndicesW
GetGlyphIndicesW.argtypes = [wintypes.HDC, wintypes.LPCWSTR, wintypes.INT, ctypes.POINTER(wintypes.WORD),
wintypes.DWORD]
GetGlyphIndicesW.restype = wintypes.DWORD
# Create a simple window to get a device context
hwnd = user32.CreateWindowExW(0, "STATIC", "dummy window", 0, 0, 0, 0, 0, None, None, None, None)
hdc = user32.GetDC(hwnd)
# Select the font into the device context
font = gdi32.CreateFontW(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Microsoft JhengHei Bold")
gdi32.SelectObject(hdc, font)
def utf16_to_glyph_index(hdc, text):
buffer = (wintypes.WORD * len(text))()
result = GetGlyphIndicesW(hdc, text, len(text), buffer, 0)
if result == 0xFFFFFFFF:
raise ctypes.WinError()
return list(buffer)
# Test with a Unicode character (e.g., "A")
glyph_indices = utf16_to_glyph_index(hdc, "打")
print(f"Glyph index for '打': {glyph_indices[0]}")
for s in glyph_indices:
print('bbbb' + str(s))
# Release resources
gdi32.DeleteObject(font)
user32.ReleaseDC(hwnd, hdc)
user32.DestroyWindow(hwnd)
glyph index转字符
最新推荐文章于 2024-08-18 12:13:21 发布