/*
 * Copyright (c) 2003 by Windward Studios, Inc. All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Windward Studios ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Windward Studios, Inc.
 */

package net.windward.util;

import java.io.*;
import java.util.*;
import com.lowagie.text.*;
import org.apache.log4j.Category;

/**
  * This registers all of the fonts on a system.
  *
  * @author David Thielen
  * @version 1.0  January 24, 2003
  */

public class RegisterFonts {

    static private Category log = Category.getInstance (RegisterFonts.class.getName());

	// used to select the files in the directory
	static class FontName implements FilenameFilter {
		
		String spec;
		
		FontName( String name ) {
			spec = name;
		}
		
		public boolean accept( File dir, String name ) {
			return name.toLowerCase().matches( spec );
		}
	}

	public static void registerFonts( String path, String name ) {

		// requires 1.4 or greater for String.matches()
		String ver = System.getProperty( "java.specification.version" );
		if ((ver.charAt(0) == '1') && (ver.charAt(2) < '4'))
			return;

		File fil = new File( path );
		String [] all = fil.list( new FontName( name ) );
		if (all != null)
			for (int ind=0; ind<all.length; ind++)
		        FontFactory.register( path + all[ind] );
	}
    
	public static void registerDefaultFonts() {
		
		String os = System.getProperty( "os.name" );
		log.info( "registerDefaultFonts OS = " + os );
		
		if (os.substring( 0, 7 ).compareToIgnoreCase( "windows" ) == 0) {
			registerFonts( "c:/windows/fonts/", ".*\\.ttf" );
			registerFonts( "c:/windows/fonts/", ".*\\.ttc" );
			registerFonts( "c:/winnt/fonts/", ".*\\.ttf" );
			registerFonts( "c:/winnt/fonts/", ".*\\.ttc" );
		}
		if (os.substring( 0, 7 ).compareToIgnoreCase( "solaris" ) == 0) {
			registerFonts( "/usr/X/lib/X11/fonts/TrueType/", ".*\\.ttf" );
			registerFonts( "/usr/X/lib/X11/fonts/TrueType/", ".*\\.ttc" );
			registerFonts( "/usr/openwin/lib/X11/fonts/TrueType/", ".*\\.ttf" );
			registerFonts( "/usr/openwin/lib/X11/fonts/TrueType/", ".*\\.ttc" );
		}
		if (os.substring( 0, 5 ).compareToIgnoreCase( "linux" ) == 0) {
			registerFonts( "/usr/share/fonts/default/TrueType/", ".*\\.ttf" );
			registerFonts( "/usr/share/fonts/default/TrueType/", ".*\\.ttc" );
			registerFonts( "/usr/X11R6/lib/X11/fonts/ttf/", ".*\\.ttf" );
			registerFonts( "/usr/X11R6/lib/X11/fonts/ttf/", ".*\\.ttc" );
		}
	}
	
	// fonts is in the form path;path;... where each path is the dir & file spec
	public static void registerFontPath( String fontpath ) {
		
		log.info( "registerFontPath( " + fontpath + " )" );

		// walk it one spec at a time
		StringTokenizer tok = new StringTokenizer( fontpath, ";" );
		while (tok.hasMoreTokens()) {
			String spec = tok.nextToken();

			// break into path and file spec
			int ind = spec.lastIndexOf( '/' );
			int indBack = spec.lastIndexOf( '\\' );
			ind = Math.max( ind, indBack );
			if (ind == -1)
				return;
			int end = ind + 1;	
			
			// we have to change *.ttf to .*\.ttf
			StringBuffer file = new StringBuffer();
			while (++ind < spec.length()) {
				switch (spec.charAt( ind )) {
					case '*' :
						file.append( ".*" );
						break;
					case '.' :
						file.append( "\\." );
						break;
					default :
						file.append( spec.charAt( ind ));
						break;
				}
			}	
			registerFonts( spec.substring( 0, end ), file.toString() );
		}
	}
}
	
