source-class-Com.Tecnick.Barcode.Type.Square.Datamatrix.Steps

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Exceptions

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 
<?php
/**
 * Steps.php
 *
 * @since       2015-02-21
 * @category    Library
 * @package     Barcode
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2015-2016 Nicola Asuni - Tecnick.com LTD
 * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 * @link        https://github.com/tecnickcom/tc-lib-barcode
 *
 * This file is part of tc-lib-barcode software library.
 */

namespace Com\Tecnick\Barcode\Type\Square\Datamatrix;

use \Com\Tecnick\Barcode\Exception as BarcodeException;
use \Com\Tecnick\Barcode\Type\Square\Datamatrix\Data;

/**
 * Com\Tecnick\Barcode\Type\Square\Datamatrix\Steps
 *
 * Steps methods for Datamatrix Barcode type class
 *
 * @since       2015-02-21
 * @category    Library
 * @package     Barcode
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2015-2016 Nicola Asuni - Tecnick.com LTD
 * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 * @link        https://github.com/tecnickcom/tc-lib-barcode
 */
abstract class Steps extends \Com\Tecnick\Barcode\Type\Square\Datamatrix\Modes
{
    /**
     * The look-ahead test scans the data to be encoded to find the best mode (Annex P - steps from J to S).
     *
     * @param string $data Data to encode
     * @param int    $pos  Current position
     * @param int    $mode Current encoding mode
     *
     * @return int encoding mode
     */
    public function lookAheadTest($data, $pos, $mode)
    {
        $data_length = strlen($data);
        if ($pos >= $data_length) {
            return $mode;
        }
        $charscount = 0; // count processed chars
        // STEP J
        if ($mode == Data::ENC_ASCII) {
            $numch = array(0, 1, 1, 1, 1, 1.25);
        } else {
            $numch = array(1, 2, 2, 2, 2, 2.25);
            $numch[$mode] = 0;
        }
        while (true) {
            if (($pos + $charscount) == $data_length) {
                return $this->stepK($numch);
            }
            $chr = ord($data[$pos + $charscount]);
            $charscount++;
            $this->stepL($chr, $numch);
            $this->stepM($chr, $numch);
            $this->stepN($chr, $numch);
            $this->stepO($chr, $numch);
            $this->stepP($chr, $numch);
            $this->stepQ($chr, $numch);
            if ($charscount >= 4) {
                $ret = $this->stepR($numch, $pos, $data_length, $charscount, $data);
                if ($ret !== null) {
                    return $ret;
                }
            }
        }
        throw new BarcodeException('LookAhead Error');
    }

    /**
     * Step K
     *
     * @param int $numch
     *
     * @return int
     */
    protected function stepK($numch)
    {
        if ($numch[Data::ENC_ASCII] <= ceil(min(
            $numch[Data::ENC_C40],
            $numch[Data::ENC_TXT],
            $numch[Data::ENC_X12],
            $numch[Data::ENC_EDF],
            $numch[Data::ENC_BASE256]
        ))) {
            return Data::ENC_ASCII;
        }
        if ($numch[Data::ENC_BASE256] < ceil(min(
            $numch[Data::ENC_ASCII],
            $numch[Data::ENC_C40],
            $numch[Data::ENC_TXT],
            $numch[Data::ENC_X12],
            $numch[Data::ENC_EDF]
        ))) {
            return Data::ENC_BASE256;
        }
        if ($numch[Data::ENC_EDF] < ceil(min(
            $numch[Data::ENC_ASCII],
            $numch[Data::ENC_C40],
            $numch[Data::ENC_TXT],
            $numch[Data::ENC_X12],
            $numch[Data::ENC_BASE256]
        ))) {
            return Data::ENC_EDF;
        }
        if ($numch[Data::ENC_TXT] < ceil(min(
            $numch[Data::ENC_ASCII],
            $numch[Data::ENC_C40],
            $numch[Data::ENC_X12],
            $numch[Data::ENC_EDF],
            $numch[Data::ENC_BASE256]
        ))) {
            return Data::ENC_TXT;
        }
        if ($numch[Data::ENC_X12] < ceil(min(
            $numch[Data::ENC_ASCII],
            $numch[Data::ENC_C40],
            $numch[Data::ENC_TXT],
            $numch[Data::ENC_EDF],
            $numch[Data::ENC_BASE256]
        ))) {
            return Data::ENC_X12;
        }
        return Data::ENC_C40;
    }

