Source for file Figlet.php
Documentation is available at Figlet.php
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Evgeny Stepanischev <se@timelabs.ru> |
// +----------------------------------------------------------------------+
// Project home page (Russian): http://bolk.exler.ru/files/figlet/
// $Id: Figlet.php,v 1.1 2004/11/09 17:17:55 bolk Exp $
* Flag - RTL (right to left) or LTR (left to right) text direction
* Information about special 'hardblank' character
* Is used for keeping font
* Flag is true if smushing occured in letters printing cycle
* Load user font. Must be invoked first.
* @param string $filename font file name
* @param bool $loadgerman (optional) load German character set or not
* @return mixed PEAR_error or true for success
function loadFont ($filename, $loadgerman = true )
return PEAR ::raiseError ('File is not found', 1 );
// If Gzip compressed font
if (substr($filename, -3 , 3 ) == '.gz') {
$filename = " compress.zlib://$filename";
return PEAR ::raiseError ('Unknown FIGlet font format.', 3 );
if ($fp = fopen($filename, 'rb')) {
if (!$compressed) flock($fp, LOCK_SH );
// flf2a$ 6 5 20 15 3 0 143 229
// Signature / / | | | | | \ Codetag_Count
// Hardblank / / | | | \ Full_Layout
// Height / | | \ Print_Direction
// Baseline / \ Comment_Lines
if (substr($header[0 ], 0 , 5 ) <> 'flf2a') {
return PEAR ::raiseError ('Unknown FIGlet font format.', 3 );
@list ($this->hardblank, $this->height,,,
$this->oldlayout, $cmt_count, $this->rtol) = $header;
$this->hardblank = substr($this->hardblank, -1 , 1 );
for ($i = 0; $i< $cmt_count; $i++ ) {
for ($i = 32; $i<127; $i++ ) {
$this->font[$i] = $this->_char ($fp);
foreach (array (91 , 92 , 93 , 123 , 124 , 125 , 126 ) as $i) {
$letter = $this->_char ($fp);
// Invalid character but main font is loaded and I can use it
// Load if it is not blank only
$this->font[$i] = $letter;
for ($n = 0; !feof($fp); $n++ ) {
if ($i{0 } === '0' && $i !== '0' || substr($i, 0 , 2 ) == '-0') {
$letter = $this->_char ($fp);
// Invalid character but main font is loaded and I can use it
$this->font[$i] = $letter;
return PEAR ::raiseError ('Cannot open font file', 2 );
* Print string using font loaded by LoadFont method
* @param string $str string for printing
* @param bool $inhtml (optional) output mode - HTML (true) or plain text (false)
* @return string contains
function lineEcho ($str, $inhtml = false )
for ($i = 0; $i< strlen($str); $i++ ) {
// Pseudo Unicode support
if (substr($str, $i, 2 ) == '%u') {
// If chosen character not found try to use default
// If default character is not defined skip it
if (!isset ($this->font[$lt])) {
if (isset ($this->font[0 ])) {
for ($j = 0; $j< $this->height; $j++ ) {
$line = $this->font[$lt][$j];
$out[$j] = $line . $out[$j]; else
if ($this->oldlayout > -1 && $i) {
// Calculate minimal distance between two last letters
for ($j = 0; $j< $this->height; $j++ ) {
if (preg_match("/\S(\s*\\x00\s*)\S/", $out[$j], $r)) {
// Remove spaces between two last letter
// dec mindiff for exclude \x00 symbol
for ($j = 0; $j< $this->height; $j++ ) {
if (preg_match(" /\\x00(\s{0,{$mindiff}})/" , $out[$j], $r)) {
$b = $mindiff - ($l = strlen($r[1 ]));
$out[$j] = preg_replace(" /\s{0,$b}\\x00\s{{$l}}/" , "\0", $out[$j], 1 );
for ($j = 0; $j< $this->height; $j++ ) {
(" #([^$sp])\\x00([^$sp])#" , array (&$this, '_rep'), $out[$j]);
// Remove one space if smushing
// and remove all \x00 except tail whenever
$pat = array ("/\s\\x00(?!$)|\\x00\s/", "/\\x00(?!$)/");
for ($j = 0; $j< $this->height; $j++ ) {
$trans = array ("\0" => '', $this->hardblank => ' ');
* It is preg_replace callback function that makes horizontal letter smushing
if ($this->oldlayout & 1 && $r[1 ] == $r[2 ]) {
if ($this->oldlayout & 2 ) {
if ($r[1 ] == '_' && strpos($symb, $r[2 ]) !== FALSE ||
$r[2 ] == '_' && strpos($symb, $r[1 ]) !== FALSE ) {
if ($this->oldlayout & 4 ) {
$classes = '|/\\[]{}()<>';
if (($left = strpos($classes, $r[1 ])) !== FALSE ) {
if (($right = strpos($classes, $r[2 ])) !== FALSE ) {
return $right > $left ? $r[2 ] : $r[1 ];
if ($this->oldlayout & 8 ) {
$t = array ('[' => ']', ']' => '[', '{' => '}', '}' => '{',
if (isset ($t[$r[2 ]]) && $r[1 ] == $t[$r[2 ]]) {
if ($this->oldlayout & 16 ) {
$t = array ("/\\" => '|', "\\/" => 'Y', '><' => 'X');
if (isset ($t[$r[1 ]. $r[2 ]])) {
if ($this->oldlayout & 32 ) {
if ($r[1 ] == $r[2 ] && $r[1 ] == $this->hardblank) {
return $r[1 ]. "\00". $r[2 ];
* Function loads one character in the internal array from file
* @param resource $fp handle of font file
* @return mixed lines of the character or false if foef occured
for ($i = 0; $i < $this->height; $i++ ) {
* Function for skipping one character in a font file
* @param resource $fp handle of font file
* @return bool always return true
for ($i = 0; $i< $this->height && !feof($fp); $i++ ) {
Documentation generated on Mon, 11 Mar 2019 13:59:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|