picrate 0.0.2-java

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