PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.2.11
MxChat – AI Chatbot & Content Generation for WordPress v3.2.11
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
mxchat-basic / includes / pdf-parser / src / Smalot / PdfParser / Encoding.php

Encoding.php in MxChat – AI Chatbot & Content Generation for WordPress 3.2.11, at includes/pdf-parser/src/Smalot/PdfParser/Encoding.php

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