hpdf 2.0.8

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