1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* This file is part of the PdfParser library. |
6
|
|
|
* |
7
|
|
|
* @author Sébastien MALOT <[email protected]> |
8
|
|
|
* @date 2017-01-03 |
9
|
|
|
* |
10
|
|
|
* @license LGPLv3 |
11
|
|
|
* @url <https://github.com/smalot/pdfparser> |
12
|
|
|
* |
13
|
|
|
* PdfParser is a pdf library written in PHP, extraction oriented. |
14
|
|
|
* Copyright (C) 2017 - Sébastien MALOT <[email protected]> |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
18
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
19
|
|
|
* (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Lesser General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
27
|
|
|
* along with this program. |
28
|
|
|
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
namespace Smalot\PdfParser; |
32
|
|
|
|
33
|
|
|
use Exception; |
34
|
|
|
use Smalot\PdfParser\Element\ElementNumeric; |
35
|
|
|
use Smalot\PdfParser\Encoding\EncodingLocator; |
36
|
|
|
use Smalot\PdfParser\Encoding\PostScriptGlyphs; |
37
|
|
|
use Smalot\PdfParser\Exception\EncodingNotFoundException; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class Encoding |
41
|
|
|
*/ |
42
|
|
|
class Encoding extends PDFObject |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $encoding; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $differences; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $mapping; |
58
|
|
|
|
59
|
8 |
|
public function init() |
60
|
|
|
{ |
61
|
8 |
|
$this->mapping = []; |
62
|
8 |
|
$this->differences = []; |
63
|
8 |
|
$this->encoding = []; |
64
|
|
|
|
65
|
8 |
|
if ($this->has('BaseEncoding')) { |
66
|
4 |
|
$this->encoding = EncodingLocator::getEncoding($this->getEncodingClass())->getTranslations(); |
67
|
|
|
|
68
|
|
|
// Build table including differences. |
69
|
3 |
|
$differences = $this->get('Differences')->getContent(); |
70
|
3 |
|
$code = 0; |
71
|
|
|
|
72
|
3 |
|
if (!\is_array($differences)) { |
73
|
2 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
foreach ($differences as $difference) { |
77
|
|
|
/** @var ElementNumeric $difference */ |
78
|
1 |
|
if ($difference instanceof ElementNumeric) { |
79
|
1 |
|
$code = $difference->getContent(); |
80
|
1 |
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// ElementName |
84
|
1 |
|
$this->differences[$code] = $difference; |
85
|
1 |
|
if (\is_object($difference)) { |
86
|
1 |
|
$this->differences[$code] = $difference->getContent(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// For the next char. |
90
|
1 |
|
++$code; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$this->mapping = $this->encoding; |
94
|
1 |
|
foreach ($this->differences as $code => $difference) { |
95
|
|
|
/* @var string $difference */ |
96
|
1 |
|
$this->mapping[$code] = $difference; |
97
|
|
|
} |
98
|
|
|
} |
99
|
5 |
|
} |
100
|
|
|
|
101
|
|
|
public function getDetails(bool $deep = true): array |
102
|
|
|
{ |
103
|
|
|
$details = []; |
104
|
|
|
|
105
|
|
|
$details['BaseEncoding'] = ($this->has('BaseEncoding') ? (string) $this->get('BaseEncoding') : 'Ansi'); |
106
|
|
|
$details['Differences'] = ($this->has('Differences') ? (string) $this->get('Differences') : ''); |
107
|
|
|
|
108
|
|
|
$details += parent::getDetails($deep); |
109
|
|
|
|
110
|
|
|
return $details; |
111
|
|
|
} |
112
|
|
|
|
113
|
4 |
|
public function translateChar($dec): ?int |
114
|
|
|
{ |
115
|
4 |
|
if (isset($this->mapping[$dec])) { |
116
|
1 |
|
$dec = $this->mapping[$dec]; |
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
return PostScriptGlyphs::getCodePoint($dec); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns encoding class name if available or empty string (only prior PHP 7.4). |
124
|
|
|
* |
125
|
|
|
* @throws \Exception On PHP 7.4+ an exception is thrown if encoding class doesn't exist. |
126
|
|
|
*/ |
127
|
4 |
|
public function __toString(): string |
128
|
|
|
{ |
129
|
|
|
try { |
130
|
4 |
|
return $this->getEncodingClass(); |
131
|
2 |
|
} catch (Exception $e) { |
132
|
|
|
// prior to PHP 7.4 toString has to return an empty string. |
133
|
2 |
|
if (version_compare(\PHP_VERSION, '7.4.0', '<')) { |
134
|
|
|
return ''; |
135
|
|
|
} |
136
|
2 |
|
throw $e; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @throws EncodingNotFoundException |
142
|
|
|
*/ |
143
|
6 |
|
protected function getEncodingClass(): string |
144
|
|
|
{ |
145
|
|
|
// Load reference table charset. |
146
|
6 |
|
$baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent()); |
147
|
6 |
|
$className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding; |
148
|
|
|
|
149
|
6 |
|
if (!class_exists($className)) { |
150
|
3 |
|
throw new EncodingNotFoundException('Missing encoding data for: "'.$baseEncoding.'".'); |
151
|
|
|
} |
152
|
|
|
|
153
|
3 |
|
return $className; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|