2 * << Haru Free PDF Library >> -- hpdf_namedict.c
4 * URL: http://libharu.org
6 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
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.
18 #include "hpdf_conf.h"
19 #include "hpdf_utils.h"
20 #include "hpdf_consts.h"
21 #include "hpdf_namedict.h"
24 #define HPDF_UNUSED(a) ((void)(a))
27 static const char * const HPDF_NAMEDICT_KEYS[] = {
32 HPDF_NameDict_New (HPDF_MMgr mmgr,
37 HPDF_PTRACE((" HPDF_NameDict_New\n"));
39 ndict = HPDF_Dict_New (mmgr);
43 if (HPDF_Xref_Add (xref, ndict) != HPDF_OK)
46 ndict->header.obj_class |= HPDF_OSUBCLASS_NAMEDICT;
52 HPDF_NameDict_GetNameTree (HPDF_NameDict namedict,
57 return HPDF_Dict_GetItem (namedict, HPDF_NAMEDICT_KEYS[key], HPDF_OCLASS_DICT);
61 HPDF_NameDict_SetNameTree (HPDF_NameDict namedict,
65 return HPDF_Dict_Add (namedict, HPDF_NAMEDICT_KEYS[key], ntree);
69 HPDF_NameDict_Validate (HPDF_NameDict namedict)
74 if (namedict->header.obj_class != (HPDF_OSUBCLASS_NAMEDICT |
76 HPDF_SetError (namedict->error, HPDF_INVALID_OBJECT, 0);
84 /*------- NameTree -------*/
87 HPDF_NameTree_New (HPDF_MMgr mmgr,
90 HPDF_STATUS ret = HPDF_OK;
94 HPDF_PTRACE((" HPDF_NameTree_New\n"));
96 ntree = HPDF_Dict_New (mmgr);
100 if (HPDF_Xref_Add (xref, ntree) != HPDF_OK)
103 ntree->header.obj_class |= HPDF_OSUBCLASS_NAMETREE;
105 items = HPDF_Array_New (mmgr);
109 ret += HPDF_Dict_Add (ntree, "Names", items);
117 HPDF_NameTree_Add (HPDF_NameTree tree,
122 HPDF_INT32 i, icount;
125 return HPDF_INVALID_PARAMETER;
127 items = HPDF_Dict_GetItem (tree, "Names", HPDF_OCLASS_ARRAY);
129 return HPDF_INVALID_OBJECT;
131 /* "The keys shall be sorted in lexical order" -- 7.9.6, Name Trees.
132 * Since we store keys sorted, it's best to do a linear insertion sort
133 * Find the first element larger than 'key', and insert 'key' and then
134 * 'obj' into the items. */
136 icount = HPDF_Array_Items(items);
138 /* If we're larger than the last element, append */
140 HPDF_String last = HPDF_Array_GetItem(items, icount - 2, HPDF_OCLASS_STRING);
142 if (HPDF_String_Cmp(name, last) > 0) {
143 HPDF_Array_Add(items, name);
144 HPDF_Array_Add(items, obj);
149 /* Walk backwards through the list until we're smaller than an element=
150 * That's the element to insert in front of. */
151 for (i = icount - 4; i >= 0; i -= 2) {
152 HPDF_String elem = HPDF_Array_GetItem(items, i, HPDF_OCLASS_STRING);
154 if (i == 0 || HPDF_String_Cmp(name, elem) < 0) {
155 HPDF_Array_Insert(items, elem, name);
156 HPDF_Array_Insert(items, elem, obj);
161 /* Items list is empty */
162 HPDF_Array_Add(items, name);
163 HPDF_Array_Add(items, obj);
168 HPDF_NameTree_Validate (HPDF_NameTree nametree)
173 if (nametree->header.obj_class != (HPDF_OSUBCLASS_NAMETREE |
175 HPDF_SetError (nametree->error, HPDF_INVALID_OBJECT, 0);
183 /*------- EmbeddedFile -------*/
186 HPDF_EmbeddedFile_New (HPDF_MMgr mmgr,
190 HPDF_STATUS ret = HPDF_OK;
191 HPDF_Dict ef; /* the dictionary for the embedded file: /Type /EF */
192 HPDF_String name; /* the name of the file: /F (name) */
193 HPDF_Dict eff; /* ef has an /EF <<blah>> key - this is it */
194 HPDF_Dict filestream; /* the stream that /EF <</F _ _ R>> refers to */
197 ef = HPDF_Dict_New (mmgr);
200 if (HPDF_Xref_Add (xref, ef) != HPDF_OK)
203 filestream = HPDF_DictStream_New (mmgr, xref);
206 stream = HPDF_FileReader_New (mmgr, file);
209 HPDF_Stream_Free(filestream->stream);
210 filestream->stream = stream;
211 filestream->filter = HPDF_STREAM_FILTER_FLATE_DECODE;
213 eff = HPDF_Dict_New (mmgr);
217 name = HPDF_String_New (mmgr, file, NULL);
221 ret += HPDF_Dict_AddName (ef, "Type", "F");
222 ret += HPDF_Dict_Add (ef, "F", name);
223 ret += HPDF_Dict_Add (ef, "EF", eff);
224 ret += HPDF_Dict_Add (eff, "F", filestream);
233 HPDF_EmbeddedFile_Validate (HPDF_EmbeddedFile emfile)
235 HPDF_UNUSED (emfile);