Skip to content

Commit 3bac133

Browse files
committed
improve character width loading from resources and error messages when the resource is not found
1 parent b1113b2 commit 3bac133

1 file changed

Lines changed: 46 additions & 45 deletions

File tree

src/main/java/com/koadweb/javafpdf/Charwidths.java

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,57 +29,58 @@
2929

3030
/**
3131
* Character widths.
32-
*
32+
*
3333
* @author Alan Plum
3434
* @since 4 Mar 2008
3535
* @version $Revision: 1.3 $
3636
*/
3737
class Charwidths {
38-
private final Properties props;
3938

40-
/**
41-
* Constructor. Creates a Charwidths object for a core font.
42-
*
43-
* @param name
44-
* name of the character widths file.
45-
* @throws IOException
46-
* if an error occurred when reading from the file.
47-
*/
48-
public Charwidths(final String name) throws IOException {
49-
InputStream stream = this.getClass().getResourceAsStream(
50-
"fonts/" + name + ".widths"); //$NON-NLS-1$//$NON-NLS-2$
51-
this.props = new Properties();
52-
this.props.load(stream);
53-
stream.close();
54-
}
39+
private final Properties props;
40+
41+
/**
42+
* Constructor. Creates a Charwidths object for a core font.
43+
*
44+
* @param name name of the character widths file.
45+
* @throws IOException if an error occurred when reading from the file.
46+
*/
47+
public Charwidths(final String name) throws IOException {
48+
String fontWidthResourceName = "/fonts/" + name + ".widths";
49+
InputStream stream = Charwidths.class.getResourceAsStream(fontWidthResourceName);
50+
if ( stream == null ) {
51+
throw new IOException("resource " + fontWidthResourceName + " not found with getResourceAsStream()");
52+
}
53+
54+
this.props = new Properties();
55+
this.props.load(stream);
56+
stream.close();
57+
}
58+
59+
/**
60+
* Constructor. Creates a Charwidths object from a local file.
61+
*
62+
* @param file file containing the character widths info.
63+
* @throws IOException if an error occurred when reading from the file.
64+
*/
65+
public Charwidths(final File file) throws IOException {
66+
InputStream stream = new FileInputStream(file);
5567

56-
/**
57-
* Constructor. Creates a Charwidths object from a local file.
58-
*
59-
* @param file
60-
* file containing the character widths info.
61-
* @throws IOException
62-
* if an error occurred when reading from the file.
63-
*/
64-
public Charwidths(final File file) throws IOException {
65-
InputStream stream = new FileInputStream(file);
66-
this.props = new Properties();
67-
this.props.load(stream);
68-
stream.close();
69-
}
68+
this.props = new Properties();
69+
this.props.load(stream);
70+
stream.close();
71+
}
7072

71-
/**
72-
* Get the width of the given character.
73-
*
74-
* @param c
75-
* a character
76-
* @return the width of that character.
77-
*/
78-
public int get(final char c) {
79-
String str = this.props.getProperty(Integer.valueOf(c).toString());
80-
if (str == null) {
81-
return 600;
82-
}
83-
return Integer.parseInt(str);
84-
}
73+
/**
74+
* Get the width of the given character.
75+
*
76+
* @param c a character
77+
* @return the width of that character.
78+
*/
79+
public int get(final char c) {
80+
String str = this.props.getProperty(Integer.valueOf(c).toString());
81+
if (str == null) {
82+
return 600;
83+
}
84+
return Integer.parseInt(str);
85+
}
8586
}

0 commit comments

Comments
 (0)