Skip to content

Commit 5976123

Browse files
committed
添加字体缓存
1 parent ed88322 commit 5976123

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

  • src/main/java/com/qbhy/poster/drawable

src/main/java/com/qbhy/poster/drawable/Text.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,33 @@
1111
import javax.validation.constraints.NotEmpty;
1212
import javax.validation.constraints.NotNull;
1313
import java.awt.*;
14+
import java.util.HashMap;
15+
import java.util.Map;
1416

1517
@Data
1618
public class Text extends Drawable {
1719

18-
@Override
19-
public void draw(Graphics2D gd, int posterWidth, int posterHeight) {
20-
Font drawFont;
20+
static Map<String, Font> drawFonts = new HashMap<>();
2121

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;
2632
}
2733

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+
2841
// 设置字体和颜色
2942
gd.setFont(drawFont);
3043
gd.setColor(ColorTools.String2Color(color));

0 commit comments

Comments
 (0)