Changeset 836


Ignore:
Timestamp:
02/04/14 23:59:48 (10 years ago)
Author:
davidb
Message:

Code change to load custom fonts in their new FontFamily sub-directory structure

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/Text.java

    r812 r836  
    2424import java.awt.geom.Rectangle2D;
    2525import java.io.File;
     26import java.io.IOException;
    2627import java.text.AttributedString;
    2728import java.util.Collection;
     
    144145        private Font _font;
    145146       
     147       
     148        protected static void InitFontFamily(GraphicsEnvironment ge, File fontFamilyDir)
     149        {
     150                File[] fontFiles = fontFamilyDir.listFiles();
     151
     152                for (File fontFile : fontFiles) {
     153                        String ext = "";
     154                        String fileName = fontFile.getName().toLowerCase();
     155
     156                        int i = fileName.lastIndexOf('.');
     157                        int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
     158
     159                        if (i > p) {
     160                                ext = fileName.substring(i+1);
     161                        }
     162
     163                        if (ext.equals("ttf")) {
     164
     165                                try {
     166                                        Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
     167
     168                                        ge.registerFont(font);
     169                                }
     170                                catch (Exception e) {
     171                                        System.err.println("Failed to load custon font file: " + fontFile);
     172                                }
     173                        }
     174                }
     175        }
     176       
    146177        public static void InitFonts() {
    147                 try {
    148                         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    149 
    150                         if (ge != null) {
    151                                
    152                             File fontDirectory = new File(FrameIO.FONT_PATH);
    153                             if (fontDirectory != null) {
    154                                 File[] fontFiles = fontDirectory.listFiles();
    155                                 if (fontFiles != null) {
    156    
    157                                     if (fontFiles.length>0) {
    158                                         System.out.println("Loading custom fonts:");
    159                                     }
    160    
    161                                     boolean first_item = true;                             
    162                                     for (File fontFile : fontFiles) {
    163                                         String ext = "";
    164                                         String fileName = fontFile.getName().toLowerCase();
    165                                
    166                                         int i = fileName.lastIndexOf('.');
    167                                 int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
    168                                
    169                                 if (i > p) {
    170                                     ext = fileName.substring(i+1);
    171                                 }
    172        
    173                                                 if (ext.equals("ttf")) {
    174                                                     if (first_item) {
    175                                                         System.out.print("  " + fontFile.getName());
    176                                                     }
    177                                                     else {
    178                                                         System.out.print(", " + fontFile.getName());
    179                                                     }
    180                                                     System.out.flush();
    181        
    182                                                     Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
    183                                                    
    184                                                     ge.registerFont(font);
    185                                                     first_item = false;
    186                                                 }
    187                                     }
    188                                     System.out.println();
    189    
    190                                 }
    191                             }
    192                         }
    193                         else {
    194                                 System.err.println("No graphics environment detected.  Skipping the loading of the custom fonts");
    195                         }
    196                 }
    197                 catch (Exception e) {
    198                         System.err.println("Failed to load custom fonts");
    199                         e.printStackTrace();
    200                 }
    201         }
     178               
     179                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     180
     181                if (ge != null) {
     182
     183                        File fontDirectory = new File(FrameIO.FONT_PATH);
     184                        if (fontDirectory != null) {
     185                                File[] fontFamilyDirs = fontDirectory.listFiles();
     186                                if (fontFamilyDirs != null) {
     187
     188                                        if (fontFamilyDirs.length>0) {
     189                                                System.out.println("Loading custom fonts:");
     190                                        }
     191
     192                                        boolean first_item = true;             
     193                                        for (File fontFamilyDir : fontFamilyDirs) {
     194                                                if (fontFamilyDir.isDirectory()) {
     195
     196                                                        if (first_item) {
     197                                                                System.out.print("  " + fontFamilyDir.getName());
     198                                                                first_item = false;
     199                                                        }
     200                                                        else {
     201                                                                System.out.print(", " + fontFamilyDir.getName());
     202                                                        }
     203                                                        System.out.flush();
     204
     205                                                        InitFontFamily(ge,fontFamilyDir);
     206                                                }
     207                                        }
     208                                        System.out.println();     
     209                                }
     210                        }
     211                }
     212                else {
     213                        System.err.println("No graphics environment detected.  Skipping the loading of the custom fonts");
     214                }
     215        }
     216
     217       
    202218
    203219        /**
Note: See TracChangeset for help on using the changeset viewer.