1 package org.joshy.html.css; 2 3 import java.awt.*; 4 import org.joshy.html.Context; 5 import org.joshy.u; 6 import java.util.HashMap ; 7 8 public class FontResolverTest extends FontResolver { 9 String [] available_fonts; 10 HashMap instance_hash; 11 HashMap available_fonts_hash; 12 public FontResolverTest() { 13 GraphicsEnvironment gfx = GraphicsEnvironment.getLocalGraphicsEnvironment(); 14 String [] available_fonts = gfx.getAvailableFontFamilyNames(); 15 instance_hash = new HashMap (); 18 19 available_fonts_hash = new HashMap (); 24 for(int i=0; i<available_fonts.length; i++) { 25 available_fonts_hash.put(available_fonts[i],new String ()); 26 } 27 28 available_fonts_hash.put("Serif",new Font("Serif",Font.PLAIN,1)); 30 available_fonts_hash.put("SansSerif",new Font("SansSerif",Font.PLAIN,1)); 31 available_fonts_hash.put("Monospaced",new Font("Monospaced",Font.PLAIN,1)); 33 } 34 35 protected Font createFont(Font root_font, float size, String weight, String style) { 36 int font_const = Font.PLAIN; 37 if(weight != null && weight.equals("bold")) { 38 font_const = font_const | Font.BOLD; 39 } 40 if(style != null && style.equals("italic")) { 41 font_const = font_const | Font.ITALIC; 42 } 43 44 Font fnt = root_font.deriveFont(font_const,size); 45 return fnt; 46 } 47 48 protected String getFontInstanceHashName(String name, float size, String weight, String style) { 49 return name + "-" + size + "-" + weight + "-" + style; 50 } 51 52 protected Font resolveFont(Context c, String font, float size, String weight, String style) { 53 if(font.equals("serif")) { 55 font = "Serif"; 56 } 57 if(font.equals("sans-serif")) { 58 font = "SansSerif"; 59 } 60 if(font.equals("monospace")) { 61 font = "Monospaced"; 62 } 63 64 65 String font_instance_name = getFontInstanceHashName(font,size,weight,style); 67 if(instance_hash.containsKey(font_instance_name)) { 70 return (Font) instance_hash.get(font_instance_name); 72 } 73 74 77 if(available_fonts_hash.containsKey(font)) { 80 Object value = available_fonts_hash.get(font); 81 Font root_font = null; 83 if(value instanceof Font) { 84 root_font = (Font)value; 85 } else { 86 root_font = new Font(font,Font.PLAIN,1); 87 available_fonts_hash.put(font,root_font); 88 } 89 90 Font fnt = createFont(root_font,size,weight,style); 92 93 instance_hash.put(font_instance_name,fnt); 95 return fnt; 96 } 97 98 return null; 100 } 101 102 public Font resolveFont(Context c, String [] families, float size, String weight, String style) { 103 104 if(families != null) { 106 for(int i=0; i<families.length; i++) { 107 Font font = resolveFont(c,families[i],size,weight,style); 108 if(font != null) { return font; } 109 } 110 } 111 112 try { 115 Font fnt = createFont((Font)available_fonts_hash.get("SansSerif"),size,weight,style); 116 instance_hash.put(getFontInstanceHashName("SansSerif",size,weight,style),fnt); 117 return fnt; 119 } catch (Exception ex) { 120 u.p("exception: " + ex); 121 return c.getGraphics().getFont(); 122 } 123 124 } 125 } 126 127 | Popular Tags |