简体   繁体   English

Python svgwrite 中的右对齐文本

[英]Right-aligning text in Python svgwrite

I'm trying to rotate text, and that part is working fine, but I want it to be right-justified and not left.我正在尝试旋转文本,该部分工作正常,但我希望它是右对齐而不是左对齐。 In other words I want the text to end on the perimeter of the circle that I have calculated, not begin.换句话说,我希望文本在我计算的圆的周长上结束,而不是开始。 This code draws a circle with 365 ticks on it, with a longer tick each 7 days, and each 7th day number inscribed inside the circle of tick marks.这段代码画了一个圆圈,上面有 365 个刻度,每 7 天有一个较长的刻度,每个第 7 天的数字都刻在刻度标记的圆圈内。

My code does this:我的代码是这样做的:

代码在行的顶部产生数字

What I want is this:我想要的是这样的:

我想要行旁边的数字,右对齐

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()

I tried adding ,style="text-align:end" to the text() call (saw that mentioned elsewhere on stackoverflow) but that makes no difference.我尝试将,style="text-align:end"添加到text()调用中(在stackoverflow的其他地方看到了),但这没有区别。

Ideally I would like the numbers centered "vertically" with the lines as well.理想情况下,我希望数字也与线条“垂直”居中。

My initial post reported that the style element was causing an error.我最初的帖子报告说样式元素导致了错误。 This was due to one version of my code having , profile='tiny' in the svgwrite.Drawing constructor.这是由于我的代码的一个版本在svgwrite.Drawing构造函数中有, profile='tiny'

I have fixed the right-align, and worked around the centring:我已经修复了右对齐,并围绕中心工作:

    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 was the style element I was looking for, not text-align . text-anchor是我正在寻找的样式元素,而不是text-align

baseline-shift of around -33% gives me the appearance of it being centred, and that's good enough for me.大约 -33% 的baseline-shift让我看起来它是居中的,这对我来说已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM