<?
class JTLFont
{
var $nSize;
var $cFont;
var $cColor;
var $nMaxWidth;
var $nMinWidth;
var $nMaxHeight;
var $nPadding = 4;
var $cCacheDir = '';
var $cFontDir = '';
function __construct($cFont, $nSize, $cColor)
{
$this->cCacheDir = 'templates_c';
$this->cFontDir = PFAD_ROOT . PFAD_FONTS;
$this->cFont = $cFont;
$this->nSize = $nSize;
$this->cColor = $cColor;
}
function html2rgb($cColor)
{
if ($cColor[0] == '#')
$cColor = substr($cColor, 1);
if (strlen($cColor) == 6)
list($r, $g, $b) = array($cColor[0].$cColor[1],
$cColor[2].$cColor[3],
$cColor[4].$cColor[5]);
elseif (strlen($cColor) == 3)
list($r, $g, $b) = array($cColor[0].$cColor[0],
$cColor[1].$cColor[1],
$cColor[2].$cColor[2]);
else
return false;
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
return array($r, $g, $b);
}
function calcRect($cText)
{
$box = ImageTTFBbox($this->nSize, 0, $this->cFontDir . '/' . $this->cFont, $cText);
$width = abs($box[2] - $box[0]);
$height = abs($box[7] - $box[1]);
$x = $box[6] * -1; $y = $box[5] * -1;
return array($width, $height, $x, $y);
}
function calcMax($cText)
{
for ($i = 0; $i < strlen($cText); $i++)
{
$wh = $this->calcRect($cText[$i]);
if ($this->nMaxWidth < $wh[0])
$this->nMaxWidth = $wh[0];
$this->nMinWidth = $this->nMaxWidth;
if ($this->nMinWidth > $wh[0])
$this->nMinWidth = $wh[0];
if ($this->nMaxHeight < $wh[1])
$this->nMaxHeight = $wh[1];
}
}
function getCacheFilePath($cText)
{
$hash = md5($this->cFont . $this->nSize . $this->cColor . $cText) . '.png';
return $this->cCacheDir . '/' . $hash;
}
function getImageFilePath($char)
{
$wh = $this->calcRect($char);
$cColor = $this->html2rgb($this->cColor);
if (!is_array($cColor))
$cColor = array(0, 0, 0);
$filePath = $this->getCacheFilePath($char);
if (!is_file($filePath))
{
$image = @ImageCreate($wh[0] + $this->nPadding, $this->nMaxHeight + $this->nPadding);
$background = ImageColorAllocate($image, 255, 255, 255);
$cTextColor = ImageColorAllocate($image, $cColor[0], $cColor[1], $cColor[2]);
ImageColorTransparent($image, $background);
ImageTTFText($image, $this->nSize, 0, $wh[2] + $this->nPadding / 2, $this->nMaxHeight + $this->nPadding / 2,
$cTextColor, $this->cFontDir . '/' . $this->cFont, $char);
ImagePNG($image, $filePath);
}
return $filePath;
}
function asArray($cText)
{
$list = array();
$this->calcMax($cText);
for ($i = 0; $i < strlen($cText); $i++)
{
if ($cText[$i] == ' ')
$list[] = false;
else
$list[] = $this->getImageFilePath($cText[$i]);
}
return $list;
}
function asHTML($cText)
{
$html = '';
$list = $this->asArray($cText);
$width = ($this->nMaxWidth + $this->nMinWidth) / 2;
foreach ($list as $l)
{
if ($l)
$html .= '<img src="' . $l . '" border=0 />';
else
$html .= '<span style="display:inline-block;width:'.$width.'px"></span>';//'<img src="blank.png" style="width:' . $width . 'px" border=0 />';
}
return $html;
}
}