Text_Figlet
[ class tree: Text_Figlet ] [ index: Text_Figlet ] [ all elements ]

Source for file Figlet.php

Documentation is available at Figlet.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2002 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Evgeny Stepanischev <se@timelabs.ru>                        |
  18. // +----------------------------------------------------------------------+
  19. // Project home page (Russian): http://bolk.exler.ru/files/figlet/
  20. //
  21. // $Id: Figlet.php,v 1.1 2004/11/09 17:17:55 bolk Exp $
  22.  
  23. require_once "PEAR.php";
  24.  
  25. class Text_Figlet
  26. {
  27.     /**
  28.      * Height of a letter
  29.      *
  30.      * @var integer 
  31.      *
  32.      * @access private
  33.      */
  34.  
  35.     var $height;
  36.  
  37.     /**
  38.      * Letter baseline
  39.      *
  40.      * @var integer 
  41.      *
  42.      * @access private
  43.      */
  44.  
  45.     var $oldlayout;
  46.  
  47.     /**
  48.      * Flag - RTL (right to left) or LTR (left to right) text direction
  49.      *
  50.      * @var integer 
  51.      *
  52.      * @access private
  53.      */
  54.  
  55.     var $rtol;
  56.  
  57.     /**
  58.      * Information about special 'hardblank' character
  59.      *
  60.      * @var integer 
  61.      *
  62.      * @access private
  63.      */
  64.  
  65.     var $hardblank;
  66.  
  67.     /**
  68.      * Is used for keeping font
  69.      *
  70.      * @var array 
  71.      *
  72.      * @access private
  73.      */
  74.  
  75.     var $font;
  76.  
  77.     /**
  78.      * Flag is true if smushing occured in letters printing cycle
  79.      *
  80.      * @var integer 
  81.      *
  82.      * @access private
  83.      */
  84.  
  85.     var $smush_flag;
  86.  
  87.     /**
  88.      * Load user font. Must be invoked first.
  89.      *
  90.      * @param string $filename font file name
  91.      * @param bool $loadgerman (optional) load German character set or not
  92.      * @access public
  93.      * @return mixed PEAR_error or true for success
  94.      */
  95.  
  96.     function loadFont($filename$loadgerman = true)
  97.     {
  98.         $this->font = array();
  99.         if (!file_exists($filename))
  100.         return PEAR::raiseError('File is not found'1);
  101.  
  102.         // If Gzip compressed font
  103.         if (substr($filename-33== '.gz'{
  104.             $filename = "compress.zlib://$filename";
  105.             $compressed = true;
  106.  
  107.             if (!function_exists('gzcompress')) {
  108.                 return PEAR::raiseError('Unknown FIGlet font format.'3);
  109.             }
  110.         else {
  111.             $compressed = false;
  112.         }
  113.  
  114.         if ($fp fopen($filename'rb')) {
  115.             if (!$compressedflock($fpLOCK_SH);
  116.  
  117. //            flf2a$ 6 5 20 15 3 0 143 229
  118. //              |  | | | |  |  | |  |   |
  119. //             /  /  | | |  |  | |  |   \
  120. //    Signature  /  /  | |  |  | |   \   Codetag_Count
  121. //      Hardblank  /  /  |  |  |  \   Full_Layout
  122. //           Height  /   |  |   \  Print_Direction
  123. //           Baseline   /    \   Comment_Lines
  124. //            Max_Length      Old_Layout
  125.  
  126.  
  127.             $header explode(' 'fgets($fp2048));
  128.  
  129.             if (substr($header[0]05<> 'flf2a'{
  130.                 return PEAR::raiseError('Unknown FIGlet font format.'3);
  131.             }
  132.  
  133.             @list ($this->hardblank$this->height,,,
  134.             $this->oldlayout$cmt_count$this->rtol$header;
  135.  
  136.             $this->hardblank substr($this->hardblank-11);
  137.  
  138.             for ($i = 0; $i<$cmt_count$i++{
  139.                 fgets($fp2048);
  140.             }
  141.  
  142.             // ASCII charcters
  143.             for ($i = 32; $i<127; $i++{
  144.                 $this->font[$i$this->_char($fp);
  145.             }
  146.  
  147.             foreach (array(919293123124125126as $i{
  148.                 if ($loadgerman{
  149.                     $letter $this->_char($fp);
  150.  
  151.                     // Invalid character but main font is loaded and I can use it
  152.                     if ($letter === false{
  153.                         fclose($fp);
  154.                         return true;
  155.                     }
  156.  
  157.                     // Load if it is not blank only
  158.                     if (trim(implode(''$letter)) <> '')
  159.                     $this->font[$i$letter;
  160.                 else {
  161.                     $this->_skip($fp);
  162.                 }
  163.             }
  164.  
  165.             // Extented characters
  166.             for ($n = 0; !feof($fp)$n++{
  167.                 list ($iexplode(' 'rtrim(fgets($fp1024))2);
  168.                 if ($i == ''{
  169.                     continue;
  170.                 }
  171.  
  172.                 // If comment
  173.                 if (preg_match('/^\-0x/i'$i)) {
  174.                     $this->_skip($fp);
  175.                 else {
  176.                     // If Unicode
  177.                     if (preg_match('/^0x/i'$i)) {
  178.                         $i hexdec(substr($i2));
  179.                     else {
  180.                     // If octal
  181.                         if ($i{0=== '0' && $i !== '0' || substr($i02== '-0'{
  182.                             $i octdec($i);
  183.                         }
  184.                     }
  185.  
  186.                     $letter $this->_char($fp);
  187.  
  188.                     // Invalid character but main font is loaded and I can use it
  189.                     if ($letter === FALSE{
  190.                         fclose($fp);
  191.                         return true;
  192.                     }
  193.  
  194.                     $this->font[$i$letter;
  195.                 }
  196.             }
  197.  
  198.             fclose($fp);
  199.             return true;
  200.         else {
  201.             return PEAR::raiseError('Cannot open font file'2);
  202.         }
  203.     }
  204.  
  205.     /**
  206.     * Print string using font loaded by LoadFont method
  207.     *
  208.     * @param string $str string for printing
  209.     * @param bool $inhtml (optional) output mode - HTML (true) or plain text (false)
  210.     * @access public
  211.     * @return string contains
  212.     */
  213.  
  214.     function lineEcho($str$inhtml = false)
  215.     {
  216.         $out = array();
  217.  
  218.         for ($i = 0; $i<strlen($str)$i++{
  219.             // Pseudo Unicode support
  220.             if (substr($str$i2== '%u'{
  221.                 $lt hexdec(substr($str$i+24));
  222.                 $i += 6;
  223.             else {
  224.                 $lt ord($str{$i});
  225.             }
  226.  
  227.             $hb preg_quote($this->hardblank'/');
  228.             $sp = "$hb\\x00\\s";
  229.  
  230.             // If chosen character not found try to use default
  231.             // If default character is not defined skip it
  232.  
  233.             if (!isset($this->font[$lt])) {
  234.                 if (isset($this->font[0])) {
  235.                     $lt = 0;
  236.                 else {
  237.                     continue;
  238.                 }
  239.             }
  240.  
  241.             for ($j = 0; $j<$this->height$j++{
  242.                 $line $this->font[$lt][$j];
  243.  
  244.                 // Replace hardblanks
  245.                 if (isset($out[$j])) {
  246.                     if ($this->rtol)
  247.                     $out[$j$line $out[$j]; else
  248.                     $out[$j].= $line;
  249.                 else {
  250.                     $out[$j$line;
  251.                 }
  252.             }
  253.  
  254.             if ($this->oldlayout > -1 && $i{
  255.                 // Calculate minimal distance between two last letters
  256.  
  257.                 $mindiff = -1;
  258.  
  259.                 for ($j = 0; $j<$this->height$j++{
  260.                     if (preg_match("/\S(\s*\\x00\s*)\S/"$out[$j]$r)) {
  261.                         $mindiff $mindiff == -1 ? strlen($r[1]min($mindiffstrlen($r[1]));
  262.                     }
  263.                 }
  264.  
  265.                 // Remove spaces between two last letter
  266.                 // dec mindiff for exclude \x00 symbol
  267.  
  268.                 if (--$mindiff > 0{
  269.                     for ($j = 0; $j<$this->height$j++{
  270.                         if (preg_match("/\\x00(\s{0,{$mindiff}})/"$out[$j]$r)) {
  271.                             $b $mindiff ($l strlen($r[1]));
  272.                             $out[$jpreg_replace("/\s{0,$b}\\x00\s{{$l}}/""\0"$out[$j]1);
  273.  
  274.                         }
  275.                     }
  276.                 }
  277.                 // Smushing
  278.  
  279.                 $this->smush_flag = 0;
  280.  
  281.                 for ($j = 0; $j<$this->height$j++{
  282.                     $out[$j
  283.                     preg_replace_callback
  284.                     ("#([^$sp])\\x00([^$sp])#"array(&$this'_rep')$out[$j]);
  285.                 }
  286.  
  287.                 // Remove one space if smushing
  288.                 // and remove all \x00 except tail whenever
  289.  
  290.                 if ($this->smush_flag{
  291.                     $pat = array("/\s\\x00(?!$)|\\x00\s/""/\\x00(?!$)/");
  292.                     $rep = array('''');
  293.                 else {
  294.                     $pat "/\\x00(?!$)/";
  295.                     $rep '';
  296.                 }
  297.  
  298.                 for ($j = 0; $j<$this->height$j++{
  299.                     $out[$jpreg_replace($pat$rep$out[$j]);
  300.                 }
  301.             }
  302.         }
  303.  
  304.         $trans = array("\0" => ''$this->hardblank => ' ');
  305.         $str strtr(implode("\n"$out)$trans);
  306.  
  307.         if ($inhtml{
  308.             return '<nobr>'.
  309.                    nl2br(str_replace(' ''&nbsp;'htmlspecialchars($str))).
  310.                    '</nobr>';
  311.         }
  312.  
  313.         return $str;
  314.     }
  315.  
  316.     /**
  317.     * It is preg_replace callback function that makes horizontal letter smushing
  318.     *
  319.     * @param array $r 
  320.     * @return string 
  321.     * @access private
  322.     */
  323.  
  324.     function _rep($r)
  325.     {
  326.         if ($this->oldlayout 1 && $r[1== $r[2]{
  327.             $this->smush_flag = 1;
  328.             return $r[1];
  329.         }
  330.  
  331.         if ($this->oldlayout 2{
  332.             $symb '|/\\[]{}()<>';
  333.  
  334.             if ($r[1== '_' && strpos($symb$r[2]!== FALSE ||
  335.                 $r[2== '_' && strpos($symb$r[1]!== FALSE{
  336.                 $this->smush_flag = 1;
  337.                 return $r[1];
  338.             }
  339.         }
  340.  
  341.         if ($this->oldlayout 4{
  342.             $classes '|/\\[]{}()<>';
  343.  
  344.             if (($left strpos($classes$r[1])) !== FALSE{
  345.                 if (($right strpos($classes$r[2])) !== FALSE{
  346.                     $this->smush_flag = 1;
  347.                     return $right $left $r[2$r[1];
  348.                 }
  349.             }
  350.         }
  351.  
  352.         if ($this->oldlayout 8{
  353.             $t = array('[' => ']'']' => '[''{' => '}''}' => '{',
  354.             '(' => ')'')' => '(');
  355.  
  356.             if (isset($t[$r[2]]&& $r[1== $t[$r[2]]{
  357.                 $this->smush_flag = 1;
  358.                 return '|';
  359.             }
  360.         }
  361.  
  362.         if ($this->oldlayout 16{
  363.             $t = array("/\\" => '|'"\\/" => 'Y''><' => 'X');
  364.  
  365.             if (isset($t[$r[1].$r[2]])) {
  366.                 $this->smush_flag = 1;
  367.                 return $t[$r[1].$r[2]];
  368.             }
  369.         }
  370.  
  371.         if ($this->oldlayout 32{
  372.             if ($r[1== $r[2&& $r[1== $this->hardblank{
  373.                 $this->smush_flag = 1;
  374.                 return $this->hardblank;
  375.             }
  376.         }
  377.  
  378.         return $r[1]."\00".$r[2];
  379.     }
  380.  
  381.  
  382.     /**
  383.     * Function loads one character in the internal array from file
  384.     *
  385.     * @param resource $fp handle of font file
  386.     * @return mixed lines of the character or false if foef occured
  387.     * @access private
  388.     */
  389.  
  390.     function _char(&$fp)
  391.     {
  392.         $out = array();
  393.  
  394.         for ($i = 0; $i $this->height$i++{
  395.             if (feof($fp)) {
  396.                 return false;
  397.             }
  398.  
  399.             $line rtrim(fgets($fp2048)"\r\n");
  400.             if (preg_match('/(.){1,2}$/'$line$r)) {
  401.                 $line str_replace($r[1]''$line);
  402.             }
  403.  
  404.             $line .= "\x00";
  405.  
  406.             $out[$line;
  407.         }
  408.                                                                             
  409.         return $out;
  410.     }
  411.  
  412.     /**
  413.     * Function for skipping one character in a font file
  414.     *
  415.     * @param resource $fp handle of font file
  416.     * @return bool always return true
  417.     * @access private
  418.     */
  419.  
  420.     function _skip(&$fp)
  421.     {
  422.         for ($i = 0; $i<$this->height && !feof($fp)$i++{
  423.             fgets($fp2048);
  424.         }
  425.  
  426.         return true;
  427.     }
  428. }
  429. ?>

Documentation generated on Mon, 11 Mar 2019 13:59:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.