SVG中有多行弯曲文本

Dav*_*eer 5 svg google-chrome

在SVG中是否有一种方法可以在一个<text>遵循相同路径轮廓的单个元素中绘制多行文本?这是我能得到的最接近的:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1">
    <defs>
        <path id="text_0_path" d="M 100 150 A 100 100 0 1 1 300 150"/>
    </defs>
    <use xlink:href="#text_0_path" stroke="blue" fill="none"/>
    <text font-family="Arial" font-size="18px" text-anchor="middle">
        <textPath xlink:href="#text_0_path" startOffset="50%">
            <!-- 157.075 is the center of the length of an arc of radius 100 -->
            <tspan x="157.075">Here is a line</tspan>
            <tspan x="157.075" dy="20">Here is a line</tspan>
            <tspan x="157.075" dy="20">Here is a line</tspan>
        </textPath>
    </text>
</svg>
Run Code Online (Sandbox Code Playgroud)

这是输出(在Chrome中):

弯曲的文字

几乎就是我想要的.问题:

  • 我希望每行文本都以弧顶为中心,而不是在那里开始文本.text-anchor当沿着路径x指定值时,似乎忘记了属性tspan.(直文不是这种情况; text-anchor记住该属性.)
  • 每个连续的文本行都是嘎吱嘎吱的,就像沿着同心路径一样.我希望每行文本都遵循相同的轮廓,就好像路径只是y按照字体的高度在方向上进行平移.

我知道我可以创建三个单独的<path>元素并将它们与三个独立的<text>(或<textPath>)元素相关联,但是将所有文本逻辑地保存在一起使用<tspan>元素以供以后的应用程序使用会非常好.

或者这应该可行,但Chrome中存在渲染错误?(不太可能,IMO)

小智 4

我不知道您是否希望将文本呈现在同心圆上,或者您只是希望将其翻译。如果是前者,那么您可能需要在 t-span 元素上尝试字母间距属性。这将为您的角色添加跟踪,将它们推得更远。我尝试了以下操作,但由于某种原因,行之间的对齐丢失了:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1">
    <defs>
        <path id="text_0_path" d="M 100 150 A 100 100 0 1 1 300 150"/>
    </defs>
    <use xlink:href="#text_0_path" stroke="blue" fill="none"/>
    <text font-family="Arial" font-size="18px" text-anchor="middle">
        <textPath xlink:href="#text_0_path" startOffset="50%">
            <!-- 157.075 is the center of the length of an arc of radius 100 -->
            <tspan x="157.075">Here is a line</tspan>
            <tspan x="157.075" dy="20" letter-spacing="2">Here is a line</tspan>
            <tspan x="157.075" dy="20" letter-spacing="4">Here is a line</tspan>
        </textPath>
    </text>
</svg>
Run Code Online (Sandbox Code Playgroud)

但是,如果您想要后者(同心圆),这似乎适用于 Mac 上的 Safari 和 Chrome:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" version="1.1">
    <defs>
        <path id="text_0_path" d="M 100 150 A 100 100 0 1 1 300 150"/>
    </defs>
    <use xlink:href="#text_0_path" stroke="blue" fill="none"/>
    <g font-family="Arial" font-size="18px">
        <text text-anchor="middle">
            <textPath xlink:href="#text_0_path" startOffset="50%">Here is a line</textPath>
        </text>
        <text text-anchor="middle" transform="translate(0, 20)">
            <textPath xlink:href="#text_0_path" startOffset="50%">Here is a line</textPath>
        </text>
        <text text-anchor="middle" transform="translate(0, 40)">
            <textPath xlink:href="#text_0_path" startOffset="50%">Here is a line</textPath>
        </text>
    </g>
</svg>
Run Code Online (Sandbox Code Playgroud)

我知道您只拍摄 tspans,但正如您所说,它似乎重置了 startOffset 值。

HTH,凯文