Changeset 1571


Ignore:
Timestamp:
08/10/23 16:16:02 (9 months ago)
Author:
davidb
Message:

More nuanced check for loading in a True-type font

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/gio/swing/SwingFontManager.java

    r1125 r1571  
    2020        /** Singleton instance. */
    2121        private static SwingFontManager _instance = null;
    22        
     22
     23        private static HashMap<String,String> _font_name_lookup = null;
     24   
     25           
    2326        /** Singleton instantiator. */
    2427        public static SwingFontManager getInstance()
    2528        {
    2629                if (_instance == null) _instance = new SwingFontManager();
     30
     31                if (_font_name_lookup == null) {
     32                    _font_name_lookup = new HashMap<String,String>();
     33                   
     34                    java.awt.Font [] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
     35                   
     36                    for (int i=0; i<fonts.length; i++) {
     37                        java.awt.Font font = fonts[i];
     38                        String font_name = font.getFontName();
     39                        String font_family = font.getFamily();
     40
     41                        // Instead of String->Integer hashmap, let's choose to store the
     42                        // font family name, in case this is useful at a later point elsewhere
     43                        _font_name_lookup.put(font_name,font_family);
     44                    }
     45                }
    2746               
    2847                return _instance;
     
    6382                        if (swingFont == null) registerOkay = false;
    6483                } catch (Exception e) {
     84                        System.err.println("Warning: createFont() failed for:\n  " + fontFile.toString());
     85                        e.printStackTrace();
    6586                        registerOkay = false;
    6687                }
     
    7596                        return register(swingFont).clone();
    7697                }
    77                
    78                 // If we made it here the registration failed
     98                else {
     99                    // One reason for failing to register is that it already exists
     100
     101                    if (swingFont != null) {
     102                        String font_name = swingFont.getFontName();
     103                   
     104                        Boolean font_already_registered = (_font_name_lookup.get(font_name) != null);
     105
     106                        // Loading font was OK after all, just that the JDK already has it
     107                        if (font_already_registered) {
     108                            return register(swingFont).clone();
     109                        }
     110                        else {
     111                            System.err.println("Error: failed to register font '" + font_name + "'");
     112                        }
     113                    }
     114                }
     115               
     116
    79117                return null;
    80118        }
Note: See TracChangeset for help on using the changeset viewer.