Skip to main content
Inspiring
March 25, 2013
Answered

Listing Font Families in an Array (or not), 3 questions

  • March 25, 2013
  • 2 replies
  • 1031 views

I'm building a script that will open a dialog box allowing the user to select a font family from a drop down list (not a list of all of the fonts), and then compare that value against the fonts that are in use, highlighting everything that doesn't match. I have not yet built the dialog, but I've got  everything else working as I want except the list of font families.

The following code loops through the list of fonts (only 20 for testing purposes), compares the family name to the previous on the list (error trapped for the first loop), and pushes the family name (if it doesn't match the previous) into an array which will be used in the dialog box. With the two alerts at the bottom (for troubleshooting), I expect the alerts to show the same family name. For the first three fonts they do, but after that the array value is "undefined." 

var iCount = 20;

//var iCount = textFonts.length;

var nameHolder = "";

var nameArray = new Array();

for(var i=0; i<iCount; i++) {

   var familyName = textFonts.family;

    try{nameHolder = textFonts[i-1].family;;}

    catch (err){nameHolder = "";}

    if (familyName != nameHolder ){

    nameArray.push(familyName);

        alert ("Family Name: " + familyName);

        alert ("Name Array: " + nameArray);

    } 

}

Questions:

1) Any speculation on why this behavior is happening (it isn't truetype versus opentype)?

2) Is there a better way to get the list of font families without duplicates??

2) Is an array the best way to populate a dropdown list in a JS dialog box??? [this will be my first dialog with a dropdown, the others have user-typed values]

Thanks,

Alex

EDIT: Additional Info

When it reaches the ninth font in the list, the above script starts writing undefined, but when tested manually, as below, it works fine. Hmmm.

alert (textFonts[9].family);

var nameArray = new Array();

nameArray.push(textFonts[9].family);

alert (nameArray[0]);

EDIT: More info

tried replacing nameArray.push(familyName); with nameArray.splice(i,0,familyName); bad behavior remains

This topic has been closed for replies.
Correct answer CarlosCanto

1. nothing wrong with your family names gathering

2. no, you're doing it right

3. yes

you're using the global counter "i", to call an item in the array that does not exist, after the 3rd item in the same family, the counter is 4, while the array items is 1, nameArray[4] does not exist

try this


alert ("Family Name: " + familyName);

alert ("Name Array: " + nameArray[nameArray.length-1]);

2 replies

Inspiring
March 25, 2013

As for your Array… This object is core JS so an easy google search would get you some thing like this off Stack Overflow… Just push all the family names to the array then dump…?

var foo = Array( 'A','A','B','b','c','D' );

Array.prototype.getUnique = function(){

   var u = {}, a = [];

   for(var i = 0, l = this.length; i < l; ++i){

      if(u.hasOwnProperty(this)) {

         continue;

      }

      a.push(this);

      u[this] = 1;

   }

   return a;

}

$.writeln( foo.getUnique() );

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
March 25, 2013

1. nothing wrong with your family names gathering

2. no, you're doing it right

3. yes

you're using the global counter "i", to call an item in the array that does not exist, after the 3rd item in the same family, the counter is 4, while the array items is 1, nameArray[4] does not exist

try this


alert ("Family Name: " + familyName);

alert ("Name Array: " + nameArray[nameArray.length-1]);
aklymanAuthor
Inspiring
March 26, 2013

Carlos, Mark, as always you two are indispensable. So, my core code was good, my troubleshooting code wrong. Usually its the other way around!


Thanks for the help.

-Alex

CarlosCanto
Community Expert
Community Expert
March 26, 2013

you're welcome