Passed
Branch master (f7fac8)
by Sebastien
02:47
created

Encoding   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 39
c 3
b 1
f 0
dl 0
loc 101
rs 10
wmc 14

3 Methods

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