Dung.Dang@gmail.com | My favorites | Profile | Sign out
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */

/*
Part of the Processing project - http://processing.org

Copyright (c) 2004-10 Ben Fry & Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology

This library is free software; you can redistribute it and/or
modify it under the terms of version 2.01 of the GNU Lesser General
Public License as published by the Free Software Foundation.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/

package processing.core;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;


/**
* Grayscale bitmap font class used by Processing.
* <P>
* Awful (and by that, I mean awesome) ASCII (non-)art for how this works:
* <PRE>
* |
* | height is the full used height of the image
* |
* | ..XX.. }
* | ..XX.. }
* | ...... }
* | XXXX.. } topExtent (top y is baseline - topExtent)
* | ..XX.. }
* | ..XX.. } dotted areas are where the image data
* | ..XX.. } is actually located for the character
* +---XXXXXX---- } (it extends to the right and down
* | for power of two texture sizes)
* ^^^^ leftExtent (amount to move over before drawing the image
*
* ^^^^^^^^^^^^^^ setWidth (width displaced by char)
* </PRE>
*/
public class PFont implements PConstants {

/** Number of character glyphs in this font. */
protected int glyphCount;

/**
* Actual glyph data. The length of this array won't necessarily be the
* same size as glyphCount, in cases where lazy font loading is in use.
*/
protected Glyph[] glyphs;

/**
* Name of the font as seen by Java when it was created.
* If the font is available, the native version will be used.
*/
protected String name;

/**
* Postscript name of the font that this bitmap was created from.
*/
protected String psname;

/**
* The original size of the font when it was first created
*/
protected int size;

/** true if smoothing was enabled for this font, used for native impl */
protected boolean smooth;

/**
* The ascent of the font. If the 'd' character is present in this PFont,
* this value is replaced with its pixel height, because the values returned
* by FontMetrics.getAscent() seem to be terrible.
*/
protected int ascent;

/**
* The descent of the font. If the 'p' character is present in this PFont,
* this value is replaced with its lowest pixel height, because the values
* returned by FontMetrics.getDescent() are gross.
*/
protected int descent;

/**
* A more efficient array lookup for straight ASCII characters. For Unicode
* characters, a QuickSort-style search is used.
*/
protected int[] ascii;

/**
* True if this font is set to load dynamically. This is the default when
* createFont() method is called without a character set. Bitmap versions of
* characters are only created when prompted by an index() call.
*/
protected boolean lazy;

/**
* Native Java version of the font. If possible, this allows the
* PGraphics subclass to just use Java's font rendering stuff
* in situations where that's faster.
*/
protected Font font;

/**
* True if this font was loaded from a stream, rather than from the OS.
* It's always safe to use the native version of a font loaded from a TTF
* file, since that's how it'll look when exported. Otherwise, you'll have
* to use hint(ENABLE_NATIVE_FONTS) to get the native version working with
* renderers that support it.
*/
protected boolean stream;

/**
* True if this font should return 'null' for getFont(), so that the native
* font will be used to create a subset, but the native version of the font
* will not be used.
*/
protected boolean subsetting;

/** True if already tried to find the native AWT version of this font. */
protected boolean fontSearched;

/**
* Array of the native system fonts. Used to lookup native fonts by their
* PostScript name. This is a workaround for a several year old Apple Java
* bug that they can't be bothered to fix.
*/
static protected Font[] fonts;
static protected HashMap<String,Font> fontDifferent;

// /**
// * If not null, this font is set to load dynamically. This is the default
// * when createFont() method is called without a character set. Bitmap
// * versions of characters are only created when prompted by an index() call.
// */
// protected Font lazyFont;
protected BufferedImage lazyImage;
protected Graphics2D lazyGraphics;
protected FontMetrics lazyMetrics;
protected int[] lazySamples;


/** for subclasses that need to store metadata about the font */
protected HashMap<PGraphics, Object> cacheMap;


public PFont() { } // for subclasses


/**
* Create a new Processing font from a native font, but don't create all the
* characters at once, instead wait until they're used to include them.
* @param font
* @param smooth
*/
public PFont(Font font, boolean smooth) {
this(font, smooth, null);
}


/**
* Create a new image-based font on the fly. If charset is set to null,
* the characters will only be created as bitmaps when they're drawn.
*
* @param font the font object to create from
* @param charset array of all unicode chars that should be included
* @param smooth true to enable smoothing/anti-aliasing
*/
public PFont(Font font, boolean smooth, char charset[]) {
// save this so that we can use the native version
this.font = font;
this.smooth = smooth;

name = font.getName();
psname = font.getPSName();
size = font.getSize();

// no, i'm not interested in getting off the couch
//lazy = true;
// not sure what else to do here
//mbox2 = 0;

int initialCount = 10;
glyphs = new Glyph[initialCount];

ascii = new int[128];
Arrays.fill(ascii, -1);

int mbox3 = size * 3;

lazyImage = new BufferedImage(mbox3, mbox3, BufferedImage.TYPE_INT_RGB);
lazyGraphics = (Graphics2D) lazyImage.getGraphics();
lazyGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
smooth ?
RenderingHints.VALUE_ANTIALIAS_ON :
RenderingHints.VALUE_ANTIALIAS_OFF);
// adding this for post-1.0.9
lazyGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
smooth ?
RenderingHints.VALUE_TEXT_ANTIALIAS_ON :
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

lazyGraphics.setFont(font);
lazyMetrics = lazyGraphics.getFontMetrics();
lazySamples = new int[mbox3 * mbox3];

// These values are terrible/unusable. Verified again for Processing 1.1.
// They vary widely per-platform and per-font, so instead we'll use the
// calculate-by-hand method of measuring pixels in characters.
//ascent = lazyMetrics.getAscent();
//descent = lazyMetrics.getDescent();

if (charset == null) {
lazy = true;
// lazyFont = font;

} else {
// charset needs to be sorted to make index lookup run more quickly
// http://dev.processing.org/bugs/show_bug.cgi?id=494
Arrays.sort(charset);

glyphs = new Glyph[charset.length];

glyphCount = 0;
for (char c : charset) {
if (font.canDisplay(c)) {
Glyph glyf = new Glyph(c);
if (glyf.value < 128) {
ascii[glyf.value] = glyphCount;
}
glyf.index = glyphCount;
glyphs[glyphCount++] = glyf;
}
}

// shorten the array if necessary
if (glyphCount != charset.length) {
glyphs = (Glyph[]) PApplet.subset(glyphs, 0, glyphCount);
}

// foreign font, so just make ascent the max topExtent
// for > 1.0.9, not doing this anymore.
// instead using getAscent() and getDescent() values for these cases.
// if ((ascent == 0) && (descent == 0)) {
// //for (int i = 0; i < charCount; i++) {
// for (Glyph glyph : glyphs) {
// char cc = (char) glyph.value;
// //char cc = (char) glyphs[i].value;
// if (Character.isWhitespace(cc) ||
// (cc == '\u00A0') || (cc == '\u2007') || (cc == '\u202F')) {
// continue;
// }
// if (glyph.topExtent > ascent) {
// ascent = glyph.topExtent;
// }
// int d = -glyph.topExtent + glyph.height;
// if (d > descent) {
// descent = d;
// }
// }
// }
}

// If not already created, just create these two characters to calculate
// the ascent and descent values for the font. This was tested to only
// require 5-10 ms on a 2.4 GHz MacBook Pro.
// In versions 1.0.9 and earlier, fonts that could not display d or p
// used the max up/down values as calculated by looking through the font.
// That's no longer valid with the auto-generating fonts, so we'll just
// use getAscent() and getDescent() in such (minor) cases.
if (ascent == 0) {
if (font.canDisplay('d')) {
new Glyph('d');
} else {
ascent = lazyMetrics.getAscent();
}
}
if (descent == 0) {
if (font.canDisplay('p')) {
new Glyph('p');
} else {
descent = lazyMetrics.getDescent();
}
}
}


