如何在iText中使用Arial Unicode的大胆版本?
问题描述:
我想在iText中使用标准的粗体Arial字体,但是如果字符不存在为粗体,我想默认为Arial Unicode。有没有在iText中结合字体的方法?我已经搜索了Arial Unicode的大胆的ttf文件,但我一直无法找到一个。如何在iText中使用Arial Unicode的大胆版本?
答
字体通常绘制为“填充形状”。您可以通过更改文本渲染模式来添加一些宽度的笔划,从而实现“穷人粗体”。
它看起来像iText的已经实现了这个非常有(伴随着“穷人的斜体”,在PdfChunk.java。它采用的1/30字体大小笔划宽度。
所以,如果你只是要求Arial Unicode的大胆版本,你应该已经得到“穷人的粗体”了。
至于组合字体,我不知道系统是这样做的,不,你可以在例如:
void addTextToPara(Paragraph paragraph, String string, Font font, Font otherFont) {
BaseFont mainfont = font.getBaseFont();
StringBuffer curChunk = new StringBuffer();
StringBuffer otherChunk = new StringBuffer();
for (int curCharIdx = 0; curCharIdx< string.length(); ++curCharIdx) {
char curChar = string.charAt(curCharIdx);
byte charBytes[] = mainFont.convertToBytes(curChar);
if (charBytes == null || charBytes.length == 0) {
// can't represent that character in the main font
if (curChunk.length() > 0) {
paragraph.add(new Chunk(curChunk.toString(), font);
curChunk.setLength(0);
}
otherChunk.append(curChar);
} else {
if (otherChunk.length() > 0) {
paragraph.add(new Chunk(otherChunk.toString(), otherFont);
otherChunk.setLength(0);
}
curChunk.append(curChar);
// can represent the character with the main font
}
}
if (curChunk.length() > 0) {
paragraph.add(new Chunk(curChunk.toString(), font);
} else if (otherChunk.length() > 0) {
paragraph.add(new Chunk(otherChunk.toString(), otherFont);
}
}
BaseFont arialBoldBaseFont = BaseFont.createFont(arialBoldPath, BaseFont.WINANSI, false); // not embedded
BaseFont arialUnicodeBaseFont = BaseFont.createFont(arialUniPath, BaseFont.IDENTITY_H, true); // IDENITY_H forces an embedded subset, ignores "embedded" param.
Font mainFont = new Font(arialBoldBaseFont, size);
Font backupFont = new Font(arialUnicodeBaseFont, size, Font.BOLD);
...
addTextToPara(paragraph, string, mainFont, backupFont);
注意事项上面的代码不会尝试查看是否可以在otherFont
中绘制给定的字符。可以重写addTextToPara()
以获得一组字体,但我几乎没有那么无聊。 ;)
通过上面的代码,你可以用你喜欢的任何编码创建BaseFonts,我只是比98%的情况更喜欢“身份H”。 mainFont
可以从WinAnsiEncoding
字体构建,并且backupFont
可以是其他东西。哎呀,你甚至可以做这样的事情:
BaseFont mainFont = BaseFont.createFont(arialBoldPath, BaseFont.WINANSI, false);
BaseFont backupFont = BaseFont.createFont(arialBoldPath, BaseFont.IDENTITY_H, true);
使用相同的系统级字体两种,只有嵌入落在外面“规范”的字符。这仍然会在PDF中产生两个单独的字体资源,但其中一个不是嵌入的(并且大部分时间都是使用的),另一个是希望的罕见字符的嵌入子集。
向短语添加这样的内容很有意义。嗯... – 2011-03-30 18:14:28