java - Graphics2d drawString 不适用于彩色字体

标签 java fonts graphics2d emoji emojione

我正在尝试在 Swing 对象(jLabel 等)中显示表情符号,因此为了做到这一点,我使用 this library和下面的类,以便表情符号仅显示

jLabel.setIcon(new EmojiIcon(":poop:"));

它适用于所有支持表情符号的字体,除了彩色字体(即 EmojiOneNoto Color Emoji )

What's the point of a monochromatic emoji?

public class EmojiIcon implements Icon {
    private Font font;
    private final static int DEFAULT_SIZE = 32;
    private int              width        = DEFAULT_SIZE;
    private int              height       = DEFAULT_SIZE;

    private String emoji;

    public EmojiIcon (String iconString){
        this.emoji = EmojiParser.parseToUnicode(iconString);
        setFont("emojione-android");
        recalculateIconWidth(emoji);
    }

    public void setFont(String strFont){
        try {
            font = Font.createFont(Font.TRUETYPE_FONT, new File("fonts/"+strFont+".ttf")).deriveFont(48f);
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void recalculateIconWidth( String iconString ){
        FontRenderContext frc = new FontRenderContext( null, true, true );
        Rectangle2D bounds = font.getStringBounds(iconString, frc );
        width = (int) bounds.getWidth();
        height = (int) bounds.getHeight();
    }

    @Override
    public void paintIcon( Component c, Graphics g, int x, int y ){
        Graphics2D g2d = (Graphics2D) g;

        g2d.setFont( font );
        g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
        g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );

        g2d.drawString( emoji, 0, height );
    }
}

我在网上查找字体具有颜色是否合法,所以我发现 here它不是标准化的(至少对于 .ttf 字体)。 有解决方法吗?我真的很想使用EmojiOne font

最佳答案

这部分用于加载字体:

            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, Thread.currentThread().getContextClassLoader().getResourceAsStream("path/milibus-rg.ttf")));

用于绘制文本:

    public static BufferedImage textToImage(String text, Font font, int bottomPadding, Color color, BufferedImage background) {
    int width = background.getGraphics().getFontMetrics(font).stringWidth(text);
    int height = background.getGraphics().getFontMetrics(font).getHeight();
    BufferedImage bufferedImage = new BufferedImage(width, height, TYPE_4BYTE_ABGR);
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.setBackground(new Color(0f, 0f, 0f, .0f)); //transparent
    graphics.setColor(color);
    graphics.setFont(font);
    graphics.drawString(text, 0, height - bottomPadding);
    graphics.dispose();
    return bufferedImage;
}

关于java - Graphics2d drawString 不适用于彩色字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43796835/

相关文章:

java - 从 JSON Java Android 检索数据

Java 服务器不从外部机器获取包

java - android如何在 Canvas 中设置自定义字体?

iphone - 旧金山字体未在 Apple Watch 模拟器中显示

java - 从十六进制/二进制序列中获取每一位

java - 使用 javapoet 构建类时返回正确类型的对象

java - libGDX 中的 TrueType 字体

java - Repaint() 不在 Java 中调用 paint()

java - 如何设置 AffineTransform 旋转而不是剪切?

java - 由于没有重新粉刷,灰色启动画面