在我的系统上,我需要注册两个外部字体.TTF文件:
HamletOrNot.ttf (74 KB)
MorrisRoman-Black.ttf (67 KB)
在创建Font()对象之前,我使用以下命令进行记录:
/* Set full path of font */
String path = filesPath + fileName;
/* Read file */
Resource resource = applicationContext.getResource( path );
File fontFile = resource.getFile();
/* Load font */
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, fontFile ));
然后我创建了对象(我创建了多个Font()因为格式不同):
Font font = new Font( "HamletOrNot" , Font.TRUETYPE_FONT , 10 );
Font font = new Font( "HamletOrNot" , Font.TRUETYPE_FONT , 12 );
Font font = new Font( "MorrisRoman-Black" , Font.TRUETYPE_FONT , 20 );
Font font = new Font( "MorrisRoman-Black" , Font.TRUETYPE_FONT , 22 );
好的,系统正常工作。问题是运行后,.TTF文件会改变大小(为83 KB和80 KB),因此:
HamletOrNot.ttf (83 KB)
MorrisRoman-Black.ttf (80 KB)
然后,当我第二次运行系统时,会发生以下错误:
...
Caused by: java.awt.FontFormatException: bad table, tag=1280594760
at sun.font.TrueTypeFont.init(TrueTypeFont.java:513)
at sun.font.TrueTypeFont.<init>(TrueTypeFont.java:162)
at sun.font.FontManager.createFont2D(FontManager.java:2474)
at java.awt.Font.<init>(Font.java:570)
at java.awt.Font.createFont(Font.java:980)
at br.com.linu.vectortown.base.screen.font.GameFontFactory.postConstruct(GameFontFactory.java:43)
at br.com.linu.vectortown.base.screen.font.GameFontContainer.postConstruct(GameFontContainer.java:27)
at br.com.linu.vectortown.client.looping.util.AutomaticInjector.postConstruct(AutomaticInjector.java:47)
... 99 more
如果我用原始文件(74 KB和67 KB)替换更改大小(83 KB和80 KB)的.TTF文件,程序将正常工作。
我究竟做错了什么?我以为我必须用applicationContext.getResource()关闭打开的文件,但不知道怎么做。我不知道如何解决它。
注意:我得到了Spring的applicationContext。原始TTF文件(具有正确的大小)位于项目的资源文件夹中,而TTF错误文件位于目标资源文件夹中(使用Maven完成构建)。
帮我...
编辑:
我怀疑maven正在破坏文件。为什么?我正在使用Eclipse和Maven插件。每当我进行部署或按F5刷新项目时,目标文件夹中的.TTF文件都会损坏。
有谁见过maven损坏的资源文件?
谢谢
找到了!在执行部署代码时,maven正在破坏.TTF文件。要修复,您需要在pom.xml(maven-resources-plugin)中添加不会通过nonFilteredFileExtension命令过滤的扩展。
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
谢谢! xD,......很幸运!
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/xyz.properties</exclude>
<exclude>**/ehcache-myApp.xml</exclude>
<exclude>**/log4j-myApp.xml</exclude>
<exclude>**/*.ttf</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.ttf</include>
</includes>
</resource>
</resources>
在复制资源时打破字体,但字体文件大小被更改,因此已损坏。以下针对Maven 3.0.4和maven-resources-plugin 3.0.1进行了修复。
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>