javascriptはシステムフォントを取得します.

13768 ワード

============================================    IE================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>              www.aa25.cn </TITLE>

<SCRIPT LANGUAGE="JavaScript">
<!--
function getSystemFonts(){
    var a=dlgHelper.fonts.count;
    var fArray = new Array();
    var oDropDown = document.all.fonts;
    for (i = 1;i < dlgHelper.fonts.count;i++){
        fArray[i] = dlgHelper.fonts(i);
        var aOptions = oDropDown.options;    
        var oOption = document.createElement("OPTION");
        aOptions.add(oOption);    
        oOption.text = fArray[i];
        oOption.Value = i;
    }
}

function getBlockFormats(){
    var a=dlgHelper.blockFormats.count;
    var fArray = new Array();
    var oDropDown = document.all.formats;
    oDropDown.setAttribute("id","FormatList");
    for (i = 1;i < dlgHelper.blockFormats.count;i++)
    {
        fArray[i] = dlgHelper.blockFormats(i);
        var aOptions = oDropDown.options;    
        var oOption = document.createElement("OPTION");
        aOptions.add(oOption);    
        oOption.text = fArray[i];
        oOption.Value = i;
    }
}
//-->
</SCRIPT>
</HEAD>

<BODY onload="getSystemFonts();getBlockFormats()">
          SystemFont and BlockFormats__www.aa25.cn<BR>
<Select NAME="fonts" onchange="document.body.style.fontFamily = this.options[this.selectedIndex].text">
</Select>
<Select NAME="formats" onchange="document.body.style.fontFamily = this.options[this.selectedIndex].text">
</Select>
<OBJECT ID=dlgHelper CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" WIDTH="0px" HEIGHT="0px"></OBJECT>
</BODY>

</HTML>
     IE     ,                  "    .html "       。

        :
<html>
<head>
</head>
<body>
<DIV style="LEFT: 0px; POSITION: absolute; TOP: 0px">
<OBJECT ID="dlgHelper" CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" WIDTH="0px" HEIGHT="0px"></OBJECT>
<script language="javascript" type="text/javascript">
// FontFacesMenu.js
var fontsArray = new Array();
var fontsIsAdded = false;
function fontFacesMenuEnter(fontFacesDropdownList, dlgHelperId)
{
 if((fontsIsAdded == false )||(fontsArray==''))
 {
  fontsArray = getSystemFonts(dlgHelperId);
 
  fontsIsAdded = true;
  for (var s=1; s < fontsArray.length; s++)
  {
   newOption = document.createElement("option");
   newOption.value = fontsArray[s];
   newOption.text = fontsArray[s];
   fontFacesDropdownList.add(newOption);
  }
 }
}
function getSystemFonts(dlgHelperId)
{
 var a = document.all(dlgHelperId).fonts.count;
 var fArray = new Array();
 for (i = 1;i <= parent.document.all(dlgHelperId).fonts.count;i++){
  fArray[i] = parent.document.all(dlgHelperId).fonts(i);
 }
 return fArray;
}
</script>
</DIV>
<select id="blessingWords_FontFamily_DL" name="blessingWords_FontFamily_DL" style="width:140px;" onmouseenter="fontFacesMenuEnter(this, 'dlgHelper');//updateFontList();">
      <option value=""><-      -></option>
</select>
</body>

      :



          
1.              <body>       :

        :
<DIV style="LEFT: 0px; POSITION: absolute; TOP: 0px">
<OBJECT ID="dlgHelper" CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" WIDTH="0px" HEIGHT="0px">
</OBJECT>
<script src="FontFacesMenu.js" language="javascript" type="text/javascript"></script>
</DIV>

2.        :FontFacesMenu.js  。

        :
// FontFacesMenu.js
var fontsArray = new Array();
var fontsIsAdded = false;
function fontFacesMenuEnter(fontFacesDropdownList, dlgHelperId)
{
 if((fontsIsAdded == false )||(fontsArray==''))
 {
  fontsArray = getSystemFonts(dlgHelperId);
  
  fontsIsAdded = true;
  for (var s=1; s < fontsArray.length; s++)
  {
   newOption = document.createElement("option");
   newOption.value = fontsArray[s];
   newOption.text = fontsArray[s];
   fontFacesDropdownList.add(newOption);
  }
 }
}

function getSystemFonts(dlgHelperId)
{
 var a = document.all(dlgHelperId).fonts.count;
 var fArray = new Array();
 for (i = 1;i <= parent.document.all(dlgHelperId).fonts.count;i++){
  fArray[i] = parent.document.all(dlgHelperId).fonts(i);
 }
 return fArray;
}

3.                       :

    :

        :
<SELECT ID="blessingWords_FontFamily_DL" name="blessingWords_FontFamily_DL" style="width:140px;" onmouseenter="fontFacesMenuEnter(this, 'dlgHelper');//updateFontList();">
<OPTION VALUE=""><-      -></OPTION>      
</SELECT> 


OK!       ?  :
 
 :            , onChange                。
