// Characters.jsl
// Roger Garrett
// 3/21/2010
function Characters()
{
}
// NOTE that this assumes that we have the following at the start of the body of the HTML document:
//
//
named CharacterNub
// visibility:hidden So that the contents of it is never visible to the user.
// width:7px ; height:7px It needs a minimum hieght and width. This effectively gets modified automatically when
// we insert HTML into the DIV, via its innerHTML. We then access the height and width.
// If we had tried tomake it "too small" some browsers would complain.
// via the offsetWidtha d offsetHeight properties of this DIV
// position:absolute; So that it effectively "overlays" the existing data on the page, although, since the visibility
// is "hidden" it doesn't actually appear.
// left:0px; top:0px So it is positioned within the page. If we had tried to put it off-page some browsers will complain.
// Each entry in this font array corresponds to an individual font.
// The index (key) into the array is the name of the font.
// Each entry in this font array is itself an array, that array
// being indexed (keyed) by pointSize.
Characters.font = new Array();
function Characters.getSize(fontName, pointSize, character)
{
// returns the height and width of the specified character within
// the specified fontName at the specified pointSize.
//report("IN Characters.getSize("+fontName+", "+pointSize+", "+character+")\n");
//reportTabIn();
if (Characters.font[fontName] == null)
{
// We do not yet have information for this font.
// Create an entry for the specified font, that entry
// itself being an array.
//report("Creating array for font: "+fontName+"\n");
Characters.font[fontName] = new Array();
}
theFontEntry = Characters.font[fontName];
if (theFontEntry[pointSize] == null)
{
// We do not yet have information for the font at the
// specified pointSize.
// Create an entry for the specified pointSize.
//report("Creating array for pointSize "+pointSize+"\n");
theFontEntry[pointSize] = new Array();
}
var theFontAtThePointSize = theFontEntry[pointSize];
if (theFontAtThePointSize[character] == null)
{
// We do not yet have the information for the font at the
// specified point size for the specified character.
// Create an entry for the character.
//report("Creating info for character "+character+"\n");
theFontAtThePointSize[character] = {width:0,height:0};
html = "
";
html += character;
html += "
";
//report("html = "+html+"\n");
var theCharacterNub = document.getElementById("CharacterNub");
theCharacterNub.innerHTML = html;
var theNub = document.getElementById("charNub");
theFontAtThePointSize[character].width = theCharacterNub.clientWidth;
theFontAtThePointSize[character].height = theCharacterNub.clientHeight;
}
//reportTabOut();
return theFontAtThePointSize[character];
}
function Characters.loadCharacterSizes(fontName, pointSize, characters)
{
// For the given font at the given pointSize this loads in the sizes for all
// the characters that are specified in the characters array.
//
// Paremeters:
//
// fontName
// pointSize The point size for which character sizes are to be loaded.
// characters An array of characters whose individual character sizes are to be loaded.
//
// returns true if successful, false otherwise.
report("IN Characters.loadCharacterSizes(fontName, pointSize, characters)\n");
if (fontName == null)
{
reportNullObject("Characters.loadCharacterSizes(fontName, pointSize, characters)","fontName");
return false;
}
if (pointSize == null)
{
reportNullObject("Characters.loadCharacterSizes(fontName, pointSize, characters)","pointSize");
return false;
}
if ((characters == null) || !(characters instanceof Array))
{
reportNullObject("Characters.loadCharacterSizes(fontName, pointSize, characters)","characters");
return false;
}
for (var index = 0 ; index < characters.length ; ++index)
{
var thisCharacter = characters[index];
Characters.getSize(fontName, pointSize, thisCharacter);
}
}
Characters.standardCharacters = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
".",",",";",":","<",">","[","]","{","}","(",")","/","?",
"-","+","=",
"!","@","#","$","%","^","&","*","~",
"0","1","2","3","4","5","6","7","8","9");
function Characters.loadStandardCharacters(fontName, pointSize)
{
report("IN Characters.loadStandardCharacters\n");
if (fontName == null)
{
reportNullObject("Characters.loadStandardCharacters(fontName, pointSize, characters)","fontName");
return false;
}
if (pointSize == null)
{
reportNullObject("Characters.loadStandardCharacters(fontName, pointSize, characters)","pointSize");
return false;
}
return Characters.loadCharacterSizes(fontName, pointSize, Characters.standardCharacters);
}