我使用開羅(pycairo專門)繪製圖形,我需要知道如何在圈內繪製文本而不重疊,通過將其保留在圈子的邊界內。我有這個簡單的代碼片段,吸引了一個字母「a」內圓:如何使用開羅將文字保留在圓圈內?
'''
Created on May 8, 2010
@author: mrios
'''
import cairo, math
WIDTH, HEIGHT = 1000, 1000
#surface = cairo.PDFSurface ("/Users/mrios/Desktop/exampleplaces.pdf", WIDTH, HEIGHT)
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context (surface)
ctx.scale (WIDTH/1.0, HEIGHT/1.0) # Normalizing the canvas
ctx.rectangle(0, 0, 1, 1) # Rectangle(x0, y0, x1, y1)
ctx.set_source_rgb(255,255,255)
ctx.fill()
ctx.arc(0.5, 0.5, .4, 0, 2*math.pi)
ctx.set_source_rgb(0,0,0)
ctx.set_line_width(0.03)
ctx.stroke()
ctx.arc(0.5, 0.5, .4, 0, 2*math.pi)
ctx.set_source_rgb(0,0,0)
ctx.set_line_width(0.01)
ctx.set_source_rgb(255,0,255)
ctx.fill()
ctx.set_source_rgb(0,0,0)
ctx.select_font_face("Georgia",
cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
ctx.set_font_size(1.0)
x_bearing, y_bearing, width, height = ctx.text_extents("a")[:4]
print ctx.text_extents("a")[:4]
ctx.move_to(0.5 - width/2 - x_bearing, 0.5 - height/2 - y_bearing)
ctx.show_text("a")
surface.write_to_png ("/Users/mrios/Desktop/node.png") # Output to PNG
的問題是,我的唱片公司有字符變量量(20的限制),我需要設置的大小字體動態。無論圓圈的大小還是標籤的大小,它都必須放在圓圈內。另外,每個標籤都有一行文字,沒有空格,沒有換行符。
有什麼建議嗎?
P.S.調用set_source_rgb()時應使用浮點數(0-1.0)。請參閱http://cairographics.org/documentation/pycairo/2/reference/context.html#class-context – dkamins 2011-11-04 22:30:37