/**
* Adds an additional parameter that indicates the font came from a file,
* not a built-in OS font.
*/
public PFont(Font font, boolean smooth, char charset[], boolean stream) {
this(font, smooth, charset);
this.stream = stream;
}


public PFont(InputStream input) throws IOException {
DataInputStream is = new DataInputStream(input);

// number of character images stored in this font
glyphCount = is.readInt();

// used to be the bitCount, but now used for version number.
// version 8 is any font before 69, so 9 is anything from 83+
// 9 was buggy so gonna increment to 10.
int version = is.readInt();

// this was formerly ignored, now it's the actual font size
//mbox = is.readInt();
size = is.readInt();

// this was formerly mboxY, the one that was used
// this will make new fonts downward compatible
is.readInt(); // ignore the other mbox attribute

ascent = is.readInt(); // formerly baseHt (zero/ignored)
descent = is.readInt(); // formerly ignored struct padding

// allocate enough space for the character info
glyphs = new Glyph[glyphCount];

ascii = new int[128];
Arrays.fill(ascii, -1);

// read the information about the individual characters
for (int i = 0; i < glyphCount; i++) {
Glyph glyph = new Glyph(is);
// cache locations of the ascii charset
if (glyph.value < 128) {
ascii[glyph.value] = i;
}
glyph.index = i;
glyphs[i] = glyph;
}

// not a roman font, so throw an error and ask to re-build.
// that way can avoid a bunch of error checking hacks in here.
if ((ascent == 0) && (descent == 0)) {
throw new RuntimeException("Please use \"Create Font\" to " +
"re-create this font.");
}

for (Glyph glyph : glyphs) {
glyph.readBitmap(is);
}

if (version >= 10) { // includes the font name at the end of the file
name = is.readUTF();
psname = is.readUTF();
}
if (version == 11) {
smooth = is.readBoolean();
}
// See if there's a native version of this font that can be used,
// in case that's of interest later.
findFont();
}


