|
11 | 11 | import javax.validation.constraints.NotEmpty; |
12 | 12 | import javax.validation.constraints.NotNull; |
13 | 13 | import java.awt.*; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
14 | 16 |
|
15 | 17 | @Data |
16 | 18 | public class Text extends Drawable { |
17 | 19 |
|
18 | | - @Override |
19 | | - public void draw(Graphics2D gd, int posterWidth, int posterHeight) { |
20 | | - Font drawFont; |
| 20 | + static Map<String, Font> drawFonts = new HashMap<>(); |
21 | 21 |
|
22 | | - try { |
23 | | - drawFont = Font.createFont(Font.TRUETYPE_FONT, ResourceUtils.getFontFile(font)).deriveFont((float) fontSize); |
24 | | - } catch (Exception e) { |
25 | | - drawFont = new Font("Default", Font.PLAIN, fontSize); |
| 22 | + public static Font getDrawFont(String name, Integer fontSize) { |
| 23 | + Font tempFont = drawFonts.get(name); |
| 24 | + if (tempFont == null) { |
| 25 | + try { |
| 26 | + tempFont = Font.createFont(Font.TRUETYPE_FONT, ResourceUtils.getFontFile(name)).deriveFont(fontSize); |
| 27 | + } catch (Exception e) { |
| 28 | + tempFont = new Font("Default", Font.PLAIN, fontSize); |
| 29 | + } |
| 30 | + drawFonts.put(name, tempFont); |
| 31 | + return tempFont; |
26 | 32 | } |
27 | 33 |
|
| 34 | + return tempFont.deriveFont(fontSize); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void draw(Graphics2D gd, int posterWidth, int posterHeight) { |
| 39 | + Font drawFont = getDrawFont(font, fontSize); |
| 40 | + |
28 | 41 | // 设置字体和颜色 |
29 | 42 | gd.setFont(drawFont); |
30 | 43 | gd.setColor(ColorTools.String2Color(color)); |
|
0 commit comments