2
2
*
3
3
* The MIT License
4
4
*
5
- * Copyright 2020 Paul Conti
5
+ * Copyright 2020-2022 Paul Conti
6
6
*
7
7
* Permission is hereby granted, free of charge, to any person obtaining a copy
8
8
* of this software and associated documentation files (the "Software"), to deal
38
38
import java .io .BufferedWriter ;
39
39
import java .io .File ;
40
40
import java .io .FileOutputStream ;
41
- import java .io .IOException ;
42
41
import java .io .OutputStreamWriter ;
43
42
import java .util .ArrayList ;
44
43
@@ -96,29 +95,32 @@ public FontGenerator() {
96
95
* the ttf font file
97
96
*/
98
97
public static void generateFontSet (File ttfFile ) {
99
- JComboBox <Integer > cbSizes = new JComboBox <Integer >(FontSizes );
100
- JTextField txtOutputName = new JTextField ();
101
- String outputFileName = ttfFile .getName ();
102
- int idx = outputFileName .indexOf (".ttf" );
103
- outputFileName = outputFileName .substring (0 ,idx );
104
- outputFileName = outputFileName .replaceAll ("[\\ s\\ -\\ .]" , "_" );
105
- txtOutputName .setText (outputFileName );
106
- nTotalBytes = 0 ;
107
- Object [] message = {
108
- "Font Size:" , cbSizes ,
109
- "Output file:" , txtOutputName
110
- };
111
- int option = JOptionPane .showConfirmDialog (null , message , "Export" , JOptionPane .OK_CANCEL_OPTION );
112
- if (option != JOptionPane .OK_OPTION ) {
113
- return ;
114
- }
115
- Integer iSize = (Integer ) cbSizes .getSelectedItem ();
116
- String userFileName = txtOutputName .getText ();
117
- userFileName = userFileName .replaceAll ("[\\ s\\ -\\ .]" , "_" );
118
-
119
- outputFileName = FontBuilder .getWorkingDir () + userFileName + "_" + iSize .toString () + "pt" + ".h" ;
120
- //create the font to use.
121
98
try {
99
+ JComboBox <Integer > cbSizes = new JComboBox <Integer >(FontSizes );
100
+ JTextField txtOutputName = new JTextField ();
101
+ String outputFileName = ttfFile .getName ().toLowerCase ();
102
+ int idx = outputFileName .indexOf (".ttf" );
103
+ if (idx != -1 ) {
104
+ outputFileName = outputFileName .substring (0 ,idx );
105
+ }
106
+ outputFileName = outputFileName .substring (0 ,idx );
107
+ outputFileName = outputFileName .replaceAll ("[\\ s\\ -\\ .]" , "_" );
108
+ txtOutputName .setText (outputFileName );
109
+ nTotalBytes = 0 ;
110
+ Object [] message = {
111
+ "Font Size:" , cbSizes ,
112
+ "Output file:" , txtOutputName
113
+ };
114
+ int option = JOptionPane .showConfirmDialog (null , message , "Export" , JOptionPane .OK_CANCEL_OPTION );
115
+ if (option != JOptionPane .OK_OPTION ) {
116
+ return ;
117
+ }
118
+ Integer iSize = (Integer ) cbSizes .getSelectedItem ();
119
+ String userFileName = txtOutputName .getText ();
120
+ userFileName = userFileName .replaceAll ("[\\ s\\ -\\ .]" , "_" );
121
+
122
+ outputFileName = FontBuilder .getWorkingDir () + userFileName + "_" + iSize .toString () + "pt" + ".h" ;
123
+ //create the font to use.
122
124
/* Use scaling factor to reset size of font,
123
125
* If we don't account for this the font will appear half the size
124
126
* of Adafruit's Free Fonts.
@@ -128,40 +130,33 @@ public static void generateFontSet(File ttfFile) {
128
130
GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment ();
129
131
//register the font
130
132
ge .registerFont (font );
131
- } catch (Exception e ) {
132
- JOptionPane .showMessageDialog (null ,
133
- e .toString (),
134
- "ERROR" ,
135
- JOptionPane .ERROR_MESSAGE );
136
- return ;
137
- }
138
133
139
- image = new BufferedImage (100 , 100 , BufferedImage .TYPE_INT_RGB );
140
- g2d = (Graphics2D ) image .getGraphics ();
141
- g2d .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING ,
142
- RenderingHints .VALUE_TEXT_ANTIALIAS_OFF );
143
- g2d .setFont (font );
144
- fontMetrics = g2d .getFontMetrics ();
134
+ image = new BufferedImage (100 , 100 , BufferedImage .TYPE_INT_RGB );
135
+ g2d = (Graphics2D ) image .getGraphics ();
136
+ g2d .setRenderingHint (RenderingHints .KEY_TEXT_ANTIALIASING ,
137
+ RenderingHints .VALUE_TEXT_ANTIALIAS_OFF );
138
+ g2d .setFont (font );
139
+ fontMetrics = g2d .getFontMetrics ();
145
140
// baselineY = fontMetrics.getMaxAscent();
146
- ArrayList <FontGFXGlyph > fontList = new ArrayList <FontGFXGlyph >();
147
- for (CharacterHelper h : DrawGFX .characterList ) {
148
- fontList .add (decodeChar (h ));
149
- }
150
- StringBuilder sBd = new StringBuilder ();
151
- File fontSet = new File (outputFileName );
152
- String fontName = fontSet .getName ();
153
- int n = fontName .indexOf (".h" );
154
- fontName = fontName .substring (0 ,n );
155
- buildFontSet (sBd , fontName , fontList );
156
- BufferedWriter bw = null ;
157
- try {
141
+ ArrayList <FontGFXGlyph > fontList = new ArrayList <FontGFXGlyph >();
142
+ for (CharacterHelper h : DrawGFX .characterList ) {
143
+ fontList .add (decodeChar (h ));
144
+ }
145
+ StringBuilder sBd = new StringBuilder ();
146
+ File fontSet = new File (outputFileName );
147
+ String fontName = fontSet .getName ();
148
+ int n = fontName .indexOf (".h" );
149
+ fontName = fontName .substring (0 ,n );
150
+ buildFontSet (sBd , fontName , fontList );
151
+ BufferedWriter bw = null ;
152
+
158
153
bw = new BufferedWriter (new OutputStreamWriter (
159
154
new FileOutputStream (fontSet ), "UTF-8" ));
160
155
bw .write (sBd .toString ());
161
156
bw .flush ();
162
157
bw .close ();
163
158
FontBuilder .postStatusMsg (String .format ("created->%s" ,outputFileName ),10000 );
164
- } catch (IOException e ) {
159
+ } catch (Exception e ) {
165
160
JOptionPane .showMessageDialog (null ,
166
161
e .toString (),
167
162
"ERROR" ,
0 commit comments