PDA

View Full Version : Out of many, one...


Quiet Storm
03-20-2003, 07:23 PM
IE 6.0+ only

Found this code to detect user's installed
fonts and was wondering how I could see if
a certain font is installed. This shows how
many fonts and the entire list of font names,
but I can't seem do figure out how to detect
one from the many.

I think I'm looking for something like this:

if (font list) CONTAINS "tahoma" {do.nothing}
else {do.something}

Anyone know how to set this up? :(

<HTML>

<SCRIPT language="javascript">
function init() {if (typeof(local_init) == "function") local_init();}
function getId(id)
{if (typeof(document.getElementById) != "undefined")
{return document.getElementById(id);}
else {return document.all[id];}}
</SCRIPT>

</HEAD>
<BODY onload=init();>

<OBJECT id=dlgocx height=0px width=0px
classid=clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b></OBJECT>
<SCRIPT language=javascript type=text/javascript>
function local_init() {
var chSet=new Object();
chSet[0]='ANSI';
chSet[1]='DEFAULT';
chSet[2]='SYMBOL';
chSet[128]='SHIFTJIS';
chSet[129]='HANGEUL';
chSet[129]='HANGUL';
chSet[134]='GB2312';
chSet[136]='CHINESEBIG5';
chSet[255]='OEM';
chSet[77]='MAC';
chSet[130]='JOHAB';
chSet[161]='GREEK';
chSet[162]='TURKISH';
chSet[163]='VIETNAMESE';
chSet[177]='HEBREW';
chSet[178]='ARABIC';
chSet[186]='BALTIC';
chSet[204]='RUSSIAN';
chSet[222]='THAI';
chSet[238]='EASTEUROPE';
var dlghelper = getId("dlgocx");
if (typeof(dlghelper.fonts) != "undefined") {
var fontObj=dlghelper.fonts;
var fontCount;
if (typeof(dlghelper.fonts) != "undefined") {
fontCount=dlghelper.fonts.count;
getId("fontsnum").innerHTML = fontCount;
}
var fontNames=new Array(fontCount);
for (var i=1;i<fontCount;i++) {
fontNames[i]=dlghelper.fonts(i);
}
fontNames.sort();
var txt = "";
for (i=0; i < fontCount; i++) {
if (fontNames[i]) {
txt += fontNames[i] + " - " + chSet[dlghelper.getCharset(fontNames[i])] + " type<br>";
}}
getId("fontsall").innerHTML = txt;
} else {
getId("fontsnum").innerHTML = "Property not supported!";
getId("fontsall").innerHTML = "Property not supported!";
}}
</SCRIPT>





<H1>Font Information</H1>
<TABLE class=content-bs>
<THEAD>
<TR>
<TH>Variable</TH>
<TH>Setting</TH></TR></THEAD>
<TBODY>
<TR>
<TD class=property>Number of Fonts installed</TD>
<TD class=value><SPAN id=fontsnum>Please wait...</SPAN> </TD></TR>
<TR>
<TD class=property>Fonts installed</TD>
<TD class=value><SPAN id=fontsall>Please wait...</SPAN>
</TD></TR></TBODY></TABLE>

</BODY></HTML>

Roy Sinclair
03-20-2003, 07:37 PM
This


for (i=0; i < fontCount; i++) {
if (fontNames[i]) {
txt += fontNames[i] + " - " + chSet[dlghelper.getCharset(fontNames[i])] + " type<br>";
}}


is where the font names are built into the text variable named txt that's written into the table leter. Use the isIndex function to search the field txt after it's all built or use isIndex to search each fontNames table entry during the loop.

Quiet Storm
03-20-2003, 07:52 PM
Originally posted by Roy Sinclair
Use the isIndex function to search the field txt after it's all built or use isIndex to search each fontNames table entry during the loop.

Thanks, RS. :thumbsup:

But, I can't find any good information on IsIndex. Anyone know a link or two?

Or could you show an actual JS example? :D

Roy Sinclair
03-20-2003, 08:36 PM
Well you might have found it had I got the name of the function right, try indexOf instead :o .

http://www.dardenhome.com/js/x64623.htm

Quiet Storm
03-20-2003, 11:30 PM
I'm sorry, but I just can't get this working. I've tried different ideas on how to go about this, but beeing (still) a JS newbie, I keep getting the error:

(whatever) is undefined.



The last attempt:
<script>
function welcome(){
var fnt="Verdana"
var iof=fontNames.indexOf(fnt)
if (fontNames.indexOf(fnt)==-1)
{
alert("an Alert")
}
}
setTimeout('welcome()',5000)

</script>

http://www.angelfire.com/mo2/cbch21/TEST/FontCheck.html

Roy Sinclair
03-21-2003, 02:54 PM
The function demonstrated in the sample code below will return "true" if the requested font is found and "false" if the requested font is not found or the browser doesn't support checking for fonts using this particular object (in other words it isn't IE).


<html>
<head>
<title>Testing font checks</title>
</head>
<body onload="document.getElementById('fontcheck').innerHTML=Check_For_Font('Verdana');">
<object id="dlgocx" height="0" width="0" classid=clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b>
</object>
<script type="text/javascript">
var fontFound = false;
function Check_For_Font(fontName)
{
var dlghelper = document.getElementById("dlgocx");
if (typeof(dlghelper.fonts) != "undefined")
{
var fontObj=dlghelper.fonts;
var fontCount;
var txt = "";
if (typeof(dlghelper.fonts) != "undefined")
{
fontCount=dlghelper.fonts.count;
for (var i=1;i<fontCount;i++)
{
txt = dlghelper.fonts(i)
if (txt.indexOf(fontName) != -1) return true;
}
}
}
return false;
}
</script>
<span id="fontcheck">
Testing ...
</span>
</body>
</html>

Quiet Storm
03-22-2003, 09:54 PM
Thanks! That works great!

Now is there a way to, instead of writing true:false, it could document.write on each statement?:


if (true) > document.write('Verdana');
if (false)> document.write('<a href="Verdana.ttf">Verdana</a>');

eggman
03-23-2003, 08:05 PM
If I'm understanding this question correctly, try using the styleSheets collection of window.document, find the rules you want to change (must be in current document and not linked in), then use its style object to change the attribute you want (which in this case would be fontFamily).

Roy Sinclair
03-24-2003, 03:58 PM
Just a simple mod of what I posted before:


<html>
<head>
<title>Testing font checks</title>
</head>
<body onload="ListFontsChecked();">
<object id="dlgocx" height="0" width="0" classid=clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b>
</object>
<script type="text/javascript">
var fontFound = false;
function Check_For_Font(fontName)
{
var dlghelper = document.getElementById("dlgocx");
if (typeof(dlghelper.fonts) != "undefined")
{
var fontObj=dlghelper.fonts;
var fontCount;
var txt = "";
if (typeof(dlghelper.fonts) != "undefined")
{
fontCount=dlghelper.fonts.count;
for (var i=1;i<fontCount;i++)
{
txt = dlghelper.fonts(i)
if (txt.indexOf(fontName) != -1) return true;
}
}
}
return false;
}
function ListFontsChecked()
{
if (Check_For_Font('Verdana'))
{
document.getElementById('fontcheck').innerHTML="Verdana";
}
else
{
document.getElementById('fontcheck').innerHTML="<a href=\"Verdana.ttf\">Verdana</a>";
}
}
</script>
<span id="fontcheck">
Testing ...
</span>
</body>
</html>

Quiet Storm
03-24-2003, 11:17 PM
Thanks, RS! Works perfect!

Now to decypher the different codes to see what I can learn. :D
:thumbsup: