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

Source for file UCS2.php

Documentation is available at UCS2.php

  1. <?php
  2. /**
  3.  * API class that controls UCS2 for KSC5601 package
  4.  *
  5.  * @category   Charset
  6.  * @package    KSC5601
  7.  * @subpackage KSC5601_pure
  8.  * @author     JoungKyun.Kim <http://oops.org>
  9.  * @copyright  (c) 2015, JoungKyun.Kim
  10.  * @license    BSD License
  11.  * @version    $Id$
  12.  * @link       http://pear.oops.org/package/KSC5601
  13.  * @filesource
  14.  */
  15.  
  16. /**
  17.  * import High level API for convert character set
  18.  */
  19. require_once 'KSC5601/Stream.php';
  20.  
  21. /**
  22.  * API class that controls UCS2 for KSC5601 package
  23.  *
  24.  * @package KSC5601
  25.  */
  26. Class KSC5601_UCS2 extends KSC5601_Stream
  27. {
  28.     // {{{ properties
  29.     /**#@+
  30.      * @access public
  31.      */
  32.     public $ksc     = NULL;
  33.     public $hanja   = NULL;
  34.     public $revs    = NULL;
  35.     public $ksc_max = 0;
  36.     public $han_max = 0;
  37.     public $rev_max = 0;
  38.     /**#@-*/
  39.     // }}}
  40.  
  41.     // {{{ constructor
  42.     /**
  43.      * @access public
  44.      * @return void 
  45.      */
  46.     function __construct () {
  47.         $this->init_ksc5601 ();
  48.     }
  49.     // }}}
  50.  
  51.     // {{{ function init_ksc5601 ()
  52.     /**
  53.      * Init KSC5601 code table
  54.      *
  55.      * If use pure code, load KSC5601 code table on memory. If loading,
  56.      * skip.
  57.      *
  58.      * @access public
  59.      * @return void 
  60.      * @param  void 
  61.      */
  62.     function init_ksc5601 () {
  63.         if ( $this->ksc != NULL ) {
  64.             if ( ! $this->ksc_max )
  65.                 $this->ksc_max = count ($this->ksc);
  66.  
  67.             if ( ! $this->han_max )
  68.                 $this->han_max = count ($this->hanja);
  69.  
  70.             if ( ! $this->rev_max )
  71.                 $this->rev_max = count ($this->revs);
  72.  
  73.             return;
  74.         }
  75.  
  76.         if ( isset ($GLOBALS['table_ksc5601']) ) {
  77.             $this->ksc   = &$GLOBALS['table_ksc5601'];
  78.             $this->hanja = &$GLOBALS['table_ksc5601_hanja'];
  79.             $this->revs  = &$GLOBALS['table_ksc5601_rev'];
  80.         } else {
  81.             #$t1 = microtime ();
  82.             require_once 'KSC5601/ksc5601.php';
  83.             #$t2 = microtime ();
  84.             #$t = $this->execute_time ($t1, $t2);
  85.             #printf ("############ INCLUDE CODE FILE (%s sec)\n", $t);
  86.             $this->ksc   = &$table_ksc5601;
  87.             $this->hanja = &$table_ksc5601_hanja;
  88.             $this->revs  = &$table_ksc5601_rev;
  89.         }
  90.         $this->ksc_max = count ($this->ksc);
  91.         $this->han_max = count ($this->hanja);
  92.         $this->rev_max = count ($this->revs);
  93.     }
  94.     // }}}
  95.  
  96.     // {{{ function ksc2ucs ($c1, $c2)
  97.     /**
  98.      * Convert KSC5601 to UCS2
  99.      * return decimical value or question mark '?'
  100.      *
  101.      * @access public
  102.      * @return string decimal string(42531) or question mark(?) that is case out of range.
  103.      * @param  string 1st byte binary character
  104.      * @param  string 2st byte binary character
  105.      */
  106.     function ksc2ucs ($c1, $c2) {
  107.         $this->init_ksc5601 ();
  108.  
  109.         $c1 = ord ($c1);
  110.         $c2 = ord ($c2);
  111.  
  112.         if ( $c1 >= 0xca && $c1 <= 0xfd ) {
  113.             /* Hanja Area */
  114.             if ( $c2 < 0xa1 || $c2 > 0xfe )
  115.                 return '??';
  116.  
  117.             $idx = ($c1 - 0xca) * 94 + ($c2 - 0xa1);
  118.             if ( $idx <= 0 || $idx > $this->han_max )
  119.                 return '??';
  120.  
  121.             return $this->hanja[$idx];
  122.         }
  123.  
  124.         if ( $c2 < 0x41 || $c2 > 0xfe )
  125.             return '??';
  126.         else if ( $c2 > 0x5a && $c2 < 0x61 )
  127.             return '??';
  128.         else if ( $c2 > 0x7a && $c2 < 0x81 )
  129.             return '??';
  130.  
  131.         if ( $c2 > 0x7a ) $c2 -= 6;
  132.         if ( $c2 > 0x5a ) $c2 -= 6;
  133.  
  134.         $idx = ($c1 - 0x81) * 178 + ($c2 - 0x41);
  135.  
  136.         if ( $idx <= 0 || $idx > $this->ksc_max )
  137.             return '??';
  138.  
  139.         return $this->ksc[$idx];
  140.     }
  141.     // }}}
  142.  
  143.     // {{{ function ucs2ksc ($s)
  144.     /**
  145.      * Convert UCS2 to KSC5601
  146.      *
  147.      * @access public
  148.      * @return string 2byte binary character (KSC5601)
  149.      * @param  string hexcial strings (UCS2)
  150.      */
  151.     function ucs2ksc ($s) {
  152.         $this->init_ksc5601 ();
  153.  
  154.         $c1 = 0x81;
  155.         $c2 = 0x41;
  156.  
  157.         if ( ! strncmp ($s, 'U+', 2) )
  158.             $s = preg_replace ('/^U\+/', '0x', $s);
  159.         else if ( strncmp ($s, '0x', 2) )
  160.             $s = '0x' . $s;
  161.  
  162.         $s = hexdec ($s);
  163.         $idx = $GLOBALS['table_ksc5601_rev'][$s];
  164.  
  165.  
  166.         if ( ! isset ($idx) )
  167.             return '??';
  168.  
  169.         $k1 = $idx >> 8;
  170.         $k2 = $idx & 0x00ff;
  171.  
  172.         # out of KSX 1001 range in CP949/UHC
  173.         if ( $this->out_ksx1001 === true ) {
  174.             if ( parent::is_out_of_ksx1001 ($k1, $k2, true) ) {
  175.                 $hex = dechex ($this->ksc2ucs (chr ($k1), chr ($k2)));
  176.                 return '&#x' . strtoupper ($hex) . ';';
  177.             }
  178.         }
  179.  
  180.         return chr ($k1) . chr ($k2);
  181.     }
  182.     // }}}
  183.  
  184.     // {{{ function mk_revTable () {
  185.     /**
  186.      * Print converting code from hexical string(UCS2) to 2byte binary(KSC5601) character.
  187.      *
  188.      * This method only for develeoper.
  189.      *
  190.      * @access public
  191.      * @param void 
  192.      * @return void 
  193.      */
  194.     function mk_revTable () {
  195.         $this->init_ksc5601 ();
  196.  
  197.         echo "<?\n" .
  198.             "/*\n" .
  199.             " * this array is made by KSC5601_UCS2::mk_revtable method\n" .
  200.             " */\n" .
  201.             "\$GLOBALS['table_ksc5601_rev'] = array (\n";
  202.  
  203.         $records = 1;
  204.         $arrno   = 0;
  205.  
  206.         /* Hangul reverse Area */
  207.  
  208.         $c1 = 0x81;
  209.         $c2 = 0x41;
  210.  
  211.         for ( $i=0; $i<$this->ksc_max; $i++ ) {
  212.             if ( $this->ksc[$i] ) {
  213.                 $r = ( $c1 << 8 ) + $c2;
  214.                 printf ('%5d => 0x%x', $this->ksc[$i], $r);
  215.             }
  216.  
  217.             $c2++;
  218.             if ( $c2 == 0x5b ) $c2 = 0x61;
  219.             else if ( $c2 == 0x7b ) $c2 = 0x81;
  220.             else if ( $c2 == 0xff ) {
  221.                 $c2 = 0x41;
  222.                 $c1++;
  223.             }
  224.  
  225.             if ( $this->ksc[$i] ) {
  226.                 if ( $records == 8 ) {
  227.                     echo ",\n";
  228.                     $records = 0;
  229.                 } else
  230.                     echo ', ';    
  231.  
  232.                 $records++;
  233.                 $arrno++;
  234.             }
  235.         }
  236.  
  237.         /* Hanja reverse Area */
  238.         $c1 = 0xca;
  239.         $c2 = 0xa1;
  240.  
  241.         for ( $i=0; $i<$this->han_max; $i++ ) {
  242.             if ( $this->hanja[$i] ) {
  243.                 $r = ( $c1 << 8 ) + $c2;
  244.                 printf ('%5d => 0x%x', $this->hanja[$i], $r);
  245.             }
  246.  
  247.             $c2++;
  248.             if ( $c2 == 0xff ) {
  249.                 $c2 = 0xa1;
  250.                 $c1++;
  251.             }
  252.  
  253.             if ( $this->hanja[$i] ) {
  254.                 if ( $records == 8 ) {
  255.                     echo ",\n";
  256.                     $records = 0;
  257.                 } else
  258.                     echo ', ';    
  259.  
  260.                 $records++;
  261.                 $arrno++;
  262.             }
  263.         }
  264.  
  265.         echo ");\n?>\n";
  266.     }
  267.     // }}}
  268. }
  269.  
  270. /*
  271.  * Local variables:
  272.  * tab-width: 4
  273.  * c-basic-offset: 4
  274.  * End:
  275.  * vim600: noet sw=4 ts=4 fdm=marker
  276.  * vim<600: noet sw=4 ts=4
  277.  */
  278. ?>

Documentation generated on Fri, 30 Aug 2024 06:11:10 +0900 by phpDocumentor 1.4.4