void delete() {
if (cacheMap != null) {
Set<PGraphics> keySet = cacheMap.keySet();
if (!keySet.isEmpty()) {
Object[] keys = keySet.toArray();
for (int i = 0; i < keys.length; i++) {
Object data = getCache((PGraphics)keys[i]);
Method del = null;

try {
Class<?> c = data.getClass();
del = c.getMethod("delete", new Class[] {});
} catch (Exception e) {}

if (del != null) {
// The metadata have a delete method. We try running it.
try {
del.invoke(data, new Object[] {});
} catch (Exception e) {}
}
}
}
}
}

/**
* Write this PFont to an OutputStream.
* <p>
* This is used by the Create Font tool, or whatever anyone else dreams
* up for messing with fonts themselves.
* <p>
* It is assumed that the calling class will handle closing
* the stream when finished.
*/
public void save(OutputStream output) throws IOException {
DataOutputStream os = new DataOutputStream(output);

os.writeInt(glyphCount);

if ((name == null) || (psname == null)) {
name = "";
psname = "";
}

os.writeInt(11); // formerly numBits, now used for version number
os.writeInt(size); // formerly mboxX (was 64, now 48)
os.writeInt(0); // formerly mboxY, now ignored
os.writeInt(ascent); // formerly baseHt (was ignored)
os.writeInt(descent); // formerly struct padding for c version

for (int i = 0; i < glyphCount; i++) {
glyphs[i].writeHeader(os);
}

for (int i = 0; i < glyphCount; i++) {
glyphs[i].writeBitmap(os);
}

// version 11
os.writeUTF(name);
os.writeUTF(psname);
os.writeBoolean(smooth);

os.flush();
}


/**
* Create a new glyph, and add the character to the current font.
* @param c character to create an image for.
*/
protected void addGlyph(char c) {
Glyph glyph = new Glyph(c);

if (glyphCount == glyphs.length) {
glyphs = (Glyph[]) PApplet.expand(glyphs);
}
if (glyphCount == 0) {
glyph.index = 0;
glyphs[glyphCount] = glyph;
if (glyph.value < 128) {
ascii[glyph.value] = 0;
}

} else if (glyphs[glyphCount-1].value < glyph.value) {
glyphs[glyphCount] = glyph;
if (glyph.value < 128) {
ascii[glyph.value] = glyphCount;
}

} else {
for (int i = 0; i < glyphCount; i++) {
if (glyphs[i].value > c) {
for (int j = glyphCount; j > i; --j) {
glyphs[j] = glyphs[j-1];
if (glyphs[j].value < 128) {
ascii[glyphs[j].value] = j;
}
}
glyph.index = i;
glyphs[i] = glyph;
// cache locations of the ascii charset
if (c < 128) ascii[c] = i;
break;
}
}
}
glyphCount++;
}


public String getName() {
return name;
}


public String getPostScriptName() {
return psname;
}


/**
* Set the native complement of this font. Might be set internally via the
* findFont() function, or externally by a deriveFont() call if the font
* is resized by PGraphicsJava2D.
*/
public void setFont(Font font) {
this.font = font;
}


/**
* Return the native java.awt.Font associated with this PFont (if any).
*/
public Font getFont() {
if (subsetting) {
return null; // don't return the font for use
}
return font;
}


/**
* Return size of this font.
*/
public int getSize() {
return size;
}


public boolean isStream() {
return stream;
}


public void setSubsetting() {
subsetting = true;
}


