Skip to content

Commit 9a63a4e

Browse files
committed
take care of font scaling for 2x density (fixes processing#2739)
1 parent 74fee4f commit 9a63a4e

5 files changed

Lines changed: 85 additions & 21 deletions

File tree

core/src/processing/core/PApplet.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5962,7 +5962,7 @@ public PFont createFont(String name, float size, boolean smooth) {
59625962
* @see PApplet#loadFont(String)
59635963
*/
59645964
public PFont createFont(String name, float size,
5965-
boolean smooth, char charset[]) {
5965+
boolean smooth, char[] charset) {
59665966
String lowerName = name.toLowerCase();
59675967
Font baseFont = null;
59685968

@@ -5982,11 +5982,12 @@ public PFont createFont(String name, float size,
59825982
} else {
59835983
baseFont = PFont.findFont(name);
59845984
}
5985-
return new PFont(baseFont.deriveFont(size), smooth, charset,
5986-
stream != null);
5985+
return new PFont(baseFont.deriveFont(size * pixelDensity),
5986+
smooth, charset, stream != null,
5987+
pixelDensity);
59875988

59885989
} catch (Exception e) {
5989-
System.err.println("Problem createFont(" + name + ")");
5990+
System.err.println("Problem with createFont(\"" + name + "\")");
59905991
e.printStackTrace();
59915992
return null;
59925993
}

core/src/processing/core/PFont.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
/*
44
Part of the Processing project - http://processing.org
55
6-
Copyright (c) 2004-10 Ben Fry & Casey Reas
6+
Copyright (c) 2012-15 The Processing Foundation
7+
Copyright (c) 2004-12 Ben Fry & Casey Reas
78
Copyright (c) 2001-04 Massachusetts Institute of Technology
89
910
This library is free software; you can redistribute it and/or
@@ -84,7 +85,15 @@ public class PFont implements PConstants {
8485
/**
8586
* The original size of the font when it was first created
8687
*/
87-
protected int size;
88+
//protected int size;
89+
private int size;
90+
91+
protected int density;
92+
// /**
93+
// * The size that this font will default to when drawn. Used to create fonts
94+
// * at 2x the size for high-res displays in OpenGL.
95+
// */
96+
// protected int defaultSize;
8897

8998
/** true if smoothing was enabled for this font, used for native impl */
9099
protected boolean smooth;
@@ -124,11 +133,10 @@ public class PFont implements PConstants {
124133
protected Font font;
125134

126135
/**
127-
* True if this font was loaded from a stream, rather than from the OS.
128-
* It's always safe to use the native version of a font loaded from a TTF
129-
* file, since that's how it'll look when exported. Otherwise, you'll have
130-
* to use hint(ENABLE_NATIVE_FONTS) to get the native version working with
131-
* renderers that support it.
136+
* True if this font was loaded from an InputStream, rather than by name
137+
* from the OS. It's best to use the native version of a font loaded from
138+
* a TTF file, since that will ensure that the font is available when the
139+
* sketch is exported.
132140
*/
133141
protected boolean stream;
134142

@@ -329,9 +337,11 @@ public PFont(Font font, boolean smooth, char charset[]) {
329337
*
330338
* @nowebref
331339
*/
332-
public PFont(Font font, boolean smooth, char charset[], boolean stream) {
340+
public PFont(Font font, boolean smooth, char charset[],
341+
boolean stream, int density) {
333342
this(font, smooth, charset);
334343
this.stream = stream;
344+
this.density = density;
335345
}
336346

337347
/**
@@ -536,6 +546,24 @@ public int getSize() {
536546
}
537547

538548

549+
// public void setDefaultSize(int size) {
550+
// defaultSize = size;
551+
// }
552+
553+
554+
/**
555+
* Returns the size that will be used when textFont(font) is called.
556+
* When drawing with 2x pixel density, bitmap fonts in OpenGL need to be
557+
* created (behind the scenes) at double the requested size. This ensures
558+
* that they're shown at half on displays (so folks don't have to change
559+
* their sketch code).
560+
*/
561+
public int getDefaultSize() {
562+
//return defaultSize;
563+
return size / density;
564+
}
565+
566+
539567
public boolean isStream() {
540568
return stream;
541569
}

core/src/processing/core/PGraphics.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.awt.Color;
2929
import java.awt.Font;
3030
import java.awt.FontMetrics;
31+
import java.awt.Frame;
3132
import java.awt.Image;
3233
import java.awt.Toolkit;
3334
import java.awt.font.FontRenderContext;
@@ -4117,7 +4118,7 @@ public void textFont(PFont which) {
41174118
// float w = font.getStringBounds(text, g2.getFontRenderContext()).getWidth();
41184119
}
41194120
*/
4120-
textSize(which.size);
4121+
textSize(which.getDefaultSize());
41214122

41224123
} else {
41234124
throw new RuntimeException(ERROR_TEXTFONT_NULL_PFONT);
@@ -4806,10 +4807,10 @@ protected void textCharImpl(char ch, float x, float y) { //, float z) {
48064807
PFont.Glyph glyph = textFont.getGlyph(ch);
48074808
if (glyph != null) {
48084809
if (textMode == MODEL) {
4809-
float high = glyph.height / (float) textFont.size;
4810-
float bwidth = glyph.width / (float) textFont.size;
4811-
float lextent = glyph.leftExtent / (float) textFont.size;
4812-
float textent = glyph.topExtent / (float) textFont.size;
4810+
float high = glyph.height / (float) textFont.getSize();
4811+
float bwidth = glyph.width / (float) textFont.getSize();
4812+
float lextent = glyph.leftExtent / (float) textFont.getSize();
4813+
float textent = glyph.topExtent / (float) textFont.getSize();
48134814

48144815
float x1 = x + lextent * textSize;
48154816
float y1 = y - textent * textSize;
@@ -4858,6 +4859,7 @@ protected void textCharModelImpl(PImage glyph,
48584859
}
48594860

48604861

4862+
/*
48614863
protected void textCharScreenImpl(PImage glyph,
48624864
int xx, int yy,
48634865
int w0, int h0) {
@@ -4908,6 +4910,7 @@ protected void textCharScreenImpl(PImage glyph,
49084910
}
49094911
}
49104912
}
4913+
*/
49114914

49124915

49134916
/**
@@ -4917,6 +4920,10 @@ protected void textCharScreenImpl(PImage glyph,
49174920
*/
49184921
@SuppressWarnings("deprecation")
49194922
public FontMetrics getFontMetrics(Font font) { // ignore
4923+
Frame frame = parent.getFrame();
4924+
if (frame != null) {
4925+
return frame.getToolkit().getFontMetrics(font);
4926+
}
49204927
return Toolkit.getDefaultToolkit().getFontMetrics(font);
49214928
}
49224929

core/todo.txt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
0238 (3.0b1)
22

3+
text/fonts
4+
X Text looks blurry in GL Retina
5+
X https://github.com/processing/processing/issues/2739
6+
7+
andres
8+
X ortho function is broken
9+
X https://github.com/processing/processing/issues/1278
10+
X errors with loading SVGs in P3D/P2D
11+
X https://github.com/processing/processing/issues/3379
12+
313

414
beta
515
_ replace sketchXxxx() methods with another mechanism?
@@ -10,9 +20,24 @@ _ surface.size(), surface.noSmooth()... just like PGraphics methods?
1020
_ make final to move folks away from these?
1121
_ or is this a bad move until we've sorted out Android?
1222
_ update wiki/docs to say "don't override sketchXxxx() methods"
23+
_ pixelDensity(BEST)? (messes with pixels, but for most sketches, helpful)
24+
_ add pixelDensity() method to PImage/PGraphics
25+
_ pixelDensity(2) does: pixelWidth = width; width /= 2; pixelDensity = 2;
26+
_ this works for both PGraphics and images (though they're a little backwards)
27+
_ for PGraphics it comes early enough
28+
_ g.pixelDensity should prolly move to PApplet?
29+
_ or is it akin to g.fill and the rest?
1330

1431

1532
high priority
33+
_ sketches with new fullScreen() method should grab focus by default
34+
_ https://github.com/processing/processing/issues/3380
35+
_ fullScreen(SPAN) not working
36+
_ probably need to re-query for displays each time opening prefs
37+
_ what happens when a screen is added after p5 has started?
38+
_ https://github.com/processing/processing/issues/3381
39+
_ clear() is broken
40+
_ https://github.com/processing/processing/issues/3378
1641
_ add the "don't use this" warning to the JFrame in PSurfaceAWT
1742
_ draw() executes twice when noLoop() called in setup()
1843
_ https://github.com/processing/processing/issues/3310
@@ -88,8 +113,6 @@ _ also not API we want to expose, so sort this out
88113
_ or maybe we're fine b/c now FX2D needs it as well
89114
_ when did setPath() sneak into PShape? API is nothing like anything else
90115
_ probably from the material stuff, but we need to fix that
91-
_ ortho function is broken
92-
_ https://github.com/processing/processing/issues/1278
93116

94117

95118
full screen
@@ -171,8 +194,6 @@ _ zero alpha values still a problem with retina renderer
171194
_ https://github.com/processing/processing/issues/2030
172195
_ NPE when using image() created with createGraphics(PGraphicsRetina2D)
173196
_ https://github.com/processing/processing/issues/2510
174-
_ Text looks blurry in GL Retina (andres)
175-
_ https://github.com/processing/processing/issues/2739
176197
_ check retina with PGraphicsRetina2D
177198
_ Text is half size in PGraphicsRetina2D
178199
_ https://github.com/processing/processing/issues/2738

todo.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
0238 (3.0b1)
22

3+
gsoc
4+
X Mode problems window wasn't doing line breaks
5+
X https://github.com/processing/processing/issues/3369
6+
X https://github.com/processing/processing/pull/3370
7+
8+
"OS X v10.11 is the last major release of OS X that will support the previously deprecated Java 6 runtime and tools provided by Apple. Applications or features that depend upon Java 6 may not function properly or will not launch when Java 6 is removed." https://developer.apple.com/library/prerelease/mac/releasenotes/General/rn-osx-10.11/index.html
9+
310

411
beta
512
_ add "welcome" or "what's new" window to explain features in 3

0 commit comments

Comments
 (0)