Blame view

export/PHPRtfLite-1.3.1/lib/PHPRtfLite/Image/Wmf.php 4.3 KB
f90e19c3   Andrea Petta   plugin update
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
  <?php
  /*
      PHPRtfLite
      Copyright 2007-2008 Denis Slaveckij <sinedas@gmail.com>
      Copyright 2010-2012 Steffen Zeidler <sigma_z@sigma-scripts.de>
  
      This file is part of PHPRtfLite.
  
      PHPRtfLite is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.
  
      PHPRtfLite is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.
  
      You should have received a copy of the GNU Lesser General Public License
      along with PHPRtfLite.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  /**
   * Class for displaying wmf images in rtf documents.
   * @since       1.2
   * @version     1.2
   * @author      Steffen Zeidler <sigma_z@sigma-scripts.de>
   * @copyright   2010-2012 Steffen Zeidler
   * @package     PHPRtfLite
   * @subpackage  PHPRtfLite_Image
   */
  class PHPRtfLite_Image_Wmf extends PHPRtfLite_Image
  {
  
      /**
       * true, if Aldus header has been found in wmf image
       * @var boolean
       */
      private $_headerStartBytesFound = false;
  
  
      /**
       * constructor
       *
       * @param PHPRtfLite    $rtf
       * @param resource      $stream
       * @param float         $width  optional
       * @param float         $height optional
       */
      public function __construct(PHPRtfLite $rtf, $stream, $width = null, $height = null)
      {
          parent::__construct($rtf, $stream, $width, $height);
          $this->_imageRtfType = '\wmetafile8';
          $this->setImageDimension();
      }
  
  
      /**
       * code for parsing the wmf is originally from: http://www.fpdf.de/downloads/addons/55/
       * as an addon for FPDF, written by Martin HALL-MAY
       */
      private function setImageDimension()
      {
          $headerSize = $this->getHeaderSize();
          fseek($this->_stream, $headerSize);
  
          while (!feof($this->_stream)) {
              $recordInfo = unpack('Lsize/Sfunc', fread($this->_stream, 6));
  
              // size of record given in WORDs (= 2 bytes)
              $size = $recordInfo['size'];
  
              // func is number of GDI function
              $func = $recordInfo['func'];
  
              // parameters are read as one block and processed
              // as necessary by the case statement below.
              // the data are stored in little-endian format and are unpacked using:
              // s - signed 16-bit int
              // S - unsigned 16-bit int (or WORD)
              // L - unsigned 32-bit int (or DWORD)
              // NB. parameters to GDI functions are stored in reverse order
              // however structures are not reversed,
              // e.g. POINT { int x, int y } where x=3000 (0x0BB8) and y=-1200 (0xFB50)
              // is stored as B8 0B 50 FB
              if ($size > 3) {
                  $params = fread($this->_stream, 2 * ($size - 3));
              }
  
              switch ($func) {
                  case 0x020c:  // SetWindowExt
                      $sizes = array_reverse(unpack('s2', $params));
                      $this->setImageWidth(PHPRtfLite_Unit::getPointsInTwips($sizes[0]));
                      $this->setImageHeight(PHPRtfLite_Unit::getPointsInTwips($sizes[1]));
                      return;
                  case 0x0000:
                      return;
              }
          }
      }
  
  
      /**
       * Get header size of wmf.
       *
       * @return integer $headerSize
       */
      private function getHeaderSize()
      {
          // reset stream pointer to 0
          fseek($this->_stream, 0);
          // check for Aldus placeable metafile header
          // L: vorzeichenloser Long-Typ (immer 32 Bit, Byte-Folge maschinenabhängig)
          // magic:
          $magic = fread($this->_stream, 4);
          $headerSize = 18; // WMF header minus four bytes already read
          if ($magic == "\xd7\xcd\xc6\x9a") {
              $this->_headerStartBytesFound = true;
              $headerSize += 22; // Aldus header
          }
          return $headerSize;
      }
  
  
      /**
       * gets rtf code of wmf
       *
       * @return string rtf code
       */
      public function render()
      {
          $startFrom = $this->_headerStartBytesFound ? 22 : 0;
          $this->writeIntoRtfStream($startFrom);
      }
  
  }