glyph index转字符

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)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值