/**
* Attempt to find the native version of this font.
* (Public so that it can be used by OpenGL or other renderers.)
*/
public Font findFont() {
if (font == null) {
if (!fontSearched) {
// this font may or may not be installed
font = new Font(name, Font.PLAIN, size);
// if the ps name matches, then we're in fine shape
if (!font.getPSName().equals(psname)) {
// on osx java 1.4 (not 1.3.. ugh), you can specify the ps name
// of the font, so try that in case this .vlw font was created on pc
// and the name is different, but the ps name is found on the
// java 1.4 mac that's currently running this sketch.
font = new Font(psname, Font.PLAIN, size);
}
// check again, and if still bad, screw em
if (!font.getPSName().equals(psname)) {
font = null;
}
fontSearched = true;
}
}
return font;
}


public Glyph getGlyph(char c) {
int index = index(c);
return (index == -1) ? null : glyphs[index];
}


/**
* Get index for the character.
* @return index into arrays or -1 if not found
*/
protected int index(char c) {
if (lazy) {
int index = indexActual(c);
if (index != -1) {
return index;
}
if (font.canDisplay(c)) {
// create the glyph
addGlyph(c);
// now where did i put that?
return indexActual(c);

} else {
return -1;
}

} else {
return indexActual(c);
}
}


protected int indexActual(char c) {
// degenerate case, but the find function will have trouble
// if there are somehow zero chars in the lookup
//if (value.length == 0) return -1;
if (glyphCount == 0) return -1;

// quicker lookup for the ascii fellers
if (c < 128) return ascii[c];

// some other unicode char, hunt it out
//return index_hunt(c, 0, value.length-1);
return indexHunt(c, 0, glyphCount-1);
}


protected int indexHunt(int c, int start, int stop) {
int pivot = (start + stop) / 2;

// if this is the char, then return it
if (c == glyphs[pivot].value) return pivot;

// char doesn't exist, otherwise would have been the pivot
//if (start == stop) return -1;
if (start >= stop) return -1;

// if it's in the lower half, continue searching that
if (c < glyphs[pivot].value) return indexHunt(c, start, pivot-1);

// if it's in the upper half, continue there
return indexHunt(c, pivot+1, stop);
}


/**
* Currently un-implemented for .vlw fonts,
* but honored for layout in case subclasses use it.
*/
public float kern(char a, char b) {
return 0;
}


/**
* Returns the ascent of this font from the baseline.
* The value is based on a font of size 1.
*/
public float ascent() {
return ((float) ascent / (float) size);
}


/**
* Returns how far this font descends from the baseline.
* The value is based on a font size of 1.
*/
public float descent() {
return ((float) descent / (float) size);
}


/**
* Width of this character for a font of size 1.
*/
public float width(char c) {
if (c == 32) return width('i');

int cc = index(c);
if (cc == -1) return 0;

return ((float) glyphs[cc].setWidth / (float) size);
}


//////////////////////////////////////////////////////////////

// METADATA REQUIRED BY THE RENDERERS


/**
* Store data of some kind for a renderer that requires extra metadata of
* some kind. Usually this is a renderer-specific representation of the
* font data, for instance a custom OpenGL texture for PGraphicsOpenGL2.
* @param renderer The PGraphics renderer associated to the font
* @param storage The metadata required by the renderer
*/
public void setCache(PGraphics renderer, Object storage) {
if (cacheMap == null) cacheMap = new HashMap<PGraphics, Object>();
cacheMap.put(renderer, storage);
}


/**
* Get cache storage data for the specified renderer. Because each renderer
* will cache data in different formats, it's necessary to store cache data
* keyed by the renderer object. Otherwise, attempting to draw the same
* image to both a PGraphicsJava2D and a PGraphicsOpenGL2 will cause errors.
* @param renderer The PGraphics renderer associated to the font
* @return metadata stored for the specified renderer
*/
public Object getCache(PGraphics renderer) {
if (cacheMap == null) return null;
return cacheMap.get(renderer);
}


/**
* Remove information associated with this renderer from the cache, if any.
* @param parent The PGraphics renderer whose cache data should be removed
*/
public void removeCache(PGraphics renderer) {
if (cacheMap != null) {
cacheMap.remove(renderer);
}
}


//////////////////////////////////////////////////////////////

public int getGlyphCount() {
return glyphCount;
}

public Glyph getGlyph(int i) {
return glyphs[i];
}

//////////////////////////////////////////////////////////////


