Skip to content

Commit 365072d

Browse files
authored
Merge pull request #5 from LibreCodeCoop/develop
Develop
2 parents c7c3e86 + 3cc9ee1 commit 365072d

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "librecode/tcpdi_parser",
3+
"license": "LGPL-3.0-or-later",
4+
"autoload": {
5+
"psr-4": {
6+
"LibreCode\\TcpdiParser\\": "src/"
7+
}
8+
},
9+
"authors": [
10+
{
11+
"name": "Vitor Mattos",
12+
"email": "vitor@php.rio"
13+
},
14+
{
15+
"name": "Paul Nicholls",
16+
"homepage": "https://github.com/pauln"
17+
}
18+
]
19+
}

tcpdi_parser.php renamed to src/tcpdi_parser.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
namespace Librecode\TcpdiParser;
4+
25
//============================================================+
36
// File name : tcpdi_parser.php
47
// Version : 1.1
@@ -483,7 +486,7 @@ protected function decodeXrefStream($startxref, $xref=array()) {
483486
$v = $sarr[$key];
484487
if (($key == '/Type') AND ($v[0] == PDF_TYPE_TOKEN AND ($v[1] == 'XRef'))) {
485488
$valid_crs = true;
486-
} elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND count($v[1] >= 2))) {
489+
} elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND (count($v[1]) >= 2))) {
487490
// first object number in the subsection
488491
$index_first = intval($v[1][0][1]);
489492
// number of entries in the subsection
@@ -709,20 +712,19 @@ protected function getRawObject($offset=0, $data=null) {
709712
$objtype = ''; // object type to be returned
710713
$objval = ''; // object value to be returned
711714
// skip initial white space chars: \x00 null (NUL), \x09 horizontal tab (HT), \x0A line feed (LF), \x0C form feed (FF), \x0D carriage return (CR), \x20 space (SP)
712-
while (strspn($data{$offset}, "\x00\x09\x0a\x0c\x0d\x20") == 1) {
715+
while (strspn($data[$offset], "\x00\x09\x0a\x0c\x0d\x20")) {
713716
$offset++;
714717
}
715718
// get first char
716-
$char = $data{$offset};
719+
$char = $data[$offset];
717720
// get object type
718721
switch ($char) {
719722
case '%': { // \x25 PERCENT SIGN
720723
// skip comment and search for next token
721724
$next = strcspn($data, "\r\n", $offset);
722725
if ($next > 0) {
723726
$offset += $next;
724-
list($obj, $unused) = $this->getRawObject($offset, $data);
725-
return $obj;
727+
return $this->getRawObject($offset, $data);
726728
}
727729
break;
728730
}
@@ -744,10 +746,10 @@ protected function getRawObject($offset=0, $data=null) {
744746
if ($char == '(') {
745747
$open_bracket = 1;
746748
while ($open_bracket > 0) {
747-
if (!isset($data{$strpos})) {
749+
if (!isset($data[$strpos])) {
748750
break;
749751
}
750-
$ch = $data{$strpos};
752+
$ch = $data[$strpos];
751753
switch ($ch) {
752754
case '\\': { // REVERSE SOLIDUS (5Ch) (Backslash)
753755
// skip next character
@@ -792,7 +794,7 @@ protected function getRawObject($offset=0, $data=null) {
792794
}
793795
case '<': // \x3C LESS-THAN SIGN
794796
case '>': { // \x3E GREATER-THAN SIGN
795-
if (isset($data{($offset + 1)}) AND ($data{($offset + 1)} == $char)) {
797+
if (isset($data[($offset + 1)]) AND ($data[($offset + 1)] == $char)) {
796798
// dictionary object
797799
$objtype = PDF_TYPE_DICTIONARY;
798800
if ($char == '<') {
@@ -809,13 +811,16 @@ protected function getRawObject($offset=0, $data=null) {
809811
if (($char == '<') AND (preg_match('/^([0-9A-Fa-f ]+)[>]/iU', substr($data, $offset), $matches) == 1)) {
810812
$objval = $matches[1];
811813
$offset += strlen($matches[0]);
812-
unset($matches);
814+
} else if (($char == '<') AND ($endpos = strpos($this->pdfdata, '>', $offset)) !== FALSE) {
815+
$objval = substr($data, $offset,$endpos-$offset);
816+
$offset = $endpos + 1;
813817
}
818+
unset($matches);
814819
}
815820
break;
816821
}
817822
default: {
818-
$frag = $data{$offset} . @$data{$offset+1} . @$data{$offset+2} . @$data{$offset+3};
823+
$frag = $data[$offset] . @$data[$offset+1] . @$data[$offset+2] . @$data[$offset+3];
819824
switch ($frag) {
820825
case 'endo':
821826
// indirect object
@@ -888,20 +893,25 @@ private function getDictValue($offset, &$data) {
888893
$objval = array();
889894

890895
// Extract dict from data.
891-
$i=1;
896+
$i=2;
892897
$dict = '';
893898
$offset += 2;
894899
do {
895-
if ($data{$offset} == '>' && $data{$offset+1} == '>') {
896-
$i--;
900+
if ($data[$offset] == '>' && $data[$offset+1] == '>') {
901+
$i -= 2;
897902
$dict .= '>>';
898903
$offset += 2;
899-
} else if ($data{$offset} == '<' && $data{$offset+1} == '<') {
900-
$i++;
904+
} else if ($data[$offset] == '<' && $data[$offset+1] == '<') {
905+
$i += 2;
901906
$dict .= '<<';
902907
$offset += 2;
903908
} else {
904-
$dict .= $data{$offset};
909+
if ($data[$offset] == '<') {
910+
$i++;
911+
} else if ($data[$offset] == '>') {
912+
$i--;
913+
}
914+
$dict .= $data[$offset];
905915
$offset++;
906916
}
907917
} while ($i>0);
@@ -1441,7 +1451,7 @@ private function _getPageRotation($obj) { // $obj = /Page
14411451
*/
14421452
public function Error($msg) {
14431453
// exit program and print error
1444-
die("<strong>TCPDI_PARSER ERROR [{$this->uniqueid}]: </strong>".$msg);
1454+
throw new Exception("<strong>TCPDI_PARSER ERROR [{$this->uniqueid}]: </strong>".$msg);
14451455
}
14461456

14471457
} // END OF TCPDF_PARSER CLASS

0 commit comments

Comments
 (0)