Crossfire JXClient, Trunk  R20561
FontParser.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.skin.io;
23 
25 import java.awt.Font;
26 import java.awt.FontFormatException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import org.jetbrains.annotations.NotNull;
30 
35 public class FontParser {
36 
40  @NotNull
41  private final JXCSkinSource skinSource;
42 
47  public FontParser(@NotNull final JXCSkinSource skinSource) {
48  this.skinSource = skinSource;
49  }
50 
57  @NotNull
58  public Font getFont(@NotNull final String name) throws IOException {
59  final String filename = "fonts/"+name+".ttf";
60 
61  final Font font;
62  try {
63  try (final InputStream ttf = skinSource.getInputStream(filename)) {
64  try {
65  font = Font.createFont(Font.TRUETYPE_FONT, ttf);
66  } catch (final FontFormatException ex) {
67  throw new IOException(filename+": invalid font format: "+ex.getMessage(), ex);
68  }
69  }
70  } catch (final IOException ex) {
71  throw new IOException(skinSource.getURI(filename)+": i/o error: "+ex.getMessage(), ex);
72  }
73  return font;
74  }
75 
76 }
FontParser(@NotNull final JXCSkinSource skinSource)
Creates a new instance.
Definition: FontParser.java:47
String getURI(@NotNull String name)
Returns a description of the location of a resource name.
InputStream getInputStream(@NotNull String name)
Returns an InputStream for a resource name.
Creates Font instances from string representations.
Definition: FontParser.java:35
Font getFont(@NotNull final String name)
Returns a font by font file base name.
Definition: FontParser.java:58
Interface for providers of JXCSkin sources.
final JXCSkinSource skinSource
The JXCSkinSource for loading resources.
Definition: FontParser.java:41