static final char[] EXTRA_CHARS = {
0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7,
0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF,
0x00B0, 0x00B1, 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00BA,
0x00BB, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5,
0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD,
0x00CE, 0x00CF, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6,
0x00D7, 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DF,
0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7,
0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8,
0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x00FD, 0x00FF, 0x0102, 0x0103,
0x0104, 0x0105, 0x0106, 0x0107, 0x010C, 0x010D, 0x010E, 0x010F,
0x0110, 0x0111, 0x0118, 0x0119, 0x011A, 0x011B, 0x0131, 0x0139,
0x013A, 0x013D, 0x013E, 0x0141, 0x0142, 0x0143, 0x0144, 0x0147,
0x0148, 0x0150, 0x0151, 0x0152, 0x0153, 0x0154, 0x0155, 0x0158,
0x0159, 0x015A, 0x015B, 0x015E, 0x015F, 0x0160, 0x0161, 0x0162,
0x0163, 0x0164, 0x0165, 0x016E, 0x016F, 0x0170, 0x0171, 0x0178,
0x0179, 0x017A, 0x017B, 0x017C, 0x017D, 0x017E, 0x0192, 0x02C6,
0x02C7, 0x02D8, 0x02D9, 0x02DA, 0x02DB, 0x02DC, 0x02DD, 0x03A9,
0x03C0, 0x2013, 0x2014, 0x2018, 0x2019, 0x201A, 0x201C, 0x201D,
0x201E, 0x2020, 0x2021, 0x2022, 0x2026, 0x2030, 0x2039, 0x203A,
0x2044, 0x20AC, 0x2122, 0x2202, 0x2206, 0x220F, 0x2211, 0x221A,
0x221E, 0x222B, 0x2248, 0x2260, 0x2264, 0x2265, 0x25CA, 0xF8FF,
0xFB01, 0xFB02
};


/**
* The default Processing character set.
* <P>
* This is the union of the Mac Roman and Windows ANSI (CP1250)
* character sets. ISO 8859-1 Latin 1 is Unicode characters 0x80 -> 0xFF,
* and would seem a good standard, but in practice, most P5 users would
* rather have characters that they expect from their platform's fonts.
* <P>
* This is more of an interim solution until a much better
* font solution can be determined. (i.e. create fonts on
* the fly from some sort of vector format).
* <P>
* Not that I expect that to happen.
*/
static public char[] CHARSET;
static {
CHARSET = new char[126-33+1 + EXTRA_CHARS.length];
int index = 0;
for (int i = 33; i <= 126; i++) {
CHARSET[index++] = (char)i;
}
for (int i = 0; i < EXTRA_CHARS.length; i++) {
CHARSET[index++] = EXTRA_CHARS[i];
}
};


/**
* Get a list of the fonts installed on the system that can be used
* by Java. Not all fonts can be used in Java, in fact it's mostly
* only TrueType fonts. OpenType fonts with CFF data such as Adobe's
* OpenType fonts seem to have trouble (even though they're sort of
* TrueType fonts as well, or may have a .ttf extension). Regular
* PostScript fonts seem to work OK, however.
* <P>
* Not recommended for use in applets, but this is implemented
* in PFont because the Java methods to access this information
* have changed between 1.1 and 1.4, and the 1.4 method is
* typical of the sort of undergraduate-level over-abstraction
* that the seems to have made its way into the Java API after 1.1.
*/
static public String[] list() {
loadFonts();
String list[] = new String[fonts.length];
for (int i = 0; i < list.length; i++) {
list[i] = fonts[i].getName();
}
return list;
}


static public void loadFonts() {
if (fonts == null) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
fonts = ge.getAllFonts();
if (PApplet.platform == PConstants.MACOSX) {
fontDifferent = new HashMap<String,Font>();
for (Font font : fonts) {
// getName() returns the PostScript name on OS X 10.6 w/ Java 6.
fontDifferent.put(font.getName(), font);
//fontDifferent.put(font.getPSName(), font);
}
}
}
}


/**
* Starting with Java 1.5, Apple broke the ability to specify most fonts.
* This bug was filed years ago as #4769141 at bugreporter.apple.com. More:
* <a href="http://dev.processing.org/bugs/show_bug.cgi?id=407">Bug 407</a>.
*/
static public Font findFont(String name) {
loadFonts();
if (PApplet.platform == PConstants.MACOSX) {
Font maybe = fontDifferent.get(name);
if (maybe != null) {
return maybe;
}
// for (int i = 0; i < fonts.length; i++) {
// if (name.equals(fonts[i].getName())) {
// return fonts[i];
// }
// }
}
return new Font(name, Font.PLAIN, 1);
}


