// JavaScript include file
//
//	ASSUMPTIONS:
//
//	1.	domlib.js already included
//	2.	markup class "fontTest" is not important to your page
//	3.	markup id "baseFont" is not important to your page
//	4.	markup id "testFont" is not important to your page
//	5.	the font "Courier New" is guaranteed on your system
//

// function fontLoaded(fname)
//
//	PURPOSE:
//		tests to see if the named font is available on your system
//
//	RETURNS:
//		false	if it can't load the font
//		true	if it can load the font
//
//	SIDE EFFECT:
//		There is a brief flicker of text as it tests.  it goes away.
//
function fontLoaded(fname) {
	if (null==fname || ''==fname) return false;
	if (fname.toLowerCase()=="courier new") return true;	// ASSUMPTION!

	var atoz = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

	var baseFont = new DomElement('baseFont','fontTest');
	baseFont.setHTML('<font face="Courier New" color="white">'+atoz+'</font>');
	baseFont.style.position = "absolute";
	document.body.appendChild(baseFont.elm)

	var testFont = new DomElement('testFont','fontTest');
	testFont.setHTML('<font face="'+fname+',Courier New" color="white">'+atoz+'</font>');
	testFont.style.position = "absolute";
	document.body.appendChild(testFont.elm)

	// we assume "Courier New" is available, test font is likely different size.
	//
	var ret = (baseFont.getW() != testFont.getW()) || (baseFont.getH() != testFont.getH());

	baseFont.setHTML('');
	testFont.setHTML('');

	baseFont = null;
	testFont = null;

	return ret;
}
function fontListLoaded( fnameList )
{
	if (null==fnameList || ''==fnameList) return "";

	var ans = "";

	var fnl = fnameList.split(',');
	for(var i=0; i<fnl.length; ++i)
	{
		var fn = fnl[i];
		while (fn.charAt(0) == ' ') fn = fn.substring(1,fn.length);
		while (fn.charAt(fn.length-1) == ' ') fn = fn.substring(0,fn.length-1);
		ans += fontLoaded(fn) ? "1" : "0";
	}
	return ans;
}

// function whichCherokee
//
//  PURPOSE:
//      to determine which of the two "Cherokee" fonts is loaded.
//
//      CNO     http://www.cherokee.org/Extras/downloads/font/CherokeeFont.htm
//      JoLo    http://www.atypical.net/Cherokee.html
//
//  RETURNS:
//      ''                if neither Cherokee font is loaded
//      "Cherokee (CNO)"  if the Cherokee Nation Organization's font is loaded
//      "Cherokee (JoLo)" if Joseph LoCicero's Cherokee font is loaded
//
//  SIDE EFFECT:
//      There is a brief flicker of text as it tests.  it goes away.
//
function whichCherokee() {
	if (!fontLoaded("Cherokee")) return '';

	// the CNO version of Cherokee font uses
	//		'1' as a shortcut for tsa-la-gi	"Ewf"
	//		'2' as a shortcut for o-si-yo	"oRY"
	//		'3' as a shortcut for wa-do		"\\4"
	//
	// check the size of "Ewf" compared to "1"
	//	should be (about) the same if CNO loaded
	//	if JoLo is loaded, then the width of "1" is about 1/3 of the width of "Ewf"
	//
	// in practice
	//	(1.width*100 / Ewf.width) about 71 == CNO
	//	(1.width*100 / Ewf.width) about 26 == JoLo
	//
	// so make my break on 50
	//
	var baseFont = new DomElement('baseFont','fontTest');
	baseFont.setHTML('<font face="Cherokee,Courier New" color="white">Ewf oRY \\4</font>');
	baseFont.style.position = "absolute";
	document.body.appendChild(baseFont.elm)

	var testFont = new DomElement('testFont','fontTest');
	testFont.setHTML('<font face="Cherokee,Courier New" color="white">1 2 3</font>');
	testFont.style.position = "absolute";
	document.body.appendChild(testFont.elm)

	var isJoLo = ((testFont.getW()*100) / baseFont.getW()) < 50.0;

	baseFont.setHTML('');
	testFont.setHTML('');

	baseFont = null;
	testFont = null;

	return isJoLo ? "Cherokee (JoLo)" : "Cherokee (CNO)";
}
function isCherokeeCNO() { return whichCherokee().indexOf("CNO") >= 0; }
function isCherokeeJoLo() { return whichCherokee().indexOf("JoLo") >= 0; }

