diff --git a/include/tfpdf/font/courier.php b/include/tfpdf/font/courier.php index 913f9a453..213bf35a5 100644 --- a/include/tfpdf/font/courier.php +++ b/include/tfpdf/font/courier.php @@ -1,7 +1,8 @@ diff --git a/include/tfpdf/font/helvetica.php b/include/tfpdf/font/helvetica.php index ca94cdf21..7e20c3a70 100644 --- a/include/tfpdf/font/helvetica.php +++ b/include/tfpdf/font/helvetica.php @@ -1,5 +1,9 @@ 278,chr(1)=>278,chr(2)=>278,chr(3)=>278,chr(4)=>278,chr(5)=>278,chr(6)=>278,chr(7)=>278,chr(8)=>278,chr(9)=>278,chr(10)=>278,chr(11)=>278,chr(12)=>278,chr(13)=>278,chr(14)=>278,chr(15)=>278,chr(16)=>278,chr(17)=>278,chr(18)=>278,chr(19)=>278,chr(20)=>278,chr(21)=>278, chr(22)=>278,chr(23)=>278,chr(24)=>278,chr(25)=>278,chr(26)=>278,chr(27)=>278,chr(28)=>278,chr(29)=>278,chr(30)=>278,chr(31)=>278,' '=>278,'!'=>278,'"'=>355,'#'=>556,'$'=>556,'%'=>889,'&'=>667,'\''=>191,'('=>333,')'=>333,'*'=>389,'+'=>584, ','=>278,'-'=>333,'.'=>278,'/'=>278,'0'=>556,'1'=>556,'2'=>556,'3'=>556,'4'=>556,'5'=>556,'6'=>556,'7'=>556,'8'=>556,'9'=>556,':'=>278,';'=>278,'<'=>584,'='=>584,'>'=>584,'?'=>556,'@'=>1015,'A'=>667, diff --git a/include/tfpdf/font/helveticab.php b/include/tfpdf/font/helveticab.php index 276cfa8cb..452e0ac21 100644 --- a/include/tfpdf/font/helveticab.php +++ b/include/tfpdf/font/helveticab.php @@ -1,5 +1,9 @@ 278,chr(1)=>278,chr(2)=>278,chr(3)=>278,chr(4)=>278,chr(5)=>278,chr(6)=>278,chr(7)=>278,chr(8)=>278,chr(9)=>278,chr(10)=>278,chr(11)=>278,chr(12)=>278,chr(13)=>278,chr(14)=>278,chr(15)=>278,chr(16)=>278,chr(17)=>278,chr(18)=>278,chr(19)=>278,chr(20)=>278,chr(21)=>278, chr(22)=>278,chr(23)=>278,chr(24)=>278,chr(25)=>278,chr(26)=>278,chr(27)=>278,chr(28)=>278,chr(29)=>278,chr(30)=>278,chr(31)=>278,' '=>278,'!'=>333,'"'=>474,'#'=>556,'$'=>556,'%'=>889,'&'=>722,'\''=>238,'('=>333,')'=>333,'*'=>389,'+'=>584, ','=>278,'-'=>333,'.'=>278,'/'=>278,'0'=>556,'1'=>556,'2'=>556,'3'=>556,'4'=>556,'5'=>556,'6'=>556,'7'=>556,'8'=>556,'9'=>556,':'=>333,';'=>333,'<'=>584,'='=>584,'>'=>584,'?'=>611,'@'=>975,'A'=>722, diff --git a/include/tfpdf/font/helveticabi.php b/include/tfpdf/font/helveticabi.php index 8d2177432..ea5c56fc8 100644 --- a/include/tfpdf/font/helveticabi.php +++ b/include/tfpdf/font/helveticabi.php @@ -1,5 +1,9 @@ 278,chr(1)=>278,chr(2)=>278,chr(3)=>278,chr(4)=>278,chr(5)=>278,chr(6)=>278,chr(7)=>278,chr(8)=>278,chr(9)=>278,chr(10)=>278,chr(11)=>278,chr(12)=>278,chr(13)=>278,chr(14)=>278,chr(15)=>278,chr(16)=>278,chr(17)=>278,chr(18)=>278,chr(19)=>278,chr(20)=>278,chr(21)=>278, chr(22)=>278,chr(23)=>278,chr(24)=>278,chr(25)=>278,chr(26)=>278,chr(27)=>278,chr(28)=>278,chr(29)=>278,chr(30)=>278,chr(31)=>278,' '=>278,'!'=>333,'"'=>474,'#'=>556,'$'=>556,'%'=>889,'&'=>722,'\''=>238,'('=>333,')'=>333,'*'=>389,'+'=>584, ','=>278,'-'=>333,'.'=>278,'/'=>278,'0'=>556,'1'=>556,'2'=>556,'3'=>556,'4'=>556,'5'=>556,'6'=>556,'7'=>556,'8'=>556,'9'=>556,':'=>333,';'=>333,'<'=>584,'='=>584,'>'=>584,'?'=>611,'@'=>975,'A'=>722, diff --git a/include/tfpdf/font/helveticai.php b/include/tfpdf/font/helveticai.php index 88bf4371b..e3c638a58 100644 --- a/include/tfpdf/font/helveticai.php +++ b/include/tfpdf/font/helveticai.php @@ -1,5 +1,9 @@ 278,chr(1)=>278,chr(2)=>278,chr(3)=>278,chr(4)=>278,chr(5)=>278,chr(6)=>278,chr(7)=>278,chr(8)=>278,chr(9)=>278,chr(10)=>278,chr(11)=>278,chr(12)=>278,chr(13)=>278,chr(14)=>278,chr(15)=>278,chr(16)=>278,chr(17)=>278,chr(18)=>278,chr(19)=>278,chr(20)=>278,chr(21)=>278, chr(22)=>278,chr(23)=>278,chr(24)=>278,chr(25)=>278,chr(26)=>278,chr(27)=>278,chr(28)=>278,chr(29)=>278,chr(30)=>278,chr(31)=>278,' '=>278,'!'=>278,'"'=>355,'#'=>556,'$'=>556,'%'=>889,'&'=>667,'\''=>191,'('=>333,')'=>333,'*'=>389,'+'=>584, ','=>278,'-'=>333,'.'=>278,'/'=>278,'0'=>556,'1'=>556,'2'=>556,'3'=>556,'4'=>556,'5'=>556,'6'=>556,'7'=>556,'8'=>556,'9'=>556,':'=>278,';'=>278,'<'=>584,'='=>584,'>'=>584,'?'=>556,'@'=>1015,'A'=>667, diff --git a/include/tfpdf/font/symbol.php b/include/tfpdf/font/symbol.php index 43b50e451..b980b07b8 100644 --- a/include/tfpdf/font/symbol.php +++ b/include/tfpdf/font/symbol.php @@ -1,5 +1,9 @@ 250,chr(1)=>250,chr(2)=>250,chr(3)=>250,chr(4)=>250,chr(5)=>250,chr(6)=>250,chr(7)=>250,chr(8)=>250,chr(9)=>250,chr(10)=>250,chr(11)=>250,chr(12)=>250,chr(13)=>250,chr(14)=>250,chr(15)=>250,chr(16)=>250,chr(17)=>250,chr(18)=>250,chr(19)=>250,chr(20)=>250,chr(21)=>250, chr(22)=>250,chr(23)=>250,chr(24)=>250,chr(25)=>250,chr(26)=>250,chr(27)=>250,chr(28)=>250,chr(29)=>250,chr(30)=>250,chr(31)=>250,' '=>250,'!'=>333,'"'=>713,'#'=>500,'$'=>549,'%'=>833,'&'=>778,'\''=>439,'('=>333,')'=>333,'*'=>500,'+'=>549, ','=>250,'-'=>549,'.'=>250,'/'=>278,'0'=>500,'1'=>500,'2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>278,';'=>278,'<'=>549,'='=>549,'>'=>549,'?'=>444,'@'=>549,'A'=>722, diff --git a/include/tfpdf/font/times.php b/include/tfpdf/font/times.php index 837c706e0..d3ea8087d 100644 --- a/include/tfpdf/font/times.php +++ b/include/tfpdf/font/times.php @@ -1,5 +1,9 @@ 250,chr(1)=>250,chr(2)=>250,chr(3)=>250,chr(4)=>250,chr(5)=>250,chr(6)=>250,chr(7)=>250,chr(8)=>250,chr(9)=>250,chr(10)=>250,chr(11)=>250,chr(12)=>250,chr(13)=>250,chr(14)=>250,chr(15)=>250,chr(16)=>250,chr(17)=>250,chr(18)=>250,chr(19)=>250,chr(20)=>250,chr(21)=>250, chr(22)=>250,chr(23)=>250,chr(24)=>250,chr(25)=>250,chr(26)=>250,chr(27)=>250,chr(28)=>250,chr(29)=>250,chr(30)=>250,chr(31)=>250,' '=>250,'!'=>333,'"'=>408,'#'=>500,'$'=>500,'%'=>833,'&'=>778,'\''=>180,'('=>333,')'=>333,'*'=>500,'+'=>564, ','=>250,'-'=>333,'.'=>250,'/'=>278,'0'=>500,'1'=>500,'2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>278,';'=>278,'<'=>564,'='=>564,'>'=>564,'?'=>444,'@'=>921,'A'=>722, diff --git a/include/tfpdf/font/timesb.php b/include/tfpdf/font/timesb.php index 09cff86ac..1c198f0a6 100644 --- a/include/tfpdf/font/timesb.php +++ b/include/tfpdf/font/timesb.php @@ -1,5 +1,9 @@ 250,chr(1)=>250,chr(2)=>250,chr(3)=>250,chr(4)=>250,chr(5)=>250,chr(6)=>250,chr(7)=>250,chr(8)=>250,chr(9)=>250,chr(10)=>250,chr(11)=>250,chr(12)=>250,chr(13)=>250,chr(14)=>250,chr(15)=>250,chr(16)=>250,chr(17)=>250,chr(18)=>250,chr(19)=>250,chr(20)=>250,chr(21)=>250, chr(22)=>250,chr(23)=>250,chr(24)=>250,chr(25)=>250,chr(26)=>250,chr(27)=>250,chr(28)=>250,chr(29)=>250,chr(30)=>250,chr(31)=>250,' '=>250,'!'=>333,'"'=>555,'#'=>500,'$'=>500,'%'=>1000,'&'=>833,'\''=>278,'('=>333,')'=>333,'*'=>500,'+'=>570, ','=>250,'-'=>333,'.'=>250,'/'=>278,'0'=>500,'1'=>500,'2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>333,';'=>333,'<'=>570,'='=>570,'>'=>570,'?'=>500,'@'=>930,'A'=>722, diff --git a/include/tfpdf/font/timesbi.php b/include/tfpdf/font/timesbi.php index b4e38d763..a6034b26a 100644 --- a/include/tfpdf/font/timesbi.php +++ b/include/tfpdf/font/timesbi.php @@ -1,5 +1,9 @@ 250,chr(1)=>250,chr(2)=>250,chr(3)=>250,chr(4)=>250,chr(5)=>250,chr(6)=>250,chr(7)=>250,chr(8)=>250,chr(9)=>250,chr(10)=>250,chr(11)=>250,chr(12)=>250,chr(13)=>250,chr(14)=>250,chr(15)=>250,chr(16)=>250,chr(17)=>250,chr(18)=>250,chr(19)=>250,chr(20)=>250,chr(21)=>250, chr(22)=>250,chr(23)=>250,chr(24)=>250,chr(25)=>250,chr(26)=>250,chr(27)=>250,chr(28)=>250,chr(29)=>250,chr(30)=>250,chr(31)=>250,' '=>250,'!'=>389,'"'=>555,'#'=>500,'$'=>500,'%'=>833,'&'=>778,'\''=>278,'('=>333,')'=>333,'*'=>500,'+'=>570, ','=>250,'-'=>333,'.'=>250,'/'=>278,'0'=>500,'1'=>500,'2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>333,';'=>333,'<'=>570,'='=>570,'>'=>570,'?'=>500,'@'=>832,'A'=>667, diff --git a/include/tfpdf/font/timesi.php b/include/tfpdf/font/timesi.php index 0ba2b7773..bd9e0d9e9 100644 --- a/include/tfpdf/font/timesi.php +++ b/include/tfpdf/font/timesi.php @@ -1,5 +1,9 @@ 250,chr(1)=>250,chr(2)=>250,chr(3)=>250,chr(4)=>250,chr(5)=>250,chr(6)=>250,chr(7)=>250,chr(8)=>250,chr(9)=>250,chr(10)=>250,chr(11)=>250,chr(12)=>250,chr(13)=>250,chr(14)=>250,chr(15)=>250,chr(16)=>250,chr(17)=>250,chr(18)=>250,chr(19)=>250,chr(20)=>250,chr(21)=>250, chr(22)=>250,chr(23)=>250,chr(24)=>250,chr(25)=>250,chr(26)=>250,chr(27)=>250,chr(28)=>250,chr(29)=>250,chr(30)=>250,chr(31)=>250,' '=>250,'!'=>333,'"'=>420,'#'=>500,'$'=>500,'%'=>833,'&'=>778,'\''=>214,'('=>333,')'=>333,'*'=>500,'+'=>675, ','=>250,'-'=>333,'.'=>250,'/'=>278,'0'=>500,'1'=>500,'2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>333,';'=>333,'<'=>675,'='=>675,'>'=>675,'?'=>500,'@'=>920,'A'=>611, diff --git a/include/tfpdf/font/unifont/DejaVuSans-Bold.ttf b/include/tfpdf/font/unifont/DejaVuSans-Bold.ttf index 7b29accc1..0f4d5e9d4 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSans-Bold.ttf and b/include/tfpdf/font/unifont/DejaVuSans-Bold.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSans-BoldOblique.ttf b/include/tfpdf/font/unifont/DejaVuSans-BoldOblique.ttf index b5440d95e..48f06a863 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSans-BoldOblique.ttf and b/include/tfpdf/font/unifont/DejaVuSans-BoldOblique.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSans-ExtraLight.ttf b/include/tfpdf/font/unifont/DejaVuSans-ExtraLight.ttf index c488d283e..01a56521d 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSans-ExtraLight.ttf and b/include/tfpdf/font/unifont/DejaVuSans-ExtraLight.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSans-Oblique.ttf b/include/tfpdf/font/unifont/DejaVuSans-Oblique.ttf index e233295c4..44856388d 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSans-Oblique.ttf and b/include/tfpdf/font/unifont/DejaVuSans-Oblique.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSans.ttf b/include/tfpdf/font/unifont/DejaVuSans.ttf index a36615b20..27cff476e 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSans.ttf and b/include/tfpdf/font/unifont/DejaVuSans.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSansCondensed-Bold.ttf b/include/tfpdf/font/unifont/DejaVuSansCondensed-Bold.ttf index 36b3060e7..9edd89c3d 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSansCondensed-Bold.ttf and b/include/tfpdf/font/unifont/DejaVuSansCondensed-Bold.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSansCondensed-BoldOblique.ttf b/include/tfpdf/font/unifont/DejaVuSansCondensed-BoldOblique.ttf index 4bb3ae18f..e2db5ce0c 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSansCondensed-BoldOblique.ttf and b/include/tfpdf/font/unifont/DejaVuSansCondensed-BoldOblique.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSansCondensed-Oblique.ttf b/include/tfpdf/font/unifont/DejaVuSansCondensed-Oblique.ttf index 1f680bd8d..7cec04c2e 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSansCondensed-Oblique.ttf and b/include/tfpdf/font/unifont/DejaVuSansCondensed-Oblique.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSansCondensed.ttf b/include/tfpdf/font/unifont/DejaVuSansCondensed.ttf index 2b0b7fe99..826e619b7 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSansCondensed.ttf and b/include/tfpdf/font/unifont/DejaVuSansCondensed.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSansMono-Bold.ttf b/include/tfpdf/font/unifont/DejaVuSansMono-Bold.ttf index 98bb6f62e..09d42796a 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSansMono-Bold.ttf and b/include/tfpdf/font/unifont/DejaVuSansMono-Bold.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSansMono-BoldOblique.ttf b/include/tfpdf/font/unifont/DejaVuSansMono-BoldOblique.ttf index 42bbcdee9..0344c2270 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSansMono-BoldOblique.ttf and b/include/tfpdf/font/unifont/DejaVuSansMono-BoldOblique.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSansMono-Oblique.ttf b/include/tfpdf/font/unifont/DejaVuSansMono-Oblique.ttf index d6ab52298..bc16d51bd 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSansMono-Oblique.ttf and b/include/tfpdf/font/unifont/DejaVuSansMono-Oblique.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSansMono.ttf b/include/tfpdf/font/unifont/DejaVuSansMono.ttf index b464c52ad..7260bd65e 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSansMono.ttf and b/include/tfpdf/font/unifont/DejaVuSansMono.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSerif-Bold.ttf b/include/tfpdf/font/unifont/DejaVuSerif-Bold.ttf index 8c53b1a11..afa2d5ec3 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSerif-Bold.ttf and b/include/tfpdf/font/unifont/DejaVuSerif-Bold.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSerif-BoldItalic.ttf b/include/tfpdf/font/unifont/DejaVuSerif-BoldItalic.ttf index 688c2b70f..ce9443eae 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSerif-BoldItalic.ttf and b/include/tfpdf/font/unifont/DejaVuSerif-BoldItalic.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSerif-Italic.ttf b/include/tfpdf/font/unifont/DejaVuSerif-Italic.ttf index b2ed04a12..c1864a307 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSerif-Italic.ttf and b/include/tfpdf/font/unifont/DejaVuSerif-Italic.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSerif.ttf b/include/tfpdf/font/unifont/DejaVuSerif.ttf index 19eba3345..ed53a2967 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSerif.ttf and b/include/tfpdf/font/unifont/DejaVuSerif.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSerifCondensed-Bold.ttf b/include/tfpdf/font/unifont/DejaVuSerifCondensed-Bold.ttf index c4b00b784..55adca412 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSerifCondensed-Bold.ttf and b/include/tfpdf/font/unifont/DejaVuSerifCondensed-Bold.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSerifCondensed-BoldItalic.ttf b/include/tfpdf/font/unifont/DejaVuSerifCondensed-BoldItalic.ttf index 4d4c7a68e..e21195734 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSerifCondensed-BoldItalic.ttf and b/include/tfpdf/font/unifont/DejaVuSerifCondensed-BoldItalic.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSerifCondensed-Italic.ttf b/include/tfpdf/font/unifont/DejaVuSerifCondensed-Italic.ttf index 04bfc3534..275b26d95 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSerifCondensed-Italic.ttf and b/include/tfpdf/font/unifont/DejaVuSerifCondensed-Italic.ttf differ diff --git a/include/tfpdf/font/unifont/DejaVuSerifCondensed.ttf b/include/tfpdf/font/unifont/DejaVuSerifCondensed.ttf index c9935e1e9..83120cd9d 100644 Binary files a/include/tfpdf/font/unifont/DejaVuSerifCondensed.ttf and b/include/tfpdf/font/unifont/DejaVuSerifCondensed.ttf differ diff --git a/include/tfpdf/font/unifont/ttfonts.php b/include/tfpdf/font/unifont/ttfonts.php index 64f95b71d..a9db5fbe8 100644 --- a/include/tfpdf/font/unifont/ttfonts.php +++ b/include/tfpdf/font/unifont/ttfonts.php @@ -8,8 +8,8 @@ * written in Python - http://www.reportlab.com/software/opensource/ * * together with ideas from the OpenOffice source code and others. * * * -* Version: 1.02 * -* Date: 2010-06-07 * +* Version: 1.04 * +* Date: 2011-09-18 * * Author: Ian Back * * License: LGPL * * Copyright (c) Ian Back, 2010 * @@ -18,6 +18,13 @@ * * *******************************************************************************/ +// Define the value used in the "head" table of a created TTF file +// 0x74727565 "true" for Mac +// 0x00010000 for Windows +// Either seems to work for a font embedded in a PDF file +// when read by Adobe Reader on a Windows PC(!) +define("_TTF_MAC_HEADER", false); + // TrueType Font Glyph operators define("GF_WORDS",(1 << 0)); @@ -30,6 +37,7 @@ define("GF_TWOBYTWO",(1 << 7)); class TTFontFile { +var $maxUni; var $_pos; var $numTables; var $searchRange; @@ -66,44 +74,38 @@ var $maxStrLenRead; } - function getMetrics($file, $presubset=0) { + function getMetrics($file) { $this->filename = $file; $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); $this->_pos = 0; - $this->charWidths = array(); - $this->hmetrics = array(); + $this->charWidths = ''; $this->glyphPos = array(); $this->charToGlyph = array(); $this->tables = array(); $this->otables = array(); $this->ascent = 0; $this->descent = 0; - if ($presubset) { $this->skip(4); } - else { $this->readHeader(); } - $this->readTableDirectory($presubset); - $this->extractInfo($presubset); - fclose($this->fh); - } - - function readHeader() { - // read the sfnt header at the current position + $this->TTCFonts = array(); $this->version = $version = $this->read_ulong(); if ($version==0x4F54544F) die("Postscript outlines are not supported"); if ($version==0x74746366) - die("TTC Font collections are not supported"); + die("ERROR - TrueType Fonts Collections not supported"); if (!in_array($version, array(0x00010000,0x74727565))) die("Not a TrueType font: version=".$version); - return true; + $this->readTableDirectory(); + $this->extractInfo(); + fclose($this->fh); } - function readTableDirectory($presubset=1) { - $this->numTables = $this->read_ushort(); + + function readTableDirectory() { + $this->numTables = $this->read_ushort(); $this->searchRange = $this->read_ushort(); $this->entrySelector = $this->read_ushort(); $this->rangeShift = $this->read_ushort(); $this->tables = array(); - for ($i=0;$i<$this->numTables;$i++) { // 1.02 + for ($i=0;$i<$this->numTables;$i++) { $record = array(); $record['tag'] = $this->read_tag(); $record['checksum'] = array($this->read_ushort(),$this->read_ushort()); @@ -111,27 +113,8 @@ var $maxStrLenRead; $record['length'] = $this->read_ulong(); $this->tables[$record['tag']] = $record; } - if (!$presubset) $this->checksumTables(); } - function checksumTables() { - // Check the checksums for all tables - foreach($this->tables AS $t) { - if ($t['length'] > 0 && $t['length'] < $this->maxStrLenRead) { // 1.02 - $table = $this->get_chunk($t['offset'], $t['length']); - $checksum = $this->calcChecksum($table); - if ($t['tag'] == 'head') { - $up = unpack('n*', substr($table,8,4)); - $adjustment[0] = $up[1]; - $adjustment[1] = $up[2]; - $checksum = $this->sub32($checksum, $adjustment); - } - $xchecksum = $t['checksum']; - if ($xchecksum != $checksum) - die(sprintf('TTF file "%s": invalid checksum %s table: %s (expected %s)', $this->filename,dechex($checksum[0]).dechex($checksum[1]),$t['tag'],dechex($xchecksum[0]).dechex($xchecksum[1]))); - } - } - } function sub32($x, $y) { $xlo = $x[1]; @@ -196,6 +179,14 @@ var $maxStrLenRead; return $a; } + function unpack_short($s) { + $a = (ord($s[0])<<8) + ord($s[1]); + if ($a & (1 << 15) ) { + $a = ($a - (1 << 16)); + } + return $a; + } + function read_ushort() { $this->_pos += 2; $s = fread($this->fh,2); @@ -240,15 +231,25 @@ var $maxStrLenRead; return $this->splice($stream, $offset, $up); } + function _set_short($stream, $offset, $val) { + if ($val<0) { + $val = abs($val); + $val = ~$val; + $val += 1; + } + $up = pack("n",$val); + return $this->splice($stream, $offset, $up); + } + function get_chunk($pos, $length) { fseek($this->fh,$pos); - if ($length <1) { return ''; } // 1.02 + if ($length <1) { return ''; } return (fread($this->fh,$length)); } function get_table($tag) { list($pos, $length) = $this->get_table_pos($tag); - if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } // 1.02 + if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } fseek($this->fh,$pos); return (fread($this->fh,$length)); } @@ -262,23 +263,24 @@ var $maxStrLenRead; +///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////////////////////// - function extractInfo($presubset=0) { - // = 0 ; validate; does not need glyphPos or charToGlyph - // presubset = 1; no validation; does not need name and other metrics + function extractInfo() { /////////////////////////////////// // name - Naming table /////////////////////////////////// - if (!$presubset) { + $this->sFamilyClass = 0; + $this->sFamilySubClass = 0; + $name_offset = $this->seek_table("name"); $format = $this->read_ushort(); if ($format != 0) die("Unknown name table format ".$format); $numRecords = $this->read_ushort(); $string_data_offset = $name_offset + $this->read_ushort(); - $names = array(1=>'',2=>'',3=>'',4=>'',6=>''); $K = array_keys($names); $nameCount = count($names); @@ -300,7 +302,7 @@ var $maxStrLenRead; $N = ''; while ($length > 0) { $char = $this->read_ushort(); - $N .= (chr($char)); // 1.02 + $N .= (chr($char)); $length -= 1; } $this->_pos = $opos; @@ -319,7 +321,7 @@ var $maxStrLenRead; } } if ($names[6]) - $psName = preg_replace('/ /','-',$names[6]); + $psName = $names[6]; else if ($names[4]) $psName = preg_replace('/ /','-',$names[4]); else if ($names[1]) @@ -328,49 +330,27 @@ var $maxStrLenRead; $psName = ''; if (!$psName) die("Could not find PostScript font name"); - for ($i=0;$i126 || strpos(' [](){}<>/%',$c)!==false) - die("psName=".$psName." contains invalid character ".$c." ie U+".ord(c)); - } $this->name = $psName; if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } if ($names[4]) { $this->fullName = $names[4]; } else { $this->fullName = $psName; } if ($names[3]) { $this->uniqueFontID = $names[3]; } else { $this->uniqueFontID = $psName; } - } + if ($names[6]) { $this->fullName = $names[6]; } /////////////////////////////////// // head - Font header table /////////////////////////////////// $this->seek_table("head"); - if ($presubset) { $this->skip(18); } - else { - $ver_maj = $this->read_ushort(); - $ver_min = $this->read_ushort(); - if ($ver_maj != 1) - die('Unknown head table version '. $ver_maj .'.'. $ver_min); - $this->fontRevision = $this->read_ushort() . $this->read_ushort(); - - $this->skip(4); - $magic = $this->read_ulong(); - if ($magic != 0x5F0F3CF5) - die('Invalid head table magic ' .$magic); - $this->skip(2); - } - $this->unitsPerEm = $unitsPerEm = $this->read_ushort(); // 1.02 - $scale = 1000 / $unitsPerEm; // 1.02 - if ($presubset) { $this->skip(30); } // 1.02 - else { - $this->skip(16); - $xMin = $this->read_short(); - $yMin = $this->read_short(); - $xMax = $this->read_short(); - $yMax = $this->read_short(); - $this->bbox = array(($xMin*$scale), ($yMin*$scale), ($xMax*$scale), ($yMax*$scale)); - $this->skip(3*2); - } + $this->skip(18); + $this->unitsPerEm = $unitsPerEm = $this->read_ushort(); + $scale = 1000 / $unitsPerEm; + $this->skip(16); + $xMin = $this->read_short(); + $yMin = $this->read_short(); + $xMax = $this->read_short(); + $yMax = $this->read_short(); + $this->bbox = array(($xMin*$scale), ($yMin*$scale), ($xMax*$scale), ($yMax*$scale)); + $this->skip(3*2); $indexToLocFormat = $this->read_ushort(); $glyphDataFormat = $this->read_ushort(); if ($glyphDataFormat != 0) @@ -399,9 +379,17 @@ var $maxStrLenRead; $usWeightClass = $this->read_ushort(); $this->skip(2); $fsType = $this->read_ushort(); - // if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) - // die('Font does not allow subsetting/embedding'); - $this->skip(58); //11*2 + 10 + 4*4 + 4 + 3*2 + if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) { + die('ERROR - Font file '.$this->filename.' cannot be embedded due to copyright restrictions.'); + $this->restrictedUse = true; + } + $this->skip(20); + $sF = $this->read_short(); + $this->sFamilyClass = ($sF >> 8); + $this->sFamilySubClass = ($sF & 0xFF); + $this->_pos += 10; //PANOSE = 10 byte length + $panose = fread($this->fh,10); + $this->skip(26); $sTypoAscender = $this->read_short(); $sTypoDescender = $this->read_short(); if (!$this->ascent) $this->ascent = ($sTypoAscender*$scale); @@ -427,13 +415,7 @@ var $maxStrLenRead; // post - PostScript table /////////////////////////////////// $this->seek_table("post"); - if ($presubset) { $this->skip(4); } - else { - $ver_maj = $this->read_ushort(); - $ver_min = $this->read_ushort(); - if ($ver_maj <1 || $ver_maj >4) - die('Unknown post table version '.$ver_maj); - } + $this->skip(4); $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0; $this->underlinePosition = $this->read_short() * $scale; $this->underlineThickness = $this->read_short() * $scale; @@ -452,14 +434,7 @@ var $maxStrLenRead; // hhea - Horizontal header table /////////////////////////////////// $this->seek_table("hhea"); - if ($presubset) { $this->skip(32); } - else { - $ver_maj = $this->read_ushort(); - $ver_min = $this->read_ushort(); - if ($ver_maj != 1) - die('Unknown hhea table version '.$ver_maj); - $this->skip(28); - } + $this->skip(32); $metricDataFormat = $this->read_ushort(); if ($metricDataFormat != 0) die('Unknown horizontal metric data format '.$metricDataFormat); @@ -471,15 +446,10 @@ var $maxStrLenRead; // maxp - Maximum profile table /////////////////////////////////// $this->seek_table("maxp"); - if ($presubset) { $this->skip(4); } - else { - $ver_maj = $this->read_ushort(); - $ver_min = $this->read_ushort(); - if ($ver_maj != 1) - die('Unknown maxp table version '.$ver_maj); - } + $this->skip(4); $numGlyphs = $this->read_ushort(); + /////////////////////////////////// // cmap - Character to glyph index mapping table /////////////////////////////////// @@ -492,14 +462,88 @@ var $maxStrLenRead; $encodingID = $this->read_ushort(); $offset = $this->read_ulong(); $save_pos = $this->_pos; - if ($platformID == 3 && $encodingID == 1) { // Microsoft, Unicode + if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode $format = $this->get_ushort($cmap_offset + $offset); if ($format == 4) { - $unicode_cmap_offset = $cmap_offset + $offset; + if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; break; } } - else if ($platformID == 0) { // Unicode -- assume all encodings are compatible + $this->seek($save_pos ); + } + if (!$unicode_cmap_offset) + die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); + + + $glyphToChar = array(); + $charToGlyph = array(); + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph ); + + /////////////////////////////////// + // hmtx - Horizontal metrics table + /////////////////////////////////// + $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); + + } + + +///////////////////////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////////////////////// + + + function makeSubset($file, &$subset) { + $this->filename = $file; + $this->fh = fopen($file ,'rb') or die('Can\'t open file ' . $file); + $this->_pos = 0; + $this->charWidths = ''; + $this->glyphPos = array(); + $this->charToGlyph = array(); + $this->tables = array(); + $this->otables = array(); + $this->ascent = 0; + $this->descent = 0; + $this->skip(4); + $this->maxUni = 0; + $this->readTableDirectory(); + + + /////////////////////////////////// + // head - Font header table + /////////////////////////////////// + $this->seek_table("head"); + $this->skip(50); + $indexToLocFormat = $this->read_ushort(); + $glyphDataFormat = $this->read_ushort(); + + /////////////////////////////////// + // hhea - Horizontal header table + /////////////////////////////////// + $this->seek_table("hhea"); + $this->skip(32); + $metricDataFormat = $this->read_ushort(); + $orignHmetrics = $numberOfHMetrics = $this->read_ushort(); + + /////////////////////////////////// + // maxp - Maximum profile table + /////////////////////////////////// + $this->seek_table("maxp"); + $this->skip(4); + $numGlyphs = $this->read_ushort(); + + + /////////////////////////////////// + // cmap - Character to glyph index mapping table + /////////////////////////////////// + $cmap_offset = $this->seek_table("cmap"); + $this->skip(2); + $cmapTableCount = $this->read_ushort(); + $unicode_cmap_offset = 0; + for ($i=0;$i<$cmapTableCount;$i++) { + $platformID = $this->read_ushort(); + $encodingID = $this->read_ushort(); + $offset = $this->read_ulong(); + $save_pos = $this->_pos; + if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode $format = $this->get_ushort($cmap_offset + $offset); if ($format == 4) { $unicode_cmap_offset = $cmap_offset + $offset; @@ -510,166 +554,68 @@ var $maxStrLenRead; } if (!$unicode_cmap_offset) - die('Font does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); - $this->seek($unicode_cmap_offset + 2); - $length = $this->read_ushort(); - $limit = $unicode_cmap_offset + $length; - $this->skip(2); + die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); - $segCount = $this->read_ushort() / 2; - $this->skip(6); - $endCount = array(); - for($i=0; $i<$segCount; $i++) { $endCount[] = $this->read_ushort(); } - $this->skip(2); - $startCount = array(); - for($i=0; $i<$segCount; $i++) { $startCount[] = $this->read_ushort(); } - $idDelta = array(); - for($i=0; $i<$segCount; $i++) { $idDelta[] = $this->read_short(); } // ???? was unsigned short - $idRangeOffset_start = $this->_pos; - $idRangeOffset = array(); - for($i=0; $i<$segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } $glyphToChar = array(); $charToGlyph = array(); - for ($n=0;$n<$segCount;$n++) { - for ($unichar=$startCount[$n];$unichar<($endCount[$n] + 1);$unichar++) { - if ($idRangeOffset[$n] == 0) - $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; - else { - $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; - $offset = $idRangeOffset_start + 2 * $n + $offset; - if ($offset >= $limit) - $glyph = 0; - else { - $glyph = $this->get_ushort($offset); - if ($glyph != 0) - $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; - } - } - if ($presubset) $charToGlyph[$unichar] = $glyph; - $glyphToChar[$glyph][] = $unichar; - } - } - if ($presubset) $this->charToGlyph = $charToGlyph; + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph ); + + $this->charToGlyph = $charToGlyph; /////////////////////////////////// // hmtx - Horizontal metrics table /////////////////////////////////// - $this->seek_table("hmtx"); - $aw = 0; - $this->charWidths = array(); - $this->hmetrics = array(); - for( $glyph=0; $glyph<$numberOfHMetrics; $glyph++) { - $aw = $this->read_ushort(); - $lsb = $this->read_short(); - $this->hmetrics[] = array($aw, $lsb); - $aw = $scale*$aw; - if ($glyph == 0) - $this->defaultWidth = $aw; - if (isset($glyphToChar[$glyph])) { - foreach($glyphToChar[$glyph] AS $char) { - $this->charWidths[$char] = round($aw); - } - } - } - for( $glyph=$numberOfHMetrics; $glyph<$numGlyphs; $glyph++) { - $lsb = $this->read_ushort(); - $this->hmetrics[] = array($aw, $lsb); - if (isset($glyphToChar[$glyph])) { - foreach($glyphToChar[$glyph] AS $char) { - $this->charWidths[$char] = round($aw); - } - } - } + $scale = 1; // not used + $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); /////////////////////////////////// // loca - Index to location /////////////////////////////////// - if ($presubset) { - $this->seek_table('loca'); - $this->glyphPos = array(); - if ($indexToLocFormat == 0) { - for ($n=0; $n<=$numGlyphs; $n++) { // 1.02 - $this->glyphPos[] = ($this->read_ushort() * 2); - } - } - else if ($indexToLocFormat == 1) { - for ($n=0; $n<=$numGlyphs; $n++) { // 1.02 - $this->glyphPos[] = ($this->read_ulong()); - } - } - else - die('Unknown location table format '.$indexToLocFormat); - } - } + $this->getLOCA($indexToLocFormat, $numGlyphs); - -///////////////////////////////////////////////////////////////////////////////////////// - - - function makeSubset($subset) { - $this->fh = fopen($this->filename ,'rb') or die('Can\'t open file ' . $this->filename ); - $this->otables = array(); - $glyphMap = array(0=>0); - $glyphSet = array(0=>0); - $codeToGlyph = array(); + $subsetglyphs = array(0=>0); + $subsetCharToGlyph = array(); foreach($subset AS $code) { - if (isset($this->charToGlyph[$code])) - $originalGlyphIdx = $this->charToGlyph[$code]; - else - $originalGlyphIdx = 0; - if (!isset($glyphSet[$originalGlyphIdx])) { - $glyphSet[$originalGlyphIdx] = count($glyphMap); - $glyphMap[] = $originalGlyphIdx; + if (isset($this->charToGlyph[$code])) { + $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode + $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID + } - $codeToGlyph[$code] = $glyphSet[$originalGlyphIdx]; + $this->maxUni = max($this->maxUni, $code); } list($start,$dummy) = $this->get_table_pos('glyf'); + $glyphSet = array(); + ksort($subsetglyphs); $n = 0; - while ($n < count($glyphMap)) { - $originalGlyphIdx = $glyphMap[$n]; - $glyphPos = $this->glyphPos[$originalGlyphIdx]; - $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; - $n += 1; - if (!$glyphLen) continue; - $this->seek($start + $glyphPos); - $numberOfContours = $this->read_short(); - if ($numberOfContours < 0) { - $this->skip(8); - $flags = GF_MORE; - while ($flags & GF_MORE) { - $flags = $this->read_ushort(); - $glyphIdx = $this->read_ushort(); - if (!isset($glyphSet[$glyphIdx])) { - $glyphSet[$glyphIdx] = count($glyphMap); - $glyphMap[] = $glyphIdx; - } - if ($flags & GF_WORDS) - $this->skip(4); - else - $this->skip(2); - if ($flags & GF_SCALE) - $this->skip(2); - else if ($flags & GF_XYSCALE) - $this->skip(4); - else if ($flags & GF_TWOBYTWO) - $this->skip(8); - } - } + $fsLastCharIndex = 0; // maximum Unicode index (character code) in this font, according to the cmap subtable for platform ID 3 and platform- specific encoding ID 0 or 1. + foreach($subsetglyphs AS $originalGlyphIdx => $uni) { + $fsLastCharIndex = max($fsLastCharIndex , $uni); + $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID + $n++; } - $numGlyphs = $n = count($glyphMap); - while ($n > 1 && $this->hmetrics[$n][0] == $this->hmetrics[$n - 1][0]) { $n -= 1; } - $numberOfHMetrics = $n; + ksort($subsetCharToGlyph); + foreach($subsetCharToGlyph AS $uni => $originalGlyphIdx) { + $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx] ; + } + $this->codeToGlyph = $codeToGlyph; + + ksort($subsetglyphs); + foreach($subsetglyphs AS $originalGlyphIdx => $uni) { + $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs); + } + + $numGlyphs = $numberOfHMetrics = count($subsetglyphs ); //tables copied from the original - $tags = array ('name', 'OS/2', 'prep'); + $tags = array ('name'); foreach($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } - $tags = array ('cvt ', 'fpgm'); - foreach($tags AS $tag) { // 1.02 - if (isset($this->table['tag'])) { $this->add($tag, $this->get_table($tag)); } + $tags = array ('cvt ', 'fpgm', 'prep', 'gasp'); + foreach($tags AS $tag) { + if (isset($this->tables[$tag])) { $this->add($tag, $this->get_table($tag)); } } // post - PostScript @@ -677,75 +623,85 @@ var $maxStrLenRead; $post = "\x00\x03\x00\x00" . substr($opost,4,12) . "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; $this->add('post', $post); - // hhea - Horizontal Header - $hhea = $this->get_table('hhea'); - $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics); - $this->add('hhea', $hhea); + // Sort CID2GID map into segments of contiguous codes + ksort($codeToGlyph); + unset($codeToGlyph[0]); + //unset($codeToGlyph[65535]); + $rangeid = 0; + $range = array(); + $prevcid = -2; + $prevglidx = -1; + // for each character + foreach ($codeToGlyph as $cid => $glidx) { + if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) { + $range[$rangeid][] = $glidx; + } else { + // new range + $rangeid = $cid; + $range[$rangeid] = array(); + $range[$rangeid][] = $glidx; + } + $prevcid = $cid; + $prevglidx = $glidx; + } - // maxp - Maximum Profile - $maxp = $this->get_table('maxp'); - $maxp = $this->_set_ushort($maxp, 4, $numGlyphs); - $this->add('maxp', $maxp); - - // cmap - Character to glyph mapping - Format 6 - $entryCount = count($subset); - $length = 10 + $entryCount * 2; - $cmap = array(0, 1, // Index : version, number of subtables - 1, 0, // Subtable : platform, encoding - 0, 12, // offset (hi,lo) - 6, $length, // Format 6 Mapping table: format, length - 0, 1, // language, First char code - $entryCount); - - foreach($subset AS $code) { $cmap[] = $codeToGlyph[$code]; } - $cmapstr = ''; - foreach($cmap AS $cm) { $cmapstr .= pack("n",$cm); } - $this->add('cmap', $cmapstr); - -/* // cmap - Character to glyph mapping - Format 4 (MS / ) - $segCount = 2; + $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF $searchRange = 1; $entrySelector = 0; while ($searchRange * 2 <= $segCount ) { $searchRange = $searchRange * 2; $entrySelector = $entrySelector + 1; } - $searchRange = $searchRange * 16; - $rangeShift = $segCount * 16 - $searchRange; - $length = 48 + ($numGlyphs-1); - $cmap = array(0, 1, // Index : version, number of subtables - 3, 0, // Subtable : platform (MS=3), encoding - 0, 12, // Subtable : offset (hi,lo) - 4, $length, 0, // Format 4 Mapping table: format, length, language + $searchRange = $searchRange * 2; + $rangeShift = $segCount * 2 - $searchRange; + $length = 16 + (8*$segCount ) + ($numGlyphs+1); + $cmap = array(0, 1, // Index : version, number of encoding subtables + 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) + 0, 12, // Encoding Subtable : offset (hi,lo) + 4, $length, 0, // Format 4 Mapping subtable: format, length, language $segCount*2, $searchRange, $entrySelector, - $rangeShift, - $numGlyphs, 0xFFFF, // endCode(s) - 0, - 1, 0xFFFF, // startCode(s) - 0, 1, // idDelta(s) Delta for all character codes in segment - 0, 0); // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 - foreach($subset AS $code) { $cmap[] = $codeToGlyph[$code]; } - $cmap[] = 0xFFFF; + $rangeShift); + + // endCode(s) + foreach($range AS $start=>$subrange) { + $endCode = $start + (count($subrange)-1); + $cmap[] = $endCode; // endCode(s) + } + $cmap[] = 0xFFFF; // endCode of last Segment + $cmap[] = 0; // reservedPad + + // startCode(s) + foreach($range AS $start=>$subrange) { + $cmap[] = $start; // startCode(s) + } + $cmap[] = 0xFFFF; // startCode of last Segment + // idDelta(s) + foreach($range AS $start=>$subrange) { + $idDelta = -($start-$subrange[0]); + $n += count($subrange); + $cmap[] = $idDelta; // idDelta(s) + } + $cmap[] = 1; // idDelta of last Segment + // idRangeOffset(s) + foreach($range AS $subrange) { + $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 + + } + $cmap[] = 0; // idRangeOffset of last Segment + foreach($range AS $subrange) { + foreach($subrange AS $glidx) { + $cmap[] = $glidx; + } + } + $cmap[] = 0; // Mapping for last character $cmapstr = ''; foreach($cmap AS $cm) { $cmapstr .= pack("n",$cm); } $this->add('cmap', $cmapstr); -*/ - // hmtx - Horizontal Metrics - $hmtxstr = ''; - for($n=0;$n<$numGlyphs;$n++) { - $originalGlyphIdx = $glyphMap[$n]; - $aw = $this->hmetrics[$originalGlyphIdx][0]; - $lsb = $this->hmetrics[$originalGlyphIdx][1]; - if ($n < $numberOfHMetrics) { $hmtxstr .= pack("n",$aw); } - $hmtxstr .= $this->pack_short($lsb); - } - $this->add('hmtx', $hmtxstr); - // glyf - Glyph data list($glyfOffset,$glyfLength) = $this->get_table_pos('glyf'); if ($glyfLength < $this->maxStrLenRead) { @@ -756,9 +712,29 @@ var $maxStrLenRead; $glyf = ''; $pos = 0; - for ($n=0;$n<$numGlyphs;$n++) { + $hmtxstr = ''; + $xMinT = 0; + $yMinT = 0; + $xMaxT = 0; + $yMaxT = 0; + $advanceWidthMax = 0; + $minLeftSideBearing = 0; + $minRightSideBearing = 0; + $xMaxExtent = 0; + $maxPoints = 0; // points in non-compound glyph + $maxContours = 0; // contours in non-compound glyph + $maxComponentPoints = 0; // points in compound glyph + $maxComponentContours = 0; // contours in compound glyph + $maxComponentElements = 0; // number of glyphs referenced at top level + $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs + $this->glyphdata = array(); + + foreach($subsetglyphs AS $originalGlyphIdx => $uni) { + // hmtx - Horizontal Metrics + $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx); + $hmtxstr .= $hm; + $offsets[] = $pos; - $originalGlyphIdx = $glyphMap[$n]; $glyphPos = $this->glyphPos[$originalGlyphIdx]; $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; if ($glyfLength < $this->maxStrLenRead) { @@ -768,15 +744,22 @@ var $maxStrLenRead; if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset+$glyphPos,$glyphLen); else $data = ''; } - if ($glyphLen > 0) $up = unpack("n", substr($data,0,2)); - if ($glyphLen > 2 && ($up[1] & (1 << 15)) ) { + + if ($glyphLen > 0) { + $up = unpack("n", substr($data,0,2)); + } + + if ($glyphLen > 2 && ($up[1] & (1 << 15)) ) { // If number of contours <= -1 i.e. composiste glyph $pos_in_glyph = 10; $flags = GF_MORE; + $nComponentElements = 0; while ($flags & GF_MORE) { + $nComponentElements += 1; // number of glyphs referenced at top level $up = unpack("n", substr($data,$pos_in_glyph,2)); $flags = $up[1]; $up = unpack("n", substr($data,$pos_in_glyph+2,2)); $glyphIdx = $up[1]; + $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); $pos_in_glyph += 4; if ($flags & GF_WORDS) { $pos_in_glyph += 4; } @@ -785,7 +768,9 @@ var $maxStrLenRead; else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } } + $maxComponentElements = max($maxComponentElements, $nComponentElements); } + $glyf .= $data; $pos += $glyphLen; if ($pos % 4 != 0) { @@ -794,9 +779,13 @@ var $maxStrLenRead; $pos += $padding; } } + $offsets[] = $pos; $this->add('glyf', $glyf); + // hmtx - Horizontal Metrics + $this->add('hmtx', $hmtxstr); + // loca - Index to location $locastr = ''; if ((($pos + 1) >> 1) > 0xFFFF) { @@ -814,9 +803,236 @@ var $maxStrLenRead; $head = $this->_set_ushort($head, 50, $indexToLocFormat); $this->add('head', $head); + + // hhea - Horizontal Header + $hhea = $this->get_table('hhea'); + $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics); + $this->add('hhea', $hhea); + + // maxp - Maximum Profile + $maxp = $this->get_table('maxp'); + $maxp = $this->_set_ushort($maxp, 4, $numGlyphs); + $this->add('maxp', $maxp); + + + // OS/2 - OS/2 + $os2 = $this->get_table('OS/2'); + $this->add('OS/2', $os2 ); + fclose($this->fh); // Put the TTF file together + $stm = ''; + $this->endTTFile($stm); + return $stm ; + } + + ////////////////////////////////////////////////////////////////////////////////// + // Recursively get composite glyph data + function getGlyphData($originalGlyphIdx, &$maxdepth, &$depth, &$points, &$contours) { + $depth++; + $maxdepth = max($maxdepth, $depth); + if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) { + foreach($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { + $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); + } + } + else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) { // simple + $contours += $this->glyphdata[$originalGlyphIdx]['nContours']; + $points += $this->glyphdata[$originalGlyphIdx]['nPoints']; + } + $depth--; + } + + + ////////////////////////////////////////////////////////////////////////////////// + // Recursively get composite glyphs + function getGlyphs($originalGlyphIdx, &$start, &$glyphSet, &$subsetglyphs) { + $glyphPos = $this->glyphPos[$originalGlyphIdx]; + $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; + if (!$glyphLen) { + return; + } + $this->seek($start + $glyphPos); + $numberOfContours = $this->read_short(); + if ($numberOfContours < 0) { + $this->skip(8); + $flags = GF_MORE; + while ($flags & GF_MORE) { + $flags = $this->read_ushort(); + $glyphIdx = $this->read_ushort(); + if (!isset($glyphSet[$glyphIdx])) { + $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID + $subsetglyphs[$glyphIdx] = true; + } + $savepos = ftell($this->fh); + $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs); + $this->seek($savepos); + if ($flags & GF_WORDS) + $this->skip(4); + else + $this->skip(2); + if ($flags & GF_SCALE) + $this->skip(2); + else if ($flags & GF_XYSCALE) + $this->skip(4); + else if ($flags & GF_TWOBYTWO) + $this->skip(8); + } + } + } + + ////////////////////////////////////////////////////////////////////////////////// + + function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { + $start = $this->seek_table("hmtx"); + $aw = 0; + $this->charWidths = str_pad('', 256*256*2, "\x00"); + $nCharWidths = 0; + if (($numberOfHMetrics*4) < $this->maxStrLenRead) { + $data = $this->get_chunk($start,($numberOfHMetrics*4)); + $arr = unpack("n*", $data); + } + else { $this->seek($start); } + for( $glyph=0; $glyph<$numberOfHMetrics; $glyph++) { + + if (($numberOfHMetrics*4) < $this->maxStrLenRead) { + $aw = $arr[($glyph*2)+1]; + } + else { + $aw = $this->read_ushort(); + $lsb = $this->read_ushort(); + } + if (isset($glyphToChar[$glyph]) || $glyph == 0) { + + if ($aw >= (1 << 15) ) { $aw = 0; } // 1.03 Some (arabic) fonts have -ve values for width + // although should be unsigned value - comes out as e.g. 65108 (intended -50) + if ($glyph == 0) { + $this->defaultWidth = $scale*$aw; + continue; + } + foreach($glyphToChar[$glyph] AS $char) { + if ($char != 0 && $char != 65535) { + $w = intval(round($scale*$aw)); + if ($w == 0) { $w = 65535; } + if ($char < 196608) { + $this->charWidths[$char*2] = chr($w >> 8); + $this->charWidths[$char*2 + 1] = chr($w & 0xFF); + $nCharWidths++; + } + } + } + } + } + $data = $this->get_chunk(($start+$numberOfHMetrics*4),($numGlyphs*2)); + $arr = unpack("n*", $data); + $diff = $numGlyphs-$numberOfHMetrics; + for( $pos=0; $pos<$diff; $pos++) { + $glyph = $pos + $numberOfHMetrics; + if (isset($glyphToChar[$glyph])) { + foreach($glyphToChar[$glyph] AS $char) { + if ($char != 0 && $char != 65535) { + $w = intval(round($scale*$aw)); + if ($w == 0) { $w = 65535; } + if ($char < 196608) { + $this->charWidths[$char*2] = chr($w >> 8); + $this->charWidths[$char*2 + 1] = chr($w & 0xFF); + $nCharWidths++; + } + } + } + } + } + // NB 65535 is a set width of 0 + // First bytes define number of chars in font + $this->charWidths[0] = chr($nCharWidths >> 8); + $this->charWidths[1] = chr($nCharWidths & 0xFF); + } + + function getHMetric($numberOfHMetrics, $gid) { + $start = $this->seek_table("hmtx"); + if ($gid < $numberOfHMetrics) { + $this->seek($start+($gid*4)); + $hm = fread($this->fh,4); + } + else { + $this->seek($start+(($numberOfHMetrics-1)*4)); + $hm = fread($this->fh,2); + $this->seek($start+($numberOfHMetrics*2)+($gid*2)); + $hm .= fread($this->fh,2); + } + return $hm; + } + + function getLOCA($indexToLocFormat, $numGlyphs) { + $start = $this->seek_table('loca'); + $this->glyphPos = array(); + if ($indexToLocFormat == 0) { + $data = $this->get_chunk($start,($numGlyphs*2)+2); + $arr = unpack("n*", $data); + for ($n=0; $n<=$numGlyphs; $n++) { + $this->glyphPos[] = ($arr[$n+1] * 2); + } + } + else if ($indexToLocFormat == 1) { + $data = $this->get_chunk($start,($numGlyphs*4)+4); + $arr = unpack("N*", $data); + for ($n=0; $n<=$numGlyphs; $n++) { + $this->glyphPos[] = ($arr[$n+1]); + } + } + else + die('Unknown location table format '.$indexToLocFormat); + } + + + // CMAP Format 4 + function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { + $this->maxUniChar = 0; + $this->seek($unicode_cmap_offset + 2); + $length = $this->read_ushort(); + $limit = $unicode_cmap_offset + $length; + $this->skip(2); + + $segCount = $this->read_ushort() / 2; + $this->skip(6); + $endCount = array(); + for($i=0; $i<$segCount; $i++) { $endCount[] = $this->read_ushort(); } + $this->skip(2); + $startCount = array(); + for($i=0; $i<$segCount; $i++) { $startCount[] = $this->read_ushort(); } + $idDelta = array(); + for($i=0; $i<$segCount; $i++) { $idDelta[] = $this->read_short(); } // ???? was unsigned short + $idRangeOffset_start = $this->_pos; + $idRangeOffset = array(); + for($i=0; $i<$segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } + + for ($n=0;$n<$segCount;$n++) { + $endpoint = ($endCount[$n] + 1); + for ($unichar=$startCount[$n];$unichar<$endpoint;$unichar++) { + if ($idRangeOffset[$n] == 0) + $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; + else { + $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; + $offset = $idRangeOffset_start + 2 * $n + $offset; + if ($offset >= $limit) + $glyph = 0; + else { + $glyph = $this->get_ushort($offset); + if ($glyph != 0) + $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; + } + } + $charToGlyph[$unichar] = $glyph; + if ($unichar < 196608) { $this->maxUniChar = max($unichar,$this->maxUniChar); } + $glyphToChar[$glyph][] = $unichar; + } + } + } + + + // Put the TTF file together + function endTTFile(&$stm) { $stm = ''; $numTables = count($this->otables); $searchRange = 1; @@ -829,11 +1045,16 @@ var $maxStrLenRead; $rangeShift = $numTables * 16 - $searchRange; // Header - $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // 0x74727565 "true" for Mac - // 0x00010000 for Windows + if (_TTF_MAC_HEADER) { + $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac + } + else { + $stm .= (pack("Nnnnn", 0x00010000 , $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows + } // Table directory $tables = $this->otables; + ksort ($tables); $offset = 12 + $numTables * 16; foreach ($tables AS $tag=>$data) { @@ -860,6 +1081,8 @@ var $maxStrLenRead; } + + } diff --git a/include/tfpdf/font/zapfdingbats.php b/include/tfpdf/font/zapfdingbats.php index 1f926a8c3..afef4d32c 100644 --- a/include/tfpdf/font/zapfdingbats.php +++ b/include/tfpdf/font/zapfdingbats.php @@ -1,5 +1,9 @@ 0,chr(1)=>0,chr(2)=>0,chr(3)=>0,chr(4)=>0,chr(5)=>0,chr(6)=>0,chr(7)=>0,chr(8)=>0,chr(9)=>0,chr(10)=>0,chr(11)=>0,chr(12)=>0,chr(13)=>0,chr(14)=>0,chr(15)=>0,chr(16)=>0,chr(17)=>0,chr(18)=>0,chr(19)=>0,chr(20)=>0,chr(21)=>0, chr(22)=>0,chr(23)=>0,chr(24)=>0,chr(25)=>0,chr(26)=>0,chr(27)=>0,chr(28)=>0,chr(29)=>0,chr(30)=>0,chr(31)=>0,' '=>278,'!'=>974,'"'=>961,'#'=>974,'$'=>980,'%'=>719,'&'=>789,'\''=>790,'('=>791,')'=>690,'*'=>960,'+'=>939, ','=>549,'-'=>855,'.'=>911,'/'=>933,'0'=>911,'1'=>945,'2'=>974,'3'=>755,'4'=>846,'5'=>762,'6'=>761,'7'=>571,'8'=>677,'9'=>763,':'=>760,';'=>759,'<'=>754,'='=>494,'>'=>552,'?'=>537,'@'=>577,'A'=>692, diff --git a/include/tfpdf/tfpdf.php b/include/tfpdf/tfpdf.php index 2184613e6..e1727f017 100644 --- a/include/tfpdf/tfpdf.php +++ b/include/tfpdf/tfpdf.php @@ -1,515 +1,515 @@ * * License: LGPL * *******************************************************************************/ -define('tFPDF_VERSION','1.01'); +define('tFPDF_VERSION','1.24'); class tFPDF { var $unifontSubset; -var $extraFontSubsets = 0; -var $t1asm; - -var $page; //current page number -var $n; //current object number -var $offsets; //array of object offsets -var $buffer; //buffer holding in-memory PDF -var $pages; //array containing pages -var $state; //current document state -var $compress; //compression flag -var $k; //scale factor (number of points in user unit) -var $DefOrientation; //default orientation -var $CurOrientation; //current orientation -var $PageFormats; //available page formats -var $DefPageFormat; //default page format -var $CurPageFormat; //current page format -var $PageSizes; //array storing non-default page sizes -var $wPt,$hPt; //dimensions of current page in points -var $w,$h; //dimensions of current page in user unit -var $lMargin; //left margin -var $tMargin; //top margin -var $rMargin; //right margin -var $bMargin; //page break margin -var $cMargin; //cell margin -var $x,$y; //current position in user unit -var $lasth; //height of last printed cell -var $LineWidth; //line width in user unit -var $CoreFonts; //array of standard font names -var $fonts; //array of used fonts -var $FontFiles; //array of font files -var $diffs; //array of encoding differences -var $FontFamily; //current font family -var $FontStyle; //current font style -var $underline; //underlining flag -var $CurrentFont; //current font info -var $FontSizePt; //current font size in points -var $FontSize; //current font size in user unit -var $DrawColor; //commands for drawing color -var $FillColor; //commands for filling color -var $TextColor; //commands for text color -var $ColorFlag; //indicates whether fill and text colors are different -var $ws; //word spacing -var $images; //array of used images -var $PageLinks; //array of links in pages -var $links; //array of internal links -var $AutoPageBreak; //automatic page breaking -var $PageBreakTrigger; //threshold used to trigger page breaks -var $InHeader; //flag set when processing header -var $InFooter; //flag set when processing footer -var $ZoomMode; //zoom display mode -var $LayoutMode; //layout display mode -var $title; //title -var $subject; //subject -var $author; //author -var $keywords; //keywords -var $creator; //creator -var $AliasNbPages; //alias for total number of pages -var $PDFVersion; //PDF version number +var $page; // current page number +var $n; // current object number +var $offsets; // array of object offsets +var $buffer; // buffer holding in-memory PDF +var $pages; // array containing pages +var $state; // current document state +var $compress; // compression flag +var $k; // scale factor (number of points in user unit) +var $DefOrientation; // default orientation +var $CurOrientation; // current orientation +var $StdPageSizes; // standard page sizes +var $DefPageSize; // default page size +var $CurPageSize; // current page size +var $PageSizes; // used for pages with non default sizes or orientations +var $wPt, $hPt; // dimensions of current page in points +var $w, $h; // dimensions of current page in user unit +var $lMargin; // left margin +var $tMargin; // top margin +var $rMargin; // right margin +var $bMargin; // page break margin +var $cMargin; // cell margin +var $x, $y; // current position in user unit +var $lasth; // height of last printed cell +var $LineWidth; // line width in user unit +var $fontpath; // path containing fonts +var $CoreFonts; // array of core font names +var $fonts; // array of used fonts +var $FontFiles; // array of font files +var $diffs; // array of encoding differences +var $FontFamily; // current font family +var $FontStyle; // current font style +var $underline; // underlining flag +var $CurrentFont; // current font info +var $FontSizePt; // current font size in points +var $FontSize; // current font size in user unit +var $DrawColor; // commands for drawing color +var $FillColor; // commands for filling color +var $TextColor; // commands for text color +var $ColorFlag; // indicates whether fill and text colors are different +var $ws; // word spacing +var $images; // array of used images +var $PageLinks; // array of links in pages +var $links; // array of internal links +var $AutoPageBreak; // automatic page breaking +var $PageBreakTrigger; // threshold used to trigger page breaks +var $InHeader; // flag set when processing header +var $InFooter; // flag set when processing footer +var $ZoomMode; // zoom display mode +var $LayoutMode; // layout display mode +var $title; // title +var $subject; // subject +var $author; // author +var $keywords; // keywords +var $creator; // creator +var $AliasNbPages; // alias for total number of pages +var $PDFVersion; // PDF version number /******************************************************************************* * * * Public methods * * * *******************************************************************************/ -function tFPDF($orientation='P', $unit='mm', $format='A4') +function tFPDF($orientation='P', $unit='mm', $size='A4') { - //Some checks + // Some checks $this->_dochecks(); - //Initialization of properties - $this->page=0; - $this->n=2; - $this->buffer=''; - $this->pages=array(); - $this->PageSizes=array(); - $this->state=0; - $this->fonts=array(); - $this->FontFiles=array(); - $this->diffs=array(); - $this->images=array(); - $this->links=array(); - $this->InHeader=false; - $this->InFooter=false; - $this->lasth=0; - $this->FontFamily=''; - $this->FontStyle=''; - $this->FontSizePt=12; - $this->underline=false; - $this->DrawColor='0 G'; - $this->FillColor='0 g'; - $this->TextColor='0 g'; - $this->ColorFlag=false; - $this->ws=0; - //Standard fonts - $this->CoreFonts=array('courier'=>'Courier', 'courierB'=>'Courier-Bold', 'courierI'=>'Courier-Oblique', 'courierBI'=>'Courier-BoldOblique', - 'helvetica'=>'Helvetica', 'helveticaB'=>'Helvetica-Bold', 'helveticaI'=>'Helvetica-Oblique', 'helveticaBI'=>'Helvetica-BoldOblique', - 'times'=>'Times-Roman', 'timesB'=>'Times-Bold', 'timesI'=>'Times-Italic', 'timesBI'=>'Times-BoldItalic', - 'symbol'=>'Symbol', 'zapfdingbats'=>'ZapfDingbats'); - //Scale factor + // Initialization of properties + $this->page = 0; + $this->n = 2; + $this->buffer = ''; + $this->pages = array(); + $this->PageSizes = array(); + $this->state = 0; + $this->fonts = array(); + $this->FontFiles = array(); + $this->diffs = array(); + $this->images = array(); + $this->links = array(); + $this->InHeader = false; + $this->InFooter = false; + $this->lasth = 0; + $this->FontFamily = ''; + $this->FontStyle = ''; + $this->FontSizePt = 12; + $this->underline = false; + $this->DrawColor = '0 G'; + $this->FillColor = '0 g'; + $this->TextColor = '0 g'; + $this->ColorFlag = false; + $this->ws = 0; + // Font path + if(defined('FPDF_FONTPATH')) + { + $this->fontpath = FPDF_FONTPATH; + if(substr($this->fontpath,-1)!='/' && substr($this->fontpath,-1)!='\\') + $this->fontpath .= '/'; + } + elseif(is_dir(dirname(__FILE__).'/font')) + $this->fontpath = dirname(__FILE__).'/font/'; + else + $this->fontpath = ''; + // Core fonts + $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats'); + // Scale factor if($unit=='pt') - $this->k=1; + $this->k = 1; elseif($unit=='mm') - $this->k=72/25.4; + $this->k = 72/25.4; elseif($unit=='cm') - $this->k=72/2.54; + $this->k = 72/2.54; elseif($unit=='in') - $this->k=72; + $this->k = 72; else $this->Error('Incorrect unit: '.$unit); - - //Page format - $this->PageFormats=array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28), + // Page sizes + $this->StdPageSizes = array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28), 'letter'=>array(612,792), 'legal'=>array(612,1008)); - if(is_string($format)) - $format=$this->_getpageformat($format); - $this->DefPageFormat=$format; - $this->CurPageFormat=$format; - //Page orientation - $orientation=strtolower($orientation); + $size = $this->_getpagesize($size); + $this->DefPageSize = $size; + $this->CurPageSize = $size; + // Page orientation + $orientation = strtolower($orientation); if($orientation=='p' || $orientation=='portrait') { - $this->DefOrientation='P'; - $this->w=$this->DefPageFormat[0]; - $this->h=$this->DefPageFormat[1]; + $this->DefOrientation = 'P'; + $this->w = $size[0]; + $this->h = $size[1]; } elseif($orientation=='l' || $orientation=='landscape') { - $this->DefOrientation='L'; - $this->w=$this->DefPageFormat[1]; - $this->h=$this->DefPageFormat[0]; + $this->DefOrientation = 'L'; + $this->w = $size[1]; + $this->h = $size[0]; } else $this->Error('Incorrect orientation: '.$orientation); - $this->CurOrientation=$this->DefOrientation; - $this->wPt=$this->w*$this->k; - $this->hPt=$this->h*$this->k; - //Page margins (1 cm) - $margin=28.35/$this->k; + $this->CurOrientation = $this->DefOrientation; + $this->wPt = $this->w*$this->k; + $this->hPt = $this->h*$this->k; + // Page margins (1 cm) + $margin = 28.35/$this->k; $this->SetMargins($margin,$margin); - //Interior cell margin (1 mm) - $this->cMargin=$margin/10; - //Line width (0.2 mm) - $this->LineWidth=.567/$this->k; - //Automatic page break + // Interior cell margin (1 mm) + $this->cMargin = $margin/10; + // Line width (0.2 mm) + $this->LineWidth = .567/$this->k; + // Automatic page break $this->SetAutoPageBreak(true,2*$margin); - //Full width display mode - $this->SetDisplayMode('fullwidth'); - //Enable compression + // Default display mode + $this->SetDisplayMode('default'); + // Enable compression $this->SetCompression(true); - //Set default PDF version number - $this->PDFVersion='1.3'; + // Set default PDF version number + $this->PDFVersion = '1.3'; } function SetMargins($left, $top, $right=null) { - //Set left, top and right margins - $this->lMargin=$left; - $this->tMargin=$top; + // Set left, top and right margins + $this->lMargin = $left; + $this->tMargin = $top; if($right===null) - $right=$left; - $this->rMargin=$right; + $right = $left; + $this->rMargin = $right; } function SetLeftMargin($margin) { - //Set left margin - $this->lMargin=$margin; + // Set left margin + $this->lMargin = $margin; if($this->page>0 && $this->x<$margin) - $this->x=$margin; + $this->x = $margin; } function SetTopMargin($margin) { - //Set top margin - $this->tMargin=$margin; + // Set top margin + $this->tMargin = $margin; } function SetRightMargin($margin) { - //Set right margin - $this->rMargin=$margin; + // Set right margin + $this->rMargin = $margin; } function SetAutoPageBreak($auto, $margin=0) { - //Set auto page break mode and triggering margin - $this->AutoPageBreak=$auto; - $this->bMargin=$margin; - $this->PageBreakTrigger=$this->h-$margin; + // Set auto page break mode and triggering margin + $this->AutoPageBreak = $auto; + $this->bMargin = $margin; + $this->PageBreakTrigger = $this->h-$margin; } -function SetDisplayMode($zoom, $layout='continuous') +function SetDisplayMode($zoom, $layout='default') { - //Set display mode in viewer + // Set display mode in viewer if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) - $this->ZoomMode=$zoom; + $this->ZoomMode = $zoom; else $this->Error('Incorrect zoom display mode: '.$zoom); if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') - $this->LayoutMode=$layout; + $this->LayoutMode = $layout; else $this->Error('Incorrect layout display mode: '.$layout); } function SetCompression($compress) { - //Set page compression + // Set page compression if(function_exists('gzcompress')) - $this->compress=$compress; + $this->compress = $compress; else - $this->compress=false; + $this->compress = false; } function SetTitle($title, $isUTF8=false) { - //Title of document + // Title of document if($isUTF8) - $title=$this->_UTF8toUTF16($title); - $this->title=$title; + $title = $this->_UTF8toUTF16($title); + $this->title = $title; } function SetSubject($subject, $isUTF8=false) { - //Subject of document + // Subject of document if($isUTF8) - $subject=$this->_UTF8toUTF16($subject); - $this->subject=$subject; + $subject = $this->_UTF8toUTF16($subject); + $this->subject = $subject; } function SetAuthor($author, $isUTF8=false) { - //Author of document + // Author of document if($isUTF8) - $author=$this->_UTF8toUTF16($author); - $this->author=$author; + $author = $this->_UTF8toUTF16($author); + $this->author = $author; } function SetKeywords($keywords, $isUTF8=false) { - //Keywords of document + // Keywords of document if($isUTF8) - $keywords=$this->_UTF8toUTF16($keywords); - $this->keywords=$keywords; + $keywords = $this->_UTF8toUTF16($keywords); + $this->keywords = $keywords; } function SetCreator($creator, $isUTF8=false) { - //Creator of document + // Creator of document if($isUTF8) - $creator=$this->_UTF8toUTF16($creator); - $this->creator=$creator; + $creator = $this->_UTF8toUTF16($creator); + $this->creator = $creator; } function AliasNbPages($alias='{nb}') { - //Define an alias for total number of pages - $this->AliasNbPages=$alias; + // Define an alias for total number of pages + $this->AliasNbPages = $alias; } function Error($msg) { - //Fatal error - die('tFPDF error: '.$msg); + // Fatal error + die('FPDF error: '.$msg); } function Open() { - //Begin document - $this->state=1; + // Begin document + $this->state = 1; } function Close() { - //Terminate document + // Terminate document if($this->state==3) return; if($this->page==0) $this->AddPage(); - //Page footer - $this->InFooter=true; + // Page footer + $this->InFooter = true; $this->Footer(); - $this->InFooter=false; - //Close page + $this->InFooter = false; + // Close page $this->_endpage(); - //Close document + // Close document $this->_enddoc(); } -function AddPage($orientation='', $format='') +function AddPage($orientation='', $size='') { - //Start a new page + // Start a new page if($this->state==0) $this->Open(); - $family=$this->FontFamily; - $style=$this->FontStyle.($this->underline ? 'U' : ''); - $size=$this->FontSizePt; - $lw=$this->LineWidth; - $dc=$this->DrawColor; - $fc=$this->FillColor; - $tc=$this->TextColor; - $cf=$this->ColorFlag; + $family = $this->FontFamily; + $style = $this->FontStyle.($this->underline ? 'U' : ''); + $fontsize = $this->FontSizePt; + $lw = $this->LineWidth; + $dc = $this->DrawColor; + $fc = $this->FillColor; + $tc = $this->TextColor; + $cf = $this->ColorFlag; if($this->page>0) { - //Page footer - $this->InFooter=true; + // Page footer + $this->InFooter = true; $this->Footer(); - $this->InFooter=false; - //Close page + $this->InFooter = false; + // Close page $this->_endpage(); } - //Start new page - $this->_beginpage($orientation,$format); - //Set line cap style to square + // Start new page + $this->_beginpage($orientation,$size); + // Set line cap style to square $this->_out('2 J'); - //Set line width - $this->LineWidth=$lw; + // Set line width + $this->LineWidth = $lw; $this->_out(sprintf('%.2F w',$lw*$this->k)); - //Set font + // Set font if($family) - $this->SetFont($family,$style,$size); - //Set colors - $this->DrawColor=$dc; + $this->SetFont($family,$style,$fontsize); + // Set colors + $this->DrawColor = $dc; if($dc!='0 G') $this->_out($dc); - $this->FillColor=$fc; + $this->FillColor = $fc; if($fc!='0 g') $this->_out($fc); - $this->TextColor=$tc; - $this->ColorFlag=$cf; - //Page header - $this->InHeader=true; + $this->TextColor = $tc; + $this->ColorFlag = $cf; + // Page header + $this->InHeader = true; $this->Header(); - $this->InHeader=false; - //Restore line width + $this->InHeader = false; + // Restore line width if($this->LineWidth!=$lw) { - $this->LineWidth=$lw; + $this->LineWidth = $lw; $this->_out(sprintf('%.2F w',$lw*$this->k)); } - //Restore font + // Restore font if($family) - $this->SetFont($family,$style,$size); - //Restore colors + $this->SetFont($family,$style,$fontsize); + // Restore colors if($this->DrawColor!=$dc) { - $this->DrawColor=$dc; + $this->DrawColor = $dc; $this->_out($dc); } if($this->FillColor!=$fc) { - $this->FillColor=$fc; + $this->FillColor = $fc; $this->_out($fc); } - $this->TextColor=$tc; - $this->ColorFlag=$cf; + $this->TextColor = $tc; + $this->ColorFlag = $cf; } function Header() { - //To be implemented in your own inherited class + // To be implemented in your own inherited class } function Footer() { - //To be implemented in your own inherited class + // To be implemented in your own inherited class } function PageNo() { - //Get current page number + // Get current page number return $this->page; } function SetDrawColor($r, $g=null, $b=null) { - //Set color for all stroking operations + // Set color for all stroking operations if(($r==0 && $g==0 && $b==0) || $g===null) - $this->DrawColor=sprintf('%.3F G',$r/255); + $this->DrawColor = sprintf('%.3F G',$r/255); else - $this->DrawColor=sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255); + $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255); if($this->page>0) $this->_out($this->DrawColor); } function SetFillColor($r, $g=null, $b=null) { - //Set color for all filling operations + // Set color for all filling operations if(($r==0 && $g==0 && $b==0) || $g===null) - $this->FillColor=sprintf('%.3F g',$r/255); + $this->FillColor = sprintf('%.3F g',$r/255); else - $this->FillColor=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); - $this->ColorFlag=($this->FillColor!=$this->TextColor); + $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); + $this->ColorFlag = ($this->FillColor!=$this->TextColor); if($this->page>0) $this->_out($this->FillColor); } function SetTextColor($r, $g=null, $b=null) { - //Set color for text + // Set color for text if(($r==0 && $g==0 && $b==0) || $g===null) - $this->TextColor=sprintf('%.3F g',$r/255); + $this->TextColor = sprintf('%.3F g',$r/255); else - $this->TextColor=sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); - $this->ColorFlag=($this->FillColor!=$this->TextColor); + $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); + $this->ColorFlag = ($this->FillColor!=$this->TextColor); } function GetStringWidth($s) { - //Get width of a string in the current font - $s=(string)$s; - $cw=&$this->CurrentFont['cw']; + // Get width of a string in the current font + $s = (string)$s; + $cw = &$this->CurrentFont['cw']; $w=0; if ($this->unifontSubset) { $unicode = $this->UTF8StringToArray($s); foreach($unicode as $char) { - if (isset($cw[$char])) { $w+=$cw[$char]; } - else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w+=$cw[chr($char)]; } + if (isset($cw[$char])) { $w += (ord($cw[2*$char])<<8) + ord($cw[2*$char+1]); } + else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w += $cw[chr($char)]; } else if(isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; } else if(isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; } else { $w += 500; } } } else { - $l=strlen($s); + $l = strlen($s); for($i=0;$i<$l;$i++) - $w+=$cw[$s[$i]]; + $w += $cw[$s[$i]]; } return $w*$this->FontSize/1000; } function SetLineWidth($width) { - //Set line width - $this->LineWidth=$width; + // Set line width + $this->LineWidth = $width; if($this->page>0) $this->_out(sprintf('%.2F w',$width*$this->k)); } function Line($x1, $y1, $x2, $y2) { - //Draw a line + // Draw a line $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)); } function Rect($x, $y, $w, $h, $style='') { - //Draw a rectangle + // Draw a rectangle if($style=='F') - $op='f'; + $op = 'f'; elseif($style=='FD' || $style=='DF') - $op='B'; + $op = 'B'; else - $op='S'; + $op = 'S'; $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op)); } - function AddFont($family, $style='', $file='', $uni=false) { - //Add a TrueType or Type1 font - $family=strtolower($family); - if($file=='') { - if ($uni) { - $file=str_replace(' ','',$family).strtolower($style).'.ttf'; - } - else { - $file=str_replace(' ','',$family).strtolower($style).'.php'; - } - } - if($family=='arial') - $family='helvetica'; - $style=strtoupper($style); + // Add a TrueType, OpenType or Type1 font + $family = strtolower($family); + $style = strtoupper($style); if($style=='IB') $style='BI'; - $fontkey=$family.$style; + if($file=='') { + if ($uni) { + $file = str_replace(' ','',$family).strtolower($style).'.ttf'; + } + else { + $file = str_replace(' ','',$family).strtolower($style).'.php'; + } + } + $fontkey = $family.$style; if(isset($this->fonts[$fontkey])) return; if ($uni) { - if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttfilename = _SYSTEM_TTFONTS.$file ; } - else { $ttfilename = $this->_getfontpath().'unifont/'.$file ; } - $filename = $file; - $filename =str_replace(' ','',$filename ); - $filename =str_replace('-','',$filename ); - $unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($filename ,0,(strpos($filename ,'.')))); - $diff = ''; - $enc = ''; + if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; } + else { $ttffilename = $this->_getfontpath().'unifont/'.$file ; } + $unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($file ,0,(strpos($file ,'.')))); + $name = ''; + $originalsize = 0; + $ttfstat = stat($ttffilename); if (file_exists($unifilename.'.mtx.php')) { include($unifilename.'.mtx.php'); } - if (!isset($type) || $type != "TrueTypesubset") { - include_once($this->_getfontpath().'unifont/ttfonts.php'); + if (!isset($type) || !isset($name) || $originalsize != $ttfstat['size']) { + $ttffile = $ttffilename; + require_once($this->_getfontpath().'unifont/ttfonts.php'); $ttf = new TTFontFile(); - $ttf->getMetrics($ttfilename); + $ttf->getMetrics($ttffile); $cw = $ttf->charWidths; - $type = "TrueTypesubset"; - $name = preg_replace('/ /','',$ttf->fullName); + $name = preg_replace('/[ ()]/','',$ttf->fullName); + $desc= array('Ascent'=>round($ttf->ascent), 'Descent'=>round($ttf->descent), 'CapHeight'=>round($ttf->capHeight), @@ -520,128 +520,117 @@ function AddFont($family, $style='', $file='', $uni=false) 'MissingWidth'=>round($ttf->defaultWidth)); $up = round($ttf->underlinePosition); $ut = round($ttf->underlineThickness); - //Generate metrics .php file + $originalsize = $ttfstat['size']+0; + $type = 'TTF'; + // Generate metrics .php file $s='\n"; - if (is_writable($this->_getfontpath().'unifont')) { + $s.='$ttffile=\''.$ttffile."';\n"; + $s.='$originalsize='.$originalsize.";\n"; + $s.='$fontkey=\''.$fontkey."';\n"; + $s.="?>"; + if (is_writable(dirname($this->_getfontpath().'unifont/'.'x'))) { $fh = fopen($unifilename.'.mtx.php',"w"); fwrite($fh,$s,strlen($s)); fclose($fh); + $fh = fopen($unifilename.'.cw.dat',"wb"); + fwrite($fh,$cw,strlen($cw)); + fclose($fh); + @unlink($unifilename.'.cw127.php'); } unset($ttf); } - if(!isset($name)) { - $this->Error('Problem with the font definition file'); + else { + $cw = @file_get_contents($unifilename.'.cw.dat'); } - $i = count($this->fonts)+$this->extraFontSubsets+1; - $sbarr = range(0,127); - $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$ttfilename, 'subsets'=>array(0=>$sbarr), 'subsetfontids'=>array($i), 'used'=>false); + $i = count($this->fonts)+1; + if(!empty($this->AliasNbPages)) + $sbarr = range(0,57); + else + $sbarr = range(0,32); + $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); + + $this->FontFiles[$fontkey]=array('length1'=>$originalsize, 'type'=>"TTF", 'ttffile'=>$ttffile); + $this->FontFiles[$file]=array('type'=>"TTF"); unset($cw); } else { - include($this->_getfontpath().$file); - if(!isset($name)) - $this->Error('Could not include font definition file'); - $i=count($this->fonts)+$this->extraFontSubsets+1; - $this->fonts[$fontkey]=array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file); - } - - if($diff) - { - //Search existing encodings - $d=0; - $nb=count($this->diffs); - for($i=1;$i<=$nb;$i++) + $info = $this->_loadfont($file); + $info['i'] = count($this->fonts)+1; + if(!empty($info['diff'])) { - if($this->diffs[$i]==$diff) + // Search existing encodings + $n = array_search($info['diff'],$this->diffs); + if(!$n) { - $d=$i; - break; + $n = count($this->diffs)+1; + $this->diffs[$n] = $info['diff']; } + $info['diffn'] = $n; } - if($d==0) + if(!empty($info['file'])) { - $d=$nb+1; - $this->diffs[$d]=$diff; + // Embedded font + if($info['type']=='TrueType') + $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']); + else + $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']); } - $this->fonts[$fontkey]['diff']=$d; - } - if($file) - { - if($type=='TrueType') - $this->FontFiles[$file]=array('length1'=>$originalsize); - else if ($uni && $type == "TrueTypesubset") - $this->FontFiles[$file]=array('type'=>"TrueTypesubset"); - else - $this->FontFiles[$file]=array('length1'=>$size1, 'length2'=>$size2); + $this->fonts[$fontkey] = $info; } } function SetFont($family, $style='', $size=0) { - //Select a font; size given in points - global $fpdf_charwidths; - - $family=strtolower($family); + // Select a font; size given in points if($family=='') - $family=$this->FontFamily; - if($family=='arial') - $family='helvetica'; - elseif($family=='symbol' || $family=='zapfdingbats') - $style=''; - $style=strtoupper($style); + $family = $this->FontFamily; + else + $family = strtolower($family); + $style = strtoupper($style); if(strpos($style,'U')!==false) { - $this->underline=true; - $style=str_replace('U','',$style); + $this->underline = true; + $style = str_replace('U','',$style); } else - $this->underline=false; + $this->underline = false; if($style=='IB') - $style='BI'; + $style = 'BI'; if($size==0) - $size=$this->FontSizePt; - //Test if font is already selected + $size = $this->FontSizePt; + // Test if font is already selected if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size) return; - //Test if used for the first time - $fontkey=$family.$style; + // Test if font is already loaded + $fontkey = $family.$style; if(!isset($this->fonts[$fontkey])) { - //Check if one of the standard fonts - if(isset($this->CoreFonts[$fontkey])) + // Test if one of the core fonts + if($family=='arial') + $family = 'helvetica'; + if(in_array($family,$this->CoreFonts)) { - if(!isset($fpdf_charwidths[$fontkey])) - { - //Load metric file - $file=$family; - if($family=='times' || $family=='helvetica') - $file.=strtolower($style); - include($this->_getfontpath().$file.'.php'); - if(!isset($fpdf_charwidths[$fontkey])) - $this->Error('Could not include font metric file'); - } - $i=count($this->fonts)+1+$this->extraFontSubsets; - $name=$this->CoreFonts[$fontkey]; - $cw=$fpdf_charwidths[$fontkey]; - $this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$name, 'up'=>-100, 'ut'=>50, 'cw'=>$cw); + if($family=='symbol' || $family=='zapfdingbats') + $style = ''; + $fontkey = $family.$style; + if(!isset($this->fonts[$fontkey])) + $this->AddFont($family,$style); } else $this->Error('Undefined font: '.$family.' '.$style); } - //Select it - $this->FontFamily=$family; - $this->FontStyle=$style; - $this->FontSizePt=$size; - $this->FontSize=$size/$this->k; - $this->CurrentFont=&$this->fonts[$fontkey]; - if ($this->fonts[$fontkey]['type']=='TrueTypesubset') { $this->unifontSubset = true; } + // Select it + $this->FontFamily = $family; + $this->FontStyle = $style; + $this->FontSizePt = $size; + $this->FontSize = $size/$this->k; + $this->CurrentFont = &$this->fonts[$fontkey]; + if ($this->fonts[$fontkey]['type']=='TTF') { $this->unifontSubset = true; } else { $this->unifontSubset = false; } if($this->page>0) $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); @@ -649,199 +638,230 @@ function SetFont($family, $style='', $size=0) function SetFontSize($size) { - //Set font size in points + // Set font size in points if($this->FontSizePt==$size) return; - $this->FontSizePt=$size; - $this->FontSize=$size/$this->k; + $this->FontSizePt = $size; + $this->FontSize = $size/$this->k; if($this->page>0) $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); } function AddLink() { - //Create a new internal link - $n=count($this->links)+1; - $this->links[$n]=array(0, 0); + // Create a new internal link + $n = count($this->links)+1; + $this->links[$n] = array(0, 0); return $n; } function SetLink($link, $y=0, $page=-1) { - //Set destination of internal link + // Set destination of internal link if($y==-1) - $y=$this->y; + $y = $this->y; if($page==-1) - $page=$this->page; - $this->links[$link]=array($page, $y); + $page = $this->page; + $this->links[$link] = array($page, $y); } function Link($x, $y, $w, $h, $link) { - //Put a link on the page - $this->PageLinks[$this->page][]=array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link); + // Put a link on the page + $this->PageLinks[$this->page][] = array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link); } function Text($x, $y, $txt) { - //Output a string + // Output a string if ($this->unifontSubset) - $txt2 = $this->UTF8toSubset($txt); + { + $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')'; + foreach($this->UTF8StringToArray($txt) as $uni) + $this->CurrentFont['subset'][$uni] = $uni; + } else - $txt2='('.$this->_escape($txt).')'; - $s=sprintf('BT %.2F %.2F Td %s Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt2); + $txt2 = '('.$this->_escape($txt).')'; + $s = sprintf('BT %.2F %.2F Td %s Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt2); if($this->underline && $txt!='') - $s.=' '.$this->_dounderline($x,$y,$txt); + $s .= ' '.$this->_dounderline($x,$y,$txt); if($this->ColorFlag) - $s='q '.$this->TextColor.' '.$s.' Q'; + $s = 'q '.$this->TextColor.' '.$s.' Q'; $this->_out($s); } function AcceptPageBreak() { - //Accept automatic page break or not + // Accept automatic page break or not return $this->AutoPageBreak; } function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='') { - //Output a cell - $k=$this->k; + // Output a cell + $k = $this->k; if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) { - //Automatic page break - $x=$this->x; - $ws=$this->ws; + // Automatic page break + $x = $this->x; + $ws = $this->ws; if($ws>0) { - $this->ws=0; + $this->ws = 0; $this->_out('0 Tw'); } - $this->AddPage($this->CurOrientation,$this->CurPageFormat); - $this->x=$x; + $this->AddPage($this->CurOrientation,$this->CurPageSize); + $this->x = $x; if($ws>0) { - $this->ws=$ws; + $this->ws = $ws; $this->_out(sprintf('%.3F Tw',$ws*$k)); } } if($w==0) - $w=$this->w-$this->rMargin-$this->x; - $s=''; + $w = $this->w-$this->rMargin-$this->x; + $s = ''; if($fill || $border==1) { if($fill) - $op=($border==1) ? 'B' : 'f'; + $op = ($border==1) ? 'B' : 'f'; else - $op='S'; - $s=sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op); + $op = 'S'; + $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op); } if(is_string($border)) { - $x=$this->x; - $y=$this->y; + $x = $this->x; + $y = $this->y; if(strpos($border,'L')!==false) - $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k); + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k); if(strpos($border,'T')!==false) - $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k); + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k); if(strpos($border,'R')!==false) - $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k); + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k); if(strpos($border,'B')!==false) - $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k); + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k); } if($txt!=='') { if($align=='R') - $dx=$w-$this->cMargin-$this->GetStringWidth($txt); + $dx = $w-$this->cMargin-$this->GetStringWidth($txt); elseif($align=='C') - $dx=($w-$this->GetStringWidth($txt))/2; + $dx = ($w-$this->GetStringWidth($txt))/2; else - $dx=$this->cMargin; + $dx = $this->cMargin; if($this->ColorFlag) - $s.='q '.$this->TextColor.' '; - if ($this->unifontSubset) - $txt2 = $this->UTF8toSubset($txt); - else - $txt2='('.str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))).')'; - $s.=sprintf('BT %.2F %.2F Td %s Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2); + $s .= 'q '.$this->TextColor.' '; + + // If multibyte, Tw has no effect - do word spacing using an adjustment before each space + if ($this->ws && $this->unifontSubset) { + foreach($this->UTF8StringToArray($txt) as $uni) + $this->CurrentFont['subset'][$uni] = $uni; + $space = $this->_escape($this->UTF8ToUTF16BE(' ', false)); + $s .= sprintf('BT 0 Tw %.2F %.2F Td [',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k); + $t = explode(' ',$txt); + $numt = count($t); + for($i=0;$i<$numt;$i++) { + $tx = $t[$i]; + $tx = '('.$this->_escape($this->UTF8ToUTF16BE($tx, false)).')'; + $s .= sprintf('%s ',$tx); + if (($i+1)<$numt) { + $adj = -($this->ws*$this->k)*1000/$this->FontSizePt; + $s .= sprintf('%d(%s) ',$adj,$space); + } + } + $s .= '] TJ'; + $s .= ' ET'; + } + else { + if ($this->unifontSubset) + { + $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')'; + foreach($this->UTF8StringToArray($txt) as $uni) + $this->CurrentFont['subset'][$uni] = $uni; + } + else + $txt2='('.str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))).')'; + $s .= sprintf('BT %.2F %.2F Td %s Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2); + } if($this->underline) - $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt); + $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt); if($this->ColorFlag) - $s.=' Q'; + $s .= ' Q'; if($link) $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link); } if($s) $this->_out($s); - $this->lasth=$h; + $this->lasth = $h; if($ln>0) { - //Go to next line - $this->y+=$h; + // Go to next line + $this->y += $h; if($ln==1) - $this->x=$this->lMargin; + $this->x = $this->lMargin; } else - $this->x+=$w; + $this->x += $w; } function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) { - //Output text with automatic or explicit line breaks - $cw=&$this->CurrentFont['cw']; + // Output text with automatic or explicit line breaks + $cw = &$this->CurrentFont['cw']; if($w==0) - $w=$this->w-$this->rMargin-$this->x; - $wmax=($w-2*$this->cMargin); - $s=str_replace("\r",'',$txt); + $w = $this->w-$this->rMargin-$this->x; + $wmax = ($w-2*$this->cMargin); + $s = str_replace("\r",'',$txt); if ($this->unifontSubset) { $nb=mb_strlen($s, 'utf-8'); while($nb>0 && mb_substr($s,$nb-1,1,'utf-8')=="\n") $nb--; } else { - $nb=strlen($s); + $nb = strlen($s); if($nb>0 && $s[$nb-1]=="\n") $nb--; } - - $b=0; + $b = 0; if($border) { if($border==1) { - $border='LTRB'; - $b='LRT'; - $b2='LR'; + $border = 'LTRB'; + $b = 'LRT'; + $b2 = 'LR'; } else { - $b2=''; + $b2 = ''; if(strpos($border,'L')!==false) - $b2.='L'; + $b2 .= 'L'; if(strpos($border,'R')!==false) - $b2.='R'; - $b=(strpos($border,'T')!==false) ? $b2.'T' : $b2; + $b2 .= 'R'; + $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2; } } - $sep=-1; - $i=0; - $j=0; - $l=0; - $ns=0; - $nl=1; + $sep = -1; + $i = 0; + $j = 0; + $l = 0; + $ns = 0; + $nl = 1; while($i<$nb) { - //Get next character + // Get next character if ($this->unifontSubset) { $c = mb_substr($s,$i,1,'UTF-8'); } else { $c=$s[$i]; } - if($c=="\n") { - //Explicit line break + if($c=="\n") + { + // Explicit line break if($this->ws>0) { - $this->ws=0; + $this->ws = 0; $this->_out('0 Tw'); } if ($this->unifontSubset) { @@ -851,19 +871,19 @@ function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); } $i++; - $sep=-1; - $j=$i; - $l=0; - $ns=0; + $sep = -1; + $j = $i; + $l = 0; + $ns = 0; $nl++; if($border && $nl==2) - $b=$b2; + $b = $b2; continue; } if($c==' ') { - $sep=$i; - $ls=$l; + $sep = $i; + $ls = $l; $ns++; } @@ -872,14 +892,14 @@ function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) if($l>$wmax) { - //Automatic line break + // Automatic line break if($sep==-1) { if($i==$j) $i++; if($this->ws>0) { - $this->ws=0; + $this->ws = 0; $this->_out('0 Tw'); } if ($this->unifontSubset) { @@ -893,7 +913,7 @@ function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) { if($align=='J') { - $this->ws=($ns>1) ? ($wmax-$ls)/($ns-1) : 0; + $this->ws = ($ns>1) ? ($wmax-$ls)/($ns-1) : 0; $this->_out(sprintf('%.3F Tw',$this->ws*$this->k)); } if ($this->unifontSubset) { @@ -902,72 +922,71 @@ function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) else { $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill); } - $i=$sep+1; + $i = $sep+1; } - $sep=-1; - $j=$i; - $l=0; - $ns=0; + $sep = -1; + $j = $i; + $l = 0; + $ns = 0; $nl++; if($border && $nl==2) - $b=$b2; + $b = $b2; } else $i++; } - //Last chunk + // Last chunk if($this->ws>0) { - $this->ws=0; + $this->ws = 0; $this->_out('0 Tw'); } if($border && strpos($border,'B')!==false) - $b.='B'; + $b .= 'B'; if ($this->unifontSubset) { $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill); } else { $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); } - $this->x=$this->lMargin; + $this->x = $this->lMargin; } function Write($h, $txt, $link='') { - //Output text in flowing mode - $cw=&$this->CurrentFont['cw']; - $w=$this->w-$this->rMargin-$this->x; + // Output text in flowing mode + $cw = &$this->CurrentFont['cw']; + $w = $this->w-$this->rMargin-$this->x; - $wmax=($w-2*$this->cMargin); - $s=str_replace("\r",'',$txt); + $wmax = ($w-2*$this->cMargin); + $s = str_replace("\r",'',$txt); if ($this->unifontSubset) { - $nb=mb_strlen($s, 'UTF-8'); + $nb = mb_strlen($s, 'UTF-8'); if($nb==1 && $s==" ") { $this->x += $this->GetStringWidth($s); return; } } else { - $nb=strlen($s); + $nb = strlen($s); } - - $sep=-1; - $i=0; - $j=0; - $l=0; - $nl=1; + $sep = -1; + $i = 0; + $j = 0; + $l = 0; + $nl = 1; while($i<$nb) { - //Get next character - //Get next character + // Get next character if ($this->unifontSubset) { $c = mb_substr($s,$i,1,'UTF-8'); } else { - $c=$s[$i]; + $c = $s[$i]; } - if($c=="\n") { - //Explicit line break + if($c=="\n") + { + // Explicit line break if ($this->unifontSubset) { $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link); } @@ -975,36 +994,36 @@ function Write($h, $txt, $link='') $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link); } $i++; - $sep=-1; - $j=$i; - $l=0; + $sep = -1; + $j = $i; + $l = 0; if($nl==1) { - $this->x=$this->lMargin; - $w=$this->w-$this->rMargin-$this->x; - $wmax=($w-2*$this->cMargin); + $this->x = $this->lMargin; + $w = $this->w-$this->rMargin-$this->x; + $wmax = ($w-2*$this->cMargin); } $nl++; continue; } if($c==' ') - $sep=$i; + $sep = $i; if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } else { $l += $cw[$c]*$this->FontSize/1000; } if($l>$wmax) { - //Automatic line break + // Automatic line break if($sep==-1) { if($this->x>$this->lMargin) { - //Move to next line - $this->x=$this->lMargin; - $this->y+=$h; - $w=$this->w-$this->rMargin-$this->x; - $wmax=($w-2*$this->cMargin); + // Move to next line + $this->x = $this->lMargin; + $this->y += $h; + $w = $this->w-$this->rMargin-$this->x; + $wmax = ($w-2*$this->cMargin); $i++; $nl++; continue; @@ -1026,23 +1045,23 @@ function Write($h, $txt, $link='') else { $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link); } - $i=$sep+1; + $i = $sep+1; } - $sep=-1; - $j=$i; - $l=0; + $sep = -1; + $j = $i; + $l = 0; if($nl==1) { - $this->x=$this->lMargin; - $w=$this->w-$this->rMargin-$this->x; - $wmax=($w-2*$this->cMargin); + $this->x = $this->lMargin; + $w = $this->w-$this->rMargin-$this->x; + $wmax = ($w-2*$this->cMargin); } $nl++; } else $i++; } - //Last chunk + // Last chunk if($i!=$j) { if ($this->unifontSubset) { $this->Cell($l,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',0,$link); @@ -1055,65 +1074,72 @@ function Write($h, $txt, $link='') function Ln($h=null) { - //Line feed; default value is last cell height - $this->x=$this->lMargin; + // Line feed; default value is last cell height + $this->x = $this->lMargin; if($h===null) - $this->y+=$this->lasth; + $this->y += $this->lasth; else - $this->y+=$h; + $this->y += $h; } function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='') { - //Put an image on the page + // Put an image on the page if(!isset($this->images[$file])) { - //First use of this image, get info + // First use of this image, get info if($type=='') { - $pos=strrpos($file,'.'); + $pos = strrpos($file,'.'); if(!$pos) $this->Error('Image file has no extension and no type was specified: '.$file); - $type=substr($file,$pos+1); + $type = substr($file,$pos+1); } - $type=strtolower($type); + $type = strtolower($type); if($type=='jpeg') - $type='jpg'; - $mtd='_parse'.$type; + $type = 'jpg'; + $mtd = '_parse'.$type; if(!method_exists($this,$mtd)) $this->Error('Unsupported image type: '.$type); - $info=$this->$mtd($file); - $info['i']=count($this->images)+1; - $this->images[$file]=$info; + $info = $this->$mtd($file); + $info['i'] = count($this->images)+1; + $this->images[$file] = $info; } else - $info=$this->images[$file]; - //Automatic width and height calculation if needed + $info = $this->images[$file]; + + // Automatic width and height calculation if needed if($w==0 && $h==0) { - //Put image at 72 dpi - $w=$info['w']/$this->k; - $h=$info['h']/$this->k; + // Put image at 96 dpi + $w = -96; + $h = -96; } - elseif($w==0) - $w=$h*$info['w']/$info['h']; - elseif($h==0) - $h=$w*$info['h']/$info['w']; - //Flowing mode + if($w<0) + $w = -$info['w']*72/$w/$this->k; + if($h<0) + $h = -$info['h']*72/$h/$this->k; + if($w==0) + $w = $h*$info['w']/$info['h']; + if($h==0) + $h = $w*$info['h']/$info['w']; + + // Flowing mode if($y===null) { if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) { - //Automatic page break - $x2=$this->x; - $this->AddPage($this->CurOrientation,$this->CurPageFormat); - $this->x=$x2; + // Automatic page break + $x2 = $this->x; + $this->AddPage($this->CurOrientation,$this->CurPageSize); + $this->x = $x2; } - $y=$this->y; - $this->y+=$h; + $y = $this->y; + $this->y += $h; } + if($x===null) - $x=$this->x; + $x = $this->x; $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'])); if($link) $this->Link($x,$y,$w,$h,$link); @@ -1121,102 +1147,92 @@ function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='') function GetX() { - //Get x position + // Get x position return $this->x; } function SetX($x) { - //Set x position + // Set x position if($x>=0) - $this->x=$x; + $this->x = $x; else - $this->x=$this->w+$x; + $this->x = $this->w+$x; } function GetY() { - //Get y position + // Get y position return $this->y; } function SetY($y) { - //Set y position and reset x - $this->x=$this->lMargin; + // Set y position and reset x + $this->x = $this->lMargin; if($y>=0) - $this->y=$y; + $this->y = $y; else - $this->y=$this->h+$y; + $this->y = $this->h+$y; } function SetXY($x, $y) { - //Set x and y positions + // Set x and y positions $this->SetY($y); $this->SetX($x); } function Output($name='', $dest='') { - //Output PDF to some destination + // Output PDF to some destination if($this->state<3) $this->Close(); - $dest=strtoupper($dest); + $dest = strtoupper($dest); if($dest=='') { if($name=='') { - $name='doc.pdf'; - $dest='I'; + $name = 'doc.pdf'; + $dest = 'I'; } else - $dest='F'; + $dest = 'F'; } switch($dest) { case 'I': - //Send to standard output - if(ob_get_length()) - $this->Error('Some data has already been output, can\'t send PDF file'); - if(php_sapi_name()!='cli') + // Send to standard output + $this->_checkoutput(); + if(PHP_SAPI!='cli') { - //We send to a browser + // We send to a browser header('Content-Type: application/pdf'); - if(headers_sent()) - $this->Error('Some data has already been output, can\'t send PDF file'); - header('Content-Length: '.strlen($this->buffer)); header('Content-Disposition: inline; filename="'.$name.'"'); header('Cache-Control: private, max-age=0, must-revalidate'); header('Pragma: public'); - ini_set('zlib.output_compression','0'); } echo $this->buffer; break; case 'D': - //Download file - if(ob_get_length()) - $this->Error('Some data has already been output, can\'t send PDF file'); + // Download file + $this->_checkoutput(); header('Content-Type: application/x-download'); - if(headers_sent()) - $this->Error('Some data has already been output, can\'t send PDF file'); - header('Content-Length: '.strlen($this->buffer)); header('Content-Disposition: attachment; filename="'.$name.'"'); header('Cache-Control: private, max-age=0, must-revalidate'); header('Pragma: public'); - ini_set('zlib.output_compression','0'); echo $this->buffer; break; case 'F': - //Save to local file - $f=fopen($name,'wb'); + // Save to local file + $f = fopen($name,'wb'); if(!$f) $this->Error('Unable to create output file: '.$name); fwrite($f,$this->buffer,strlen($this->buffer)); fclose($f); break; case 'S': - //Return as a string + // Return as a string return $this->buffer; default: $this->Error('Incorrect output destination: '.$dest); @@ -1231,125 +1247,163 @@ function Output($name='', $dest='') *******************************************************************************/ function _dochecks() { - //Check availability of %F + // Check availability of %F if(sprintf('%.1F',1.0)!='1.0') $this->Error('This version of PHP is not supported'); - //Check mbstring overloading + // Check availability of mbstring + if(!function_exists('mb_strlen')) + $this->Error('mbstring extension is not available'); + // Check mbstring overloading if(ini_get('mbstring.func_overload') & 2) $this->Error('mbstring overloading must be disabled'); - //Disable runtime magic quotes + // Ensure runtime magic quotes are disabled if(get_magic_quotes_runtime()) @set_magic_quotes_runtime(0); } -function _getpageformat($format) -{ - $format=strtolower($format); - if(!isset($this->PageFormats[$format])) - $this->Error('Unknown page format: '.$format); - $a=$this->PageFormats[$format]; - return array($a[0]/$this->k, $a[1]/$this->k); -} - function _getfontpath() { - if(!defined('FPDF_FONTPATH') && is_dir(dirname(__FILE__).'/font')) - define('FPDF_FONTPATH',dirname(__FILE__).'/font/'); - return defined('FPDF_FONTPATH') ? FPDF_FONTPATH : ''; + return $this->fontpath; } -function _beginpage($orientation, $format) +function _checkoutput() +{ + if(PHP_SAPI!='cli') + { + if(headers_sent($file,$line)) + $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)"); + } + if(ob_get_length()) + { + // The output buffer is not empty + if(preg_match('/^(\xEF\xBB\xBF)?\s*$/',ob_get_contents())) + { + // It contains only a UTF-8 BOM and/or whitespace, let's clean it + ob_clean(); + } + else + $this->Error("Some data has already been output, can't send PDF file"); + } +} + +function _getpagesize($size) +{ + if(is_string($size)) + { + $size = strtolower($size); + if(!isset($this->StdPageSizes[$size])) + $this->Error('Unknown page size: '.$size); + $a = $this->StdPageSizes[$size]; + return array($a[0]/$this->k, $a[1]/$this->k); + } + else + { + if($size[0]>$size[1]) + return array($size[1], $size[0]); + else + return $size; + } +} + +function _beginpage($orientation, $size) { $this->page++; - $this->pages[$this->page]=''; - $this->state=2; - $this->x=$this->lMargin; - $this->y=$this->tMargin; - $this->FontFamily=''; - //Check page size + $this->pages[$this->page] = ''; + $this->state = 2; + $this->x = $this->lMargin; + $this->y = $this->tMargin; + $this->FontFamily = ''; + // Check page size and orientation if($orientation=='') - $orientation=$this->DefOrientation; + $orientation = $this->DefOrientation; else - $orientation=strtoupper($orientation[0]); - if($format=='') - $format=$this->DefPageFormat; + $orientation = strtoupper($orientation[0]); + if($size=='') + $size = $this->DefPageSize; else + $size = $this->_getpagesize($size); + if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1]) { - if(is_string($format)) - $format=$this->_getpageformat($format); - } - if($orientation!=$this->CurOrientation || $format[0]!=$this->CurPageFormat[0] || $format[1]!=$this->CurPageFormat[1]) - { - //New size + // New size or orientation if($orientation=='P') { - $this->w=$format[0]; - $this->h=$format[1]; + $this->w = $size[0]; + $this->h = $size[1]; } else { - $this->w=$format[1]; - $this->h=$format[0]; + $this->w = $size[1]; + $this->h = $size[0]; } - $this->wPt=$this->w*$this->k; - $this->hPt=$this->h*$this->k; - $this->PageBreakTrigger=$this->h-$this->bMargin; - $this->CurOrientation=$orientation; - $this->CurPageFormat=$format; + $this->wPt = $this->w*$this->k; + $this->hPt = $this->h*$this->k; + $this->PageBreakTrigger = $this->h-$this->bMargin; + $this->CurOrientation = $orientation; + $this->CurPageSize = $size; } - if($orientation!=$this->DefOrientation || $format[0]!=$this->DefPageFormat[0] || $format[1]!=$this->DefPageFormat[1]) - $this->PageSizes[$this->page]=array($this->wPt, $this->hPt); + if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1]) + $this->PageSizes[$this->page] = array($this->wPt, $this->hPt); } function _endpage() { - $this->state=1; + $this->state = 1; +} + +function _loadfont($font) +{ + // Load a font definition file from the font directory + include($this->fontpath.$font); + $a = get_defined_vars(); + if(!isset($a['name'])) + $this->Error('Could not include font definition file'); + return $a; } function _escape($s) { - //Escape special characters in strings - $s=str_replace('\\','\\\\',$s); - $s=str_replace('(','\\(',$s); - $s=str_replace(')','\\)',$s); - $s=str_replace("\r",'\\r',$s); + // Escape special characters in strings + $s = str_replace('\\','\\\\',$s); + $s = str_replace('(','\\(',$s); + $s = str_replace(')','\\)',$s); + $s = str_replace("\r",'\\r',$s); return $s; } function _textstring($s) { - //Format a text string + // Format a text string return '('.$this->_escape($s).')'; } function _UTF8toUTF16($s) { - //Convert UTF-8 to UTF-16BE with BOM - $res="\xFE\xFF"; - $nb=strlen($s); - $i=0; + // Convert UTF-8 to UTF-16BE with BOM + $res = "\xFE\xFF"; + $nb = strlen($s); + $i = 0; while($i<$nb) { - $c1=ord($s[$i++]); + $c1 = ord($s[$i++]); if($c1>=224) { - //3-byte character - $c2=ord($s[$i++]); - $c3=ord($s[$i++]); - $res.=chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2)); - $res.=chr((($c2 & 0x03)<<6) + ($c3 & 0x3F)); + // 3-byte character + $c2 = ord($s[$i++]); + $c3 = ord($s[$i++]); + $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2)); + $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F)); } elseif($c1>=192) { - //2-byte character - $c2=ord($s[$i++]); - $res.=chr(($c1 & 0x1C)>>2); - $res.=chr((($c1 & 0x03)<<6) + ($c2 & 0x3F)); + // 2-byte character + $c2 = ord($s[$i++]); + $res .= chr(($c1 & 0x1C)>>2); + $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F)); } else { - //Single-byte character - $res.="\0".chr($c1); + // Single-byte character + $res .= "\0".chr($c1); } } return $res; @@ -1357,64 +1411,67 @@ function _UTF8toUTF16($s) function _dounderline($x, $y, $txt) { - //Underline text - $up=$this->CurrentFont['up']; - $ut=$this->CurrentFont['ut']; - $w=$this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '); + // Underline text + $up = $this->CurrentFont['up']; + $ut = $this->CurrentFont['ut']; + $w = $this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '); 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); } function _parsejpg($file) { - //Extract info from a JPEG file - $a=GetImageSize($file); + // Extract info from a JPEG file + $a = getimagesize($file); if(!$a) $this->Error('Missing or incorrect image file: '.$file); if($a[2]!=2) $this->Error('Not a JPEG file: '.$file); if(!isset($a['channels']) || $a['channels']==3) - $colspace='DeviceRGB'; + $colspace = 'DeviceRGB'; elseif($a['channels']==4) - $colspace='DeviceCMYK'; + $colspace = 'DeviceCMYK'; else - $colspace='DeviceGray'; - $bpc=isset($a['bits']) ? $a['bits'] : 8; - //Read whole file - $f=fopen($file,'rb'); - $data=''; - while(!feof($f)) - $data.=fread($f,8192); - fclose($f); + $colspace = 'DeviceGray'; + $bpc = isset($a['bits']) ? $a['bits'] : 8; + $data = file_get_contents($file); return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data); } function _parsepng($file) { - //Extract info from a PNG file - $f=fopen($file,'rb'); + // Extract info from a PNG file + $f = fopen($file,'rb'); if(!$f) $this->Error('Can\'t open image file: '.$file); - //Check signature + $info = $this->_parsepngstream($f,$file); + fclose($f); + return $info; +} + +function _parsepngstream($f, $file) +{ + // Check signature if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) $this->Error('Not a PNG file: '.$file); - //Read header chunk + + // Read header chunk $this->_readstream($f,4); if($this->_readstream($f,4)!='IHDR') $this->Error('Incorrect PNG file: '.$file); - $w=$this->_readint($f); - $h=$this->_readint($f); - $bpc=ord($this->_readstream($f,1)); + $w = $this->_readint($f); + $h = $this->_readint($f); + $bpc = ord($this->_readstream($f,1)); if($bpc>8) $this->Error('16-bit depth not supported: '.$file); - $ct=ord($this->_readstream($f,1)); - if($ct==0) - $colspace='DeviceGray'; - elseif($ct==2) - $colspace='DeviceRGB'; + $ct = ord($this->_readstream($f,1)); + if($ct==0 || $ct==4) + $colspace = 'DeviceGray'; + elseif($ct==2 || $ct==6) + $colspace = 'DeviceRGB'; elseif($ct==3) - $colspace='Indexed'; + $colspace = 'Indexed'; else - $this->Error('Alpha channel not supported: '.$file); + $this->Error('Unknown color type: '.$file); if(ord($this->_readstream($f,1))!=0) $this->Error('Unknown compression method: '.$file); if(ord($this->_readstream($f,1))!=0) @@ -1422,41 +1479,42 @@ function _parsepng($file) if(ord($this->_readstream($f,1))!=0) $this->Error('Interlacing not supported: '.$file); $this->_readstream($f,4); - $parms='/DecodeParms <>'; - //Scan chunks looking for palette, transparency and image data - $pal=''; - $trns=''; - $data=''; + $dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w; + + // Scan chunks looking for palette, transparency and image data + $pal = ''; + $trns = ''; + $data = ''; do { - $n=$this->_readint($f); - $type=$this->_readstream($f,4); + $n = $this->_readint($f); + $type = $this->_readstream($f,4); if($type=='PLTE') { - //Read palette - $pal=$this->_readstream($f,$n); + // Read palette + $pal = $this->_readstream($f,$n); $this->_readstream($f,4); } elseif($type=='tRNS') { - //Read transparency info - $t=$this->_readstream($f,$n); + // Read transparency info + $t = $this->_readstream($f,$n); if($ct==0) - $trns=array(ord(substr($t,1,1))); + $trns = array(ord(substr($t,1,1))); elseif($ct==2) - $trns=array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1))); + $trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1))); else { - $pos=strpos($t,chr(0)); + $pos = strpos($t,chr(0)); if($pos!==false) - $trns=array($pos); + $trns = array($pos); } $this->_readstream($f,4); } elseif($type=='IDAT') { - //Read image data block - $data.=$this->_readstream($f,$n); + // Read image data block + $data .= $this->_readstream($f,$n); $this->_readstream($f,4); } elseif($type=='IEND') @@ -1465,23 +1523,67 @@ function _parsepng($file) $this->_readstream($f,$n+4); } while($n); + if($colspace=='Indexed' && empty($pal)) $this->Error('Missing palette in '.$file); - fclose($f); - return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data); + $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns); + if($ct>=4) + { + // Extract alpha channel + if(!function_exists('gzuncompress')) + $this->Error('Zlib not available, can\'t handle alpha channel: '.$file); + $data = gzuncompress($data); + $color = ''; + $alpha = ''; + if($ct==4) + { + // Gray image + $len = 2*$w; + for($i=0;$i<$h;$i++) + { + $pos = (1+$len)*$i; + $color .= $data[$pos]; + $alpha .= $data[$pos]; + $line = substr($data,$pos+1,$len); + $color .= preg_replace('/(.)./s','$1',$line); + $alpha .= preg_replace('/.(.)/s','$1',$line); + } + } + else + { + // RGB image + $len = 4*$w; + for($i=0;$i<$h;$i++) + { + $pos = (1+$len)*$i; + $color .= $data[$pos]; + $alpha .= $data[$pos]; + $line = substr($data,$pos+1,$len); + $color .= preg_replace('/(.{3})./s','$1',$line); + $alpha .= preg_replace('/.{3}(.)/s','$1',$line); + } + } + unset($data); + $data = gzcompress($color); + $info['smask'] = gzcompress($alpha); + if($this->PDFVersion<'1.4') + $this->PDFVersion = '1.4'; + } + $info['data'] = $data; + return $info; } function _readstream($f, $n) { - //Read n bytes from stream - $res=''; + // Read n bytes from stream + $res = ''; while($n>0 && !feof($f)) { - $s=fread($f,$n); + $s = fread($f,$n); if($s===false) $this->Error('Error while reading stream'); - $n-=strlen($s); - $res.=$s; + $n -= strlen($s); + $res .= $s; } if($n>0) $this->Error('Unexpected end of stream'); @@ -1490,38 +1592,55 @@ function _readstream($f, $n) function _readint($f) { - //Read a 4-byte integer from stream - $a=unpack('Ni',$this->_readstream($f,4)); + // Read a 4-byte integer from stream + $a = unpack('Ni',$this->_readstream($f,4)); return $a['i']; } function _parsegif($file) { - //Extract info from a GIF file (via PNG conversion) + // Extract info from a GIF file (via PNG conversion) if(!function_exists('imagepng')) $this->Error('GD extension is required for GIF support'); if(!function_exists('imagecreatefromgif')) $this->Error('GD has no GIF read support'); - $im=imagecreatefromgif($file); + $im = imagecreatefromgif($file); if(!$im) $this->Error('Missing or incorrect image file: '.$file); imageinterlace($im,0); - $tmp=tempnam('.','gif'); - if(!$tmp) - $this->Error('Unable to create a temporary file'); - if(!imagepng($im,$tmp)) - $this->Error('Error while saving to temporary file'); - imagedestroy($im); - $info=$this->_parsepng($tmp); - unlink($tmp); + $f = @fopen('php://temp','rb+'); + if($f) + { + // Perform conversion in memory + ob_start(); + imagepng($im); + $data = ob_get_clean(); + imagedestroy($im); + fwrite($f,$data); + rewind($f); + $info = $this->_parsepngstream($f,$file); + fclose($f); + } + else + { + // Use temporary file + $tmp = tempnam('.','gif'); + if(!$tmp) + $this->Error('Unable to create a temporary file'); + if(!imagepng($im,$tmp)) + $this->Error('Error while saving to temporary file'); + imagedestroy($im); + $info = $this->_parsepng($tmp); + unlink($tmp); + } return $info; } function _newobj() { - //Begin a new object + // Begin a new object $this->n++; - $this->offsets[$this->n]=strlen($this->buffer); + $this->offsets[$this->n] = strlen($this->buffer); $this->_out($this->n.' 0 obj'); } @@ -1534,46 +1653,41 @@ function _putstream($s) function _out($s) { - //Add a line to the document + // Add a line to the document if($this->state==2) - $this->pages[$this->page].=$s."\n"; + $this->pages[$this->page] .= $s."\n"; else - $this->buffer.=$s."\n"; + $this->buffer .= $s."\n"; } function _putpages() { - $nb=$this->page; + $nb = $this->page; if(!empty($this->AliasNbPages)) { - //Replace number of pages in fonts using subsets - $r = ''; - $nstr = "$nb"; - for($i=0;$iUTF8ToUTF16BE($this->AliasNbPages, false); + $r = $this->UTF8ToUTF16BE("$nb", false); for($n=1;$n<=$nb;$n++) - $this->pages[$n]=str_replace('`'.$this->AliasNbPages.'`',$r,$this->pages[$n]); + $this->pages[$n] = str_replace($alias,$r,$this->pages[$n]); // Now repeat for no pages in non-subset fonts - $r = $nb; - //Replace number of pages for($n=1;$n<=$nb;$n++) - $this->pages[$n]=str_replace($this->AliasNbPages,$r,$this->pages[$n]); + $this->pages[$n] = str_replace($this->AliasNbPages,$nb,$this->pages[$n]); } if($this->DefOrientation=='P') { - $wPt=$this->DefPageFormat[0]*$this->k; - $hPt=$this->DefPageFormat[1]*$this->k; + $wPt = $this->DefPageSize[0]*$this->k; + $hPt = $this->DefPageSize[1]*$this->k; } else { - $wPt=$this->DefPageFormat[1]*$this->k; - $hPt=$this->DefPageFormat[0]*$this->k; + $wPt = $this->DefPageSize[1]*$this->k; + $hPt = $this->DefPageSize[0]*$this->k; } - $filter=($this->compress) ? '/Filter /FlateDecode ' : ''; + $filter = ($this->compress) ? '/Filter /FlateDecode ' : ''; for($n=1;$n<=$nb;$n++) { - //Page + // Page $this->_newobj(); $this->_out('<_out('/Parent 1 0 R'); @@ -1582,39 +1696,41 @@ function _putpages() $this->_out('/Resources 2 0 R'); if(isset($this->PageLinks[$n])) { - //Links - $annots='/Annots ['; + // Links + $annots = '/Annots ['; foreach($this->PageLinks[$n] as $pl) { - $rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); - $annots.='<_textstring($pl[4]).'>>>>'; + $annots .= '/A <_textstring($pl[4]).'>>>>'; else { - $l=$this->links[$pl[4]]; - $h=isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt; - $annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k); + $l = $this->links[$pl[4]]; + $h = isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt; + $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k); } } $this->_out($annots.']'); } + if($this->PDFVersion>'1.3') + $this->_out('/Group <>'); $this->_out('/Contents '.($this->n+1).' 0 R>>'); $this->_out('endobj'); - //Page content - $p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n]; + // Page content + $p = ($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n]; $this->_newobj(); $this->_out('<<'.$filter.'/Length '.strlen($p).'>>'); $this->_putstream($p); $this->_out('endobj'); } - //Pages root - $this->offsets[1]=strlen($this->buffer); + // Pages root + $this->offsets[1] = strlen($this->buffer); $this->_out('1 0 obj'); $this->_out('<_out($kids.']'); $this->_out('/Count '.$nb); $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); @@ -1627,15 +1743,15 @@ function _putfonts() $nf=$this->n; foreach($this->diffs as $diff) { - //Encodings + // Encodings $this->_newobj(); $this->_out('<>'); $this->_out('endobj'); } foreach($this->FontFiles as $file=>$info) { - if (!isset($info['type']) || $info['type']!='TrueTypesubset') { - //Font file embedding + if (!isset($info['type']) || $info['type']!='TTF') { + // Font file embedding $this->_newobj(); $this->FontFiles[$file]['n']=$this->n; $font=''; @@ -1651,12 +1767,12 @@ function _putfonts() $header=(ord($font[0])==128); if($header) { - //Strip first binary header + // Strip first binary header $font=substr($font,6); } if($header && ord($font[$info['length1']])==128) { - //Strip second binary header + // Strip second binary header $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6); } } @@ -1673,13 +1789,13 @@ function _putfonts() } foreach($this->fonts as $k=>$font) { - //Font objects + // Font objects //$this->fonts[$k]['n']=$this->n+1; - $type=$font['type']; - $name=$font['name']; - if($type=='core') + $type = $font['type']; + $name = $font['name']; + if($type=='Core') { - //Standard font + // Standard font $this->fonts[$k]['n']=$this->n+1; $this->_newobj(); $this->_out('<fonts[$k]['n']=$this->n+1; $this->_newobj(); $this->_out('<_out('>>'); $this->_out('endobj'); - //Widths + // Widths $this->_newobj(); $cw=&$font['cw']; $s='['; @@ -1718,7 +1834,7 @@ function _putfonts() $s.=$cw[chr($i)].' '; $this->_out($s.']'); $this->_out('endobj'); - //Descriptor + // Descriptor $this->_newobj(); $s='<$v) @@ -1729,65 +1845,53 @@ function _putfonts() $this->_out($s.'>>'); $this->_out('endobj'); } - // TrueType embedded SUBSETS - else if ($type=='TrueTypesubset') { - $ssfaid="A"; - include_once($this->_getfontpath().'unifont/ttfonts.php'); - $ttf = new TTFontFile(); - $ttf->getMetrics($font['file'], 1); - for($sfid=0;$sfidfonts[$k]['n'][$sfid]=$this->n+1; // NB an array for subset - $subsetname = 'MPDFA'.$ssfaid.'+'.$font['name']; - $ssfaid++; - $subset = $font['subsets'][$sfid]; + // TrueType embedded SUBSETS or FULL + else if ($type=='TTF') { + $this->fonts[$k]['n']=$this->n+1; + require_once($this->_getfontpath().'unifont/ttfonts.php'); + $ttf = new TTFontFile(); + $fontname = 'MPDFAA'.'+'.$font['name']; + $subset = $font['subset']; unset($subset[0]); - $ttfontstream = $ttf->makeSubset($subset); + $ttfontstream = $ttf->makeSubset($font['ttffile'], $subset); $ttfontsize = strlen($ttfontstream); $fontstream = gzcompress($ttfontstream); - $widthstring = ''; - $toUnistring = ''; - foreach($font['subsets'][$sfid] AS $cp=>$u) { - if (isset($font['cw'][$u])) { - $widthstring .= $font['cw'][$u].' '; - } - else { - $widthstring .= $ttf->defaultWidth.' '; - } - $toUnistring .= sprintf("<%02s> <%04s>\n", strtoupper(dechex($cp)), strtoupper(dechex($u))); - } + $codeToGlyph = $ttf->codeToGlyph; + unset($codeToGlyph[0]); - //Additional Type1 or TrueType font + // Type0 Font + // A composite font - a font composed of other fonts, organized hierarchically $this->_newobj(); $this->_out('<_out('/BaseFont /'.$subsetname); - $this->_out('/Subtype /TrueType'); - $this->_out('/FirstChar 0 /LastChar '.(count($font['subsets'][$sfid]))); - $this->_out('/Widths '.($this->n+1).' 0 R'); - $this->_out('/FontDescriptor '.($this->n+2).' 0 R'); - $this->_out('/ToUnicode '.($this->n + 3).' 0 R'); + $this->_out('/Subtype /Type0'); + $this->_out('/BaseFont /'.$fontname.''); + $this->_out('/Encoding /Identity-H'); + $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]'); + $this->_out('/ToUnicode '.($this->n + 2).' 0 R'); $this->_out('>>'); $this->_out('endobj'); - //Widths + // CIDFontType2 + // A CIDFont whose glyph descriptions are based on TrueType font technology $this->_newobj(); - $this->_out('['.$widthstring.']'); - $this->_out('endobj'); - - //Descriptor - $this->_newobj(); - $s='<$v) { - if ($kd == 'Flags') { $v = $v | 4; $v = $v & ~32; } - $s.=' /'.$kd.' '.$v."\n"; + $this->_out('<_out('/Subtype /CIDFontType2'); + $this->_out('/BaseFont /'.$fontname.''); + $this->_out('/CIDSystemInfo '.($this->n + 2).' 0 R'); + $this->_out('/FontDescriptor '.($this->n + 3).' 0 R'); + if (isset($font['desc']['MissingWidth'])){ + $this->_out('/DW '.$font['desc']['MissingWidth'].''); } - $s.='/FontFile2 '.($this->n + 2).' 0 R'; - $this->_out($s.'>>'); + $this->_putTTfontwidths($font, $ttf->maxUni); + + $this->_out('/CIDToGIDMap '.($this->n + 4).' 0 R'); + $this->_out('>>'); $this->_out('endobj'); // ToUnicode - $toUni = "stream\n"; - $toUni .= "/CIDInit /ProcSet findresource begin\n"; + $this->_newobj(); + $toUni = "/CIDInit /ProcSet findresource begin\n"; $toUni .= "12 dict begin\n"; $toUni .= "begincmap\n"; $toUni .= "/CIDSystemInfo\n"; @@ -1798,20 +1902,54 @@ function _putfonts() $toUni .= "/CMapName /Adobe-Identity-UCS def\n"; $toUni .= "/CMapType 2 def\n"; $toUni .= "1 begincodespacerange\n"; - $toUni .= "<00> \n"; + $toUni .= "<0000> \n"; $toUni .= "endcodespacerange\n"; - $toUni .= count($font['subsets'][$sfid])." beginbfchar\n"; - $toUni .= $toUnistring; - $toUni .= "endbfchar\n"; + $toUni .= "1 beginbfrange\n"; + $toUni .= "<0000> <0000>\n"; + $toUni .= "endbfrange\n"; $toUni .= "endcmap\n"; $toUni .= "CMapName currentdict /CMap defineresource pop\n"; $toUni .= "end\n"; - $toUni .= "end\n"; - $toUni .= "endstream\n"; - $toUni .= "endobj"; + $toUni .= "end"; + $this->_out('<>'); + $this->_putstream($toUni); + $this->_out('endobj'); + + // CIDSystemInfo dictionary $this->_newobj(); - $this->_out('<>'); - $this->_out($toUni); + $this->_out('<_out('/Ordering (UCS)'); + $this->_out('/Supplement 0'); + $this->_out('>>'); + $this->_out('endobj'); + + // Font descriptor + $this->_newobj(); + $this->_out('<_out('/FontName /'.$fontname); + foreach($font['desc'] as $kd=>$v) { + if ($kd == 'Flags') { $v = $v | 4; $v = $v & ~32; } // SYMBOLIC font flag + $this->_out(' /'.$kd.' '.$v); + } + $this->_out('/FontFile2 '.($this->n + 2).' 0 R'); + $this->_out('>>'); + $this->_out('endobj'); + + // Embed CIDToGIDMap + // A specification of the mapping from CIDs to glyph indices + $cidtogidmap = ''; + $cidtogidmap = str_pad('', 256*256*2, "\x00"); + foreach($codeToGlyph as $cc=>$glyph) { + $cidtogidmap[$cc*2] = chr($glyph >> 8); + $cidtogidmap[$cc*2 + 1] = chr($glyph & 0xFF); + } + $cidtogidmap = gzcompress($cidtogidmap); + $this->_newobj(); + $this->_out('<_out('/Filter /FlateDecode'); + $this->_out('>>'); + $this->_putstream($cidtogidmap); + $this->_out('endobj'); //Font file $this->_newobj(); @@ -1821,13 +1959,12 @@ function _putfonts() $this->_out('>>'); $this->_putstream($fontstream); $this->_out('endobj'); - } - unset($ttf); + unset($ttf); } else { - //Allow for additional types - $this->fonts[$k]['n']=$this->n+1; + // Allow for additional types + $this->fonts[$k]['n'] = $this->n+1; $mtd='_put'.strtolower($type); if(!method_exists($this,$mtd)) $this->Error('Unsupported font type: '.$type); @@ -1836,51 +1973,165 @@ function _putfonts() } } +function _putTTfontwidths(&$font, $maxUni) { + if (file_exists($font['unifilename'].'.cw127.php')) { + include($font['unifilename'].'.cw127.php') ; + $startcid = 128; + } + else { + $rangeid = 0; + $range = array(); + $prevcid = -2; + $prevwidth = -1; + $interval = false; + $startcid = 1; + } + $cwlen = $maxUni + 1; + + // for each character + for ($cid=$startcid; $cid<$cwlen; $cid++) { + if ($cid==128 && (!file_exists($font['unifilename'].'.cw127.php'))) { + if (is_writable(dirname($this->_getfontpath().'unifont/x'))) { + $fh = fopen($font['unifilename'].'.cw127.php',"wb"); + $cw127='"; + fwrite($fh,$cw127,strlen($cw127)); + fclose($fh); + } + } + if ($font['cw'][$cid*2] == "\00" && $font['cw'][$cid*2+1] == "\00") { continue; } + $width = (ord($font['cw'][$cid*2]) << 8) + ord($font['cw'][$cid*2+1]); + if ($width == 65535) { $width = 0; } + if ($cid > 255 && (!isset($font['subset'][$cid]) || !$font['subset'][$cid])) { continue; } + if (!isset($font['dw']) || (isset($font['dw']) && $width != $font['dw'])) { + if ($cid == ($prevcid + 1)) { + if ($width == $prevwidth) { + if ($width == $range[$rangeid][0]) { + $range[$rangeid][] = $width; + } + else { + array_pop($range[$rangeid]); + // new range + $rangeid = $prevcid; + $range[$rangeid] = array(); + $range[$rangeid][] = $prevwidth; + $range[$rangeid][] = $width; + } + $interval = true; + $range[$rangeid]['interval'] = true; + } else { + if ($interval) { + // new range + $rangeid = $cid; + $range[$rangeid] = array(); + $range[$rangeid][] = $width; + } + else { $range[$rangeid][] = $width; } + $interval = false; + } + } else { + $rangeid = $cid; + $range[$rangeid] = array(); + $range[$rangeid][] = $width; + $interval = false; + } + $prevcid = $cid; + $prevwidth = $width; + } + } + $prevk = -1; + $nextk = -1; + $prevint = false; + foreach ($range as $k => $ws) { + $cws = count($ws); + if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) { + if (isset($range[$k]['interval'])) { unset($range[$k]['interval']); } + $range[$prevk] = array_merge($range[$prevk], $range[$k]); + unset($range[$k]); + } + else { $prevk = $k; } + $nextk = $k + $cws; + if (isset($ws['interval'])) { + if ($cws > 3) { $prevint = true; } + else { $prevint = false; } + unset($range[$k]['interval']); + --$nextk; + } + else { $prevint = false; } + } + $w = ''; + foreach ($range as $k => $ws) { + if (count(array_count_values($ws)) == 1) { $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0]; } + else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]' . "\n"; } + } + $this->_out('/W ['.$w.' ]'); +} + function _putimages() { - $filter=($this->compress) ? '/Filter /FlateDecode ' : ''; - reset($this->images); - while(list($file,$info)=each($this->images)) + foreach(array_keys($this->images) as $file) { - $this->_newobj(); - $this->images[$file]['n']=$this->n; - $this->_out('<_out('/Subtype /Image'); - $this->_out('/Width '.$info['w']); - $this->_out('/Height '.$info['h']); - if($info['cs']=='Indexed') - $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]'); - else - { - $this->_out('/ColorSpace /'.$info['cs']); - if($info['cs']=='DeviceCMYK') - $this->_out('/Decode [1 0 1 0 1 0 1 0]'); - } - $this->_out('/BitsPerComponent '.$info['bpc']); - if(isset($info['f'])) - $this->_out('/Filter /'.$info['f']); - if(isset($info['parms'])) - $this->_out($info['parms']); - if(isset($info['trns']) && is_array($info['trns'])) - { - $trns=''; - for($i=0;$i_out('/Mask ['.$trns.']'); - } - $this->_out('/Length '.strlen($info['data']).'>>'); - $this->_putstream($info['data']); + $this->_putimage($this->images[$file]); unset($this->images[$file]['data']); + unset($this->images[$file]['smask']); + } +} + +function _putimage(&$info) +{ + $this->_newobj(); + $info['n'] = $this->n; + $this->_out('<_out('/Subtype /Image'); + $this->_out('/Width '.$info['w']); + $this->_out('/Height '.$info['h']); + if($info['cs']=='Indexed') + $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]'); + else + { + $this->_out('/ColorSpace /'.$info['cs']); + if($info['cs']=='DeviceCMYK') + $this->_out('/Decode [1 0 1 0 1 0 1 0]'); + } + $this->_out('/BitsPerComponent '.$info['bpc']); + if(isset($info['f'])) + $this->_out('/Filter /'.$info['f']); + if(isset($info['dp'])) + $this->_out('/DecodeParms <<'.$info['dp'].'>>'); + if(isset($info['trns']) && is_array($info['trns'])) + { + $trns = ''; + for($i=0;$i_out('/Mask ['.$trns.']'); + } + if(isset($info['smask'])) + $this->_out('/SMask '.($this->n+1).' 0 R'); + $this->_out('/Length '.strlen($info['data']).'>>'); + $this->_putstream($info['data']); + $this->_out('endobj'); + // Soft mask + if(isset($info['smask'])) + { + $dp = '/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns '.$info['w']; + $smask = array('w'=>$info['w'], 'h'=>$info['h'], 'cs'=>'DeviceGray', 'bpc'=>8, 'f'=>$info['f'], 'dp'=>$dp, 'data'=>$info['smask']); + $this->_putimage($smask); + } + // Palette + if($info['cs']=='Indexed') + { + $filter = ($this->compress) ? '/Filter /FlateDecode ' : ''; + $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal']; + $this->_newobj(); + $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>'); + $this->_putstream($pal); $this->_out('endobj'); - //Palette - if($info['cs']=='Indexed') - { - $this->_newobj(); - $pal=($this->compress) ? gzcompress($info['pal']) : $info['pal']; - $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>'); - $this->_putstream($pal); - $this->_out('endobj'); - } } } @@ -1895,14 +2146,7 @@ function _putresourcedict() $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); $this->_out('/Font <<'); foreach($this->fonts as $font) { - if ($font['type']=='TrueTypesubset') { - foreach($font['n'] AS $k => $fid) { - $this->_out('/F'.$font['subsetfontids'][$k].' '.$font['n'][$k].' 0 R'); - } - } - else { - $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R'); - } + $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R'); } $this->_out('>>'); $this->_out('/XObject <<'); @@ -1914,8 +2158,8 @@ function _putresources() { $this->_putfonts(); $this->_putimages(); - //Resource dictionary - $this->offsets[2]=strlen($this->buffer); + // Resource dictionary + $this->offsets[2] = strlen($this->buffer); $this->_out('2 0 obj'); $this->_out('<<'); $this->_putresourcedict(); @@ -1950,7 +2194,7 @@ function _putcatalog() elseif($this->ZoomMode=='real') $this->_out('/OpenAction [3 0 R /XYZ null null 1]'); elseif(!is_string($this->ZoomMode)) - $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode/100).']'); + $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']'); if($this->LayoutMode=='single') $this->_out('/PageLayout /SinglePage'); elseif($this->LayoutMode=='continuous') @@ -1976,26 +2220,26 @@ function _enddoc() $this->_putheader(); $this->_putpages(); $this->_putresources(); - //Info + // Info $this->_newobj(); $this->_out('<<'); $this->_putinfo(); $this->_out('>>'); $this->_out('endobj'); - //Catalog + // Catalog $this->_newobj(); $this->_out('<<'); $this->_putcatalog(); $this->_out('>>'); $this->_out('endobj'); - //Cross-ref - $o=strlen($this->buffer); + // Cross-ref + $o = strlen($this->buffer); $this->_out('xref'); $this->_out('0 '.($this->n+1)); $this->_out('0000000000 65535 f '); for($i=1;$i<=$this->n;$i++) $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i])); - //Trailer + // Trailer $this->_out('trailer'); $this->_out('<<'); $this->_puttrailer(); @@ -2003,62 +2247,18 @@ function _enddoc() $this->_out('startxref'); $this->_out($o); $this->_out('%%EOF'); - $this->state=3; + $this->state = 3; } // ********* NEW FUNCTIONS ********* - -// Convert utf-8 string to for Font Subsets -function UTF8toSubset($str) { - $ret = '<'; - if ($this->AliasNbPages) - $str = preg_replace('/'.preg_quote($this->AliasNbPages,'/').'/', chr(7), $str ); - $unicode = $this->UTF8StringToArray($str); - $orig_fid = $this->CurrentFont['subsetfontids'][0]; - $last_fid = $this->CurrentFont['subsetfontids'][0]; - foreach($unicode as $c) { - if ($c == 7) { - if ($orig_fid != $last_fid) { - $ret .= '> Tj /F'.$orig_fid.' '.$this->FontSizePt.' Tf <'; - $last_fid = $orig_fid; - } - $ret .= '`'.$this->AliasNbPages.'`'; - continue; - } - for ($i=0; $i<99; $i++) { - // return c as decimal char - $init = array_search($c, $this->CurrentFont['subsets'][$i]); - if ($init!==false) { - if ($this->CurrentFont['subsetfontids'][$i] != $last_fid) { - $ret .= '> Tj /F'.$this->CurrentFont['subsetfontids'][$i].' '.$this->FontSizePt.' Tf <'; - $last_fid = $this->CurrentFont['subsetfontids'][$i]; - } - $ret .= sprintf("%02s", strtoupper(dechex($init))); - break; - } - else if (count($this->CurrentFont['subsets'][$i]) < 255) { - $n = count($this->CurrentFont['subsets'][$i]); - $this->CurrentFont['subsets'][$i][$n] = $c; - if ($this->CurrentFont['subsetfontids'][$i] != $last_fid) { - $ret .= '> Tj /F'.$this->CurrentFont['subsetfontids'][$i].' '.$this->FontSizePt.' Tf <'; - $last_fid = $this->CurrentFont['subsetfontids'][$i]; - } - $ret .= sprintf("%02s", strtoupper(dechex($n))); - break; - } - else if (!isset($this->CurrentFont['subsets'][($i+1)])) { - $this->CurrentFont['subsets'][($i+1)] = array(0=>0); - $new_fid = count($this->fonts)+$this->extraFontSubsets+1; - $this->CurrentFont['subsetfontids'][($i+1)] = $new_fid; - $this->extraFontSubsets++; - } - } +// Converts UTF-8 strings to UTF16-BE. +function UTF8ToUTF16BE($str, $setbom=true) { + $outstr = ""; + if ($setbom) { + $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM) } - $ret .= '>'; - if ($last_fid != $orig_fid) { - $ret .= ' Tj /F'.$orig_fid.' '.$this->FontSizePt.' Tf <> '; - } - return $ret; + $outstr .= mb_convert_encoding($str, 'UTF-16BE', 'UTF-8'); + return $outstr; } // Converts UTF-8 strings to codepoints array @@ -2066,33 +2266,37 @@ function UTF8StringToArray($str) { $out = array(); $len = strlen($str); for ($i = 0; $i < $len; $i++) { + $uni = -1; $h = ord($str[$i]); if ( $h <= 0x7F ) - $out[] = $h; + $uni = $h; elseif ( $h >= 0xC2 ) { if ( ($h <= 0xDF) && ($i < $len -1) ) - $out[] = ($h & 0x1F) << 6 | (ord($str{++$i}) & 0x3F); + $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F); elseif ( ($h <= 0xEF) && ($i < $len -2) ) - $out[] = ($h & 0x0F) << 12 | (ord($str{++$i}) & 0x3F) << 6 - | (ord($str{++$i}) & 0x3F); + $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6 + | (ord($str[++$i]) & 0x3F); elseif ( ($h <= 0xF4) && ($i < $len -3) ) - $out[] = ($h & 0x0F) << 18 | (ord($str{++$i}) & 0x3F) << 12 - | (ord($str{++$i}) & 0x3F) << 6 - | (ord($str{++$i}) & 0x3F); + $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12 + | (ord($str[++$i]) & 0x3F) << 6 + | (ord($str[++$i]) & 0x3F); } + if ($uni >= 0) { + $out[] = $uni; + } } return $out; } -//End of class +// End of class } -//Handle special IE contype request +// Handle special IE contype request if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype') { header('Content-Type: application/pdf'); exit; } -?> \ No newline at end of file +?>