2828import java .io .*;
2929import java .util .Arrays ;
3030import java .util .HashMap ;
31+ import java .util .Set ;
3132
3233
3334/**
@@ -154,7 +155,11 @@ public class PFont implements PConstants {
154155 protected FontMetrics lazyMetrics ;
155156 protected int [] lazySamples ;
156157
158+
159+ /** for subclasses that need to store metadata about the font */
160+ protected HashMap <PGraphics , PMetadata > cacheMap ;
157161
162+
158163 public PFont () { } // for subclasses
159164
160165
@@ -239,7 +244,8 @@ public PFont(Font font, boolean smooth, char charset[]) {
239244 if (glyf .value < 128 ) {
240245 ascii [glyf .value ] = glyphCount ;
241246 }
242- glyphs [glyphCount ++] = glyf ;
247+ glyf .index = glyphCount ;
248+ glyphs [glyphCount ++] = glyf ;
243249 }
244250 }
245251
@@ -340,6 +346,7 @@ public PFont(InputStream input) throws IOException {
340346 if (glyph .value < 128 ) {
341347 ascii [glyph .value ] = i ;
342348 }
349+ glyph .index = i ;
343350 glyphs [i ] = glyph ;
344351 }
345352
@@ -366,6 +373,19 @@ public PFont(InputStream input) throws IOException {
366373 findFont ();
367374 }
368375
376+
377+ void delete () {
378+ if (cacheMap != null ) {
379+ Set <PGraphics > keySet = cacheMap .keySet ();
380+ if (!keySet .isEmpty ()) {
381+ Object [] keys = keySet .toArray ();
382+ for (int i = 0 ; i < keys .length ; i ++) {
383+ PMetadata data = getCache ((PGraphics )keys [i ]);
384+ data .delete ();
385+ }
386+ }
387+ }
388+ }
369389
370390 /**
371391 * Write this PFont to an OutputStream.
@@ -420,6 +440,7 @@ protected void addGlyph(char c) {
420440 glyphs = (Glyph []) PApplet .expand (glyphs );
421441 }
422442 if (glyphCount == 0 ) {
443+ glyph .index = 0 ;
423444 glyphs [glyphCount ] = glyph ;
424445 if (glyph .value < 128 ) {
425446 ascii [glyph .value ] = 0 ;
@@ -440,6 +461,7 @@ protected void addGlyph(char c) {
440461 ascii [glyphs [j ].value ] = j ;
441462 }
442463 }
464+ glyph .index = i ;
443465 glyphs [i ] = glyph ;
444466 // cache locations of the ascii charset
445467 if (c < 128 ) ascii [c ] = i ;
@@ -481,6 +503,14 @@ public Font getFont() {
481503 return font ;
482504 }
483505
506+
507+ /**
508+ * Return size of this font.
509+ */
510+ public int getSize () {
511+ return size ;
512+ }
513+
484514
485515 public boolean isStream () {
486516 return stream ;
@@ -625,6 +655,59 @@ public float width(char c) {
625655 }
626656
627657
658+ //////////////////////////////////////////////////////////////
659+
660+ // METADATA REQUIRED BY THE RENDERERS
661+
662+
663+ /**
664+ * Store data of some kind for a renderer that requires extra metadata of
665+ * some kind. Usually this is a renderer-specific representation of the
666+ * font data, for instance a custom OpenGL texture for PGraphicsOpenGL2.
667+ * @param renderer The PGraphics renderer associated to the font
668+ * @param storage The metadata required by the renderer
669+ */
670+ public void setCache (PGraphics renderer , PMetadata storage ) {
671+ if (cacheMap == null ) cacheMap = new HashMap <PGraphics , PMetadata >();
672+ cacheMap .put (renderer , storage );
673+ }
674+
675+
676+ /**
677+ * Get cache storage data for the specified renderer. Because each renderer
678+ * will cache data in different formats, it's necessary to store cache data
679+ * keyed by the renderer object. Otherwise, attempting to draw the same
680+ * image to both a PGraphicsJava2D and a PGraphicsOpenGL2 will cause errors.
681+ * @param renderer The PGraphics renderer associated to the font
682+ * @return metadata stored for the specified renderer
683+ */
684+ public PMetadata getCache (PGraphics renderer ) {
685+ if (cacheMap == null ) return null ;
686+ return cacheMap .get (renderer );
687+ }
688+
689+
690+ /**
691+ * Remove information associated with this renderer from the cache, if any.
692+ * @param parent The PGraphics renderer whose cache data should be removed
693+ */
694+ public void removeCache (PGraphics renderer ) {
695+ if (cacheMap != null ) {
696+ cacheMap .remove (renderer );
697+ }
698+ }
699+
700+
701+ //////////////////////////////////////////////////////////////
702+
703+ public int getGlyphCount () {
704+ return glyphCount ;
705+ }
706+
707+ public Glyph getGlyph (int i ) {
708+ return glyphs [i ];
709+ }
710+
628711 //////////////////////////////////////////////////////////////
629712
630713
@@ -757,21 +840,24 @@ static public Font findFont(String name) {
757840 * A single character, and its visage.
758841 */
759842 public class Glyph {
760- PImage image ;
761- int value ;
762- int height ;
763- int width ;
764- int setWidth ;
765- int topExtent ;
766- int leftExtent ;
767-
843+ public PImage image ;
844+ public int value ;
845+ public int height ;
846+ public int width ;
847+ public int index ;
848+ public int setWidth ;
849+ public int topExtent ;
850+ public int leftExtent ;
851+
768852
769853 protected Glyph () {
854+ index = -1 ;
770855 // used when reading from a stream or for subclasses
771856 }
772857
773858
774859 protected Glyph (DataInputStream is ) throws IOException {
860+ index = -1 ;
775861 readHeader (is );
776862 }
777863
0 commit comments