Completed
Pull Request — master (#349)
by
unknown
05:52
created

Encoding::translateChar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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