source-class-Com.Tecnick.Pdf.Image.Output

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: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 
<?php
/**
 * Output.php
 *
 * @since       2011-05-23
 * @category    Library
 * @package     PdfImage
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2011-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-pdf-image
 *
 * This file is part of tc-lib-pdf-image software library.
 */

namespace Com\Tecnick\Pdf\Image;

use \Com\Tecnick\Pdf\Image\Import;
use \Com\Tecnick\Pdf\Encrypt\Encrypt;
use \Com\Tecnick\Pdf\Image\Exception as ImageException;

/**
 * Com\Tecnick\Pdf\Image\Output
 *
 * @since       2011-05-23
 * @category    Library
 * @package     PdfImage
 * @author      Nicola Asuni <info@tecnick.com>
 * @copyright   2011-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-pdf-image
 */
abstract class Output
{
    /**
     * Current PDF object number
     *
     * @var int
     */
    protected $pon;

    /**
     * Unit of measure conversion ratio
     *
     * @var float
     */
    protected $kunit = 1.0;

    /**
     * Encrypt object
     *
     * @var Encrypt
     */
    protected $enc;

    /**
     * True if we are in PDF/A mode.
     *
     * @var bool
     */
    protected $pdfa = false;

    /**
     * Store image object IDs for the XObject Dctionary.
     *
     * @var array
     */
    protected $xobjdict = array();

    /**
     * Initialize images data
     *
     * @param float   $kunit  Unit of measure conversion ratio.
     * @param Encrypt $enc    Encrypt object.
     * @param bool    $pdfa   True if we are in PDF/A mode.
     */
    public function __construct($kunit, Encrypt $enc, $pdfa = false)
    {
        $this->kunit = (float) $kunit;
        $this->enc = $enc;
        $this->pdfa = (bool) $pdfa;
    }

    /**
     * Returns current PDF object number
     *
     * @return int
     */
    public function getObjectNumber()
    {
        return $this->pon;
    }

    /**
     * Get the PDF output string to print the specified image ID
     *
     * @param int $iid        Image ID
     * @param int $xpos       Abscissa (X coordinate) of the upper-left Image corner
     * @param int $ypos       Ordinate (Y coordinate) of the upper-left Image corner
     * @param int $width      Image width in user units
     * @param int $height     Image height in user units
     * @param int $pageheight Page height in user units
     *
     * @return string
     */
    public function getSetImage($iid, $xpos, $ypos, $width, $height, $pageheight)
    {
        if (empty($this->image[$iid])) {
            throw new ImageException('Unknown image ID: '.$iid);
        }
        $out = 'q';
        $out .= sprintf(
            ' %F 0 0 %F %F %F cm',
            ($width * $this->kunit),
            ($height * $this->kunit),
            ($xpos * $this->kunit),
            (($pageheight - $ypos - $height) * $this->kunit) // reverse coordinate
        );
        if (!empty($this->cache[$this->image[$iid]['key']]['mask'])) {
            $out .= ' /IMGmask'.$iid.' Do';
            if (!empty($this->cache[$this->image[$iid]['key']]['plain'])) {
                $out .= ' /IMGplain'.$iid.' Do';
            }
        } else {
            $out .= ' /IMG'.$iid.' Do';
        }
        $out .= ' Q';
        return $out;
    }

    /**
     * Get the PDF output string for Images
     *
     * @param int $pon Current PDF Object Number
     *
     * @return string
     */
    public function getOutImagesBlock($pon)
    {
        $this->pon = (int) $pon;
        $out = '';
        foreach ($this->image as $iid => $img) {
            if (empty($this->cache[$img['key']]['out'])) {
                if (!empty($this->cache[$img['key']]['mask'])) {
                    $out .= $this->getOutImage($img, $this->cache[$img['key']]['mask'], 'mask');
                    if (!empty($this->cache[$img['key']]['plain'])) {
                        $out .= $this->getOutImage($img, $this->cache[$img['key']]['plain'], 'plain');
                    }
                } else {
                    $out .= $this->getOutImage($img, $this->cache[$img['key']]);
                }
                $this->image[$iid] = $img;
            }

            if (!empty($this->cache[$img['key']]['mask']['obj'])) {
                $this->xobjdict['IMG'.$img['iid']] = $this->cache[$img['key']]['mask']['obj'];
                if (!empty($this->cache[$img['key']]['plain']['obj'])) {
                    $this->xobjdict['IMG'.$img['iid']] = $this->cache[$img['key']]['plain']['obj'];
                }
            } else {
                $this->xobjdict['IMG'.$img['iid']] = $this->cache[$img['key']]['obj'];
            }
        }
        return $out;
    }

