我具有被用于产生具有预定义的字体和文本的图像的小脚本。我想改变它使用多种字体来呈现相同的文本,例如字母A
5种字体。我定义我的字体列表如下:在Python使用正则表达式来产生列表
fonts = [ 'Georgia', 'Consolas', 'Arial'
]
,然后我用它:
for item in enumerate(fonts) :
...
我想生成所有例如列表Times New Roman家庭字体。我试图用正则表达式生成列表,但没有运气。我真的不知道该如何将它嵌入在列表中(带引号,用/
在开始和结束等)
我已经试过这样的事情:fonts = [ '/^.Times.*$/' ]
这fonts = [ '/Times.*/g' ]
,但没有成功。
Seond问题出现了,当我想用3名字的字体像Luicida Console Regular
,然后我得到这个错误:
C:\Users\xxx\Desktop\test.py:46: PangoWarning: couldn't load font "Luicid
a Console 40", falling back to "Sans 40", expect ugly output.
pangctx.show_layout (layout)
看起来像脚本是从字体的名称只得到两个单词。
编辑
def main():
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
context = cairo.Context (surface)
source = context.get_source()
font = sys.argv[1]
fonts = [ 'Georgia', 'Consolas', 'Arial', 'Lucida Console', 'Times New Roman' ]
output = sys.argv[2]
text = sys.argv[3]
background = cairo.SolidPattern (255, 255, 255)
context.rectangle (0, 0, WIDTH, HEIGHT)
context.set_source (background)
context.fill()
pangctx = pangocairo.CairoContext (context)
layout = pangctx.create_layout()
layout.set_width ((WIDTH - 2 * PADDING) * pango.SCALE)
layout.set_single_paragraph_mode (True)
layout.set_wrap (pango.WRAP_CHAR)
size = 40 * pango.SCALE
spacing = 10 * pango.SCALE
markup = ''
for index, item in enumerate(fonts):
print index, item
markup += '<span font="'+ item +'" size="' + str(size) + '" letter_spacing="' + str(spacing) + '">' + text +'</span>'
layout.set_markup (markup)
pangctx.update_layout (layout)
context.new_path()
context.move_to (PADDING, PADDING)
context.set_source (source)
context.set_source_rgb (0, 0, 0)
pangctx.show_layout (layout)
surface.write_to_png (output)
编辑 看来,这仍是攀高launchpad link
为什么你认为一个正则表达式在那里是有效的,什么是“Luicida Consol E“? – 2012-02-26 18:08:42
@ IgnacioVazquez-Abrams“Luicida Console Regular”是一种字体 - 至少在我的系统(Win7)上。 '正则表达式在那里有效'是什么意思? – Patryk 2012-02-26 18:15:18
不,它不是。是什么让你相信一个正则表达式会被你传递给它的任何东西所使用? – 2012-02-26 18:18:45