7

On my system I need to register two external font .TTF files:

HamletOrNot.ttf (74 KB)
MorrisRoman-Black.ttf (67 KB)

Before creating the Font() objects, I record with the following commands:

/* 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 ));

Then I create the objects (I create more than one Font () because the formatting is different):

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 );

Ok, system works normally. The problem is that after running, the .TTF files change size (to 83 KB and 80 KB), thus:

HamletOrNot.ttf (83 KB)
MorrisRoman-Black.ttf (80 KB)

Then when I go to run the system for the second time, the following error occurs:

...
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

If I replace the .TTF files with changed size (83 KB and 80 KB) by the original (with 74 KB and 67 KB), the program works normally.

What am I doing wrong? I thought I'd have to close open files with applicationContext.getResource(), but do not know how to do this. I do not know how to solve it.

Note: I get the applicationContext of the Spring. Original TTF files (with correct sizes) are in the resources folder of the project, while TTF bad files are in the target resource folder (the build is done with Maven).

Help me...

EDIT:

I'm suspicious that maven is corrupting the files. Why? I'm using Eclipse with the Maven plugin. Whenever I do a deploy or press F5 to refresh the project, .TTF files from target folder become corrupted.

Has anyone seen the maven corrupt resource files?

thanks

lgapontes
  • 467
  • 7
  • 12

2 Answers2

31

Found it! The maven was corrupting .TTF files when doing the deploy code. To fix, you need to add the extensions that will not be filtered through nonFilteredFileExtension command in pom.xml (maven-resources-plugin).

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <encoding>UTF-8</encoding>
    <nonFilteredFileExtensions>
      <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
    </nonFilteredFileExtensions>
  </configuration>
</plugin>

Thank you! xD, ... was lucky!

lgapontes
  • 467
  • 7
  • 12
  • Thanks, this also helped me http://stackoverflow.com/questions/27030615/runtimeexception-native-typeface-cannot-be-made-but-only-from-jenkins-build#autocomment42591213 – Kevin D. Nov 20 '14 at 08:54
  • We had the same problem. Thanks for pointing out the problem. Our doubts were also on filtering of resources as we used maven profiles. as you said "nonFilteredFileExtensions" fixed problem. THX ! – Peter Aug 31 '15 at 18:17
  • 3
    After long debugging I came to the same point where the `maven-resources-plugin` is breaking my fonts when copying to `target` folder. I tried `` (basically same config as yours above), `false`, every possible version of the plugin (2.5, 2.6, 2.7, 3.0.1) but still my font files are broken by maven build... – jakub.g Sep 02 '16 at 16:03
  • Thanks! This comment fixed my Java Spring Setup where .ttf and .woff and .woff2 are now included.Thanks again for making me happy! – Dirk Schumacher Jan 17 '18 at 11:59
  • Sadly this does not fix it for me with Maven 3.6.3 and maven-resources-plugin 3.2.0... Edit: With Maven 3.5.4 and Maven 3.6.1 it worked. – KnechtRootrecht Jan 14 '21 at 13:40
0
 <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>

Broke the fonts as it copies over resources but font file sizes are altered and hence corrupted. This below fixed for Maven 3.0.4 and 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>
Rahul Saini
  • 2,353
  • 1
  • 16
  • 19