Passed
Pull Request — master (#378)
by
unknown
02:37
created

Encoding::init()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 30.4863

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 7
eloc 22
c 3
b 1
f 0
nc 10
nop 0
dl 0
loc 40
ccs 5
cts 23
cp 0.2174
crap 30.4863
rs 8.6346
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
            $className = $this->getEncodingClass();
64
            $class = new $className();
65
            $this->encoding = $class->getTranslations();
66
67
            // Build table including differences.
68
            $differences = $this->get('Differences')->getContent();
69
            $code = 0;
70
71
            if (!\is_array($differences)) {
72
                return;
73
            }
74
75
            foreach ($differences as $difference) {
76
                /** @var ElementNumeric $difference */
77
                if ($difference instanceof ElementNumeric) {
78
                    $code = $difference->getContent();
79
                    continue;
80
                }
81
82
                // ElementName
83
                $this->differences[$code] = $difference;
84
                if (\is_object($difference)) {
85
                    $this->differences[$code] = $difference->getContent();
86
                }
87
88
                // For the next char.
89
                ++$code;
90
            }
91
92
            $this->mapping = $this->encoding;
93
            foreach ($this->differences as $code => $difference) {
94
                /* @var string $difference */
95
                $this->mapping[$code] = $difference;
96
            }
97
        }
98 4
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getDetails($deep = true)
104
    {
105
        $details = [];
106
107
        $details['BaseEncoding'] = ($this->has('BaseEncoding') ? (string) $this->get('BaseEncoding') : 'Ansi');
108
        $details['Differences'] = ($this->has('Differences') ? (string) $this->get('Differences') : '');
109
110
        $details += parent::getDetails($deep);
111
112
        return $details;
113
    }
114
115
    /**
116
     * @return int
117
     */
118 2
    public function translateChar($dec)
119
    {
120 2
        if (isset($this->mapping[$dec])) {
121
            $dec = $this->mapping[$dec];
122
        }
123
124 2
        return PostScriptGlyphs::getCodePoint($dec);
125
    }
126
127
    /**
128
     * @return string
129
     * @throws \Exception
130
     */
131
    public function __toString()
132
    {
133
        return $this->getEncodingClass();
134
    }
135
136
    /**
137
     * @return string
138
     * @throws \Exception
139
     */
140
    protected function getEncodingClass()
141
    {
142
        // Load reference table charset.
143
        $baseEncoding = preg_replace('/[^A-Z0-9]/is', '', $this->get('BaseEncoding')->getContent());
144
        $className = '\\Smalot\\PdfParser\\Encoding\\'.$baseEncoding;
145
146
        if (!class_exists($className)) {
147
            throw new \Exception('Missing encoding data for: "'.$baseEncoding.'".');
148
        }
149
150
        return $className;
151
    }
152
}
153