Skip to content

Commit 39c8cc2

Browse files
committed
adding built-in fonts for GUI consistency
1 parent 26eba7c commit 39c8cc2

File tree

10 files changed

+398
-91
lines changed

10 files changed

+398
-91
lines changed

app/src/processing/app/EditorToolbar.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public EditorToolbar(Editor editor, Base base) { //, JMenu menu) {
9797
modeTitle = mode.getTitle();
9898
modeTextFont = mode.getFont("mode.button.font");
9999
modeButtonColor = mode.getColor("mode.button.color");
100+
101+
System.out.println(modeTextFont.getPSName() + " " + mode.theme.get("mode.button.font"));
100102

101103
if (modeArrow == null) {
102104
String suffix = Toolkit.highResDisplay() ? "-2x.png" : ".png";

app/src/processing/app/Preferences.java

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -930,41 +930,61 @@ static public void setColor(String attr, Color what) {
930930
}
931931

932932

933+
// Identical version found in Settings.java
933934
static public Font getFont(String attr) {
934-
boolean replace = false;
935-
String value = get(attr);
936-
if (value == null) {
937-
//System.out.println("reset 1");
938-
value = getDefault(attr);
939-
replace = true;
940-
}
935+
try {
936+
boolean replace = false;
937+
String value = get(attr);
938+
if (value == null) {
939+
// use the default font instead
940+
value = getDefault(attr);
941+
replace = true;
942+
}
941943

942-
String[] pieces = PApplet.split(value, ',');
943-
if (pieces.length != 3) {
944-
value = getDefault(attr);
945-
//System.out.println("reset 2 for " + attr);
946-
pieces = PApplet.split(value, ',');
947-
//PApplet.println(pieces);
948-
replace = true;
949-
}
944+
String[] pieces = PApplet.split(value, ',');
950945

951-
String name = pieces[0];
952-
int style = Font.PLAIN; // equals zero
953-
if (pieces[1].indexOf("bold") != -1) { //$NON-NLS-1$
954-
style |= Font.BOLD;
955-
}
956-
if (pieces[1].indexOf("italic") != -1) { //$NON-NLS-1$
957-
style |= Font.ITALIC;
958-
}
959-
int size = PApplet.parseInt(pieces[2], 12);
960-
Font font = new Font(name, style, size);
946+
if (pieces.length != 3) {
947+
value = getDefault(attr);
948+
pieces = PApplet.split(value, ',');
949+
replace = true;
950+
}
961951

962-
// replace bad font with the default
963-
if (replace) {
964-
set(attr, value);
965-
}
952+
String name = pieces[0];
953+
int style = Font.PLAIN; // equals zero
954+
if (pieces[1].indexOf("bold") != -1) { //$NON-NLS-1$
955+
style |= Font.BOLD;
956+
}
957+
if (pieces[1].indexOf("italic") != -1) { //$NON-NLS-1$
958+
style |= Font.ITALIC;
959+
}
960+
int size = PApplet.parseInt(pieces[2], 12);
961+
962+
// replace bad font with the default from lib/preferences.txt
963+
if (replace) {
964+
set(attr, value);
965+
}
966+
967+
if (!name.startsWith("processing.")) {
968+
return new Font(name, style, size);
966969

967-
return font;
970+
} else {
971+
if (pieces[0].equals("processing.sans")) {
972+
if (style == Font.BOLD) {
973+
return Toolkit.getBoldFont(size);
974+
} else {
975+
return Toolkit.getPlainFont(size);
976+
}
977+
} else if (pieces[0].equals("processing.mono")) {
978+
return Toolkit.getMonoFont(size);
979+
}
980+
}
981+
982+
} catch (Exception e) {
983+
// Adding try/catch block because this may be where
984+
// a lot of startup crashes are happening.
985+
Base.log("Error with font " + get(attr) + " for attribute " + attr);
986+
}
987+
return new Font("Dialog", Font.PLAIN, 12);
968988
}
969989

970990

app/src/processing/app/Settings.java

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -176,42 +176,60 @@ public void setColor(String attr, Color what) {
176176
}
177177

178178

179+
// identical version found in Preferences.java
179180
public Font getFont(String attr) {
180-
boolean replace = false;
181-
String value = get(attr);
182-
if (value == null) {
183-
//System.out.println("reset 1");
184-
value = getDefault(attr);
185-
replace = true;
186-
}
181+
try {
182+
boolean replace = false;
183+
String value = get(attr);
184+
if (value == null) {
185+
// use the default font instead
186+
value = getDefault(attr);
187+
replace = true;
188+
}
187189

188-
String[] pieces = PApplet.split(value, ',');
189-
if (pieces.length != 3) {
190-
value = getDefault(attr);
191-
//System.out.println("reset 2 for " + attr);
192-
pieces = PApplet.split(value, ',');
193-
//PApplet.println(pieces);
194-
replace = true;
195-
}
190+
String[] pieces = PApplet.split(value, ',');
196191

197-
String name = pieces[0];
198-
int style = Font.PLAIN; // equals zero
199-
if (pieces[1].indexOf("bold") != -1) {
200-
style |= Font.BOLD;
201-
}
202-
if (pieces[1].indexOf("italic") != -1) {
203-
style |= Font.ITALIC;
204-
}
205-
int size = PApplet.parseInt(pieces[2], 12);
206-
Font font = new Font(name, style, size);
207-
208-
// replace bad font with the default
209-
if (replace) {
210-
//System.out.println(attr + " > " + value);
211-
//setString(attr, font.getName() + ",plain," + font.getSize());
212-
set(attr, value);
213-
}
192+
if (pieces.length != 3) {
193+
value = getDefault(attr);
194+
pieces = PApplet.split(value, ',');
195+
replace = true;
196+
}
214197

215-
return font;
198+
String name = pieces[0];
199+
int style = Font.PLAIN; // equals zero
200+
if (pieces[1].indexOf("bold") != -1) { //$NON-NLS-1$
201+
style |= Font.BOLD;
202+
}
203+
if (pieces[1].indexOf("italic") != -1) { //$NON-NLS-1$
204+
style |= Font.ITALIC;
205+
}
206+
int size = PApplet.parseInt(pieces[2], 12);
207+
208+
// replace bad font with the default from lib/preferences.txt
209+
if (replace) {
210+
set(attr, value);
211+
}
212+
213+
if (!name.startsWith("processing.")) {
214+
return new Font(name, style, size);
215+
216+
} else {
217+
if (pieces[0].equals("processing.sans")) {
218+
if (style == Font.BOLD) {
219+
return Toolkit.getBoldFont(size);
220+
} else {
221+
return Toolkit.getPlainFont(size);
222+
}
223+
} else if (pieces[0].equals("processing.mono")) {
224+
return Toolkit.getMonoFont(size);
225+
}
226+
}
227+
228+
} catch (Exception e) {
229+
// Adding try/catch block because this may be where
230+
// a lot of startup crashes are happening.
231+
Base.log("Error with font " + get(attr) + " for attribute " + attr);
232+
}
233+
return new Font("Dialog", Font.PLAIN, 12);
216234
}
217235
}

app/src/processing/app/Toolkit.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@
2222
package processing.app;
2323

2424
import java.awt.Dimension;
25+
import java.awt.Font;
26+
import java.awt.FontFormatException;
2527
import java.awt.Frame;
2628
import java.awt.Image;
2729
import java.awt.datatransfer.Clipboard;
2830
import java.awt.event.ActionEvent;
2931
import java.awt.event.ActionListener;
3032
import java.awt.event.KeyEvent;
33+
import java.io.BufferedInputStream;
3134
import java.io.File;
35+
import java.io.IOException;
36+
import java.io.InputStream;
3237
import java.util.ArrayList;
3338

3439
import javax.swing.ImageIcon;
@@ -228,4 +233,57 @@ static public boolean highResDisplay() {
228233
}
229234
return false;
230235
}
236+
237+
238+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
239+
240+
241+
static Font monoFont;
242+
static Font plainFont;
243+
static Font boldFont;
244+
245+
246+
static public Font getMonoFont(int size) {
247+
if (monoFont == null) {
248+
try {
249+
monoFont = createFont("DroidSansMono.ttf", size);
250+
} catch (Exception e) {
251+
monoFont = new Font("Monospaced", Font.PLAIN, size);
252+
}
253+
}
254+
return monoFont;
255+
}
256+
257+
258+
static public Font getPlainFont(int size) {
259+
if (plainFont == null) {
260+
try {
261+
plainFont = createFont("DroidSans.ttf", size);
262+
} catch (Exception e) {
263+
plainFont = new Font("SansSerif", Font.PLAIN, size);
264+
}
265+
}
266+
return plainFont;
267+
}
268+
269+
270+
static public Font getBoldFont(int size) {
271+
if (boldFont == null) {
272+
try {
273+
boldFont = createFont("DroidSans-Bold.ttf", size);
274+
} catch (Exception e) {
275+
boldFont = new Font("SansSerif", Font.BOLD, size);
276+
}
277+
}
278+
return boldFont;
279+
}
280+
281+
282+
static private Font createFont(String filename, int size) throws IOException, FontFormatException {
283+
InputStream is = Base.getLibStream("fonts/" + filename);
284+
BufferedInputStream input = new BufferedInputStream(is);
285+
Font font = Font.createFont(Font.TRUETYPE_FONT, input);
286+
input.close();
287+
return font.deriveFont((float) size);
288+
}
231289
}
41.5 KB
Binary file not shown.
40.1 KB
Binary file not shown.
114 KB
Binary file not shown.

0 commit comments

Comments
 (0)