source-class-Com.Tecnick.Pdf.Font.Import.TypeOne

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.
  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: 
<?php
/**
 * TypeOne.php
 *
 * @since       2011-05-23
 * @category    Library
 * @package     PdfFont
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2011-2015 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-pdf-font
 *
 * This file is part of tc-lib-pdf-font software library.
 */

namespace Com\Tecnick\Pdf\Font\Import;

use \Com\Tecnick\File\File;
use \Com\Tecnick\Unicode\Data\Encoding;
use \Com\Tecnick\Pdf\Font\Exception as FontException;

/**
 * Com\Tecnick\Pdf\Font\Import\TypeOne
 *
 * @since       2011-05-23
 * @category    Library
 * @package     PdfFont
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2011-2015 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-pdf-font
 */
class TypeOne extends \Com\Tecnick\Pdf\Font\Import\Core
{
    /**
     * Store font data
     */
    protected function storeFontData()
    {
        // read first segment
        $dat = unpack('Cmarker/Ctype/Vsize', substr($this->font, 0, 6));
        if ($dat['marker'] != 128) {
            throw new FontException('Font file is not a valid binary Type1');
        }
        $this->fdt['size1'] = $dat['size'];
        $data = substr($this->font, 6, $this->fdt['size1']);
        // read second segment
        $dat = unpack('Cmarker/Ctype/Vsize', substr($this->font, (6 + $this->fdt['size1']), 6));
        if ($dat['marker'] != 128) {
            throw new FontException('Font file is not a valid binary Type1');
        }
        $this->fdt['size2'] = $dat['size'];
        $this->fdt['encrypted'] = substr($this->font, (12 + $this->fdt['size1']), $this->fdt['size2']);
        $data .= $this->fdt['encrypted'];
        // store compressed font
        $this->fdt['file'] = $this->fdt['file_name'].'.z';
        $file = new File();
        $fpt = $file->fopenLocal($this->fdt['dir'].$this->fdt['file'], 'wb');
        fwrite($fpt, gzcompress($data));
        fclose($fpt);
    }

