Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0297f92994950db012384df7a052fe0e83aee09c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.css2.font;

import org.eclipse.jst.pagedesigner.css2.ICSSStyle;
import org.eclipse.jst.pagedesigner.css2.property.FontFamilyMeta;
import org.eclipse.jst.pagedesigner.css2.property.FontSizeMeta;
import org.eclipse.jst.pagedesigner.css2.property.FontWeightMeta;
import org.eclipse.jst.pagedesigner.css2.property.ICSSPropertyID;
import org.eclipse.jst.pagedesigner.css2.value.Length;
import org.eclipse.jst.pagedesigner.utils.CacheManager;
import org.eclipse.jst.pagedesigner.utils.ICacheEntryCreator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;

/**
 * @author mengbo
 */
public class CSSFontManager implements ICSSFontManager {
	private static CSSFontManager _instance;

	private static final boolean DEBUG = false;

	private int _totalFont = 0;

	// private static FontPoolManager _fontPoolManager;
	// Map _cache = new HashMap();

	private static final int CACHESIZE = 100; // we cache 100 font.

	// the scale to convert the px to pt.
	private final static double FONT_SCALE = ((double) Display.getCurrent()
			.getDPI().x) / 72;

	static String cssFontToLocalFont(String original) {
		if ("serif".equalsIgnoreCase(original)) { //$NON-NLS-1$
			return "Georgia"; //$NON-NLS-1$
		} else if ("sans-serif".equalsIgnoreCase(original)) { //$NON-NLS-1$
			return "Arial"; //$NON-NLS-1$
		} else if ("cursive".equalsIgnoreCase(original)) { //$NON-NLS-1$
			// FIXME: MS windows does not support the alternative fonts that
			// match cursive defined at
			// http://www.w3.org/TR/REC-CSS2/fonts.html#generic-font-families,
			// We use Comic Sans MS font family
			// because it is MS alternative.
			return "Comic Sans MS"; //$NON-NLS-1$
		} else if ("fantasy".equalsIgnoreCase(original)) { //$NON-NLS-1$
			return cssFontToLocalFont("serif"); //$NON-NLS-1$
		} else if ("monospace".equalsIgnoreCase(original)) { //$NON-NLS-1$
			return "Courier New"; //$NON-NLS-1$
		} else {
			return original;
		}
	}

	private CacheManager _cacheManager = new CacheManager(
			new ICacheEntryCreator<CSSFont, Font>() {
				public Font createEntry(CSSFont key) {
					if (DEBUG) {
						_totalFont++;
						System.out.println("TotalFont++: " + _totalFont); //$NON-NLS-1$
					}
					Font font = new Font(null, cssFontToLocalFont(key
							.getFontFamily()), (int) Math.round(key
							.getFontSize()
							/ FONT_SCALE), key.getSwtFontStyle());
					return font;
				}

				public void dispose(CSSFont key, Font entry) {
					if (DEBUG) {
						_totalFont--;
						System.out.println("TotalFont--: " + _totalFont); //$NON-NLS-1$
					}
					entry.dispose();

				}
			}, CACHESIZE);

	/**
	 * constructor
	 */
	private CSSFontManager() {
		super();
	}

	private String resolveFontStyleString(ICSSStyle style) {
		StringBuffer sb = new StringBuffer();
		sb.append(ICSSPropertyID.ATTR_FONT_FAMILY).append(":"); //$NON-NLS-1$
		sb.append("'").append( //$NON-NLS-1$
				(String) style
						.getStyleProperty(ICSSPropertyID.ATTR_FONT_FAMILY))
				.append("'"); //$NON-NLS-1$
		sb.append(";"); //$NON-NLS-1$
		sb.append(ICSSPropertyID.ATTR_FONT_STYLE).append(":"); //$NON-NLS-1$
		sb
				.append(
						(String) style
								.getStyleProperty(ICSSPropertyID.ATTR_FONT_STYLE))
				.append(";"); //$NON-NLS-1$
		sb.append(ICSSPropertyID.ATTR_FONT_WEIGHT).append(":"); //$NON-NLS-1$
		sb.append(
				((Integer) style
						.getStyleProperty(ICSSPropertyID.ATTR_FONT_WEIGHT))
						.toString()).append(";"); //$NON-NLS-1$
		sb.append(ICSSPropertyID.ATTR_FONT_SIZE).append(":"); //$NON-NLS-1$
		int fontSize = getFontSize(style, style
				.getStyleProperty(ICSSPropertyID.ATTR_FONT_SIZE));
		sb.append(Integer.toString(fontSize));
		return sb.toString();
	}

	public ICSSFont createFont(ICSSStyle style) {
		String fontfamily = (String) style
				.getStyleProperty(ICSSPropertyID.ATTR_FONT_FAMILY);
		Object fontsizeobj = style
				.getStyleProperty(ICSSPropertyID.ATTR_FONT_SIZE);
		int fontsize;
		fontsize = getFontSize(style, fontsizeobj);
		int fontstyle = getFontStyle(style);
		int fontweight = ((Integer) style
				.getStyleProperty(ICSSPropertyID.ATTR_FONT_WEIGHT)).intValue();

		return new CSSFont(fontfamily, fontsize, fontstyle, fontweight,
				resolveFontStyleString(style));
	}

	private int getFontSize(ICSSStyle style, Object fontsizeobj) {
		int fontsize;
		if (fontsizeobj instanceof Length) {
			fontsize = ((Length) fontsizeobj).getValue();
		} else {
			fontsize = style.getParentStyle().getCSSFont().getFontSize();
		}
		return fontsize;
	}

	/**
	 * @param style
	 */
	private int getFontStyle(ICSSStyle style) {
		int fontstyle;
		String fontstylestr = (String) style
				.getStyleProperty(ICSSPropertyID.ATTR_FONT_STYLE);
		if (ICSSPropertyID.VAL_ITALIC.equals(fontstylestr)
				|| ICSSPropertyID.VAL_OBLIQUE.equals(fontstylestr)) {
			fontstyle = SWT.ITALIC;
		} else {
			fontstyle = SWT.NORMAL;
		}
		return fontstyle;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.font.ICSSFontManager#dispose()
	 */
	public void dispose() {
		_cacheManager.disposeAll();
	}

	/**
	 * @return the default css font
	 */
	public ICSSFont createDefaultFont() {
		CSSFont result = new CSSFont(FontFamilyMeta.DEFAULT_FONT,
				(int) FontSizeMeta.MEDIUM_VAL_INT, SWT.NORMAL,
				FontWeightMeta.NORMAL_WEIGHT.intValue(), ""); //$NON-NLS-1$
		return result;
	}

	/**
	 * @param f
	 * @return the swt font for f
	 */
	public Font getSwtFont(CSSFont f) {
		return (Font) _cacheManager.getEntry(f);
	}

	/**
	 * @return the singleton font manager
	 */
	public static CSSFontManager getInstance() {
		if (_instance == null) {
			_instance = new CSSFontManager();
		}
		return _instance;
	}
}

Back to the top