@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | * @param mixed $value The value this instance represents |
| 50 | 50 | * @param boolean $strict Not Implemented at this time |
| 51 | 51 | * |
| 52 | - * @return void |
|
| 52 | + * @return Strength |
|
| 53 | 53 | * @throws UnexpectedValueException If the value is not a constant |
| 54 | 54 | */ |
| 55 | 55 | public function __construct($value = null, $strict = false) { |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | /** |
| 70 | 70 | * Cast the current object to a string and return its value |
| 71 | 71 | * |
| 72 | - * @return mixed the current value of the instance |
|
| 72 | + * @return string the current value of the instance |
|
| 73 | 73 | */ |
| 74 | 74 | public function __toString() { |
| 75 | 75 | return (string) $this->value; |
@@ -60,7 +60,7 @@ |
||
| 60 | 60 | $this->name = array_search($value, $validValues); |
| 61 | 61 | if (!$this->name) { |
| 62 | 62 | throw new \UnexpectedValueException( |
| 63 | - 'Value not a const in enum ' . get_class($this) |
|
| 63 | + 'Value not a const in enum '.get_class($this) |
|
| 64 | 64 | ); |
| 65 | 65 | } |
| 66 | 66 | $this->value = $value; |
@@ -35,10 +35,9 @@ |
||
| 35 | 35 | /** |
| 36 | 36 | * Derive a key from the supplied arguments |
| 37 | 37 | * |
| 38 | - * @param string $password The password to derive from |
|
| 39 | 38 | * @param string $salt The salt string to use |
| 40 | 39 | * @param int $iterations The number of iterations to use |
| 41 | - * @param int $length The size of the string to generate |
|
| 40 | + * @param integer $klen |
|
| 42 | 41 | * |
| 43 | 42 | * @return string The derived key |
| 44 | 43 | */ |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | * Build a new instance |
| 78 | 78 | * |
| 79 | 79 | * @param array $options An array of options for the password isntance |
| 80 | - * @param Generator $generator The random generator to use for seeds |
|
| 80 | + * @param \PasswordLib\Random\Generator $generator The random generator to use for seeds |
|
| 81 | 81 | * |
| 82 | 82 | * @return void |
| 83 | 83 | */ |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | /** |
| 125 | 125 | * Set the random number generator to use |
| 126 | 126 | * |
| 127 | - * @param Generator $generator The random generator to use for seeds |
|
| 127 | + * @param \PasswordLib\Random\Generator $generator The random generator to use for seeds |
|
| 128 | 128 | * |
| 129 | 129 | * @return void |
| 130 | 130 | */ |
@@ -74,6 +74,9 @@ discard block |
||
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | |
| 77 | + /** |
|
| 78 | + * @param string $file |
|
| 79 | + */ |
|
| 77 | 80 | function getMetrics($file) { |
| 78 | 81 | $this->filename = $file; |
| 79 | 82 | $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); |
@@ -116,6 +119,10 @@ discard block |
||
| 116 | 119 | } |
| 117 | 120 | |
| 118 | 121 | |
| 122 | + /** |
|
| 123 | + * @param integer[] $x |
|
| 124 | + * @param integer[] $y |
|
| 125 | + */ |
|
| 119 | 126 | function sub32($x, $y) { |
| 120 | 127 | $xlo = $x[1]; |
| 121 | 128 | $xhi = $x[0]; |
@@ -154,11 +161,17 @@ discard block |
||
| 154 | 161 | fseek($this->fh,$this->_pos); |
| 155 | 162 | } |
| 156 | 163 | |
| 164 | + /** |
|
| 165 | + * @param integer $delta |
|
| 166 | + */ |
|
| 157 | 167 | function skip($delta) { |
| 158 | 168 | $this->_pos = $this->_pos + $delta; |
| 159 | 169 | fseek($this->fh,$this->_pos); |
| 160 | 170 | } |
| 161 | 171 | |
| 172 | + /** |
|
| 173 | + * @param string $tag |
|
| 174 | + */ |
|
| 162 | 175 | function seek_table($tag, $offset_in_table = 0) { |
| 163 | 176 | $tpos = $this->get_table_pos($tag); |
| 164 | 177 | $this->_pos = $tpos[0] + $offset_in_table; |
@@ -222,10 +235,17 @@ discard block |
||
| 222 | 235 | return pack("n",$val); |
| 223 | 236 | } |
| 224 | 237 | |
| 238 | + /** |
|
| 239 | + * @param string $value |
|
| 240 | + */ |
|
| 225 | 241 | function splice($stream, $offset, $value) { |
| 226 | 242 | return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); |
| 227 | 243 | } |
| 228 | 244 | |
| 245 | + /** |
|
| 246 | + * @param string|null $stream |
|
| 247 | + * @param integer $offset |
|
| 248 | + */ |
|
| 229 | 249 | function _set_ushort($stream, $offset, $value) { |
| 230 | 250 | $up = pack("n", $value); |
| 231 | 251 | return $this->splice($stream, $offset, $up); |
@@ -247,6 +267,9 @@ discard block |
||
| 247 | 267 | return (fread($this->fh,$length)); |
| 248 | 268 | } |
| 249 | 269 | |
| 270 | + /** |
|
| 271 | + * @param string $tag |
|
| 272 | + */ |
|
| 250 | 273 | function get_table($tag) { |
| 251 | 274 | list($pos, $length) = $this->get_table_pos($tag); |
| 252 | 275 | if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
@@ -254,6 +277,10 @@ discard block |
||
| 254 | 277 | return (fread($this->fh,$length)); |
| 255 | 278 | } |
| 256 | 279 | |
| 280 | + /** |
|
| 281 | + * @param string $tag |
|
| 282 | + * @param null|string $data |
|
| 283 | + */ |
|
| 257 | 284 | function add($tag, $data) { |
| 258 | 285 | if ($tag == 'head') { |
| 259 | 286 | $data = $this->splice($data, 8, "\0\0\0\0"); |
@@ -491,6 +518,9 @@ discard block |
||
| 491 | 518 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 492 | 519 | |
| 493 | 520 | |
| 521 | + /** |
|
| 522 | + * @return string |
|
| 523 | + */ |
|
| 494 | 524 | function makeSubset($file, &$subset) { |
| 495 | 525 | $this->filename = $file; |
| 496 | 526 | $this->fh = fopen($file ,'rb') or die('Can\'t open file ' . $file); |
@@ -884,6 +914,11 @@ discard block |
||
| 884 | 914 | |
| 885 | 915 | ////////////////////////////////////////////////////////////////////////////////// |
| 886 | 916 | |
| 917 | + /** |
|
| 918 | + * @param integer $numberOfHMetrics |
|
| 919 | + * @param integer $numGlyphs |
|
| 920 | + * @param integer $scale |
|
| 921 | + */ |
|
| 887 | 922 | function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { |
| 888 | 923 | $start = $this->seek_table("hmtx"); |
| 889 | 924 | $aw = 0; |
@@ -949,6 +984,9 @@ discard block |
||
| 949 | 984 | $this->charWidths[1] = chr($nCharWidths & 0xFF); |
| 950 | 985 | } |
| 951 | 986 | |
| 987 | + /** |
|
| 988 | + * @param integer $numberOfHMetrics |
|
| 989 | + */ |
|
| 952 | 990 | function getHMetric($numberOfHMetrics, $gid) { |
| 953 | 991 | $start = $this->seek_table("hmtx"); |
| 954 | 992 | if ($gid < $numberOfHMetrics) { |
@@ -964,6 +1002,10 @@ discard block |
||
| 964 | 1002 | return $hm; |
| 965 | 1003 | } |
| 966 | 1004 | |
| 1005 | + /** |
|
| 1006 | + * @param integer $indexToLocFormat |
|
| 1007 | + * @param integer $numGlyphs |
|
| 1008 | + */ |
|
| 967 | 1009 | function getLOCA($indexToLocFormat, $numGlyphs) { |
| 968 | 1010 | $start = $this->seek_table('loca'); |
| 969 | 1011 | $this->glyphPos = array(); |
@@ -987,6 +1029,10 @@ discard block |
||
| 987 | 1029 | |
| 988 | 1030 | |
| 989 | 1031 | // CMAP Format 4 |
| 1032 | + |
|
| 1033 | + /** |
|
| 1034 | + * @param integer $unicode_cmap_offset |
|
| 1035 | + */ |
|
| 990 | 1036 | function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { |
| 991 | 1037 | $this->maxUniChar = 0; |
| 992 | 1038 | $this->seek($unicode_cmap_offset + 2); |
@@ -1032,6 +1078,10 @@ discard block |
||
| 1032 | 1078 | |
| 1033 | 1079 | |
| 1034 | 1080 | // Put the TTF file together |
| 1081 | + |
|
| 1082 | + /** |
|
| 1083 | + * @param string $stm |
|
| 1084 | + */ |
|
| 1035 | 1085 | function endTTFile(&$stm) { |
| 1036 | 1086 | $stm = ''; |
| 1037 | 1087 | $numTables = count($this->otables); |
@@ -69,38 +69,38 @@ discard block |
||
| 69 | 69 | var $defaultWidth; |
| 70 | 70 | var $maxStrLenRead; |
| 71 | 71 | |
| 72 | - function __construct() { |
|
| 73 | - $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - function getMetrics($file) { |
|
| 78 | - $this->filename = $file; |
|
| 79 | - $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); |
|
| 80 | - $this->_pos = 0; |
|
| 81 | - $this->charWidths = ''; |
|
| 82 | - $this->glyphPos = array(); |
|
| 83 | - $this->charToGlyph = array(); |
|
| 84 | - $this->tables = array(); |
|
| 85 | - $this->otables = array(); |
|
| 86 | - $this->ascent = 0; |
|
| 87 | - $this->descent = 0; |
|
| 88 | - $this->TTCFonts = array(); |
|
| 89 | - $this->version = $version = $this->read_ulong(); |
|
| 90 | - if ($version==0x4F54544F) |
|
| 91 | - die("Postscript outlines are not supported"); |
|
| 92 | - if ($version==0x74746366) |
|
| 93 | - die("ERROR - TrueType Fonts Collections not supported"); |
|
| 94 | - if (!in_array($version, array(0x00010000,0x74727565))) |
|
| 95 | - die("Not a TrueType font: version=".$version); |
|
| 96 | - $this->readTableDirectory(); |
|
| 97 | - $this->extractInfo(); |
|
| 98 | - fclose($this->fh); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - |
|
| 102 | - function readTableDirectory() { |
|
| 103 | - $this->numTables = $this->read_ushort(); |
|
| 72 | + function __construct() { |
|
| 73 | + $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + function getMetrics($file) { |
|
| 78 | + $this->filename = $file; |
|
| 79 | + $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); |
|
| 80 | + $this->_pos = 0; |
|
| 81 | + $this->charWidths = ''; |
|
| 82 | + $this->glyphPos = array(); |
|
| 83 | + $this->charToGlyph = array(); |
|
| 84 | + $this->tables = array(); |
|
| 85 | + $this->otables = array(); |
|
| 86 | + $this->ascent = 0; |
|
| 87 | + $this->descent = 0; |
|
| 88 | + $this->TTCFonts = array(); |
|
| 89 | + $this->version = $version = $this->read_ulong(); |
|
| 90 | + if ($version==0x4F54544F) |
|
| 91 | + die("Postscript outlines are not supported"); |
|
| 92 | + if ($version==0x74746366) |
|
| 93 | + die("ERROR - TrueType Fonts Collections not supported"); |
|
| 94 | + if (!in_array($version, array(0x00010000,0x74727565))) |
|
| 95 | + die("Not a TrueType font: version=".$version); |
|
| 96 | + $this->readTableDirectory(); |
|
| 97 | + $this->extractInfo(); |
|
| 98 | + fclose($this->fh); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + |
|
| 102 | + function readTableDirectory() { |
|
| 103 | + $this->numTables = $this->read_ushort(); |
|
| 104 | 104 | $this->searchRange = $this->read_ushort(); |
| 105 | 105 | $this->entrySelector = $this->read_ushort(); |
| 106 | 106 | $this->rangeShift = $this->read_ushort(); |
@@ -112,154 +112,154 @@ discard block |
||
| 112 | 112 | $record['offset'] = $this->read_ulong(); |
| 113 | 113 | $record['length'] = $this->read_ulong(); |
| 114 | 114 | $this->tables[$record['tag']] = $record; |
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - function sub32($x, $y) { |
|
| 120 | - $xlo = $x[1]; |
|
| 121 | - $xhi = $x[0]; |
|
| 122 | - $ylo = $y[1]; |
|
| 123 | - $yhi = $y[0]; |
|
| 124 | - if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
|
| 125 | - $reslo = $xlo-$ylo; |
|
| 126 | - if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
| 127 | - $reshi = $xhi-$yhi; |
|
| 128 | - $reshi = $reshi & 0xFFFF; |
|
| 129 | - return array($reshi, $reslo); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - function calcChecksum($data) { |
|
| 133 | - if (strlen($data) % 4) { $data .= str_repeat("\0",(4-(strlen($data) % 4))); } |
|
| 134 | - $hi=0x0000; |
|
| 135 | - $lo=0x0000; |
|
| 136 | - for($i=0;$i<strlen($data);$i+=4) { |
|
| 137 | - $hi += (ord($data[$i])<<8) + ord($data[$i+1]); |
|
| 138 | - $lo += (ord($data[$i+2])<<8) + ord($data[$i+3]); |
|
| 139 | - $hi += $lo >> 16; |
|
| 140 | - $lo = $lo & 0xFFFF; |
|
| 141 | - $hi = $hi & 0xFFFF; |
|
| 142 | - } |
|
| 143 | - return array($hi, $lo); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - function get_table_pos($tag) { |
|
| 147 | - $offset = $this->tables[$tag]['offset']; |
|
| 148 | - $length = $this->tables[$tag]['length']; |
|
| 149 | - return array($offset, $length); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - function seek($pos) { |
|
| 153 | - $this->_pos = $pos; |
|
| 154 | - fseek($this->fh,$this->_pos); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - function skip($delta) { |
|
| 158 | - $this->_pos = $this->_pos + $delta; |
|
| 159 | - fseek($this->fh,$this->_pos); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - function seek_table($tag, $offset_in_table = 0) { |
|
| 163 | - $tpos = $this->get_table_pos($tag); |
|
| 164 | - $this->_pos = $tpos[0] + $offset_in_table; |
|
| 165 | - fseek($this->fh, $this->_pos); |
|
| 166 | - return $this->_pos; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - function read_tag() { |
|
| 170 | - $this->_pos += 4; |
|
| 171 | - return fread($this->fh,4); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - function read_short() { |
|
| 175 | - $this->_pos += 2; |
|
| 176 | - $s = fread($this->fh,2); |
|
| 177 | - $a = (ord($s[0])<<8) + ord($s[1]); |
|
| 178 | - if ($a & (1 << 15) ) { $a = ($a - (1 << 16)) ; } |
|
| 179 | - return $a; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - function unpack_short($s) { |
|
| 183 | - $a = (ord($s[0])<<8) + ord($s[1]); |
|
| 184 | - if ($a & (1 << 15) ) { |
|
| 185 | - $a = ($a - (1 << 16)); |
|
| 186 | - } |
|
| 187 | - return $a; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - function read_ushort() { |
|
| 191 | - $this->_pos += 2; |
|
| 192 | - $s = fread($this->fh,2); |
|
| 193 | - return (ord($s[0])<<8) + ord($s[1]); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - function read_ulong() { |
|
| 197 | - $this->_pos += 4; |
|
| 198 | - $s = fread($this->fh,4); |
|
| 199 | - // if large uInt32 as an integer, PHP converts it to -ve |
|
| 200 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - function get_ushort($pos) { |
|
| 204 | - fseek($this->fh,$pos); |
|
| 205 | - $s = fread($this->fh,2); |
|
| 206 | - return (ord($s[0])<<8) + ord($s[1]); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - function get_ulong($pos) { |
|
| 210 | - fseek($this->fh,$pos); |
|
| 211 | - $s = fread($this->fh,4); |
|
| 212 | - // iF large uInt32 as an integer, PHP converts it to -ve |
|
| 213 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - function pack_short($val) { |
|
| 217 | - if ($val<0) { |
|
| 218 | - $val = abs($val); |
|
| 219 | - $val = ~$val; |
|
| 220 | - $val += 1; |
|
| 221 | - } |
|
| 222 | - return pack("n",$val); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - function splice($stream, $offset, $value) { |
|
| 226 | - return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - function _set_ushort($stream, $offset, $value) { |
|
| 230 | - $up = pack("n", $value); |
|
| 231 | - return $this->splice($stream, $offset, $up); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - function _set_short($stream, $offset, $val) { |
|
| 235 | - if ($val<0) { |
|
| 236 | - $val = abs($val); |
|
| 237 | - $val = ~$val; |
|
| 238 | - $val += 1; |
|
| 239 | - } |
|
| 240 | - $up = pack("n",$val); |
|
| 241 | - return $this->splice($stream, $offset, $up); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - function get_chunk($pos, $length) { |
|
| 245 | - fseek($this->fh,$pos); |
|
| 246 | - if ($length <1) { return ''; } |
|
| 247 | - return (fread($this->fh,$length)); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - function get_table($tag) { |
|
| 251 | - list($pos, $length) = $this->get_table_pos($tag); |
|
| 252 | - if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
|
| 253 | - fseek($this->fh,$pos); |
|
| 254 | - return (fread($this->fh,$length)); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - function add($tag, $data) { |
|
| 258 | - if ($tag == 'head') { |
|
| 259 | - $data = $this->splice($data, 8, "\0\0\0\0"); |
|
| 260 | - } |
|
| 261 | - $this->otables[$tag] = $data; |
|
| 262 | - } |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + function sub32($x, $y) { |
|
| 120 | + $xlo = $x[1]; |
|
| 121 | + $xhi = $x[0]; |
|
| 122 | + $ylo = $y[1]; |
|
| 123 | + $yhi = $y[0]; |
|
| 124 | + if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
|
| 125 | + $reslo = $xlo-$ylo; |
|
| 126 | + if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
| 127 | + $reshi = $xhi-$yhi; |
|
| 128 | + $reshi = $reshi & 0xFFFF; |
|
| 129 | + return array($reshi, $reslo); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + function calcChecksum($data) { |
|
| 133 | + if (strlen($data) % 4) { $data .= str_repeat("\0",(4-(strlen($data) % 4))); } |
|
| 134 | + $hi=0x0000; |
|
| 135 | + $lo=0x0000; |
|
| 136 | + for($i=0;$i<strlen($data);$i+=4) { |
|
| 137 | + $hi += (ord($data[$i])<<8) + ord($data[$i+1]); |
|
| 138 | + $lo += (ord($data[$i+2])<<8) + ord($data[$i+3]); |
|
| 139 | + $hi += $lo >> 16; |
|
| 140 | + $lo = $lo & 0xFFFF; |
|
| 141 | + $hi = $hi & 0xFFFF; |
|
| 142 | + } |
|
| 143 | + return array($hi, $lo); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + function get_table_pos($tag) { |
|
| 147 | + $offset = $this->tables[$tag]['offset']; |
|
| 148 | + $length = $this->tables[$tag]['length']; |
|
| 149 | + return array($offset, $length); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + function seek($pos) { |
|
| 153 | + $this->_pos = $pos; |
|
| 154 | + fseek($this->fh,$this->_pos); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + function skip($delta) { |
|
| 158 | + $this->_pos = $this->_pos + $delta; |
|
| 159 | + fseek($this->fh,$this->_pos); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + function seek_table($tag, $offset_in_table = 0) { |
|
| 163 | + $tpos = $this->get_table_pos($tag); |
|
| 164 | + $this->_pos = $tpos[0] + $offset_in_table; |
|
| 165 | + fseek($this->fh, $this->_pos); |
|
| 166 | + return $this->_pos; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + function read_tag() { |
|
| 170 | + $this->_pos += 4; |
|
| 171 | + return fread($this->fh,4); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + function read_short() { |
|
| 175 | + $this->_pos += 2; |
|
| 176 | + $s = fread($this->fh,2); |
|
| 177 | + $a = (ord($s[0])<<8) + ord($s[1]); |
|
| 178 | + if ($a & (1 << 15) ) { $a = ($a - (1 << 16)) ; } |
|
| 179 | + return $a; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + function unpack_short($s) { |
|
| 183 | + $a = (ord($s[0])<<8) + ord($s[1]); |
|
| 184 | + if ($a & (1 << 15) ) { |
|
| 185 | + $a = ($a - (1 << 16)); |
|
| 186 | + } |
|
| 187 | + return $a; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + function read_ushort() { |
|
| 191 | + $this->_pos += 2; |
|
| 192 | + $s = fread($this->fh,2); |
|
| 193 | + return (ord($s[0])<<8) + ord($s[1]); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + function read_ulong() { |
|
| 197 | + $this->_pos += 4; |
|
| 198 | + $s = fread($this->fh,4); |
|
| 199 | + // if large uInt32 as an integer, PHP converts it to -ve |
|
| 200 | + return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + function get_ushort($pos) { |
|
| 204 | + fseek($this->fh,$pos); |
|
| 205 | + $s = fread($this->fh,2); |
|
| 206 | + return (ord($s[0])<<8) + ord($s[1]); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + function get_ulong($pos) { |
|
| 210 | + fseek($this->fh,$pos); |
|
| 211 | + $s = fread($this->fh,4); |
|
| 212 | + // iF large uInt32 as an integer, PHP converts it to -ve |
|
| 213 | + return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + function pack_short($val) { |
|
| 217 | + if ($val<0) { |
|
| 218 | + $val = abs($val); |
|
| 219 | + $val = ~$val; |
|
| 220 | + $val += 1; |
|
| 221 | + } |
|
| 222 | + return pack("n",$val); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + function splice($stream, $offset, $value) { |
|
| 226 | + return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + function _set_ushort($stream, $offset, $value) { |
|
| 230 | + $up = pack("n", $value); |
|
| 231 | + return $this->splice($stream, $offset, $up); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + function _set_short($stream, $offset, $val) { |
|
| 235 | + if ($val<0) { |
|
| 236 | + $val = abs($val); |
|
| 237 | + $val = ~$val; |
|
| 238 | + $val += 1; |
|
| 239 | + } |
|
| 240 | + $up = pack("n",$val); |
|
| 241 | + return $this->splice($stream, $offset, $up); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + function get_chunk($pos, $length) { |
|
| 245 | + fseek($this->fh,$pos); |
|
| 246 | + if ($length <1) { return ''; } |
|
| 247 | + return (fread($this->fh,$length)); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + function get_table($tag) { |
|
| 251 | + list($pos, $length) = $this->get_table_pos($tag); |
|
| 252 | + if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
|
| 253 | + fseek($this->fh,$pos); |
|
| 254 | + return (fread($this->fh,$length)); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + function add($tag, $data) { |
|
| 258 | + if ($tag == 'head') { |
|
| 259 | + $data = $this->splice($data, 8, "\0\0\0\0"); |
|
| 260 | + } |
|
| 261 | + $this->otables[$tag] = $data; |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | 264 | |
| 265 | 265 | |
@@ -268,817 +268,817 @@ discard block |
||
| 268 | 268 | |
| 269 | 269 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 270 | 270 | |
| 271 | - function extractInfo() { |
|
| 272 | - /////////////////////////////////// |
|
| 273 | - // name - Naming table |
|
| 274 | - /////////////////////////////////// |
|
| 275 | - $this->sFamilyClass = 0; |
|
| 276 | - $this->sFamilySubClass = 0; |
|
| 277 | - |
|
| 278 | - $name_offset = $this->seek_table("name"); |
|
| 279 | - $format = $this->read_ushort(); |
|
| 280 | - if ($format != 0) |
|
| 281 | - die("Unknown name table format ".$format); |
|
| 282 | - $numRecords = $this->read_ushort(); |
|
| 283 | - $string_data_offset = $name_offset + $this->read_ushort(); |
|
| 284 | - $names = array(1=>'',2=>'',3=>'',4=>'',6=>''); |
|
| 285 | - $K = array_keys($names); |
|
| 286 | - $nameCount = count($names); |
|
| 287 | - for ($i=0;$i<$numRecords; $i++) { |
|
| 288 | - $platformId = $this->read_ushort(); |
|
| 289 | - $encodingId = $this->read_ushort(); |
|
| 290 | - $languageId = $this->read_ushort(); |
|
| 291 | - $nameId = $this->read_ushort(); |
|
| 292 | - $length = $this->read_ushort(); |
|
| 293 | - $offset = $this->read_ushort(); |
|
| 294 | - if (!in_array($nameId,$K)) continue; |
|
| 295 | - $N = ''; |
|
| 296 | - if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name |
|
| 297 | - $opos = $this->_pos; |
|
| 298 | - $this->seek($string_data_offset + $offset); |
|
| 299 | - if ($length % 2 != 0) |
|
| 300 | - die("PostScript name is UTF-16BE string of odd length"); |
|
| 301 | - $length /= 2; |
|
| 302 | - $N = ''; |
|
| 303 | - while ($length > 0) { |
|
| 304 | - $char = $this->read_ushort(); |
|
| 305 | - $N .= (chr($char)); |
|
| 306 | - $length -= 1; |
|
| 307 | - } |
|
| 308 | - $this->_pos = $opos; |
|
| 309 | - $this->seek($opos); |
|
| 310 | - } |
|
| 311 | - else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name |
|
| 312 | - $opos = $this->_pos; |
|
| 313 | - $N = $this->get_chunk($string_data_offset + $offset, $length); |
|
| 314 | - $this->_pos = $opos; |
|
| 315 | - $this->seek($opos); |
|
| 316 | - } |
|
| 317 | - if ($N && $names[$nameId]=='') { |
|
| 318 | - $names[$nameId] = $N; |
|
| 319 | - $nameCount -= 1; |
|
| 320 | - if ($nameCount==0) break; |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - if ($names[6]) |
|
| 324 | - $psName = $names[6]; |
|
| 325 | - else if ($names[4]) |
|
| 326 | - $psName = preg_replace('/ /','-',$names[4]); |
|
| 327 | - else if ($names[1]) |
|
| 328 | - $psName = preg_replace('/ /','-',$names[1]); |
|
| 329 | - else |
|
| 330 | - $psName = ''; |
|
| 331 | - if (!$psName) |
|
| 332 | - die("Could not find PostScript font name"); |
|
| 333 | - $this->name = $psName; |
|
| 334 | - if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } |
|
| 335 | - if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } |
|
| 336 | - if ($names[4]) { $this->fullName = $names[4]; } else { $this->fullName = $psName; } |
|
| 337 | - if ($names[3]) { $this->uniqueFontID = $names[3]; } else { $this->uniqueFontID = $psName; } |
|
| 338 | - if ($names[6]) { $this->fullName = $names[6]; } |
|
| 339 | - |
|
| 340 | - /////////////////////////////////// |
|
| 341 | - // head - Font header table |
|
| 342 | - /////////////////////////////////// |
|
| 343 | - $this->seek_table("head"); |
|
| 344 | - $this->skip(18); |
|
| 345 | - $this->unitsPerEm = $unitsPerEm = $this->read_ushort(); |
|
| 346 | - $scale = 1000 / $unitsPerEm; |
|
| 347 | - $this->skip(16); |
|
| 348 | - $xMin = $this->read_short(); |
|
| 349 | - $yMin = $this->read_short(); |
|
| 350 | - $xMax = $this->read_short(); |
|
| 351 | - $yMax = $this->read_short(); |
|
| 352 | - $this->bbox = array(($xMin*$scale), ($yMin*$scale), ($xMax*$scale), ($yMax*$scale)); |
|
| 353 | - $this->skip(3*2); |
|
| 354 | - $indexToLocFormat = $this->read_ushort(); |
|
| 355 | - $glyphDataFormat = $this->read_ushort(); |
|
| 356 | - if ($glyphDataFormat != 0) |
|
| 357 | - die('Unknown glyph data format '.$glyphDataFormat); |
|
| 358 | - |
|
| 359 | - /////////////////////////////////// |
|
| 360 | - // hhea metrics table |
|
| 361 | - /////////////////////////////////// |
|
| 362 | - // ttf2t1 seems to use this value rather than the one in OS/2 - so put in for compatibility |
|
| 363 | - if (isset($this->tables["hhea"])) { |
|
| 364 | - $this->seek_table("hhea"); |
|
| 365 | - $this->skip(4); |
|
| 366 | - $hheaAscender = $this->read_short(); |
|
| 367 | - $hheaDescender = $this->read_short(); |
|
| 368 | - $this->ascent = ($hheaAscender *$scale); |
|
| 369 | - $this->descent = ($hheaDescender *$scale); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /////////////////////////////////// |
|
| 373 | - // OS/2 - OS/2 and Windows metrics table |
|
| 374 | - /////////////////////////////////// |
|
| 375 | - if (isset($this->tables["OS/2"])) { |
|
| 376 | - $this->seek_table("OS/2"); |
|
| 377 | - $version = $this->read_ushort(); |
|
| 378 | - $this->skip(2); |
|
| 379 | - $usWeightClass = $this->read_ushort(); |
|
| 380 | - $this->skip(2); |
|
| 381 | - $fsType = $this->read_ushort(); |
|
| 382 | - if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) { |
|
| 383 | - die('ERROR - Font file '.$this->filename.' cannot be embedded due to copyright restrictions.'); |
|
| 384 | - $this->restrictedUse = true; |
|
| 385 | - } |
|
| 386 | - $this->skip(20); |
|
| 387 | - $sF = $this->read_short(); |
|
| 388 | - $this->sFamilyClass = ($sF >> 8); |
|
| 389 | - $this->sFamilySubClass = ($sF & 0xFF); |
|
| 390 | - $this->_pos += 10; //PANOSE = 10 byte length |
|
| 391 | - $panose = fread($this->fh,10); |
|
| 392 | - $this->skip(26); |
|
| 393 | - $sTypoAscender = $this->read_short(); |
|
| 394 | - $sTypoDescender = $this->read_short(); |
|
| 395 | - if (!$this->ascent) $this->ascent = ($sTypoAscender*$scale); |
|
| 396 | - if (!$this->descent) $this->descent = ($sTypoDescender*$scale); |
|
| 397 | - if ($version > 1) { |
|
| 398 | - $this->skip(16); |
|
| 399 | - $sCapHeight = $this->read_short(); |
|
| 400 | - $this->capHeight = ($sCapHeight*$scale); |
|
| 401 | - } |
|
| 402 | - else { |
|
| 403 | - $this->capHeight = $this->ascent; |
|
| 404 | - } |
|
| 405 | - } |
|
| 406 | - else { |
|
| 407 | - $usWeightClass = 500; |
|
| 408 | - if (!$this->ascent) $this->ascent = ($yMax*$scale); |
|
| 409 | - if (!$this->descent) $this->descent = ($yMin*$scale); |
|
| 410 | - $this->capHeight = $this->ascent; |
|
| 411 | - } |
|
| 412 | - $this->stemV = 50 + intval(pow(($usWeightClass / 65.0),2)); |
|
| 413 | - |
|
| 414 | - /////////////////////////////////// |
|
| 415 | - // post - PostScript table |
|
| 416 | - /////////////////////////////////// |
|
| 417 | - $this->seek_table("post"); |
|
| 418 | - $this->skip(4); |
|
| 419 | - $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0; |
|
| 420 | - $this->underlinePosition = $this->read_short() * $scale; |
|
| 421 | - $this->underlineThickness = $this->read_short() * $scale; |
|
| 422 | - $isFixedPitch = $this->read_ulong(); |
|
| 423 | - |
|
| 424 | - $this->flags = 4; |
|
| 425 | - |
|
| 426 | - if ($this->italicAngle!= 0) |
|
| 427 | - $this->flags = $this->flags | 64; |
|
| 428 | - if ($usWeightClass >= 600) |
|
| 429 | - $this->flags = $this->flags | 262144; |
|
| 430 | - if ($isFixedPitch) |
|
| 431 | - $this->flags = $this->flags | 1; |
|
| 432 | - |
|
| 433 | - /////////////////////////////////// |
|
| 434 | - // hhea - Horizontal header table |
|
| 435 | - /////////////////////////////////// |
|
| 436 | - $this->seek_table("hhea"); |
|
| 437 | - $this->skip(32); |
|
| 438 | - $metricDataFormat = $this->read_ushort(); |
|
| 439 | - if ($metricDataFormat != 0) |
|
| 440 | - die('Unknown horizontal metric data format '.$metricDataFormat); |
|
| 441 | - $numberOfHMetrics = $this->read_ushort(); |
|
| 442 | - if ($numberOfHMetrics == 0) |
|
| 443 | - die('Number of horizontal metrics is 0'); |
|
| 444 | - |
|
| 445 | - /////////////////////////////////// |
|
| 446 | - // maxp - Maximum profile table |
|
| 447 | - /////////////////////////////////// |
|
| 448 | - $this->seek_table("maxp"); |
|
| 449 | - $this->skip(4); |
|
| 450 | - $numGlyphs = $this->read_ushort(); |
|
| 451 | - |
|
| 452 | - |
|
| 453 | - /////////////////////////////////// |
|
| 454 | - // cmap - Character to glyph index mapping table |
|
| 455 | - /////////////////////////////////// |
|
| 456 | - $cmap_offset = $this->seek_table("cmap"); |
|
| 457 | - $this->skip(2); |
|
| 458 | - $cmapTableCount = $this->read_ushort(); |
|
| 459 | - $unicode_cmap_offset = 0; |
|
| 460 | - for ($i=0;$i<$cmapTableCount;$i++) { |
|
| 461 | - $platformID = $this->read_ushort(); |
|
| 462 | - $encodingID = $this->read_ushort(); |
|
| 463 | - $offset = $this->read_ulong(); |
|
| 464 | - $save_pos = $this->_pos; |
|
| 465 | - if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
|
| 466 | - $format = $this->get_ushort($cmap_offset + $offset); |
|
| 467 | - if ($format == 4) { |
|
| 468 | - if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; |
|
| 469 | - break; |
|
| 470 | - } |
|
| 471 | - } |
|
| 472 | - $this->seek($save_pos ); |
|
| 473 | - } |
|
| 474 | - if (!$unicode_cmap_offset) |
|
| 475 | - die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 476 | - |
|
| 477 | - |
|
| 478 | - $glyphToChar = array(); |
|
| 479 | - $charToGlyph = array(); |
|
| 480 | - $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph ); |
|
| 481 | - |
|
| 482 | - /////////////////////////////////// |
|
| 483 | - // hmtx - Horizontal metrics table |
|
| 484 | - /////////////////////////////////// |
|
| 485 | - $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
|
| 486 | - |
|
| 487 | - } |
|
| 271 | + function extractInfo() { |
|
| 272 | + /////////////////////////////////// |
|
| 273 | + // name - Naming table |
|
| 274 | + /////////////////////////////////// |
|
| 275 | + $this->sFamilyClass = 0; |
|
| 276 | + $this->sFamilySubClass = 0; |
|
| 277 | + |
|
| 278 | + $name_offset = $this->seek_table("name"); |
|
| 279 | + $format = $this->read_ushort(); |
|
| 280 | + if ($format != 0) |
|
| 281 | + die("Unknown name table format ".$format); |
|
| 282 | + $numRecords = $this->read_ushort(); |
|
| 283 | + $string_data_offset = $name_offset + $this->read_ushort(); |
|
| 284 | + $names = array(1=>'',2=>'',3=>'',4=>'',6=>''); |
|
| 285 | + $K = array_keys($names); |
|
| 286 | + $nameCount = count($names); |
|
| 287 | + for ($i=0;$i<$numRecords; $i++) { |
|
| 288 | + $platformId = $this->read_ushort(); |
|
| 289 | + $encodingId = $this->read_ushort(); |
|
| 290 | + $languageId = $this->read_ushort(); |
|
| 291 | + $nameId = $this->read_ushort(); |
|
| 292 | + $length = $this->read_ushort(); |
|
| 293 | + $offset = $this->read_ushort(); |
|
| 294 | + if (!in_array($nameId,$K)) continue; |
|
| 295 | + $N = ''; |
|
| 296 | + if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name |
|
| 297 | + $opos = $this->_pos; |
|
| 298 | + $this->seek($string_data_offset + $offset); |
|
| 299 | + if ($length % 2 != 0) |
|
| 300 | + die("PostScript name is UTF-16BE string of odd length"); |
|
| 301 | + $length /= 2; |
|
| 302 | + $N = ''; |
|
| 303 | + while ($length > 0) { |
|
| 304 | + $char = $this->read_ushort(); |
|
| 305 | + $N .= (chr($char)); |
|
| 306 | + $length -= 1; |
|
| 307 | + } |
|
| 308 | + $this->_pos = $opos; |
|
| 309 | + $this->seek($opos); |
|
| 310 | + } |
|
| 311 | + else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name |
|
| 312 | + $opos = $this->_pos; |
|
| 313 | + $N = $this->get_chunk($string_data_offset + $offset, $length); |
|
| 314 | + $this->_pos = $opos; |
|
| 315 | + $this->seek($opos); |
|
| 316 | + } |
|
| 317 | + if ($N && $names[$nameId]=='') { |
|
| 318 | + $names[$nameId] = $N; |
|
| 319 | + $nameCount -= 1; |
|
| 320 | + if ($nameCount==0) break; |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + if ($names[6]) |
|
| 324 | + $psName = $names[6]; |
|
| 325 | + else if ($names[4]) |
|
| 326 | + $psName = preg_replace('/ /','-',$names[4]); |
|
| 327 | + else if ($names[1]) |
|
| 328 | + $psName = preg_replace('/ /','-',$names[1]); |
|
| 329 | + else |
|
| 330 | + $psName = ''; |
|
| 331 | + if (!$psName) |
|
| 332 | + die("Could not find PostScript font name"); |
|
| 333 | + $this->name = $psName; |
|
| 334 | + if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } |
|
| 335 | + if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } |
|
| 336 | + if ($names[4]) { $this->fullName = $names[4]; } else { $this->fullName = $psName; } |
|
| 337 | + if ($names[3]) { $this->uniqueFontID = $names[3]; } else { $this->uniqueFontID = $psName; } |
|
| 338 | + if ($names[6]) { $this->fullName = $names[6]; } |
|
| 339 | + |
|
| 340 | + /////////////////////////////////// |
|
| 341 | + // head - Font header table |
|
| 342 | + /////////////////////////////////// |
|
| 343 | + $this->seek_table("head"); |
|
| 344 | + $this->skip(18); |
|
| 345 | + $this->unitsPerEm = $unitsPerEm = $this->read_ushort(); |
|
| 346 | + $scale = 1000 / $unitsPerEm; |
|
| 347 | + $this->skip(16); |
|
| 348 | + $xMin = $this->read_short(); |
|
| 349 | + $yMin = $this->read_short(); |
|
| 350 | + $xMax = $this->read_short(); |
|
| 351 | + $yMax = $this->read_short(); |
|
| 352 | + $this->bbox = array(($xMin*$scale), ($yMin*$scale), ($xMax*$scale), ($yMax*$scale)); |
|
| 353 | + $this->skip(3*2); |
|
| 354 | + $indexToLocFormat = $this->read_ushort(); |
|
| 355 | + $glyphDataFormat = $this->read_ushort(); |
|
| 356 | + if ($glyphDataFormat != 0) |
|
| 357 | + die('Unknown glyph data format '.$glyphDataFormat); |
|
| 358 | + |
|
| 359 | + /////////////////////////////////// |
|
| 360 | + // hhea metrics table |
|
| 361 | + /////////////////////////////////// |
|
| 362 | + // ttf2t1 seems to use this value rather than the one in OS/2 - so put in for compatibility |
|
| 363 | + if (isset($this->tables["hhea"])) { |
|
| 364 | + $this->seek_table("hhea"); |
|
| 365 | + $this->skip(4); |
|
| 366 | + $hheaAscender = $this->read_short(); |
|
| 367 | + $hheaDescender = $this->read_short(); |
|
| 368 | + $this->ascent = ($hheaAscender *$scale); |
|
| 369 | + $this->descent = ($hheaDescender *$scale); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /////////////////////////////////// |
|
| 373 | + // OS/2 - OS/2 and Windows metrics table |
|
| 374 | + /////////////////////////////////// |
|
| 375 | + if (isset($this->tables["OS/2"])) { |
|
| 376 | + $this->seek_table("OS/2"); |
|
| 377 | + $version = $this->read_ushort(); |
|
| 378 | + $this->skip(2); |
|
| 379 | + $usWeightClass = $this->read_ushort(); |
|
| 380 | + $this->skip(2); |
|
| 381 | + $fsType = $this->read_ushort(); |
|
| 382 | + if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) { |
|
| 383 | + die('ERROR - Font file '.$this->filename.' cannot be embedded due to copyright restrictions.'); |
|
| 384 | + $this->restrictedUse = true; |
|
| 385 | + } |
|
| 386 | + $this->skip(20); |
|
| 387 | + $sF = $this->read_short(); |
|
| 388 | + $this->sFamilyClass = ($sF >> 8); |
|
| 389 | + $this->sFamilySubClass = ($sF & 0xFF); |
|
| 390 | + $this->_pos += 10; //PANOSE = 10 byte length |
|
| 391 | + $panose = fread($this->fh,10); |
|
| 392 | + $this->skip(26); |
|
| 393 | + $sTypoAscender = $this->read_short(); |
|
| 394 | + $sTypoDescender = $this->read_short(); |
|
| 395 | + if (!$this->ascent) $this->ascent = ($sTypoAscender*$scale); |
|
| 396 | + if (!$this->descent) $this->descent = ($sTypoDescender*$scale); |
|
| 397 | + if ($version > 1) { |
|
| 398 | + $this->skip(16); |
|
| 399 | + $sCapHeight = $this->read_short(); |
|
| 400 | + $this->capHeight = ($sCapHeight*$scale); |
|
| 401 | + } |
|
| 402 | + else { |
|
| 403 | + $this->capHeight = $this->ascent; |
|
| 404 | + } |
|
| 405 | + } |
|
| 406 | + else { |
|
| 407 | + $usWeightClass = 500; |
|
| 408 | + if (!$this->ascent) $this->ascent = ($yMax*$scale); |
|
| 409 | + if (!$this->descent) $this->descent = ($yMin*$scale); |
|
| 410 | + $this->capHeight = $this->ascent; |
|
| 411 | + } |
|
| 412 | + $this->stemV = 50 + intval(pow(($usWeightClass / 65.0),2)); |
|
| 413 | + |
|
| 414 | + /////////////////////////////////// |
|
| 415 | + // post - PostScript table |
|
| 416 | + /////////////////////////////////// |
|
| 417 | + $this->seek_table("post"); |
|
| 418 | + $this->skip(4); |
|
| 419 | + $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0; |
|
| 420 | + $this->underlinePosition = $this->read_short() * $scale; |
|
| 421 | + $this->underlineThickness = $this->read_short() * $scale; |
|
| 422 | + $isFixedPitch = $this->read_ulong(); |
|
| 423 | + |
|
| 424 | + $this->flags = 4; |
|
| 425 | + |
|
| 426 | + if ($this->italicAngle!= 0) |
|
| 427 | + $this->flags = $this->flags | 64; |
|
| 428 | + if ($usWeightClass >= 600) |
|
| 429 | + $this->flags = $this->flags | 262144; |
|
| 430 | + if ($isFixedPitch) |
|
| 431 | + $this->flags = $this->flags | 1; |
|
| 432 | + |
|
| 433 | + /////////////////////////////////// |
|
| 434 | + // hhea - Horizontal header table |
|
| 435 | + /////////////////////////////////// |
|
| 436 | + $this->seek_table("hhea"); |
|
| 437 | + $this->skip(32); |
|
| 438 | + $metricDataFormat = $this->read_ushort(); |
|
| 439 | + if ($metricDataFormat != 0) |
|
| 440 | + die('Unknown horizontal metric data format '.$metricDataFormat); |
|
| 441 | + $numberOfHMetrics = $this->read_ushort(); |
|
| 442 | + if ($numberOfHMetrics == 0) |
|
| 443 | + die('Number of horizontal metrics is 0'); |
|
| 444 | + |
|
| 445 | + /////////////////////////////////// |
|
| 446 | + // maxp - Maximum profile table |
|
| 447 | + /////////////////////////////////// |
|
| 448 | + $this->seek_table("maxp"); |
|
| 449 | + $this->skip(4); |
|
| 450 | + $numGlyphs = $this->read_ushort(); |
|
| 451 | + |
|
| 452 | + |
|
| 453 | + /////////////////////////////////// |
|
| 454 | + // cmap - Character to glyph index mapping table |
|
| 455 | + /////////////////////////////////// |
|
| 456 | + $cmap_offset = $this->seek_table("cmap"); |
|
| 457 | + $this->skip(2); |
|
| 458 | + $cmapTableCount = $this->read_ushort(); |
|
| 459 | + $unicode_cmap_offset = 0; |
|
| 460 | + for ($i=0;$i<$cmapTableCount;$i++) { |
|
| 461 | + $platformID = $this->read_ushort(); |
|
| 462 | + $encodingID = $this->read_ushort(); |
|
| 463 | + $offset = $this->read_ulong(); |
|
| 464 | + $save_pos = $this->_pos; |
|
| 465 | + if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
|
| 466 | + $format = $this->get_ushort($cmap_offset + $offset); |
|
| 467 | + if ($format == 4) { |
|
| 468 | + if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; |
|
| 469 | + break; |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | + $this->seek($save_pos ); |
|
| 473 | + } |
|
| 474 | + if (!$unicode_cmap_offset) |
|
| 475 | + die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 476 | + |
|
| 477 | + |
|
| 478 | + $glyphToChar = array(); |
|
| 479 | + $charToGlyph = array(); |
|
| 480 | + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph ); |
|
| 481 | + |
|
| 482 | + /////////////////////////////////// |
|
| 483 | + // hmtx - Horizontal metrics table |
|
| 484 | + /////////////////////////////////// |
|
| 485 | + $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
|
| 486 | + |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | 489 | |
| 490 | 490 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 491 | 491 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 492 | 492 | |
| 493 | 493 | |
| 494 | - function makeSubset($file, &$subset) { |
|
| 495 | - $this->filename = $file; |
|
| 496 | - $this->fh = fopen($file ,'rb') or die('Can\'t open file ' . $file); |
|
| 497 | - $this->_pos = 0; |
|
| 498 | - $this->charWidths = ''; |
|
| 499 | - $this->glyphPos = array(); |
|
| 500 | - $this->charToGlyph = array(); |
|
| 501 | - $this->tables = array(); |
|
| 502 | - $this->otables = array(); |
|
| 503 | - $this->ascent = 0; |
|
| 504 | - $this->descent = 0; |
|
| 505 | - $this->skip(4); |
|
| 506 | - $this->maxUni = 0; |
|
| 507 | - $this->readTableDirectory(); |
|
| 508 | - |
|
| 509 | - |
|
| 510 | - /////////////////////////////////// |
|
| 511 | - // head - Font header table |
|
| 512 | - /////////////////////////////////// |
|
| 513 | - $this->seek_table("head"); |
|
| 514 | - $this->skip(50); |
|
| 515 | - $indexToLocFormat = $this->read_ushort(); |
|
| 516 | - $glyphDataFormat = $this->read_ushort(); |
|
| 517 | - |
|
| 518 | - /////////////////////////////////// |
|
| 519 | - // hhea - Horizontal header table |
|
| 520 | - /////////////////////////////////// |
|
| 521 | - $this->seek_table("hhea"); |
|
| 522 | - $this->skip(32); |
|
| 523 | - $metricDataFormat = $this->read_ushort(); |
|
| 524 | - $orignHmetrics = $numberOfHMetrics = $this->read_ushort(); |
|
| 525 | - |
|
| 526 | - /////////////////////////////////// |
|
| 527 | - // maxp - Maximum profile table |
|
| 528 | - /////////////////////////////////// |
|
| 529 | - $this->seek_table("maxp"); |
|
| 530 | - $this->skip(4); |
|
| 531 | - $numGlyphs = $this->read_ushort(); |
|
| 532 | - |
|
| 533 | - |
|
| 534 | - /////////////////////////////////// |
|
| 535 | - // cmap - Character to glyph index mapping table |
|
| 536 | - /////////////////////////////////// |
|
| 537 | - $cmap_offset = $this->seek_table("cmap"); |
|
| 538 | - $this->skip(2); |
|
| 539 | - $cmapTableCount = $this->read_ushort(); |
|
| 540 | - $unicode_cmap_offset = 0; |
|
| 541 | - for ($i=0;$i<$cmapTableCount;$i++) { |
|
| 542 | - $platformID = $this->read_ushort(); |
|
| 543 | - $encodingID = $this->read_ushort(); |
|
| 544 | - $offset = $this->read_ulong(); |
|
| 545 | - $save_pos = $this->_pos; |
|
| 546 | - if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
|
| 547 | - $format = $this->get_ushort($cmap_offset + $offset); |
|
| 548 | - if ($format == 4) { |
|
| 549 | - $unicode_cmap_offset = $cmap_offset + $offset; |
|
| 550 | - break; |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - $this->seek($save_pos ); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - if (!$unicode_cmap_offset) |
|
| 557 | - die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 558 | - |
|
| 559 | - |
|
| 560 | - $glyphToChar = array(); |
|
| 561 | - $charToGlyph = array(); |
|
| 562 | - $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph ); |
|
| 563 | - |
|
| 564 | - $this->charToGlyph = $charToGlyph; |
|
| 565 | - |
|
| 566 | - /////////////////////////////////// |
|
| 567 | - // hmtx - Horizontal metrics table |
|
| 568 | - /////////////////////////////////// |
|
| 569 | - $scale = 1; // not used |
|
| 570 | - $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
|
| 571 | - |
|
| 572 | - /////////////////////////////////// |
|
| 573 | - // loca - Index to location |
|
| 574 | - /////////////////////////////////// |
|
| 575 | - $this->getLOCA($indexToLocFormat, $numGlyphs); |
|
| 576 | - |
|
| 577 | - $subsetglyphs = array(0=>0); |
|
| 578 | - $subsetCharToGlyph = array(); |
|
| 579 | - foreach($subset AS $code) { |
|
| 580 | - if (isset($this->charToGlyph[$code])) { |
|
| 581 | - $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode |
|
| 582 | - $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID |
|
| 583 | - |
|
| 584 | - } |
|
| 585 | - $this->maxUni = max($this->maxUni, $code); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - list($start,$dummy) = $this->get_table_pos('glyf'); |
|
| 589 | - |
|
| 590 | - $glyphSet = array(); |
|
| 591 | - ksort($subsetglyphs); |
|
| 592 | - $n = 0; |
|
| 593 | - $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. |
|
| 594 | - foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 595 | - $fsLastCharIndex = max($fsLastCharIndex , $uni); |
|
| 596 | - $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID |
|
| 597 | - $n++; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - ksort($subsetCharToGlyph); |
|
| 601 | - foreach($subsetCharToGlyph AS $uni => $originalGlyphIdx) { |
|
| 602 | - $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx] ; |
|
| 603 | - } |
|
| 604 | - $this->codeToGlyph = $codeToGlyph; |
|
| 605 | - |
|
| 606 | - ksort($subsetglyphs); |
|
| 607 | - foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 608 | - $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs); |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - $numGlyphs = $numberOfHMetrics = count($subsetglyphs ); |
|
| 612 | - |
|
| 613 | - //tables copied from the original |
|
| 614 | - $tags = array ('name'); |
|
| 615 | - foreach($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } |
|
| 616 | - $tags = array ('cvt ', 'fpgm', 'prep', 'gasp'); |
|
| 617 | - foreach($tags AS $tag) { |
|
| 618 | - if (isset($this->tables[$tag])) { $this->add($tag, $this->get_table($tag)); } |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - // post - PostScript |
|
| 622 | - $opost = $this->get_table('post'); |
|
| 623 | - $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"; |
|
| 624 | - $this->add('post', $post); |
|
| 625 | - |
|
| 626 | - // Sort CID2GID map into segments of contiguous codes |
|
| 627 | - ksort($codeToGlyph); |
|
| 628 | - unset($codeToGlyph[0]); |
|
| 629 | - //unset($codeToGlyph[65535]); |
|
| 630 | - $rangeid = 0; |
|
| 631 | - $range = array(); |
|
| 632 | - $prevcid = -2; |
|
| 633 | - $prevglidx = -1; |
|
| 634 | - // for each character |
|
| 635 | - foreach ($codeToGlyph as $cid => $glidx) { |
|
| 636 | - if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) { |
|
| 637 | - $range[$rangeid][] = $glidx; |
|
| 638 | - } else { |
|
| 639 | - // new range |
|
| 640 | - $rangeid = $cid; |
|
| 641 | - $range[$rangeid] = array(); |
|
| 642 | - $range[$rangeid][] = $glidx; |
|
| 643 | - } |
|
| 644 | - $prevcid = $cid; |
|
| 645 | - $prevglidx = $glidx; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - // cmap - Character to glyph mapping - Format 4 (MS / ) |
|
| 649 | - $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF |
|
| 650 | - $searchRange = 1; |
|
| 651 | - $entrySelector = 0; |
|
| 652 | - while ($searchRange * 2 <= $segCount ) { |
|
| 653 | - $searchRange = $searchRange * 2; |
|
| 654 | - $entrySelector = $entrySelector + 1; |
|
| 655 | - } |
|
| 656 | - $searchRange = $searchRange * 2; |
|
| 657 | - $rangeShift = $segCount * 2 - $searchRange; |
|
| 658 | - $length = 16 + (8*$segCount ) + ($numGlyphs+1); |
|
| 659 | - $cmap = array(0, 1, // Index : version, number of encoding subtables |
|
| 660 | - 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) |
|
| 661 | - 0, 12, // Encoding Subtable : offset (hi,lo) |
|
| 662 | - 4, $length, 0, // Format 4 Mapping subtable: format, length, language |
|
| 663 | - $segCount*2, |
|
| 664 | - $searchRange, |
|
| 665 | - $entrySelector, |
|
| 666 | - $rangeShift); |
|
| 667 | - |
|
| 668 | - // endCode(s) |
|
| 669 | - foreach($range AS $start=>$subrange) { |
|
| 670 | - $endCode = $start + (count($subrange)-1); |
|
| 671 | - $cmap[] = $endCode; // endCode(s) |
|
| 672 | - } |
|
| 673 | - $cmap[] = 0xFFFF; // endCode of last Segment |
|
| 674 | - $cmap[] = 0; // reservedPad |
|
| 675 | - |
|
| 676 | - // startCode(s) |
|
| 677 | - foreach($range AS $start=>$subrange) { |
|
| 678 | - $cmap[] = $start; // startCode(s) |
|
| 679 | - } |
|
| 680 | - $cmap[] = 0xFFFF; // startCode of last Segment |
|
| 681 | - // idDelta(s) |
|
| 682 | - foreach($range AS $start=>$subrange) { |
|
| 683 | - $idDelta = -($start-$subrange[0]); |
|
| 684 | - $n += count($subrange); |
|
| 685 | - $cmap[] = $idDelta; // idDelta(s) |
|
| 686 | - } |
|
| 687 | - $cmap[] = 1; // idDelta of last Segment |
|
| 688 | - // idRangeOffset(s) |
|
| 689 | - foreach($range AS $subrange) { |
|
| 690 | - $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 |
|
| 691 | - |
|
| 692 | - } |
|
| 693 | - $cmap[] = 0; // idRangeOffset of last Segment |
|
| 694 | - foreach($range AS $subrange) { |
|
| 695 | - foreach($subrange AS $glidx) { |
|
| 696 | - $cmap[] = $glidx; |
|
| 697 | - } |
|
| 698 | - } |
|
| 699 | - $cmap[] = 0; // Mapping for last character |
|
| 700 | - $cmapstr = ''; |
|
| 701 | - foreach($cmap AS $cm) { $cmapstr .= pack("n",$cm); } |
|
| 702 | - $this->add('cmap', $cmapstr); |
|
| 703 | - |
|
| 704 | - |
|
| 705 | - // glyf - Glyph data |
|
| 706 | - list($glyfOffset,$glyfLength) = $this->get_table_pos('glyf'); |
|
| 707 | - if ($glyfLength < $this->maxStrLenRead) { |
|
| 708 | - $glyphData = $this->get_table('glyf'); |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - $offsets = array(); |
|
| 712 | - $glyf = ''; |
|
| 713 | - $pos = 0; |
|
| 714 | - |
|
| 715 | - $hmtxstr = ''; |
|
| 716 | - $xMinT = 0; |
|
| 717 | - $yMinT = 0; |
|
| 718 | - $xMaxT = 0; |
|
| 719 | - $yMaxT = 0; |
|
| 720 | - $advanceWidthMax = 0; |
|
| 721 | - $minLeftSideBearing = 0; |
|
| 722 | - $minRightSideBearing = 0; |
|
| 723 | - $xMaxExtent = 0; |
|
| 724 | - $maxPoints = 0; // points in non-compound glyph |
|
| 725 | - $maxContours = 0; // contours in non-compound glyph |
|
| 726 | - $maxComponentPoints = 0; // points in compound glyph |
|
| 727 | - $maxComponentContours = 0; // contours in compound glyph |
|
| 728 | - $maxComponentElements = 0; // number of glyphs referenced at top level |
|
| 729 | - $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs |
|
| 730 | - $this->glyphdata = array(); |
|
| 731 | - |
|
| 732 | - foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 733 | - // hmtx - Horizontal Metrics |
|
| 734 | - $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx); |
|
| 735 | - $hmtxstr .= $hm; |
|
| 736 | - |
|
| 737 | - $offsets[] = $pos; |
|
| 738 | - $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
|
| 739 | - $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
|
| 740 | - if ($glyfLength < $this->maxStrLenRead) { |
|
| 741 | - $data = substr($glyphData,$glyphPos,$glyphLen); |
|
| 742 | - } |
|
| 743 | - else { |
|
| 744 | - if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset+$glyphPos,$glyphLen); |
|
| 745 | - else $data = ''; |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - if ($glyphLen > 0) { |
|
| 749 | - $up = unpack("n", substr($data,0,2)); |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - if ($glyphLen > 2 && ($up[1] & (1 << 15)) ) { // If number of contours <= -1 i.e. composiste glyph |
|
| 753 | - $pos_in_glyph = 10; |
|
| 754 | - $flags = GF_MORE; |
|
| 755 | - $nComponentElements = 0; |
|
| 756 | - while ($flags & GF_MORE) { |
|
| 757 | - $nComponentElements += 1; // number of glyphs referenced at top level |
|
| 758 | - $up = unpack("n", substr($data,$pos_in_glyph,2)); |
|
| 759 | - $flags = $up[1]; |
|
| 760 | - $up = unpack("n", substr($data,$pos_in_glyph+2,2)); |
|
| 761 | - $glyphIdx = $up[1]; |
|
| 762 | - $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; |
|
| 763 | - $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); |
|
| 764 | - $pos_in_glyph += 4; |
|
| 765 | - if ($flags & GF_WORDS) { $pos_in_glyph += 4; } |
|
| 766 | - else { $pos_in_glyph += 2; } |
|
| 767 | - if ($flags & GF_SCALE) { $pos_in_glyph += 2; } |
|
| 768 | - else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } |
|
| 769 | - else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } |
|
| 770 | - } |
|
| 771 | - $maxComponentElements = max($maxComponentElements, $nComponentElements); |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - $glyf .= $data; |
|
| 775 | - $pos += $glyphLen; |
|
| 776 | - if ($pos % 4 != 0) { |
|
| 777 | - $padding = 4 - ($pos % 4); |
|
| 778 | - $glyf .= str_repeat("\0",$padding); |
|
| 779 | - $pos += $padding; |
|
| 780 | - } |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - $offsets[] = $pos; |
|
| 784 | - $this->add('glyf', $glyf); |
|
| 785 | - |
|
| 786 | - // hmtx - Horizontal Metrics |
|
| 787 | - $this->add('hmtx', $hmtxstr); |
|
| 788 | - |
|
| 789 | - // loca - Index to location |
|
| 790 | - $locastr = ''; |
|
| 791 | - if ((($pos + 1) >> 1) > 0xFFFF) { |
|
| 792 | - $indexToLocFormat = 1; // long format |
|
| 793 | - foreach($offsets AS $offset) { $locastr .= pack("N",$offset); } |
|
| 794 | - } |
|
| 795 | - else { |
|
| 796 | - $indexToLocFormat = 0; // short format |
|
| 797 | - foreach($offsets AS $offset) { $locastr .= pack("n",($offset/2)); } |
|
| 798 | - } |
|
| 799 | - $this->add('loca', $locastr); |
|
| 800 | - |
|
| 801 | - // head - Font header |
|
| 802 | - $head = $this->get_table('head'); |
|
| 803 | - $head = $this->_set_ushort($head, 50, $indexToLocFormat); |
|
| 804 | - $this->add('head', $head); |
|
| 805 | - |
|
| 806 | - |
|
| 807 | - // hhea - Horizontal Header |
|
| 808 | - $hhea = $this->get_table('hhea'); |
|
| 809 | - $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics); |
|
| 810 | - $this->add('hhea', $hhea); |
|
| 811 | - |
|
| 812 | - // maxp - Maximum Profile |
|
| 813 | - $maxp = $this->get_table('maxp'); |
|
| 814 | - $maxp = $this->_set_ushort($maxp, 4, $numGlyphs); |
|
| 815 | - $this->add('maxp', $maxp); |
|
| 816 | - |
|
| 817 | - |
|
| 818 | - // OS/2 - OS/2 |
|
| 819 | - $os2 = $this->get_table('OS/2'); |
|
| 820 | - $this->add('OS/2', $os2 ); |
|
| 821 | - |
|
| 822 | - fclose($this->fh); |
|
| 823 | - |
|
| 824 | - // Put the TTF file together |
|
| 825 | - $stm = ''; |
|
| 826 | - $this->endTTFile($stm); |
|
| 827 | - return $stm ; |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - ////////////////////////////////////////////////////////////////////////////////// |
|
| 831 | - // Recursively get composite glyph data |
|
| 832 | - function getGlyphData($originalGlyphIdx, &$maxdepth, &$depth, &$points, &$contours) { |
|
| 833 | - $depth++; |
|
| 834 | - $maxdepth = max($maxdepth, $depth); |
|
| 835 | - if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) { |
|
| 836 | - foreach($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { |
|
| 837 | - $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); |
|
| 838 | - } |
|
| 839 | - } |
|
| 840 | - else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) { // simple |
|
| 841 | - $contours += $this->glyphdata[$originalGlyphIdx]['nContours']; |
|
| 842 | - $points += $this->glyphdata[$originalGlyphIdx]['nPoints']; |
|
| 843 | - } |
|
| 844 | - $depth--; |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - |
|
| 848 | - ////////////////////////////////////////////////////////////////////////////////// |
|
| 849 | - // Recursively get composite glyphs |
|
| 850 | - function getGlyphs($originalGlyphIdx, &$start, &$glyphSet, &$subsetglyphs) { |
|
| 851 | - $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
|
| 852 | - $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
|
| 853 | - if (!$glyphLen) { |
|
| 854 | - return; |
|
| 855 | - } |
|
| 856 | - $this->seek($start + $glyphPos); |
|
| 857 | - $numberOfContours = $this->read_short(); |
|
| 858 | - if ($numberOfContours < 0) { |
|
| 859 | - $this->skip(8); |
|
| 860 | - $flags = GF_MORE; |
|
| 861 | - while ($flags & GF_MORE) { |
|
| 862 | - $flags = $this->read_ushort(); |
|
| 863 | - $glyphIdx = $this->read_ushort(); |
|
| 864 | - if (!isset($glyphSet[$glyphIdx])) { |
|
| 865 | - $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID |
|
| 866 | - $subsetglyphs[$glyphIdx] = true; |
|
| 867 | - } |
|
| 868 | - $savepos = ftell($this->fh); |
|
| 869 | - $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs); |
|
| 870 | - $this->seek($savepos); |
|
| 871 | - if ($flags & GF_WORDS) |
|
| 872 | - $this->skip(4); |
|
| 873 | - else |
|
| 874 | - $this->skip(2); |
|
| 875 | - if ($flags & GF_SCALE) |
|
| 876 | - $this->skip(2); |
|
| 877 | - else if ($flags & GF_XYSCALE) |
|
| 878 | - $this->skip(4); |
|
| 879 | - else if ($flags & GF_TWOBYTWO) |
|
| 880 | - $this->skip(8); |
|
| 881 | - } |
|
| 882 | - } |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - ////////////////////////////////////////////////////////////////////////////////// |
|
| 886 | - |
|
| 887 | - function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { |
|
| 888 | - $start = $this->seek_table("hmtx"); |
|
| 889 | - $aw = 0; |
|
| 890 | - $this->charWidths = str_pad('', 256*256*2, "\x00"); |
|
| 891 | - $nCharWidths = 0; |
|
| 892 | - if (($numberOfHMetrics*4) < $this->maxStrLenRead) { |
|
| 893 | - $data = $this->get_chunk($start,($numberOfHMetrics*4)); |
|
| 894 | - $arr = unpack("n*", $data); |
|
| 895 | - } |
|
| 896 | - else { $this->seek($start); } |
|
| 897 | - for( $glyph=0; $glyph<$numberOfHMetrics; $glyph++) { |
|
| 898 | - |
|
| 899 | - if (($numberOfHMetrics*4) < $this->maxStrLenRead) { |
|
| 900 | - $aw = $arr[($glyph*2)+1]; |
|
| 901 | - } |
|
| 902 | - else { |
|
| 903 | - $aw = $this->read_ushort(); |
|
| 904 | - $lsb = $this->read_ushort(); |
|
| 905 | - } |
|
| 906 | - if (isset($glyphToChar[$glyph]) || $glyph == 0) { |
|
| 907 | - |
|
| 908 | - if ($aw >= (1 << 15) ) { $aw = 0; } // 1.03 Some (arabic) fonts have -ve values for width |
|
| 909 | - // although should be unsigned value - comes out as e.g. 65108 (intended -50) |
|
| 910 | - if ($glyph == 0) { |
|
| 911 | - $this->defaultWidth = $scale*$aw; |
|
| 912 | - continue; |
|
| 913 | - } |
|
| 914 | - foreach($glyphToChar[$glyph] AS $char) { |
|
| 915 | - if ($char != 0 && $char != 65535) { |
|
| 916 | - $w = intval(round($scale*$aw)); |
|
| 917 | - if ($w == 0) { $w = 65535; } |
|
| 918 | - if ($char < 196608) { |
|
| 919 | - $this->charWidths[$char*2] = chr($w >> 8); |
|
| 920 | - $this->charWidths[$char*2 + 1] = chr($w & 0xFF); |
|
| 921 | - $nCharWidths++; |
|
| 922 | - } |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - } |
|
| 926 | - } |
|
| 927 | - $data = $this->get_chunk(($start+$numberOfHMetrics*4),($numGlyphs*2)); |
|
| 928 | - $arr = unpack("n*", $data); |
|
| 929 | - $diff = $numGlyphs-$numberOfHMetrics; |
|
| 930 | - for( $pos=0; $pos<$diff; $pos++) { |
|
| 931 | - $glyph = $pos + $numberOfHMetrics; |
|
| 932 | - if (isset($glyphToChar[$glyph])) { |
|
| 933 | - foreach($glyphToChar[$glyph] AS $char) { |
|
| 934 | - if ($char != 0 && $char != 65535) { |
|
| 935 | - $w = intval(round($scale*$aw)); |
|
| 936 | - if ($w == 0) { $w = 65535; } |
|
| 937 | - if ($char < 196608) { |
|
| 938 | - $this->charWidths[$char*2] = chr($w >> 8); |
|
| 939 | - $this->charWidths[$char*2 + 1] = chr($w & 0xFF); |
|
| 940 | - $nCharWidths++; |
|
| 941 | - } |
|
| 942 | - } |
|
| 943 | - } |
|
| 944 | - } |
|
| 945 | - } |
|
| 946 | - // NB 65535 is a set width of 0 |
|
| 947 | - // First bytes define number of chars in font |
|
| 948 | - $this->charWidths[0] = chr($nCharWidths >> 8); |
|
| 949 | - $this->charWidths[1] = chr($nCharWidths & 0xFF); |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - function getHMetric($numberOfHMetrics, $gid) { |
|
| 953 | - $start = $this->seek_table("hmtx"); |
|
| 954 | - if ($gid < $numberOfHMetrics) { |
|
| 955 | - $this->seek($start+($gid*4)); |
|
| 956 | - $hm = fread($this->fh,4); |
|
| 957 | - } |
|
| 958 | - else { |
|
| 959 | - $this->seek($start+(($numberOfHMetrics-1)*4)); |
|
| 960 | - $hm = fread($this->fh,2); |
|
| 961 | - $this->seek($start+($numberOfHMetrics*2)+($gid*2)); |
|
| 962 | - $hm .= fread($this->fh,2); |
|
| 963 | - } |
|
| 964 | - return $hm; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - function getLOCA($indexToLocFormat, $numGlyphs) { |
|
| 968 | - $start = $this->seek_table('loca'); |
|
| 969 | - $this->glyphPos = array(); |
|
| 970 | - if ($indexToLocFormat == 0) { |
|
| 971 | - $data = $this->get_chunk($start,($numGlyphs*2)+2); |
|
| 972 | - $arr = unpack("n*", $data); |
|
| 973 | - for ($n=0; $n<=$numGlyphs; $n++) { |
|
| 974 | - $this->glyphPos[] = ($arr[$n+1] * 2); |
|
| 975 | - } |
|
| 976 | - } |
|
| 977 | - else if ($indexToLocFormat == 1) { |
|
| 978 | - $data = $this->get_chunk($start,($numGlyphs*4)+4); |
|
| 979 | - $arr = unpack("N*", $data); |
|
| 980 | - for ($n=0; $n<=$numGlyphs; $n++) { |
|
| 981 | - $this->glyphPos[] = ($arr[$n+1]); |
|
| 982 | - } |
|
| 983 | - } |
|
| 984 | - else |
|
| 985 | - die('Unknown location table format '.$indexToLocFormat); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - |
|
| 989 | - // CMAP Format 4 |
|
| 990 | - function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { |
|
| 991 | - $this->maxUniChar = 0; |
|
| 992 | - $this->seek($unicode_cmap_offset + 2); |
|
| 993 | - $length = $this->read_ushort(); |
|
| 994 | - $limit = $unicode_cmap_offset + $length; |
|
| 995 | - $this->skip(2); |
|
| 996 | - |
|
| 997 | - $segCount = $this->read_ushort() / 2; |
|
| 998 | - $this->skip(6); |
|
| 999 | - $endCount = array(); |
|
| 1000 | - for($i=0; $i<$segCount; $i++) { $endCount[] = $this->read_ushort(); } |
|
| 1001 | - $this->skip(2); |
|
| 1002 | - $startCount = array(); |
|
| 1003 | - for($i=0; $i<$segCount; $i++) { $startCount[] = $this->read_ushort(); } |
|
| 1004 | - $idDelta = array(); |
|
| 1005 | - for($i=0; $i<$segCount; $i++) { $idDelta[] = $this->read_short(); } // ???? was unsigned short |
|
| 1006 | - $idRangeOffset_start = $this->_pos; |
|
| 1007 | - $idRangeOffset = array(); |
|
| 1008 | - for($i=0; $i<$segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } |
|
| 1009 | - |
|
| 1010 | - for ($n=0;$n<$segCount;$n++) { |
|
| 1011 | - $endpoint = ($endCount[$n] + 1); |
|
| 1012 | - for ($unichar=$startCount[$n];$unichar<$endpoint;$unichar++) { |
|
| 1013 | - if ($idRangeOffset[$n] == 0) |
|
| 1014 | - $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
|
| 1015 | - else { |
|
| 1016 | - $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; |
|
| 1017 | - $offset = $idRangeOffset_start + 2 * $n + $offset; |
|
| 1018 | - if ($offset >= $limit) |
|
| 1019 | - $glyph = 0; |
|
| 1020 | - else { |
|
| 1021 | - $glyph = $this->get_ushort($offset); |
|
| 1022 | - if ($glyph != 0) |
|
| 1023 | - $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; |
|
| 1024 | - } |
|
| 1025 | - } |
|
| 1026 | - $charToGlyph[$unichar] = $glyph; |
|
| 1027 | - if ($unichar < 196608) { $this->maxUniChar = max($unichar,$this->maxUniChar); } |
|
| 1028 | - $glyphToChar[$glyph][] = $unichar; |
|
| 1029 | - } |
|
| 1030 | - } |
|
| 1031 | - } |
|
| 1032 | - |
|
| 1033 | - |
|
| 1034 | - // Put the TTF file together |
|
| 1035 | - function endTTFile(&$stm) { |
|
| 1036 | - $stm = ''; |
|
| 1037 | - $numTables = count($this->otables); |
|
| 1038 | - $searchRange = 1; |
|
| 1039 | - $entrySelector = 0; |
|
| 1040 | - while ($searchRange * 2 <= $numTables) { |
|
| 1041 | - $searchRange = $searchRange * 2; |
|
| 1042 | - $entrySelector = $entrySelector + 1; |
|
| 1043 | - } |
|
| 1044 | - $searchRange = $searchRange * 16; |
|
| 1045 | - $rangeShift = $numTables * 16 - $searchRange; |
|
| 1046 | - |
|
| 1047 | - // Header |
|
| 1048 | - if (_TTF_MAC_HEADER) { |
|
| 1049 | - $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac |
|
| 1050 | - } |
|
| 1051 | - else { |
|
| 1052 | - $stm .= (pack("Nnnnn", 0x00010000 , $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows |
|
| 1053 | - } |
|
| 1054 | - |
|
| 1055 | - // Table directory |
|
| 1056 | - $tables = $this->otables; |
|
| 1057 | - |
|
| 1058 | - ksort ($tables); |
|
| 1059 | - $offset = 12 + $numTables * 16; |
|
| 1060 | - foreach ($tables AS $tag=>$data) { |
|
| 1061 | - if ($tag == 'head') { $head_start = $offset; } |
|
| 1062 | - $stm .= $tag; |
|
| 1063 | - $checksum = $this->calcChecksum($data); |
|
| 1064 | - $stm .= pack("nn", $checksum[0],$checksum[1]); |
|
| 1065 | - $stm .= pack("NN", $offset, strlen($data)); |
|
| 1066 | - $paddedLength = (strlen($data)+3)&~3; |
|
| 1067 | - $offset = $offset + $paddedLength; |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - // Table data |
|
| 1071 | - foreach ($tables AS $tag=>$data) { |
|
| 1072 | - $data .= "\0\0\0"; |
|
| 1073 | - $stm .= substr($data,0,(strlen($data)&~3)); |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - $checksum = $this->calcChecksum($stm); |
|
| 1077 | - $checksum = $this->sub32(array(0xB1B0,0xAFBA), $checksum); |
|
| 1078 | - $chk = pack("nn", $checksum[0],$checksum[1]); |
|
| 1079 | - $stm = $this->splice($stm,($head_start + 8),$chk); |
|
| 1080 | - return $stm ; |
|
| 1081 | - } |
|
| 494 | + function makeSubset($file, &$subset) { |
|
| 495 | + $this->filename = $file; |
|
| 496 | + $this->fh = fopen($file ,'rb') or die('Can\'t open file ' . $file); |
|
| 497 | + $this->_pos = 0; |
|
| 498 | + $this->charWidths = ''; |
|
| 499 | + $this->glyphPos = array(); |
|
| 500 | + $this->charToGlyph = array(); |
|
| 501 | + $this->tables = array(); |
|
| 502 | + $this->otables = array(); |
|
| 503 | + $this->ascent = 0; |
|
| 504 | + $this->descent = 0; |
|
| 505 | + $this->skip(4); |
|
| 506 | + $this->maxUni = 0; |
|
| 507 | + $this->readTableDirectory(); |
|
| 508 | + |
|
| 509 | + |
|
| 510 | + /////////////////////////////////// |
|
| 511 | + // head - Font header table |
|
| 512 | + /////////////////////////////////// |
|
| 513 | + $this->seek_table("head"); |
|
| 514 | + $this->skip(50); |
|
| 515 | + $indexToLocFormat = $this->read_ushort(); |
|
| 516 | + $glyphDataFormat = $this->read_ushort(); |
|
| 517 | + |
|
| 518 | + /////////////////////////////////// |
|
| 519 | + // hhea - Horizontal header table |
|
| 520 | + /////////////////////////////////// |
|
| 521 | + $this->seek_table("hhea"); |
|
| 522 | + $this->skip(32); |
|
| 523 | + $metricDataFormat = $this->read_ushort(); |
|
| 524 | + $orignHmetrics = $numberOfHMetrics = $this->read_ushort(); |
|
| 525 | + |
|
| 526 | + /////////////////////////////////// |
|
| 527 | + // maxp - Maximum profile table |
|
| 528 | + /////////////////////////////////// |
|
| 529 | + $this->seek_table("maxp"); |
|
| 530 | + $this->skip(4); |
|
| 531 | + $numGlyphs = $this->read_ushort(); |
|
| 532 | + |
|
| 533 | + |
|
| 534 | + /////////////////////////////////// |
|
| 535 | + // cmap - Character to glyph index mapping table |
|
| 536 | + /////////////////////////////////// |
|
| 537 | + $cmap_offset = $this->seek_table("cmap"); |
|
| 538 | + $this->skip(2); |
|
| 539 | + $cmapTableCount = $this->read_ushort(); |
|
| 540 | + $unicode_cmap_offset = 0; |
|
| 541 | + for ($i=0;$i<$cmapTableCount;$i++) { |
|
| 542 | + $platformID = $this->read_ushort(); |
|
| 543 | + $encodingID = $this->read_ushort(); |
|
| 544 | + $offset = $this->read_ulong(); |
|
| 545 | + $save_pos = $this->_pos; |
|
| 546 | + if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
|
| 547 | + $format = $this->get_ushort($cmap_offset + $offset); |
|
| 548 | + if ($format == 4) { |
|
| 549 | + $unicode_cmap_offset = $cmap_offset + $offset; |
|
| 550 | + break; |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | + $this->seek($save_pos ); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + if (!$unicode_cmap_offset) |
|
| 557 | + die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 558 | + |
|
| 559 | + |
|
| 560 | + $glyphToChar = array(); |
|
| 561 | + $charToGlyph = array(); |
|
| 562 | + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph ); |
|
| 563 | + |
|
| 564 | + $this->charToGlyph = $charToGlyph; |
|
| 565 | + |
|
| 566 | + /////////////////////////////////// |
|
| 567 | + // hmtx - Horizontal metrics table |
|
| 568 | + /////////////////////////////////// |
|
| 569 | + $scale = 1; // not used |
|
| 570 | + $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
|
| 571 | + |
|
| 572 | + /////////////////////////////////// |
|
| 573 | + // loca - Index to location |
|
| 574 | + /////////////////////////////////// |
|
| 575 | + $this->getLOCA($indexToLocFormat, $numGlyphs); |
|
| 576 | + |
|
| 577 | + $subsetglyphs = array(0=>0); |
|
| 578 | + $subsetCharToGlyph = array(); |
|
| 579 | + foreach($subset AS $code) { |
|
| 580 | + if (isset($this->charToGlyph[$code])) { |
|
| 581 | + $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode |
|
| 582 | + $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID |
|
| 583 | + |
|
| 584 | + } |
|
| 585 | + $this->maxUni = max($this->maxUni, $code); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + list($start,$dummy) = $this->get_table_pos('glyf'); |
|
| 589 | + |
|
| 590 | + $glyphSet = array(); |
|
| 591 | + ksort($subsetglyphs); |
|
| 592 | + $n = 0; |
|
| 593 | + $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. |
|
| 594 | + foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 595 | + $fsLastCharIndex = max($fsLastCharIndex , $uni); |
|
| 596 | + $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID |
|
| 597 | + $n++; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + ksort($subsetCharToGlyph); |
|
| 601 | + foreach($subsetCharToGlyph AS $uni => $originalGlyphIdx) { |
|
| 602 | + $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx] ; |
|
| 603 | + } |
|
| 604 | + $this->codeToGlyph = $codeToGlyph; |
|
| 605 | + |
|
| 606 | + ksort($subsetglyphs); |
|
| 607 | + foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 608 | + $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs); |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + $numGlyphs = $numberOfHMetrics = count($subsetglyphs ); |
|
| 612 | + |
|
| 613 | + //tables copied from the original |
|
| 614 | + $tags = array ('name'); |
|
| 615 | + foreach($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } |
|
| 616 | + $tags = array ('cvt ', 'fpgm', 'prep', 'gasp'); |
|
| 617 | + foreach($tags AS $tag) { |
|
| 618 | + if (isset($this->tables[$tag])) { $this->add($tag, $this->get_table($tag)); } |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + // post - PostScript |
|
| 622 | + $opost = $this->get_table('post'); |
|
| 623 | + $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"; |
|
| 624 | + $this->add('post', $post); |
|
| 625 | + |
|
| 626 | + // Sort CID2GID map into segments of contiguous codes |
|
| 627 | + ksort($codeToGlyph); |
|
| 628 | + unset($codeToGlyph[0]); |
|
| 629 | + //unset($codeToGlyph[65535]); |
|
| 630 | + $rangeid = 0; |
|
| 631 | + $range = array(); |
|
| 632 | + $prevcid = -2; |
|
| 633 | + $prevglidx = -1; |
|
| 634 | + // for each character |
|
| 635 | + foreach ($codeToGlyph as $cid => $glidx) { |
|
| 636 | + if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) { |
|
| 637 | + $range[$rangeid][] = $glidx; |
|
| 638 | + } else { |
|
| 639 | + // new range |
|
| 640 | + $rangeid = $cid; |
|
| 641 | + $range[$rangeid] = array(); |
|
| 642 | + $range[$rangeid][] = $glidx; |
|
| 643 | + } |
|
| 644 | + $prevcid = $cid; |
|
| 645 | + $prevglidx = $glidx; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + // cmap - Character to glyph mapping - Format 4 (MS / ) |
|
| 649 | + $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF |
|
| 650 | + $searchRange = 1; |
|
| 651 | + $entrySelector = 0; |
|
| 652 | + while ($searchRange * 2 <= $segCount ) { |
|
| 653 | + $searchRange = $searchRange * 2; |
|
| 654 | + $entrySelector = $entrySelector + 1; |
|
| 655 | + } |
|
| 656 | + $searchRange = $searchRange * 2; |
|
| 657 | + $rangeShift = $segCount * 2 - $searchRange; |
|
| 658 | + $length = 16 + (8*$segCount ) + ($numGlyphs+1); |
|
| 659 | + $cmap = array(0, 1, // Index : version, number of encoding subtables |
|
| 660 | + 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) |
|
| 661 | + 0, 12, // Encoding Subtable : offset (hi,lo) |
|
| 662 | + 4, $length, 0, // Format 4 Mapping subtable: format, length, language |
|
| 663 | + $segCount*2, |
|
| 664 | + $searchRange, |
|
| 665 | + $entrySelector, |
|
| 666 | + $rangeShift); |
|
| 667 | + |
|
| 668 | + // endCode(s) |
|
| 669 | + foreach($range AS $start=>$subrange) { |
|
| 670 | + $endCode = $start + (count($subrange)-1); |
|
| 671 | + $cmap[] = $endCode; // endCode(s) |
|
| 672 | + } |
|
| 673 | + $cmap[] = 0xFFFF; // endCode of last Segment |
|
| 674 | + $cmap[] = 0; // reservedPad |
|
| 675 | + |
|
| 676 | + // startCode(s) |
|
| 677 | + foreach($range AS $start=>$subrange) { |
|
| 678 | + $cmap[] = $start; // startCode(s) |
|
| 679 | + } |
|
| 680 | + $cmap[] = 0xFFFF; // startCode of last Segment |
|
| 681 | + // idDelta(s) |
|
| 682 | + foreach($range AS $start=>$subrange) { |
|
| 683 | + $idDelta = -($start-$subrange[0]); |
|
| 684 | + $n += count($subrange); |
|
| 685 | + $cmap[] = $idDelta; // idDelta(s) |
|
| 686 | + } |
|
| 687 | + $cmap[] = 1; // idDelta of last Segment |
|
| 688 | + // idRangeOffset(s) |
|
| 689 | + foreach($range AS $subrange) { |
|
| 690 | + $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 |
|
| 691 | + |
|
| 692 | + } |
|
| 693 | + $cmap[] = 0; // idRangeOffset of last Segment |
|
| 694 | + foreach($range AS $subrange) { |
|
| 695 | + foreach($subrange AS $glidx) { |
|
| 696 | + $cmap[] = $glidx; |
|
| 697 | + } |
|
| 698 | + } |
|
| 699 | + $cmap[] = 0; // Mapping for last character |
|
| 700 | + $cmapstr = ''; |
|
| 701 | + foreach($cmap AS $cm) { $cmapstr .= pack("n",$cm); } |
|
| 702 | + $this->add('cmap', $cmapstr); |
|
| 703 | + |
|
| 704 | + |
|
| 705 | + // glyf - Glyph data |
|
| 706 | + list($glyfOffset,$glyfLength) = $this->get_table_pos('glyf'); |
|
| 707 | + if ($glyfLength < $this->maxStrLenRead) { |
|
| 708 | + $glyphData = $this->get_table('glyf'); |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + $offsets = array(); |
|
| 712 | + $glyf = ''; |
|
| 713 | + $pos = 0; |
|
| 714 | + |
|
| 715 | + $hmtxstr = ''; |
|
| 716 | + $xMinT = 0; |
|
| 717 | + $yMinT = 0; |
|
| 718 | + $xMaxT = 0; |
|
| 719 | + $yMaxT = 0; |
|
| 720 | + $advanceWidthMax = 0; |
|
| 721 | + $minLeftSideBearing = 0; |
|
| 722 | + $minRightSideBearing = 0; |
|
| 723 | + $xMaxExtent = 0; |
|
| 724 | + $maxPoints = 0; // points in non-compound glyph |
|
| 725 | + $maxContours = 0; // contours in non-compound glyph |
|
| 726 | + $maxComponentPoints = 0; // points in compound glyph |
|
| 727 | + $maxComponentContours = 0; // contours in compound glyph |
|
| 728 | + $maxComponentElements = 0; // number of glyphs referenced at top level |
|
| 729 | + $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs |
|
| 730 | + $this->glyphdata = array(); |
|
| 731 | + |
|
| 732 | + foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 733 | + // hmtx - Horizontal Metrics |
|
| 734 | + $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx); |
|
| 735 | + $hmtxstr .= $hm; |
|
| 736 | + |
|
| 737 | + $offsets[] = $pos; |
|
| 738 | + $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
|
| 739 | + $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
|
| 740 | + if ($glyfLength < $this->maxStrLenRead) { |
|
| 741 | + $data = substr($glyphData,$glyphPos,$glyphLen); |
|
| 742 | + } |
|
| 743 | + else { |
|
| 744 | + if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset+$glyphPos,$glyphLen); |
|
| 745 | + else $data = ''; |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + if ($glyphLen > 0) { |
|
| 749 | + $up = unpack("n", substr($data,0,2)); |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + if ($glyphLen > 2 && ($up[1] & (1 << 15)) ) { // If number of contours <= -1 i.e. composiste glyph |
|
| 753 | + $pos_in_glyph = 10; |
|
| 754 | + $flags = GF_MORE; |
|
| 755 | + $nComponentElements = 0; |
|
| 756 | + while ($flags & GF_MORE) { |
|
| 757 | + $nComponentElements += 1; // number of glyphs referenced at top level |
|
| 758 | + $up = unpack("n", substr($data,$pos_in_glyph,2)); |
|
| 759 | + $flags = $up[1]; |
|
| 760 | + $up = unpack("n", substr($data,$pos_in_glyph+2,2)); |
|
| 761 | + $glyphIdx = $up[1]; |
|
| 762 | + $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; |
|
| 763 | + $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); |
|
| 764 | + $pos_in_glyph += 4; |
|
| 765 | + if ($flags & GF_WORDS) { $pos_in_glyph += 4; } |
|
| 766 | + else { $pos_in_glyph += 2; } |
|
| 767 | + if ($flags & GF_SCALE) { $pos_in_glyph += 2; } |
|
| 768 | + else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } |
|
| 769 | + else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } |
|
| 770 | + } |
|
| 771 | + $maxComponentElements = max($maxComponentElements, $nComponentElements); |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + $glyf .= $data; |
|
| 775 | + $pos += $glyphLen; |
|
| 776 | + if ($pos % 4 != 0) { |
|
| 777 | + $padding = 4 - ($pos % 4); |
|
| 778 | + $glyf .= str_repeat("\0",$padding); |
|
| 779 | + $pos += $padding; |
|
| 780 | + } |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + $offsets[] = $pos; |
|
| 784 | + $this->add('glyf', $glyf); |
|
| 785 | + |
|
| 786 | + // hmtx - Horizontal Metrics |
|
| 787 | + $this->add('hmtx', $hmtxstr); |
|
| 788 | + |
|
| 789 | + // loca - Index to location |
|
| 790 | + $locastr = ''; |
|
| 791 | + if ((($pos + 1) >> 1) > 0xFFFF) { |
|
| 792 | + $indexToLocFormat = 1; // long format |
|
| 793 | + foreach($offsets AS $offset) { $locastr .= pack("N",$offset); } |
|
| 794 | + } |
|
| 795 | + else { |
|
| 796 | + $indexToLocFormat = 0; // short format |
|
| 797 | + foreach($offsets AS $offset) { $locastr .= pack("n",($offset/2)); } |
|
| 798 | + } |
|
| 799 | + $this->add('loca', $locastr); |
|
| 800 | + |
|
| 801 | + // head - Font header |
|
| 802 | + $head = $this->get_table('head'); |
|
| 803 | + $head = $this->_set_ushort($head, 50, $indexToLocFormat); |
|
| 804 | + $this->add('head', $head); |
|
| 805 | + |
|
| 806 | + |
|
| 807 | + // hhea - Horizontal Header |
|
| 808 | + $hhea = $this->get_table('hhea'); |
|
| 809 | + $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics); |
|
| 810 | + $this->add('hhea', $hhea); |
|
| 811 | + |
|
| 812 | + // maxp - Maximum Profile |
|
| 813 | + $maxp = $this->get_table('maxp'); |
|
| 814 | + $maxp = $this->_set_ushort($maxp, 4, $numGlyphs); |
|
| 815 | + $this->add('maxp', $maxp); |
|
| 816 | + |
|
| 817 | + |
|
| 818 | + // OS/2 - OS/2 |
|
| 819 | + $os2 = $this->get_table('OS/2'); |
|
| 820 | + $this->add('OS/2', $os2 ); |
|
| 821 | + |
|
| 822 | + fclose($this->fh); |
|
| 823 | + |
|
| 824 | + // Put the TTF file together |
|
| 825 | + $stm = ''; |
|
| 826 | + $this->endTTFile($stm); |
|
| 827 | + return $stm ; |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + ////////////////////////////////////////////////////////////////////////////////// |
|
| 831 | + // Recursively get composite glyph data |
|
| 832 | + function getGlyphData($originalGlyphIdx, &$maxdepth, &$depth, &$points, &$contours) { |
|
| 833 | + $depth++; |
|
| 834 | + $maxdepth = max($maxdepth, $depth); |
|
| 835 | + if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) { |
|
| 836 | + foreach($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { |
|
| 837 | + $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); |
|
| 838 | + } |
|
| 839 | + } |
|
| 840 | + else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) { // simple |
|
| 841 | + $contours += $this->glyphdata[$originalGlyphIdx]['nContours']; |
|
| 842 | + $points += $this->glyphdata[$originalGlyphIdx]['nPoints']; |
|
| 843 | + } |
|
| 844 | + $depth--; |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + |
|
| 848 | + ////////////////////////////////////////////////////////////////////////////////// |
|
| 849 | + // Recursively get composite glyphs |
|
| 850 | + function getGlyphs($originalGlyphIdx, &$start, &$glyphSet, &$subsetglyphs) { |
|
| 851 | + $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
|
| 852 | + $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
|
| 853 | + if (!$glyphLen) { |
|
| 854 | + return; |
|
| 855 | + } |
|
| 856 | + $this->seek($start + $glyphPos); |
|
| 857 | + $numberOfContours = $this->read_short(); |
|
| 858 | + if ($numberOfContours < 0) { |
|
| 859 | + $this->skip(8); |
|
| 860 | + $flags = GF_MORE; |
|
| 861 | + while ($flags & GF_MORE) { |
|
| 862 | + $flags = $this->read_ushort(); |
|
| 863 | + $glyphIdx = $this->read_ushort(); |
|
| 864 | + if (!isset($glyphSet[$glyphIdx])) { |
|
| 865 | + $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID |
|
| 866 | + $subsetglyphs[$glyphIdx] = true; |
|
| 867 | + } |
|
| 868 | + $savepos = ftell($this->fh); |
|
| 869 | + $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs); |
|
| 870 | + $this->seek($savepos); |
|
| 871 | + if ($flags & GF_WORDS) |
|
| 872 | + $this->skip(4); |
|
| 873 | + else |
|
| 874 | + $this->skip(2); |
|
| 875 | + if ($flags & GF_SCALE) |
|
| 876 | + $this->skip(2); |
|
| 877 | + else if ($flags & GF_XYSCALE) |
|
| 878 | + $this->skip(4); |
|
| 879 | + else if ($flags & GF_TWOBYTWO) |
|
| 880 | + $this->skip(8); |
|
| 881 | + } |
|
| 882 | + } |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + ////////////////////////////////////////////////////////////////////////////////// |
|
| 886 | + |
|
| 887 | + function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { |
|
| 888 | + $start = $this->seek_table("hmtx"); |
|
| 889 | + $aw = 0; |
|
| 890 | + $this->charWidths = str_pad('', 256*256*2, "\x00"); |
|
| 891 | + $nCharWidths = 0; |
|
| 892 | + if (($numberOfHMetrics*4) < $this->maxStrLenRead) { |
|
| 893 | + $data = $this->get_chunk($start,($numberOfHMetrics*4)); |
|
| 894 | + $arr = unpack("n*", $data); |
|
| 895 | + } |
|
| 896 | + else { $this->seek($start); } |
|
| 897 | + for( $glyph=0; $glyph<$numberOfHMetrics; $glyph++) { |
|
| 898 | + |
|
| 899 | + if (($numberOfHMetrics*4) < $this->maxStrLenRead) { |
|
| 900 | + $aw = $arr[($glyph*2)+1]; |
|
| 901 | + } |
|
| 902 | + else { |
|
| 903 | + $aw = $this->read_ushort(); |
|
| 904 | + $lsb = $this->read_ushort(); |
|
| 905 | + } |
|
| 906 | + if (isset($glyphToChar[$glyph]) || $glyph == 0) { |
|
| 907 | + |
|
| 908 | + if ($aw >= (1 << 15) ) { $aw = 0; } // 1.03 Some (arabic) fonts have -ve values for width |
|
| 909 | + // although should be unsigned value - comes out as e.g. 65108 (intended -50) |
|
| 910 | + if ($glyph == 0) { |
|
| 911 | + $this->defaultWidth = $scale*$aw; |
|
| 912 | + continue; |
|
| 913 | + } |
|
| 914 | + foreach($glyphToChar[$glyph] AS $char) { |
|
| 915 | + if ($char != 0 && $char != 65535) { |
|
| 916 | + $w = intval(round($scale*$aw)); |
|
| 917 | + if ($w == 0) { $w = 65535; } |
|
| 918 | + if ($char < 196608) { |
|
| 919 | + $this->charWidths[$char*2] = chr($w >> 8); |
|
| 920 | + $this->charWidths[$char*2 + 1] = chr($w & 0xFF); |
|
| 921 | + $nCharWidths++; |
|
| 922 | + } |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + } |
|
| 926 | + } |
|
| 927 | + $data = $this->get_chunk(($start+$numberOfHMetrics*4),($numGlyphs*2)); |
|
| 928 | + $arr = unpack("n*", $data); |
|
| 929 | + $diff = $numGlyphs-$numberOfHMetrics; |
|
| 930 | + for( $pos=0; $pos<$diff; $pos++) { |
|
| 931 | + $glyph = $pos + $numberOfHMetrics; |
|
| 932 | + if (isset($glyphToChar[$glyph])) { |
|
| 933 | + foreach($glyphToChar[$glyph] AS $char) { |
|
| 934 | + if ($char != 0 && $char != 65535) { |
|
| 935 | + $w = intval(round($scale*$aw)); |
|
| 936 | + if ($w == 0) { $w = 65535; } |
|
| 937 | + if ($char < 196608) { |
|
| 938 | + $this->charWidths[$char*2] = chr($w >> 8); |
|
| 939 | + $this->charWidths[$char*2 + 1] = chr($w & 0xFF); |
|
| 940 | + $nCharWidths++; |
|
| 941 | + } |
|
| 942 | + } |
|
| 943 | + } |
|
| 944 | + } |
|
| 945 | + } |
|
| 946 | + // NB 65535 is a set width of 0 |
|
| 947 | + // First bytes define number of chars in font |
|
| 948 | + $this->charWidths[0] = chr($nCharWidths >> 8); |
|
| 949 | + $this->charWidths[1] = chr($nCharWidths & 0xFF); |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + function getHMetric($numberOfHMetrics, $gid) { |
|
| 953 | + $start = $this->seek_table("hmtx"); |
|
| 954 | + if ($gid < $numberOfHMetrics) { |
|
| 955 | + $this->seek($start+($gid*4)); |
|
| 956 | + $hm = fread($this->fh,4); |
|
| 957 | + } |
|
| 958 | + else { |
|
| 959 | + $this->seek($start+(($numberOfHMetrics-1)*4)); |
|
| 960 | + $hm = fread($this->fh,2); |
|
| 961 | + $this->seek($start+($numberOfHMetrics*2)+($gid*2)); |
|
| 962 | + $hm .= fread($this->fh,2); |
|
| 963 | + } |
|
| 964 | + return $hm; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + function getLOCA($indexToLocFormat, $numGlyphs) { |
|
| 968 | + $start = $this->seek_table('loca'); |
|
| 969 | + $this->glyphPos = array(); |
|
| 970 | + if ($indexToLocFormat == 0) { |
|
| 971 | + $data = $this->get_chunk($start,($numGlyphs*2)+2); |
|
| 972 | + $arr = unpack("n*", $data); |
|
| 973 | + for ($n=0; $n<=$numGlyphs; $n++) { |
|
| 974 | + $this->glyphPos[] = ($arr[$n+1] * 2); |
|
| 975 | + } |
|
| 976 | + } |
|
| 977 | + else if ($indexToLocFormat == 1) { |
|
| 978 | + $data = $this->get_chunk($start,($numGlyphs*4)+4); |
|
| 979 | + $arr = unpack("N*", $data); |
|
| 980 | + for ($n=0; $n<=$numGlyphs; $n++) { |
|
| 981 | + $this->glyphPos[] = ($arr[$n+1]); |
|
| 982 | + } |
|
| 983 | + } |
|
| 984 | + else |
|
| 985 | + die('Unknown location table format '.$indexToLocFormat); |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + |
|
| 989 | + // CMAP Format 4 |
|
| 990 | + function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { |
|
| 991 | + $this->maxUniChar = 0; |
|
| 992 | + $this->seek($unicode_cmap_offset + 2); |
|
| 993 | + $length = $this->read_ushort(); |
|
| 994 | + $limit = $unicode_cmap_offset + $length; |
|
| 995 | + $this->skip(2); |
|
| 996 | + |
|
| 997 | + $segCount = $this->read_ushort() / 2; |
|
| 998 | + $this->skip(6); |
|
| 999 | + $endCount = array(); |
|
| 1000 | + for($i=0; $i<$segCount; $i++) { $endCount[] = $this->read_ushort(); } |
|
| 1001 | + $this->skip(2); |
|
| 1002 | + $startCount = array(); |
|
| 1003 | + for($i=0; $i<$segCount; $i++) { $startCount[] = $this->read_ushort(); } |
|
| 1004 | + $idDelta = array(); |
|
| 1005 | + for($i=0; $i<$segCount; $i++) { $idDelta[] = $this->read_short(); } // ???? was unsigned short |
|
| 1006 | + $idRangeOffset_start = $this->_pos; |
|
| 1007 | + $idRangeOffset = array(); |
|
| 1008 | + for($i=0; $i<$segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } |
|
| 1009 | + |
|
| 1010 | + for ($n=0;$n<$segCount;$n++) { |
|
| 1011 | + $endpoint = ($endCount[$n] + 1); |
|
| 1012 | + for ($unichar=$startCount[$n];$unichar<$endpoint;$unichar++) { |
|
| 1013 | + if ($idRangeOffset[$n] == 0) |
|
| 1014 | + $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
|
| 1015 | + else { |
|
| 1016 | + $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; |
|
| 1017 | + $offset = $idRangeOffset_start + 2 * $n + $offset; |
|
| 1018 | + if ($offset >= $limit) |
|
| 1019 | + $glyph = 0; |
|
| 1020 | + else { |
|
| 1021 | + $glyph = $this->get_ushort($offset); |
|
| 1022 | + if ($glyph != 0) |
|
| 1023 | + $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; |
|
| 1024 | + } |
|
| 1025 | + } |
|
| 1026 | + $charToGlyph[$unichar] = $glyph; |
|
| 1027 | + if ($unichar < 196608) { $this->maxUniChar = max($unichar,$this->maxUniChar); } |
|
| 1028 | + $glyphToChar[$glyph][] = $unichar; |
|
| 1029 | + } |
|
| 1030 | + } |
|
| 1031 | + } |
|
| 1032 | + |
|
| 1033 | + |
|
| 1034 | + // Put the TTF file together |
|
| 1035 | + function endTTFile(&$stm) { |
|
| 1036 | + $stm = ''; |
|
| 1037 | + $numTables = count($this->otables); |
|
| 1038 | + $searchRange = 1; |
|
| 1039 | + $entrySelector = 0; |
|
| 1040 | + while ($searchRange * 2 <= $numTables) { |
|
| 1041 | + $searchRange = $searchRange * 2; |
|
| 1042 | + $entrySelector = $entrySelector + 1; |
|
| 1043 | + } |
|
| 1044 | + $searchRange = $searchRange * 16; |
|
| 1045 | + $rangeShift = $numTables * 16 - $searchRange; |
|
| 1046 | + |
|
| 1047 | + // Header |
|
| 1048 | + if (_TTF_MAC_HEADER) { |
|
| 1049 | + $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac |
|
| 1050 | + } |
|
| 1051 | + else { |
|
| 1052 | + $stm .= (pack("Nnnnn", 0x00010000 , $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows |
|
| 1053 | + } |
|
| 1054 | + |
|
| 1055 | + // Table directory |
|
| 1056 | + $tables = $this->otables; |
|
| 1057 | + |
|
| 1058 | + ksort ($tables); |
|
| 1059 | + $offset = 12 + $numTables * 16; |
|
| 1060 | + foreach ($tables AS $tag=>$data) { |
|
| 1061 | + if ($tag == 'head') { $head_start = $offset; } |
|
| 1062 | + $stm .= $tag; |
|
| 1063 | + $checksum = $this->calcChecksum($data); |
|
| 1064 | + $stm .= pack("nn", $checksum[0],$checksum[1]); |
|
| 1065 | + $stm .= pack("NN", $offset, strlen($data)); |
|
| 1066 | + $paddedLength = (strlen($data)+3)&~3; |
|
| 1067 | + $offset = $offset + $paddedLength; |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + // Table data |
|
| 1071 | + foreach ($tables AS $tag=>$data) { |
|
| 1072 | + $data .= "\0\0\0"; |
|
| 1073 | + $stm .= substr($data,0,(strlen($data)&~3)); |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + $checksum = $this->calcChecksum($stm); |
|
| 1077 | + $checksum = $this->sub32(array(0xB1B0,0xAFBA), $checksum); |
|
| 1078 | + $chk = pack("nn", $checksum[0],$checksum[1]); |
|
| 1079 | + $stm = $this->splice($stm,($head_start + 8),$chk); |
|
| 1080 | + return $stm ; |
|
| 1081 | + } |
|
| 1082 | 1082 | |
| 1083 | 1083 | |
| 1084 | 1084 | |
@@ -27,11 +27,11 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | |
| 29 | 29 | // TrueType Font Glyph operators |
| 30 | -define("GF_WORDS",(1 << 0)); |
|
| 31 | -define("GF_SCALE",(1 << 3)); |
|
| 32 | -define("GF_MORE",(1 << 5)); |
|
| 33 | -define("GF_XYSCALE",(1 << 6)); |
|
| 34 | -define("GF_TWOBYTWO",(1 << 7)); |
|
| 30 | +define("GF_WORDS", (1 << 0)); |
|
| 31 | +define("GF_SCALE", (1 << 3)); |
|
| 32 | +define("GF_MORE", (1 << 5)); |
|
| 33 | +define("GF_XYSCALE", (1 << 6)); |
|
| 34 | +define("GF_TWOBYTWO", (1 << 7)); |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | 37 | |
@@ -70,13 +70,13 @@ discard block |
||
| 70 | 70 | var $maxStrLenRead; |
| 71 | 71 | |
| 72 | 72 | function __construct() { |
| 73 | - $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
| 73 | + $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | |
| 77 | 77 | function getMetrics($file) { |
| 78 | 78 | $this->filename = $file; |
| 79 | - $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); |
|
| 79 | + $this->fh = fopen($file, 'rb') or die('Can\'t open file '.$file); |
|
| 80 | 80 | $this->_pos = 0; |
| 81 | 81 | $this->charWidths = ''; |
| 82 | 82 | $this->glyphPos = array(); |
@@ -87,11 +87,11 @@ discard block |
||
| 87 | 87 | $this->descent = 0; |
| 88 | 88 | $this->TTCFonts = array(); |
| 89 | 89 | $this->version = $version = $this->read_ulong(); |
| 90 | - if ($version==0x4F54544F) |
|
| 90 | + if ($version == 0x4F54544F) |
|
| 91 | 91 | die("Postscript outlines are not supported"); |
| 92 | - if ($version==0x74746366) |
|
| 92 | + if ($version == 0x74746366) |
|
| 93 | 93 | die("ERROR - TrueType Fonts Collections not supported"); |
| 94 | - if (!in_array($version, array(0x00010000,0x74727565))) |
|
| 94 | + if (!in_array($version, array(0x00010000, 0x74727565))) |
|
| 95 | 95 | die("Not a TrueType font: version=".$version); |
| 96 | 96 | $this->readTableDirectory(); |
| 97 | 97 | $this->extractInfo(); |
@@ -105,10 +105,10 @@ discard block |
||
| 105 | 105 | $this->entrySelector = $this->read_ushort(); |
| 106 | 106 | $this->rangeShift = $this->read_ushort(); |
| 107 | 107 | $this->tables = array(); |
| 108 | - for ($i=0;$i<$this->numTables;$i++) { |
|
| 108 | + for ($i = 0; $i < $this->numTables; $i++) { |
|
| 109 | 109 | $record = array(); |
| 110 | 110 | $record['tag'] = $this->read_tag(); |
| 111 | - $record['checksum'] = array($this->read_ushort(),$this->read_ushort()); |
|
| 111 | + $record['checksum'] = array($this->read_ushort(), $this->read_ushort()); |
|
| 112 | 112 | $record['offset'] = $this->read_ulong(); |
| 113 | 113 | $record['length'] = $this->read_ulong(); |
| 114 | 114 | $this->tables[$record['tag']] = $record; |
@@ -122,20 +122,20 @@ discard block |
||
| 122 | 122 | $ylo = $y[1]; |
| 123 | 123 | $yhi = $y[0]; |
| 124 | 124 | if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
| 125 | - $reslo = $xlo-$ylo; |
|
| 126 | - if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
| 127 | - $reshi = $xhi-$yhi; |
|
| 125 | + $reslo = $xlo - $ylo; |
|
| 126 | + if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
| 127 | + $reshi = $xhi - $yhi; |
|
| 128 | 128 | $reshi = $reshi & 0xFFFF; |
| 129 | 129 | return array($reshi, $reslo); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - function calcChecksum($data) { |
|
| 133 | - if (strlen($data) % 4) { $data .= str_repeat("\0",(4-(strlen($data) % 4))); } |
|
| 134 | - $hi=0x0000; |
|
| 135 | - $lo=0x0000; |
|
| 136 | - for($i=0;$i<strlen($data);$i+=4) { |
|
| 137 | - $hi += (ord($data[$i])<<8) + ord($data[$i+1]); |
|
| 138 | - $lo += (ord($data[$i+2])<<8) + ord($data[$i+3]); |
|
| 132 | + function calcChecksum($data) { |
|
| 133 | + if (strlen($data) % 4) { $data .= str_repeat("\0", (4 - (strlen($data) % 4))); } |
|
| 134 | + $hi = 0x0000; |
|
| 135 | + $lo = 0x0000; |
|
| 136 | + for ($i = 0; $i < strlen($data); $i += 4) { |
|
| 137 | + $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]); |
|
| 138 | + $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]); |
|
| 139 | 139 | $hi += $lo >> 16; |
| 140 | 140 | $lo = $lo & 0xFFFF; |
| 141 | 141 | $hi = $hi & 0xFFFF; |
@@ -151,12 +151,12 @@ discard block |
||
| 151 | 151 | |
| 152 | 152 | function seek($pos) { |
| 153 | 153 | $this->_pos = $pos; |
| 154 | - fseek($this->fh,$this->_pos); |
|
| 154 | + fseek($this->fh, $this->_pos); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | function skip($delta) { |
| 158 | 158 | $this->_pos = $this->_pos + $delta; |
| 159 | - fseek($this->fh,$this->_pos); |
|
| 159 | + fseek($this->fh, $this->_pos); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | function seek_table($tag, $offset_in_table = 0) { |
@@ -168,20 +168,20 @@ discard block |
||
| 168 | 168 | |
| 169 | 169 | function read_tag() { |
| 170 | 170 | $this->_pos += 4; |
| 171 | - return fread($this->fh,4); |
|
| 171 | + return fread($this->fh, 4); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | function read_short() { |
| 175 | 175 | $this->_pos += 2; |
| 176 | - $s = fread($this->fh,2); |
|
| 177 | - $a = (ord($s[0])<<8) + ord($s[1]); |
|
| 178 | - if ($a & (1 << 15) ) { $a = ($a - (1 << 16)) ; } |
|
| 176 | + $s = fread($this->fh, 2); |
|
| 177 | + $a = (ord($s[0]) << 8) + ord($s[1]); |
|
| 178 | + if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } |
|
| 179 | 179 | return $a; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | function unpack_short($s) { |
| 183 | - $a = (ord($s[0])<<8) + ord($s[1]); |
|
| 184 | - if ($a & (1 << 15) ) { |
|
| 183 | + $a = (ord($s[0]) << 8) + ord($s[1]); |
|
| 184 | + if ($a & (1 << 15)) { |
|
| 185 | 185 | $a = ($a - (1 << 16)); |
| 186 | 186 | } |
| 187 | 187 | return $a; |
@@ -189,41 +189,41 @@ discard block |
||
| 189 | 189 | |
| 190 | 190 | function read_ushort() { |
| 191 | 191 | $this->_pos += 2; |
| 192 | - $s = fread($this->fh,2); |
|
| 193 | - return (ord($s[0])<<8) + ord($s[1]); |
|
| 192 | + $s = fread($this->fh, 2); |
|
| 193 | + return (ord($s[0]) << 8) + ord($s[1]); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | function read_ulong() { |
| 197 | 197 | $this->_pos += 4; |
| 198 | - $s = fread($this->fh,4); |
|
| 198 | + $s = fread($this->fh, 4); |
|
| 199 | 199 | // if large uInt32 as an integer, PHP converts it to -ve |
| 200 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 200 | + return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | function get_ushort($pos) { |
| 204 | - fseek($this->fh,$pos); |
|
| 205 | - $s = fread($this->fh,2); |
|
| 206 | - return (ord($s[0])<<8) + ord($s[1]); |
|
| 204 | + fseek($this->fh, $pos); |
|
| 205 | + $s = fread($this->fh, 2); |
|
| 206 | + return (ord($s[0]) << 8) + ord($s[1]); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | function get_ulong($pos) { |
| 210 | - fseek($this->fh,$pos); |
|
| 211 | - $s = fread($this->fh,4); |
|
| 210 | + fseek($this->fh, $pos); |
|
| 211 | + $s = fread($this->fh, 4); |
|
| 212 | 212 | // iF large uInt32 as an integer, PHP converts it to -ve |
| 213 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 213 | + return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | function pack_short($val) { |
| 217 | - if ($val<0) { |
|
| 217 | + if ($val < 0) { |
|
| 218 | 218 | $val = abs($val); |
| 219 | 219 | $val = ~$val; |
| 220 | 220 | $val += 1; |
| 221 | 221 | } |
| 222 | - return pack("n",$val); |
|
| 222 | + return pack("n", $val); |
|
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | function splice($stream, $offset, $value) { |
| 226 | - return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); |
|
| 226 | + return substr($stream, 0, $offset).$value.substr($stream, $offset + strlen($value)); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | function _set_ushort($stream, $offset, $value) { |
@@ -232,26 +232,26 @@ discard block |
||
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | function _set_short($stream, $offset, $val) { |
| 235 | - if ($val<0) { |
|
| 235 | + if ($val < 0) { |
|
| 236 | 236 | $val = abs($val); |
| 237 | 237 | $val = ~$val; |
| 238 | 238 | $val += 1; |
| 239 | 239 | } |
| 240 | - $up = pack("n",$val); |
|
| 240 | + $up = pack("n", $val); |
|
| 241 | 241 | return $this->splice($stream, $offset, $up); |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | function get_chunk($pos, $length) { |
| 245 | - fseek($this->fh,$pos); |
|
| 246 | - if ($length <1) { return ''; } |
|
| 247 | - return (fread($this->fh,$length)); |
|
| 245 | + fseek($this->fh, $pos); |
|
| 246 | + if ($length < 1) { return ''; } |
|
| 247 | + return (fread($this->fh, $length)); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | function get_table($tag) { |
| 251 | 251 | list($pos, $length) = $this->get_table_pos($tag); |
| 252 | 252 | if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
| 253 | - fseek($this->fh,$pos); |
|
| 254 | - return (fread($this->fh,$length)); |
|
| 253 | + fseek($this->fh, $pos); |
|
| 254 | + return (fread($this->fh, $length)); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | function add($tag, $data) { |
@@ -281,17 +281,17 @@ discard block |
||
| 281 | 281 | die("Unknown name table format ".$format); |
| 282 | 282 | $numRecords = $this->read_ushort(); |
| 283 | 283 | $string_data_offset = $name_offset + $this->read_ushort(); |
| 284 | - $names = array(1=>'',2=>'',3=>'',4=>'',6=>''); |
|
| 284 | + $names = array(1=>'', 2=>'', 3=>'', 4=>'', 6=>''); |
|
| 285 | 285 | $K = array_keys($names); |
| 286 | 286 | $nameCount = count($names); |
| 287 | - for ($i=0;$i<$numRecords; $i++) { |
|
| 287 | + for ($i = 0; $i < $numRecords; $i++) { |
|
| 288 | 288 | $platformId = $this->read_ushort(); |
| 289 | 289 | $encodingId = $this->read_ushort(); |
| 290 | 290 | $languageId = $this->read_ushort(); |
| 291 | 291 | $nameId = $this->read_ushort(); |
| 292 | 292 | $length = $this->read_ushort(); |
| 293 | 293 | $offset = $this->read_ushort(); |
| 294 | - if (!in_array($nameId,$K)) continue; |
|
| 294 | + if (!in_array($nameId, $K)) continue; |
|
| 295 | 295 | $N = ''; |
| 296 | 296 | if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name |
| 297 | 297 | $opos = $this->_pos; |
@@ -314,18 +314,18 @@ discard block |
||
| 314 | 314 | $this->_pos = $opos; |
| 315 | 315 | $this->seek($opos); |
| 316 | 316 | } |
| 317 | - if ($N && $names[$nameId]=='') { |
|
| 317 | + if ($N && $names[$nameId] == '') { |
|
| 318 | 318 | $names[$nameId] = $N; |
| 319 | 319 | $nameCount -= 1; |
| 320 | - if ($nameCount==0) break; |
|
| 320 | + if ($nameCount == 0) break; |
|
| 321 | 321 | } |
| 322 | 322 | } |
| 323 | 323 | if ($names[6]) |
| 324 | 324 | $psName = $names[6]; |
| 325 | 325 | else if ($names[4]) |
| 326 | - $psName = preg_replace('/ /','-',$names[4]); |
|
| 326 | + $psName = preg_replace('/ /', '-', $names[4]); |
|
| 327 | 327 | else if ($names[1]) |
| 328 | - $psName = preg_replace('/ /','-',$names[1]); |
|
| 328 | + $psName = preg_replace('/ /', '-', $names[1]); |
|
| 329 | 329 | else |
| 330 | 330 | $psName = ''; |
| 331 | 331 | if (!$psName) |
@@ -349,8 +349,8 @@ discard block |
||
| 349 | 349 | $yMin = $this->read_short(); |
| 350 | 350 | $xMax = $this->read_short(); |
| 351 | 351 | $yMax = $this->read_short(); |
| 352 | - $this->bbox = array(($xMin*$scale), ($yMin*$scale), ($xMax*$scale), ($yMax*$scale)); |
|
| 353 | - $this->skip(3*2); |
|
| 352 | + $this->bbox = array(($xMin * $scale), ($yMin * $scale), ($xMax * $scale), ($yMax * $scale)); |
|
| 353 | + $this->skip(3 * 2); |
|
| 354 | 354 | $indexToLocFormat = $this->read_ushort(); |
| 355 | 355 | $glyphDataFormat = $this->read_ushort(); |
| 356 | 356 | if ($glyphDataFormat != 0) |
@@ -365,8 +365,8 @@ discard block |
||
| 365 | 365 | $this->skip(4); |
| 366 | 366 | $hheaAscender = $this->read_short(); |
| 367 | 367 | $hheaDescender = $this->read_short(); |
| 368 | - $this->ascent = ($hheaAscender *$scale); |
|
| 369 | - $this->descent = ($hheaDescender *$scale); |
|
| 368 | + $this->ascent = ($hheaAscender * $scale); |
|
| 369 | + $this->descent = ($hheaDescender * $scale); |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | /////////////////////////////////// |
@@ -387,17 +387,17 @@ discard block |
||
| 387 | 387 | $sF = $this->read_short(); |
| 388 | 388 | $this->sFamilyClass = ($sF >> 8); |
| 389 | 389 | $this->sFamilySubClass = ($sF & 0xFF); |
| 390 | - $this->_pos += 10; //PANOSE = 10 byte length |
|
| 391 | - $panose = fread($this->fh,10); |
|
| 390 | + $this->_pos += 10; //PANOSE = 10 byte length |
|
| 391 | + $panose = fread($this->fh, 10); |
|
| 392 | 392 | $this->skip(26); |
| 393 | 393 | $sTypoAscender = $this->read_short(); |
| 394 | 394 | $sTypoDescender = $this->read_short(); |
| 395 | - if (!$this->ascent) $this->ascent = ($sTypoAscender*$scale); |
|
| 396 | - if (!$this->descent) $this->descent = ($sTypoDescender*$scale); |
|
| 395 | + if (!$this->ascent) $this->ascent = ($sTypoAscender * $scale); |
|
| 396 | + if (!$this->descent) $this->descent = ($sTypoDescender * $scale); |
|
| 397 | 397 | if ($version > 1) { |
| 398 | 398 | $this->skip(16); |
| 399 | 399 | $sCapHeight = $this->read_short(); |
| 400 | - $this->capHeight = ($sCapHeight*$scale); |
|
| 400 | + $this->capHeight = ($sCapHeight * $scale); |
|
| 401 | 401 | } |
| 402 | 402 | else { |
| 403 | 403 | $this->capHeight = $this->ascent; |
@@ -405,11 +405,11 @@ discard block |
||
| 405 | 405 | } |
| 406 | 406 | else { |
| 407 | 407 | $usWeightClass = 500; |
| 408 | - if (!$this->ascent) $this->ascent = ($yMax*$scale); |
|
| 409 | - if (!$this->descent) $this->descent = ($yMin*$scale); |
|
| 408 | + if (!$this->ascent) $this->ascent = ($yMax * $scale); |
|
| 409 | + if (!$this->descent) $this->descent = ($yMin * $scale); |
|
| 410 | 410 | $this->capHeight = $this->ascent; |
| 411 | 411 | } |
| 412 | - $this->stemV = 50 + intval(pow(($usWeightClass / 65.0),2)); |
|
| 412 | + $this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2)); |
|
| 413 | 413 | |
| 414 | 414 | /////////////////////////////////// |
| 415 | 415 | // post - PostScript table |
@@ -423,7 +423,7 @@ discard block |
||
| 423 | 423 | |
| 424 | 424 | $this->flags = 4; |
| 425 | 425 | |
| 426 | - if ($this->italicAngle!= 0) |
|
| 426 | + if ($this->italicAngle != 0) |
|
| 427 | 427 | $this->flags = $this->flags | 64; |
| 428 | 428 | if ($usWeightClass >= 600) |
| 429 | 429 | $this->flags = $this->flags | 262144; |
@@ -457,7 +457,7 @@ discard block |
||
| 457 | 457 | $this->skip(2); |
| 458 | 458 | $cmapTableCount = $this->read_ushort(); |
| 459 | 459 | $unicode_cmap_offset = 0; |
| 460 | - for ($i=0;$i<$cmapTableCount;$i++) { |
|
| 460 | + for ($i = 0; $i < $cmapTableCount; $i++) { |
|
| 461 | 461 | $platformID = $this->read_ushort(); |
| 462 | 462 | $encodingID = $this->read_ushort(); |
| 463 | 463 | $offset = $this->read_ulong(); |
@@ -469,15 +469,15 @@ discard block |
||
| 469 | 469 | break; |
| 470 | 470 | } |
| 471 | 471 | } |
| 472 | - $this->seek($save_pos ); |
|
| 472 | + $this->seek($save_pos); |
|
| 473 | 473 | } |
| 474 | 474 | if (!$unicode_cmap_offset) |
| 475 | - die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 475 | + die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 476 | 476 | |
| 477 | 477 | |
| 478 | 478 | $glyphToChar = array(); |
| 479 | 479 | $charToGlyph = array(); |
| 480 | - $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph ); |
|
| 480 | + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); |
|
| 481 | 481 | |
| 482 | 482 | /////////////////////////////////// |
| 483 | 483 | // hmtx - Horizontal metrics table |
@@ -493,7 +493,7 @@ discard block |
||
| 493 | 493 | |
| 494 | 494 | function makeSubset($file, &$subset) { |
| 495 | 495 | $this->filename = $file; |
| 496 | - $this->fh = fopen($file ,'rb') or die('Can\'t open file ' . $file); |
|
| 496 | + $this->fh = fopen($file, 'rb') or die('Can\'t open file '.$file); |
|
| 497 | 497 | $this->_pos = 0; |
| 498 | 498 | $this->charWidths = ''; |
| 499 | 499 | $this->glyphPos = array(); |
@@ -538,7 +538,7 @@ discard block |
||
| 538 | 538 | $this->skip(2); |
| 539 | 539 | $cmapTableCount = $this->read_ushort(); |
| 540 | 540 | $unicode_cmap_offset = 0; |
| 541 | - for ($i=0;$i<$cmapTableCount;$i++) { |
|
| 541 | + for ($i = 0; $i < $cmapTableCount; $i++) { |
|
| 542 | 542 | $platformID = $this->read_ushort(); |
| 543 | 543 | $encodingID = $this->read_ushort(); |
| 544 | 544 | $offset = $this->read_ulong(); |
@@ -550,23 +550,23 @@ discard block |
||
| 550 | 550 | break; |
| 551 | 551 | } |
| 552 | 552 | } |
| 553 | - $this->seek($save_pos ); |
|
| 553 | + $this->seek($save_pos); |
|
| 554 | 554 | } |
| 555 | 555 | |
| 556 | 556 | if (!$unicode_cmap_offset) |
| 557 | - die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 557 | + die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 558 | 558 | |
| 559 | 559 | |
| 560 | 560 | $glyphToChar = array(); |
| 561 | 561 | $charToGlyph = array(); |
| 562 | - $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph ); |
|
| 562 | + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); |
|
| 563 | 563 | |
| 564 | 564 | $this->charToGlyph = $charToGlyph; |
| 565 | 565 | |
| 566 | 566 | /////////////////////////////////// |
| 567 | 567 | // hmtx - Horizontal metrics table |
| 568 | 568 | /////////////////////////////////// |
| 569 | - $scale = 1; // not used |
|
| 569 | + $scale = 1; // not used |
|
| 570 | 570 | $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
| 571 | 571 | |
| 572 | 572 | /////////////////////////////////// |
@@ -576,51 +576,51 @@ discard block |
||
| 576 | 576 | |
| 577 | 577 | $subsetglyphs = array(0=>0); |
| 578 | 578 | $subsetCharToGlyph = array(); |
| 579 | - foreach($subset AS $code) { |
|
| 579 | + foreach ($subset AS $code) { |
|
| 580 | 580 | if (isset($this->charToGlyph[$code])) { |
| 581 | - $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode |
|
| 582 | - $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID |
|
| 581 | + $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode |
|
| 582 | + $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID |
|
| 583 | 583 | |
| 584 | 584 | } |
| 585 | 585 | $this->maxUni = max($this->maxUni, $code); |
| 586 | 586 | } |
| 587 | 587 | |
| 588 | - list($start,$dummy) = $this->get_table_pos('glyf'); |
|
| 588 | + list($start, $dummy) = $this->get_table_pos('glyf'); |
|
| 589 | 589 | |
| 590 | 590 | $glyphSet = array(); |
| 591 | 591 | ksort($subsetglyphs); |
| 592 | 592 | $n = 0; |
| 593 | - $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. |
|
| 594 | - foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 595 | - $fsLastCharIndex = max($fsLastCharIndex , $uni); |
|
| 596 | - $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID |
|
| 593 | + $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. |
|
| 594 | + foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 595 | + $fsLastCharIndex = max($fsLastCharIndex, $uni); |
|
| 596 | + $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID |
|
| 597 | 597 | $n++; |
| 598 | 598 | } |
| 599 | 599 | |
| 600 | 600 | ksort($subsetCharToGlyph); |
| 601 | - foreach($subsetCharToGlyph AS $uni => $originalGlyphIdx) { |
|
| 602 | - $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx] ; |
|
| 601 | + foreach ($subsetCharToGlyph AS $uni => $originalGlyphIdx) { |
|
| 602 | + $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx]; |
|
| 603 | 603 | } |
| 604 | 604 | $this->codeToGlyph = $codeToGlyph; |
| 605 | 605 | |
| 606 | 606 | ksort($subsetglyphs); |
| 607 | - foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 607 | + foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 608 | 608 | $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs); |
| 609 | 609 | } |
| 610 | 610 | |
| 611 | - $numGlyphs = $numberOfHMetrics = count($subsetglyphs ); |
|
| 611 | + $numGlyphs = $numberOfHMetrics = count($subsetglyphs); |
|
| 612 | 612 | |
| 613 | 613 | //tables copied from the original |
| 614 | - $tags = array ('name'); |
|
| 615 | - foreach($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } |
|
| 616 | - $tags = array ('cvt ', 'fpgm', 'prep', 'gasp'); |
|
| 617 | - foreach($tags AS $tag) { |
|
| 614 | + $tags = array('name'); |
|
| 615 | + foreach ($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } |
|
| 616 | + $tags = array('cvt ', 'fpgm', 'prep', 'gasp'); |
|
| 617 | + foreach ($tags AS $tag) { |
|
| 618 | 618 | if (isset($this->tables[$tag])) { $this->add($tag, $this->get_table($tag)); } |
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | // post - PostScript |
| 622 | 622 | $opost = $this->get_table('post'); |
| 623 | - $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"; |
|
| 623 | + $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"; |
|
| 624 | 624 | $this->add('post', $post); |
| 625 | 625 | |
| 626 | 626 | // Sort CID2GID map into segments of contiguous codes |
@@ -646,64 +646,64 @@ discard block |
||
| 646 | 646 | } |
| 647 | 647 | |
| 648 | 648 | // cmap - Character to glyph mapping - Format 4 (MS / ) |
| 649 | - $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF |
|
| 649 | + $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF |
|
| 650 | 650 | $searchRange = 1; |
| 651 | 651 | $entrySelector = 0; |
| 652 | - while ($searchRange * 2 <= $segCount ) { |
|
| 652 | + while ($searchRange * 2 <= $segCount) { |
|
| 653 | 653 | $searchRange = $searchRange * 2; |
| 654 | 654 | $entrySelector = $entrySelector + 1; |
| 655 | 655 | } |
| 656 | 656 | $searchRange = $searchRange * 2; |
| 657 | 657 | $rangeShift = $segCount * 2 - $searchRange; |
| 658 | - $length = 16 + (8*$segCount ) + ($numGlyphs+1); |
|
| 659 | - $cmap = array(0, 1, // Index : version, number of encoding subtables |
|
| 660 | - 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) |
|
| 661 | - 0, 12, // Encoding Subtable : offset (hi,lo) |
|
| 662 | - 4, $length, 0, // Format 4 Mapping subtable: format, length, language |
|
| 663 | - $segCount*2, |
|
| 658 | + $length = 16 + (8 * $segCount) + ($numGlyphs + 1); |
|
| 659 | + $cmap = array(0, 1, // Index : version, number of encoding subtables |
|
| 660 | + 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) |
|
| 661 | + 0, 12, // Encoding Subtable : offset (hi,lo) |
|
| 662 | + 4, $length, 0, // Format 4 Mapping subtable: format, length, language |
|
| 663 | + $segCount * 2, |
|
| 664 | 664 | $searchRange, |
| 665 | 665 | $entrySelector, |
| 666 | 666 | $rangeShift); |
| 667 | 667 | |
| 668 | 668 | // endCode(s) |
| 669 | - foreach($range AS $start=>$subrange) { |
|
| 670 | - $endCode = $start + (count($subrange)-1); |
|
| 671 | - $cmap[] = $endCode; // endCode(s) |
|
| 669 | + foreach ($range AS $start=>$subrange) { |
|
| 670 | + $endCode = $start + (count($subrange) - 1); |
|
| 671 | + $cmap[] = $endCode; // endCode(s) |
|
| 672 | 672 | } |
| 673 | - $cmap[] = 0xFFFF; // endCode of last Segment |
|
| 674 | - $cmap[] = 0; // reservedPad |
|
| 673 | + $cmap[] = 0xFFFF; // endCode of last Segment |
|
| 674 | + $cmap[] = 0; // reservedPad |
|
| 675 | 675 | |
| 676 | 676 | // startCode(s) |
| 677 | - foreach($range AS $start=>$subrange) { |
|
| 678 | - $cmap[] = $start; // startCode(s) |
|
| 677 | + foreach ($range AS $start=>$subrange) { |
|
| 678 | + $cmap[] = $start; // startCode(s) |
|
| 679 | 679 | } |
| 680 | - $cmap[] = 0xFFFF; // startCode of last Segment |
|
| 680 | + $cmap[] = 0xFFFF; // startCode of last Segment |
|
| 681 | 681 | // idDelta(s) |
| 682 | - foreach($range AS $start=>$subrange) { |
|
| 683 | - $idDelta = -($start-$subrange[0]); |
|
| 682 | + foreach ($range AS $start=>$subrange) { |
|
| 683 | + $idDelta = -($start - $subrange[0]); |
|
| 684 | 684 | $n += count($subrange); |
| 685 | - $cmap[] = $idDelta; // idDelta(s) |
|
| 685 | + $cmap[] = $idDelta; // idDelta(s) |
|
| 686 | 686 | } |
| 687 | - $cmap[] = 1; // idDelta of last Segment |
|
| 687 | + $cmap[] = 1; // idDelta of last Segment |
|
| 688 | 688 | // idRangeOffset(s) |
| 689 | - foreach($range AS $subrange) { |
|
| 690 | - $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 |
|
| 689 | + foreach ($range AS $subrange) { |
|
| 690 | + $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 |
|
| 691 | 691 | |
| 692 | 692 | } |
| 693 | - $cmap[] = 0; // idRangeOffset of last Segment |
|
| 694 | - foreach($range AS $subrange) { |
|
| 695 | - foreach($subrange AS $glidx) { |
|
| 693 | + $cmap[] = 0; // idRangeOffset of last Segment |
|
| 694 | + foreach ($range AS $subrange) { |
|
| 695 | + foreach ($subrange AS $glidx) { |
|
| 696 | 696 | $cmap[] = $glidx; |
| 697 | 697 | } |
| 698 | 698 | } |
| 699 | - $cmap[] = 0; // Mapping for last character |
|
| 699 | + $cmap[] = 0; // Mapping for last character |
|
| 700 | 700 | $cmapstr = ''; |
| 701 | - foreach($cmap AS $cm) { $cmapstr .= pack("n",$cm); } |
|
| 701 | + foreach ($cmap AS $cm) { $cmapstr .= pack("n", $cm); } |
|
| 702 | 702 | $this->add('cmap', $cmapstr); |
| 703 | 703 | |
| 704 | 704 | |
| 705 | 705 | // glyf - Glyph data |
| 706 | - list($glyfOffset,$glyfLength) = $this->get_table_pos('glyf'); |
|
| 706 | + list($glyfOffset, $glyfLength) = $this->get_table_pos('glyf'); |
|
| 707 | 707 | if ($glyfLength < $this->maxStrLenRead) { |
| 708 | 708 | $glyphData = $this->get_table('glyf'); |
| 709 | 709 | } |
@@ -721,15 +721,15 @@ discard block |
||
| 721 | 721 | $minLeftSideBearing = 0; |
| 722 | 722 | $minRightSideBearing = 0; |
| 723 | 723 | $xMaxExtent = 0; |
| 724 | - $maxPoints = 0; // points in non-compound glyph |
|
| 725 | - $maxContours = 0; // contours in non-compound glyph |
|
| 726 | - $maxComponentPoints = 0; // points in compound glyph |
|
| 727 | - $maxComponentContours = 0; // contours in compound glyph |
|
| 728 | - $maxComponentElements = 0; // number of glyphs referenced at top level |
|
| 729 | - $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs |
|
| 724 | + $maxPoints = 0; // points in non-compound glyph |
|
| 725 | + $maxContours = 0; // contours in non-compound glyph |
|
| 726 | + $maxComponentPoints = 0; // points in compound glyph |
|
| 727 | + $maxComponentContours = 0; // contours in compound glyph |
|
| 728 | + $maxComponentElements = 0; // number of glyphs referenced at top level |
|
| 729 | + $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs |
|
| 730 | 730 | $this->glyphdata = array(); |
| 731 | 731 | |
| 732 | - foreach($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 732 | + foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
| 733 | 733 | // hmtx - Horizontal Metrics |
| 734 | 734 | $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx); |
| 735 | 735 | $hmtxstr .= $hm; |
@@ -738,26 +738,26 @@ discard block |
||
| 738 | 738 | $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
| 739 | 739 | $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
| 740 | 740 | if ($glyfLength < $this->maxStrLenRead) { |
| 741 | - $data = substr($glyphData,$glyphPos,$glyphLen); |
|
| 741 | + $data = substr($glyphData, $glyphPos, $glyphLen); |
|
| 742 | 742 | } |
| 743 | 743 | else { |
| 744 | - if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset+$glyphPos,$glyphLen); |
|
| 744 | + if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); |
|
| 745 | 745 | else $data = ''; |
| 746 | 746 | } |
| 747 | 747 | |
| 748 | 748 | if ($glyphLen > 0) { |
| 749 | - $up = unpack("n", substr($data,0,2)); |
|
| 749 | + $up = unpack("n", substr($data, 0, 2)); |
|
| 750 | 750 | } |
| 751 | 751 | |
| 752 | - if ($glyphLen > 2 && ($up[1] & (1 << 15)) ) { // If number of contours <= -1 i.e. composiste glyph |
|
| 752 | + if ($glyphLen > 2 && ($up[1] & (1 << 15))) { // If number of contours <= -1 i.e. composiste glyph |
|
| 753 | 753 | $pos_in_glyph = 10; |
| 754 | 754 | $flags = GF_MORE; |
| 755 | 755 | $nComponentElements = 0; |
| 756 | 756 | while ($flags & GF_MORE) { |
| 757 | - $nComponentElements += 1; // number of glyphs referenced at top level |
|
| 758 | - $up = unpack("n", substr($data,$pos_in_glyph,2)); |
|
| 757 | + $nComponentElements += 1; // number of glyphs referenced at top level |
|
| 758 | + $up = unpack("n", substr($data, $pos_in_glyph, 2)); |
|
| 759 | 759 | $flags = $up[1]; |
| 760 | - $up = unpack("n", substr($data,$pos_in_glyph+2,2)); |
|
| 760 | + $up = unpack("n", substr($data, $pos_in_glyph + 2, 2)); |
|
| 761 | 761 | $glyphIdx = $up[1]; |
| 762 | 762 | $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; |
| 763 | 763 | $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); |
@@ -775,7 +775,7 @@ discard block |
||
| 775 | 775 | $pos += $glyphLen; |
| 776 | 776 | if ($pos % 4 != 0) { |
| 777 | 777 | $padding = 4 - ($pos % 4); |
| 778 | - $glyf .= str_repeat("\0",$padding); |
|
| 778 | + $glyf .= str_repeat("\0", $padding); |
|
| 779 | 779 | $pos += $padding; |
| 780 | 780 | } |
| 781 | 781 | } |
@@ -789,12 +789,12 @@ discard block |
||
| 789 | 789 | // loca - Index to location |
| 790 | 790 | $locastr = ''; |
| 791 | 791 | if ((($pos + 1) >> 1) > 0xFFFF) { |
| 792 | - $indexToLocFormat = 1; // long format |
|
| 793 | - foreach($offsets AS $offset) { $locastr .= pack("N",$offset); } |
|
| 792 | + $indexToLocFormat = 1; // long format |
|
| 793 | + foreach ($offsets AS $offset) { $locastr .= pack("N", $offset); } |
|
| 794 | 794 | } |
| 795 | 795 | else { |
| 796 | - $indexToLocFormat = 0; // short format |
|
| 797 | - foreach($offsets AS $offset) { $locastr .= pack("n",($offset/2)); } |
|
| 796 | + $indexToLocFormat = 0; // short format |
|
| 797 | + foreach ($offsets AS $offset) { $locastr .= pack("n", ($offset / 2)); } |
|
| 798 | 798 | } |
| 799 | 799 | $this->add('loca', $locastr); |
| 800 | 800 | |
@@ -817,14 +817,14 @@ discard block |
||
| 817 | 817 | |
| 818 | 818 | // OS/2 - OS/2 |
| 819 | 819 | $os2 = $this->get_table('OS/2'); |
| 820 | - $this->add('OS/2', $os2 ); |
|
| 820 | + $this->add('OS/2', $os2); |
|
| 821 | 821 | |
| 822 | 822 | fclose($this->fh); |
| 823 | 823 | |
| 824 | 824 | // Put the TTF file together |
| 825 | 825 | $stm = ''; |
| 826 | 826 | $this->endTTFile($stm); |
| 827 | - return $stm ; |
|
| 827 | + return $stm; |
|
| 828 | 828 | } |
| 829 | 829 | |
| 830 | 830 | ////////////////////////////////////////////////////////////////////////////////// |
@@ -833,7 +833,7 @@ discard block |
||
| 833 | 833 | $depth++; |
| 834 | 834 | $maxdepth = max($maxdepth, $depth); |
| 835 | 835 | if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) { |
| 836 | - foreach($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { |
|
| 836 | + foreach ($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { |
|
| 837 | 837 | $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); |
| 838 | 838 | } |
| 839 | 839 | } |
@@ -862,7 +862,7 @@ discard block |
||
| 862 | 862 | $flags = $this->read_ushort(); |
| 863 | 863 | $glyphIdx = $this->read_ushort(); |
| 864 | 864 | if (!isset($glyphSet[$glyphIdx])) { |
| 865 | - $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID |
|
| 865 | + $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID |
|
| 866 | 866 | $subsetglyphs[$glyphIdx] = true; |
| 867 | 867 | } |
| 868 | 868 | $savepos = ftell($this->fh); |
@@ -887,17 +887,17 @@ discard block |
||
| 887 | 887 | function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { |
| 888 | 888 | $start = $this->seek_table("hmtx"); |
| 889 | 889 | $aw = 0; |
| 890 | - $this->charWidths = str_pad('', 256*256*2, "\x00"); |
|
| 890 | + $this->charWidths = str_pad('', 256 * 256 * 2, "\x00"); |
|
| 891 | 891 | $nCharWidths = 0; |
| 892 | - if (($numberOfHMetrics*4) < $this->maxStrLenRead) { |
|
| 893 | - $data = $this->get_chunk($start,($numberOfHMetrics*4)); |
|
| 892 | + if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { |
|
| 893 | + $data = $this->get_chunk($start, ($numberOfHMetrics * 4)); |
|
| 894 | 894 | $arr = unpack("n*", $data); |
| 895 | 895 | } |
| 896 | 896 | else { $this->seek($start); } |
| 897 | - for( $glyph=0; $glyph<$numberOfHMetrics; $glyph++) { |
|
| 897 | + for ($glyph = 0; $glyph < $numberOfHMetrics; $glyph++) { |
|
| 898 | 898 | |
| 899 | - if (($numberOfHMetrics*4) < $this->maxStrLenRead) { |
|
| 900 | - $aw = $arr[($glyph*2)+1]; |
|
| 899 | + if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { |
|
| 900 | + $aw = $arr[($glyph * 2) + 1]; |
|
| 901 | 901 | } |
| 902 | 902 | else { |
| 903 | 903 | $aw = $this->read_ushort(); |
@@ -905,38 +905,38 @@ discard block |
||
| 905 | 905 | } |
| 906 | 906 | if (isset($glyphToChar[$glyph]) || $glyph == 0) { |
| 907 | 907 | |
| 908 | - if ($aw >= (1 << 15) ) { $aw = 0; } // 1.03 Some (arabic) fonts have -ve values for width |
|
| 908 | + if ($aw >= (1 << 15)) { $aw = 0; } // 1.03 Some (arabic) fonts have -ve values for width |
|
| 909 | 909 | // although should be unsigned value - comes out as e.g. 65108 (intended -50) |
| 910 | 910 | if ($glyph == 0) { |
| 911 | - $this->defaultWidth = $scale*$aw; |
|
| 911 | + $this->defaultWidth = $scale * $aw; |
|
| 912 | 912 | continue; |
| 913 | 913 | } |
| 914 | - foreach($glyphToChar[$glyph] AS $char) { |
|
| 914 | + foreach ($glyphToChar[$glyph] AS $char) { |
|
| 915 | 915 | if ($char != 0 && $char != 65535) { |
| 916 | - $w = intval(round($scale*$aw)); |
|
| 916 | + $w = intval(round($scale * $aw)); |
|
| 917 | 917 | if ($w == 0) { $w = 65535; } |
| 918 | 918 | if ($char < 196608) { |
| 919 | - $this->charWidths[$char*2] = chr($w >> 8); |
|
| 920 | - $this->charWidths[$char*2 + 1] = chr($w & 0xFF); |
|
| 919 | + $this->charWidths[$char * 2] = chr($w >> 8); |
|
| 920 | + $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); |
|
| 921 | 921 | $nCharWidths++; |
| 922 | 922 | } |
| 923 | 923 | } |
| 924 | 924 | } |
| 925 | 925 | } |
| 926 | 926 | } |
| 927 | - $data = $this->get_chunk(($start+$numberOfHMetrics*4),($numGlyphs*2)); |
|
| 927 | + $data = $this->get_chunk(($start + $numberOfHMetrics * 4), ($numGlyphs * 2)); |
|
| 928 | 928 | $arr = unpack("n*", $data); |
| 929 | - $diff = $numGlyphs-$numberOfHMetrics; |
|
| 930 | - for( $pos=0; $pos<$diff; $pos++) { |
|
| 929 | + $diff = $numGlyphs - $numberOfHMetrics; |
|
| 930 | + for ($pos = 0; $pos < $diff; $pos++) { |
|
| 931 | 931 | $glyph = $pos + $numberOfHMetrics; |
| 932 | 932 | if (isset($glyphToChar[$glyph])) { |
| 933 | - foreach($glyphToChar[$glyph] AS $char) { |
|
| 933 | + foreach ($glyphToChar[$glyph] AS $char) { |
|
| 934 | 934 | if ($char != 0 && $char != 65535) { |
| 935 | - $w = intval(round($scale*$aw)); |
|
| 935 | + $w = intval(round($scale * $aw)); |
|
| 936 | 936 | if ($w == 0) { $w = 65535; } |
| 937 | 937 | if ($char < 196608) { |
| 938 | - $this->charWidths[$char*2] = chr($w >> 8); |
|
| 939 | - $this->charWidths[$char*2 + 1] = chr($w & 0xFF); |
|
| 938 | + $this->charWidths[$char * 2] = chr($w >> 8); |
|
| 939 | + $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); |
|
| 940 | 940 | $nCharWidths++; |
| 941 | 941 | } |
| 942 | 942 | } |
@@ -952,14 +952,14 @@ discard block |
||
| 952 | 952 | function getHMetric($numberOfHMetrics, $gid) { |
| 953 | 953 | $start = $this->seek_table("hmtx"); |
| 954 | 954 | if ($gid < $numberOfHMetrics) { |
| 955 | - $this->seek($start+($gid*4)); |
|
| 956 | - $hm = fread($this->fh,4); |
|
| 955 | + $this->seek($start + ($gid * 4)); |
|
| 956 | + $hm = fread($this->fh, 4); |
|
| 957 | 957 | } |
| 958 | 958 | else { |
| 959 | - $this->seek($start+(($numberOfHMetrics-1)*4)); |
|
| 960 | - $hm = fread($this->fh,2); |
|
| 961 | - $this->seek($start+($numberOfHMetrics*2)+($gid*2)); |
|
| 962 | - $hm .= fread($this->fh,2); |
|
| 959 | + $this->seek($start + (($numberOfHMetrics - 1) * 4)); |
|
| 960 | + $hm = fread($this->fh, 2); |
|
| 961 | + $this->seek($start + ($numberOfHMetrics * 2) + ($gid * 2)); |
|
| 962 | + $hm .= fread($this->fh, 2); |
|
| 963 | 963 | } |
| 964 | 964 | return $hm; |
| 965 | 965 | } |
@@ -968,17 +968,17 @@ discard block |
||
| 968 | 968 | $start = $this->seek_table('loca'); |
| 969 | 969 | $this->glyphPos = array(); |
| 970 | 970 | if ($indexToLocFormat == 0) { |
| 971 | - $data = $this->get_chunk($start,($numGlyphs*2)+2); |
|
| 971 | + $data = $this->get_chunk($start, ($numGlyphs * 2) + 2); |
|
| 972 | 972 | $arr = unpack("n*", $data); |
| 973 | - for ($n=0; $n<=$numGlyphs; $n++) { |
|
| 974 | - $this->glyphPos[] = ($arr[$n+1] * 2); |
|
| 973 | + for ($n = 0; $n <= $numGlyphs; $n++) { |
|
| 974 | + $this->glyphPos[] = ($arr[$n + 1] * 2); |
|
| 975 | 975 | } |
| 976 | 976 | } |
| 977 | 977 | else if ($indexToLocFormat == 1) { |
| 978 | - $data = $this->get_chunk($start,($numGlyphs*4)+4); |
|
| 978 | + $data = $this->get_chunk($start, ($numGlyphs * 4) + 4); |
|
| 979 | 979 | $arr = unpack("N*", $data); |
| 980 | - for ($n=0; $n<=$numGlyphs; $n++) { |
|
| 981 | - $this->glyphPos[] = ($arr[$n+1]); |
|
| 980 | + for ($n = 0; $n <= $numGlyphs; $n++) { |
|
| 981 | + $this->glyphPos[] = ($arr[$n + 1]); |
|
| 982 | 982 | } |
| 983 | 983 | } |
| 984 | 984 | else |
@@ -987,7 +987,7 @@ discard block |
||
| 987 | 987 | |
| 988 | 988 | |
| 989 | 989 | // CMAP Format 4 |
| 990 | - function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { |
|
| 990 | + function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph) { |
|
| 991 | 991 | $this->maxUniChar = 0; |
| 992 | 992 | $this->seek($unicode_cmap_offset + 2); |
| 993 | 993 | $length = $this->read_ushort(); |
@@ -997,19 +997,19 @@ discard block |
||
| 997 | 997 | $segCount = $this->read_ushort() / 2; |
| 998 | 998 | $this->skip(6); |
| 999 | 999 | $endCount = array(); |
| 1000 | - for($i=0; $i<$segCount; $i++) { $endCount[] = $this->read_ushort(); } |
|
| 1000 | + for ($i = 0; $i < $segCount; $i++) { $endCount[] = $this->read_ushort(); } |
|
| 1001 | 1001 | $this->skip(2); |
| 1002 | 1002 | $startCount = array(); |
| 1003 | - for($i=0; $i<$segCount; $i++) { $startCount[] = $this->read_ushort(); } |
|
| 1003 | + for ($i = 0; $i < $segCount; $i++) { $startCount[] = $this->read_ushort(); } |
|
| 1004 | 1004 | $idDelta = array(); |
| 1005 | - for($i=0; $i<$segCount; $i++) { $idDelta[] = $this->read_short(); } // ???? was unsigned short |
|
| 1005 | + for ($i = 0; $i < $segCount; $i++) { $idDelta[] = $this->read_short(); } // ???? was unsigned short |
|
| 1006 | 1006 | $idRangeOffset_start = $this->_pos; |
| 1007 | 1007 | $idRangeOffset = array(); |
| 1008 | - for($i=0; $i<$segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } |
|
| 1008 | + for ($i = 0; $i < $segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } |
|
| 1009 | 1009 | |
| 1010 | - for ($n=0;$n<$segCount;$n++) { |
|
| 1010 | + for ($n = 0; $n < $segCount; $n++) { |
|
| 1011 | 1011 | $endpoint = ($endCount[$n] + 1); |
| 1012 | - for ($unichar=$startCount[$n];$unichar<$endpoint;$unichar++) { |
|
| 1012 | + for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) { |
|
| 1013 | 1013 | if ($idRangeOffset[$n] == 0) |
| 1014 | 1014 | $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
| 1015 | 1015 | else { |
@@ -1024,7 +1024,7 @@ discard block |
||
| 1024 | 1024 | } |
| 1025 | 1025 | } |
| 1026 | 1026 | $charToGlyph[$unichar] = $glyph; |
| 1027 | - if ($unichar < 196608) { $this->maxUniChar = max($unichar,$this->maxUniChar); } |
|
| 1027 | + if ($unichar < 196608) { $this->maxUniChar = max($unichar, $this->maxUniChar); } |
|
| 1028 | 1028 | $glyphToChar[$glyph][] = $unichar; |
| 1029 | 1029 | } |
| 1030 | 1030 | } |
@@ -1046,38 +1046,38 @@ discard block |
||
| 1046 | 1046 | |
| 1047 | 1047 | // Header |
| 1048 | 1048 | if (_TTF_MAC_HEADER) { |
| 1049 | - $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac |
|
| 1049 | + $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac |
|
| 1050 | 1050 | } |
| 1051 | 1051 | else { |
| 1052 | - $stm .= (pack("Nnnnn", 0x00010000 , $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows |
|
| 1052 | + $stm .= (pack("Nnnnn", 0x00010000, $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows |
|
| 1053 | 1053 | } |
| 1054 | 1054 | |
| 1055 | 1055 | // Table directory |
| 1056 | 1056 | $tables = $this->otables; |
| 1057 | 1057 | |
| 1058 | - ksort ($tables); |
|
| 1058 | + ksort($tables); |
|
| 1059 | 1059 | $offset = 12 + $numTables * 16; |
| 1060 | 1060 | foreach ($tables AS $tag=>$data) { |
| 1061 | 1061 | if ($tag == 'head') { $head_start = $offset; } |
| 1062 | 1062 | $stm .= $tag; |
| 1063 | 1063 | $checksum = $this->calcChecksum($data); |
| 1064 | - $stm .= pack("nn", $checksum[0],$checksum[1]); |
|
| 1064 | + $stm .= pack("nn", $checksum[0], $checksum[1]); |
|
| 1065 | 1065 | $stm .= pack("NN", $offset, strlen($data)); |
| 1066 | - $paddedLength = (strlen($data)+3)&~3; |
|
| 1066 | + $paddedLength = (strlen($data) + 3) & ~3; |
|
| 1067 | 1067 | $offset = $offset + $paddedLength; |
| 1068 | 1068 | } |
| 1069 | 1069 | |
| 1070 | 1070 | // Table data |
| 1071 | 1071 | foreach ($tables AS $tag=>$data) { |
| 1072 | 1072 | $data .= "\0\0\0"; |
| 1073 | - $stm .= substr($data,0,(strlen($data)&~3)); |
|
| 1073 | + $stm .= substr($data, 0, (strlen($data) & ~3)); |
|
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | 1076 | $checksum = $this->calcChecksum($stm); |
| 1077 | - $checksum = $this->sub32(array(0xB1B0,0xAFBA), $checksum); |
|
| 1078 | - $chk = pack("nn", $checksum[0],$checksum[1]); |
|
| 1079 | - $stm = $this->splice($stm,($head_start + 8),$chk); |
|
| 1080 | - return $stm ; |
|
| 1077 | + $checksum = $this->sub32(array(0xB1B0, 0xAFBA), $checksum); |
|
| 1078 | + $chk = pack("nn", $checksum[0], $checksum[1]); |
|
| 1079 | + $stm = $this->splice($stm, ($head_start + 8), $chk); |
|
| 1080 | + return $stm; |
|
| 1081 | 1081 | } |
| 1082 | 1082 | |
| 1083 | 1083 | |
@@ -87,12 +87,15 @@ discard block |
||
| 87 | 87 | $this->descent = 0; |
| 88 | 88 | $this->TTCFonts = array(); |
| 89 | 89 | $this->version = $version = $this->read_ulong(); |
| 90 | - if ($version==0x4F54544F) |
|
| 91 | - die("Postscript outlines are not supported"); |
|
| 92 | - if ($version==0x74746366) |
|
| 93 | - die("ERROR - TrueType Fonts Collections not supported"); |
|
| 94 | - if (!in_array($version, array(0x00010000,0x74727565))) |
|
| 95 | - die("Not a TrueType font: version=".$version); |
|
| 90 | + if ($version==0x4F54544F) { |
|
| 91 | + die("Postscript outlines are not supported"); |
|
| 92 | + } |
|
| 93 | + if ($version==0x74746366) { |
|
| 94 | + die("ERROR - TrueType Fonts Collections not supported"); |
|
| 95 | + } |
|
| 96 | + if (!in_array($version, array(0x00010000,0x74727565))) { |
|
| 97 | + die("Not a TrueType font: version=".$version); |
|
| 98 | + } |
|
| 96 | 99 | $this->readTableDirectory(); |
| 97 | 100 | $this->extractInfo(); |
| 98 | 101 | fclose($this->fh); |
@@ -277,8 +280,9 @@ discard block |
||
| 277 | 280 | |
| 278 | 281 | $name_offset = $this->seek_table("name"); |
| 279 | 282 | $format = $this->read_ushort(); |
| 280 | - if ($format != 0) |
|
| 281 | - die("Unknown name table format ".$format); |
|
| 283 | + if ($format != 0) { |
|
| 284 | + die("Unknown name table format ".$format); |
|
| 285 | + } |
|
| 282 | 286 | $numRecords = $this->read_ushort(); |
| 283 | 287 | $string_data_offset = $name_offset + $this->read_ushort(); |
| 284 | 288 | $names = array(1=>'',2=>'',3=>'',4=>'',6=>''); |
@@ -291,13 +295,16 @@ discard block |
||
| 291 | 295 | $nameId = $this->read_ushort(); |
| 292 | 296 | $length = $this->read_ushort(); |
| 293 | 297 | $offset = $this->read_ushort(); |
| 294 | - if (!in_array($nameId,$K)) continue; |
|
| 298 | + if (!in_array($nameId,$K)) { |
|
| 299 | + continue; |
|
| 300 | + } |
|
| 295 | 301 | $N = ''; |
| 296 | 302 | if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name |
| 297 | 303 | $opos = $this->_pos; |
| 298 | 304 | $this->seek($string_data_offset + $offset); |
| 299 | - if ($length % 2 != 0) |
|
| 300 | - die("PostScript name is UTF-16BE string of odd length"); |
|
| 305 | + if ($length % 2 != 0) { |
|
| 306 | + die("PostScript name is UTF-16BE string of odd length"); |
|
| 307 | + } |
|
| 301 | 308 | $length /= 2; |
| 302 | 309 | $N = ''; |
| 303 | 310 | while ($length > 0) { |
@@ -307,8 +314,7 @@ discard block |
||
| 307 | 314 | } |
| 308 | 315 | $this->_pos = $opos; |
| 309 | 316 | $this->seek($opos); |
| 310 | - } |
|
| 311 | - else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name |
|
| 317 | + } else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name |
|
| 312 | 318 | $opos = $this->_pos; |
| 313 | 319 | $N = $this->get_chunk($string_data_offset + $offset, $length); |
| 314 | 320 | $this->_pos = $opos; |
@@ -317,19 +323,23 @@ discard block |
||
| 317 | 323 | if ($N && $names[$nameId]=='') { |
| 318 | 324 | $names[$nameId] = $N; |
| 319 | 325 | $nameCount -= 1; |
| 320 | - if ($nameCount==0) break; |
|
| 326 | + if ($nameCount==0) { |
|
| 327 | + break; |
|
| 328 | + } |
|
| 321 | 329 | } |
| 322 | 330 | } |
| 323 | - if ($names[6]) |
|
| 324 | - $psName = $names[6]; |
|
| 325 | - else if ($names[4]) |
|
| 326 | - $psName = preg_replace('/ /','-',$names[4]); |
|
| 327 | - else if ($names[1]) |
|
| 328 | - $psName = preg_replace('/ /','-',$names[1]); |
|
| 329 | - else |
|
| 330 | - $psName = ''; |
|
| 331 | - if (!$psName) |
|
| 332 | - die("Could not find PostScript font name"); |
|
| 331 | + if ($names[6]) { |
|
| 332 | + $psName = $names[6]; |
|
| 333 | + } else if ($names[4]) { |
|
| 334 | + $psName = preg_replace('/ /','-',$names[4]); |
|
| 335 | + } else if ($names[1]) { |
|
| 336 | + $psName = preg_replace('/ /','-',$names[1]); |
|
| 337 | + } else { |
|
| 338 | + $psName = ''; |
|
| 339 | + } |
|
| 340 | + if (!$psName) { |
|
| 341 | + die("Could not find PostScript font name"); |
|
| 342 | + } |
|
| 333 | 343 | $this->name = $psName; |
| 334 | 344 | if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } |
| 335 | 345 | if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } |
@@ -353,8 +363,9 @@ discard block |
||
| 353 | 363 | $this->skip(3*2); |
| 354 | 364 | $indexToLocFormat = $this->read_ushort(); |
| 355 | 365 | $glyphDataFormat = $this->read_ushort(); |
| 356 | - if ($glyphDataFormat != 0) |
|
| 357 | - die('Unknown glyph data format '.$glyphDataFormat); |
|
| 366 | + if ($glyphDataFormat != 0) { |
|
| 367 | + die('Unknown glyph data format '.$glyphDataFormat); |
|
| 368 | + } |
|
| 358 | 369 | |
| 359 | 370 | /////////////////////////////////// |
| 360 | 371 | // hhea metrics table |
@@ -392,21 +403,27 @@ discard block |
||
| 392 | 403 | $this->skip(26); |
| 393 | 404 | $sTypoAscender = $this->read_short(); |
| 394 | 405 | $sTypoDescender = $this->read_short(); |
| 395 | - if (!$this->ascent) $this->ascent = ($sTypoAscender*$scale); |
|
| 396 | - if (!$this->descent) $this->descent = ($sTypoDescender*$scale); |
|
| 406 | + if (!$this->ascent) { |
|
| 407 | + $this->ascent = ($sTypoAscender*$scale); |
|
| 408 | + } |
|
| 409 | + if (!$this->descent) { |
|
| 410 | + $this->descent = ($sTypoDescender*$scale); |
|
| 411 | + } |
|
| 397 | 412 | if ($version > 1) { |
| 398 | 413 | $this->skip(16); |
| 399 | 414 | $sCapHeight = $this->read_short(); |
| 400 | 415 | $this->capHeight = ($sCapHeight*$scale); |
| 401 | - } |
|
| 402 | - else { |
|
| 416 | + } else { |
|
| 403 | 417 | $this->capHeight = $this->ascent; |
| 404 | 418 | } |
| 405 | - } |
|
| 406 | - else { |
|
| 419 | + } else { |
|
| 407 | 420 | $usWeightClass = 500; |
| 408 | - if (!$this->ascent) $this->ascent = ($yMax*$scale); |
|
| 409 | - if (!$this->descent) $this->descent = ($yMin*$scale); |
|
| 421 | + if (!$this->ascent) { |
|
| 422 | + $this->ascent = ($yMax*$scale); |
|
| 423 | + } |
|
| 424 | + if (!$this->descent) { |
|
| 425 | + $this->descent = ($yMin*$scale); |
|
| 426 | + } |
|
| 410 | 427 | $this->capHeight = $this->ascent; |
| 411 | 428 | } |
| 412 | 429 | $this->stemV = 50 + intval(pow(($usWeightClass / 65.0),2)); |
@@ -423,12 +440,15 @@ discard block |
||
| 423 | 440 | |
| 424 | 441 | $this->flags = 4; |
| 425 | 442 | |
| 426 | - if ($this->italicAngle!= 0) |
|
| 427 | - $this->flags = $this->flags | 64; |
|
| 428 | - if ($usWeightClass >= 600) |
|
| 429 | - $this->flags = $this->flags | 262144; |
|
| 430 | - if ($isFixedPitch) |
|
| 431 | - $this->flags = $this->flags | 1; |
|
| 443 | + if ($this->italicAngle!= 0) { |
|
| 444 | + $this->flags = $this->flags | 64; |
|
| 445 | + } |
|
| 446 | + if ($usWeightClass >= 600) { |
|
| 447 | + $this->flags = $this->flags | 262144; |
|
| 448 | + } |
|
| 449 | + if ($isFixedPitch) { |
|
| 450 | + $this->flags = $this->flags | 1; |
|
| 451 | + } |
|
| 432 | 452 | |
| 433 | 453 | /////////////////////////////////// |
| 434 | 454 | // hhea - Horizontal header table |
@@ -436,11 +456,13 @@ discard block |
||
| 436 | 456 | $this->seek_table("hhea"); |
| 437 | 457 | $this->skip(32); |
| 438 | 458 | $metricDataFormat = $this->read_ushort(); |
| 439 | - if ($metricDataFormat != 0) |
|
| 440 | - die('Unknown horizontal metric data format '.$metricDataFormat); |
|
| 459 | + if ($metricDataFormat != 0) { |
|
| 460 | + die('Unknown horizontal metric data format '.$metricDataFormat); |
|
| 461 | + } |
|
| 441 | 462 | $numberOfHMetrics = $this->read_ushort(); |
| 442 | - if ($numberOfHMetrics == 0) |
|
| 443 | - die('Number of horizontal metrics is 0'); |
|
| 463 | + if ($numberOfHMetrics == 0) { |
|
| 464 | + die('Number of horizontal metrics is 0'); |
|
| 465 | + } |
|
| 444 | 466 | |
| 445 | 467 | /////////////////////////////////// |
| 446 | 468 | // maxp - Maximum profile table |
@@ -465,14 +487,17 @@ discard block |
||
| 465 | 487 | if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
| 466 | 488 | $format = $this->get_ushort($cmap_offset + $offset); |
| 467 | 489 | if ($format == 4) { |
| 468 | - if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; |
|
| 490 | + if (!$unicode_cmap_offset) { |
|
| 491 | + $unicode_cmap_offset = $cmap_offset + $offset; |
|
| 492 | + } |
|
| 469 | 493 | break; |
| 470 | 494 | } |
| 471 | 495 | } |
| 472 | 496 | $this->seek($save_pos ); |
| 473 | 497 | } |
| 474 | - if (!$unicode_cmap_offset) |
|
| 475 | - die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 498 | + if (!$unicode_cmap_offset) { |
|
| 499 | + die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 500 | + } |
|
| 476 | 501 | |
| 477 | 502 | |
| 478 | 503 | $glyphToChar = array(); |
@@ -553,8 +578,9 @@ discard block |
||
| 553 | 578 | $this->seek($save_pos ); |
| 554 | 579 | } |
| 555 | 580 | |
| 556 | - if (!$unicode_cmap_offset) |
|
| 557 | - die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 581 | + if (!$unicode_cmap_offset) { |
|
| 582 | + die('Font ('.$this->filename .') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
| 583 | + } |
|
| 558 | 584 | |
| 559 | 585 | |
| 560 | 586 | $glyphToChar = array(); |
@@ -739,10 +765,12 @@ discard block |
||
| 739 | 765 | $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
| 740 | 766 | if ($glyfLength < $this->maxStrLenRead) { |
| 741 | 767 | $data = substr($glyphData,$glyphPos,$glyphLen); |
| 742 | - } |
|
| 743 | - else { |
|
| 744 | - if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset+$glyphPos,$glyphLen); |
|
| 745 | - else $data = ''; |
|
| 768 | + } else { |
|
| 769 | + if ($glyphLen > 0) { |
|
| 770 | + $data = $this->get_chunk($glyfOffset+$glyphPos,$glyphLen); |
|
| 771 | + } else { |
|
| 772 | + $data = ''; |
|
| 773 | + } |
|
| 746 | 774 | } |
| 747 | 775 | |
| 748 | 776 | if ($glyphLen > 0) { |
@@ -762,11 +790,8 @@ discard block |
||
| 762 | 790 | $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; |
| 763 | 791 | $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); |
| 764 | 792 | $pos_in_glyph += 4; |
| 765 | - if ($flags & GF_WORDS) { $pos_in_glyph += 4; } |
|
| 766 | - else { $pos_in_glyph += 2; } |
|
| 767 | - if ($flags & GF_SCALE) { $pos_in_glyph += 2; } |
|
| 768 | - else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } |
|
| 769 | - else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } |
|
| 793 | + if ($flags & GF_WORDS) { $pos_in_glyph += 4; } else { $pos_in_glyph += 2; } |
|
| 794 | + if ($flags & GF_SCALE) { $pos_in_glyph += 2; } else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } |
|
| 770 | 795 | } |
| 771 | 796 | $maxComponentElements = max($maxComponentElements, $nComponentElements); |
| 772 | 797 | } |
@@ -791,8 +816,7 @@ discard block |
||
| 791 | 816 | if ((($pos + 1) >> 1) > 0xFFFF) { |
| 792 | 817 | $indexToLocFormat = 1; // long format |
| 793 | 818 | foreach($offsets AS $offset) { $locastr .= pack("N",$offset); } |
| 794 | - } |
|
| 795 | - else { |
|
| 819 | + } else { |
|
| 796 | 820 | $indexToLocFormat = 0; // short format |
| 797 | 821 | foreach($offsets AS $offset) { $locastr .= pack("n",($offset/2)); } |
| 798 | 822 | } |
@@ -836,8 +860,7 @@ discard block |
||
| 836 | 860 | foreach($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { |
| 837 | 861 | $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); |
| 838 | 862 | } |
| 839 | - } |
|
| 840 | - else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) { // simple |
|
| 863 | + } else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) { // simple |
|
| 841 | 864 | $contours += $this->glyphdata[$originalGlyphIdx]['nContours']; |
| 842 | 865 | $points += $this->glyphdata[$originalGlyphIdx]['nPoints']; |
| 843 | 866 | } |
@@ -868,16 +891,18 @@ discard block |
||
| 868 | 891 | $savepos = ftell($this->fh); |
| 869 | 892 | $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs); |
| 870 | 893 | $this->seek($savepos); |
| 871 | - if ($flags & GF_WORDS) |
|
| 872 | - $this->skip(4); |
|
| 873 | - else |
|
| 874 | - $this->skip(2); |
|
| 875 | - if ($flags & GF_SCALE) |
|
| 876 | - $this->skip(2); |
|
| 877 | - else if ($flags & GF_XYSCALE) |
|
| 878 | - $this->skip(4); |
|
| 879 | - else if ($flags & GF_TWOBYTWO) |
|
| 880 | - $this->skip(8); |
|
| 894 | + if ($flags & GF_WORDS) { |
|
| 895 | + $this->skip(4); |
|
| 896 | + } else { |
|
| 897 | + $this->skip(2); |
|
| 898 | + } |
|
| 899 | + if ($flags & GF_SCALE) { |
|
| 900 | + $this->skip(2); |
|
| 901 | + } else if ($flags & GF_XYSCALE) { |
|
| 902 | + $this->skip(4); |
|
| 903 | + } else if ($flags & GF_TWOBYTWO) { |
|
| 904 | + $this->skip(8); |
|
| 905 | + } |
|
| 881 | 906 | } |
| 882 | 907 | } |
| 883 | 908 | } |
@@ -892,14 +917,12 @@ discard block |
||
| 892 | 917 | if (($numberOfHMetrics*4) < $this->maxStrLenRead) { |
| 893 | 918 | $data = $this->get_chunk($start,($numberOfHMetrics*4)); |
| 894 | 919 | $arr = unpack("n*", $data); |
| 895 | - } |
|
| 896 | - else { $this->seek($start); } |
|
| 920 | + } else { $this->seek($start); } |
|
| 897 | 921 | for( $glyph=0; $glyph<$numberOfHMetrics; $glyph++) { |
| 898 | 922 | |
| 899 | 923 | if (($numberOfHMetrics*4) < $this->maxStrLenRead) { |
| 900 | 924 | $aw = $arr[($glyph*2)+1]; |
| 901 | - } |
|
| 902 | - else { |
|
| 925 | + } else { |
|
| 903 | 926 | $aw = $this->read_ushort(); |
| 904 | 927 | $lsb = $this->read_ushort(); |
| 905 | 928 | } |
@@ -954,8 +977,7 @@ discard block |
||
| 954 | 977 | if ($gid < $numberOfHMetrics) { |
| 955 | 978 | $this->seek($start+($gid*4)); |
| 956 | 979 | $hm = fread($this->fh,4); |
| 957 | - } |
|
| 958 | - else { |
|
| 980 | + } else { |
|
| 959 | 981 | $this->seek($start+(($numberOfHMetrics-1)*4)); |
| 960 | 982 | $hm = fread($this->fh,2); |
| 961 | 983 | $this->seek($start+($numberOfHMetrics*2)+($gid*2)); |
@@ -973,16 +995,15 @@ discard block |
||
| 973 | 995 | for ($n=0; $n<=$numGlyphs; $n++) { |
| 974 | 996 | $this->glyphPos[] = ($arr[$n+1] * 2); |
| 975 | 997 | } |
| 976 | - } |
|
| 977 | - else if ($indexToLocFormat == 1) { |
|
| 998 | + } else if ($indexToLocFormat == 1) { |
|
| 978 | 999 | $data = $this->get_chunk($start,($numGlyphs*4)+4); |
| 979 | 1000 | $arr = unpack("N*", $data); |
| 980 | 1001 | for ($n=0; $n<=$numGlyphs; $n++) { |
| 981 | 1002 | $this->glyphPos[] = ($arr[$n+1]); |
| 982 | 1003 | } |
| 1004 | + } else { |
|
| 1005 | + die('Unknown location table format '.$indexToLocFormat); |
|
| 983 | 1006 | } |
| 984 | - else |
|
| 985 | - die('Unknown location table format '.$indexToLocFormat); |
|
| 986 | 1007 | } |
| 987 | 1008 | |
| 988 | 1009 | |
@@ -1010,17 +1031,18 @@ discard block |
||
| 1010 | 1031 | for ($n=0;$n<$segCount;$n++) { |
| 1011 | 1032 | $endpoint = ($endCount[$n] + 1); |
| 1012 | 1033 | for ($unichar=$startCount[$n];$unichar<$endpoint;$unichar++) { |
| 1013 | - if ($idRangeOffset[$n] == 0) |
|
| 1014 | - $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
|
| 1015 | - else { |
|
| 1034 | + if ($idRangeOffset[$n] == 0) { |
|
| 1035 | + $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
|
| 1036 | + } else { |
|
| 1016 | 1037 | $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; |
| 1017 | 1038 | $offset = $idRangeOffset_start + 2 * $n + $offset; |
| 1018 | - if ($offset >= $limit) |
|
| 1019 | - $glyph = 0; |
|
| 1020 | - else { |
|
| 1039 | + if ($offset >= $limit) { |
|
| 1040 | + $glyph = 0; |
|
| 1041 | + } else { |
|
| 1021 | 1042 | $glyph = $this->get_ushort($offset); |
| 1022 | - if ($glyph != 0) |
|
| 1023 | - $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; |
|
| 1043 | + if ($glyph != 0) { |
|
| 1044 | + $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; |
|
| 1045 | + } |
|
| 1024 | 1046 | } |
| 1025 | 1047 | } |
| 1026 | 1048 | $charToGlyph[$unichar] = $glyph; |
@@ -1047,8 +1069,7 @@ discard block |
||
| 1047 | 1069 | // Header |
| 1048 | 1070 | if (_TTF_MAC_HEADER) { |
| 1049 | 1071 | $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac |
| 1050 | - } |
|
| 1051 | - else { |
|
| 1072 | + } else { |
|
| 1052 | 1073 | $stm .= (pack("Nnnnn", 0x00010000 , $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows |
| 1053 | 1074 | } |
| 1054 | 1075 | |
@@ -120,6 +120,7 @@ discard block |
||
| 120 | 120 | |
| 121 | 121 | /** |
| 122 | 122 | * Compute key depending on object number where the encrypted data is stored |
| 123 | + * @param integer $n |
|
| 123 | 124 | */ |
| 124 | 125 | function _objectkey($n) |
| 125 | 126 | { |
@@ -168,6 +169,8 @@ discard block |
||
| 168 | 169 | |
| 169 | 170 | /** |
| 170 | 171 | * Compute O value |
| 172 | + * @param string $user_pass |
|
| 173 | + * @param string $owner_pass |
|
| 171 | 174 | */ |
| 172 | 175 | function _Ovalue($user_pass, $owner_pass) |
| 173 | 176 | { |
@@ -186,6 +189,8 @@ discard block |
||
| 186 | 189 | |
| 187 | 190 | /** |
| 188 | 191 | * Compute encryption key |
| 192 | + * @param string $user_pass |
|
| 193 | + * @param integer $protection |
|
| 189 | 194 | */ |
| 190 | 195 | function _generateencryptionkey($user_pass, $owner_pass, $protection) |
| 191 | 196 | { |
@@ -69,15 +69,15 @@ discard block |
||
| 69 | 69 | var $enc_obj_id; //encryption object id |
| 70 | 70 | |
| 71 | 71 | /** |
| 72 | - * Function to set permissions as well as user and owner passwords |
|
| 73 | - * |
|
| 74 | - * - permissions is an array with values taken from the following list: |
|
| 75 | - * copy, print, modify, annot-forms |
|
| 76 | - * If a value is present it means that the permission is granted |
|
| 77 | - * - If a user password is set, user will be prompted before document is opened |
|
| 78 | - * - If an owner password is set, document can be opened in privilege mode with no |
|
| 79 | - * restriction if that password is entered |
|
| 80 | - */ |
|
| 72 | + * Function to set permissions as well as user and owner passwords |
|
| 73 | + * |
|
| 74 | + * - permissions is an array with values taken from the following list: |
|
| 75 | + * copy, print, modify, annot-forms |
|
| 76 | + * If a value is present it means that the permission is granted |
|
| 77 | + * - If a user password is set, user will be prompted before document is opened |
|
| 78 | + * - If an owner password is set, document can be opened in privilege mode with no |
|
| 79 | + * restriction if that password is entered |
|
| 80 | + */ |
|
| 81 | 81 | function SetProtection($permissions=array(), $user_pass='', $owner_pass=null) |
| 82 | 82 | { |
| 83 | 83 | $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32 ); |
@@ -119,8 +119,8 @@ discard block |
||
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | /** |
| 122 | - * Compute key depending on object number where the encrypted data is stored |
|
| 123 | - */ |
|
| 122 | + * Compute key depending on object number where the encrypted data is stored |
|
| 123 | + */ |
|
| 124 | 124 | function _objectkey($n) |
| 125 | 125 | { |
| 126 | 126 | return substr($this->_md5_16($this->encryption_key.pack('VXxx',$n)),0,10); |
@@ -159,16 +159,16 @@ discard block |
||
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
| 162 | - * Get MD5 as binary string |
|
| 163 | - */ |
|
| 162 | + * Get MD5 as binary string |
|
| 163 | + */ |
|
| 164 | 164 | function _md5_16($string) |
| 165 | 165 | { |
| 166 | 166 | return pack('H*',md5($string)); |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
| 170 | - * Compute O value |
|
| 171 | - */ |
|
| 170 | + * Compute O value |
|
| 171 | + */ |
|
| 172 | 172 | function _Ovalue($user_pass, $owner_pass) |
| 173 | 173 | { |
| 174 | 174 | $tmp = $this->_md5_16($owner_pass); |
@@ -177,16 +177,16 @@ discard block |
||
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
| 180 | - * Compute U value |
|
| 181 | - */ |
|
| 180 | + * Compute U value |
|
| 181 | + */ |
|
| 182 | 182 | function _Uvalue() |
| 183 | 183 | { |
| 184 | 184 | return RC4($this->encryption_key, $this->padding); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /** |
| 188 | - * Compute encryption key |
|
| 189 | - */ |
|
| 188 | + * Compute encryption key |
|
| 189 | + */ |
|
| 190 | 190 | function _generateencryptionkey($user_pass, $owner_pass, $protection) |
| 191 | 191 | { |
| 192 | 192 | // Pad passwords |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | require('tfpdf.class.php'); |
| 15 | 15 | |
| 16 | -if(function_exists('mcrypt_encrypt')) |
|
| 16 | +if (function_exists('mcrypt_encrypt')) |
|
| 17 | 17 | { |
| 18 | 18 | function RC4($key, $data) |
| 19 | 19 | { |
@@ -26,12 +26,12 @@ discard block |
||
| 26 | 26 | { |
| 27 | 27 | static $last_key, $last_state; |
| 28 | 28 | |
| 29 | - if($key != $last_key) |
|
| 29 | + if ($key != $last_key) |
|
| 30 | 30 | { |
| 31 | - $k = str_repeat($key, 256/strlen($key)+1); |
|
| 31 | + $k = str_repeat($key, 256 / strlen($key) + 1); |
|
| 32 | 32 | $state = range(0, 255); |
| 33 | 33 | $j = 0; |
| 34 | - for ($i=0; $i<256; $i++){ |
|
| 34 | + for ($i = 0; $i < 256; $i++) { |
|
| 35 | 35 | $t = $state[$i]; |
| 36 | 36 | $j = ($j + $t + ord($k[$i])) % 256; |
| 37 | 37 | $state[$i] = $state[$j]; |
@@ -47,13 +47,13 @@ discard block |
||
| 47 | 47 | $a = 0; |
| 48 | 48 | $b = 0; |
| 49 | 49 | $out = ''; |
| 50 | - for ($i=0; $i<$len; $i++){ |
|
| 51 | - $a = ($a+1) % 256; |
|
| 50 | + for ($i = 0; $i < $len; $i++) { |
|
| 51 | + $a = ($a + 1) % 256; |
|
| 52 | 52 | $t = $state[$a]; |
| 53 | - $b = ($b+$t) % 256; |
|
| 53 | + $b = ($b + $t) % 256; |
|
| 54 | 54 | $state[$a] = $state[$b]; |
| 55 | 55 | $state[$b] = $t; |
| 56 | - $k = $state[($state[$a]+$state[$b]) % 256]; |
|
| 56 | + $k = $state[($state[$a] + $state[$b]) % 256]; |
|
| 57 | 57 | $out .= chr(ord($data[$i]) ^ $k); |
| 58 | 58 | } |
| 59 | 59 | return $out; |
@@ -62,11 +62,11 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | class FPDF_Protection extends tFPDF |
| 64 | 64 | { |
| 65 | - var $encrypted = false; //whether document is protected |
|
| 66 | - var $Uvalue; //U entry in pdf document |
|
| 67 | - var $Ovalue; //O entry in pdf document |
|
| 68 | - var $Pvalue; //P entry in pdf document |
|
| 69 | - var $enc_obj_id; //encryption object id |
|
| 65 | + var $encrypted = false; //whether document is protected |
|
| 66 | + var $Uvalue; //U entry in pdf document |
|
| 67 | + var $Ovalue; //O entry in pdf document |
|
| 68 | + var $Pvalue; //P entry in pdf document |
|
| 69 | + var $enc_obj_id; //encryption object id |
|
| 70 | 70 | |
| 71 | 71 | /** |
| 72 | 72 | * Function to set permissions as well as user and owner passwords |
@@ -78,11 +78,11 @@ discard block |
||
| 78 | 78 | * - If an owner password is set, document can be opened in privilege mode with no |
| 79 | 79 | * restriction if that password is entered |
| 80 | 80 | */ |
| 81 | - function SetProtection($permissions=array(), $user_pass='', $owner_pass=null) |
|
| 81 | + function SetProtection($permissions = array(), $user_pass = '', $owner_pass = null) |
|
| 82 | 82 | { |
| 83 | - $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32 ); |
|
| 83 | + $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32); |
|
| 84 | 84 | $protection = 192; |
| 85 | - foreach($permissions as $permission) |
|
| 85 | + foreach ($permissions as $permission) |
|
| 86 | 86 | { |
| 87 | 87 | if (!isset($options[$permission])) |
| 88 | 88 | $this->Error('Incorrect permission: '.$permission); |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | */ |
| 124 | 124 | function _objectkey($n) |
| 125 | 125 | { |
| 126 | - return substr($this->_md5_16($this->encryption_key.pack('VXxx',$n)),0,10); |
|
| 126 | + return substr($this->_md5_16($this->encryption_key.pack('VXxx', $n)), 0, 10); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | function _putresources() |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | */ |
| 164 | 164 | function _md5_16($string) |
| 165 | 165 | { |
| 166 | - return pack('H*',md5($string)); |
|
| 166 | + return pack('H*', md5($string)); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | function _Ovalue($user_pass, $owner_pass) |
| 173 | 173 | { |
| 174 | 174 | $tmp = $this->_md5_16($owner_pass); |
| 175 | - $owner_RC4_key = substr($tmp,0,5); |
|
| 175 | + $owner_RC4_key = substr($tmp, 0, 5); |
|
| 176 | 176 | return RC4($owner_RC4_key, $user_pass); |
| 177 | 177 | } |
| 178 | 178 | |
@@ -190,28 +190,28 @@ discard block |
||
| 190 | 190 | function _generateencryptionkey($user_pass, $owner_pass, $protection) |
| 191 | 191 | { |
| 192 | 192 | // Pad passwords |
| 193 | - $user_pass = substr($user_pass.$this->padding,0,32); |
|
| 194 | - $owner_pass = substr($owner_pass.$this->padding,0,32); |
|
| 193 | + $user_pass = substr($user_pass.$this->padding, 0, 32); |
|
| 194 | + $owner_pass = substr($owner_pass.$this->padding, 0, 32); |
|
| 195 | 195 | // Compute O value |
| 196 | - $this->Ovalue = $this->_Ovalue($user_pass,$owner_pass); |
|
| 196 | + $this->Ovalue = $this->_Ovalue($user_pass, $owner_pass); |
|
| 197 | 197 | // Compute encyption key |
| 198 | 198 | $tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF"); |
| 199 | - $this->encryption_key = substr($tmp,0,5); |
|
| 199 | + $this->encryption_key = substr($tmp, 0, 5); |
|
| 200 | 200 | // Compute U value |
| 201 | 201 | $this->Uvalue = $this->_Uvalue(); |
| 202 | 202 | // Compute P value |
| 203 | - $this->Pvalue = -(($protection^255)+1); |
|
| 203 | + $this->Pvalue = -(($protection ^ 255) + 1); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | function Header() |
| 207 | 207 | { |
| 208 | 208 | global $LANG, $k; |
| 209 | - $this->SetFont('helvetica','B',12); |
|
| 209 | + $this->SetFont('helvetica', 'B', 12); |
|
| 210 | 210 | // Teampass |
| 211 | - $this->Cell(100,10, "Teampass" ,0,0,'L', 0); |
|
| 211 | + $this->Cell(100, 10, "Teampass", 0, 0, 'L', 0); |
|
| 212 | 212 | // |
| 213 | - $this->SetFont('helvetica','B',12); |
|
| 214 | - $this->Cell(0,10,$LANG['print_out_pdf_title'],0,0,'L'); |
|
| 213 | + $this->SetFont('helvetica', 'B', 12); |
|
| 214 | + $this->Cell(0, 10, $LANG['print_out_pdf_title'], 0, 0, 'L'); |
|
| 215 | 215 | // Line break |
| 216 | 216 | $this->Ln(10); |
| 217 | 217 | } |
@@ -222,10 +222,10 @@ discard block |
||
| 222 | 222 | // Position at 1.5 cm from bottom |
| 223 | 223 | $this->SetY(-15); |
| 224 | 224 | // Arial italic 8 |
| 225 | - $this->SetFont('helvetica','I',8); |
|
| 225 | + $this->SetFont('helvetica', 'I', 8); |
|
| 226 | 226 | // info |
| 227 | - $this->Cell(0,10,$LANG['pdf_del_date']." ".date($_SESSION['settings']['date_format']." ".$_SESSION['settings']['time_format'], time()).' '.$LANG['by'].' '.$_SESSION['login'],0,0,'C'); |
|
| 227 | + $this->Cell(0, 10, $LANG['pdf_del_date']." ".date($_SESSION['settings']['date_format']." ".$_SESSION['settings']['time_format'], time()).' '.$LANG['by'].' '.$_SESSION['login'], 0, 0, 'C'); |
|
| 228 | 228 | // Page number |
| 229 | - $this->Cell(0,10,$LANG['pages'].' '.$this->PageNo().'/{nb}',0,0,'R'); |
|
| 229 | + $this->Cell(0, 10, $LANG['pages'].' '.$this->PageNo().'/{nb}', 0, 0, 'R'); |
|
| 230 | 230 | } |
| 231 | 231 | } |
@@ -19,8 +19,7 @@ discard block |
||
| 19 | 19 | { |
| 20 | 20 | return mcrypt_encrypt(MCRYPT_ARCFOUR, $key, $data, MCRYPT_MODE_STREAM, ''); |
| 21 | 21 | } |
| 22 | -} |
|
| 23 | -else |
|
| 22 | +} else |
|
| 24 | 23 | { |
| 25 | 24 | function RC4($key, $data) |
| 26 | 25 | { |
@@ -39,9 +38,9 @@ discard block |
||
| 39 | 38 | } |
| 40 | 39 | $last_key = $key; |
| 41 | 40 | $last_state = $state; |
| 41 | + } else { |
|
| 42 | + $state = $last_state; |
|
| 42 | 43 | } |
| 43 | - else |
|
| 44 | - $state = $last_state; |
|
| 45 | 44 | |
| 46 | 45 | $len = strlen($data); |
| 47 | 46 | $a = 0; |
@@ -84,12 +83,14 @@ discard block |
||
| 84 | 83 | $protection = 192; |
| 85 | 84 | foreach($permissions as $permission) |
| 86 | 85 | { |
| 87 | - if (!isset($options[$permission])) |
|
| 88 | - $this->Error('Incorrect permission: '.$permission); |
|
| 86 | + if (!isset($options[$permission])) { |
|
| 87 | + $this->Error('Incorrect permission: '.$permission); |
|
| 88 | + } |
|
| 89 | 89 | $protection += $options[$permission]; |
| 90 | 90 | } |
| 91 | - if ($owner_pass === null) |
|
| 92 | - $owner_pass = uniqid(rand()); |
|
| 91 | + if ($owner_pass === null) { |
|
| 92 | + $owner_pass = uniqid(rand()); |
|
| 93 | + } |
|
| 93 | 94 | $this->encrypted = true; |
| 94 | 95 | $this->padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08". |
| 95 | 96 | "\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A"; |
@@ -104,8 +105,9 @@ discard block |
||
| 104 | 105 | |
| 105 | 106 | function _putstream($s) |
| 106 | 107 | { |
| 107 | - if ($this->encrypted) |
|
| 108 | - $s = RC4($this->_objectkey($this->n), $s); |
|
| 108 | + if ($this->encrypted) { |
|
| 109 | + $s = RC4($this->_objectkey($this->n), $s); |
|
| 110 | + } |
|
| 109 | 111 | parent::_putstream($s); |
| 110 | 112 | } |
| 111 | 113 | |
@@ -113,8 +115,9 @@ discard block |
||
| 113 | 115 | { |
| 114 | 116 | //if (!$this->_isascii($s)) |
| 115 | 117 | // $s = $this->_UTF8toUTF16($s); |
| 116 | - if ($this->encrypted) |
|
| 117 | - $s = RC4($this->_objectkey($this->n), $s); |
|
| 118 | + if ($this->encrypted) { |
|
| 119 | + $s = RC4($this->_objectkey($this->n), $s); |
|
| 120 | + } |
|
| 118 | 121 | return '('.$this->_escape($s).')'; |
| 119 | 122 | } |
| 120 | 123 | |
@@ -170,6 +170,10 @@ discard block |
||
| 170 | 170 | $this->PDFVersion = '1.3'; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | +/** |
|
| 174 | + * @param double $left |
|
| 175 | + * @param double $top |
|
| 176 | + */ |
|
| 173 | 177 | function SetMargins($left, $top, $right=null) |
| 174 | 178 | { |
| 175 | 179 | // Set left, top and right margins |
@@ -200,6 +204,9 @@ discard block |
||
| 200 | 204 | $this->rMargin = $margin; |
| 201 | 205 | } |
| 202 | 206 | |
| 207 | +/** |
|
| 208 | + * @param boolean $auto |
|
| 209 | + */ |
|
| 203 | 210 | function SetAutoPageBreak($auto, $margin=0) |
| 204 | 211 | { |
| 205 | 212 | // Set auto page break mode and triggering margin |
@@ -208,6 +215,9 @@ discard block |
||
| 208 | 215 | $this->PageBreakTrigger = $this->h-$margin; |
| 209 | 216 | } |
| 210 | 217 | |
| 218 | +/** |
|
| 219 | + * @param string $zoom |
|
| 220 | + */ |
|
| 211 | 221 | function SetDisplayMode($zoom, $layout='default') |
| 212 | 222 | { |
| 213 | 223 | // Set display mode in viewer |
@@ -221,6 +231,9 @@ discard block |
||
| 221 | 231 | $this->Error('Incorrect layout display mode: '.$layout); |
| 222 | 232 | } |
| 223 | 233 | |
| 234 | +/** |
|
| 235 | + * @param boolean $compress |
|
| 236 | + */ |
|
| 224 | 237 | function SetCompression($compress) |
| 225 | 238 | { |
| 226 | 239 | // Set page compression |
@@ -473,6 +486,9 @@ discard block |
||
| 473 | 486 | $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op)); |
| 474 | 487 | } |
| 475 | 488 | |
| 489 | +/** |
|
| 490 | + * @param string $family |
|
| 491 | + */ |
|
| 476 | 492 | function AddFont($family, $style='', $file='', $uni=false) |
| 477 | 493 | { |
| 478 | 494 | // Add a TrueType, OpenType or Type1 font |
@@ -584,6 +600,9 @@ discard block |
||
| 584 | 600 | } |
| 585 | 601 | } |
| 586 | 602 | |
| 603 | +/** |
|
| 604 | + * @param string $family |
|
| 605 | + */ |
|
| 587 | 606 | function SetFont($family, $style='', $size=0) |
| 588 | 607 | { |
| 589 | 608 | // Select a font; size given in points |
@@ -665,6 +684,10 @@ discard block |
||
| 665 | 684 | $this->links[$link] = array($page, $y); |
| 666 | 685 | } |
| 667 | 686 | |
| 687 | +/** |
|
| 688 | + * @param double $y |
|
| 689 | + * @param string $link |
|
| 690 | + */ |
|
| 668 | 691 | function Link($x, $y, $w, $h, $link) |
| 669 | 692 | { |
| 670 | 693 | // Put a link on the page |
@@ -1072,6 +1095,9 @@ discard block |
||
| 1072 | 1095 | } |
| 1073 | 1096 | } |
| 1074 | 1097 | |
| 1098 | +/** |
|
| 1099 | + * @param integer $h |
|
| 1100 | + */ |
|
| 1075 | 1101 | function Ln($h=null) |
| 1076 | 1102 | { |
| 1077 | 1103 | // Line feed; default value is last cell height |
@@ -1305,6 +1331,10 @@ discard block |
||
| 1305 | 1331 | } |
| 1306 | 1332 | } |
| 1307 | 1333 | |
| 1334 | +/** |
|
| 1335 | + * @param string $orientation |
|
| 1336 | + * @param string $size |
|
| 1337 | + */ |
|
| 1308 | 1338 | function _beginpage($orientation, $size) |
| 1309 | 1339 | { |
| 1310 | 1340 | $this->page++; |
@@ -1350,6 +1380,9 @@ discard block |
||
| 1350 | 1380 | $this->state = 1; |
| 1351 | 1381 | } |
| 1352 | 1382 | |
| 1383 | +/** |
|
| 1384 | + * @param string $font |
|
| 1385 | + */ |
|
| 1353 | 1386 | function _loadfont($font) |
| 1354 | 1387 | { |
| 1355 | 1388 | // Load a font definition file from the font directory |
@@ -1437,6 +1470,9 @@ discard block |
||
| 1437 | 1470 | return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data); |
| 1438 | 1471 | } |
| 1439 | 1472 | |
| 1473 | +/** |
|
| 1474 | + * @param string $file |
|
| 1475 | + */ |
|
| 1440 | 1476 | function _parsepng($file) |
| 1441 | 1477 | { |
| 1442 | 1478 | // Extract info from a PNG file |
@@ -1973,6 +2009,9 @@ discard block |
||
| 1973 | 2009 | } |
| 1974 | 2010 | } |
| 1975 | 2011 | |
| 2012 | +/** |
|
| 2013 | + * @param integer $maxUni |
|
| 2014 | + */ |
|
| 1976 | 2015 | function _putTTfontwidths(&$font, $maxUni) { |
| 1977 | 2016 | if (file_exists($font['unifilename'].'.cw127.php')) { |
| 1978 | 2017 | include($font['unifilename'].'.cw127.php') ; |
@@ -481,12 +481,12 @@ discard block |
||
| 481 | 481 | if($style=='IB') |
| 482 | 482 | $style='BI'; |
| 483 | 483 | if($file=='') { |
| 484 | - if ($uni) { |
|
| 484 | + if ($uni) { |
|
| 485 | 485 | $file = str_replace(' ','',$family).strtolower($style).'.ttf'; |
| 486 | - } |
|
| 487 | - else { |
|
| 486 | + } |
|
| 487 | + else { |
|
| 488 | 488 | $file = str_replace(' ','',$family).strtolower($style).'.php'; |
| 489 | - } |
|
| 489 | + } |
|
| 490 | 490 | } |
| 491 | 491 | $fontkey = $family.$style; |
| 492 | 492 | if(isset($this->fonts[$fontkey])) |
@@ -1750,7 +1750,7 @@ discard block |
||
| 1750 | 1750 | } |
| 1751 | 1751 | foreach($this->FontFiles as $file=>$info) |
| 1752 | 1752 | { |
| 1753 | - if (!isset($info['type']) || $info['type']!='TTF') { |
|
| 1753 | + if (!isset($info['type']) || $info['type']!='TTF') { |
|
| 1754 | 1754 | // Font file embedding |
| 1755 | 1755 | $this->_newobj(); |
| 1756 | 1756 | $this->FontFiles[$file]['n']=$this->n; |
@@ -1785,7 +1785,7 @@ discard block |
||
| 1785 | 1785 | $this->_out('>>'); |
| 1786 | 1786 | $this->_putstream($font); |
| 1787 | 1787 | $this->_out('endobj'); |
| 1788 | - } |
|
| 1788 | + } |
|
| 1789 | 1789 | } |
| 1790 | 1790 | foreach($this->fonts as $k=>$font) |
| 1791 | 1791 | { |
@@ -2263,29 +2263,29 @@ discard block |
||
| 2263 | 2263 | |
| 2264 | 2264 | // Converts UTF-8 strings to codepoints array |
| 2265 | 2265 | function UTF8StringToArray($str) { |
| 2266 | - $out = array(); |
|
| 2267 | - $len = strlen($str); |
|
| 2268 | - for ($i = 0; $i < $len; $i++) { |
|
| 2266 | + $out = array(); |
|
| 2267 | + $len = strlen($str); |
|
| 2268 | + for ($i = 0; $i < $len; $i++) { |
|
| 2269 | 2269 | $uni = -1; |
| 2270 | - $h = ord($str[$i]); |
|
| 2271 | - if ( $h <= 0x7F ) |
|
| 2272 | - $uni = $h; |
|
| 2273 | - elseif ( $h >= 0xC2 ) { |
|
| 2274 | - if ( ($h <= 0xDF) && ($i < $len -1) ) |
|
| 2270 | + $h = ord($str[$i]); |
|
| 2271 | + if ( $h <= 0x7F ) |
|
| 2272 | + $uni = $h; |
|
| 2273 | + elseif ( $h >= 0xC2 ) { |
|
| 2274 | + if ( ($h <= 0xDF) && ($i < $len -1) ) |
|
| 2275 | 2275 | $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F); |
| 2276 | - elseif ( ($h <= 0xEF) && ($i < $len -2) ) |
|
| 2276 | + elseif ( ($h <= 0xEF) && ($i < $len -2) ) |
|
| 2277 | 2277 | $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6 |
| 2278 | - | (ord($str[++$i]) & 0x3F); |
|
| 2279 | - elseif ( ($h <= 0xF4) && ($i < $len -3) ) |
|
| 2278 | + | (ord($str[++$i]) & 0x3F); |
|
| 2279 | + elseif ( ($h <= 0xF4) && ($i < $len -3) ) |
|
| 2280 | 2280 | $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12 |
| 2281 | - | (ord($str[++$i]) & 0x3F) << 6 |
|
| 2282 | - | (ord($str[++$i]) & 0x3F); |
|
| 2283 | - } |
|
| 2281 | + | (ord($str[++$i]) & 0x3F) << 6 |
|
| 2282 | + | (ord($str[++$i]) & 0x3F); |
|
| 2283 | + } |
|
| 2284 | 2284 | if ($uni >= 0) { |
| 2285 | 2285 | $out[] = $uni; |
| 2286 | 2286 | } |
| 2287 | - } |
|
| 2288 | - return $out; |
|
| 2287 | + } |
|
| 2288 | + return $out; |
|
| 2289 | 2289 | } |
| 2290 | 2290 | |
| 2291 | 2291 | |
@@ -8,75 +8,75 @@ discard block |
||
| 8 | 8 | * License: LGPL * |
| 9 | 9 | *******************************************************************************/ |
| 10 | 10 | |
| 11 | -define('tFPDF_VERSION','1.24'); |
|
| 11 | +define('tFPDF_VERSION', '1.24'); |
|
| 12 | 12 | |
| 13 | 13 | class tFPDF |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | 16 | var $unifontSubset; |
| 17 | -var $page; // current page number |
|
| 18 | -var $n; // current object number |
|
| 19 | -var $offsets; // array of object offsets |
|
| 20 | -var $buffer; // buffer holding in-memory PDF |
|
| 21 | -var $pages; // array containing pages |
|
| 22 | -var $state; // current document state |
|
| 23 | -var $compress; // compression flag |
|
| 24 | -var $k; // scale factor (number of points in user unit) |
|
| 25 | -var $DefOrientation; // default orientation |
|
| 26 | -var $CurOrientation; // current orientation |
|
| 27 | -var $StdPageSizes; // standard page sizes |
|
| 28 | -var $DefPageSize; // default page size |
|
| 29 | -var $CurPageSize; // current page size |
|
| 30 | -var $PageSizes; // used for pages with non default sizes or orientations |
|
| 31 | -var $wPt, $hPt; // dimensions of current page in points |
|
| 32 | -var $w, $h; // dimensions of current page in user unit |
|
| 33 | -var $lMargin; // left margin |
|
| 34 | -var $tMargin; // top margin |
|
| 35 | -var $rMargin; // right margin |
|
| 36 | -var $bMargin; // page break margin |
|
| 37 | -var $cMargin; // cell margin |
|
| 38 | -var $x, $y; // current position in user unit |
|
| 39 | -var $lasth; // height of last printed cell |
|
| 40 | -var $LineWidth; // line width in user unit |
|
| 41 | -var $fontpath; // path containing fonts |
|
| 42 | -var $CoreFonts; // array of core font names |
|
| 43 | -var $fonts; // array of used fonts |
|
| 44 | -var $FontFiles; // array of font files |
|
| 45 | -var $diffs; // array of encoding differences |
|
| 46 | -var $FontFamily; // current font family |
|
| 47 | -var $FontStyle; // current font style |
|
| 48 | -var $underline; // underlining flag |
|
| 49 | -var $CurrentFont; // current font info |
|
| 50 | -var $FontSizePt; // current font size in points |
|
| 51 | -var $FontSize; // current font size in user unit |
|
| 52 | -var $DrawColor; // commands for drawing color |
|
| 53 | -var $FillColor; // commands for filling color |
|
| 54 | -var $TextColor; // commands for text color |
|
| 55 | -var $ColorFlag; // indicates whether fill and text colors are different |
|
| 56 | -var $ws; // word spacing |
|
| 57 | -var $images; // array of used images |
|
| 58 | -var $PageLinks; // array of links in pages |
|
| 59 | -var $links; // array of internal links |
|
| 60 | -var $AutoPageBreak; // automatic page breaking |
|
| 61 | -var $PageBreakTrigger; // threshold used to trigger page breaks |
|
| 62 | -var $InHeader; // flag set when processing header |
|
| 63 | -var $InFooter; // flag set when processing footer |
|
| 64 | -var $ZoomMode; // zoom display mode |
|
| 65 | -var $LayoutMode; // layout display mode |
|
| 66 | -var $title; // title |
|
| 67 | -var $subject; // subject |
|
| 68 | -var $author; // author |
|
| 69 | -var $keywords; // keywords |
|
| 70 | -var $creator; // creator |
|
| 71 | -var $AliasNbPages; // alias for total number of pages |
|
| 72 | -var $PDFVersion; // PDF version number |
|
| 17 | +var $page; // current page number |
|
| 18 | +var $n; // current object number |
|
| 19 | +var $offsets; // array of object offsets |
|
| 20 | +var $buffer; // buffer holding in-memory PDF |
|
| 21 | +var $pages; // array containing pages |
|
| 22 | +var $state; // current document state |
|
| 23 | +var $compress; // compression flag |
|
| 24 | +var $k; // scale factor (number of points in user unit) |
|
| 25 | +var $DefOrientation; // default orientation |
|
| 26 | +var $CurOrientation; // current orientation |
|
| 27 | +var $StdPageSizes; // standard page sizes |
|
| 28 | +var $DefPageSize; // default page size |
|
| 29 | +var $CurPageSize; // current page size |
|
| 30 | +var $PageSizes; // used for pages with non default sizes or orientations |
|
| 31 | +var $wPt, $hPt; // dimensions of current page in points |
|
| 32 | +var $w, $h; // dimensions of current page in user unit |
|
| 33 | +var $lMargin; // left margin |
|
| 34 | +var $tMargin; // top margin |
|
| 35 | +var $rMargin; // right margin |
|
| 36 | +var $bMargin; // page break margin |
|
| 37 | +var $cMargin; // cell margin |
|
| 38 | +var $x, $y; // current position in user unit |
|
| 39 | +var $lasth; // height of last printed cell |
|
| 40 | +var $LineWidth; // line width in user unit |
|
| 41 | +var $fontpath; // path containing fonts |
|
| 42 | +var $CoreFonts; // array of core font names |
|
| 43 | +var $fonts; // array of used fonts |
|
| 44 | +var $FontFiles; // array of font files |
|
| 45 | +var $diffs; // array of encoding differences |
|
| 46 | +var $FontFamily; // current font family |
|
| 47 | +var $FontStyle; // current font style |
|
| 48 | +var $underline; // underlining flag |
|
| 49 | +var $CurrentFont; // current font info |
|
| 50 | +var $FontSizePt; // current font size in points |
|
| 51 | +var $FontSize; // current font size in user unit |
|
| 52 | +var $DrawColor; // commands for drawing color |
|
| 53 | +var $FillColor; // commands for filling color |
|
| 54 | +var $TextColor; // commands for text color |
|
| 55 | +var $ColorFlag; // indicates whether fill and text colors are different |
|
| 56 | +var $ws; // word spacing |
|
| 57 | +var $images; // array of used images |
|
| 58 | +var $PageLinks; // array of links in pages |
|
| 59 | +var $links; // array of internal links |
|
| 60 | +var $AutoPageBreak; // automatic page breaking |
|
| 61 | +var $PageBreakTrigger; // threshold used to trigger page breaks |
|
| 62 | +var $InHeader; // flag set when processing header |
|
| 63 | +var $InFooter; // flag set when processing footer |
|
| 64 | +var $ZoomMode; // zoom display mode |
|
| 65 | +var $LayoutMode; // layout display mode |
|
| 66 | +var $title; // title |
|
| 67 | +var $subject; // subject |
|
| 68 | +var $author; // author |
|
| 69 | +var $keywords; // keywords |
|
| 70 | +var $creator; // creator |
|
| 71 | +var $AliasNbPages; // alias for total number of pages |
|
| 72 | +var $PDFVersion; // PDF version number |
|
| 73 | 73 | |
| 74 | 74 | /******************************************************************************* |
| 75 | 75 | * * |
| 76 | 76 | * Public methods * |
| 77 | 77 | * * |
| 78 | 78 | *******************************************************************************/ |
| 79 | -function __construct($orientation='P', $unit='mm', $size='A4', $footer_text_1 = "Page") |
|
| 79 | +function __construct($orientation = 'P', $unit = 'mm', $size = 'A4', $footer_text_1 = "Page") |
|
| 80 | 80 | { |
| 81 | 81 | // Some checks |
| 82 | 82 | $this->_dochecks(); |
@@ -105,44 +105,44 @@ discard block |
||
| 105 | 105 | $this->ColorFlag = false; |
| 106 | 106 | $this->ws = 0; |
| 107 | 107 | // Font path |
| 108 | - if(defined('FPDF_FONTPATH')) |
|
| 108 | + if (defined('FPDF_FONTPATH')) |
|
| 109 | 109 | { |
| 110 | 110 | $this->fontpath = FPDF_FONTPATH; |
| 111 | - if(substr($this->fontpath,-1)!='/' && substr($this->fontpath,-1)!='\\') |
|
| 111 | + if (substr($this->fontpath, -1) != '/' && substr($this->fontpath, -1) != '\\') |
|
| 112 | 112 | $this->fontpath .= '/'; |
| 113 | 113 | } |
| 114 | - elseif(is_dir(dirname(__FILE__).'/font')) |
|
| 114 | + elseif (is_dir(dirname(__FILE__).'/font')) |
|
| 115 | 115 | $this->fontpath = dirname(__FILE__).'/font/'; |
| 116 | 116 | else |
| 117 | 117 | $this->fontpath = ''; |
| 118 | 118 | // Core fonts |
| 119 | 119 | $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats'); |
| 120 | 120 | // Scale factor |
| 121 | - if($unit=='pt') |
|
| 121 | + if ($unit == 'pt') |
|
| 122 | 122 | $this->k = 1; |
| 123 | - elseif($unit=='mm') |
|
| 124 | - $this->k = 72/25.4; |
|
| 125 | - elseif($unit=='cm') |
|
| 126 | - $this->k = 72/2.54; |
|
| 127 | - elseif($unit=='in') |
|
| 123 | + elseif ($unit == 'mm') |
|
| 124 | + $this->k = 72 / 25.4; |
|
| 125 | + elseif ($unit == 'cm') |
|
| 126 | + $this->k = 72 / 2.54; |
|
| 127 | + elseif ($unit == 'in') |
|
| 128 | 128 | $this->k = 72; |
| 129 | 129 | else |
| 130 | 130 | $this->Error('Incorrect unit: '.$unit); |
| 131 | 131 | // Page sizes |
| 132 | - $this->StdPageSizes = array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28), |
|
| 133 | - 'letter'=>array(612,792), 'legal'=>array(612,1008)); |
|
| 132 | + $this->StdPageSizes = array('a3'=>array(841.89, 1190.55), 'a4'=>array(595.28, 841.89), 'a5'=>array(420.94, 595.28), |
|
| 133 | + 'letter'=>array(612, 792), 'legal'=>array(612, 1008)); |
|
| 134 | 134 | $size = $this->_getpagesize($size); |
| 135 | 135 | $this->DefPageSize = $size; |
| 136 | 136 | $this->CurPageSize = $size; |
| 137 | 137 | // Page orientation |
| 138 | 138 | $orientation = strtolower($orientation); |
| 139 | - if($orientation=='p' || $orientation=='portrait') |
|
| 139 | + if ($orientation == 'p' || $orientation == 'portrait') |
|
| 140 | 140 | { |
| 141 | 141 | $this->DefOrientation = 'P'; |
| 142 | 142 | $this->w = $size[0]; |
| 143 | 143 | $this->h = $size[1]; |
| 144 | 144 | } |
| 145 | - elseif($orientation=='l' || $orientation=='landscape') |
|
| 145 | + elseif ($orientation == 'l' || $orientation == 'landscape') |
|
| 146 | 146 | { |
| 147 | 147 | $this->DefOrientation = 'L'; |
| 148 | 148 | $this->w = $size[1]; |
@@ -151,17 +151,17 @@ discard block |
||
| 151 | 151 | else |
| 152 | 152 | $this->Error('Incorrect orientation: '.$orientation); |
| 153 | 153 | $this->CurOrientation = $this->DefOrientation; |
| 154 | - $this->wPt = $this->w*$this->k; |
|
| 155 | - $this->hPt = $this->h*$this->k; |
|
| 154 | + $this->wPt = $this->w * $this->k; |
|
| 155 | + $this->hPt = $this->h * $this->k; |
|
| 156 | 156 | // Page margins (1 cm) |
| 157 | - $margin = 28.35/$this->k; |
|
| 158 | - $this->SetMargins($margin,$margin); |
|
| 157 | + $margin = 28.35 / $this->k; |
|
| 158 | + $this->SetMargins($margin, $margin); |
|
| 159 | 159 | // Interior cell margin (1 mm) |
| 160 | - $this->cMargin = $margin/10; |
|
| 160 | + $this->cMargin = $margin / 10; |
|
| 161 | 161 | // Line width (0.2 mm) |
| 162 | - $this->LineWidth = .567/$this->k; |
|
| 162 | + $this->LineWidth = .567 / $this->k; |
|
| 163 | 163 | // Automatic page break |
| 164 | - $this->SetAutoPageBreak(true,2*$margin); |
|
| 164 | + $this->SetAutoPageBreak(true, 2 * $margin); |
|
| 165 | 165 | // Default display mode |
| 166 | 166 | $this->SetDisplayMode('default'); |
| 167 | 167 | // Enable compression |
@@ -170,12 +170,12 @@ discard block |
||
| 170 | 170 | $this->PDFVersion = '1.3'; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | -function SetMargins($left, $top, $right=null) |
|
| 173 | +function SetMargins($left, $top, $right = null) |
|
| 174 | 174 | { |
| 175 | 175 | // Set left, top and right margins |
| 176 | 176 | $this->lMargin = $left; |
| 177 | 177 | $this->tMargin = $top; |
| 178 | - if($right===null) |
|
| 178 | + if ($right === null) |
|
| 179 | 179 | $right = $left; |
| 180 | 180 | $this->rMargin = $right; |
| 181 | 181 | } |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | { |
| 185 | 185 | // Set left margin |
| 186 | 186 | $this->lMargin = $margin; |
| 187 | - if($this->page>0 && $this->x<$margin) |
|
| 187 | + if ($this->page > 0 && $this->x < $margin) |
|
| 188 | 188 | $this->x = $margin; |
| 189 | 189 | } |
| 190 | 190 | |
@@ -200,22 +200,22 @@ discard block |
||
| 200 | 200 | $this->rMargin = $margin; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | -function SetAutoPageBreak($auto, $margin=0) |
|
| 203 | +function SetAutoPageBreak($auto, $margin = 0) |
|
| 204 | 204 | { |
| 205 | 205 | // Set auto page break mode and triggering margin |
| 206 | 206 | $this->AutoPageBreak = $auto; |
| 207 | 207 | $this->bMargin = $margin; |
| 208 | - $this->PageBreakTrigger = $this->h-$margin; |
|
| 208 | + $this->PageBreakTrigger = $this->h - $margin; |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | -function SetDisplayMode($zoom, $layout='default') |
|
| 211 | +function SetDisplayMode($zoom, $layout = 'default') |
|
| 212 | 212 | { |
| 213 | 213 | // Set display mode in viewer |
| 214 | - if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) |
|
| 214 | + if ($zoom == 'fullpage' || $zoom == 'fullwidth' || $zoom == 'real' || $zoom == 'default' || !is_string($zoom)) |
|
| 215 | 215 | $this->ZoomMode = $zoom; |
| 216 | 216 | else |
| 217 | 217 | $this->Error('Incorrect zoom display mode: '.$zoom); |
| 218 | - if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') |
|
| 218 | + if ($layout == 'single' || $layout == 'continuous' || $layout == 'two' || $layout == 'default') |
|
| 219 | 219 | $this->LayoutMode = $layout; |
| 220 | 220 | else |
| 221 | 221 | $this->Error('Incorrect layout display mode: '.$layout); |
@@ -224,53 +224,53 @@ discard block |
||
| 224 | 224 | function SetCompression($compress) |
| 225 | 225 | { |
| 226 | 226 | // Set page compression |
| 227 | - if(function_exists('gzcompress')) |
|
| 227 | + if (function_exists('gzcompress')) |
|
| 228 | 228 | $this->compress = $compress; |
| 229 | 229 | else |
| 230 | 230 | $this->compress = false; |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | -function SetTitle($title, $isUTF8=false) |
|
| 233 | +function SetTitle($title, $isUTF8 = false) |
|
| 234 | 234 | { |
| 235 | 235 | // Title of document |
| 236 | - if($isUTF8) |
|
| 236 | + if ($isUTF8) |
|
| 237 | 237 | $title = $this->_UTF8toUTF16($title); |
| 238 | 238 | $this->title = $title; |
| 239 | 239 | } |
| 240 | 240 | |
| 241 | -function SetSubject($subject, $isUTF8=false) |
|
| 241 | +function SetSubject($subject, $isUTF8 = false) |
|
| 242 | 242 | { |
| 243 | 243 | // Subject of document |
| 244 | - if($isUTF8) |
|
| 244 | + if ($isUTF8) |
|
| 245 | 245 | $subject = $this->_UTF8toUTF16($subject); |
| 246 | 246 | $this->subject = $subject; |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | -function SetAuthor($author, $isUTF8=false) |
|
| 249 | +function SetAuthor($author, $isUTF8 = false) |
|
| 250 | 250 | { |
| 251 | 251 | // Author of document |
| 252 | - if($isUTF8) |
|
| 252 | + if ($isUTF8) |
|
| 253 | 253 | $author = $this->_UTF8toUTF16($author); |
| 254 | 254 | $this->author = $author; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | -function SetKeywords($keywords, $isUTF8=false) |
|
| 257 | +function SetKeywords($keywords, $isUTF8 = false) |
|
| 258 | 258 | { |
| 259 | 259 | // Keywords of document |
| 260 | - if($isUTF8) |
|
| 260 | + if ($isUTF8) |
|
| 261 | 261 | $keywords = $this->_UTF8toUTF16($keywords); |
| 262 | 262 | $this->keywords = $keywords; |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | -function SetCreator($creator, $isUTF8=false) |
|
| 265 | +function SetCreator($creator, $isUTF8 = false) |
|
| 266 | 266 | { |
| 267 | 267 | // Creator of document |
| 268 | - if($isUTF8) |
|
| 268 | + if ($isUTF8) |
|
| 269 | 269 | $creator = $this->_UTF8toUTF16($creator); |
| 270 | 270 | $this->creator = $creator; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | -function AliasNbPages($alias='{nb}') |
|
| 273 | +function AliasNbPages($alias = '{nb}') |
|
| 274 | 274 | { |
| 275 | 275 | // Define an alias for total number of pages |
| 276 | 276 | $this->AliasNbPages = $alias; |
@@ -291,9 +291,9 @@ discard block |
||
| 291 | 291 | function Close() |
| 292 | 292 | { |
| 293 | 293 | // Terminate document |
| 294 | - if($this->state==3) |
|
| 294 | + if ($this->state == 3) |
|
| 295 | 295 | return; |
| 296 | - if($this->page==0) |
|
| 296 | + if ($this->page == 0) |
|
| 297 | 297 | $this->AddPage(); |
| 298 | 298 | // Page footer |
| 299 | 299 | $this->InFooter = true; |
@@ -305,10 +305,10 @@ discard block |
||
| 305 | 305 | $this->_enddoc(); |
| 306 | 306 | } |
| 307 | 307 | |
| 308 | -function AddPage($orientation='', $size='') |
|
| 308 | +function AddPage($orientation = '', $size = '') |
|
| 309 | 309 | { |
| 310 | 310 | // Start a new page |
| 311 | - if($this->state==0) |
|
| 311 | + if ($this->state == 0) |
|
| 312 | 312 | $this->Open(); |
| 313 | 313 | $family = $this->FontFamily; |
| 314 | 314 | $style = $this->FontStyle.($this->underline ? 'U' : ''); |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | $fc = $this->FillColor; |
| 319 | 319 | $tc = $this->TextColor; |
| 320 | 320 | $cf = $this->ColorFlag; |
| 321 | - if($this->page>0) |
|
| 321 | + if ($this->page > 0) |
|
| 322 | 322 | { |
| 323 | 323 | // Page footer |
| 324 | 324 | $this->InFooter = true; |
@@ -328,21 +328,21 @@ discard block |
||
| 328 | 328 | $this->_endpage(); |
| 329 | 329 | } |
| 330 | 330 | // Start new page |
| 331 | - $this->_beginpage($orientation,$size); |
|
| 331 | + $this->_beginpage($orientation, $size); |
|
| 332 | 332 | // Set line cap style to square |
| 333 | 333 | $this->_out('2 J'); |
| 334 | 334 | // Set line width |
| 335 | 335 | $this->LineWidth = $lw; |
| 336 | - $this->_out(sprintf('%.2F w',$lw*$this->k)); |
|
| 336 | + $this->_out(sprintf('%.2F w', $lw * $this->k)); |
|
| 337 | 337 | // Set font |
| 338 | - if($family) |
|
| 339 | - $this->SetFont($family,$style,$fontsize); |
|
| 338 | + if ($family) |
|
| 339 | + $this->SetFont($family, $style, $fontsize); |
|
| 340 | 340 | // Set colors |
| 341 | 341 | $this->DrawColor = $dc; |
| 342 | - if($dc!='0 G') |
|
| 342 | + if ($dc != '0 G') |
|
| 343 | 343 | $this->_out($dc); |
| 344 | 344 | $this->FillColor = $fc; |
| 345 | - if($fc!='0 g') |
|
| 345 | + if ($fc != '0 g') |
|
| 346 | 346 | $this->_out($fc); |
| 347 | 347 | $this->TextColor = $tc; |
| 348 | 348 | $this->ColorFlag = $cf; |
@@ -351,21 +351,21 @@ discard block |
||
| 351 | 351 | $this->Header(); |
| 352 | 352 | $this->InHeader = false; |
| 353 | 353 | // Restore line width |
| 354 | - if($this->LineWidth!=$lw) |
|
| 354 | + if ($this->LineWidth != $lw) |
|
| 355 | 355 | { |
| 356 | 356 | $this->LineWidth = $lw; |
| 357 | - $this->_out(sprintf('%.2F w',$lw*$this->k)); |
|
| 357 | + $this->_out(sprintf('%.2F w', $lw * $this->k)); |
|
| 358 | 358 | } |
| 359 | 359 | // Restore font |
| 360 | - if($family) |
|
| 361 | - $this->SetFont($family,$style,$fontsize); |
|
| 360 | + if ($family) |
|
| 361 | + $this->SetFont($family, $style, $fontsize); |
|
| 362 | 362 | // Restore colors |
| 363 | - if($this->DrawColor!=$dc) |
|
| 363 | + if ($this->DrawColor != $dc) |
|
| 364 | 364 | { |
| 365 | 365 | $this->DrawColor = $dc; |
| 366 | 366 | $this->_out($dc); |
| 367 | 367 | } |
| 368 | - if($this->FillColor!=$fc) |
|
| 368 | + if ($this->FillColor != $fc) |
|
| 369 | 369 | { |
| 370 | 370 | $this->FillColor = $fc; |
| 371 | 371 | $this->_out($fc); |
@@ -390,127 +390,127 @@ discard block |
||
| 390 | 390 | return $this->page; |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | -function SetDrawColor($r, $g=null, $b=null) |
|
| 393 | +function SetDrawColor($r, $g = null, $b = null) |
|
| 394 | 394 | { |
| 395 | 395 | // Set color for all stroking operations |
| 396 | - if(($r==0 && $g==0 && $b==0) || $g===null) |
|
| 397 | - $this->DrawColor = sprintf('%.3F G',$r/255); |
|
| 396 | + if (($r == 0 && $g == 0 && $b == 0) || $g === null) |
|
| 397 | + $this->DrawColor = sprintf('%.3F G', $r / 255); |
|
| 398 | 398 | else |
| 399 | - $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255); |
|
| 400 | - if($this->page>0) |
|
| 399 | + $this->DrawColor = sprintf('%.3F %.3F %.3F RG', $r / 255, $g / 255, $b / 255); |
|
| 400 | + if ($this->page > 0) |
|
| 401 | 401 | $this->_out($this->DrawColor); |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | -function SetFillColor($r, $g=null, $b=null) |
|
| 404 | +function SetFillColor($r, $g = null, $b = null) |
|
| 405 | 405 | { |
| 406 | 406 | // Set color for all filling operations |
| 407 | - if(($r==0 && $g==0 && $b==0) || $g===null) |
|
| 408 | - $this->FillColor = sprintf('%.3F g',$r/255); |
|
| 407 | + if (($r == 0 && $g == 0 && $b == 0) || $g === null) |
|
| 408 | + $this->FillColor = sprintf('%.3F g', $r / 255); |
|
| 409 | 409 | else |
| 410 | - $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); |
|
| 411 | - $this->ColorFlag = ($this->FillColor!=$this->TextColor); |
|
| 412 | - if($this->page>0) |
|
| 410 | + $this->FillColor = sprintf('%.3F %.3F %.3F rg', $r / 255, $g / 255, $b / 255); |
|
| 411 | + $this->ColorFlag = ($this->FillColor != $this->TextColor); |
|
| 412 | + if ($this->page > 0) |
|
| 413 | 413 | $this->_out($this->FillColor); |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | -function SetTextColor($r, $g=null, $b=null) |
|
| 416 | +function SetTextColor($r, $g = null, $b = null) |
|
| 417 | 417 | { |
| 418 | 418 | // Set color for text |
| 419 | - if(($r==0 && $g==0 && $b==0) || $g===null) |
|
| 420 | - $this->TextColor = sprintf('%.3F g',$r/255); |
|
| 419 | + if (($r == 0 && $g == 0 && $b == 0) || $g === null) |
|
| 420 | + $this->TextColor = sprintf('%.3F g', $r / 255); |
|
| 421 | 421 | else |
| 422 | - $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); |
|
| 423 | - $this->ColorFlag = ($this->FillColor!=$this->TextColor); |
|
| 422 | + $this->TextColor = sprintf('%.3F %.3F %.3F rg', $r / 255, $g / 255, $b / 255); |
|
| 423 | + $this->ColorFlag = ($this->FillColor != $this->TextColor); |
|
| 424 | 424 | } |
| 425 | 425 | |
| 426 | 426 | function GetStringWidth($s) |
| 427 | 427 | { |
| 428 | 428 | // Get width of a string in the current font |
| 429 | - $s = (string)$s; |
|
| 429 | + $s = (string) $s; |
|
| 430 | 430 | $cw = &$this->CurrentFont['cw']; |
| 431 | - $w=0; |
|
| 431 | + $w = 0; |
|
| 432 | 432 | if ($this->unifontSubset) { |
| 433 | 433 | $unicode = $this->UTF8StringToArray($s); |
| 434 | - foreach($unicode as $char) { |
|
| 435 | - if (isset($cw[$char])) { $w += (ord($cw[2*$char])<<8) + ord($cw[2*$char+1]); } |
|
| 436 | - else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w += $cw[chr($char)]; } |
|
| 437 | - else if(isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; } |
|
| 438 | - else if(isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; } |
|
| 434 | + foreach ($unicode as $char) { |
|
| 435 | + if (isset($cw[$char])) { $w += (ord($cw[2 * $char]) << 8) + ord($cw[2 * $char + 1]); } |
|
| 436 | + else if ($char > 0 && $char < 128 && isset($cw[chr($char)])) { $w += $cw[chr($char)]; } |
|
| 437 | + else if (isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; } |
|
| 438 | + else if (isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; } |
|
| 439 | 439 | else { $w += 500; } |
| 440 | 440 | } |
| 441 | 441 | } |
| 442 | 442 | else { |
| 443 | 443 | $l = strlen($s); |
| 444 | - for($i=0;$i<$l;$i++) |
|
| 444 | + for ($i = 0; $i < $l; $i++) |
|
| 445 | 445 | $w += $cw[$s[$i]]; |
| 446 | 446 | } |
| 447 | - return $w*$this->FontSize/1000; |
|
| 447 | + return $w * $this->FontSize / 1000; |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | function SetLineWidth($width) |
| 451 | 451 | { |
| 452 | 452 | // Set line width |
| 453 | 453 | $this->LineWidth = $width; |
| 454 | - if($this->page>0) |
|
| 455 | - $this->_out(sprintf('%.2F w',$width*$this->k)); |
|
| 454 | + if ($this->page > 0) |
|
| 455 | + $this->_out(sprintf('%.2F w', $width * $this->k)); |
|
| 456 | 456 | } |
| 457 | 457 | |
| 458 | 458 | function Line($x1, $y1, $x2, $y2) |
| 459 | 459 | { |
| 460 | 460 | // Draw a line |
| 461 | - $this->_out(sprintf('%.2F %.2F m %.2F %.2F l S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k)); |
|
| 461 | + $this->_out(sprintf('%.2F %.2F m %.2F %.2F l S', $x1 * $this->k, ($this->h - $y1) * $this->k, $x2 * $this->k, ($this->h - $y2) * $this->k)); |
|
| 462 | 462 | } |
| 463 | 463 | |
| 464 | -function Rect($x, $y, $w, $h, $style='') |
|
| 464 | +function Rect($x, $y, $w, $h, $style = '') |
|
| 465 | 465 | { |
| 466 | 466 | // Draw a rectangle |
| 467 | - if($style=='F') |
|
| 467 | + if ($style == 'F') |
|
| 468 | 468 | $op = 'f'; |
| 469 | - elseif($style=='FD' || $style=='DF') |
|
| 469 | + elseif ($style == 'FD' || $style == 'DF') |
|
| 470 | 470 | $op = 'B'; |
| 471 | 471 | else |
| 472 | 472 | $op = 'S'; |
| 473 | - $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op)); |
|
| 473 | + $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op)); |
|
| 474 | 474 | } |
| 475 | 475 | |
| 476 | -function AddFont($family, $style='', $file='', $uni=false) |
|
| 476 | +function AddFont($family, $style = '', $file = '', $uni = false) |
|
| 477 | 477 | { |
| 478 | 478 | // Add a TrueType, OpenType or Type1 font |
| 479 | 479 | $family = strtolower($family); |
| 480 | 480 | $style = strtoupper($style); |
| 481 | - if($style=='IB') |
|
| 482 | - $style='BI'; |
|
| 483 | - if($file=='') { |
|
| 481 | + if ($style == 'IB') |
|
| 482 | + $style = 'BI'; |
|
| 483 | + if ($file == '') { |
|
| 484 | 484 | if ($uni) { |
| 485 | - $file = str_replace(' ','',$family).strtolower($style).'.ttf'; |
|
| 485 | + $file = str_replace(' ', '', $family).strtolower($style).'.ttf'; |
|
| 486 | 486 | } |
| 487 | 487 | else { |
| 488 | - $file = str_replace(' ','',$family).strtolower($style).'.php'; |
|
| 488 | + $file = str_replace(' ', '', $family).strtolower($style).'.php'; |
|
| 489 | 489 | } |
| 490 | 490 | } |
| 491 | 491 | $fontkey = $family.$style; |
| 492 | - if(isset($this->fonts[$fontkey])) |
|
| 492 | + if (isset($this->fonts[$fontkey])) |
|
| 493 | 493 | return; |
| 494 | 494 | |
| 495 | 495 | if ($uni) { |
| 496 | - if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; } |
|
| 497 | - else { $ttffilename = $this->_getfontpath().'unifont/'.$file ; } |
|
| 498 | - $unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($file ,0,(strpos($file ,'.')))); |
|
| 496 | + if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file)) { $ttffilename = _SYSTEM_TTFONTS.$file; } |
|
| 497 | + else { $ttffilename = $this->_getfontpath().'unifont/'.$file; } |
|
| 498 | + $unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($file, 0, (strpos($file, '.')))); |
|
| 499 | 499 | $name = ''; |
| 500 | 500 | $originalsize = 0; |
| 501 | 501 | $ttfstat = stat($ttffilename); |
| 502 | 502 | if (file_exists($unifilename.'.mtx.php')) { |
| 503 | 503 | include($unifilename.'.mtx.php'); |
| 504 | 504 | } |
| 505 | - if (!isset($type) || !isset($name) || $originalsize != $ttfstat['size']) { |
|
| 505 | + if (!isset($type) || !isset($name) || $originalsize != $ttfstat['size']) { |
|
| 506 | 506 | $ttffile = $ttffilename; |
| 507 | 507 | require_once($this->_getfontpath().'unifont/ttfonts.php'); |
| 508 | 508 | $ttf = new TTFontFile(); |
| 509 | 509 | $ttf->getMetrics($ttffile); |
| 510 | 510 | $cw = $ttf->charWidths; |
| 511 | - $name = preg_replace('/[ ()]/','',$ttf->fullName); |
|
| 511 | + $name = preg_replace('/[ ()]/', '', $ttf->fullName); |
|
| 512 | 512 | |
| 513 | - $desc= array('Ascent'=>round($ttf->ascent), |
|
| 513 | + $desc = array('Ascent'=>round($ttf->ascent), |
|
| 514 | 514 | 'Descent'=>round($ttf->descent), |
| 515 | 515 | 'CapHeight'=>round($ttf->capHeight), |
| 516 | 516 | 'Flags'=>$ttf->flags, |
@@ -520,25 +520,25 @@ discard block |
||
| 520 | 520 | 'MissingWidth'=>round($ttf->defaultWidth)); |
| 521 | 521 | $up = round($ttf->underlinePosition); |
| 522 | 522 | $ut = round($ttf->underlineThickness); |
| 523 | - $originalsize = $ttfstat['size']+0; |
|
| 523 | + $originalsize = $ttfstat['size'] + 0; |
|
| 524 | 524 | $type = 'TTF'; |
| 525 | 525 | // Generate metrics .php file |
| 526 | - $s='<?php'."\n"; |
|
| 527 | - $s.='$name=\''.$name."';\n"; |
|
| 528 | - $s.='$type=\''.$type."';\n"; |
|
| 529 | - $s.='$desc='.var_export($desc,true).";\n"; |
|
| 530 | - $s.='$up='.$up.";\n"; |
|
| 531 | - $s.='$ut='.$ut.";\n"; |
|
| 532 | - $s.='$ttffile=\''.$ttffile."';\n"; |
|
| 533 | - $s.='$originalsize='.$originalsize.";\n"; |
|
| 534 | - $s.='$fontkey=\''.$fontkey."';\n"; |
|
| 535 | - $s.="?>"; |
|
| 526 | + $s = '<?php'."\n"; |
|
| 527 | + $s .= '$name=\''.$name."';\n"; |
|
| 528 | + $s .= '$type=\''.$type."';\n"; |
|
| 529 | + $s .= '$desc='.var_export($desc, true).";\n"; |
|
| 530 | + $s .= '$up='.$up.";\n"; |
|
| 531 | + $s .= '$ut='.$ut.";\n"; |
|
| 532 | + $s .= '$ttffile=\''.$ttffile."';\n"; |
|
| 533 | + $s .= '$originalsize='.$originalsize.";\n"; |
|
| 534 | + $s .= '$fontkey=\''.$fontkey."';\n"; |
|
| 535 | + $s .= "?>"; |
|
| 536 | 536 | if (is_writable(dirname($this->_getfontpath().'unifont/'.'x'))) { |
| 537 | - $fh = fopen($unifilename.'.mtx.php',"w"); |
|
| 538 | - fwrite($fh,$s,strlen($s)); |
|
| 537 | + $fh = fopen($unifilename.'.mtx.php', "w"); |
|
| 538 | + fwrite($fh, $s, strlen($s)); |
|
| 539 | 539 | fclose($fh); |
| 540 | - $fh = fopen($unifilename.'.cw.dat',"wb"); |
|
| 541 | - fwrite($fh,$cw,strlen($cw)); |
|
| 540 | + $fh = fopen($unifilename.'.cw.dat', "wb"); |
|
| 541 | + fwrite($fh, $cw, strlen($cw)); |
|
| 542 | 542 | fclose($fh); |
| 543 | 543 | @unlink($unifilename.'.cw127.php'); |
| 544 | 544 | } |
@@ -547,35 +547,35 @@ discard block |
||
| 547 | 547 | else { |
| 548 | 548 | $cw = @file_get_contents($unifilename.'.cw.dat'); |
| 549 | 549 | } |
| 550 | - $i = count($this->fonts)+1; |
|
| 551 | - if(!empty($this->AliasNbPages)) |
|
| 552 | - $sbarr = range(0,57); |
|
| 550 | + $i = count($this->fonts) + 1; |
|
| 551 | + if (!empty($this->AliasNbPages)) |
|
| 552 | + $sbarr = range(0, 57); |
|
| 553 | 553 | else |
| 554 | - $sbarr = range(0,32); |
|
| 554 | + $sbarr = range(0, 32); |
|
| 555 | 555 | $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'ttffile'=>$ttffile, 'fontkey'=>$fontkey, 'subset'=>$sbarr, 'unifilename'=>$unifilename); |
| 556 | 556 | |
| 557 | - $this->FontFiles[$fontkey]=array('length1'=>$originalsize, 'type'=>"TTF", 'ttffile'=>$ttffile); |
|
| 558 | - $this->FontFiles[$file]=array('type'=>"TTF"); |
|
| 557 | + $this->FontFiles[$fontkey] = array('length1'=>$originalsize, 'type'=>"TTF", 'ttffile'=>$ttffile); |
|
| 558 | + $this->FontFiles[$file] = array('type'=>"TTF"); |
|
| 559 | 559 | unset($cw); |
| 560 | 560 | } |
| 561 | 561 | else { |
| 562 | 562 | $info = $this->_loadfont($file); |
| 563 | - $info['i'] = count($this->fonts)+1; |
|
| 564 | - if(!empty($info['diff'])) |
|
| 563 | + $info['i'] = count($this->fonts) + 1; |
|
| 564 | + if (!empty($info['diff'])) |
|
| 565 | 565 | { |
| 566 | 566 | // Search existing encodings |
| 567 | - $n = array_search($info['diff'],$this->diffs); |
|
| 568 | - if(!$n) |
|
| 567 | + $n = array_search($info['diff'], $this->diffs); |
|
| 568 | + if (!$n) |
|
| 569 | 569 | { |
| 570 | - $n = count($this->diffs)+1; |
|
| 570 | + $n = count($this->diffs) + 1; |
|
| 571 | 571 | $this->diffs[$n] = $info['diff']; |
| 572 | 572 | } |
| 573 | 573 | $info['diffn'] = $n; |
| 574 | 574 | } |
| 575 | - if(!empty($info['file'])) |
|
| 575 | + if (!empty($info['file'])) |
|
| 576 | 576 | { |
| 577 | 577 | // Embedded font |
| 578 | - if($info['type']=='TrueType') |
|
| 578 | + if ($info['type'] == 'TrueType') |
|
| 579 | 579 | $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']); |
| 580 | 580 | else |
| 581 | 581 | $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']); |
@@ -584,42 +584,42 @@ discard block |
||
| 584 | 584 | } |
| 585 | 585 | } |
| 586 | 586 | |
| 587 | -function SetFont($family, $style='', $size=0) |
|
| 587 | +function SetFont($family, $style = '', $size = 0) |
|
| 588 | 588 | { |
| 589 | 589 | // Select a font; size given in points |
| 590 | - if($family=='') |
|
| 590 | + if ($family == '') |
|
| 591 | 591 | $family = $this->FontFamily; |
| 592 | 592 | else |
| 593 | 593 | $family = strtolower($family); |
| 594 | 594 | $style = strtoupper($style); |
| 595 | - if(strpos($style,'U')!==false) |
|
| 595 | + if (strpos($style, 'U') !== false) |
|
| 596 | 596 | { |
| 597 | 597 | $this->underline = true; |
| 598 | - $style = str_replace('U','',$style); |
|
| 598 | + $style = str_replace('U', '', $style); |
|
| 599 | 599 | } |
| 600 | 600 | else |
| 601 | 601 | $this->underline = false; |
| 602 | - if($style=='IB') |
|
| 602 | + if ($style == 'IB') |
|
| 603 | 603 | $style = 'BI'; |
| 604 | - if($size==0) |
|
| 604 | + if ($size == 0) |
|
| 605 | 605 | $size = $this->FontSizePt; |
| 606 | 606 | // Test if font is already selected |
| 607 | - if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size) |
|
| 607 | + if ($this->FontFamily == $family && $this->FontStyle == $style && $this->FontSizePt == $size) |
|
| 608 | 608 | return; |
| 609 | 609 | // Test if font is already loaded |
| 610 | 610 | $fontkey = $family.$style; |
| 611 | - if(!isset($this->fonts[$fontkey])) |
|
| 611 | + if (!isset($this->fonts[$fontkey])) |
|
| 612 | 612 | { |
| 613 | 613 | // Test if one of the core fonts |
| 614 | - if($family=='arial') |
|
| 614 | + if ($family == 'arial') |
|
| 615 | 615 | $family = 'helvetica'; |
| 616 | - if(in_array($family,$this->CoreFonts)) |
|
| 616 | + if (in_array($family, $this->CoreFonts)) |
|
| 617 | 617 | { |
| 618 | - if($family=='symbol' || $family=='zapfdingbats') |
|
| 618 | + if ($family == 'symbol' || $family == 'zapfdingbats') |
|
| 619 | 619 | $style = ''; |
| 620 | 620 | $fontkey = $family.$style; |
| 621 | - if(!isset($this->fonts[$fontkey])) |
|
| 622 | - $this->AddFont($family,$style); |
|
| 621 | + if (!isset($this->fonts[$fontkey])) |
|
| 622 | + $this->AddFont($family, $style); |
|
| 623 | 623 | } |
| 624 | 624 | else |
| 625 | 625 | $this->Error('Undefined font: '.$family.' '.$style); |
@@ -628,39 +628,39 @@ discard block |
||
| 628 | 628 | $this->FontFamily = $family; |
| 629 | 629 | $this->FontStyle = $style; |
| 630 | 630 | $this->FontSizePt = $size; |
| 631 | - $this->FontSize = $size/$this->k; |
|
| 631 | + $this->FontSize = $size / $this->k; |
|
| 632 | 632 | $this->CurrentFont = &$this->fonts[$fontkey]; |
| 633 | - if ($this->fonts[$fontkey]['type']=='TTF') { $this->unifontSubset = true; } |
|
| 633 | + if ($this->fonts[$fontkey]['type'] == 'TTF') { $this->unifontSubset = true; } |
|
| 634 | 634 | else { $this->unifontSubset = false; } |
| 635 | - if($this->page>0) |
|
| 636 | - $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); |
|
| 635 | + if ($this->page > 0) |
|
| 636 | + $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt)); |
|
| 637 | 637 | } |
| 638 | 638 | |
| 639 | 639 | function SetFontSize($size) |
| 640 | 640 | { |
| 641 | 641 | // Set font size in points |
| 642 | - if($this->FontSizePt==$size) |
|
| 642 | + if ($this->FontSizePt == $size) |
|
| 643 | 643 | return; |
| 644 | 644 | $this->FontSizePt = $size; |
| 645 | - $this->FontSize = $size/$this->k; |
|
| 646 | - if($this->page>0) |
|
| 647 | - $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); |
|
| 645 | + $this->FontSize = $size / $this->k; |
|
| 646 | + if ($this->page > 0) |
|
| 647 | + $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt)); |
|
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | function AddLink() |
| 651 | 651 | { |
| 652 | 652 | // Create a new internal link |
| 653 | - $n = count($this->links)+1; |
|
| 653 | + $n = count($this->links) + 1; |
|
| 654 | 654 | $this->links[$n] = array(0, 0); |
| 655 | 655 | return $n; |
| 656 | 656 | } |
| 657 | 657 | |
| 658 | -function SetLink($link, $y=0, $page=-1) |
|
| 658 | +function SetLink($link, $y = 0, $page = -1) |
|
| 659 | 659 | { |
| 660 | 660 | // Set destination of internal link |
| 661 | - if($y==-1) |
|
| 661 | + if ($y == -1) |
|
| 662 | 662 | $y = $this->y; |
| 663 | - if($page==-1) |
|
| 663 | + if ($page == -1) |
|
| 664 | 664 | $page = $this->page; |
| 665 | 665 | $this->links[$link] = array($page, $y); |
| 666 | 666 | } |
@@ -668,7 +668,7 @@ discard block |
||
| 668 | 668 | function Link($x, $y, $w, $h, $link) |
| 669 | 669 | { |
| 670 | 670 | // Put a link on the page |
| 671 | - $this->PageLinks[$this->page][] = array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link); |
|
| 671 | + $this->PageLinks[$this->page][] = array($x * $this->k, $this->hPt - $y * $this->k, $w * $this->k, $h * $this->k, $link); |
|
| 672 | 672 | } |
| 673 | 673 | |
| 674 | 674 | function Text($x, $y, $txt) |
@@ -677,15 +677,15 @@ discard block |
||
| 677 | 677 | if ($this->unifontSubset) |
| 678 | 678 | { |
| 679 | 679 | $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')'; |
| 680 | - foreach($this->UTF8StringToArray($txt) as $uni) |
|
| 680 | + foreach ($this->UTF8StringToArray($txt) as $uni) |
|
| 681 | 681 | $this->CurrentFont['subset'][$uni] = $uni; |
| 682 | 682 | } |
| 683 | 683 | else |
| 684 | 684 | $txt2 = '('.$this->_escape($txt).')'; |
| 685 | - $s = sprintf('BT %.2F %.2F Td %s Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt2); |
|
| 686 | - if($this->underline && $txt!='') |
|
| 687 | - $s .= ' '.$this->_dounderline($x,$y,$txt); |
|
| 688 | - if($this->ColorFlag) |
|
| 685 | + $s = sprintf('BT %.2F %.2F Td %s Tj ET', $x * $this->k, ($this->h - $y) * $this->k, $txt2); |
|
| 686 | + if ($this->underline && $txt != '') |
|
| 687 | + $s .= ' '.$this->_dounderline($x, $y, $txt); |
|
| 688 | + if ($this->ColorFlag) |
|
| 689 | 689 | $s = 'q '.$this->TextColor.' '.$s.' Q'; |
| 690 | 690 | $this->_out($s); |
| 691 | 691 | } |
@@ -696,78 +696,78 @@ discard block |
||
| 696 | 696 | return $this->AutoPageBreak; |
| 697 | 697 | } |
| 698 | 698 | |
| 699 | -function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='') |
|
| 699 | +function Cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = false, $link = '') |
|
| 700 | 700 | { |
| 701 | 701 | // Output a cell |
| 702 | 702 | $k = $this->k; |
| 703 | - if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) |
|
| 703 | + if ($this->y + $h > $this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) |
|
| 704 | 704 | { |
| 705 | 705 | // Automatic page break |
| 706 | 706 | $x = $this->x; |
| 707 | 707 | $ws = $this->ws; |
| 708 | - if($ws>0) |
|
| 708 | + if ($ws > 0) |
|
| 709 | 709 | { |
| 710 | 710 | $this->ws = 0; |
| 711 | 711 | $this->_out('0 Tw'); |
| 712 | 712 | } |
| 713 | - $this->AddPage($this->CurOrientation,$this->CurPageSize); |
|
| 713 | + $this->AddPage($this->CurOrientation, $this->CurPageSize); |
|
| 714 | 714 | $this->x = $x; |
| 715 | - if($ws>0) |
|
| 715 | + if ($ws > 0) |
|
| 716 | 716 | { |
| 717 | 717 | $this->ws = $ws; |
| 718 | - $this->_out(sprintf('%.3F Tw',$ws*$k)); |
|
| 718 | + $this->_out(sprintf('%.3F Tw', $ws * $k)); |
|
| 719 | 719 | } |
| 720 | 720 | } |
| 721 | - if($w==0) |
|
| 722 | - $w = $this->w-$this->rMargin-$this->x; |
|
| 721 | + if ($w == 0) |
|
| 722 | + $w = $this->w - $this->rMargin - $this->x; |
|
| 723 | 723 | $s = ''; |
| 724 | - if($fill || $border==1) |
|
| 724 | + if ($fill || $border == 1) |
|
| 725 | 725 | { |
| 726 | - if($fill) |
|
| 727 | - $op = ($border==1) ? 'B' : 'f'; |
|
| 726 | + if ($fill) |
|
| 727 | + $op = ($border == 1) ? 'B' : 'f'; |
|
| 728 | 728 | else |
| 729 | 729 | $op = 'S'; |
| 730 | - $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op); |
|
| 730 | + $s = sprintf('%.2F %.2F %.2F %.2F re %s ', $this->x * $k, ($this->h - $this->y) * $k, $w * $k, -$h * $k, $op); |
|
| 731 | 731 | } |
| 732 | - if(is_string($border)) |
|
| 732 | + if (is_string($border)) |
|
| 733 | 733 | { |
| 734 | 734 | $x = $this->x; |
| 735 | 735 | $y = $this->y; |
| 736 | - if(strpos($border,'L')!==false) |
|
| 737 | - $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k); |
|
| 738 | - if(strpos($border,'T')!==false) |
|
| 739 | - $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k); |
|
| 740 | - if(strpos($border,'R')!==false) |
|
| 741 | - $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k); |
|
| 742 | - if(strpos($border,'B')!==false) |
|
| 743 | - $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k); |
|
| 744 | - } |
|
| 745 | - if($txt!=='') |
|
| 736 | + if (strpos($border, 'L') !== false) |
|
| 737 | + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - $y) * $k, $x * $k, ($this->h - ($y + $h)) * $k); |
|
| 738 | + if (strpos($border, 'T') !== false) |
|
| 739 | + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - $y) * $k); |
|
| 740 | + if (strpos($border, 'R') !== false) |
|
| 741 | + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', ($x + $w) * $k, ($this->h - $y) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k); |
|
| 742 | + if (strpos($border, 'B') !== false) |
|
| 743 | + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $x * $k, ($this->h - ($y + $h)) * $k, ($x + $w) * $k, ($this->h - ($y + $h)) * $k); |
|
| 744 | + } |
|
| 745 | + if ($txt !== '') |
|
| 746 | 746 | { |
| 747 | - if($align=='R') |
|
| 748 | - $dx = $w-$this->cMargin-$this->GetStringWidth($txt); |
|
| 749 | - elseif($align=='C') |
|
| 750 | - $dx = ($w-$this->GetStringWidth($txt))/2; |
|
| 747 | + if ($align == 'R') |
|
| 748 | + $dx = $w - $this->cMargin - $this->GetStringWidth($txt); |
|
| 749 | + elseif ($align == 'C') |
|
| 750 | + $dx = ($w - $this->GetStringWidth($txt)) / 2; |
|
| 751 | 751 | else |
| 752 | 752 | $dx = $this->cMargin; |
| 753 | - if($this->ColorFlag) |
|
| 753 | + if ($this->ColorFlag) |
|
| 754 | 754 | $s .= 'q '.$this->TextColor.' '; |
| 755 | 755 | |
| 756 | 756 | // If multibyte, Tw has no effect - do word spacing using an adjustment before each space |
| 757 | 757 | if ($this->ws && $this->unifontSubset) { |
| 758 | - foreach($this->UTF8StringToArray($txt) as $uni) |
|
| 758 | + foreach ($this->UTF8StringToArray($txt) as $uni) |
|
| 759 | 759 | $this->CurrentFont['subset'][$uni] = $uni; |
| 760 | 760 | $space = $this->_escape($this->UTF8ToUTF16BE(' ', false)); |
| 761 | - $s .= sprintf('BT 0 Tw %.2F %.2F Td [',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k); |
|
| 762 | - $t = explode(' ',$txt); |
|
| 761 | + $s .= sprintf('BT 0 Tw %.2F %.2F Td [', ($this->x + $dx) * $k, ($this->h - ($this->y + .5 * $h + .3 * $this->FontSize)) * $k); |
|
| 762 | + $t = explode(' ', $txt); |
|
| 763 | 763 | $numt = count($t); |
| 764 | - for($i=0;$i<$numt;$i++) { |
|
| 764 | + for ($i = 0; $i < $numt; $i++) { |
|
| 765 | 765 | $tx = $t[$i]; |
| 766 | 766 | $tx = '('.$this->_escape($this->UTF8ToUTF16BE($tx, false)).')'; |
| 767 | - $s .= sprintf('%s ',$tx); |
|
| 768 | - if (($i+1)<$numt) { |
|
| 769 | - $adj = -($this->ws*$this->k)*1000/$this->FontSizePt; |
|
| 770 | - $s .= sprintf('%d(%s) ',$adj,$space); |
|
| 767 | + $s .= sprintf('%s ', $tx); |
|
| 768 | + if (($i + 1) < $numt) { |
|
| 769 | + $adj = -($this->ws * $this->k) * 1000 / $this->FontSizePt; |
|
| 770 | + $s .= sprintf('%d(%s) ', $adj, $space); |
|
| 771 | 771 | } |
| 772 | 772 | } |
| 773 | 773 | $s .= '] TJ'; |
@@ -777,55 +777,55 @@ discard block |
||
| 777 | 777 | if ($this->unifontSubset) |
| 778 | 778 | { |
| 779 | 779 | $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')'; |
| 780 | - foreach($this->UTF8StringToArray($txt) as $uni) |
|
| 780 | + foreach ($this->UTF8StringToArray($txt) as $uni) |
|
| 781 | 781 | $this->CurrentFont['subset'][$uni] = $uni; |
| 782 | 782 | } |
| 783 | 783 | else |
| 784 | - $txt2='('.str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))).')'; |
|
| 785 | - $s .= sprintf('BT %.2F %.2F Td %s Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2); |
|
| 784 | + $txt2 = '('.str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $txt))).')'; |
|
| 785 | + $s .= sprintf('BT %.2F %.2F Td %s Tj ET', ($this->x + $dx) * $k, ($this->h - ($this->y + .5 * $h + .3 * $this->FontSize)) * $k, $txt2); |
|
| 786 | 786 | } |
| 787 | - if($this->underline) |
|
| 788 | - $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt); |
|
| 789 | - if($this->ColorFlag) |
|
| 787 | + if ($this->underline) |
|
| 788 | + $s .= ' '.$this->_dounderline($this->x + $dx, $this->y + .5 * $h + .3 * $this->FontSize, $txt); |
|
| 789 | + if ($this->ColorFlag) |
|
| 790 | 790 | $s .= ' Q'; |
| 791 | - if($link) |
|
| 792 | - $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link); |
|
| 791 | + if ($link) |
|
| 792 | + $this->Link($this->x + $dx, $this->y + .5 * $h - .5 * $this->FontSize, $this->GetStringWidth($txt), $this->FontSize, $link); |
|
| 793 | 793 | } |
| 794 | - if($s) |
|
| 794 | + if ($s) |
|
| 795 | 795 | $this->_out($s); |
| 796 | 796 | $this->lasth = $h; |
| 797 | - if($ln>0) |
|
| 797 | + if ($ln > 0) |
|
| 798 | 798 | { |
| 799 | 799 | // Go to next line |
| 800 | 800 | $this->y += $h; |
| 801 | - if($ln==1) |
|
| 801 | + if ($ln == 1) |
|
| 802 | 802 | $this->x = $this->lMargin; |
| 803 | 803 | } |
| 804 | 804 | else |
| 805 | 805 | $this->x += $w; |
| 806 | 806 | } |
| 807 | 807 | |
| 808 | -function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) |
|
| 808 | +function MultiCell($w, $h, $txt, $border = 0, $align = 'J', $fill = false) |
|
| 809 | 809 | { |
| 810 | 810 | // Output text with automatic or explicit line breaks |
| 811 | 811 | $cw = &$this->CurrentFont['cw']; |
| 812 | - if($w==0) |
|
| 813 | - $w = $this->w-$this->rMargin-$this->x; |
|
| 814 | - $wmax = ($w-2*$this->cMargin); |
|
| 815 | - $s = str_replace("\r",'',$txt); |
|
| 812 | + if ($w == 0) |
|
| 813 | + $w = $this->w - $this->rMargin - $this->x; |
|
| 814 | + $wmax = ($w - 2 * $this->cMargin); |
|
| 815 | + $s = str_replace("\r", '', $txt); |
|
| 816 | 816 | if ($this->unifontSubset) { |
| 817 | - $nb=mb_strlen($s, 'utf-8'); |
|
| 818 | - while($nb>0 && mb_substr($s,$nb-1,1,'utf-8')=="\n") $nb--; |
|
| 817 | + $nb = mb_strlen($s, 'utf-8'); |
|
| 818 | + while ($nb > 0 && mb_substr($s, $nb - 1, 1, 'utf-8') == "\n") $nb--; |
|
| 819 | 819 | } |
| 820 | 820 | else { |
| 821 | 821 | $nb = strlen($s); |
| 822 | - if($nb>0 && $s[$nb-1]=="\n") |
|
| 822 | + if ($nb > 0 && $s[$nb - 1] == "\n") |
|
| 823 | 823 | $nb--; |
| 824 | 824 | } |
| 825 | 825 | $b = 0; |
| 826 | - if($border) |
|
| 826 | + if ($border) |
|
| 827 | 827 | { |
| 828 | - if($border==1) |
|
| 828 | + if ($border == 1) |
|
| 829 | 829 | { |
| 830 | 830 | $border = 'LTRB'; |
| 831 | 831 | $b = 'LRT'; |
@@ -834,11 +834,11 @@ discard block |
||
| 834 | 834 | else |
| 835 | 835 | { |
| 836 | 836 | $b2 = ''; |
| 837 | - if(strpos($border,'L')!==false) |
|
| 837 | + if (strpos($border, 'L') !== false) |
|
| 838 | 838 | $b2 .= 'L'; |
| 839 | - if(strpos($border,'R')!==false) |
|
| 839 | + if (strpos($border, 'R') !== false) |
|
| 840 | 840 | $b2 .= 'R'; |
| 841 | - $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2; |
|
| 841 | + $b = (strpos($border, 'T') !== false) ? $b2.'T' : $b2; |
|
| 842 | 842 | } |
| 843 | 843 | } |
| 844 | 844 | $sep = -1; |
@@ -847,28 +847,28 @@ discard block |
||
| 847 | 847 | $l = 0; |
| 848 | 848 | $ns = 0; |
| 849 | 849 | $nl = 1; |
| 850 | - while($i<$nb) |
|
| 850 | + while ($i < $nb) |
|
| 851 | 851 | { |
| 852 | 852 | // Get next character |
| 853 | 853 | if ($this->unifontSubset) { |
| 854 | - $c = mb_substr($s,$i,1,'UTF-8'); |
|
| 854 | + $c = mb_substr($s, $i, 1, 'UTF-8'); |
|
| 855 | 855 | } |
| 856 | 856 | else { |
| 857 | - $c=$s[$i]; |
|
| 857 | + $c = $s[$i]; |
|
| 858 | 858 | } |
| 859 | - if($c=="\n") |
|
| 859 | + if ($c == "\n") |
|
| 860 | 860 | { |
| 861 | 861 | // Explicit line break |
| 862 | - if($this->ws>0) |
|
| 862 | + if ($this->ws > 0) |
|
| 863 | 863 | { |
| 864 | 864 | $this->ws = 0; |
| 865 | 865 | $this->_out('0 Tw'); |
| 866 | 866 | } |
| 867 | 867 | if ($this->unifontSubset) { |
| 868 | - $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill); |
|
| 868 | + $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), $b, 2, $align, $fill); |
|
| 869 | 869 | } |
| 870 | 870 | else { |
| 871 | - $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); |
|
| 871 | + $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); |
|
| 872 | 872 | } |
| 873 | 873 | $i++; |
| 874 | 874 | $sep = -1; |
@@ -876,11 +876,11 @@ discard block |
||
| 876 | 876 | $l = 0; |
| 877 | 877 | $ns = 0; |
| 878 | 878 | $nl++; |
| 879 | - if($border && $nl==2) |
|
| 879 | + if ($border && $nl == 2) |
|
| 880 | 880 | $b = $b2; |
| 881 | 881 | continue; |
| 882 | 882 | } |
| 883 | - if($c==' ') |
|
| 883 | + if ($c == ' ') |
|
| 884 | 884 | { |
| 885 | 885 | $sep = $i; |
| 886 | 886 | $ls = $l; |
@@ -888,81 +888,81 @@ discard block |
||
| 888 | 888 | } |
| 889 | 889 | |
| 890 | 890 | if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } |
| 891 | - else { $l += $cw[$c]*$this->FontSize/1000; } |
|
| 891 | + else { $l += $cw[$c] * $this->FontSize / 1000; } |
|
| 892 | 892 | |
| 893 | - if($l>$wmax) |
|
| 893 | + if ($l > $wmax) |
|
| 894 | 894 | { |
| 895 | 895 | // Automatic line break |
| 896 | - if($sep==-1) |
|
| 896 | + if ($sep == -1) |
|
| 897 | 897 | { |
| 898 | - if($i==$j) |
|
| 898 | + if ($i == $j) |
|
| 899 | 899 | $i++; |
| 900 | - if($this->ws>0) |
|
| 900 | + if ($this->ws > 0) |
|
| 901 | 901 | { |
| 902 | 902 | $this->ws = 0; |
| 903 | 903 | $this->_out('0 Tw'); |
| 904 | 904 | } |
| 905 | 905 | if ($this->unifontSubset) { |
| 906 | - $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill); |
|
| 906 | + $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), $b, 2, $align, $fill); |
|
| 907 | 907 | } |
| 908 | 908 | else { |
| 909 | - $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); |
|
| 909 | + $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); |
|
| 910 | 910 | } |
| 911 | 911 | } |
| 912 | 912 | else |
| 913 | 913 | { |
| 914 | - if($align=='J') |
|
| 914 | + if ($align == 'J') |
|
| 915 | 915 | { |
| 916 | - $this->ws = ($ns>1) ? ($wmax-$ls)/($ns-1) : 0; |
|
| 917 | - $this->_out(sprintf('%.3F Tw',$this->ws*$this->k)); |
|
| 916 | + $this->ws = ($ns > 1) ? ($wmax - $ls) / ($ns - 1) : 0; |
|
| 917 | + $this->_out(sprintf('%.3F Tw', $this->ws * $this->k)); |
|
| 918 | 918 | } |
| 919 | 919 | if ($this->unifontSubset) { |
| 920 | - $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),$b,2,$align,$fill); |
|
| 920 | + $this->Cell($w, $h, mb_substr($s, $j, $sep - $j, 'UTF-8'), $b, 2, $align, $fill); |
|
| 921 | 921 | } |
| 922 | 922 | else { |
| 923 | - $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill); |
|
| 923 | + $this->Cell($w, $h, substr($s, $j, $sep - $j), $b, 2, $align, $fill); |
|
| 924 | 924 | } |
| 925 | - $i = $sep+1; |
|
| 925 | + $i = $sep + 1; |
|
| 926 | 926 | } |
| 927 | 927 | $sep = -1; |
| 928 | 928 | $j = $i; |
| 929 | 929 | $l = 0; |
| 930 | 930 | $ns = 0; |
| 931 | 931 | $nl++; |
| 932 | - if($border && $nl==2) |
|
| 932 | + if ($border && $nl == 2) |
|
| 933 | 933 | $b = $b2; |
| 934 | 934 | } |
| 935 | 935 | else |
| 936 | 936 | $i++; |
| 937 | 937 | } |
| 938 | 938 | // Last chunk |
| 939 | - if($this->ws>0) |
|
| 939 | + if ($this->ws > 0) |
|
| 940 | 940 | { |
| 941 | 941 | $this->ws = 0; |
| 942 | 942 | $this->_out('0 Tw'); |
| 943 | 943 | } |
| 944 | - if($border && strpos($border,'B')!==false) |
|
| 944 | + if ($border && strpos($border, 'B') !== false) |
|
| 945 | 945 | $b .= 'B'; |
| 946 | 946 | if ($this->unifontSubset) { |
| 947 | - $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill); |
|
| 947 | + $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), $b, 2, $align, $fill); |
|
| 948 | 948 | } |
| 949 | 949 | else { |
| 950 | - $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); |
|
| 950 | + $this->Cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); |
|
| 951 | 951 | } |
| 952 | 952 | $this->x = $this->lMargin; |
| 953 | 953 | } |
| 954 | 954 | |
| 955 | -function Write($h, $txt, $link='') |
|
| 955 | +function Write($h, $txt, $link = '') |
|
| 956 | 956 | { |
| 957 | 957 | // Output text in flowing mode |
| 958 | 958 | $cw = &$this->CurrentFont['cw']; |
| 959 | - $w = $this->w-$this->rMargin-$this->x; |
|
| 959 | + $w = $this->w - $this->rMargin - $this->x; |
|
| 960 | 960 | |
| 961 | - $wmax = ($w-2*$this->cMargin); |
|
| 962 | - $s = str_replace("\r",'',$txt); |
|
| 961 | + $wmax = ($w - 2 * $this->cMargin); |
|
| 962 | + $s = str_replace("\r", '', $txt); |
|
| 963 | 963 | if ($this->unifontSubset) { |
| 964 | 964 | $nb = mb_strlen($s, 'UTF-8'); |
| 965 | - if($nb==1 && $s==" ") { |
|
| 965 | + if ($nb == 1 && $s == " ") { |
|
| 966 | 966 | $this->x += $this->GetStringWidth($s); |
| 967 | 967 | return; |
| 968 | 968 | } |
@@ -975,86 +975,86 @@ discard block |
||
| 975 | 975 | $j = 0; |
| 976 | 976 | $l = 0; |
| 977 | 977 | $nl = 1; |
| 978 | - while($i<$nb) |
|
| 978 | + while ($i < $nb) |
|
| 979 | 979 | { |
| 980 | 980 | // Get next character |
| 981 | 981 | if ($this->unifontSubset) { |
| 982 | - $c = mb_substr($s,$i,1,'UTF-8'); |
|
| 982 | + $c = mb_substr($s, $i, 1, 'UTF-8'); |
|
| 983 | 983 | } |
| 984 | 984 | else { |
| 985 | 985 | $c = $s[$i]; |
| 986 | 986 | } |
| 987 | - if($c=="\n") |
|
| 987 | + if ($c == "\n") |
|
| 988 | 988 | { |
| 989 | 989 | // Explicit line break |
| 990 | 990 | if ($this->unifontSubset) { |
| 991 | - $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link); |
|
| 991 | + $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 2, '', 0, $link); |
|
| 992 | 992 | } |
| 993 | 993 | else { |
| 994 | - $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link); |
|
| 994 | + $this->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', 0, $link); |
|
| 995 | 995 | } |
| 996 | 996 | $i++; |
| 997 | 997 | $sep = -1; |
| 998 | 998 | $j = $i; |
| 999 | 999 | $l = 0; |
| 1000 | - if($nl==1) |
|
| 1000 | + if ($nl == 1) |
|
| 1001 | 1001 | { |
| 1002 | 1002 | $this->x = $this->lMargin; |
| 1003 | - $w = $this->w-$this->rMargin-$this->x; |
|
| 1004 | - $wmax = ($w-2*$this->cMargin); |
|
| 1003 | + $w = $this->w - $this->rMargin - $this->x; |
|
| 1004 | + $wmax = ($w - 2 * $this->cMargin); |
|
| 1005 | 1005 | } |
| 1006 | 1006 | $nl++; |
| 1007 | 1007 | continue; |
| 1008 | 1008 | } |
| 1009 | - if($c==' ') |
|
| 1009 | + if ($c == ' ') |
|
| 1010 | 1010 | $sep = $i; |
| 1011 | 1011 | |
| 1012 | 1012 | if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } |
| 1013 | - else { $l += $cw[$c]*$this->FontSize/1000; } |
|
| 1013 | + else { $l += $cw[$c] * $this->FontSize / 1000; } |
|
| 1014 | 1014 | |
| 1015 | - if($l>$wmax) |
|
| 1015 | + if ($l > $wmax) |
|
| 1016 | 1016 | { |
| 1017 | 1017 | // Automatic line break |
| 1018 | - if($sep==-1) |
|
| 1018 | + if ($sep == -1) |
|
| 1019 | 1019 | { |
| 1020 | - if($this->x>$this->lMargin) |
|
| 1020 | + if ($this->x > $this->lMargin) |
|
| 1021 | 1021 | { |
| 1022 | 1022 | // Move to next line |
| 1023 | 1023 | $this->x = $this->lMargin; |
| 1024 | 1024 | $this->y += $h; |
| 1025 | - $w = $this->w-$this->rMargin-$this->x; |
|
| 1026 | - $wmax = ($w-2*$this->cMargin); |
|
| 1025 | + $w = $this->w - $this->rMargin - $this->x; |
|
| 1026 | + $wmax = ($w - 2 * $this->cMargin); |
|
| 1027 | 1027 | $i++; |
| 1028 | 1028 | $nl++; |
| 1029 | 1029 | continue; |
| 1030 | 1030 | } |
| 1031 | - if($i==$j) |
|
| 1031 | + if ($i == $j) |
|
| 1032 | 1032 | $i++; |
| 1033 | 1033 | if ($this->unifontSubset) { |
| 1034 | - $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link); |
|
| 1034 | + $this->Cell($w, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 2, '', 0, $link); |
|
| 1035 | 1035 | } |
| 1036 | 1036 | else { |
| 1037 | - $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link); |
|
| 1037 | + $this->Cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', 0, $link); |
|
| 1038 | 1038 | } |
| 1039 | 1039 | } |
| 1040 | 1040 | else |
| 1041 | 1041 | { |
| 1042 | 1042 | if ($this->unifontSubset) { |
| 1043 | - $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),0,2,'',0,$link); |
|
| 1043 | + $this->Cell($w, $h, mb_substr($s, $j, $sep - $j, 'UTF-8'), 0, 2, '', 0, $link); |
|
| 1044 | 1044 | } |
| 1045 | 1045 | else { |
| 1046 | - $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link); |
|
| 1046 | + $this->Cell($w, $h, substr($s, $j, $sep - $j), 0, 2, '', 0, $link); |
|
| 1047 | 1047 | } |
| 1048 | - $i = $sep+1; |
|
| 1048 | + $i = $sep + 1; |
|
| 1049 | 1049 | } |
| 1050 | 1050 | $sep = -1; |
| 1051 | 1051 | $j = $i; |
| 1052 | 1052 | $l = 0; |
| 1053 | - if($nl==1) |
|
| 1053 | + if ($nl == 1) |
|
| 1054 | 1054 | { |
| 1055 | 1055 | $this->x = $this->lMargin; |
| 1056 | - $w = $this->w-$this->rMargin-$this->x; |
|
| 1057 | - $wmax = ($w-2*$this->cMargin); |
|
| 1056 | + $w = $this->w - $this->rMargin - $this->x; |
|
| 1057 | + $wmax = ($w - 2 * $this->cMargin); |
|
| 1058 | 1058 | } |
| 1059 | 1059 | $nl++; |
| 1060 | 1060 | } |
@@ -1062,87 +1062,87 @@ discard block |
||
| 1062 | 1062 | $i++; |
| 1063 | 1063 | } |
| 1064 | 1064 | // Last chunk |
| 1065 | - if($i!=$j) { |
|
| 1065 | + if ($i != $j) { |
|
| 1066 | 1066 | if ($this->unifontSubset) { |
| 1067 | - $this->Cell($l,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',0,$link); |
|
| 1067 | + $this->Cell($l, $h, mb_substr($s, $j, $i - $j, 'UTF-8'), 0, 0, '', 0, $link); |
|
| 1068 | 1068 | } |
| 1069 | 1069 | else { |
| 1070 | - $this->Cell($l,$h,substr($s,$j),0,0,'',0,$link); |
|
| 1070 | + $this->Cell($l, $h, substr($s, $j), 0, 0, '', 0, $link); |
|
| 1071 | 1071 | } |
| 1072 | 1072 | } |
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | -function Ln($h=null) |
|
| 1075 | +function Ln($h = null) |
|
| 1076 | 1076 | { |
| 1077 | 1077 | // Line feed; default value is last cell height |
| 1078 | 1078 | $this->x = $this->lMargin; |
| 1079 | - if($h===null) |
|
| 1079 | + if ($h === null) |
|
| 1080 | 1080 | $this->y += $this->lasth; |
| 1081 | 1081 | else |
| 1082 | 1082 | $this->y += $h; |
| 1083 | 1083 | } |
| 1084 | 1084 | |
| 1085 | -function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='') |
|
| 1085 | +function Image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '') |
|
| 1086 | 1086 | { |
| 1087 | 1087 | // Put an image on the page |
| 1088 | - if(!isset($this->images[$file])) |
|
| 1088 | + if (!isset($this->images[$file])) |
|
| 1089 | 1089 | { |
| 1090 | 1090 | // First use of this image, get info |
| 1091 | - if($type=='') |
|
| 1091 | + if ($type == '') |
|
| 1092 | 1092 | { |
| 1093 | - $pos = strrpos($file,'.'); |
|
| 1094 | - if(!$pos) |
|
| 1093 | + $pos = strrpos($file, '.'); |
|
| 1094 | + if (!$pos) |
|
| 1095 | 1095 | $this->Error('Image file has no extension and no type was specified: '.$file); |
| 1096 | - $type = substr($file,$pos+1); |
|
| 1096 | + $type = substr($file, $pos + 1); |
|
| 1097 | 1097 | } |
| 1098 | 1098 | $type = strtolower($type); |
| 1099 | - if($type=='jpeg') |
|
| 1099 | + if ($type == 'jpeg') |
|
| 1100 | 1100 | $type = 'jpg'; |
| 1101 | 1101 | $mtd = '_parse'.$type; |
| 1102 | - if(!method_exists($this,$mtd)) |
|
| 1102 | + if (!method_exists($this, $mtd)) |
|
| 1103 | 1103 | $this->Error('Unsupported image type: '.$type); |
| 1104 | 1104 | $info = $this->$mtd($file); |
| 1105 | - $info['i'] = count($this->images)+1; |
|
| 1105 | + $info['i'] = count($this->images) + 1; |
|
| 1106 | 1106 | $this->images[$file] = $info; |
| 1107 | 1107 | } |
| 1108 | 1108 | else |
| 1109 | 1109 | $info = $this->images[$file]; |
| 1110 | 1110 | |
| 1111 | 1111 | // Automatic width and height calculation if needed |
| 1112 | - if($w==0 && $h==0) |
|
| 1112 | + if ($w == 0 && $h == 0) |
|
| 1113 | 1113 | { |
| 1114 | 1114 | // Put image at 96 dpi |
| 1115 | 1115 | $w = -96; |
| 1116 | 1116 | $h = -96; |
| 1117 | 1117 | } |
| 1118 | - if($w<0) |
|
| 1119 | - $w = -$info['w']*72/$w/$this->k; |
|
| 1120 | - if($h<0) |
|
| 1121 | - $h = -$info['h']*72/$h/$this->k; |
|
| 1122 | - if($w==0) |
|
| 1123 | - $w = $h*$info['w']/$info['h']; |
|
| 1124 | - if($h==0) |
|
| 1125 | - $h = $w*$info['h']/$info['w']; |
|
| 1118 | + if ($w < 0) |
|
| 1119 | + $w = -$info['w'] * 72 / $w / $this->k; |
|
| 1120 | + if ($h < 0) |
|
| 1121 | + $h = -$info['h'] * 72 / $h / $this->k; |
|
| 1122 | + if ($w == 0) |
|
| 1123 | + $w = $h * $info['w'] / $info['h']; |
|
| 1124 | + if ($h == 0) |
|
| 1125 | + $h = $w * $info['h'] / $info['w']; |
|
| 1126 | 1126 | |
| 1127 | 1127 | // Flowing mode |
| 1128 | - if($y===null) |
|
| 1128 | + if ($y === null) |
|
| 1129 | 1129 | { |
| 1130 | - if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) |
|
| 1130 | + if ($this->y + $h > $this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak()) |
|
| 1131 | 1131 | { |
| 1132 | 1132 | // Automatic page break |
| 1133 | 1133 | $x2 = $this->x; |
| 1134 | - $this->AddPage($this->CurOrientation,$this->CurPageSize); |
|
| 1134 | + $this->AddPage($this->CurOrientation, $this->CurPageSize); |
|
| 1135 | 1135 | $this->x = $x2; |
| 1136 | 1136 | } |
| 1137 | 1137 | $y = $this->y; |
| 1138 | 1138 | $this->y += $h; |
| 1139 | 1139 | } |
| 1140 | 1140 | |
| 1141 | - if($x===null) |
|
| 1141 | + if ($x === null) |
|
| 1142 | 1142 | $x = $this->x; |
| 1143 | - $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i'])); |
|
| 1144 | - if($link) |
|
| 1145 | - $this->Link($x,$y,$w,$h,$link); |
|
| 1143 | + $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q', $w * $this->k, $h * $this->k, $x * $this->k, ($this->h - ($y + $h)) * $this->k, $info['i'])); |
|
| 1144 | + if ($link) |
|
| 1145 | + $this->Link($x, $y, $w, $h, $link); |
|
| 1146 | 1146 | } |
| 1147 | 1147 | |
| 1148 | 1148 | function GetX() |
@@ -1154,10 +1154,10 @@ discard block |
||
| 1154 | 1154 | function SetX($x) |
| 1155 | 1155 | { |
| 1156 | 1156 | // Set x position |
| 1157 | - if($x>=0) |
|
| 1157 | + if ($x >= 0) |
|
| 1158 | 1158 | $this->x = $x; |
| 1159 | 1159 | else |
| 1160 | - $this->x = $this->w+$x; |
|
| 1160 | + $this->x = $this->w + $x; |
|
| 1161 | 1161 | } |
| 1162 | 1162 | |
| 1163 | 1163 | function GetY() |
@@ -1170,10 +1170,10 @@ discard block |
||
| 1170 | 1170 | { |
| 1171 | 1171 | // Set y position and reset x |
| 1172 | 1172 | $this->x = $this->lMargin; |
| 1173 | - if($y>=0) |
|
| 1173 | + if ($y >= 0) |
|
| 1174 | 1174 | $this->y = $y; |
| 1175 | 1175 | else |
| 1176 | - $this->y = $this->h+$y; |
|
| 1176 | + $this->y = $this->h + $y; |
|
| 1177 | 1177 | } |
| 1178 | 1178 | |
| 1179 | 1179 | function SetXY($x, $y) |
@@ -1183,15 +1183,15 @@ discard block |
||
| 1183 | 1183 | $this->SetX($x); |
| 1184 | 1184 | } |
| 1185 | 1185 | |
| 1186 | -function Output($name='', $dest='') |
|
| 1186 | +function Output($name = '', $dest = '') |
|
| 1187 | 1187 | { |
| 1188 | 1188 | // Output PDF to some destination |
| 1189 | - if($this->state<3) |
|
| 1189 | + if ($this->state < 3) |
|
| 1190 | 1190 | $this->Close(); |
| 1191 | 1191 | $dest = strtoupper($dest); |
| 1192 | - if($dest=='') |
|
| 1192 | + if ($dest == '') |
|
| 1193 | 1193 | { |
| 1194 | - if($name=='') |
|
| 1194 | + if ($name == '') |
|
| 1195 | 1195 | { |
| 1196 | 1196 | $name = 'doc.pdf'; |
| 1197 | 1197 | $dest = 'I'; |
@@ -1199,12 +1199,12 @@ discard block |
||
| 1199 | 1199 | else |
| 1200 | 1200 | $dest = 'F'; |
| 1201 | 1201 | } |
| 1202 | - switch($dest) |
|
| 1202 | + switch ($dest) |
|
| 1203 | 1203 | { |
| 1204 | 1204 | case 'I': |
| 1205 | 1205 | // Send to standard output |
| 1206 | 1206 | $this->_checkoutput(); |
| 1207 | - if(PHP_SAPI!='cli') |
|
| 1207 | + if (PHP_SAPI != 'cli') |
|
| 1208 | 1208 | { |
| 1209 | 1209 | // We send to a browser |
| 1210 | 1210 | header('Content-Type: application/pdf'); |
@@ -1225,10 +1225,10 @@ discard block |
||
| 1225 | 1225 | break; |
| 1226 | 1226 | case 'F': |
| 1227 | 1227 | // Save to local file |
| 1228 | - $f = fopen($name,'wb'); |
|
| 1229 | - if(!$f) |
|
| 1228 | + $f = fopen($name, 'wb'); |
|
| 1229 | + if (!$f) |
|
| 1230 | 1230 | $this->Error('Unable to create output file: '.$name); |
| 1231 | - fwrite($f,$this->buffer,strlen($this->buffer)); |
|
| 1231 | + fwrite($f, $this->buffer, strlen($this->buffer)); |
|
| 1232 | 1232 | fclose($f); |
| 1233 | 1233 | break; |
| 1234 | 1234 | case 'S': |
@@ -1248,16 +1248,16 @@ discard block |
||
| 1248 | 1248 | function _dochecks() |
| 1249 | 1249 | { |
| 1250 | 1250 | // Check availability of %F |
| 1251 | - if(sprintf('%.1F',1.0)!='1.0') |
|
| 1251 | + if (sprintf('%.1F', 1.0) != '1.0') |
|
| 1252 | 1252 | $this->Error('This version of PHP is not supported'); |
| 1253 | 1253 | // Check availability of mbstring |
| 1254 | - if(!function_exists('mb_strlen')) |
|
| 1254 | + if (!function_exists('mb_strlen')) |
|
| 1255 | 1255 | $this->Error('mbstring extension is not available'); |
| 1256 | 1256 | // Check mbstring overloading |
| 1257 | - if(ini_get('mbstring.func_overload') & 2) |
|
| 1257 | + if (ini_get('mbstring.func_overload') & 2) |
|
| 1258 | 1258 | $this->Error('mbstring overloading must be disabled'); |
| 1259 | 1259 | // Ensure runtime magic quotes are disabled |
| 1260 | - if(get_magic_quotes_runtime()) |
|
| 1260 | + if (get_magic_quotes_runtime()) |
|
| 1261 | 1261 | @set_magic_quotes_runtime(0); |
| 1262 | 1262 | } |
| 1263 | 1263 | |
@@ -1268,15 +1268,15 @@ discard block |
||
| 1268 | 1268 | |
| 1269 | 1269 | function _checkoutput() |
| 1270 | 1270 | { |
| 1271 | - if(PHP_SAPI!='cli') |
|
| 1271 | + if (PHP_SAPI != 'cli') |
|
| 1272 | 1272 | { |
| 1273 | - if(headers_sent($file,$line)) |
|
| 1273 | + if (headers_sent($file, $line)) |
|
| 1274 | 1274 | $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)"); |
| 1275 | 1275 | } |
| 1276 | - if(ob_get_length()) |
|
| 1276 | + if (ob_get_length()) |
|
| 1277 | 1277 | { |
| 1278 | 1278 | // The output buffer is not empty |
| 1279 | - if(preg_match('/^(\xEF\xBB\xBF)?\s*$/',ob_get_contents())) |
|
| 1279 | + if (preg_match('/^(\xEF\xBB\xBF)?\s*$/', ob_get_contents())) |
|
| 1280 | 1280 | { |
| 1281 | 1281 | // It contains only a UTF-8 BOM and/or whitespace, let's clean it |
| 1282 | 1282 | ob_clean(); |
@@ -1288,17 +1288,17 @@ discard block |
||
| 1288 | 1288 | |
| 1289 | 1289 | function _getpagesize($size) |
| 1290 | 1290 | { |
| 1291 | - if(is_string($size)) |
|
| 1291 | + if (is_string($size)) |
|
| 1292 | 1292 | { |
| 1293 | 1293 | $size = strtolower($size); |
| 1294 | - if(!isset($this->StdPageSizes[$size])) |
|
| 1294 | + if (!isset($this->StdPageSizes[$size])) |
|
| 1295 | 1295 | $this->Error('Unknown page size: '.$size); |
| 1296 | 1296 | $a = $this->StdPageSizes[$size]; |
| 1297 | - return array($a[0]/$this->k, $a[1]/$this->k); |
|
| 1297 | + return array($a[0] / $this->k, $a[1] / $this->k); |
|
| 1298 | 1298 | } |
| 1299 | 1299 | else |
| 1300 | 1300 | { |
| 1301 | - if($size[0]>$size[1]) |
|
| 1301 | + if ($size[0] > $size[1]) |
|
| 1302 | 1302 | return array($size[1], $size[0]); |
| 1303 | 1303 | else |
| 1304 | 1304 | return $size; |
@@ -1314,18 +1314,18 @@ discard block |
||
| 1314 | 1314 | $this->y = $this->tMargin; |
| 1315 | 1315 | $this->FontFamily = ''; |
| 1316 | 1316 | // Check page size and orientation |
| 1317 | - if($orientation=='') |
|
| 1317 | + if ($orientation == '') |
|
| 1318 | 1318 | $orientation = $this->DefOrientation; |
| 1319 | 1319 | else |
| 1320 | 1320 | $orientation = strtoupper($orientation[0]); |
| 1321 | - if($size=='') |
|
| 1321 | + if ($size == '') |
|
| 1322 | 1322 | $size = $this->DefPageSize; |
| 1323 | 1323 | else |
| 1324 | 1324 | $size = $this->_getpagesize($size); |
| 1325 | - if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1]) |
|
| 1325 | + if ($orientation != $this->CurOrientation || $size[0] != $this->CurPageSize[0] || $size[1] != $this->CurPageSize[1]) |
|
| 1326 | 1326 | { |
| 1327 | 1327 | // New size or orientation |
| 1328 | - if($orientation=='P') |
|
| 1328 | + if ($orientation == 'P') |
|
| 1329 | 1329 | { |
| 1330 | 1330 | $this->w = $size[0]; |
| 1331 | 1331 | $this->h = $size[1]; |
@@ -1335,13 +1335,13 @@ discard block |
||
| 1335 | 1335 | $this->w = $size[1]; |
| 1336 | 1336 | $this->h = $size[0]; |
| 1337 | 1337 | } |
| 1338 | - $this->wPt = $this->w*$this->k; |
|
| 1339 | - $this->hPt = $this->h*$this->k; |
|
| 1340 | - $this->PageBreakTrigger = $this->h-$this->bMargin; |
|
| 1338 | + $this->wPt = $this->w * $this->k; |
|
| 1339 | + $this->hPt = $this->h * $this->k; |
|
| 1340 | + $this->PageBreakTrigger = $this->h - $this->bMargin; |
|
| 1341 | 1341 | $this->CurOrientation = $orientation; |
| 1342 | 1342 | $this->CurPageSize = $size; |
| 1343 | 1343 | } |
| 1344 | - if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1]) |
|
| 1344 | + if ($orientation != $this->DefOrientation || $size[0] != $this->DefPageSize[0] || $size[1] != $this->DefPageSize[1]) |
|
| 1345 | 1345 | $this->PageSizes[$this->page] = array($this->wPt, $this->hPt); |
| 1346 | 1346 | } |
| 1347 | 1347 | |
@@ -1355,7 +1355,7 @@ discard block |
||
| 1355 | 1355 | // Load a font definition file from the font directory |
| 1356 | 1356 | include($this->fontpath.$font); |
| 1357 | 1357 | $a = get_defined_vars(); |
| 1358 | - if(!isset($a['name'])) |
|
| 1358 | + if (!isset($a['name'])) |
|
| 1359 | 1359 | $this->Error('Could not include font definition file'); |
| 1360 | 1360 | return $a; |
| 1361 | 1361 | } |
@@ -1363,10 +1363,10 @@ discard block |
||
| 1363 | 1363 | function _escape($s) |
| 1364 | 1364 | { |
| 1365 | 1365 | // Escape special characters in strings |
| 1366 | - $s = str_replace('\\','\\\\',$s); |
|
| 1367 | - $s = str_replace('(','\\(',$s); |
|
| 1368 | - $s = str_replace(')','\\)',$s); |
|
| 1369 | - $s = str_replace("\r",'\\r',$s); |
|
| 1366 | + $s = str_replace('\\', '\\\\', $s); |
|
| 1367 | + $s = str_replace('(', '\\(', $s); |
|
| 1368 | + $s = str_replace(')', '\\)', $s); |
|
| 1369 | + $s = str_replace("\r", '\\r', $s); |
|
| 1370 | 1370 | return $s; |
| 1371 | 1371 | } |
| 1372 | 1372 | |
@@ -1382,23 +1382,23 @@ discard block |
||
| 1382 | 1382 | $res = "\xFE\xFF"; |
| 1383 | 1383 | $nb = strlen($s); |
| 1384 | 1384 | $i = 0; |
| 1385 | - while($i<$nb) |
|
| 1385 | + while ($i < $nb) |
|
| 1386 | 1386 | { |
| 1387 | 1387 | $c1 = ord($s[$i++]); |
| 1388 | - if($c1>=224) |
|
| 1388 | + if ($c1 >= 224) |
|
| 1389 | 1389 | { |
| 1390 | 1390 | // 3-byte character |
| 1391 | 1391 | $c2 = ord($s[$i++]); |
| 1392 | 1392 | $c3 = ord($s[$i++]); |
| 1393 | - $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2)); |
|
| 1394 | - $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F)); |
|
| 1393 | + $res .= chr((($c1 & 0x0F) << 4) + (($c2 & 0x3C) >> 2)); |
|
| 1394 | + $res .= chr((($c2 & 0x03) << 6) + ($c3 & 0x3F)); |
|
| 1395 | 1395 | } |
| 1396 | - elseif($c1>=192) |
|
| 1396 | + elseif ($c1 >= 192) |
|
| 1397 | 1397 | { |
| 1398 | 1398 | // 2-byte character |
| 1399 | 1399 | $c2 = ord($s[$i++]); |
| 1400 | - $res .= chr(($c1 & 0x1C)>>2); |
|
| 1401 | - $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F)); |
|
| 1400 | + $res .= chr(($c1 & 0x1C) >> 2); |
|
| 1401 | + $res .= chr((($c1 & 0x03) << 6) + ($c2 & 0x3F)); |
|
| 1402 | 1402 | } |
| 1403 | 1403 | else |
| 1404 | 1404 | { |
@@ -1414,21 +1414,21 @@ discard block |
||
| 1414 | 1414 | // Underline text |
| 1415 | 1415 | $up = $this->CurrentFont['up']; |
| 1416 | 1416 | $ut = $this->CurrentFont['ut']; |
| 1417 | - $w = $this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '); |
|
| 1418 | - return sprintf('%.2F %.2F %.2F %.2F re f',$x*$this->k,($this->h-($y-$up/1000*$this->FontSize))*$this->k,$w*$this->k,-$ut/1000*$this->FontSizePt); |
|
| 1417 | + $w = $this->GetStringWidth($txt) + $this->ws * substr_count($txt, ' '); |
|
| 1418 | + return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ($this->h - ($y - $up / 1000 * $this->FontSize)) * $this->k, $w * $this->k, -$ut / 1000 * $this->FontSizePt); |
|
| 1419 | 1419 | } |
| 1420 | 1420 | |
| 1421 | 1421 | function _parsejpg($file) |
| 1422 | 1422 | { |
| 1423 | 1423 | // Extract info from a JPEG file |
| 1424 | 1424 | $a = getimagesize($file); |
| 1425 | - if(!$a) |
|
| 1425 | + if (!$a) |
|
| 1426 | 1426 | $this->Error('Missing or incorrect image file: '.$file); |
| 1427 | - if($a[2]!=2) |
|
| 1427 | + if ($a[2] != 2) |
|
| 1428 | 1428 | $this->Error('Not a JPEG file: '.$file); |
| 1429 | - if(!isset($a['channels']) || $a['channels']==3) |
|
| 1429 | + if (!isset($a['channels']) || $a['channels'] == 3) |
|
| 1430 | 1430 | $colspace = 'DeviceRGB'; |
| 1431 | - elseif($a['channels']==4) |
|
| 1431 | + elseif ($a['channels'] == 4) |
|
| 1432 | 1432 | $colspace = 'DeviceCMYK'; |
| 1433 | 1433 | else |
| 1434 | 1434 | $colspace = 'DeviceGray'; |
@@ -1440,10 +1440,10 @@ discard block |
||
| 1440 | 1440 | function _parsepng($file) |
| 1441 | 1441 | { |
| 1442 | 1442 | // Extract info from a PNG file |
| 1443 | - $f = fopen($file,'rb'); |
|
| 1444 | - if(!$f) |
|
| 1443 | + $f = fopen($file, 'rb'); |
|
| 1444 | + if (!$f) |
|
| 1445 | 1445 | $this->Error('Can\'t open image file: '.$file); |
| 1446 | - $info = $this->_parsepngstream($f,$file); |
|
| 1446 | + $info = $this->_parsepngstream($f, $file); |
|
| 1447 | 1447 | fclose($f); |
| 1448 | 1448 | return $info; |
| 1449 | 1449 | } |
@@ -1451,35 +1451,35 @@ discard block |
||
| 1451 | 1451 | function _parsepngstream($f, $file) |
| 1452 | 1452 | { |
| 1453 | 1453 | // Check signature |
| 1454 | - if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) |
|
| 1454 | + if ($this->_readstream($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) |
|
| 1455 | 1455 | $this->Error('Not a PNG file: '.$file); |
| 1456 | 1456 | |
| 1457 | 1457 | // Read header chunk |
| 1458 | - $this->_readstream($f,4); |
|
| 1459 | - if($this->_readstream($f,4)!='IHDR') |
|
| 1458 | + $this->_readstream($f, 4); |
|
| 1459 | + if ($this->_readstream($f, 4) != 'IHDR') |
|
| 1460 | 1460 | $this->Error('Incorrect PNG file: '.$file); |
| 1461 | 1461 | $w = $this->_readint($f); |
| 1462 | 1462 | $h = $this->_readint($f); |
| 1463 | - $bpc = ord($this->_readstream($f,1)); |
|
| 1464 | - if($bpc>8) |
|
| 1463 | + $bpc = ord($this->_readstream($f, 1)); |
|
| 1464 | + if ($bpc > 8) |
|
| 1465 | 1465 | $this->Error('16-bit depth not supported: '.$file); |
| 1466 | - $ct = ord($this->_readstream($f,1)); |
|
| 1467 | - if($ct==0 || $ct==4) |
|
| 1466 | + $ct = ord($this->_readstream($f, 1)); |
|
| 1467 | + if ($ct == 0 || $ct == 4) |
|
| 1468 | 1468 | $colspace = 'DeviceGray'; |
| 1469 | - elseif($ct==2 || $ct==6) |
|
| 1469 | + elseif ($ct == 2 || $ct == 6) |
|
| 1470 | 1470 | $colspace = 'DeviceRGB'; |
| 1471 | - elseif($ct==3) |
|
| 1471 | + elseif ($ct == 3) |
|
| 1472 | 1472 | $colspace = 'Indexed'; |
| 1473 | 1473 | else |
| 1474 | 1474 | $this->Error('Unknown color type: '.$file); |
| 1475 | - if(ord($this->_readstream($f,1))!=0) |
|
| 1475 | + if (ord($this->_readstream($f, 1)) != 0) |
|
| 1476 | 1476 | $this->Error('Unknown compression method: '.$file); |
| 1477 | - if(ord($this->_readstream($f,1))!=0) |
|
| 1477 | + if (ord($this->_readstream($f, 1)) != 0) |
|
| 1478 | 1478 | $this->Error('Unknown filter method: '.$file); |
| 1479 | - if(ord($this->_readstream($f,1))!=0) |
|
| 1479 | + if (ord($this->_readstream($f, 1)) != 0) |
|
| 1480 | 1480 | $this->Error('Interlacing not supported: '.$file); |
| 1481 | - $this->_readstream($f,4); |
|
| 1482 | - $dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w; |
|
| 1481 | + $this->_readstream($f, 4); |
|
| 1482 | + $dp = '/Predictor 15 /Colors '.($colspace == 'DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w; |
|
| 1483 | 1483 | |
| 1484 | 1484 | // Scan chunks looking for palette, transparency and image data |
| 1485 | 1485 | $pal = ''; |
@@ -1488,85 +1488,85 @@ discard block |
||
| 1488 | 1488 | do |
| 1489 | 1489 | { |
| 1490 | 1490 | $n = $this->_readint($f); |
| 1491 | - $type = $this->_readstream($f,4); |
|
| 1492 | - if($type=='PLTE') |
|
| 1491 | + $type = $this->_readstream($f, 4); |
|
| 1492 | + if ($type == 'PLTE') |
|
| 1493 | 1493 | { |
| 1494 | 1494 | // Read palette |
| 1495 | - $pal = $this->_readstream($f,$n); |
|
| 1496 | - $this->_readstream($f,4); |
|
| 1495 | + $pal = $this->_readstream($f, $n); |
|
| 1496 | + $this->_readstream($f, 4); |
|
| 1497 | 1497 | } |
| 1498 | - elseif($type=='tRNS') |
|
| 1498 | + elseif ($type == 'tRNS') |
|
| 1499 | 1499 | { |
| 1500 | 1500 | // Read transparency info |
| 1501 | - $t = $this->_readstream($f,$n); |
|
| 1502 | - if($ct==0) |
|
| 1503 | - $trns = array(ord(substr($t,1,1))); |
|
| 1504 | - elseif($ct==2) |
|
| 1505 | - $trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1))); |
|
| 1501 | + $t = $this->_readstream($f, $n); |
|
| 1502 | + if ($ct == 0) |
|
| 1503 | + $trns = array(ord(substr($t, 1, 1))); |
|
| 1504 | + elseif ($ct == 2) |
|
| 1505 | + $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1))); |
|
| 1506 | 1506 | else |
| 1507 | 1507 | { |
| 1508 | - $pos = strpos($t,chr(0)); |
|
| 1509 | - if($pos!==false) |
|
| 1508 | + $pos = strpos($t, chr(0)); |
|
| 1509 | + if ($pos !== false) |
|
| 1510 | 1510 | $trns = array($pos); |
| 1511 | 1511 | } |
| 1512 | - $this->_readstream($f,4); |
|
| 1512 | + $this->_readstream($f, 4); |
|
| 1513 | 1513 | } |
| 1514 | - elseif($type=='IDAT') |
|
| 1514 | + elseif ($type == 'IDAT') |
|
| 1515 | 1515 | { |
| 1516 | 1516 | // Read image data block |
| 1517 | - $data .= $this->_readstream($f,$n); |
|
| 1518 | - $this->_readstream($f,4); |
|
| 1517 | + $data .= $this->_readstream($f, $n); |
|
| 1518 | + $this->_readstream($f, 4); |
|
| 1519 | 1519 | } |
| 1520 | - elseif($type=='IEND') |
|
| 1520 | + elseif ($type == 'IEND') |
|
| 1521 | 1521 | break; |
| 1522 | 1522 | else |
| 1523 | - $this->_readstream($f,$n+4); |
|
| 1523 | + $this->_readstream($f, $n + 4); |
|
| 1524 | 1524 | } |
| 1525 | - while($n); |
|
| 1525 | + while ($n); |
|
| 1526 | 1526 | |
| 1527 | - if($colspace=='Indexed' && empty($pal)) |
|
| 1527 | + if ($colspace == 'Indexed' && empty($pal)) |
|
| 1528 | 1528 | $this->Error('Missing palette in '.$file); |
| 1529 | 1529 | $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns); |
| 1530 | - if($ct>=4) |
|
| 1530 | + if ($ct >= 4) |
|
| 1531 | 1531 | { |
| 1532 | 1532 | // Extract alpha channel |
| 1533 | - if(!function_exists('gzuncompress')) |
|
| 1533 | + if (!function_exists('gzuncompress')) |
|
| 1534 | 1534 | $this->Error('Zlib not available, can\'t handle alpha channel: '.$file); |
| 1535 | 1535 | $data = gzuncompress($data); |
| 1536 | 1536 | $color = ''; |
| 1537 | 1537 | $alpha = ''; |
| 1538 | - if($ct==4) |
|
| 1538 | + if ($ct == 4) |
|
| 1539 | 1539 | { |
| 1540 | 1540 | // Gray image |
| 1541 | - $len = 2*$w; |
|
| 1542 | - for($i=0;$i<$h;$i++) |
|
| 1541 | + $len = 2 * $w; |
|
| 1542 | + for ($i = 0; $i < $h; $i++) |
|
| 1543 | 1543 | { |
| 1544 | - $pos = (1+$len)*$i; |
|
| 1544 | + $pos = (1 + $len) * $i; |
|
| 1545 | 1545 | $color .= $data[$pos]; |
| 1546 | 1546 | $alpha .= $data[$pos]; |
| 1547 | - $line = substr($data,$pos+1,$len); |
|
| 1548 | - $color .= preg_replace('/(.)./s','$1',$line); |
|
| 1549 | - $alpha .= preg_replace('/.(.)/s','$1',$line); |
|
| 1547 | + $line = substr($data, $pos + 1, $len); |
|
| 1548 | + $color .= preg_replace('/(.)./s', '$1', $line); |
|
| 1549 | + $alpha .= preg_replace('/.(.)/s', '$1', $line); |
|
| 1550 | 1550 | } |
| 1551 | 1551 | } |
| 1552 | 1552 | else |
| 1553 | 1553 | { |
| 1554 | 1554 | // RGB image |
| 1555 | - $len = 4*$w; |
|
| 1556 | - for($i=0;$i<$h;$i++) |
|
| 1555 | + $len = 4 * $w; |
|
| 1556 | + for ($i = 0; $i < $h; $i++) |
|
| 1557 | 1557 | { |
| 1558 | - $pos = (1+$len)*$i; |
|
| 1558 | + $pos = (1 + $len) * $i; |
|
| 1559 | 1559 | $color .= $data[$pos]; |
| 1560 | 1560 | $alpha .= $data[$pos]; |
| 1561 | - $line = substr($data,$pos+1,$len); |
|
| 1562 | - $color .= preg_replace('/(.{3})./s','$1',$line); |
|
| 1563 | - $alpha .= preg_replace('/.{3}(.)/s','$1',$line); |
|
| 1561 | + $line = substr($data, $pos + 1, $len); |
|
| 1562 | + $color .= preg_replace('/(.{3})./s', '$1', $line); |
|
| 1563 | + $alpha .= preg_replace('/.{3}(.)/s', '$1', $line); |
|
| 1564 | 1564 | } |
| 1565 | 1565 | } |
| 1566 | 1566 | unset($data); |
| 1567 | 1567 | $data = gzcompress($color); |
| 1568 | 1568 | $info['smask'] = gzcompress($alpha); |
| 1569 | - if($this->PDFVersion<'1.4') |
|
| 1569 | + if ($this->PDFVersion < '1.4') |
|
| 1570 | 1570 | $this->PDFVersion = '1.4'; |
| 1571 | 1571 | } |
| 1572 | 1572 | $info['data'] = $data; |
@@ -1577,15 +1577,15 @@ discard block |
||
| 1577 | 1577 | { |
| 1578 | 1578 | // Read n bytes from stream |
| 1579 | 1579 | $res = ''; |
| 1580 | - while($n>0 && !feof($f)) |
|
| 1580 | + while ($n > 0 && !feof($f)) |
|
| 1581 | 1581 | { |
| 1582 | - $s = fread($f,$n); |
|
| 1583 | - if($s===false) |
|
| 1582 | + $s = fread($f, $n); |
|
| 1583 | + if ($s === false) |
|
| 1584 | 1584 | $this->Error('Error while reading stream'); |
| 1585 | 1585 | $n -= strlen($s); |
| 1586 | 1586 | $res .= $s; |
| 1587 | 1587 | } |
| 1588 | - if($n>0) |
|
| 1588 | + if ($n > 0) |
|
| 1589 | 1589 | $this->Error('Unexpected end of stream'); |
| 1590 | 1590 | return $res; |
| 1591 | 1591 | } |
@@ -1593,41 +1593,41 @@ discard block |
||
| 1593 | 1593 | function _readint($f) |
| 1594 | 1594 | { |
| 1595 | 1595 | // Read a 4-byte integer from stream |
| 1596 | - $a = unpack('Ni',$this->_readstream($f,4)); |
|
| 1596 | + $a = unpack('Ni', $this->_readstream($f, 4)); |
|
| 1597 | 1597 | return $a['i']; |
| 1598 | 1598 | } |
| 1599 | 1599 | |
| 1600 | 1600 | function _parsegif($file) |
| 1601 | 1601 | { |
| 1602 | 1602 | // Extract info from a GIF file (via PNG conversion) |
| 1603 | - if(!function_exists('imagepng')) |
|
| 1603 | + if (!function_exists('imagepng')) |
|
| 1604 | 1604 | $this->Error('GD extension is required for GIF support'); |
| 1605 | - if(!function_exists('imagecreatefromgif')) |
|
| 1605 | + if (!function_exists('imagecreatefromgif')) |
|
| 1606 | 1606 | $this->Error('GD has no GIF read support'); |
| 1607 | 1607 | $im = imagecreatefromgif($file); |
| 1608 | - if(!$im) |
|
| 1608 | + if (!$im) |
|
| 1609 | 1609 | $this->Error('Missing or incorrect image file: '.$file); |
| 1610 | - imageinterlace($im,0); |
|
| 1611 | - $f = @fopen('php://temp','rb+'); |
|
| 1612 | - if($f) |
|
| 1610 | + imageinterlace($im, 0); |
|
| 1611 | + $f = @fopen('php://temp', 'rb+'); |
|
| 1612 | + if ($f) |
|
| 1613 | 1613 | { |
| 1614 | 1614 | // Perform conversion in memory |
| 1615 | 1615 | ob_start(); |
| 1616 | 1616 | imagepng($im); |
| 1617 | 1617 | $data = ob_get_clean(); |
| 1618 | 1618 | imagedestroy($im); |
| 1619 | - fwrite($f,$data); |
|
| 1619 | + fwrite($f, $data); |
|
| 1620 | 1620 | rewind($f); |
| 1621 | - $info = $this->_parsepngstream($f,$file); |
|
| 1621 | + $info = $this->_parsepngstream($f, $file); |
|
| 1622 | 1622 | fclose($f); |
| 1623 | 1623 | } |
| 1624 | 1624 | else |
| 1625 | 1625 | { |
| 1626 | 1626 | // Use temporary file |
| 1627 | - $tmp = tempnam('.','gif'); |
|
| 1628 | - if(!$tmp) |
|
| 1627 | + $tmp = tempnam('.', 'gif'); |
|
| 1628 | + if (!$tmp) |
|
| 1629 | 1629 | $this->Error('Unable to create a temporary file'); |
| 1630 | - if(!imagepng($im,$tmp)) |
|
| 1630 | + if (!imagepng($im, $tmp)) |
|
| 1631 | 1631 | $this->Error('Error while saving to temporary file'); |
| 1632 | 1632 | imagedestroy($im); |
| 1633 | 1633 | $info = $this->_parsepng($tmp); |
@@ -1654,7 +1654,7 @@ discard block |
||
| 1654 | 1654 | function _out($s) |
| 1655 | 1655 | { |
| 1656 | 1656 | // Add a line to the document |
| 1657 | - if($this->state==2) |
|
| 1657 | + if ($this->state == 2) |
|
| 1658 | 1658 | $this->pages[$this->page] .= $s."\n"; |
| 1659 | 1659 | else |
| 1660 | 1660 | $this->buffer .= $s."\n"; |
@@ -1663,59 +1663,59 @@ discard block |
||
| 1663 | 1663 | function _putpages() |
| 1664 | 1664 | { |
| 1665 | 1665 | $nb = $this->page; |
| 1666 | - if(!empty($this->AliasNbPages)) |
|
| 1666 | + if (!empty($this->AliasNbPages)) |
|
| 1667 | 1667 | { |
| 1668 | 1668 | // Replace number of pages in fonts using subsets |
| 1669 | 1669 | $alias = $this->UTF8ToUTF16BE($this->AliasNbPages, false); |
| 1670 | 1670 | $r = $this->UTF8ToUTF16BE("$nb", false); |
| 1671 | - for($n=1;$n<=$nb;$n++) |
|
| 1672 | - $this->pages[$n] = str_replace($alias,$r,$this->pages[$n]); |
|
| 1671 | + for ($n = 1; $n <= $nb; $n++) |
|
| 1672 | + $this->pages[$n] = str_replace($alias, $r, $this->pages[$n]); |
|
| 1673 | 1673 | // Now repeat for no pages in non-subset fonts |
| 1674 | - for($n=1;$n<=$nb;$n++) |
|
| 1675 | - $this->pages[$n] = str_replace($this->AliasNbPages,$nb,$this->pages[$n]); |
|
| 1674 | + for ($n = 1; $n <= $nb; $n++) |
|
| 1675 | + $this->pages[$n] = str_replace($this->AliasNbPages, $nb, $this->pages[$n]); |
|
| 1676 | 1676 | } |
| 1677 | - if($this->DefOrientation=='P') |
|
| 1677 | + if ($this->DefOrientation == 'P') |
|
| 1678 | 1678 | { |
| 1679 | - $wPt = $this->DefPageSize[0]*$this->k; |
|
| 1680 | - $hPt = $this->DefPageSize[1]*$this->k; |
|
| 1679 | + $wPt = $this->DefPageSize[0] * $this->k; |
|
| 1680 | + $hPt = $this->DefPageSize[1] * $this->k; |
|
| 1681 | 1681 | } |
| 1682 | 1682 | else |
| 1683 | 1683 | { |
| 1684 | - $wPt = $this->DefPageSize[1]*$this->k; |
|
| 1685 | - $hPt = $this->DefPageSize[0]*$this->k; |
|
| 1684 | + $wPt = $this->DefPageSize[1] * $this->k; |
|
| 1685 | + $hPt = $this->DefPageSize[0] * $this->k; |
|
| 1686 | 1686 | } |
| 1687 | 1687 | $filter = ($this->compress) ? '/Filter /FlateDecode ' : ''; |
| 1688 | - for($n=1;$n<=$nb;$n++) |
|
| 1688 | + for ($n = 1; $n <= $nb; $n++) |
|
| 1689 | 1689 | { |
| 1690 | 1690 | // Page |
| 1691 | 1691 | $this->_newobj(); |
| 1692 | 1692 | $this->_out('<</Type /Page'); |
| 1693 | 1693 | $this->_out('/Parent 1 0 R'); |
| 1694 | - if(isset($this->PageSizes[$n])) |
|
| 1695 | - $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1])); |
|
| 1694 | + if (isset($this->PageSizes[$n])) |
|
| 1695 | + $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]', $this->PageSizes[$n][0], $this->PageSizes[$n][1])); |
|
| 1696 | 1696 | $this->_out('/Resources 2 0 R'); |
| 1697 | - if(isset($this->PageLinks[$n])) |
|
| 1697 | + if (isset($this->PageLinks[$n])) |
|
| 1698 | 1698 | { |
| 1699 | 1699 | // Links |
| 1700 | 1700 | $annots = '/Annots ['; |
| 1701 | - foreach($this->PageLinks[$n] as $pl) |
|
| 1701 | + foreach ($this->PageLinks[$n] as $pl) |
|
| 1702 | 1702 | { |
| 1703 | - $rect = sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); |
|
| 1703 | + $rect = sprintf('%.2F %.2F %.2F %.2F', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]); |
|
| 1704 | 1704 | $annots .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] '; |
| 1705 | - if(is_string($pl[4])) |
|
| 1705 | + if (is_string($pl[4])) |
|
| 1706 | 1706 | $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>'; |
| 1707 | 1707 | else |
| 1708 | 1708 | { |
| 1709 | 1709 | $l = $this->links[$pl[4]]; |
| 1710 | 1710 | $h = isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt; |
| 1711 | - $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k); |
|
| 1711 | + $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>', 1 + 2 * $l[0], $h - $l[1] * $this->k); |
|
| 1712 | 1712 | } |
| 1713 | 1713 | } |
| 1714 | 1714 | $this->_out($annots.']'); |
| 1715 | 1715 | } |
| 1716 | - if($this->PDFVersion>'1.3') |
|
| 1716 | + if ($this->PDFVersion > '1.3') |
|
| 1717 | 1717 | $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>'); |
| 1718 | - $this->_out('/Contents '.($this->n+1).' 0 R>>'); |
|
| 1718 | + $this->_out('/Contents '.($this->n + 1).' 0 R>>'); |
|
| 1719 | 1719 | $this->_out('endobj'); |
| 1720 | 1720 | // Page content |
| 1721 | 1721 | $p = ($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n]; |
@@ -1729,98 +1729,98 @@ discard block |
||
| 1729 | 1729 | $this->_out('1 0 obj'); |
| 1730 | 1730 | $this->_out('<</Type /Pages'); |
| 1731 | 1731 | $kids = '/Kids ['; |
| 1732 | - for($i=0;$i<$nb;$i++) |
|
| 1733 | - $kids .= (3+2*$i).' 0 R '; |
|
| 1732 | + for ($i = 0; $i < $nb; $i++) |
|
| 1733 | + $kids .= (3 + 2 * $i).' 0 R '; |
|
| 1734 | 1734 | $this->_out($kids.']'); |
| 1735 | 1735 | $this->_out('/Count '.$nb); |
| 1736 | - $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); |
|
| 1736 | + $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]', $wPt, $hPt)); |
|
| 1737 | 1737 | $this->_out('>>'); |
| 1738 | 1738 | $this->_out('endobj'); |
| 1739 | 1739 | } |
| 1740 | 1740 | |
| 1741 | 1741 | function _putfonts() |
| 1742 | 1742 | { |
| 1743 | - $nf=$this->n; |
|
| 1744 | - foreach($this->diffs as $diff) |
|
| 1743 | + $nf = $this->n; |
|
| 1744 | + foreach ($this->diffs as $diff) |
|
| 1745 | 1745 | { |
| 1746 | 1746 | // Encodings |
| 1747 | 1747 | $this->_newobj(); |
| 1748 | 1748 | $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>'); |
| 1749 | 1749 | $this->_out('endobj'); |
| 1750 | 1750 | } |
| 1751 | - foreach($this->FontFiles as $file=>$info) |
|
| 1751 | + foreach ($this->FontFiles as $file=>$info) |
|
| 1752 | 1752 | { |
| 1753 | - if (!isset($info['type']) || $info['type']!='TTF') { |
|
| 1753 | + if (!isset($info['type']) || $info['type'] != 'TTF') { |
|
| 1754 | 1754 | // Font file embedding |
| 1755 | 1755 | $this->_newobj(); |
| 1756 | - $this->FontFiles[$file]['n']=$this->n; |
|
| 1757 | - $font=''; |
|
| 1758 | - $f=fopen($this->_getfontpath().$file,'rb',1); |
|
| 1759 | - if(!$f) |
|
| 1756 | + $this->FontFiles[$file]['n'] = $this->n; |
|
| 1757 | + $font = ''; |
|
| 1758 | + $f = fopen($this->_getfontpath().$file, 'rb', 1); |
|
| 1759 | + if (!$f) |
|
| 1760 | 1760 | $this->Error('Font file not found'); |
| 1761 | - while(!feof($f)) |
|
| 1762 | - $font.=fread($f,8192); |
|
| 1761 | + while (!feof($f)) |
|
| 1762 | + $font .= fread($f, 8192); |
|
| 1763 | 1763 | fclose($f); |
| 1764 | - $compressed=(substr($file,-2)=='.z'); |
|
| 1765 | - if(!$compressed && isset($info['length2'])) |
|
| 1764 | + $compressed = (substr($file, -2) == '.z'); |
|
| 1765 | + if (!$compressed && isset($info['length2'])) |
|
| 1766 | 1766 | { |
| 1767 | - $header=(ord($font[0])==128); |
|
| 1768 | - if($header) |
|
| 1767 | + $header = (ord($font[0]) == 128); |
|
| 1768 | + if ($header) |
|
| 1769 | 1769 | { |
| 1770 | 1770 | // Strip first binary header |
| 1771 | - $font=substr($font,6); |
|
| 1771 | + $font = substr($font, 6); |
|
| 1772 | 1772 | } |
| 1773 | - if($header && ord($font[$info['length1']])==128) |
|
| 1773 | + if ($header && ord($font[$info['length1']]) == 128) |
|
| 1774 | 1774 | { |
| 1775 | 1775 | // Strip second binary header |
| 1776 | - $font=substr($font,0,$info['length1']).substr($font,$info['length1']+6); |
|
| 1776 | + $font = substr($font, 0, $info['length1']).substr($font, $info['length1'] + 6); |
|
| 1777 | 1777 | } |
| 1778 | 1778 | } |
| 1779 | 1779 | $this->_out('<</Length '.strlen($font)); |
| 1780 | - if($compressed) |
|
| 1780 | + if ($compressed) |
|
| 1781 | 1781 | $this->_out('/Filter /FlateDecode'); |
| 1782 | 1782 | $this->_out('/Length1 '.$info['length1']); |
| 1783 | - if(isset($info['length2'])) |
|
| 1783 | + if (isset($info['length2'])) |
|
| 1784 | 1784 | $this->_out('/Length2 '.$info['length2'].' /Length3 0'); |
| 1785 | 1785 | $this->_out('>>'); |
| 1786 | 1786 | $this->_putstream($font); |
| 1787 | 1787 | $this->_out('endobj'); |
| 1788 | 1788 | } |
| 1789 | 1789 | } |
| 1790 | - foreach($this->fonts as $k=>$font) |
|
| 1790 | + foreach ($this->fonts as $k=>$font) |
|
| 1791 | 1791 | { |
| 1792 | 1792 | // Font objects |
| 1793 | 1793 | //$this->fonts[$k]['n']=$this->n+1; |
| 1794 | 1794 | $type = $font['type']; |
| 1795 | 1795 | $name = $font['name']; |
| 1796 | - if($type=='Core') |
|
| 1796 | + if ($type == 'Core') |
|
| 1797 | 1797 | { |
| 1798 | 1798 | // Standard font |
| 1799 | - $this->fonts[$k]['n']=$this->n+1; |
|
| 1799 | + $this->fonts[$k]['n'] = $this->n + 1; |
|
| 1800 | 1800 | $this->_newobj(); |
| 1801 | 1801 | $this->_out('<</Type /Font'); |
| 1802 | 1802 | $this->_out('/BaseFont /'.$name); |
| 1803 | 1803 | $this->_out('/Subtype /Type1'); |
| 1804 | - if($name!='Symbol' && $name!='ZapfDingbats') |
|
| 1804 | + if ($name != 'Symbol' && $name != 'ZapfDingbats') |
|
| 1805 | 1805 | $this->_out('/Encoding /WinAnsiEncoding'); |
| 1806 | 1806 | $this->_out('>>'); |
| 1807 | 1807 | $this->_out('endobj'); |
| 1808 | 1808 | } |
| 1809 | - elseif($type=='Type1' || $type=='TrueType') |
|
| 1809 | + elseif ($type == 'Type1' || $type == 'TrueType') |
|
| 1810 | 1810 | { |
| 1811 | 1811 | // Additional Type1 or TrueType font |
| 1812 | - $this->fonts[$k]['n']=$this->n+1; |
|
| 1812 | + $this->fonts[$k]['n'] = $this->n + 1; |
|
| 1813 | 1813 | $this->_newobj(); |
| 1814 | 1814 | $this->_out('<</Type /Font'); |
| 1815 | 1815 | $this->_out('/BaseFont /'.$name); |
| 1816 | 1816 | $this->_out('/Subtype /'.$type); |
| 1817 | 1817 | $this->_out('/FirstChar 32 /LastChar 255'); |
| 1818 | - $this->_out('/Widths '.($this->n+1).' 0 R'); |
|
| 1819 | - $this->_out('/FontDescriptor '.($this->n+2).' 0 R'); |
|
| 1820 | - if($font['enc']) |
|
| 1818 | + $this->_out('/Widths '.($this->n + 1).' 0 R'); |
|
| 1819 | + $this->_out('/FontDescriptor '.($this->n + 2).' 0 R'); |
|
| 1820 | + if ($font['enc']) |
|
| 1821 | 1821 | { |
| 1822 | - if(isset($font['diff'])) |
|
| 1823 | - $this->_out('/Encoding '.($nf+$font['diff']).' 0 R'); |
|
| 1822 | + if (isset($font['diff'])) |
|
| 1823 | + $this->_out('/Encoding '.($nf + $font['diff']).' 0 R'); |
|
| 1824 | 1824 | else |
| 1825 | 1825 | $this->_out('/Encoding /WinAnsiEncoding'); |
| 1826 | 1826 | } |
@@ -1828,26 +1828,26 @@ discard block |
||
| 1828 | 1828 | $this->_out('endobj'); |
| 1829 | 1829 | // Widths |
| 1830 | 1830 | $this->_newobj(); |
| 1831 | - $cw=&$font['cw']; |
|
| 1832 | - $s='['; |
|
| 1833 | - for($i=32;$i<=255;$i++) |
|
| 1834 | - $s.=$cw[chr($i)].' '; |
|
| 1831 | + $cw = &$font['cw']; |
|
| 1832 | + $s = '['; |
|
| 1833 | + for ($i = 32; $i <= 255; $i++) |
|
| 1834 | + $s .= $cw[chr($i)].' '; |
|
| 1835 | 1835 | $this->_out($s.']'); |
| 1836 | 1836 | $this->_out('endobj'); |
| 1837 | 1837 | // Descriptor |
| 1838 | 1838 | $this->_newobj(); |
| 1839 | - $s='<</Type /FontDescriptor /FontName /'.$name; |
|
| 1840 | - foreach($font['desc'] as $k=>$v) |
|
| 1841 | - $s.=' /'.$k.' '.$v; |
|
| 1842 | - $file=$font['file']; |
|
| 1843 | - if($file) |
|
| 1844 | - $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R'; |
|
| 1839 | + $s = '<</Type /FontDescriptor /FontName /'.$name; |
|
| 1840 | + foreach ($font['desc'] as $k=>$v) |
|
| 1841 | + $s .= ' /'.$k.' '.$v; |
|
| 1842 | + $file = $font['file']; |
|
| 1843 | + if ($file) |
|
| 1844 | + $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R'; |
|
| 1845 | 1845 | $this->_out($s.'>>'); |
| 1846 | 1846 | $this->_out('endobj'); |
| 1847 | 1847 | } |
| 1848 | 1848 | // TrueType embedded SUBSETS or FULL |
| 1849 | - else if ($type=='TTF') { |
|
| 1850 | - $this->fonts[$k]['n']=$this->n+1; |
|
| 1849 | + else if ($type == 'TTF') { |
|
| 1850 | + $this->fonts[$k]['n'] = $this->n + 1; |
|
| 1851 | 1851 | require_once($this->_getfontpath().'unifont/ttfonts.php'); |
| 1852 | 1852 | $ttf = new TTFontFile(); |
| 1853 | 1853 | $fontname = 'MPDFAA'.'+'.$font['name']; |
@@ -1879,7 +1879,7 @@ discard block |
||
| 1879 | 1879 | $this->_out('/BaseFont /'.$fontname.''); |
| 1880 | 1880 | $this->_out('/CIDSystemInfo '.($this->n + 2).' 0 R'); |
| 1881 | 1881 | $this->_out('/FontDescriptor '.($this->n + 3).' 0 R'); |
| 1882 | - if (isset($font['desc']['MissingWidth'])){ |
|
| 1882 | + if (isset($font['desc']['MissingWidth'])) { |
|
| 1883 | 1883 | $this->_out('/DW '.$font['desc']['MissingWidth'].''); |
| 1884 | 1884 | } |
| 1885 | 1885 | |
@@ -1927,7 +1927,7 @@ discard block |
||
| 1927 | 1927 | $this->_newobj(); |
| 1928 | 1928 | $this->_out('<</Type /FontDescriptor'); |
| 1929 | 1929 | $this->_out('/FontName /'.$fontname); |
| 1930 | - foreach($font['desc'] as $kd=>$v) { |
|
| 1930 | + foreach ($font['desc'] as $kd=>$v) { |
|
| 1931 | 1931 | if ($kd == 'Flags') { $v = $v | 4; $v = $v & ~32; } // SYMBOLIC font flag |
| 1932 | 1932 | $this->_out(' /'.$kd.' '.$v); |
| 1933 | 1933 | } |
@@ -1938,10 +1938,10 @@ discard block |
||
| 1938 | 1938 | // Embed CIDToGIDMap |
| 1939 | 1939 | // A specification of the mapping from CIDs to glyph indices |
| 1940 | 1940 | $cidtogidmap = ''; |
| 1941 | - $cidtogidmap = str_pad('', 256*256*2, "\x00"); |
|
| 1942 | - foreach($codeToGlyph as $cc=>$glyph) { |
|
| 1943 | - $cidtogidmap[$cc*2] = chr($glyph >> 8); |
|
| 1944 | - $cidtogidmap[$cc*2 + 1] = chr($glyph & 0xFF); |
|
| 1941 | + $cidtogidmap = str_pad('', 256 * 256 * 2, "\x00"); |
|
| 1942 | + foreach ($codeToGlyph as $cc=>$glyph) { |
|
| 1943 | + $cidtogidmap[$cc * 2] = chr($glyph >> 8); |
|
| 1944 | + $cidtogidmap[$cc * 2 + 1] = chr($glyph & 0xFF); |
|
| 1945 | 1945 | } |
| 1946 | 1946 | $cidtogidmap = gzcompress($cidtogidmap); |
| 1947 | 1947 | $this->_newobj(); |
@@ -1964,9 +1964,9 @@ discard block |
||
| 1964 | 1964 | else |
| 1965 | 1965 | { |
| 1966 | 1966 | // Allow for additional types |
| 1967 | - $this->fonts[$k]['n'] = $this->n+1; |
|
| 1968 | - $mtd='_put'.strtolower($type); |
|
| 1969 | - if(!method_exists($this,$mtd)) |
|
| 1967 | + $this->fonts[$k]['n'] = $this->n + 1; |
|
| 1968 | + $mtd = '_put'.strtolower($type); |
|
| 1969 | + if (!method_exists($this, $mtd)) |
|
| 1970 | 1970 | $this->Error('Unsupported font type: '.$type); |
| 1971 | 1971 | $this->$mtd($font); |
| 1972 | 1972 | } |
@@ -1975,7 +1975,7 @@ discard block |
||
| 1975 | 1975 | |
| 1976 | 1976 | function _putTTfontwidths(&$font, $maxUni) { |
| 1977 | 1977 | if (file_exists($font['unifilename'].'.cw127.php')) { |
| 1978 | - include($font['unifilename'].'.cw127.php') ; |
|
| 1978 | + include($font['unifilename'].'.cw127.php'); |
|
| 1979 | 1979 | $startcid = 128; |
| 1980 | 1980 | } |
| 1981 | 1981 | else { |
@@ -1989,24 +1989,24 @@ discard block |
||
| 1989 | 1989 | $cwlen = $maxUni + 1; |
| 1990 | 1990 | |
| 1991 | 1991 | // for each character |
| 1992 | - for ($cid=$startcid; $cid<$cwlen; $cid++) { |
|
| 1993 | - if ($cid==128 && (!file_exists($font['unifilename'].'.cw127.php'))) { |
|
| 1992 | + for ($cid = $startcid; $cid < $cwlen; $cid++) { |
|
| 1993 | + if ($cid == 128 && (!file_exists($font['unifilename'].'.cw127.php'))) { |
|
| 1994 | 1994 | if (is_writable(dirname($this->_getfontpath().'unifont/x'))) { |
| 1995 | - $fh = fopen($font['unifilename'].'.cw127.php',"wb"); |
|
| 1996 | - $cw127='<?php'."\n"; |
|
| 1997 | - $cw127.='$rangeid='.$rangeid.";\n"; |
|
| 1998 | - $cw127.='$prevcid='.$prevcid.";\n"; |
|
| 1999 | - $cw127.='$prevwidth='.$prevwidth.";\n"; |
|
| 2000 | - if ($interval) { $cw127.='$interval=true'.";\n"; } |
|
| 2001 | - else { $cw127.='$interval=false'.";\n"; } |
|
| 2002 | - $cw127.='$range='.var_export($range,true).";\n"; |
|
| 2003 | - $cw127.="?>"; |
|
| 2004 | - fwrite($fh,$cw127,strlen($cw127)); |
|
| 1995 | + $fh = fopen($font['unifilename'].'.cw127.php', "wb"); |
|
| 1996 | + $cw127 = '<?php'."\n"; |
|
| 1997 | + $cw127 .= '$rangeid='.$rangeid.";\n"; |
|
| 1998 | + $cw127 .= '$prevcid='.$prevcid.";\n"; |
|
| 1999 | + $cw127 .= '$prevwidth='.$prevwidth.";\n"; |
|
| 2000 | + if ($interval) { $cw127 .= '$interval=true'.";\n"; } |
|
| 2001 | + else { $cw127 .= '$interval=false'.";\n"; } |
|
| 2002 | + $cw127 .= '$range='.var_export($range, true).";\n"; |
|
| 2003 | + $cw127 .= "?>"; |
|
| 2004 | + fwrite($fh, $cw127, strlen($cw127)); |
|
| 2005 | 2005 | fclose($fh); |
| 2006 | 2006 | } |
| 2007 | 2007 | } |
| 2008 | - if ($font['cw'][$cid*2] == "\00" && $font['cw'][$cid*2+1] == "\00") { continue; } |
|
| 2009 | - $width = (ord($font['cw'][$cid*2]) << 8) + ord($font['cw'][$cid*2+1]); |
|
| 2008 | + if ($font['cw'][$cid * 2] == "\00" && $font['cw'][$cid * 2 + 1] == "\00") { continue; } |
|
| 2009 | + $width = (ord($font['cw'][$cid * 2]) << 8) + ord($font['cw'][$cid * 2 + 1]); |
|
| 2010 | 2010 | if ($width == 65535) { $width = 0; } |
| 2011 | 2011 | if ($cid > 255 && (!isset($font['subset'][$cid]) || !$font['subset'][$cid])) { continue; } |
| 2012 | 2012 | if (!isset($font['dw']) || (isset($font['dw']) && $width != $font['dw'])) { |
@@ -2068,14 +2068,14 @@ discard block |
||
| 2068 | 2068 | $w = ''; |
| 2069 | 2069 | foreach ($range as $k => $ws) { |
| 2070 | 2070 | if (count(array_count_values($ws)) == 1) { $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0]; } |
| 2071 | - else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]' . "\n"; } |
|
| 2071 | + else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]'."\n"; } |
|
| 2072 | 2072 | } |
| 2073 | 2073 | $this->_out('/W ['.$w.' ]'); |
| 2074 | 2074 | } |
| 2075 | 2075 | |
| 2076 | 2076 | function _putimages() |
| 2077 | 2077 | { |
| 2078 | - foreach(array_keys($this->images) as $file) |
|
| 2078 | + foreach (array_keys($this->images) as $file) |
|
| 2079 | 2079 | { |
| 2080 | 2080 | $this->_putimage($this->images[$file]); |
| 2081 | 2081 | unset($this->images[$file]['data']); |
@@ -2091,40 +2091,40 @@ discard block |
||
| 2091 | 2091 | $this->_out('/Subtype /Image'); |
| 2092 | 2092 | $this->_out('/Width '.$info['w']); |
| 2093 | 2093 | $this->_out('/Height '.$info['h']); |
| 2094 | - if($info['cs']=='Indexed') |
|
| 2095 | - $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]'); |
|
| 2094 | + if ($info['cs'] == 'Indexed') |
|
| 2095 | + $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal']) / 3 - 1).' '.($this->n + 1).' 0 R]'); |
|
| 2096 | 2096 | else |
| 2097 | 2097 | { |
| 2098 | 2098 | $this->_out('/ColorSpace /'.$info['cs']); |
| 2099 | - if($info['cs']=='DeviceCMYK') |
|
| 2099 | + if ($info['cs'] == 'DeviceCMYK') |
|
| 2100 | 2100 | $this->_out('/Decode [1 0 1 0 1 0 1 0]'); |
| 2101 | 2101 | } |
| 2102 | 2102 | $this->_out('/BitsPerComponent '.$info['bpc']); |
| 2103 | - if(isset($info['f'])) |
|
| 2103 | + if (isset($info['f'])) |
|
| 2104 | 2104 | $this->_out('/Filter /'.$info['f']); |
| 2105 | - if(isset($info['dp'])) |
|
| 2105 | + if (isset($info['dp'])) |
|
| 2106 | 2106 | $this->_out('/DecodeParms <<'.$info['dp'].'>>'); |
| 2107 | - if(isset($info['trns']) && is_array($info['trns'])) |
|
| 2107 | + if (isset($info['trns']) && is_array($info['trns'])) |
|
| 2108 | 2108 | { |
| 2109 | 2109 | $trns = ''; |
| 2110 | - for($i=0;$i<count($info['trns']);$i++) |
|
| 2110 | + for ($i = 0; $i < count($info['trns']); $i++) |
|
| 2111 | 2111 | $trns .= $info['trns'][$i].' '.$info['trns'][$i].' '; |
| 2112 | 2112 | $this->_out('/Mask ['.$trns.']'); |
| 2113 | 2113 | } |
| 2114 | - if(isset($info['smask'])) |
|
| 2115 | - $this->_out('/SMask '.($this->n+1).' 0 R'); |
|
| 2114 | + if (isset($info['smask'])) |
|
| 2115 | + $this->_out('/SMask '.($this->n + 1).' 0 R'); |
|
| 2116 | 2116 | $this->_out('/Length '.strlen($info['data']).'>>'); |
| 2117 | 2117 | $this->_putstream($info['data']); |
| 2118 | 2118 | $this->_out('endobj'); |
| 2119 | 2119 | // Soft mask |
| 2120 | - if(isset($info['smask'])) |
|
| 2120 | + if (isset($info['smask'])) |
|
| 2121 | 2121 | { |
| 2122 | 2122 | $dp = '/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns '.$info['w']; |
| 2123 | 2123 | $smask = array('w'=>$info['w'], 'h'=>$info['h'], 'cs'=>'DeviceGray', 'bpc'=>8, 'f'=>$info['f'], 'dp'=>$dp, 'data'=>$info['smask']); |
| 2124 | 2124 | $this->_putimage($smask); |
| 2125 | 2125 | } |
| 2126 | 2126 | // Palette |
| 2127 | - if($info['cs']=='Indexed') |
|
| 2127 | + if ($info['cs'] == 'Indexed') |
|
| 2128 | 2128 | { |
| 2129 | 2129 | $filter = ($this->compress) ? '/Filter /FlateDecode ' : ''; |
| 2130 | 2130 | $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal']; |
@@ -2137,7 +2137,7 @@ discard block |
||
| 2137 | 2137 | |
| 2138 | 2138 | function _putxobjectdict() |
| 2139 | 2139 | { |
| 2140 | - foreach($this->images as $image) |
|
| 2140 | + foreach ($this->images as $image) |
|
| 2141 | 2141 | $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R'); |
| 2142 | 2142 | } |
| 2143 | 2143 | |
@@ -2145,7 +2145,7 @@ discard block |
||
| 2145 | 2145 | { |
| 2146 | 2146 | $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); |
| 2147 | 2147 | $this->_out('/Font <<'); |
| 2148 | - foreach($this->fonts as $font) { |
|
| 2148 | + foreach ($this->fonts as $font) { |
|
| 2149 | 2149 | $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R'); |
| 2150 | 2150 | } |
| 2151 | 2151 | $this->_out('>>'); |
@@ -2170,15 +2170,15 @@ discard block |
||
| 2170 | 2170 | function _putinfo() |
| 2171 | 2171 | { |
| 2172 | 2172 | $this->_out('/Producer '.$this->_textstring('tFPDF '.tFPDF_VERSION)); |
| 2173 | - if(!empty($this->title)) |
|
| 2173 | + if (!empty($this->title)) |
|
| 2174 | 2174 | $this->_out('/Title '.$this->_textstring($this->title)); |
| 2175 | - if(!empty($this->subject)) |
|
| 2175 | + if (!empty($this->subject)) |
|
| 2176 | 2176 | $this->_out('/Subject '.$this->_textstring($this->subject)); |
| 2177 | - if(!empty($this->author)) |
|
| 2177 | + if (!empty($this->author)) |
|
| 2178 | 2178 | $this->_out('/Author '.$this->_textstring($this->author)); |
| 2179 | - if(!empty($this->keywords)) |
|
| 2179 | + if (!empty($this->keywords)) |
|
| 2180 | 2180 | $this->_out('/Keywords '.$this->_textstring($this->keywords)); |
| 2181 | - if(!empty($this->creator)) |
|
| 2181 | + if (!empty($this->creator)) |
|
| 2182 | 2182 | $this->_out('/Creator '.$this->_textstring($this->creator)); |
| 2183 | 2183 | $this->_out('/CreationDate '.$this->_textstring('D:'.@date('YmdHis'))); |
| 2184 | 2184 | } |
@@ -2187,19 +2187,19 @@ discard block |
||
| 2187 | 2187 | { |
| 2188 | 2188 | $this->_out('/Type /Catalog'); |
| 2189 | 2189 | $this->_out('/Pages 1 0 R'); |
| 2190 | - if($this->ZoomMode=='fullpage') |
|
| 2190 | + if ($this->ZoomMode == 'fullpage') |
|
| 2191 | 2191 | $this->_out('/OpenAction [3 0 R /Fit]'); |
| 2192 | - elseif($this->ZoomMode=='fullwidth') |
|
| 2192 | + elseif ($this->ZoomMode == 'fullwidth') |
|
| 2193 | 2193 | $this->_out('/OpenAction [3 0 R /FitH null]'); |
| 2194 | - elseif($this->ZoomMode=='real') |
|
| 2194 | + elseif ($this->ZoomMode == 'real') |
|
| 2195 | 2195 | $this->_out('/OpenAction [3 0 R /XYZ null null 1]'); |
| 2196 | - elseif(!is_string($this->ZoomMode)) |
|
| 2197 | - $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']'); |
|
| 2198 | - if($this->LayoutMode=='single') |
|
| 2196 | + elseif (!is_string($this->ZoomMode)) |
|
| 2197 | + $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F', $this->ZoomMode / 100).']'); |
|
| 2198 | + if ($this->LayoutMode == 'single') |
|
| 2199 | 2199 | $this->_out('/PageLayout /SinglePage'); |
| 2200 | - elseif($this->LayoutMode=='continuous') |
|
| 2200 | + elseif ($this->LayoutMode == 'continuous') |
|
| 2201 | 2201 | $this->_out('/PageLayout /OneColumn'); |
| 2202 | - elseif($this->LayoutMode=='two') |
|
| 2202 | + elseif ($this->LayoutMode == 'two') |
|
| 2203 | 2203 | $this->_out('/PageLayout /TwoColumnLeft'); |
| 2204 | 2204 | } |
| 2205 | 2205 | |
@@ -2210,9 +2210,9 @@ discard block |
||
| 2210 | 2210 | |
| 2211 | 2211 | function _puttrailer() |
| 2212 | 2212 | { |
| 2213 | - $this->_out('/Size '.($this->n+1)); |
|
| 2213 | + $this->_out('/Size '.($this->n + 1)); |
|
| 2214 | 2214 | $this->_out('/Root '.$this->n.' 0 R'); |
| 2215 | - $this->_out('/Info '.($this->n-1).' 0 R'); |
|
| 2215 | + $this->_out('/Info '.($this->n - 1).' 0 R'); |
|
| 2216 | 2216 | } |
| 2217 | 2217 | |
| 2218 | 2218 | function _enddoc() |
@@ -2235,10 +2235,10 @@ discard block |
||
| 2235 | 2235 | // Cross-ref |
| 2236 | 2236 | $o = strlen($this->buffer); |
| 2237 | 2237 | $this->_out('xref'); |
| 2238 | - $this->_out('0 '.($this->n+1)); |
|
| 2238 | + $this->_out('0 '.($this->n + 1)); |
|
| 2239 | 2239 | $this->_out('0000000000 65535 f '); |
| 2240 | - for($i=1;$i<=$this->n;$i++) |
|
| 2241 | - $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i])); |
|
| 2240 | + for ($i = 1; $i <= $this->n; $i++) |
|
| 2241 | + $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i])); |
|
| 2242 | 2242 | // Trailer |
| 2243 | 2243 | $this->_out('trailer'); |
| 2244 | 2244 | $this->_out('<<'); |
@@ -2252,7 +2252,7 @@ discard block |
||
| 2252 | 2252 | |
| 2253 | 2253 | // ********* NEW FUNCTIONS ********* |
| 2254 | 2254 | // Converts UTF-8 strings to UTF16-BE. |
| 2255 | -function UTF8ToUTF16BE($str, $setbom=true) { |
|
| 2255 | +function UTF8ToUTF16BE($str, $setbom = true) { |
|
| 2256 | 2256 | $outstr = ""; |
| 2257 | 2257 | if ($setbom) { |
| 2258 | 2258 | $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM) |
@@ -2268,15 +2268,15 @@ discard block |
||
| 2268 | 2268 | for ($i = 0; $i < $len; $i++) { |
| 2269 | 2269 | $uni = -1; |
| 2270 | 2270 | $h = ord($str[$i]); |
| 2271 | - if ( $h <= 0x7F ) |
|
| 2271 | + if ($h <= 0x7F) |
|
| 2272 | 2272 | $uni = $h; |
| 2273 | - elseif ( $h >= 0xC2 ) { |
|
| 2274 | - if ( ($h <= 0xDF) && ($i < $len -1) ) |
|
| 2273 | + elseif ($h >= 0xC2) { |
|
| 2274 | + if (($h <= 0xDF) && ($i < $len - 1)) |
|
| 2275 | 2275 | $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F); |
| 2276 | - elseif ( ($h <= 0xEF) && ($i < $len -2) ) |
|
| 2276 | + elseif (($h <= 0xEF) && ($i < $len - 2)) |
|
| 2277 | 2277 | $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6 |
| 2278 | 2278 | | (ord($str[++$i]) & 0x3F); |
| 2279 | - elseif ( ($h <= 0xF4) && ($i < $len -3) ) |
|
| 2279 | + elseif (($h <= 0xF4) && ($i < $len - 3)) |
|
| 2280 | 2280 | $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12 |
| 2281 | 2281 | | (ord($str[++$i]) & 0x3F) << 6 |
| 2282 | 2282 | | (ord($str[++$i]) & 0x3F); |
@@ -2293,7 +2293,7 @@ discard block |
||
| 2293 | 2293 | } |
| 2294 | 2294 | |
| 2295 | 2295 | // Handle special IE contype request |
| 2296 | -if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT']=='contype') |
|
| 2296 | +if (isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] == 'contype') |
|
| 2297 | 2297 | { |
| 2298 | 2298 | header('Content-Type: application/pdf'); |
| 2299 | 2299 | exit; |
@@ -108,26 +108,28 @@ discard block |
||
| 108 | 108 | if(defined('FPDF_FONTPATH')) |
| 109 | 109 | { |
| 110 | 110 | $this->fontpath = FPDF_FONTPATH; |
| 111 | - if(substr($this->fontpath,-1)!='/' && substr($this->fontpath,-1)!='\\') |
|
| 112 | - $this->fontpath .= '/'; |
|
| 111 | + if(substr($this->fontpath,-1)!='/' && substr($this->fontpath,-1)!='\\') { |
|
| 112 | + $this->fontpath .= '/'; |
|
| 113 | + } |
|
| 114 | + } elseif(is_dir(dirname(__FILE__).'/font')) { |
|
| 115 | + $this->fontpath = dirname(__FILE__).'/font/'; |
|
| 116 | + } else { |
|
| 117 | + $this->fontpath = ''; |
|
| 113 | 118 | } |
| 114 | - elseif(is_dir(dirname(__FILE__).'/font')) |
|
| 115 | - $this->fontpath = dirname(__FILE__).'/font/'; |
|
| 116 | - else |
|
| 117 | - $this->fontpath = ''; |
|
| 118 | 119 | // Core fonts |
| 119 | 120 | $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats'); |
| 120 | 121 | // Scale factor |
| 121 | - if($unit=='pt') |
|
| 122 | - $this->k = 1; |
|
| 123 | - elseif($unit=='mm') |
|
| 124 | - $this->k = 72/25.4; |
|
| 125 | - elseif($unit=='cm') |
|
| 126 | - $this->k = 72/2.54; |
|
| 127 | - elseif($unit=='in') |
|
| 128 | - $this->k = 72; |
|
| 129 | - else |
|
| 130 | - $this->Error('Incorrect unit: '.$unit); |
|
| 122 | + if($unit=='pt') { |
|
| 123 | + $this->k = 1; |
|
| 124 | + } elseif($unit=='mm') { |
|
| 125 | + $this->k = 72/25.4; |
|
| 126 | + } elseif($unit=='cm') { |
|
| 127 | + $this->k = 72/2.54; |
|
| 128 | + } elseif($unit=='in') { |
|
| 129 | + $this->k = 72; |
|
| 130 | + } else { |
|
| 131 | + $this->Error('Incorrect unit: '.$unit); |
|
| 132 | + } |
|
| 131 | 133 | // Page sizes |
| 132 | 134 | $this->StdPageSizes = array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28), |
| 133 | 135 | 'letter'=>array(612,792), 'legal'=>array(612,1008)); |
@@ -141,15 +143,14 @@ discard block |
||
| 141 | 143 | $this->DefOrientation = 'P'; |
| 142 | 144 | $this->w = $size[0]; |
| 143 | 145 | $this->h = $size[1]; |
| 144 | - } |
|
| 145 | - elseif($orientation=='l' || $orientation=='landscape') |
|
| 146 | + } elseif($orientation=='l' || $orientation=='landscape') |
|
| 146 | 147 | { |
| 147 | 148 | $this->DefOrientation = 'L'; |
| 148 | 149 | $this->w = $size[1]; |
| 149 | 150 | $this->h = $size[0]; |
| 151 | + } else { |
|
| 152 | + $this->Error('Incorrect orientation: '.$orientation); |
|
| 150 | 153 | } |
| 151 | - else |
|
| 152 | - $this->Error('Incorrect orientation: '.$orientation); |
|
| 153 | 154 | $this->CurOrientation = $this->DefOrientation; |
| 154 | 155 | $this->wPt = $this->w*$this->k; |
| 155 | 156 | $this->hPt = $this->h*$this->k; |
@@ -175,8 +176,9 @@ discard block |
||
| 175 | 176 | // Set left, top and right margins |
| 176 | 177 | $this->lMargin = $left; |
| 177 | 178 | $this->tMargin = $top; |
| 178 | - if($right===null) |
|
| 179 | - $right = $left; |
|
| 179 | + if($right===null) { |
|
| 180 | + $right = $left; |
|
| 181 | + } |
|
| 180 | 182 | $this->rMargin = $right; |
| 181 | 183 | } |
| 182 | 184 | |
@@ -184,9 +186,10 @@ discard block |
||
| 184 | 186 | { |
| 185 | 187 | // Set left margin |
| 186 | 188 | $this->lMargin = $margin; |
| 187 | - if($this->page>0 && $this->x<$margin) |
|
| 188 | - $this->x = $margin; |
|
| 189 | -} |
|
| 189 | + if($this->page>0 && $this->x<$margin) { |
|
| 190 | + $this->x = $margin; |
|
| 191 | + } |
|
| 192 | + } |
|
| 190 | 193 | |
| 191 | 194 | function SetTopMargin($margin) |
| 192 | 195 | { |
@@ -211,62 +214,70 @@ discard block |
||
| 211 | 214 | function SetDisplayMode($zoom, $layout='default') |
| 212 | 215 | { |
| 213 | 216 | // Set display mode in viewer |
| 214 | - if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) |
|
| 215 | - $this->ZoomMode = $zoom; |
|
| 216 | - else |
|
| 217 | - $this->Error('Incorrect zoom display mode: '.$zoom); |
|
| 218 | - if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') |
|
| 219 | - $this->LayoutMode = $layout; |
|
| 220 | - else |
|
| 221 | - $this->Error('Incorrect layout display mode: '.$layout); |
|
| 222 | -} |
|
| 217 | + if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom)) { |
|
| 218 | + $this->ZoomMode = $zoom; |
|
| 219 | + } else { |
|
| 220 | + $this->Error('Incorrect zoom display mode: '.$zoom); |
|
| 221 | + } |
|
| 222 | + if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default') { |
|
| 223 | + $this->LayoutMode = $layout; |
|
| 224 | + } else { |
|
| 225 | + $this->Error('Incorrect layout display mode: '.$layout); |
|
| 226 | + } |
|
| 227 | + } |
|
| 223 | 228 | |
| 224 | 229 | function SetCompression($compress) |
| 225 | 230 | { |
| 226 | 231 | // Set page compression |
| 227 | - if(function_exists('gzcompress')) |
|
| 228 | - $this->compress = $compress; |
|
| 229 | - else |
|
| 230 | - $this->compress = false; |
|
| 231 | -} |
|
| 232 | + if(function_exists('gzcompress')) { |
|
| 233 | + $this->compress = $compress; |
|
| 234 | + } else { |
|
| 235 | + $this->compress = false; |
|
| 236 | + } |
|
| 237 | + } |
|
| 232 | 238 | |
| 233 | 239 | function SetTitle($title, $isUTF8=false) |
| 234 | 240 | { |
| 235 | 241 | // Title of document |
| 236 | - if($isUTF8) |
|
| 237 | - $title = $this->_UTF8toUTF16($title); |
|
| 242 | + if($isUTF8) { |
|
| 243 | + $title = $this->_UTF8toUTF16($title); |
|
| 244 | + } |
|
| 238 | 245 | $this->title = $title; |
| 239 | 246 | } |
| 240 | 247 | |
| 241 | 248 | function SetSubject($subject, $isUTF8=false) |
| 242 | 249 | { |
| 243 | 250 | // Subject of document |
| 244 | - if($isUTF8) |
|
| 245 | - $subject = $this->_UTF8toUTF16($subject); |
|
| 251 | + if($isUTF8) { |
|
| 252 | + $subject = $this->_UTF8toUTF16($subject); |
|
| 253 | + } |
|
| 246 | 254 | $this->subject = $subject; |
| 247 | 255 | } |
| 248 | 256 | |
| 249 | 257 | function SetAuthor($author, $isUTF8=false) |
| 250 | 258 | { |
| 251 | 259 | // Author of document |
| 252 | - if($isUTF8) |
|
| 253 | - $author = $this->_UTF8toUTF16($author); |
|
| 260 | + if($isUTF8) { |
|
| 261 | + $author = $this->_UTF8toUTF16($author); |
|
| 262 | + } |
|
| 254 | 263 | $this->author = $author; |
| 255 | 264 | } |
| 256 | 265 | |
| 257 | 266 | function SetKeywords($keywords, $isUTF8=false) |
| 258 | 267 | { |
| 259 | 268 | // Keywords of document |
| 260 | - if($isUTF8) |
|
| 261 | - $keywords = $this->_UTF8toUTF16($keywords); |
|
| 269 | + if($isUTF8) { |
|
| 270 | + $keywords = $this->_UTF8toUTF16($keywords); |
|
| 271 | + } |
|
| 262 | 272 | $this->keywords = $keywords; |
| 263 | 273 | } |
| 264 | 274 | |
| 265 | 275 | function SetCreator($creator, $isUTF8=false) |
| 266 | 276 | { |
| 267 | 277 | // Creator of document |
| 268 | - if($isUTF8) |
|
| 269 | - $creator = $this->_UTF8toUTF16($creator); |
|
| 278 | + if($isUTF8) { |
|
| 279 | + $creator = $this->_UTF8toUTF16($creator); |
|
| 280 | + } |
|
| 270 | 281 | $this->creator = $creator; |
| 271 | 282 | } |
| 272 | 283 | |
@@ -291,10 +302,12 @@ discard block |
||
| 291 | 302 | function Close() |
| 292 | 303 | { |
| 293 | 304 | // Terminate document |
| 294 | - if($this->state==3) |
|
| 295 | - return; |
|
| 296 | - if($this->page==0) |
|
| 297 | - $this->AddPage(); |
|
| 305 | + if($this->state==3) { |
|
| 306 | + return; |
|
| 307 | + } |
|
| 308 | + if($this->page==0) { |
|
| 309 | + $this->AddPage(); |
|
| 310 | + } |
|
| 298 | 311 | // Page footer |
| 299 | 312 | $this->InFooter = true; |
| 300 | 313 | $this->Footer(); |
@@ -308,8 +321,9 @@ discard block |
||
| 308 | 321 | function AddPage($orientation='', $size='') |
| 309 | 322 | { |
| 310 | 323 | // Start a new page |
| 311 | - if($this->state==0) |
|
| 312 | - $this->Open(); |
|
| 324 | + if($this->state==0) { |
|
| 325 | + $this->Open(); |
|
| 326 | + } |
|
| 313 | 327 | $family = $this->FontFamily; |
| 314 | 328 | $style = $this->FontStyle.($this->underline ? 'U' : ''); |
| 315 | 329 | $fontsize = $this->FontSizePt; |
@@ -335,15 +349,18 @@ discard block |
||
| 335 | 349 | $this->LineWidth = $lw; |
| 336 | 350 | $this->_out(sprintf('%.2F w',$lw*$this->k)); |
| 337 | 351 | // Set font |
| 338 | - if($family) |
|
| 339 | - $this->SetFont($family,$style,$fontsize); |
|
| 352 | + if($family) { |
|
| 353 | + $this->SetFont($family,$style,$fontsize); |
|
| 354 | + } |
|
| 340 | 355 | // Set colors |
| 341 | 356 | $this->DrawColor = $dc; |
| 342 | - if($dc!='0 G') |
|
| 343 | - $this->_out($dc); |
|
| 357 | + if($dc!='0 G') { |
|
| 358 | + $this->_out($dc); |
|
| 359 | + } |
|
| 344 | 360 | $this->FillColor = $fc; |
| 345 | - if($fc!='0 g') |
|
| 346 | - $this->_out($fc); |
|
| 361 | + if($fc!='0 g') { |
|
| 362 | + $this->_out($fc); |
|
| 363 | + } |
|
| 347 | 364 | $this->TextColor = $tc; |
| 348 | 365 | $this->ColorFlag = $cf; |
| 349 | 366 | // Page header |
@@ -357,8 +374,9 @@ discard block |
||
| 357 | 374 | $this->_out(sprintf('%.2F w',$lw*$this->k)); |
| 358 | 375 | } |
| 359 | 376 | // Restore font |
| 360 | - if($family) |
|
| 361 | - $this->SetFont($family,$style,$fontsize); |
|
| 377 | + if($family) { |
|
| 378 | + $this->SetFont($family,$style,$fontsize); |
|
| 379 | + } |
|
| 362 | 380 | // Restore colors |
| 363 | 381 | if($this->DrawColor!=$dc) |
| 364 | 382 | { |
@@ -393,33 +411,38 @@ discard block |
||
| 393 | 411 | function SetDrawColor($r, $g=null, $b=null) |
| 394 | 412 | { |
| 395 | 413 | // Set color for all stroking operations |
| 396 | - if(($r==0 && $g==0 && $b==0) || $g===null) |
|
| 397 | - $this->DrawColor = sprintf('%.3F G',$r/255); |
|
| 398 | - else |
|
| 399 | - $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255); |
|
| 400 | - if($this->page>0) |
|
| 401 | - $this->_out($this->DrawColor); |
|
| 402 | -} |
|
| 414 | + if(($r==0 && $g==0 && $b==0) || $g===null) { |
|
| 415 | + $this->DrawColor = sprintf('%.3F G',$r/255); |
|
| 416 | + } else { |
|
| 417 | + $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255); |
|
| 418 | + } |
|
| 419 | + if($this->page>0) { |
|
| 420 | + $this->_out($this->DrawColor); |
|
| 421 | + } |
|
| 422 | + } |
|
| 403 | 423 | |
| 404 | 424 | function SetFillColor($r, $g=null, $b=null) |
| 405 | 425 | { |
| 406 | 426 | // Set color for all filling operations |
| 407 | - if(($r==0 && $g==0 && $b==0) || $g===null) |
|
| 408 | - $this->FillColor = sprintf('%.3F g',$r/255); |
|
| 409 | - else |
|
| 410 | - $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); |
|
| 427 | + if(($r==0 && $g==0 && $b==0) || $g===null) { |
|
| 428 | + $this->FillColor = sprintf('%.3F g',$r/255); |
|
| 429 | + } else { |
|
| 430 | + $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); |
|
| 431 | + } |
|
| 411 | 432 | $this->ColorFlag = ($this->FillColor!=$this->TextColor); |
| 412 | - if($this->page>0) |
|
| 413 | - $this->_out($this->FillColor); |
|
| 414 | -} |
|
| 433 | + if($this->page>0) { |
|
| 434 | + $this->_out($this->FillColor); |
|
| 435 | + } |
|
| 436 | + } |
|
| 415 | 437 | |
| 416 | 438 | function SetTextColor($r, $g=null, $b=null) |
| 417 | 439 | { |
| 418 | 440 | // Set color for text |
| 419 | - if(($r==0 && $g==0 && $b==0) || $g===null) |
|
| 420 | - $this->TextColor = sprintf('%.3F g',$r/255); |
|
| 421 | - else |
|
| 422 | - $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); |
|
| 441 | + if(($r==0 && $g==0 && $b==0) || $g===null) { |
|
| 442 | + $this->TextColor = sprintf('%.3F g',$r/255); |
|
| 443 | + } else { |
|
| 444 | + $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255); |
|
| 445 | + } |
|
| 423 | 446 | $this->ColorFlag = ($this->FillColor!=$this->TextColor); |
| 424 | 447 | } |
| 425 | 448 | |
@@ -432,17 +455,13 @@ discard block |
||
| 432 | 455 | if ($this->unifontSubset) { |
| 433 | 456 | $unicode = $this->UTF8StringToArray($s); |
| 434 | 457 | foreach($unicode as $char) { |
| 435 | - if (isset($cw[$char])) { $w += (ord($cw[2*$char])<<8) + ord($cw[2*$char+1]); } |
|
| 436 | - else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w += $cw[chr($char)]; } |
|
| 437 | - else if(isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; } |
|
| 438 | - else if(isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; } |
|
| 439 | - else { $w += 500; } |
|
| 458 | + 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; } |
|
| 440 | 459 | } |
| 441 | - } |
|
| 442 | - else { |
|
| 460 | + } else { |
|
| 443 | 461 | $l = strlen($s); |
| 444 | - for($i=0;$i<$l;$i++) |
|
| 445 | - $w += $cw[$s[$i]]; |
|
| 462 | + for($i=0;$i<$l;$i++) { |
|
| 463 | + $w += $cw[$s[$i]]; |
|
| 464 | + } |
|
| 446 | 465 | } |
| 447 | 466 | return $w*$this->FontSize/1000; |
| 448 | 467 | } |
@@ -451,9 +470,10 @@ discard block |
||
| 451 | 470 | { |
| 452 | 471 | // Set line width |
| 453 | 472 | $this->LineWidth = $width; |
| 454 | - if($this->page>0) |
|
| 455 | - $this->_out(sprintf('%.2F w',$width*$this->k)); |
|
| 456 | -} |
|
| 473 | + if($this->page>0) { |
|
| 474 | + $this->_out(sprintf('%.2F w',$width*$this->k)); |
|
| 475 | + } |
|
| 476 | + } |
|
| 457 | 477 | |
| 458 | 478 | function Line($x1, $y1, $x2, $y2) |
| 459 | 479 | { |
@@ -464,12 +484,13 @@ discard block |
||
| 464 | 484 | function Rect($x, $y, $w, $h, $style='') |
| 465 | 485 | { |
| 466 | 486 | // Draw a rectangle |
| 467 | - if($style=='F') |
|
| 468 | - $op = 'f'; |
|
| 469 | - elseif($style=='FD' || $style=='DF') |
|
| 470 | - $op = 'B'; |
|
| 471 | - else |
|
| 472 | - $op = 'S'; |
|
| 487 | + if($style=='F') { |
|
| 488 | + $op = 'f'; |
|
| 489 | + } elseif($style=='FD' || $style=='DF') { |
|
| 490 | + $op = 'B'; |
|
| 491 | + } else { |
|
| 492 | + $op = 'S'; |
|
| 493 | + } |
|
| 473 | 494 | $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op)); |
| 474 | 495 | } |
| 475 | 496 | |
@@ -478,23 +499,23 @@ discard block |
||
| 478 | 499 | // Add a TrueType, OpenType or Type1 font |
| 479 | 500 | $family = strtolower($family); |
| 480 | 501 | $style = strtoupper($style); |
| 481 | - if($style=='IB') |
|
| 482 | - $style='BI'; |
|
| 502 | + if($style=='IB') { |
|
| 503 | + $style='BI'; |
|
| 504 | + } |
|
| 483 | 505 | if($file=='') { |
| 484 | 506 | if ($uni) { |
| 485 | 507 | $file = str_replace(' ','',$family).strtolower($style).'.ttf'; |
| 486 | - } |
|
| 487 | - else { |
|
| 508 | + } else { |
|
| 488 | 509 | $file = str_replace(' ','',$family).strtolower($style).'.php'; |
| 489 | 510 | } |
| 490 | 511 | } |
| 491 | 512 | $fontkey = $family.$style; |
| 492 | - if(isset($this->fonts[$fontkey])) |
|
| 493 | - return; |
|
| 513 | + if(isset($this->fonts[$fontkey])) { |
|
| 514 | + return; |
|
| 515 | + } |
|
| 494 | 516 | |
| 495 | 517 | if ($uni) { |
| 496 | - if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; } |
|
| 497 | - else { $ttffilename = $this->_getfontpath().'unifont/'.$file ; } |
|
| 518 | + if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; } else { $ttffilename = $this->_getfontpath().'unifont/'.$file ; } |
|
| 498 | 519 | $unifilename = $this->_getfontpath().'unifont/'.strtolower(substr($file ,0,(strpos($file ,'.')))); |
| 499 | 520 | $name = ''; |
| 500 | 521 | $originalsize = 0; |
@@ -543,22 +564,21 @@ discard block |
||
| 543 | 564 | @unlink($unifilename.'.cw127.php'); |
| 544 | 565 | } |
| 545 | 566 | unset($ttf); |
| 546 | - } |
|
| 547 | - else { |
|
| 567 | + } else { |
|
| 548 | 568 | $cw = @file_get_contents($unifilename.'.cw.dat'); |
| 549 | 569 | } |
| 550 | 570 | $i = count($this->fonts)+1; |
| 551 | - if(!empty($this->AliasNbPages)) |
|
| 552 | - $sbarr = range(0,57); |
|
| 553 | - else |
|
| 554 | - $sbarr = range(0,32); |
|
| 571 | + if(!empty($this->AliasNbPages)) { |
|
| 572 | + $sbarr = range(0,57); |
|
| 573 | + } else { |
|
| 574 | + $sbarr = range(0,32); |
|
| 575 | + } |
|
| 555 | 576 | $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'ttffile'=>$ttffile, 'fontkey'=>$fontkey, 'subset'=>$sbarr, 'unifilename'=>$unifilename); |
| 556 | 577 | |
| 557 | 578 | $this->FontFiles[$fontkey]=array('length1'=>$originalsize, 'type'=>"TTF", 'ttffile'=>$ttffile); |
| 558 | 579 | $this->FontFiles[$file]=array('type'=>"TTF"); |
| 559 | 580 | unset($cw); |
| 560 | - } |
|
| 561 | - else { |
|
| 581 | + } else { |
|
| 562 | 582 | $info = $this->_loadfont($file); |
| 563 | 583 | $info['i'] = count($this->fonts)+1; |
| 564 | 584 | if(!empty($info['diff'])) |
@@ -575,10 +595,11 @@ discard block |
||
| 575 | 595 | if(!empty($info['file'])) |
| 576 | 596 | { |
| 577 | 597 | // Embedded font |
| 578 | - if($info['type']=='TrueType') |
|
| 579 | - $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']); |
|
| 580 | - else |
|
| 581 | - $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']); |
|
| 598 | + if($info['type']=='TrueType') { |
|
| 599 | + $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']); |
|
| 600 | + } else { |
|
| 601 | + $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']); |
|
| 602 | + } |
|
| 582 | 603 | } |
| 583 | 604 | $this->fonts[$fontkey] = $info; |
| 584 | 605 | } |
@@ -587,42 +608,49 @@ discard block |
||
| 587 | 608 | function SetFont($family, $style='', $size=0) |
| 588 | 609 | { |
| 589 | 610 | // Select a font; size given in points |
| 590 | - if($family=='') |
|
| 591 | - $family = $this->FontFamily; |
|
| 592 | - else |
|
| 593 | - $family = strtolower($family); |
|
| 611 | + if($family=='') { |
|
| 612 | + $family = $this->FontFamily; |
|
| 613 | + } else { |
|
| 614 | + $family = strtolower($family); |
|
| 615 | + } |
|
| 594 | 616 | $style = strtoupper($style); |
| 595 | 617 | if(strpos($style,'U')!==false) |
| 596 | 618 | { |
| 597 | 619 | $this->underline = true; |
| 598 | 620 | $style = str_replace('U','',$style); |
| 621 | + } else { |
|
| 622 | + $this->underline = false; |
|
| 623 | + } |
|
| 624 | + if($style=='IB') { |
|
| 625 | + $style = 'BI'; |
|
| 626 | + } |
|
| 627 | + if($size==0) { |
|
| 628 | + $size = $this->FontSizePt; |
|
| 599 | 629 | } |
| 600 | - else |
|
| 601 | - $this->underline = false; |
|
| 602 | - if($style=='IB') |
|
| 603 | - $style = 'BI'; |
|
| 604 | - if($size==0) |
|
| 605 | - $size = $this->FontSizePt; |
|
| 606 | 630 | // Test if font is already selected |
| 607 | - if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size) |
|
| 608 | - return; |
|
| 631 | + if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size) { |
|
| 632 | + return; |
|
| 633 | + } |
|
| 609 | 634 | // Test if font is already loaded |
| 610 | 635 | $fontkey = $family.$style; |
| 611 | 636 | if(!isset($this->fonts[$fontkey])) |
| 612 | 637 | { |
| 613 | 638 | // Test if one of the core fonts |
| 614 | - if($family=='arial') |
|
| 615 | - $family = 'helvetica'; |
|
| 639 | + if($family=='arial') { |
|
| 640 | + $family = 'helvetica'; |
|
| 641 | + } |
|
| 616 | 642 | if(in_array($family,$this->CoreFonts)) |
| 617 | 643 | { |
| 618 | - if($family=='symbol' || $family=='zapfdingbats') |
|
| 619 | - $style = ''; |
|
| 644 | + if($family=='symbol' || $family=='zapfdingbats') { |
|
| 645 | + $style = ''; |
|
| 646 | + } |
|
| 620 | 647 | $fontkey = $family.$style; |
| 621 | - if(!isset($this->fonts[$fontkey])) |
|
| 622 | - $this->AddFont($family,$style); |
|
| 648 | + if(!isset($this->fonts[$fontkey])) { |
|
| 649 | + $this->AddFont($family,$style); |
|
| 650 | + } |
|
| 651 | + } else { |
|
| 652 | + $this->Error('Undefined font: '.$family.' '.$style); |
|
| 623 | 653 | } |
| 624 | - else |
|
| 625 | - $this->Error('Undefined font: '.$family.' '.$style); |
|
| 626 | 654 | } |
| 627 | 655 | // Select it |
| 628 | 656 | $this->FontFamily = $family; |
@@ -630,22 +658,24 @@ discard block |
||
| 630 | 658 | $this->FontSizePt = $size; |
| 631 | 659 | $this->FontSize = $size/$this->k; |
| 632 | 660 | $this->CurrentFont = &$this->fonts[$fontkey]; |
| 633 | - if ($this->fonts[$fontkey]['type']=='TTF') { $this->unifontSubset = true; } |
|
| 634 | - else { $this->unifontSubset = false; } |
|
| 635 | - if($this->page>0) |
|
| 636 | - $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); |
|
| 637 | -} |
|
| 661 | + if ($this->fonts[$fontkey]['type']=='TTF') { $this->unifontSubset = true; } else { $this->unifontSubset = false; } |
|
| 662 | + if($this->page>0) { |
|
| 663 | + $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); |
|
| 664 | + } |
|
| 665 | + } |
|
| 638 | 666 | |
| 639 | 667 | function SetFontSize($size) |
| 640 | 668 | { |
| 641 | 669 | // Set font size in points |
| 642 | - if($this->FontSizePt==$size) |
|
| 643 | - return; |
|
| 670 | + if($this->FontSizePt==$size) { |
|
| 671 | + return; |
|
| 672 | + } |
|
| 644 | 673 | $this->FontSizePt = $size; |
| 645 | 674 | $this->FontSize = $size/$this->k; |
| 646 | - if($this->page>0) |
|
| 647 | - $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); |
|
| 648 | -} |
|
| 675 | + if($this->page>0) { |
|
| 676 | + $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt)); |
|
| 677 | + } |
|
| 678 | + } |
|
| 649 | 679 | |
| 650 | 680 | function AddLink() |
| 651 | 681 | { |
@@ -658,10 +688,12 @@ discard block |
||
| 658 | 688 | function SetLink($link, $y=0, $page=-1) |
| 659 | 689 | { |
| 660 | 690 | // Set destination of internal link |
| 661 | - if($y==-1) |
|
| 662 | - $y = $this->y; |
|
| 663 | - if($page==-1) |
|
| 664 | - $page = $this->page; |
|
| 691 | + if($y==-1) { |
|
| 692 | + $y = $this->y; |
|
| 693 | + } |
|
| 694 | + if($page==-1) { |
|
| 695 | + $page = $this->page; |
|
| 696 | + } |
|
| 665 | 697 | $this->links[$link] = array($page, $y); |
| 666 | 698 | } |
| 667 | 699 | |
@@ -677,16 +709,19 @@ discard block |
||
| 677 | 709 | if ($this->unifontSubset) |
| 678 | 710 | { |
| 679 | 711 | $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')'; |
| 680 | - foreach($this->UTF8StringToArray($txt) as $uni) |
|
| 681 | - $this->CurrentFont['subset'][$uni] = $uni; |
|
| 712 | + foreach($this->UTF8StringToArray($txt) as $uni) { |
|
| 713 | + $this->CurrentFont['subset'][$uni] = $uni; |
|
| 714 | + } |
|
| 715 | + } else { |
|
| 716 | + $txt2 = '('.$this->_escape($txt).')'; |
|
| 682 | 717 | } |
| 683 | - else |
|
| 684 | - $txt2 = '('.$this->_escape($txt).')'; |
|
| 685 | 718 | $s = sprintf('BT %.2F %.2F Td %s Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt2); |
| 686 | - if($this->underline && $txt!='') |
|
| 687 | - $s .= ' '.$this->_dounderline($x,$y,$txt); |
|
| 688 | - if($this->ColorFlag) |
|
| 689 | - $s = 'q '.$this->TextColor.' '.$s.' Q'; |
|
| 719 | + if($this->underline && $txt!='') { |
|
| 720 | + $s .= ' '.$this->_dounderline($x,$y,$txt); |
|
| 721 | + } |
|
| 722 | + if($this->ColorFlag) { |
|
| 723 | + $s = 'q '.$this->TextColor.' '.$s.' Q'; |
|
| 724 | + } |
|
| 690 | 725 | $this->_out($s); |
| 691 | 726 | } |
| 692 | 727 | |
@@ -718,45 +753,54 @@ discard block |
||
| 718 | 753 | $this->_out(sprintf('%.3F Tw',$ws*$k)); |
| 719 | 754 | } |
| 720 | 755 | } |
| 721 | - if($w==0) |
|
| 722 | - $w = $this->w-$this->rMargin-$this->x; |
|
| 756 | + if($w==0) { |
|
| 757 | + $w = $this->w-$this->rMargin-$this->x; |
|
| 758 | + } |
|
| 723 | 759 | $s = ''; |
| 724 | 760 | if($fill || $border==1) |
| 725 | 761 | { |
| 726 | - if($fill) |
|
| 727 | - $op = ($border==1) ? 'B' : 'f'; |
|
| 728 | - else |
|
| 729 | - $op = 'S'; |
|
| 762 | + if($fill) { |
|
| 763 | + $op = ($border==1) ? 'B' : 'f'; |
|
| 764 | + } else { |
|
| 765 | + $op = 'S'; |
|
| 766 | + } |
|
| 730 | 767 | $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op); |
| 731 | 768 | } |
| 732 | 769 | if(is_string($border)) |
| 733 | 770 | { |
| 734 | 771 | $x = $this->x; |
| 735 | 772 | $y = $this->y; |
| 736 | - if(strpos($border,'L')!==false) |
|
| 737 | - $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k); |
|
| 738 | - if(strpos($border,'T')!==false) |
|
| 739 | - $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k); |
|
| 740 | - if(strpos($border,'R')!==false) |
|
| 741 | - $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k); |
|
| 742 | - if(strpos($border,'B')!==false) |
|
| 743 | - $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k); |
|
| 773 | + if(strpos($border,'L')!==false) { |
|
| 774 | + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k); |
|
| 775 | + } |
|
| 776 | + if(strpos($border,'T')!==false) { |
|
| 777 | + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k); |
|
| 778 | + } |
|
| 779 | + if(strpos($border,'R')!==false) { |
|
| 780 | + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k); |
|
| 781 | + } |
|
| 782 | + if(strpos($border,'B')!==false) { |
|
| 783 | + $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k); |
|
| 784 | + } |
|
| 744 | 785 | } |
| 745 | 786 | if($txt!=='') |
| 746 | 787 | { |
| 747 | - if($align=='R') |
|
| 748 | - $dx = $w-$this->cMargin-$this->GetStringWidth($txt); |
|
| 749 | - elseif($align=='C') |
|
| 750 | - $dx = ($w-$this->GetStringWidth($txt))/2; |
|
| 751 | - else |
|
| 752 | - $dx = $this->cMargin; |
|
| 753 | - if($this->ColorFlag) |
|
| 754 | - $s .= 'q '.$this->TextColor.' '; |
|
| 788 | + if($align=='R') { |
|
| 789 | + $dx = $w-$this->cMargin-$this->GetStringWidth($txt); |
|
| 790 | + } elseif($align=='C') { |
|
| 791 | + $dx = ($w-$this->GetStringWidth($txt))/2; |
|
| 792 | + } else { |
|
| 793 | + $dx = $this->cMargin; |
|
| 794 | + } |
|
| 795 | + if($this->ColorFlag) { |
|
| 796 | + $s .= 'q '.$this->TextColor.' '; |
|
| 797 | + } |
|
| 755 | 798 | |
| 756 | 799 | // If multibyte, Tw has no effect - do word spacing using an adjustment before each space |
| 757 | 800 | if ($this->ws && $this->unifontSubset) { |
| 758 | - foreach($this->UTF8StringToArray($txt) as $uni) |
|
| 759 | - $this->CurrentFont['subset'][$uni] = $uni; |
|
| 801 | + foreach($this->UTF8StringToArray($txt) as $uni) { |
|
| 802 | + $this->CurrentFont['subset'][$uni] = $uni; |
|
| 803 | + } |
|
| 760 | 804 | $space = $this->_escape($this->UTF8ToUTF16BE(' ', false)); |
| 761 | 805 | $s .= sprintf('BT 0 Tw %.2F %.2F Td [',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k); |
| 762 | 806 | $t = explode(' ',$txt); |
@@ -772,55 +816,63 @@ discard block |
||
| 772 | 816 | } |
| 773 | 817 | $s .= '] TJ'; |
| 774 | 818 | $s .= ' ET'; |
| 775 | - } |
|
| 776 | - else { |
|
| 819 | + } else { |
|
| 777 | 820 | if ($this->unifontSubset) |
| 778 | 821 | { |
| 779 | 822 | $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')'; |
| 780 | - foreach($this->UTF8StringToArray($txt) as $uni) |
|
| 781 | - $this->CurrentFont['subset'][$uni] = $uni; |
|
| 823 | + foreach($this->UTF8StringToArray($txt) as $uni) { |
|
| 824 | + $this->CurrentFont['subset'][$uni] = $uni; |
|
| 825 | + } |
|
| 826 | + } else { |
|
| 827 | + $txt2='('.str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))).')'; |
|
| 782 | 828 | } |
| 783 | - else |
|
| 784 | - $txt2='('.str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt))).')'; |
|
| 785 | 829 | $s .= sprintf('BT %.2F %.2F Td %s Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2); |
| 786 | 830 | } |
| 787 | - if($this->underline) |
|
| 788 | - $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt); |
|
| 789 | - if($this->ColorFlag) |
|
| 790 | - $s .= ' Q'; |
|
| 791 | - if($link) |
|
| 792 | - $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link); |
|
| 831 | + if($this->underline) { |
|
| 832 | + $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt); |
|
| 833 | + } |
|
| 834 | + if($this->ColorFlag) { |
|
| 835 | + $s .= ' Q'; |
|
| 836 | + } |
|
| 837 | + if($link) { |
|
| 838 | + $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link); |
|
| 839 | + } |
|
| 840 | + } |
|
| 841 | + if($s) { |
|
| 842 | + $this->_out($s); |
|
| 793 | 843 | } |
| 794 | - if($s) |
|
| 795 | - $this->_out($s); |
|
| 796 | 844 | $this->lasth = $h; |
| 797 | 845 | if($ln>0) |
| 798 | 846 | { |
| 799 | 847 | // Go to next line |
| 800 | 848 | $this->y += $h; |
| 801 | - if($ln==1) |
|
| 802 | - $this->x = $this->lMargin; |
|
| 849 | + if($ln==1) { |
|
| 850 | + $this->x = $this->lMargin; |
|
| 851 | + } |
|
| 852 | + } else { |
|
| 853 | + $this->x += $w; |
|
| 854 | + } |
|
| 803 | 855 | } |
| 804 | - else |
|
| 805 | - $this->x += $w; |
|
| 806 | -} |
|
| 807 | 856 | |
| 808 | 857 | function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false) |
| 809 | 858 | { |
| 810 | 859 | // Output text with automatic or explicit line breaks |
| 811 | 860 | $cw = &$this->CurrentFont['cw']; |
| 812 | - if($w==0) |
|
| 813 | - $w = $this->w-$this->rMargin-$this->x; |
|
| 861 | + if($w==0) { |
|
| 862 | + $w = $this->w-$this->rMargin-$this->x; |
|
| 863 | + } |
|
| 814 | 864 | $wmax = ($w-2*$this->cMargin); |
| 815 | 865 | $s = str_replace("\r",'',$txt); |
| 816 | 866 | if ($this->unifontSubset) { |
| 817 | 867 | $nb=mb_strlen($s, 'utf-8'); |
| 818 | - while($nb>0 && mb_substr($s,$nb-1,1,'utf-8')=="\n") $nb--; |
|
| 819 | - } |
|
| 820 | - else { |
|
| 821 | - $nb = strlen($s); |
|
| 822 | - if($nb>0 && $s[$nb-1]=="\n") |
|
| 868 | + while($nb>0 && mb_substr($s,$nb-1,1,'utf-8')=="\n") { |
|
| 823 | 869 | $nb--; |
| 870 | + } |
|
| 871 | + } else { |
|
| 872 | + $nb = strlen($s); |
|
| 873 | + if($nb>0 && $s[$nb-1]=="\n") { |
|
| 874 | + $nb--; |
|
| 875 | + } |
|
| 824 | 876 | } |
| 825 | 877 | $b = 0; |
| 826 | 878 | if($border) |
@@ -830,14 +882,15 @@ discard block |
||
| 830 | 882 | $border = 'LTRB'; |
| 831 | 883 | $b = 'LRT'; |
| 832 | 884 | $b2 = 'LR'; |
| 833 | - } |
|
| 834 | - else |
|
| 885 | + } else |
|
| 835 | 886 | { |
| 836 | 887 | $b2 = ''; |
| 837 | - if(strpos($border,'L')!==false) |
|
| 838 | - $b2 .= 'L'; |
|
| 839 | - if(strpos($border,'R')!==false) |
|
| 840 | - $b2 .= 'R'; |
|
| 888 | + if(strpos($border,'L')!==false) { |
|
| 889 | + $b2 .= 'L'; |
|
| 890 | + } |
|
| 891 | + if(strpos($border,'R')!==false) { |
|
| 892 | + $b2 .= 'R'; |
|
| 893 | + } |
|
| 841 | 894 | $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2; |
| 842 | 895 | } |
| 843 | 896 | } |
@@ -852,8 +905,7 @@ discard block |
||
| 852 | 905 | // Get next character |
| 853 | 906 | if ($this->unifontSubset) { |
| 854 | 907 | $c = mb_substr($s,$i,1,'UTF-8'); |
| 855 | - } |
|
| 856 | - else { |
|
| 908 | + } else { |
|
| 857 | 909 | $c=$s[$i]; |
| 858 | 910 | } |
| 859 | 911 | if($c=="\n") |
@@ -866,8 +918,7 @@ discard block |
||
| 866 | 918 | } |
| 867 | 919 | if ($this->unifontSubset) { |
| 868 | 920 | $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill); |
| 869 | - } |
|
| 870 | - else { |
|
| 921 | + } else { |
|
| 871 | 922 | $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); |
| 872 | 923 | } |
| 873 | 924 | $i++; |
@@ -876,8 +927,9 @@ discard block |
||
| 876 | 927 | $l = 0; |
| 877 | 928 | $ns = 0; |
| 878 | 929 | $nl++; |
| 879 | - if($border && $nl==2) |
|
| 880 | - $b = $b2; |
|
| 930 | + if($border && $nl==2) { |
|
| 931 | + $b = $b2; |
|
| 932 | + } |
|
| 881 | 933 | continue; |
| 882 | 934 | } |
| 883 | 935 | if($c==' ') |
@@ -887,16 +939,16 @@ discard block |
||
| 887 | 939 | $ns++; |
| 888 | 940 | } |
| 889 | 941 | |
| 890 | - if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } |
|
| 891 | - else { $l += $cw[$c]*$this->FontSize/1000; } |
|
| 942 | + if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } else { $l += $cw[$c]*$this->FontSize/1000; } |
|
| 892 | 943 | |
| 893 | 944 | if($l>$wmax) |
| 894 | 945 | { |
| 895 | 946 | // Automatic line break |
| 896 | 947 | if($sep==-1) |
| 897 | 948 | { |
| 898 | - if($i==$j) |
|
| 899 | - $i++; |
|
| 949 | + if($i==$j) { |
|
| 950 | + $i++; |
|
| 951 | + } |
|
| 900 | 952 | if($this->ws>0) |
| 901 | 953 | { |
| 902 | 954 | $this->ws = 0; |
@@ -904,12 +956,10 @@ discard block |
||
| 904 | 956 | } |
| 905 | 957 | if ($this->unifontSubset) { |
| 906 | 958 | $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill); |
| 907 | - } |
|
| 908 | - else { |
|
| 959 | + } else { |
|
| 909 | 960 | $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); |
| 910 | 961 | } |
| 911 | - } |
|
| 912 | - else |
|
| 962 | + } else |
|
| 913 | 963 | { |
| 914 | 964 | if($align=='J') |
| 915 | 965 | { |
@@ -918,8 +968,7 @@ discard block |
||
| 918 | 968 | } |
| 919 | 969 | if ($this->unifontSubset) { |
| 920 | 970 | $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),$b,2,$align,$fill); |
| 921 | - } |
|
| 922 | - else { |
|
| 971 | + } else { |
|
| 923 | 972 | $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill); |
| 924 | 973 | } |
| 925 | 974 | $i = $sep+1; |
@@ -929,11 +978,12 @@ discard block |
||
| 929 | 978 | $l = 0; |
| 930 | 979 | $ns = 0; |
| 931 | 980 | $nl++; |
| 932 | - if($border && $nl==2) |
|
| 933 | - $b = $b2; |
|
| 981 | + if($border && $nl==2) { |
|
| 982 | + $b = $b2; |
|
| 983 | + } |
|
| 984 | + } else { |
|
| 985 | + $i++; |
|
| 934 | 986 | } |
| 935 | - else |
|
| 936 | - $i++; |
|
| 937 | 987 | } |
| 938 | 988 | // Last chunk |
| 939 | 989 | if($this->ws>0) |
@@ -941,12 +991,12 @@ discard block |
||
| 941 | 991 | $this->ws = 0; |
| 942 | 992 | $this->_out('0 Tw'); |
| 943 | 993 | } |
| 944 | - if($border && strpos($border,'B')!==false) |
|
| 945 | - $b .= 'B'; |
|
| 994 | + if($border && strpos($border,'B')!==false) { |
|
| 995 | + $b .= 'B'; |
|
| 996 | + } |
|
| 946 | 997 | if ($this->unifontSubset) { |
| 947 | 998 | $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill); |
| 948 | - } |
|
| 949 | - else { |
|
| 999 | + } else { |
|
| 950 | 1000 | $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill); |
| 951 | 1001 | } |
| 952 | 1002 | $this->x = $this->lMargin; |
@@ -966,8 +1016,7 @@ discard block |
||
| 966 | 1016 | $this->x += $this->GetStringWidth($s); |
| 967 | 1017 | return; |
| 968 | 1018 | } |
| 969 | - } |
|
| 970 | - else { |
|
| 1019 | + } else { |
|
| 971 | 1020 | $nb = strlen($s); |
| 972 | 1021 | } |
| 973 | 1022 | $sep = -1; |
@@ -980,8 +1029,7 @@ discard block |
||
| 980 | 1029 | // Get next character |
| 981 | 1030 | if ($this->unifontSubset) { |
| 982 | 1031 | $c = mb_substr($s,$i,1,'UTF-8'); |
| 983 | - } |
|
| 984 | - else { |
|
| 1032 | + } else { |
|
| 985 | 1033 | $c = $s[$i]; |
| 986 | 1034 | } |
| 987 | 1035 | if($c=="\n") |
@@ -989,8 +1037,7 @@ discard block |
||
| 989 | 1037 | // Explicit line break |
| 990 | 1038 | if ($this->unifontSubset) { |
| 991 | 1039 | $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link); |
| 992 | - } |
|
| 993 | - else { |
|
| 1040 | + } else { |
|
| 994 | 1041 | $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link); |
| 995 | 1042 | } |
| 996 | 1043 | $i++; |
@@ -1006,11 +1053,11 @@ discard block |
||
| 1006 | 1053 | $nl++; |
| 1007 | 1054 | continue; |
| 1008 | 1055 | } |
| 1009 | - if($c==' ') |
|
| 1010 | - $sep = $i; |
|
| 1056 | + if($c==' ') { |
|
| 1057 | + $sep = $i; |
|
| 1058 | + } |
|
| 1011 | 1059 | |
| 1012 | - if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } |
|
| 1013 | - else { $l += $cw[$c]*$this->FontSize/1000; } |
|
| 1060 | + if ($this->unifontSubset) { $l += $this->GetStringWidth($c); } else { $l += $cw[$c]*$this->FontSize/1000; } |
|
| 1014 | 1061 | |
| 1015 | 1062 | if($l>$wmax) |
| 1016 | 1063 | { |
@@ -1028,21 +1075,19 @@ discard block |
||
| 1028 | 1075 | $nl++; |
| 1029 | 1076 | continue; |
| 1030 | 1077 | } |
| 1031 | - if($i==$j) |
|
| 1032 | - $i++; |
|
| 1078 | + if($i==$j) { |
|
| 1079 | + $i++; |
|
| 1080 | + } |
|
| 1033 | 1081 | if ($this->unifontSubset) { |
| 1034 | 1082 | $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',0,$link); |
| 1035 | - } |
|
| 1036 | - else { |
|
| 1083 | + } else { |
|
| 1037 | 1084 | $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link); |
| 1038 | 1085 | } |
| 1039 | - } |
|
| 1040 | - else |
|
| 1086 | + } else |
|
| 1041 | 1087 | { |
| 1042 | 1088 | if ($this->unifontSubset) { |
| 1043 | 1089 | $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),0,2,'',0,$link); |
| 1044 | - } |
|
| 1045 | - else { |
|
| 1090 | + } else { |
|
| 1046 | 1091 | $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link); |
| 1047 | 1092 | } |
| 1048 | 1093 | $i = $sep+1; |
@@ -1057,16 +1102,15 @@ discard block |
||
| 1057 | 1102 | $wmax = ($w-2*$this->cMargin); |
| 1058 | 1103 | } |
| 1059 | 1104 | $nl++; |
| 1105 | + } else { |
|
| 1106 | + $i++; |
|
| 1060 | 1107 | } |
| 1061 | - else |
|
| 1062 | - $i++; |
|
| 1063 | 1108 | } |
| 1064 | 1109 | // Last chunk |
| 1065 | 1110 | if($i!=$j) { |
| 1066 | 1111 | if ($this->unifontSubset) { |
| 1067 | 1112 | $this->Cell($l,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',0,$link); |
| 1068 | - } |
|
| 1069 | - else { |
|
| 1113 | + } else { |
|
| 1070 | 1114 | $this->Cell($l,$h,substr($s,$j),0,0,'',0,$link); |
| 1071 | 1115 | } |
| 1072 | 1116 | } |
@@ -1076,11 +1120,12 @@ discard block |
||
| 1076 | 1120 | { |
| 1077 | 1121 | // Line feed; default value is last cell height |
| 1078 | 1122 | $this->x = $this->lMargin; |
| 1079 | - if($h===null) |
|
| 1080 | - $this->y += $this->lasth; |
|
| 1081 | - else |
|
| 1082 | - $this->y += $h; |
|
| 1083 | -} |
|
| 1123 | + if($h===null) { |
|
| 1124 | + $this->y += $this->lasth; |
|
| 1125 | + } else { |
|
| 1126 | + $this->y += $h; |
|
| 1127 | + } |
|
| 1128 | + } |
|
| 1084 | 1129 | |
| 1085 | 1130 | function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='') |
| 1086 | 1131 | { |
@@ -1091,22 +1136,25 @@ discard block |
||
| 1091 | 1136 | if($type=='') |
| 1092 | 1137 | { |
| 1093 | 1138 | $pos = strrpos($file,'.'); |
| 1094 | - if(!$pos) |
|
| 1095 | - $this->Error('Image file has no extension and no type was specified: '.$file); |
|
| 1139 | + if(!$pos) { |
|
| 1140 | + $this->Error('Image file has no extension and no type was specified: '.$file); |
|
| 1141 | + } |
|
| 1096 | 1142 | $type = substr($file,$pos+1); |
| 1097 | 1143 | } |
| 1098 | 1144 | $type = strtolower($type); |
| 1099 | - if($type=='jpeg') |
|
| 1100 | - $type = 'jpg'; |
|
| 1145 | + if($type=='jpeg') { |
|
| 1146 | + $type = 'jpg'; |
|
| 1147 | + } |
|
| 1101 | 1148 | $mtd = '_parse'.$type; |
| 1102 | - if(!method_exists($this,$mtd)) |
|
| 1103 | - $this->Error('Unsupported image type: '.$type); |
|
| 1149 | + if(!method_exists($this,$mtd)) { |
|
| 1150 | + $this->Error('Unsupported image type: '.$type); |
|
| 1151 | + } |
|
| 1104 | 1152 | $info = $this->$mtd($file); |
| 1105 | 1153 | $info['i'] = count($this->images)+1; |
| 1106 | 1154 | $this->images[$file] = $info; |
| 1155 | + } else { |
|
| 1156 | + $info = $this->images[$file]; |
|
| 1107 | 1157 | } |
| 1108 | - else |
|
| 1109 | - $info = $this->images[$file]; |
|
| 1110 | 1158 | |
| 1111 | 1159 | // Automatic width and height calculation if needed |
| 1112 | 1160 | if($w==0 && $h==0) |
@@ -1115,14 +1163,18 @@ discard block |
||
| 1115 | 1163 | $w = -96; |
| 1116 | 1164 | $h = -96; |
| 1117 | 1165 | } |
| 1118 | - if($w<0) |
|
| 1119 | - $w = -$info['w']*72/$w/$this->k; |
|
| 1120 | - if($h<0) |
|
| 1121 | - $h = -$info['h']*72/$h/$this->k; |
|
| 1122 | - if($w==0) |
|
| 1123 | - $w = $h*$info['w']/$info['h']; |
|
| 1124 | - if($h==0) |
|
| 1125 | - $h = $w*$info['h']/$info['w']; |
|
| 1166 | + if($w<0) { |
|
| 1167 | + $w = -$info['w']*72/$w/$this->k; |
|
| 1168 | + } |
|
| 1169 | + if($h<0) { |
|
| 1170 | + $h = -$info['h']*72/$h/$this->k; |
|
| 1171 | + } |
|
| 1172 | + if($w==0) { |
|
| 1173 | + $w = $h*$info['w']/$info['h']; |
|
| 1174 | + } |
|
| 1175 | + if($h==0) { |
|
| 1176 | + $h = $w*$info['h']/$info['w']; |
|
| 1177 | + } |
|
| 1126 | 1178 | |
| 1127 | 1179 | // Flowing mode |
| 1128 | 1180 | if($y===null) |
@@ -1138,12 +1190,14 @@ discard block |
||
| 1138 | 1190 | $this->y += $h; |
| 1139 | 1191 | } |
| 1140 | 1192 | |
| 1141 | - if($x===null) |
|
| 1142 | - $x = $this->x; |
|
| 1193 | + if($x===null) { |
|
| 1194 | + $x = $this->x; |
|
| 1195 | + } |
|
| 1143 | 1196 | $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i'])); |
| 1144 | - if($link) |
|
| 1145 | - $this->Link($x,$y,$w,$h,$link); |
|
| 1146 | -} |
|
| 1197 | + if($link) { |
|
| 1198 | + $this->Link($x,$y,$w,$h,$link); |
|
| 1199 | + } |
|
| 1200 | + } |
|
| 1147 | 1201 | |
| 1148 | 1202 | function GetX() |
| 1149 | 1203 | { |
@@ -1154,11 +1208,12 @@ discard block |
||
| 1154 | 1208 | function SetX($x) |
| 1155 | 1209 | { |
| 1156 | 1210 | // Set x position |
| 1157 | - if($x>=0) |
|
| 1158 | - $this->x = $x; |
|
| 1159 | - else |
|
| 1160 | - $this->x = $this->w+$x; |
|
| 1161 | -} |
|
| 1211 | + if($x>=0) { |
|
| 1212 | + $this->x = $x; |
|
| 1213 | + } else { |
|
| 1214 | + $this->x = $this->w+$x; |
|
| 1215 | + } |
|
| 1216 | + } |
|
| 1162 | 1217 | |
| 1163 | 1218 | function GetY() |
| 1164 | 1219 | { |
@@ -1170,11 +1225,12 @@ discard block |
||
| 1170 | 1225 | { |
| 1171 | 1226 | // Set y position and reset x |
| 1172 | 1227 | $this->x = $this->lMargin; |
| 1173 | - if($y>=0) |
|
| 1174 | - $this->y = $y; |
|
| 1175 | - else |
|
| 1176 | - $this->y = $this->h+$y; |
|
| 1177 | -} |
|
| 1228 | + if($y>=0) { |
|
| 1229 | + $this->y = $y; |
|
| 1230 | + } else { |
|
| 1231 | + $this->y = $this->h+$y; |
|
| 1232 | + } |
|
| 1233 | + } |
|
| 1178 | 1234 | |
| 1179 | 1235 | function SetXY($x, $y) |
| 1180 | 1236 | { |
@@ -1186,8 +1242,9 @@ discard block |
||
| 1186 | 1242 | function Output($name='', $dest='') |
| 1187 | 1243 | { |
| 1188 | 1244 | // Output PDF to some destination |
| 1189 | - if($this->state<3) |
|
| 1190 | - $this->Close(); |
|
| 1245 | + if($this->state<3) { |
|
| 1246 | + $this->Close(); |
|
| 1247 | + } |
|
| 1191 | 1248 | $dest = strtoupper($dest); |
| 1192 | 1249 | if($dest=='') |
| 1193 | 1250 | { |
@@ -1195,9 +1252,9 @@ discard block |
||
| 1195 | 1252 | { |
| 1196 | 1253 | $name = 'doc.pdf'; |
| 1197 | 1254 | $dest = 'I'; |
| 1255 | + } else { |
|
| 1256 | + $dest = 'F'; |
|
| 1198 | 1257 | } |
| 1199 | - else |
|
| 1200 | - $dest = 'F'; |
|
| 1201 | 1258 | } |
| 1202 | 1259 | switch($dest) |
| 1203 | 1260 | { |
@@ -1226,8 +1283,9 @@ discard block |
||
| 1226 | 1283 | case 'F': |
| 1227 | 1284 | // Save to local file |
| 1228 | 1285 | $f = fopen($name,'wb'); |
| 1229 | - if(!$f) |
|
| 1230 | - $this->Error('Unable to create output file: '.$name); |
|
| 1286 | + if(!$f) { |
|
| 1287 | + $this->Error('Unable to create output file: '.$name); |
|
| 1288 | + } |
|
| 1231 | 1289 | fwrite($f,$this->buffer,strlen($this->buffer)); |
| 1232 | 1290 | fclose($f); |
| 1233 | 1291 | break; |
@@ -1248,18 +1306,22 @@ discard block |
||
| 1248 | 1306 | function _dochecks() |
| 1249 | 1307 | { |
| 1250 | 1308 | // Check availability of %F |
| 1251 | - if(sprintf('%.1F',1.0)!='1.0') |
|
| 1252 | - $this->Error('This version of PHP is not supported'); |
|
| 1309 | + if(sprintf('%.1F',1.0)!='1.0') { |
|
| 1310 | + $this->Error('This version of PHP is not supported'); |
|
| 1311 | + } |
|
| 1253 | 1312 | // Check availability of mbstring |
| 1254 | - if(!function_exists('mb_strlen')) |
|
| 1255 | - $this->Error('mbstring extension is not available'); |
|
| 1313 | + if(!function_exists('mb_strlen')) { |
|
| 1314 | + $this->Error('mbstring extension is not available'); |
|
| 1315 | + } |
|
| 1256 | 1316 | // Check mbstring overloading |
| 1257 | - if(ini_get('mbstring.func_overload') & 2) |
|
| 1258 | - $this->Error('mbstring overloading must be disabled'); |
|
| 1317 | + if(ini_get('mbstring.func_overload') & 2) { |
|
| 1318 | + $this->Error('mbstring overloading must be disabled'); |
|
| 1319 | + } |
|
| 1259 | 1320 | // Ensure runtime magic quotes are disabled |
| 1260 | - if(get_magic_quotes_runtime()) |
|
| 1261 | - @set_magic_quotes_runtime(0); |
|
| 1262 | -} |
|
| 1321 | + if(get_magic_quotes_runtime()) { |
|
| 1322 | + @set_magic_quotes_runtime(0); |
|
| 1323 | + } |
|
| 1324 | + } |
|
| 1263 | 1325 | |
| 1264 | 1326 | function _getfontpath() |
| 1265 | 1327 | { |
@@ -1270,8 +1332,9 @@ discard block |
||
| 1270 | 1332 | { |
| 1271 | 1333 | if(PHP_SAPI!='cli') |
| 1272 | 1334 | { |
| 1273 | - if(headers_sent($file,$line)) |
|
| 1274 | - $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)"); |
|
| 1335 | + if(headers_sent($file,$line)) { |
|
| 1336 | + $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)"); |
|
| 1337 | + } |
|
| 1275 | 1338 | } |
| 1276 | 1339 | if(ob_get_length()) |
| 1277 | 1340 | { |
@@ -1280,9 +1343,9 @@ discard block |
||
| 1280 | 1343 | { |
| 1281 | 1344 | // It contains only a UTF-8 BOM and/or whitespace, let's clean it |
| 1282 | 1345 | ob_clean(); |
| 1346 | + } else { |
|
| 1347 | + $this->Error("Some data has already been output, can't send PDF file"); |
|
| 1283 | 1348 | } |
| 1284 | - else |
|
| 1285 | - $this->Error("Some data has already been output, can't send PDF file"); |
|
| 1286 | 1349 | } |
| 1287 | 1350 | } |
| 1288 | 1351 | |
@@ -1291,17 +1354,18 @@ discard block |
||
| 1291 | 1354 | if(is_string($size)) |
| 1292 | 1355 | { |
| 1293 | 1356 | $size = strtolower($size); |
| 1294 | - if(!isset($this->StdPageSizes[$size])) |
|
| 1295 | - $this->Error('Unknown page size: '.$size); |
|
| 1357 | + if(!isset($this->StdPageSizes[$size])) { |
|
| 1358 | + $this->Error('Unknown page size: '.$size); |
|
| 1359 | + } |
|
| 1296 | 1360 | $a = $this->StdPageSizes[$size]; |
| 1297 | 1361 | return array($a[0]/$this->k, $a[1]/$this->k); |
| 1298 | - } |
|
| 1299 | - else |
|
| 1362 | + } else |
|
| 1300 | 1363 | { |
| 1301 | - if($size[0]>$size[1]) |
|
| 1302 | - return array($size[1], $size[0]); |
|
| 1303 | - else |
|
| 1304 | - return $size; |
|
| 1364 | + if($size[0]>$size[1]) { |
|
| 1365 | + return array($size[1], $size[0]); |
|
| 1366 | + } else { |
|
| 1367 | + return $size; |
|
| 1368 | + } |
|
| 1305 | 1369 | } |
| 1306 | 1370 | } |
| 1307 | 1371 | |
@@ -1314,14 +1378,16 @@ discard block |
||
| 1314 | 1378 | $this->y = $this->tMargin; |
| 1315 | 1379 | $this->FontFamily = ''; |
| 1316 | 1380 | // Check page size and orientation |
| 1317 | - if($orientation=='') |
|
| 1318 | - $orientation = $this->DefOrientation; |
|
| 1319 | - else |
|
| 1320 | - $orientation = strtoupper($orientation[0]); |
|
| 1321 | - if($size=='') |
|
| 1322 | - $size = $this->DefPageSize; |
|
| 1323 | - else |
|
| 1324 | - $size = $this->_getpagesize($size); |
|
| 1381 | + if($orientation=='') { |
|
| 1382 | + $orientation = $this->DefOrientation; |
|
| 1383 | + } else { |
|
| 1384 | + $orientation = strtoupper($orientation[0]); |
|
| 1385 | + } |
|
| 1386 | + if($size=='') { |
|
| 1387 | + $size = $this->DefPageSize; |
|
| 1388 | + } else { |
|
| 1389 | + $size = $this->_getpagesize($size); |
|
| 1390 | + } |
|
| 1325 | 1391 | if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1]) |
| 1326 | 1392 | { |
| 1327 | 1393 | // New size or orientation |
@@ -1329,8 +1395,7 @@ discard block |
||
| 1329 | 1395 | { |
| 1330 | 1396 | $this->w = $size[0]; |
| 1331 | 1397 | $this->h = $size[1]; |
| 1332 | - } |
|
| 1333 | - else |
|
| 1398 | + } else |
|
| 1334 | 1399 | { |
| 1335 | 1400 | $this->w = $size[1]; |
| 1336 | 1401 | $this->h = $size[0]; |
@@ -1341,9 +1406,10 @@ discard block |
||
| 1341 | 1406 | $this->CurOrientation = $orientation; |
| 1342 | 1407 | $this->CurPageSize = $size; |
| 1343 | 1408 | } |
| 1344 | - if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1]) |
|
| 1345 | - $this->PageSizes[$this->page] = array($this->wPt, $this->hPt); |
|
| 1346 | -} |
|
| 1409 | + if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1]) { |
|
| 1410 | + $this->PageSizes[$this->page] = array($this->wPt, $this->hPt); |
|
| 1411 | + } |
|
| 1412 | + } |
|
| 1347 | 1413 | |
| 1348 | 1414 | function _endpage() |
| 1349 | 1415 | { |
@@ -1355,8 +1421,9 @@ discard block |
||
| 1355 | 1421 | // Load a font definition file from the font directory |
| 1356 | 1422 | include($this->fontpath.$font); |
| 1357 | 1423 | $a = get_defined_vars(); |
| 1358 | - if(!isset($a['name'])) |
|
| 1359 | - $this->Error('Could not include font definition file'); |
|
| 1424 | + if(!isset($a['name'])) { |
|
| 1425 | + $this->Error('Could not include font definition file'); |
|
| 1426 | + } |
|
| 1360 | 1427 | return $a; |
| 1361 | 1428 | } |
| 1362 | 1429 | |
@@ -1392,15 +1459,13 @@ discard block |
||
| 1392 | 1459 | $c3 = ord($s[$i++]); |
| 1393 | 1460 | $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2)); |
| 1394 | 1461 | $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F)); |
| 1395 | - } |
|
| 1396 | - elseif($c1>=192) |
|
| 1462 | + } elseif($c1>=192) |
|
| 1397 | 1463 | { |
| 1398 | 1464 | // 2-byte character |
| 1399 | 1465 | $c2 = ord($s[$i++]); |
| 1400 | 1466 | $res .= chr(($c1 & 0x1C)>>2); |
| 1401 | 1467 | $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F)); |
| 1402 | - } |
|
| 1403 | - else |
|
| 1468 | + } else |
|
| 1404 | 1469 | { |
| 1405 | 1470 | // Single-byte character |
| 1406 | 1471 | $res .= "\0".chr($c1); |
@@ -1422,16 +1487,19 @@ discard block |
||
| 1422 | 1487 | { |
| 1423 | 1488 | // Extract info from a JPEG file |
| 1424 | 1489 | $a = getimagesize($file); |
| 1425 | - if(!$a) |
|
| 1426 | - $this->Error('Missing or incorrect image file: '.$file); |
|
| 1427 | - if($a[2]!=2) |
|
| 1428 | - $this->Error('Not a JPEG file: '.$file); |
|
| 1429 | - if(!isset($a['channels']) || $a['channels']==3) |
|
| 1430 | - $colspace = 'DeviceRGB'; |
|
| 1431 | - elseif($a['channels']==4) |
|
| 1432 | - $colspace = 'DeviceCMYK'; |
|
| 1433 | - else |
|
| 1434 | - $colspace = 'DeviceGray'; |
|
| 1490 | + if(!$a) { |
|
| 1491 | + $this->Error('Missing or incorrect image file: '.$file); |
|
| 1492 | + } |
|
| 1493 | + if($a[2]!=2) { |
|
| 1494 | + $this->Error('Not a JPEG file: '.$file); |
|
| 1495 | + } |
|
| 1496 | + if(!isset($a['channels']) || $a['channels']==3) { |
|
| 1497 | + $colspace = 'DeviceRGB'; |
|
| 1498 | + } elseif($a['channels']==4) { |
|
| 1499 | + $colspace = 'DeviceCMYK'; |
|
| 1500 | + } else { |
|
| 1501 | + $colspace = 'DeviceGray'; |
|
| 1502 | + } |
|
| 1435 | 1503 | $bpc = isset($a['bits']) ? $a['bits'] : 8; |
| 1436 | 1504 | $data = file_get_contents($file); |
| 1437 | 1505 | return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data); |
@@ -1441,8 +1509,9 @@ discard block |
||
| 1441 | 1509 | { |
| 1442 | 1510 | // Extract info from a PNG file |
| 1443 | 1511 | $f = fopen($file,'rb'); |
| 1444 | - if(!$f) |
|
| 1445 | - $this->Error('Can\'t open image file: '.$file); |
|
| 1512 | + if(!$f) { |
|
| 1513 | + $this->Error('Can\'t open image file: '.$file); |
|
| 1514 | + } |
|
| 1446 | 1515 | $info = $this->_parsepngstream($f,$file); |
| 1447 | 1516 | fclose($f); |
| 1448 | 1517 | return $info; |
@@ -1451,33 +1520,40 @@ discard block |
||
| 1451 | 1520 | function _parsepngstream($f, $file) |
| 1452 | 1521 | { |
| 1453 | 1522 | // Check signature |
| 1454 | - if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) |
|
| 1455 | - $this->Error('Not a PNG file: '.$file); |
|
| 1523 | + if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) { |
|
| 1524 | + $this->Error('Not a PNG file: '.$file); |
|
| 1525 | + } |
|
| 1456 | 1526 | |
| 1457 | 1527 | // Read header chunk |
| 1458 | 1528 | $this->_readstream($f,4); |
| 1459 | - if($this->_readstream($f,4)!='IHDR') |
|
| 1460 | - $this->Error('Incorrect PNG file: '.$file); |
|
| 1529 | + if($this->_readstream($f,4)!='IHDR') { |
|
| 1530 | + $this->Error('Incorrect PNG file: '.$file); |
|
| 1531 | + } |
|
| 1461 | 1532 | $w = $this->_readint($f); |
| 1462 | 1533 | $h = $this->_readint($f); |
| 1463 | 1534 | $bpc = ord($this->_readstream($f,1)); |
| 1464 | - if($bpc>8) |
|
| 1465 | - $this->Error('16-bit depth not supported: '.$file); |
|
| 1535 | + if($bpc>8) { |
|
| 1536 | + $this->Error('16-bit depth not supported: '.$file); |
|
| 1537 | + } |
|
| 1466 | 1538 | $ct = ord($this->_readstream($f,1)); |
| 1467 | - if($ct==0 || $ct==4) |
|
| 1468 | - $colspace = 'DeviceGray'; |
|
| 1469 | - elseif($ct==2 || $ct==6) |
|
| 1470 | - $colspace = 'DeviceRGB'; |
|
| 1471 | - elseif($ct==3) |
|
| 1472 | - $colspace = 'Indexed'; |
|
| 1473 | - else |
|
| 1474 | - $this->Error('Unknown color type: '.$file); |
|
| 1475 | - if(ord($this->_readstream($f,1))!=0) |
|
| 1476 | - $this->Error('Unknown compression method: '.$file); |
|
| 1477 | - if(ord($this->_readstream($f,1))!=0) |
|
| 1478 | - $this->Error('Unknown filter method: '.$file); |
|
| 1479 | - if(ord($this->_readstream($f,1))!=0) |
|
| 1480 | - $this->Error('Interlacing not supported: '.$file); |
|
| 1539 | + if($ct==0 || $ct==4) { |
|
| 1540 | + $colspace = 'DeviceGray'; |
|
| 1541 | + } elseif($ct==2 || $ct==6) { |
|
| 1542 | + $colspace = 'DeviceRGB'; |
|
| 1543 | + } elseif($ct==3) { |
|
| 1544 | + $colspace = 'Indexed'; |
|
| 1545 | + } else { |
|
| 1546 | + $this->Error('Unknown color type: '.$file); |
|
| 1547 | + } |
|
| 1548 | + if(ord($this->_readstream($f,1))!=0) { |
|
| 1549 | + $this->Error('Unknown compression method: '.$file); |
|
| 1550 | + } |
|
| 1551 | + if(ord($this->_readstream($f,1))!=0) { |
|
| 1552 | + $this->Error('Unknown filter method: '.$file); |
|
| 1553 | + } |
|
| 1554 | + if(ord($this->_readstream($f,1))!=0) { |
|
| 1555 | + $this->Error('Interlacing not supported: '.$file); |
|
| 1556 | + } |
|
| 1481 | 1557 | $this->_readstream($f,4); |
| 1482 | 1558 | $dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w; |
| 1483 | 1559 | |
@@ -1494,44 +1570,45 @@ discard block |
||
| 1494 | 1570 | // Read palette |
| 1495 | 1571 | $pal = $this->_readstream($f,$n); |
| 1496 | 1572 | $this->_readstream($f,4); |
| 1497 | - } |
|
| 1498 | - elseif($type=='tRNS') |
|
| 1573 | + } elseif($type=='tRNS') |
|
| 1499 | 1574 | { |
| 1500 | 1575 | // Read transparency info |
| 1501 | 1576 | $t = $this->_readstream($f,$n); |
| 1502 | - if($ct==0) |
|
| 1503 | - $trns = array(ord(substr($t,1,1))); |
|
| 1504 | - elseif($ct==2) |
|
| 1505 | - $trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1))); |
|
| 1506 | - else |
|
| 1577 | + if($ct==0) { |
|
| 1578 | + $trns = array(ord(substr($t,1,1))); |
|
| 1579 | + } elseif($ct==2) { |
|
| 1580 | + $trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1))); |
|
| 1581 | + } else |
|
| 1507 | 1582 | { |
| 1508 | 1583 | $pos = strpos($t,chr(0)); |
| 1509 | - if($pos!==false) |
|
| 1510 | - $trns = array($pos); |
|
| 1584 | + if($pos!==false) { |
|
| 1585 | + $trns = array($pos); |
|
| 1586 | + } |
|
| 1511 | 1587 | } |
| 1512 | 1588 | $this->_readstream($f,4); |
| 1513 | - } |
|
| 1514 | - elseif($type=='IDAT') |
|
| 1589 | + } elseif($type=='IDAT') |
|
| 1515 | 1590 | { |
| 1516 | 1591 | // Read image data block |
| 1517 | 1592 | $data .= $this->_readstream($f,$n); |
| 1518 | 1593 | $this->_readstream($f,4); |
| 1594 | + } elseif($type=='IEND') { |
|
| 1595 | + break; |
|
| 1596 | + } else { |
|
| 1597 | + $this->_readstream($f,$n+4); |
|
| 1519 | 1598 | } |
| 1520 | - elseif($type=='IEND') |
|
| 1521 | - break; |
|
| 1522 | - else |
|
| 1523 | - $this->_readstream($f,$n+4); |
|
| 1524 | 1599 | } |
| 1525 | 1600 | while($n); |
| 1526 | 1601 | |
| 1527 | - if($colspace=='Indexed' && empty($pal)) |
|
| 1528 | - $this->Error('Missing palette in '.$file); |
|
| 1602 | + if($colspace=='Indexed' && empty($pal)) { |
|
| 1603 | + $this->Error('Missing palette in '.$file); |
|
| 1604 | + } |
|
| 1529 | 1605 | $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns); |
| 1530 | 1606 | if($ct>=4) |
| 1531 | 1607 | { |
| 1532 | 1608 | // Extract alpha channel |
| 1533 | - if(!function_exists('gzuncompress')) |
|
| 1534 | - $this->Error('Zlib not available, can\'t handle alpha channel: '.$file); |
|
| 1609 | + if(!function_exists('gzuncompress')) { |
|
| 1610 | + $this->Error('Zlib not available, can\'t handle alpha channel: '.$file); |
|
| 1611 | + } |
|
| 1535 | 1612 | $data = gzuncompress($data); |
| 1536 | 1613 | $color = ''; |
| 1537 | 1614 | $alpha = ''; |
@@ -1548,8 +1625,7 @@ discard block |
||
| 1548 | 1625 | $color .= preg_replace('/(.)./s','$1',$line); |
| 1549 | 1626 | $alpha .= preg_replace('/.(.)/s','$1',$line); |
| 1550 | 1627 | } |
| 1551 | - } |
|
| 1552 | - else |
|
| 1628 | + } else |
|
| 1553 | 1629 | { |
| 1554 | 1630 | // RGB image |
| 1555 | 1631 | $len = 4*$w; |
@@ -1566,8 +1642,9 @@ discard block |
||
| 1566 | 1642 | unset($data); |
| 1567 | 1643 | $data = gzcompress($color); |
| 1568 | 1644 | $info['smask'] = gzcompress($alpha); |
| 1569 | - if($this->PDFVersion<'1.4') |
|
| 1570 | - $this->PDFVersion = '1.4'; |
|
| 1645 | + if($this->PDFVersion<'1.4') { |
|
| 1646 | + $this->PDFVersion = '1.4'; |
|
| 1647 | + } |
|
| 1571 | 1648 | } |
| 1572 | 1649 | $info['data'] = $data; |
| 1573 | 1650 | return $info; |
@@ -1580,13 +1657,15 @@ discard block |
||
| 1580 | 1657 | while($n>0 && !feof($f)) |
| 1581 | 1658 | { |
| 1582 | 1659 | $s = fread($f,$n); |
| 1583 | - if($s===false) |
|
| 1584 | - $this->Error('Error while reading stream'); |
|
| 1660 | + if($s===false) { |
|
| 1661 | + $this->Error('Error while reading stream'); |
|
| 1662 | + } |
|
| 1585 | 1663 | $n -= strlen($s); |
| 1586 | 1664 | $res .= $s; |
| 1587 | 1665 | } |
| 1588 | - if($n>0) |
|
| 1589 | - $this->Error('Unexpected end of stream'); |
|
| 1666 | + if($n>0) { |
|
| 1667 | + $this->Error('Unexpected end of stream'); |
|
| 1668 | + } |
|
| 1590 | 1669 | return $res; |
| 1591 | 1670 | } |
| 1592 | 1671 | |
@@ -1600,13 +1679,16 @@ discard block |
||
| 1600 | 1679 | function _parsegif($file) |
| 1601 | 1680 | { |
| 1602 | 1681 | // Extract info from a GIF file (via PNG conversion) |
| 1603 | - if(!function_exists('imagepng')) |
|
| 1604 | - $this->Error('GD extension is required for GIF support'); |
|
| 1605 | - if(!function_exists('imagecreatefromgif')) |
|
| 1606 | - $this->Error('GD has no GIF read support'); |
|
| 1682 | + if(!function_exists('imagepng')) { |
|
| 1683 | + $this->Error('GD extension is required for GIF support'); |
|
| 1684 | + } |
|
| 1685 | + if(!function_exists('imagecreatefromgif')) { |
|
| 1686 | + $this->Error('GD has no GIF read support'); |
|
| 1687 | + } |
|
| 1607 | 1688 | $im = imagecreatefromgif($file); |
| 1608 | - if(!$im) |
|
| 1609 | - $this->Error('Missing or incorrect image file: '.$file); |
|
| 1689 | + if(!$im) { |
|
| 1690 | + $this->Error('Missing or incorrect image file: '.$file); |
|
| 1691 | + } |
|
| 1610 | 1692 | imageinterlace($im,0); |
| 1611 | 1693 | $f = @fopen('php://temp','rb+'); |
| 1612 | 1694 | if($f) |
@@ -1620,15 +1702,16 @@ discard block |
||
| 1620 | 1702 | rewind($f); |
| 1621 | 1703 | $info = $this->_parsepngstream($f,$file); |
| 1622 | 1704 | fclose($f); |
| 1623 | - } |
|
| 1624 | - else |
|
| 1705 | + } else |
|
| 1625 | 1706 | { |
| 1626 | 1707 | // Use temporary file |
| 1627 | 1708 | $tmp = tempnam('.','gif'); |
| 1628 | - if(!$tmp) |
|
| 1629 | - $this->Error('Unable to create a temporary file'); |
|
| 1630 | - if(!imagepng($im,$tmp)) |
|
| 1631 | - $this->Error('Error while saving to temporary file'); |
|
| 1709 | + if(!$tmp) { |
|
| 1710 | + $this->Error('Unable to create a temporary file'); |
|
| 1711 | + } |
|
| 1712 | + if(!imagepng($im,$tmp)) { |
|
| 1713 | + $this->Error('Error while saving to temporary file'); |
|
| 1714 | + } |
|
| 1632 | 1715 | imagedestroy($im); |
| 1633 | 1716 | $info = $this->_parsepng($tmp); |
| 1634 | 1717 | unlink($tmp); |
@@ -1654,11 +1737,12 @@ discard block |
||
| 1654 | 1737 | function _out($s) |
| 1655 | 1738 | { |
| 1656 | 1739 | // Add a line to the document |
| 1657 | - if($this->state==2) |
|
| 1658 | - $this->pages[$this->page] .= $s."\n"; |
|
| 1659 | - else |
|
| 1660 | - $this->buffer .= $s."\n"; |
|
| 1661 | -} |
|
| 1740 | + if($this->state==2) { |
|
| 1741 | + $this->pages[$this->page] .= $s."\n"; |
|
| 1742 | + } else { |
|
| 1743 | + $this->buffer .= $s."\n"; |
|
| 1744 | + } |
|
| 1745 | + } |
|
| 1662 | 1746 | |
| 1663 | 1747 | function _putpages() |
| 1664 | 1748 | { |
@@ -1668,18 +1752,19 @@ discard block |
||
| 1668 | 1752 | // Replace number of pages in fonts using subsets |
| 1669 | 1753 | $alias = $this->UTF8ToUTF16BE($this->AliasNbPages, false); |
| 1670 | 1754 | $r = $this->UTF8ToUTF16BE("$nb", false); |
| 1671 | - for($n=1;$n<=$nb;$n++) |
|
| 1672 | - $this->pages[$n] = str_replace($alias,$r,$this->pages[$n]); |
|
| 1755 | + for($n=1;$n<=$nb;$n++) { |
|
| 1756 | + $this->pages[$n] = str_replace($alias,$r,$this->pages[$n]); |
|
| 1757 | + } |
|
| 1673 | 1758 | // Now repeat for no pages in non-subset fonts |
| 1674 | - for($n=1;$n<=$nb;$n++) |
|
| 1675 | - $this->pages[$n] = str_replace($this->AliasNbPages,$nb,$this->pages[$n]); |
|
| 1759 | + for($n=1;$n<=$nb;$n++) { |
|
| 1760 | + $this->pages[$n] = str_replace($this->AliasNbPages,$nb,$this->pages[$n]); |
|
| 1761 | + } |
|
| 1676 | 1762 | } |
| 1677 | 1763 | if($this->DefOrientation=='P') |
| 1678 | 1764 | { |
| 1679 | 1765 | $wPt = $this->DefPageSize[0]*$this->k; |
| 1680 | 1766 | $hPt = $this->DefPageSize[1]*$this->k; |
| 1681 | - } |
|
| 1682 | - else |
|
| 1767 | + } else |
|
| 1683 | 1768 | { |
| 1684 | 1769 | $wPt = $this->DefPageSize[1]*$this->k; |
| 1685 | 1770 | $hPt = $this->DefPageSize[0]*$this->k; |
@@ -1691,8 +1776,9 @@ discard block |
||
| 1691 | 1776 | $this->_newobj(); |
| 1692 | 1777 | $this->_out('<</Type /Page'); |
| 1693 | 1778 | $this->_out('/Parent 1 0 R'); |
| 1694 | - if(isset($this->PageSizes[$n])) |
|
| 1695 | - $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1])); |
|
| 1779 | + if(isset($this->PageSizes[$n])) { |
|
| 1780 | + $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageSizes[$n][0],$this->PageSizes[$n][1])); |
|
| 1781 | + } |
|
| 1696 | 1782 | $this->_out('/Resources 2 0 R'); |
| 1697 | 1783 | if(isset($this->PageLinks[$n])) |
| 1698 | 1784 | { |
@@ -1702,9 +1788,9 @@ discard block |
||
| 1702 | 1788 | { |
| 1703 | 1789 | $rect = sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]); |
| 1704 | 1790 | $annots .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] '; |
| 1705 | - if(is_string($pl[4])) |
|
| 1706 | - $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>'; |
|
| 1707 | - else |
|
| 1791 | + if(is_string($pl[4])) { |
|
| 1792 | + $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>'; |
|
| 1793 | + } else |
|
| 1708 | 1794 | { |
| 1709 | 1795 | $l = $this->links[$pl[4]]; |
| 1710 | 1796 | $h = isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt; |
@@ -1713,8 +1799,9 @@ discard block |
||
| 1713 | 1799 | } |
| 1714 | 1800 | $this->_out($annots.']'); |
| 1715 | 1801 | } |
| 1716 | - if($this->PDFVersion>'1.3') |
|
| 1717 | - $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>'); |
|
| 1802 | + if($this->PDFVersion>'1.3') { |
|
| 1803 | + $this->_out('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>'); |
|
| 1804 | + } |
|
| 1718 | 1805 | $this->_out('/Contents '.($this->n+1).' 0 R>>'); |
| 1719 | 1806 | $this->_out('endobj'); |
| 1720 | 1807 | // Page content |
@@ -1729,8 +1816,9 @@ discard block |
||
| 1729 | 1816 | $this->_out('1 0 obj'); |
| 1730 | 1817 | $this->_out('<</Type /Pages'); |
| 1731 | 1818 | $kids = '/Kids ['; |
| 1732 | - for($i=0;$i<$nb;$i++) |
|
| 1733 | - $kids .= (3+2*$i).' 0 R '; |
|
| 1819 | + for($i=0;$i<$nb;$i++) { |
|
| 1820 | + $kids .= (3+2*$i).' 0 R '; |
|
| 1821 | + } |
|
| 1734 | 1822 | $this->_out($kids.']'); |
| 1735 | 1823 | $this->_out('/Count '.$nb); |
| 1736 | 1824 | $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]',$wPt,$hPt)); |
@@ -1756,10 +1844,12 @@ discard block |
||
| 1756 | 1844 | $this->FontFiles[$file]['n']=$this->n; |
| 1757 | 1845 | $font=''; |
| 1758 | 1846 | $f=fopen($this->_getfontpath().$file,'rb',1); |
| 1759 | - if(!$f) |
|
| 1760 | - $this->Error('Font file not found'); |
|
| 1761 | - while(!feof($f)) |
|
| 1762 | - $font.=fread($f,8192); |
|
| 1847 | + if(!$f) { |
|
| 1848 | + $this->Error('Font file not found'); |
|
| 1849 | + } |
|
| 1850 | + while(!feof($f)) { |
|
| 1851 | + $font.=fread($f,8192); |
|
| 1852 | + } |
|
| 1763 | 1853 | fclose($f); |
| 1764 | 1854 | $compressed=(substr($file,-2)=='.z'); |
| 1765 | 1855 | if(!$compressed && isset($info['length2'])) |
@@ -1777,11 +1867,13 @@ discard block |
||
| 1777 | 1867 | } |
| 1778 | 1868 | } |
| 1779 | 1869 | $this->_out('<</Length '.strlen($font)); |
| 1780 | - if($compressed) |
|
| 1781 | - $this->_out('/Filter /FlateDecode'); |
|
| 1870 | + if($compressed) { |
|
| 1871 | + $this->_out('/Filter /FlateDecode'); |
|
| 1872 | + } |
|
| 1782 | 1873 | $this->_out('/Length1 '.$info['length1']); |
| 1783 | - if(isset($info['length2'])) |
|
| 1784 | - $this->_out('/Length2 '.$info['length2'].' /Length3 0'); |
|
| 1874 | + if(isset($info['length2'])) { |
|
| 1875 | + $this->_out('/Length2 '.$info['length2'].' /Length3 0'); |
|
| 1876 | + } |
|
| 1785 | 1877 | $this->_out('>>'); |
| 1786 | 1878 | $this->_putstream($font); |
| 1787 | 1879 | $this->_out('endobj'); |
@@ -1801,12 +1893,12 @@ discard block |
||
| 1801 | 1893 | $this->_out('<</Type /Font'); |
| 1802 | 1894 | $this->_out('/BaseFont /'.$name); |
| 1803 | 1895 | $this->_out('/Subtype /Type1'); |
| 1804 | - if($name!='Symbol' && $name!='ZapfDingbats') |
|
| 1805 | - $this->_out('/Encoding /WinAnsiEncoding'); |
|
| 1896 | + if($name!='Symbol' && $name!='ZapfDingbats') { |
|
| 1897 | + $this->_out('/Encoding /WinAnsiEncoding'); |
|
| 1898 | + } |
|
| 1806 | 1899 | $this->_out('>>'); |
| 1807 | 1900 | $this->_out('endobj'); |
| 1808 | - } |
|
| 1809 | - elseif($type=='Type1' || $type=='TrueType') |
|
| 1901 | + } elseif($type=='Type1' || $type=='TrueType') |
|
| 1810 | 1902 | { |
| 1811 | 1903 | // Additional Type1 or TrueType font |
| 1812 | 1904 | $this->fonts[$k]['n']=$this->n+1; |
@@ -1819,10 +1911,11 @@ discard block |
||
| 1819 | 1911 | $this->_out('/FontDescriptor '.($this->n+2).' 0 R'); |
| 1820 | 1912 | if($font['enc']) |
| 1821 | 1913 | { |
| 1822 | - if(isset($font['diff'])) |
|
| 1823 | - $this->_out('/Encoding '.($nf+$font['diff']).' 0 R'); |
|
| 1824 | - else |
|
| 1825 | - $this->_out('/Encoding /WinAnsiEncoding'); |
|
| 1914 | + if(isset($font['diff'])) { |
|
| 1915 | + $this->_out('/Encoding '.($nf+$font['diff']).' 0 R'); |
|
| 1916 | + } else { |
|
| 1917 | + $this->_out('/Encoding /WinAnsiEncoding'); |
|
| 1918 | + } |
|
| 1826 | 1919 | } |
| 1827 | 1920 | $this->_out('>>'); |
| 1828 | 1921 | $this->_out('endobj'); |
@@ -1830,18 +1923,21 @@ discard block |
||
| 1830 | 1923 | $this->_newobj(); |
| 1831 | 1924 | $cw=&$font['cw']; |
| 1832 | 1925 | $s='['; |
| 1833 | - for($i=32;$i<=255;$i++) |
|
| 1834 | - $s.=$cw[chr($i)].' '; |
|
| 1926 | + for($i=32;$i<=255;$i++) { |
|
| 1927 | + $s.=$cw[chr($i)].' '; |
|
| 1928 | + } |
|
| 1835 | 1929 | $this->_out($s.']'); |
| 1836 | 1930 | $this->_out('endobj'); |
| 1837 | 1931 | // Descriptor |
| 1838 | 1932 | $this->_newobj(); |
| 1839 | 1933 | $s='<</Type /FontDescriptor /FontName /'.$name; |
| 1840 | - foreach($font['desc'] as $k=>$v) |
|
| 1841 | - $s.=' /'.$k.' '.$v; |
|
| 1934 | + foreach($font['desc'] as $k=>$v) { |
|
| 1935 | + $s.=' /'.$k.' '.$v; |
|
| 1936 | + } |
|
| 1842 | 1937 | $file=$font['file']; |
| 1843 | - if($file) |
|
| 1844 | - $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R'; |
|
| 1938 | + if($file) { |
|
| 1939 | + $s.=' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R'; |
|
| 1940 | + } |
|
| 1845 | 1941 | $this->_out($s.'>>'); |
| 1846 | 1942 | $this->_out('endobj'); |
| 1847 | 1943 | } |
@@ -1960,14 +2056,14 @@ discard block |
||
| 1960 | 2056 | $this->_putstream($fontstream); |
| 1961 | 2057 | $this->_out('endobj'); |
| 1962 | 2058 | unset($ttf); |
| 1963 | - } |
|
| 1964 | - else |
|
| 2059 | + } else |
|
| 1965 | 2060 | { |
| 1966 | 2061 | // Allow for additional types |
| 1967 | 2062 | $this->fonts[$k]['n'] = $this->n+1; |
| 1968 | 2063 | $mtd='_put'.strtolower($type); |
| 1969 | - if(!method_exists($this,$mtd)) |
|
| 1970 | - $this->Error('Unsupported font type: '.$type); |
|
| 2064 | + if(!method_exists($this,$mtd)) { |
|
| 2065 | + $this->Error('Unsupported font type: '.$type); |
|
| 2066 | + } |
|
| 1971 | 2067 | $this->$mtd($font); |
| 1972 | 2068 | } |
| 1973 | 2069 | } |
@@ -1977,8 +2073,7 @@ discard block |
||
| 1977 | 2073 | if (file_exists($font['unifilename'].'.cw127.php')) { |
| 1978 | 2074 | include($font['unifilename'].'.cw127.php') ; |
| 1979 | 2075 | $startcid = 128; |
| 1980 | - } |
|
| 1981 | - else { |
|
| 2076 | + } else { |
|
| 1982 | 2077 | $rangeid = 0; |
| 1983 | 2078 | $range = array(); |
| 1984 | 2079 | $prevcid = -2; |
@@ -1997,8 +2092,7 @@ discard block |
||
| 1997 | 2092 | $cw127.='$rangeid='.$rangeid.";\n"; |
| 1998 | 2093 | $cw127.='$prevcid='.$prevcid.";\n"; |
| 1999 | 2094 | $cw127.='$prevwidth='.$prevwidth.";\n"; |
| 2000 | - if ($interval) { $cw127.='$interval=true'.";\n"; } |
|
| 2001 | - else { $cw127.='$interval=false'.";\n"; } |
|
| 2095 | + if ($interval) { $cw127.='$interval=true'.";\n"; } else { $cw127.='$interval=false'.";\n"; } |
|
| 2002 | 2096 | $cw127.='$range='.var_export($range,true).";\n"; |
| 2003 | 2097 | $cw127.="?>"; |
| 2004 | 2098 | fwrite($fh,$cw127,strlen($cw127)); |
@@ -2014,8 +2108,7 @@ discard block |
||
| 2014 | 2108 | if ($width == $prevwidth) { |
| 2015 | 2109 | if ($width == $range[$rangeid][0]) { |
| 2016 | 2110 | $range[$rangeid][] = $width; |
| 2017 | - } |
|
| 2018 | - else { |
|
| 2111 | + } else { |
|
| 2019 | 2112 | array_pop($range[$rangeid]); |
| 2020 | 2113 | // new range |
| 2021 | 2114 | $rangeid = $prevcid; |
@@ -2031,8 +2124,7 @@ discard block |
||
| 2031 | 2124 | $rangeid = $cid; |
| 2032 | 2125 | $range[$rangeid] = array(); |
| 2033 | 2126 | $range[$rangeid][] = $width; |
| 2034 | - } |
|
| 2035 | - else { $range[$rangeid][] = $width; } |
|
| 2127 | + } else { $range[$rangeid][] = $width; } |
|
| 2036 | 2128 | $interval = false; |
| 2037 | 2129 | } |
| 2038 | 2130 | } else { |
@@ -2054,21 +2146,17 @@ discard block |
||
| 2054 | 2146 | if (isset($range[$k]['interval'])) { unset($range[$k]['interval']); } |
| 2055 | 2147 | $range[$prevk] = array_merge($range[$prevk], $range[$k]); |
| 2056 | 2148 | unset($range[$k]); |
| 2057 | - } |
|
| 2058 | - else { $prevk = $k; } |
|
| 2149 | + } else { $prevk = $k; } |
|
| 2059 | 2150 | $nextk = $k + $cws; |
| 2060 | 2151 | if (isset($ws['interval'])) { |
| 2061 | - if ($cws > 3) { $prevint = true; } |
|
| 2062 | - else { $prevint = false; } |
|
| 2152 | + if ($cws > 3) { $prevint = true; } else { $prevint = false; } |
|
| 2063 | 2153 | unset($range[$k]['interval']); |
| 2064 | 2154 | --$nextk; |
| 2065 | - } |
|
| 2066 | - else { $prevint = false; } |
|
| 2155 | + } else { $prevint = false; } |
|
| 2067 | 2156 | } |
| 2068 | 2157 | $w = ''; |
| 2069 | 2158 | foreach ($range as $k => $ws) { |
| 2070 | - if (count(array_count_values($ws)) == 1) { $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0]; } |
|
| 2071 | - else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]' . "\n"; } |
|
| 2159 | + if (count(array_count_values($ws)) == 1) { $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0]; } else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]' . "\n"; } |
|
| 2072 | 2160 | } |
| 2073 | 2161 | $this->_out('/W ['.$w.' ]'); |
| 2074 | 2162 | } |
@@ -2091,28 +2179,33 @@ discard block |
||
| 2091 | 2179 | $this->_out('/Subtype /Image'); |
| 2092 | 2180 | $this->_out('/Width '.$info['w']); |
| 2093 | 2181 | $this->_out('/Height '.$info['h']); |
| 2094 | - if($info['cs']=='Indexed') |
|
| 2095 | - $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]'); |
|
| 2096 | - else |
|
| 2182 | + if($info['cs']=='Indexed') { |
|
| 2183 | + $this->_out('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]'); |
|
| 2184 | + } else |
|
| 2097 | 2185 | { |
| 2098 | 2186 | $this->_out('/ColorSpace /'.$info['cs']); |
| 2099 | - if($info['cs']=='DeviceCMYK') |
|
| 2100 | - $this->_out('/Decode [1 0 1 0 1 0 1 0]'); |
|
| 2187 | + if($info['cs']=='DeviceCMYK') { |
|
| 2188 | + $this->_out('/Decode [1 0 1 0 1 0 1 0]'); |
|
| 2189 | + } |
|
| 2101 | 2190 | } |
| 2102 | 2191 | $this->_out('/BitsPerComponent '.$info['bpc']); |
| 2103 | - if(isset($info['f'])) |
|
| 2104 | - $this->_out('/Filter /'.$info['f']); |
|
| 2105 | - if(isset($info['dp'])) |
|
| 2106 | - $this->_out('/DecodeParms <<'.$info['dp'].'>>'); |
|
| 2192 | + if(isset($info['f'])) { |
|
| 2193 | + $this->_out('/Filter /'.$info['f']); |
|
| 2194 | + } |
|
| 2195 | + if(isset($info['dp'])) { |
|
| 2196 | + $this->_out('/DecodeParms <<'.$info['dp'].'>>'); |
|
| 2197 | + } |
|
| 2107 | 2198 | if(isset($info['trns']) && is_array($info['trns'])) |
| 2108 | 2199 | { |
| 2109 | 2200 | $trns = ''; |
| 2110 | - for($i=0;$i<count($info['trns']);$i++) |
|
| 2111 | - $trns .= $info['trns'][$i].' '.$info['trns'][$i].' '; |
|
| 2201 | + for($i=0;$i<count($info['trns']);$i++) { |
|
| 2202 | + $trns .= $info['trns'][$i].' '.$info['trns'][$i].' '; |
|
| 2203 | + } |
|
| 2112 | 2204 | $this->_out('/Mask ['.$trns.']'); |
| 2113 | 2205 | } |
| 2114 | - if(isset($info['smask'])) |
|
| 2115 | - $this->_out('/SMask '.($this->n+1).' 0 R'); |
|
| 2206 | + if(isset($info['smask'])) { |
|
| 2207 | + $this->_out('/SMask '.($this->n+1).' 0 R'); |
|
| 2208 | + } |
|
| 2116 | 2209 | $this->_out('/Length '.strlen($info['data']).'>>'); |
| 2117 | 2210 | $this->_putstream($info['data']); |
| 2118 | 2211 | $this->_out('endobj'); |
@@ -2137,9 +2230,10 @@ discard block |
||
| 2137 | 2230 | |
| 2138 | 2231 | function _putxobjectdict() |
| 2139 | 2232 | { |
| 2140 | - foreach($this->images as $image) |
|
| 2141 | - $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R'); |
|
| 2142 | -} |
|
| 2233 | + foreach($this->images as $image) { |
|
| 2234 | + $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R'); |
|
| 2235 | + } |
|
| 2236 | + } |
|
| 2143 | 2237 | |
| 2144 | 2238 | function _putresourcedict() |
| 2145 | 2239 | { |
@@ -2170,16 +2264,21 @@ discard block |
||
| 2170 | 2264 | function _putinfo() |
| 2171 | 2265 | { |
| 2172 | 2266 | $this->_out('/Producer '.$this->_textstring('tFPDF '.tFPDF_VERSION)); |
| 2173 | - if(!empty($this->title)) |
|
| 2174 | - $this->_out('/Title '.$this->_textstring($this->title)); |
|
| 2175 | - if(!empty($this->subject)) |
|
| 2176 | - $this->_out('/Subject '.$this->_textstring($this->subject)); |
|
| 2177 | - if(!empty($this->author)) |
|
| 2178 | - $this->_out('/Author '.$this->_textstring($this->author)); |
|
| 2179 | - if(!empty($this->keywords)) |
|
| 2180 | - $this->_out('/Keywords '.$this->_textstring($this->keywords)); |
|
| 2181 | - if(!empty($this->creator)) |
|
| 2182 | - $this->_out('/Creator '.$this->_textstring($this->creator)); |
|
| 2267 | + if(!empty($this->title)) { |
|
| 2268 | + $this->_out('/Title '.$this->_textstring($this->title)); |
|
| 2269 | + } |
|
| 2270 | + if(!empty($this->subject)) { |
|
| 2271 | + $this->_out('/Subject '.$this->_textstring($this->subject)); |
|
| 2272 | + } |
|
| 2273 | + if(!empty($this->author)) { |
|
| 2274 | + $this->_out('/Author '.$this->_textstring($this->author)); |
|
| 2275 | + } |
|
| 2276 | + if(!empty($this->keywords)) { |
|
| 2277 | + $this->_out('/Keywords '.$this->_textstring($this->keywords)); |
|
| 2278 | + } |
|
| 2279 | + if(!empty($this->creator)) { |
|
| 2280 | + $this->_out('/Creator '.$this->_textstring($this->creator)); |
|
| 2281 | + } |
|
| 2183 | 2282 | $this->_out('/CreationDate '.$this->_textstring('D:'.@date('YmdHis'))); |
| 2184 | 2283 | } |
| 2185 | 2284 | |
@@ -2187,21 +2286,23 @@ discard block |
||
| 2187 | 2286 | { |
| 2188 | 2287 | $this->_out('/Type /Catalog'); |
| 2189 | 2288 | $this->_out('/Pages 1 0 R'); |
| 2190 | - if($this->ZoomMode=='fullpage') |
|
| 2191 | - $this->_out('/OpenAction [3 0 R /Fit]'); |
|
| 2192 | - elseif($this->ZoomMode=='fullwidth') |
|
| 2193 | - $this->_out('/OpenAction [3 0 R /FitH null]'); |
|
| 2194 | - elseif($this->ZoomMode=='real') |
|
| 2195 | - $this->_out('/OpenAction [3 0 R /XYZ null null 1]'); |
|
| 2196 | - elseif(!is_string($this->ZoomMode)) |
|
| 2197 | - $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']'); |
|
| 2198 | - if($this->LayoutMode=='single') |
|
| 2199 | - $this->_out('/PageLayout /SinglePage'); |
|
| 2200 | - elseif($this->LayoutMode=='continuous') |
|
| 2201 | - $this->_out('/PageLayout /OneColumn'); |
|
| 2202 | - elseif($this->LayoutMode=='two') |
|
| 2203 | - $this->_out('/PageLayout /TwoColumnLeft'); |
|
| 2204 | -} |
|
| 2289 | + if($this->ZoomMode=='fullpage') { |
|
| 2290 | + $this->_out('/OpenAction [3 0 R /Fit]'); |
|
| 2291 | + } elseif($this->ZoomMode=='fullwidth') { |
|
| 2292 | + $this->_out('/OpenAction [3 0 R /FitH null]'); |
|
| 2293 | + } elseif($this->ZoomMode=='real') { |
|
| 2294 | + $this->_out('/OpenAction [3 0 R /XYZ null null 1]'); |
|
| 2295 | + } elseif(!is_string($this->ZoomMode)) { |
|
| 2296 | + $this->_out('/OpenAction [3 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']'); |
|
| 2297 | + } |
|
| 2298 | + if($this->LayoutMode=='single') { |
|
| 2299 | + $this->_out('/PageLayout /SinglePage'); |
|
| 2300 | + } elseif($this->LayoutMode=='continuous') { |
|
| 2301 | + $this->_out('/PageLayout /OneColumn'); |
|
| 2302 | + } elseif($this->LayoutMode=='two') { |
|
| 2303 | + $this->_out('/PageLayout /TwoColumnLeft'); |
|
| 2304 | + } |
|
| 2305 | + } |
|
| 2205 | 2306 | |
| 2206 | 2307 | function _putheader() |
| 2207 | 2308 | { |
@@ -2237,8 +2338,9 @@ discard block |
||
| 2237 | 2338 | $this->_out('xref'); |
| 2238 | 2339 | $this->_out('0 '.($this->n+1)); |
| 2239 | 2340 | $this->_out('0000000000 65535 f '); |
| 2240 | - for($i=1;$i<=$this->n;$i++) |
|
| 2241 | - $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i])); |
|
| 2341 | + for($i=1;$i<=$this->n;$i++) { |
|
| 2342 | + $this->_out(sprintf('%010d 00000 n ',$this->offsets[$i])); |
|
| 2343 | + } |
|
| 2242 | 2344 | // Trailer |
| 2243 | 2345 | $this->_out('trailer'); |
| 2244 | 2346 | $this->_out('<<'); |
@@ -2268,18 +2370,19 @@ discard block |
||
| 2268 | 2370 | for ($i = 0; $i < $len; $i++) { |
| 2269 | 2371 | $uni = -1; |
| 2270 | 2372 | $h = ord($str[$i]); |
| 2271 | - if ( $h <= 0x7F ) |
|
| 2272 | - $uni = $h; |
|
| 2273 | - elseif ( $h >= 0xC2 ) { |
|
| 2274 | - if ( ($h <= 0xDF) && ($i < $len -1) ) |
|
| 2275 | - $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F); |
|
| 2276 | - elseif ( ($h <= 0xEF) && ($i < $len -2) ) |
|
| 2277 | - $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6 |
|
| 2373 | + if ( $h <= 0x7F ) { |
|
| 2374 | + $uni = $h; |
|
| 2375 | + } elseif ( $h >= 0xC2 ) { |
|
| 2376 | + if ( ($h <= 0xDF) && ($i < $len -1) ) { |
|
| 2377 | + $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F); |
|
| 2378 | + } elseif ( ($h <= 0xEF) && ($i < $len -2) ) { |
|
| 2379 | + $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6 |
|
| 2278 | 2380 | | (ord($str[++$i]) & 0x3F); |
| 2279 | - elseif ( ($h <= 0xF4) && ($i < $len -3) ) |
|
| 2280 | - $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12 |
|
| 2381 | + } elseif ( ($h <= 0xF4) && ($i < $len -3) ) { |
|
| 2382 | + $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12 |
|
| 2281 | 2383 | | (ord($str[++$i]) & 0x3F) << 6 |
| 2282 | 2384 | | (ord($str[++$i]) & 0x3F); |
| 2385 | + } |
|
| 2283 | 2386 | } |
| 2284 | 2387 | if ($uni >= 0) { |
| 2285 | 2388 | $out[] = $uni; |
@@ -98,7 +98,6 @@ discard block |
||
| 98 | 98 | * it into 3 64 bit parts, and then does the following |
| 99 | 99 | * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
| 100 | 100 | * |
| 101 | - * @param string $data A plain text string |
|
| 102 | 101 | * @return boolean Returns true |
| 103 | 102 | */ |
| 104 | 103 | public function encrypt(&$text) |
@@ -128,7 +127,6 @@ discard block |
||
| 128 | 127 | * it into 3 64 bit parts, and then does the following |
| 129 | 128 | * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
| 130 | 129 | * |
| 131 | - * @param string $encrypted A DES encrypted string |
|
| 132 | 130 | * @return boolean Returns true |
| 133 | 131 | */ |
| 134 | 132 | public function decrypt(&$text) |
@@ -229,6 +227,7 @@ discard block |
||
| 229 | 227 | * to make a 24 byte key |
| 230 | 228 | * |
| 231 | 229 | * @param string $key The 8 or 16 byte key to expand |
| 230 | + * @param integer $len |
|
| 232 | 231 | * @return string If the key given is 8 or 16 bytes it returns the |
| 233 | 232 | * expanded 24 byte key, else it returns the original key unexpanded |
| 234 | 233 | */ |
@@ -43,209 +43,209 @@ |
||
| 43 | 43 | */ |
| 44 | 44 | class Cipher_3DES extends Cipher_DES |
| 45 | 45 | { |
| 46 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 47 | - const BYTES_BLOCK = 8; // 64 bits |
|
| 46 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 47 | + const BYTES_BLOCK = 8; // 64 bits |
|
| 48 | 48 | |
| 49 | - /** @type integer BYTES_KEY The size of the key. The key can be |
|
| 49 | + /** @type integer BYTES_KEY The size of the key. The key can be |
|
| 50 | 50 | 8, 16, or 24 bytes but gets expanded to 24 bytes before used */ |
| 51 | - const BYTES_KEY = 24; |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Constructor |
|
| 56 | - * |
|
| 57 | - * @param string $key The key used for Encryption/Decryption |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function __construct($key) |
|
| 61 | - { |
|
| 62 | - $key_len = strlen($key); |
|
| 63 | - |
|
| 64 | - if($key_len == 8 || $key_len == 16) |
|
| 65 | - $key = self::expandKey($key, $key_len); |
|
| 66 | - else if($key_len < self::BYTES_KEY) |
|
| 67 | - { |
|
| 68 | - $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
|
| 69 | - $msg .= "$key_len bytes received."; |
|
| 70 | - trigger_error($msg, E_USER_WARNING); |
|
| 71 | - } |
|
| 72 | - // else if the key is longer than 24 bytes, phpCrypt will shorten it |
|
| 73 | - |
|
| 74 | - // set the 3DES key, note that we call __construct1() not __construct() |
|
| 75 | - // this is a second contructor we created for classes that extend the |
|
| 76 | - // DES class |
|
| 77 | - parent::__construct1(PHP_Crypt::CIPHER_3DES, $key, self::BYTES_KEY); |
|
| 78 | - |
|
| 79 | - // 3DES requires that data is 64 bits |
|
| 80 | - $this->blockSize(self::BYTES_BLOCK); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Destructor |
|
| 86 | - * |
|
| 87 | - * @return void |
|
| 88 | - */ |
|
| 89 | - public function __destruct() |
|
| 90 | - { |
|
| 91 | - parent::__destruct(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Encrypt plain text data using TripleDES |
|
| 97 | - * For encryption 3DES takes a 192 bit key, splits |
|
| 98 | - * it into 3 64 bit parts, and then does the following |
|
| 99 | - * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
|
| 100 | - * |
|
| 101 | - * @param string $data A plain text string |
|
| 102 | - * @return boolean Returns true |
|
| 103 | - */ |
|
| 104 | - public function encrypt(&$text) |
|
| 105 | - { |
|
| 106 | - $blocksz = $this->blockSize(); |
|
| 107 | - |
|
| 108 | - for($i = 0; $i < 3; ++$i) |
|
| 109 | - { |
|
| 110 | - $key = substr($this->key(), ($i * 8), $blocksz); |
|
| 111 | - $this->keyPermutation($key); |
|
| 112 | - |
|
| 113 | - if($i % 2) // round 1 |
|
| 114 | - $this->operation(parent::DECRYPT); |
|
| 115 | - else // rounds 0 and 2 |
|
| 116 | - $this->operation(parent::ENCRYPT); |
|
| 117 | - |
|
| 118 | - $this->des($text); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Decrypt a TripleDES encrypted string |
|
| 127 | - * For decryption 3DES takes a 192 bit key, splits |
|
| 128 | - * it into 3 64 bit parts, and then does the following |
|
| 129 | - * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
|
| 130 | - * |
|
| 131 | - * @param string $encrypted A DES encrypted string |
|
| 132 | - * @return boolean Returns true |
|
| 133 | - */ |
|
| 134 | - public function decrypt(&$text) |
|
| 135 | - { |
|
| 136 | - $blocksz = $this->blockSize(); |
|
| 137 | - |
|
| 138 | - for($i = 2; $i >= 0; --$i) |
|
| 139 | - { |
|
| 140 | - $key = substr($this->key(), ($i * 8), $blocksz); |
|
| 141 | - $this->keyPermutation($key); |
|
| 142 | - |
|
| 143 | - if($i % 2) // round 1 |
|
| 144 | - $this->operation(parent::ENCRYPT); |
|
| 145 | - else // round 0 and 2 |
|
| 146 | - $this->operation(parent::DECRYPT); |
|
| 147 | - |
|
| 148 | - $this->des($text); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return true; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * We overload the DES::keyPermutation() function so it takes |
|
| 157 | - * a 64 bit key as a parameter. This is because 3DES uses a 192 bit |
|
| 158 | - * key which is split into 3 64 bit parts, each of which must be |
|
| 159 | - * passed through the keyPermutation() function |
|
| 160 | - * Key permutation, based on tables $_pc1 and $_pc2 |
|
| 161 | - * Create 16 subkeys, each of which is 48-bits long. |
|
| 162 | - * NOTE: The $key parameter is required, however because PHP expects |
|
| 163 | - * an overloaded method to contain the same number of parameters |
|
| 164 | - * (when E_STRICT is set) as the parent class, we make it optional |
|
| 165 | - * to shut the 'error' up |
|
| 166 | - * |
|
| 167 | - * @param string $key A 64 bit key |
|
| 168 | - * @return void |
|
| 169 | - */ |
|
| 170 | - private function keyPermutation($key = "") |
|
| 171 | - { |
|
| 172 | - $this->sub_keys = array(); |
|
| 173 | - $pc1m = array(); |
|
| 174 | - $pcr = array(); |
|
| 175 | - $c = array(); |
|
| 176 | - $d = array(); |
|
| 177 | - |
|
| 178 | - // convert the key to binary |
|
| 179 | - $binkey = parent::str2Bin($key); |
|
| 180 | - |
|
| 181 | - // reduce the key down to 56bits based on table $_pc1 |
|
| 182 | - for($i = 0; $i < 56; ++$i) |
|
| 183 | - { |
|
| 184 | - $pos = parent::$_pc1[$i] - 1; |
|
| 51 | + const BYTES_KEY = 24; |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Constructor |
|
| 56 | + * |
|
| 57 | + * @param string $key The key used for Encryption/Decryption |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function __construct($key) |
|
| 61 | + { |
|
| 62 | + $key_len = strlen($key); |
|
| 63 | + |
|
| 64 | + if($key_len == 8 || $key_len == 16) |
|
| 65 | + $key = self::expandKey($key, $key_len); |
|
| 66 | + else if($key_len < self::BYTES_KEY) |
|
| 67 | + { |
|
| 68 | + $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
|
| 69 | + $msg .= "$key_len bytes received."; |
|
| 70 | + trigger_error($msg, E_USER_WARNING); |
|
| 71 | + } |
|
| 72 | + // else if the key is longer than 24 bytes, phpCrypt will shorten it |
|
| 73 | + |
|
| 74 | + // set the 3DES key, note that we call __construct1() not __construct() |
|
| 75 | + // this is a second contructor we created for classes that extend the |
|
| 76 | + // DES class |
|
| 77 | + parent::__construct1(PHP_Crypt::CIPHER_3DES, $key, self::BYTES_KEY); |
|
| 78 | + |
|
| 79 | + // 3DES requires that data is 64 bits |
|
| 80 | + $this->blockSize(self::BYTES_BLOCK); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Destructor |
|
| 86 | + * |
|
| 87 | + * @return void |
|
| 88 | + */ |
|
| 89 | + public function __destruct() |
|
| 90 | + { |
|
| 91 | + parent::__destruct(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Encrypt plain text data using TripleDES |
|
| 97 | + * For encryption 3DES takes a 192 bit key, splits |
|
| 98 | + * it into 3 64 bit parts, and then does the following |
|
| 99 | + * DES ENCRYPT(key1) -> DES DECRYPT(key2) -> DES ENCRYPT(key3) |
|
| 100 | + * |
|
| 101 | + * @param string $data A plain text string |
|
| 102 | + * @return boolean Returns true |
|
| 103 | + */ |
|
| 104 | + public function encrypt(&$text) |
|
| 105 | + { |
|
| 106 | + $blocksz = $this->blockSize(); |
|
| 107 | + |
|
| 108 | + for($i = 0; $i < 3; ++$i) |
|
| 109 | + { |
|
| 110 | + $key = substr($this->key(), ($i * 8), $blocksz); |
|
| 111 | + $this->keyPermutation($key); |
|
| 112 | + |
|
| 113 | + if($i % 2) // round 1 |
|
| 114 | + $this->operation(parent::DECRYPT); |
|
| 115 | + else // rounds 0 and 2 |
|
| 116 | + $this->operation(parent::ENCRYPT); |
|
| 117 | + |
|
| 118 | + $this->des($text); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + return true; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Decrypt a TripleDES encrypted string |
|
| 127 | + * For decryption 3DES takes a 192 bit key, splits |
|
| 128 | + * it into 3 64 bit parts, and then does the following |
|
| 129 | + * DES DECRYPT(key1) -> DES ENCRYPT(key2) -> DES DECRYPT(key3) |
|
| 130 | + * |
|
| 131 | + * @param string $encrypted A DES encrypted string |
|
| 132 | + * @return boolean Returns true |
|
| 133 | + */ |
|
| 134 | + public function decrypt(&$text) |
|
| 135 | + { |
|
| 136 | + $blocksz = $this->blockSize(); |
|
| 137 | + |
|
| 138 | + for($i = 2; $i >= 0; --$i) |
|
| 139 | + { |
|
| 140 | + $key = substr($this->key(), ($i * 8), $blocksz); |
|
| 141 | + $this->keyPermutation($key); |
|
| 142 | + |
|
| 143 | + if($i % 2) // round 1 |
|
| 144 | + $this->operation(parent::ENCRYPT); |
|
| 145 | + else // round 0 and 2 |
|
| 146 | + $this->operation(parent::DECRYPT); |
|
| 147 | + |
|
| 148 | + $this->des($text); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return true; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * We overload the DES::keyPermutation() function so it takes |
|
| 157 | + * a 64 bit key as a parameter. This is because 3DES uses a 192 bit |
|
| 158 | + * key which is split into 3 64 bit parts, each of which must be |
|
| 159 | + * passed through the keyPermutation() function |
|
| 160 | + * Key permutation, based on tables $_pc1 and $_pc2 |
|
| 161 | + * Create 16 subkeys, each of which is 48-bits long. |
|
| 162 | + * NOTE: The $key parameter is required, however because PHP expects |
|
| 163 | + * an overloaded method to contain the same number of parameters |
|
| 164 | + * (when E_STRICT is set) as the parent class, we make it optional |
|
| 165 | + * to shut the 'error' up |
|
| 166 | + * |
|
| 167 | + * @param string $key A 64 bit key |
|
| 168 | + * @return void |
|
| 169 | + */ |
|
| 170 | + private function keyPermutation($key = "") |
|
| 171 | + { |
|
| 172 | + $this->sub_keys = array(); |
|
| 173 | + $pc1m = array(); |
|
| 174 | + $pcr = array(); |
|
| 175 | + $c = array(); |
|
| 176 | + $d = array(); |
|
| 177 | + |
|
| 178 | + // convert the key to binary |
|
| 179 | + $binkey = parent::str2Bin($key); |
|
| 180 | + |
|
| 181 | + // reduce the key down to 56bits based on table $_pc1 |
|
| 182 | + for($i = 0; $i < 56; ++$i) |
|
| 183 | + { |
|
| 184 | + $pos = parent::$_pc1[$i] - 1; |
|
| 185 | 185 | $pc1m[$i] = $binkey[$pos]; |
| 186 | - } |
|
| 187 | - |
|
| 188 | - // split $pc1m in half (C0 and D0) |
|
| 189 | - $c[0] = array_slice($pc1m, 0, 28); |
|
| 190 | - $d[0] = array_slice($pc1m, 28, 28); |
|
| 191 | - |
|
| 192 | - // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
| 193 | - // where 1 <= n <= 16 |
|
| 194 | - for($i = 1; $i <= 16; ++$i) |
|
| 195 | - { |
|
| 196 | - // now set the next Cn and Dn as the previous Cn and Dn |
|
| 197 | - $c[$i] = $c[$i-1]; |
|
| 198 | - $d[$i] = $d[$i-1]; |
|
| 199 | - |
|
| 200 | - for($j = 0; $j < parent::$_key_sched[$i-1]; ++$j) |
|
| 201 | - { |
|
| 202 | - // do a left shift, move each bit one place to the left, |
|
| 203 | - // except for the first bit, which is cycled to the end |
|
| 204 | - // of the block. |
|
| 205 | - $c[$i][] = array_shift($c[$i]); |
|
| 206 | - $d[$i][] = array_shift($d[$i]); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
| 210 | - // following permutation table to each of the concatenated |
|
| 211 | - // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
| 212 | - // of these. |
|
| 213 | - $CnDn = array_merge($c[$i], $d[$i]); |
|
| 214 | - $this->sub_keys[$i-1] = ""; |
|
| 215 | - for($j = 0; $j < 48; ++$j) |
|
| 216 | - $this->sub_keys[$i-1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // the sub_keys are created, we are done with the key permutation |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Triple DES accepts an 8, 16, or 24 bytes key. If the key is |
|
| 225 | - * 8 or 16 bytes, it is expanded to 24 bytes |
|
| 226 | - * An 8 byte key is expanded by repeating it 3 times to make a 24 |
|
| 227 | - * byte key |
|
| 228 | - * A 16 byte key add the first 8 bytes to the end of the string |
|
| 229 | - * to make a 24 byte key |
|
| 230 | - * |
|
| 231 | - * @param string $key The 8 or 16 byte key to expand |
|
| 232 | - * @return string If the key given is 8 or 16 bytes it returns the |
|
| 233 | - * expanded 24 byte key, else it returns the original key unexpanded |
|
| 234 | - */ |
|
| 235 | - private static function expandKey($key, $len) |
|
| 236 | - { |
|
| 237 | - // if we were given an 8 byte key, repeat it |
|
| 238 | - // 3 times to produce a 24 byte key |
|
| 239 | - if($len == 8) |
|
| 240 | - $key = str_repeat($key, 3); |
|
| 241 | - |
|
| 242 | - // if we were given a 16 byte key, add the first |
|
| 243 | - // 8 bytes to the end of the key to produce 24 bytes |
|
| 244 | - if($len == 16) |
|
| 245 | - $key .= substr($key, 0, 8); |
|
| 246 | - |
|
| 247 | - // return the key |
|
| 248 | - return $key; |
|
| 249 | - } |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // split $pc1m in half (C0 and D0) |
|
| 189 | + $c[0] = array_slice($pc1m, 0, 28); |
|
| 190 | + $d[0] = array_slice($pc1m, 28, 28); |
|
| 191 | + |
|
| 192 | + // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
| 193 | + // where 1 <= n <= 16 |
|
| 194 | + for($i = 1; $i <= 16; ++$i) |
|
| 195 | + { |
|
| 196 | + // now set the next Cn and Dn as the previous Cn and Dn |
|
| 197 | + $c[$i] = $c[$i-1]; |
|
| 198 | + $d[$i] = $d[$i-1]; |
|
| 199 | + |
|
| 200 | + for($j = 0; $j < parent::$_key_sched[$i-1]; ++$j) |
|
| 201 | + { |
|
| 202 | + // do a left shift, move each bit one place to the left, |
|
| 203 | + // except for the first bit, which is cycled to the end |
|
| 204 | + // of the block. |
|
| 205 | + $c[$i][] = array_shift($c[$i]); |
|
| 206 | + $d[$i][] = array_shift($d[$i]); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
| 210 | + // following permutation table to each of the concatenated |
|
| 211 | + // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
| 212 | + // of these. |
|
| 213 | + $CnDn = array_merge($c[$i], $d[$i]); |
|
| 214 | + $this->sub_keys[$i-1] = ""; |
|
| 215 | + for($j = 0; $j < 48; ++$j) |
|
| 216 | + $this->sub_keys[$i-1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // the sub_keys are created, we are done with the key permutation |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Triple DES accepts an 8, 16, or 24 bytes key. If the key is |
|
| 225 | + * 8 or 16 bytes, it is expanded to 24 bytes |
|
| 226 | + * An 8 byte key is expanded by repeating it 3 times to make a 24 |
|
| 227 | + * byte key |
|
| 228 | + * A 16 byte key add the first 8 bytes to the end of the string |
|
| 229 | + * to make a 24 byte key |
|
| 230 | + * |
|
| 231 | + * @param string $key The 8 or 16 byte key to expand |
|
| 232 | + * @return string If the key given is 8 or 16 bytes it returns the |
|
| 233 | + * expanded 24 byte key, else it returns the original key unexpanded |
|
| 234 | + */ |
|
| 235 | + private static function expandKey($key, $len) |
|
| 236 | + { |
|
| 237 | + // if we were given an 8 byte key, repeat it |
|
| 238 | + // 3 times to produce a 24 byte key |
|
| 239 | + if($len == 8) |
|
| 240 | + $key = str_repeat($key, 3); |
|
| 241 | + |
|
| 242 | + // if we were given a 16 byte key, add the first |
|
| 243 | + // 8 bytes to the end of the key to produce 24 bytes |
|
| 244 | + if($len == 16) |
|
| 245 | + $key .= substr($key, 0, 8); |
|
| 246 | + |
|
| 247 | + // return the key |
|
| 248 | + return $key; |
|
| 249 | + } |
|
| 250 | 250 | } |
| 251 | 251 | ?> |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | { |
| 62 | 62 | $key_len = strlen($key); |
| 63 | 63 | |
| 64 | - if($key_len == 8 || $key_len == 16) |
|
| 64 | + if ($key_len == 8 || $key_len == 16) |
|
| 65 | 65 | $key = self::expandKey($key, $key_len); |
| 66 | - else if($key_len < self::BYTES_KEY) |
|
| 66 | + else if ($key_len < self::BYTES_KEY) |
|
| 67 | 67 | { |
| 68 | 68 | $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
| 69 | 69 | $msg .= "$key_len bytes received."; |
@@ -105,12 +105,12 @@ discard block |
||
| 105 | 105 | { |
| 106 | 106 | $blocksz = $this->blockSize(); |
| 107 | 107 | |
| 108 | - for($i = 0; $i < 3; ++$i) |
|
| 108 | + for ($i = 0; $i < 3; ++$i) |
|
| 109 | 109 | { |
| 110 | 110 | $key = substr($this->key(), ($i * 8), $blocksz); |
| 111 | 111 | $this->keyPermutation($key); |
| 112 | 112 | |
| 113 | - if($i % 2) // round 1 |
|
| 113 | + if ($i % 2) // round 1 |
|
| 114 | 114 | $this->operation(parent::DECRYPT); |
| 115 | 115 | else // rounds 0 and 2 |
| 116 | 116 | $this->operation(parent::ENCRYPT); |
@@ -135,12 +135,12 @@ discard block |
||
| 135 | 135 | { |
| 136 | 136 | $blocksz = $this->blockSize(); |
| 137 | 137 | |
| 138 | - for($i = 2; $i >= 0; --$i) |
|
| 138 | + for ($i = 2; $i >= 0; --$i) |
|
| 139 | 139 | { |
| 140 | 140 | $key = substr($this->key(), ($i * 8), $blocksz); |
| 141 | 141 | $this->keyPermutation($key); |
| 142 | 142 | |
| 143 | - if($i % 2) // round 1 |
|
| 143 | + if ($i % 2) // round 1 |
|
| 144 | 144 | $this->operation(parent::ENCRYPT); |
| 145 | 145 | else // round 0 and 2 |
| 146 | 146 | $this->operation(parent::DECRYPT); |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | $binkey = parent::str2Bin($key); |
| 180 | 180 | |
| 181 | 181 | // reduce the key down to 56bits based on table $_pc1 |
| 182 | - for($i = 0; $i < 56; ++$i) |
|
| 182 | + for ($i = 0; $i < 56; ++$i) |
|
| 183 | 183 | { |
| 184 | 184 | $pos = parent::$_pc1[$i] - 1; |
| 185 | 185 | $pc1m[$i] = $binkey[$pos]; |
@@ -191,13 +191,13 @@ discard block |
||
| 191 | 191 | |
| 192 | 192 | // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
| 193 | 193 | // where 1 <= n <= 16 |
| 194 | - for($i = 1; $i <= 16; ++$i) |
|
| 194 | + for ($i = 1; $i <= 16; ++$i) |
|
| 195 | 195 | { |
| 196 | 196 | // now set the next Cn and Dn as the previous Cn and Dn |
| 197 | - $c[$i] = $c[$i-1]; |
|
| 198 | - $d[$i] = $d[$i-1]; |
|
| 197 | + $c[$i] = $c[$i - 1]; |
|
| 198 | + $d[$i] = $d[$i - 1]; |
|
| 199 | 199 | |
| 200 | - for($j = 0; $j < parent::$_key_sched[$i-1]; ++$j) |
|
| 200 | + for ($j = 0; $j < parent::$_key_sched[$i - 1]; ++$j) |
|
| 201 | 201 | { |
| 202 | 202 | // do a left shift, move each bit one place to the left, |
| 203 | 203 | // except for the first bit, which is cycled to the end |
@@ -211,9 +211,9 @@ discard block |
||
| 211 | 211 | // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
| 212 | 212 | // of these. |
| 213 | 213 | $CnDn = array_merge($c[$i], $d[$i]); |
| 214 | - $this->sub_keys[$i-1] = ""; |
|
| 215 | - for($j = 0; $j < 48; ++$j) |
|
| 216 | - $this->sub_keys[$i-1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
| 214 | + $this->sub_keys[$i - 1] = ""; |
|
| 215 | + for ($j = 0; $j < 48; ++$j) |
|
| 216 | + $this->sub_keys[$i - 1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | // the sub_keys are created, we are done with the key permutation |
@@ -236,12 +236,12 @@ discard block |
||
| 236 | 236 | { |
| 237 | 237 | // if we were given an 8 byte key, repeat it |
| 238 | 238 | // 3 times to produce a 24 byte key |
| 239 | - if($len == 8) |
|
| 239 | + if ($len == 8) |
|
| 240 | 240 | $key = str_repeat($key, 3); |
| 241 | 241 | |
| 242 | 242 | // if we were given a 16 byte key, add the first |
| 243 | 243 | // 8 bytes to the end of the key to produce 24 bytes |
| 244 | - if($len == 16) |
|
| 244 | + if ($len == 16) |
|
| 245 | 245 | $key .= substr($key, 0, 8); |
| 246 | 246 | |
| 247 | 247 | // return the key |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | { |
| 62 | 62 | $key_len = strlen($key); |
| 63 | 63 | |
| 64 | - if($key_len == 8 || $key_len == 16) |
|
| 65 | - $key = self::expandKey($key, $key_len); |
|
| 66 | - else if($key_len < self::BYTES_KEY) |
|
| 64 | + if($key_len == 8 || $key_len == 16) { |
|
| 65 | + $key = self::expandKey($key, $key_len); |
|
| 66 | + } else if($key_len < self::BYTES_KEY) |
|
| 67 | 67 | { |
| 68 | 68 | $msg = PHP_Crypt::CIPHER_3DES." requires an 8, 16, or 24 byte key. "; |
| 69 | 69 | $msg .= "$key_len bytes received."; |
@@ -110,10 +110,13 @@ discard block |
||
| 110 | 110 | $key = substr($this->key(), ($i * 8), $blocksz); |
| 111 | 111 | $this->keyPermutation($key); |
| 112 | 112 | |
| 113 | - if($i % 2) // round 1 |
|
| 113 | + if($i % 2) { |
|
| 114 | + // round 1 |
|
| 114 | 115 | $this->operation(parent::DECRYPT); |
| 115 | - else // rounds 0 and 2 |
|
| 116 | + } else { |
|
| 117 | + // rounds 0 and 2 |
|
| 116 | 118 | $this->operation(parent::ENCRYPT); |
| 119 | + } |
|
| 117 | 120 | |
| 118 | 121 | $this->des($text); |
| 119 | 122 | } |
@@ -140,10 +143,13 @@ discard block |
||
| 140 | 143 | $key = substr($this->key(), ($i * 8), $blocksz); |
| 141 | 144 | $this->keyPermutation($key); |
| 142 | 145 | |
| 143 | - if($i % 2) // round 1 |
|
| 146 | + if($i % 2) { |
|
| 147 | + // round 1 |
|
| 144 | 148 | $this->operation(parent::ENCRYPT); |
| 145 | - else // round 0 and 2 |
|
| 149 | + } else { |
|
| 150 | + // round 0 and 2 |
|
| 146 | 151 | $this->operation(parent::DECRYPT); |
| 152 | + } |
|
| 147 | 153 | |
| 148 | 154 | $this->des($text); |
| 149 | 155 | } |
@@ -212,8 +218,9 @@ discard block |
||
| 212 | 218 | // of these. |
| 213 | 219 | $CnDn = array_merge($c[$i], $d[$i]); |
| 214 | 220 | $this->sub_keys[$i-1] = ""; |
| 215 | - for($j = 0; $j < 48; ++$j) |
|
| 216 | - $this->sub_keys[$i-1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
| 221 | + for($j = 0; $j < 48; ++$j) { |
|
| 222 | + $this->sub_keys[$i-1] .= $CnDn[parent::$_pc2[$j] - 1]; |
|
| 223 | + } |
|
| 217 | 224 | } |
| 218 | 225 | |
| 219 | 226 | // the sub_keys are created, we are done with the key permutation |
@@ -236,13 +243,15 @@ discard block |
||
| 236 | 243 | { |
| 237 | 244 | // if we were given an 8 byte key, repeat it |
| 238 | 245 | // 3 times to produce a 24 byte key |
| 239 | - if($len == 8) |
|
| 240 | - $key = str_repeat($key, 3); |
|
| 246 | + if($len == 8) { |
|
| 247 | + $key = str_repeat($key, 3); |
|
| 248 | + } |
|
| 241 | 249 | |
| 242 | 250 | // if we were given a 16 byte key, add the first |
| 243 | 251 | // 8 bytes to the end of the key to produce 24 bytes |
| 244 | - if($len == 16) |
|
| 245 | - $key .= substr($key, 0, 8); |
|
| 252 | + if($len == 16) { |
|
| 253 | + $key .= substr($key, 0, 8); |
|
| 254 | + } |
|
| 246 | 255 | |
| 247 | 256 | // return the key |
| 248 | 257 | return $key; |
@@ -247,7 +247,7 @@ |
||
| 247 | 247 | * NOTE: Please read the comments in the $this->gamma() function. This |
| 248 | 248 | * function calls the $this->gamma() function which does not do anything. |
| 249 | 249 | * |
| 250 | - * @param array $d A 3 element 32 bit integer array |
|
| 250 | + * @param integer[] $d A 3 element 32 bit integer array |
|
| 251 | 251 | * @return void |
| 252 | 252 | */ |
| 253 | 253 | private function rho(&$d) |
@@ -51,186 +51,186 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | class Cipher_3Way extends Cipher |
| 53 | 53 | { |
| 54 | - /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 55 | - const BYTES_BLOCK = 12; // 96 bits; |
|
| 56 | - |
|
| 57 | - /** @type integer BYTES_KEY The size of the key, in bytes */ |
|
| 58 | - const BYTES_KEY = 12; // 96 bits; |
|
| 59 | - |
|
| 60 | - /** @type integer ROUNDS The number of rounds to implement */ |
|
| 61 | - const ROUNDS = 11; |
|
| 62 | - |
|
| 63 | - /** @type array $_rconst_enc The round constants for encryption */ |
|
| 64 | - private static $_rcon_enc = array(); |
|
| 65 | - |
|
| 66 | - /** @type array $_rconst_enc The round constants for decryption */ |
|
| 67 | - private static $_rcon_dec = array(); |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Constructor |
|
| 72 | - * |
|
| 73 | - * @param string $key The key used for Encryption/Decryption |
|
| 74 | - * @return void |
|
| 75 | - */ |
|
| 76 | - public function __construct($key) |
|
| 77 | - { |
|
| 78 | - // set the key, make sure the required length is set in bytes |
|
| 79 | - parent::__construct(PHP_Crypt::CIPHER_3WAY, $key, self::BYTES_KEY); |
|
| 80 | - |
|
| 81 | - // set the block size |
|
| 82 | - $this->blockSize(self::BYTES_BLOCK); |
|
| 83 | - |
|
| 84 | - // initialize the round constants |
|
| 85 | - $this->initTables(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Destructor |
|
| 91 | - * |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - public function __destruct() |
|
| 95 | - { |
|
| 96 | - parent::__destruct(); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Encrypt plain text data |
|
| 102 | - * |
|
| 103 | - * @param string $data A 96 bit plain data |
|
| 104 | - * @return boolean Returns true |
|
| 105 | - */ |
|
| 106 | - public function encrypt(&$data) |
|
| 107 | - { |
|
| 108 | - $this->operation(parent::ENCRYPT); |
|
| 109 | - return $this->threeway($data); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Decrypt an encrypted string |
|
| 115 | - * |
|
| 116 | - * @param string $data A 96 bit block encrypted data |
|
| 117 | - * @return boolean Returns true |
|
| 118 | - */ |
|
| 119 | - public function decrypt(&$data) |
|
| 120 | - { |
|
| 121 | - $this->operation(parent::DECRYPT); |
|
| 122 | - return $this->threeway($data); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * The same alorigthm is used for both Encryption, and Decryption |
|
| 128 | - * |
|
| 129 | - * @param string $data A 96 bit block of data |
|
| 130 | - * @return boolean Returns true |
|
| 131 | - */ |
|
| 132 | - private function threeway(&$data) |
|
| 133 | - { |
|
| 134 | - // first split $data into three 32 bit parts |
|
| 135 | - $data = str_split($data, 4); |
|
| 136 | - $data = array_map("parent::str2Dec", $data); |
|
| 137 | - |
|
| 138 | - // split the key into three 32 bit parts |
|
| 139 | - $key = str_split($this->key(), 4); |
|
| 140 | - $key = array_map("parent::str2Dec", $key); |
|
| 141 | - |
|
| 142 | - // determine which round constant to use |
|
| 143 | - if($this->operation() == parent::ENCRYPT) |
|
| 144 | - $rcon = self::$_rcon_enc; |
|
| 145 | - else |
|
| 146 | - $rcon = self::$_rcon_dec; |
|
| 147 | - |
|
| 148 | - if($this->operation() == parent::DECRYPT) |
|
| 149 | - { |
|
| 150 | - $this->theta($key); |
|
| 151 | - $this->invertBits($key); |
|
| 152 | - $this->invertBits($data); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // 3Way uses 11 rounds |
|
| 156 | - for($i = 0; $i < self::ROUNDS; ++$i) |
|
| 157 | - { |
|
| 158 | - $data[0] = parent::uInt32($data[0] ^ $key[0] ^ ($rcon[$i] << 16)); |
|
| 159 | - $data[1] = parent::uInt32($data[1] ^ $key[1]); |
|
| 160 | - $data[2] = parent::uInt32($data[2] ^ $key[2] ^ $rcon[$i]); |
|
| 161 | - |
|
| 162 | - $this->rho($data); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $data[0] = parent::uInt32($data[0] ^ $key[0] ^ ($rcon[self::ROUNDS] << 16)); |
|
| 166 | - $data[1] = parent::uInt32($data[1] ^ $key[1]); |
|
| 167 | - $data[2] = parent::uInt32($data[2] ^ $key[2] ^ $rcon[self::ROUNDS]); |
|
| 168 | - |
|
| 169 | - $this->theta($data); |
|
| 170 | - |
|
| 171 | - if($this->operation() == parent::DECRYPT) |
|
| 172 | - $this->invertBits($data); |
|
| 173 | - |
|
| 174 | - // assemble the three 32 bit parts back to a 96 bit string |
|
| 175 | - $data = parent::dec2Str($data[0], 4).parent::dec2Str($data[1], 4). |
|
| 176 | - parent::dec2Str($data[2], 4); |
|
| 177 | - |
|
| 178 | - return true; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * 3-Way's Theta function |
|
| 184 | - * This was translated from mcrypt's 3-Way theta() function |
|
| 185 | - * |
|
| 186 | - * @param array $d A 3 element array of 32 bit integers |
|
| 187 | - * @return void |
|
| 188 | - */ |
|
| 189 | - private function theta(&$d) |
|
| 190 | - { |
|
| 191 | - $tmp = array(); |
|
| 192 | - |
|
| 193 | - $tmp[0] = parent::uInt32( |
|
| 194 | - $d[0] ^ ($d[0] >> 16) ^ ($d[1] << 16) ^ ($d[1] >> 16) ^ ($d[2] << 16) ^ |
|
| 195 | - ($d[1] >> 24) ^ ($d[2] << 8) ^ ($d[2] >> 8) ^ ($d[0] << 24) ^ ($d[2] >> 16) ^ |
|
| 196 | - ($d[0] << 16) ^ ($d[2] >> 24) ^ ($d[0] << 8) |
|
| 197 | - ); |
|
| 198 | - |
|
| 199 | - $tmp[1] = parent::uInt32( |
|
| 200 | - $d[1] ^ ($d[1] >> 16) ^ ($d[2] << 16) ^ ($d[2] >> 16) ^ ($d[0] << 16) ^ |
|
| 201 | - ($d[2] >> 24) ^ ($d[0] << 8) ^ ($d[0] >> 8) ^ ($d[1] << 24) ^ ($d[0] >> 16) ^ |
|
| 202 | - ($d[1] << 16) ^ ($d[0] >> 24) ^ ($d[1] << 8) |
|
| 203 | - ); |
|
| 204 | - |
|
| 205 | - $tmp[2] = parent::uInt32( |
|
| 206 | - $d[2] ^ ($d[2] >> 16) ^ ($d[0] << 16) ^ ($d[0] >> 16) ^ ($d[1] << 16) ^ |
|
| 207 | - ($d[0] >> 24) ^ ($d[1] << 8) ^ ($d[1] >> 8) ^ ($d[2] << 24) ^ ($d[1] >> 16) ^ |
|
| 208 | - ($d[2] << 16) ^ ($d[1] >> 24) ^ ($d[2] << 8) |
|
| 209 | - ); |
|
| 210 | - |
|
| 211 | - $d = $tmp; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * 3-Ways gamma() function |
|
| 217 | - * NOTE: After extensive testing against mcrypt's 3way, it appears |
|
| 218 | - * mcrypt's 3way gamma() function does not modify the array passed into |
|
| 219 | - * it. During testing mcrypt's 3way gamma() function returned the exact |
|
| 220 | - * same values sent into it. I'm confused as to why this is, and |
|
| 221 | - * can't find enough information about 3-Way to know if this is correct |
|
| 222 | - * though I suspect it's not. For compatibility, I am going to make |
|
| 223 | - * phpCrypt's 3way gamma() function leave the values unmodified. I will change |
|
| 224 | - * this at a later date if I find this is incorrect and mcrypt has a bug |
|
| 225 | - * |
|
| 226 | - * This was translated from mcrypt's 3-Way gamma() function |
|
| 227 | - * |
|
| 228 | - * @param array $d A 3 element array of 32 bit integers |
|
| 229 | - * @return void |
|
| 230 | - */ |
|
| 231 | - private function gamma(&$d) |
|
| 232 | - { |
|
| 233 | - /* |
|
| 54 | + /** @type integer BYTES_BLOCK The size of the block, in bytes */ |
|
| 55 | + const BYTES_BLOCK = 12; // 96 bits; |
|
| 56 | + |
|
| 57 | + /** @type integer BYTES_KEY The size of the key, in bytes */ |
|
| 58 | + const BYTES_KEY = 12; // 96 bits; |
|
| 59 | + |
|
| 60 | + /** @type integer ROUNDS The number of rounds to implement */ |
|
| 61 | + const ROUNDS = 11; |
|
| 62 | + |
|
| 63 | + /** @type array $_rconst_enc The round constants for encryption */ |
|
| 64 | + private static $_rcon_enc = array(); |
|
| 65 | + |
|
| 66 | + /** @type array $_rconst_enc The round constants for decryption */ |
|
| 67 | + private static $_rcon_dec = array(); |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Constructor |
|
| 72 | + * |
|
| 73 | + * @param string $key The key used for Encryption/Decryption |
|
| 74 | + * @return void |
|
| 75 | + */ |
|
| 76 | + public function __construct($key) |
|
| 77 | + { |
|
| 78 | + // set the key, make sure the required length is set in bytes |
|
| 79 | + parent::__construct(PHP_Crypt::CIPHER_3WAY, $key, self::BYTES_KEY); |
|
| 80 | + |
|
| 81 | + // set the block size |
|
| 82 | + $this->blockSize(self::BYTES_BLOCK); |
|
| 83 | + |
|
| 84 | + // initialize the round constants |
|
| 85 | + $this->initTables(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Destructor |
|
| 91 | + * |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + public function __destruct() |
|
| 95 | + { |
|
| 96 | + parent::__destruct(); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Encrypt plain text data |
|
| 102 | + * |
|
| 103 | + * @param string $data A 96 bit plain data |
|
| 104 | + * @return boolean Returns true |
|
| 105 | + */ |
|
| 106 | + public function encrypt(&$data) |
|
| 107 | + { |
|
| 108 | + $this->operation(parent::ENCRYPT); |
|
| 109 | + return $this->threeway($data); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Decrypt an encrypted string |
|
| 115 | + * |
|
| 116 | + * @param string $data A 96 bit block encrypted data |
|
| 117 | + * @return boolean Returns true |
|
| 118 | + */ |
|
| 119 | + public function decrypt(&$data) |
|
| 120 | + { |
|
| 121 | + $this->operation(parent::DECRYPT); |
|
| 122 | + return $this->threeway($data); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * The same alorigthm is used for both Encryption, and Decryption |
|
| 128 | + * |
|
| 129 | + * @param string $data A 96 bit block of data |
|
| 130 | + * @return boolean Returns true |
|
| 131 | + */ |
|
| 132 | + private function threeway(&$data) |
|
| 133 | + { |
|
| 134 | + // first split $data into three 32 bit parts |
|
| 135 | + $data = str_split($data, 4); |
|
| 136 | + $data = array_map("parent::str2Dec", $data); |
|
| 137 | + |
|
| 138 | + // split the key into three 32 bit parts |
|
| 139 | + $key = str_split($this->key(), 4); |
|
| 140 | + $key = array_map("parent::str2Dec", $key); |
|
| 141 | + |
|
| 142 | + // determine which round constant to use |
|
| 143 | + if($this->operation() == parent::ENCRYPT) |
|
| 144 | + $rcon = self::$_rcon_enc; |
|
| 145 | + else |
|
| 146 | + $rcon = self::$_rcon_dec; |
|
| 147 | + |
|
| 148 | + if($this->operation() == parent::DECRYPT) |
|
| 149 | + { |
|
| 150 | + $this->theta($key); |
|
| 151 | + $this->invertBits($key); |
|
| 152 | + $this->invertBits($data); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // 3Way uses 11 rounds |
|
| 156 | + for($i = 0; $i < self::ROUNDS; ++$i) |
|
| 157 | + { |
|
| 158 | + $data[0] = parent::uInt32($data[0] ^ $key[0] ^ ($rcon[$i] << 16)); |
|
| 159 | + $data[1] = parent::uInt32($data[1] ^ $key[1]); |
|
| 160 | + $data[2] = parent::uInt32($data[2] ^ $key[2] ^ $rcon[$i]); |
|
| 161 | + |
|
| 162 | + $this->rho($data); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $data[0] = parent::uInt32($data[0] ^ $key[0] ^ ($rcon[self::ROUNDS] << 16)); |
|
| 166 | + $data[1] = parent::uInt32($data[1] ^ $key[1]); |
|
| 167 | + $data[2] = parent::uInt32($data[2] ^ $key[2] ^ $rcon[self::ROUNDS]); |
|
| 168 | + |
|
| 169 | + $this->theta($data); |
|
| 170 | + |
|
| 171 | + if($this->operation() == parent::DECRYPT) |
|
| 172 | + $this->invertBits($data); |
|
| 173 | + |
|
| 174 | + // assemble the three 32 bit parts back to a 96 bit string |
|
| 175 | + $data = parent::dec2Str($data[0], 4).parent::dec2Str($data[1], 4). |
|
| 176 | + parent::dec2Str($data[2], 4); |
|
| 177 | + |
|
| 178 | + return true; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * 3-Way's Theta function |
|
| 184 | + * This was translated from mcrypt's 3-Way theta() function |
|
| 185 | + * |
|
| 186 | + * @param array $d A 3 element array of 32 bit integers |
|
| 187 | + * @return void |
|
| 188 | + */ |
|
| 189 | + private function theta(&$d) |
|
| 190 | + { |
|
| 191 | + $tmp = array(); |
|
| 192 | + |
|
| 193 | + $tmp[0] = parent::uInt32( |
|
| 194 | + $d[0] ^ ($d[0] >> 16) ^ ($d[1] << 16) ^ ($d[1] >> 16) ^ ($d[2] << 16) ^ |
|
| 195 | + ($d[1] >> 24) ^ ($d[2] << 8) ^ ($d[2] >> 8) ^ ($d[0] << 24) ^ ($d[2] >> 16) ^ |
|
| 196 | + ($d[0] << 16) ^ ($d[2] >> 24) ^ ($d[0] << 8) |
|
| 197 | + ); |
|
| 198 | + |
|
| 199 | + $tmp[1] = parent::uInt32( |
|
| 200 | + $d[1] ^ ($d[1] >> 16) ^ ($d[2] << 16) ^ ($d[2] >> 16) ^ ($d[0] << 16) ^ |
|
| 201 | + ($d[2] >> 24) ^ ($d[0] << 8) ^ ($d[0] >> 8) ^ ($d[1] << 24) ^ ($d[0] >> 16) ^ |
|
| 202 | + ($d[1] << 16) ^ ($d[0] >> 24) ^ ($d[1] << 8) |
|
| 203 | + ); |
|
| 204 | + |
|
| 205 | + $tmp[2] = parent::uInt32( |
|
| 206 | + $d[2] ^ ($d[2] >> 16) ^ ($d[0] << 16) ^ ($d[0] >> 16) ^ ($d[1] << 16) ^ |
|
| 207 | + ($d[0] >> 24) ^ ($d[1] << 8) ^ ($d[1] >> 8) ^ ($d[2] << 24) ^ ($d[1] >> 16) ^ |
|
| 208 | + ($d[2] << 16) ^ ($d[1] >> 24) ^ ($d[2] << 8) |
|
| 209 | + ); |
|
| 210 | + |
|
| 211 | + $d = $tmp; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * 3-Ways gamma() function |
|
| 217 | + * NOTE: After extensive testing against mcrypt's 3way, it appears |
|
| 218 | + * mcrypt's 3way gamma() function does not modify the array passed into |
|
| 219 | + * it. During testing mcrypt's 3way gamma() function returned the exact |
|
| 220 | + * same values sent into it. I'm confused as to why this is, and |
|
| 221 | + * can't find enough information about 3-Way to know if this is correct |
|
| 222 | + * though I suspect it's not. For compatibility, I am going to make |
|
| 223 | + * phpCrypt's 3way gamma() function leave the values unmodified. I will change |
|
| 224 | + * this at a later date if I find this is incorrect and mcrypt has a bug |
|
| 225 | + * |
|
| 226 | + * This was translated from mcrypt's 3-Way gamma() function |
|
| 227 | + * |
|
| 228 | + * @param array $d A 3 element array of 32 bit integers |
|
| 229 | + * @return void |
|
| 230 | + */ |
|
| 231 | + private function gamma(&$d) |
|
| 232 | + { |
|
| 233 | + /* |
|
| 234 | 234 | $tmp = array(); |
| 235 | 235 | |
| 236 | 236 | $tmp[0] = parent::uInt32($d[0] ^ ($d[1] | (~$d[2]))); |
@@ -239,102 +239,102 @@ discard block |
||
| 239 | 239 | |
| 240 | 240 | $d = $tmp; |
| 241 | 241 | */ |
| 242 | - } |
|
| 243 | - |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Applies several of 3Way's functions used for encryption and decryption |
|
| 247 | - * NOTE: Please read the comments in the $this->gamma() function. This |
|
| 248 | - * function calls the $this->gamma() function which does not do anything. |
|
| 249 | - * |
|
| 250 | - * @param array $d A 3 element 32 bit integer array |
|
| 251 | - * @return void |
|
| 252 | - */ |
|
| 253 | - private function rho(&$d) |
|
| 254 | - { |
|
| 255 | - $this->theta($d); |
|
| 256 | - $this->pi1($d); |
|
| 257 | - $this->gamma($d); |
|
| 258 | - $this->pi2($d); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * 3Way's PI_1 function |
|
| 264 | - * This was taken from mcrypt's 3-way pi_1() function |
|
| 265 | - * |
|
| 266 | - * @param array $d A 3 element 32 bit integer array |
|
| 267 | - * @return void |
|
| 268 | - */ |
|
| 269 | - private function pi1(&$d) |
|
| 270 | - { |
|
| 271 | - $d[0] = parent::uInt32(($d[0] >> 10) ^ ($d[0] << 22)); |
|
| 272 | - $d[2] = parent::uInt32(($d[2] << 1) ^ ($d[2] >> 31)); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * 3Way's PI_2 function |
|
| 278 | - * This was taken from mcrypt's 3-way pi_2() function |
|
| 279 | - * |
|
| 280 | - * @param array $d A 3 element 32 bit integer array |
|
| 281 | - * @return void |
|
| 282 | - */ |
|
| 283 | - private function pi2(&$d) |
|
| 284 | - { |
|
| 285 | - $d[0] = parent::uInt32(($d[0] << 1) ^ ($d[0] >> 31)); |
|
| 286 | - $d[2] = parent::uInt32(($d[2] >> 10) ^ ($d[2] << 22)); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Reverse the bits of each element of array $d, and |
|
| 292 | - * reverses the order of array $d, used only during |
|
| 293 | - * decryption |
|
| 294 | - * |
|
| 295 | - * @param array $d A 3 element array of a 32 bit integers |
|
| 296 | - * @return void |
|
| 297 | - */ |
|
| 298 | - private function invertBits(&$d) |
|
| 299 | - { |
|
| 300 | - $d = array_map("parent::dec2Bin", $d); |
|
| 301 | - $d = array_map("strrev", $d); |
|
| 302 | - $d = array_map("parent::bin2Dec", $d); |
|
| 303 | - $d = array_reverse($d); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Initialize the tables used in 3Way Encryption. |
|
| 309 | - * |
|
| 310 | - * @return void |
|
| 311 | - */ |
|
| 312 | - private function initTables() |
|
| 313 | - { |
|
| 314 | - // round constants for encryption |
|
| 315 | - self::$_rcon_enc = array( |
|
| 316 | - 0x0B0B, 0x1616, 0x2C2C, 0x5858, |
|
| 317 | - 0xB0B0, 0x7171, 0xE2E2, 0xD5D5, |
|
| 318 | - 0xBBBB, 0x6767, 0xCECE, 0x8D8D |
|
| 319 | - ); |
|
| 320 | - |
|
| 321 | - // round constants for decryption |
|
| 322 | - self::$_rcon_dec = array( |
|
| 323 | - 0xB1B1, 0x7373, 0xE6E6, 0xDDDD, |
|
| 324 | - 0xABAB, 0x4747, 0x8E8E, 0x0D0D, |
|
| 325 | - 0x1A1A, 0x3434, 0x6868, 0xD0D0 |
|
| 326 | - ); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Indicates this is a block cipher |
|
| 332 | - * |
|
| 333 | - * @return integer Returns Cipher::BLOCK |
|
| 334 | - */ |
|
| 335 | - public function type() |
|
| 336 | - { |
|
| 337 | - return parent::BLOCK; |
|
| 338 | - } |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Applies several of 3Way's functions used for encryption and decryption |
|
| 247 | + * NOTE: Please read the comments in the $this->gamma() function. This |
|
| 248 | + * function calls the $this->gamma() function which does not do anything. |
|
| 249 | + * |
|
| 250 | + * @param array $d A 3 element 32 bit integer array |
|
| 251 | + * @return void |
|
| 252 | + */ |
|
| 253 | + private function rho(&$d) |
|
| 254 | + { |
|
| 255 | + $this->theta($d); |
|
| 256 | + $this->pi1($d); |
|
| 257 | + $this->gamma($d); |
|
| 258 | + $this->pi2($d); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * 3Way's PI_1 function |
|
| 264 | + * This was taken from mcrypt's 3-way pi_1() function |
|
| 265 | + * |
|
| 266 | + * @param array $d A 3 element 32 bit integer array |
|
| 267 | + * @return void |
|
| 268 | + */ |
|
| 269 | + private function pi1(&$d) |
|
| 270 | + { |
|
| 271 | + $d[0] = parent::uInt32(($d[0] >> 10) ^ ($d[0] << 22)); |
|
| 272 | + $d[2] = parent::uInt32(($d[2] << 1) ^ ($d[2] >> 31)); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * 3Way's PI_2 function |
|
| 278 | + * This was taken from mcrypt's 3-way pi_2() function |
|
| 279 | + * |
|
| 280 | + * @param array $d A 3 element 32 bit integer array |
|
| 281 | + * @return void |
|
| 282 | + */ |
|
| 283 | + private function pi2(&$d) |
|
| 284 | + { |
|
| 285 | + $d[0] = parent::uInt32(($d[0] << 1) ^ ($d[0] >> 31)); |
|
| 286 | + $d[2] = parent::uInt32(($d[2] >> 10) ^ ($d[2] << 22)); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Reverse the bits of each element of array $d, and |
|
| 292 | + * reverses the order of array $d, used only during |
|
| 293 | + * decryption |
|
| 294 | + * |
|
| 295 | + * @param array $d A 3 element array of a 32 bit integers |
|
| 296 | + * @return void |
|
| 297 | + */ |
|
| 298 | + private function invertBits(&$d) |
|
| 299 | + { |
|
| 300 | + $d = array_map("parent::dec2Bin", $d); |
|
| 301 | + $d = array_map("strrev", $d); |
|
| 302 | + $d = array_map("parent::bin2Dec", $d); |
|
| 303 | + $d = array_reverse($d); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Initialize the tables used in 3Way Encryption. |
|
| 309 | + * |
|
| 310 | + * @return void |
|
| 311 | + */ |
|
| 312 | + private function initTables() |
|
| 313 | + { |
|
| 314 | + // round constants for encryption |
|
| 315 | + self::$_rcon_enc = array( |
|
| 316 | + 0x0B0B, 0x1616, 0x2C2C, 0x5858, |
|
| 317 | + 0xB0B0, 0x7171, 0xE2E2, 0xD5D5, |
|
| 318 | + 0xBBBB, 0x6767, 0xCECE, 0x8D8D |
|
| 319 | + ); |
|
| 320 | + |
|
| 321 | + // round constants for decryption |
|
| 322 | + self::$_rcon_dec = array( |
|
| 323 | + 0xB1B1, 0x7373, 0xE6E6, 0xDDDD, |
|
| 324 | + 0xABAB, 0x4747, 0x8E8E, 0x0D0D, |
|
| 325 | + 0x1A1A, 0x3434, 0x6868, 0xD0D0 |
|
| 326 | + ); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Indicates this is a block cipher |
|
| 332 | + * |
|
| 333 | + * @return integer Returns Cipher::BLOCK |
|
| 334 | + */ |
|
| 335 | + public function type() |
|
| 336 | + { |
|
| 337 | + return parent::BLOCK; |
|
| 338 | + } |
|
| 339 | 339 | } |
| 340 | 340 | ?> |
@@ -140,12 +140,12 @@ discard block |
||
| 140 | 140 | $key = array_map("parent::str2Dec", $key); |
| 141 | 141 | |
| 142 | 142 | // determine which round constant to use |
| 143 | - if($this->operation() == parent::ENCRYPT) |
|
| 143 | + if ($this->operation() == parent::ENCRYPT) |
|
| 144 | 144 | $rcon = self::$_rcon_enc; |
| 145 | 145 | else |
| 146 | 146 | $rcon = self::$_rcon_dec; |
| 147 | 147 | |
| 148 | - if($this->operation() == parent::DECRYPT) |
|
| 148 | + if ($this->operation() == parent::DECRYPT) |
|
| 149 | 149 | { |
| 150 | 150 | $this->theta($key); |
| 151 | 151 | $this->invertBits($key); |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | // 3Way uses 11 rounds |
| 156 | - for($i = 0; $i < self::ROUNDS; ++$i) |
|
| 156 | + for ($i = 0; $i < self::ROUNDS; ++$i) |
|
| 157 | 157 | { |
| 158 | 158 | $data[0] = parent::uInt32($data[0] ^ $key[0] ^ ($rcon[$i] << 16)); |
| 159 | 159 | $data[1] = parent::uInt32($data[1] ^ $key[1]); |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | |
| 169 | 169 | $this->theta($data); |
| 170 | 170 | |
| 171 | - if($this->operation() == parent::DECRYPT) |
|
| 171 | + if ($this->operation() == parent::DECRYPT) |
|
| 172 | 172 | $this->invertBits($data); |
| 173 | 173 | |
| 174 | 174 | // assemble the three 32 bit parts back to a 96 bit string |
@@ -140,10 +140,11 @@ discard block |
||
| 140 | 140 | $key = array_map("parent::str2Dec", $key); |
| 141 | 141 | |
| 142 | 142 | // determine which round constant to use |
| 143 | - if($this->operation() == parent::ENCRYPT) |
|
| 144 | - $rcon = self::$_rcon_enc; |
|
| 145 | - else |
|
| 146 | - $rcon = self::$_rcon_dec; |
|
| 143 | + if($this->operation() == parent::ENCRYPT) { |
|
| 144 | + $rcon = self::$_rcon_enc; |
|
| 145 | + } else { |
|
| 146 | + $rcon = self::$_rcon_dec; |
|
| 147 | + } |
|
| 147 | 148 | |
| 148 | 149 | if($this->operation() == parent::DECRYPT) |
| 149 | 150 | { |
@@ -168,8 +169,9 @@ discard block |
||
| 168 | 169 | |
| 169 | 170 | $this->theta($data); |
| 170 | 171 | |
| 171 | - if($this->operation() == parent::DECRYPT) |
|
| 172 | - $this->invertBits($data); |
|
| 172 | + if($this->operation() == parent::DECRYPT) { |
|
| 173 | + $this->invertBits($data); |
|
| 174 | + } |
|
| 173 | 175 | |
| 174 | 176 | // assemble the three 32 bit parts back to a 96 bit string |
| 175 | 177 | $data = parent::dec2Str($data[0], 4).parent::dec2Str($data[1], 4). |
@@ -73,7 +73,6 @@ discard block |
||
| 73 | 73 | /** |
| 74 | 74 | * Encrypt plain text data using ARC4 |
| 75 | 75 | * |
| 76 | - * @param string $data A plain text string, 8 bytes long |
|
| 77 | 76 | * @return boolean Returns true |
| 78 | 77 | */ |
| 79 | 78 | public function encrypt(&$text) |
@@ -86,7 +85,6 @@ discard block |
||
| 86 | 85 | /** |
| 87 | 86 | * Decrypt a ARC4 encrypted string |
| 88 | 87 | * |
| 89 | - * @param string $encrypted A ARC4 encrypted string, 8 bytes long |
|
| 90 | 88 | * @return boolean Returns true |
| 91 | 89 | */ |
| 92 | 90 | public function decrypt(&$text) |
@@ -39,163 +39,163 @@ |
||
| 39 | 39 | */ |
| 40 | 40 | class Cipher_ARC4 extends Cipher |
| 41 | 41 | { |
| 42 | - /** @type string $_s The S List */ |
|
| 43 | - private $_s = ""; |
|
| 44 | - |
|
| 45 | - /** @type string $_key_stream The Key Stream */ |
|
| 46 | - private $_key_stream = ""; |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Constructor |
|
| 51 | - * |
|
| 52 | - * @param string $key The key used for Encryption/Decryption |
|
| 53 | - * @return void |
|
| 54 | - */ |
|
| 55 | - public function __construct($key) |
|
| 56 | - { |
|
| 57 | - // set the ARC4 key |
|
| 58 | - parent::__construct(PHP_Crypt::CIPHER_ARC4, $key); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Destructor |
|
| 64 | - * |
|
| 65 | - * @return void |
|
| 66 | - */ |
|
| 67 | - public function __destruct() |
|
| 68 | - { |
|
| 69 | - parent::__destruct(); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Encrypt plain text data using ARC4 |
|
| 75 | - * |
|
| 76 | - * @param string $data A plain text string, 8 bytes long |
|
| 77 | - * @return boolean Returns true |
|
| 78 | - */ |
|
| 79 | - public function encrypt(&$text) |
|
| 80 | - { |
|
| 81 | - $this->operation(parent::ENCRYPT); |
|
| 82 | - return $this->arc4($text); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Decrypt a ARC4 encrypted string |
|
| 88 | - * |
|
| 89 | - * @param string $encrypted A ARC4 encrypted string, 8 bytes long |
|
| 90 | - * @return boolean Returns true |
|
| 91 | - */ |
|
| 92 | - public function decrypt(&$text) |
|
| 93 | - { |
|
| 94 | - $this->operation(parent::DECRYPT); |
|
| 95 | - return $this->arc4($text); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Performs the ARC4 algorithm, since encryption and decryption |
|
| 101 | - * are the same, all the work is done here |
|
| 102 | - * |
|
| 103 | - * @param string $text The string to encrypt/decrypt |
|
| 104 | - * @return boolean Returns true |
|
| 105 | - */ |
|
| 106 | - private function arc4(&$text) |
|
| 107 | - { |
|
| 108 | - $len = strlen($text); |
|
| 109 | - $this->prga($len); |
|
| 110 | - |
|
| 111 | - for($i = 0; $i < $len; ++$i) |
|
| 112 | - $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
|
| 113 | - |
|
| 114 | - return true; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * The key scheduling algorithm (KSA) |
|
| 120 | - * |
|
| 121 | - * @return void |
|
| 122 | - */ |
|
| 123 | - private function ksa() |
|
| 124 | - { |
|
| 125 | - $j = 0; |
|
| 126 | - $this->_s = array(); |
|
| 127 | - $keylen = $this->keySize(); |
|
| 128 | - $key = $this->key(); |
|
| 129 | - |
|
| 130 | - // fill $this->_s with all the values from 0-255 |
|
| 131 | - for($i = 0; $i < 256; ++$i) |
|
| 132 | - $this->_s[$i] = $i; |
|
| 133 | - |
|
| 134 | - // the changing S List |
|
| 135 | - for($i = 0; $i < 256; ++$i) |
|
| 136 | - { |
|
| 137 | - $k = $key[$i % $keylen]; |
|
| 138 | - $j = ($j + $this->_s[$i] + ord($k)) % 256; |
|
| 139 | - |
|
| 140 | - // swap bytes |
|
| 141 | - self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * The Pseudo-Random Generation Algorithm (PGRA) |
|
| 148 | - * Creates the key stream used for encryption / decryption |
|
| 149 | - * |
|
| 150 | - * @param integer $data_len The length of the data we are encrypting/decrypting |
|
| 151 | - * @return void |
|
| 152 | - */ |
|
| 153 | - private function prga($data_len) |
|
| 154 | - { |
|
| 155 | - $i = 0; |
|
| 156 | - $j = 0; |
|
| 157 | - $this->_key_stream = ""; |
|
| 158 | - |
|
| 159 | - // set up the key schedule |
|
| 160 | - $this->ksa(); |
|
| 161 | - |
|
| 162 | - for($c = 0; $c < $data_len; ++$c) |
|
| 163 | - { |
|
| 164 | - $i = ($i + 1) % 256; |
|
| 165 | - $j = ($j + $this->_s[$i]) % 256; |
|
| 166 | - |
|
| 167 | - // swap bytes |
|
| 168 | - self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
| 169 | - |
|
| 170 | - $pos = ($this->_s[$i] + $this->_s[$j]) % 256; |
|
| 171 | - $this->_key_stream .= chr($this->_s[$pos]); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Swap two individual bytes |
|
| 178 | - * |
|
| 179 | - * @param string $a A single byte |
|
| 180 | - * @param string $b A single byte |
|
| 181 | - * @return void |
|
| 182 | - */ |
|
| 183 | - private static function swapBytes(&$a, &$b) |
|
| 184 | - { |
|
| 185 | - $tmp = $a; |
|
| 186 | - $a = $b; |
|
| 187 | - $b = $tmp; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Indicates that this is a stream cipher |
|
| 193 | - * |
|
| 194 | - * @return integer Returns Cipher::STREAM |
|
| 195 | - */ |
|
| 196 | - public function type() |
|
| 197 | - { |
|
| 198 | - return parent::STREAM; |
|
| 199 | - } |
|
| 42 | + /** @type string $_s The S List */ |
|
| 43 | + private $_s = ""; |
|
| 44 | + |
|
| 45 | + /** @type string $_key_stream The Key Stream */ |
|
| 46 | + private $_key_stream = ""; |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Constructor |
|
| 51 | + * |
|
| 52 | + * @param string $key The key used for Encryption/Decryption |
|
| 53 | + * @return void |
|
| 54 | + */ |
|
| 55 | + public function __construct($key) |
|
| 56 | + { |
|
| 57 | + // set the ARC4 key |
|
| 58 | + parent::__construct(PHP_Crypt::CIPHER_ARC4, $key); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Destructor |
|
| 64 | + * |
|
| 65 | + * @return void |
|
| 66 | + */ |
|
| 67 | + public function __destruct() |
|
| 68 | + { |
|
| 69 | + parent::__destruct(); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Encrypt plain text data using ARC4 |
|
| 75 | + * |
|
| 76 | + * @param string $data A plain text string, 8 bytes long |
|
| 77 | + * @return boolean Returns true |
|
| 78 | + */ |
|
| 79 | + public function encrypt(&$text) |
|
| 80 | + { |
|
| 81 | + $this->operation(parent::ENCRYPT); |
|
| 82 | + return $this->arc4($text); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Decrypt a ARC4 encrypted string |
|
| 88 | + * |
|
| 89 | + * @param string $encrypted A ARC4 encrypted string, 8 bytes long |
|
| 90 | + * @return boolean Returns true |
|
| 91 | + */ |
|
| 92 | + public function decrypt(&$text) |
|
| 93 | + { |
|
| 94 | + $this->operation(parent::DECRYPT); |
|
| 95 | + return $this->arc4($text); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Performs the ARC4 algorithm, since encryption and decryption |
|
| 101 | + * are the same, all the work is done here |
|
| 102 | + * |
|
| 103 | + * @param string $text The string to encrypt/decrypt |
|
| 104 | + * @return boolean Returns true |
|
| 105 | + */ |
|
| 106 | + private function arc4(&$text) |
|
| 107 | + { |
|
| 108 | + $len = strlen($text); |
|
| 109 | + $this->prga($len); |
|
| 110 | + |
|
| 111 | + for($i = 0; $i < $len; ++$i) |
|
| 112 | + $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
|
| 113 | + |
|
| 114 | + return true; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * The key scheduling algorithm (KSA) |
|
| 120 | + * |
|
| 121 | + * @return void |
|
| 122 | + */ |
|
| 123 | + private function ksa() |
|
| 124 | + { |
|
| 125 | + $j = 0; |
|
| 126 | + $this->_s = array(); |
|
| 127 | + $keylen = $this->keySize(); |
|
| 128 | + $key = $this->key(); |
|
| 129 | + |
|
| 130 | + // fill $this->_s with all the values from 0-255 |
|
| 131 | + for($i = 0; $i < 256; ++$i) |
|
| 132 | + $this->_s[$i] = $i; |
|
| 133 | + |
|
| 134 | + // the changing S List |
|
| 135 | + for($i = 0; $i < 256; ++$i) |
|
| 136 | + { |
|
| 137 | + $k = $key[$i % $keylen]; |
|
| 138 | + $j = ($j + $this->_s[$i] + ord($k)) % 256; |
|
| 139 | + |
|
| 140 | + // swap bytes |
|
| 141 | + self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * The Pseudo-Random Generation Algorithm (PGRA) |
|
| 148 | + * Creates the key stream used for encryption / decryption |
|
| 149 | + * |
|
| 150 | + * @param integer $data_len The length of the data we are encrypting/decrypting |
|
| 151 | + * @return void |
|
| 152 | + */ |
|
| 153 | + private function prga($data_len) |
|
| 154 | + { |
|
| 155 | + $i = 0; |
|
| 156 | + $j = 0; |
|
| 157 | + $this->_key_stream = ""; |
|
| 158 | + |
|
| 159 | + // set up the key schedule |
|
| 160 | + $this->ksa(); |
|
| 161 | + |
|
| 162 | + for($c = 0; $c < $data_len; ++$c) |
|
| 163 | + { |
|
| 164 | + $i = ($i + 1) % 256; |
|
| 165 | + $j = ($j + $this->_s[$i]) % 256; |
|
| 166 | + |
|
| 167 | + // swap bytes |
|
| 168 | + self::swapBytes($this->_s[$i], $this->_s[$j]); |
|
| 169 | + |
|
| 170 | + $pos = ($this->_s[$i] + $this->_s[$j]) % 256; |
|
| 171 | + $this->_key_stream .= chr($this->_s[$pos]); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Swap two individual bytes |
|
| 178 | + * |
|
| 179 | + * @param string $a A single byte |
|
| 180 | + * @param string $b A single byte |
|
| 181 | + * @return void |
|
| 182 | + */ |
|
| 183 | + private static function swapBytes(&$a, &$b) |
|
| 184 | + { |
|
| 185 | + $tmp = $a; |
|
| 186 | + $a = $b; |
|
| 187 | + $b = $tmp; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Indicates that this is a stream cipher |
|
| 193 | + * |
|
| 194 | + * @return integer Returns Cipher::STREAM |
|
| 195 | + */ |
|
| 196 | + public function type() |
|
| 197 | + { |
|
| 198 | + return parent::STREAM; |
|
| 199 | + } |
|
| 200 | 200 | } |
| 201 | 201 | ?> |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | $len = strlen($text); |
| 109 | 109 | $this->prga($len); |
| 110 | 110 | |
| 111 | - for($i = 0; $i < $len; ++$i) |
|
| 111 | + for ($i = 0; $i < $len; ++$i) |
|
| 112 | 112 | $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
| 113 | 113 | |
| 114 | 114 | return true; |
@@ -128,11 +128,11 @@ discard block |
||
| 128 | 128 | $key = $this->key(); |
| 129 | 129 | |
| 130 | 130 | // fill $this->_s with all the values from 0-255 |
| 131 | - for($i = 0; $i < 256; ++$i) |
|
| 131 | + for ($i = 0; $i < 256; ++$i) |
|
| 132 | 132 | $this->_s[$i] = $i; |
| 133 | 133 | |
| 134 | 134 | // the changing S List |
| 135 | - for($i = 0; $i < 256; ++$i) |
|
| 135 | + for ($i = 0; $i < 256; ++$i) |
|
| 136 | 136 | { |
| 137 | 137 | $k = $key[$i % $keylen]; |
| 138 | 138 | $j = ($j + $this->_s[$i] + ord($k)) % 256; |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | // set up the key schedule |
| 160 | 160 | $this->ksa(); |
| 161 | 161 | |
| 162 | - for($c = 0; $c < $data_len; ++$c) |
|
| 162 | + for ($c = 0; $c < $data_len; ++$c) |
|
| 163 | 163 | { |
| 164 | 164 | $i = ($i + 1) % 256; |
| 165 | 165 | $j = ($j + $this->_s[$i]) % 256; |
@@ -108,8 +108,9 @@ discard block |
||
| 108 | 108 | $len = strlen($text); |
| 109 | 109 | $this->prga($len); |
| 110 | 110 | |
| 111 | - for($i = 0; $i < $len; ++$i) |
|
| 112 | - $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
|
| 111 | + for($i = 0; $i < $len; ++$i) { |
|
| 112 | + $text[$i] = $text[$i] ^ $this->_key_stream[$i]; |
|
| 113 | + } |
|
| 113 | 114 | |
| 114 | 115 | return true; |
| 115 | 116 | } |
@@ -128,8 +129,9 @@ discard block |
||
| 128 | 129 | $key = $this->key(); |
| 129 | 130 | |
| 130 | 131 | // fill $this->_s with all the values from 0-255 |
| 131 | - for($i = 0; $i < 256; ++$i) |
|
| 132 | - $this->_s[$i] = $i; |
|
| 132 | + for($i = 0; $i < 256; ++$i) { |
|
| 133 | + $this->_s[$i] = $i; |
|
| 134 | + } |
|
| 133 | 135 | |
| 134 | 136 | // the changing S List |
| 135 | 137 | for($i = 0; $i < 256; ++$i) |