【问题标题】:How to center text along a path with svgwrite如何使用 svgwrite 使文本沿路径居中
【发布时间】:2023-01-13 15:16:38
【问题描述】:

我有一条在 svgwrite 中创建的路径,我想让我的文本沿着这条路径居中。我怎么做?

【问题讨论】:

    标签: python svg svgwrite


    【解决方案1】:

    您需要在 TextPath 构造函数上设置两个参数:

    1. text_anchor="middle" 表示文本应该以锚点为中心
    2. startOffset="50%" 使用路径的中间标记作为文本的锚点。 (如果没有这个,看起来你的文本仍然是左对齐的,而且它的前半部分已经被切掉了)

      示例代码:

      # setup canvas
      dwg = svgwrite.Drawing()
      dwg.viewbox(0,0,200,100)
      
      # Create some path. This bit really matter
      x1 = 20
      y1 = 50
      r1 = 50
      arc = math.sqrt(2*(r1**2))
      path = dwg.path(
              d=f"m {x1},{y1} " + # starting point
                f"a{r1},{r1} 0 0 1 {arc},0 " + # first arc
                f"a{r1},{r1} 0 0 0 {arc},0 ", # second arc
              stroke="#DDD",
              fill="none")
      
      # Center your text along path
      text = svgwrite.text.Text("") # The text path must go inside a text object. Parameter used here gets ignored
      text.add(svgwrite.text.TextPath(path, text="soy sol sonatora", startOffset="50%", method="align", text_anchor="middle"))
      
      # Draw path and text
      dwg.add(path)
      dwg.add(text)
      

      哪个创造

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2011-09-22
      • 2016-07-18
      • 2011-08-29
      • 1970-01-01
      相关资源
      最近更新 更多