| Total Complexity | 74 |
| Total Lines | 479 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 2 | Features | 1 |
Complex classes like Font often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Font, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Font extends PDFObject |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * |
||
| 42 | */ |
||
| 43 | const MISSING = '?'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $table = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $tableSizes = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * |
||
| 57 | */ |
||
| 58 | public function init() |
||
| 59 | { |
||
| 60 | // Load translate table. |
||
| 61 | $this->loadTranslateTable(); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getName() |
||
| 68 | { |
||
| 69 | return $this->has('BaseFont') ? (string)$this->get('BaseFont') : '[Unknown]'; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function getType() |
||
| 76 | { |
||
| 77 | return (string)$this->header->get('Subtype'); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function getDetails($deep = true) |
||
| 84 | { |
||
| 85 | $details = array(); |
||
| 86 | |||
| 87 | $details['Name'] = $this->getName(); |
||
| 88 | $details['Type'] = $this->getType(); |
||
| 89 | $details['Encoding'] = ($this->has('Encoding') ? (string)$this->get('Encoding') : 'Ansi'); |
||
| 90 | |||
| 91 | $details += parent::getDetails($deep); |
||
| 92 | |||
| 93 | return $details; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $char |
||
| 98 | * @param bool $use_default |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function translateChar($char, $use_default = true) |
||
| 103 | { |
||
| 104 | $dec = hexdec(bin2hex($char)); |
||
| 105 | |||
| 106 | if (array_key_exists($dec, $this->table)) { |
||
| 107 | $char = $this->table[$dec]; |
||
| 108 | } else { |
||
| 109 | $char = ($use_default ? self::MISSING : $char); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $char; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param int $code |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public static function uchr($code) |
||
| 121 | { |
||
| 122 | return html_entity_decode('&#' . ((int)$code) . ';', ENT_NOQUOTES, 'UTF-8'); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public function loadTranslateTable() |
||
| 129 | { |
||
| 130 | if (!is_null($this->table)) { |
||
|
|
|||
| 131 | return $this->table; |
||
| 132 | } |
||
| 133 | |||
| 134 | $this->table = array(); |
||
| 135 | $this->tableSizes = array( |
||
| 136 | 'from' => 1, |
||
| 137 | 'to' => 1, |
||
| 138 | ); |
||
| 139 | |||
| 140 | if ($this->has('ToUnicode')) { |
||
| 141 | $content = $this->get('ToUnicode')->getContent(); |
||
| 142 | $matches = array(); |
||
| 143 | |||
| 144 | // Support for multiple spacerange sections |
||
| 145 | if (preg_match_all('/begincodespacerange(?P<sections>.*?)endcodespacerange/s', $content, $matches)) { |
||
| 146 | foreach ($matches['sections'] as $section) { |
||
| 147 | $regexp = '/<(?P<from>[0-9A-F]+)> *<(?P<to>[0-9A-F]+)>[ \r\n]+/is'; |
||
| 148 | |||
| 149 | preg_match_all($regexp, $section, $matches); |
||
| 150 | |||
| 151 | $this->tableSizes = array( |
||
| 152 | 'from' => max(1, strlen(current($matches['from'])) / 2), |
||
| 153 | 'to' => max(1, strlen(current($matches['to'])) / 2), |
||
| 154 | ); |
||
| 155 | |||
| 156 | break; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // Support for multiple bfchar sections |
||
| 161 | if (preg_match_all('/beginbfchar(?P<sections>.*?)endbfchar/s', $content, $matches)) { |
||
| 162 | foreach ($matches['sections'] as $section) { |
||
| 163 | $regexp = '/<(?P<from>[0-9A-F]+)> +<(?P<to>[0-9A-F]+)>[ \r\n]+/is'; |
||
| 164 | |||
| 165 | preg_match_all($regexp, $section, $matches); |
||
| 166 | |||
| 167 | $this->tableSizes['from'] = max(1, strlen(current($matches['from'])) / 2); |
||
| 168 | |||
| 169 | foreach ($matches['from'] as $key => $from) { |
||
| 170 | $parts = preg_split( |
||
| 171 | '/([0-9A-F]{4})/i', |
||
| 172 | $matches['to'][$key], |
||
| 173 | 0, |
||
| 174 | PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE |
||
| 175 | ); |
||
| 176 | $text = ''; |
||
| 177 | foreach ($parts as $part) { |
||
| 178 | $text .= self::uchr(hexdec($part)); |
||
| 179 | } |
||
| 180 | $this->table[hexdec($from)] = $text; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | // Support for multiple bfrange sections |
||
| 186 | if (preg_match_all('/beginbfrange(?P<sections>.*?)endbfrange/s', $content, $matches)) { |
||
| 187 | foreach ($matches['sections'] as $section) { |
||
| 188 | // Support for : <srcCode1> <srcCode2> <dstString> |
||
| 189 | $regexp = '/<(?P<from>[0-9A-F]+)> *<(?P<to>[0-9A-F]+)> *<(?P<offset>[0-9A-F]+)>[ \r\n]+/is'; |
||
| 190 | |||
| 191 | preg_match_all($regexp, $section, $matches); |
||
| 192 | |||
| 193 | foreach ($matches['from'] as $key => $from) { |
||
| 194 | $char_from = hexdec($from); |
||
| 195 | $char_to = hexdec($matches['to'][$key]); |
||
| 196 | $offset = hexdec($matches['offset'][$key]); |
||
| 197 | |||
| 198 | for ($char = $char_from; $char <= $char_to; $char++) { |
||
| 199 | $this->table[$char] = self::uchr($char - $char_from + $offset); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | // Support for : <srcCode1> <srcCodeN> [<dstString1> <dstString2> ... <dstStringN>] |
||
| 204 | // Some PDF file has 2-byte Unicode values on new lines > added \r\n |
||
| 205 | $regexp = '/<(?P<from>[0-9A-F]+)> *<(?P<to>[0-9A-F]+)> *\[(?P<strings>[\r\n<>0-9A-F ]+)\][ \r\n]+/is'; |
||
| 206 | |||
| 207 | preg_match_all($regexp, $section, $matches); |
||
| 208 | |||
| 209 | foreach ($matches['from'] as $key => $from) { |
||
| 210 | $char_from = hexdec($from); |
||
| 211 | $strings = array(); |
||
| 212 | |||
| 213 | preg_match_all('/<(?P<string>[0-9A-F]+)> */is', $matches['strings'][$key], $strings); |
||
| 214 | |||
| 215 | foreach ($strings['string'] as $position => $string) { |
||
| 216 | $parts = preg_split( |
||
| 217 | '/([0-9A-F]{4})/i', |
||
| 218 | $string, |
||
| 219 | 0, |
||
| 220 | PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE |
||
| 221 | ); |
||
| 222 | $text = ''; |
||
| 223 | foreach ($parts as $part) { |
||
| 224 | $text .= self::uchr(hexdec($part)); |
||
| 225 | } |
||
| 226 | $this->table[$char_from + $position] = $text; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | return $this->table; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param array $table |
||
| 238 | */ |
||
| 239 | public function setTable($table) |
||
| 240 | { |
||
| 241 | $this->table = $table; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $hexa |
||
| 246 | * @param bool $add_braces |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public static function decodeHexadecimal($hexa, $add_braces = false) |
||
| 251 | { |
||
| 252 | // Special shortcut for XML content. |
||
| 253 | if (stripos($hexa, '<?xml') !== false) { |
||
| 254 | return $hexa; |
||
| 255 | } |
||
| 256 | |||
| 257 | $text = ''; |
||
| 258 | $parts = preg_split('/(<[a-f0-9]+>)/si', $hexa, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
||
| 259 | |||
| 260 | foreach ($parts as $part) { |
||
| 261 | if (preg_match('/^<.*>$/', $part) && stripos($part, '<?xml') === false) { |
||
| 262 | $part = trim($part, '<>'); |
||
| 263 | if ($add_braces) { |
||
| 264 | $text .= '('; |
||
| 265 | } |
||
| 266 | |||
| 267 | $part = pack('H*', $part); |
||
| 268 | $text .= ($add_braces ? preg_replace('/\\\/s', '\\\\\\', $part) : $part); |
||
| 269 | |||
| 270 | if ($add_braces) { |
||
| 271 | $text .= ')'; |
||
| 272 | } |
||
| 273 | } else { |
||
| 274 | $text .= $part; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | return $text; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $text |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public static function decodeOctal($text) |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param $text |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public static function decodeEntities($text) |
||
| 308 | { |
||
| 309 | $parts = preg_split('/(#\d{2})/s', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
||
| 310 | $text = ''; |
||
| 311 | |||
| 312 | foreach ($parts as $part) { |
||
| 313 | if (preg_match('/^#\d{2}$/', $part)) { |
||
| 314 | $text .= chr(hexdec(trim($part, '#'))); |
||
| 315 | } else { |
||
| 316 | $text .= $part; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | return $text; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $text |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | public static function decodeUnicode($text) |
||
| 329 | { |
||
| 330 | if (preg_match('/^\xFE\xFF/i', $text)) { |
||
| 331 | // Strip U+FEFF byte order marker. |
||
| 332 | $decode = substr($text, 2); |
||
| 333 | $text = ''; |
||
| 334 | $length = strlen($decode); |
||
| 335 | |||
| 336 | for ($i = 0; $i < $length; $i += 2) { |
||
| 337 | $text .= self::uchr(hexdec(bin2hex(substr($decode, $i, 2)))); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | return $text; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | protected function getFontSpaceLimit() |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param array $commands |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function decodeText($commands) |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $text |
||
| 408 | * @param bool $unicode |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | protected function decodeContent($text, &$unicode) |
||
| 517 | } |
||
| 518 | } |
||
| 519 |