source-class-Com.Tecnick.Pdf.Font.Load

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: 
<?php
/**
 * Load.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;

use \Com\Tecnick\File\Dir;
use \Com\Tecnick\Pdf\Font\Core;
use \Com\Tecnick\Pdf\Font\Exception as FontException;

/**
 * Com\Tecnick\Pdf\Font\Load
 *
 * @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
 */
abstract class Load
{
    /**
     * Load the font data
     *
     * @throws FontException in case of error
     */
    public function load()
    {
        $this->data = array_merge($this->data, $this->getFontInfo());
        $this->checkType();
        $this->setName();
        $this->setDefaultWidth();
        if (($this->data['type'] == 'Core') || $this->data['fakestyle']) {
            $this->setArtificialStyles();
        }
        $this->setFileData();
    }

    /**
     * Load the font data
     *
     * @return array Font data
     *
     * @throws FontException in case of error
     */
    protected function getFontInfo()
    {
        $this->findFontFile();

        // read the font definition file
        if (!@is_readable($this->data['ifile'])) {
            throw new FontException('unable to read file: '.$this->data['ifile']);
        }

        $fdt = @file_get_contents($this->data['ifile']);
        $fdt = @json_decode($fdt, true);
        if ($fdt === null) {
            throw new FontException('JSON decoding error ['.json_last_error().']');
        }

        if (empty($fdt['type']) || empty($fdt['cw'])) {
            throw new FontException('fhe font definition file has a bad format: '.$this->data['ifile']);
        }

        return $fdt;
    }

    /**
     * Returns a list of font directories
     *
     * @return array Font directories
     */
    protected function findFontDirectories()
    {
        $dirobj = new Dir();
        $dirs =  array('');
        if (defined('K_PATH_FONTS')) {
            $dirs[] = K_PATH_FONTS;
            $dirs = array_merge($dirs, array_filter(glob(K_PATH_FONTS.'/*'), 'is_dir'));
        }
        $parent_font_dir = $dirobj->findParentDir('fonts', __DIR__);
        if (!empty($parent_font_dir)) {
            $dirs[] = $parent_font_dir;
            $dirs = array_merge($dirs, array_filter(glob($parent_font_dir.'/*'), 'is_dir'));
        }
        return array_unique($dirs);
    }

    /**
     * Load the font data
     *
     * @return array Font data
     *
     * @throws FontException in case of error
     */
    protected function findFontFile()
    {
        if (!empty($this->data['ifile'])) {
            return;
        }

        $this->data['ifile'] = strtolower($this->data['key']).'.json';
 
        // directories where to search for the font definition file
        $dirs = $this->findFontDirectories();

        // find font definition file names
        $files = array_unique(
            array(
                strtolower($this->data['key']).'.json',
                strtolower($this->data['family']).'.json'
            )
        );

        foreach ($files as $file) {
            foreach ($dirs as $dir) {
                if (@is_readable($dir.$file)) {
                    $this->data['ifile'] = $dir.$file;
                    break 2;
                }
            }
            // we haven't found the version with style variations
            $this->data['fakestyle'] = true;
        }
    }

    /**
     * Set default width
     */
    protected function setDefaultWidth()
    {
        if (!empty($this->data['dw'])) {
            return;
        }
        if (isset($this->data['desc']['MissingWidth']) && ($this->data['desc']['MissingWidth'] > 0)) {
            $this->data['dw'] = $this->data['desc']['MissingWidth'];
        } elseif (!empty($this->data['cw'][32])) {
            $this->data['dw'] = $this->data['cw'][32];
        } else {
            $this->data['dw'] = 600;
        }
    }

    /**
     * Check Font Type
     */
    protected function checkType()
    {
        if (in_array($this->data['type'], array('Core', 'Type1', 'TrueType', 'TrueTypeUnicode', 'cidfont0'))) {
            return;
        }
        throw new FontException('Unknow font type: '.$this->data['type']);
    }

    /**
     * Set name
     */
    protected function setName()
    {
        if ($this->data['type'] == 'Core') {
            $this->data['name'] = Core::$font[$this->data['key']];
            $this->data['subset'] = false;
        } elseif (($this->data['type'] == 'Type1') || ($this->data['type'] == 'TrueType')) {
            $this->data['subset'] = false;
        } elseif ($this->data['type'] == 'TrueTypeUnicode') {
            $this->data['enc'] = 'Identity-H';
        } elseif (($this->data['type'] == 'cidfont0') && ($this->data['pdfa'])) {
            throw new FontException('CID0 fonts are not supported, all fonts must be embedded in PDF/A mode!');
        }
        if (empty($this->data['name'])) {
            $this->data['name'] = $this->data['key'];
        }
    }

    /**
     * Set artificial styles if the font variation file is missing
     */
    protected function setArtificialStyles()
    {
        // artificial bold
        if ($this->data['mode']['bold']) {
            $this->data['name'] .= 'Bold';
            if (isset($this->data['desc']['StemV'])) {
                $this->data['desc']['StemV'] = round($this->data['desc']['StemV'] * 1.75);
            } else {
                $this->data['desc']['StemV'] = 123;
            }
        }
        // artificial italic
        if ($this->data['mode']['italic']) {
            $this->data['name'] .= 'Italic';
            if (isset($this->data['desc']['ItalicAngle'])) {
                $this->data['desc']['ItalicAngle'] -= 11;
            } else {
                $this->data['desc']['ItalicAngle'] = -11;
            }
            if (isset($this->data['desc']['Flags'])) {
                $this->data['desc']['Flags'] |= 64; //bit 7
            } else {
                $this->data['desc']['Flags'] = 64;
            }
        }
    }

    /**
     * Set File data
     */
    public function setFileData()
    {
        if (empty($this->data['file'])) {
            return;
        }
        if (strpos($this->data['type'], 'TrueType') !== false) {
            $this->data['length1'] = $this->data['originalsize'];
            $this->data['length2'] = false;
        } elseif ($this->data['type'] != 'Core') {
            $this->data['length1'] = $this->data['size1'];
            $this->data['length2'] = $this->data['size2'];
        }
    }
}
 

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