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

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: 
<?php
/**
 * OutUtil.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\Exception as FontException;

/**
 * Com\Tecnick\Pdf\Font\OutUtil
 *
 * @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 OutUtil
{
    /**
     * Return font full path
     *
     * @param string $fontdir Original font directory
     * @param string $file    Font file name.
     *
     * @return string Font full path or empty string
     */
    protected function getFontFullPath($fontdir, $file)
    {
        $dirobj = new Dir();
        // directories where to search for the font definition file
        $dirs = array_unique(
            array(
                '',
                $fontdir,
                (defined('K_PATH_FONTS') ? K_PATH_FONTS : ''),
                $dirobj->findParentDir('fonts', __DIR__),
            )
        );
        foreach ($dirs as $dir) {
            if (@is_readable($dir.$file)) {
                return $dir.$file;
            }
        }
        throw new FontException('Unable to locate the file: '.$file);
    }

    /**
     * Outputs font widths
     *
     * @param array $font      Font to process
     * @param int   $cidoffset Offset for CID values
     *
     * @return string PDF command string for font widths
     */
    protected function getCharWidths(array $font, $cidoffset = 0)
    {
        ksort($font['cw']);
        $range = $this->getWidthRanges($font, $cidoffset);
        // output data
        $wdt = '';
        foreach ($range as $kdx => $wds) {
            if (count(array_count_values($wds)) == 1) {
                // interval mode is more compact
                $wdt .= ' '.$kdx.' '.($kdx + count($wds) - 1).' '.$wds[0];
            } else {
                // range mode
                $wdt .= ' '.$kdx.' [ '.implode(' ', $wds).' ]';
            }
        }
        return '/W ['.$wdt.' ]';
    }

    /**
     * get width ranges of characters
     *
     * @param array $font      Font to process
     * @param int   $cidoffset Offset for CID values
     *
     * @return array
     */
    protected function getWidthRanges(array $font, $cidoffset = 0)
    {
        $range = array();
        $rangeid = 0;
        $prevcid = -2;
        $prevwidth = -1;
        $interval = false;
        // for each character
        foreach ($font['cw'] as $cid => $width) {
            $cid -= $cidoffset;
            if ($font['subset'] && (!isset($font['subsetchars'][$cid]))) {
                // ignore the unused characters (font subsetting)
                continue;
            }
            if ($width != $font['dw']) {
                if ($cid == ($prevcid + 1)) {
                    // consecutive CID
                    if ($width == $prevwidth) {
                        if ($width == $range[$rangeid][0]) {
                            $range[$rangeid][] = $width;
                        } else {
                            array_pop($range[$rangeid]);
                            // new range
                            $rangeid = $prevcid;
                            $range[$rangeid] = array();
                            $range[$rangeid][] = $prevwidth;
                            $range[$rangeid][] = $width;
                        }
                        $interval = true;
                        $range[$rangeid]['interval'] = true;
                    } else {
                        if ($interval) {
                            // new range
                            $rangeid = $cid;
                            $range[$rangeid] = array();
                            $range[$rangeid][] = $width;
                        } else {
                            $range[$rangeid][] = $width;
                        }
                        $interval = false;
                    }
                } else {
                    // new range
                    $rangeid = $cid;
                    $range[$rangeid] = array();
                    $range[$rangeid][] = $width;
                    $interval = false;
                }
                $prevcid = $cid;
                $prevwidth = $width;
            }
        }
        return $this->optimizeWidthRanges($range);
    }

    /**
     * Optimize width ranges
     *
     * @param array $range Widht Ranges
     *
     * @return array
     */
    protected function optimizeWidthRanges($range)
    {
        $prevk = -1;
        $nextk = -1;
        $prevint = false;
        foreach ($range as $kdx => $wds) {
            $cws = count($wds);
            if (($kdx == $nextk) && (!$prevint) && ((!isset($wds['interval'])) || ($cws < 4))) {
                unset($range[$kdx]['interval']);
                $range[$prevk] = array_merge($range[$prevk], $range[$kdx]);
                unset($range[$kdx]);
            } else {
                $prevk = $kdx;
            }
            $nextk = $kdx + $cws;
            if (isset($wds['interval'])) {
                if ($cws > 3) {
                    $prevint = true;
                } else {
                    $prevint = false;
                }
                if (isset($range[$kdx]['interval'])) {
                    unset($range[$kdx]['interval']);
                }
                --$nextk;
            } else {
                $prevint = false;
            }
        }
        return $range;
    }
}
 

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