propane 3.4.2-java → 3.8.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -1
  3. data/.mvn/wrapper/MavenWrapperDownloader.java +2 -2
  4. data/.mvn/wrapper/maven-wrapper.properties +2 -2
  5. data/.travis.yml +1 -1
  6. data/CHANGELOG.md +9 -1
  7. data/Gemfile +2 -0
  8. data/README.md +7 -10
  9. data/Rakefile +10 -11
  10. data/bin/propane +3 -1
  11. data/lib/propane.rb +4 -2
  12. data/lib/propane/app.rb +5 -1
  13. data/lib/propane/creators/sketch_class.rb +7 -1
  14. data/lib/propane/creators/sketch_factory.rb +4 -2
  15. data/lib/propane/creators/sketch_writer.rb +1 -0
  16. data/lib/propane/helper_methods.rb +22 -23
  17. data/lib/propane/helpers/numeric.rb +2 -0
  18. data/lib/propane/helpers/version_error.rb +1 -0
  19. data/lib/propane/library.rb +5 -1
  20. data/lib/propane/library_loader.rb +2 -0
  21. data/lib/propane/native_folder.rb +10 -9
  22. data/lib/propane/native_loader.rb +3 -0
  23. data/lib/propane/runner.rb +20 -14
  24. data/lib/propane/version.rb +2 -1
  25. data/library/boids/boids.rb +21 -11
  26. data/library/color_group/color_group.rb +2 -0
  27. data/library/control_panel/control_panel.rb +8 -5
  28. data/library/dxf/dxf.rb +2 -0
  29. data/library/file_chooser/chooser.rb +10 -9
  30. data/library/file_chooser/file_chooser.rb +10 -9
  31. data/library/library_proxy/library_proxy.rb +2 -0
  32. data/library/net/net.rb +2 -0
  33. data/library/slider/slider.rb +23 -22
  34. data/library/vector_utils/vector_utils.rb +4 -0
  35. data/library/video_event/video_event.rb +2 -0
  36. data/pom.rb +37 -36
  37. data/pom.xml +7 -7
  38. data/propane.gemspec +13 -9
  39. data/src/main/java/japplemenubar/JAppleMenuBar.java +3 -3
  40. data/src/main/java/monkstone/ColorUtil.java +1 -3
  41. data/src/main/java/monkstone/MathToolModule.java +1 -1
  42. data/src/main/java/monkstone/PropaneLibrary.java +2 -2
  43. data/src/main/java/monkstone/fastmath/DegLutTables.java +111 -0
  44. data/src/main/java/monkstone/fastmath/Deglut.java +6 -56
  45. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  46. data/src/main/java/monkstone/noise/Noise.java +116 -0
  47. data/src/main/java/monkstone/noise/NoiseGenerator.java +63 -0
  48. data/src/main/java/monkstone/noise/NoiseMode.java +15 -0
  49. data/src/main/java/monkstone/noise/SimplexNoise.java +137 -103
  50. data/src/main/java/monkstone/noise/ValueNoise.java +170 -0
  51. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
  52. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  53. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
  54. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
  55. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  56. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  57. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  58. data/src/main/java/monkstone/vecmath/package-info.java +1 -1
  59. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +1 -1
  60. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -2
  61. data/src/main/java/monkstone/videoevent/CaptureEvent.java +1 -1
  62. data/src/main/java/monkstone/videoevent/MovieEvent.java +1 -1
  63. data/src/main/java/monkstone/videoevent/package-info.java +1 -1
  64. data/src/main/java/processing/awt/PGraphicsJava2D.java +781 -285
  65. data/src/main/java/processing/awt/PImageAWT.java +377 -0
  66. data/src/main/java/processing/awt/PShapeJava2D.java +56 -52
  67. data/src/main/java/processing/awt/PSurfaceAWT.java +308 -208
  68. data/src/main/java/processing/awt/ShimAWT.java +581 -0
  69. data/src/main/java/processing/core/PApplet.java +13142 -13883
  70. data/src/main/java/processing/core/PConstants.java +477 -447
  71. data/src/main/java/processing/core/PFont.java +914 -880
  72. data/src/main/java/processing/core/PGraphics.java +152 -136
  73. data/src/main/java/processing/core/PImage.java +275 -372
  74. data/src/main/java/processing/core/PMatrix.java +172 -159
  75. data/src/main/java/processing/core/PMatrix2D.java +478 -415
  76. data/src/main/java/processing/core/PMatrix3D.java +762 -735
  77. data/src/main/java/processing/core/PShape.java +2887 -2651
  78. data/src/main/java/processing/core/PShapeOBJ.java +97 -92
  79. data/src/main/java/processing/core/PShapeSVG.java +1705 -1490
  80. data/src/main/java/processing/core/PStyle.java +40 -37
  81. data/src/main/java/processing/core/PSurface.java +139 -97
  82. data/src/main/java/processing/core/PSurfaceNone.java +296 -218
  83. data/src/main/java/processing/core/PVector.java +995 -963
  84. data/src/main/java/processing/core/ThinkDifferent.java +12 -8
  85. data/src/main/java/processing/data/DoubleDict.java +756 -710
  86. data/src/main/java/processing/data/DoubleList.java +749 -696
  87. data/src/main/java/processing/data/FloatDict.java +748 -702
  88. data/src/main/java/processing/data/FloatList.java +751 -697
  89. data/src/main/java/processing/data/IntDict.java +720 -673
  90. data/src/main/java/processing/data/IntList.java +699 -633
  91. data/src/main/java/processing/data/JSONArray.java +931 -873
  92. data/src/main/java/processing/data/JSONObject.java +1262 -1165
  93. data/src/main/java/processing/data/JSONTokener.java +351 -341
  94. data/src/main/java/processing/data/LongDict.java +710 -663
  95. data/src/main/java/processing/data/LongList.java +701 -635
  96. data/src/main/java/processing/data/Sort.java +37 -41
  97. data/src/main/java/processing/data/StringDict.java +525 -486
  98. data/src/main/java/processing/data/StringList.java +626 -580
  99. data/src/main/java/processing/data/Table.java +3690 -3510
  100. data/src/main/java/processing/data/TableRow.java +182 -183
  101. data/src/main/java/processing/data/XML.java +957 -883
  102. data/src/main/java/processing/event/Event.java +87 -67
  103. data/src/main/java/processing/event/KeyEvent.java +48 -41
  104. data/src/main/java/processing/event/MouseEvent.java +88 -113
  105. data/src/main/java/processing/event/TouchEvent.java +10 -6
  106. data/src/main/java/processing/javafx/PGraphicsFX2D.java +20 -345
  107. data/src/main/java/processing/javafx/PSurfaceFX.java +149 -121
  108. data/src/main/java/processing/net/Client.java +20 -20
  109. data/src/main/java/processing/net/Server.java +9 -9
  110. data/src/main/java/processing/opengl/FontTexture.java +286 -266
  111. data/src/main/java/processing/opengl/FrameBuffer.java +389 -377
  112. data/src/main/java/processing/opengl/LinePath.java +132 -89
  113. data/src/main/java/processing/opengl/LineStroker.java +588 -581
  114. data/src/main/java/processing/opengl/PGL.java +660 -567
  115. data/src/main/java/processing/opengl/PGraphics2D.java +408 -315
  116. data/src/main/java/processing/opengl/PGraphics3D.java +107 -72
  117. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12378 -12075
  118. data/src/main/java/processing/opengl/PJOGL.java +1753 -1670
  119. data/src/main/java/processing/opengl/PShader.java +369 -461
  120. data/src/main/java/processing/opengl/PShapeOpenGL.java +4678 -4580
  121. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1114 -1027
  122. data/src/main/java/processing/opengl/Texture.java +1492 -1401
  123. data/src/main/java/processing/opengl/VertexBuffer.java +57 -55
  124. data/test/create_test.rb +21 -20
  125. data/test/deglut_spec_test.rb +4 -2
  126. data/test/helper_methods_test.rb +49 -20
  127. data/test/math_tool_test.rb +39 -32
  128. data/test/native_folder.rb +47 -0
  129. data/test/respond_to_test.rb +3 -2
  130. data/test/sketches/key_event.rb +2 -2
  131. data/test/sketches/library/my_library/my_library.rb +3 -0
  132. data/test/test_helper.rb +2 -0
  133. data/test/vecmath_spec_test.rb +35 -22
  134. data/vendors/Rakefile +35 -40
  135. metadata +47 -23
  136. data/library/simplex_noise/simplex_noise.rb +0 -3
  137. data/src/main/java/processing/opengl/shaders/LightVert-brcm.glsl +0 -154
  138. data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +0 -154
  139. data/src/main/java/processing/opengl/shaders/TexLightVert-brcm.glsl +0 -160
  140. data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +0 -160
@@ -1,7 +1,9 @@
1
+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
1
3
  /*
2
4
  Part of the Processing project - http://processing.org
3
5
 
4
- Copyright (c) 2012-15 The Processing Foundation
6
+ Copyright (c) 2012-19 The Processing Foundation
5
7
  Copyright (c) 2004-12 Ben Fry & Casey Reas
6
8
  Copyright (c) 2001-04 Massachusetts Institute of Technology
7
9
 
@@ -18,7 +20,8 @@
18
20
  Public License along with this library; if not, write to the
19
21
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
22
  Boston, MA 02111-1307 USA
21
- */
23
+ */
24
+
22
25
  package processing.core;
23
26
 
24
27
  import java.awt.*;
@@ -30,6 +33,7 @@ import java.io.*;
30
33
  import java.util.Arrays;
31
34
  import java.util.HashMap;
32
35
 
36
+
33
37
  /**
34
38
  * Grayscale bitmap font class used by Processing.
35
39
  * <P>
@@ -51,7 +55,6 @@ import java.util.HashMap;
51
55
  *
52
56
  * ^^^^^^^^^^^^^^ setWidth (width displaced by char)
53
57
  * </PRE>
54
- *
55
58
  * @webref typography
56
59
  * @see PApplet#loadFont(String)
57
60
  * @see PApplet#createFont(String, float, boolean, char[])
@@ -59,231 +62,223 @@ import java.util.HashMap;
59
62
  */
