View Javadoc

1   package org.catacomb.druid.xtext.canvas;
2   
3   import org.catacomb.report.E;
4   
5   import java.util.HashMap;
6   
7   import java.awt.Font;
8   import java.awt.Graphics;
9   
10  
11  public class FontStore {
12  
13      HashMap<String, Font> fontHM;
14  
15      Font activeFont;
16      Font defaultFont;
17      Font defaultItalicFont;
18  
19      Font h2Font;
20      Font boldFont;
21  
22      static FontStore p_instance;
23  
24  
25  
26  
27      private FontStore() {
28          fontHM = new HashMap<String, Font>();
29          activeFont = new Font("sanserif", Font.PLAIN, 12);
30          h2Font = new Font("sanserif", Font.BOLD, 14);
31          boldFont = new Font("sanserif", Font.BOLD, 12);
32      }
33  
34      // any reason not to be a singleton?
35      public static FontStore instance() {
36          if (p_instance == null) {
37              p_instance = new FontStore();
38          }
39          return p_instance;
40      }
41  
42  
43      public Font getFont(String fam, String sty, String siz) {
44          String key = fam + "-" + sty + "-" + siz;
45  
46          int isiz = 12;
47          if (siz == null) {
48              E.warning("null size in get font ");
49          } else {
50  
51              isiz = Integer.parseInt(siz);
52          }
53  
54          int isty = Font.PLAIN;
55          if (sty == null) {
56              E.warning("null style ");
57          } else if (sty.equals("plain")) {
58  
59          } else if (sty.equals("bold")) {
60              isty = Font.BOLD;
61  
62          } else if (sty.equals("italic")) {
63              isty = Font.ITALIC;
64  
65          } else {
66              E.error("unrecognized style " + sty);
67          }
68  
69  
70          return getFont(fam, isty, isiz, key);
71      }
72  
73  
74      public Font getFont(String famin, int isty, int isiz, String key) {
75          String fam = famin;
76          Font ret = null;
77          if (fontHM.containsKey(key)) {
78              ret = fontHM.get(key);
79          } else {
80  
81  
82              if (fam == null) {
83                  E.warning("null family in get font ");
84                  fam = "serif";
85              }
86  
87              ret = new Font(fam, isty, isiz);
88              if (key != null) {
89                  fontHM.put(key, ret);
90              }
91          }
92          return ret;
93      }
94  
95  
96      public int stringWidth(Graphics g, String text) {
97          return g.getFontMetrics().stringWidth(text);
98      }
99  
100 
101     public Font getActiveFont() {
102         return activeFont;
103     }
104 
105     public Font getDefaultFont() {
106         if (defaultFont == null) {
107             defaultFont = getFont("sansserif", Font.PLAIN, 12, null);
108         }
109         return defaultFont;
110     }
111 
112 
113     public Font getDefaultItalicFont() {
114         if (defaultItalicFont == null) {
115             defaultItalicFont = getFont("sansserif", Font.ITALIC, 12, null);
116         }
117         return defaultItalicFont;
118     }
119 
120 
121     public Font getH2Font() {
122         return h2Font;
123     }
124 
125     public Font getBoldFont() {
126         return boldFont;
127     }
128 
129 }