    /**
     * Step L
     *
     * @param int $chr
     * @param int $numch
     */
    protected function stepL($chr, &$numch)
    {
        if ($this->isCharMode($chr, Data::ENC_ASCII_NUM)) {
            $numch[Data::ENC_ASCII] += (1 / 2);
        } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
            $numch[Data::ENC_ASCII] = ceil($numch[Data::ENC_ASCII]);
            $numch[Data::ENC_ASCII] += 2;
        } else {
            $numch[Data::ENC_ASCII] = ceil($numch[Data::ENC_ASCII]);
            $numch[Data::ENC_ASCII] += 1;
        }
    }

    /**
     * Step M
     *
     * @param int $chr
     * @param int $numch
     */
    protected function stepM($chr, &$numch)
    {
        if ($this->isCharMode($chr, Data::ENC_C40)) {
            $numch[Data::ENC_C40] += (2 / 3);
        } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
            $numch[Data::ENC_C40] += (8 / 3);
        } else {
            $numch[Data::ENC_C40] += (4 / 3);
        }
    }

    /**
     * Step N
     *
     * @param int $chr
     * @param int $numch
     */
    protected function stepN($chr, &$numch)
    {
        if ($this->isCharMode($chr, Data::ENC_TXT)) {
            $numch[Data::ENC_TXT] += (2 / 3);
        } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
            $numch[Data::ENC_TXT] += (8 / 3);
        } else {
            $numch[Data::ENC_TXT] += (4 / 3);
        }
    }

    /**
     * Step O
     *
     * @param int $chr
     * @param int $numch
     */
    protected function stepO($chr, &$numch)
    {
        if ($this->isCharMode($chr, Data::ENC_X12) || $this->isCharMode($chr, Data::ENC_C40)) {
            $numch[Data::ENC_X12] += (2 / 3);
        } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
            $numch[Data::ENC_X12] += (13 / 3);
        } else {
            $numch[Data::ENC_X12] += (10 / 3);
        }
    }

    /**
     * Step P
     *
     * @param int $chr
     * @param int $numch
     */
    protected function stepP($chr, &$numch)
    {
        if ($this->isCharMode($chr, Data::ENC_EDF)) {
            $numch[Data::ENC_EDF] += (3 / 4);
        } elseif ($this->isCharMode($chr, Data::ENC_ASCII_EXT)) {
            $numch[Data::ENC_EDF] += (17 / 4);
        } else {
            $numch[Data::ENC_EDF] += (13 / 4);
        }
    }

    /**
     * Step Q
     *
     * @param int $chr
     * @param int $numch
     */
    protected function stepQ($chr, &$numch)
    {
        if ($this->isCharMode($chr, Data::ENC_BASE256)) {
            $numch[Data::ENC_BASE256] += 4;
        } else {
            $numch[Data::ENC_BASE256] += 1;
        }
    }

    /**
     * Step R-f
     *
     * @param int    $numch
     * @param int    $pos
     * @param int    $data_length
     * @param int    $charscount
     * @param string $data
     *
     * @return int   Encoding mode
     */
    protected function stepRf($numch, $pos, $data_length, $charscount, $data)
    {
        if (($numch[Data::ENC_C40] + 1) < min(
            $numch[Data::ENC_ASCII],
            $numch[Data::ENC_TXT],
            $numch[Data::ENC_EDF],
            $numch[Data::ENC_BASE256]
        )) {
            if ($numch[Data::ENC_C40] < $numch[Data::ENC_X12]) {
                return Data::ENC_C40;
            }
            if ($numch[Data::ENC_C40] == $numch[Data::ENC_X12]) {
                $ker = ($pos + $charscount + 1);
                while ($ker < $data_length) {
                    $tmpchr = ord($data[$ker]);
                    if ($this->isCharMode($tmpchr, Data::ENC_X12)) {
                        return Data::ENC_X12;
                    } elseif (!($this->isCharMode($tmpchr, Data::ENC_X12)
                        || $this->isCharMode($tmpchr, Data::ENC_C40))) {
                        break;
                    }
                    ++$ker;
                }
                return Data::ENC_C40;
            }
        }
        return null;
    }

    /**
     * Step R
     *
     * @param int    $numch
     * @param int    $pos
     * @param int    $data_length
     * @param int    $charscount
     * @param string $data
     */
    protected function stepR($numch, $pos, $data_length, $charscount, $data)
    {
        if (($numch[Data::ENC_ASCII] + 1) <= min(
            $numch[Data::ENC_C40],
            $numch[Data::ENC_TXT],
            $numch[Data::ENC_X12],
            $numch[Data::ENC_EDF],
            $numch[Data::ENC_BASE256]
        )) {
            return Data::ENC_ASCII;
        }
        if ((($numch[Data::ENC_BASE256] + 1) <= $numch[Data::ENC_ASCII])
            || (($numch[Data::ENC_BASE256] + 1) < min(
                $numch[Data::ENC_C40],
                $numch[Data::ENC_TXT],
                $numch[Data::ENC_X12],
                $numch[Data::ENC_EDF]
            ))) {
            return Data::ENC_BASE256;
        }
        if (($numch[Data::ENC_EDF] + 1) < min(
            $numch[Data::ENC_ASCII],
            $numch[Data::ENC_C40],
            $numch[Data::ENC_TXT],
            $numch[Data::ENC_X12],
            $numch[Data::ENC_BASE256]
        )) {
            return Data::ENC_EDF;
        }
        if (($numch[Data::ENC_TXT] + 1) < min(
            $numch[Data::ENC_ASCII],
            $numch[Data::ENC_C40],
            $numch[Data::ENC_X12],
            $numch[Data::ENC_EDF],
            $numch[Data::ENC_BASE256]
        )) {
            return Data::ENC_TXT;
        }
        if (($numch[Data::ENC_X12] + 1) < min(
            $numch[Data::ENC_ASCII],
            $numch[Data::ENC_C40],
            $numch[Data::ENC_TXT],
            $numch[Data::ENC_EDF],
            $numch[Data::ENC_BASE256]
        )) {
            return Data::ENC_X12;
        }
        return $this->stepRf($numch, $pos, $data_length, $charscount, $data);
    }
}
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy