~ubuntu-branches/ubuntu/natty/libharu/natty

« back to all changes in this revision

Viewing changes to .pc/0009-fix-bug-13-various-fixes.patch/src/hpdf_doc.c

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-18 11:35:53 UTC
  • Revision ID: james.westby@ubuntu.com-20100718113553-5bkdgz35iie7f50n
Tags: 2.1.0+dfsg-1
* Initial release (Closes: #581397)
* Delete unused windows build files
* Delete non-free pngsuite from demos
* Remove other copy of pngsuite
* Remove demo pdf files
* Remove font from demo (already in debian)
* Remove font Penguinattack, already packaged
* Delete unneeded autom4te cache files
* Update example to work with font in debian
* Initial import debian/* files
* Update autotools
* Different patches backported from the upstream git repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * << Haru Free PDF Library >> -- hpdf_doc.c
 
3
 *
 
4
 * URL: http://libharu.org
 
5
 *
 
6
 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
 
7
 * Copyright (c) 2007-2008 Antony Dovgal <tony@daylessday.org>
 
8
 *
 
9
 * Permission to use, copy, modify, distribute and sell this software
 
10
 * and its documentation for any purpose is hereby granted without fee,
 
11
 * provided that the above copyright notice appear in all copies and
 
12
 * that both that copyright notice and this permission notice appear
 
13
 * in supporting documentation.
 
14
 * It is provided "as is" without express or implied warranty.
 
15
 *
 
16
 */
 
17
 
 
18
 
 
19
#include "hpdf_conf.h"
 
20
#include "hpdf_utils.h"
 
21
#include "hpdf_encryptdict.h"
 
22
#include "hpdf_destination.h"
 
23
#include "hpdf_info.h"
 
24
#include "hpdf_page_label.h"
 
25
#include "hpdf.h"
 
26
 
 
27
 
 
28
static const char *HPDF_VERSION_STR[6] = {
 
29
                "%PDF-1.2\012%\267\276\255\252\012",
 
30
                "%PDF-1.3\012%\267\276\255\252\012",
 
31
                "%PDF-1.4\012%\267\276\255\252\012",
 
32
                "%PDF-1.5\012%\267\276\255\252\012",
 
33
                "%PDF-1.6\012%\267\276\255\252\012",
 
34
                "%PDF-1.7\012%\267\276\255\252\012"
 
35
};
 
36
 
 
37
 
 
38
static HPDF_STATUS
 
39
WriteHeader  (HPDF_Doc      pdf,
 
40
              HPDF_Stream   stream);
 
41
 
 
42
 
 
43
static HPDF_STATUS
 
44
PrepareTrailer  (HPDF_Doc   pdf);
 
45
 
 
46
 
 
47
static void
 
48
FreeEncoderList (HPDF_Doc  pdf);
 
49
 
 
50
 
 
51
static void
 
52
FreeFontDefList (HPDF_Doc  pdf);
 
53
 
 
54
 
 
55
static void
 
56
CleanupFontDefList (HPDF_Doc  pdf);
 
57
 
 
58
 
 
59
static HPDF_Dict
 
60
GetInfo  (HPDF_Doc  pdf);
 
61
 
 
62
static HPDF_STATUS
 
63
InternalSaveToStream  (HPDF_Doc      pdf,
 
64
                       HPDF_Stream   stream);
 
65
 
 
66
static const char*
 
67
LoadType1FontFromStream (HPDF_Doc     pdf,
 
68
                         HPDF_Stream  afmdata,
 
69
                         HPDF_Stream  pfmdata);
 
70
 
 
71
 
 
72
static const char*
 
73
LoadTTFontFromStream (HPDF_Doc         pdf,
 
74
                      HPDF_Stream      font_data,
 
75
                      HPDF_BOOL        embedding,
 
76
                       const char      *file_name);
 
77
 
 
78
 
 
79
static const char*
 
80
LoadTTFontFromStream2 (HPDF_Doc         pdf,
 
81
                       HPDF_Stream      font_data,
 
82
                       HPDF_UINT        index,
 
83
                       HPDF_BOOL        embedding,
 
84
                       const char      *file_name);
 
85
 
 
86
 
 
87
/*---------------------------------------------------------------------------*/
 
88
 
 
89
HPDF_EXPORT(const char *)
 
90
HPDF_GetVersion (void)
 
91
{
 
92
    return HPDF_VERSION_TEXT;
 
93
}
 
94
 
 
95
 
 
96
HPDF_BOOL
 
97
HPDF_Doc_Validate  (HPDF_Doc  pdf)
 
98
{
 
99
    HPDF_PTRACE ((" HPDF_Doc_Validate\n"));
 
100
 
 
101
    if (!pdf || pdf->sig_bytes != HPDF_SIG_BYTES)
 
102
        return HPDF_FALSE;
 
103
    else
 
104
        return HPDF_TRUE;
 
105
}
 
106
 
 
107
 
 
108
HPDF_EXPORT(HPDF_BOOL)
 
109
HPDF_HasDoc  (HPDF_Doc  pdf)
 
110
{
 
111
    HPDF_PTRACE ((" HPDF_HasDoc\n"));
 
112
 
 
113
    if (!pdf || pdf->sig_bytes != HPDF_SIG_BYTES)
 
114
        return HPDF_FALSE;
 
115
 
 
116
    if (!pdf->catalog || pdf->error.error_no != HPDF_NOERROR) {
 
117
        HPDF_RaiseError (&pdf->error, HPDF_INVALID_DOCUMENT, 0);
 
118
        return HPDF_FALSE;
 
119
    } else
 
120
        return HPDF_TRUE;
 
121
}
 
122
 
 
123
 
 
124
HPDF_EXPORT(HPDF_Doc)
 
125
HPDF_New  (HPDF_Error_Handler    user_error_fn,
 
126
           void                  *user_data)
 
127
{
 
128
    HPDF_PTRACE ((" HPDF_New\n"));
 
129
 
 
130
    return HPDF_NewEx (user_error_fn, NULL, NULL, 0, user_data);
 
131
}
 
132
 
 
133
 
 
134
HPDF_EXPORT(HPDF_Doc)
 
135
HPDF_NewEx  (HPDF_Error_Handler    user_error_fn,
 
136
             HPDF_Alloc_Func       user_alloc_fn,
 
137
             HPDF_Free_Func        user_free_fn,
 
138
             HPDF_UINT             mem_pool_buf_size,
 
139
             void                 *user_data)
 
140
{
 
141
    HPDF_Doc pdf;
 
142
    HPDF_MMgr mmgr;
 
143
    HPDF_Error_Rec tmp_error;
 
144
 
 
145
    HPDF_PTRACE ((" HPDF_NewEx\n"));
 
146
 
 
147
    /* initialize temporary-error object */
 
148
    HPDF_Error_Init (&tmp_error, user_data);
 
149
 
 
150
    /* create memory-manager object */
 
151
    mmgr = HPDF_MMgr_New (&tmp_error, mem_pool_buf_size, user_alloc_fn,
 
152
            user_free_fn);
 
153
    if (!mmgr) {
 
154
        HPDF_CheckError (&tmp_error);
 
155
        return NULL;
 
156
    }
 
157
 
 
158
    /* now create pdf_doc object */
 
159
    pdf = HPDF_GetMem (mmgr, sizeof (HPDF_Doc_Rec));
 
160
    if (!pdf) {
 
161
        HPDF_MMgr_Free (mmgr);
 
162
        HPDF_CheckError (&tmp_error);
 
163
        return NULL;
 
164
    }
 
165
 
 
166
    HPDF_MemSet (pdf, 0, sizeof (HPDF_Doc_Rec));
 
167
    pdf->sig_bytes = HPDF_SIG_BYTES;
 
168
    pdf->mmgr = mmgr;
 
169
    pdf->pdf_version = HPDF_VER_13;
 
170
    pdf->compression_mode = HPDF_COMP_NONE;
 
171
 
 
172
    /* copy the data of temporary-error object to the one which is
 
173
       included in pdf_doc object */
 
174
    pdf->error = tmp_error;
 
175
 
 
176
    /* switch the error-object of memory-manager */
 
177
    mmgr->error = &pdf->error;
 
178
 
 
179
    if (HPDF_NewDoc (pdf) != HPDF_OK) {
 
180
        HPDF_Free (pdf);
 
181
        HPDF_CheckError (&tmp_error);
 
182
        return NULL;
 
183
    }
 
184
 
 
185
    pdf->error.error_fn = user_error_fn;
 
186
 
 
187
    return pdf;
 
188
}
 
189
 
 
190
 
 
191
HPDF_EXPORT(void)
 
192
HPDF_Free  (HPDF_Doc  pdf)
 
193
{
 
194
    HPDF_PTRACE ((" HPDF_Free\n"));
 
195
 
 
196
    if (pdf) {
 
197
        HPDF_MMgr mmgr = pdf->mmgr;
 
198
 
 
199
        HPDF_FreeDocAll (pdf);
 
200
 
 
201
        pdf->sig_bytes = 0;
 
202
 
 
203
        HPDF_FreeMem (mmgr, pdf);
 
204
        HPDF_MMgr_Free (mmgr);
 
205
    }
 
206
}
 
207
 
 
208
 
 
209
HPDF_EXPORT(HPDF_STATUS)
 
210
HPDF_NewDoc  (HPDF_Doc  pdf)
 
211
{
 
212
    char buf[HPDF_TMP_BUF_SIZ];
 
213
    char *ptr = buf;
 
214
    char *eptr = buf + HPDF_TMP_BUF_SIZ - 1;
 
215
    const char *version;
 
216
 
 
217
    HPDF_PTRACE ((" HPDF_NewDoc\n"));
 
218
 
 
219
    if (!HPDF_Doc_Validate (pdf))
 
220
        return HPDF_DOC_INVALID_OBJECT;
 
221
 
 
222
    HPDF_FreeDoc (pdf);
 
223
 
 
224
    pdf->xref = HPDF_Xref_New (pdf->mmgr, 0);
 
225
    if (!pdf->xref)
 
226
        return HPDF_CheckError (&pdf->error);
 
227
 
 
228
    pdf->trailer = pdf->xref->trailer;
 
229
 
 
230
    pdf->font_mgr = HPDF_List_New (pdf->mmgr, HPDF_DEF_ITEMS_PER_BLOCK);
 
231
    if (!pdf->font_mgr)
 
232
        return HPDF_CheckError (&pdf->error);
 
233
 
 
234
    if (!pdf->fontdef_list) {
 
235
        pdf->fontdef_list = HPDF_List_New (pdf->mmgr,
 
236
                HPDF_DEF_ITEMS_PER_BLOCK);
 
237
        if (!pdf->fontdef_list)
 
238
            return HPDF_CheckError (&pdf->error);
 
239
    }
 
240
 
 
241
    if (!pdf->encoder_list) {
 
242
        pdf->encoder_list = HPDF_List_New (pdf->mmgr,
 
243
                HPDF_DEF_ITEMS_PER_BLOCK);
 
244
        if (!pdf->encoder_list)
 
245
            return HPDF_CheckError (&pdf->error);
 
246
    }
 
247
 
 
248
    pdf->catalog = HPDF_Catalog_New (pdf->mmgr, pdf->xref);
 
249
    if (!pdf->catalog)
 
250
        return HPDF_CheckError (&pdf->error);
 
251
 
 
252
    pdf->root_pages = HPDF_Catalog_GetRoot (pdf->catalog);
 
253
    if (!pdf->root_pages)
 
254
        return HPDF_CheckError (&pdf->error);
 
255
 
 
256
    pdf->page_list = HPDF_List_New (pdf->mmgr, HPDF_DEF_PAGE_LIST_NUM);
 
257
    if (!pdf->page_list)
 
258
        return HPDF_CheckError (&pdf->error);
 
259
 
 
260
    pdf->cur_pages = pdf->root_pages;
 
261
 
 
262
    ptr = (char *)HPDF_StrCpy (ptr, (const char *)"Haru Free PDF Library ", eptr);
 
263
    version = HPDF_GetVersion ();
 
264
    HPDF_StrCpy (ptr, version, eptr);
 
265
 
 
266
    if (HPDF_SetInfoAttr (pdf, HPDF_INFO_PRODUCER, buf) != HPDF_OK)
 
267
        return HPDF_CheckError (&pdf->error);
 
268
 
 
269
    return HPDF_OK;
 
270
}
 
271
 
 
272
 
 
273
HPDF_EXPORT(void)
 
274
HPDF_FreeDoc  (HPDF_Doc  pdf)
 
275
{
 
276
    HPDF_PTRACE ((" HPDF_FreeDoc\n"));
 
277
 
 
278
    if (HPDF_Doc_Validate (pdf)) {
 
279
        if (pdf->xref) {
 
280
           HPDF_Xref_Free (pdf->xref);
 
281
           pdf->xref = NULL;
 
282
        }
 
283
 
 
284
        if (pdf->font_mgr) {
 
285
            HPDF_List_Free (pdf->font_mgr);
 
286
            pdf->font_mgr = NULL;
 
287
        }
 
288
 
 
289
        if (pdf->fontdef_list)
 
290
            CleanupFontDefList (pdf);
 
291
 
 
292
        HPDF_MemSet(pdf->ttfont_tag, 0, 6);
 
293
 
 
294
        pdf->pdf_version = HPDF_VER_13;
 
295
        pdf->outlines = NULL;
 
296
        pdf->catalog = NULL;
 
297
        pdf->root_pages = NULL;
 
298
        pdf->cur_pages = NULL;
 
299
        pdf->cur_page = NULL;
 
300
        pdf->encrypt_on = HPDF_FALSE;
 
301
        pdf->cur_page_num = 0;
 
302
        pdf->cur_encoder = NULL;
 
303
        pdf->def_encoder = NULL;
 
304
        pdf->page_per_pages = 0;
 
305
 
 
306
        if (pdf->page_list) {
 
307
            HPDF_List_Free (pdf->page_list);
 
308
            pdf->page_list = NULL;
 
309
        }
 
310
 
 
311
        pdf->encrypt_dict = NULL;
 
312
        pdf->info = NULL;
 
313
 
 
314
        HPDF_Error_Reset (&pdf->error);
 
315
 
 
316
        if (pdf->stream) {
 
317
            HPDF_Stream_Free (pdf->stream);
 
318
            pdf->stream = NULL;
 
319
        }
 
320
    }
 
321
}
 
322
 
 
323
 
 
324
HPDF_EXPORT(void)
 
325
HPDF_FreeDocAll  (HPDF_Doc  pdf)
 
326
{
 
327
    HPDF_PTRACE ((" HPDF_FreeDocAll\n"));
 
328
 
 
329
    if (HPDF_Doc_Validate (pdf)) {
 
330
        HPDF_FreeDoc (pdf);
 
331
 
 
332
        if (pdf->fontdef_list)
 
333
            FreeFontDefList (pdf);
 
334
 
 
335
        if (pdf->encoder_list)
 
336
            FreeEncoderList (pdf);
 
337
 
 
338
        pdf->compression_mode = HPDF_COMP_NONE;
 
339
 
 
340
        HPDF_Error_Reset (&pdf->error);
 
341
    }
 
342
}
 
343
 
 
344
 
 
345
HPDF_EXPORT(HPDF_STATUS)
 
346
HPDF_SetPagesConfiguration  (HPDF_Doc    pdf,
 
347
                             HPDF_UINT   page_per_pages)
 
348
{
 
349
    HPDF_PTRACE ((" HPDF_SetPagesConfiguration\n"));
 
350
 
 
351
    if (!HPDF_HasDoc (pdf))
 
352
        return HPDF_INVALID_DOCUMENT;
 
353
 
 
354
    if (pdf->cur_page)
 
355
        return HPDF_RaiseError (&pdf->error, HPDF_INVALID_DOCUMENT_STATE, 0);
 
356
 
 
357
    if (page_per_pages > HPDF_LIMIT_MAX_ARRAY)
 
358
        return HPDF_RaiseError (&pdf->error, HPDF_INVALID_PARAMETER, 0);
 
359
 
 
360
    if (pdf->cur_pages == pdf->root_pages) {
 
361
        pdf->cur_pages = HPDF_Doc_AddPagesTo (pdf, pdf->root_pages);
 
362
        if (!pdf->cur_pages)
 
363
            return pdf->error.error_no;
 
364
        pdf->cur_page_num = 0;
 
365
    }
 
366
 
 
367
    pdf->page_per_pages = page_per_pages;
 
368
 
 
369
    return HPDF_OK;
 
370
}
 
371
 
 
372
 
 
373
static HPDF_STATUS
 
374
WriteHeader  (HPDF_Doc      pdf,
 
375
              HPDF_Stream   stream)
 
376
{
 
377
    HPDF_UINT idx = (HPDF_INT)pdf->pdf_version;
 
378
 
 
379
    HPDF_PTRACE ((" WriteHeader\n"));
 
380
 
 
381
    if (HPDF_Stream_WriteStr (stream, HPDF_VERSION_STR[idx]) != HPDF_OK)
 
382
        return pdf->error.error_no;
 
383
 
 
384
    return HPDF_OK;
 
385
}
 
386
 
 
387
 
 
388
static HPDF_STATUS
 
389
PrepareTrailer  (HPDF_Doc    pdf)
 
390
{
 
391
    HPDF_PTRACE ((" PrepareTrailer\n"));
 
392
 
 
393
    if (HPDF_Dict_Add (pdf->trailer, "Root", pdf->catalog) != HPDF_OK)
 
394
        return pdf->error.error_no;
 
395
 
 
396
    if (HPDF_Dict_Add (pdf->trailer, "Info", pdf->info) != HPDF_OK)
 
397
        return pdf->error.error_no;
 
398
 
 
399
    return HPDF_OK;
 
400
}
 
401
 
 
402
 
 
403
HPDF_STATUS
 
404
HPDF_Doc_SetEncryptOn  (HPDF_Doc   pdf)
 
405
{
 
406
    HPDF_PTRACE ((" HPDF_Doc_SetEncryptOn\n"));
 
407
 
 
408
    if (pdf->encrypt_on)
 
409
        return HPDF_OK;
 
410
 
 
411
    if (!pdf->encrypt_dict)
 
412
        return HPDF_SetError (&pdf->error, HPDF_DOC_ENCRYPTDICT_NOT_FOUND,
 
413
                0);
 
414
 
 
415
    if (pdf->encrypt_dict->header.obj_id == HPDF_OTYPE_NONE)
 
416
        if (HPDF_Xref_Add (pdf->xref, pdf->encrypt_dict) != HPDF_OK)
 
417
            return pdf->error.error_no;
 
418
 
 
419
    if (HPDF_Dict_Add (pdf->trailer, "Encrypt", pdf->encrypt_dict) != HPDF_OK)
 
420
        return pdf->error.error_no;
 
421
 
 
422
    pdf->encrypt_on = HPDF_TRUE;
 
423
 
 
424
    return HPDF_OK;
 
425
}
 
426
 
 
427
HPDF_EXPORT(HPDF_STATUS)
 
428
HPDF_SetPassword  (HPDF_Doc          pdf,
 
429
                   const char  *owner_passwd,
 
430
                   const char  *user_passwd)
 
431
{
 
432
    HPDF_PTRACE ((" HPDF_SetPassword\n"));
 
433
 
 
434
    if (!HPDF_HasDoc (pdf))
 
435
        return HPDF_DOC_INVALID_OBJECT;
 
436
 
 
437
    if (!pdf->encrypt_dict) {
 
438
        pdf->encrypt_dict = HPDF_EncryptDict_New (pdf->mmgr, pdf->xref);
 
439
 
 
440
        if (!pdf->encrypt_dict)
 
441
            return HPDF_CheckError (&pdf->error);
 
442
    }
 
443
 
 
444
    if (HPDF_EncryptDict_SetPassword (pdf->encrypt_dict, owner_passwd,
 
445
                user_passwd) != HPDF_OK)
 
446
        return HPDF_CheckError (&pdf->error);
 
447
 
 
448
    return HPDF_Doc_SetEncryptOn (pdf);
 
449
}
 
450
 
 
451
 
 
452
HPDF_EXPORT(HPDF_STATUS)
 
453
HPDF_SetPermission  (HPDF_Doc    pdf,
 
454
                     HPDF_UINT   permission)
 
455
{
 
456
    HPDF_Encrypt e;
 
457
 
 
458
    HPDF_PTRACE ((" HPDF_SetPermission\n"));
 
459
 
 
460
    if (!HPDF_HasDoc (pdf))
 
461
        return HPDF_DOC_INVALID_OBJECT;
 
462
 
 
463
    e = HPDF_EncryptDict_GetAttr (pdf->encrypt_dict);
 
464
 
 
465
    if (!e)
 
466
        return HPDF_RaiseError (&pdf->error,
 
467
                HPDF_DOC_ENCRYPTDICT_NOT_FOUND, 0);
 
468
    else
 
469
        e->permission = permission;
 
470
 
 
471
    return HPDF_OK;
 
472
}
 
473
 
 
474
 
 
475
HPDF_EXPORT(HPDF_STATUS)
 
476
HPDF_SetEncryptionMode  (HPDF_Doc           pdf,
 
477
                         HPDF_EncryptMode   mode,
 
478
                         HPDF_UINT          key_len)
 
479
{
 
480
    HPDF_Encrypt e;
 
481
 
 
482
    HPDF_PTRACE ((" HPDF_SetEncryptionMode\n"));
 
483
 
 
484
    if (!HPDF_Doc_Validate (pdf))
 
485
        return HPDF_DOC_INVALID_OBJECT;
 
486
 
 
487
    e = HPDF_EncryptDict_GetAttr (pdf->encrypt_dict);
 
488
 
 
489
    if (!e)
 
490
        return HPDF_RaiseError (&pdf->error,
 
491
                HPDF_DOC_ENCRYPTDICT_NOT_FOUND, 0);
 
492
    else {
 
493
        if (mode == HPDF_ENCRYPT_R2)
 
494
            e->key_len = 5;
 
495
        else {
 
496
            /* if encryption mode is specified revision-3, the version of
 
497
             * pdf file is set to 1.4
 
498
             */
 
499
            pdf->pdf_version = HPDF_VER_14;
 
500
 
 
501
            if (key_len >= 5 && key_len <= 16)
 
502
                e->key_len = key_len;
 
503
            else if (key_len == 0)
 
504
                e->key_len = 16;
 
505
            else
 
506
                return HPDF_RaiseError (&pdf->error,
 
507
                        HPDF_INVALID_ENCRYPT_KEY_LEN, 0);
 
508
        }
 
509
        e->mode = mode;
 
510
    }
 
511
 
 
512
    return HPDF_OK;
 
513
}
 
514
 
 
515
 
 
516
HPDF_STATUS
 
517
HPDF_Doc_SetEncryptOff  (HPDF_Doc   pdf)
 
518
{
 
519
    HPDF_PTRACE ((" HPDF_Doc_SetEncryptOff\n"));
 
520
 
 
521
    if (!pdf->encrypt_on)
 
522
        return HPDF_OK;
 
523
 
 
524
    /* if encrypy-dict object is registered to cross-reference-table,
 
525
     * replace it to null-object.
 
526
     * additionally remove encrypt-dict object from trailer-object.
 
527
     */
 
528
    if (pdf->encrypt_dict) {
 
529
        HPDF_UINT obj_id = pdf->encrypt_dict->header.obj_id;
 
530
 
 
531
        if (obj_id & HPDF_OTYPE_INDIRECT) {
 
532
            HPDF_XrefEntry entry;
 
533
            HPDF_Null null_obj;
 
534
 
 
535
            HPDF_Dict_RemoveElement (pdf->trailer, "Encrypt");
 
536
 
 
537
            entry = HPDF_Xref_GetEntryByObjectId (pdf->xref,
 
538
                        obj_id & 0x00FFFFFF);
 
539
 
 
540
            if (!entry) {
 
541
                return HPDF_SetError (&pdf->error,
 
542
                        HPDF_DOC_ENCRYPTDICT_NOT_FOUND, 0);
 
543
            }
 
544
 
 
545
            null_obj = HPDF_Null_New (pdf->mmgr);
 
546
            if (!null_obj)
 
547
                return pdf->error.error_no;
 
548
 
 
549
            entry->obj = null_obj;
 
550
            null_obj->header.obj_id = obj_id | HPDF_OTYPE_INDIRECT;
 
551
 
 
552
            pdf->encrypt_dict->header.obj_id = HPDF_OTYPE_NONE;
 
553
        }
 
554
    }
 
555
 
 
556
    pdf->encrypt_on = HPDF_FALSE;
 
557
    return HPDF_OK;
 
558
}
 
559
 
 
560
 
 
561
HPDF_STATUS
 
562
HPDF_Doc_PrepareEncryption  (HPDF_Doc   pdf)
 
563
{
 
564
    HPDF_Encrypt e= HPDF_EncryptDict_GetAttr (pdf->encrypt_dict);
 
565
    HPDF_Dict info = GetInfo (pdf);
 
566
    HPDF_Array id;
 
567
 
 
568
    if (!e)
 
569
        return HPDF_DOC_ENCRYPTDICT_NOT_FOUND;
 
570
 
 
571
    if (!info)
 
572
        return pdf->error.error_no;
 
573
 
 
574
    if (HPDF_EncryptDict_Prepare (pdf->encrypt_dict, info, pdf->xref) !=
 
575
            HPDF_OK)
 
576
        return pdf->error.error_no;
 
577
 
 
578
    /* reset 'ID' to trailer-dictionary */
 
579
    id = HPDF_Dict_GetItem (pdf->trailer, "ID", HPDF_OCLASS_ARRAY);
 
580
    if (!id) {
 
581
        id = HPDF_Array_New (pdf->mmgr);
 
582
 
 
583
        if (!id || HPDF_Dict_Add (pdf->trailer, "ID", id) != HPDF_OK)
 
584
            return pdf->error.error_no;
 
585
    } else
 
586
        HPDF_Array_Clear (id);
 
587
 
 
588
    if (HPDF_Array_Add (id, HPDF_Binary_New (pdf->mmgr, e->encrypt_id,
 
589
                HPDF_ID_LEN)) != HPDF_OK)
 
590
        return pdf->error.error_no;
 
591
 
 
592
    if (HPDF_Array_Add (id, HPDF_Binary_New (pdf->mmgr, e->encrypt_id,
 
593
                HPDF_ID_LEN)) != HPDF_OK)
 
594
        return pdf->error.error_no;
 
595
 
 
596
    return HPDF_OK;
 
597
}
 
598
 
 
599
 
 
600
static HPDF_STATUS
 
601
InternalSaveToStream  (HPDF_Doc      pdf,
 
602
                       HPDF_Stream   stream)
 
603
{
 
604
    HPDF_STATUS ret;
 
605
 
 
606
    if ((ret = WriteHeader (pdf, stream)) != HPDF_OK)
 
607
        return ret;
 
608
 
 
609
    /* prepare trailer */
 
610
    if ((ret = PrepareTrailer (pdf)) != HPDF_OK)
 
611
        return ret;
 
612
 
 
613
    /* prepare encription */
 
614
    if (pdf->encrypt_on) {
 
615
        HPDF_Encrypt e= HPDF_EncryptDict_GetAttr (pdf->encrypt_dict);
 
616
 
 
617
        if ((ret = HPDF_Doc_PrepareEncryption (pdf)) != HPDF_OK)
 
618
            return ret;
 
619
 
 
620
        if ((ret = HPDF_Xref_WriteToStream (pdf->xref, stream, e)) != HPDF_OK)
 
621
            return ret;
 
622
    } else {
 
623
        if ((ret = HPDF_Xref_WriteToStream (pdf->xref, stream, NULL)) !=
 
624
                HPDF_OK)
 
625
            return ret;
 
626
    }
 
627
 
 
628
    return HPDF_OK;
 
629
}
 
630
 
 
631
 
 
632
HPDF_EXPORT(HPDF_STATUS)
 
633
HPDF_SaveToStream  (HPDF_Doc   pdf)
 
634
{
 
635
    HPDF_PTRACE ((" HPDF_SaveToStream\n"));
 
636
 
 
637
    if (!HPDF_HasDoc (pdf))
 
638
        return HPDF_INVALID_DOCUMENT;
 
639
 
 
640
    if (!pdf->stream)
 
641
        pdf->stream = HPDF_MemStream_New (pdf->mmgr, HPDF_STREAM_BUF_SIZ);
 
642
 
 
643
    if (!HPDF_Stream_Validate (pdf->stream))
 
644
        return HPDF_RaiseError (&pdf->error, HPDF_INVALID_STREAM, 0);
 
645
 
 
646
    HPDF_MemStream_FreeData (pdf->stream);
 
647
 
 
648
    if (InternalSaveToStream (pdf, pdf->stream) != HPDF_OK)
 
649
        return HPDF_CheckError (&pdf->error);
 
650
 
 
651
    return HPDF_OK;
 
652
}
 
653
 
 
654
 
 
655
HPDF_EXPORT(HPDF_UINT32)
 
656
HPDF_GetStreamSize  (HPDF_Doc   pdf)
 
657
{
 
658
    HPDF_PTRACE ((" HPDF_GetStreamSize\n"));
 
659
 
 
660
    if (!HPDF_HasDoc (pdf))
 
661
        return HPDF_INVALID_DOCUMENT;
 
662
 
 
663
    if (!HPDF_Stream_Validate (pdf->stream))
 
664
        return 0;
 
665
 
 
666
    return HPDF_Stream_Size(pdf->stream);
 
667
}
 
668
 
 
669
 
 
670
HPDF_EXPORT(HPDF_STATUS)
 
671
HPDF_ReadFromStream  (HPDF_Doc       pdf,
 
672
                      HPDF_BYTE     *buf,
 
673
                      HPDF_UINT32   *size)
 
674
{
 
675
    HPDF_UINT isize = *size;
 
676
    HPDF_STATUS ret;
 
677
 
 
678
    if (!HPDF_HasDoc (pdf))
 
679
        return HPDF_INVALID_DOCUMENT;
 
680
 
 
681
    if (!HPDF_Stream_Validate (pdf->stream))
 
682
        return HPDF_RaiseError (&pdf->error, HPDF_INVALID_OPERATION, 0);
 
683
 
 
684
    if (*size == 0)
 
685
        return HPDF_RaiseError (&pdf->error, HPDF_INVALID_PARAMETER, 0);
 
686
 
 
687
    ret = HPDF_Stream_Read (pdf->stream, buf, &isize);
 
688
 
 
689
    *size = isize;
 
690
 
 
691
    if (ret != HPDF_OK)
 
692
        HPDF_CheckError (&pdf->error);
 
693
 
 
694
    return ret;
 
695
}
 
696
 
 
697
 
 
698
HPDF_EXPORT(HPDF_STATUS)
 
699
HPDF_ResetStream  (HPDF_Doc     pdf)
 
700
{
 
701
    if (!HPDF_HasDoc (pdf))
 
702
        return HPDF_INVALID_DOCUMENT;
 
703
 
 
704
    if (!HPDF_Stream_Validate (pdf->stream))
 
705
        return HPDF_RaiseError (&pdf->error, HPDF_INVALID_OPERATION, 0);
 
706
 
 
707
    return HPDF_Stream_Seek (pdf->stream, 0, HPDF_SEEK_SET);
 
708
}
 
709
 
 
710
 
 
711
HPDF_EXPORT(HPDF_STATUS)
 
712
HPDF_SaveToFile  (HPDF_Doc     pdf,
 
713
                  const char  *file_name)
 
714
{
 
715
    HPDF_Stream stream;
 
716
 
 
717
    HPDF_PTRACE ((" HPDF_SaveToFile\n"));
 
718
 
 
719
    if (!HPDF_HasDoc (pdf))
 
720
        return HPDF_INVALID_DOCUMENT;
 
721
 
 
722
    stream = HPDF_FileWriter_New (pdf->mmgr, file_name);
 
723
    if (!stream)
 
724
        return HPDF_CheckError (&pdf->error);
 
725
 
 
726
    InternalSaveToStream (pdf, stream);
 
727
 
 
728
    HPDF_Stream_Free (stream);
 
729
 
 
730
    return HPDF_CheckError (&pdf->error);
 
731
}
 
732
 
 
733
 
 
734
HPDF_EXPORT(HPDF_Page)
 
735
HPDF_GetCurrentPage  (HPDF_Doc   pdf)
 
736
{
 
737
    HPDF_PTRACE ((" HPDF_GetCurrentPage\n"));
 
738
 
 
739
    if (!HPDF_HasDoc (pdf))
 
740
        return NULL;
 
741
 
 
742
    return pdf->cur_page;
 
743
}
 
744
 
 
745
 
 
746
HPDF_EXPORT(HPDF_Page)
 
747
HPDF_GetPageByIndex  (HPDF_Doc    pdf,
 
748
                      HPDF_UINT   index)
 
749
{
 
750
    HPDF_Page ret;
 
751
 
 
752
    HPDF_PTRACE ((" HPDF_GetPageByIndex\n"));
 
753
 
 
754
    if (!HPDF_HasDoc (pdf))
 
755
        return NULL;
 
756
 
 
757
    ret = HPDF_List_ItemAt (pdf->page_list, index);
 
758
    if (!ret) {
 
759
        HPDF_RaiseError (&pdf->error, HPDF_INVALID_PAGE_INDEX, 0);
 
760
        return NULL;
 
761
    }
 
762
 
 
763
    return ret;
 
764
}
 
765
 
 
766
 
 
767
HPDF_Pages
 
768
HPDF_Doc_GetCurrentPages  (HPDF_Doc   pdf)
 
769
{
 
770
    HPDF_PTRACE ((" HPDF_GetCurrentPages\n"));
 
771
 
 
772
    if (!HPDF_HasDoc (pdf))
 
773
        return NULL;
 
774
 
 
775
    return pdf->cur_pages;
 
776
}
 
777
 
 
778
 
 
779
HPDF_STATUS
 
780
HPDF_Doc_SetCurrentPages  (HPDF_Doc     pdf,
 
781
                           HPDF_Pages   pages)
 
782
{
 
783
    HPDF_PTRACE ((" HPDF_Doc_SetCurrentPages\n"));
 
784
 
 
785
    if (!HPDF_HasDoc (pdf))
 
786
        return HPDF_INVALID_DOCUMENT;
 
787
 
 
788
    if (!HPDF_Pages_Validate (pages))
 
789
        return HPDF_SetError (&pdf->error, HPDF_INVALID_PAGES, 0);
 
790
 
 
791
    /* check whether the pages belong to the pdf */
 
792
    if (pdf->mmgr != pages->mmgr)
 
793
        return HPDF_SetError (&pdf->error, HPDF_INVALID_PAGES, 0);
 
794
 
 
795
    pdf->cur_pages = pages;
 
796
 
 
797
    return HPDF_OK;
 
798
}
 
799
 
 
800
 
 
801
HPDF_STATUS
 
802
HPDF_Doc_SetCurrentPage  (HPDF_Doc    pdf,
 
803
                          HPDF_Page   page)
 
804
{
 
805
    HPDF_PTRACE ((" HPDF_Doc_SetCurrentPage\n"));
 
806
 
 
807
    if (!HPDF_HasDoc (pdf))
 
808
        return HPDF_INVALID_DOCUMENT;
 
809
 
 
810
    if (!HPDF_Page_Validate (page))
 
811
        return HPDF_SetError (&pdf->error, HPDF_INVALID_PAGE, 0);
 
812
 
 
813
    /* check whether the page belong to the pdf */
 
814
    if (pdf->mmgr != page->mmgr)
 
815
        return HPDF_SetError (&pdf->error, HPDF_INVALID_PAGE, 0);
 
816
 
 
817
    pdf->cur_page = page;
 
818
 
 
819
    return HPDF_OK;
 
820
}
 
821
 
 
822
 
 
823
HPDF_EXPORT(HPDF_Page)
 
824
HPDF_AddPage  (HPDF_Doc  pdf)
 
825
{
 
826
    HPDF_Page page;
 
827
    HPDF_STATUS ret;
 
828
 
 
829
    HPDF_PTRACE ((" HPDF_AddPage\n"));
 
830
 
 
831
    if (!HPDF_HasDoc (pdf))
 
832
        return NULL;
 
833
 
 
834
    if (pdf->page_per_pages) {
 
835
        if (pdf->page_per_pages <= pdf->cur_page_num) {
 
836
            pdf->cur_pages = HPDF_Doc_AddPagesTo (pdf, pdf->root_pages);
 
837
            if (!pdf->cur_pages)
 
838
                return NULL;
 
839
            pdf->cur_page_num = 0;
 
840
        }
 
841
    }
 
842
 
 
843
    page = HPDF_Page_New (pdf->mmgr, pdf->xref);
 
844
    if (!page) {
 
845
        HPDF_CheckError (&pdf->error);
 
846
        return NULL;
 
847
    }
 
848
 
 
849
    if ((ret = HPDF_Pages_AddKids (pdf->cur_pages, page)) != HPDF_OK) {
 
850
        HPDF_RaiseError (&pdf->error, ret, 0);
 
851
        return NULL;
 
852
    }
 
853
 
 
854
    if ((ret = HPDF_List_Add (pdf->page_list, page)) != HPDF_OK) {
 
855
        HPDF_RaiseError (&pdf->error, ret, 0);
 
856
        return NULL;
 
857
    }
 
858
 
 
859
    pdf->cur_page = page;
 
860
 
 
861
    if (pdf->compression_mode & HPDF_COMP_TEXT)
 
862
        HPDF_Page_SetFilter (page, HPDF_STREAM_FILTER_FLATE_DECODE);
 
863
 
 
864
    pdf->cur_page_num++;
 
865
 
 
866
    return page;
 
867
}
 
868
 
 
869
 
 
870
HPDF_Pages
 
871
HPDF_Doc_AddPagesTo  (HPDF_Doc     pdf,
 
872
                      HPDF_Pages   parent)
 
873
{
 
874
    HPDF_Pages pages;
 
875
 
 
876
    HPDF_PTRACE ((" HPDF_AddPagesTo\n"));
 
877
 
 
878
    if (!HPDF_HasDoc (pdf))
 
879
        return NULL;
 
880
 
 
881
    if (!HPDF_Pages_Validate (parent)) {
 
882
        HPDF_RaiseError (&pdf->error, HPDF_INVALID_PAGES, 0);
 
883
        return NULL;
 
884
    }
 
885
 
 
886
    /* check whether the page belong to the pdf */
 
887
    if (pdf->mmgr != parent->mmgr) {
 
888
        HPDF_RaiseError (&pdf->error, HPDF_INVALID_PAGES, 0);
 
889
        return NULL;
 
890
    }
 
891
 
 
892
    pages = HPDF_Pages_New (pdf->mmgr, parent, pdf->xref);
 
893
    if (pages)
 
894
        pdf->cur_pages = pages;
 
895
    else
 
896
        HPDF_CheckError (&pdf->error);
 
897
 
 
898
 
 
899
    return  pages;
 
900
}
 
901
 
 
902
 
 
903
HPDF_EXPORT(HPDF_Page)
 
904
HPDF_InsertPage  (HPDF_Doc    pdf,
 
905
                  HPDF_Page   target)
 
906
{
 
907
    HPDF_Page page;
 
908
    HPDF_STATUS ret;
 
909
 
 
910
    HPDF_PTRACE ((" HPDF_InsertPage\n"));
 
911
 
 
912
    if (!HPDF_HasDoc (pdf))
 
913
        return NULL;
 
914
 
 
915
    if (!HPDF_Page_Validate (target)) {
 
916
        HPDF_RaiseError (&pdf->error, HPDF_INVALID_PAGE, 0);
 
917
        return NULL;
 
918
    }
 
919
 
 
920
    /* check whether the page belong to the pdf */
 
921
    if (pdf->mmgr != target->mmgr) {
 
922
        HPDF_RaiseError (&pdf->error, HPDF_INVALID_PAGE, 0);
 
923
        return NULL;
 
924
    }
 
925
 
 
926
    page = HPDF_Page_New (pdf->mmgr, pdf->xref);
 
927
    if (!page) {
 
928
        HPDF_CheckError (&pdf->error);
 
929
        return NULL;
 
930
    }
 
931
 
 
932
    if ((ret = HPDF_Page_InsertBefore (page, target)) != HPDF_OK) {
 
933
        HPDF_RaiseError (&pdf->error, ret, 0);
 
934
        return NULL;
 
935
    }
 
936
 
 
937
    if ((ret = HPDF_List_Insert (pdf->page_list, target, page)) != HPDF_OK) {
 
938
        HPDF_RaiseError (&pdf->error, ret, 0);
 
939
        return NULL;
 
940
    }
 
941
 
 
942
    if (pdf->compression_mode & HPDF_COMP_TEXT)
 
943
        HPDF_Page_SetFilter (page, HPDF_STREAM_FILTER_FLATE_DECODE);
 
944
 
 
945
    return page;
 
946
}
 
947
 
 
948
 
 
949
HPDF_EXPORT(HPDF_STATUS)
 
950
HPDF_SetErrorHandler  (HPDF_Doc             pdf,
 
951
                       HPDF_Error_Handler   user_error_fn)
 
952
{
 
953
    if (!HPDF_Doc_Validate (pdf))
 
954
        return HPDF_INVALID_DOCUMENT;
 
955
 
 
956
    pdf->error.error_fn = user_error_fn;
 
957
 
 
958
    return HPDF_OK;
 
959
}
 
960
 
 
961
 
 
962
/*----- font handling -------------------------------------------------------*/
 
963
 
 
964
 
 
965
static void
 
966
FreeFontDefList  (HPDF_Doc   pdf)
 
967
{
 
968
    HPDF_List list = pdf->fontdef_list;
 
969
    HPDF_UINT i;
 
970
 
 
971
    HPDF_PTRACE ((" HPDF_Doc_FreeFontDefList\n"));
 
972
 
 
973
    for (i = 0; i < list->count; i++) {
 
974
        HPDF_FontDef def = (HPDF_FontDef)HPDF_List_ItemAt (list, i);
 
975
 
 
976
        HPDF_FontDef_Free (def);
 
977
    }
 
978
 
 
979
    HPDF_List_Free (list);
 
980
 
 
981
    pdf->fontdef_list = NULL;
 
982
}
 
983
 
 
984
static void
 
985
CleanupFontDefList  (HPDF_Doc  pdf)
 
986
{
 
987
    HPDF_List list = pdf->fontdef_list;
 
988
    HPDF_UINT i;
 
989
 
 
990
    HPDF_PTRACE ((" CleanupFontDefList\n"));
 
991
 
 
992
    for (i = 0; i < list->count; i++) {
 
993
        HPDF_FontDef def = (HPDF_FontDef)HPDF_List_ItemAt (list, i);
 
994
 
 
995
        HPDF_FontDef_Cleanup (def);
 
996
    }
 
997
}
 
998
 
 
999
 
 
1000
HPDF_FontDef
 
1001
HPDF_Doc_FindFontDef  (HPDF_Doc          pdf,
 
1002
                       const char  *font_name)
 
1003
{
 
1004
    HPDF_List list = pdf->fontdef_list;
 
1005
    HPDF_UINT i;
 
1006
 
 
1007
    HPDF_PTRACE ((" HPDF_Doc_FindFontDef\n"));
 
1008
 
 
1009
    for (i = 0; i < list->count; i++) {
 
1010
        HPDF_FontDef def = (HPDF_FontDef)HPDF_List_ItemAt (list, i);
 
1011
 
 
1012
        if (HPDF_StrCmp (font_name, def->base_font) == 0) {
 
1013
            if (def->type == HPDF_FONTDEF_TYPE_UNINITIALIZED) {
 
1014
                if (!def->init_fn ||
 
1015
                    def->init_fn (def) != HPDF_OK)
 
1016
                    return NULL;
 
1017
            }
 
1018
 
 
1019
            return def;
 
1020
        }
 
1021
    }
 
1022
 
 
1023
    return NULL;
 
1024
}
 
1025
 
 
1026
 
 
1027
HPDF_STATUS
 
1028
HPDF_Doc_RegisterFontDef  (HPDF_Doc       pdf,
 
1029
                           HPDF_FontDef   fontdef)
 
1030
{
 
1031
    HPDF_STATUS ret;
 
1032
 
 
1033
    HPDF_PTRACE ((" HPDF_Doc_RegisterFontDef\n"));
 
1034
 
 
1035
    if (!fontdef)
 
1036
        return HPDF_SetError (&pdf->error, HPDF_INVALID_OBJECT, 0);
 
1037
 
 
1038
    if (HPDF_Doc_FindFontDef (pdf, fontdef->base_font) != NULL) {
 
1039
        HPDF_FontDef_Free (fontdef);
 
1040
        return HPDF_SetError (&pdf->error, HPDF_DUPLICATE_REGISTRATION, 0);
 
1041
    }
 
1042
 
 
1043
    if ((ret = HPDF_List_Add (pdf->fontdef_list, fontdef)) != HPDF_OK) {
 
1044
        HPDF_FontDef_Free (fontdef);
 
1045
        return HPDF_SetError (&pdf->error, ret, 0);
 
1046
    }
 
1047
 
 
1048
    return HPDF_OK;
 
1049
}
 
1050
 
 
1051
 
 
1052
 
 
1053
HPDF_FontDef
 
1054
HPDF_GetFontDef  (HPDF_Doc          pdf,
 
1055
                  const char  *font_name)
 
1056
{
 
1057
    HPDF_STATUS ret;
 
1058
    HPDF_FontDef def;
 
1059
 
 
1060
    HPDF_PTRACE ((" HPDF_GetFontDef\n"));
 
1061
 
 
1062
    if (!HPDF_HasDoc (pdf))
 
1063
        return NULL;
 
1064
 
 
1065
    def = HPDF_Doc_FindFontDef (pdf, font_name);
 
1066
 
 
1067
    if (!def) {
 
1068
        def = HPDF_Base14FontDef_New (pdf->mmgr, font_name);
 
1069
 
 
1070
        if (!def)
 
1071
            return NULL;
 
1072
 
 
1073
        if ((ret = HPDF_List_Add (pdf->fontdef_list, def)) != HPDF_OK) {
 
1074
            HPDF_FontDef_Free (def);
 
1075
            HPDF_RaiseError (&pdf->error, ret, 0);
 
1076
            def = NULL;
 
1077
        }
 
1078
    }
 
1079
 
 
1080
    return def;
 
1081
}
 
1082
 
 
1083
 
 
1084
/*----- encoder handling ----------------------------------------------------*/
 
1085
 
 
1086
 
 
1087
HPDF_Encoder
 
1088
HPDF_Doc_FindEncoder  (HPDF_Doc         pdf,
 
1089
                       const char  *encoding_name)
 
1090
{
 
1091
    HPDF_List list = pdf->encoder_list;
 
1092
    HPDF_UINT i;
 
1093
 
 
1094
    HPDF_PTRACE ((" HPDF_Doc_FindEncoder\n"));
 
1095
 
 
1096
    for (i = 0; i < list->count; i++) {
 
1097
        HPDF_Encoder encoder = (HPDF_Encoder)HPDF_List_ItemAt (list, i);
 
1098
 
 
1099
        if (HPDF_StrCmp (encoding_name, encoder->name) == 0) {
 
1100
 
 
1101
            /* if encoder is uninitialize, call init_fn() */
 
1102
            if (encoder->type == HPDF_ENCODER_TYPE_UNINITIALIZED) {
 
1103
                if (!encoder->init_fn ||
 
1104
                    encoder->init_fn (encoder) != HPDF_OK)
 
1105
                    return NULL;
 
1106
            }
 
1107
 
 
1108
            return encoder;
 
1109
        }
 
1110
    }
 
1111
 
 
1112
    return NULL;
 
1113
}
 
1114
 
 
1115
 
 
1116
 
 
1117
HPDF_STATUS
 
1118
HPDF_Doc_RegisterEncoder  (HPDF_Doc       pdf,
 
1119
                           HPDF_Encoder   encoder)
 
1120
{
 
1121
    HPDF_STATUS ret;
 
1122
 
 
1123
    if (!encoder)
 
1124
        return HPDF_SetError (&pdf->error, HPDF_INVALID_OBJECT, 0);
 
1125
 
 
1126
    if (HPDF_Doc_FindEncoder (pdf, encoder->name) != NULL) {
 
1127
        HPDF_Encoder_Free (encoder);
 
1128
        return HPDF_SetError (&pdf->error, HPDF_DUPLICATE_REGISTRATION, 0);
 
1129
    }
 
1130
 
 
1131
    if ((ret = HPDF_List_Add (pdf->encoder_list, encoder)) != HPDF_OK) {
 
1132
        HPDF_Encoder_Free (encoder);
 
1133
        return HPDF_SetError (&pdf->error, ret, 0);
 
1134
    }
 
1135
 
 
1136
    return HPDF_OK;
 
1137
}
 
1138
 
 
1139
 
 
1140
HPDF_EXPORT(HPDF_Encoder)
 
1141
HPDF_GetEncoder  (HPDF_Doc         pdf,
 
1142
                  const char  *encoding_name)
 
1143
{
 
1144
    HPDF_Encoder encoder;
 
1145
    HPDF_STATUS ret;
 
1146
 
 
1147
    HPDF_PTRACE ((" HPDF_GetEncoder\n"));
 
1148
 
 
1149
    if (!HPDF_HasDoc (pdf))
 
1150
        return NULL;
 
1151
 
 
1152
    encoder = HPDF_Doc_FindEncoder (pdf, encoding_name);
 
1153
 
 
1154
    if (!encoder) {
 
1155
        encoder = HPDF_BasicEncoder_New (pdf->mmgr, encoding_name);
 
1156
 
 
1157
        if (!encoder) {
 
1158
            HPDF_CheckError (&pdf->error);
 
1159
            return NULL;
 
1160
        }
 
1161
 
 
1162
        if ((ret = HPDF_List_Add (pdf->encoder_list, encoder)) != HPDF_OK) {
 
1163
            HPDF_Encoder_Free (encoder);
 
1164
            HPDF_RaiseError (&pdf->error, ret, 0);
 
1165
            return NULL;
 
1166
        }
 
1167
    }
 
1168
 
 
1169
    return encoder;
 
1170
}
 
1171
 
 
1172
 
 
1173
HPDF_EXPORT(HPDF_Encoder)
 
1174
HPDF_GetCurrentEncoder  (HPDF_Doc    pdf)
 
1175
{
 
1176
    if (!HPDF_HasDoc (pdf))
 
1177
        return NULL;
 
1178
 
 
1179
    return pdf->cur_encoder;
 
1180
}
 
1181
 
 
1182
 
 
1183
HPDF_EXPORT(HPDF_STATUS)
 
1184
HPDF_SetCurrentEncoder  (HPDF_Doc    pdf,
 
1185
                         const char  *encoding_name)
 
1186
{
 
1187
    HPDF_Encoder encoder;
 
1188
 
 
1189
    if (!HPDF_HasDoc (pdf))
 
1190
        return HPDF_GetError (pdf);
 
1191
 
 
1192
    encoder = HPDF_GetEncoder (pdf, encoding_name);
 
1193
    pdf->cur_encoder = encoder;
 
1194
 
 
1195
    if (!pdf)
 
1196
        return HPDF_GetError (pdf);
 
1197
 
 
1198
    return HPDF_OK;
 
1199
}
 
1200
 
 
1201
 
 
1202
static void
 
1203
FreeEncoderList  (HPDF_Doc  pdf)
 
1204
{
 
1205
    HPDF_List list = pdf->encoder_list;
 
1206
    HPDF_UINT i;
 
1207
 
 
1208
    HPDF_PTRACE ((" FreeEncoderList\n"));
 
1209
 
 
1210
    for (i = 0; i < list->count; i++) {
 
1211
        HPDF_Encoder encoder = (HPDF_Encoder)HPDF_List_ItemAt (list, i);
 
1212
 
 
1213
        HPDF_Encoder_Free (encoder);
 
1214
    }
 
1215
 
 
1216
    HPDF_List_Free (list);
 
1217
 
 
1218
    pdf->encoder_list = NULL;
 
1219
}
 
1220
 
 
1221
 
 
1222
/*----- font handling -------------------------------------------------------*/
 
1223
 
 
1224
 
 
1225
HPDF_Font
 
1226
HPDF_Doc_FindFont  (HPDF_Doc          pdf,
 
1227
                    const char  *font_name,
 
1228
                    const char  *encoding_name)
 
1229
{
 
1230
    HPDF_UINT i;
 
1231
    HPDF_Font font;
 
1232
 
 
1233
    HPDF_PTRACE ((" HPDF_Doc_FindFont\n"));
 
1234
 
 
1235
    for (i = 0; i < pdf->font_mgr->count; i++) {
 
1236
        HPDF_FontAttr attr;
 
1237
 
 
1238
        font = (HPDF_Font)HPDF_List_ItemAt (pdf->font_mgr, i);
 
1239
        attr = (HPDF_FontAttr) font->attr;
 
1240
 
 
1241
        if (HPDF_StrCmp (attr->fontdef->base_font, font_name) == 0 &&
 
1242
                HPDF_StrCmp (attr->encoder->name, encoding_name) == 0)
 
1243
            return font;
 
1244
    }
 
1245
 
 
1246
    return NULL;
 
1247
}
 
1248
 
 
1249
 
 
1250
HPDF_EXPORT(HPDF_Font)
 
1251
HPDF_GetFont  (HPDF_Doc          pdf,
 
1252
               const char  *font_name,
 
1253
               const char  *encoding_name)
 
1254
{
 
1255
    HPDF_FontDef fontdef = NULL;
 
1256
    HPDF_Encoder encoder = NULL;
 
1257
    HPDF_Font font;
 
1258
 
 
1259
    HPDF_PTRACE ((" HPDF_GetFont\n"));
 
1260
 
 
1261
    if (!HPDF_HasDoc (pdf))
 
1262
        return NULL;
 
1263
 
 
1264
    if (!font_name) {
 
1265
        HPDF_RaiseError (&pdf->error, HPDF_INVALID_FONT_NAME, 0);
 
1266
        return NULL;
 
1267
    }
 
1268
 
 
1269
    /* if encoding-name is not specified, find default-encoding of fontdef
 
1270
     */
 
1271
    if (!encoding_name) {
 
1272
        fontdef = HPDF_GetFontDef (pdf, font_name);
 
1273
 
 
1274
        if (fontdef) {
 
1275
            HPDF_Type1FontDefAttr attr = (HPDF_Type1FontDefAttr)fontdef->attr;
 
1276
 
 
1277
            if (fontdef->type == HPDF_FONTDEF_TYPE_TYPE1 &&
 
1278
                HPDF_StrCmp (attr->encoding_scheme,
 
1279
                            HPDF_ENCODING_FONT_SPECIFIC) == 0)
 
1280
                encoder = HPDF_GetEncoder (pdf, HPDF_ENCODING_FONT_SPECIFIC);
 
1281
            else
 
1282
                encoder = HPDF_GetEncoder (pdf, HPDF_ENCODING_STANDARD);
 
1283
        } else {
 
1284
            HPDF_CheckError (&pdf->error);
 
1285
            return NULL;
 
1286
        }
 
1287
 
 
1288
        font = HPDF_Doc_FindFont (pdf, font_name, encoder->name);
 
1289
    } else {
 
1290
        font = HPDF_Doc_FindFont (pdf, font_name, encoding_name);
 
1291
    }
 
1292
 
 
1293
    if (font)
 
1294
        return font;
 
1295
 
 
1296
    if (!fontdef) {
 
1297
        fontdef = HPDF_GetFontDef (pdf, font_name);
 
1298
 
 
1299
        if (!fontdef) {
 
1300
            HPDF_CheckError (&pdf->error);
 
1301
            return NULL;
 
1302
        }
 
1303
    }
 
1304
 
 
1305
    if (!encoder) {
 
1306
        encoder = HPDF_GetEncoder (pdf, encoding_name);
 
1307
 
 
1308
        if (!encoder)
 
1309
            return NULL;
 
1310
    }
 
1311
 
 
1312
    switch (fontdef->type) {
 
1313
        case HPDF_FONTDEF_TYPE_TYPE1:
 
1314
            font = HPDF_Type1Font_New (pdf->mmgr, fontdef, encoder, pdf->xref);
 
1315
 
 
1316
            if (font)
 
1317
                HPDF_List_Add (pdf->font_mgr, font);
 
1318
 
 
1319
            break;
 
1320
        case HPDF_FONTDEF_TYPE_TRUETYPE:
 
1321
            if (encoder->type == HPDF_ENCODER_TYPE_DOUBLE_BYTE)
 
1322
                font = HPDF_Type0Font_New (pdf->mmgr, fontdef, encoder,
 
1323
                        pdf->xref);
 
1324
            else
 
1325
                font = HPDF_TTFont_New (pdf->mmgr, fontdef, encoder, pdf->xref);
 
1326
 
 
1327
            if (font)
 
1328
                HPDF_List_Add (pdf->font_mgr, font);
 
1329
 
 
1330
            break;
 
1331
        case HPDF_FONTDEF_TYPE_CID:
 
1332
            font = HPDF_Type0Font_New (pdf->mmgr, fontdef, encoder, pdf->xref);
 
1333
 
 
1334
            if (font)
 
1335
                HPDF_List_Add (pdf->font_mgr, font);
 
1336
 
 
1337
            break;
 
1338
        default:
 
1339
            HPDF_RaiseError (&pdf->error, HPDF_UNSUPPORTED_FONT_TYPE, 0);
 
1340
            return NULL;
 
1341
    }
 
1342
 
 
1343
    if (!font)
 
1344
        HPDF_CheckError (&pdf->error);
 
1345
 
 
1346
    if (font && (pdf->compression_mode & HPDF_COMP_METADATA))
 
1347
        font->filter = HPDF_STREAM_FILTER_FLATE_DECODE;
 
1348
 
 
1349
    return font;
 
1350
}
 
1351
 
 
1352
 
 
1353
HPDF_EXPORT(const char*)
 
1354
HPDF_LoadType1FontFromFile  (HPDF_Doc     pdf,
 
1355
                             const char  *afm_file_name,
 
1356
                             const char  *data_file_name)
 
1357
{
 
1358
    HPDF_Stream afm;
 
1359
    HPDF_Stream pfm = NULL;
 
1360
    const char *ret;
 
1361
 
 
1362
    HPDF_PTRACE ((" HPDF_LoadType1FontFromFile\n"));
 
1363
 
 
1364
    if (!HPDF_HasDoc (pdf))
 
1365
        return NULL;
 
1366
 
 
1367
    /* create file stream */
 
1368
    afm = HPDF_FileReader_New (pdf->mmgr, afm_file_name);
 
1369
 
 
1370
    if (data_file_name)
 
1371
        pfm = HPDF_FileReader_New (pdf->mmgr, data_file_name);
 
1372
 
 
1373
    if (HPDF_Stream_Validate (afm) &&
 
1374
            (!data_file_name || HPDF_Stream_Validate (pfm))) {
 
1375
 
 
1376
        ret = LoadType1FontFromStream (pdf, afm, pfm);
 
1377
    } else
 
1378
        ret = NULL;
 
1379
 
 
1380
    /* destroy file stream */
 
1381
    if (afm)
 
1382
        HPDF_Stream_Free (afm);
 
1383
 
 
1384
    if (pfm)
 
1385
        HPDF_Stream_Free (pfm);
 
1386
 
 
1387
    if (!ret)
 
1388
        HPDF_CheckError (&pdf->error);
 
1389
 
 
1390
    return ret;
 
1391
}
 
1392
 
 
1393
 
 
1394
static const char*
 
1395
LoadType1FontFromStream  (HPDF_Doc      pdf,
 
1396
                          HPDF_Stream   afmdata,
 
1397
                          HPDF_Stream   pfmdata)
 
1398
{
 
1399
    HPDF_FontDef def;
 
1400
 
 
1401
    HPDF_PTRACE ((" HPDF_LoadType1FontFromStream\n"));
 
1402
 
 
1403
    if (!HPDF_HasDoc (pdf))
 
1404
        return NULL;
 
1405
 
 
1406
    def = HPDF_Type1FontDef_Load (pdf->mmgr, afmdata, pfmdata);
 
1407
    if (def) {
 
1408
        HPDF_FontDef  tmpdef = HPDF_Doc_FindFontDef (pdf, def->base_font);
 
1409
        if (tmpdef) {
 
1410
            HPDF_FontDef_Free (def);
 
1411
            HPDF_SetError (&pdf->error, HPDF_FONT_EXISTS, 0);
 
1412
            return NULL;
 
1413
        }
 
1414
 
 
1415
        if (HPDF_List_Add (pdf->fontdef_list, def) != HPDF_OK) {
 
1416
            HPDF_FontDef_Free (def);
 
1417
            return NULL;
 
1418
        }
 
1419
        return def->base_font;
 
1420
    }
 
1421
    return NULL;
 
1422
}
 
1423
 
 
1424
HPDF_EXPORT(HPDF_FontDef)
 
1425
HPDF_GetTTFontDefFromFile (HPDF_Doc      pdf,
 
1426
                           const char   *file_name,
 
1427
                           HPDF_BOOL     embedding)
 
1428
{
 
1429
        HPDF_Stream font_data;
 
1430
        HPDF_FontDef def;
 
1431
 
 
1432
        HPDF_PTRACE ((" HPDF_GetTTFontDefFromFile\n"));
 
1433
 
 
1434
        /* create file stream */
 
1435
        font_data = HPDF_FileReader_New (pdf->mmgr, file_name);
 
1436
 
 
1437
        if (HPDF_Stream_Validate (font_data)) {
 
1438
                def = HPDF_TTFontDef_Load (pdf->mmgr, font_data, embedding);
 
1439
        } else {
 
1440
                HPDF_CheckError (&pdf->error);
 
1441
                return NULL;
 
1442
        }
 
1443
 
 
1444
        return def;
 
1445
}
 
1446
 
 
1447
HPDF_EXPORT(const char*)
 
1448
HPDF_LoadTTFontFromFile (HPDF_Doc         pdf,
 
1449
                         const char      *file_name,
 
1450
                         HPDF_BOOL        embedding)
 
1451
{
 
1452
    HPDF_Stream font_data;
 
1453
    const char *ret;
 
1454
 
 
1455
    HPDF_PTRACE ((" HPDF_LoadTTFontFromFile\n"));
 
1456
 
 
1457
    if (!HPDF_HasDoc (pdf))
 
1458
        return NULL;
 
1459
 
 
1460
    /* create file stream */
 
1461
    font_data = HPDF_FileReader_New (pdf->mmgr, file_name);
 
1462
 
 
1463
    if (HPDF_Stream_Validate (font_data)) {
 
1464
        ret = LoadTTFontFromStream (pdf, font_data, embedding, file_name);
 
1465
    } else
 
1466
        ret = NULL;
 
1467
 
 
1468
    if (!ret)
 
1469
        HPDF_CheckError (&pdf->error);
 
1470
 
 
1471
    return ret;
 
1472
}
 
1473
 
 
1474
 
 
1475
static const char*
 
1476
LoadTTFontFromStream (HPDF_Doc         pdf,
 
1477
                      HPDF_Stream      font_data,
 
1478
                      HPDF_BOOL        embedding,
 
1479
                      const char      *file_name)
 
1480
{
 
1481
    HPDF_FontDef def;
 
1482
 
 
1483
    HPDF_PTRACE ((" HPDF_LoadTTFontFromStream\n"));
 
1484
 
 
1485
    def = HPDF_TTFontDef_Load (pdf->mmgr, font_data, embedding);
 
1486
    if (def) {
 
1487
        HPDF_FontDef  tmpdef = HPDF_Doc_FindFontDef (pdf, def->base_font);
 
1488
        if (tmpdef) {
 
1489
            HPDF_FontDef_Free (def);
 
1490
            HPDF_SetError (&pdf->error, HPDF_FONT_EXISTS, 0);
 
1491
            return NULL;
 
1492
        }
 
1493
 
 
1494
        if (HPDF_List_Add (pdf->fontdef_list, def) != HPDF_OK) {
 
1495
            HPDF_FontDef_Free (def);
 
1496
            return NULL;
 
1497
        }
 
1498
    } else
 
1499
        return NULL;
 
1500
 
 
1501
    if (embedding) {
 
1502
        if (pdf->ttfont_tag[0] == 0) {
 
1503
            HPDF_MemCpy (pdf->ttfont_tag, (HPDF_BYTE *)"HPDFAA", 6);
 
1504
        } else {
 
1505
            HPDF_INT i;
 
1506
 
 
1507
            for (i = 5; i >= 0; i--) {
 
1508
                pdf->ttfont_tag[i] += 1;
 
1509
                if (pdf->ttfont_tag[i] > 'Z')
 
1510
                    pdf->ttfont_tag[i] = 'A';
 
1511
                else
 
1512
                    break;
 
1513
            }
 
1514
        }
 
1515
 
 
1516
        HPDF_TTFontDef_SetTagName (def, (char *)pdf->ttfont_tag);
 
1517
    }
 
1518
 
 
1519
    return def->base_font;
 
1520
}
 
1521
 
 
1522
 
 
1523
HPDF_EXPORT(const char*)
 
1524
HPDF_LoadTTFontFromFile2 (HPDF_Doc         pdf,
 
1525
                          const char      *file_name,
 
1526
                          HPDF_UINT        index,
 
1527
                          HPDF_BOOL        embedding)
 
1528
{
 
1529
    HPDF_Stream font_data;
 
1530
    const char *ret;
 
1531
 
 
1532
    HPDF_PTRACE ((" HPDF_LoadTTFontFromFile2\n"));
 
1533
 
 
1534
    if (!HPDF_HasDoc (pdf))
 
1535
        return NULL;
 
1536
 
 
1537
    /* create file stream */
 
1538
    font_data = HPDF_FileReader_New (pdf->mmgr, file_name);
 
1539
 
 
1540
    if (HPDF_Stream_Validate (font_data)) {
 
1541
        ret = LoadTTFontFromStream2 (pdf, font_data, index, embedding, file_name);
 
1542
    } else
 
1543
        ret = NULL;
 
1544
 
 
1545
    if (!ret)
 
1546
        HPDF_CheckError (&pdf->error);
 
1547
 
 
1548
    return ret;
 
1549
}
 
1550
 
 
1551
 
 
1552
static const char*
 
1553
LoadTTFontFromStream2 (HPDF_Doc         pdf,
 
1554
                       HPDF_Stream      font_data,
 
1555
                       HPDF_UINT        index,
 
1556
                       HPDF_BOOL        embedding,
 
1557
                       const char      *file_name)
 
1558
{
 
1559
    HPDF_FontDef def;
 
1560
 
 
1561
    HPDF_PTRACE ((" HPDF_LoadTTFontFromStream2\n"));
 
1562
 
 
1563
    def = HPDF_TTFontDef_Load2 (pdf->mmgr, font_data, index, embedding);
 
1564
    if (def) {
 
1565
        HPDF_FontDef  tmpdef = HPDF_Doc_FindFontDef (pdf, def->base_font);
 
1566
        if (tmpdef) {
 
1567
            HPDF_FontDef_Free (def);
 
1568
            HPDF_SetError (&pdf->error, HPDF_FONT_EXISTS, 0);
 
1569
            return NULL;
 
1570
        }
 
1571
 
 
1572
        if (HPDF_List_Add (pdf->fontdef_list, def) != HPDF_OK) {
 
1573
            HPDF_FontDef_Free (def);
 
1574
            return NULL;
 
1575
        }
 
1576
    } else
 
1577
        return NULL;
 
1578
 
 
1579
    if (embedding) {
 
1580
        if (pdf->ttfont_tag[0] == 0) {
 
1581
            HPDF_MemCpy (pdf->ttfont_tag, (HPDF_BYTE *)"HPDFAA", 6);
 
1582
        } else {
 
1583
            HPDF_INT i;
 
1584
 
 
1585
            for (i = 5; i >= 0; i--) {
 
1586
                pdf->ttfont_tag[i] += 1;
 
1587
                if (pdf->ttfont_tag[i] > 'Z')
 
1588
                    pdf->ttfont_tag[i] = 'A';
 
1589
                else
 
1590
                    break;
 
1591
            }
 
1592
        }
 
1593
 
 
1594
        HPDF_TTFontDef_SetTagName (def, (char *)pdf->ttfont_tag);
 
1595
    }
 
1596
 
 
1597
    return def->base_font;
 
1598
}
 
1599
 
 
1600
 
 
1601
HPDF_EXPORT(HPDF_Image)
 
1602
HPDF_LoadRawImageFromFile  (HPDF_Doc          pdf,
 
1603
                            const char       *filename,
 
1604
                            HPDF_UINT         width,
 
1605
                            HPDF_UINT         height,
 
1606
                            HPDF_ColorSpace   color_space)
 
1607
{
 
1608
    HPDF_Stream imagedata;
 
1609
    HPDF_Image image;
 
1610
 
 
1611
    HPDF_PTRACE ((" HPDF_LoadRawImageFromFile\n"));
 
1612
 
 
1613
    if (!HPDF_HasDoc (pdf))
 
1614
        return NULL;
 
1615
 
 
1616
    /* create file stream */
 
1617
    imagedata = HPDF_FileReader_New (pdf->mmgr, filename);
 
1618
 
 
1619
    if (HPDF_Stream_Validate (imagedata))
 
1620
        image = HPDF_Image_LoadRawImage (pdf->mmgr, imagedata, pdf->xref, width,
 
1621
                    height, color_space);
 
1622
    else
 
1623
        image = NULL;
 
1624
 
 
1625
    /* destroy file stream */
 
1626
    HPDF_Stream_Free (imagedata);
 
1627
 
 
1628
    if (!image)
 
1629
        HPDF_CheckError (&pdf->error);
 
1630
 
 
1631
    if (image && pdf->compression_mode & HPDF_COMP_IMAGE)
 
1632
        image->filter = HPDF_STREAM_FILTER_FLATE_DECODE;
 
1633
 
 
1634
    return image;
 
1635
}
 
1636
 
 
1637
 
 
1638
HPDF_EXPORT(HPDF_Image)
 
1639
HPDF_LoadRawImageFromMem  (HPDF_Doc           pdf,
 
1640
                           const HPDF_BYTE   *buf,
 
1641
                           HPDF_UINT          width,
 
1642
                           HPDF_UINT          height,
 
1643
                           HPDF_ColorSpace    color_space,
 
1644
                           HPDF_UINT          bits_per_component)
 
1645
{
 
1646
    HPDF_Image image;
 
1647
 
 
1648
    HPDF_PTRACE ((" HPDF_LoadRawImageFromMem\n"));
 
1649
 
 
1650
    if (!HPDF_HasDoc (pdf))
 
1651
        return NULL;
 
1652
 
 
1653
    image = HPDF_Image_LoadRawImageFromMem (pdf->mmgr, buf, pdf->xref, width,
 
1654
                height, color_space, bits_per_component);
 
1655
 
 
1656
    if (!image)
 
1657
        HPDF_CheckError (&pdf->error);
 
1658
 
 
1659
    if (image && pdf->compression_mode & HPDF_COMP_IMAGE)
 
1660
        image->filter = HPDF_STREAM_FILTER_FLATE_DECODE;
 
1661
 
 
1662
    return image;
 
1663
}
 
1664
 
 
1665
 
 
1666
HPDF_EXPORT(HPDF_Image)
 
1667
HPDF_LoadJpegImageFromFile  (HPDF_Doc     pdf,
 
1668
                             const char  *filename)
 
1669
{
 
1670
    HPDF_Stream imagedata;
 
1671
    HPDF_Image image;
 
1672
 
 
1673
    HPDF_PTRACE ((" HPDF_LoadJpegImageFromFile\n"));
 
1674
 
 
1675
    if (!HPDF_HasDoc (pdf))
 
1676
        return NULL;
 
1677
 
 
1678
    /* create file stream */
 
1679
    imagedata = HPDF_FileReader_New (pdf->mmgr, filename);
 
1680
 
 
1681
    if (HPDF_Stream_Validate (imagedata))
 
1682
        image = HPDF_Image_LoadJpegImage (pdf->mmgr, imagedata, pdf->xref);
 
1683
    else
 
1684
        image = NULL;
 
1685
 
 
1686
    /* destroy file stream */
 
1687
    HPDF_Stream_Free (imagedata);
 
1688
 
 
1689
    if (!image)
 
1690
        HPDF_CheckError (&pdf->error);
 
1691
 
 
1692
    return image;
 
1693
}
 
1694
 
 
1695
 
 
1696
/*----- Catalog ------------------------------------------------------------*/
 
1697
 
 
1698
HPDF_EXPORT(HPDF_PageLayout)
 
1699
HPDF_GetPageLayout  (HPDF_Doc   pdf)
 
1700
{
 
1701
    HPDF_PTRACE ((" HPDF_GetPageLayout\n"));
 
1702
 
 
1703
    if (!HPDF_HasDoc (pdf))
 
1704
        return HPDF_PAGE_LAYOUT_SINGLE;
 
1705
 
 
1706
    return HPDF_Catalog_GetPageLayout (pdf->catalog);
 
1707
}
 
1708
 
 
1709
 
 
1710
HPDF_EXPORT(HPDF_STATUS)
 
1711
HPDF_SetPageLayout  (HPDF_Doc          pdf,
 
1712
                     HPDF_PageLayout   layout)
 
1713
{
 
1714
    HPDF_STATUS ret;
 
1715
 
 
1716
    HPDF_PTRACE ((" HPDF_GetPageLayout\n"));
 
1717
 
 
1718
    if (!HPDF_HasDoc (pdf))
 
1719
        return HPDF_INVALID_DOCUMENT;
 
1720
 
 
1721
    if (layout < 0 || layout >= HPDF_PAGE_LAYOUT_EOF)
 
1722
        return HPDF_RaiseError (&pdf->error, HPDF_PAGE_LAYOUT_OUT_OF_RANGE,
 
1723
                (HPDF_STATUS)layout);
 
1724
 
 
1725
    ret = HPDF_Catalog_SetPageLayout (pdf->catalog, layout);
 
1726
    if (ret != HPDF_OK)
 
1727
        HPDF_CheckError (&pdf->error);
 
1728
 
 
1729
    return HPDF_OK;
 
1730
}
 
1731
 
 
1732
HPDF_EXPORT(HPDF_PageMode)
 
1733
HPDF_GetPageMode  (HPDF_Doc   pdf)
 
1734
{
 
1735
    HPDF_PTRACE ((" HPDF_GetPageMode\n"));
 
1736
 
 
1737
    if (!HPDF_HasDoc (pdf))
 
1738
        return HPDF_PAGE_MODE_USE_NONE;
 
1739
 
 
1740
    return HPDF_Catalog_GetPageMode (pdf->catalog);
 
1741
}
 
1742
 
 
1743
 
 
1744
HPDF_EXPORT(HPDF_STATUS)
 
1745
HPDF_SetPageMode  (HPDF_Doc        pdf,
 
1746
                   HPDF_PageMode   mode)
 
1747
{
 
1748
    HPDF_STATUS ret;
 
1749
 
 
1750
    HPDF_PTRACE ((" HPDF_GetPageMode\n"));
 
1751
 
 
1752
    if (!HPDF_HasDoc (pdf))
 
1753
        return HPDF_INVALID_DOCUMENT;
 
1754
 
 
1755
    if (mode < 0 || mode >= HPDF_PAGE_MODE_EOF)
 
1756
        return HPDF_RaiseError (&pdf->error, HPDF_PAGE_MODE_OUT_OF_RANGE,
 
1757
                (HPDF_STATUS)mode);
 
1758
 
 
1759
    ret = HPDF_Catalog_SetPageMode (pdf->catalog, mode);
 
1760
    if (ret != HPDF_OK)
 
1761
        return HPDF_CheckError (&pdf->error);
 
1762
 
 
1763
    return HPDF_OK;
 
1764
}
 
1765
 
 
1766
 
 
1767
HPDF_EXPORT(HPDF_STATUS)
 
1768
HPDF_SetOpenAction  (HPDF_Doc            pdf,
 
1769
                     HPDF_Destination    open_action)
 
1770
{
 
1771
    HPDF_STATUS ret;
 
1772
 
 
1773
    HPDF_PTRACE ((" HPDF_SetOpenAction\n"));
 
1774
 
 
1775
    if (!HPDF_HasDoc (pdf))
 
1776
        return HPDF_INVALID_DOCUMENT;
 
1777
 
 
1778
    if (open_action && !HPDF_Destination_Validate (open_action))
 
1779
        return HPDF_RaiseError (&pdf->error, HPDF_INVALID_DESTINATION, 0);
 
1780
 
 
1781
    ret = HPDF_Catalog_SetOpenAction (pdf->catalog, open_action);
 
1782
    if (ret != HPDF_OK)
 
1783
        return HPDF_CheckError (&pdf->error);
 
1784
 
 
1785
    return HPDF_OK;
 
1786
}
 
1787
 
 
1788
 
 
1789
HPDF_EXPORT(HPDF_UINT)
 
1790
HPDF_GetViewerPreference  (HPDF_Doc   pdf)
 
1791
{
 
1792
    HPDF_PTRACE ((" HPDF_Catalog_GetViewerPreference\n"));
 
1793
 
 
1794
    if (!HPDF_HasDoc (pdf))
 
1795
        return 0;
 
1796
 
 
1797
    return HPDF_Catalog_GetViewerPreference (pdf->catalog);
 
1798
}
 
1799
 
 
1800
 
 
1801
HPDF_EXPORT(HPDF_STATUS)
 
1802
HPDF_SetViewerPreference  (HPDF_Doc     pdf,
 
1803
                           HPDF_UINT    value)
 
1804
{
 
1805
    HPDF_STATUS ret;
 
1806
 
 
1807
    HPDF_PTRACE ((" HPDF_Catalog_SetViewerPreference\n"));
 
1808
 
 
1809
    if (!HPDF_HasDoc (pdf))
 
1810
        return HPDF_INVALID_DOCUMENT;
 
1811
 
 
1812
    ret = HPDF_Catalog_SetViewerPreference (pdf->catalog, value);
 
1813
    if (ret != HPDF_OK)
 
1814
        return HPDF_CheckError (&pdf->error);
 
1815
 
 
1816
    return HPDF_OK;
 
1817
}
 
1818
 
 
1819
 
 
1820
HPDF_EXPORT(HPDF_STATUS)
 
1821
HPDF_AddPageLabel  (HPDF_Doc             pdf,
 
1822
                    HPDF_UINT            page_num,
 
1823
                    HPDF_PageNumStyle    style,
 
1824
                    HPDF_UINT            first_page,
 
1825
                    const char     *prefix)
 
1826
{
 
1827
    HPDF_Dict page_label;
 
1828
    HPDF_STATUS ret;
 
1829
 
 
1830
    HPDF_PTRACE ((" HPDF_AddPageLabel\n"));
 
1831
 
 
1832
    if (!HPDF_HasDoc (pdf))
 
1833
        return HPDF_INVALID_DOCUMENT;
 
1834
 
 
1835
    page_label = HPDF_PageLabel_New (pdf, style, first_page, prefix);
 
1836
 
 
1837
    if (!page_label)
 
1838
        return HPDF_CheckError (&pdf->error);
 
1839
 
 
1840
    if (style < 0 || style >= HPDF_PAGE_NUM_STYLE_EOF)
 
1841
        return HPDF_RaiseError (&pdf->error, HPDF_PAGE_NUM_STYLE_OUT_OF_RANGE,
 
1842
                    (HPDF_STATUS)style);
 
1843
 
 
1844
    ret = HPDF_Catalog_AddPageLabel (pdf->catalog, page_num, page_label);
 
1845
    if (ret != HPDF_OK)
 
1846
        return HPDF_CheckError (&pdf->error);
 
1847
 
 
1848
    return HPDF_OK;
 
1849
}
 
1850
 
 
1851
 
 
1852
/*----- Info ---------------------------------------------------------------*/
 
1853
 
 
1854
static HPDF_Dict
 
1855
GetInfo  (HPDF_Doc  pdf)
 
1856
{
 
1857
    if (!HPDF_HasDoc (pdf))
 
1858
        return NULL;
 
1859
 
 
1860
    if (!pdf->info) {
 
1861
        pdf->info = HPDF_Dict_New (pdf->mmgr);
 
1862
 
 
1863
        if (!pdf->info || HPDF_Xref_Add (pdf->xref, pdf->info) != HPDF_OK)
 
1864
            pdf->info = NULL;
 
1865
    }
 
1866
 
 
1867
    return pdf->info;
 
1868
}
 
1869
 
 
1870
 
 
1871
HPDF_EXPORT(HPDF_STATUS)
 
1872
HPDF_SetInfoAttr  (HPDF_Doc          pdf,
 
1873
                   HPDF_InfoType     type,
 
1874
                   const char  *value)
 
1875
{
 
1876
    HPDF_STATUS ret;
 
1877
    HPDF_Dict info = GetInfo (pdf);
 
1878
 
 
1879
    HPDF_PTRACE((" HPDF_SetInfoAttr\n"));
 
1880
 
 
1881
    if (!info)
 
1882
        return HPDF_CheckError (&pdf->error);
 
1883
 
 
1884
    ret = HPDF_Info_SetInfoAttr (info, type, value, pdf->cur_encoder);
 
1885
    if (ret != HPDF_OK)
 
1886
        return HPDF_CheckError (&pdf->error);
 
1887
 
 
1888
    return ret;
 
1889
}
 
1890
 
 
1891
 
 
1892
HPDF_EXPORT(const char*)
 
1893
HPDF_GetInfoAttr  (HPDF_Doc        pdf,
 
1894
                   HPDF_InfoType   type)
 
1895
{
 
1896
    const char *ret = NULL;
 
1897
    HPDF_Dict info = GetInfo (pdf);
 
1898
 
 
1899
    HPDF_PTRACE((" HPDF_GetInfoAttr\n"));
 
1900
 
 
1901
    if (info)
 
1902
        ret = HPDF_Info_GetInfoAttr (info, type);
 
1903
    else
 
1904
        HPDF_CheckError (&pdf->error);
 
1905
 
 
1906
    return ret;
 
1907
}
 
1908
 
 
1909
 
 
1910
HPDF_EXPORT(HPDF_STATUS)
 
1911
HPDF_SetInfoDateAttr  (HPDF_Doc        pdf,
 
1912
                       HPDF_InfoType   type,
 
1913
                       HPDF_Date       value)
 
1914
{
 
1915
    HPDF_STATUS ret;
 
1916
    HPDF_Dict info = GetInfo (pdf);
 
1917
 
 
1918
    HPDF_PTRACE((" HPDF_SetInfoDateAttr\n"));
 
1919
 
 
1920
    if (!info)
 
1921
        return HPDF_CheckError (&pdf->error);
 
1922
 
 
1923
    ret = HPDF_Info_SetInfoDateAttr (info, type, value);
 
1924
    if (ret != HPDF_OK)
 
1925
        return HPDF_CheckError (&pdf->error);
 
1926
 
 
1927
    return ret;
 
1928
}
 
1929
 
 
1930
 
 
1931
HPDF_EXPORT(HPDF_Outline)
 
1932
HPDF_CreateOutline  (HPDF_Doc       pdf,
 
1933
                     HPDF_Outline   parent,
 
1934
                     const char    *title,
 
1935
                     HPDF_Encoder   encoder)
 
1936
{
 
1937
    HPDF_Outline outline;
 
1938
 
 
1939
    if (!HPDF_HasDoc (pdf))
 
1940
        return NULL;
 
1941
 
 
1942
    if (!parent) {
 
1943
        if (pdf->outlines) {
 
1944
            parent = pdf->outlines;
 
1945
        } else {
 
1946
            pdf->outlines = HPDF_OutlineRoot_New (pdf->mmgr, pdf->xref);
 
1947
 
 
1948
            if (pdf->outlines) {
 
1949
                HPDF_STATUS ret = HPDF_Dict_Add (pdf->catalog, "Outlines",
 
1950
                            pdf->outlines);
 
1951
                if (ret != HPDF_OK) {
 
1952
                    HPDF_CheckError (&pdf->error);
 
1953
                    pdf->outlines = NULL;
 
1954
                    return NULL;
 
1955
                }
 
1956
 
 
1957
                parent = pdf->outlines;
 
1958
            } else {
 
1959
                HPDF_CheckError (&pdf->error);
 
1960
                return NULL;
 
1961
            }
 
1962
        }
 
1963
    }
 
1964
 
 
1965
    if (!HPDF_Outline_Validate (parent) || pdf->mmgr != parent->mmgr) {
 
1966
        HPDF_RaiseError (&pdf->error, HPDF_INVALID_OUTLINE, 0);
 
1967
        return NULL;
 
1968
    }
 
1969
 
 
1970
    outline = HPDF_Outline_New (pdf->mmgr, parent, title, encoder, pdf->xref);
 
1971
    if (!outline)
 
1972
        HPDF_CheckError (&pdf->error);
 
1973
 
 
1974
    return outline;
 
1975
}
 
1976
 
 
1977
 
 
1978
HPDF_EXPORT(HPDF_ExtGState)
 
1979
HPDF_CreateExtGState  (HPDF_Doc  pdf)
 
1980
{
 
1981
    HPDF_ExtGState ext_gstate;
 
1982
 
 
1983
    if (!HPDF_HasDoc (pdf))
 
1984
        return NULL;
 
1985
 
 
1986
    pdf->pdf_version = HPDF_VER_14;
 
1987
 
 
1988
    ext_gstate = HPDF_ExtGState_New (pdf->mmgr, pdf->xref);
 
1989
    if (!ext_gstate)
 
1990
        HPDF_CheckError (&pdf->error);
 
1991
 
 
1992
    return ext_gstate;
 
1993
}
 
1994
 
 
1995
 
 
1996
HPDF_EXPORT(HPDF_STATUS)
 
1997
HPDF_SetCompressionMode  (HPDF_Doc    pdf,
 
1998
                          HPDF_UINT   mode)
 
1999
{
 
2000
    if (!HPDF_Doc_Validate (pdf))
 
2001
        return HPDF_INVALID_DOCUMENT;
 
2002
 
 
2003
    if (mode != (mode & HPDF_COMP_MASK))
 
2004
        return HPDF_RaiseError (&pdf->error, HPDF_INVALID_COMPRESSION_MODE, 0);
 
2005
 
 
2006
#ifndef HPDF_NOZLIB
 
2007
    pdf->compression_mode = mode;
 
2008
 
 
2009
    return HPDF_OK;
 
2010
 
 
2011
#else /* HPDF_NOZLIB */
 
2012
 
 
2013
    return HPDF_INVALID_COMPRESSION_MODE;
 
2014
 
 
2015
#endif /* HPDF_NOZLIB */
 
2016
}
 
2017
 
 
2018
 
 
2019
HPDF_EXPORT(HPDF_STATUS)
 
2020
HPDF_GetError  (HPDF_Doc   pdf)
 
2021
{
 
2022
    if (!HPDF_Doc_Validate (pdf))
 
2023
        return HPDF_INVALID_DOCUMENT;
 
2024
 
 
2025
    return HPDF_Error_GetCode (&pdf->error);
 
2026
}
 
2027
 
 
2028
 
 
2029
HPDF_EXPORT(HPDF_STATUS)
 
2030
HPDF_GetErrorDetail  (HPDF_Doc   pdf)
 
2031
{
 
2032
    if (!HPDF_Doc_Validate (pdf))
 
2033
        return HPDF_INVALID_DOCUMENT;
 
2034
 
 
2035
    return HPDF_Error_GetDetailCode (&pdf->error);
 
2036
}
 
2037
 
 
2038
 
 
2039
HPDF_EXPORT(void)
 
2040
HPDF_ResetError  (HPDF_Doc   pdf)
 
2041
{
 
2042
    if (!HPDF_Doc_Validate (pdf))
 
2043
        return;
 
2044
 
 
2045
    HPDF_Error_Reset (&pdf->error);
 
2046
}
 
2047