2 * << Haru Free PDF Library >> -- hpdf_catalog.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_catalog.h"
21 #include "hpdf_pages.h"
23 static const char * const HPDF_PAGE_LAYOUT_NAMES[] = {
34 static const char * const HPDF_PAGE_MODE_NAMES[] = {
46 HPDF_Catalog_New (HPDF_MMgr mmgr,
52 catalog = HPDF_Dict_New (mmgr);
56 catalog->header.obj_class |= HPDF_OSUBCLASS_CATALOG;
58 if (HPDF_Xref_Add (xref, catalog) != HPDF_OK)
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));
73 HPDF_Catalog_GetRoot (HPDF_Catalog catalog)
80 pages = HPDF_Dict_GetItem (catalog, "Pages", HPDF_OCLASS_DICT);
81 if (!pages || pages->header.obj_class != (HPDF_OSUBCLASS_PAGES |
83 HPDF_SetError (catalog->error, HPDF_PAGE_CANNOT_GET_ROOT_PAGES, 0);
90 HPDF_Catalog_GetNames (HPDF_Catalog catalog)
94 return HPDF_Dict_GetItem (catalog, "Names", HPDF_OCLASS_DICT);
99 HPDF_Catalog_SetNames (HPDF_Catalog catalog,
102 return HPDF_Dict_Add (catalog, "Names", dict);
107 HPDF_Catalog_GetPageLayout (HPDF_Catalog catalog)
112 layout = (HPDF_Name)HPDF_Dict_GetItem (catalog, "PageLayout",
115 return HPDF_PAGE_LAYOUT_EOF;
117 while (HPDF_PAGE_LAYOUT_NAMES[i]) {
118 if (HPDF_StrCmp (layout->value, HPDF_PAGE_LAYOUT_NAMES[i]) == 0)
119 return (HPDF_PageLayout)i;
123 return HPDF_PAGE_LAYOUT_EOF;
128 HPDF_Catalog_SetPageLayout (HPDF_Catalog catalog,
129 HPDF_PageLayout layout)
131 return HPDF_Dict_AddName (catalog, "PageLayout",
132 HPDF_PAGE_LAYOUT_NAMES[(HPDF_INT)layout]);
139 HPDF_Catalog_GetPageMode (HPDF_Catalog catalog)
144 mode = (HPDF_Name)HPDF_Dict_GetItem (catalog, "PageMode", HPDF_OCLASS_NAME);
146 return HPDF_PAGE_MODE_USE_NONE;
148 while (HPDF_PAGE_MODE_NAMES[i]) {
149 if (HPDF_StrCmp (mode->value, HPDF_PAGE_MODE_NAMES[i]) == 0)
150 return (HPDF_PageMode)i;
154 return HPDF_PAGE_MODE_USE_NONE;
159 HPDF_Catalog_SetPageMode (HPDF_Catalog catalog,
162 return HPDF_Dict_AddName (catalog, "PageMode",
163 HPDF_PAGE_MODE_NAMES[(HPDF_INT)mode]);
168 HPDF_Catalog_SetOpenAction (HPDF_Catalog catalog,
169 HPDF_Destination open_action)
172 HPDF_Dict_RemoveElement (catalog, "OpenAction");
176 return HPDF_Dict_Add (catalog, "OpenAction", open_action);
181 HPDF_Catalog_Validate (HPDF_Catalog catalog)
186 if (catalog->header.obj_class != (HPDF_OSUBCLASS_CATALOG |
188 HPDF_SetError (catalog->error, HPDF_INVALID_OBJECT, 0);
197 HPDF_Catalog_AddPageLabel (HPDF_Catalog catalog,
199 HPDF_Dict page_label)
203 HPDF_Dict labels = HPDF_Dict_GetItem (catalog, "PageLabels",
206 HPDF_PTRACE ((" HPDF_Catalog_AddPageLabel\n"));
209 labels = HPDF_Dict_New (catalog->mmgr);
212 return catalog->error->error_no;
214 if ((ret = HPDF_Dict_Add (catalog, "PageLabels", labels)) != HPDF_OK)
218 nums = HPDF_Dict_GetItem (labels, "Nums", HPDF_OCLASS_ARRAY);
221 nums = HPDF_Array_New (catalog->mmgr);
224 return catalog->error->error_no;
226 if ((ret = HPDF_Dict_Add (labels, "Nums", nums)) != HPDF_OK)
230 if ((ret = HPDF_Array_AddNumber (nums, page_num)) != HPDF_OK)
233 return HPDF_Array_Add (nums, page_label);
237 HPDF_Catalog_SetViewerPreference (HPDF_Catalog catalog,
241 HPDF_Dict preferences;
243 HPDF_PTRACE ((" HPDF_Catalog_SetViewerPreference\n"));
246 ret = HPDF_Dict_RemoveElement (catalog, "ViewerPreferences");
248 if (ret == HPDF_DICT_ITEM_NOT_FOUND)
254 preferences = HPDF_Dict_New (catalog->mmgr);
256 return catalog->error->error_no;
258 if ((ret = HPDF_Dict_Add (catalog, "ViewerPreferences", preferences))
264 if (value & HPDF_HIDE_TOOLBAR) {
265 if ((ret = HPDF_Dict_AddBoolean (preferences, "HideToolbar",
266 HPDF_TRUE)) != HPDF_OK)
269 if ((ret = HPDF_Dict_RemoveElement (preferences, "HideToolbar")) !=
271 if (ret != HPDF_DICT_ITEM_NOT_FOUND)
275 if (value & HPDF_HIDE_MENUBAR) {
276 if ((ret = HPDF_Dict_AddBoolean (preferences, "HideMenubar",
277 HPDF_TRUE)) != HPDF_OK)
280 if ((ret = HPDF_Dict_RemoveElement (preferences, "HideMenubar")) !=
282 if (ret != HPDF_DICT_ITEM_NOT_FOUND)
286 if (value & HPDF_HIDE_WINDOW_UI) {
287 if ((ret = HPDF_Dict_AddBoolean (preferences, "HideWindowUI",
288 HPDF_TRUE)) != HPDF_OK)
291 if ((ret = HPDF_Dict_RemoveElement (preferences, "HideWindowUI")) !=
293 if (ret != HPDF_DICT_ITEM_NOT_FOUND)
297 if (value & HPDF_FIT_WINDOW) {
298 if ((ret = HPDF_Dict_AddBoolean (preferences, "FitWindow",
299 HPDF_TRUE)) != HPDF_OK)
302 if ((ret = HPDF_Dict_RemoveElement (preferences, "FitWindow")) !=
304 if (ret != HPDF_DICT_ITEM_NOT_FOUND)
308 if (value & HPDF_CENTER_WINDOW) {
309 if ((ret = HPDF_Dict_AddBoolean (preferences, "CenterWindow",
310 HPDF_TRUE)) != HPDF_OK)
313 if ((ret = HPDF_Dict_RemoveElement (preferences, "CenterWindow")) !=
315 if (ret != HPDF_DICT_ITEM_NOT_FOUND)
319 if (value & HPDF_PRINT_SCALING_NONE) {
320 if ((ret = HPDF_Dict_AddName (preferences, "PrintScaling",
324 if ((ret = HPDF_Dict_RemoveElement (preferences, "PrintScaling")) !=
326 if (ret != HPDF_DICT_ITEM_NOT_FOUND)
334 HPDF_Catalog_GetViewerPreference (HPDF_Catalog catalog)
336 HPDF_Dict preferences;
340 HPDF_PTRACE ((" HPDF_Catalog_GetViewerPreference\n"));
342 preferences = (HPDF_Dict)HPDF_Dict_GetItem (catalog, "ViewerPreferences",
348 obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "HideToolbar",
349 HPDF_OCLASS_BOOLEAN);
352 value += HPDF_HIDE_TOOLBAR;
355 obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "HideMenubar",
356 HPDF_OCLASS_BOOLEAN);
359 value += HPDF_HIDE_MENUBAR;
362 obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "HideWindowUI",
363 HPDF_OCLASS_BOOLEAN);
366 value += HPDF_HIDE_WINDOW_UI;
369 obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "FitWindow",
370 HPDF_OCLASS_BOOLEAN);
373 value += HPDF_FIT_WINDOW;
376 obj = (HPDF_Boolean)HPDF_Dict_GetItem (preferences, "CenterWindow",
377 HPDF_OCLASS_BOOLEAN);
380 value += HPDF_CENTER_WINDOW;