1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
tfpdf.php
См. документацию.
1<?php
2/*******************************************************************************
3* tFPDF (based on FPDF 1.7) *
4* *
5* Version: 1.24 *
6* Date: 2011-09-24 *
7* Author: Ian Back <ianb@bpm1.com> *
8* License: LGPL *
9*******************************************************************************/
10
11define('tFPDF_VERSION','1.24');
12
13class tFPDF
14{
15
16var $unifontSubset;
17var $page; // current page number
18var $n; // current object number
19var $offsets; // array of object offsets
20var $buffer; // buffer holding in-memory PDF
21var $pages; // array containing pages
22var $state; // current document state
23var $compress; // compression flag
24var $k; // scale factor (number of points in user unit)
25var $DefOrientation; // default orientation
26var $CurOrientation; // current orientation
27var $StdPageSizes; // standard page sizes
28var $DefPageSize; // default page size
29var $CurPageSize; // current page size
30var $PageSizes; // used for pages with non default sizes or orientations
31var $wPt, $hPt; // dimensions of current page in points
32var $w, $h; // dimensions of current page in user unit
33var $lMargin; // left margin
34var $tMargin; // top margin
35var $rMargin; // right margin
36var $bMargin; // page break margin
37var $cMargin; // cell margin
38var $x, $y; // current position in user unit
39var $lasth; // height of last printed cell
40var $LineWidth; // line width in user unit
41var $fontpath; // path containing fonts
42var $CoreFonts; // array of core font names
43var $fonts; // array of used fonts
44var $FontFiles; // array of font files
45var $diffs; // array of encoding differences
46var $FontFamily; // current font family
47var $FontStyle; // current font style
48var $underline; // underlining flag
49var $CurrentFont; // current font info
50var $FontSizePt; // current font size in points
51var $FontSize; // current font size in user unit
52var $DrawColor; // commands for drawing color
53var $FillColor; // commands for filling color
54var $TextColor; // commands for text color
55var $ColorFlag; // indicates whether fill and text colors are different
56var $ws; // word spacing
57var $images; // array of used images
58var $PageLinks; // array of links in pages
59var $links; // array of internal links
60var $AutoPageBreak; // automatic page breaking
61var $PageBreakTrigger; // threshold used to trigger page breaks
62var $InHeader; // flag set when processing header
63var $InFooter; // flag set when processing footer
64var $ZoomMode; // zoom display mode
65var $LayoutMode; // layout display mode
66var $title; // title
67var $subject; // subject
68var $author; // author
69var $keywords; // keywords
70var $creator; // creator
71var $AliasNbPages; // alias for total number of pages
72var $PDFVersion; // PDF version number
73
74/*******************************************************************************
75* *
76* Public methods *
77* *
78*******************************************************************************/
79public function __construct($orientation='P', $unit='mm', $size='A4')
80{
81 // Some checks
82 $this->_dochecks();
83 // Initialization of properties
84 $this->page = 0;
85 $this->n = 2;
86 $this->buffer = '';
87 $this->pages = array();
88 $this->PageSizes = array();
89 $this->state = 0;
90 $this->fonts = array();
91 $this->FontFiles = array();
92 $this->diffs = array();
93 $this->images = array();
94 $this->links = array();
95 $this->InHeader = false;
96 $this->InFooter = false;
97 $this->lasth = 0;
98 $this->FontFamily = '';
99 $this->FontStyle = '';
100 $this->FontSizePt = 12;
101 $this->underline = false;
102 $this->DrawColor = '0 G';
103 $this->FillColor = '0 g';
104 $this->TextColor = '0 g';
105 $this->ColorFlag = false;
106 $this->ws = 0;
107 // Font path
108 if(defined('FPDF_FONTPATH'))
109 {
110 $this->fontpath = FPDF_FONTPATH;
111 if(mb_substr($this->fontpath,-1,mb_strlen($this->fontpath,'8bit'),'8bit')!='/' && mb_substr($this->fontpath,-1,mb_strlen($this->fontpath,'8bit'),'8bit')!='\\')
112 $this->fontpath .= '/';
113 }
114 elseif(is_dir(__DIR__.'/font'))
115 $this->fontpath = __DIR__.'/font/';
116 else
117 $this->fontpath = '';
118 // Core fonts
119 $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats');
120 // Scale factor
121 if($unit=='pt')
122 $this->k = 1;
123 elseif($unit=='mm')
124 $this->k = 72/25.4;
125 elseif($unit=='cm')
126 $this->k = 72/2.54;
127 elseif($unit=='in')
128 $this->k = 72;
129 else
130 $this->Error('Incorrect unit: '.$unit);
131 // Page sizes
132 $this->StdPageSizes = array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),
133 'letter'=>array(612,792), 'legal'=>array(612,1008));
134 $size = $this->_getpagesize($size);
135 $this->DefPageSize = $size;
136 $this->CurPageSize = $size;
137 // Page orientation
138 $orientation = mb_strtolower($orientation);
139 if($orientation=='p' || $orientation=='portrait')
140 {
141 $this->DefOrientation = 'P';
142 $this->w = $size[0];
143 $this->h = $size[1];
144 }
145 elseif($orientation=='l' || $orientation=='landscape')
146 {
147 $this->DefOrientation = 'L';
148 $this->w = $size[1];
149 $this->h = $size[0];
150 }
151 else
152 $this->Error('Incorrect orientation: '.$orientation);
153 $this->CurOrientation = $this->DefOrientation;
154 $this->wPt = $this->w*$this->k;
155 $this->hPt = $this->h*$this->k;
156 // Page margins (1 cm)
157 $margin = 28.35/$this->k;
158 $this->SetMargins($margin,$margin);
159 // Interior cell margin (1 mm)
160 $this->cMargin = $margin/10;
161 // Line width (0.2 mm)
162 $this->LineWidth = .567/$this->k;
163 // Automatic page break
164 $this->SetAutoPageBreak(true,2*$margin);
165 // Default display mode
166 $this->SetDisplayMode('default');
167 // Enable compression
168 $this->SetCompression(true);
169 // Set default PDF version number
170 $this->PDFVersion = '1.3';
171}
172
173function SetMargins($left, $top, $right=null)
174{
175 // Set left, top and right margins
176 $this->lMargin = $left;
177 $this->tMargin = $top;
178 if($right===null)
179 $right = $left;
180 $this->rMargin = $right;
181}
182
183function SetLeftMargin($margin)
184{
185 // Set left margin
186 $this->lMargin = $margin;
187 if($this->page>0 && $this->x<$margin)
188 $this->x = $margin;
189}
190
191function SetTopMargin($margin)
192{
193 // Set top margin
194 $this->tMargin = $margin;
195}
196
197function SetRightMargin($margin)
198{
199 // Set right margin
200 $this->rMargin = $margin;
201}
202
203function SetAutoPageBreak($auto, $margin=0)
204{
205 // Set auto page break mode and triggering margin
206 $this->AutoPageBreak = $auto;
207 $this->bMargin = $margin;
208 $this->PageBreakTrigger = $this->h-$margin;
209}
210
211function SetDisplayMode($zoom, $layout='default')
212{
213 // Set display mode in viewer
214 if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
215 $this->ZoomMode = $zoom;
216 else
217 $this->Error('Incorrect zoom display mode: '.$zoom);
218 if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
219 $this->LayoutMode = $layout;
220 else
221 $this->Error('Incorrect layout display mode: '.$layout);
222}
223
224function SetCompression($compress)
225{
226 // Set page compression
227 if(function_exists('gzcompress'))
228 $this->compress = $compress;
229 else
230 $this->compress = false;
231}
232
233function SetTitle($title, $isUTF8=false)
234{
235 // Title of document
236 if($isUTF8)
237 $title = $this->_UTF8toUTF16($title);
238 $this->title = $title;
239}
240
241function SetSubject($subject, $isUTF8=false)
242{
243 // Subject of document
244 if($isUTF8)
245 $subject = $this->_UTF8toUTF16($subject);
246 $this->subject = $subject;
247}
248
249function SetAuthor($author, $isUTF8=false)
250{
251 // Author of document
252 if($isUTF8)
253 $author = $this->_UTF8toUTF16($author);
254 $this->author = $author;
255}
256
257function SetKeywords($keywords, $isUTF8=false)
258{
259 // Keywords of document
260 if($isUTF8)
261 $keywords = $this->_UTF8toUTF16($keywords);
262 $this->keywords = $keywords;
263}
264
265function SetCreator($creator, $isUTF8=false)
266{
267 // Creator of document
268 if($isUTF8)
269 $creator = $this->_UTF8toUTF16($creator);
270 $this->creator = $creator;
271}
272
273function AliasNbPages($alias='{nb}')
274{
275 // Define an alias for total number of pages
276 $this->AliasNbPages = $alias;
277}
278
279function Error($msg)
280{
281 // Fatal error
282 throw new Exception($msg, E_USER_ERROR);
283}
284
285function Open()
286{
287 // Begin document
288 $this->state = 1;
289}
290
291function Close()
292{
293 // Terminate document
294 if($this->state==3)
295 return;
296 if($this->page==0)
297 $this->AddPage();
298 // Page footer
299 $this->InFooter = true;
300 $this->Footer();
301 $this->InFooter = false;
302 // Close page
303 $this->_endpage();
304 // Close document
305 $this->_enddoc();
306}
307
308function AddPage($orientation='', $size='')
309{
310 // Start a new page
311 if($this->state==0)
312 $this->Open();
313 $family = $this->FontFamily;
314 $style = $this->FontStyle.($this->underline ? 'U' : '');
315 $fontsize = $this->FontSizePt;
316 $lw = $this->LineWidth;
317 $dc = $this->DrawColor;
318 $fc = $this->FillColor;
319 $tc = $this->TextColor;
320 $cf = $this->ColorFlag;
321 if($this->page>0)
322 {
323 // Page footer
324 $this->InFooter = true;
325 $this->Footer();
326 $this->InFooter = false;
327 // Close page
328 $this->_endpage();
329 }
330 // Start new page
331 $this->_beginpage($orientation,$size);
332 // Set line cap style to square
333 $this->_out('2 J');
334 // Set line width
335 $this->LineWidth = $lw;
336 $this->_out(sprintf('%.2F w',$lw*$this->k));
337 // Set font
338 if($family)
339 $this->SetFont($family,$style,$fontsize);
340 // Set colors
341 $this->DrawColor = $dc;
342 if($dc!='0 G')
343 $this->_out($dc);
344 $this->FillColor = $fc;
345 if($fc!='0 g')
346 $this->_out($fc);
347 $this->TextColor = $tc;
348 $this->ColorFlag = $cf;
349 // Page header
350 $this->InHeader = true;
351 $this->Header();
352 $this->InHeader = false;
353 // Restore line width
354 if($this->LineWidth!=$lw)
355 {
356 $this->LineWidth = $lw;
357 $this->_out(sprintf('%.2F w',$lw*$this->k));
358 }
359 // Restore font
360 if($family)
361 $this->SetFont($family,$style,$fontsize);
362 // Restore colors
363 if($this->DrawColor!=$dc)
364 {
365 $this->DrawColor = $dc;
366 $this->_out($dc);
367 }
368 if($this->FillColor!=$fc)
369 {
370 $this->FillColor = $fc;
371 $this->_out($fc);
372 }
373 $this->TextColor = $tc;
374 $this->ColorFlag = $cf;
375}
376
377function Header()
378{
379 // To be implemented in your own inherited class
380}
381
382function Footer()
383{
384 // To be implemented in your own inherited class
385}
386
387function PageNo()
388{
389 // Get current page number
390 return $this->page;
391}
392
393function SetDrawColor($r, $g=null, $b=null)
394{
395 // Set color for all stroking operations
396 if(($r==0 && $g==0 && $b==0) || $g===null)
397 $this->DrawColor = sprintf('%.3F G',$r/255);
398 else
399 $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255);
400 if($this->page>0)
401 $this->_out($this->DrawColor);
402}
403
404function SetFillColor($r, $g=null, $b=null)
405{
406 // Set color for all filling operations
407 if(($r==0 && $g==0 && $b==0) || $g===null)
408 $this->FillColor = sprintf('%.3F g',$r/255);
409 else
410 $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
411 $this->ColorFlag = ($this->FillColor!=$this->TextColor);
412 if($this->page>0)
413 $this->_out($this->FillColor);
414}
415
416function SetTextColor($r, $g=null, $b=null)
417{
418 // Set color for text
419 if(($r==0 && $g==0 && $b==0) || $g===null)
420 $this->TextColor = sprintf('%.3F g',$r/255);
421 else
422 $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
423 $this->ColorFlag = ($this->FillColor!=$this->TextColor);
424}
425
426function GetStringWidth($s)
427{
428 // Get width of a string in the current font
429 $s = (string)$s;
430 $cw = &$this->CurrentFont['cw'];
431 $w=0;
432 if ($this->unifontSubset) {
433 $unicode = $this->UTF8StringToArray($s);
434 foreach($unicode as $char) {
435 if (isset($cw[$char])) { $w += (ord($cw[2*$char])<<8) + ord($cw[2*$char+1]); }
436 else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w += $cw[chr($char)]; }
437 else if(isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; }
438 else if(isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; }
439 else { $w += 500; }
440 }
441 }
442 else {
443 $l = mb_strlen($s,'8bit');
444 for($i=0;$i<$l;$i++)
445 $w += $cw[$s[$i]];
446 }
447 return $w*$this->FontSize/1000;
448}
449
450function SetLineWidth($width)
451{
452 // Set line width
453 $this->LineWidth = $width;
454 if($this->page>0)
455 $this->_out(sprintf('%.2F w',$width*$this->k));
456}
457
458function Line($x1, $y1, $x2, $y2)
459{
460 // Draw a line
461 $this->_out(sprintf('%.2F %.2F m %.2F %.2F l S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k));
462}
463
464function Rect($x, $y, $w, $h, $style='')
465{
466 // Draw a rectangle
467 if($style=='F')
468 $op = 'f';
469 elseif($style=='FD' || $style=='DF')
470 $op = 'B';
471 else
472 $op = 'S';
473 $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
474}
475
476function AddFont($family, $style='', $file='', $uni=false)
477{
478 // Add a TrueType, OpenType or Type1 font
479 $family = mb_strtolower($family);
480 $style = mb_strtoupper($style);
481 if($style=='IB')
482 $style='BI';
483 if($file=='') {
484 if ($uni) {
485 $file = str_replace(' ','',$family).mb_strtolower($style).'.ttf';
486 }
487 else {
488 $file = str_replace(' ','',$family).mb_strtolower($style).'.php';
489 }
490 }
491 $fontkey = $family.$style;
492 if(isset($this->fonts[$fontkey]))
493 return;
494
495 if ($uni) {
496 if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; }
497 else { $ttffilename = $this->_getfontpath().'/'.$file ; }
498 $unifilename = $this->_getfontpath().'/'.mb_strtolower(mb_substr($file,0,(mb_strpos($file,'.',0,'8bit')),'8bit'));
499 $name = '';
500 $originalsize = 0;
501 $ttfstat = stat($ttffilename);
502 if (file_exists($unifilename.'.mtx.php')) {
503 include($unifilename.'.mtx.php');
504 }
505 if (!isset($type) || !isset($name) || $originalsize != $ttfstat['size']) {
506 $ttffile = $ttffilename;
507 require_once(__DIR__.'/ttfonts.php');
508 $ttf = new TTFontFile();
509 $ttf->getMetrics($ttffile);
510 $cw = $ttf->charWidths;
511 $name = preg_replace('/[ ()]/','',$ttf->fullName);
512
513 $desc= array('Ascent'=>round($ttf->ascent),
514 'Descent'=>round($ttf->descent),
515 'CapHeight'=>round($ttf->capHeight),
516 'Flags'=>$ttf->flags,
517 'FontBBox'=>'['.round($ttf->bbox[0])." ".round($ttf->bbox[1])." ".round($ttf->bbox[2])." ".round($ttf->bbox[3]).']',
518 'ItalicAngle'=>$ttf->italicAngle,
519 'StemV'=>round($ttf->stemV),
520 'MissingWidth'=>round($ttf->defaultWidth));
521 $up = round($ttf->underlinePosition);
522 $ut = round($ttf->underlineThickness);
523 $originalsize = $ttfstat['size']+0;
524 $type = 'TTF';
525 // Generate metrics .php file
526 $s='<?php'."\n";
527 $s.='$name=\''.$name."';\n";
528 $s.='$type=\''.$type."';\n";
529 $s.='$desc='.var_export($desc,true).";\n";
530 $s.='$up='.$up.";\n";
531 $s.='$ut='.$ut.";\n";
532 $s.='$ttffile=dirname(__FILE__).\'/'.$file."';\n";
533 $s.='$originalsize='.$originalsize.";\n";
534 $s.='$fontkey=\''.$fontkey."';\n";
535 $s.="?>";
536 if (is_writable(dirname($this->_getfontpath().'/'.'x'))) {
537 $fh = fopen($unifilename.'.mtx.php',"w");
538 fwrite($fh,$s,mb_strlen($s,'8bit'));
539 fclose($fh);
540 $fh = fopen($unifilename.'.cw.dat',"wb");
541 fwrite($fh,$cw,mb_strlen($cw,'8bit'));
542 fclose($fh);
543 @unlink($unifilename.'.cw127.php');
544 }
545 unset($ttf);
546 }
547 else {
548 $cw = @file_get_contents($unifilename.'.cw.dat');
549 }
550 $i = count($this->fonts)+1;
551 if(!empty($this->AliasNbPages))
552 $sbarr = range(0,57);
553 else
554 $sbarr = range(0,32);
555 $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'ttffile'=>$ttffile, 'fontkey'=>$fontkey, 'subset'=>$sbarr, 'unifilename'=>$unifilename);
556
557 $this->FontFiles[$fontkey]=array('length1'=>$originalsize, 'type'=>"TTF", 'ttffile'=>$ttffile);
558 $this->FontFiles[$file]=array('type'=>"TTF");
559 unset($cw);
560 }
561 else {
562 $info = $this->_loadfont($file);
563 $info['i'] = count($this->fonts)+1;
564 if(!empty($info['diff']))
565 {
566 // Search existing encodings
567 $n = array_search($info['diff'],$this->diffs);
568 if(!$n)
569 {
570 $n = count($this->diffs)+1;
571 $this->diffs[$n] = $info['diff'];
572 }
573 $info['diffn'] = $n;
574 }
575 if(!empty($info['file']))
576 {
577 // Embedded font
578 if($info['type']=='TrueType')
579 $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']);
580 else
581 $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']);
582 }
583 $this->fonts[$fontkey] = $info;
584 }
585}
586
587function SetFont($family, $style='', $size=0)
588{
589 // Select a font; size given in points
590 if($family=='')
591 $family = $this->FontFamily;
592 else
593 $family = mb_strtolower($family);
594 $style = mb_strtoupper($style);
595 if(mb_strpos($style,'U',0,'8bit')!==false)
596 {
597 $this->underline = true;
598 $style = str_replace('U','',$style);
599 }
600 else
601 $this->underline = false;
602 if($style=='IB')
603 $style = 'BI';
604 if($size==0)
605 $size = $this->FontSizePt;
606 // Test if font is already selected
607 if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
608 return;
609 // Test if font is already loaded
610 $fontkey = $family.$style;
611 if(!isset($this->fonts[$fontkey]))
612 {
613 // Test if one of the core fonts
614 if($family=='arial')
615 $family = 'helvetica';
616 if(in_array($family,$this->CoreFonts))
617 {
618 if($family=='symbol' || $family=='zapfdingbats')
619 $style = '';
620 $fontkey = $family.$style;
621 if(!isset($this->fonts[$fontkey]))
622 $this->AddFont($family,$style);
623 }
624 else
625 $this->Error('Undefined font: '.$family.' '.$style);
626 }
627 // Select it
628 $this->FontFamily = $family;
629 $this->FontStyle = $style;
630 $this->FontSizePt = $size;
631 $this->FontSize = $size/$this->k;
632 $this->CurrentFont = &$this->fonts[$fontkey];
633 if ($this->fonts[$fontkey]['type']=='TTF') { $this->unifontSubset = true; }
634 else { $this->unifontSubset = false; }
635 if($this->page>0)
636 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
637}
638
639function SetFontSize($size)
640{
641 // Set font size in points
642 if($this->FontSizePt==$size)
643 return;
644 $this->FontSizePt = $size;
645 $this->FontSize = $size/$this->k;
646 if($this->page>0)
647 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
648}
649
650function AddLink()
651{
652 // Create a new internal link
653 $n = count($this->links)+1;
654 $this->links[$n] = array(0, 0);
655 return $n;
656}
657
658function SetLink($link, $y=0, $page=-1)
659{
660 // Set destination of internal link
661 if($y==-1)
662 $y = $this->y;
663 if($page==-1)
665 $this->links[$link] = array($page, $y);
666}
667
668function Link($x, $y, $w, $h, $link)
669{
670 // Put a link on the page
671 $this->PageLinks[$this->page][] = array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link);
672}
673
674function Text($x, $y, $txt)
675{
676 // Output a string
677 if ($this->unifontSubset)
678 {
679 $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')';
680 foreach($this->UTF8StringToArray($txt) as $uni)
681 $this->CurrentFont['subset'][$uni] = $uni;
682 }
683 else
684 $txt2 = '('.$this->_escape($txt).')';
685 $s = sprintf('BT %.2F %.2F Td %s Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt2);
686 if($this->underline && $txt!='')
687 $s .= ' '.$this->_dounderline($x,$y,$txt);
688 if($this->ColorFlag)
689 $s = 'q '.$this->TextColor.' '.$s.' Q';
690 $this->_out($s);
691}
692
693function AcceptPageBreak()
694{
695 // Accept automatic page break or not
696 return $this->AutoPageBreak;
697}
698
699function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
700{
701 // Output a cell
702 $k = $this->k;
703 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
704 {
705 // Automatic page break
706 $x = $this->x;
707 $ws = $this->ws;
708 if($ws>0)
709 {
710 $this->ws = 0;
711 $this->_out('0 Tw');
712 }
713 $this->AddPage($this->CurOrientation,$this->CurPageSize);
714 $this->x = $x;
715 if($ws>0)
716 {
717 $this->ws = $ws;
718 $this->_out(sprintf('%.3F Tw',$ws*$k));
719 }
720 }
721 if($w==0)
722 $w = $this->w-$this->rMargin-$this->x;
723 $s = '';
724 if($fill || $border==1)
725 {
726 if($fill)
727 $op = ($border==1) ? 'B' : 'f';
728 else
729 $op = 'S';
730 $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
731 }
732 if(is_string($border))
733 {
734 $x = $this->x;
735 $y = $this->y;
736 if(mb_strpos($border,'L',0,'8bit')!==false)
737 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
738 if(mb_strpos($border,'T',0,'8bit')!==false)
739 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
740 if(mb_strpos($border,'R',0,'8bit')!==false)
741 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
742 if(mb_strpos($border,'B',0,'8bit')!==false)
743 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
744 }
745 if($txt!=='')
746 {
747 if($align=='R')
748 $dx = $w-$this->cMargin-$this->GetStringWidth($txt);
749 elseif($align=='C')
750 $dx = ($w-$this->GetStringWidth($txt))/2;
751 else
752 $dx = $this->cMargin;
753 if($this->ColorFlag)
754 $s .= 'q '.$this->TextColor.' ';
755
756 // If multibyte, Tw has no effect - do word spacing using an adjustment before each space
757 if ($this->ws && $this->unifontSubset) {
758 foreach($this->UTF8StringToArray($txt) as $uni)
759 $this->CurrentFont['subset'][$uni] = $uni;
760 $space = $this->_escape($this->UTF8ToUTF16BE(' ', false));
761 $s .= sprintf('BT 0 Tw %.2F %.2F Td [',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k);
762 $t = explode(' ',$txt);
763 $numt = count($t);
764 for($i=0;$i<$numt;$i++) {
765 $tx = $t[$i];
766 $tx = '('.$this->_escape($this->UTF8ToUTF16BE($tx, false)).')';
767 $s .= sprintf('%s ',$tx);
768 if (($i+1)<$numt) {
769 $adj = -($this->ws*$this->k)*1000/$this->FontSizePt;
770 $s .= sprintf('%d(%s) ',$adj,$space);
771 }
772 }
773 $s .= '] TJ';
774 $s .= ' ET';
775 }
776 else {
777 if ($this->unifontSubset)
778 {
779 $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')';
780 foreach($this->UTF8StringToArray($txt) as $uni)
781 $this->CurrentFont['subset'][$uni] = $uni;
782 }
783 else
784 $txt2='('.str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))).')';
785 $s .= sprintf('BT %.2F %.2F Td %s Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
786 }
787 if($this->underline)
788 $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
789 if($this->ColorFlag)
790 $s .= ' Q';
791 if($link)
792 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
793 }
794 if($s)
795 $this->_out($s);
796 $this->lasth = $h;
797 if($ln>0)
798 {
799 // Go to next line
800 $this->y += $h;
801 if($ln==1)
802 $this->x = $this->lMargin;
803 }
804 else
805 $this->x += $w;
806}
807
808function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
809{
810 // Output text with automatic or explicit line breaks
811 $cw = &$this->CurrentFont['cw'];
812 if($w==0)
813 $w = $this->w-$this->rMargin-$this->x;
814 $wmax = ($w-2*$this->cMargin);
815 $s = str_replace("\r",'',$txt);
816 if ($this->unifontSubset) {
817 $nb=mb_strlen($s, 'utf-8');
818 while($nb>0 && mb_substr($s,$nb-1,1,'utf-8')=="\n") $nb--;
819 }
820 else {
821 $nb = mb_strlen($s,'8bit');
822 if($nb>0 && $s[$nb-1]=="\n")
823 $nb--;
824 }
825 $b = 0;
826 if($border)
827 {
828 if($border==1)
829 {
830 $border = 'LTRB';
831 $b = 'LRT';
832 $b2 = 'LR';
833 }
834 else
835 {
836 $b2 = '';
837 if(mb_strpos($border,'L',0,'8bit')!==false)
838 $b2 .= 'L';
839 if(mb_strpos($border,'R',0,'8bit')!==false)
840 $b2 .= 'R';
841 $b = (mb_strpos($border,'T',0,'8bit')!==false) ? $b2.'T' : $b2;
842 }
843 }
844 $sep = -1;
845 $i = 0;
846 $j = 0;
847 $l = 0;
848 $ns = 0;
849 $nl = 1;
850 while($i<$nb)
851 {
852 // Get next character
853 if ($this->unifontSubset) {
854 $c = mb_substr($s,$i,1,'UTF-8');
855 }
856 else {
857 $c=$s[$i];
858 }
859 if($c=="\n")
860 {
861 // Explicit line break
862 if($this->ws>0)
863 {
864 $this->ws = 0;
865 $this->_out('0 Tw');
866 }
867 if ($this->unifontSubset) {
868 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
869 }
870 else {
871 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'8bit'),$b,2,$align,$fill);
872 }
873 $i++;
874 $sep = -1;
875 $j = $i;
876 $l = 0;
877 $ns = 0;
878 $nl++;
879 if($border && $nl==2)
880 $b = $b2;
881 continue;
882 }
883 if($c==' ')
884 {
885 $sep = $i;
886 $ls = $l;
887 $ns++;
888 }
889
890 if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
891 else { $l += $cw[$c]*$this->FontSize/1000; }
892
893 if($l>$wmax)
894 {
895 // Automatic line break
896 if($sep==-1)
897 {
898 if($i==$j)
899 $i++;
900 if($this->ws>0)
901 {
902 $this->ws = 0;
903 $this->_out('0 Tw');
904 }
905 if ($this->unifontSubset) {
906 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
907 }
908 else {
909 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'8bit'),$b,2,$align,$fill);
910 }
911 }
912 else
913 {
914 if($align=='J')
915 {
916 $this->ws = ($ns>1) ? ($wmax-$ls)/($ns-1) : 0;
917 $this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
918 }
919 if ($this->unifontSubset) {
920 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),$b,2,$align,$fill);
921 }
922 else {
923 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'8bit'),$b,2,$align,$fill);
924 }
925 $i = $sep+1;
926 }
927 $sep = -1;
928 $j = $i;
929 $l = 0;
930 $ns = 0;
931 $nl++;
932 if($border && $nl==2)
933 $b = $b2;
934 }
935 else
936 $i++;
937 }
938 // Last chunk
939 if($this->ws>0)
940 {
941 $this->ws = 0;
942 $this->_out('0 Tw');
943 }
944 if($border && mb_strpos($border,'B',0,'8bit')!==false)
945 $b .= 'B';
946 if ($this->unifontSubset) {
947 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
948 }
949 else {
950 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'8bit'),$b,2,$align,$fill);
951 }
952 $this->x = $this->lMargin;
953}
954
955function Write($h, $txt, $link='')
956{
957 // Output text in flowing mode
958 $cw = &$this->CurrentFont['cw'];
959 $w = $this->w-$this->rMargin-$this->x;
960
961 $wmax = ($w-2*$this->cMargin);
962 $s = str_replace("\r",'',$txt);
963 if ($this->unifontSubset) {
964 $nb = mb_strlen($s, 'UTF-8');
965 if($nb==1 && $s==" ") {
966 $this->x += $this->GetStringWidth($s);
967 return;
968 }
969 }
970 else {
971 $nb = mb_strlen($s,'8bit');
972 }
973 $sep = -1;
974 $i = 0;
975 $j = 0;
976 $l = 0;
977 $nl = 1;
978 while($i<$nb)
979 {
980 // Get next character
981 if ($this->unifontSubset) {
982 $c = mb_substr($s,$i,1,'UTF-8');
983 }
984 else {
985 $c = $s[$i];
986 }
987 if($c=="\n")
988 {
989 // Explicit line break
990 if ($this->unifontSubset) {
991 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link);
992 }
993 else {
994 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'8bit'),0,2,'',0,$link);
995 }
996 $i++;
997 $sep = -1;
998 $j = $i;
999 $l = 0;
1000 if($nl==1)
1001 {
1002 $this->x = $this->lMargin;
1003 $w = $this->w-$this->rMargin-$this->x;
1004 $wmax = ($w-2*$this->cMargin);
1005 }
1006 $nl++;
1007 continue;
1008 }
1009 if($c==' ')
1010 $sep = $i;
1011
1012 if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
1013 else { $l += $cw[$c]*$this->FontSize/1000; }
1014
1015 if($l>$wmax)
1016 {
1017 // Automatic line break
1018 if($sep==-1)
1019 {
1020 if($this->x>$this->lMargin)
1021 {
1022 // Move to next line
1023 $this->x = $this->lMargin;
1024 $this->y += $h;
1025 $w = $this->w-$this->rMargin-$this->x;
1026 $wmax = ($w-2*$this->cMargin);
1027 $i++;
1028 $nl++;
1029 continue;
1030 }
1031 if($i==$j)
1032 $i++;
1033 if ($this->unifontSubset) {
1034 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link);
1035 }
1036 else {
1037 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'8bit'),0,2,'',0,$link);
1038 }
1039 }
1040 else
1041 {
1042 if ($this->unifontSubset) {
1043 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),0,2,'',0,$link);
1044 }
1045 else {
1046 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'8bit'),0,2,'',0,$link);
1047 }
1048 $i = $sep+1;
1049 }
1050 $sep = -1;
1051 $j = $i;
1052 $l = 0;
1053 if($nl==1)
1054 {
1055 $this->x = $this->lMargin;
1056 $w = $this->w-$this->rMargin-$this->x;
1057 $wmax = ($w-2*$this->cMargin);
1058 }
1059 $nl++;
1060 }
1061 else
1062 $i++;
1063 }
1064 // Last chunk
1065 if($i!=$j) {
1066 if ($this->unifontSubset) {
1067 $this->Cell($l,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',0,$link);
1068 }
1069 else {
1070 $this->Cell($l,$h,mb_substr($s,$j,mb_strlen($s,'8bit'),'8bit'),0,0,'',0,$link);
1071 }
1072 }
1073}
1074
1075function Ln($h=null)
1076{
1077 if ($h === null)
1078 $h = $this->lasth;
1079
1080 $k = $this->k;
1081 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
1082 {
1083 // Automatic page break
1084 $x = $this->x;
1085 $ws = $this->ws;
1086 if($ws>0)
1087 {
1088 $this->ws = 0;
1089 $this->_out('0 Tw');
1090 }
1091 $this->AddPage($this->CurOrientation,$this->CurPageSize);
1092 $this->x = $x;
1093 if($ws>0)
1094 {
1095 $this->ws = $ws;
1096 $this->_out(sprintf('%.3F Tw',$ws*$k));
1097 }
1098 }
1099
1100 // Line feed; default value is last cell height
1101 $this->x = $this->lMargin;
1102 $this->y += $h;
1103}
1104
1105function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
1106{
1107 // Put an image on the page
1108 if(!isset($this->images[$file]))
1109 {
1110 // First use of this image, get info
1111 if($type=='')
1112 {
1113 $pos = mb_strrpos($file,'.',0,'8bit');
1114 if(!$pos)
1115 $this->Error('Image file has no extension and no type was specified: '.$file);
1116 $type = mb_substr($file,$pos+1,mb_strlen($file,'8bit'),'8bit');
1117 }
1118 $type = mb_strtolower($type);
1119 if($type=='jpeg')
1120 $type = 'jpg';
1121 $mtd = '_parse'.$type;
1122 if(!method_exists($this,$mtd))
1123 $this->Error('Unsupported image type: '.$type);
1124 $info = $this->$mtd($file);
1125 $info['i'] = count($this->images)+1;
1126 $this->images[$file] = $info;
1127 }
1128 else
1129 $info = $this->images[$file];
1130
1131 // Automatic width and height calculation if needed
1132 if($w==0 && $h==0)
1133 {
1134 // Put image at 96 dpi
1135 $w = -96;
1136 $h = -96;
1137 }
1138 if($w<0)
1139 $w = -$info['w']*72/$w/$this->k;
1140 if($h<0)
1141 $h = -$info['h']*72/$h/$this->k;
1142 if($w==0)
1143 $w = $h*$info['w']/$info['h'];
1144 if($h==0)
1145 $h = $w*$info['h']/$info['w'];
1146
1147 // Flowing mode
1148 if($y===null)
1149 {
1150 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
1151 {
1152 // Automatic page break
1153 $x2 = $this->x;
1154 $this->AddPage($this->CurOrientation,$this->CurPageSize);
1155 $this->x = $x2;
1156 }
1157 $y = $this->y;
1158 $this->y += $h;
1159 }
1160
1161 if($x===null)
1162 $x = $this->x;
1163 $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i']));
1164 if($link)
1165 $this->Link($x,$y,$w,$h,$link);
1166}
1167
1168function GetX()
1169{
1170 // Get x position
1171 return $this->x;
1172}
1173
1174function SetX($x)
1175{
1176 // Set x position
1177 if($x>=0)
1178 $this->x = $x;
1179 else
1180 $this->x = $this->w+$x;
1181}
1182
1183function GetY()
1184{
1185 // Get y position
1186 return $this->y;
1187}
1188
1189function SetY($y)
1190{
1191 // Set y position and reset x
1192 $this->x = $this->lMargin;
1193 if($y>=0)
1194 $this->y = $y;
1195 else
1196 $this->y = $this->h+$y;
1197}
1198
1199function SetXY($x, $y)
1200{
1201 // Set x and y positions
1202 $this->SetY($y);
1203 $this->SetX($x);
1204}
1205
1206function Output($name='', $dest='', $utfName = '')
1207{
1208 // Output PDF to some destination
1209 if($this->state<3)
1210 $this->Close();
1211 $dest = mb_strtoupper($dest);
1212 if($dest=='')
1213 {
1214 if($name=='')
1215 {
1216 $name = 'doc.pdf';
1217 $dest = 'I';
1218 }
1219 else
1220 $dest = 'F';
1221 }
1222 switch($dest)
1223 {
1224 case 'I':
1225 // Send to standard output
1226 $this->_checkoutput();
1227 if(PHP_SAPI!='cli')
1228 {
1229 // We send to a browser
1230 header('Content-Type: application/pdf');
1231 if (is_string($utfName) && !empty($utfName))
1232 {
1233 $utfName = CHTTP::urnEncode($utfName, 'UTF-8');
1234 header("Content-Disposition: attachment; filename=\"".$name."\"; filename*=utf-8''".$utfName);
1235 }
1236 else
1237 {
1238 header('Content-Disposition: inline; filename="'.$name.'"');
1239 }
1240 header('Cache-Control: private, max-age=0, must-revalidate');
1241 header('Pragma: public');
1242 }
1243 echo $this->buffer;
1244 break;
1245 case 'D':
1246 // Download file
1247 $this->_checkoutput();
1248 header('Content-Type: application/octet-stream');
1249 if (is_string($utfName) && !empty($utfName))
1250 {
1251 $utfName = CHTTP::urnEncode($utfName, 'UTF-8');
1252 header("Content-Disposition: attachment; filename=\"".$name."\"; filename*=utf-8''".$utfName);
1253 }
1254 else
1255 {
1256 header('Content-Disposition: inline; filename="'.$name.'"');
1257 }
1258 header('Cache-Control: private, max-age=0, must-revalidate');
1259 header('Pragma: public');
1260 echo $this->buffer;
1261 break;
1262 case 'F':
1263 // Save to local file
1264 $f = fopen($name,'wb');
1265 if(!$f)
1266 $this->Error('Unable to create output file: '.$name);
1267 fwrite($f,$this->buffer,mb_strlen($this->buffer,'8bit'));
1268 fclose($f);
1269 break;
1270 case 'S':
1271 // Return as a string
1272 return $this->buffer;
1273 default:
1274 $this->Error('Incorrect output destination: '.$dest);
1275 }
1276 return '';
1277}
1278
1279/*******************************************************************************
1280* *
1281* Protected methods *
1282* *
1283*******************************************************************************/
1284function _dochecks()
1285{
1286 // Check availability of %F
1287 if(sprintf('%.1F',1.0)!='1.0')
1288 $this->Error('This version of PHP is not supported');
1289 // Check availability of mbstring
1290 if(!function_exists('mb_strlen'))
1291 $this->Error('mbstring extension is not available');
1292 // Check mbstring overloading
1293 //if(ini_get('mbstring.func_overload') & 2)
1294 // $this->Error('mbstring overloading must be disabled');
1295 // Ensure runtime magic quotes are disabled
1296 //if(get_magic_quotes_runtime())
1297 // @set_magic_quotes_runtime(0);
1298}
1299
1300function _getfontpath()
1301{
1302 return $this->fontpath;
1303}
1304
1305function _checkoutput()
1306{
1307 if(PHP_SAPI!='cli')
1308 {
1309 if(headers_sent($file,$line))
1310 $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)");
1311 }
1312 if(ob_get_length())
1313 {
1314 // The output buffer is not empty
1315 if(preg_match('/^(\xEF\xBB\xBF)?\s*$/',ob_get_contents()))
1316 {
1317 // It contains only a UTF-8 BOM and/or whitespace, let's clean it
1318 ob_clean();
1319 }
1320 else
1321 $this->Error("Some data has already been output, can't send PDF file");
1322 }
1323}
1324
1325function _getpagesize($size)
1326{
1327 if(is_string($size))
1328 {
1329 $size = mb_strtolower($size);
1330 if(!isset($this->StdPageSizes[$size]))
1331 $this->Error('Unknown page size: '.$size);
1332 $a = $this->StdPageSizes[$size];
1333 return array($a[0]/$this->k, $a[1]/$this->k);
1334 }
1335 else
1336 {
1337 if($size[0]>$size[1])
1338 return array($size[1], $size[0]);
1339 else
1340 return $size;
1341 }
1342}
1343
1344function _beginpage($orientation, $size)
1345{
1346 $this->page++;
1347 $this->pages[$this->page] = '';
1348 $this->state = 2;
1349 $this->x = $this->lMargin;
1350 $this->y = $this->tMargin;
1351 $this->FontFamily = '';
1352 // Check page size and orientation
1353 if($orientation=='')
1354 $orientation = $this->DefOrientation;
1355 else
1356 $orientation = mb_strtoupper($orientation[0]);
1357 if($size=='')
1358 $size = $this->DefPageSize;
1359 else
1360 $size = $this->_getpagesize($size);
1361 if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1])
1362 {
1363 // New size or orientation
1364 if($orientation=='P')
1365 {
1366 $this->w = $size[0];
1367 $this->h = $size[1];
1368 }
1369 else
1370 {
1371 $this->w = $size[1];
1372 $this->h = $size[0];
1373 }
1374 $this->wPt = $this->w*$this->k;
1375 $this->hPt = $this->h*$this->k;
1376 $this->PageBreakTrigger = $this->h-$this->bMargin;
1377 $this->CurOrientation = $orientation;
1378 $this->CurPageSize = $size;
1379 }
1380 if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1])
1381 $this->PageSizes[$this->page] = array($this->wPt, $this->hPt);
1382}
1383
1384function _endpage()
1385{
1386 $this->state = 1;
1387}
1388
1389function _loadfont($font)
1390{
1391 // Load a font definition file from the font directory
1392 include($this->fontpath.$font);
1393 $a = get_defined_vars();
1394 if(!isset($a['name']))
1395 $this->Error('Could not include font definition file');
1396 return $a;
1397}
1398
1399function _escape($s)
1400{
1401 // Escape special characters in strings
1402 $s = str_replace('\\','\\\\',$s);
1403 $s = str_replace('(','\\(',$s);
1404 $s = str_replace(')','\\)',$s);
1405 $s = str_replace("\r",'\\r',$s);
1406 return $s;
1407}
1408
1409function _textstring($s)
1410{
1411 // Format a text string
1412 return '('.$this->_escape($s).')';
1413}
1414
1415function _UTF8toUTF16($s)
1416{
1417 // Convert UTF-8 to UTF-16BE with BOM
1418 $res = "\xFE\xFF";
1419 $nb = mb_strlen($s,'8bit');
1420 $i = 0;
1421 while($i<$nb)
1422 {
1423 $c1 = ord($s[$i++]);
1424 if($c1>=224)
1425 {
1426 // 3-byte character
1427 $c2 = ord($s[$i++]);
1428 $c3 = ord($s[$i++]);
1429 $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2));
1430 $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F));
1431 }
1432 elseif($c1>=192)
1433 {
1434 // 2-byte character
1435 $c2 = ord($s[$i++]);
1436 $res .= chr(($c1 & 0x1C)>>2);
1437 $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));
1438 }
1439 else
1440 {
1441 // Single-byte character
1442 $res .= "\0".chr($c1);
1443 }
1444 }
1445 return $res;
1446}
1447
1448function _dounderline($x, $y, $txt)
1449{
1450 // Underline text
1451 $up = $this->CurrentFont['up'];
1452 $ut = $this->CurrentFont['ut'];
1453 $w = $this->GetStringWidth($txt)+$this->ws*mb_substr_count($txt,' ','8bit');
1454 return sprintf('%.2F %.2F %.2F %.2F re f',$x*$this->k,($this->h-($y-$up/1000*$this->FontSize))*$this->k,$w*$this->k,-$ut/1000*$this->FontSizePt);
1455}
1456
1457function _parsejpg($file)
1458{
1459 // Extract info from a JPEG file
1460 $a = getimagesize($file);
1461 if(!$a)
1462 $this->Error('Missing or incorrect image file: '.$file);
1463 if($a[2]!=2)
1464 $this->Error('Not a JPEG file: '.$file);
1465 if(!isset($a['channels']) || $a['channels']==3)
1466 $colspace = 'DeviceRGB';
1467 elseif($a['channels']==4)
1468 $colspace = 'DeviceCMYK';
1469 else
1470 $colspace = 'DeviceGray';
1471 $bpc = isset($a['bits']) ? $a['bits'] : 8;
1472 $data = file_get_contents($file);
1473 return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
1474}
1475
1476function _parsepng($file)
1477{
1478 // Extract info from a PNG file
1479 $f = fopen($file,'rb');
1480 if(!$f)
1481 $this->Error('Can\'t open image file: '.$file);
1482 $info = $this->_parsepngstream($f,$file);
1483 fclose($f);
1484 return $info;
1485}
1486
1487function _parsepngstream($f, $file)
1488{
1489 // Check signature
1490 if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1491 $this->Error('Not a PNG file: '.$file);
1492
1493 // Read header chunk
1494 $this->_readstream($f,4);
1495 if($this->_readstream($f,4)!='IHDR')
1496 $this->Error('Incorrect PNG file: '.$file);
1497 $w = $this->_readint($f);
1498 $h = $this->_readint($f);
1499 $bpc = ord($this->_readstream($f,1));
1500 if($bpc>8)
1501 $this->Error('16-bit depth not supported: '.$file);
1502 $ct = ord($this->_readstream($f,1));
1503 if($ct==0 || $ct==4)
1504 $colspace = 'DeviceGray';
1505 elseif($ct==2 || $ct==6)
1506 $colspace = 'DeviceRGB';
1507 elseif($ct==3)
1508 $colspace = 'Indexed';
1509 else
1510 $this->Error('Unknown color type: '.$file);
1511 if(ord($this->_readstream($f,1))!=0)
1512 $this->Error('Unknown compression method: '.$file);
1513 if(ord($this->_readstream($f,1))!=0)
1514 $this->Error('Unknown filter method: '.$file);
1515 if(ord($this->_readstream($f,1))!=0)
1516 $this->Error('Interlacing not supported: '.$file);
1517 $this->_readstream($f,4);
1518 $dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w;
1519
1520 // Scan chunks looking for palette, transparency and image data
1521 $pal = '';
1522 $trns = '';
1523 $data = '';
1524 do
1525 {
1526 $n = $this->_readint($f);
1527 $type = $this->_readstream($f,4);
1528 if($type=='PLTE')
1529 {
1530 // Read palette
1531 $pal = $this->_readstream($f,$n);
1532 $this->_readstream($f,4);
1533 }
1534 elseif($type=='tRNS')
1535 {
1536 // Read transparency info
1537 $t = $this->_readstream($f,$n);
1538 if($ct==0)
1539 $trns = array(ord(mb_substr($t,1,1,'8bit')));
1540 elseif($ct==2)
1541 $trns = array(ord(mb_substr($t,1,1,'8bit')), ord(mb_substr($t,3,1,'8bit')), ord(mb_substr($t,5,1,'8bit')));
1542 else
1543 {
1544 $pos = mb_strpos($t,chr(0),0,'8bit');
1545 if($pos!==false)
1546 $trns = array($pos);
1547 }
1548 $this->_readstream($f,4);
1549 }
1550 elseif($type=='IDAT')
1551 {
1552 // Read image data block
1553 $data .= $this->_readstream($f,$n);
1554 $this->_readstream($f,4);
1555 }
1556 elseif($type=='IEND')
1557 break;
1558 else
1559 $this->_readstream($f,$n+4);
1560 }
1561 while($n);
1562
1563 if($colspace=='Indexed' && empty($pal))
1564 $this->Error('Missing palette in '.$file);
1565 $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns);
1566 if($ct>=4)
1567 {
1568 // Extract alpha channel
1569 if(!function_exists('gzuncompress'))
1570 $this->Error('Zlib not available, can\'t handle alpha channel: '.$file);
1571 $data = gzuncompress($data);
1572 $color = '';
1573 $alpha = '';
1574 if($ct==4)
1575 {
1576 // Gray image
1577 $len = 2*$w;
1578 for($i=0;$i<$h;$i++)
1579 {
1580 $pos = (1+$len)*$i;
1581 $color .= $data[$pos];
1582 $alpha .= $data[$pos];
1583 $line = mb_substr($data,$pos+1,$len,'8bit');
1584 $color .= preg_replace('/(.)./s','$1',$line);
1585 $alpha .= preg_replace('/.(.)/s','$1',$line);
1586 }
1587 }
1588 else
1589 {
1590 // RGB image
1591 $len = 4*$w;
1592 for($i=0;$i<$h;$i++)
1593 {
1594 $pos = (1+$len)*$i;
1595 $color .= $data[$pos];
1596 $alpha .= $data[$pos];
1597 $line = mb_substr($data,$pos+1,$len,'8bit');
1598 $color .= preg_replace('/(.{3})./s','$1',$line);
1599 $alpha .= preg_replace('/.{3}(.)/s','$1',$line);
1600 }
1601 }
1602 unset($data);
1603 $data = gzcompress($color);
1604 $info['smask'] = gzcompress($alpha);
1605 if($this->PDFVersion<'1.4')
1606 $this->PDFVersion = '1.4';
1607 }
1608 $info['data'] = $data;
1609 return $info;
1610}
1611
1612function _readstream($f, $n)
1613{
1614 // Read n bytes from stream
1615 $res = '';
1616 while($n>0 && !feof($f))
1617 {
1618 $s = fread($f,$n);
1619 if($s===false)
1620 $this->Error('Error while reading stream');
1621 $n -= mb_strlen($s,'8bit');
1622 $res .= $s;
1623 }
1624 if($n>0)
1625 $this->Error('Unexpected end of stream');
1626 return $res;
1627}
1628
1629function _readint($f)
1630{
1631 // Read a 4-byte integer from stream
1632 $a = unpack('Ni',$this->_readstream($f,4));
1633 return $a['i'];
1634}
1635
1636function _parsegif($file)
1637{
1638 // Extract info from a GIF file (via PNG conversion)
1639 if(!function_exists('imagepng'))
1640 $this->Error('GD extension is required for GIF support');
1641 if(!function_exists('imagecreatefromgif'))
1642 $this->Error('GD has no GIF read support');
1643 $im = imagecreatefromgif($file);
1644 if(!$im)
1645 $this->Error('Missing or incorrect image file: '.$file);
1646 imageinterlace($im,0);
1647 $f = @fopen('php://temp','rb+');
1648 if($f)
1649 {
1650 // Perform conversion in memory
1651 ob_start();
1652 imagepng($im);
1653 $data = ob_get_clean();
1654 imagedestroy($im);
1655 fwrite($f,$data);
1656 rewind($f);
1657 $info = $this->_parsepngstream($f,$file);
1658 fclose($f);
1659 }
1660 else
1661 {
1662 // Use temporary file
1663 $tmp = tempnam('.','gif');
1664 if(!$tmp)
1665 $this->Error('Unable to create a temporary file');
1666 if(!imagepng($im,$tmp))
1667 $this->Error('Error while saving to temporary file');
1668 imagedestroy($im);
1669 $info = $this->_parsepng($tmp);
1670 unlink($tmp);
1671 }
1672 return $info;
1673}
1674
1675function _newobj()
1676{
1677 // Begin a new object
1678 $this->n++;
1679 $this->offsets[$this->n] = mb_strlen($this->buffer,'8bit');
1680 $this->_out($this->n.' 0 obj');
1681}
1682
1683function _putstream($s)
1684{
1685 $this->_out('stream');
1686 $this->_out($s);
1687 $this->_out('endstream');
1688}
1689
1690function _out($s)
1691{
1692 // Add a line to the document
1693 if($this->state==2)
1694 $this->pages[$this->page] .= $s."\n";
1695 else
1696 $this->buffer .= $s."\n";
1697}
1698
1699function _putpages()
1700{
1701 $nb = $this->page;
1702 if(!empty($this->AliasNbPages))
1703 {
1704 // Replace number of pages in fonts using subsets
1705 $alias = $this->UTF8ToUTF16BE($this->AliasNbPages, false);
1706 $r = $this->UTF8ToUTF16BE("$nb", false);
1707 for($n=1;$n<=$nb;$n++)
1708 $this->pages[$n] = str_replace($alias,$r,$this->pages[$n]);
1709 // Now repeat for no pages in non-subset fonts
1710 for($n=1;$n<=$nb;$n++)
1711 $this->pages[$n] = str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
1712 }
1713 if($this->DefOrientation=='P')
1714 {
1715 $wPt = $this->DefPageSize[0]*$this->k;
1716 $hPt = $this->DefPageSize[1]*$this->k;
1717 }
1718 else
1719 {
1720 $wPt = $this->DefPageSize[1]*$this->k;
1721 $hPt = $this->DefPageSize[0]*$this->k;
1722 }
1723 $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
1724 for($n=1;$n<=$nb;$n++)
1725 {
1726 // Page
1727 $this->_newobj();
1728 $this->_out('<</Type /Page');
1729 $this->_out('/Parent 1 0 R');
1730 if(isset($this->PageSizes[$n]))
1731 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1]));
1732 $this->_out('/Resources 2 0 R');
1733 if(isset($this->PageLinks[$n]))
1734 {
1735 // Links
1736 $annots = '/Annots [';
1737 foreach($this->PageLinks[$n] as $pl)
1738 {
1739 $rect = sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
1740 $annots .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1741 if(is_string($pl[4]))
1742 $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1743 else
1744 {
1745 $l = $this->links[$pl[4]];
1746 $h = isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt;
1747 $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k);
1748 }
1749 }
1750 $this->_out($annots.']');
1751 }
1752 if($this->PDFVersion>'1.3')
1753 $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>');
1754 $this->_out('/Contents '.($this->n+1).' 0 R>>');
1755 $this->_out('endobj');
1756 // Page content
1757 $p = ($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
1758 $this->_newobj();
1759 $this->_out('<<'.$filter.'/Length '.mb_strlen($p,'8bit').'>>');
1760 $this->_putstream($p);
1761 $this->_out('endobj');
1762 }
1763 // Pages root
1764 $this->offsets[1] = mb_strlen($this->buffer,'8bit');
1765 $this->_out('1 0 obj');
1766 $this->_out('<</Type /Pages');
1767 $kids = '/Kids [';
1768 for($i=0;$i<$nb;$i++)
1769 $kids .= (3+2*$i).' 0 R ';
1770 $this->_out($kids.']');
1771 $this->_out('/Count '.$nb);
1772 $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt));
1773 $this->_out('>>');
1774 $this->_out('endobj');
1775}
1776
1777function _putfonts()
1778{
1779 $nf=$this->n;
1780 foreach($this->diffs as $diff)
1781 {
1782 // Encodings
1783 $this->_newobj();
1784 $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
1785 $this->_out('endobj');
1786 }
1787 foreach($this->FontFiles as $file=>$info)
1788 {
1789 if (!isset($info['type']) || $info['type']!='TTF') {
1790 // Font file embedding
1791 $this->_newobj();
1792 $this->FontFiles[$file]['n']=$this->n;
1793 $font='';
1794 $f=fopen($this->_getfontpath().$file,'rb',1);
1795 if(!$f)
1796 $this->Error('Font file not found');
1797 while(!feof($f))
1798 $font.=fread($f,8192);
1799 fclose($f);
1800 $compressed=(mb_substr($file,-2,mb_strlen($file,'8bit'),'8bit')=='.z');
1801 if(!$compressed && isset($info['length2']))
1802 {
1803 $header=(ord($font[0])==128);
1804 if($header)
1805 {
1806 // Strip first binary header
1807 $font=mb_substr($font,6,mb_strlen($font,'8bit'),'8bit');
1808 }
1809 if($header && ord($font[$info['length1']])==128)
1810 {
1811 // Strip second binary header
1812 $font=mb_substr($font,0,$info['length1'],'8bit').mb_substr($font,$info['length1']+6,mb_strlen($font,'8bit'),'8bit');
1813 }
1814 }
1815 $this->_out('<</Length '.mb_strlen($font,'8bit'));
1816 if($compressed)
1817 $this->_out('/Filter /FlateDecode');
1818 $this->_out('/Length1 '.$info['length1']);
1819 if(isset($info['length2']))
1820 $this->_out('/Length2 '.$info['length2'].' /Length3 0');
1821 $this->_out('>>');
1822 $this->_putstream($font);
1823 $this->_out('endobj');
1824 }
1825 }
1826 foreach($this->fonts as $k=>$font)
1827 {
1828 // Font objects
1829 //$this->fonts[$k]['n']=$this->n+1;
1830 $type = $font['type'];
1831 $name = $font['name'];
1832 if($type=='Core')
1833 {
1834 // Standard font
1835 $this->fonts[$k]['n']=$this->n+1;
1836 $this->_newobj();
1837 $this->_out('<</Type /Font');
1838 $this->_out('/BaseFont /'.$name);
1839 $this->_out('/Subtype /Type1');
1840 if($name!='Symbol' && $name!='ZapfDingbats')
1841 $this->_out('/Encoding /WinAnsiEncoding');
1842 $this->_out('>>');
1843 $this->_out('endobj');
1844 }
1845 elseif($type=='Type1' || $type=='TrueType')
1846 {
1847 // Additional Type1 or TrueType font
1848 $this->fonts[$k]['n']=$this->n+1;
1849 $this->_newobj();
1850 $this->_out('<</Type /Font');
1851 $this->_out('/BaseFont /'.$name);
1852 $this->_out('/Subtype /'.$type);
1853 $this->_out('/FirstChar 32 /LastChar 255');
1854 $this->_out('/Widths '.($this->n+1).' 0 R');
1855 $this->_out('/FontDescriptor '.($this->n+2).' 0 R');
1856 if($font['enc'])
1857 {
1858 if(isset($font['diff']))
1859 $this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
1860 else
1861 $this->_out('/Encoding /WinAnsiEncoding');
1862 }
1863 $this->_out('>>');
1864 $this->_out('endobj');
1865 // Widths
1866 $this->_newobj();
1867 $cw=&$font['cw'];
1868 $s='[';
1869 for($i=32;$i<=255;$i++)
1870 $s.=$cw[chr($i)].' ';
1871 $this->_out($s.']');
1872 $this->_out('endobj');
1873 // Descriptor
1874 $this->_newobj();
1875 $s='<</Type /FontDescriptor /FontName /'.$name;
1876 foreach($font['desc'] as $k=>$v)
1877 $s.=' /'.$k.' '.$v;
1878 $file=$font['file'];
1879 if($file)
1880 $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
1881 $this->_out($s.'>>');
1882 $this->_out('endobj');
1883 }
1884 // TrueType embedded SUBSETS or FULL
1885 else if ($type=='TTF') {
1886 $this->fonts[$k]['n']=$this->n+1;
1887 require_once(__DIR__.'/ttfonts.php');
1888 $ttf = new TTFontFile();
1889 $fontname = 'MPDFAA'.'+'.$font['name'];
1890 $subset = $font['subset'];
1891 unset($subset[0]);
1892 $ttfontstream = $ttf->makeSubset($font['ttffile'], $subset);
1893 $ttfontsize = mb_strlen($ttfontstream,'8bit');
1894 $fontstream = gzcompress($ttfontstream);
1895 $codeToGlyph = $ttf->codeToGlyph;
1896 unset($codeToGlyph[0]);
1897
1898 // Type0 Font
1899 // A composite font - a font composed of other fonts, organized hierarchically
1900 $this->_newobj();
1901 $this->_out('<</Type /Font');
1902 $this->_out('/Subtype /Type0');
1903 $this->_out('/BaseFont /'.$fontname.'');
1904 $this->_out('/Encoding /Identity-H');
1905 $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
1906 $this->_out('/ToUnicode '.($this->n + 2).' 0 R');
1907 $this->_out('>>');
1908 $this->_out('endobj');
1909
1910 // CIDFontType2
1911 // A CIDFont whose glyph descriptions are based on TrueType font technology
1912 $this->_newobj();
1913 $this->_out('<</Type /Font');
1914 $this->_out('/Subtype /CIDFontType2');
1915 $this->_out('/BaseFont /'.$fontname.'');
1916 $this->_out('/CIDSystemInfo '.($this->n + 2).' 0 R');
1917 $this->_out('/FontDescriptor '.($this->n + 3).' 0 R');
1918 if (isset($font['desc']['MissingWidth'])){
1919 $this->_out('/DW '.$font['desc']['MissingWidth'].'');
1920 }
1921
1922 $this->_putTTfontwidths($font, $ttf->maxUni);
1923
1924 $this->_out('/CIDToGIDMap '.($this->n + 4).' 0 R');
1925 $this->_out('>>');
1926 $this->_out('endobj');
1927
1928 // ToUnicode
1929 $this->_newobj();
1930 $toUni = "/CIDInit /ProcSet findresource begin\n";
1931 $toUni .= "12 dict begin\n";
1932 $toUni .= "begincmap\n";
1933 $toUni .= "/CIDSystemInfo\n";
1934 $toUni .= "<</Registry (Adobe)\n";
1935 $toUni .= "/Ordering (UCS)\n";
1936 $toUni .= "/Supplement 0\n";
1937 $toUni .= ">> def\n";
1938 $toUni .= "/CMapName /Adobe-Identity-UCS def\n";
1939 $toUni .= "/CMapType 2 def\n";
1940 $toUni .= "1 begincodespacerange\n";
1941 $toUni .= "<0000> <FFFF>\n";
1942 $toUni .= "endcodespacerange\n";
1943 $toUni .= "1 beginbfrange\n";
1944 $toUni .= "<0000> <FFFF> <0000>\n";
1945 $toUni .= "endbfrange\n";
1946 $toUni .= "endcmap\n";
1947 $toUni .= "CMapName currentdict /CMap defineresource pop\n";
1948 $toUni .= "end\n";
1949 $toUni .= "end";
1950 $this->_out('<</Length '.(mb_strlen($toUni,'8bit')).'>>');
1951 $this->_putstream($toUni);
1952 $this->_out('endobj');
1953
1954 // CIDSystemInfo dictionary
1955 $this->_newobj();
1956 $this->_out('<</Registry (Adobe)');
1957 $this->_out('/Ordering (UCS)');
1958 $this->_out('/Supplement 0');
1959 $this->_out('>>');
1960 $this->_out('endobj');
1961
1962 // Font descriptor
1963 $this->_newobj();
1964 $this->_out('<</Type /FontDescriptor');
1965 $this->_out('/FontName /'.$fontname);
1966 foreach($font['desc'] as $kd=>$v) {
1967 if ($kd == 'Flags') { $v = $v | 4; $v = $v & ~32; } // SYMBOLIC font flag
1968 $this->_out(' /'.$kd.' '.$v);
1969 }
1970 $this->_out('/FontFile2 '.($this->n + 2).' 0 R');
1971 $this->_out('>>');
1972 $this->_out('endobj');
1973
1974 // Embed CIDToGIDMap
1975 // A specification of the mapping from CIDs to glyph indices
1976 $cidtogidmap = '';
1977 $cidtogidmap = str_pad('', 256*256*2, "\x00");
1978 foreach($codeToGlyph as $cc=>$glyph) {
1979 $cidtogidmap[$cc*2] = chr($glyph >> 8);
1980 $cidtogidmap[$cc*2 + 1] = chr($glyph & 0xFF);
1981 }
1982 $cidtogidmap = gzcompress($cidtogidmap);
1983 $this->_newobj();
1984 $this->_out('<</Length '.mb_strlen($cidtogidmap,'8bit').'');
1985 $this->_out('/Filter /FlateDecode');
1986 $this->_out('>>');
1987 $this->_putstream($cidtogidmap);
1988 $this->_out('endobj');
1989
1990 //Font file
1991 $this->_newobj();
1992 $this->_out('<</Length '.mb_strlen($fontstream,'8bit'));
1993 $this->_out('/Filter /FlateDecode');
1994 $this->_out('/Length1 '.$ttfontsize);
1995 $this->_out('>>');
1996 $this->_putstream($fontstream);
1997 $this->_out('endobj');
1998 unset($ttf);
1999 }
2000 else
2001 {
2002 // Allow for additional types
2003 $this->fonts[$k]['n'] = $this->n+1;
2004 $mtd='_put'.mb_strtolower($type);
2005 if(!method_exists($this,$mtd))
2006 $this->Error('Unsupported font type: '.$type);
2007 $this->$mtd($font);
2008 }
2009 }
2010}
2011
2012function _putTTfontwidths(&$font, $maxUni) {
2013 if (file_exists($font['unifilename'].'.cw127.php')) {
2014 include($font['unifilename'].'.cw127.php') ;
2015 $startcid = 128;
2016 }
2017 else {
2018 $rangeid = 0;
2019 $range = array();
2020 $prevcid = -2;
2021 $prevwidth = -1;
2022 $interval = false;
2023 $startcid = 1;
2024 }
2025 $cwlen = $maxUni + 1;
2026
2027 // for each character
2028 for ($cid=$startcid; $cid<$cwlen; $cid++) {
2029 if ($cid==128 && (!file_exists($font['unifilename'].'.cw127.php'))) {
2030 if (is_writable(dirname($this->_getfontpath().'/x'))) {
2031 $fh = fopen($font['unifilename'].'.cw127.php',"wb");
2032 $cw127='<?php'."\n";
2033 $cw127.='$rangeid='.$rangeid.";\n";
2034 $cw127.='$prevcid='.$prevcid.";\n";
2035 $cw127.='$prevwidth='.$prevwidth.";\n";
2036 if ($interval) { $cw127.='$interval=true'.";\n"; }
2037 else { $cw127.='$interval=false'.";\n"; }
2038 $cw127.='$range='.var_export($range,true).";\n";
2039 $cw127.="?>";
2040 fwrite($fh,$cw127,mb_strlen($cw127,'8bit'));
2041 fclose($fh);
2042 }
2043 }
2044 if ($font['cw'][$cid*2] == "\00" && $font['cw'][$cid*2+1] == "\00") { continue; }
2045 $width = (ord($font['cw'][$cid*2]) << 8) + ord($font['cw'][$cid*2+1]);
2046 if ($width == 65535) { $width = 0; }
2047 if ($cid > 255 && (!isset($font['subset'][$cid]) || !$font['subset'][$cid])) { continue; }
2048 if (!isset($font['dw']) || (isset($font['dw']) && $width != $font['dw'])) {
2049 if ($cid == ($prevcid + 1)) {
2050 if ($width == $prevwidth) {
2051 if ($width == $range[$rangeid][0]) {
2052 $range[$rangeid][] = $width;
2053 }
2054 else {
2055 array_pop($range[$rangeid]);
2056 // new range
2057 $rangeid = $prevcid;
2058 $range[$rangeid] = array();
2059 $range[$rangeid][] = $prevwidth;
2060 $range[$rangeid][] = $width;
2061 }
2062 $interval = true;
2063 $range[$rangeid]['interval'] = true;
2064 } else {
2065 if ($interval) {
2066 // new range
2067 $rangeid = $cid;
2068 $range[$rangeid] = array();
2069 $range[$rangeid][] = $width;
2070 }
2071 else { $range[$rangeid][] = $width; }
2072 $interval = false;
2073 }
2074 } else {
2075 $rangeid = $cid;
2076 $range[$rangeid] = array();
2077 $range[$rangeid][] = $width;
2078 $interval = false;
2079 }
2080 $prevcid = $cid;
2081 $prevwidth = $width;
2082 }
2083 }
2084 $prevk = -1;
2085 $nextk = -1;
2086 $prevint = false;
2087 foreach ($range as $k => $ws) {
2088 $cws = count($ws);
2089 if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
2090 if (isset($range[$k]['interval'])) { unset($range[$k]['interval']); }
2091 $range[$prevk] = array_merge($range[$prevk], $range[$k]);
2092 unset($range[$k]);
2093 }
2094 else { $prevk = $k; }
2095 $nextk = $k + $cws;
2096 if (isset($ws['interval'])) {
2097 if ($cws > 3) { $prevint = true; }
2098 else { $prevint = false; }
2099 unset($range[$k]['interval']);
2100 --$nextk;
2101 }
2102 else { $prevint = false; }
2103 }
2104 $w = '';
2105 foreach ($range as $k => $ws) {
2106 if (count(array_count_values($ws)) == 1) { $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0]; }
2107 else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]' . "\n"; }
2108 }
2109 $this->_out('/W ['.$w.' ]');
2110}
2111
2112function _putimages()
2113{
2114 foreach(array_keys($this->images) as $file)
2115 {
2116 $this->_putimage($this->images[$file]);
2117 unset($this->images[$file]['data']);
2118 unset($this->images[$file]['smask']);
2119 }
2120}
2121
2122function _putimage(&$info)
2123{
2124 $this->_newobj();
2125 $info['n'] = $this->n;
2126 $this->_out('<</Type /XObject');
2127 $this->_out('/Subtype /Image');
2128 $this->_out('/Width '.$info['w']);
2129 $this->_out('/Height '.$info['h']);
2130 if($info['cs']=='Indexed')
2131 $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(mb_strlen($info['pal'],'8bit')/3-1).' '.($this->n+1).' 0 R]');
2132 else
2133 {
2134 $this->_out('/ColorSpace /'.$info['cs']);
2135 if($info['cs']=='DeviceCMYK')
2136 $this->_out('/Decode [1 0 1 0 1 0 1 0]');
2137 }
2138 $this->_out('/BitsPerComponent '.$info['bpc']);
2139 if(isset($info['f']))
2140 $this->_out('/Filter /'.$info['f']);
2141 if(isset($info['dp']))
2142 $this->_out('/DecodeParms <<'.$info['dp'].'>>');
2143 if(isset($info['trns']) && is_array($info['trns']))
2144 {
2145 $trns = '';
2146 for($i=0;$i<count($info['trns']);$i++)
2147 $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
2148 $this->_out('/Mask ['.$trns.']');
2149 }
2150 if(isset($info['smask']))
2151 $this->_out('/SMask '.($this->n+1).' 0 R');
2152 $this->_out('/Length '.mb_strlen($info['data'],'8bit').'>>');
2153 $this->_putstream($info['data']);
2154 $this->_out('endobj');
2155 // Soft mask
2156 if(isset($info['smask']))
2157 {
2158 $dp = '/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns '.$info['w'];
2159 $smask = array('w'=>$info['w'], 'h'=>$info['h'], 'cs'=>'DeviceGray', 'bpc'=>8, 'f'=>$info['f'], 'dp'=>$dp, 'data'=>$info['smask']);
2160 $this->_putimage($smask);
2161 }
2162 // Palette
2163 if($info['cs']=='Indexed')
2164 {
2165 $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
2166 $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
2167 $this->_newobj();
2168 $this->_out('<<'.$filter.'/Length '.mb_strlen($pal,'8bit').'>>');
2169 $this->_putstream($pal);
2170 $this->_out('endobj');
2171 }
2172}
2173
2174function _putxobjectdict()
2175{
2176 foreach($this->images as $image)
2177 $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
2178}
2179
2180function _putresourcedict()
2181{
2182 $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
2183 $this->_out('/Font <<');
2184 foreach($this->fonts as $font) {
2185 $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
2186 }
2187 $this->_out('>>');
2188 $this->_out('/XObject <<');
2189 $this->_putxobjectdict();
2190 $this->_out('>>');
2191}
2192
2193function _putresources()
2194{
2195 $this->_putfonts();
2196 $this->_putimages();
2197 // Resource dictionary
2198 $this->offsets[2] = mb_strlen($this->buffer,'8bit');
2199 $this->_out('2 0 obj');
2200 $this->_out('<<');
2201 $this->_putresourcedict();
2202 $this->_out('>>');
2203 $this->_out('endobj');
2204}
2205
2206function _putinfo()
2207{
2208 $this->_out('/Producer '.$this->_textstring('tFPDF '.tFPDF_VERSION));
2209 if(!empty($this->title))
2210 $this->_out('/Title '.$this->_textstring($this->title));
2211 if(!empty($this->subject))
2212 $this->_out('/Subject '.$this->_textstring($this->subject));
2213 if(!empty($this->author))
2214 $this->_out('/Author '.$this->_textstring($this->author));
2215 if(!empty($this->keywords))
2216 $this->_out('/Keywords '.$this->_textstring($this->keywords));
2217 if(!empty($this->creator))
2218 $this->_out('/Creator '.$this->_textstring($this->creator));
2219 $this->_out('/CreationDate '.$this->_textstring('D:'.@date('YmdHis')));
2220}
2221
2222function _putcatalog()
2223{
2224 $this->_out('/Type /Catalog');
2225 $this->_out('/Pages 1 0 R');
2226 if($this->ZoomMode=='fullpage')
2227 $this->_out('/OpenAction [3 0 R /Fit]');
2228 elseif($this->ZoomMode=='fullwidth')
2229 $this->_out('/OpenAction [3 0 R /FitH null]');
2230 elseif($this->ZoomMode=='real')
2231 $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
2232 elseif(!is_string($this->ZoomMode))
2233 $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']');
2234 if($this->LayoutMode=='single')
2235 $this->_out('/PageLayout /SinglePage');
2236 elseif($this->LayoutMode=='continuous')
2237 $this->_out('/PageLayout /OneColumn');
2238 elseif($this->LayoutMode=='two')
2239 $this->_out('/PageLayout /TwoColumnLeft');
2240}
2241
2242function _putheader()
2243{
2244 $this->_out('%PDF-'.$this->PDFVersion);
2245}
2246
2247function _puttrailer()
2248{
2249 $this->_out('/Size '.($this->n+1));
2250 $this->_out('/Root '.$this->n.' 0 R');
2251 $this->_out('/Info '.($this->n-1).' 0 R');
2252}
2253
2254function _enddoc()
2255{
2256 $this->_putheader();
2257 $this->_putpages();
2258 $this->_putresources();
2259 // Info
2260 $this->_newobj();
2261 $this->_out('<<');
2262 $this->_putinfo();
2263 $this->_out('>>');
2264 $this->_out('endobj');
2265 // Catalog
2266 $this->_newobj();
2267 $this->_out('<<');
2268 $this->_putcatalog();
2269 $this->_out('>>');
2270 $this->_out('endobj');
2271 // Cross-ref
2272 $o = mb_strlen($this->buffer,'8bit');
2273 $this->_out('xref');
2274 $this->_out('0 '.($this->n+1));
2275 $this->_out('0000000000 65535 f ');
2276 for($i=1;$i<=$this->n;$i++)
2277 $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i]));
2278 // Trailer
2279 $this->_out('trailer');
2280 $this->_out('<<');
2281 $this->_puttrailer();
2282 $this->_out('>>');
2283 $this->_out('startxref');
2284 $this->_out($o);
2285 $this->_out('%%EOF');
2286 $this->state = 3;
2287}
2288
2289// ********* NEW FUNCTIONS *********
2290// Converts UTF-8 strings to UTF16-BE.
2291function UTF8ToUTF16BE($str, $setbom=true) {
2292 $outstr = "";
2293 if ($setbom) {
2294 $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
2295 }
2296 $outstr .= mb_convert_encoding($str, 'UTF-16BE', 'UTF-8');
2297 return $outstr;
2298}
2299
2300// Converts UTF-8 strings to codepoints array
2301function UTF8StringToArray($str) {
2302 $out = array();
2303 $len = mb_strlen($str,'8bit');
2304 for ($i = 0; $i < $len; $i++) {
2305 $uni = -1;
2306 $h = ord($str[$i]);
2307 if ( $h <= 0x7F )
2308 $uni = $h;
2309 elseif ( $h >= 0xC2 ) {
2310 if ( ($h <= 0xDF) && ($i < $len -1) )
2311 $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F);
2312 elseif ( ($h <= 0xEF) && ($i < $len -2) )
2313 $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6
2314 | (ord($str[++$i]) & 0x3F);
2315 elseif ( ($h <= 0xF4) && ($i < $len -3) )
2316 $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12
2317 | (ord($str[++$i]) & 0x3F) << 6
2318 | (ord($str[++$i]) & 0x3F);
2319 }
2320 if ($uni >= 0) {
2321 $out[] = $uni;
2322 }
2323 }
2324 return $out;
2325}
2326
2327
2328// End of class
2329}
2330
2331// Handle special IE contype request
2332if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype')
2333{
2334 header('Content-Type: application/pdf');
2335 exit;
2336}
2337
2338?>
return[Dependency::CONTAINER=> Container::class, Dependency::IBLOCK_INFO=> IblockInfo::class, Dependency::PRODUCT_CONVERTER=> ProductConverter::class, Dependency::REPOSITORY_FACADE=> Repository::class, Dependency::PRODUCT_FACTORY=> ProductFactory::class, Dependency::PRODUCT_REPOSITORY=> ProductRepository::class, ProductFactory::PRODUCT=> Product::class, Dependency::SECTION_FACTORY=> SectionFactory::class, Dependency::SECTION_REPOSITORY=> SectionRepository::class, SectionFactory::SECTION=> Section::class, SectionFactory::SECTION_COLLECTION=> SectionCollection::class, Dependency::SKU_FACTORY=> SkuFactory::class, Dependency::SKU_REPOSITORY=> SkuRepository::class, SkuFactory::SIMPLE_SKU=> SimpleSku::class, SkuFactory::SKU=> Sku::class, SkuFactory::SKU_COLLECTION=> SkuCollection::class, Dependency::PROPERTY_FACTORY=> PropertyFactory::class, Dependency::PROPERTY_REPOSITORY=> PropertyRepository::class, PropertyFactory::PROPERTY=> Property::class, PropertyFactory::PROPERTY_COLLECTION=> PropertyCollection::class, Dependency::PROPERTY_VALUE_FACTORY=> PropertyValueFactory::class, PropertyValueFactory::PROPERTY_VALUE=> PropertyValue::class, PropertyValueFactory::PROPERTY_VALUE_COLLECTION=> PropertyValueCollection::class, Dependency::PROPERTY_FEATURE_FACTORY=> PropertyFeatureFactory::class, Dependency::PROPERTY_FEATURE_REPOSITORY=> PropertyFeatureRepository::class, PropertyFeatureFactory::PROPERTY_FEATURE=> PropertyFeature::class, PropertyFeatureFactory::PROPERTY_FEATURE_COLLECTION=> PropertyFeatureCollection::class, Dependency::PRICE_FACTORY=> PriceFactory::class, Dependency::PRICE_REPOSITORY=> PriceRepository::class, PriceFactory::SIMPLE_PRICE=> SimplePrice::class, PriceFactory::QUANTITY_DEPENDENT_PRICE=> QuantityDependentPrice::class, PriceFactory::PRICE_COLLECTION=> PriceCollection::class, Dependency::IMAGE_FACTORY=> ImageFactory::class, Dependency::IMAGE_REPOSITORY=> ImageRepository::class, ImageFactory::DETAIL_IMAGE=> DetailImage::class, ImageFactory::PREVIEW_IMAGE=> PreviewImage::class, ImageFactory::MORE_PHOTO_IMAGE=> MorePhotoImage::class, ImageFactory::IMAGE_COLLECTION=> ImageCollection::class, Dependency::MEASURE_RATIO_FACTORY=> MeasureRatioFactory::class, Dependency::MEASURE_RATIO_REPOSITORY=> MeasureRatioRepository::class, MeasureRatioFactory::SIMPLE_MEASURE_RATIO=> SimpleMeasureRatio::class, MeasureRatioFactory::MEASURE_RATIO_COLLECTION=> MeasureRatioCollection::class, Dependency::BARCODE_FACTORY=> BarcodeFactory::class, Dependency::BARCODE_REPOSITORY=> BarcodeRepository::class, BarcodeFactory::BARCODE=> Barcode::class, BarcodeFactory::BARCODE_COLLECTION=> BarcodeCollection::class, Dependency::STORE_PRODUCT_FACTORY=> StoreProductFactory::class, Dependency::STORE_PRODUCT_REPOSITORY=> StoreProductRepository::class, StoreProductFactory::STORE_PRODUCT=> StoreProduct::class, StoreProductFactory::STORE_PRODUCT_COLLECTION=> StoreProductCollection::class, 'sku.tree'=> SkuTree::class, 'integration.seo.facebook.facade'=> FacebookFacade::class, 'integration.seo.facebook.product.processor'=> FacebookProductProcessor::class, 'integration.seo.facebook.product.repository'=> FacebookProductRepository::class,]
Определения .container.php:139
$type
Определения options.php:106
static urnEncode($str, $charset=false)
Определения http.php:596
$right
Определения options.php:8
$str
Определения commerceml2.php:63
while( $arBasket=$dbBasket->Fetch())
Определения commerceml.php:73
$f
Определения component_props.php:52
if(!is_array($prop["VALUES"])) $tmp
Определения component_props.php:203
$data['IS_AVAILABLE']
Определения .description.php:13
buffer
Определения ebay_mip_setup.php:303
if(!defined("ADMIN_AJAX_MODE") &&(($_REQUEST["mode"] ?? '') !='excel')) $buffer
Определения epilog_admin_after.php:40
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
background color
Определения file_new.php:745
$res
Определения filter_act.php:7
$p
Определения group_list_element_edit.php:23
$filter
Определения iblock_catalog_list.php:54
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
if(preg_match('/^ else[a-z0-9_]{2}$/i', $siteID)===1)
Определения cron_frame.php:23
if( $adminSidePanelHelper->isSidePanel())
Определения csv_new_setup.php:231
$l
Определения options.php:783
if($NS['step']==6) if( $NS[ 'step']==7) if(COption::GetOptionInt('main', 'disk_space', 0) > 0) $info
Определения backup.php:924
$name
Определения menu_edit.php:35
Layout $layout
Определения columnfields.php:113
trait Error
Определения error.php:11
if(mb_strlen($order)< 6) $desc
Определения payment.php:44
return false
Определения prolog_main_admin.php:185
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
<? endif;?> window document title
Определения prolog_main_admin.php:76
$i
Определения factura.php:643
$page
Определения order_form.php:33
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$width
Определения html.php:68
if(CSalePaySystemAction::GetParamValue('BACKGROUND', false)) $margin
Определения html.php:61
if(!empty($sellerData)) $dest
Определения pdf.php:818
const FPDF_FONTPATH
Определения pdf.php:3
else $a
Определения template.php:137
$title
Определения pdf.php:123
$y1
Определения pdf.php:133
$y2
Определения pdf.php:144
$x2
Определения pdf.php:124
do
Определения template_pdf.php:569
$k
Определения template_pdf.php:567
const tFPDF_VERSION
Определения tfpdf.php:11
$n
Определения update_log.php:107
switch( $_REQUEST[ 'bxsender'])().Authorize(<? break
Определения wrapper_popup.php:7
$x1
Определения template_pdf.php:419