All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.framework.util.style.FontFamily Maven / Gradle / Ivy

/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package org.ajax4jsf.framework.util.style;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @author Maksim Kaszynski
 *
 */
public class FontFamily {
	public static final String CSS_SANS_SERIF = "SANS-SERIF";
	public static final String JAVA_SANS_SERIF = "SANSSERIF";
	public static final String CSS_MONOSPACED = "MONOSPACE";
	public static final String JAVA_MONOSPACED = "MONOSPACED";
	
	private static final String[] fontFamilies = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
	static{
		for (int i = 0; i < fontFamilies.length; i++) {
			fontFamilies[i] = fontFamilies[i].toUpperCase();
			//System.out.println(fontFamilies[i]);
		}
		Arrays.sort(fontFamilies);
	}
	
	public static Font getFont(String fontFamily, int style, int size){
		String fontUsed = null;
		StringTokenizer tokenizer = new StringTokenizer(fontFamily, ",");
		while(tokenizer.hasMoreElements()){
			String fontName = tokenizer.nextToken().trim().toUpperCase();
			if(fontName.equals(CSS_SANS_SERIF)){
				fontName = JAVA_SANS_SERIF;
			} else if(fontName.equals(CSS_MONOSPACED)){
				fontName = JAVA_MONOSPACED;
			}
			
			if(Arrays.binarySearch(fontFamilies, fontName) >= 0){
				fontUsed = fontName;
			}
		}
		if(fontUsed == null){
			fontUsed = JAVA_SANS_SERIF;
		}
		Font f = new Font(fontUsed, style, size);
		return f;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy