| Total Complexity | 71 |
| Total Lines | 442 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| 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 |
||
| 36 | class Font extends PDFObject |
||
| 37 | { |
||
| 38 | const MISSING = '?'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $table = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $tableSizes = null; |
||
| 49 | |||
| 50 | public function init() |
||
| 51 | { |
||
| 52 | // Load translate table. |
||
| 53 | $this->loadTranslateTable(); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public function getName() |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getType() |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public function getDetails($deep = true) |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $char |
||
| 90 | * @param bool $use_default |
||
| 91 | * |
||
| 92 | * @return string|bool |
||
| 93 | */ |
||
| 94 | public function translateChar($char, $use_default = true) |
||
| 95 | { |
||
| 96 | $dec = hexdec(bin2hex($char)); |
||
| 97 | |||
| 98 | if (\array_key_exists($dec, $this->table)) { |
||
| 99 | return $this->table[$dec]; |
||
| 100 | } |
||
| 101 | |||
| 102 | return $use_default ? self::MISSING : $char; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param int $code |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public static function uchr($code) |
||
| 111 | { |
||
| 112 | return html_entity_decode('&#'.((int) $code).';', ENT_NOQUOTES, 'UTF-8'); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function loadTranslateTable() |
||
| 119 | { |
||
| 120 | if (null !== $this->table) { |
||
| 121 | return $this->table; |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->table = []; |
||
| 125 | $this->tableSizes = [ |
||
| 126 | 'from' => 1, |
||
| 127 | 'to' => 1, |
||
| 128 | ]; |
||
| 129 | |||
| 130 | if ($this->has('ToUnicode')) { |
||
| 131 | $content = $this->get('ToUnicode')->getContent(); |
||
| 132 | $matches = []; |
||
| 133 | |||
| 134 | // Support for multiple spacerange sections |
||
| 135 | if (preg_match_all('/begincodespacerange(?P<sections>.*?)endcodespacerange/s', $content, $matches)) { |
||
| 136 | foreach ($matches['sections'] as $section) { |
||
| 137 | $regexp = '/<(?P<from>[0-9A-F]+)> *<(?P<to>[0-9A-F]+)>[ \r\n]+/is'; |
||
| 138 | |||
| 139 | preg_match_all($regexp, $section, $matches); |
||
| 140 | |||
| 141 | $this->tableSizes = [ |
||
| 142 | 'from' => max(1, \strlen(current($matches['from'])) / 2), |
||
| 143 | 'to' => max(1, \strlen(current($matches['to'])) / 2), |
||
| 144 | ]; |
||
| 145 | |||
| 146 | break; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | // Support for multiple bfchar sections |
||
| 151 | if (preg_match_all('/beginbfchar(?P<sections>.*?)endbfchar/s', $content, $matches)) { |
||
| 152 | foreach ($matches['sections'] as $section) { |
||
| 153 | $regexp = '/<(?P<from>[0-9A-F]+)> +<(?P<to>[0-9A-F]+)>[ \r\n]+/is'; |
||
| 154 | |||
| 155 | preg_match_all($regexp, $section, $matches); |
||
| 156 | |||
| 157 | $this->tableSizes['from'] = max(1, \strlen(current($matches['from'])) / 2); |
||
| 158 | |||
| 159 | foreach ($matches['from'] as $key => $from) { |
||
| 160 | $parts = preg_split( |
||
| 161 | '/([0-9A-F]{4})/i', |
||
| 162 | $matches['to'][$key], |
||
| 163 | 0, |
||
| 164 | PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE |
||
| 165 | ); |
||
| 166 | $text = ''; |
||
| 167 | foreach ($parts as $part) { |
||
| 168 | $text .= self::uchr(hexdec($part)); |
||
|
|
|||
| 169 | } |
||
| 170 | $this->table[hexdec($from)] = $text; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | // Support for multiple bfrange sections |
||
| 176 | if (preg_match_all('/beginbfrange(?P<sections>.*?)endbfrange/s', $content, $matches)) { |
||
| 177 | foreach ($matches['sections'] as $section) { |
||
| 178 | // Support for : <srcCode1> <srcCode2> <dstString> |
||
| 179 | $regexp = '/<(?P<from>[0-9A-F]+)> *<(?P<to>[0-9A-F]+)> *<(?P<offset>[0-9A-F]+)>[ \r\n]+/is'; |
||
| 180 | |||
| 181 | preg_match_all($regexp, $section, $matches); |
||
| 182 | |||
| 183 | foreach ($matches['from'] as $key => $from) { |
||
| 184 | $char_from = hexdec($from); |
||
| 185 | $char_to = hexdec($matches['to'][$key]); |
||
| 186 | $offset = hexdec($matches['offset'][$key]); |
||
| 187 | |||
| 188 | for ($char = $char_from; $char <= $char_to; ++$char) { |
||
| 189 | $this->table[$char] = self::uchr($char - $char_from + $offset); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | // Support for : <srcCode1> <srcCodeN> [<dstString1> <dstString2> ... <dstStringN>] |
||
| 194 | // Some PDF file has 2-byte Unicode values on new lines > added \r\n |
||
| 195 | $regexp = '/<(?P<from>[0-9A-F]+)> *<(?P<to>[0-9A-F]+)> *\[(?P<strings>[\r\n<>0-9A-F ]+)\][ \r\n]+/is'; |
||
| 196 | |||
| 197 | preg_match_all($regexp, $section, $matches); |
||
| 198 | |||
| 199 | foreach ($matches['from'] as $key => $from) { |
||
| 200 | $char_from = hexdec($from); |
||
| 201 | $strings = []; |
||
| 202 | |||
| 203 | preg_match_all('/<(?P<string>[0-9A-F]+)> */is', $matches['strings'][$key], $strings); |
||
| 204 | |||
| 205 | foreach ($strings['string'] as $position => $string) { |
||
| 206 | $parts = preg_split( |
||
| 207 | '/([0-9A-F]{4})/i', |
||
| 208 | $string, |
||
| 209 | 0, |
||
| 210 | PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE |
||
| 211 | ); |
||
| 212 | $text = ''; |
||
| 213 | foreach ($parts as $part) { |
||
| 214 | $text .= self::uchr(hexdec($part)); |
||
| 215 | } |
||
| 216 | $this->table[$char_from + $position] = $text; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | return $this->table; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param array $table |
||
| 228 | */ |
||
| 229 | public function setTable($table) |
||
| 230 | { |
||
| 231 | $this->table = $table; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $hexa |
||
| 236 | * @param bool $add_braces |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public static function decodeHexadecimal($hexa, $add_braces = false) |
||
| 241 | { |
||
| 242 | // Special shortcut for XML content. |
||
| 243 | if (false !== stripos($hexa, '<?xml')) { |
||
| 244 | return $hexa; |
||
| 245 | } |
||
| 246 | |||
| 247 | $text = ''; |
||
| 248 | $parts = preg_split('/(<[a-f0-9]+>)/si', $hexa, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
||
| 249 | |||
| 250 | foreach ($parts as $part) { |
||
| 251 | if (preg_match('/^<.*>$/', $part) && false === stripos($part, '<?xml')) { |
||
| 252 | $part = trim($part, '<>'); |
||
| 253 | if ($add_braces) { |
||
| 254 | $text .= '('; |
||
| 255 | } |
||
| 256 | |||
| 257 | $part = pack('H*', $part); |
||
| 258 | $text .= ($add_braces ? preg_replace('/\\\/s', '\\\\\\', $part) : $part); |
||
| 259 | |||
| 260 | if ($add_braces) { |
||
| 261 | $text .= ')'; |
||
| 262 | } |
||
| 263 | } else { |
||
| 264 | $text .= $part; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | return $text; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $text |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public static function decodeOctal($text) |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $text |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public static function decodeEntities($text) |
||
| 298 | { |
||
| 299 | $parts = preg_split('/(#\d{2})/s', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
||
| 300 | $text = ''; |
||
| 301 | |||
| 302 | foreach ($parts as $part) { |
||
| 303 | if (preg_match('/^#\d{2}$/', $part)) { |
||
| 304 | $text .= \chr(hexdec(trim($part, '#'))); |
||
| 305 | } else { |
||
| 306 | $text .= $part; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | return $text; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $text |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public static function decodeUnicode($text) |
||
| 319 | { |
||
| 320 | if (preg_match('/^\xFE\xFF/i', $text)) { |
||
| 321 | // Strip U+FEFF byte order marker. |
||
| 322 | $decode = substr($text, 2); |
||
| 323 | $text = ''; |
||
| 324 | $length = \strlen($decode); |
||
| 325 | |||
| 326 | for ($i = 0; $i < $length; $i += 2) { |
||
| 327 | $text .= self::uchr(hexdec(bin2hex(substr($decode, $i, 2)))); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | return $text; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return int |
||
| 336 | */ |
||
| 337 | protected function getFontSpaceLimit() |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param array $commands |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function decodeText($commands) |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $text |
||
| 392 | * @param bool $unicode |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public function decodeContent($text) |
||
| 480 |