Graphics2d drawString无法使用彩色字体

时间:2017-05-05 04:59:00

标签: java fonts graphics2d emoji emojione

我正在尝试在swing对象(jLabel等)中显示表情符号,所以为了这样做,我使用this library和下面的类,以便emojis只显示

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

适用于所有具有表情符号支持的字体,除了那些有彩色的字体(例如EmojiOneNoto Color 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

1 个答案:

答案 0 :(得分:0)

此部分用于加载字体:

Route::group(['middleware' => ['auth:api']], function() {
       Route::get('/get-user','ApiController@getUserInfo');
});

这用于绘制文字:

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