![](/img/trans.png)
[英]How to determine text width and height when using svgwrite for python?
[英]Right-aligning text in Python svgwrite
我正在嘗試旋轉文本,該部分工作正常,但我希望它是右對齊而不是左對齊。 換句話說,我希望文本在我計算的圓的周長上結束,而不是開始。 這段代碼畫了一個圓圈,上面有 365 個刻度,每 7 天有一個較長的刻度,每個第 7 天的數字都刻在刻度標記的圓圈內。
我的代碼是這樣做的:
我想要的是這樣的:
import math
import svgwrite
radius_inner = 200
radius_width = 40
radius_outer = radius_inner + radius_width
week_line_start = 10
day_line_start = 16
line_end = 10
day_font_size=15
days = 365
dwg = svgwrite.Drawing('YearWheel.svg')
for i in range(0,days):
angle = (math.pi*2 / days) * i
if i % 7 == 0:
startr = week_line_start
else:
startr = day_line_start
startx = math.sin(angle) * (radius_inner + startr)
endx = math.sin(angle) * (radius_outer - line_end)
starty = -math.cos(angle) * (radius_inner + startr)
endy = -math.cos(angle) * (radius_outer - line_end)
print("({},{}),({},{})".format(startx,starty,endx,endy))
dwg.add(dwg.line((startx,starty), (endx,endy), stroke=svgwrite.rgb(10, 10, 16, '%')))
if i > 0 and i % 7 == 0:
rot = 'rotate({},{}, {})'.format(math.degrees(angle)-90,startx,starty)
dwg.add(dwg.text("{}".format(i), insert=(startx,starty), transform=rot, font_size='{}px'.format(day_font_size)))
dwg.save()
我嘗試將,style="text-align:end"
添加到text()
調用中(在stackoverflow的其他地方看到了),但這沒有區別。
理想情況下,我希望數字也與線條“垂直”居中。
我最初的帖子報告說樣式元素導致了錯誤。 這是由於我的代碼的一個版本在svgwrite.Drawing
構造函數中有, profile='tiny'
。
我已經修復了右對齊,並圍繞中心工作:
dwg.add( dwg.text( "{}".format(i)
, insert=(startx,starty)
, transform=rot
, style="text-anchor:end;baseline-shift:-33%"
, font_size="{}px".format(day_font_size)
) )
text-anchor
是我正在尋找的樣式元素,而不是text-align
。
大約 -33% 的baseline-shift
讓我看起來它是居中的,這對我來說已經足夠了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.