OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / src / hpdf_catalog.c
1 /*
2  * << Haru Free PDF Library >> -- hpdf_catalog.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-2009 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 #include "hpdf_conf.h"
19 #include "hpdf_utils.h"
20 #include "hpdf_catalog.h"
21 #include "hpdf_pages.h"
22
23 static const char * const HPDF_PAGE_LAYOUT_NAMES[] = {
24                         "SinglePage",
25                         "OneColumn",
26                         "TwoColumnLeft",
27                         "TwoColumnRight",
28                         "TwoPageLeft",
29                         "TwoPageRight",
30                         NULL
31 };
32
33
34 static const char * const HPDF_PAGE_MODE_NAMES[] = {
35                         "UseNone",
36                         "UseOutlines",
37                         "UseThumbs",
38                         "FullScreen",
39                         "UseOC",
40                         "UseAttachments",
41                         NULL
42 };
43
44
45 HPDF_Catalog
46 HPDF_Catalog_New  (HPDF_MMgr  mmgr,
47                    HPDF_Xref  xref)
48 {
49     HPDF_Catalog catalog;
50     HPDF_STATUS ret = 0;
51
52     catalog = HPDF_Dict_New (mmgr);
53     if (!catalog)
54         return NULL;
55
56     catalog->header.obj_class |= HPDF_OSUBCLASS_CATALOG;
57
58     if (HPDF_Xref_Add (xref, catalog) != HPDF_OK)
59         return NULL;
60
61     /* add requiered elements */
62     ret += HPDF_Dict_AddName (catalog, "Type", "Catalog");
63     ret += HPDF_Dict_Add (catalog, "Pages", HPDF_Pages_New (mmgr, NULL, xref));
64
65     if (ret != HPDF_OK)
66         return NULL;
67
68     return catalog;
69 }
70
71
72 HPDF_Pages
73 HPDF_Catalog_GetRoot  (HPDF_Catalog  catalog)
74 {
75     HPDF_Dict pages;
76
77     if (!catalog)
78         return NULL;
79
80     pages = HPDF_Dict_GetItem (catalog, "Pages", HPDF_OCLASS_DICT);
81     if (!pages || pages->header.obj_class != (HPDF_OSUBCLASS_PAGES |
82                 HPDF_OCLASS_DICT))
83         HPDF_SetError (catalog->error, HPDF_PAGE_CANNOT_GET_ROOT_PAGES, 0);
84
85     return pages;
86 }
87
88
89 HPDF_NameDict
90 HPDF_Catalog_GetNames  (HPDF_Catalog catalog)
91 {
92     if (!catalog)
93         return NULL;
94     return HPDF_Dict_GetItem (catalog, "Names", HPDF_OCLASS_DICT);
95 }
96
97
98 HPDF_STATUS
99 HPDF_Catalog_SetNames  (HPDF_Catalog catalog,
100                         HPDF_NameDict dict)
101 {
102     return HPDF_Dict_Add (catalog, "Names", dict);
103 }
104
105
106 HPDF_PageLayout
107 HPDF_Catalog_GetPageLayout  (HPDF_Catalog  catalog)
108 {
109     HPDF_Name layout;
110     HPDF_UINT i = 0;
111
112     layout = (HPDF_Name)HPDF_Dict_GetItem (catalog, "PageLayout",
113             HPDF_OCLASS_NAME);
114     if (!layout)
115         return HPDF_PAGE_LAYOUT_EOF;
116
117     while (HPDF_PAGE_LAYOUT_NAMES[i]) {
118         if (HPDF_StrCmp (layout->value, HPDF_PAGE_LAYOUT_NAMES[i]) == 0)
119             return (HPDF_PageLayout)i;
120         i++;
121     }
122
123     return HPDF_PAGE_LAYOUT_EOF;
124 }
125
126
127 HPDF_STATUS
128 HPDF_Catalog_SetPageLayout  (HPDF_Catalog      catalog,
129                              HPDF_PageLayout   layout)
130 {
131     return HPDF_Dict_AddName (catalog, "PageLayout",
132                     HPDF_PAGE_LAYOUT_NAMES[(HPDF_INT)layout]);
133 }
134
135
136
137
138 HPDF_PageMode
139 HPDF_Catalog_GetPageMode  (HPDF_Catalog  catalog)
140 {
141     HPDF_Name mode;
142     HPDF_UINT i = 0;
143
144     mode = (HPDF_Name)HPDF_Dict_GetItem (catalog, "PageMode", HPDF_OCLASS_NAME);
145     if (!mode)
146         return HPDF_PAGE_MODE_USE_NONE;
147
148     while (HPDF_PAGE_MODE_NAMES[i]) {
149         if (HPDF_StrCmp (mode->value, HPDF_PAGE_MODE_NAMES[i]) == 0)
150             return (HPDF_PageMode)i;
151         i++;
152     }
153
154     return HPDF_PAGE_MODE_USE_NONE;
155 }
156
157
158 HPDF_STATUS
159 HPDF_Catalog_SetPageMode  (HPDF_Catalog   catalog,
160                            HPDF_PageMode  mode)
161 {
162     return HPDF_Dict_AddName (catalog, "PageMode",
163                     HPDF_PAGE_MODE_NAMES[(HPDF_INT)mode]);
164 }
165
166
167 HPDF_STATUS
168 HPDF_Catalog_SetOpenAction  (HPDF_Catalog       catalog,
169                              HPDF_Destination   open_action)
170 {
171     if (!open_action) {
172         HPDF_Dict_RemoveElement (catalog, "OpenAction");
173         return HPDF_OK;
174     }
175
176     return HPDF_Dict_Add (catalog, "OpenAction", open_action);
177 }
178
179
180 HPDF_BOOL
181 HPDF_Catalog_Validate  (HPDF_Catalog   catalog)
182 {
183     if (!catalog)
184         return HPDF_FALSE;
185
186     if (catalog->header.obj_class != (HPDF_OSUBCLASS_CATALOG |
187                 HPDF_OCLASS_DICT)) {
188         HPDF_SetError (catalog->error, HPDF_INVALID_OBJECT, 0);
189         return HPDF_FALSE;
190     }
191
192     return HPDF_TRUE;
193 }
194
195
196 HPDF_STATUS
197 HPDF_Catalog_AddPageLabel  (HPDF_Catalog   catalog,
198                             HPDF_UINT      page_num,
199                             HPDF_Dict      page_label)
200 {
201     HPDF_STATUS ret;
202     HPDF_Array nums;
203     HPDF_Dict labels = HPDF_Dict_GetItem (catalog, "PageLabels",
204         HPDF_OCLASS_DICT);
205
206     HPDF_PTRACE ((" HPDF_Catalog_AddPageLabel\n"));
207
208     if (!labels) {
209         labels = HPDF_Dict_New (catalog->mmgr);
210
211         if (!labels)
212             return catalog->error->error_no;
213
214         if ((ret = HPDF_Dict_Add (catalog, "PageLabels", labels)) != HPDF_OK)
215             return ret;
216     }
217
218     nums = HPDF_Dict_GetItem (labels, "Nums", HPDF_OCLASS_ARRAY);
219
220     if (!nums) {
221         nums = HPDF_Array_New (catalog->mmgr);
222
223         if (!nums)
224             return catalog->error->error_no;
225
226         if ((ret = HPDF_Dict_Add (labels, "Nums", nums)) != HPDF_OK)
227             return ret;
228     }
229
230     if ((ret = HPDF_Array_AddNumber (nums, page_num)) != HPDF_OK)
231         return ret;
232
233     return HPDF_Array_Add (nums, page_label);
234 }
235
236 HPDF_STATUS
237 HPDF_Catalog_SetViewerPreference  (HPDF_Catalog   catalog,
238                                    HPDF_UINT      value)
239 {
240     HPDF_STATUS ret;
241     HPDF_Dict preferences;
242
243     HPDF_PTRACE ((" HPDF_Catalog_SetViewerPreference\n"));
244
245     if (!value) {
246         ret = HPDF_Dict_RemoveElement (catalog, "ViewerPreferences");
247
248         if (ret == HPDF_DICT_ITEM_NOT_FOUND)
249             ret = HPDF_OK;
250
251         return ret;
252     }
253
254     preferences = HPDF_Dict_New (catalog->mmgr);
255     if (!preferences)
256         return catalog->error->error_no;
257
258     if ((ret = HPDF_Dict_Add (catalog, "ViewerPreferences", preferences))
259             != HPDF_OK)
260         return ret;
261
262     /*  */
263
264     if (value & HPDF_HIDE_TOOLBAR) {
265         if ((ret = HPDF_Dict_AddBoolean (preferences, "HideToolbar",
266                 HPDF_TRUE)) != HPDF_OK)
267             return ret;
268     } else {
269         if ((ret = HPDF_Dict_RemoveElement (preferences, "HideToolbar")) !=
270                 HPDF_OK)
271             if (ret != HPDF_DICT_ITEM_NOT_FOUND)
272                 return ret;
273     }
274
275     if (value & HPDF_HIDE_MENUBAR) {
276         if ((ret = HPDF_Dict_AddBoolean (preferences, "HideMenubar",
277                 HPDF_TRUE)) != HPDF_OK)
278             return ret;
279     } else {
280         if ((ret = HPDF_Dict_RemoveElement (preferences, "HideMenubar")) !=
281                 HPDF_OK)
282             if (ret != HPDF_DICT_ITEM_NOT_FOUND)
283                 return ret;
284     }
285
286     if (value & HPDF_HIDE_WINDOW_UI) {
287         if ((ret = HPDF_Dict_AddBoolean (preferences, "HideWindowUI",
288                 HPDF_TRUE)) != HPDF_OK)
289             return ret;
290     } else {
291         if ((ret = HPDF_Dict_RemoveElement (preferences, "HideWindowUI")) !=
292                 HPDF_OK)
293             if (ret != HPDF_DICT_ITEM_NOT_FOUND)
294                 return ret;
295     }
296
297     if (value & HPDF_FIT_WINDOW) {
298         if ((ret = HPDF_Dict_AddBoolean (preferences, "FitWindow",
299                 HPDF_TRUE)) != HPDF_OK)
300             return ret;
301     } else {
302         if ((ret = HPDF_Dict_RemoveElement (preferences, "FitWindow")) !=
303                 HPDF_OK)
304             if (ret != HPDF_DICT_ITEM_NOT_FOUND)
305                 return ret;
306     }
307
308     if (value & HPDF_CENTER_WINDOW) {
309         if ((ret = HPDF_Dict_AddBoolean (preferences, "CenterWindow",
310                 HPDF_TRUE)) != HPDF_OK)
311             return ret;
312     } else {
313         if ((ret = HPDF_Dict_RemoveElement (preferences, "CenterWindow")) !=
314                 HPDF_OK)
315             if (ret != HPDF_DICT_ITEM_NOT_FOUND)
316                 return ret;
317     }
318
319     if (value & HPDF_PRINT_SCALING_NONE) {
320         if ((ret = HPDF_Dict_AddName (preferences, "PrintScaling",
321                 "None")) != HPDF_OK)
322             return ret;
323     } else {
324         if ((ret = HPDF_Dict_RemoveElement (preferences, "PrintScaling")) !=
325                 HPDF_OK)
326             if (ret != HPDF_DICT_ITEM_NOT_FOUND)
327                 return ret;
328     }
329
330     return HPDF_OK;
331 }
332
333 HPDF_UINT
334 HPDF_Catalog_GetViewerPreference  (HPDF_Catalog   catalog)
335 {
336     HPDF_Dict preferences;
337     HPDF_UINT value = 0;
338     HPDF_Boolean obj;
339
340     HPDF_PTRACE ((" HPDF_Catalog_GetViewerPreference\n"));
341
342     preferences = (HPDF_Dict)HPDF_Dict_GetItem (catalog, "ViewerPreferences",
343             HPDF_OCLASS_DICT);
344
345     if (!preferences)
346         return 0;
347
348     obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "HideToolbar",
349             HPDF_OCLASS_BOOLEAN);
350     if (obj) {
351         if (obj->value)
352             value += HPDF_HIDE_TOOLBAR;
353     }
354
355     obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "HideMenubar",
356             HPDF_OCLASS_BOOLEAN);
357     if (obj) {
358         if (obj->value)
359             value += HPDF_HIDE_MENUBAR;
360     }
361
362     obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "HideWindowUI",
363             HPDF_OCLASS_BOOLEAN);
364     if (obj) {
365         if (obj->value)
366             value += HPDF_HIDE_WINDOW_UI;
367     }
368
369     obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "FitWindow",
370             HPDF_OCLASS_BOOLEAN);
371     if (obj) {
372         if (obj->value)
373             value += HPDF_FIT_WINDOW;
374     }
375
376     obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "CenterWindow",
377             HPDF_OCLASS_BOOLEAN);
378     if (obj) {
379         if (obj->value)
380             value += HPDF_CENTER_WINDOW;
381     }
382
383     return value;
384 }
385