xref: /plugin/captcha/figlet.php (revision 09b1e97e3cb9f2c4be8ca729baa9d49a3ba58ba1)
10110319eSAndreas Gohr<?php
2*09b1e97eSAndreas Gohr
30110319eSAndreas Gohr/*        _         _____  _       _       _
40110319eSAndreas Gohr *   ___ | |_  ___ |   __||_| ___ | | ___ | |_
50110319eSAndreas Gohr *  | . ||   || . ||   __|| || . || || -_||  _|
60110319eSAndreas Gohr *  |  _||_|_||  _||__|   |_||_  ||_||___||_|
70110319eSAndreas Gohr *  |_|       |_|            |___|
80110319eSAndreas Gohr *
90110319eSAndreas Gohr *  Author   :      Lucas Baltes (lucas@thebobo.com)
100110319eSAndreas Gohr *                  $Author: lhb $
110110319eSAndreas Gohr *
120110319eSAndreas Gohr *  Website  :      http://www.thebobo.com/
130110319eSAndreas Gohr *
140110319eSAndreas Gohr *  Date     :      $Date: 2003/03/16 10:08:01 $
150110319eSAndreas Gohr *  Rev      :      $Revision: 1.0 $
160110319eSAndreas Gohr *
170110319eSAndreas Gohr *  Copyright:      2003 - Lucas Baltes
180110319eSAndreas Gohr *  License  :      GPL - http://www.gnu.org/licenses/gpl.html
190110319eSAndreas Gohr *
200110319eSAndreas Gohr *  Purpose  :      Figlet font class
210110319eSAndreas Gohr *
220110319eSAndreas Gohr *  Comments :      phpFiglet is a php class to somewhat recreate the
230110319eSAndreas Gohr *                  functionality provided by the original figlet program
240110319eSAndreas Gohr *                  (http://www.figlet.org/). It does not (yet) support the
250110319eSAndreas Gohr *                  more advanced features like kerning or smushing. It can
260110319eSAndreas Gohr *                  use the same (flf2a) fonts as the original figlet program
270110319eSAndreas Gohr *                  (see their website for more fonts).
280110319eSAndreas Gohr *
290110319eSAndreas Gohr *  Usage    :      $phpFiglet = new phpFiglet();
300110319eSAndreas Gohr *
310110319eSAndreas Gohr *                  if ($phpFiglet->loadFont("fonts/standard.flf")) {
320110319eSAndreas Gohr *                      $phpFiglet->display("Hello World");
330110319eSAndreas Gohr *                  } else {
340110319eSAndreas Gohr *                      trigger_error("Could not load font file");
350110319eSAndreas Gohr *                  }
360110319eSAndreas Gohr *
370110319eSAndreas Gohr */
380110319eSAndreas Gohr
390110319eSAndreas Gohrclass phpFiglet
400110319eSAndreas Gohr{
410110319eSAndreas Gohr    /*
420110319eSAndreas Gohr     *  Internal variables
430110319eSAndreas Gohr     */
440110319eSAndreas Gohr
45*09b1e97eSAndreas Gohr    public $signature;
46*09b1e97eSAndreas Gohr    public $hardblank;
47*09b1e97eSAndreas Gohr    public $height;
48*09b1e97eSAndreas Gohr    public $baseline;
49*09b1e97eSAndreas Gohr    public $maxLenght;
50*09b1e97eSAndreas Gohr    public $oldLayout;
51*09b1e97eSAndreas Gohr    public $commentLines;
52*09b1e97eSAndreas Gohr    public $printDirection;
53*09b1e97eSAndreas Gohr    public $fullLayout;
54*09b1e97eSAndreas Gohr    public $codeTagCount;
55*09b1e97eSAndreas Gohr    public $fontFile;
560110319eSAndreas Gohr
570110319eSAndreas Gohr    /*
580110319eSAndreas Gohr     *  Load an flf font file. Return true on success, false on error.
590110319eSAndreas Gohr     */
600110319eSAndreas Gohr
61*09b1e97eSAndreas Gohr    public function loadfont($fontfile)
620110319eSAndreas Gohr    {
630110319eSAndreas Gohr        $this->fontFile = @file($fontfile);
640110319eSAndreas Gohr        if (!$this->fontFile) return false;
650110319eSAndreas Gohr
660110319eSAndreas Gohr        $hp = explode(" ", $this->fontFile[0]); // get header
670110319eSAndreas Gohr
680110319eSAndreas Gohr        $this->signature = substr($hp[0], 0, strlen($hp[0]) - 1);
690110319eSAndreas Gohr        $this->hardblank = substr($hp[0], strlen($hp[0]) - 1, 1);
700110319eSAndreas Gohr        $this->height = $hp[1];
710110319eSAndreas Gohr        $this->baseline = $hp[2];
720110319eSAndreas Gohr        $this->maxLenght = $hp[3];
730110319eSAndreas Gohr        $this->oldLayout = $hp[4];
740110319eSAndreas Gohr        $this->commentLines = $hp[5] + 1;
750110319eSAndreas Gohr        $this->printDirection = $hp[6];
760110319eSAndreas Gohr        $this->fullLayout = $hp[7];
770110319eSAndreas Gohr        $this->codeTagCount = $hp[8];
780110319eSAndreas Gohr
790110319eSAndreas Gohr        unset($hp);
800110319eSAndreas Gohr
810110319eSAndreas Gohr        if ($this->signature != "flf2a") {
820110319eSAndreas Gohr            return false;
830110319eSAndreas Gohr        } else {
840110319eSAndreas Gohr            return true;
850110319eSAndreas Gohr        }
860110319eSAndreas Gohr    }
870110319eSAndreas Gohr
880110319eSAndreas Gohr    /*
890110319eSAndreas Gohr     *  Get a character as a string, or an array with one line
900110319eSAndreas Gohr     *  for each font height.
910110319eSAndreas Gohr     */
920110319eSAndreas Gohr
93*09b1e97eSAndreas Gohr    public function getCharacter($character, $asarray = false)
940110319eSAndreas Gohr    {
950110319eSAndreas Gohr        $asciValue = ord($character);
960110319eSAndreas Gohr        $start = $this->commentLines + ($asciValue - 32) * $this->height;
97*09b1e97eSAndreas Gohr        $data = ($asarray) ? [] : "";
980110319eSAndreas Gohr
9918622736SAndreas Gohr        for ($a = 0; $a < $this->height; $a++) {
1000110319eSAndreas Gohr            $tmp = $this->fontFile[$start + $a];
1010110319eSAndreas Gohr            $tmp = str_replace("@", "", $tmp);
1020110319eSAndreas Gohr            //$tmp = trim($tmp);
1030110319eSAndreas Gohr            $tmp = str_replace($this->hardblank, " ", $tmp);
1040110319eSAndreas Gohr
1050110319eSAndreas Gohr            if ($asarray) {
1060110319eSAndreas Gohr                $data[] = $tmp;
1070110319eSAndreas Gohr            } else {
1080110319eSAndreas Gohr                $data .= $tmp;
1090110319eSAndreas Gohr            }
1100110319eSAndreas Gohr        }
1110110319eSAndreas Gohr
1120110319eSAndreas Gohr        return $data;
1130110319eSAndreas Gohr    }
1140110319eSAndreas Gohr
1150110319eSAndreas Gohr    /*
1160110319eSAndreas Gohr     *  Returns a figletized line of characters.
1170110319eSAndreas Gohr     */
1180110319eSAndreas Gohr
119*09b1e97eSAndreas Gohr    public function fetch($line)
1200110319eSAndreas Gohr    {
1210110319eSAndreas Gohr        $ret = "";
1220110319eSAndreas Gohr
12318622736SAndreas Gohr        for ($i = 0; $i < (strlen($line)); $i++) {
1240110319eSAndreas Gohr            $data[] = $this->getCharacter($line[$i], true);
1250110319eSAndreas Gohr        }
1260110319eSAndreas Gohr
1270110319eSAndreas Gohr        @reset($data);
1280110319eSAndreas Gohr
12918622736SAndreas Gohr        for ($i = 0; $i < $this->height; $i++) {
130*09b1e97eSAndreas Gohr            foreach ($data as $v) {
1310110319eSAndreas Gohr                $ret .= str_replace("\n", "", $v[$i]);
1320110319eSAndreas Gohr            }
1330110319eSAndreas Gohr            reset($data);
1340110319eSAndreas Gohr            $ret .= "\n";
1350110319eSAndreas Gohr        }
1360110319eSAndreas Gohr
1370110319eSAndreas Gohr        return $ret;
1380110319eSAndreas Gohr    }
1390110319eSAndreas Gohr
1400110319eSAndreas Gohr    /*
1410110319eSAndreas Gohr     *  Display (print) a figletized line of characters.
1420110319eSAndreas Gohr     */
1430110319eSAndreas Gohr
144*09b1e97eSAndreas Gohr    public function display($line)
1450110319eSAndreas Gohr    {
146*09b1e97eSAndreas Gohr        echo $this->fetch($line);
1470110319eSAndreas Gohr    }
1480110319eSAndreas Gohr}
149