Skip to content

Commit 154d44e

Browse files
committed
warn the user if drawing unavailable chars with default font
1 parent 5278908 commit 154d44e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

core/src/processing/awt/PGraphicsJava2D.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,8 +2055,20 @@ protected float textWidthImpl(char[] buffer, int start, int stop) {
20552055
protected void textLineImpl(char[] buffer, int start, int stop,
20562056
float x, float y) {
20572057
Font font = (Font) textFont.getNative();
2058-
// if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
20592058
if (font != null) {
2059+
// If using the default font, warn the user when their code calls
2060+
// text() called with unavailable characters. Not done with all
2061+
// fonts because it would be too slow, but useful/acceptable for
2062+
// the default case because it will hit beginners/casual use.
2063+
if (textFont.getName().equals(defaultFontName)) {
2064+
if (font.canDisplayUpTo(buffer, start, stop) != -1) {
2065+
final String msg =
2066+
"Some characters not available in the current font, " +
2067+
"use createFont() to specify a typeface the includes them.";
2068+
showWarning(msg);
2069+
}
2070+
}
2071+
20602072
/*
20612073
// save the current setting for text smoothing. note that this is
20622074
// different from the smooth() function, because the font smoothing
@@ -2109,7 +2121,7 @@ protected void textLineImpl(char[] buffer, int start, int stop,
21092121
//g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, textAntialias);
21102122
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialias);
21112123

2112-
} else { // otherwise just do the default
2124+
} else { // otherwise, just do the default
21132125
super.textLineImpl(buffer, start, stop, x, y);
21142126
}
21152127
}

core/src/processing/core/PGraphics.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@ public class PGraphics extends PImage implements PConstants {
460460
/** The current text leading (read-only) */
461461
public float textLeading;
462462

463+
/** Used internally to check whether still using the default font */
464+
protected String defaultFontName;
465+
463466
static final protected String ERROR_TEXTFONT_NULL_PFONT =
464467
"A null PFont was passed to textFont()";
465468

@@ -4243,6 +4246,7 @@ protected PFont createDefaultFont(float size) {
42434246
InputStream input = getClass().getResourceAsStream("/font/ProcessingSansPro-Regular.ttf");
42444247
if (input != null) {
42454248
baseFont = Font.createFont(Font.TRUETYPE_FONT, input);
4249+
defaultFontName = baseFont.getName();
42464250
}
42474251
} catch (Exception e) {
42484252
e.printStackTrace(); // dammit

0 commit comments

Comments
 (0)