001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Ilya S. Okomin
019: * @version $Revision$
020: */package org.apache.harmony.awt.gl.font;
021:
022: import java.awt.Font;
023: import java.awt.peer.FontPeer;
024: import java.io.File;
025: import java.util.Properties;
026: import java.util.Vector;
027:
028: import org.apache.harmony.awt.gl.font.FontManager;
029: import org.apache.harmony.awt.gl.font.FontProperty;
030:
031: /**
032: *
033: * Windows FontManager implementation.
034: */
035: public class WinFontManager extends FontManager {
036:
037: static final int DEFAULT_PITCH = 0; // GDI DEFAULT_PITCH
038: static final int FIXED_PITCH = 1; // GDI FIXED_PITCH
039: static final int VARIABLE_PITCH = 2; // GDI VARIABLE_PITCH
040:
041: static final int FF_DONTCARE = (0 << 4); // GDI FF_DONTCARE
042: static final int FF_SWISS = (2 << 4); // GDI FF_SWISS
043: static final int FF_MODERN = (3 << 4); // GDI FF_MODERN
044: static final int FF_ROMAN = (1 << 4); // GDI FF_ROMAN
045:
046: /** Available windows charset names */
047: public static final String[] WINDOWS_CHARSET_NAMES = {
048: "ANSI_CHARSET", "DEFAULT_CHARSET", "SYMBOL_CHARSET", "SHIFTJIS_CHARSET", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
049: "GB2312_CHARSET", "HANGEUL_CHARSET", "CHINESEBIG5_CHARSET", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
050: "OEM_CHARSET", "JOHAB_CHARSET", "HEBREW_CHARSET", "ARABIC_CHARSET", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
051: "GREEK_CHARSET", "TURKISH_CHARSET", "VIETNAMESE_CHARSET", "THAI_CHARSET", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
052: "EASTEUROPE_CHARSET", "RUSSIAN_CHARSET", "MAC_CHARSET", "BALTIC_CHARSET" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
053: };
054:
055: /** WinFontManager singleton instance */
056: public static final WinFontManager inst = new WinFontManager();
057:
058: private WinFontManager() {
059: super ();
060: initFontProperties();
061: }
062:
063: @Override
064: public void initLCIDTable() {
065: NativeFont.initLCIDsTable(this .tableLCID);
066: }
067:
068: /**
069: * Initializes fProperties array field for the current system configuration font
070: * property file.
071: *
072: * @return true is success, false if font property doesn't exist or doesn't
073: * contain properties.
074: */
075: public boolean initFontProperties() {
076: File fpFile = getFontPropertyFile();
077: if (fpFile == null) {
078: return false;
079: }
080:
081: Properties props = getProperties(fpFile);
082: if (props == null) {
083: return false;
084: }
085:
086: for (String element : LOGICAL_FONT_NAMES) {
087: for (int j = 0; j < STYLE_NAMES.length; j++) {
088: Vector<FontProperty> propsVector = new Vector<FontProperty>();
089:
090: // Number of entries for a logical font
091: int numComp = 0;
092: // Is more entries for this style and logical font name left
093: boolean moreEntries = true;
094: String value = null;
095:
096: while (moreEntries) {
097: // Component Font Mappings property name
098: String property = FONT_MAPPING_KEYS[0]
099: .replaceAll("LogicalFontName", element).replaceAll("StyleName", STYLE_NAMES[j]).replaceAll("ComponentIndex", String.valueOf(numComp)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
100: value = props.getProperty(property);
101:
102: // If the StyleName is omitted, it's assumed to be plain
103: if ((j == 0) && (value == null)) {
104: property = FONT_MAPPING_KEYS[1]
105: .replaceAll("LogicalFontName", element).replaceAll("ComponentIndex", String.valueOf(numComp)); //$NON-NLS-1$ //$NON-NLS-2$
106: value = props.getProperty(property);
107: }
108:
109: if (value != null) {
110:
111: String fontName = value.substring(0, value
112: .indexOf(",")); //$NON-NLS-1$
113: int ind = fontName.lastIndexOf("Bold"); //$NON-NLS-1$
114: if (ind != -1) {
115: fontName = fontName.substring(0, ind - 1);
116: } else {
117: ind = fontName.lastIndexOf("Italic"); //$NON-NLS-1$
118: if (ind != -1) {
119: fontName = fontName.substring(0,
120: ind - 1);
121: }
122: }
123:
124: String charset = value.substring(value
125: .indexOf(",") + 1, value.length()); //$NON-NLS-1$
126:
127: // Font File Names property value
128: String fileName = props
129: .getProperty(FONT_FILE_NAME
130: .replaceAll(
131: "PlatformFontName", fontName).replaceAll(" ", "_")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
132:
133: // Exclusion Ranges property value
134: String exclString = props
135: .getProperty(EXCLUSION_RANGES
136: .replaceAll(
137: "LogicalFontName", element).replaceAll("ComponentIndex", String.valueOf(numComp))); //$NON-NLS-1$ //$NON-NLS-2$
138: int[] exclRange = parseIntervals(exclString);
139:
140: // Component Font Character Encodings property value
141: String encoding = props
142: .getProperty(FONT_CHARACTER_ENCODING
143: .replaceAll(
144: "LogicalFontName", element).replaceAll("ComponentIndex", String.valueOf(numComp))); //$NON-NLS-1$ //$NON-NLS-2$
145:
146: FontProperty fp = new WinFontProperty(fileName,
147: fontName, charset, j, exclRange,
148: encoding);
149: propsVector.add(fp);
150: numComp++;
151: } else {
152: moreEntries = false;
153: }
154: }
155: fProperties.put(element + "." + j, propsVector); //$NON-NLS-1$
156: }
157: }
158:
159: return true;
160:
161: }
162:
163: @Override
164: public int getFaceIndex(String faceName) {
165: for (int i = 0; i < NativeFont.faces.length; i++) {
166: if (NativeFont.faces[i].equalsIgnoreCase(faceName)) {
167: return i;
168: }
169: }
170: return -1;
171: }
172:
173: @Override
174: public Font[] getAllFonts() {
175: String faces[] = NativeFont.getAvailableFaces();
176: int count = faces.length;
177: Font[] fonts = new Font[count];
178:
179: for (int i = 0; i < count; i++) {
180: fonts[i] = new Font(faces[i], Font.PLAIN, 1);
181: }
182:
183: return fonts;
184: }
185:
186: @Override
187: public String[] getAllFamilies() {
188: if (allFamilies == null) {
189: allFamilies = NativeFont.getFamilies();
190: }
191: return allFamilies;
192: }
193:
194: @Override
195: public FontPeer createPhysicalFontPeer(String name, int style,
196: int size) {
197: WindowsFont peer;
198: if (isFamilyExist(name)) {
199: peer = new WindowsFont(name, style, size);
200: peer.setFamily(name);
201: return peer;
202: }
203: int faceIndex = getFaceIndex(name);
204: if (faceIndex != -1) {
205: style |= NativeFont.fontStyles[faceIndex];
206: name = NativeFont.getFamily(faceIndex);
207:
208: peer = new WindowsFont(name, style, size);
209: return peer;
210: }
211:
212: return null;
213: }
214:
215: @Override
216: public FontPeer createDefaultFont(int style, int size) {
217: return new WindowsFont(DEFAULT_NAME, style, size);
218: }
219:
220: }
|