![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
想不到这样也可以~! 拽
<?
// array ttfName(string $font)
//
// where $font is a string pointing to the font file
//
// Purpose:
//
// Reads a font and stores all information related to the font name and
// copyright in an associative array
//
// Returns:
//
// $array["copy"] = copyright information
// $array["font family"] = font family information
// $array["font subfamily"] = font subfamily name
// $array["id"] = unique font identification
// $array["name"] = full font name
// $array["version"] = font version
// $array["postscript name"] = postscript name for the font
// $array["tm"] = trademark information
// $array["manufacturer"] = font manufacturer name
// $array["designer"] = font designer name
// $array["description"] = font description
// $array["vendor url"] = vendor url
// $array["designer url"] = designer url
// $array["license"] = the terms under which the font may be used
// $array["license url"] = the url pointing to information about the font license
// $array["sample text"] = sample text included with the font for preview displays
// $array["platform"] = platform the font was designed under
//
// or
//
// returns boolean false if name tag can't be found, if the file
// doesn't exist, or if the magic number is not valid.
///////////////////////////
$font="D:/WINDOWS/Fonts/simhei.ttf";
#$font指向具体的字体
$array=ttfName($font);
echo "
";";
print_r($array);
echo "
function ttfName($font) {
$found=false;
if (!$fp = @fopen ("$font","r")) {
return false;
}
$magic=false;
$pos=0;
// here we check to see if a magic number is there, which
// should be common to all truetype fonts
// The header should appear somewhere in the first 1k,
// if not, this is not a truetype font file. in this event,
// return false
while (($magic==false) && (ftell($fp)<=1024)) {
$data=fread($fp,4);
// if we find the head tag, try to get the
// location of the head table
if ($data=="head") {
$data=fread($fp, 12);
$headData=unpack("N3",$data);
// jump to the offset of the header table
fseek($fp,$headData[2], SEEK_SET);
$data=fread($fp,16);
$headTableData=unpack("N4",$data);
// is this our magic number ?
if ($headTableData[4]==0x5F0F3CF5) {
$magic=true;
} else {
return false;
}
}
}
// if we never found the head to begin with,
// no magic at all
if ($magic==false) {
return false;
}
// return to the start of the file to begin processing
// the font name information
fseek($fp,0,SEEK_SET);
while (!feof($fp)) {
$data=fread($fp,4);
// continue to loop until the 'name' tag is located
if ($data=="name") {
// read data present after tag, 2 unsigned Long, big endian
$tableDir=fread($fp, 8);
$tableDirData=unpack("N2",$tableDir);
$found=true;
break;
}
}
if ($found) {
// jump to offset of table, from beginning of file
fseek($fp,$tableDirData[2], SEEK_SET);
// read three unsigned short, big endian. get next table offset,
// and number of records present, as well as the platform ID
$data=fread($fp, 8);
$info=unpack("n4", $data);
// figure out the platform ID, and add it to our final array
switch($info[4]) {
case 0:
$name["platform"]="Unicode";
break;
case 1:
$name["platform"]="Apple Macintosh";
break;
case 2:
$name["platform"]="ISO";
break;
case 3:
$name["platform"]="Microsoft Windows";
break;
case 4:
$name["platform"]="Custom";
break;
default:
$name["platform"]="unknown";
}
//jump back 2 bytes, so we're in the right place
fseek($fp, ftell($fp)-2, SEEK_SET);
// read six unsigned integers, big endian - loop through all records
// the number of records is defined in the pervious table
$i=0;
for ($x=0; $x<=$info[2]; $x++) {
$a=fread($fp, 12);
$data=unpack("n6",$a);
// in these IF statements we find the first occurances of each specific
// piece of data, and store the appropriate information
if (($data[4]==0) && (!isset($indexData["copy"]))) {
$indexData["copy"]=$data;
} else
if (($data[4]==1) && (!isset($indexData["font family"]))) {
$indexData["font family"]=$data;
} else
if (($data[4]==2) && (!isset($indexData["font subfamily"]))) {
$indexData["font subfamily"]=$data;
} else
if (($data[4]==3) && (!isset($indexData["id"]))) {
$indexData["id"]=$data;
} else
if (($data[4]==4) && (!isset($indexData["name"]))) {
$indexData["name"]=$data;
} else
if (($data[4]==5) && (!isset($indexData["version"]))) {
$indexData["version"]=$data;
} else
if (($data[4]==6) && (!isset($indexData["postscript name"]))) {
$indexData["postscript name"]=$data;
} else
if (($data[4]==7) && (!isset($indexData["tm"]))) {
$indexData["tm"]=$data;
} else
if (($data[4]==8) && (!isset($indexData["manufacturer"]))) {
$indexData["manufacturer"]=$data;
} else
if (($data[4]==9) && (!isset($indexData["designer"]))) {
$indexData["designer"]=$data;
} else
if (($data[4]==10) && (!isset($indexData["description"]))) {
$indexData["description"]=$data;
} else
if (($data[4]==11) && (!isset($indexData["vendor url"]))) {
$indexData["vendor url"]=$data;
} else
if (($data[4]==12) && (!isset($indexData["designer url"]))) {
$indexData["designer url"]=$data;
} else
if (($data[4]==13) && (!isset($indexData["license"]))) {
$indexData["license"]=$data;
} else
if (($data[4]==14) && (!isset($indexData["license url"]))) {
$indexData["license url"]=$data;
} else
if (($data[4]==19) && (!isset($indexData["sample text"]))) {
$indexData["sample text"]=$data;
}
}
// set the beginning of the variable area
$stringStorageStart=ftell($fp)-12;
foreach ($indexData as $key=>$val) {
fseek($fp, ($stringStorageStart+$indexData[$key][6]), SEEK_SET);
$name[$key]=fread($fp, $indexData[$key][5]);
}
// remove pointless characters users put in to be l33t
$name=str_replace("\n"," ",$name);
$name=str_replace("\r","",$name);
$name=str_replace("\x00","",$name);
fclose($fp);
return $name;
} else {
fclose($fp);
return false;
}
}
?>

