package com.bjtdba.poster.utils;

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

public class FontUtil {

    /**
     * 根据字体类型获取字体
     *
     * @param size
     * @return
     */
    public static Font getFont(float size) throws IOException {
        // 字体路径 TODO
        //String path = ResourceUtils.getURL("classpath:").getPath() + "font/simsun.ttc";
        //System.out.println("--------------------------------------------"+path);

        File directory = new File("classes");
        String reportPath = directory.getCanonicalPath();
        String resource =reportPath + "/font/SourceHanSansCN-Bold.otf";
        System.out.println("-------------------字体路径-------------------------"+resource);
        File file = new File(resource);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            Font sPfBoldFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
            sPfBoldFont = sPfBoldFont.deriveFont(size);
            sPfBoldFont.deriveFont(Font.BOLD);
            return sPfBoldFont;
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
