java graphics2d 字体,如何在Java2D / Graphics2D中指定备用字体

I'm using g.drawString(str, x, y) to draw a String with a Graphics2D object g. The current font of g does not cover all the characters of str (I have e.g. Chinese chars in there). On Mac OS X, a fallback font seems to be automatically used, but not on Windows, where black square outlines appear instead of the wanted characters.

Why is the behavior different depending on the platform?

How do I specify a fallback font (or several fallback fonts) in case of missing characters?

(For instance, one of the nice fonts there.)

Update/More Info

So, the original font that doesn't support all characters is not one of the JVM's logical fonts, but is a bundled font that comes with my app and was obtained with Font.createFont(). So, adding fonts to the JRE's lib/fonts/fallback folder doesn't work here.

解决方案

We could attribute string to switch font on "bad" symbols and use Graphics2D.drawString(AttributedCharacterIterator iterator, int x, int y) to render result. Advantage: this will work with any font. Drawback: without some sort of caching of intermediate objects this will work slower and dirtier.

So, i suggest using AttributedString with main font attribute on whole string:

AttributedString astr = new AttributedString(text);

astr.addAttribute(TextAttribute.FONT, mainFont, 0, textLength);

and with fallback font on specific parts:

astr.addAttribute(TextAttribute.FONT, fallbackFont, fallbackBegin, fallbackEnd);

The rendering itself:

g2d.drawString(astr.getIterator(), 20, 30);

The result (Physical "Segoe Print" as main font, logical "Serif" as fallback):

70386a63f571a44562a7bf6f8f89330e.png

Complete supposed-to-be-SSCCE code:

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.font.TextAttribute;

import java.text.AttributedString;

import javax.swing.JComponent;

import javax.swing.JFrame;

public class FontTest extends JFrame {

public FontTest() {

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

getContentPane().setLayout(new BorderLayout());

getContentPane().add(new TestStringComponent());

pack();

}

public static void main(String[] args) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new FontTest().setVisible(true);

}

});

}

}

class TestStringComponent extends JComponent {

protected void paintComponent(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

g.setColor(getBackground());

g.fillRect(0, 0, getWidth(), getHeight());

g.setColor(getForeground());

Font mainFont = new Font("Segoe Print", Font.PLAIN, 25);

Font fallbackFont = new Font("Serif", Font.PLAIN, 25);

String s = "Test 漢鼎繁古印 Test 漢鼎繁古印 Test";

g2d.drawString(createFallbackString(s, mainFont, fallbackFont).getIterator(), 20, 30);

}

public Dimension getPreferredSize() {

return new Dimension(500, 40);

}

private AttributedString createFallbackString(String text, Font mainFont, Font fallbackFont) {

AttributedString result = new AttributedString(text);

int textLength = text.length();

result.addAttribute(TextAttribute.FONT, mainFont, 0, textLength);

boolean fallback = false;

int fallbackBegin = 0;

for (int i = 0; i < text.length(); i++) {

boolean curFallback = !mainFont.canDisplay(text.charAt(i));

if (curFallback != fallback) {

fallback = curFallback;

if (fallback) {

fallbackBegin = i;

} else {

result.addAttribute(TextAttribute.FONT, fallbackFont, fallbackBegin, i);

}

}

}

return result;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值