// return a string of flags, each flag is '0' or '1' for whether or not that
//	Cherokee font is loaded and available on the system.
//
// NOTE: This is still essentially true, but all _known_ fonts are checked
// in their native order in the knownFontFaceList variable.
//
//	[0] == "Cherokee"			Cherokee Nation Official (CNO)
//	[1] == "Cherokee"			Joanne Touzette's (JoLo)
//	[2] == "Cherokee WP"			Cherokee Companion
//	[3] == "Sequoyah"			Kenneth Hirst
//	[4] == "TsaliText"			Lee Anne Philips
//	[5] == "Cher_Sound"			Jim Hicks
//	[6] == "Cherokee Arial"		Arial variant of (JoLo) font [some errors]
//	[7] == "Code2000"			Unicode font that includes Cherokee range
//	[8] == "ClearlyU"			Unicode font that includes Cherokee range
//	[9] == "Everson Mono Unicode"	Unicode font that includes Cherokee range
//	[10] == "Aboriginal Sans"		Unicode font that includes Cherokee range
//	[11] == "Aboriginal Serif"		Unicode font that includes Cherokee range
//	[18] == "Marin Regular"		Unicode font that includes Cherokee range
//	[19] == "MPH 2B Damase"		Unicode font that includes Cherokee range
//	[20] == "Hindsight"			Unicode font that includes Cherokee range
//	[21] == "Digowheki"			Unicode font that includes Cherokee range
//
//	Example:	"01110100000001"
//		Cherokee (CNO) is not available
//		Cherokee (JoLo) is available
//		Cherokee WP is available
//		Sequoyah is available
//		TsaliText is not available
//		Cher_Sound is available
//		Code2000 is not available
//		ClearlyU is not available
//		Cherokee Arial is not available
//		Everson Mono Unicode is not available
//		Marin Unicode is not available
//		Damase Unicode is not available
//		Hindsight Unicode is not available
//		Digohweli Unicode is available
//
function getCherokeeFontMap()
{
	// "Cherokee" tests for either the CNO font or the JoLo font
	//	This list tests for all the Cherokee fonts that I am aware of.
	//	[ as of 2003.07.15, anyway ]
	//
	// NOTE: Keep the order the same as in fontInfo.js. With the exception
	//	that "Cherokee" is listed only once, but in fontInfo it is entered twice.
	//	That is because "Cherokee" (CNO) and "Cherokee" (JoLo) use the same face name.
	//	We catch them in the following if cascade.
	//
//	fm = fontListLoaded("Cherokee,Cherokee WP,Sequoyah,TsaliText,Cher_Sound,Cherokee Arial,Code2000,ClearlyU,Everson Mono Unicode,Aboriginal Sans,Aboriginal Serif,Marin Regular,MPH 2B Damase,Hindsight,Digohweli");
	fm = fontListLoaded(knownFontFaceList);

	if (""==fm)
	{
		// mark all Cherokee fonts as absent
		for(var i=0; i<knownFonts.length; ++i)
			fm = fm + "0";
//		fm = "0000000000000000";
	}
	else
	{
		// NOTE: This _ASSUMES_ that Cherokee CNO and Cherokee JoLo are
		//	the first two fonts, in that order.
		//
		if (fm.substring(0,2)=="00")
		{
			// both CNO and JoLo as absent
		}
		else
		{
			if (isCherokeeCNO())
			{
				// flag CNO as present and JoLo as absent
				fm = "10" + fm.substring(2,fm.length);
			}
			else
			{
				// flag CNO as absent and JoLo as present
				fm = "01" + fm.substring(2,fm.length);
			}
		}
	}
	return fm;
}
