帮助将python ctypes结构转换为64位
我发现这个脚本附加到了。这正是我想要的。然而,它似乎只适用于32位系统,我真的很想在我的64位系统上使用它 我读了一些书,但我不知道应该如何修改这个脚本(大概是stuct?),使它在64位架构下工作。有人能帮忙吗 干杯帮助将python ctypes结构转换为64位,python,struct,32bit-64bit,ctypes,Python,Struct,32bit 64bit,Ctypes,我发现这个脚本附加到了。这正是我想要的。然而,它似乎只适用于32位系统,我真的很想在我的64位系统上使用它 我读了一些书,但我不知道应该如何修改这个脚本(大概是stuct?),使它在64位架构下工作。有人能帮忙吗 干杯 #!/usr/bin/python # vim:ts=8:sw=4:expandtab:encoding=utf-8 # Export named font from PDF file using fontforge and ctypes import sys from cty
#!/usr/bin/python
# vim:ts=8:sw=4:expandtab:encoding=utf-8
# Export named font from PDF file using fontforge and ctypes
import sys
from ctypes import *
STRING = c_char_p
real = c_longdouble
# We need the `map` attribute of SplineFont, so declear an incomplete struct.
# see: http://sourceforge.net/projects/wqy/files/misc/
# file: fontforge-bindctypes-0.1.tar.bz2
class splinefont(Structure):
pass
SplineFont = splinefont
splinefont._fields_ = [
('fontname', STRING),
('fullname', STRING),
('familyname', STRING),
('weight', STRING),
('copyright', STRING),
('filename', STRING),
('defbasefilename', STRING),
('version', STRING),
('italicangle', real),
('upos', real),
('uwidth', real),
('ascent', c_int),
('descent', c_int),
('uniqueid', c_int),
('glyphcnt', c_int),
('glyphmax', c_int),
('glyphs', POINTER(c_void_p)),
('changed', c_uint, 1),
('changed_since_autosave', c_uint, 1),
('changed_since_xuidchanged', c_uint, 1),
('display_antialias', c_uint, 1),
('display_bbsized', c_uint, 1),
('dotlesswarn', c_uint, 1),
('onlybitmaps', c_uint, 1),
('serifcheck', c_uint, 1),
('issans', c_uint, 1),
('isserif', c_uint, 1),
('hasvmetrics', c_uint, 1),
('loading_cid_map', c_uint, 1),
('dupnamewarn', c_uint, 1),
('encodingchanged', c_uint, 1),
('multilayer', c_uint, 1),
('strokedfont', c_uint, 1),
('new', c_uint, 1),
('compacted', c_uint, 1),
('backedup', c_uint, 2),
('use_typo_metrics', c_uint, 1),
('weight_width_slope_only', c_uint, 1),
('save_to_dir', c_uint, 1),
('head_optimized_for_cleartype', c_uint, 1),
('ticked', c_uint, 1),
('internal_temp', c_uint, 1),
('complained_about_spiros', c_uint, 1),
('use_xuid', c_uint, 1),
('use_uniqueid', c_uint, 1),
('fv', c_void_p),
('metrics', c_void_p),
('uni_interp', c_int),
('for_new_glyphs', c_void_p),
('map', c_void_p),
# ...
]
def main():
if len(sys.argv) != 3:
print "Usage: %s doc.pdf fontname" % sys.argv[0]
sys.exit(2)
pdfname = sys.argv[1]
fontname = sys.argv[2]
fontfile = fontname + '.ttf'
# ctypes functions
libc = CDLL("libc.so.6")
libc.fopen.restype = c_void_p
libc.fopen.argtype = [c_char_p, c_char_p]
lib_ff = CDLL('libfontforge.so.1')
# SplineFont *_SFReadPdfFont(FILE *pdf,char *filename,
# char *select_this_font, enum openflags openflags)
lib_ff._SFReadPdfFont.argtypes = [c_void_p, c_char_p, c_char_p, c_int]
lib_ff._SFReadPdfFont.restype = POINTER(SplineFont)
# int GenerateScript(SplineFont *sf, char *filename, char *bitmaptype,
# int fmflags, int res, char *subfontdefinition, struct sflist *sfs,
# EncMap *map, NameList *rename_to,int layer)
lib_ff.GenerateScript.argytpes = [POINTER(SplineFont), c_char_p, c_char_p,
c_int, c_int, c_char_p, c_void_p, c_void_p, c_void_p, c_int]
lib_ff.GenerateScript.restype = c_int
# need to somehow initialize libfontforge or it will segfault somewhere.
lib_ff.doinitFontForgeMain()
fobj = libc.fopen(pdfname, "rb")
if not fobj:
print "%s not found" % pdfname
sys.exit(1)
font = lib_ff._SFReadPdfFont(fobj, pdfname, fontname, 0)
ret = 0
if bool(font):
ret = lib_ff.GenerateScript(font, fontfile, None, -1, -1, None, None,
font.contents.map, None, 1)
if ret:
print 'Font export to "%s".' % fontfile
else:
print "** Error ** Failed to export font!!"
if __name__ == '__main__':
main()
问题是/usr/include/FONTFORGE/CONFIG.h中是否定义了FONTFORGE_CONFIG_USE_longdoull。如果已定义,则代码的定义是正确的。在我的amd64 linux安装中,没有定义FONTFORGE_CONFIG_USE_longdoull和FONTFORGE_CONFIG_USE_DOUBLE,因此我需要更改
real = c_float
有了这个更改,它就可以正常工作。为什么有人用Python编写这个代码?这是我在C.Sven Marnach中为数不多的几件事之一——我明白你的意思。从我自己的角度来看,我正在寻找一种从更复杂的python脚本中调用它的方法,所以这种方法,如果我能让它工作的话,对我很有吸引力。如果我能用C重写这个,我会很乐意,但我想我还是要处理同样的架构问题?我看不出64位问题的原因,无论是在上面的Python代码中,还是在用C重新编码它。但是这个脚本的C版本会很短。您可以将其作为子进程从Python调用,也可以通过ctypes调用Python@David赫弗南:是的,这实际上是我目前正在做的,但这很尴尬,而且完全不令人满意……:)非常感谢您的回答,但我无法复制您的结果!在我的安装(64位Ubuntu 10.10)中,
FONTFORGE\u-CONFIG\u-USE\u-longdoull
和FONTFORGE\u-CONFIG\u-USE\u-DOUBLE
都没有在/usr/include/FONTFORGE/CONFIG.h
中定义,但是将real
的定义更改为c\u-float
仍然会导致错误。它可能是别的东西吗?为了进一步分析,您必须提供一个特定的PDF文件和一个特定的调用。我想不出还有什么可能导致这个问题。谢谢你的回复。如果你愿意看的话,我已经在这里发布了我的测试pdf()-这只是我手边第一个多语言的测试工具。它只包含两种字体(PMingLiu和Gentium),脚本能够通过调用python ff_font_extract.py test\u fore.pdf baaaa+Gentium
和python ff_font_extract.py test\u fore.pdf caaaa+PMingLiu
在32位系统上提取它们。我可以问一下您使用的64位Linux发行版是什么吗?(附言-我非常感谢你的帮助!)两种字体都可以很好地导出。我正在使用Debian测试,libfontforge 0.0.20100501-2。我同意海报上的说法,他们说这最好用C写。谢谢Martin,我可以通过Debian测试和这个版本的libfontforge库来证实这一点。我会尽力找出差异,但你已经解决了。谢谢:)