Skip to content

Commit fd8de35

Browse files
committed
refactor some text methods following core's PApplet
1 parent 1ef9863 commit fd8de35

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

core/src/processing/core/PFont.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ public int getSize() {
417417
}
418418

419419

420+
/**
421+
* Returns the size that will be used when textFont(font) is called.
422+
*/
423+
public int getDefaultSize() {
424+
return size;
425+
}
426+
427+
420428
public void setSubsetting() {
421429
subsetting = true;
422430
}

core/src/processing/core/PGraphics.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,10 +3090,47 @@ public float textDescent() {
30903090
* The leading will also be reset.
30913091
*/
30923092
public void textFont(PFont which) {
3093-
if (which != null) {
3094-
textFont = which;
3093+
if (which == null) {
3094+
throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT);
3095+
}
3096+
textFontImpl(which, which.getDefaultSize());
3097+
}
3098+
3099+
3100+
/**
3101+
* Useful function to set the font and size at the same time.
3102+
*/
3103+
public void textFont(PFont which, float size) {
3104+
if (which == null) {
3105+
throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT);
3106+
}
3107+
// https://github.com/processing/processing/issues/3110
3108+
if (size <= 0) {
3109+
// Using System.err instead of showWarning to avoid running out of
3110+
// memory with a bunch of textSize() variants (cause of this bug is
3111+
// usually something done with map() or in a loop).
3112+
System.err.println("textFont: ignoring size " + size + " px:" +
3113+
"the text size must be larger than zero");
3114+
size = textSize;
3115+
}
3116+
textFontImpl(which, size);
3117+
}
3118+
3119+
3120+
/**
3121+
* Called from textFont. Check the validity of args and
3122+
* print possible errors to the user before calling this.
3123+
* Subclasses will want to override this one.
3124+
*
3125+
* @param which font to set, not null
3126+
* @param size size to set, greater than zero
3127+
*/
3128+
protected void textFontImpl(PFont which, float size) {
3129+
textFont = which;
30953130
// if (hints[ENABLE_NATIVE_FONTS]) {
3096-
// which.findTypeface(name);
3131+
// //if (which.font == null) {
3132+
// which.findNative();
3133+
// //}
30973134
// }
30983135
/*
30993136
textFontNative = which.font;
@@ -3118,20 +3155,8 @@ public void textFont(PFont which) {
31183155
// float w = font.getStringBounds(text, g2.getFontRenderContext()).getWidth();
31193156
}
31203157
*/
3121-
textSize(which.size);
31223158

3123-
} else {
3124-
throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT);
3125-
}
3126-
}
3127-
3128-
3129-
/**
3130-
* Useful function to set the font and size at the same time.
3131-
*/
3132-
public void textFont(PFont which, float size) {
3133-
textFont(which);
3134-
textSize(size);
3159+
handleTextSize(size);
31353160
}
31363161

31373162

0 commit comments

Comments
 (0)