| 1 |
|
| 2 |
``pplib`` |
| 3 |
========= |
| 4 |
|
| 5 |
``pplib`` is a library for raw PDF access. It parses PDF documents and provides an interface to access document structures in C. |
| 6 |
|
| 7 |
C-API |
| 8 |
===== |
| 9 |
|
| 10 |
Types |
| 11 |
----- |
| 12 |
|
| 13 |
``pplib`` defines several C-types to represent PDF types: |
| 14 |
|
| 15 |
- ``ppint`` - signed integer (``int64_t``) |
| 16 |
- ``ppnum`` - real number (double) |
| 17 |
- ``ppname`` - PDF name |
| 18 |
- ``ppstring`` - PDF string |
| 19 |
- ``pparray`` - PDF array |
| 20 |
- ``ppdict`` - PDF dict |
| 21 |
- ``ppstream`` - PDF stream |
| 22 |
- ``ppref`` - PDF indirect reference |
| 23 |
- ``ppobj`` - a container of all above |
| 24 |
|
| 25 |
Among ``ppint`` and ``ppnum``, we also use ``ppuint`` - unsigned integer (machine word, alias to ``size_t``). |
| 26 |
|
| 27 |
Other API types: |
| 28 |
|
| 29 |
- ``ppdoc`` - PDF document |
| 30 |
- ``ppxref`` - cross-references table |
| 31 |
- ``ppcontext`` - ... later |
| 32 |
- ``pprect`` - rectangle |
| 33 |
- ``ppmatrix`` - matrix |
| 34 |
|
| 35 |
Integer, number, name and string are treated as simple types. |
| 36 |
Names and strings are actually C-structures, but exposed to API as typedefs to ``const char *``. |
| 37 |
Other types (array, dict, stream, reference, object container, xref, PDF) are C-structures, |
| 38 |
and you operate it their pointers. So when you declare a simple type variable you say:: |
| 39 |
|
| 40 |
ppuint u; |
| 41 |
ppnum n; |
| 42 |
ppname name; |
| 43 |
ppstring string; |
| 44 |
... |
| 45 |
|
| 46 |
And when you declare a compound type you operate on pointers:: |
| 47 |
|
| 48 |
ppobj *obj; |
| 49 |
pparray *array; |
| 50 |
ppdict *dict; |
| 51 |
ppstream *stream; |
| 52 |
ppref *ref; |
| 53 |
ppdoc *pdf; |
| 54 |
|
| 55 |
Some of those C-types are defined in library header ``ppapi.h`` (complete types). Some others are incomplete |
| 56 |
(eg. you can't say ``sizeof(ppdoc)`` or ``sizeof(ppxref)``). This is to avoid unnecesary dependencies in |
| 57 |
the header. [At some points it's not clear to me what to hide and what to expose, will see.] The library itself |
| 58 |
uses ``pplib.h`` but for auxilary applications including a standalone ``ppapi.h`` header should be enough. |
| 59 |
|
| 60 |
``pplib`` was designed having **read-only** PDF access in mind. Even if some structure is completelly exposed, |
| 61 |
so that you can directly access its members, you should treat them as read-only. I don't make them ``const`` because |
| 62 |
then all variable declarations would need to be ``const``, which is annoying, and I'd need some trickery in the library |
| 63 |
internals to unconst. Besides, nothing is really const for C type casts. |
| 64 |
|
| 65 |
Object |
| 66 |
------ |
| 67 |
|
| 68 |
A common container for all elementary PDF object types is ``ppobj`` structure. ``ppobj`` has a type identifier |
| 69 |
(integer) and union of values:: |
| 70 |
|
| 71 |
struct ppobj { |
| 72 |
ppobjtp type; |
| 73 |
union { |
| 74 |
ppint integer; |
| 75 |
ppnum number; |
| 76 |
ppname name; |
| 77 |
ppstring string; |
| 78 |
pparray *array; |
| 79 |
ppdict *dict; |
| 80 |
ppstream *stream; |
| 81 |
ppref *ref; |
| 82 |
void *any; |
| 83 |
}; |
| 84 |
}; |
| 85 |
|
| 86 |
Object type is one of constants (enum):: |
| 87 |
|
| 88 |
PPNONE |
| 89 |
PPNULL |
| 90 |
PPBOOL |
| 91 |
PPINT |
| 92 |
PPNUM |
| 93 |
PPNAME |
| 94 |
PPSTRING |
| 95 |
PPARRAY |
| 96 |
PPDICT |
| 97 |
PPSTREAM |
| 98 |
PPREF |
| 99 |
|
| 100 |
The type determines the structure member you're allowed to access:: |
| 101 |
|
| 102 |
ppobj *obj; |
| 103 |
... |
| 104 |
switch (obj->type) |
| 105 |
{ |
| 106 |
case PPNONE: // shouldn't actually happen, indicates some failure |
| 107 |
break; |
| 108 |
case PPNULL: // valid PDF null object, no value |
| 109 |
break; |
| 110 |
case PPBOOL: // do something with obj->integer (ppint), value 0 or 1 |
| 111 |
break; |
| 112 |
case PPINT: // do something with obj->integer (ppint) |
| 113 |
break; |
| 114 |
case PPNUM: // do something with obj->number (ppnum) |
| 115 |
break; |
| 116 |
case PPNAME: // do something with obj->name (ppname) |
| 117 |
break; |
| 118 |
case PPSTRING: // do something with obj->string (ppstring) |
| 119 |
break; |
| 120 |
case PPARRAY: // do something with obj->array (pparray *) |
| 121 |
break; |
| 122 |
case PPDICT: // do something with obj->dict (ppdict *) |
| 123 |
break; |
| 124 |
case PPSTREAM: // do something with obj->stream (ppstream *) |
| 125 |
break; |
| 126 |
case PPREF: // do something with obj->ref (ppref *) |
| 127 |
break; |
| 128 |
} |
| 129 |
|
| 130 |
More often then not you know exactly what type of object value is expected, in which case |
| 131 |
you may use one of the following macros:: |
| 132 |
|
| 133 |
// returns 1 if o->type is PPNULL |
| 134 |
int ppobj_get_null(o) \ |
| 135 |
|
| 136 |
// if o->type is PPBOOL, sets int v to 0 or 1 and returns 1, 0 otherwise |
| 137 |
int ppobj_get_bool(o, v) |
| 138 |
|
| 139 |
// if o->type is PPINT, sets ppint v and returns 1, 0 otherwise |
| 140 |
int ppobj_get_int(o, v) |
| 141 |
|
| 142 |
// if o->type is PPINT and >= 0, sets ppuint v and returns 1, 0 otherwise |
| 143 |
int ppobj_get_uint(o, v) |
| 144 |
|
| 145 |
// if o->type is PPNUM or PPINT, sets ppnum v and returns 1, 0 otherwise |
| 146 |
int ppobj_get_num(o, v) |
| 147 |
|
| 148 |
// if o->type is PPNAME returns the name, NULL otherwise |
| 149 |
ppname ppobj_get_name(o) |
| 150 |
|
| 151 |
// if o->type is PPSTRING returns the string, NULL otherwise |
| 152 |
ppstring ppobj_get_string(o) |
| 153 |
|
| 154 |
// if o->type is PPARRAY returns the array, NULL otherwise |
| 155 |
pparray * ppobj_get_array(o) |
| 156 |
|
| 157 |
// if o->type is PPDICT returns the dict, NULL otherwise |
| 158 |
ppdict * ppobj_get_dict(o) |
| 159 |
|
| 160 |
// if o->type is PPSTREAM returns the stream, NULL otherwise |
| 161 |
ppstream * ppobj_get_stream(o) |
| 162 |
|
| 163 |
// if o->type is PPREF returns the reference, NULL otherwise |
| 164 |
ppref * ppobj_get_ref(o) |
| 165 |
|
| 166 |
Note the coercion from integer to real number, but not reverse. In practise, whenever you expect a real number, |
| 167 |
you should also handle integer (eg. '1' used instead of '1.0'). |
| 168 |
|
| 169 |
It is a common case that the object is given as an indirect reference, but what you actually |
| 170 |
want is not the reference, but the object referred by it. Here is a helper for it:: |
| 171 |
|
| 172 |
// if o->type is PPREF, returns what the reference points, otherwise returns o |
| 173 |
ppobj * ppobj_rget_obj(o) |
| 174 |
|
| 175 |
Also every ``ppobj_get_*`` macro has ``ppobj_rget_*`` counterpart that makes a check |
| 176 |
for the expected type, but if the object is PPREF, it jumps to the target object. |
| 177 |
So for example ``ppobj_rget_dict(obj)`` will return dict if ``obj`` is of type PPDICT |
| 178 |
or if it is of type PPREF and ``obj->ref`` hosts an object of type PPDICT. |
| 179 |
|
| 180 |
Names |
| 181 |
----- |
| 182 |
|
| 183 |
PDF names are represented as ``ppname``. |
| 184 |
I find it convenient to have ``ppname`` type pretending ``const char *``. This allows to use ``ppname`` |
| 185 |
in all C-style string functions like ``printf("%s", name)``. |
| 186 |
|
| 187 |
Be aware, however, that ``ppname`` is actually a C-structure. It is perfectly ok to cast ``ppname`` to ``const char *``:: |
| 188 |
|
| 189 |
ppname name; |
| 190 |
... |
| 191 |
(const char *)name; |
| 192 |
|
| 193 |
But reverse is forbidden:: |
| 194 |
|
| 195 |
const char *cstr = "cstring"; |
| 196 |
... |
| 197 |
(ppname)cstr; // expect segmentation fault soon |
| 198 |
|
| 199 |
For convenient use in C, names are ``'\0'`` terminated. But to get the length of name better always use |
| 200 |
``ppname_size()`` macro. ``ppname`` object knows its size, don't use ``strlen()``:: |
| 201 |
|
| 202 |
size_t ppname_size(ppname name); // macro, returns length of name in bytes |
| 203 |
|
| 204 |
In current implementation names are not hashed anyhow, so name-to-name comparison is not smarter than ``memcmp()``. |
| 205 |
Use macros:: |
| 206 |
|
| 207 |
int ppname_is(ppname name, "literal"); // to compare ppname with C-literal string |
| 208 |
int ppname_eq(ppname name, ppname other); // to compare ppname with a different name |
| 209 |
|
| 210 |
If you'll use ``pplib`` to parse contents streams, you may need to distinguish names from operators |
| 211 |
(more precisely executable names). Names in PDF are preceeded by '/', executable names aren't. In both |
| 212 |
cases PDF parser will produce ``ppname`` but can be distingushed with:: |
| 213 |
|
| 214 |
int ppname_exec(ppname name); // returns non-zero if name is executable |
| 215 |
|
| 216 |
Names are kept in their raw form, with possible PDF specific escapes (in text below we call it **encoded** form). |
| 217 |
Leading '/' is omitted, though. One may need a decoded name, with no PDF escapes. |
| 218 |
A pair of functions provides a simple interface to switch between those two forms:: |
| 219 |
|
| 220 |
ppname ppname_decoded (ppname name); // returns decoded (unescaped) form of the name |
| 221 |
ppname ppname_encoded (ppname name); // returns encoded (escaped) form of the name |
| 222 |
|
| 223 |
In pretty most cases PDF names contains only letters (no special characters, no escapes), so decoded and encoded forms are identical. |
| 224 |
In that case both functions simply return the argument. It is ok to call ``ppname_decoded()`` on already decoded form |
| 225 |
and ``ppname_encoded()`` on already encoded form. Both forms are produced by PDF objects parser, so accessing ``ppname`` alter ego |
| 226 |
in whatever direction needs no extra decoding or allocation costs. |
| 227 |
|
| 228 |
String |
| 229 |
------ |
| 230 |
|
| 231 |
PDF strings have the same internal construction as names, so most of names description above applies to strings as well. |
| 232 |
``ppstring`` is a typedef of ``const char *``, roughly ``'\0'`` terminiated C-string. To get the size of the string:: |
| 233 |
|
| 234 |
size_t ppstring_size(ppstring string); // macro, returns the length of the string in bytes |
| 235 |
|
| 236 |
Strings are provided in their raw form, preserving PDF specific escapes, but with no |
| 237 |
``()`` or ``<>`` delims. To distinguish plain strings from hex strings:: |
| 238 |
|
| 239 |
int ppstring_hex(ppstring string); // macro, returns non zero if hex string |
| 240 |
|
| 241 |
Or if you prefer:: |
| 242 |
|
| 243 |
switch (ppstring_type(string)) |
| 244 |
{ |
| 245 |
case PPSTRING_PLAIN: // literal string, surrounded by ``(`` and ``)`` in PDF |
| 246 |
break; |
| 247 |
case PPSTRING_BASE16: // hex string, surrounded by ``<`` and ``>`` in PDF |
| 248 |
break; |
| 249 |
case PPSTRING_BASE85: // base85 string surrounded by ``<~`` and ``~>`` in PDF |
| 250 |
break; |
| 251 |
} |
| 252 |
|
| 253 |
The last is actually Postscript specific, not used in PDF, but I think it might appear in contents streams... |
| 254 |
No matter how the string is given in PDF (plain or hex), here are two functions to |
| 255 |
switch between encoded and decoded strings forms:: |
| 256 |
|
| 257 |
ppstring ppstring_decoded (ppstring string); // returns decoded string possibly with PDF escapes |
| 258 |
ppstring ppstring_encoded (ppstring string); // returns encoded string with no PDF escapes |
| 259 |
|
| 260 |
For hex strings, encoded form contains hex digits, while decoded form contains arbitrary bytes (the result of hex decoding). |
| 261 |
Plain strings usually contains printable ASCII characters, but they might contain any binary data. |
| 262 |
As with names, objects parser produces both forms. The raw form with PDF escapes (or raw hex form) is considered the main one. |
| 263 |
Eg. when you access ``obj->string`` you always get the encoded form. At any moment you can switch to its alter ego. |
| 264 |
|
| 265 |
No matter if the string is plain or hex, if its first two bytes (decoded) are UTF16 BOM, the string |
| 266 |
is considered unicode. ``ppstring`` object *knows* it is unicode or not:: |
| 267 |
|
| 268 |
switch (ppstring_utf(string)) |
| 269 |
{ |
| 270 |
case PPSTRING_UTF16LE: // unicode string, utf16le |
| 271 |
break; |
| 272 |
case PPSTRING_UTF16BE: // unicode string, utf16be |
| 273 |
break; |
| 274 |
default: // no unicode |
| 275 |
} |
| 276 |
|
| 277 |
Or simply:: |
| 278 |
|
| 279 |
if (ppstring_utf(string) != 0) { |
| 280 |
// handle unicode string |
| 281 |
} |
| 282 |
|
| 283 |
If the string is unicode, BOM remains the part of the string -- ``pplib`` parser does not strip it. |
| 284 |
Unicode or not, encoded or decoded, strings are always C-arrays of bytes and ``ppstring_size()`` |
| 285 |
always returns the size in bytes. |
| 286 |
|
| 287 |
Array |
| 288 |
----- |
| 289 |
|
| 290 |
PDF arrays are represented as ``pparray`` type, which is C-array of ``ppobj`` structures. |
| 291 |
To get the size:: |
| 292 |
|
| 293 |
size_t pparray_size(pparray *array) // macro, returns the number of array items |
| 294 |
|
| 295 |
To get ``ppobj *`` at a given index:: |
| 296 |
|
| 297 |
ppobj * pparray_at(array, index) // macro, returns ppobj * (no index check) |
| 298 |
ppobj * pparray_get(array, index) // macro, returns ppobj * or NULL (with index check) |
| 299 |
ppobj * pparray_get_obj (pparray *array, size_t index); // function equiv to pparray_get() |
| 300 |
|
| 301 |
Iterating over array elements:: |
| 302 |
|
| 303 |
pparray *array; |
| 304 |
size_t index, size; |
| 305 |
ppobj *obj; |
| 306 |
for (size = pparray_size(array), pparray_first(array, index, obj); index < size; pparray_next(index, obj)) |
| 307 |
{ |
| 308 |
// do something with index and obj |
| 309 |
} |
| 310 |
|
| 311 |
There is no magic first/next macros, just iteration over pointers. One could also use something like:: |
| 312 |
|
| 313 |
for (index = 0, size = array->size; index < size; ++index) |
| 314 |
{ |
| 315 |
obj = pparray_at(array, index); |
| 316 |
// do something with index and obj |
| 317 |
} |
| 318 |
|
| 319 |
When getting values from array and expecting a result of known type, use one of the following:: |
| 320 |
|
| 321 |
int pparray_get_bool (pparray *array, size_t index, int *v); // get boolean value |
| 322 |
int pparray_get_int (pparray *array, size_t index, ppint *v); // get ppint value |
| 323 |
int pparray_get_uint (pparray *array, size_t index, ppuint *v); // get ppuint value |
| 324 |
int pparray_get_num (pparray *array, size_t index, ppnum *v); // get ppnum value |
| 325 |
ppname pparray_get_name (pparray *array, size_t index); // get ppname value |
| 326 |
ppstring pparray_get_string (pparray *array, size_t index); // get ppstring value |
| 327 |
pparray * pparray_get_array (pparray *array, size_t index); // get pparray * value |
| 328 |
ppdict * pparray_get_dict (pparray *array, size_t index); // get ppdict * value |
| 329 |
ppref * pparray_get_ref (pparray *array, size_t index); // get ppref * value |
| 330 |
|
| 331 |
As with ``ppobj_get_*`` suite, numeric types getters set the value of a given type and returns 1, if the type matches. |
| 332 |
Otherwise sets nothing and returns 0. Other getters return the value if the type matches, or NULL. |
| 333 |
|
| 334 |
Every function from ``pparray_get_*`` suite have its ``pparray_rget_*`` counterpart that |
| 335 |
that dereferences indirect objects (as explained for ``ppobj_rget_*`` getters). Note that |
| 336 |
there is no ``pparray_get_stream()`` function, as streams in PDF are always indirect. |
| 337 |
To get the stream from array use:: |
| 338 |
|
| 339 |
ppstream * pparray_rget_stream (pparray *array, size_t index); |
| 340 |
|
| 341 |
Dict |
| 342 |
---- |
| 343 |
|
| 344 |
PDF dicts are represented as ``ppdict`` structure, which is C-array of ``ppobj`` with parallel |
| 345 |
C-array of ``ppname`` pointers. To get the size of a dict:: |
| 346 |
|
| 347 |
size_t ppdict_size(ppdict *dict) // macro, returns the number of key-val pairs |
| 348 |
|
| 349 |
To get the value at a given index (integer):: |
| 350 |
|
| 351 |
ppobj * ppdict_at(ppdict *dict, index) // macro, no index check |
| 352 |
|
| 353 |
To get the name (key) at a given index:: |
| 354 |
|
| 355 |
ppname ppdict_key(ppdict *dict, index) // macro, no index check |
| 356 |
|
| 357 |
To iterate over dict key-val pairs:: |
| 358 |
|
| 359 |
ppdict *dict; |
| 360 |
ppname *pkey; |
| 361 |
ppobj *obj; |
| 362 |
|
| 363 |
for (ppdict_first(dict, pkey, obj); *pkey != NULL; ppdict_next(pkey, obj)) |
| 364 |
{ |
| 365 |
// do something with *pkey and obj |
| 366 |
} |
| 367 |
|
| 368 |
There is no magic in first/next macros, just iteration through keys and values lists pointers. |
| 369 |
For convenient iteration, a list of keys is terminated with NULL, so in the code above ``*pkey != NULL`` |
| 370 |
is used as the loop condition. One may also iterate via indices:: |
| 371 |
|
| 372 |
ppdict *dict; |
| 373 |
size_t index, size; |
| 374 |
ppname key; |
| 375 |
ppobj *obj; |
| 376 |
for (index = 0, size = ppdict_size(dict); index < size; ++index) |
| 377 |
{ |
| 378 |
key = ppdict_key(dict, index); |
| 379 |
obj = ppdict_at(dict, index); |
| 380 |
// do something with key and obj |
| 381 |
} |
| 382 |
|
| 383 |
To get the object associated with a given name, use one of the following:: |
| 384 |
|
| 385 |
ppobj * ppdict_get_obj (ppdict *dict, const char *name); |
| 386 |
int ppdict_get_bool (ppdict *dict, const char *name, int *v); |
| 387 |
int ppdict_get_int (ppdict *dict, const char *name, ppint *v); |
| 388 |
int ppdict_get_uint (ppdict *dict, const char *name, ppuint *v); |
| 389 |
int ppdict_get_num (ppdict *dict, const char *name, ppnum *v); |
| 390 |
ppname ppdict_get_name (ppdict *dict, const char *name); |
| 391 |
ppstring ppdict_get_string (ppdict *dict, const char *name); |
| 392 |
pparray * ppdict_get_array (ppdict *dict, const char *name); |
| 393 |
ppdict * ppdict_get_dict (ppdict *dict, const char *name); |
| 394 |
ppref * ppdict_get_ref (ppdict *dict, const char *name); |
| 395 |
|
| 396 |
Note that all getters accepts ``const char *`` as key, so it is ok to say:: |
| 397 |
|
| 398 |
ppdict_rget_dict(dict, "Resources"); |
| 399 |
|
| 400 |
as well as:: |
| 401 |
|
| 402 |
ppdic_rget_dict(dict, name); // ppname name |
| 403 |
|
| 404 |
Every ``ppdict_get_*`` getter has ``ppdict_rget_*`` counterpart that dereferences |
| 405 |
indirect objects if necessary. Note that there is no ``ppdict_get_stream()`` function, |
| 406 |
but there is:: |
| 407 |
|
| 408 |
ppstream * ppdict_rget_stream (ppdict *dict, const char *name); |
| 409 |
|
| 410 |
So far dicts comes with no names mapping, so by-name dict accessors perform a linear search |
| 411 |
through the keys list. PDF dicts are usually small, so it is fast enough. |
| 412 |
Building names lookup for every dict in PDF makes no sense I think, as ``pplib`` applications |
| 413 |
will query just several dicts I guess.. However, some apps may extensively query |
| 414 |
resources, which may refer to hundreds of objects (eg. images). So some mapping for dicts |
| 415 |
is still considered. |
| 416 |
|
| 417 |
Stream |
| 418 |
------ |
| 419 |
|
| 420 |
PDF streams are represented as ``ppstream`` objects. To get the stream dict:: |
| 421 |
|
| 422 |
ppdict * ppstream_dict(ppstream *stream) // macro |
| 423 |
|
| 424 |
To read the stream data:: |
| 425 |
|
| 426 |
uint8_t * ppstream_first (ppstream *stream, size_t *size, int decode); |
| 427 |
uint8_t * ppstream_next (ppstream *stream, size_t *size); |
| 428 |
void ppstream_done (ppstream *stream); |
| 429 |
|
| 430 |
Both ``first` and ``next`` functions return a chunk of stream data and sets the ``size`` of the chunk. |
| 431 |
``decode`` parameter tell the reader to decompress the stream (1) or return raw (0). A call to ``ppstream_next()`` |
| 432 |
must be preceeded by ``ppstream_first()``. Once you're done with the stream, you have to call ``ppstream_done()``, |
| 433 |
no matter if the stream has been read to the end or not. The stream data iterator in use:: |
| 434 |
|
| 435 |
uint8_t *data; |
| 436 |
size_t size; |
| 437 |
ppstream *stream; |
| 438 |
int decode = 1; // 1 - get decompressed, 0 - get raw |
| 439 |
|
| 440 |
for (data = ppstream_first(stream, &size, decode); data != NULL; data = ppstream_next(stream, &size)) |
| 441 |
{ |
| 442 |
// do something with data and its size |
| 443 |
} |
| 444 |
ppstream_done(stream); |
| 445 |
|
| 446 |
Every subsequent iterator call invalidates the previous reader output, so you have to utilize the returned chunk |
| 447 |
of data just after you ot that. So the following is wrong:: |
| 448 |
|
| 449 |
data1 = ppstream_first(stream, &size, 1); |
| 450 |
data2 = ppstream_next(stream, &size); |
| 451 |
data3 = ppstream_next(stream, &size); |
| 452 |
some_output(data1, size); |
| 453 |
some_output(data2, size); |
| 454 |
|
| 455 |
The reader calls usually return the same pointer to internal buffer, just filled with a different data. |
| 456 |
``pplib`` allocates reasonably large buffer and fills that buffer on subsequent calls to the reader. |
| 457 |
|
| 458 |
If the source stream has no compression, using both ``decode == 1`` and ``decode == 0`` should give the same result. |
| 459 |
You can check if the stream is actually compressed with:: |
| 460 |
|
| 461 |
ppstream_compressed(stream) // macro, returns non zero if /Filter is present |
| 462 |
|
| 463 |
It might be necessary to load the entire stream data at once:: |
| 464 |
|
| 465 |
uint8_t * ppstream_all (ppstream *stream, size_t *size, int decode); |
| 466 |
|
| 467 |
If the initial buffer size is insufficient, it grows until the entire stream data is loaded. You must call |
| 468 |
``ppstream_done(stream)`` after using returned data. |
| 469 |
|
| 470 |
``ppstream_done()`` doesn't invalidate the stream object, it just closes its internal reader. |
| 471 |
The stream itself remains a valid object (eg. one can read it again if necessary), |
| 472 |
but the reader buffer is released. It is actually not freed but kept for future the reuse with that on some other stream, |
| 473 |
but you still need to mark it ready for reuse to avoid allocating a separate buffer for every stream you're going to read. |
| 474 |
|
| 475 |
Stream data readers will return ``NULL`` if you haven't close the previous reader process with ``ppstream_done()``. All below is wrong:: |
| 476 |
|
| 477 |
data1 = ppstream_all(stream, &size, 1); |
| 478 |
data2 = ppstream_all(stream, &size, 1); // data2 == NULL |
| 479 |
// or |
| 480 |
data1 = ppstream_first(stream, &size, 1); |
| 481 |
data2 = ppstream_first(stream, &size, 1); // data2 == NULL |
| 482 |
// or |
| 483 |
data1 = ppstream_first(stream, &size, 1); |
| 484 |
data2 = ppstream_all(stream, &size, 1); // data2 == NULL |
| 485 |
|
| 486 |
To avoid unnecessary dependencies, ``pplib`` does not support image filters (``/DCT``, ``/JPX``, ``/JBIG``, ``/CCITT``). |
| 487 |
But it is ok to read the stream with ``decode`` set to 1 on such streams. ``pplib`` assumes that the image is the |
| 488 |
final/target stream form and just returns it as-is. Eg. in the case of JPEG (``/DCT`` filtered) image both calls should |
| 489 |
give the same results:: |
| 490 |
|
| 491 |
ppstream_all(jpegstream, &jpegsize, 0); // don't decode, return what's there |
| 492 |
ppstream_all(jpegstream, &jpegsize, 1); // decode but found image filter, effectively the same |
| 493 |
|
| 494 |
A bit more about streams memory. As mentioned, ``pplib`` allocates buffers for stream readers. After ``ppstream_done()``, |
| 495 |
the stream no longer *owns* the buffer space. But the buffer may remain allocated, to be reused with future readers. |
| 496 |
``pplib`` keeps a pool of several buffers. This means, that when you use stream readers, ``pplib`` eats |
| 497 |
some memory (1MB or so) that is not freed, even if no streams are used. And even if you free all objects. |
| 498 |
If you suffer from this, you can optionally use a pair of functions:: |
| 499 |
|
| 500 |
void ppstream_init_buffers (void); |
| 501 |
void ppstream_free_buffers (void); |
| 502 |
|
| 503 |
The first initializes buffers pool, unless done so far. Currently ``pplib`` cares of it before opening every stream reader, |
| 504 |
so it is not obligatory. The second frees a pool of buffers. The intended use is to call ``ppstream_init_buffers()`` once |
| 505 |
as kind of library initializer and to call ``ppstream_free_buffers()`` once, as the library finalizer. |
| 506 |
|
| 507 |
Filters |
| 508 |
------- |
| 509 |
|
| 510 |
In version v1.00 (20190916) ``ppstream`` API has been extended with filters information. |
| 511 |
``ppstream`` knows its filter(s) and keps it as ``stream->filter``:: |
| 512 |
|
| 513 |
// ppstream *stream; |
| 514 |
ppstream_filter *info = &stream->filter; |
| 515 |
|
| 516 |
``ppstream_filter`` is the following structure:: |
| 517 |
|
| 518 |
typedef struct { |
| 519 |
ppstreamtp *filters; // c-array of filter identifiers (enum integers) |
| 520 |
ppdict **params; // c-array of ppdict pointers |
| 521 |
size_t count; // number of filters, length of the arrays (typically 1) |
| 522 |
} ppstream_filter; |
| 523 |
|
| 524 |
If ``count > 0`` then ``filters`` member is not NULL. Filters array keeps integer constants:: |
| 525 |
|
| 526 |
PPSTREAM_BASE16 /* /ASCIIHexDecode */ |
| 527 |
PPSTREAM_BASE85 /* /ASCII85Decode */ |
| 528 |
PPSTREAM_RUNLENGTH /* /RunLengthDecode */ |
| 529 |
PPSTREAM_FLATE /* /FlateDecode */ |
| 530 |
PPSTREAM_LZW /* /LZWDecode */ |
| 531 |
PPSTREAM_CCITT /* /CCITTFaxDecode */ |
| 532 |
PPSTREAM_DCT /* /DCTDecode */ |
| 533 |
PPSTREAM_JBIG2 /* /JBIG2Decode */ |
| 534 |
PPSTREAM_JPX /* /JPXDecode */ |
| 535 |
PPSTREAM_CRYPT /* /Crypt */ |
| 536 |
|
| 537 |
Params array keeps corresponding filter parameters (``/DecodeParms``) if present. ``params`` member is not NULL |
| 538 |
if ``count > 0`` and the stream dict has ``/DecodeParms`` entry. Even if ``params`` is there, |
| 539 |
for every N-th filter, ``params[N]`` may be NULL (corresponding to PDF ``null``). |
| 540 |
|
| 541 |
``stream->filter`` keeps the source stream filter information, which may not correspond to the result of stream readers |
| 542 |
(``ppstream_first()``, ``ppstream_next()``, ``ppstream_all()``). The get the filters info relevant to the result from readers:: |
| 543 |
|
| 544 |
void ppstream_filter_info (ppstream *stream, ppstream_filter *info, int decode); |
| 545 |
|
| 546 |
The function fills ``ppstream_filter`` structure according to the expected result from stream readers (example 3 shows |
| 547 |
how to use it to reconstruct ``/Filter`` and ``/DecodeParms`` when copying the stream to some other PDF). |
| 548 |
|
| 549 |
To convert filter identifier (``ppstreamtp``) to a corresponding PDF filter name:: |
| 550 |
|
| 551 |
const char * ppstream_filter_name[]; |
| 552 |
|
| 553 |
To covert ``ppname`` to filter identifier:: |
| 554 |
|
| 555 |
int ppstream_filter_type (ppname filtername, ppstreamtp *filtertype); |
| 556 |
// returns 1 and sets filtertype if filtername is the proper filter name |
| 557 |
|
| 558 |
Additional information about the stream can be fetched from macros:: |
| 559 |
|
| 560 |
ppstream_compressed(stream) /* stream->flags & (PPSTREAM_FILTER|PPSTREAM_IMAGE) */ |
| 561 |
ppstream_filtered(stream) /* stream->flags & PPSTREAM_FILTER */ |
| 562 |
ppstream_image(stream) /* stream->flags * PPSTREAM_IMAGE */ |
| 563 |
|
| 564 |
``stream->flags`` is a binary sum of the following:: |
| 565 |
|
| 566 |
PPSTREAM_FILTER /* set iff the stream filters list has one of: BASE16, BASE85, RUNLENGTH, FLATE, LZW */ |
| 567 |
PPSTREAM_IMAGE /* set iff the stream filters list has one of: CCITT, DCT, JBIG2, JPX */ |
| 568 |
PPSTREAM_ENCRYPTED /* set iff the stream is encrypted */ |
| 569 |
PPSTREAM_ENCRYPTED_OWN /* set iff the stream has own CRYPT filter */ |
| 570 |
|
| 571 |
Note that ``PPSTREAM_COMPRESSED`` is not there any longer, use ``ppstream_compressed()`` instead. |
| 572 |
And there is some more, see ``ppapi.h``. |
| 573 |
|
| 574 |
Ref |
| 575 |
--- |
| 576 |
|
| 577 |
Indirect objects are represented as ``ppref`` structure. To get the object that the |
| 578 |
reference refers to:: |
| 579 |
|
| 580 |
ppobj * ppref_obj(ppref *ref) // macro |
| 581 |
|
| 582 |
``ppref`` structure also keeps the reference number and version, a pointer to cross reference table it belongs |
| 583 |
to and others, but I guess you won't need anything but the referenced object. ``pplib`` parser resolves references |
| 584 |
on-fly. So if there is a dict with indirect objects:: |
| 585 |
|
| 586 |
<< |
| 587 |
/Type /Page |
| 588 |
/Resources 123 0 R |
| 589 |
... |
| 590 |
>> |
| 591 |
|
| 592 |
the parser will produce ``ppdict`` with ``Resources`` key pointing the proper ``ppref *`` value. |
| 593 |
If you need more, access ``ppref`` members:: |
| 594 |
|
| 595 |
struct ppref { |
| 596 |
ppobj object; // target object |
| 597 |
ppuint number, version; // identifiers |
| 598 |
size_t offset; // file offset (useless for you, may be zero for compressed objects) |
| 599 |
ppuint length; // the length of the original object data |
| 600 |
ppxref *xref; // cross reference table it belongs to |
| 601 |
}; |
| 602 |
|
| 603 |
|
| 604 |
XRef |
| 605 |
---- |
| 606 |
|
| 607 |
Cross reference table is exposed as ``ppxref`` (incomplete type, you can only oprate on its pointer). |
| 608 |
To get top document xref:: |
| 609 |
|
| 610 |
ppxref * ppdoc_xref (ppdoc *pdf); |
| 611 |
|
| 612 |
To get previous (older) xref:: |
| 613 |
|
| 614 |
ppxref * ppxref_prev (ppxref *xref); |
| 615 |
|
| 616 |
To find an object of a given refnumber:: |
| 617 |
|
| 618 |
ppref * ppxref_find (ppxref *xref, ppuint refnumber); |
| 619 |
|
| 620 |
[Note: since pplib v0.98 in case of documents with incremental update, ``ppxref_find()`` returns |
| 621 |
the newest available version of a given object rather than the object in a given body.] |
| 622 |
|
| 623 |
PDF |
| 624 |
--- |
| 625 |
|
| 626 |
PDF document is represented as ``ppdoc`` structure (incomplete type, you can only operate on its pointer). |
| 627 |
To load a document from file:: |
| 628 |
|
| 629 |
ppdoc * ppdoc_load (const char *filename); |
| 630 |
|
| 631 |
To load a document from memory data:: |
| 632 |
|
| 633 |
ppdoc * ppdoc_mem (const void *data, size_t size); |
| 634 |
|
| 635 |
The data is assumed to be a buffer allocated with ``malloc`` - it is freed when destroying ``ppdoc``. |
| 636 |
|
| 637 |
Both loaders returns ``NULL`` on failure. |
| 638 |
|
| 639 |
To free ``ppdoc`` and all objects it refers to:: |
| 640 |
|
| 641 |
void ppdoc_free (ppdoc *pdf); |
| 642 |
|
| 643 |
So far we haven't mention about any explicit object reclaimers. There are no dedicated ``free`` functions |
| 644 |
for other objects. You don't allocate or free objects yourself. ``ppdoc`` object is an owner of all |
| 645 |
beings it refers to. It also means that every object described so far is alive as long as the containing |
| 646 |
``ppdoc`` is alive. |
| 647 |
|
| 648 |
To access main PDF dicts:: |
| 649 |
|
| 650 |
ppdict * ppdoc_trailer(ppdoc *pdf); // returns top xref trailer dict |
| 651 |
ppdict * ppdoc_catalog(ppdoc *pdf); // returns catalog referred from the trailer |
| 652 |
ppdict * ppdoc_info(ppdoc *pdf); // returns info dict referred from the trailer |
| 653 |
|
| 654 |
To get the PDF version:: |
| 655 |
|
| 656 |
const char * ppdoc_version_string (ppdoc *pdf); // version string |
| 657 |
int ppdoc_version_number (ppdoc *pdf, int *minor); // minor and major numbers |
| 658 |
|
| 659 |
To get the file size of the source PDF document:: |
| 660 |
|
| 661 |
size_t ppdoc_file_size (ppdoc *pdf); |
| 662 |
|
| 663 |
To get the number of objects in all xrefs:: |
| 664 |
|
| 665 |
ppuint ppdoc_objects (ppdoc *pdf); |
| 666 |
|
| 667 |
To get the approx usage of memory:: |
| 668 |
|
| 669 |
size_t ppdoc_memory (ppdoc *pdf, size_t *waste); |
| 670 |
|
| 671 |
Encryption |
| 672 |
---------- |
| 673 |
|
| 674 |
``pplib`` handles encrypted (password protected) documents. If a document is encrypted, most of strings and streams are ciphered. |
| 675 |
In that form they are unreadable and rather useless, you can't even rewrite such strings/streams as-is to a different PDF output. |
| 676 |
It is a common practise to *protect* documents with an empty password. Such documents remain readable in Acrobat (just opens them without prompting |
| 677 |
for a password), but some features (eg. printing) may restricted by the application. |
| 678 |
|
| 679 |
When ``pplib`` detects encryption, it follows Acrobat approach and first tries an empty password. If it succeeds, ``pplib`` proceeeds normally, providing |
| 680 |
an access to decrypted strings and streams, as if they weren't ciphered. If the document is protected with non-empty password, ``pplib`` gives |
| 681 |
a way to provide a password and proceed. Until you provide a password, ``ppdoc`` object returned by ``ppdoc_load()`` function has all object wntries |
| 682 |
set to ``null``. |
| 683 |
|
| 684 |
After loading a document you should check encryption status with:: |
| 685 |
|
| 686 |
ppcrypt_status ppdoc_crypt_status (ppdoc *pdf); |
| 687 |
|
| 688 |
``ppcrypt_status`` (integer) may have the following values: |
| 689 |
|
| 690 |
``PPCRYPT_NONE`` - no encryption, go ahead |
| 691 |
``PPCRYPT_DONE`` - encryption present but password succeeded, go ahead |
| 692 |
``PPCRYPT_PASS`` - encryption present, need non-empty password |
| 693 |
``PPCRYPT_FAIL`` - invalid or unsupported encryption (eg. undocumented in pdf spec) |
| 694 |
|
| 695 |
If a password is needed, you can provide one with:: |
| 696 |
|
| 697 |
ppcrypt_status ppdoc_crypt_pass (ppdoc *pdf, const void *userpass, size_t userpasslength, |
| 698 |
const void *ownerpass, size_t ownerpasslength); |
| 699 |
|
| 700 |
Well, yes, there are actually two passwords in encrypted documents. Relation between them is obscure to me, but enough |
| 701 |
to know that having one of them is enough to decrypt the document. If you know the password, you probably mean |
| 702 |
``userpass``, in which case you should put ``NULL`` as ``ownerpass``. The function returns ``PPCRYPT_DONE`` if the password |
| 703 |
succeeds and the previous status otherwise. Your custom loader function may look like that:: |
| 704 |
|
| 705 |
ppdoc *pdf; |
| 706 |
pdf = ppdoc_load("file.pdf"); |
| 707 |
if (pdf == NULL) |
| 708 |
return NULL; |
| 709 |
switch (ppdoc_crypt_status(pdf)) |
| 710 |
{ |
| 711 |
case PPCRYPT_NONE: |
| 712 |
case PPCRYPT_DONE: |
| 713 |
return pdf; |
| 714 |
case PPCRYPT_PASS: |
| 715 |
if (ppdoc_crypt_pass(pdf, "dummy", 5, NULL, 0) == PPCRYPT_DONE || |
| 716 |
ppdoc_crypt_pass(pdf, NULL, 0, "dummy", 5) == PPCRYPT_DONE) |
| 717 |
return pdf; |
| 718 |
printf("sorry, password needed\n"); |
| 719 |
ppdoc_free(pdf); |
| 720 |
return NULL; |
| 721 |
case PPCRYPT_FAIL: |
| 722 |
printf("sorry, encryption failed\n"); |
| 723 |
ppdoc_free(pdf); |
| 724 |
return NULL; |
| 725 |
} |
| 726 |
|
| 727 |
[If you get ``PPCRYPT_FAIL`` it might mean *I failed*, so treat as a bug.] |
| 728 |
|
| 729 |
If you'd like to know what permissions are given/restricted to encrypted document:: |
| 730 |
|
| 731 |
ppint ppdoc_permissions (ppdoc *pdf); |
| 732 |
|
| 733 |
Returned value can be queried with the following binary flags (you can verify with Acrobat *File -> Properties -> Security* tab):: |
| 734 |
|
| 735 |
PPDOC_ALLOW_PRINT // printing |
| 736 |
PPDOC_ALLOW_MODIFY // filling form fields, signing, creating template pages |
| 737 |
PPDOC_ALLOW_COPY // copying, copying for accessibility |
| 738 |
PPDOC_ALLOW_ANNOTS // filling form fields, copying, signing |
| 739 |
PPDOC_ALLOW_EXTRACT // contents copying for accessibility |
| 740 |
PPDOC_ALLOW_ASSEMBLY // (no effect) |
| 741 |
PPDOC_ALLOW_PRINT_HIRES // (no effect) |
| 742 |
|
| 743 |
``pplib`` does absolutelly nothing with permissions, it cares only to decrypt the document. As mentioned, encryption applies to strings |
| 744 |
and streams. ``pplib`` decrypt strings when parsing document objects, so the result you get is *normal* (not ciphered). |
| 745 |
Streams are decrypted whenever you access them. Even if you ask for a raw stream data, you'll get a raw (compressed) stream, but decrypted. |
| 746 |
So except the check to ``ppdoc_crypt_status()``, you shouldn't bother about encryption. |
| 747 |
|
| 748 |
In encrypted documents most of streams are encrypted. To check if a given stream is encrypted:: |
| 749 |
|
| 750 |
ppstream_encrypted(stream) // macro, returns non-zero if encrypted |
| 751 |
|
| 752 |
Encryption is independent from compression, don't confuse with ``ppstream_compressed()`` |
| 753 |
|
| 754 |
Pages |
| 755 |
----- |
| 756 |
|
| 757 |
Several helpers to deal with pages. To get the number of pages:: |
| 758 |
|
| 759 |
ppuint ppdoc_page_count (ppdoc *pdf); |
| 760 |
|
| 761 |
To access the root pages tree node:: |
| 762 |
|
| 763 |
ppref * ppdoc_pages(ppdoc *pdf); |
| 764 |
|
| 765 |
To get the page reference at a given index:: |
| 766 |
|
| 767 |
ppref * ppdoc_page (ppdoc *pdf, ppuint index); |
| 768 |
|
| 769 |
``index`` is a page number. First page has number 1. For index out of bounds ``ppdoc_page()`` returns NULL. |
| 770 |
Iterating over pages using index from 1 to ``ppdoc_page_count()`` and calling ``ppdoc_page()`` on each iteration |
| 771 |
would be suboptimal. Here is a dedicted iterator for this:: |
| 772 |
|
| 773 |
ppref * ppdoc_first_page (ppdoc *pdf); |
| 774 |
ppref * ppdoc_next_page (ppdoc *pdf); |
| 775 |
|
| 776 |
The iterator in use:: |
| 777 |
|
| 778 |
ppdoc *pdf; |
| 779 |
ppref *ref; |
| 780 |
ppdict *dict; |
| 781 |
int pageno; |
| 782 |
|
| 783 |
pdf = ppdoc_load("file.pdf"); |
| 784 |
for (ref = ppdoc_first_page(pdf), pageno = 1; ref != NULL; ref = ppdoc_next_page(pdf), ++pageno) |
| 785 |
{ |
| 786 |
dict = ppref_obj(obj)->dict; // take for granted it is a dict |
| 787 |
// do something with the page dict |
| 788 |
} |
| 789 |
|
| 790 |
Functions related to pages return ``ppref *`` ensured to contain dict object, so you don't need sanity |
| 791 |
type checks here. |
| 792 |
|
| 793 |
Contents |
| 794 |
-------- |
| 795 |
|
| 796 |
PDF page contents can be given as a stream or array of streams. Here is a convenience iterator over page |
| 797 |
contents streams:: |
| 798 |
|
| 799 |
ppstream * ppcontents_first (ppdict *dict); |
| 800 |
ppstream * ppcontents_next (ppdict *dict, ppstream *stream); |
| 801 |
|
| 802 |
A complete example of contents stream parser use is given below (example 2). |
| 803 |
But before we get there, we need to introduce ``ppcontext`` object. Conceptually, |
| 804 |
``ppcontext`` is an owner (memory handler) of objects created on demand (beyond the ``ppdoc``). |
| 805 |
So far used only with contents stream parser, which might produce quite some data that we want |
| 806 |
to release just after used. To create a new context:: |
| 807 |
|
| 808 |
pcontext * ppcontext_new (void); |
| 809 |
|
| 810 |
It initializes a new context and its internal memory heap, taking about 64kB on start. After that, |
| 811 |
the context is ready to produce objects (contents parsing functions below). Once objects produced |
| 812 |
from a given context are no longer needed:: |
| 813 |
|
| 814 |
void ppcontext_done (ppcontext *context); |
| 815 |
|
| 816 |
It restores the context to its initial state, as after ``ppcontext_new()``. It means that the context |
| 817 |
is ready to produce another bunch of beings (in the example below, all objects from the next page contents). |
| 818 |
Once the context is not needed anymore:: |
| 819 |
|
| 820 |
void ppcontext_free (ppcontext *context); |
| 821 |
|
| 822 |
Now, contents stream parser functions take the context as an argument. Iterator form of contents stream parser |
| 823 |
that allows to process the contents operator by operator:: |
| 824 |
|
| 825 |
ppobj * ppcontents_first_op (ppcontext *context, ppstream *stream, size_t *psize, ppname *pname); |
| 826 |
ppobj * ppcontents_next_op (ppcontext *context, ppstream *stream, size_t *psize, ppname *pname); |
| 827 |
|
| 828 |
Returned ``ppobj *`` is a pointer to operands list. ``*psize`` is the number of operands on stack. |
| 829 |
The operator itself is stored as ``*pname``. |
| 830 |
|
| 831 |
To parse the entire contents stream at once with no stop at every operator:: |
| 832 |
|
| 833 |
ppobj * ppcontents_parse (ppcontext *context, ppstream *stream, size_t *psize); |
| 834 |
|
| 835 |
Returns probably quite long list of all parsed objects (operands and operatos) in one piece. |
| 836 |
The number of objects is stored to ``*psize``. |
| 837 |
|
| 838 |
[Contents may contain so called inline images, that breaks a simple scheme of operands / operator syntax:: |
| 839 |
|
| 840 |
BI <keyval pairs> ID <binary image data> EI |
| 841 |
|
| 842 |
Contents parser treats this genuine triplet as a single piece, producing two operands (dict and string) |
| 843 |
followed by ``EI`` operator name.] |
| 844 |
|
| 845 |
Boxes |
| 846 |
----- |
| 847 |
|
| 848 |
Boxes (rectangles) in PDF are roughly 4-number arrays, but with a special intent. |
| 849 |
``pplib`` provides a basic interface for these special arrays:: |
| 850 |
|
| 851 |
typedef struct { |
| 852 |
ppnum lx, ly, rx, ry; |
| 853 |
} pprect; |
| 854 |
|
| 855 |
This type is used only by helper functions - PDF parser is not aware of the rectangle type. |
| 856 |
To convert ``pparray`` to ``pprect``:: |
| 857 |
|
| 858 |
pprect * pparray_to_rect (pparray *array, pprect *rect); // returns rect or NULL |
| 859 |
|
| 860 |
In example:: |
| 861 |
|
| 862 |
pprect rect; |
| 863 |
if (pparray_to_rect(array, &rect) != NULL) |
| 864 |
; // do something with rect |
| 865 |
|
| 866 |
To get some image bounding box:: |
| 867 |
|
| 868 |
pprect * ppdict_get_rect (ppdict *dict, const char *name, pprect *rect); |
| 869 |
// eg. ppdict_get_rect(imagedict, "BBox", &rect) |
| 870 |
|
| 871 |
To get some page box:: |
| 872 |
|
| 873 |
pprect * ppdict_get_box (ppdict *dict, const char *name, pprect *rect); |
| 874 |
// eg. ppdict_get_box(pagedict, "MediaBox", &rect) |
| 875 |
|
| 876 |
The later not only checks the pagedict, but also goes through parent page nodes. |
| 877 |
|
| 878 |
Transforms |
| 879 |
---------- |
| 880 |
|
| 881 |
Transformations are given as 6-number arrays, but with a special intent. |
| 882 |
``pplib`` provides a basic interface for these special arrays:: |
| 883 |
|
| 884 |
typedef struct { |
| 885 |
ppnum xx, xy, yx, yy, x, y; |
| 886 |
} ppmatrix; |
| 887 |
|
| 888 |
This type is used only by helper functions - PDF parser is not aware of the matrix type. |
| 889 |
To convert ``pparray`` to ``ppmatrix``:: |
| 890 |
|
| 891 |
ppmatrix * pparray_to_matrix (pparray *array, ppmatrix *matrix); |
| 892 |
|
| 893 |
In example:: |
| 894 |
|
| 895 |
ppmatrix matrix; |
| 896 |
if (pparray_to_matrix(array, &matrix) != NULL) |
| 897 |
; // do something with matrix |
| 898 |
|
| 899 |
To get the matrix from dict:: |
| 900 |
|
| 901 |
ppmatrix * ppdict_get_matrix (ppdict *dict, const char *name, ppmatrix *matrix); |
| 902 |
// eg. ppdict_get_matrix(imagedict, "Matrix", &matrix) |
| 903 |
|
| 904 |
Errors handling |
| 905 |
--------------- |
| 906 |
|
| 907 |
``pplib`` is not verbose, but might happen that it needs to log some error message, eg. when parsing |
| 908 |
of some PDF boject fails due to invalid offsets. By default, ``pplib`` prints the message to stdout, eg.:: |
| 909 |
|
| 910 |
invalid 123 0 R object at offset 123123 |
| 911 |
|
| 912 |
To replace the default logger, you can provide your own:: |
| 913 |
|
| 914 |
void pplog_callback (pplogger_callback logger, void *alien); |
| 915 |
|
| 916 |
``pplogger_callback`` is a function:: |
| 917 |
|
| 918 |
void your_callback (const char *message, void *alien); |
| 919 |
|
| 920 |
In example, to redirect messages to stderr you may define a function:: |
| 921 |
|
| 922 |
void your_callback (const char *message, void *alien) |
| 923 |
{ |
| 924 |
fprintf((FILE *)alien, "\nooops: %s\n", message); |
| 925 |
} |
| 926 |
|
| 927 |
Then set the callback somewhere before loading documents:: |
| 928 |
|
| 929 |
pplog_callback(your_callback, stderr); |
| 930 |
|
| 931 |
(example 2 uses that). |
| 932 |
|
| 933 |
To set the default log messages prefix, eg. ``pplib:``, use:: |
| 934 |
|
| 935 |
int pplog_prefix (const char *prefix) |
| 936 |
|
| 937 |
Default is empty. The function succeeds if provided prefix is reasonably short (less then 32 bytes). |