PageRenderTime 102ms CodeModel.GetById 2ms app.highlight 84ms RepoModel.GetById 1ms app.codeStats 1ms

/views/shared/items/tfpdf.php

https://github.com/mjlassila/plugin-PDFOutput
PHP | 2302 lines | 2201 code | 36 blank | 65 comment | 116 complexity | 9169622ef85a9b4ec854aea329c4d99c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   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*******************************************************************************/
  79function tFPDF($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(substr($this->fontpath,-1)!='/' && substr($this->fontpath,-1)!='\\')
 112			$this->fontpath .= '/';
 113	}
 114	elseif(is_dir(dirname(__FILE__).'/font'))
 115		$this->fontpath = dirname(__FILE__).'/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 = 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	die('<b>FPDF error:</b> '.$msg);
 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 = strlen($s);
 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 = strtolower($family);
 480	$style = strtoupper($style);
 481	if($style=='IB')
 482		$style='BI';
 483	if($file=='') {
 484	   if ($uni) {
 485		$file = str_replace(' ','',$family).strtolower($style).'.ttf';
 486	   }
 487	   else {
 488		$file = str_replace(' ','',$family).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().'unifont/'.$file ; }
 498		$unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($file ,0,(strpos($file ,'.'))));
 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($this->_getfontpath().'unifont/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=\''.$ttffile."';\n";
 533			$s.='$originalsize='.$originalsize.";\n";
 534			$s.='$fontkey=\''.$fontkey."';\n";
 535			$s.="?>";
 536			if (is_writable(dirname($this->_getfontpath().'unifont/'.'x'))) {
 537				$fh = fopen($unifilename.'.mtx.php',"w");
 538				fwrite($fh,$s,strlen($s));
 539				fclose($fh);
 540				$fh = fopen($unifilename.'.cw.dat',"wb");
 541				fwrite($fh,$cw,strlen($cw));
 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 = strtolower($family);
 594	$style = strtoupper($style);
 595	if(strpos($style,'U')!==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)
 664		$page = $this->page;
 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(strpos($border,'L')!==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(strpos($border,'T')!==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(strpos($border,'R')!==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(strpos($border,'B')!==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 = strlen($s);
 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(strpos($border,'L')!==false)
 838				$b2 .= 'L';
 839			if(strpos($border,'R')!==false)
 840				$b2 .= 'R';
 841			$b = (strpos($border,'T')!==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,substr($s,$j,$i-$j),$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,substr($s,$j,$i-$j),$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,substr($s,$j,$sep-$j),$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 && strpos($border,'B')!==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,substr($s,$j,$i-$j),$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 = strlen($s);
 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,substr($s,$j,$i-$j),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,substr($s,$j,$i-$j),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,substr($s,$j,$sep-$j),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,substr($s,$j),0,0,'',0,$link);
1071		}
1072	}
1073}
1074
1075function Ln($h=null)
1076{
1077	// Line feed; default value is last cell height
1078	$this->x = $this->lMargin;
1079	if($h===null)
1080		$this->y += $this->lasth;
1081	else
1082		$this->y += $h;
1083}
1084
1085function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
1086{
1087	// Put an image on the page
1088	if(!isset($this->images[$file]))
1089	{
1090		// First use of this image, get info
1091		if($type=='')
1092		{
1093			$pos = strrpos($file,'.');
1094			if(!$pos)
1095				$this->Error('Image file has no extension and no type was specified: '.$file);
1096			$type = substr($file,$pos+1);
1097		}
1098		$type = strtolower($type);
1099		if($type=='jpeg')
1100			$type = 'jpg';
1101		$mtd = '_parse'.$type;
1102		if(!method_exists($this,$mtd))
1103			$this->Error('Unsupported image type: '.$type);
1104		$info = $this->$mtd($file);
1105		$info['i'] = count($this->images)+1;
1106		$this->images[$file] = $info;
1107	}
1108	else
1109		$info = $this->images[$file];
1110
1111	// Automatic width and height calculation if needed
1112	if($w==0 && $h==0)
1113	{
1114		// Put image at 96 dpi
1115		$w = -96;
1116		$h = -96;
1117	}
1118	if($w<0)
1119		$w = -$info['w']*72/$w/$this->k;
1120	if($h<0)
1121		$h = -$info['h']*72/$h/$this->k;
1122	if($w==0)
1123		$w = $h*$info['w']/$info['h'];
1124	if($h==0)
1125		$h = $w*$info['h']/$info['w'];
1126
1127	// Flowing mode
1128	if($y===null)
1129	{
1130		if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
1131		{
1132			// Automatic page break
1133			$x2 = $this->x;
1134			$this->AddPage($this->CurOrientation,$this->CurPageSize);
1135			$this->x = $x2;
1136		}
1137		$y = $this->y;
1138		$this->y += $h;
1139	}
1140
1141	if($x===null)
1142		$x = $this->x;
1143	$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']));
1144	if($link)
1145		$this->Link($x,$y,$w,$h,$link);
1146}
1147
1148function GetX()
1149{
1150	// Get x position
1151	return $this->x;
1152}
1153
1154function SetX($x)
1155{
1156	// Set x position
1157	if($x>=0)
1158		$this->x = $x;
1159	else
1160		$this->x = $this->w+$x;
1161}
1162
1163function GetY()
1164{
1165	// Get y position
1166	return $this->y;
1167}
1168
1169function SetY($y)
1170{
1171	// Set y position and reset x
1172	$this->x = $this->lMargin;
1173	if($y>=0)
1174		$this->y = $y;
1175	else
1176		$this->y = $this->h+$y;
1177}
1178
1179function SetXY($x, $y)
1180{
1181	// Set x and y positions
1182	$this->SetY($y);
1183	$this->SetX($x);
1184}
1185
1186function Output($name='', $dest='')
1187{
1188	// Output PDF to some destination
1189	if($this->state<3)
1190		$this->Close();
1191	$dest = strtoupper($dest);
1192	if($dest=='')
1193	{
1194		if($name=='')
1195		{
1196			$name = 'doc.pdf';
1197			$dest = 'I';
1198		}
1199		else
1200			$dest = 'F';
1201	}
1202	switch($dest)
1203	{
1204		case 'I':
1205			// Send to standard output
1206			$this->_checkoutput();
1207			if(PHP_SAPI!='cli')
1208			{
1209				// We send to a browser
1210				header('Content-Type: application/pdf');
1211				header('Content-Disposition: inline; filename="'.$name.'"');
1212				header('Cache-Control: private, max-age=0, must-revalidate');
1213				header('Pragma: public');
1214			}
1215			echo $this->buffer;
1216			break;
1217		case 'D':
1218			// Download file
1219			$this->_checkoutput();
1220			header('Content-Type: application/x-download');
1221			header('Content-Disposition: attachment; filename="'.$name.'"');
1222			header('Cache-Control: private, max-age=0, must-revalidate');
1223			header('Pragma: public');
1224			echo $this->buffer;
1225			break;
1226		case 'F':
1227			// Save to local file
1228			$f = fopen($name,'wb');
1229			if(!$f)
1230				$this->Error('Unable to create output file: '.$name);
1231			fwrite($f,$this->buffer,strlen($this->buffer));
1232			fclose($f);
1233			break;
1234		case 'S':
1235			// Return as a string
1236			return $this->buffer;
1237		default:
1238			$this->Error('Incorrect output destination: '.$dest);
1239	}
1240	return '';
1241}
1242
1243/*******************************************************************************
1244*                                                                              *
1245*                              Protected methods                               *
1246*                                                                              *
1247*******************************************************************************/
1248function _dochecks()
1249{
1250	// Check availability of %F
1251	if(sprintf('%.1F',1.0)!='1.0')
1252		$this->Error('This version of PHP is not supported');
1253	// Check availability of mbstring
1254	if(!function_exists('mb_strlen'))
1255		$this->Error('mbstring extension is not available');
1256	// Check mbstring overloading
1257	if(ini_get('mbstring.func_overload') & 2)
1258		$this->Error('mbstring overloading must be disabled');
1259	// Ensure runtime magic quotes are disabled
1260	if(get_magic_quotes_runtime())
1261		@set_magic_quotes_runtime(0);
1262}
1263
1264function _getfontpath()
1265{
1266	return $this->fontpath;
1267}
1268
1269function _checkoutput()
1270{
1271	if(PHP_SAPI!='cli')
1272	{
1273		if(headers_sent($file,$line))
1274			$this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)");
1275	}
1276	if(ob_get_length())
1277	{
1278		// The output buffer is not empty
1279		if(preg_match('/^(\xEF\xBB\xBF)?\s*$/',ob_get_contents()))
1280		{
1281			// It contains only a UTF-8 BOM and/or whitespace, let's clean it
1282			ob_clean();
1283		}
1284		else
1285			$this->Error("Some data has already been output, can't send PDF file");
1286	}
1287}
1288
1289function _getpagesize($size)
1290{
1291	if(is_string($size))
1292	{
1293		$size = strtolower($size);
1294		if(!isset($this->StdPageSizes[$size]))
1295			$this->Error('Unknown page size: '.$size);
1296		$a = $this->StdPageSizes[$size];
1297		return array($a[0]/$this->k, $a[1]/$this->k);
1298	}
1299	else
1300	{
1301		if($size[0]>$size[1])
1302			return array($size[1], $size[0]);
1303		else
1304			return $size;
1305	}
1306}
1307
1308function _beginpage($orientation, $size)
1309{
1310	$this->page++;
1311	$this->pages[$this->page] = '';
1312	$this->state = 2;
1313	$this->x = $this->lMargin;
1314	$this->y = $this->tMargin;
1315	$this->FontFamily = '';
1316	// Check page size and orientation
1317	if($orientation=='')
1318		$orientation = $this->DefOrientation;
1319	else
1320		$orientation = strtoupper($orientation[0]);
1321	if($size=='')
1322		$size = $this->DefPageSize;
1323	else
1324		$size = $this->_getpagesize($size);
1325	if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1])
1326	{
1327		// New size or orientation
1328		if($orientation=='P')
1329		{
1330			$this->w = $size[0];
1331			$this->h = $size[1];
1332		}
1333		else
1334		{
1335			$this->w = $size[1];
1336			$this->h = $size[0];
1337		}
1338		$this->wPt = $this->w*$this->k;
1339		$this->hPt = $this->h*$this->k;
1340		$this->PageBreakTrigger = $this->h-$this->bMargin;
1341		$this->CurOrientation = $orientation;
1342		$this->CurPageSize = $size;
1343	}
1344	if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1])
1345		$this->PageSizes[$this->page] = array($this->wPt, $this->hPt);
1346}
1347
1348function _endpage()
1349{
1350	$this->state = 1;
1351}
1352
1353function _loadfont($font)
1354{
1355	// Load a font definition file from the font directory
1356	include($this->fontpath.$font);
1357	$a = get_defined_vars();
1358	if(!isset($a['name']))
1359		$this->Error('Could not include font definition file');
1360	return $a;
1361}
1362
1363function _escape($s)
1364{
1365	// Escape special characters in strings
1366	$s = str_replace('\\','\\\\',$s);
1367	$s = str_replace('(','\\(',$s);
1368	$s = str_replace(')','\\)',$s);
1369	$s = str_replace("\r",'\\r',$s);
1370	return $s;
1371}
1372
1373function _textstring($s)
1374{
1375	// Format a text string
1376	return '('.$this->_escape($s).')';
1377}
1378
1379function _UTF8toUTF16($s)
1380{
1381	// Convert UTF-8 to UTF-16BE with BOM
1382	$res = "\xFE\xFF";
1383	$nb = strlen($s);
1384	$i = 0;
1385	while($i<$nb)
1386	{
1387		$c1 = ord($s[$i++]);
1388		if($c1>=224)
1389		{
1390			// 3-byte character
1391			$c2 = ord($s[$i++]);
1392			$c3 = ord($s[$i++]);
1393			$res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2));
1394			$res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F));
1395		}
1396		elseif($c1>=192)
1397		{
1398			// 2-byte character
1399			$c2 = ord($s[$i++]);
1400			$res .= chr(($c1 & 0x1C)>>2);
1401			$res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));
1402		}
1403		else
1404		{
1405			// Single-byte character
1406			$res .= "\0".chr($c1);
1407		}
1408	}
1409	return $res;
1410}
1411
1412function _dounderline($x, $y, $txt)
1413{
1414	// Underline text
1415	$up = $this->CurrentFont['up'];
1416	$ut = $this->CurrentFont['ut'];
1417	$w = $this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
1418	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);
1419}
1420
1421function _parsejpg($file)
1422{
1423	// Extract info from a JPEG file
1424	$a = getimagesize($file);
1425	if(!$a)
1426		$this->Error('Missing or incorrect image file: '.$file);
1427	if($a[2]!=2)
1428		$this->Error('Not a JPEG file: '.$file);
1429	if(!isset($a['channels']) || $a['channels']==3)
1430		$colspace = 'DeviceRGB';
1431	elseif($a['channels']==4)
1432		$colspace = 'DeviceCMYK';
1433	else
1434		$colspace = 'DeviceGray';
1435	$bpc = isset($a['bits']) ? $a['bits'] : 8;
1436	$data = file_get_contents($file);
1437	return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
1438}
1439
1440function _parsepng($file)
1441{
1442	// Extract info from a PNG file
1443	$f = fopen($file,'rb');
1444	if(!$f)
1445		$this->Error('Can\'t open image file: '.$file);
1446	$info = $this->_parsepngstream($f,$file);
1447	fclose($f);
1448	return $info;
1449}
1450
1451function _parsepngstream($f, $file)
1452{
1453	// Check signature
1454	if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1455		$this->Error('Not a PNG file: '.$file);
1456
1457	// Read header chunk
1458	$this->_readstream($f,4);
1459	if($this->_readstream($f,4)!='IHDR')
1460		$this->Error('Incorrect PNG file: '.$file);
1461	$w = $this->_readint($f);
1462	$h = $this->_readint($f);
1463	$bpc = ord($this->_readstream($f,1));
1464	if($bpc>8)
1465		$this->Error('16-bit depth not supported: '.$file);
1466	$ct = ord($this->_readstream($f,1));
1467	if($ct==0 || $ct==4)
1468		$colspace = 'DeviceGray';
1469	elseif($ct==2 || $ct==6)
1470		$colspace = 'DeviceRGB';
1471	elseif($ct==3)
1472		$colspace = 'Indexed';
1473	else
1474		$this->Error('Unknown color type: '.$file);
1475	if(ord($this->_readstream($f,1))!=0)
1476		$this->Error('Unknown compression method: '.$file);
1477	if(ord($this->_readstream($f,1))!=0)
1478		$this->Error('Unknown filter method: '.$file);
1479	if(ord($this->_readstream($f,1))!=0)
1480		$this->Error('Interlacing not supported: '.$file);
1481	$this->_readstream($f,4);
1482	$dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w;
1483
1484	// Scan chunks looking for palette, transparency and image data
1485	$pal = '';
1486	$trns = '';
1487	$data = '';
1488	do
1489	{
1490		$n = $this->_readint($f);
1491		$type = $this->_readstream($f,4);
1492		if($type=='PLTE')
1493		{
1494			// Read palette
1495			$pal = $this->_readstream($f,$n);
1496			$this->_readstream($f,4);
1497		}
1498		elseif($type=='tRNS')
1499		{
1500			// Read transparency info
1501			$t = $this->_readstream($f,$n);
1502			if($ct==0)
1503				$trns = array(ord(substr($t,1,1)));
1504			elseif($ct==2)
1505				$trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));
1506			else
1507			{
1508				$pos = strpos($t,chr(0));
1509				if($pos!==false)
1510					$trns = array($pos);
1511			}
1512			$this->_readstream($f,4);
1513		}
1514		elseif($type=='IDAT')
1515		{
1516			// Read image data block
1517			$data .= $this->_readstream($f,$n);
1518			$this->_readstream($f,4);
1519		}
1520		elseif($type=='IEND')
1521			break;
1522		else
1523			$this->_readstream($f,$n+4);
1524	}
1525	while($n);
1526
1527	if($colspace=='Indexed' && empty($pal))
1528		$this->Error('Missing palette in '.$file);
1529	$info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns);
1530	if($ct>=4)
1531	{
1532		// Extract alpha channel
1533		if(!function_exists('gzuncompress'))
1534			$this->Error('Zlib not available, can\'t handle alpha channel: '.$file);
1535		$data = gzuncompress($data);
1536		$color = '';
1537		$alpha = '';
1538		if($ct==4)
1539		{
1540			// Gray image
1541			$len = 2*$w;
1542			for($i=0;$i<$h;$i++)
1543			{
1544				$pos = (1+$len)*$i;
1545				$color .= $data[$pos];
1546				$alpha .= $data[$pos];
1547				$line = substr($data,$pos+1,$len);
1548				$color .= preg_replace('/(.)./s','$1',$line);
1549				$alpha .= preg_replace('/.(.)/s','$1',$line);
1550			}
1551		}
1552		else
1553		{
1554			// RGB image
1555			$len = 4*$w;
1556			for($i=0;$i<$h;$i++)
1557			{
1558				$pos = (1+$len)*$i;
1559				$color .= $data[$pos];
1560				$alpha .= $data[$pos];
1561				$line = substr($data,$pos+1,$len);
1562				$color .= preg_replace('/(.{3})./s','$1',$line);
1563				$alpha .= preg_replace('/.{3}(.)/s','$1',$line);
1564			}
1565		}
1566		unset($data);
1567		$data = gzcompress($color);
1568		$info['smask'] = gzcompress($alpha);
1569		if($this->PDFVersion<'1.4')
1570			$this->PDFVersion = '1.4';
1571	}
1572	$info['data'] = $data;
1573	return $info;
1574}
1575
1576function _readstream($f, $n)
1577{
1578	// Read n bytes from stream
1579	$res = '';
1580	while($n>0 && !feof($f))
1581	{
1582		$s = fread($f,$n);
1583		if($s===false)
1584			$this->Error('Error while reading stream');
1585		$n -= strlen($s);
1586		$res .= $s;
1587	}
1588	if($n>0)
1589		$this->Error('Unexpected end of stream');
1590	return $res;
1591}
1592
1593function _readint($f)
1594{
1595	// Read a 4-byte integer from stream
1596	$a = unpack('Ni',$this->_readstream($f,4));
1597	return $a['i'];
1598}
1599
1600function _parsegif($file)
1601{
1602	// Extract info from a GIF file (via PNG conversion)
1603	if(!function_exists('imagepng'))
1604		$this->Error('GD extension is required for GIF support');
1605	if(!function_exists('imagecreatefromgif'))
1606		$this->Error('GD has no GIF read support');
1607	$im = imagecreatefromgif($file);
1608	if(!$im)
1609		$this->Error('Missing or incorrect image file: '.$file);
1610	imageinterlace($im,0);
1611	$f = @fopen('php://temp','rb+');
1612	if($f)
1613	{
1614		// Perform conversion in memory
1615		ob_start();
1616		imagepng($im);
1617		$data = ob_get_clean();
1618		imagedestroy($im);
1619		fwrite($f,$data);
1620		rewind($f);
1621		$info = $this->_parsepngstream($f,$file);
1622		fclose($f);
1623	}
1624	else
1625	{
1626		// Use temporary file
1627		$tmp = tempnam('.','gif');
1628		if(!$tmp)
1629			$this->Error('Unable to create a temporary file');
1630		if(!imagepng($im,$tmp))
1631			$this->Error('Error while saving to temporary file');
1632		imagedestroy($im);
1633		$info = $this->_parsepng($tmp);
1634		unlink($tmp);
1635	}
1636	return $info;
1637}
1638
1639function _newobj()
1640{
1641	// Begin a new object
1642	$this->n++;
1643	$this->offsets[$this->n] = strlen($this->buffer);
1644	$this->_out($this->n.' 0 obj');
1645}
1646
1647function _putstream($s)
1648{
1649	$this->_out('stream');
1650	$this->_out($s);
1651	$this->_out('endstream');
1652}
1653
1654function _out($s)
1655{
1656	// Add a line to the document
1657	if($this->state==2)
1658		$this->pages[$this->page] .= $s."\n";
1659	else
1660		$this->buffer .= $s."\n";
1661}
1662
1663function _putpages()
1664{
1665	$nb = $this->page;
1666	if(!empty($this->AliasNbPages))
1667	{
1668		// Replace number of pages in fonts using subsets
1669		$alias = $this->UTF8ToUTF16BE($this->AliasNbPages, false);
1670		$r = $this->UTF8ToUTF16BE("$nb", false);
1671		for($n=1;$n<=$nb;$n++)
1672			$this->pages[$n] = str_replace($alias,$r,$this->pages[$n]);
1673		// Now repeat for no pages in non-subset fonts
1674		for($n=1;$n<=$nb;$n++)
1675			$this->pages[$n] = str_replace($this->AliasNbPages,$nb,$this->pages[$n]);
1676	}
1677	if($this->DefOrientation=='P')
1678	{
1679		$wPt = $this->DefPageSize[0]*$this->k;
1680		$hPt = $this->DefPageSize[1]*$this->k;
1681	}
1682	else
1683	{
1684		$wPt = $this->DefPageSize[1]*$this->k;
1685		$hPt = $this->DefPageSize[0]*$this->k;
1686	}
1687	$filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
1688	for($n=1;$n<=$nb;$n++)
1689	{
1690		// Page
1691		$this->_newobj();
1692		$this->_out('<</Type /Page');
1693		$this->_out('/Parent 1 0 R');
1694		if(isset($this->PageSizes[$n]))
1695			$this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1]));
1696		$this->_out('/Resources 2 0 R');
1697		if(isset($this->PageLinks[$n]))
1698		{
1699			// Links
1700			$annots = '/Annots [';
1701			foreach($this->PageLinks[$n] as $pl)
1702			{
1703				$rect = sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
1704				$annots .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1705				if(is_string($pl[4]))
1706					$annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1707				else
1708				{
1709					$l = $this->links[$pl[4]];
1710					$h = isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt;
1711					$annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k);
1712				}
1713			}
1714			$this->_out($annots.']');
1715		}
1716		if($this->PDFVersion>'1.3')
1717			$this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>');
1718		$this->_out('/Contents '.($this->n+1).' 0 R>>');
1719		$this->_out('endobj');
1720		// Page content
1721		$p = ($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
1722		$this->_newobj();
1723		$this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
1724		$this->_putstream($p);
1725		$this->_out('endobj');
1726	}
1727	// Pages root
1728	$this->offsets[1] = strlen($this->buffer);
1729	$this->_out('1 0 obj');
1730	$this->_out('<</Type /Pages');
1731	$kids = '/Kids [';
1732	for($i=0;$i<$nb;$i++)
1733		$kids .= (3+2*$i).' 0 R ';
1734	$this->_out($kids.']');
1735	$this->_out('/Count '.$nb);
1736	$this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt));
1737	$this->_out('>>');
1738	$this->_out('endobj');
1739}
1740
1741function _putfonts()
1742{
1743	$nf=$this->n;
1744	foreach($this->diffs as $diff)
1745	{
1746		// Encodings
1747		$this->_newobj();
1748		$this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
1749		$this->_out('endobj');
1750	}
1751	foreach($this->FontFiles as $file=>$info)
1752	{
1753	   if (!isset($info['type']) || $info['type']!='TTF') {
1754		// Font file embedding
1755		$this->_newobj();
1756		$this->FontFiles[$file]['n']=$this->n;
1757		$font='';
1758		$f=fopen($this->_getfontpath().$file,'rb',1);
1759		if(!$f)
1760			$this->Error('Font file not found');
1761		while(!feof($f))
1762			$font.=fread($f,8192);
1763		fclose($f);
1764		$compressed=(substr($file,-2)=='.z');
1765		if(!$compressed && isset($info['length2']))
1766		{
1767			$header=(ord($font[0])==128);
1768			if($header)
1769			{
1770				// Strip first binary header
1771				$font=substr($font,6);
1772			}
1773			if($header && ord($font[$info['length1']])==128)
1774			{
1775				// Strip second binary header
1776				$font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
1777			}
1778		}
1779		$this->_out('<</Length '.strlen($font));
1780		if($compressed)
1781			$this->_out('/Filter /FlateDecode');
1782		$this->_out('/Length1 '.$info['length1']);
1783		if(isset($info['length2']))
1784			$this->_out('/Length2 '.$info['length2'].' /Length3 0');
1785		$this->_out('>>');
1786		$this->_putstream($font);
1787		$this->_out('endobj');
1788	   }
1789	}
1790	foreach($this->fonts as $k=>$font)
1791	{
1792		// Font objects
1793		//$this->fonts[$k]['n']=$this->n+1;
1794		$type = $font['type'];
1795		$name = $font['name'];
1796		if($type=='Core')
1797		{
1798			// Standard font
1799			$this->fonts[$k]['n']=$this->n+1;
1800			$this->_newobj();
1801			$this->_out('<</Type /Font');
1802			$this->_out('/BaseFont /'.$name);
1803			$this->_out('/Subtype /Type1');
1804			if($name!='Symbol' && $name!='ZapfDingbats')
1805				$this->_out('/Encoding /WinAnsiEncoding');
1806			$this->_out('>>');
1807			$this->_out('endobj');
1808		}
1809		elseif($type=='Type1' || $type=='TrueType')
1810		{
1811			// Additional Type1 or TrueType font
1812			$this->fonts[$k]['n']=$this->n+1;
1813			$this->_newobj();
1814			$this->_out('<</Type /Font');
1815			$this->_out('/BaseFont /'.$name);
1816			$this->_out('/Subtype /'.$type);
1817			$this->_out('/FirstChar 32 /LastChar 255');
1818			$this->_out('/Widths '.($this->n+1).' 0 R');
1819			$this->_out('/FontDescriptor '.($this->n+2).' 0 R');
1820			if($font['enc'])
1821			{
1822				if(isset($font['diff']))
1823					$this->_out('/Encoding '.($nf+$font['diff']).' 0 R');
1824				else
1825					$this->_out('/Encoding /WinAnsiEncoding');
1826			}
1827			$this->_out('>>');
1828			$this->_out('endobj');
1829			// Widths
1830			$this->_newobj();
1831			$cw=&$font['cw'];
1832			$s='[';
1833			for($i=32;$i<=255;$i++)
1834				$s.=$cw[chr($i)].' ';
1835			$this->_out($s.']');
1836			$this->_out('endobj');
1837			// Descriptor
1838			$this->_newobj();
1839			$s='<</Type /FontDescriptor /FontName /'.$name;
1840			foreach($font['desc'] as $k=>$v)
1841				$s.=' /'.$k.' '.$v;
1842			$file=$font['file'];
1843			if($file)
1844				$s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
1845			$this->_out($s.'>>');
1846			$this->_out('endobj');
1847		}
1848		// TrueType embedded SUBSETS or FULL
1849		else if ($type=='TTF') {
1850			$this->fonts[$k]['n']=$this->n+1;
1851			require_once($this->_getfontpath().'unifont/ttfonts.php');
1852			$ttf = new TTFontFile();
1853			$fontname = 'MPDFAA'.'+'.$font['name'];
1854			$subset = $font['subset'];
1855			unset($subset[0]);
1856			$ttfontstream = $ttf->makeSubset($font['ttffile'], $subset);
1857			$ttfontsize = strlen($ttfontstream);
1858			$fontstream = gzcompress($ttfontstream);
1859			$codeToGlyph = $ttf->codeToGlyph;
1860			unset($codeToGlyph[0]);
1861
1862			// Type0 Font
1863			// A composite font - a font composed of other fonts, organized hierarchically
1864			$this->_newobj();
1865			$this->_out('<</Type /Font');
1866			$this->_out('/Subtype /Type0');
1867			$this->_out('/BaseFont /'.$fontname.'');
1868			$this->_out('/Encoding /Identity-H'); 
1869			$this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
1870			$this->_out('/ToUnicode '.($this->n + 2).' 0 R');
1871			$this->_out('>>');
1872			$this->_out('endobj');
1873
1874			// CIDFontType2
1875			// A CIDFont whose glyph descriptions are based on TrueType font te…

Large files files are truncated, but you can click here to view the full file