View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.microemu.device.swt;
21  
22  import java.net.URL;
23  import java.util.Enumeration;
24  import java.util.Hashtable;
25  
26  import javax.microedition.lcdui.Font;
27  
28  import org.eclipse.swt.graphics.FontMetrics;
29  import org.microemu.app.ui.swt.SwtDeviceComponent;
30  import org.microemu.device.impl.FontManagerImpl;
31  
32  public class SwtFontManager implements FontManagerImpl {
33  
34  	static String FACE_SYSTEM_NAME = "SansSerif";
35  
36  	static String FACE_MONOSPACE_NAME = "Monospaced";
37  
38  	static String FACE_PROPORTIONAL_NAME = "SansSerif";
39  
40  	static int SIZE_SMALL = 6;
41  
42  	static int SIZE_MEDIUM = 8;
43  
44  	static int SIZE_LARGE = 10;
45  
46  	private Hashtable fonts = new Hashtable();
47  
48  	private boolean antialiasing;
49  
50  	org.microemu.device.impl.Font getFont(Font meFont) {
51      	int key = 0;
52      	key |= meFont.getFace();
53      	key |= meFont.getStyle();
54      	key |= meFont.getSize();
55      	
56      	org.microemu.device.impl.Font result = (org.microemu.device.impl.Font) fonts.get(new Integer(key));
57  	    
58  	    if (result == null) {
59  	    	String name = null;
60  	    	if (meFont.getFace() == Font.FACE_SYSTEM) {
61  	    		name = FACE_SYSTEM_NAME;
62  	    	} else if (meFont.getFace() == Font.FACE_MONOSPACE) {
63  	    		name = FACE_MONOSPACE_NAME;
64  	    	} else if (meFont.getFace() == Font.FACE_PROPORTIONAL) {
65  	    		name = FACE_PROPORTIONAL_NAME;
66  	    	}
67  	    	String style = ",";
68  	    	if ((meFont.getStyle() & Font.STYLE_PLAIN) != 0) {
69  	    		style += "plain,";
70  	    	}
71  	    	if ((meFont.getStyle() & Font.STYLE_BOLD) != 0) {
72  	    		style += "bold,";
73  	    	}
74  	    	if ((meFont.getStyle() & Font.STYLE_ITALIC) != 0) {
75  	    		style += "italic,";
76  	    	}
77  	    	if ((meFont.getStyle() & Font.STYLE_ITALIC) != 0) {
78  	    		style += "underlined,";
79  	    	}
80  	    	style = style.substring(0, style.length() - 1);
81  	    	int size = 0;
82  	    	if (meFont.getSize() == Font.SIZE_SMALL) {
83  	    		size = SIZE_SMALL;
84  	    	} else if (meFont.getSize() == Font.SIZE_MEDIUM) {
85  	    		size = SIZE_MEDIUM;
86  	    	} else if (meFont.getSize() == Font.SIZE_LARGE) {
87  	    		size = SIZE_LARGE;
88  	    	}
89  	    	result = new SwtSystemFont(name, style, size, antialiasing);
90  	    	fonts.put(new Integer(key), result);
91  	    }
92  	    
93  	    return result;
94  	}
95  	
96  	public void init() {
97  		fonts.clear();
98  	}
99  
100 	public int charWidth(Font f, char ch) {
101 		return getFont(f).charWidth(ch);
102 	}
103 
104 	public int charsWidth(Font f, char[] ch, int offset, int length) {
105 		return getFont(f).charsWidth(ch, offset, length);
106 	}
107 
108 	public int getBaselinePosition(Font f) {
109 		return getFont(f).getBaselinePosition();
110 	}
111 
112 	public int getHeight(Font f) {
113 		return getFont(f).getHeight();
114 	}
115 
116 	public int stringWidth(Font f, String str) {
117 		return getFont(f).stringWidth(str);
118 	}
119 
120 	public boolean getAntialiasing() {
121 		return antialiasing;
122 	}
123 
124 	public void setAntialiasing(boolean antialiasing) 
125 	{
126 		this.antialiasing = antialiasing;
127 		
128 		Enumeration en = fonts.elements();
129 		while (en.hasMoreElements()) {
130 			SwtFont font = (SwtFont) en.nextElement();
131 			font.setAntialiasing(antialiasing);
132 		}
133 	}
134 
135 	public void setFont(String face, String style, String size, org.microemu.device.impl.Font font) {
136 		int key = 0;
137 		
138 		if (face.equalsIgnoreCase("system")) {
139 			key |= Font.FACE_SYSTEM;
140 		} else if (face.equalsIgnoreCase("monospace")) {
141 			key |= Font.FACE_MONOSPACE;
142 		} else if (face.equalsIgnoreCase("proportional")) {
143 			key |= Font.FACE_PROPORTIONAL;
144 		}
145 		
146 		String testStyle = style.toLowerCase();
147 		if (testStyle.indexOf("plain") != -1) {
148 			key |= Font.STYLE_PLAIN;
149 		} 
150 		if (testStyle.indexOf("bold") != -1) {
151 			key |= Font.STYLE_BOLD;
152 		} 
153 		if (testStyle.indexOf("italic") != -1) {
154 			key |= Font.STYLE_ITALIC;
155 		} 
156 		if (testStyle.indexOf("underlined") != -1) {
157 			key |= Font.STYLE_UNDERLINED;
158 		}
159 		
160 		if (size.equalsIgnoreCase("small")) {
161 			key |= Font.SIZE_SMALL;
162 		} else if (size.equalsIgnoreCase("medium")) {
163 			key |= Font.SIZE_MEDIUM;
164 		} else if (size.equalsIgnoreCase("large")) {
165 			key |= Font.SIZE_LARGE;
166 		}
167 		
168 		fonts.put(new Integer(key), font);
169 	}
170 
171 	public org.microemu.device.impl.Font createSystemFont(String defName, String defStyle, int defSize, boolean antialiasing) {
172 		return new SwtSystemFont(defName, defStyle, defSize, antialiasing);
173 	}
174 
175 	public org.microemu.device.impl.Font createTrueTypeFont(URL defUrl, String defStyle, int defSize, boolean antialiasing) {
176 		return new SwtTrueTypeFont(defUrl, defStyle, defSize, antialiasing);
177 	}
178 	
179 }