java斜体,Java没有合成斜体字体

Main question:

Is there a setting in OpenJDK that will synthesize italic font faces like the Oracle JDK does.

Background:

Using OpenJDK, when drawing text on a Graphics2D object, text will not appear with an oblique style unless there is an italic font face registered with the desired font family. Oracle's JDK does synthesize italic font faces. Swing components also are synthesized.

Notes:

Bold is synthesized, bold italic synthesizes just the bold weight.

I am aware that for best results, italic font faces should be registered. This is perhaps not a perfect solution for applications that are already taking advantage of synthesized font faces.

There is a lot out there about JavaFX and fonts, this is not JavaFX, just AWT.

Here is a simple class that illustrates the issue.

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.GraphicsConfiguration;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.UnsupportedLookAndFeelException;

public class SimpleFontTest {

private static Font FONT = new Font("Impact", Font.ITALIC, 18);

private static String TEXT = "The Quick Brown Fox";

public static void main(String args[]) throws UnsupportedLookAndFeelException {

if (args.length > 0 && args[0] != null) {

String fontName = args[0];

FONT = new Font(fontName, Font.ITALIC, 18);

}

JFrame f = new JFrame("Simple Font Test: " + System.getProperty("java.vendor"));

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("JLabel: " + TEXT);

label.setFont(FONT);

GraphicsConfiguration gc = f.getGraphicsConfiguration();

BufferedImage image = gc.createCompatibleImage(400, 50);

Graphics2D g = image.createGraphics();

g.setFont(FONT);

g.setColor(Color.BLACK);

g.setBackground(Color.WHITE);

g.clearRect(0, 0, image.getWidth(), image.getHeight());

g.drawString("BufferedImage: " + TEXT, 10, 15);

g.dispose();

JLabel picLabel = new JLabel(new ImageIcon(image));

f.add(label, BorderLayout.PAGE_START);

f.add(picLabel, BorderLayout.CENTER);

f.setSize(400, 200);

f.setVisible(true);

}

}

The output will depend on the JDK used to build and run the class. Here is a comparison image:

ASKEO.png

Thanks Everyone.

解决方案

Because I don't like seeing questions without answers: it looks like this is just a result of the change from T2K font renderer to FreeType.

Some workarounds:

Make an italic font face (if you are legally allowed to by license)

Apply an AffineTransform to the Font object (skew of -20 works well).

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值