package com.zd.exam.util;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;

public class FontsUtil {

    public static Font getMsyh(int size) {
        Font font;
        //获取字体流
        InputStream simsunFontFile = FontsUtil.class.getResourceAsStream("/fonts/msyh.ttc");
        try {
            //创建字体
            font = Font.createFont(Font.PLAIN, simsunFontFile).deriveFont(Font.PLAIN, size);
        } catch (IOException | FontFormatException e) {
            font = new Font("微软雅黑", Font.PLAIN, size);
        }finally {
            try {
                simsunFontFile.close();
            } catch (IOException e) {

            }
        }
        return font;
    }

    public static Font getMsyh(int size, int fontStyle) {
        Font font;
        //获取字体流
        InputStream simsunFontFile = FontsUtil.class.getResourceAsStream("/fonts/msyh.ttc");
        try {
            //创建字体
            font = Font.createFont(fontStyle, simsunFontFile).deriveFont(fontStyle, size);
        } catch (IOException | FontFormatException e) {
            font = new Font("微软雅黑", fontStyle, size);
        }finally {
            try {
                simsunFontFile.close();
            } catch (IOException e) {

            }
        }
        return font;
    }

    public static Font getSimkai(int size) {
        Font font = null;
        //获取字体流
        InputStream simsunFontFile = FontsUtil.class.getResourceAsStream("/fonts/simkai.ttf");
        try {
            //创建字体
            font = Font.createFont(Font.PLAIN, simsunFontFile).deriveFont(Font.PLAIN, size);
        } catch (IOException | FontFormatException e) {
            e.printStackTrace();
            font = new Font("楷体", Font.PLAIN, size);
        }finally {
            try {
                simsunFontFile.close();
            } catch (IOException e) {

            }
        }
        return font;
    }

}
