Bladeren bron

feat:
添加字体库

18339543638 2 jaren geleden
bovenliggende
commit
bbca4c77ed

+ 25 - 6
tr-framework/src/main/java/cn/tr/core/utils/AvatarUtil.java

@@ -2,6 +2,7 @@ package cn.tr.core.utils;
 
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.StrUtil;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.util.Base64Utils;
 
 import javax.imageio.ImageIO;
@@ -22,9 +23,27 @@ import java.util.regex.Pattern;
  * @Author : LF
  * @Date: 2023年09月04日
  */
-
+@Slf4j
 public class AvatarUtil {
     private volatile static Map<String, String> avatarMap=new WeakHashMap<>();
+    private static Font singleWordFont;
+
+    static {
+        try {
+            //小米字体MiSans-Medium
+            singleWordFont = Font.createFont(Font.TRUETYPE_FONT, AvatarUtil.class.getClassLoader().getResourceAsStream("avatarFont.ttf")).deriveFont(Font.PLAIN, 50);
+        } catch (FontFormatException e) {
+            log.error("字体加载失败",e);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    ;
+
+    static {
+
+    }
     /**
      * 圆角大小
      */
@@ -107,14 +126,14 @@ public class AvatarUtil {
         if(nameWritten.length() ==1) {
             // 中文
             if(isChinese(nameWritten)) {
-                font = new Font("华文中宋", Font.PLAIN, 50);
-                g2.setFont(font);
-                g2.drawString(nameWritten, 25, 65);
+//                font = new Font("华文中宋", Font.PLAIN, 50);
+                g2.setFont(singleWordFont);
+                g2.drawString(nameWritten, 25, 67);
             }
             // 英文
             else {
-                font = new Font("华文中宋", Font.PLAIN, 55);
-                g2.setFont(font);
+//                font = new Font("华文中宋", Font.PLAIN, 55);
+                g2.setFont(singleWordFont);
                 g2.drawString(nameWritten.toUpperCase(), 33, 67);
             }
         }

BIN
tr-framework/src/main/resources/avatarFont.ttf