View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2006 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 org.eclipse.swt.SWT;
23  import org.eclipse.swt.graphics.Font;
24  import org.microemu.app.ui.swt.SwtDeviceComponent;
25  
26  public class SwtSystemFont implements SwtFont {
27  
28  	private String name;
29  	
30  	private String style;
31  	
32  	private int size;
33  	
34  	private boolean antialiasing;
35  	
36  	private boolean initialized;
37  	
38  	private Font font;
39  
40  	public SwtSystemFont(String name, String style, int size, boolean antialiasing) {
41  		this.name = name;
42  		this.style = style.toLowerCase();
43  		this.size = size;
44  		this.antialiasing = antialiasing;
45  		
46  		this.initialized = false;
47  	}
48  
49  	public void setAntialiasing(boolean antialiasing) {
50  		if (this.antialiasing != antialiasing) {
51  			this.antialiasing = antialiasing;
52  			initialized = false;
53  		}
54  	}
55  
56  	public Font getFont() {
57  		checkInitialized();
58  
59  		return font;
60  	}
61  
62  	private synchronized void checkInitialized() {
63  		if (!initialized) {
64  			int swtStyle = 0;
65  			if (style.indexOf("plain") != -1) {
66  				swtStyle |= SWT.NORMAL;
67  			}
68  			if (style.indexOf("bold") != -1) {
69  				swtStyle |= SWT.BOLD;
70  			}
71  			if (style.indexOf("italic") != -1) {
72  				swtStyle |= SWT.ITALIC;
73  			}
74  			if (style.indexOf("underlined") != -1) {
75  				// TODO underlined style not implemented
76  			}
77  			font = SwtDeviceComponent.getFont(name, size, swtStyle, antialiasing);
78  			initialized = true;
79  		}
80  	}
81  
82  	public int charWidth(char ch) {
83  		return charsWidth(new char[] {ch}, 0, 1);
84  	}
85  
86  	public int charsWidth(char[] ch, int offset, int length) {
87  		checkInitialized();
88  
89  		return SwtDeviceComponent.stringWidth(font, new String(ch, offset, length));
90  	}
91  
92  	public int getBaselinePosition() {
93  		checkInitialized();
94  		
95  		return SwtDeviceComponent.getFontMetrics(font).getAscent();
96  	}
97  
98  	public int getHeight() {
99  		checkInitialized();
100 		
101 		return SwtDeviceComponent.getFontMetrics(font).getHeight();
102 	}
103 
104 	public int stringWidth(String str) {
105 		checkInitialized();
106 		
107 		return SwtDeviceComponent.stringWidth(font, str);
108 	}
109 
110 }