=====================================================================================================================================================================
JavaScript code to detect availability of a particular font in a browser using JavaScript and CSS.Update:I have udated the algo of the script and it almost 100%accurate.
This JavaScript class can be used to gess if a paparticularfont is present in a machie.This may be help of desssssktop-likeweappppppppppweaplication developers whehen theywantto provide differentsssssininintotototototototototosssfffffininininininststststststststststststststststststststststststststststststststststststststststssssssffffffffffinininininininins based on the list offonts on their machine.Designers don’t have to rely on the most common fonts like Arial,Versdana or Times New Roman.Since increase number of users have PC with newoperation system/appications,thearemens thearey
How does it work
This code works on the simple price that each character apper differently in different fonts.So different font.So different will take different width and height for the same stricters of same font-size.
We try to create a string with 3 generanic font-face、mons-serif and sans and note their width.The n try to create the same sting in HTML the font to be tested and the generance fallback font.If the font fantit tars up the generanic fallback font-face.We then compre the width of the string with the new font and width of the string with the fallback generance font-face,if the y are different,then the the font exists.with.witter.with.with.with.with.with.with.with.with.with.
<span style="font-family: monospace; font-size: 72px">mmmmmmmmmmlli</span>
vs.
<span style="font-family: New-Font, monospace; font-size: 72px">mmmmmmmmmmlli</span>
Since this test is repeat three times against 3 font famiries、ギves almost 100%acaccurate reults.The string which we will use to geneneneraate the widths can be anythiing.But I ggess we'm'r'r's'because thee the aaaaaaaaaaaaathe maximmwidth width.And thethe atttttttttttttttttwidth.Andth the aaaaaaaaaaaaaaaaaattttttttttttttttttttttttwidth.Anthththththththththththththththracter.I have tested iton Firefox,IE,Opera,Safari,Chrome
Note:It required to execute the script after its copletely loaded for some browsers like Opera.(Opera cannot calculate the offsetWidth until all the parents complettely loaded)
Download ver 0.3(24 Mar 2012)
Released under Apache Software License 2.0.
Plain text JS code(v 0.3)
モモ
You can test any Font here:
// Usage
window.onload = function() {
    var detective = new Detector();
    alert(detective.detect('font name'));
};
Fonts on your computer
This table below show which fonts are present on your system.(I have listed some of the most common and some uncomon fonts.)
Font Name
Detected
cursive
false
monos pace
true
serif
true
サンズ-serif
true
fantasy
false
default
true
アール
true
Arial Black
false
Arial Narrow
false
Arial Rounded MT Bold
false
Bookman Old Style
false
Bradley Hand ITC
false
Century
false
Century Gothic
false
Commic Sans MS
false
Courier
true
Courier New
true
Georgia
false
Gentium
false
Impact
false
King
false
Lucida Consolie
false
lit
false
Modena
false
Monotype Corsiva
false
Papyrus
false
Tahoma
false
TeX
false
Times
true
Times New Roman
true
Trebuchet MS
false
Ver dana
false
Verona
false
=====================================================================================================================================================================
もう一つの広範でよく使われているのはflashを使ってシステムサポートフォントを取得することです.flashは現在のところ、サポートされている心配がないからです.インターネットで検索してみたら、多くのものがflashでシステムフォントを取得して表示する方法はこのようです.
[javascript]view plin copy
import flash.text.Font;  
  • var fontList:Aray = Font.enumerate Fonts;  
  • fontList.sortOn(「font Name」、 Aray.C.ASEIC ENSITIVE;  
  • for each(var font:Font in font List trace(font.font Name);  
  • はい、AS 3に基づくものです.flashツールは遅れています.それとも数年前のMicromedia flash 8.0です.AS 3はまだサポートされていません.はい、AS 2を使います.
    [javascript]view plin copy
    var font.array:Aray = Text Field.get FontList()  
  • font_array.sort();  
  • トレイ(「You」 すばらしい "+font garray.length+" fonts currently installed");  
  • トレイ(----------------------------------);  
  • for (var i = 0; i
  •     トラック #「+(i+1)+」:\t"+font garray[i];  
  • はい、次はjsと接ぎ木します.ある外国人の文章を参考にして、flash 8が持っているヘルプ文書を参考にします.flashは内蔵のflash.external.External Interface類を通じて、jsとの相互呼出し問題を解決します.flashのコードはこのようなものかもしれません.
    [javascript]view plin copy
    import flash.external.External Interface;  
  • /声明jsはgetAllFontsという関数を使用して、名来でこのflashの中のget AllFontsを呼び出すことができます.  
  • External Interface.addCallback(「getAll Fonts」) this、 get All Fonts)  
  • function get All Fonts():Aray{  
  • var font garray:Aray = Text Field.get FontList()  
  • font garray.sort();return font garray;  
  • )  
  • import flash.external.External Interface;  
  • //JsはgetAllFontsという関数を使って、名来で本flashの中のgetAllFontsを呼び出すことができます.  
  • External Interface.addCallback(「get AllFonts」) this、 get All Fonts)  
  • 機能 get All Fonts():Aray{  
  •     var font garray:Aray = Text Field.get FontList()  
  •     font garray.sort()  
  •     return font garray;  
  • )  
  • External Interface.call(「show」);/jsのshow方法を呼び出します.  
  • jsでショー方法を実現します.可能なコード:
    [javascript]view plin copy
    機能 get Movie(movie Name) {  
  •     var IE = navigator.appName.indexOf(「Microsoft」) != -1;  
  •     return (isIE) ? window[movie Name] : document[movieName]  
  • }  
  •   
  •   
  • 機能 get FontList() {  
  •     return get Movie(「font ListFlash」).get All Fonts()  
  • )  
  • function ショー(){  
  •     var fonts=get FontList()  
  •     var selectStr=" onchange='choseFont""";  
  •     for(var) i=0;i
  •         selectStr+=""option value=「+fonts[i]+」「+fonts[i]+」」;  
  •     selectStr+=「