beta-0.89.2
[luatex.git] / source / libs / poppler / poppler-src / poppler / Linearization.cc
blobbe041f96407a452293c1ffbdaff52cb6b714f6c0
1 //========================================================================
2 //
3 // Linearization.cc
4 //
5 // This file is licensed under the GPLv2 or later
6 //
7 // Copyright 2010, 2012 Hib Eris <hib@hiberis.nl>
8 // Copyright 2015 Jason Crain <jason@aquaticape.us>
9 //
10 //========================================================================
12 #include "Linearization.h"
13 #include "Parser.h"
14 #include "Lexer.h"
16 //------------------------------------------------------------------------
17 // Linearization
18 //------------------------------------------------------------------------
20 Linearization::Linearization (BaseStream *str)
22 Parser *parser;
23 Object obj1, obj2, obj3, obj5;
25 linDict.initNull();
27 str->reset();
28 obj1.initNull();
29 parser = new Parser(NULL,
30 new Lexer(NULL, str->makeSubStream(str->getStart(), gFalse, 0, &obj1)),
31 gFalse);
32 parser->getObj(&obj1);
33 parser->getObj(&obj2);
34 parser->getObj(&obj3);
35 parser->getObj(&linDict);
36 if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") && linDict.isDict()) {
37 linDict.dictLookup("Linearized", &obj5);
38 if (!(obj5.isNum() && obj5.getNum() > 0)) {
39 linDict.free();
40 linDict.initNull();
42 obj5.free();
44 obj3.free();
45 obj2.free();
46 obj1.free();
47 delete parser;
50 Linearization:: ~Linearization()
52 linDict.free();
55 Guint Linearization::getLength()
57 if (!linDict.isDict()) return 0;
59 int length;
60 if (linDict.getDict()->lookupInt("L", NULL, &length) &&
61 length > 0) {
62 return length;
63 } else {
64 error(errSyntaxWarning, -1, "Length in linearization table is invalid");
65 return 0;
69 Guint Linearization::getHintsOffset()
71 int hintsOffset;
73 Object obj1, obj2;
74 if (linDict.isDict() &&
75 linDict.dictLookup("H", &obj1)->isArray() &&
76 obj1.arrayGetLength()>=2 &&
77 obj1.arrayGet(0, &obj2)->isInt() &&
78 obj2.getInt() > 0) {
79 hintsOffset = obj2.getInt();
80 } else {
81 error(errSyntaxWarning, -1, "Hints table offset in linearization table is invalid");
82 hintsOffset = 0;
84 obj2.free();
85 obj1.free();
87 return hintsOffset;
90 Guint Linearization::getHintsLength()
92 int hintsLength;
94 Object obj1, obj2;
95 if (linDict.isDict() &&
96 linDict.dictLookup("H", &obj1)->isArray() &&
97 obj1.arrayGetLength()>=2 &&
98 obj1.arrayGet(1, &obj2)->isInt() &&
99 obj2.getInt() > 0) {
100 hintsLength = obj2.getInt();
101 } else {
102 error(errSyntaxWarning, -1, "Hints table length in linearization table is invalid");
103 hintsLength = 0;
105 obj2.free();
106 obj1.free();
108 return hintsLength;
111 Guint Linearization::getHintsOffset2()
113 int hintsOffset2 = 0; // default to 0
115 Object obj1, obj2;
116 if (linDict.isDict() &&
117 linDict.dictLookup("H", &obj1)->isArray() &&
118 obj1.arrayGetLength()>=4) {
119 if (obj1.arrayGet(2, &obj2)->isInt() &&
120 obj2.getInt() > 0) {
121 hintsOffset2 = obj2.getInt();
122 } else {
123 error(errSyntaxWarning, -1, "Second hints table offset in linearization table is invalid");
124 hintsOffset2 = 0;
127 obj2.free();
128 obj1.free();
130 return hintsOffset2;
133 Guint Linearization::getHintsLength2()
135 int hintsLength2 = 0; // default to 0
137 Object obj1, obj2;
138 if (linDict.isDict() &&
139 linDict.dictLookup("H", &obj1)->isArray() &&
140 obj1.arrayGetLength()>=4) {
141 if (obj1.arrayGet(3, &obj2)->isInt() &&
142 obj2.getInt() > 0) {
143 hintsLength2 = obj2.getInt();
144 } else {
145 error(errSyntaxWarning, -1, "Second hints table length in linearization table is invalid");
146 hintsLength2 = 0;
149 obj2.free();
150 obj1.free();
152 return hintsLength2;
155 int Linearization::getObjectNumberFirst()
157 int objectNumberFirst = 0;
158 if (linDict.isDict() &&
159 linDict.getDict()->lookupInt("O", NULL, &objectNumberFirst) &&
160 objectNumberFirst > 0) {
161 return objectNumberFirst;
162 } else {
163 error(errSyntaxWarning, -1, "Object number of first page in linearization table is invalid");
164 return 0;
168 Guint Linearization::getEndFirst()
170 int pageEndFirst = 0;
171 if (linDict.isDict() &&
172 linDict.getDict()->lookupInt("E", NULL, &pageEndFirst) &&
173 pageEndFirst > 0) {
174 return pageEndFirst;
175 } else {
176 error(errSyntaxWarning, -1, "First page end offset in linearization table is invalid");
177 return 0;
181 int Linearization::getNumPages()
183 int numPages = 0;
184 if (linDict.isDict() &&
185 linDict.getDict()->lookupInt("N", NULL, &numPages) &&
186 numPages > 0) {
187 return numPages;
188 } else {
189 error(errSyntaxWarning, -1, "Page count in linearization table is invalid");
190 return 0;
194 Guint Linearization::getMainXRefEntriesOffset()
196 int mainXRefEntriesOffset = 0;
197 if (linDict.isDict() &&
198 linDict.getDict()->lookupInt("T", NULL, &mainXRefEntriesOffset) &&
199 mainXRefEntriesOffset > 0) {
200 return mainXRefEntriesOffset;
201 } else {
202 error(errSyntaxWarning, -1, "Main Xref offset in linearization table is invalid");
203 return 0;
207 int Linearization::getPageFirst()
209 int pageFirst = 0; // Optional, defaults to 0.
211 if (linDict.isDict()) {
212 linDict.getDict()->lookupInt("P", NULL, &pageFirst);
215 if ((pageFirst < 0) || (pageFirst >= getNumPages())) {
216 error(errSyntaxWarning, -1, "First page in linearization table is invalid");
217 return 0;
220 return pageFirst;