Passed
Pull Request — master (#378)
by
unknown
08:15
created

Encoding::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 Smalot\PdfParser\Element\ElementNumeric;
34
use Smalot\PdfParser\Encoding\PostScriptGlyphs;
35
36
/**
37
 * Class Encoding
38
 */
39
class Encoding extends PDFObject
40
{
41
    /**
42
     * @var array
43
     */
44
    protected $encoding;
45
46
    /**
47
     * @var array
48
     */
49
    protected $differences;
50
51
    /**
52
     * @var array
53
     */
54
    protected $mapping;
55
56 4
    public function init()
57
    {
58 4
        $this->mapping = [];
59 4
        $this->differences = [];
60 4
        $this->encoding = [];
61
62 4
        if ($this->has('BaseEncoding')) {
63
            $class = $this->getEncodingClass();
64
            $this->encoding = $class->getTranslations();
65
66
            // Build table including differences.
67
            $differences = $this->get('Differences')->getContent();
68
            $code = 0;
69
70
            if (!\is_array($differences)) {
71
                return;
72
            }
73
74
            foreach ($differences as $difference) {
75
                /** @var ElementNumeric $difference */
76
                if ($difference instanceof ElementNumeric) {
77
                    $code = $difference->getContent();
78
                    continue;
79
                }
80
81
                // ElementName
82
                $this->differences[$code] = $difference;
83
                if (\is_object($difference)) {
84
                    $this->differences[$code] = $difference->getContent();
85
                }
86
87
                // For the next char.
88
                ++$code;
89
            }
90
91
            $this->mapping = $this->encoding;
92
            foreach ($this->differences as $code => $difference) {
93
                /* @var string $difference */
94
                $this->mapping[$code] = $difference;
95
            }
96
        }
97 4
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getDetails($deep = true)
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
    /**
115
     * @return int
116
     */
117 2
    public function translateChar($dec)
118
    {
119 2
        if (isset($this->mapping[$dec])) {
120
            $dec = $this->mapping[$dec];
121
        }
122
123 2
        return PostScriptGlyphs::getCodePoint($dec);
124
    }
125
126
    public function __toString()
127
    {
128
        return $this->getEncodingClass();
129
    }
130
131
132
    protected function getEncodingClass()
133
    {
134
        // Load reference table charset.
135
        $baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent());
136
        $className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding;
137
138
        if (!class_exists($className)) {
139
            throw new \Exception('Missing encoding data for: "'.$baseEncoding.'".');
140
        }
141
142
        return new $className();
143
    }
144
}
145