10

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.

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?

4

2 回答 2

8

首先,您需要使用 batik 的 ttf2svg 将字体文件从 TTF 转换为 SVG,一旦您拥有转换后的文件,您必须在 SVG 文档的“defs”部分添加参考。我就是这样做的:

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);

创建文本元素时,像设置任何其他字体一样设置字体系列

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希望它有所帮助。

于 2012-08-28T21:43:28.163 回答
2

只需添加以下元素作为子元素<svg/><style type="text/css">然后,类似于 HTML...

于 2013-10-06T20:06:21.197 回答