PluginProbe
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin / trunk
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin vtrunk
trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 All 71 releases
← All changes | mpdf/classes/wmf.php +271 -236 1.7.1trunk View file →
@@ -1,236 +1,271 @@
1 -<?php
2 -
3 -class wmf {
4 -
5 -var $mpdf = null;
6 -var $gdiObjectArray;
7 -
8 -function wmf(&$mpdf) {
9 - $this->mpdf = $mpdf;
10 -}
11 -
12 -
13 -function _getWMFimage($data) {
14 - $k = _MPDFK;
15 -
16 - $this->gdiObjectArray = array();
17 - $a=unpack('stest',"\1\0");
18 - if ($a['test']!=1)
19 - return array(0, 'Error parsing WMF image - Big-endian architecture not supported');
20 - // check for Aldus placeable metafile header
21 - $key = unpack('Lmagic', substr($data, 0, 4));
22 - $p = 18; // WMF header
23 - if ($key['magic'] == (int)0x9AC6CDD7) { $p +=22; } // Aldus header
24 - // define some state variables
25 - $wo=null; // window origin
26 - $we=null; // window extent
27 - $polyFillMode = 0;
28 - $nullPen = false;
29 - $nullBrush = false;
30 - $endRecord = false;
31 - $wmfdata = '';
32 - while ($p < strlen($data) && !$endRecord) {
33 - $recordInfo = unpack('Lsize/Sfunc', substr($data, $p, 6)); $p += 6;
34 - // size of record given in WORDs (= 2 bytes)
35 - $size = $recordInfo['size'];
36 - // func is number of GDI function
37 - $func = $recordInfo['func'];
38 - if ($size > 3) {
39 - $parms = substr($data, $p, 2*($size-3)); $p += 2*($size-3);
40 - }
41 - switch ($func) {
42 - case 0x020b: // SetWindowOrg
43 - // do not allow window origin to be changed
44 - // after drawing has begun
45 - if (!$wmfdata)
46 - $wo = array_reverse(unpack('s2', $parms));
47 - break;
48 - case 0x020c: // SetWindowExt
49 - // do not allow window extent to be changed
50 - // after drawing has begun
51 - if (!$wmfdata)
52 - $we = array_reverse(unpack('s2', $parms));
53 - break;
54 - case 0x02fc: // CreateBrushIndirect
55 - $brush = unpack('sstyle/Cr/Cg/Cb/Ca/Shatch', $parms);
56 - $brush['type'] = 'B';
57 - $this->_AddGDIObject($brush);
58 - break;
59 - case 0x02fa: // CreatePenIndirect
60 - $pen = unpack('Sstyle/swidth/sdummy/Cr/Cg/Cb/Ca', $parms);
61 - // convert width from twips to user unit
62 - $pen['width'] /= (20 * $k);
63 - $pen['type'] = 'P';
64 - $this->_AddGDIObject($pen);
65 - break;
66 -
67 - // MUST create other GDI objects even if we don't handle them
68 - case 0x06fe: // CreateBitmap
69 - case 0x02fd: // CreateBitmapIndirect
70 - case 0x00f8: // CreateBrush
71 - case 0x02fb: // CreateFontIndirect
72 - case 0x00f7: // CreatePalette
73 - case 0x01f9: // CreatePatternBrush
74 - case 0x06ff: // CreateRegion
75 - case 0x0142: // DibCreatePatternBrush
76 - $dummyObject = array('type'=>'D');
77 - $this->_AddGDIObject($dummyObject);
78 - break;
79 - case 0x0106: // SetPolyFillMode
80 - $polyFillMode = unpack('smode', $parms);
81 - $polyFillMode = $polyFillMode['mode'];
82 - break;
83 - case 0x01f0: // DeleteObject
84 - $idx = unpack('Sidx', $parms);
85 - $idx = $idx['idx'];
86 - $this->_DeleteGDIObject($idx);
87 - break;
88 - case 0x012d: // SelectObject
89 - $idx = unpack('Sidx', $parms);
90 - $idx = $idx['idx'];
91 - $obj = $this->_GetGDIObject($idx);
92 - switch ($obj['type']) {
93 - case 'B':
94 - $nullBrush = false;
95 - if ($obj['style'] == 1) { $nullBrush = true; }
96 - else {
97 - $wmfdata .= $this->mpdf->SetFColor($this->mpdf->ConvertColor('rgb('.$obj['r'].','.$obj['g'].','.$obj['b'].')'), true)."\n";
98 - }
99 - break;
100 - case 'P':
101 - $nullPen = false;
102 - $dashArray = array();
103 - // dash parameters are custom
104 - switch ($obj['style']) {
105 - case 0: // PS_SOLID
106 - break;
107 - case 1: // PS_DASH
108 - $dashArray = array(3,1);
109 - break;
110 - case 2: // PS_DOT
111 - $dashArray = array(0.5,0.5);
112 - break;
113 - case 3: // PS_DASHDOT
114 - $dashArray = array(2,1,0.5,1);
115 - break;
116 - case 4: // PS_DASHDOTDOT
117 - $dashArray = array(2,1,0.5,1,0.5,1);
118 - break;
119 - case 5: // PS_NULL
120 - $nullPen = true;
121 - break;
122 - }
123 - if (!$nullPen) {
124 - $wmfdata .= $this->mpdf->SetDColor($this->mpdf->ConvertColor('rgb('.$obj['r'].','.$obj['g'].','.$obj['b'].')'), true)."\n";
125 - $wmfdata .= sprintf("%.3F w\n",$obj['width']*$k);
126 - }
127 - if (!empty($dashArray)) {
128 - $s = '[';
129 - for ($i=0; $i<count($dashArray);$i++) {
130 - $s .= $dashArray[$i] * $k;
131 - if ($i != count($dashArray)-1) { $s .= ' '; }
132 - }
133 - $s .= '] 0 d';
134 - $wmfdata .= $s."\n";
135 - }
136 - break;
137 - }
138 - break;
139 - case 0x0325: // Polyline
140 - case 0x0324: // Polygon
141 - $coords = unpack('s'.($size-3), $parms);
142 - $numpoints = $coords[1];
143 - for ($i = $numpoints; $i > 0; $i--) {
144 - $px = $coords[2*$i];
145 - $py = $coords[2*$i+1];
146 -
147 - if ($i < $numpoints) { $wmfdata .= $this->_LineTo($px, $py); }
148 - else { $wmfdata .= $this->_MoveTo($px, $py); }
149 - }
150 - if ($func == 0x0325) { $op = 's'; }
151 - else if ($func == 0x0324) {
152 - if ($nullPen) {
153 - if ($nullBrush) { $op = 'n'; } // no op
154 - else { $op = 'f'; } // fill
155 - }
156 - else {
157 - if ($nullBrush) { $op = 's'; } // stroke
158 - else { $op = 'b'; } // stroke and fill
159 - }
160 - if ($polyFillMode==1 && ($op=='b' || $op=='f')) { $op .= '*'; } // use even-odd fill rule
161 - }
162 - $wmfdata .= $op."\n";
163 - break;
164 - case 0x0538: // PolyPolygon
165 - $coords = unpack('s'.($size-3), $parms);
166 - $numpolygons = $coords[1];
167 - $adjustment = $numpolygons;
168 - for ($j = 1; $j <= $numpolygons; $j++) {
169 - $numpoints = $coords[$j + 1];
170 - for ($i = $numpoints; $i > 0; $i--) {
171 - $px = $coords[2*$i + $adjustment];
172 - $py = $coords[2*$i+1 + $adjustment];
173 - if ($i == $numpoints) { $wmfdata .= $this->_MoveTo($px, $py); }
174 - else { $wmfdata .= $this->_LineTo($px, $py); }
175 - }
176 - $adjustment += $numpoints * 2;
177 - }
178 -
179 - if ($nullPen) {
180 - if ($nullBrush) { $op = 'n'; } // no op
181 - else { $op = 'f'; } // fill
182 - }
183 - else {
184 - if ($nullBrush) { $op = 's'; } // stroke
185 - else { $op = 'b'; } // stroke and fill
186 - }
187 - if ($polyFillMode==1 && ($op=='b' || $op=='f')) { $op .= '*'; } // use even-odd fill rule
188 - $wmfdata .= $op."\n";
189 - break;
190 - case 0x0000:
191 - $endRecord = true;
192 - break;
193 - }
194 - }
195 -
196 -
197 - return array(1,$wmfdata,$wo,$we);
198 -}
199 -
200 -
201 -function _MoveTo($x, $y) {
202 - return "$x $y m\n";
203 -}
204 -
205 -// a line must have been started using _MoveTo() first
206 -function _LineTo($x, $y) {
207 - return "$x $y l\n";
208 -}
209 -
210 -function _AddGDIObject($obj) {
211 - // find next available slot
212 - $idx = 0;
213 - if (!empty($this->gdiObjectArray)) {
214 - $empty = false;
215 - $i = 0;
216 - while (!$empty) {
217 - $empty = !isset($this->gdiObjectArray[$i]);
218 - $i++;
219 - }
220 - $idx = $i-1;
221 - }
222 - $this->gdiObjectArray[$idx] = $obj;
223 -}
224 -
225 -function _GetGDIObject($idx) {
226 - return $this->gdiObjectArray[$idx];
227 -}
228 -
229 -function _DeleteGDIObject($idx) {
230 - unset($this->gdiObjectArray[$idx]);
231 -}
232 -
233 -
234 -}
235 -
236 -?>
1 +<?php
2 +
3 +class wmf
4 +{
5 +
6 + var $mpdf = null;
7 +
8 + var $gdiObjectArray;
9 +
10 + public function __construct(mPDF $mpdf)
11 + {
12 + $this->mpdf = $mpdf;
13 + }
14 +
15 + function _getWMFimage($data)
16 + {
17 + $k = _MPDFK;
18 +
19 + $this->gdiObjectArray = array();
20 + $a = unpack('stest', "\1\0");
21 + if ($a['test'] != 1)
22 + return array(0, 'Error parsing WMF image - Big-endian architecture not supported');
23 + // check for Aldus placeable metafile header
24 + $key = unpack('Lmagic', substr($data, 0, 4));
25 + $p = 18; // WMF header
26 + if ($key['magic'] == (int) 0x9AC6CDD7) {
27 + $p +=22;
28 + } // Aldus header
29 + // define some state variables
30 + $wo = null; // window origin
31 + $we = null; // window extent
32 + $polyFillMode = 0;
33 + $nullPen = false;
34 + $nullBrush = false;
35 + $endRecord = false;
36 + $wmfdata = '';
37 + while ($p < strlen($data) && !$endRecord) {
38 + $recordInfo = unpack('Lsize/Sfunc', substr($data, $p, 6));
39 + $p += 6;
40 + // size of record given in WORDs (= 2 bytes)
41 + $size = $recordInfo['size'];
42 + // func is number of GDI function
43 + $func = $recordInfo['func'];
44 + if ($size > 3) {
45 + $parms = substr($data, $p, 2 * ($size - 3));
46 + $p += 2 * ($size - 3);
47 + }
48 + switch ($func) {
49 + case 0x020b: // SetWindowOrg
50 + // do not allow window origin to be changed
51 + // after drawing has begun
52 + if (!$wmfdata)
53 + $wo = array_reverse(unpack('s2', $parms));
54 + break;
55 + case 0x020c: // SetWindowExt
56 + // do not allow window extent to be changed
57 + // after drawing has begun
58 + if (!$wmfdata)
59 + $we = array_reverse(unpack('s2', $parms));
60 + break;
61 + case 0x02fc: // CreateBrushIndirect
62 + $brush = unpack('sstyle/Cr/Cg/Cb/Ca/Shatch', $parms);
63 + $brush['type'] = 'B';
64 + $this->_AddGDIObject($brush);
65 + break;
66 + case 0x02fa: // CreatePenIndirect
67 + $pen = unpack('Sstyle/swidth/sdummy/Cr/Cg/Cb/Ca', $parms);
68 + // convert width from twips to user unit
69 + $pen['width'] /= (20 * $k);
70 + $pen['type'] = 'P';
71 + $this->_AddGDIObject($pen);
72 + break;
73 +
74 + // MUST create other GDI objects even if we don't handle them
75 + case 0x06fe: // CreateBitmap
76 + case 0x02fd: // CreateBitmapIndirect
77 + case 0x00f8: // CreateBrush
78 + case 0x02fb: // CreateFontIndirect
79 + case 0x00f7: // CreatePalette
80 + case 0x01f9: // CreatePatternBrush
81 + case 0x06ff: // CreateRegion
82 + case 0x0142: // DibCreatePatternBrush
83 + $dummyObject = array('type' => 'D');
84 + $this->_AddGDIObject($dummyObject);
85 + break;
86 + case 0x0106: // SetPolyFillMode
87 + $polyFillMode = unpack('smode', $parms);
88 + $polyFillMode = $polyFillMode['mode'];
89 + break;
90 + case 0x01f0: // DeleteObject
91 + $idx = unpack('Sidx', $parms);
92 + $idx = $idx['idx'];
93 + $this->_DeleteGDIObject($idx);
94 + break;
95 + case 0x012d: // SelectObject
96 + $idx = unpack('Sidx', $parms);
97 + $idx = $idx['idx'];
98 + $obj = $this->_GetGDIObject($idx);
99 + switch ($obj['type']) {
100 + case 'B':
101 + $nullBrush = false;
102 + if ($obj['style'] == 1) {
103 + $nullBrush = true;
104 + } else {
105 + $wmfdata .= $this->mpdf->SetFColor($this->mpdf->ConvertColor('rgb(' . $obj['r'] . ',' . $obj['g'] . ',' . $obj['b'] . ')'), true) . "\n";
106 + }
107 + break;
108 + case 'P':
109 + $nullPen = false;
110 + $dashArray = array();
111 + // dash parameters are custom
112 + switch ($obj['style']) {
113 + case 0: // PS_SOLID
114 + break;
115 + case 1: // PS_DASH
116 + $dashArray = array(3, 1);
117 + break;
118 + case 2: // PS_DOT
119 + $dashArray = array(0.5, 0.5);
120 + break;
121 + case 3: // PS_DASHDOT
122 + $dashArray = array(2, 1, 0.5, 1);
123 + break;
124 + case 4: // PS_DASHDOTDOT
125 + $dashArray = array(2, 1, 0.5, 1, 0.5, 1);
126 + break;
127 + case 5: // PS_NULL
128 + $nullPen = true;
129 + break;
130 + }
131 + if (!$nullPen) {
132 + $wmfdata .= $this->mpdf->SetDColor($this->mpdf->ConvertColor('rgb(' . $obj['r'] . ',' . $obj['g'] . ',' . $obj['b'] . ')'), true) . "\n";
133 + $wmfdata .= sprintf("%.3F w\n", $obj['width'] * $k);
134 + }
135 + if (!empty($dashArray)) {
136 + $s = '[';
137 + for ($i = 0; $i < count($dashArray); $i++) {
138 + $s .= $dashArray[$i] * $k;
139 + if ($i != count($dashArray) - 1) {
140 + $s .= ' ';
141 + }
142 + }
143 + $s .= '] 0 d';
144 + $wmfdata .= $s . "\n";
145 + }
146 + break;
147 + }
148 + break;
149 + case 0x0325: // Polyline
150 + case 0x0324: // Polygon
151 + $coords = unpack('s' . ($size - 3), $parms);
152 + $numpoints = $coords[1];
153 + for ($i = $numpoints; $i > 0; $i--) {
154 + $px = $coords[2 * $i];
155 + $py = $coords[2 * $i + 1];
156 +
157 + if ($i < $numpoints) {
158 + $wmfdata .= $this->_LineTo($px, $py);
159 + } else {
160 + $wmfdata .= $this->_MoveTo($px, $py);
161 + }
162 + }
163 + if ($func == 0x0325) {
164 + $op = 's';
165 + } else if ($func == 0x0324) {
166 + if ($nullPen) {
167 + if ($nullBrush) {
168 + $op = 'n';
169 + } // no op
170 + else {
171 + $op = 'f';
172 + } // fill
173 + } else {
174 + if ($nullBrush) {
175 + $op = 's';
176 + } // stroke
177 + else {
178 + $op = 'b';
179 + } // stroke and fill
180 + }
181 + if ($polyFillMode == 1 && ($op == 'b' || $op == 'f')) {
182 + $op .= '*';
183 + } // use even-odd fill rule
184 + }
185 + $wmfdata .= $op . "\n";
186 + break;
187 + case 0x0538: // PolyPolygon
188 + $coords = unpack('s' . ($size - 3), $parms);
189 + $numpolygons = $coords[1];
190 + $adjustment = $numpolygons;
191 + for ($j = 1; $j <= $numpolygons; $j++) {
192 + $numpoints = $coords[$j + 1];
193 + for ($i = $numpoints; $i > 0; $i--) {
194 + $px = $coords[2 * $i + $adjustment];
195 + $py = $coords[2 * $i + 1 + $adjustment];
196 + if ($i == $numpoints) {
197 + $wmfdata .= $this->_MoveTo($px, $py);
198 + } else {
199 + $wmfdata .= $this->_LineTo($px, $py);
200 + }
201 + }
202 + $adjustment += $numpoints * 2;
203 + }
204 +
205 + if ($nullPen) {
206 + if ($nullBrush) {
207 + $op = 'n';
208 + } // no op
209 + else {
210 + $op = 'f';
211 + } // fill
212 + } else {
213 + if ($nullBrush) {
214 + $op = 's';
215 + } // stroke
216 + else {
217 + $op = 'b';
218 + } // stroke and fill
219 + }
220 + if ($polyFillMode == 1 && ($op == 'b' || $op == 'f')) {
221 + $op .= '*';
222 + } // use even-odd fill rule
223 + $wmfdata .= $op . "\n";
224 + break;
225 + case 0x0000:
226 + $endRecord = true;
227 + break;
228 + }
229 + }
230 +
231 + return array(1, $wmfdata, $wo, $we);
232 + }
233 +
234 + function _MoveTo($x, $y)
235 + {
236 + return "$x $y m\n";
237 + }
238 +
239 + // a line must have been started using _MoveTo() first
240 + function _LineTo($x, $y)
241 + {
242 + return "$x $y l\n";
243 + }
244 +
245 + function _AddGDIObject($obj)
246 + {
247 + // find next available slot
248 + $idx = 0;
249 + if (!empty($this->gdiObjectArray)) {
250 + $empty = false;
251 + $i = 0;
252 + while (!$empty) {
253 + $empty = !isset($this->gdiObjectArray[$i]);
254 + $i++;
255 + }
256 + $idx = $i - 1;
257 + }
258 + $this->gdiObjectArray[$idx] = $obj;
259 + }
260 +
261 + function _GetGDIObject($idx)
262 + {
263 + return $this->gdiObjectArray[$idx];
264 + }
265 +
266 + function _DeleteGDIObject($idx)
267 + {
268 + unset($this->gdiObjectArray[$idx]);
269 + }
270 +
271 +}