60
63
  public class PFont implements PConstants {
61
64
 
62
- /**
63
- * Number of character glyphs in this font.
64
- */
65
- protected int glyphCount;
66
-
67
- /**
68
- * Actual glyph data. The length of this array won't necessarily be the same
69
- * size as glyphCount, in cases where lazy font loading is in use.
70
- */
71
- protected Glyph[] glyphs;
72
-
73
- /**
74
- * Name of the font as seen by Java when it was created. If the font is
75
- * available, the native version will be used.
76
- */
77
- protected String name;
78
-
79
- /**
80
- * Postscript name of the font that this bitmap was created from.
81
- */
82
- protected String psname;
83
-
84
- /**
85
- * The original size of the font when it was first created
86
- */
87
- protected int size;
88
-
89
- /**
90
- * Default density set to 1 for backwards compatibility with loadFont().
91
- */
92
- protected int density = 1;
93
-
94
- /**
95
- * true if smoothing was enabled for this font, used for native impl
96
- */
97
- protected boolean smooth;
98
-
99
- /**
100
- * The ascent of the font. If the 'd' character is present in this PFont,
101
- * this value is replaced with its pixel height, because the values returned
102
- * by FontMetrics.getAscent() seem to be terrible.
103
- */
104
- protected int ascent;
105
-
106
- /**
107
- * The descent of the font. If the 'p' character is present in this PFont,
108
- * this value is replaced with its lowest pixel height, because the values
109
- * returned by FontMetrics.getDescent() are gross.
110
- */
111
- protected int descent;
112
-
113
- /**
114
- * A more efficient array lookup for straight ASCII characters. For Unicode
115
- * characters, a QuickSort-style search is used.
116
- */
117
- protected int[] ascii;
118
-
119
- /**
120
- * True if this font is set to load dynamically. This is the default when
121
- * createFont() method is called without a character set. Bitmap versions of
122
- * characters are only created when prompted by an index() call.
123
- */
124
- protected boolean lazy;
125
-
126
- /**
127
- * Native Java version of the font. If possible, this allows the PGraphics
128
- * subclass to just use Java's font rendering stuff in situations where
129
- * that's faster.
130
- */
131
- protected Font font;
132
-
133
- /**
134
- * True if this font was loaded from an InputStream, rather than by name
135
- * from the OS. It's best to use the native version of a font loaded from a
136
- * TTF file, since that will ensure that the font is available when the
137
- * sketch is exported.
138
- */
139
- protected boolean stream;
140
-
141
- /**
142
- * True if this font should return 'null' for getFont(), so that the native
143
- * font will be used to create a subset, but the native version of the font
144
- * will not be used.
145
- */
146
- protected boolean subsetting;
147
-
148
- /**
149
- * True if already tried to find the native AWT version of this font.
150
- */
151
- protected boolean fontSearched;
152
-
153
- /**
154
- * Array of the native system fonts. Used to lookup native fonts by their
155
- * PostScript name. This is a workaround for a several year old Apple Java
156
- * bug that they can't be bothered to fix.
157
- */
158
- static protected Font[] fonts;
159
- static protected HashMap<String, Font> fontDifferent;
160
-
161
- // /**
162
- // * If not null, this font is set to load dynamically. This is the default
163
- // * when createFont() method is called without a character set. Bitmap
164
- // * versions of characters are only created when prompted by an index() call.
165
- // */
166
- // protected Font lazyFont;
167
- protected BufferedImage lazyImage;
168
- protected Graphics2D lazyGraphics;
169
- protected FontMetrics lazyMetrics;
170
- protected int[] lazySamples;
171
-
172
- /**
173
- * for subclasses that need to store metadata about the font
174
- */
175
- // protected HashMap<PGraphics, Object> cacheMap;
176
- /**
177
- * @nowebref
178
- */
179
- public PFont() {
180
- } // for subclasses
181
-
182
- /**
183
- * ( begin auto-generated from PFont.xml )
184
- *
185
- * PFont is the font class for Processing. To create a font to use with
186
- * Processing, select "Create Font..." from the Tools menu. This will create
187
- * a font in the format Processing requires and also adds it to the current
188
- * sketch's data directory. Processing displays fonts using the .vlw font
189
- * format, which uses images for each letter, rather than defining them
190
- * through vector data. The <b>loadFont()</b> function constructs a new font
191
- * and <b>textFont()</b> makes a font active. The
192
- * <b>list()</b> method creates a list of the fonts installed on the
193
- * computer, which is useful information to use with the
194
- * <b>createFont()</b> function for dynamically converting fonts into a
195
- * format to use with Processing.
196
- *
197
- * ( end auto-generated )
198
- *
199
- * @nowebref
200
- * @param font font the font object to create from
201
- * @param smooth smooth true to enable smoothing/anti-aliasing
202
- */
203
- public PFont(Font font, boolean smooth) {
204
- this(font, smooth, null);
205
- }
65
+ /** Number of character glyphs in this font. */
66
+ protected int glyphCount;
67
+
68
+ /**
69
+ * Actual glyph data. The length of this array won't necessarily be the
70
+ * same size as glyphCount, in cases where lazy font loading is in use.
71
+ */
72
+ protected Glyph[] glyphs;
73
+
74
+ /**
75
+ * Name of the font as seen by Java when it was created.
76
+ * If the font is available, the native version will be used.
77
+ */
78
+ protected String name;
79
+
80
+ /**
81
+ * Postscript name of the font that this bitmap was created from.
82
+ */
83
+ protected String psname;
84
+
85
+ /**
86
+ * The original size of the font when it was first created
87
+ */
88
+ protected int size;
89
+
90
+ /** Default density set to 1 for backwards compatibility with loadFont(). */
91
+ protected int density = 1;
92
+
93
+ /** true if smoothing was enabled for this font, used for native impl */
94
+ protected boolean smooth;
95
+
96
+ /**
97
+ * The ascent of the font. If the 'd' character is present in this PFont,
98
+ * this value is replaced with its pixel height, because the values returned
99
+ * by FontMetrics.getAscent() seem to be terrible.
100
+ */
101
+ protected int ascent;
102
+
103
+ /**
104
+ * The descent of the font. If the 'p' character is present in this PFont,
105
+ * this value is replaced with its lowest pixel height, because the values
106
+ * returned by FontMetrics.getDescent() are gross.
107
+ */
108
+ protected int descent;
109
+
110
+ /**
111
+ * A more efficient array lookup for straight ASCII characters. For Unicode
112
+ * characters, a QuickSort-style search is used.
113
+ */
114
+ protected int[] ascii;
115
+
116
+ /**
117
+ * True if this font is set to load dynamically. This is the default when
118
+ * createFont() method is called without a character set. Bitmap versions of
119
+ * characters are only created when prompted by an index() call.
120
+ */
121
+ protected boolean lazy;
206
122
 
207
- /**
208
- * Create a new image-based font on the fly. If charset is set to null, the
209
- * characters will only be created as bitmaps when they're drawn.
210
- *
211
- * @nowebref
212
- * @param charset array of all unicode chars that should be included
213
- */
214
- public PFont(Font font, boolean smooth, char charset[]) {
215
- // save this so that we can use the native version
216
- this.font = font;
217
- this.smooth = smooth;
218
-
219
- name = font.getName();
220
- psname = font.getPSName();
221
- size = font.getSize();
222
-
223
- // no, i'm not interested in getting off the couch
224
- //lazy = true;
225
- // not sure what else to do here
226
- //mbox2 = 0;
227
- int initialCount = 10;
228
- glyphs = new Glyph[initialCount];
229
-
230
- ascii = new int[128];
231
- Arrays.fill(ascii, -1);
232
-
233
- int mbox3 = size * 3;
234
-
235
- lazyImage = new BufferedImage(mbox3, mbox3, BufferedImage.TYPE_INT_RGB);
236
- lazyGraphics = (Graphics2D) lazyImage.getGraphics();
237
- lazyGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
238
- smooth
239
- ? RenderingHints.VALUE_ANTIALIAS_ON
240
- : RenderingHints.VALUE_ANTIALIAS_OFF);
241
- // adding this for post-1.0.9
242
- lazyGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
243
- smooth
244
- ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
245
- : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
246
-
247
- lazyGraphics.setFont(font);
248
- lazyMetrics = lazyGraphics.getFontMetrics();
249
- lazySamples = new int[mbox3 * mbox3];
250
-
251
- // These values are terrible/unusable. Verified again for Processing 1.1.
252
- // They vary widely per-platform and per-font, so instead we'll use the
253
- // calculate-by-hand method of measuring pixels in characters.
254
- //ascent = lazyMetrics.getAscent();
255
- //descent = lazyMetrics.getDescent();
256
- if (charset == null) {
257
- lazy = true;
123
+ /**
124
+ * Native Java version of the font. If possible, this allows the
125
+ * PGraphics subclass to just use Java's font rendering stuff
126
+ * in situations where that's faster.
127
+ */
128
+ protected Font font;
129
+
130
+ /**
131
+ * True if this font was loaded from an InputStream, rather than by name
132
+ * from the OS. It's best to use the native version of a font loaded from
133
+ * a TTF file, since that will ensure that the font is available when the
134
+ * sketch is exported.
135
+ */
136
+ protected boolean stream;
137
+
138
+ /**
139
+ * True if this font should return 'null' for getFont(), so that the native
140
+ * font will be used to create a subset, but the native version of the font
141
+ * will not be used.
142
+ */
143
+ protected boolean subsetting;
144
+
145
+ /** True if already tried to find the native AWT version of this font. */
146
+ protected boolean fontSearched;
147
+
148
+ /**
149
+ * The name of the font that Java uses when a font isn't found.
150
+ * See {@link #findFont(String)} and {@link #loadFonts()} for more info.
151
+ */
152
+ static protected String systemFontName;
153
+
154
+ /**
155
+ * Array of the native system fonts. Used to lookup native fonts by their
156
+ * PostScript name. This is a workaround for a several year old Apple Java
157
+ * bug that they can't be bothered to fix.
158
+ */
159
+ static protected Font[] fonts;
160
+ static protected HashMap<String, Font> fontDifferent;
161
+
162
+ protected BufferedImage lazyImage;
163
+ protected Graphics2D lazyGraphics;
164
+ protected FontMetrics lazyMetrics;
165
+ protected int[] lazySamples;
166
+
167
+
168
+ /**
169
+ * @nowebref
170
+ */
171
+ public PFont() { } // for subclasses
172
+
173
+
174
+ /**
175
+ * ( begin auto-generated from PFont.xml )
176
+ *
177
+ * PFont is the font class for Processing. To create a font to use with
178
+ * Processing, select "Create Font..." from the Tools menu. This will
179
+ * create a font in the format Processing requires and also adds it to the
180
+ * current sketch's data directory. Processing displays fonts using the
181
+ * .vlw font format, which uses images for each letter, rather than
182
+ * defining them through vector data. The <b>loadFont()</b> function
183
+ * constructs a new font and <b>textFont()</b> makes a font active. The
184
+ * <b>list()</b> method creates a list of the fonts installed on the
185
+ * computer, which is useful information to use with the
186
+ * <b>createFont()</b> function for dynamically converting fonts into a
187
+ * format to use with Processing.
188
+ *
189
+ * ( end auto-generated )
190
+ *
191
+ * @nowebref
192
+ * @param font font the font object to create from
193
+ * @param smooth smooth true to enable smoothing/anti-aliasing
194
+ */
195
+ public PFont(Font font, boolean smooth) {
196
+ this(font, smooth, null);
197
+ }
198
+
199
+
200
+ /**
201
+ * Create a new image-based font on the fly. If charset is set to null,
202
+ * the characters will only be created as bitmaps when they're drawn.
203
+ *
204
+ * @nowebref
205
+ * @param charset array of all unicode chars that should be included
206
+ */
207
+ public PFont(Font font, boolean smooth, char[] charset) {
208
+ // save this so that we can use the native version
209
+ this.font = font;
210
+ this.smooth = smooth;
211
+
212
+ name = font.getName();
213
+ psname = font.getPSName();
214
+ size = font.getSize();
215
+
216
+ // no, i'm not interested in getting off the couch
217
+ //lazy = true;
218
+ // not sure what else to do here
219
+ //mbox2 = 0;
220
+
221
+ int initialCount = 10;
222
+ glyphs = new Glyph[initialCount];
223
+
224
+ ascii = new int[128];
225
+ Arrays.fill(ascii, -1);
226
+
227
+ int mbox3 = size * 3;
228
+
229
+ lazyImage = new BufferedImage(mbox3, mbox3, BufferedImage.TYPE_INT_RGB);
230
+ lazyGraphics = (Graphics2D) lazyImage.getGraphics();
231
+ lazyGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
232
+ smooth ?
233
+ RenderingHints.VALUE_ANTIALIAS_ON :
234
+ RenderingHints.VALUE_ANTIALIAS_OFF);
235
+ // adding this for post-1.0.9
236
+ lazyGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
237
+ smooth ?
238
+ RenderingHints.VALUE_TEXT_ANTIALIAS_ON :
239
+ RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
240
+
241
+ lazyGraphics.setFont(font);
242
+ lazyMetrics = lazyGraphics.getFontMetrics();
243
+ lazySamples = new int[mbox3 * mbox3];
244
+
245
+ // These values are terrible/unusable. Verified again for Processing 1.1.
246
+ // They vary widely per-platform and per-font, so instead we'll use the
247
+ // calculate-by-hand method of measuring pixels in characters.
248
+ //ascent = lazyMetrics.getAscent();
249
+ //descent = lazyMetrics.getDescent();
250
+
251
+ if (charset == null) {
252
+ lazy = true;
258
253
  // lazyFont = font;
259
254
 
260
- } else {
261
- // charset needs to be sorted to make index lookup run more quickly
262
- // http://dev.processing.org/bugs/show_bug.cgi?id=494
263
- Arrays.sort(charset);
264
-
265
- glyphs = new Glyph[charset.length];
266
-
267
- glyphCount = 0;
268
- for (char c : charset) {
269
- if (font.canDisplay(c)) {
270
- Glyph glyf = new Glyph(c);
271
- if (glyf.value < 128) {
272
- ascii[glyf.value] = glyphCount;
273
- }
274
- glyf.index = glyphCount;
275
- glyphs[glyphCount++] = glyf;
276
- }
277
- }
255
+ } else {
256
+ // charset needs to be sorted to make index lookup run more quickly
257
+ // http://dev.processing.org/bugs/show_bug.cgi?id=494
258
+ Arrays.sort(charset);
259
+
260
+ glyphs = new Glyph[charset.length];
261
+
262
+ glyphCount = 0;
263
+ for (char c : charset) {
264
+ if (font.canDisplay(c)) {
265
+ Glyph glyf = new Glyph(c);
266
+ if (glyf.value < 128) {
267
+ ascii[glyf.value] = glyphCount;
268
+ }
269
+ glyf.index = glyphCount;
270
+ glyphs[glyphCount++] = glyf;
271
+ }
272
+ }
278
273
 
279
- // shorten the array if necessary
280
- if (glyphCount != charset.length) {
281
- glyphs = (Glyph[]) PApplet.subset(glyphs, 0, glyphCount);
282
- }
274
+ // shorten the array if necessary
275
+ if (glyphCount != charset.length) {
276
+ glyphs = (Glyph[]) PApplet.subset(glyphs, 0, glyphCount);
277
+ }
283
278
 
284
- // foreign font, so just make ascent the max topExtent
285
- // for > 1.0.9, not doing this anymore.
286
- // instead using getAscent() and getDescent() values for these cases.
279
+ // foreign font, so just make ascent the max topExtent
280
+ // for > 1.0.9, not doing this anymore.
281
+ // instead using getAscent() and getDescent() values for these cases.
287
282
  // if ((ascent == 0) && (descent == 0)) {
288
283
  // //for (int i = 0; i < charCount; i++) {
289
284
  // for (Glyph glyph : glyphs) {
@@ -302,562 +297,576 @@ public class PFont implements PConstants {
302
297
  // }
303
298
  // }
304
299
  // }
305
- }
306
-
307
- // If not already created, just create these two characters to calculate
308
- // the ascent and descent values for the font. This was tested to only
309
- // require 5-10 ms on a 2.4 GHz MacBook Pro.
310
- // In versions 1.0.9 and earlier, fonts that could not display d or p
311
- // used the max up/down values as calculated by looking through the font.
312
- // That's no longer valid with the auto-generating fonts, so we'll just
313
- // use getAscent() and getDescent() in such (minor) cases.
314
- if (ascent == 0) {
315
- if (font.canDisplay('d')) {
316
- new Glyph('d');
317
- } else {
318
- ascent = lazyMetrics.getAscent();
319
- }
320
- }
321
- if (descent == 0) {
322
- if (font.canDisplay('p')) {
323
- new Glyph('p');
324
- } else {
325
- descent = lazyMetrics.getDescent();
326
- }
327
- }
328
300
  }
329
301
 
330
- /**
331
- * Adds an additional parameter that indicates the font came from a file,
332
- * not a built-in OS font.
333
- *
334
- * @nowebref
335
- */
336
- public PFont(Font font, boolean smooth, char charset[],
337
- boolean stream, int density) {
338
- this(font, smooth, charset);
339
- this.stream = stream;
340
- this.density = density;
302
+ // If not already created, just create these two characters to calculate
303
+ // the ascent and descent values for the font. This was tested to only
304
+ // require 5-10 ms on a 2.4 GHz MacBook Pro.
305
+ // In versions 1.0.9 and earlier, fonts that could not display d or p
306
+ // used the max up/down values as calculated by looking through the font.
307
+ // That's no longer valid with the auto-generating fonts, so we'll just
308
+ // use getAscent() and getDescent() in such (minor) cases.
309
+ if (ascent == 0) {
310
+ if (font.canDisplay('d')) {
311
+ new Glyph('d');
312
+ } else {
313
+ ascent = lazyMetrics.getAscent();
314
+ }
315
+ }
316
+ if (descent == 0) {
317
+ if (font.canDisplay('p')) {
318
+ new Glyph('p');
319
+ } else {
320
+ descent = lazyMetrics.getDescent();
321
+ }
341
322
  }
323
+ }
342
324
 
343
- /**
344
- * @nowebref @param input InputStream
345
- */
346
- public PFont(InputStream input) throws IOException {
347
- DataInputStream is = new DataInputStream(input);
348
325
 
349
- // number of character images stored in this font
350
- glyphCount = is.readInt();
326
+ /**
327
+ * Adds an additional parameter that indicates the font came from a file,
328
+ * not a built-in OS font.
329
+ *
330
+ * @nowebref
331
+ */
332
+ public PFont(Font font, boolean smooth, char[] charset,
333
+ boolean stream, int density) {
334
+ this(font, smooth, charset);
335
+ this.stream = stream;
336
+ this.density = density;
337
+ }
351
338
 
352
- // used to be the bitCount, but now used for version number.
353
- // version 8 is any font before 69, so 9 is anything from 83+
354
- // 9 was buggy so gonna increment to 10.
355
- int version = is.readInt();
339
+ /**
340
+ * @throws java.io.IOException
341
+ * @nowebref
342
+ * @param input InputStream
343
+ */
344
+ public PFont(InputStream input) throws IOException {
345
+ DataInputStream is = new DataInputStream(input);
346
+
347
+ // number of character images stored in this font
348
+ glyphCount = is.readInt();
349
+
350
+ // used to be the bitCount, but now used for version number.
351
+ // version 8 is any font before 69, so 9 is anything from 83+
352
+ // 9 was buggy so gonna increment to 10.
353
+ int version = is.readInt();
354
+
355
+ // this was formerly ignored, now it's the actual font size
356
+ //mbox = is.readInt();
357
+ size = is.readInt();
358
+
359
+ // this was formerly mboxY, the one that was used
360
+ // this will make new fonts downward compatible
361
+ is.readInt(); // ignore the other mbox attribute
362
+
363
+ ascent = is.readInt(); // formerly baseHt (zero/ignored)
364
+ descent = is.readInt(); // formerly ignored struct padding
365
+
366
+ // allocate enough space for the character info
367
+ glyphs = new Glyph[glyphCount];
368
+
369
+ ascii = new int[128];
370
+ Arrays.fill(ascii, -1);
371
+
372
+ // read the information about the individual characters
373
+ for (int i = 0; i < glyphCount; i++) {
374
+ Glyph glyph = new Glyph(is);
375
+ // cache locations of the ascii charset
376
+ if (glyph.value < 128) {
377
+ ascii[glyph.value] = i;
378
+ }
379
+ glyph.index = i;
380
+ glyphs[i] = glyph;
381
+ }
356
382
 
357
- // this was formerly ignored, now it's the actual font size
358
- //mbox = is.readInt();
359
- size = is.readInt();
383
+ // not a roman font, so throw an error and ask to re-build.
384
+ // that way can avoid a bunch of error checking hacks in here.
385
+ if ((ascent == 0) && (descent == 0)) {
386
+ throw new RuntimeException("Please use \"Create Font\" to " +
387
+ "re-create this font.");
388
+ }
360
389
 
361
- // this was formerly mboxY, the one that was used
362
- // this will make new fonts downward compatible
363
- is.readInt(); // ignore the other mbox attribute
390
+ for (Glyph glyph : glyphs) {
391
+ glyph.readBitmap(is);
392
+ }
364
393
 
365
- ascent = is.readInt(); // formerly baseHt (zero/ignored)
366
- descent = is.readInt(); // formerly ignored struct padding
394
+ if (version >= 10) { // includes the font name at the end of the file
395
+ name = is.readUTF();
396
+ psname = is.readUTF();
397
+ }
398
+ if (version == 11) {
399
+ smooth = is.readBoolean();
400
+ }
401
+ // See if there's a native version of this font that can be used,
402
+ // in case that's of interest later.
403
+ // findNative();
404
+ }
367
405
 
368
- // allocate enough space for the character info
369
- glyphs = new Glyph[glyphCount];
370
406
 
371
- ascii = new int[128];
372
- Arrays.fill(ascii, -1);
407
+ /**
408
+ * Write this PFont to an OutputStream.
409
+ * <p>
410
+ * This is used by the Create Font tool, or whatever anyone else dreams
411
+ * up for messing with fonts themselves.
412
+ * <p>
413
+ * It is assumed that the calling class will handle closing
414
+ * the stream when finished.
415
+ */
416
+ public void save(OutputStream output) throws IOException {
417
+ DataOutputStream os = new DataOutputStream(output);
373
418
 
374
- // read the information about the individual characters
375
- for (int i = 0; i < glyphCount; i++) {
376
- Glyph glyph = new Glyph(is);
377
- // cache locations of the ascii charset
378
- if (glyph.value < 128) {
379
- ascii[glyph.value] = i;
380
- }
381
- glyph.index = i;
382
- glyphs[i] = glyph;
383
- }
419
+ os.writeInt(glyphCount);
384
420
 
385
- // not a roman font, so throw an error and ask to re-build.
386
- // that way can avoid a bunch of error checking hacks in here.
387
- if ((ascent == 0) && (descent == 0)) {
388
- throw new RuntimeException("Please use \"Create Font\" to "
389
- + "re-create this font.");
390
- }
421
+ if ((name == null) || (psname == null)) {
422
+ name = "";
423
+ psname = "";
424
+ }
391
425
 
392
- for (Glyph glyph : glyphs) {
393
- glyph.readBitmap(is);
394
- }
426
+ os.writeInt(11); // formerly numBits, now used for version number
427
+ os.writeInt(size); // formerly mboxX (was 64, now 48)
428
+ os.writeInt(0); // formerly mboxY, now ignored
429
+ os.writeInt(ascent); // formerly baseHt (was ignored)
430
+ os.writeInt(descent); // formerly struct padding for c version
395
431
 
396
- if (version >= 10) { // includes the font name at the end of the file
397
- name = is.readUTF();
398
- psname = is.readUTF();
399
- }
400
- if (version == 11) {
401
- smooth = is.readBoolean();
402
- }
403
- // See if there's a native version of this font that can be used,
404
- // in case that's of interest later.
405
- // findNative();
432
+ for (int i = 0; i < glyphCount; i++) {
433
+ glyphs[i].writeHeader(os);
406
434
  }
407
435
 
408
- /**
409
- * Write this PFont to an OutputStream.
410
- * <p>
411
- * This is used by the Create Font tool, or whatever anyone else dreams up
412
- * for messing with fonts themselves.
413
- * <p>
414
- * It is assumed that the calling class will handle closing the stream when
415
- * finished.
416
- */
417
- public void save(OutputStream output) throws IOException {
418
- DataOutputStream os = new DataOutputStream(output);
419
-
420
- os.writeInt(glyphCount);
421
-
422
- if ((name == null) || (psname == null)) {
423
- name = "";
424
- psname = "";
425
- }
436
+ for (int i = 0; i < glyphCount; i++) {
437
+ glyphs[i].writeBitmap(os);
438
+ }
426
439
 
427
- os.writeInt(11); // formerly numBits, now used for version number
428
- os.writeInt(size); // formerly mboxX (was 64, now 48)
429
- os.writeInt(0); // formerly mboxY, now ignored
430
- os.writeInt(ascent); // formerly baseHt (was ignored)
431
- os.writeInt(descent); // formerly struct padding for c version
440
+ // version 11
441
+ os.writeUTF(name);
442
+ os.writeUTF(psname);
443
+ os.writeBoolean(smooth);
432
444
 
433
- for (int i = 0; i < glyphCount; i++) {
434
- glyphs[i].writeHeader(os);
435
- }
445
+ os.flush();
446
+ }
436
447
 
437
- for (int i = 0; i < glyphCount; i++) {
438
- glyphs[i].writeBitmap(os);
439
- }
440
448
 
441
- // version 11
442
- os.writeUTF(name);
443
- os.writeUTF(psname);
444
- os.writeBoolean(smooth);
449
+ /**
450
+ * Create a new glyph, and add the character to the current font.
451
+ * @param c character to create an image for.
452
+ */
453
+ protected void addGlyph(char c) {
454
+ Glyph glyph = new Glyph(c);
445
455
 
446
- os.flush();
456
+ if (glyphCount == glyphs.length) {
457
+ glyphs = (Glyph[]) PApplet.expand(glyphs);
458
+ }
459
+ if (glyphCount == 0) {
460
+ glyph.index = 0;
461
+ glyphs[glyphCount] = glyph;
462
+ if (glyph.value < 128) {
463
+ ascii[glyph.value] = 0;
464
+ }
465
+
466
+ } else if (glyphs[glyphCount-1].value < glyph.value) {
467
+ glyphs[glyphCount] = glyph;
468
+ if (glyph.value < 128) {
469
+ ascii[glyph.value] = glyphCount;
470
+ }
471
+
472
+ } else {
473
+ for (int i = 0; i < glyphCount; i++) {
474
+ if (glyphs[i].value > c) {
475
+ for (int j = glyphCount; j > i; --j) {
476
+ glyphs[j] = glyphs[j-1];
477
+ if (glyphs[j].value < 128) {
478
+ ascii[glyphs[j].value] = j;
479
+ }
480
+ }
481
+ glyph.index = i;
482
+ glyphs[i] = glyph;
483
+ // cache locations of the ascii charset
484
+ if (c < 128) ascii[c] = i;
485
+ break;
486
+ }
487
+ }
447
488
  }
489
+ glyphCount++;
490
+ }
448
491
 
449
- /**
450
- * Create a new glyph, and add the character to the current font.
451
- *
452
- * @param c character to create an image for.
453
- */
454
- protected void addGlyph(char c) {
455
- Glyph glyph = new Glyph(c);
456
492
 
457
- if (glyphCount == glyphs.length) {
458
- glyphs = (Glyph[]) PApplet.expand(glyphs);
459
- }
460
- if (glyphCount == 0) {
461
- glyph.index = 0;
462
- glyphs[glyphCount] = glyph;
463
- if (glyph.value < 128) {
464
- ascii[glyph.value] = 0;
465
- }
493
+ public String getName() {
494
+ return name;
495
+ }
466
496
 
467
- } else if (glyphs[glyphCount - 1].value < glyph.value) {
468
- glyphs[glyphCount] = glyph;
469
- if (glyph.value < 128) {
470
- ascii[glyph.value] = glyphCount;
471
- }
472
497
 
473
- } else {
474
- for (int i = 0; i < glyphCount; i++) {
475
- if (glyphs[i].value > c) {
476
- for (int j = glyphCount; j > i; --j) {
477
- glyphs[j] = glyphs[j - 1];
478
- if (glyphs[j].value < 128) {
479
- ascii[glyphs[j].value] = j;
480
- }
481
- }
482
- glyph.index = i;
483
- glyphs[i] = glyph;
484
- // cache locations of the ascii charset
485
- if (c < 128) {
486
- ascii[c] = i;
487
- }
488
- break;
489
- }
490
- }
491
- }
492
- glyphCount++;
493
- }
498
+ public String getPostScriptName() {
499
+ return psname;
500
+ }
494
501
 
495
- public String getName() {
496
- return name;
497
- }
498
502
 
499
- public String getPostScriptName() {
500
- return psname;
501
- }
503
+ /**
504
+ * Set the native complement of this font. Might be set internally via the
505
+ * findFont() function, or externally by a deriveFont() call if the font
506
+ * is resized by PGraphicsJava2D.
507
+ */
508
+ public void setNative(Object font) {
509
+ this.font = (Font) font;
510
+ }
502
511
 
503
- /**
504
- * Set the native complement of this font. Might be set internally via the
505
- * findFont() function, or externally by a deriveFont() call if the font is
506
- * resized by PGraphicsJava2D.
507
- */
508
- public void setNative(Object font) {
509
- this.font = (Font) font;
510
- }
511
512
 
512
- /**
513
- * Use the getNative() method instead, which allows library interfaces to be
514
- * written in a cross-platform fashion for desktop, Android, and others.
515
- */
516
- @Deprecated
517
- public Font getFont() {
518
- return font;
519
- }
513
+ /**
514
+ * Use the getNative() method instead, which allows library interfaces to be
515
+ * written in a cross-platform fashion for desktop, Android, and others.
516
+ */
517
+ @Deprecated
518
+ public Font getFont() {
519
+ return font;
520
+ }
520
521
 
521
- /**
522
- * Return the native java.awt.Font associated with this PFont (if any).
523
- */
524
- public Object getNative() {
525
- if (subsetting) {
526
- return null; // don't return the font for use
527
- }
528
- return font;
529
- }
530
522
 
531
- /**
532
- * Return size of this font.
533
- */
534
- public int getSize() {
535
- return size;
523
+ /**
524
+ * Return the native java.awt.Font associated with this PFont (if any).
525
+ */
526
+ public Object getNative() {
527
+ if (subsetting) {
528
+ return null; // don't return the font for use
536
529
  }
530
+ return font;
531
+ }
532
+
533
+
534
+ /**
535
+ * Return size of this font.
536
+ */
537
+ public int getSize() {
538
+ return size;
539
+ }
540
+
537
541
 
538
542
  // public void setDefaultSize(int size) {
539
543
  // defaultSize = size;
540
544
  // }
541
- /**
542
- * Returns the size that will be used when textFont(font) is called. When
543
- * drawing with 2x pixel density, bitmap fonts in OpenGL need to be created
544
- * (behind the scenes) at double the requested size. This ensures that
545
- * they're shown at half on displays (so folks don't have to change their
546
- * sketch code).
547
- */
548
- public int getDefaultSize() {
549
- //return defaultSize;
550
- return size / density;
551
- }
552
545
 
553
- public boolean isSmooth() {
554
- return smooth;
555
- }
556
546
 
557
- public boolean isStream() {
558
- return stream;
559
- }
547
+ /**
548
+ * Returns the size that will be used when textFont(font) is called.
549
+ * When drawing with 2x pixel density, bitmap fonts in OpenGL need to be
550
+ * created (behind the scenes) at double the requested size. This ensures
551
+ * that they're shown at half on displays (so folks don't have to change
552
+ * their sketch code).
553
+ */
554
+ public int getDefaultSize() {
555
+ //return defaultSize;
556
+ return size / density;
557
+ }
560
558
 
561
- public void setSubsetting() {
562
- subsetting = true;
563
- }
564
559
 
565
- /**
566
- * Attempt to find the native version of this font. (Public so that it can
567
- * be used by OpenGL or other renderers.)
568
- */
569
- public Object findNative() {
570
- if (font == null) {
571
- if (!fontSearched) {
572
- // this font may or may not be installed
573
- font = new Font(name, Font.PLAIN, size);
574
- // if the ps name matches, then we're in fine shape
575
- if (!font.getPSName().equals(psname)) {
576
- // on osx java 1.4 (not 1.3.. ugh), you can specify the ps name
577
- // of the font, so try that in case this .vlw font was created on pc
578
- // and the name is different, but the ps name is found on the
579
- // java 1.4 mac that's currently running this sketch.
580
- font = new Font(psname, Font.PLAIN, size);
581
- }
582
- // check again, and if still bad, screw em
583
- if (!font.getPSName().equals(psname)) {
584
- font = null;
585
- }
586
- fontSearched = true;
587
- }
588
- }
589
- return font;
590
- }
560
+ public boolean isSmooth() {
561
+ return smooth;
562
+ }
591
563
 
592
- public Glyph getGlyph(char c) {
593
- int index = index(c);
594
- return (index == -1) ? null : glyphs[index];
595
- }
596
564
 
597
- /**
598
- * Get index for the character.
599
- *
600
- * @return index into arrays or -1 if not found
601
- */
602
- protected int index(char c) {
603
- if (lazy) {
604
- int index = indexActual(c);
605
- if (index != -1) {
606
- return index;
607
- }
608
- if (font != null && font.canDisplay(c)) {
609
- // create the glyph
610
- addGlyph(c);
611
- // now where did i put that?
612
- return indexActual(c);
613
-
614
- } else {
615
- return -1;
616
- }
565
+ public boolean isStream() {
566
+ return stream;
567
+ }
617
568
 
618
- } else {
619
- return indexActual(c);
569
+
570
+ public void setSubsetting() {
571
+ subsetting = true;
572
+ }
573
+
574
+
575
+ /**
576
+ * Attempt to find the native version of this font.
577
+ * (Public so that it can be used by OpenGL or other renderers.)
578
+ */
579
+ public Object findNative() {
580
+ if (font == null) {
581
+ if (!fontSearched) {
582
+ // this font may or may not be installed
583
+ font = new Font(name, Font.PLAIN, size);
584
+ // if the ps name matches, then we're in fine shape
585
+ if (!font.getPSName().equals(psname)) {
586
+ // on osx java 1.4 (not 1.3.. ugh), you can specify the ps name
587
+ // of the font, so try that in case this .vlw font was created on pc
588
+ // and the name is different, but the ps name is found on the
589
+ // java 1.4 mac that's currently running this sketch.
590
+ font = new Font(psname, Font.PLAIN, size);
620
591
  }
592
+ // check again, and if still bad, screw em
593
+ if (!font.getPSName().equals(psname)) {
594
+ font = null;
595
+ }
596
+ fontSearched = true;
597
+ }
621
598
  }
599
+ return font;
600
+ }
622
601
 
623
- protected int indexActual(char c) {
624
- // degenerate case, but the find function will have trouble
625
- // if there are somehow zero chars in the lookup
626
- //if (value.length == 0) return -1;
627
- if (glyphCount == 0) {
628
- return -1;
629
- }
630
602
 
631
- // quicker lookup for the ascii fellers
632
- if (c < 128) {
633
- return ascii[c];
634
- }
603
+ public Glyph getGlyph(char c) {
604
+ int index = index(c);
605
+ return (index == -1) ? null : glyphs[index];
606
+ }
607
+
635
608
 
636
- // some other unicode char, hunt it out
637
- //return index_hunt(c, 0, value.length-1);
638
- return indexHunt(c, 0, glyphCount - 1);
609
+ /**
610
+ * Get index for the character.
611
+ * @return index into arrays or -1 if not found
612
+ */
613
+ protected int index(char c) {
614
+ if (lazy) {
615
+ int index = indexActual(c);
616
+ if (index != -1) {
617
+ return index;
618
+ }
619
+ if (font != null && font.canDisplay(c)) {
620
+ // create the glyph
621
+ addGlyph(c);
622
+ // now where did i put that?
623
+ return indexActual(c);
624
+
625
+ } else {
626
+ return -1;
627
+ }
628
+
629
+ } else {
630
+ return indexActual(c);
639
631
  }
632
+ }
640
633
 
641
- protected int indexHunt(int c, int start, int stop) {
642
- int pivot = (start + stop) / 2;
643
634
 
644
- // if this is the char, then return it
645
- if (c == glyphs[pivot].value) {
646
- return pivot;
647
- }
635
+ protected int indexActual(char c) {
636
+ // degenerate case, but the find function will have trouble
637
+ // if there are somehow zero chars in the lookup
638
+ //if (value.length == 0) return -1;
639
+ if (glyphCount == 0) return -1;
648
640
 
649
- // char doesn't exist, otherwise would have been the pivot
650
- //if (start == stop) return -1;
651
- if (start >= stop) {
652
- return -1;
653
- }
641
+ // quicker lookup for the ascii fellers
642
+ if (c < 128) return ascii[c];
654
643
 
655
- // if it's in the lower half, continue searching that
656
- if (c < glyphs[pivot].value) {
657
- return indexHunt(c, start, pivot - 1);
658
- }
644
+ // some other unicode char, hunt it out
645
+ //return index_hunt(c, 0, value.length-1);
646
+ return indexHunt(c, 0, glyphCount-1);
647
+ }
659
648
 
660
- // if it's in the upper half, continue there
661
- return indexHunt(c, pivot + 1, stop);
662
- }
663
649
 
664
- /**
665
- * Currently un-implemented for .vlw fonts, but honored for layout in case
666
- * subclasses use it.
667
- */
668
- public float kern(char a, char b) {
669
- return 0;
670
- }
650
+ protected int indexHunt(int c, int start, int stop) {
651
+ int pivot = (start + stop) / 2;
671
652
 
672
- /**
673
- * Returns the ascent of this font from the baseline. The value is based on
674
- * a font of size 1.
675
- */
676
- public float ascent() {
677
- return ((float) ascent / (float) size);
678
- }
653
+ // if this is the char, then return it
654
+ if (c == glyphs[pivot].value) return pivot;
679
655
 
680
- /**
681
- * Returns how far this font descends from the baseline. The value is based
682
- * on a font size of 1.
683
- */
684
- public float descent() {
685
- return ((float) descent / (float) size);
686
- }
656
+ // char doesn't exist, otherwise would have been the pivot
657
+ //if (start == stop) return -1;
658
+ if (start >= stop) return -1;
687
659
 
688
- /**
689
- * Width of this character for a font of size 1.
690
- */
691
- public float width(char c) {
692
- if (c == 32) {
693
- return width('i');
694
- }
660
+ // if it's in the lower half, continue searching that
661
+ if (c < glyphs[pivot].value) return indexHunt(c, start, pivot-1);
695
662
 
696
- int cc = index(c);
697
- if (cc == -1) {
698
- return 0;
699
- }
663
+ // if it's in the upper half, continue there
664
+ return indexHunt(c, pivot+1, stop);
665
+ }
700
666
 
701
- return ((float) glyphs[cc].setWidth / (float) size);
702
- }
703
667
 
704
- //////////////////////////////////////////////////////////////
705
- public int getGlyphCount() {
706
- return glyphCount;
707
- }
668
+ /**
669
+ * Currently un-implemented for .vlw fonts,
670
+ * but honored for layout in case subclasses use it.
671
+ */
672
+ public float kern(char a, char b) {
673
+ return 0;
674
+ }
708
675
 
709
- public Glyph getGlyph(int i) {
710
- return glyphs[i];
711
- }
712
676
 
713
- public PShape getShape(char ch) {
714
- return getShape(ch, 0);
715
- }
677
+ /**
678
+ * Returns the ascent of this font from the baseline.
679
+ * The value is based on a font of size 1.
680
+ */
681
+ public float ascent() {
682
+ return ((float) ascent / (float) size);
683
+ }
716
684
 
717
- public PShape getShape(char ch, float detail) {
718
- Font font = (Font) getNative();
719
- if (font == null) {
720
- throw new IllegalArgumentException("getShape() only works on fonts loaded with createFont()");
721
- }
722
685
 
723
- PShape s = new PShape(PShape.PATH);
724
-
725
- // six element array received from the Java2D path iterator
726
- float[] iterPoints = new float[6];
727
- // array passed to createGylphVector
728
- char[] textArray = new char[]{ch};
729
-
730
- //Graphics2D graphics = (Graphics2D) this.getGraphics();
731
- //FontRenderContext frc = graphics.getFontRenderContext();
732
- @SuppressWarnings("deprecation")
733
- FontRenderContext frc
734
- = Toolkit.getDefaultToolkit().getFontMetrics(font).getFontRenderContext();
735
- GlyphVector gv = font.createGlyphVector(frc, textArray);
736
- Shape shp = gv.getOutline();
737
- // make everything into moveto and lineto
738
- PathIterator iter = (detail == 0)
739
- ? shp.getPathIterator(null)
740
- : // maintain curves
741
- shp.getPathIterator(null, detail); // convert to line segments
742
-
743
- int contours = 0;
744
- //boolean outer = true;
686
+ /**
687
+ * Returns how far this font descends from the baseline.
688
+ * The value is based on a font size of 1.
689
+ */
690
+ public float descent() {
691
+ return ((float) descent / (float) size);
692
+ }
693
+
694
+
695
+ /**
696
+ * Width of this character for a font of size 1.
697
+ */
698
+ public float width(char c) {
699
+ if (c == 32) return width('i');
700
+
701
+ int cc = index(c);
702
+ if (cc == -1) return 0;
703
+
704
+ return ((float) glyphs[cc].setWidth / (float) size);
705
+ }
706
+
707
+
708
+ //////////////////////////////////////////////////////////////
709
+
710
+
711
+ public int getGlyphCount() {
712
+ return glyphCount;
713
+ }
714
+
715
+
716
+ public Glyph getGlyph(int i) {
717
+ return glyphs[i];
718
+ }
719
+
720
+
721
+ public PShape getShape(char ch) {
722
+ return getShape(ch, 0);
723
+ }
724
+
725
+
726
+ public PShape getShape(char ch, float detail) {
727
+ Font font = (Font) getNative();
728
+ if (font == null) {
729
+ throw new IllegalArgumentException("getShape() only works on fonts loaded with createFont()");
730
+ }
731
+
732
+ PShape s = new PShape(PShape.PATH);
733
+
734
+ // six element array received from the Java2D path iterator
735
+ float[] iterPoints = new float[6];
736
+ // array passed to createGylphVector
737
+ char[] textArray = new char[] { ch };
738
+
739
+ //Graphics2D graphics = (Graphics2D) this.getGraphics();
740
+ //FontRenderContext frc = graphics.getFontRenderContext();
741
+ @SuppressWarnings("deprecation")
742
+ FontRenderContext frc =
743
+ Toolkit.getDefaultToolkit().getFontMetrics(font).getFontRenderContext();
744
+ GlyphVector gv = font.createGlyphVector(frc, textArray);
745
+ Shape shp = gv.getOutline();
746
+ // make everything into moveto and lineto
747
+ PathIterator iter = (detail == 0) ?
748
+ shp.getPathIterator(null) : // maintain curves
749
+ shp.getPathIterator(null, detail); // convert to line segments
750
+
751
+ int contours = 0;
752
+ //boolean outer = true;
745
753
  // boolean contour = false;
746
- while (!iter.isDone()) {
747
- int type = iter.currentSegment(iterPoints);
748
- switch (type) {
749
- case PathIterator.SEG_MOVETO: // 1 point (2 vars) in textPoints
754
+ while (!iter.isDone()) {
755
+ int type = iter.currentSegment(iterPoints);
756
+ switch (type) {
757
+ case PathIterator.SEG_MOVETO: // 1 point (2 vars) in textPoints
750
758
  // System.out.println("moveto");
751
759
  // if (!contour) {
752
- if (contours == 0) {
753
- s.beginShape();
754
- } else {
755
- s.beginContour();
760
+ if (contours == 0) {
761
+ s.beginShape();
762
+ } else {
763
+ s.beginContour();
756
764
  // contour = true;
757
- }
758
- contours++;
759
- s.vertex(iterPoints[0], iterPoints[1]);
760
- break;
765
+ }
766
+ contours++;
767
+ s.vertex(iterPoints[0], iterPoints[1]);
768
+ break;
761
769
 
762
- case PathIterator.SEG_LINETO: // 1 point
770
+ case PathIterator.SEG_LINETO: // 1 point
763
771
  // System.out.println("lineto");
764
772
  // PApplet.println(PApplet.subset(iterPoints, 0, 2));
765
- s.vertex(iterPoints[0], iterPoints[1]);
766
- break;
773
+ s.vertex(iterPoints[0], iterPoints[1]);
774
+ break;
767
775
 
768
- case PathIterator.SEG_QUADTO: // 2 points
776
+ case PathIterator.SEG_QUADTO: // 2 points
769
777
  // System.out.println("quadto");
770
778
  // PApplet.println(PApplet.subset(iterPoints, 0, 4));
771
- s.quadraticVertex(iterPoints[0], iterPoints[1],
772
- iterPoints[2], iterPoints[3]);
773
- break;
779
+ s.quadraticVertex(iterPoints[0], iterPoints[1],
780
+ iterPoints[2], iterPoints[3]);
781
+ break;
774
782
 
775
- case PathIterator.SEG_CUBICTO: // 3 points
783
+ case PathIterator.SEG_CUBICTO: // 3 points
776
784
  // System.out.println("cubicto");
777
785
  // PApplet.println(iterPoints);
778
- s.quadraticVertex(iterPoints[0], iterPoints[1],
779
- iterPoints[2], iterPoints[3],
780
- iterPoints[4], iterPoints[5]);
781
- break;
786
+ s.quadraticVertex(iterPoints[0], iterPoints[1],
787
+ iterPoints[2], iterPoints[3],
788
+ iterPoints[4], iterPoints[5]);
789
+ break;
782
790
 
783
- case PathIterator.SEG_CLOSE:
791
+ case PathIterator.SEG_CLOSE:
784
792
  // System.out.println("close");
785
- if (contours > 1) {
793
+ if (contours > 1) {
786
794
  // contours--;
787
795
  // if (contours == 0) {
788
796
  //// s.endShape();
789
797
  // } else {
790
- s.endContour();
791
- }
792
- break;
793
- }
794
- // PApplet.println(iterPoints);
795
- iter.next();
798
+ s.endContour();
796
799
  }
797
- s.endShape(CLOSE);
798
- return s;
800
+ break;
801
+ }
802
+ // PApplet.println(iterPoints);
803
+ iter.next();
799
804
  }
805
+ s.endShape(CLOSE);
806
+ return s;
807
+ }
808
+
809
+
810
+ //////////////////////////////////////////////////////////////
811
+
812
+
813
+ static final char[] EXTRA_CHARS = {
814
+ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
815
+ 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
816
+ 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
817
+ 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
818
+ 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
819
+ 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
820
+ 0x00B0, 0x00B1, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00BA,
821
+ 0x00BB, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5,
822
+ 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD,
823
+ 0x00CE, 0x00CF, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6,
824
+ 0x00D7, 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DF,
825
+ 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
826
+ 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
827
+ 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8,
828
+ 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FF, 0x0102, 0x0103,
829
+ 0x0104, 0x0105, 0x0106, 0x0107, 0x010C, 0x010D, 0x010E, 0x010F,
830
+ 0x0110, 0x0111, 0x0118, 0x0119, 0x011A, 0x011B, 0x0131, 0x0139,
831
+ 0x013A, 0x013D, 0x013E, 0x0141, 0x0142, 0x0143, 0x0144, 0x0147,
832
+ 0x0148, 0x0150, 0x0151, 0x0152, 0x0153, 0x0154, 0x0155, 0x0158,
833
+ 0x0159, 0x015A, 0x015B, 0x015E, 0x015F, 0x0160, 0x0161, 0x0162,
834
+ 0x0163, 0x0164, 0x0165, 0x016E, 0x016F, 0x0170, 0x0171, 0x0178,
835
+ 0x0179, 0x017A, 0x017B, 0x017C, 0x017D, 0x017E, 0x0192, 0x02C6,
836
+ 0x02C7, 0x02D8, 0x02D9, 0x02DA, 0x02DB, 0x02DC, 0x02DD, 0x03A9,
837
+ 0x03C0, 0x2013, 0x2014, 0x2018, 0x2019, 0x201A, 0x201C, 0x201D,
838
+ 0x201E, 0x2020, 0x2021, 0x2022, 0x2026, 0x2030, 0x2039, 0x203A,
839
+ 0x2044, 0x20AC, 0x2122, 0x2202, 0x2206, 0x220F, 0x2211, 0x221A,
840
+ 0x221E, 0x222B, 0x2248, 0x2260, 0x2264, 0x2265, 0x25CA, 0xF8FF,
841
+ 0xFB01, 0xFB02
842
+ };
800
843
 
801
- //////////////////////////////////////////////////////////////
802
- static final char[] EXTRA_CHARS = {
803
- 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
804
- 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
805
- 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
806
- 0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
807
- 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
808
- 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
809
- 0x00B0, 0x00B1, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00BA,
810
- 0x00BB, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5,
811
- 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD,
812
- 0x00CE, 0x00CF, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6,
813
- 0x00D7, 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DF,
814
- 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
815
- 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
816
- 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8,
817
- 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FF, 0x0102, 0x0103,
818
- 0x0104, 0x0105, 0x0106, 0x0107, 0x010C, 0x010D, 0x010E, 0x010F,
819
- 0x0110, 0x0111, 0x0118, 0x0119, 0x011A, 0x011B, 0x0131, 0x0139,
820
- 0x013A, 0x013D, 0x013E, 0x0141, 0x0142, 0x0143, 0x0144, 0x0147,
821
- 0x0148, 0x0150, 0x0151, 0x0152, 0x0153, 0x0154, 0x0155, 0x0158,
822
- 0x0159, 0x015A, 0x015B, 0x015E, 0x015F, 0x0160, 0x0161, 0x0162,
823
- 0x0163, 0x0164, 0x0165, 0x016E, 0x016F, 0x0170, 0x0171, 0x0178,
824
- 0x0179, 0x017A, 0x017B, 0x017C, 0x017D, 0x017E, 0x0192, 0x02C6,
825
- 0x02C7, 0x02D8, 0x02D9, 0x02DA, 0x02DB, 0x02DC, 0x02DD, 0x03A9,
826
- 0x03C0, 0x2013, 0x2014, 0x2018, 0x2019, 0x201A, 0x201C, 0x201D,
827
- 0x201E, 0x2020, 0x2021, 0x2022, 0x2026, 0x2030, 0x2039, 0x203A,
828
- 0x2044, 0x20AC, 0x2122, 0x2202, 0x2206, 0x220F, 0x2211, 0x221A,
829
- 0x221E, 0x222B, 0x2248, 0x2260, 0x2264, 0x2265, 0x25CA, 0xF8FF,
830
- 0xFB01, 0xFB02
831
- };
832
-
833
- /**
834
- * The default Processing character set.
835
- * <P>
836
- * This is the union of the Mac Roman and Windows ANSI (CP1250) character
837
- * sets. ISO 8859-1 Latin 1 is Unicode characters 0x80 -> 0xFF, and would
838
- * seem a good standard, but in practice, most P5 users would rather have
839
- * characters that they expect from their platform's fonts.
840
- * <P>
841
- * This is more of an interim solution until a much better font solution can
842
- * be determined. (i.e. create fonts on the fly from some sort of vector
843
- * format).
844
- * <P>
845
- * Not that I expect that to happen.
846
- */
847
- static public char[] CHARSET;
848
-
849
- static {
850
- CHARSET = new char[126 - 33 + 1 + EXTRA_CHARS.length];
851
- int index = 0;
852
- for (int i = 33; i <= 126; i++) {
853
- CHARSET[index++] = (char) i;
854
- }
855
- for (int i = 0; i < EXTRA_CHARS.length; i++) {
856
- CHARSET[index++] = EXTRA_CHARS[i];
857
- }
858
- }
859
844
 
860
- ;
845
+ /**
846
+ * The default Processing character set.
847
+ * <P>
848
+ * This is the union of the Mac Roman and Windows ANSI (CP1250)
849
+ * character sets. ISO 8859-1 Latin 1 is Unicode characters 0x80 -> 0xFF,
850
+ * and would seem a good standard, but in practice, most P5 users would
851
+ * rather have characters that they expect from their platform's fonts.
852
+ * <P>
853
+ * This is more of an interim solution until a much better
854
+ * font solution can be determined. (i.e. create fonts on
855
+ * the fly from some sort of vector format).
856
+ * <P>
857
+ * Not that I expect that to happen.
858
+ */
859
+ static public char[] CHARSET;
860
+ static {
861
+ CHARSET = new char[126-33+1 + EXTRA_CHARS.length];
862
+ int index = 0;
863
+ for (int i = 33; i <= 126; i++) {
864
+ CHARSET[index++] = (char)i;
865
+ }
866
+ for (int i = 0; i < EXTRA_CHARS.length; i++) {
867
+ CHARSET[index++] = EXTRA_CHARS[i];
868
+ }
869
+ }
861
870
 
862
871
 
863
872
  /**
@@ -876,217 +885,242 @@ public class PFont implements PConstants {
876
885
  * @brief Gets a list of the fonts installed on the system
877
886
  */
878
887
  static public String[] list() {
879
- loadFonts();
880
- String list[] = new String[fonts.length];
881
- for (int i = 0; i < list.length; i++) {
882
- list[i] = fonts[i].getName();
883
- }
884
- return list;
888
+ loadFonts();
889
+ String[] list = new String[fonts.length];
890
+ for (int i = 0; i < list.length; i++) {
891
+ list[i] = fonts[i].getName();
885
892
  }
893
+ return list;
894
+ }
886
895
 
887
- static public void loadFonts() {
888
- if (fonts == null) {
889
- GraphicsEnvironment ge
890
- = GraphicsEnvironment.getLocalGraphicsEnvironment();
891
- fonts = ge.getAllFonts();
892
- if (PApplet.platform == PConstants.MACOSX) {
893
- fontDifferent = new HashMap<String, Font>();
894
- for (Font font : fonts) {
895
- // getName() returns the PostScript name on OS X 10.6 w/ Java 6.
896
- fontDifferent.put(font.getName(), font);
897
- //fontDifferent.put(font.getPSName(), font);
898
- }
899
- }
896
+
897
+ /**
898
+ * Make an internal list of all installed fonts.
899
+ *
900
+ * This can take a while with a lot of fonts installed, but running it on
901
+ * a separate thread may not help much. As of the commit that's adding this
902
+ * note, loadFonts() will only be called by PFont.list() and when loading a
903
+ * font by name, both of which are occasions when we'd need to block until
904
+ * this was finished anyway. It's also possible that running getAllFonts()
905
+ * on a non-EDT thread could cause graphics system issues. Further, the first
906
+ * fonts are usually loaded at the beginning of a sketch, meaning that sketch
907
+ * startup time will still be affected, even with threading in place.
908
+ *
909
+ * Where we're getting killed on font performance is due to this bug:
910
+ * https://bugs.openjdk.java.net/browse/JDK-8179209
911
+ */
912
+ static public void loadFonts() {
913
+ if (fonts == null) {
914
+ GraphicsEnvironment ge =
915
+ GraphicsEnvironment.getLocalGraphicsEnvironment();
916
+ fonts = ge.getAllFonts();
917
+
918
+ if (PApplet.platform == PConstants.MACOS) {
919
+ fontDifferent = new HashMap<>();
920
+ for (Font font : fonts) {
921
+ // No need to use getPSName() anymore because getName()
922
+ // returns the PostScript name on OS X 10.6 w/ Java 6.
923
+ fontDifferent.put(font.getName(), font);
900
924
  }
925
+ }
901
926
  }
927
+ }
902
928
 
903
- /**
904
- * Starting with Java 1.5, Apple broke the ability to specify most fonts.
905
- * This bug was filed years ago as #4769141 at bugreporter.apple.com. More:
906
- * <a href="http://dev.processing.org/bugs/show_bug.cgi?id=407">Bug 407</a>.
907
- */
908
- static public Font findFont(String name) {
909
- loadFonts();
910
- if (PApplet.platform == PConstants.MACOSX) {
911
- Font maybe = fontDifferent.get(name);
912
- if (maybe != null) {
913
- return maybe;
914
- }
915
- // for (int i = 0; i < fonts.length; i++) {
916
- // if (name.equals(fonts[i].getName())) {
917
- // return fonts[i];
918
- // }
919
- // }
920
- }
921
- return new Font(name, Font.PLAIN, 1);
929
+
930
+ /**
931
+ * Starting with Java 1.5, Apple broke the ability to specify most fonts.
932
+ * This bug was filed years ago as #4769141 at bugreporter.apple.com. More:
933
+ * <a href="http://dev.processing.org/bugs/show_bug.cgi?id=407">Bug 407</a>.
934
+ * <br>
935
+ * This function displays a warning when the font is not found
936
+ * and Java's system font is used.
937
+ * See: <a href="https://github.com/processing/processing/issues/5481">issue #5481</a>
938
+ */
939
+ static public Font findFont(String name) {
940
+ if (PApplet.platform == PConstants.MACOS) {
941
+ loadFonts();
942
+ Font maybe = fontDifferent.get(name);
943
+ if (maybe != null) {
944
+ return maybe;
945
+ }
922
946
  }
947
+ Font font = new Font(name, Font.PLAIN, 1);
923
948
 
924
- //////////////////////////////////////////////////////////////
925
- /**
926
- * A single character, and its visage.
927
- */
928
- public class Glyph {
929
-
930
- public PImage image;
931
- public int value;
932
- public int height;
933
- public int width;
934
- public int index;
935
- public int setWidth;
936
- public int topExtent;
937
- public int leftExtent;
938
-
939
- public Glyph() {
940
- index = -1;
941
- // used when reading from a stream or for subclasses
942
- }
949
+ // make sure we have the name of the system fallback font
950
+ if (systemFontName == null) {
951
+ // Figure out what the font is named when things fail
952
+ systemFontName = new Font("", Font.PLAIN, 1).getFontName();
953
+ }
943
954
 
944
- public Glyph(DataInputStream is) throws IOException {
945
- index = -1;
946
- readHeader(is);
947
- }
955
+ // warn the user if they didn't get the font they want
956
+ if (!name.equals(systemFontName) &&
957
+ font.getFontName().equals(systemFontName)) {
958
+ PGraphics.showWarning("\"" + name + "\" is not available, " +
959
+ "so another font will be used. " +
960
+ "Use PFont.list() to show available fonts.");
961
+ }
962
+ return font;
963
+ }
948
964
 
949
- protected void readHeader(DataInputStream is) throws IOException {
950
- value = is.readInt();
951
- height = is.readInt();
952
- width = is.readInt();
953
- setWidth = is.readInt();
954
- topExtent = is.readInt();
955
- leftExtent = is.readInt();
956
-
957
- // pointer from a struct in the c version, ignored
958
- is.readInt();
959
-
960
- // the values for getAscent() and getDescent() from FontMetrics
961
- // seem to be way too large.. perhaps they're the max?
962
- // as such, use a more traditional marker for ascent/descent
963
- if (value == 'd') {
964
- if (ascent == 0) {
965
- ascent = topExtent;
966
- }
967
- }
968
- if (value == 'p') {
969
- if (descent == 0) {
970
- descent = -topExtent + height;
971
- }
972
- }
973
- }
974
965
 
975
- protected void writeHeader(DataOutputStream os) throws IOException {
976
- os.writeInt(value);
977
- os.writeInt(height);
978
- os.writeInt(width);
979
- os.writeInt(setWidth);
980
- os.writeInt(topExtent);
981
- os.writeInt(leftExtent);
982
- os.writeInt(0); // padding
983
- }
966
+ //////////////////////////////////////////////////////////////
967
+
968
+
969
+ /**
970
+ * A single character, and its visage.
971
+ */
972
+ public class Glyph {
973
+ public PImage image;
974
+ public int value;
975
+ public int height;
976
+ public int width;
977
+ public int index;
978
+ public int setWidth;
979
+ public int topExtent;
980
+ public int leftExtent;
981
+
982
+
983
+ public Glyph() {
984
+ index = -1;
985
+ // used when reading from a stream or for subclasses
986
+ }
987
+
988
+
989
+ public Glyph(DataInputStream is) throws IOException {
990
+ index = -1;
991
+ readHeader(is);
992
+ }
993
+
994
+
995
+ protected void readHeader(DataInputStream is) throws IOException {
996
+ value = is.readInt();
997
+ height = is.readInt();
998
+ width = is.readInt();
999
+ setWidth = is.readInt();
1000
+ topExtent = is.readInt();
1001
+ leftExtent = is.readInt();
1002
+
1003
+ // pointer from a struct in the c version, ignored
1004
+ is.readInt();
1005
+
1006
+ // the values for getAscent() and getDescent() from FontMetrics
1007
+ // seem to be way too large.. perhaps they're the max?
1008
+ // as such, use a more traditional marker for ascent/descent
1009
+ if (value == 'd') {
1010
+ if (ascent == 0) ascent = topExtent;
1011
+ }
1012
+ if (value == 'p') {
1013
+ if (descent == 0) descent = -topExtent + height;
1014
+ }
1015
+ }
1016
+
1017
+
1018
+ protected void writeHeader(DataOutputStream os) throws IOException {
1019
+ os.writeInt(value);
1020
+ os.writeInt(height);
1021
+ os.writeInt(width);
1022
+ os.writeInt(setWidth);
1023
+ os.writeInt(topExtent);
1024
+ os.writeInt(leftExtent);
1025
+ os.writeInt(0); // padding
1026
+ }
984
1027
 
985
- protected void readBitmap(DataInputStream is) throws IOException {
986
- image = new PImage(width, height, ALPHA);
987
- int bitmapSize = width * height;
988
1028
 
989
- byte[] temp = new byte[bitmapSize];
990
- is.readFully(temp);
1029
+ protected void readBitmap(DataInputStream is) throws IOException {
1030
+ image = new PImage(width, height, ALPHA);
1031
+ int bitmapSize = width * height;
991
1032
 
992
- // convert the bitmap to an alpha channel
993
- int w = width;
994
- int h = height;
995
- int[] pixels = image.pixels;
996
- for (int y = 0; y < h; y++) {
997
- for (int x = 0; x < w; x++) {
998
- pixels[y * width + x] = temp[y * w + x] & 0xff;
1033
+ byte[] temp = new byte[bitmapSize];
1034
+ is.readFully(temp);
1035
+
1036
+ // convert the bitmap to an alpha channel
1037
+ int w = width;
1038
+ int h = height;
1039
+ int[] pixels = image.pixels;
1040
+ for (int y = 0; y < h; y++) {
1041
+ for (int x = 0; x < w; x++) {
1042
+ pixels[y * width + x] = temp[y*w + x] & 0xff;
999
1043
  // System.out.print((image.pixels[y*64+x] > 128) ? "*" : ".");
1000
- }
1044
+ }
1001
1045
  // System.out.println();
1002
- }
1046
+ }
1003
1047
  // System.out.println();
1004
- }
1005
-
1006
- protected void writeBitmap(DataOutputStream os) throws IOException {
1007
- int[] pixels = image.pixels;
1008
- for (int y = 0; y < height; y++) {
1009
- for (int x = 0; x < width; x++) {
1010
- os.write(pixels[y * width + x] & 0xff);
1011
- }
1012
- }
1013
- }
1048
+ }
1014
1049
 
1015
- protected Glyph(char c) {
1016
- int mbox3 = size * 3;
1017
- lazyGraphics.setColor(Color.white);
1018
- lazyGraphics.fillRect(0, 0, mbox3, mbox3);
1019
- lazyGraphics.setColor(Color.black);
1020
- lazyGraphics.drawString(String.valueOf(c), size, size * 2);
1021
-
1022
- WritableRaster raster = lazyImage.getRaster();
1023
- raster.getDataElements(0, 0, mbox3, mbox3, lazySamples);
1024
-
1025
- int minX = 1000, maxX = 0;
1026
- int minY = 1000, maxY = 0;
1027
- boolean pixelFound = false;
1028
-
1029
- for (int y = 0; y < mbox3; y++) {
1030
- for (int x = 0; x < mbox3; x++) {
1031
- int sample = lazySamples[y * mbox3 + x] & 0xff;
1032
- if (sample != 255) {
1033
- if (x < minX) {
1034
- minX = x;
1035
- }
1036
- if (y < minY) {
1037
- minY = y;
1038
- }
1039
- if (x > maxX) {
1040
- maxX = x;
1041
- }
1042
- if (y > maxY) {
1043
- maxY = y;
1044
- }
1045
- pixelFound = true;
1046
- }
1047
- }
1048
- }
1049
1050
 
1050
- if (!pixelFound) {
1051
- minX = minY = 0;
1052
- maxX = maxY = 0;
1053
- // this will create a 1 pixel white (clear) character..
1054
- // maybe better to set one to -1 so nothing is added?
1055
- }
1051
+ protected void writeBitmap(DataOutputStream os) throws IOException {
1052
+ int[] pixels = image.pixels;
1053
+ for (int y = 0; y < height; y++) {
1054
+ for (int x = 0; x < width; x++) {
1055
+ os.write(pixels[y * width + x] & 0xff);
1056
+ }
1057
+ }
1058
+ }
1056
1059
 
1057
- value = c;
1058
- height = (maxY - minY) + 1;
1059
- width = (maxX - minX) + 1;
1060
- setWidth = lazyMetrics.charWidth(c);
1061
-
1062
- // offset from vertical location of baseline
1063
- // of where the char was drawn (size*2)
1064
- topExtent = size * 2 - minY;
1065
-
1066
- // offset from left of where coord was drawn
1067
- leftExtent = minX - size;
1068
-
1069
- image = new PImage(width, height, ALPHA);
1070
- int[] pixels = image.pixels;
1071
- for (int y = minY; y <= maxY; y++) {
1072
- for (int x = minX; x <= maxX; x++) {
1073
- int val = 255 - (lazySamples[y * mbox3 + x] & 0xff);
1074
- int pindex = (y - minY) * width + (x - minX);
1075
- pixels[pindex] = val;
1076
- }
1077
- }
1078
1060
 
1079
- // replace the ascent/descent values with something.. err, decent.
1080
- if (value == 'd') {
1081
- if (ascent == 0) {
1082
- ascent = topExtent;
1083
- }
1084
- }
1085
- if (value == 'p') {
1086
- if (descent == 0) {
1087
- descent = -topExtent + height;
1088
- }
1089
- }
1061
+ protected Glyph(char c) {
1062
+ int mbox3 = size * 3;
1063
+ lazyGraphics.setColor(Color.white);
1064
+ lazyGraphics.fillRect(0, 0, mbox3, mbox3);
1065
+ lazyGraphics.setColor(Color.black);
1066
+ lazyGraphics.drawString(String.valueOf(c), size, size * 2);
1067
+
1068
+ WritableRaster raster = lazyImage.getRaster();
1069
+ raster.getDataElements(0, 0, mbox3, mbox3, lazySamples);
1070
+
1071
+ int minX = 1000, maxX = 0;
1072
+ int minY = 1000, maxY = 0;
1073
+ boolean pixelFound = false;
1074
+
1075
+ for (int y = 0; y < mbox3; y++) {
1076
+ for (int x = 0; x < mbox3; x++) {
1077
+ int sample = lazySamples[y * mbox3 + x] & 0xff;
1078
+ if (sample != 255) {
1079
+ if (x < minX) minX = x;
1080
+ if (y < minY) minY = y;
1081
+ if (x > maxX) maxX = x;
1082
+ if (y > maxY) maxY = y;
1083
+ pixelFound = true;
1084
+ }
1085
+ }
1086
+ }
1087
+
1088
+ if (!pixelFound) {
1089
+ minX = minY = 0;
1090
+ maxX = maxY = 0;
1091
+ // this will create a 1 pixel white (clear) character..
1092
+ // maybe better to set one to -1 so nothing is added?
1093
+ }
1094
+
1095
+ value = c;
1096
+ height = (maxY - minY) + 1;
1097
+ width = (maxX - minX) + 1;
1098
+ setWidth = lazyMetrics.charWidth(c);
1099
+
1100
+ // offset from vertical location of baseline
1101
+ // of where the char was drawn (size*2)
1102
+ topExtent = size*2 - minY;
1103
+
1104
+ // offset from left of where coord was drawn
1105
+ leftExtent = minX - size;
1106
+
1107
+ image = new PImage(width, height, ALPHA);
1108
+ int[] pixels = image.pixels;
1109
+ for (int y = minY; y <= maxY; y++) {
1110
+ for (int x = minX; x <= maxX; x++) {
1111
+ int val = 255 - (lazySamples[y * mbox3 + x] & 0xff);
1112
+ int pindex = (y - minY) * width + (x - minX);
1113
+ pixels[pindex] = val;
1090
1114
  }
1115
+ }
1116
+
1117
+ // replace the ascent/descent values with something.. err, decent.
1118
+ if (value == 'd') {
1119
+ if (ascent == 0) ascent = topExtent;
1120
+ }
1121
+ if (value == 'p') {
1122
+ if (descent == 0) descent = -topExtent + height;
1123
+ }
1091
1124
  }
1125
+ }
1092
1126
  }