是不是可以使用XSLT将字体导出为Base64?

huangapple go评论69阅读模式
英文:

Is it possible to export a font as base64 using xslt?

问题

Sure, here is the translation of the text you provided:

我想从 .fo 文件创建一些 HTML 输出,并编写了一些 XSL 模板来获得所需的输出。对于 HTML 元素的样式,每个元素都有一个具有匹配的 CSS 类的 class 属性。

看起来像这样:

.page_2_text_3 {
   transform: translate(0pt, 82.96pt);
   font-family: ITCFranklinGothicStd-Book;
   font-style: normal;
   font-weight: 400;
   font-variant: normal;
   font-size: 10pt;
   color: #000000;
}

问题在于字体 ITCFranklinGothicStd-Book 找不到,被其他字体替代了。

是否有办法在模板中将字体导出为 base64,以便我可以像这样在我的 CSS 中添加 @font-face

<xsl:template name="fontexport">
  <xsl:param name="font"/>
  <xsl:param name="fontname"/>
  @font-face {
   font-family: "<xsl:value-of select="$fontname"/>";
   src: url("<xsl:value-of select="$font" mode="base64"/>")<!-- 将 $font 转换为 base64 -->
  }
</xsl:template>

当调用带有字体和字体名称作为参数的 call-template 时,应该创建以下输出:

@font-face {
   font-family: "ITCFranklinGothicStd-Book";
   src: url("data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAAB...)
}

是否有可能以某种方式实现这个目标?

英文:

I want to create some html output from .fo file and I wrote myself some xsl templates to get the desired output.
For the styling of the html-elements, each element has a class attribute with the matching css-class.

That looks like the following:

.page_2_text_3 {
   transform: translate(0pt, 82.96pt);
   font-family: ITCFranklinGothicStd-Book;
   font-style: normal;
   font-weight: 400;
   font-variant: normal;
   font-size: 10pt;
   color: #000000;
}

The problem here is that the font ITCFranklinGothicStd-Book can't be found and is substituted by some other font.

Is there any way to export the font as base64 in a template, so i can add a @font-face to my css like so:

&lt;xsl:template name=&quot;fontexport&quot;&gt;
  &lt;xsl:param name=&quot;font&quot;/&gt;
  &lt;xsl:param name=&quot;fontname&quot;/&gt;
  @font-face {
   font-family: &quot;&lt;xsl:value-of select=&quot;$fontname&quot;/&gt;&quot;;
   src: url(&quot;&lt;xsl:value-of select=&quot;$font&quot; mode=&quot;base64&quot;/&gt;&quot;)&lt;!-- convert the $font into base64 --&gt;
  }
&lt;/xsl:template&gt;

this should create the following output when calling call-template with font and fontname as parameter

@font-face {
   font-family: &quot;ITCFranklinGothicStd-Book&quot;;
   src: url(&quot;data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAAB...)
}

Is that possible somehow?

答案1

得分: 2

如果您切换到支持EXPath文件模块的XSLT处理器,比如Saxon-PE,那么您可以使用file:read-binary()来读取字体文件,它会返回一个xs:base64Binary值,对这个值应用string()函数会给您Base64表示。

英文:

If you move to an XSLT processor that supports the EXPath file module, such as Saxon-PE, then you can read the font file using file:read-binary(), which returns an xs:base64Binary value, and the string() function applied to this value gives you the Base64 representation.

huangapple
  • 本文由 发表于 2023年5月6日 20:18:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定