1 14 package org.compiere.print; 15 16 import java.awt.*; 17 import java.awt.font.*; 18 import java.util.*; 19 20 import org.compiere.model.*; 21 import org.compiere.util.*; 22 23 29 public class MPrintFont extends X_AD_PrintFont 30 { 31 36 private MPrintFont(Properties ctx, int AD_PrintFont_ID) 37 { 38 super (ctx, AD_PrintFont_ID); 39 if (AD_PrintFont_ID == 0) 40 setIsDefault(false); 41 } 43 44 private Font m_cacheFont = null; 45 46 47 48 52 public Font getFont() 53 { 54 if (m_cacheFont != null) 55 return m_cacheFont; 56 String code = (String )getValue("Code"); 57 if (code == null || code.equals(".")) 58 m_cacheFont = new Font (null); 59 try 60 { 61 if (code != null && !code.equals(".")) 62 m_cacheFont = Font.decode(code); 64 } 65 catch (Exception e) 66 { 67 Log.error("MPrintFont.getFont", e); 68 } 69 if (code == null) 70 m_cacheFont = new Font (null); return m_cacheFont; 73 } 75 79 public void setFont (Font font) 80 { 81 StringBuffer sb = new StringBuffer (); 83 sb.append(font.getFamily()).append("-"); 84 int style = font.getStyle(); 85 if (style == Font.PLAIN) 86 sb.append("PLAIN"); 87 else if (style == Font.BOLD) 88 sb.append("BOLD"); 89 else if (style == Font.ITALIC) 90 sb.append("ITALIC"); 91 else if (style == (Font.BOLD + Font.ITALIC)) 92 sb.append("BOLDITALIC"); 93 sb.append("-").append(font.getSize()); 94 setCode(sb.toString()); 95 } 97 98 99 104 static MPrintFont create (Font font) 105 { 106 MPrintFont pf = new MPrintFont(Env.getCtx(), 0); 107 StringBuffer name = new StringBuffer (font.getName()); 108 if (font.isBold()) 109 name.append(" bold"); 110 if (font.isItalic()) 111 name.append(" italic"); 112 name.append(" ").append(font.getSize()); 113 pf.setName(name.toString()); 114 pf.setFont(font); 115 pf.save(); 116 return pf; 117 } 119 123 public String toString() 124 { 125 StringBuffer sb = new StringBuffer ("MPrintFont["); 126 sb.append("ID=").append(getID()) 127 .append(",Name=").append(getName()) 128 .append("PSName=").append(getFont().getPSName()) 129 .append(getFont()) 130 .append("]"); 131 return sb.toString(); 132 } 134 139 public String toPS() 140 { 141 StringBuffer sb = new StringBuffer ("/"); 142 sb.append(getFont().getPSName()); 143 if (getFont().isBold()) 144 sb.append(" Bold"); 145 if (getFont().isItalic()) 146 sb.append(" Italic"); 147 sb.append(" ").append(getFont().getSize()) 148 .append(" selectfont"); 149 return sb.toString(); 150 } 152 156 static void dump (Font font) 157 { 158 System.out.println("Family=" + font.getFamily()); 159 System.out.println("FontName=" + font.getFontName()); 160 System.out.println("Name=" + font.getName()); 161 System.out.println("PSName=" + font.getPSName()); 162 System.out.println("Style=" + font.getStyle()); 163 System.out.println("Size=" + font.getSize()); 164 System.out.println("Attributes:"); 165 Map map = font.getAttributes(); 166 Iterator keys = map.keySet().iterator(); 167 while (keys.hasNext()) 168 { 169 Object key = keys.next(); 170 Object value = map.get(key); 171 System.out.println(" - " + key + "=" + value); 172 } 173 System.out.println(font); 174 } 176 177 178 179 static private CCache s_fonts = new CCache("printFonts", 20); 180 181 186 static public MPrintFont get (int AD_PrintFont_ID) 187 { 188 Integer key = new Integer (AD_PrintFont_ID); 189 MPrintFont pf = (MPrintFont)s_fonts.get(key); 190 if (pf == null) 191 { 192 pf = new MPrintFont (Env.getCtx(), AD_PrintFont_ID); 193 s_fonts.put(key, pf); 194 } 195 return pf; 196 } 198 199 200 204 public static void main(String [] args) 205 { 206 System.out.println("Available Fonts:"); 207 String [] family = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 208 for (int i = 0; i < family.length; i++) 209 System.out.println(" - " + family[i]); 210 211 org.compiere.Compiere.startupClient(); 212 MPrintFont pf = new MPrintFont(Env.getCtx(), 100); 213 dump( pf.getFont() ); 214 215 String [] systemLocical = new String [] {"Dialog", "DialogInput", "Monospaced", "Serif", "SansSerif"}; 216 for (int i = 0; i < systemLocical.length; i++) 217 { 218 } 232 233 int[] IDs = PO.getAllIDs ("AD_PrintFont", null); 235 for (int i = 0; i < IDs.length; i++) 236 { 237 pf = new MPrintFont(Env.getCtx(), IDs[i]); 238 System.out.println(IDs[i] + " = " + pf.getFont()); 239 } 240 241 } } | Popular Tags |