hpdf 2.0.8

Sign up to get free protection for your applications and to get access to all the features.
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,339 @@
1
+ /*
2
+ * << Haru Free PDF Library 2.0.0 >> -- hpdf_destination.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
+ #include "hpdf_conf.h"
16
+ #include "hpdf_utils.h"
17
+ #include "hpdf.h"
18
+
19
+ const char *HPDF_DESTINATION_TYPE_NAMES[] = {
20
+ "XYZ",
21
+ "Fit",
22
+ "FitH",
23
+ "FitV",
24
+ "FitR",
25
+ "FitB",
26
+ "FitBH",
27
+ "FitBV",
28
+ NULL
29
+ };
30
+
31
+ /*----------------------------------------------------------------------------*/
32
+ /*----- HPDF_Destination -----------------------------------------------------*/
33
+
34
+ HPDF_Destination
35
+ HPDF_Destination_New (HPDF_MMgr mmgr,
36
+ HPDF_Page target,
37
+ HPDF_Xref xref)
38
+ {
39
+ HPDF_Destination dst;
40
+
41
+ HPDF_PTRACE((" HPDF_Destination_New\n"));
42
+
43
+ if (!HPDF_Page_Validate (target)) {
44
+ HPDF_SetError (mmgr->error, HPDF_INVALID_PAGE, 0);
45
+ return NULL;
46
+ }
47
+
48
+ dst = HPDF_Array_New (mmgr);
49
+ if (!dst)
50
+ return NULL;
51
+
52
+ dst->header.obj_class |= HPDF_OSUBCLASS_DESTINATION;
53
+
54
+ if (HPDF_Xref_Add (xref, dst) != HPDF_OK)
55
+ return NULL;
56
+
57
+ /* first item of array must be target page */
58
+ if (HPDF_Array_Add (dst, target) != HPDF_OK)
59
+ return NULL;
60
+
61
+ /* default type is HPDF_FIT */
62
+ if (HPDF_Array_AddName (dst,
63
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT]) != HPDF_OK)
64
+ return NULL;
65
+
66
+ return dst;
67
+ }
68
+
69
+
70
+ HPDF_BOOL
71
+ HPDF_Destination_Validate (HPDF_Destination dst)
72
+ {
73
+ HPDF_Obj_Header *header = (HPDF_Obj_Header *)dst;
74
+ HPDF_Page target;
75
+
76
+ if (!dst || header->obj_class !=
77
+ (HPDF_OCLASS_ARRAY | HPDF_OSUBCLASS_DESTINATION))
78
+ return HPDF_FALSE;
79
+
80
+ /* destination-types not defined. */
81
+ if (dst->list->count < 2)
82
+ return HPDF_FALSE;
83
+
84
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
85
+ if (!HPDF_Page_Validate (target))
86
+ return HPDF_SetError (dst->error, HPDF_INVALID_PAGE, 0);
87
+
88
+ return HPDF_TRUE;
89
+ }
90
+
91
+
92
+ HPDF_EXPORT(HPDF_STATUS)
93
+ HPDF_Destination_SetXYZ (HPDF_Destination dst,
94
+ HPDF_REAL left,
95
+ HPDF_REAL top,
96
+ HPDF_REAL zoom)
97
+ {
98
+ HPDF_STATUS ret = HPDF_OK;
99
+ HPDF_Page target;
100
+
101
+ HPDF_PTRACE((" HPDF_Destination_SetXYZ\n"));
102
+
103
+ if (!HPDF_Destination_Validate (dst))
104
+ return HPDF_INVALID_DESTINATION;
105
+
106
+ if (left < 0 || top < 0 || zoom < 0.08 || zoom > 32)
107
+ return HPDF_RaiseError (dst->error, HPDF_INVALID_PARAMETER, 0);
108
+
109
+
110
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
111
+
112
+ if (dst->list->count > 1) {
113
+ HPDF_Array_Clear (dst);
114
+ ret += HPDF_Array_Add (dst, target);
115
+ }
116
+
117
+ ret += HPDF_Array_AddName (dst,
118
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_XYZ]);
119
+ ret += HPDF_Array_AddReal (dst, left);
120
+ ret += HPDF_Array_AddReal (dst, top);
121
+ ret += HPDF_Array_AddReal (dst, zoom);
122
+
123
+ if (ret != HPDF_OK)
124
+ return HPDF_CheckError (dst->error);
125
+
126
+ return HPDF_OK;
127
+ }
128
+
129
+
130
+ HPDF_EXPORT(HPDF_STATUS)
131
+ HPDF_Destination_SetFit (HPDF_Destination dst)
132
+ {
133
+ HPDF_STATUS ret = HPDF_OK;
134
+ HPDF_Page target;
135
+
136
+ HPDF_PTRACE((" HPDF_Destination_SetFit\n"));
137
+
138
+ if (!HPDF_Destination_Validate (dst))
139
+ return HPDF_INVALID_DESTINATION;
140
+
141
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
142
+
143
+ if (dst->list->count > 1) {
144
+ HPDF_Array_Clear (dst);
145
+ ret += HPDF_Array_Add (dst, target);
146
+ }
147
+
148
+ ret += HPDF_Array_AddName (dst,
149
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT]);
150
+
151
+ if (ret != HPDF_OK)
152
+ return HPDF_CheckError (dst->error);
153
+
154
+ return HPDF_OK;
155
+ }
156
+
157
+
158
+ HPDF_EXPORT(HPDF_STATUS)
159
+ HPDF_Destination_SetFitH (HPDF_Destination dst,
160
+ HPDF_REAL top)
161
+ {
162
+ HPDF_STATUS ret = HPDF_OK;
163
+ HPDF_Page target;
164
+
165
+ HPDF_PTRACE((" HPDF_Destination_SetFitH\n"));
166
+
167
+ if (!HPDF_Destination_Validate (dst))
168
+ return HPDF_INVALID_DESTINATION;
169
+
170
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
171
+
172
+ if (dst->list->count > 1) {
173
+ HPDF_Array_Clear (dst);
174
+ ret += HPDF_Array_Add (dst, target);
175
+ }
176
+
177
+ ret += HPDF_Array_AddName (dst,
178
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT_H]);
179
+ ret += HPDF_Array_AddReal (dst, top);
180
+
181
+ if (ret != HPDF_OK)
182
+ return HPDF_CheckError (dst->error);
183
+
184
+ return HPDF_OK;
185
+ }
186
+
187
+ HPDF_EXPORT(HPDF_STATUS)
188
+ HPDF_Destination_SetFitV (HPDF_Destination dst,
189
+ HPDF_REAL left)
190
+ {
191
+ HPDF_STATUS ret = HPDF_OK;
192
+ HPDF_Page target;
193
+
194
+ HPDF_PTRACE((" HPDF_Destination_SetFitV\n"));
195
+
196
+ if (!HPDF_Destination_Validate (dst))
197
+ return HPDF_INVALID_DESTINATION;
198
+
199
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
200
+
201
+ if (dst->list->count > 1) {
202
+ HPDF_Array_Clear (dst);
203
+ ret += HPDF_Array_Add (dst, target);
204
+ }
205
+
206
+ ret += HPDF_Array_AddName (dst,
207
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT_V]);
208
+ ret += HPDF_Array_AddReal (dst, left);
209
+
210
+ if (ret != HPDF_OK)
211
+ return HPDF_CheckError (dst->error);
212
+
213
+ return HPDF_OK;
214
+ }
215
+
216
+
217
+ HPDF_EXPORT(HPDF_STATUS)
218
+ HPDF_Destination_SetFitR (HPDF_Destination dst,
219
+ HPDF_REAL left,
220
+ HPDF_REAL bottom,
221
+ HPDF_REAL right,
222
+ HPDF_REAL top)
223
+ {
224
+ HPDF_STATUS ret = HPDF_OK;
225
+ HPDF_Page target;
226
+
227
+ HPDF_PTRACE((" HPDF_Destination_SetFitR\n"));
228
+
229
+ if (!HPDF_Destination_Validate (dst))
230
+ return HPDF_INVALID_DESTINATION;
231
+
232
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
233
+
234
+ if (dst->list->count > 1) {
235
+ HPDF_Array_Clear (dst);
236
+ ret += HPDF_Array_Add (dst, target);
237
+ }
238
+
239
+ ret += HPDF_Array_AddName (dst,
240
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT_R]);
241
+ ret += HPDF_Array_AddReal (dst, left);
242
+ ret += HPDF_Array_AddReal (dst, bottom);
243
+ ret += HPDF_Array_AddReal (dst, right);
244
+ ret += HPDF_Array_AddReal (dst, top);
245
+
246
+ if (ret != HPDF_OK)
247
+ return HPDF_CheckError (dst->error);
248
+
249
+ return HPDF_OK;
250
+ }
251
+
252
+
253
+ HPDF_EXPORT(HPDF_STATUS)
254
+ HPDF_Destination_SetFitB (HPDF_Destination dst)
255
+ {
256
+ HPDF_STATUS ret = HPDF_OK;
257
+ HPDF_Page target;
258
+
259
+ HPDF_PTRACE((" HPDF_Destination_SetFitB\n"));
260
+
261
+ if (!HPDF_Destination_Validate (dst))
262
+ return HPDF_INVALID_DESTINATION;
263
+
264
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
265
+
266
+ if (dst->list->count > 1) {
267
+ HPDF_Array_Clear (dst);
268
+ ret += HPDF_Array_Add (dst, target);
269
+ }
270
+
271
+ ret += HPDF_Array_AddName (dst,
272
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT_B]);
273
+
274
+ if (ret != HPDF_OK)
275
+ return HPDF_CheckError (dst->error);
276
+
277
+ return HPDF_OK;
278
+ }
279
+
280
+
281
+ HPDF_EXPORT(HPDF_STATUS)
282
+ HPDF_Destination_SetFitBH (HPDF_Destination dst,
283
+ HPDF_REAL top)
284
+ {
285
+ HPDF_STATUS ret = HPDF_OK;
286
+ HPDF_Page target;
287
+
288
+ HPDF_PTRACE((" HPDF_Destination_SetFitBH\n"));
289
+
290
+ if (!HPDF_Destination_Validate (dst))
291
+ return HPDF_INVALID_DESTINATION;
292
+
293
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
294
+
295
+ if (dst->list->count > 1) {
296
+ HPDF_Array_Clear (dst);
297
+ ret += HPDF_Array_Add (dst, target);
298
+ }
299
+
300
+ ret += HPDF_Array_AddName (dst,
301
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT_BH]);
302
+ ret += HPDF_Array_AddReal (dst, top);
303
+
304
+ if (ret != HPDF_OK)
305
+ return HPDF_CheckError (dst->error);
306
+
307
+ return HPDF_OK;
308
+ }
309
+
310
+ HPDF_EXPORT(HPDF_STATUS)
311
+ HPDF_Destination_SetFitBV (HPDF_Destination dst,
312
+ HPDF_REAL left)
313
+ {
314
+ HPDF_STATUS ret = HPDF_OK;
315
+ HPDF_Page target;
316
+
317
+ HPDF_PTRACE((" HPDF_Destination_SetFitBV\n"));
318
+
319
+ if (!HPDF_Destination_Validate (dst))
320
+ return HPDF_INVALID_DESTINATION;
321
+
322
+ target = (HPDF_Page)HPDF_Array_GetItem (dst, 0, HPDF_OCLASS_DICT);
323
+
324
+ if (dst->list->count > 1) {
325
+ HPDF_Array_Clear (dst);
326
+ ret += HPDF_Array_Add (dst, target);
327
+ }
328
+
329
+ ret += HPDF_Array_AddName (dst,
330
+ HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT_BV]);
331
+ ret += HPDF_Array_AddReal (dst, left);
332
+
333
+ if (ret != HPDF_OK)
334
+ return HPDF_CheckError (dst->error);
335
+
336
+ return HPDF_OK;
337
+
338
+ }
339
+
@@ -0,0 +1,488 @@
1
+ /*
2
+ * << Haru Free PDF Library 2.0.5 >> -- HPDF_Dict.c
3
+ *
4
+ * Copyright (c) 1999-2004 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
+ #include "hpdf_conf.h"
16
+ #include "hpdf_utils.h"
17
+ #include "hpdf_objects.h"
18
+
19
+ HPDF_DictElement
20
+ GetElement (HPDF_Dict dict,
21
+ const char *key);
22
+
23
+ /*--------------------------------------------------------------------------*/
24
+
25
+ HPDF_Dict
26
+ HPDF_Dict_New (HPDF_MMgr mmgr)
27
+ {
28
+ HPDF_Dict obj;
29
+
30
+ obj = (HPDF_Dict)HPDF_GetMem (mmgr, sizeof(HPDF_Dict_Rec));
31
+ if (obj) {
32
+ HPDF_MemSet (obj, 0, sizeof(HPDF_Dict_Rec));
33
+ obj->header.obj_class = HPDF_OCLASS_DICT;
34
+ obj->mmgr = mmgr;
35
+ obj->error = mmgr->error;
36
+ obj->list = HPDF_List_New (mmgr, HPDF_DEF_ITEMS_PER_BLOCK);
37
+ obj->filter = HPDF_STREAM_FILTER_NONE;
38
+ if (!obj->list) {
39
+ HPDF_FreeMem (mmgr, obj);
40
+ obj = NULL;
41
+ }
42
+ }
43
+
44
+ return obj;
45
+ }
46
+
47
+
48
+ HPDF_Dict
49
+ HPDF_DictStream_New (HPDF_MMgr mmgr,
50
+ HPDF_Xref xref)
51
+ {
52
+ HPDF_Dict obj;
53
+ HPDF_Number length;
54
+ HPDF_STATUS ret = 0;
55
+
56
+ obj = HPDF_Dict_New (mmgr);
57
+ if (!obj)
58
+ return NULL;
59
+
60
+ /* only stream object is added to xref automatically */
61
+ ret += HPDF_Xref_Add (xref, obj);
62
+ if (ret != HPDF_OK)
63
+ return NULL;
64
+
65
+ length = HPDF_Number_New (mmgr, 0);
66
+ if (!length)
67
+ return NULL;
68
+
69
+ ret = HPDF_Xref_Add (xref, length);
70
+ if (ret != HPDF_OK)
71
+ return NULL;
72
+
73
+ ret = HPDF_Dict_Add (obj, "Length", length);
74
+ if (ret != HPDF_OK)
75
+ return NULL;
76
+
77
+ obj->stream = HPDF_MemStream_New (mmgr, HPDF_STREAM_BUF_SIZ);
78
+ if (!obj->stream)
79
+ return NULL;
80
+
81
+ return obj;
82
+ }
83
+
84
+
85
+ void
86
+ HPDF_Dict_Free (HPDF_Dict dict)
87
+ {
88
+ HPDF_UINT i;
89
+
90
+ if (!dict)
91
+ return;
92
+
93
+ if (dict->free_fn)
94
+ dict->free_fn (dict);
95
+
96
+ for (i = 0; i < dict->list->count; i++) {
97
+ HPDF_DictElement element =
98
+ (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
99
+
100
+ if (element) {
101
+ HPDF_Obj_Free (dict->mmgr, element->value);
102
+ HPDF_FreeMem (dict->mmgr, element);
103
+ }
104
+ }
105
+
106
+ if (dict->stream)
107
+ HPDF_Stream_Free (dict->stream);
108
+
109
+ HPDF_List_Free (dict->list);
110
+
111
+ dict->header.obj_class = 0;
112
+
113
+ HPDF_FreeMem (dict->mmgr, dict);
114
+ }
115
+
116
+
117
+ HPDF_STATUS
118
+ HPDF_Dict_Write (HPDF_Dict dict,
119
+ HPDF_Stream stream,
120
+ HPDF_Encrypt e)
121
+ {
122
+ HPDF_UINT i;
123
+ HPDF_STATUS ret;
124
+
125
+ ret = HPDF_Stream_WriteStr (stream, "<<\012");
126
+ if (ret != HPDF_OK)
127
+ return ret;
128
+
129
+ if (dict->before_write_fn) {
130
+ if ((ret = dict->before_write_fn (dict)) != HPDF_OK)
131
+ return ret;
132
+ }
133
+
134
+ /* encrypt-dict must not be encrypted. */
135
+ if (dict->header.obj_class == (HPDF_OCLASS_DICT | HPDF_OSUBCLASS_ENCRYPT))
136
+ e = NULL;
137
+
138
+ if (dict->stream) {
139
+ /* set filter element */
140
+ if (dict->filter == HPDF_STREAM_FILTER_NONE)
141
+ HPDF_Dict_RemoveElement (dict, "Filter");
142
+ else {
143
+ HPDF_Array array = HPDF_Dict_GetItem (dict, "Filter",
144
+ HPDF_OCLASS_ARRAY);
145
+
146
+ if (!array) {
147
+ array = HPDF_Array_New (dict->mmgr);
148
+ if (!array)
149
+ return HPDF_Error_GetCode (dict->error);
150
+
151
+ ret = HPDF_Dict_Add (dict, "Filter", array);
152
+ if (ret != HPDF_OK)
153
+ return ret;
154
+ }
155
+
156
+ HPDF_Array_Clear (array);
157
+
158
+ #ifndef HPDF_NOZLIB
159
+ if (dict->filter & HPDF_STREAM_FILTER_FLATE_DECODE)
160
+ HPDF_Array_AddName (array, "FlateDecode");
161
+ #endif /* HPDF_NOZLIB */
162
+
163
+ if (dict->filter & HPDF_STREAM_FILTER_DCT_DECODE)
164
+ HPDF_Array_AddName (array, "DCTDecode");
165
+ }
166
+ }
167
+
168
+ for (i = 0; i < dict->list->count; i++) {
169
+ HPDF_DictElement element =
170
+ (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
171
+ HPDF_Obj_Header *header = (HPDF_Obj_Header *)(element->value);
172
+
173
+ if (!element->value)
174
+ return HPDF_SetError (dict->error, HPDF_INVALID_OBJECT, 0);
175
+
176
+ if (header->obj_id & HPDF_OTYPE_HIDDEN) {
177
+ HPDF_PTRACE((" HPDF_Dict_Write obj=%p skipped obj_id=0x%08X\n",
178
+ element->value, (HPDF_UINT)header->obj_id));
179
+ } else {
180
+ ret = HPDF_Stream_WriteEscapeName (stream, element->key);
181
+ if (ret != HPDF_OK)
182
+ return ret;
183
+
184
+ ret = HPDF_Stream_WriteChar (stream, ' ');
185
+ if (ret != HPDF_OK)
186
+ return ret;
187
+
188
+ ret = HPDF_Obj_Write (element->value, stream, e);
189
+ if (ret != HPDF_OK)
190
+ return ret;
191
+
192
+ ret = HPDF_Stream_WriteStr (stream, "\012");
193
+ if (ret != HPDF_OK)
194
+ return ret;
195
+ }
196
+ }
197
+
198
+ if (dict->write_fn) {
199
+ if ((ret = dict->write_fn (dict, stream)) != HPDF_OK)
200
+ return ret;
201
+ }
202
+
203
+ if ((ret = HPDF_Stream_WriteStr (stream, ">>")) != HPDF_OK)
204
+ return ret;
205
+
206
+ if (dict->stream) {
207
+ HPDF_UINT32 strptr;
208
+ HPDF_Number length;
209
+
210
+ /* get "length" element */
211
+ length = (HPDF_Number)HPDF_Dict_GetItem (dict, "Length",
212
+ HPDF_OCLASS_NUMBER);
213
+ if (!length)
214
+ return HPDF_SetError (dict->error,
215
+ HPDF_DICT_STREAM_LENGTH_NOT_FOUND, 0);
216
+
217
+ /* "length" element must be indirect-object */
218
+ if (!(length->header.obj_id & HPDF_OTYPE_INDIRECT)) {
219
+ return HPDF_SetError (dict->error, HPDF_DICT_ITEM_UNEXPECTED_TYPE,
220
+ 0);
221
+ }
222
+
223
+ if ((ret = HPDF_Stream_WriteStr (stream, "\012stream\015\012"))
224
+ != HPDF_OK)
225
+ return ret;
226
+
227
+ strptr = stream->size;
228
+
229
+ if (e)
230
+ HPDF_Encrypt_Reset (e);
231
+
232
+ if ((ret = HPDF_Stream_WriteToStream (dict->stream, stream,
233
+ dict->filter, e)) != HPDF_OK)
234
+ return ret;
235
+
236
+ HPDF_Number_SetValue (length, stream->size - strptr);
237
+
238
+ ret = HPDF_Stream_WriteStr (stream, "\012endstream");
239
+ }
240
+
241
+ /* 2006.08.13 add. */
242
+ if (dict->after_write_fn) {
243
+ if ((ret = dict->after_write_fn (dict)) != HPDF_OK)
244
+ return ret;
245
+ }
246
+
247
+ return ret;
248
+ }
249
+
250
+ HPDF_STATUS
251
+ HPDF_Dict_Add (HPDF_Dict dict,
252
+ const char *key,
253
+ void *obj)
254
+ {
255
+ HPDF_Obj_Header *header;
256
+ HPDF_STATUS ret = HPDF_OK;
257
+ HPDF_DictElement element;
258
+
259
+ if (!obj) {
260
+ if (HPDF_Error_GetCode (dict->error) == HPDF_OK)
261
+ return HPDF_SetError (dict->error, HPDF_INVALID_OBJECT, 0);
262
+ else
263
+ return HPDF_INVALID_OBJECT;
264
+ }
265
+
266
+ header = (HPDF_Obj_Header *)obj;
267
+
268
+ if (header->obj_id & HPDF_OTYPE_DIRECT)
269
+ return HPDF_SetError (dict->error, HPDF_INVALID_OBJECT, 0);
270
+
271
+ if (!key) {
272
+ HPDF_Obj_Free (dict->mmgr, obj);
273
+ return HPDF_SetError (dict->error, HPDF_INVALID_OBJECT, 0);
274
+ }
275
+
276
+ if (dict->list->count >= HPDF_LIMIT_MAX_DICT_ELEMENT) {
277
+ HPDF_PTRACE((" HPDF_Dict_Add exceed limitatin of dict count(%d)\n",
278
+ HPDF_LIMIT_MAX_DICT_ELEMENT));
279
+
280
+ HPDF_Obj_Free (dict->mmgr, obj);
281
+ return HPDF_SetError (dict->error, HPDF_DICT_COUNT_ERR, 0);
282
+ }
283
+
284
+ /* check whether there is an object which has same name */
285
+ element = GetElement (dict, key);
286
+
287
+ if (element) {
288
+ HPDF_Obj_Free (dict->mmgr, element->value);
289
+ element->value = NULL;
290
+ } else {
291
+ element = (HPDF_DictElement)HPDF_GetMem (dict->mmgr,
292
+ sizeof(HPDF_DictElement_Rec));
293
+
294
+ if (!element) {
295
+ /* cannot create element object */
296
+ if (!(header->obj_id & HPDF_OTYPE_INDIRECT))
297
+ HPDF_Obj_Free (dict->mmgr, obj);
298
+
299
+ return HPDF_Error_GetCode (dict->error);
300
+ }
301
+
302
+ HPDF_StrCpy (element->key, key, element->key +
303
+ HPDF_LIMIT_MAX_NAME_LEN + 1);
304
+ element->value = NULL;
305
+
306
+ ret = HPDF_List_Add (dict->list, element);
307
+ if (ret != HPDF_OK) {
308
+ if (!(header->obj_id & HPDF_OTYPE_INDIRECT))
309
+ HPDF_Obj_Free (dict->mmgr, obj);
310
+
311
+ HPDF_FreeMem (dict->mmgr, element);
312
+
313
+ return HPDF_Error_GetCode (dict->error);
314
+ }
315
+ }
316
+
317
+ if (header->obj_id & HPDF_OTYPE_INDIRECT) {
318
+ HPDF_Proxy proxy = HPDF_Proxy_New (dict->mmgr, obj);
319
+
320
+ if (!proxy)
321
+ return HPDF_Error_GetCode (dict->error);
322
+
323
+ element->value = proxy;
324
+ proxy->header.obj_id |= HPDF_OTYPE_DIRECT;
325
+ } else {
326
+ element->value = obj;
327
+ header->obj_id |= HPDF_OTYPE_DIRECT;
328
+ }
329
+
330
+ return ret;
331
+ }
332
+
333
+
334
+ HPDF_STATUS
335
+ HPDF_Dict_AddName (HPDF_Dict dict,
336
+ const char *key,
337
+ const char *value)
338
+ {
339
+ HPDF_Name name = HPDF_Name_New (dict->mmgr, value);
340
+ if (!name)
341
+ return HPDF_Error_GetCode (dict->error);
342
+
343
+ return HPDF_Dict_Add (dict, key, name);
344
+ }
345
+
346
+
347
+ HPDF_STATUS
348
+ HPDF_Dict_AddNumber (HPDF_Dict dict,
349
+ const char *key,
350
+ HPDF_INT32 value)
351
+ {
352
+ HPDF_Number number = HPDF_Number_New (dict->mmgr, value);
353
+
354
+ if (!number)
355
+ return HPDF_Error_GetCode (dict->error);
356
+
357
+ return HPDF_Dict_Add (dict, key, number);
358
+ }
359
+
360
+
361
+ HPDF_STATUS
362
+ HPDF_Dict_AddReal (HPDF_Dict dict,
363
+ const char *key,
364
+ HPDF_REAL value)
365
+ {
366
+ HPDF_Real real = HPDF_Real_New (dict->mmgr, value);
367
+
368
+ if (!real)
369
+ return HPDF_Error_GetCode (dict->error);
370
+
371
+ return HPDF_Dict_Add (dict, key, real);
372
+ }
373
+
374
+
375
+ HPDF_STATUS
376
+ HPDF_Dict_AddBoolean (HPDF_Dict dict,
377
+ const char *key,
378
+ HPDF_BOOL value)
379
+ {
380
+ HPDF_Boolean obj = HPDF_Boolean_New (dict->mmgr, value);
381
+
382
+ if (!obj)
383
+ return HPDF_Error_GetCode (dict->error);
384
+
385
+ return HPDF_Dict_Add (dict, key, obj);
386
+ }
387
+
388
+
389
+ void*
390
+ HPDF_Dict_GetItem (HPDF_Dict dict,
391
+ const char *key,
392
+ HPDF_UINT16 obj_class)
393
+ {
394
+ HPDF_DictElement element = GetElement (dict, key);
395
+ void *obj;
396
+
397
+ if (element && HPDF_StrCmp(key, element->key) == 0) {
398
+ HPDF_Obj_Header *header = (HPDF_Obj_Header *)element->value;
399
+
400
+ if (header->obj_class == HPDF_OCLASS_PROXY) {
401
+ HPDF_Proxy p = element->value;
402
+ header = (HPDF_Obj_Header *)p->obj;
403
+ obj = p->obj;
404
+ } else
405
+ obj = element->value;
406
+
407
+ if ((header->obj_class & HPDF_OCLASS_ANY) != obj_class) {
408
+ HPDF_PTRACE((" HPDF_Dict_GetItem dict=%p key=%s obj_class=0x%08X\n",
409
+ dict, key, (HPDF_UINT)header->obj_class));
410
+ HPDF_SetError (dict->error, HPDF_DICT_ITEM_UNEXPECTED_TYPE, 0);
411
+
412
+ return NULL;
413
+ }
414
+
415
+ return obj;
416
+ }
417
+
418
+ return NULL;
419
+ }
420
+
421
+
422
+ HPDF_DictElement
423
+ GetElement (HPDF_Dict dict,
424
+ const char *key)
425
+ {
426
+ HPDF_UINT i;
427
+
428
+ for (i = 0; i < dict->list->count; i++) {
429
+ HPDF_DictElement element =
430
+ (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
431
+
432
+ if (HPDF_StrCmp (key, element->key) == 0)
433
+ return element;
434
+ }
435
+
436
+ return NULL;
437
+ }
438
+
439
+
440
+ HPDF_STATUS
441
+ HPDF_Dict_RemoveElement (HPDF_Dict dict,
442
+ const char *key)
443
+ {
444
+ HPDF_UINT i;
445
+
446
+ for (i = 0; i < dict->list->count; i++) {
447
+ HPDF_DictElement element =
448
+ (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
449
+
450
+ if (HPDF_StrCmp (key, element->key) == 0) {
451
+ HPDF_List_Remove (dict->list, element);
452
+
453
+ HPDF_Obj_Free (dict->mmgr, element->value);
454
+ HPDF_FreeMem (dict->mmgr, element);
455
+
456
+ return HPDF_OK;
457
+ }
458
+ }
459
+
460
+ return HPDF_DICT_ITEM_NOT_FOUND;
461
+ }
462
+
463
+ const char*
464
+ HPDF_Dict_GetKeyByObj (HPDF_Dict dict,
465
+ void *obj)
466
+ {
467
+ HPDF_UINT i;
468
+
469
+ for (i = 0; i < dict->list->count; i++) {
470
+ HPDF_Obj_Header *header;
471
+ HPDF_DictElement element =
472
+ (HPDF_DictElement)HPDF_List_ItemAt (dict->list, i);
473
+
474
+ header = (HPDF_Obj_Header *)(element->value);
475
+ if (header->obj_class == HPDF_OCLASS_PROXY) {
476
+ HPDF_Proxy p = element->value;
477
+
478
+ if (p->obj == obj)
479
+ return element->key;
480
+ } else {
481
+ if (element->value == obj)
482
+ return element->key;
483
+ }
484
+ }
485
+
486
+ return NULL;
487
+ }
488
+