    /**
     * Extract Font information
     */
    protected function extractFontInfo()
    {
        if (preg_match('#/FontName[\s]*\/([^\s]*)#', $this->font, $matches) !== 1) {
            preg_match('#/FullName[\s]*\(([^\)]*)#', $this->font, $matches);
        }
        $this->fdt['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
        preg_match('#/FontBBox[\s]*{([^}]*)#', $this->font, $matches);
        $this->fdt['bbox'] = trim($matches[1]);
        $bvl = explode(' ', $this->fdt['bbox']);
        $this->fdt['Ascent'] = intval($bvl[3]);
        $this->fdt['Descent'] = intval($bvl[1]);
        preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $this->font, $matches);
        $this->fdt['italicAngle'] = intval($matches[1]);
        if ($this->fdt['italicAngle'] != 0) {
            $this->fdt['Flags'] |= 64;
        }
        preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $this->font, $matches);
        $this->fdt['underlinePosition'] = intval($matches[1]);
        preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $this->font, $matches);
        $this->fdt['underlineThickness'] = intval($matches[1]);
        preg_match('#/isFixedPitch[\s]*([^\s]*)#', $this->font, $matches);
        if ($matches[1] == 'true') {
            $this->fdt['Flags'] |= 1;
        }
        preg_match('#/Weight[\s]*\(([^\)]*)#', $this->font, $matches);
        if (!empty($matches[1])) {
            $this->fdt['weight'] = strtolower($matches[1]);
        }
        $this->fdt['weight'] = 'Book';
        $this->fdt['Leading'] = 0;
    }

    /**
     * Extract Font information
     *
     * @return array
     */
    protected function getInternalMap()
    {
        $imap = array();
        if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $this->font, $fmap, PREG_SET_ORDER) > 0) {
            foreach ($fmap as $val) {
                $imap[$val[2]] = $val[1];
            }
        }
        return $imap;
    }

    /**
     * Decrypt eexec encrypted part
     *
     * @return string
     */
    protected function getEplain()
    {
        $csr = 55665; // eexec encryption constant
        $cc1 = 52845;
        $cc2 = 22719;
        $elen = strlen($this->fdt['encrypted']);
        $eplain = '';
        for ($idx = 0; $idx < $elen; ++$idx) {
            $chr = ord($this->fdt['encrypted'][$idx]);
            $eplain .= chr($chr ^ ($csr >> 8));
            $csr = ((($chr + $csr) * $cc1 + $cc2) % 65536);
        }
        return $eplain;
    }

    /**
     * Extract eexec info
     *
     * @return array
     */
    protected function extractEplainInfo()
    {
        $eplain = $this->getEplain();
        if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
            if ($matches[1] == 'true') {
                $this->fdt['Flags'] |= 0x40000;
            }
        }
        $this->extractStem($eplain);
        if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
            $bvl = explode(' ', $matches[1]);
            if (count($bvl) >= 6) {
                $vl1 = intval($bvl[2]);
                $vl2 = intval($bvl[4]);
                $this->fdt['XHeight'] = min($vl1, $vl2);
                $this->fdt['CapHeight'] = max($vl1, $vl2);
            }
        }
        $this->getRandomBytes($eplain);
        return $this->getCharstringData($eplain);
    }

    /**
     * Extract eexec info
     *
     * @param string $eplain Decoded eexec encrypted part
     *
     * @return array
     */
    protected function extractStem($eplain)
    {
        if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
            $this->fdt['StemV'] = intval($matches[1]);
        } elseif (($this->fdt['weight'] == 'bold') || ($this->fdt['weight'] == 'black')) {
            $this->fdt['StemV'] = 123;
        } else {
            $this->fdt['StemV'] = 70;
        }
        if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
            $this->fdt['StemH'] = intval($matches[1]);
        } else {
            $this->fdt['StemH'] = 30;
        }
        if (preg_match('#/Cap[X]?Height[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
            $this->fdt['CapHeight'] = intval($matches[1]);
        } else {
            $this->fdt['CapHeight'] = $this->fdt['Ascent'];
        }
        $this->fdt['XHeight'] = ($this->fdt['Ascent'] + $this->fdt['Descent']);
    }

    /**
     * Get the number of random bytes at the beginning of charstrings
     */
    protected function getRandomBytes($eplain)
    {
        $this->fdt['lenIV'] = 4;
        if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
            $this->fdt['lenIV'] = intval($matches[1]);
        }
    }

    /**
     * Get charstring data
     */
    protected function getCharstringData($eplain)
    {
        $this->fdt['enc_map'] = false;
        $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
        preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
        if (!empty($this->fdt['enc']) && isset(Encoding::$map[$this->fdt['enc']])) {
            $this->fdt['enc_map'] = Encoding::$map[$this->fdt['enc']];
        }
        return $matches;
    }

    /**
     * get CID
     *
     * @param array $imap
     * @param array $val
     *
     * @return int
     */
    protected function getCid($imap, $val)
    {
        if (isset($imap[$val[1]])) {
            return $imap[$val[1]];
        }
        if ($this->fdt['enc_map'] === false) {
            return 0;
        }
        $cid = array_search($val[1], $this->fdt['enc_map']);
        if ($cid === false) {
            return 0;
        }
        if ($cid > 1000) {
            return 1000;
        }
        return $cid;
    }

    /**
     * Decode number
     *
     * @param int   $idx
     * @param int   $cck
     * @param int   $cid
     * @param array $ccom
     * @param array $cdec
     * @param array $cwidths
     *
     * @return $int
     */
    protected function decodeNumber($idx, &$cck, &$cid, &$ccom, &$cdec, &$cwidths)
    {
        if ($ccom[$idx] == 255) {
            $sval = chr($ccom[($idx + 1)]).chr($ccom[($idx + 2)]).chr($ccom[($idx + 3)]).chr($ccom[($idx + 4)]);
            $vsval = unpack('li', $sval);
            $cdec[$cck] = $vsval['i'];
            return ($idx + 5);
        }
        if ($ccom[$idx] >= 251) {
            $cdec[$cck] = ((-($ccom[$idx] - 251) * 256) - $ccom[($idx + 1)] - 108);
            return ($idx + 2);
        }
        if ($ccom[$idx] >= 247) {
            $cdec[$cck] = ((($ccom[$idx] - 247) * 256) + $ccom[($idx + 1)] + 108);
            return ($idx + 2);
        }
        if ($ccom[$idx] >= 32) {
            $cdec[$cck] = ($ccom[$idx] - 139);
            return ++$idx;
        }
        $cdec[$cck] = $ccom[$idx];
        if (($cck > 0) && ($cdec[$cck] == 13)) {
            // hsbw command: update width
            $cwidths[$cid] = $cdec[($cck - 1)];
        }
        return ++$idx;
    }

    /**
     * Process Type1 font
     */
    protected function process()
    {
        $this->storeFontData();
        $this->extractFontInfo();
        $imap = $this->getInternalMap();
        $matches = $this->extractEplainInfo();
        $cwidths = array();
        $cc1 = 52845;
        $cc2 = 22719;
        foreach ($matches as $val) {
            $cid = $this->getCid($imap, $val);
            // decrypt charstring encrypted part
            $csr = 4330; // charstring encryption constant
            $ccd = $val[2];
            $clen = strlen($ccd);
            $ccom = array();
            for ($idx = 0; $idx < $clen; ++$idx) {
                $chr = ord($ccd[$idx]);
                $ccom[] = ($chr ^ ($csr >> 8));
                $csr = ((($chr + $csr) * $cc1 + $cc2) % 65536);
            }
            // decode numbers
            $cdec = array();
            $cck = 0;
            $idx = $this->fdt['lenIV'];
            while ($idx < $clen) {
                $idx = $this->decodeNumber($idx, $cck, $cid, $ccom, $cdec, $cwidths);
                ++$cck;
            }
        }
        $this->setCharWidths($cwidths);
    }
}
 

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