如何在 Batik SVG 库中使用自定义字体? [英] How I use a custom font with the Batik SVG library?

查看:90
本文介绍了如何在 Batik SVG 库中使用自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个为衣服创建模板的 Java 程序.用户输入他们想在衣服上看到的词,系统会创建一个 PDF 模板.为了创建模板,我以编程方式创建了一个 SVG 文档,然后使用 Batik 将 SVG 转码为 PDF 格式.

I'm working on a Java program which creates templates for clothes. The user enters the word they want to see on the item of clothing and the system creates a PDF template. To create the template I create an SVG document programatically then use Batik to transcode the SVG to the PDF format.

我的客户现在希望能够使用自定义字体来创建模板.我想知道是否可以在 Batik 转码器中使用像 TTF 这样的字体?如果是这样,您如何设置 SVG?

My client now wants to be able to use custom fonts to create the templates. I was wondering if it's possible to use fonts like a TTF with the Batik transcoder? If so how do you go about setting up the SVG?

推荐答案

首先你需要使用 batik 的 ttf2svg 将字体文件从 TTF 转换为 SVG,一旦你有了转换后的文件,你必须在 'defs' 部分添加引用SVG 文档.我就是这样做的:

First you need to transform the font file from TTF to SVG with batik's ttf2svg, once you have the converted file you have to add reference in the 'defs' section of your SVG document. this is how i did it:

Element defs = doc.createElementNS(svgNS, "defs");

Element fontface = doc.createElementNS(svgNS, "font-face");
fontface.setAttributeNS(null, "font-family", "DroidSansRegular");

Element fontfacesrc = doc.createElementNS(svgNS, "font-face-src");
Element fontfaceuri = doc.createElementNS(svgNS, "font-face-uri");

fontfaceuri.setAttributeNS(svgNS, "xlink:href", "fonts/DroidSans-webfont.svg#DroidSansRegular");

Element fontfaceformat = doc.createElementNS(svgNS, "font-face-format");
fontfaceformat.setAttributeNS(svgNS, "string", "svg");

fontfaceuri.appendChild(fontfaceformat);
fontfacesrc.appendChild(fontfaceuri);
fontface.appendChild(fontfacesrc);
defs.appendChild(fontface);
svgRoot.appendChild(defs);

在创建文本元素时设置字体系列就像任何其他字体

when creating the text element set the font family just like any other font

Element txtElem = doc.createElementNS(svgNS, "text");

txtElem.setAttributeNS(svgNS, "style", "font-family:DroidSansRegular;font-size:" + fontsize + ";stroke:#000000;#fill:#00ff00;");
txtElem.setTextContent("some text");
svgRoot.appendChild(txtElem);

我从这篇文章中得到了信息:http://frabru.de/c.php/article/SVGFonts-usage 希望对您有所帮助.

i got the info from this article: http://frabru.de/c.php/article/SVGFonts-usage hope it helps.

这篇关于如何在 Batik SVG 库中使用自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