【问题标题】:SVG, XML & font-familySVG、XML 和字体系列
【发布时间】:2012-05-30 12:42:37
【问题描述】:

我希望获得更多关于在 SVG 中格式化文本的最佳方式的信息 w3提供了这个 http://www.w3.org/Graphics/SVG/IG/resources/svgprimer.html#text_methods

但没有指定如何构建字体堆栈,我尝试将以下 xml 代码视为 css(如一些建议的那样),但我没有运气。

font-family:Times New Roman;
-inkscape-font-specification:Times New Roman

问题。如何根据 css 为 svg 构建字体堆栈?

(我对 font-face 或 svg 字体不太感兴趣,因为我相信从我读到的内容来看,它们目前还没有得到广泛的支持。)

我尝试在 xml 中以这些模式格式化文本,但没有成功;

 font-family:Verdana,Times New Roman;
 font-family:'Verdana,"Times New Roman"';
 font-family:Verdana,"Times New Roman";
 font-family:'Verdana,Times New Roman';

编辑 我也从 w3 找到了这个 http://www.w3.org/TR/SVG/text.html#FontPropertiesUsedBySVG 它告诉我按照 http://www.w3.org/TR/2008/REC-CSS2-20080411/fonts.html#font-specification 关注 css 但是像 css (font-family:"Times New Roman",Times,serif;) 一样把它放进去是行不通的。

【问题讨论】:

  • 您尝试过哪些查看器?提供一个完整的例子?
  • 我正在使用 ,所以我想作为一个后备来清除 xml 中的字体并将字体系列应用于 。但这将是一个可怕的工作(如果它甚至可以工作的话),因为我将被限制为每个 svg 一个字体堆栈。
  • 也许越来越接近这个问题,你知道样式不会继承到 标签,对吧?如果你想要继承,你必须在文档中包含 svg inline。
  • 我的评论只是我想知道如何解决这个问题,我实际上并没有尝试使用 css 来格式化文本,这是将来可能发生的事情吗?我正在 Inkscape 的 xml 编辑器中编辑字体,我也尝试过使用 notepad++。
  • 您是否尝试过使用内联 svg?那么我的css通常没有任何问题..

标签: svg font-family


【解决方案1】:

检查 Inkscape 生成的 xml 输出。 Inkscape 生成错误的字体系列值“font-family:Sans”。我猜这应该是“Sans-Serif”,但在破折号处被截断了。此值附加到样式字符串的末尾,因此当您在文本编辑器中查看 xml 时可能看不到它。如果您编辑样式字符串以放置您自己的字体系列定义,但不删除不正确的定义(因为您还没有看到它),什么都不会改变。

 <!--this wont work. Two font-family specs (scroll to the end).-->       
    style="font-family:arial,sans-serif;font-size:40px;font-style:normal;font-family:Sans"

    <!--this wont work either. Well you'll get the default Serif font-->
    style="font-family:Sans;font-size:40px;font-style:normal;font-weight:normal;"

    <!--this will work-->
    style="font-family:arial,sans-serif;font-size:40px;font-style:normal;font-weight:normal;"

    <!--so will this-->
    style="font-family:Arial,Sans-Serif;font-size:40px;font-style:normal;font-weight:normal;"

    <!--and this-->
    style="font-family:tahoma,arial,Sans-Serif;font-size:40px;font-style:normal;font-weight:normal;"

 <!--this too-->
style="font-family:Times New Roman, Serif;font-size:40px;font-style:normal;font-weight:normal;"

例子:

<text
   style="font-family:Times New Roman, Serif;font-size:40px;font-style:normal;font-weight:normal;">
    <tspan
     x="60"
     y="60">
    The rain in spain
    </tspan>
</text>          

【讨论】:

  • 我相信我们可能需要强调,在 Steve Mc 的回答中,字体堆栈是用文本元素的 de 'style' 属性编写的名称,即使它包含空格。这与我们过去在 CSS 中所做的不同。 “Times new Roman”可能是一种不适合用于测试的字体,因为至少当 Firefox 可以找到其他任何东西时,它会默认使用这个字体。我试过“Arial Black”。
【解决方案2】:

font-family 属性与 CSS 定义的相同,因此您可以根据需要指定字体堆栈。

一个使用属性的例子:

<svg xmlns="http://www.w3.org/2000/svg">
  <text x="10" y="100" 
   font-family="'Times New Roman', Times, serif" font-size="48">
    Some text
  </text>
</svg>

或者更好的是,使用类:

<svg xmlns="http://www.w3.org/2000/svg">
  <style>
    .seriftext {
      font-family: 'Times New Roman', Times, serif;
      font-size: 48px;
    }
  </style>
  <text x="10" y="100" class="seriftext">
    Some text
  </text>
</svg>

【讨论】:

  • 嘿 Erik,谢谢,我添加了一些在 xml 中指定字体堆栈的尝试,但没有一个有效,当我“设置”它们时,它们甚至在 Inkscape 中消失了。你有正确格式的例子吗?
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-19
  • 2011-12-01
相关资源
最近更新 更多