xref: /plugin/captcha/figlet.php (revision 186227361d0efe9f0e20bd52c87dfbf939efb53a)
10110319eSAndreas Gohr<?php
20110319eSAndreas Gohr/*        _         _____  _       _       _
30110319eSAndreas Gohr *   ___ | |_  ___ |   __||_| ___ | | ___ | |_
40110319eSAndreas Gohr *  | . ||   || . ||   __|| || . || || -_||  _|
50110319eSAndreas Gohr *  |  _||_|_||  _||__|   |_||_  ||_||___||_|
60110319eSAndreas Gohr *  |_|       |_|            |___|
70110319eSAndreas Gohr *
80110319eSAndreas Gohr *  Author   :      Lucas Baltes (lucas@thebobo.com)
90110319eSAndreas Gohr *                  $Author: lhb $
100110319eSAndreas Gohr *
110110319eSAndreas Gohr *  Website  :      http://www.thebobo.com/
120110319eSAndreas Gohr *
130110319eSAndreas Gohr *  Date     :      $Date: 2003/03/16 10:08:01 $
140110319eSAndreas Gohr *  Rev      :      $Revision: 1.0 $
150110319eSAndreas Gohr *
160110319eSAndreas Gohr *  Copyright:      2003 - Lucas Baltes
170110319eSAndreas Gohr *  License  :      GPL - http://www.gnu.org/licenses/gpl.html
180110319eSAndreas Gohr *
190110319eSAndreas Gohr *  Purpose  :      Figlet font class
200110319eSAndreas Gohr *
210110319eSAndreas Gohr *  Comments :      phpFiglet is a php class to somewhat recreate the
220110319eSAndreas Gohr *                  functionality provided by the original figlet program
230110319eSAndreas Gohr *                  (http://www.figlet.org/). It does not (yet) support the
240110319eSAndreas Gohr *                  more advanced features like kerning or smushing. It can
250110319eSAndreas Gohr *                  use the same (flf2a) fonts as the original figlet program
260110319eSAndreas Gohr *                  (see their website for more fonts).
270110319eSAndreas Gohr *
280110319eSAndreas Gohr *  Usage    :      $phpFiglet = new phpFiglet();
290110319eSAndreas Gohr *
300110319eSAndreas Gohr *                  if ($phpFiglet->loadFont("fonts/standard.flf")) {
310110319eSAndreas Gohr *                      $phpFiglet->display("Hello World");
320110319eSAndreas Gohr *                  } else {
330110319eSAndreas Gohr *                      trigger_error("Could not load font file");
340110319eSAndreas Gohr *                  }
350110319eSAndreas Gohr *
360110319eSAndreas Gohr */
370110319eSAndreas Gohr
380110319eSAndreas Gohrclass phpFiglet
390110319eSAndreas Gohr{
400110319eSAndreas Gohr
410110319eSAndreas Gohr    /*
420110319eSAndreas Gohr     *  Internal variables
430110319eSAndreas Gohr     */
440110319eSAndreas Gohr
450110319eSAndreas Gohr    var $signature;
460110319eSAndreas Gohr    var $hardblank;
470110319eSAndreas Gohr    var $height;
480110319eSAndreas Gohr    var $baseline;
490110319eSAndreas Gohr    var $maxLenght;
500110319eSAndreas Gohr    var $oldLayout;
510110319eSAndreas Gohr    var $commentLines;
520110319eSAndreas Gohr    var $printDirection;
530110319eSAndreas Gohr    var $fullLayout;
540110319eSAndreas Gohr    var $codeTagCount;
550110319eSAndreas Gohr    var $fontFile;
560110319eSAndreas Gohr
570110319eSAndreas Gohr    /*
580110319eSAndreas Gohr     *  Contructor
590110319eSAndreas Gohr     */
600110319eSAndreas Gohr
610110319eSAndreas Gohr    function phpFiglet()
620110319eSAndreas Gohr    {
630110319eSAndreas Gohr
640110319eSAndreas Gohr    }
650110319eSAndreas Gohr
660110319eSAndreas Gohr    /*
670110319eSAndreas Gohr     *  Load an flf font file. Return true on success, false on error.
680110319eSAndreas Gohr     */
690110319eSAndreas Gohr
700110319eSAndreas Gohr    function loadfont($fontfile)
710110319eSAndreas Gohr    {
720110319eSAndreas Gohr        $this->fontFile = @file($fontfile);
730110319eSAndreas Gohr        if (!$this->fontFile) return false;
740110319eSAndreas Gohr
750110319eSAndreas Gohr        $hp = explode(" ", $this->fontFile[0]); // get header
760110319eSAndreas Gohr
770110319eSAndreas Gohr        $this->signature = substr($hp[0], 0, strlen($hp[0]) - 1);
780110319eSAndreas Gohr        $this->hardblank = substr($hp[0], strlen($hp[0]) - 1, 1);
790110319eSAndreas Gohr        $this->height = $hp[1];
800110319eSAndreas Gohr        $this->baseline = $hp[2];
810110319eSAndreas Gohr        $this->maxLenght = $hp[3];
820110319eSAndreas Gohr        $this->oldLayout = $hp[4];
830110319eSAndreas Gohr        $this->commentLines = $hp[5] + 1;
840110319eSAndreas Gohr        $this->printDirection = $hp[6];
850110319eSAndreas Gohr        $this->fullLayout = $hp[7];
860110319eSAndreas Gohr        $this->codeTagCount = $hp[8];
870110319eSAndreas Gohr
880110319eSAndreas Gohr        unset($hp);
890110319eSAndreas Gohr
900110319eSAndreas Gohr        if ($this->signature != "flf2a") {
910110319eSAndreas Gohr            return false;
920110319eSAndreas Gohr        } else {
930110319eSAndreas Gohr            return true;
940110319eSAndreas Gohr        }
950110319eSAndreas Gohr    }
960110319eSAndreas Gohr
970110319eSAndreas Gohr    /*
980110319eSAndreas Gohr     *  Get a character as a string, or an array with one line
990110319eSAndreas Gohr     *  for each font height.
1000110319eSAndreas Gohr     */
1010110319eSAndreas Gohr
1020110319eSAndreas Gohr    function getCharacter($character, $asarray = false)
1030110319eSAndreas Gohr    {
1040110319eSAndreas Gohr        $asciValue = ord($character);
1050110319eSAndreas Gohr        $start = $this->commentLines + ($asciValue - 32) * $this->height;
1060110319eSAndreas Gohr        $data = ($asarray) ? array() : "";
1070110319eSAndreas Gohr
108*18622736SAndreas Gohr        for ($a = 0; $a < $this->height; $a++) {
1090110319eSAndreas Gohr            $tmp = $this->fontFile[$start + $a];
1100110319eSAndreas Gohr            $tmp = str_replace("@", "", $tmp);
1110110319eSAndreas Gohr            //$tmp = trim($tmp);
1120110319eSAndreas Gohr            $tmp = str_replace($this->hardblank, " ", $tmp);
1130110319eSAndreas Gohr
1140110319eSAndreas Gohr            if ($asarray) {
1150110319eSAndreas Gohr                $data[] = $tmp;
1160110319eSAndreas Gohr            } else {
1170110319eSAndreas Gohr                $data .= $tmp;
1180110319eSAndreas Gohr            }
1190110319eSAndreas Gohr        }
1200110319eSAndreas Gohr
1210110319eSAndreas Gohr        return $data;
1220110319eSAndreas Gohr    }
1230110319eSAndreas Gohr
1240110319eSAndreas Gohr    /*
1250110319eSAndreas Gohr     *  Returns a figletized line of characters.
1260110319eSAndreas Gohr     */
1270110319eSAndreas Gohr
1280110319eSAndreas Gohr    function fetch($line)
1290110319eSAndreas Gohr    {
1300110319eSAndreas Gohr        $ret = "";
1310110319eSAndreas Gohr
132*18622736SAndreas Gohr        for ($i = 0; $i < (strlen($line)); $i++) {
1330110319eSAndreas Gohr            $data[] = $this->getCharacter($line[$i], true);
1340110319eSAndreas Gohr        }
1350110319eSAndreas Gohr
1360110319eSAndreas Gohr        @reset($data);
1370110319eSAndreas Gohr
138*18622736SAndreas Gohr        for ($i = 0; $i < $this->height; $i++) {
139*18622736SAndreas Gohr            while (list($k, $v) = each($data)) {
1400110319eSAndreas Gohr                $ret .= str_replace("\n", "", $v[$i]);
1410110319eSAndreas Gohr            }
1420110319eSAndreas Gohr            reset($data);
1430110319eSAndreas Gohr            $ret .= "\n";
1440110319eSAndreas Gohr        }
1450110319eSAndreas Gohr
1460110319eSAndreas Gohr        return $ret;
1470110319eSAndreas Gohr    }
1480110319eSAndreas Gohr
1490110319eSAndreas Gohr    /*
1500110319eSAndreas Gohr     *  Display (print) a figletized line of characters.
1510110319eSAndreas Gohr     */
1520110319eSAndreas Gohr
1530110319eSAndreas Gohr    function display($line)
1540110319eSAndreas Gohr    {
1550110319eSAndreas Gohr        print $this->fetch($line);
1560110319eSAndreas Gohr    }
1570110319eSAndreas Gohr
1580110319eSAndreas Gohr}
159*18622736SAndreas Gohr
1600110319eSAndreas Gohr?>
161