    /**
     * Get the PDF output string for Image object
     *
     * @param array  $img  Image reference
     * @param array  $data Image raw data
     * @param string $sub  Sub image ('mask', 'plain' or empty string)
     *
     * @return string
     */
    protected function getOutImage(&$img, &$data, $sub = '')
    {
        $out = $this->getOutIcc($data)
                .$this->getOutPalette($data)
                .$this->getOutAltImages($img, $data, $sub);

        $data['obj'] = ++$this->pon;

        $out .= $this->pon.' 0 obj'."\n"
            .'<</Type /XObject'
            .' /Subtype /Image'
            .' /Width '.$data['width']
            .' /Height '.$data['height']
            .$this->getOutColorInfo($data);

        if (!empty($data['exturl'])) {
            // external stream
            $out .= ' /Length 0'
                .' /F << /FS /URL /F '.$this->enc->escapeDataString($data['exturl'], $this->pon).' >>';
            if (!empty($data['filter'])) {
                $out .= ' /FFilter /'.$data['filter'];
            }
            $out .= ' >> stream'."\n"
                .'endstream'."\n";
        } else {
            if (!empty($data['filter'])) {
                $out .= ' /Filter /'.$data['filter'];
            }
            if (!empty($data['parms'])) {
                $out .= ' '.$data['parms'];
            }

            // Colour Key Masking
            if (!empty($data['trns'])) {
                $trns = $this->getOutTransparency($data);
                if (!empty($trns)) {
                    $out .= ' /Mask [ '.$trns.']';
                }
            }

            $stream = $this->enc->encryptString($data['data'], $this->pon);
            $out .=' /Length '.strlen($stream)
                .'>> stream'."\n"
                .$stream."\n"
                .'endstream'."\n";
        }

        $out .= 'endobj'."\n";

        $this->cache[$img['key']]['out'] = true; // mark this as done

        return $out;
    }

    /**
    * Return XObjects Dictionary portion for the images
    *
    * @return string
    */
    public function getXobjectDict()
    {
        $out = '';
        foreach ($this->xobjdict as $iid => $objid) {
            $out .= ' /'.$iid.' '.$objid.' 0 R';
        }
        return $out;
    }

    /**
     * Get the PDF output string for ICC object
     *
     * @param array  $data Image raw data
     *
     * @return string
     */
    protected function getOutIcc(&$data)
    {
        if (empty($data['icc'])) {
            return '';
        }

        $data['obj_icc'] = ++$this->pon;
        $stream = $this->enc->encryptString(gzcompress($data['icc']), $this->pon);

        return $this->pon.' 0 obj'."\n"
            .'<</N '.$data['channels']
            .' /Alternate /'.$data['colspace']
            .' /Filter /FlateDecode'
            .' /Length '.strlen($stream)
            .'>> stream'."\n"
            .$stream."\n"
            .'endstream'."\n"
            .'endobj'."\n";
    }

    /**
     * Get the PDF output string for Indexed palette object
     *
     * @param array  $data Image raw data
     *
     * @return string
     */
    protected function getOutPalette(&$data)
    {
        if ($data['colspace'] != 'Indexed') {
            return '';
        }

        $data['obj_pal'] = ++$this->pon;
        $stream = $this->enc->encryptString(gzcompress($data['pal']), $this->pon);

        return $this->pon.' 0 obj'."\n"
            .'<</Filter /FlateDecode'
            .' /Length '.strlen($stream)
            .'>> stream'."\n"
            .$stream."\n"
            .'endstream'."\n"
            .'endobj'."\n";
    }

    /**
     * Get the PDF output string for color and mask information
     *
     * @param array  $data Image raw data
     *
     * @return string
     */
    protected function getOutColorInfo($data)
    {
        $out = '';
        // set color space
        if (!empty($data['obj_icc'])) {
            // ICC Colour Space
            $out .= ' /ColorSpace [/ICCBased '.$data['obj_icc'].' 0 R]';
        } elseif (!empty($data['obj_pal'])) {
            // Indexed Colour Space
            $out .= ' /ColorSpace [/Indexed /DeviceRGB '
                .((strlen($data['pal']) / 3) - 1)
                .' '.$data['obj_pal'].' 0 R]';
        } else {
            // Device Colour Space
            $out .= ' /ColorSpace /'.$data['colspace'];
        }
        if ($data['colspace'] == 'DeviceCMYK') {
            $out .= ' /Decode [1 0 1 0 1 0 1 0]';
        }
        $out .= ' /BitsPerComponent '.$data['bits'];
        
        if (!empty($this->cache[$data['key']]['mask']['obj'])) {
            $out .= ' /SMask '.$this->cache[$data['key']]['mask']['obj'].' 0 R';
        }
        
        if (!empty($data['obj_alt'])) {
            // reference to alternate images dictionary
            $out .= ' /Alternates '.$data['obj_alt'].' 0 R';
        }
        return $out;
    }

    /**
     * Get the PDF output string for Alternate images object
     *
     * @param array  $img  Image reference
     * @param array  $data Image raw data
     * @param string $sub  Sub image ('mask', 'plain' or empty string)
     *
     * @return string
     */
    protected function getOutAltImages($img, &$data, $sub = '')
    {
        if ($this->pdfa || empty($img['altimgs']) || ($sub == 'mask')) {
            return '';
        }

        $data['obj_alt'] = ++$this->pon;

        $out = $this->pon.' 0 obj'."\n"
            .'[';
        foreach ($img['altimgs'] as $iid) {
            if (!empty($this->cache[$this->image[$iid]['key']]['obj'])) {
                $out .= ' <<'
                    .' /Image '.$this->cache[$this->image[$iid]['key']]['obj'].' 0 R'
                    .' /DefaultForPrinting '.(empty($this->image[$iid]['defprint']) ? 'false' : 'true')
                    .' >>';
            }
        }
        $out .= ' ]'."\n"
            .'endobj'."\n";

        return $out;
    }

    /**
     * Get the PDF output string for color and mask information
     *
     * @param array  $data Image raw data
     *
     * @return string
     */
    protected function getOutTransparency($data)
    {
        $trns = '';
        foreach ($data['trns'] as $idx => $val) {
            if ($val == 0) {
                $trns .= $idx.' '.$idx.' ';
            }
        }
        return $trns;
    }
}
 

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