//////////////////////////////////////////////////////////////


/**
* A single character, and its visage.
*/
public class Glyph {
public PImage image;
public int value;
public int height;
public int width;
public int index;
public int setWidth;
public int topExtent;
public int leftExtent;


protected Glyph() {
index = -1;
// used when reading from a stream or for subclasses
}


protected Glyph(DataInputStream is) throws IOException {
index = -1;
readHeader(is);
}


protected void readHeader(DataInputStream is) throws IOException {
value = is.readInt();
height = is.readInt();
width = is.readInt();
setWidth = is.readInt();
topExtent = is.readInt();
leftExtent = is.readInt();

// pointer from a struct in the c version, ignored
is.readInt();

// the values for getAscent() and getDescent() from FontMetrics
// seem to be way too large.. perhaps they're the max?
// as such, use a more traditional marker for ascent/descent
if (value == 'd') {
if (ascent == 0) ascent = topExtent;
}
if (value == 'p') {
if (descent == 0) descent = -topExtent + height;
}
}


protected void writeHeader(DataOutputStream os) throws IOException {
os.writeInt(value);
os.writeInt(height);
os.writeInt(width);
os.writeInt(setWidth);
os.writeInt(topExtent);
os.writeInt(leftExtent);
os.writeInt(0); // padding
}


protected void readBitmap(DataInputStream is) throws IOException {
image = new PImage(width, height, ALPHA);
int bitmapSize = width * height;

byte[] temp = new byte[bitmapSize];
is.readFully(temp);

// convert the bitmap to an alpha channel
int w = width;
int h = height;
int[] pixels = image.pixels;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
pixels[y * width + x] = temp[y*w + x] & 0xff;
// System.out.print((image.pixels[y*64+x] > 128) ? "*" : ".");
}
// System.out.println();
}
// System.out.println();
}


protected void writeBitmap(DataOutputStream os) throws IOException {
int[] pixels = image.pixels;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
os.write(pixels[y * width + x] & 0xff);
}
}
}


protected Glyph(char c) {
int mbox3 = size * 3;
lazyGraphics.setColor(Color.white);
lazyGraphics.fillRect(0, 0, mbox3, mbox3);
lazyGraphics.setColor(Color.black);
lazyGraphics.drawString(String.valueOf(c), size, size * 2);

WritableRaster raster = lazyImage.getRaster();
raster.getDataElements(0, 0, mbox3, mbox3, lazySamples);

int minX = 1000, maxX = 0;
int minY = 1000, maxY = 0;
boolean pixelFound = false;

for (int y = 0; y < mbox3; y++) {
for (int x = 0; x < mbox3; x++) {
int sample = lazySamples[y * mbox3 + x] & 0xff;
if (sample != 255) {
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
pixelFound = true;
}
}
}

if (!pixelFound) {
minX = minY = 0;
maxX = maxY = 0;
// this will create a 1 pixel white (clear) character..
// maybe better to set one to -1 so nothing is added?
}

value = c;
height = (maxY - minY) + 1;
width = (maxX - minX) + 1;
setWidth = lazyMetrics.charWidth(c);

// offset from vertical location of baseline
// of where the char was drawn (size*2)
topExtent = size*2 - minY;

// offset from left of where coord was drawn
leftExtent = minX - size;

image = new PImage(width, height, ALPHA);
int[] pixels = image.pixels;
for (int y = minY; y <= maxY; y++) {
for (int x = minX; x <= maxX; x++) {
int val = 255 - (lazySamples[y * mbox3 + x] & 0xff);
int pindex = (y - minY) * width + (x - minX);
pixels[pindex] = val;
}
}

// replace the ascent/descent values with something.. err, decent.
if (value == 'd') {
if (ascent == 0) ascent = topExtent;
}
if (value == 'p') {
if (descent == 0) descent = -topExtent + height;
}
}
}
}

Change log

r7659 by andres.colubri on Feb 06, 2011   Diff
Removed PMetadata class
Go to: 

Older revisions

r7647 by andres.colubri on Jan 31, 2011   Diff
Merged-in the changes in the core
needed for the new opengl renderer
r7430 by f...@processing.org on Nov 15, 2010   Diff
add subsetting mode.. this needs a
better name
r7149 by f...@processing.org on Aug 06, 2010   Diff
working on how native fonts are
handled for better consistency
(incomplete)
All revisions of this file

File info

Size: 30684 bytes, 1009 lines
Powered by Google Project Hosting