1/*
2 * << Haru Free PDF Library >> -- hpdf_pages.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.h"
21#include "hpdf_annotation.h"
22#include "hpdf_destination.h"
23#include "hpdf_3dmeasure.h"
24#include "hpdf_exdata.h"
25#include "hpdf_u3d.h"
26
27/*----------------------------------------------------------------------------*/
28
29typedef struct _HPDF_PageSizeValue {
30 HPDF_REAL x;
31 HPDF_REAL y;
32} HPDF_PageSizeValue;
33
34static const HPDF_PageSizeValue HPDF_PREDEFINED_PAGE_SIZES[] = {
35 {612, 792}, /* HPDF_PAGE_SIZE_LETTER */
36 {612, 1008}, /* HPDF_PAGE_SIZE_LEGAL */
37 {(HPDF_REAL)841.89, (HPDF_REAL)1190.551}, /* HPDF_PAGE_SIZE_A3 */
38 {(HPDF_REAL)595.276, (HPDF_REAL)841.89}, /* HPDF_PAGE_SIZE_A4 */
39 {(HPDF_REAL)419.528, (HPDF_REAL)595.276}, /* HPDF_PAGE_SIZE_A5 */
40 {(HPDF_REAL)708.661, (HPDF_REAL)1000.63}, /* HPDF_PAGE_SIZE_B4 */
41 {(HPDF_REAL)498.898, (HPDF_REAL)708.661}, /* HPDF_PAGE_SIZE_B5 */
42 {522, 756}, /* HPDF_PAGE_SIZE_EXECUTIVE */
43 {288, 432}, /* HPDF_PAGE_SIZE_US4x6 */
44 {288, 576}, /* HPDF_PAGE_SIZE_US4x8 */
45 {360, 504}, /* HPDF_PAGE_SIZE_US5x7 */
46 {297, 684} /* HPDF_PAGE_SIZE_COMM10 */
47};
48
49
50static const HPDF_RGBColor DEF_RGB_COLOR = {0, 0, 0};
51
52static const HPDF_CMYKColor DEF_CMYK_COLOR = {0, 0, 0, 0};
53
54
55static HPDF_STATUS
56Pages_BeforeWrite (HPDF_Dict obj);
57
58
59static HPDF_STATUS
60Page_BeforeWrite (HPDF_Dict obj);
61
62
63static void
64Page_OnFree (HPDF_Dict obj);
65
66
67static HPDF_STATUS
68AddResource (HPDF_Page page);
69
70
71static HPDF_STATUS
72AddAnnotation (HPDF_Page page,
73 HPDF_Annotation annot);
74
75
76
77static HPDF_UINT
78GetPageCount (HPDF_Dict pages);
79
80static const char * const HPDF_INHERITABLE_ENTRIES[5] = {
81 "Resources",
82 "MediaBox",
83 "CropBox",
84 "Rotate",
85 NULL
86 };
87
88
89/*----------------------------------------------------------------------------*/
90/*----- HPDF_Pages -----------------------------------------------------------*/
91
92HPDF_Pages
93HPDF_Pages_New (HPDF_MMgr mmgr,
94 HPDF_Pages parent,
95 HPDF_Xref xref)
96{
97 HPDF_STATUS ret = HPDF_OK;
98 HPDF_Pages pages;
99
100
101 HPDF_PTRACE((" HPDF_Pages_New\n"));
102
103 pages = HPDF_Dict_New (mmgr);
104 if (!pages)
105 return NULL;
106
107 pages->header.obj_class |= HPDF_OSUBCLASS_PAGES;
108 pages->before_write_fn = Pages_BeforeWrite;
109
110 if (HPDF_Xref_Add (xref, pages) != HPDF_OK)
111 return NULL;
112
113 /* add requiered elements */
114 ret += HPDF_Dict_AddName (pages, "Type", "Pages");
115 ret += HPDF_Dict_Add (pages, "Kids", HPDF_Array_New (pages->mmgr));
116 ret += HPDF_Dict_Add (pages, "Count", HPDF_Number_New (pages->mmgr, 0));
117
118 if (ret == HPDF_OK && parent)
119 ret += HPDF_Pages_AddKids (parent, pages);
120
121 if (ret != HPDF_OK)
122 return NULL;
123
124 return pages;
125}
126
127
128HPDF_STATUS
129HPDF_Pages_AddKids (HPDF_Pages parent,
130 HPDF_Dict kid)
131{
132 HPDF_Array kids;
133 HPDF_STATUS ret;
134
135 HPDF_PTRACE((" HPDF_Pages_AddKids\n"));
136
137 if (HPDF_Dict_GetItem (kid, "Parent", HPDF_OCLASS_DICT))
138 return HPDF_SetError (parent->error, HPDF_PAGE_CANNOT_SET_PARENT, 0);
139
140 if ((ret = HPDF_Dict_Add (kid, "Parent", parent)) != HPDF_OK)
141 return ret;
142
143 kids = (HPDF_Array )HPDF_Dict_GetItem (parent, "Kids", HPDF_OCLASS_ARRAY);
144 if (!kids)
145 return HPDF_SetError (parent->error, HPDF_PAGES_MISSING_KIDS_ENTRY, 0);
146
147 if (kid->header.obj_class == (HPDF_OCLASS_DICT | HPDF_OSUBCLASS_PAGE)) {
148 HPDF_PageAttr attr = (HPDF_PageAttr)kid->attr;
149
150 attr->parent = parent;
151 }
152
153 return HPDF_Array_Add (kids, kid);
154}
155
156
157HPDF_STATUS
158HPDF_Page_InsertBefore (HPDF_Page page,
159 HPDF_Page target)
160{
161 HPDF_Page parent;
162 HPDF_Array kids;
163 HPDF_STATUS ret;
164 HPDF_PageAttr attr;
165
166 HPDF_PTRACE((" HPDF_Page_InsertBefore\n"));
167
168 if (!target)
169 return HPDF_INVALID_PARAMETER;
170
171 attr = (HPDF_PageAttr )target->attr;
172 parent = attr->parent;
173
174 if (!parent)
175 return HPDF_PAGE_CANNOT_SET_PARENT;
176
177 if (HPDF_Dict_GetItem (page, "Parent", HPDF_OCLASS_DICT))
178 return HPDF_SetError (parent->error, HPDF_PAGE_CANNOT_SET_PARENT, 0);
179
180 if ((ret = HPDF_Dict_Add (page, "Parent", parent)) != HPDF_OK)
181 return ret;
182
183 kids = (HPDF_Array )HPDF_Dict_GetItem (parent, "Kids", HPDF_OCLASS_ARRAY);
184 if (!kids)
185 return HPDF_SetError (parent->error, HPDF_PAGES_MISSING_KIDS_ENTRY, 0);
186
187 attr = (HPDF_PageAttr)page->attr;
188 attr->parent = parent;
189
190 return HPDF_Array_Insert (kids, target, page);
191}
192
193
194HPDF_STATUS
195Pages_BeforeWrite (HPDF_Dict obj)
196{
197 HPDF_Array kids = (HPDF_Array )HPDF_Dict_GetItem (obj, "Kids",
198 HPDF_OCLASS_ARRAY);
199 HPDF_Number count = (HPDF_Number)HPDF_Dict_GetItem (obj, "Count",
200 HPDF_OCLASS_NUMBER);
201 HPDF_STATUS ret;
202
203 HPDF_PTRACE((" HPDF_Pages_BeforeWrite\n"));
204
205 if (!kids)
206 return HPDF_SetError (obj->error, HPDF_PAGES_MISSING_KIDS_ENTRY, 0);
207
208 if (count)
209 count->value = GetPageCount (obj);
210 else {
211 count = HPDF_Number_New (obj->mmgr, GetPageCount (obj));
212 if (!count)
213 return HPDF_Error_GetCode (obj->error);
214
215 if ((ret = HPDF_Dict_Add (obj, "Count", count)) != HPDF_OK)
216 return ret;
217 }
218
219 return HPDF_OK;
220}
221
222
223static HPDF_STATUS
224Page_BeforeWrite (HPDF_Dict obj)
225{
226 HPDF_STATUS ret;
227 HPDF_Page page = (HPDF_Page)obj;
228 HPDF_PageAttr attr = (HPDF_PageAttr)obj->attr;
229
230 HPDF_PTRACE((" HPDF_Page_BeforeWrite\n"));
231
232 if (attr->gmode == HPDF_GMODE_PATH_OBJECT) {
233 HPDF_PTRACE((" HPDF_Page_BeforeWrite warning path object is not"
234 " end\n"));
235
236 if ((ret = HPDF_Page_EndPath (page)) != HPDF_OK)
237 return ret;
238 }
239
240 if (attr->gmode == HPDF_GMODE_TEXT_OBJECT) {
241 HPDF_PTRACE((" HPDF_Page_BeforeWrite warning text block is not end\n"));
242
243 if ((ret = HPDF_Page_EndText (page)) != HPDF_OK)
244 return ret;
245 }
246
247 if (attr->gstate)
248 while (attr->gstate->prev) {
249 if ((ret = HPDF_Page_GRestore (page)) != HPDF_OK)
250 return ret;
251 }
252
253 return HPDF_OK;
254}
255
256
257static HPDF_UINT
258GetPageCount (HPDF_Dict pages)
259{
260 HPDF_UINT i;
261 HPDF_UINT count = 0;
262 HPDF_Array kids = (HPDF_Array)HPDF_Dict_GetItem (pages, "Kids",
263 HPDF_OCLASS_ARRAY);
264
265 HPDF_PTRACE((" GetPageCount\n"));
266
267 if (!kids)
268 return 0;
269
270 for (i = 0; i < kids->list->count; i++) {
271 void *obj = HPDF_Array_GetItem (kids, i, HPDF_OCLASS_DICT);
272 HPDF_Obj_Header *header = (HPDF_Obj_Header *)obj;
273
274 if (header->obj_class == (HPDF_OCLASS_DICT | HPDF_OSUBCLASS_PAGES))
275 count += GetPageCount ((HPDF_Dict)obj);
276 else if (header->obj_class == (HPDF_OCLASS_DICT | HPDF_OSUBCLASS_PAGE))
277 count += 1;
278 }
279
280 return count;
281}
282
283
284HPDF_BOOL
285HPDF_Pages_Validate (HPDF_Pages pages)
286{
287 HPDF_Obj_Header *header = (HPDF_Obj_Header *)pages;
288
289 HPDF_PTRACE((" HPDF_Pages_Validate\n"));
290
291 if (!pages || header->obj_class != (HPDF_OCLASS_DICT |
292 HPDF_OSUBCLASS_PAGES))
293 return HPDF_FALSE;
294
295 return HPDF_TRUE;
296}
297
298
299/*----------------------------------------------------------------------------*/
300/*----- HPDF_Page ------------------------------------------------------------*/
301
302
303HPDF_Page
304HPDF_Page_New (HPDF_MMgr mmgr,
305 HPDF_Xref xref)
306{
307 HPDF_STATUS ret;
308 HPDF_PageAttr attr;
309 HPDF_Page page;
310
311 HPDF_PTRACE((" HPDF_Page_New\n"));
312
313 page = HPDF_Dict_New (mmgr);
314 if (!page)
315 return NULL;
316
317 page->header.obj_class |= HPDF_OSUBCLASS_PAGE;
318 page->free_fn = Page_OnFree;
319 page->before_write_fn = Page_BeforeWrite;
320
321 attr = HPDF_GetMem (page->mmgr, sizeof(HPDF_PageAttr_Rec));
322 if (!attr) {
323 HPDF_Dict_Free (page);
324 return NULL;
325 }
326
327 page->attr = attr;
328 HPDF_MemSet (attr, 0, sizeof(HPDF_PageAttr_Rec));
329 attr->gmode = HPDF_GMODE_PAGE_DESCRIPTION;
330 attr->cur_pos = HPDF_ToPoint (0, 0);
331 attr->text_pos = HPDF_ToPoint (0, 0);
332
333 ret = HPDF_Xref_Add (xref, page);
334 if (ret != HPDF_OK)
335 return NULL;
336
337 attr->gstate = HPDF_GState_New (page->mmgr, NULL);
338 attr->contents = HPDF_DictStream_New (page->mmgr, xref);
339
340 if (!attr->gstate || !attr->contents)
341 return NULL;
342
343 attr->stream = attr->contents->stream;
344 attr->xref = xref;
345
346 /* add requiered elements */
347 ret += HPDF_Dict_AddName (page, "Type", "Page");
348 ret += HPDF_Dict_Add (page, "MediaBox", HPDF_Box_Array_New (page->mmgr,
349 HPDF_ToBox (0, 0, (HPDF_INT16)(HPDF_DEF_PAGE_WIDTH), (HPDF_INT16)(HPDF_DEF_PAGE_HEIGHT))));
350 ret += HPDF_Dict_Add (page, "Contents", attr->contents);
351
352 ret += AddResource (page);
353
354 if (ret != HPDF_OK)
355 return NULL;
356
357 return page;
358}
359
360
361
362
363static void
364Page_OnFree (HPDF_Dict obj)
365{
366 HPDF_PageAttr attr = (HPDF_PageAttr)obj->attr;
367
368 HPDF_PTRACE((" HPDF_Page_OnFree\n"));
369
370 if (attr) {
371 if (attr->gstate)
372 HPDF_GState_Free (obj->mmgr, attr->gstate);
373
374 HPDF_FreeMem (obj->mmgr, attr);
375 }
376}
377
378
379HPDF_STATUS
380HPDF_Page_CheckState (HPDF_Page page,
381 HPDF_UINT mode)
382{
383 if (!page)
384 return HPDF_INVALID_OBJECT;
385
386 if (page->header.obj_class != (HPDF_OSUBCLASS_PAGE | HPDF_OCLASS_DICT))
387 return HPDF_INVALID_PAGE;
388
389 if (!(((HPDF_PageAttr)page->attr)->gmode & mode))
390 return HPDF_RaiseError (page->error, HPDF_PAGE_INVALID_GMODE, 0);
391
392 return HPDF_OK;
393}
394
395
396void*
397HPDF_Page_GetInheritableItem (HPDF_Page page,
398 const char *key,
399 HPDF_UINT16 obj_class)
400{
401 HPDF_BOOL chk = HPDF_FALSE;
402 HPDF_INT i = 0;
403 void * obj;
404
405 HPDF_PTRACE((" HPDF_Page_GetInheritableItem\n"));
406
407 /* check whether the specified key is valid */
408 while (HPDF_INHERITABLE_ENTRIES[i]) {
409 if (HPDF_StrCmp (key, HPDF_INHERITABLE_ENTRIES[i]) == 0) {
410 chk = HPDF_TRUE;
411 break;
412 }
413 i++;
414 }
415
416 /* the key is not inheritable */
417 if (chk != HPDF_TRUE) {
418 HPDF_SetError (page->error, HPDF_INVALID_PARAMETER, 0);
419 return NULL;
420 }
421
422 obj = HPDF_Dict_GetItem (page, key, obj_class);
423
424 /* if resources of the object is NULL, search resources of parent
425 * pages recursivly
426 */
427 if (!obj) {
428 HPDF_Pages pages = HPDF_Dict_GetItem (page, "Parent", HPDF_OCLASS_DICT);
429 while (pages) {
430 obj = HPDF_Dict_GetItem (page, key, obj_class);
431
432 if (obj)
433 break;
434
435 pages = HPDF_Dict_GetItem (pages, "Parent", HPDF_OCLASS_DICT);
436 }
437 }
438
439 return obj;
440}
441
442
443HPDF_STATUS
444AddResource (HPDF_Page page)
445{
446 HPDF_STATUS ret = HPDF_OK;
447 HPDF_Dict resource;
448 HPDF_Array procset;
449
450 HPDF_PTRACE((" HPDF_Page_AddResource\n"));
451
452 resource = HPDF_Dict_New (page->mmgr);
453 if (!resource)
454 return HPDF_Error_GetCode (page->error);
455
456 /* althoth ProcSet-entry is obsolete, add it to resouce for
457 * compatibility
458 */
459
460 ret += HPDF_Dict_Add (page, "Resources", resource);
461
462 procset = HPDF_Array_New (page->mmgr);
463 if (!procset)
464 return HPDF_Error_GetCode (page->error);
465
466 if (HPDF_Dict_Add (resource, "ProcSet", procset) != HPDF_OK)
467 return HPDF_Error_GetCode (resource->error);
468
469 ret += HPDF_Array_Add (procset, HPDF_Name_New (page->mmgr, "PDF"));
470 ret += HPDF_Array_Add (procset, HPDF_Name_New (page->mmgr, "Text"));
471 ret += HPDF_Array_Add (procset, HPDF_Name_New (page->mmgr, "ImageB"));
472 ret += HPDF_Array_Add (procset, HPDF_Name_New (page->mmgr, "ImageC"));
473 ret += HPDF_Array_Add (procset, HPDF_Name_New (page->mmgr, "ImageI"));
474
475 if (ret != HPDF_OK)
476 return HPDF_Error_GetCode (procset->error);
477
478 return HPDF_OK;
479}
480
481
482const char*
483HPDF_Page_GetLocalFontName (HPDF_Page page,
484 HPDF_Font font)
485{
486 HPDF_PageAttr attr = (HPDF_PageAttr )page->attr;
487 const char *key;
488
489 HPDF_PTRACE((" HPDF_Page_GetLocalFontName\n"));
490
491 /* whether check font-resource exists. when it does not exists,
492 * create font-resource
493 * 2006.07.21 Fixed a problem which may cause a memory leak.
494 */
495 if (!attr->fonts) {
496 HPDF_Dict resources;
497 HPDF_Dict fonts;
498
499 resources = HPDF_Page_GetInheritableItem (page, "Resources",
500 HPDF_OCLASS_DICT);
501 if (!resources)
502 return NULL;
503
504 fonts = HPDF_Dict_New (page->mmgr);
505 if (!fonts)
506 return NULL;
507
508 if (HPDF_Dict_Add (resources, "Font", fonts) != HPDF_OK)
509 return NULL;
510
511 attr->fonts = fonts;
512 }
513
514 /* search font-object from font-resource */
515 key = HPDF_Dict_GetKeyByObj (attr->fonts, font);
516 if (!key) {
517 /* if the font is not resisterd in font-resource, register font to
518 * font-resource.
519 */
520 char fontName[HPDF_LIMIT_MAX_NAME_LEN + 1];
521 char *ptr;
522 char *end_ptr = fontName + HPDF_LIMIT_MAX_NAME_LEN;
523
524 ptr = (char *)HPDF_StrCpy (fontName, "F", end_ptr);
525 HPDF_IToA (ptr, attr->fonts->list->count + 1, end_ptr);
526
527 if (HPDF_Dict_Add (attr->fonts, fontName, font) != HPDF_OK)
528 return NULL;
529
530 key = HPDF_Dict_GetKeyByObj (attr->fonts, font);
531 }
532
533 return key;
534}
535
536
537HPDF_Box
538HPDF_Page_GetMediaBox (HPDF_Page page)
539{
540 HPDF_Box media_box = {0, 0, 0, 0};
541
542 HPDF_PTRACE((" HPDF_Page_GetMediaBox\n"));
543
544 if (HPDF_Page_Validate (page)) {
545 HPDF_Array array = HPDF_Page_GetInheritableItem (page, "MediaBox",
546 HPDF_OCLASS_ARRAY);
547
548 if (array) {
549 HPDF_Real r;
550
551 r = HPDF_Array_GetItem (array, 0, HPDF_OCLASS_REAL);
552 if (r)
553 media_box.left = r->value;
554
555 r = HPDF_Array_GetItem (array, 1, HPDF_OCLASS_REAL);
556 if (r)
557 media_box.bottom = r->value;
558
559 r = HPDF_Array_GetItem (array, 2, HPDF_OCLASS_REAL);
560 if (r)
561 media_box.right = r->value;
562
563 r = HPDF_Array_GetItem (array, 3, HPDF_OCLASS_REAL);
564 if (r)
565 media_box.top = r->value;
566
567 HPDF_CheckError (page->error);
568 } else HPDF_RaiseError (page->error, HPDF_PAGE_CANNOT_FIND_OBJECT, 0);
569 }
570
571 return media_box;
572}
573
574
575const char*
576HPDF_Page_GetXObjectName (HPDF_Page page,
577 HPDF_XObject xobj)
578{
579 HPDF_PageAttr attr = (HPDF_PageAttr )page->attr;
580 const char *key;
581
582 HPDF_PTRACE((" HPDF_Page_GetXObjectName\n"));
583
584 if (!attr->xobjects) {
585 HPDF_Dict resources;
586 HPDF_Dict xobjects;
587
588 resources = HPDF_Page_GetInheritableItem (page, "Resources",
589 HPDF_OCLASS_DICT);
590 if (!resources)
591 return NULL;
592
593 xobjects = HPDF_Dict_New (page->mmgr);
594 if (!xobjects)
595 return NULL;
596
597 if (HPDF_Dict_Add (resources, "XObject", xobjects) != HPDF_OK)
598 return NULL;
599
600 attr->xobjects = xobjects;
601 }
602
603 /* search xobject-object from xobject-resource */
604 key = HPDF_Dict_GetKeyByObj (attr->xobjects, xobj);
605 if (!key) {
606 /* if the xobject is not resisterd in xobject-resource, register
607 * xobject to xobject-resource.
608 */
609 char xobj_name[HPDF_LIMIT_MAX_NAME_LEN + 1];
610 char *ptr;
611 char *end_ptr = xobj_name + HPDF_LIMIT_MAX_NAME_LEN;
612
613 ptr = (char *)HPDF_StrCpy (xobj_name, "X", end_ptr);
614 HPDF_IToA (ptr, attr->xobjects->list->count + 1, end_ptr);
615
616 if (HPDF_Dict_Add (attr->xobjects, xobj_name, xobj) != HPDF_OK)
617 return NULL;
618
619 key = HPDF_Dict_GetKeyByObj (attr->xobjects, xobj);
620 }
621
622 return key;
623}
624
625
626const char*
627HPDF_Page_GetExtGStateName (HPDF_Page page,
628 HPDF_ExtGState state)
629{
630 HPDF_PageAttr attr = (HPDF_PageAttr )page->attr;
631 const char *key;
632
633 HPDF_PTRACE((" HPDF_Page_GetExtGStateName\n"));
634
635 if (!attr->ext_gstates) {
636 HPDF_Dict resources;
637 HPDF_Dict ext_gstates;
638
639 resources = HPDF_Page_GetInheritableItem (page, "Resources",
640 HPDF_OCLASS_DICT);
641 if (!resources)
642 return NULL;
643
644 ext_gstates = HPDF_Dict_New (page->mmgr);
645 if (!ext_gstates)
646 return NULL;
647
648 if (HPDF_Dict_Add (resources, "ExtGState", ext_gstates) != HPDF_OK)
649 return NULL;
650
651 attr->ext_gstates = ext_gstates;
652 }
653
654 /* search ext_gstate-object from ext_gstate-resource */
655 key = HPDF_Dict_GetKeyByObj (attr->ext_gstates, state);
656 if (!key) {
657 /* if the ext-gstate is not resisterd in ext-gstate resource, register
658 * to ext-gstate resource.
659 */
660 char ext_gstate_name[HPDF_LIMIT_MAX_NAME_LEN + 1];
661 char *ptr;
662 char *end_ptr = ext_gstate_name + HPDF_LIMIT_MAX_NAME_LEN;
663
664 ptr = (char *)HPDF_StrCpy (ext_gstate_name, "E", end_ptr);
665 HPDF_IToA (ptr, attr->ext_gstates->list->count + 1, end_ptr);
666
667 if (HPDF_Dict_Add (attr->ext_gstates, ext_gstate_name, state) != HPDF_OK)
668 return NULL;
669
670 key = HPDF_Dict_GetKeyByObj (attr->ext_gstates, state);
671 }
672
673 return key;
674}
675
676
677static HPDF_STATUS
678AddAnnotation (HPDF_Page page,
679 HPDF_Annotation annot)
680{
681 HPDF_Array array;
682 HPDF_STATUS ret = HPDF_OK;
683
684 HPDF_PTRACE((" HPDF_Pages\n"));
685
686 /* find "Annots" entry */
687 array = HPDF_Dict_GetItem (page, "Annots", HPDF_OCLASS_ARRAY);
688
689 if (!array) {
690 array = HPDF_Array_New (page->mmgr);
691 if (!array)
692 return HPDF_Error_GetCode (page->error);
693
694 ret = HPDF_Dict_Add (page, "Annots", array);
695 if (ret != HPDF_OK)
696 return ret;
697 }
698
699 if ((ret = HPDF_Array_Add (array, annot)) != HPDF_OK)
700 return ret;
701
702 /* Add Parent to the annotation */
703 ret = HPDF_Dict_Add( annot, "P", page);
704
705 return ret;
706}
707
708
709HPDF_EXPORT(HPDF_REAL)
710HPDF_Page_TextWidth (HPDF_Page page,
711 const char *text)
712{
713 HPDF_PageAttr attr;
714 HPDF_TextWidth tw;
715 HPDF_REAL ret = 0;
716 HPDF_UINT len = HPDF_StrLen(text, HPDF_LIMIT_MAX_STRING_LEN + 1);
717
718 HPDF_PTRACE((" HPDF_Page_TextWidth\n"));
719
720 if (!HPDF_Page_Validate (page) || len == 0)
721 return 0;
722
723 attr = (HPDF_PageAttr )page->attr;
724
725 /* no font exists */
726 if (!attr->gstate->font) {
727 HPDF_RaiseError (page->error, HPDF_PAGE_FONT_NOT_FOUND, 0);
728 return 0;
729 }
730
731 tw = HPDF_Font_TextWidth (attr->gstate->font, (HPDF_BYTE *)text, len);
732
733 ret += attr->gstate->word_space * tw.numspace;
734 ret += tw.width * attr->gstate->font_size / 1000;
735 ret += attr->gstate->char_space * tw.numchars;
736
737 HPDF_CheckError (page->error);
738
739 return ret;
740}
741
742
743HPDF_EXPORT(HPDF_UINT)
744HPDF_Page_MeasureText (HPDF_Page page,
745 const char *text,
746 HPDF_REAL width,
747 HPDF_BOOL wordwrap,
748 HPDF_REAL *real_width)
749{
750 HPDF_PageAttr attr;
751 HPDF_UINT len = HPDF_StrLen(text, HPDF_LIMIT_MAX_STRING_LEN + 1);
752 HPDF_UINT ret;
753
754 if (!HPDF_Page_Validate (page) || len == 0)
755 return 0;
756
757 attr = (HPDF_PageAttr )page->attr;
758
759 HPDF_PTRACE((" HPDF_Page_MeasureText\n"));
760
761 /* no font exists */
762 if (!attr->gstate->font) {
763 HPDF_RaiseError (page->error, HPDF_PAGE_FONT_NOT_FOUND, 0);
764 return 0;
765 }
766
767 ret = HPDF_Font_MeasureText (attr->gstate->font, (HPDF_BYTE *)text, len, width,
768 attr->gstate->font_size, attr->gstate->char_space,
769 attr->gstate->word_space, wordwrap, real_width);
770
771 HPDF_CheckError (page->error);
772
773 return ret;
774}
775
776
777HPDF_EXPORT(HPDF_REAL)
778HPDF_Page_GetWidth (HPDF_Page page)
779{
780 return HPDF_Page_GetMediaBox (page).right;
781}
782
783
784HPDF_EXPORT(HPDF_REAL)
785HPDF_Page_GetHeight (HPDF_Page page)
786{
787 return HPDF_Page_GetMediaBox (page).top;
788}
789
790
791HPDF_EXPORT(HPDF_Font)
792HPDF_Page_GetCurrentFont (HPDF_Page page)
793{
794 HPDF_PTRACE((" HPDF_Page_GetFontName\n"));
795
796 if (HPDF_Page_Validate (page)) {
797 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
798
799 return attr->gstate->font;
800 } else
801 return NULL;
802}
803
804
805HPDF_EXPORT(HPDF_REAL)
806HPDF_Page_GetCurrentFontSize (HPDF_Page page)
807{
808 HPDF_PTRACE((" HPDF_Page_GetCurrentFontSize\n"));
809
810 if (HPDF_Page_Validate (page)) {
811 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
812
813 return (attr->gstate->font) ? attr->gstate->font_size : 0;
814 } else
815 return 0;
816}
817
818
819HPDF_EXPORT(HPDF_TransMatrix)
820HPDF_Page_GetTransMatrix (HPDF_Page page)
821{
822 HPDF_TransMatrix DEF_MATRIX = {1, 0, 0, 1, 0, 0};
823
824 HPDF_PTRACE((" HPDF_Page_GetTransMatrix\n"));
825 if (HPDF_Page_Validate (page)) {
826 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
827
828 return attr->gstate->trans_matrix;
829 } else
830 return DEF_MATRIX;
831}
832
833
834HPDF_EXPORT(HPDF_REAL)
835HPDF_Page_GetLineWidth (HPDF_Page page)
836{
837 HPDF_PTRACE((" HPDF_Page_GetLineWidth\n"));
838
839 if (HPDF_Page_Validate (page)) {
840 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
841
842 return attr->gstate->line_width;
843 } else
844 return HPDF_DEF_LINEWIDTH;
845}
846
847
848HPDF_EXPORT(HPDF_LineCap)
849HPDF_Page_GetLineCap (HPDF_Page page)
850{
851 HPDF_PTRACE((" HPDF_Page_GetLineCap\n"));
852
853 if (HPDF_Page_Validate (page)) {
854 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
855
856 return attr->gstate->line_cap;
857 } else
858 return HPDF_DEF_LINECAP;
859}
860
861
862HPDF_EXPORT(HPDF_LineJoin)
863HPDF_Page_GetLineJoin (HPDF_Page page)
864{
865 HPDF_PTRACE((" HPDF_Page_GetLineJoin\n"));
866
867 if (HPDF_Page_Validate (page)) {
868 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
869
870 return attr->gstate->line_join;
871 } else
872 return HPDF_DEF_LINEJOIN;
873}
874
875
876HPDF_EXPORT(HPDF_REAL)
877HPDF_Page_GetMiterLimit (HPDF_Page page)
878{
879 HPDF_PTRACE((" HPDF_Page_GetMiterLimit\n"));
880
881 if (HPDF_Page_Validate (page)) {
882 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
883
884 return attr->gstate->miter_limit;
885 } else
886 return HPDF_DEF_MITERLIMIT;
887}
888
889
890HPDF_EXPORT(HPDF_DashMode)
891HPDF_Page_GetDash (HPDF_Page page)
892{
893 HPDF_DashMode mode = {{0, 0, 0, 0, 0, 0, 0, 0}, 0, 0};
894
895 HPDF_PTRACE((" HPDF_Page_GetDash\n"));
896
897 if (HPDF_Page_Validate (page)) {
898 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
899
900 mode = attr->gstate->dash_mode;
901 }
902
903 return mode;
904}
905
906
907HPDF_EXPORT(HPDF_REAL)
908HPDF_Page_GetFlat (HPDF_Page page)
909{
910 HPDF_PTRACE((" HPDF_Page_GetFlat\n"));
911
912 if (HPDF_Page_Validate (page)) {
913 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
914
915 return attr->gstate->flatness;
916 } else
917 return HPDF_DEF_FLATNESS;
918}
919
920
921HPDF_EXPORT(HPDF_REAL)
922HPDF_Page_GetWordSpace (HPDF_Page page)
923{
924 HPDF_PTRACE((" HPDF_Page_GetWordSpace\n"));
925
926 if (HPDF_Page_Validate (page)) {
927 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
928
929 return attr->gstate->word_space;
930 } else
931 return HPDF_DEF_WORDSPACE;
932}
933
934
935HPDF_EXPORT(HPDF_REAL)
936HPDF_Page_GetCharSpace (HPDF_Page page)
937{
938 HPDF_PTRACE((" HPDF_Page_GetCharSpace\n"));
939
940 if (HPDF_Page_Validate (page)) {
941 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
942
943 return attr->gstate->char_space;
944 } else
945 return HPDF_DEF_CHARSPACE;
946}
947
948
949HPDF_EXPORT(HPDF_REAL)
950HPDF_Page_GetHorizontalScalling (HPDF_Page page)
951{
952 HPDF_PTRACE((" HPDF_Page_GetHorizontalScalling\n"));
953
954 if (HPDF_Page_Validate (page)) {
955 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
956
957 return attr->gstate->h_scalling;
958 } else
959 return HPDF_DEF_HSCALING;
960}
961
962
963HPDF_EXPORT(HPDF_REAL)
964HPDF_Page_GetTextLeading (HPDF_Page page)
965{
966 HPDF_PTRACE((" HPDF_Page_GetTextLeading\n"));
967
968 if (HPDF_Page_Validate (page)) {
969 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
970
971 return attr->gstate->text_leading;
972 } else
973 return HPDF_DEF_LEADING;
974}
975
976
977HPDF_EXPORT(HPDF_TextRenderingMode)
978HPDF_Page_GetTextRenderingMode (HPDF_Page page)
979{
980 HPDF_PTRACE((" HPDF_Page_GettextRenderingMode\n"));
981
982 if (HPDF_Page_Validate (page)) {
983 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
984
985 return attr->gstate->rendering_mode;
986 } else
987 return HPDF_DEF_RENDERING_MODE;
988}
989
990
991HPDF_EXPORT(HPDF_REAL)
992HPDF_Page_GetTextRaise (HPDF_Page page)
993{
994 return HPDF_Page_GetTextRise (page);
995}
996
997HPDF_EXPORT(HPDF_REAL)
998HPDF_Page_GetTextRise (HPDF_Page page)
999{
1000 HPDF_PTRACE((" HPDF_Page_GetTextRise\n"));
1001
1002 if (HPDF_Page_Validate (page)) {
1003 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1004
1005 return attr->gstate->text_rise;
1006 } else
1007 return HPDF_DEF_RISE;
1008}
1009
1010
1011HPDF_EXPORT(HPDF_RGBColor)
1012HPDF_Page_GetRGBFill (HPDF_Page page)
1013{
1014 HPDF_RGBColor DEF_RGB_COLOR = {0, 0, 0};
1015
1016 HPDF_PTRACE((" HPDF_Page_GetRGBFill\n"));
1017
1018 if (HPDF_Page_Validate (page)) {
1019 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1020
1021 if (attr->gstate->cs_fill == HPDF_CS_DEVICE_RGB)
1022 return attr->gstate->rgb_fill;
1023 }
1024
1025 return DEF_RGB_COLOR;
1026}
1027
1028
1029HPDF_EXPORT(HPDF_RGBColor)
1030HPDF_Page_GetRGBStroke (HPDF_Page page)
1031{
1032 HPDF_RGBColor DEF_RGB_COLOR = {0, 0, 0};
1033
1034 HPDF_PTRACE((" HPDF_Page_GetRGBStroke\n"));
1035
1036 if (HPDF_Page_Validate (page)) {
1037 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1038
1039 if (attr->gstate->cs_stroke == HPDF_CS_DEVICE_RGB)
1040 return attr->gstate->rgb_stroke;
1041 }
1042
1043 return DEF_RGB_COLOR;
1044}
1045
1046HPDF_EXPORT(HPDF_CMYKColor)
1047HPDF_Page_GetCMYKFill (HPDF_Page page)
1048{
1049 HPDF_CMYKColor DEF_CMYK_COLOR = {0, 0, 0, 0};
1050
1051 HPDF_PTRACE((" HPDF_Page_GetCMYKFill\n"));
1052
1053 if (HPDF_Page_Validate (page)) {
1054 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1055
1056 if (attr->gstate->cs_fill == HPDF_CS_DEVICE_CMYK)
1057 return attr->gstate->cmyk_fill;
1058 }
1059
1060 return DEF_CMYK_COLOR;
1061}
1062
1063
1064HPDF_EXPORT(HPDF_CMYKColor)
1065HPDF_Page_GetCMYKStroke (HPDF_Page page)
1066{
1067 HPDF_CMYKColor DEF_CMYK_COLOR = {0, 0, 0, 0};
1068
1069 HPDF_PTRACE((" HPDF_Page_GetCMYKStroke\n"));
1070
1071 if (HPDF_Page_Validate (page)) {
1072 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1073
1074 if (attr->gstate->cs_stroke == HPDF_CS_DEVICE_CMYK)
1075 return attr->gstate->cmyk_stroke;
1076 }
1077
1078 return DEF_CMYK_COLOR;
1079}
1080
1081
1082HPDF_EXPORT(HPDF_REAL)
1083HPDF_Page_GetGrayFill (HPDF_Page page)
1084{
1085 HPDF_PTRACE((" HPDF_Page_GetGrayFill\n"));
1086
1087 if (HPDF_Page_Validate (page)) {
1088 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1089
1090 if (attr->gstate->cs_fill == HPDF_CS_DEVICE_GRAY)
1091 return attr->gstate->gray_fill;
1092 }
1093
1094 return 0;
1095}
1096
1097
1098HPDF_EXPORT(HPDF_REAL)
1099HPDF_Page_GetGrayStroke (HPDF_Page page)
1100{
1101 HPDF_PTRACE((" HPDF_Page_GetGrayStroke\n"));
1102
1103 if (HPDF_Page_Validate (page)) {
1104 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1105
1106 if (attr->gstate->cs_stroke == HPDF_CS_DEVICE_GRAY)
1107 return attr->gstate->gray_stroke;
1108 }
1109
1110 return 0;
1111}
1112
1113
1114HPDF_EXPORT(HPDF_ColorSpace)
1115HPDF_Page_GetStrokingColorSpace (HPDF_Page page)
1116{
1117 HPDF_PTRACE((" HPDF_Page_GetStrokingColorSpace\n"));
1118
1119 if (HPDF_Page_Validate (page))
1120 return ((HPDF_PageAttr)page->attr)->gstate->cs_stroke;
1121
1122 return HPDF_CS_EOF;
1123}
1124
1125
1126HPDF_EXPORT(HPDF_ColorSpace)
1127HPDF_Page_GetFillingColorSpace (HPDF_Page page)
1128{
1129 HPDF_PTRACE((" HPDF_Page_GetFillingColorSpace\n"));
1130
1131 if (HPDF_Page_Validate (page))
1132 return ((HPDF_PageAttr)page->attr)->gstate->cs_fill;
1133
1134 return HPDF_CS_EOF;
1135}
1136
1137
1138HPDF_EXPORT(HPDF_TransMatrix)
1139HPDF_Page_GetTextMatrix (HPDF_Page page)
1140{
1141 HPDF_TransMatrix DEF_MATRIX = {1, 0, 0, 1, 0, 0};
1142
1143 HPDF_PTRACE((" HPDF_Page_GetTextMatrix\n"));
1144 if (HPDF_Page_Validate (page)) {
1145 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1146
1147 return attr->text_matrix;
1148 } else
1149 return DEF_MATRIX;
1150}
1151
1152
1153HPDF_EXPORT(HPDF_UINT)
1154HPDF_Page_GetGStateDepth (HPDF_Page page)
1155{
1156 HPDF_PTRACE((" HPDF_Page_GetGStateDepth\n"));
1157
1158 if (HPDF_Page_Validate (page)) {
1159 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1160
1161 return attr->gstate->depth;
1162 } else
1163 return 0;
1164}
1165
1166
1167HPDF_EXPORT(HPDF_UINT16)
1168HPDF_Page_GetGMode (HPDF_Page page)
1169{
1170 if (HPDF_Page_Validate (page))
1171 return ((HPDF_PageAttr)page->attr)->gmode;
1172
1173 return 0;
1174}
1175
1176HPDF_EXPORT(HPDF_Point)
1177HPDF_Page_GetCurrentPos (HPDF_Page page)
1178{
1179 HPDF_Point pos = {0, 0};
1180
1181 HPDF_PTRACE((" HPDF_Page_GetCurrentPos\n"));
1182
1183 if (HPDF_Page_Validate (page)) {
1184 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1185
1186 if (attr->gmode & HPDF_GMODE_PATH_OBJECT)
1187 pos = attr->cur_pos;
1188 }
1189
1190 return pos;
1191}
1192
1193
1194HPDF_EXPORT(HPDF_STATUS)
1195HPDF_Page_GetCurrentPos2 (HPDF_Page page,
1196 HPDF_Point *pos)
1197{
1198 HPDF_PageAttr attr;
1199 HPDF_PTRACE((" HPDF_Page_GetCurrentPos2\n"));
1200
1201 pos->x = 0;
1202 pos->y = 0;
1203 if (!HPDF_Page_Validate (page))
1204 return HPDF_INVALID_PAGE;
1205
1206 attr = (HPDF_PageAttr)page->attr;
1207
1208 if (attr->gmode & HPDF_GMODE_PATH_OBJECT)
1209 *pos = attr->cur_pos;
1210
1211 return HPDF_OK;
1212}
1213
1214
1215HPDF_EXPORT(HPDF_Point)
1216HPDF_Page_GetCurrentTextPos (HPDF_Page page)
1217{
1218 HPDF_Point pos = {0, 0};
1219
1220 HPDF_PTRACE((" HPDF_Page_GetCurrentTextPos\n"));
1221
1222 if (HPDF_Page_Validate (page)) {
1223 HPDF_PageAttr attr = (HPDF_PageAttr)page->attr;
1224
1225 if (attr->gmode & HPDF_GMODE_TEXT_OBJECT)
1226 pos = attr->text_pos;
1227 }
1228
1229 return pos;
1230}
1231
1232
1233HPDF_EXPORT(HPDF_STATUS)
1234HPDF_Page_GetCurrentTextPos2 (HPDF_Page page,
1235 HPDF_Point *pos)
1236{
1237 HPDF_PageAttr attr;
1238
1239 HPDF_PTRACE((" HPDF_Page_GetCurrentTextPos2\n"));
1240
1241 pos->x = 0;
1242 pos->y = 0;
1243 if (!HPDF_Page_Validate (page))
1244 return HPDF_INVALID_PAGE;
1245
1246 attr = (HPDF_PageAttr)page->attr;
1247
1248 if (attr->gmode & HPDF_GMODE_TEXT_OBJECT)
1249 *pos = attr->text_pos;
1250
1251 return HPDF_OK;
1252}
1253
1254
1255HPDF_STATUS
1256HPDF_Page_SetBoxValue (HPDF_Page page,
1257 const char *name,
1258 HPDF_UINT index,
1259 HPDF_REAL value)
1260{
1261 HPDF_Real r;
1262 HPDF_Array array;
1263
1264 HPDF_PTRACE((" HPDF_Page_SetBoxValue\n"));
1265
1266 if (!HPDF_Page_Validate (page))
1267 return HPDF_INVALID_PAGE;
1268
1269 array = HPDF_Page_GetInheritableItem (page, name, HPDF_OCLASS_ARRAY);
1270 if (!array)
1271 return HPDF_SetError (page->error, HPDF_PAGE_CANNOT_FIND_OBJECT, 0);
1272
1273 r = HPDF_Array_GetItem (array, index, HPDF_OCLASS_REAL);
1274 if (!r)
1275 return HPDF_SetError (page->error, HPDF_PAGE_INVALID_INDEX, 0);
1276
1277 r->value = value;
1278
1279 return HPDF_OK;
1280}
1281
1282HPDF_EXPORT(HPDF_STATUS)
1283HPDF_Page_SetRotate (HPDF_Page page,
1284 HPDF_UINT16 angle)
1285{
1286 HPDF_Number n;
1287 HPDF_STATUS ret = HPDF_OK;
1288
1289 HPDF_PTRACE((" HPDF_Page_SetRotate\n"));
1290
1291 if (!HPDF_Page_Validate (page))
1292 return HPDF_INVALID_PAGE;
1293
1294 if (angle % 90 != 0)
1295 return HPDF_RaiseError (page->error, HPDF_PAGE_INVALID_ROTATE_VALUE,
1296 (HPDF_STATUS)angle);
1297
1298 n = HPDF_Page_GetInheritableItem (page, "Rotate", HPDF_OCLASS_NUMBER);
1299
1300 if (!n)
1301 ret = HPDF_Dict_AddNumber (page, "Rotate", angle);
1302 else
1303 n->value = angle;
1304
1305 return ret;
1306}
1307
1308HPDF_EXPORT(HPDF_STATUS)
1309HPDF_Page_SetZoom (HPDF_Page page,
1310 HPDF_REAL zoom)
1311{
1312 HPDF_STATUS ret = HPDF_OK;
1313
1314 HPDF_PTRACE((" HPDF_Page_SetZoom\n"));
1315
1316 if (!HPDF_Page_Validate (page)) {
1317 return HPDF_INVALID_PAGE;
1318 }
1319
1320 if (zoom < 0.08 || zoom > 32) {
1321 return HPDF_RaiseError (page->error, HPDF_INVALID_PARAMETER, 0);
1322 }
1323
1324 ret = HPDF_Dict_AddReal (page, "PZ", zoom);
1325 return ret;
1326}
1327
1328HPDF_EXPORT(HPDF_STATUS)
1329HPDF_Page_SetWidth (HPDF_Page page,
1330 HPDF_REAL value)
1331{
1332 HPDF_PTRACE((" HPDF_Page_SetWidth\n"));
1333
1334 if (value < 3 || value > 14400)
1335 return HPDF_RaiseError (page->error, HPDF_PAGE_INVALID_SIZE, 0);
1336
1337 if (HPDF_Page_SetBoxValue (page, "MediaBox", 2, value) != HPDF_OK)
1338 return HPDF_CheckError (page->error);
1339
1340 return HPDF_OK;
1341}
1342
1343
1344HPDF_EXPORT(HPDF_STATUS)
1345HPDF_Page_SetHeight (HPDF_Page page,
1346 HPDF_REAL value)
1347{
1348 HPDF_PTRACE((" HPDF_Page_SetWidth\n"));
1349
1350 if (value < 3 || value > 14400)
1351 return HPDF_RaiseError (page->error, HPDF_PAGE_INVALID_SIZE, 0);
1352
1353 if (HPDF_Page_SetBoxValue (page, "MediaBox", 3, value) != HPDF_OK)
1354 return HPDF_CheckError (page->error);
1355
1356 return HPDF_OK;
1357}
1358
1359
1360HPDF_EXPORT(HPDF_STATUS)
1361HPDF_Page_SetSize (HPDF_Page page,
1362 HPDF_PageSizes size,
1363 HPDF_PageDirection direction)
1364{
1365 HPDF_STATUS ret = HPDF_OK;
1366
1367 HPDF_PTRACE((" HPDF_Page_SetSize\n"));
1368
1369 if (!HPDF_Page_Validate (page))
1370 return HPDF_INVALID_PAGE;
1371
1372 if (size < 0 || size > HPDF_PAGE_SIZE_EOF)
1373 return HPDF_RaiseError (page->error, HPDF_PAGE_INVALID_SIZE,
1374 (HPDF_STATUS)direction);
1375
1376 if (direction == HPDF_PAGE_LANDSCAPE) {
1377 ret += HPDF_Page_SetHeight (page,
1378 HPDF_PREDEFINED_PAGE_SIZES[(HPDF_UINT)size].x);
1379 ret += HPDF_Page_SetWidth (page,
1380 HPDF_PREDEFINED_PAGE_SIZES[(HPDF_UINT)size].y);
1381 } else if (direction == HPDF_PAGE_PORTRAIT) {
1382 ret += HPDF_Page_SetHeight (page,
1383 HPDF_PREDEFINED_PAGE_SIZES[(HPDF_UINT)size].y);
1384 ret += HPDF_Page_SetWidth (page,
1385 HPDF_PREDEFINED_PAGE_SIZES[(HPDF_UINT)size].x);
1386 } else
1387 ret = HPDF_SetError (page->error, HPDF_PAGE_INVALID_DIRECTION,
1388 (HPDF_STATUS)direction);
1389
1390 if (ret != HPDF_OK)
1391 return HPDF_CheckError (page->error);
1392
1393 return HPDF_OK;
1394}
1395
1396
1397HPDF_BOOL
1398HPDF_Page_Validate (HPDF_Page page)
1399{
1400 HPDF_Obj_Header *header = (HPDF_Obj_Header *)page;
1401
1402 HPDF_PTRACE((" HPDF_Page_Validate\n"));
1403
1404 if (!page || !page->attr)
1405 return HPDF_FALSE;
1406
1407 if (header->obj_class != (HPDF_OCLASS_DICT | HPDF_OSUBCLASS_PAGE))
1408 return HPDF_FALSE;
1409
1410 return HPDF_TRUE;
1411}
1412
1413
1414HPDF_EXPORT(HPDF_Destination)
1415HPDF_Page_CreateDestination (HPDF_Page page)
1416{
1417 HPDF_PageAttr attr;
1418 HPDF_Destination dst;
1419
1420 HPDF_PTRACE((" HPDF_Page_CreateDestination\n"));
1421
1422 if (!HPDF_Page_Validate (page))
1423 return NULL;
1424
1425 attr = (HPDF_PageAttr)page->attr;
1426
1427 dst = HPDF_Destination_New (page->mmgr, page, attr->xref);
1428 if (!dst)
1429 HPDF_CheckError (page->error);
1430
1431 return dst;
1432}
1433
1434
1435HPDF_EXPORT(HPDF_Annotation)
1436HPDF_Page_Create3DAnnot (HPDF_Page page,
1437 HPDF_Rect rect,
1438 HPDF_U3D u3d)
1439{
1440 HPDF_PageAttr attr;
1441 HPDF_Annotation annot;
1442
1443 HPDF_PTRACE((" HPDF_Page_Create3DAnnot\n"));
1444
1445 if (!HPDF_Page_Validate (page))
1446 return NULL;
1447
1448 attr = (HPDF_PageAttr)page->attr;
1449
1450 annot = HPDF_3DAnnot_New (page->mmgr, attr->xref, rect, u3d);
1451 if (annot) {
1452 if (AddAnnotation (page, annot) != HPDF_OK) {
1453 HPDF_CheckError (page->error);
1454 annot = NULL;
1455 }
1456 } else
1457 HPDF_CheckError (page->error);
1458
1459 return annot;
1460}
1461
1462HPDF_EXPORT(HPDF_Annotation)
1463HPDF_Page_CreateTextAnnot (HPDF_Page page,
1464 HPDF_Rect rect,
1465 const char *text,
1466 HPDF_Encoder encoder)
1467{
1468 HPDF_PageAttr attr;
1469 HPDF_Annotation annot;
1470
1471 HPDF_PTRACE((" HPDF_Page_CreateTextAnnot\n"));
1472
1473 if (!HPDF_Page_Validate (page))
1474 return NULL;
1475
1476 attr = (HPDF_PageAttr)page->attr;
1477
1478 if (encoder && !HPDF_Encoder_Validate (encoder)) {
1479 HPDF_RaiseError (page->error, HPDF_INVALID_ENCODER, 0);
1480 return NULL;
1481 }
1482
1483 annot = HPDF_MarkupAnnot_New (page->mmgr, attr->xref, rect, text, encoder, HPDF_ANNOT_TEXT_NOTES);
1484 if (annot) {
1485 if (AddAnnotation (page, annot) != HPDF_OK) {
1486 HPDF_CheckError (page->error);
1487 annot = NULL;
1488 }
1489 } else
1490 HPDF_CheckError (page->error);
1491
1492 return annot;
1493}
1494
1495HPDF_EXPORT(HPDF_Annotation)
1496HPDF_Page_CreateFreeTextAnnot (HPDF_Page page,
1497 HPDF_Rect rect,
1498 const char *text,
1499 HPDF_Encoder encoder)
1500{
1501 HPDF_PageAttr attr;
1502 HPDF_Annotation annot;
1503
1504 HPDF_PTRACE((" HPDF_Page_CreateFreeTextAnnot\n"));
1505
1506 if (!HPDF_Page_Validate (page))
1507 return NULL;
1508
1509 attr = (HPDF_PageAttr)page->attr;
1510
1511 if (encoder && !HPDF_Encoder_Validate (encoder)) {
1512 HPDF_RaiseError (page->error, HPDF_INVALID_ENCODER, 0);
1513 return NULL;
1514 }
1515
1516 annot = HPDF_MarkupAnnot_New (page->mmgr, attr->xref, rect, text, encoder, HPDF_ANNOT_FREE_TEXT);
1517 if (annot) {
1518 if (AddAnnotation (page, annot) != HPDF_OK) {
1519 HPDF_CheckError (page->error);
1520 annot = NULL;
1521 }
1522 } else
1523 HPDF_CheckError (page->error);
1524
1525 return annot;
1526}
1527
1528HPDF_EXPORT(HPDF_Annotation)
1529HPDF_Page_CreateLineAnnot (HPDF_Page page,
1530 const char *text,
1531 HPDF_Encoder encoder)
1532{
1533 HPDF_PageAttr attr;
1534 HPDF_Annotation annot;
1535 HPDF_Rect rect = {0,0,0,0};
1536
1537 HPDF_PTRACE((" HPDF_Page_CreateLineAnnot\n"));
1538
1539 if (!HPDF_Page_Validate (page))
1540 return NULL;
1541
1542 attr = (HPDF_PageAttr)page->attr;
1543
1544 if (encoder && !HPDF_Encoder_Validate (encoder)) {
1545 HPDF_RaiseError (page->error, HPDF_INVALID_ENCODER, 0);
1546 return NULL;
1547 }
1548
1549 annot = HPDF_MarkupAnnot_New (page->mmgr, attr->xref, rect, text, encoder, HPDF_ANNOT_LINE);
1550 if (annot) {
1551 if (AddAnnotation (page, annot) != HPDF_OK) {
1552 HPDF_CheckError (page->error);
1553 annot = NULL;
1554 }
1555 } else
1556 HPDF_CheckError (page->error);
1557
1558 return annot;
1559}
1560
1561HPDF_EXPORT(HPDF_Annotation)
1562HPDF_Page_CreateLinkAnnot (HPDF_Page page,
1563 HPDF_Rect rect,
1564 HPDF_Destination dst)
1565{
1566 HPDF_PageAttr attr;
1567 HPDF_Annotation annot;
1568
1569 HPDF_PTRACE((" HPDF_Page_CreateLinkAnnot\n"));
1570
1571 if (!HPDF_Page_Validate (page))
1572 return NULL;
1573
1574 attr = (HPDF_PageAttr)page->attr;
1575
1576 if (!HPDF_Destination_Validate (dst)) {
1577 HPDF_RaiseError (page->error, HPDF_INVALID_DESTINATION, 0);
1578 return NULL;
1579 }
1580
1581 annot = HPDF_LinkAnnot_New (page->mmgr, attr->xref, rect, dst);
1582 if (annot) {
1583 if (AddAnnotation (page, annot) != HPDF_OK) {
1584 HPDF_CheckError (page->error);
1585 annot = NULL;
1586 }
1587 } else
1588 HPDF_CheckError (page->error);
1589
1590 return annot;
1591}
1592
1593
1594HPDF_EXPORT(HPDF_Annotation)
1595HPDF_Page_CreateURILinkAnnot (HPDF_Page page,
1596 HPDF_Rect rect,
1597 const char *uri)
1598{
1599 HPDF_PageAttr attr;
1600 HPDF_Annotation annot;
1601
1602 HPDF_PTRACE((" HPDF_Page_CreateURILinkAnnot\n"));
1603
1604 if (!HPDF_Page_Validate (page))
1605 return NULL;
1606
1607 attr = (HPDF_PageAttr)page->attr;
1608
1609 if (HPDF_StrLen (uri, HPDF_LIMIT_MAX_STRING_LEN) <= 0) {
1610 HPDF_RaiseError (page->error, HPDF_INVALID_URI, 0);
1611 return NULL;
1612 }
1613
1614 annot = HPDF_URILinkAnnot_New (page->mmgr, attr->xref, rect, uri);
1615 if (annot) {
1616 if (AddAnnotation (page, annot) != HPDF_OK) {
1617 HPDF_CheckError (page->error);
1618 annot = NULL;
1619 }
1620 } else
1621 HPDF_CheckError (page->error);
1622
1623 return annot;
1624}
1625
1626HPDF_EXPORT(HPDF_Annotation)
1627HPDF_Page_CreateCircleAnnot (HPDF_Page page,
1628 HPDF_Rect rect,
1629 const char *text,
1630 HPDF_Encoder encoder)
1631{
1632 HPDF_PageAttr attr;
1633 HPDF_Annotation annot;
1634
1635 HPDF_PTRACE((" HPDF_Page_CreateCircleAnnot\n"));
1636
1637 if (!HPDF_Page_Validate (page))
1638 return NULL;
1639
1640 attr = (HPDF_PageAttr)page->attr;
1641
1642 if (encoder && !HPDF_Encoder_Validate (encoder)) {
1643 HPDF_RaiseError (page->error, HPDF_INVALID_ENCODER, 0);
1644 return NULL;
1645 }
1646
1647 annot = HPDF_MarkupAnnot_New (page->mmgr, attr->xref, rect, text, encoder, HPDF_ANNOT_CIRCLE);
1648 if (annot) {
1649 if (AddAnnotation (page, annot) != HPDF_OK) {
1650 HPDF_CheckError (page->error);
1651 annot = NULL;
1652 }
1653 } else
1654 HPDF_CheckError (page->error);
1655
1656 return annot;
1657}
1658
1659HPDF_EXPORT(HPDF_Annotation)
1660HPDF_Page_CreateSquareAnnot (HPDF_Page page,
1661 HPDF_Rect rect,
1662 const char *text,
1663 HPDF_Encoder encoder)
1664{
1665 HPDF_PageAttr attr;
1666 HPDF_Annotation annot;
1667
1668 HPDF_PTRACE((" HPDF_Page_CreateCircleAnnot\n"));
1669
1670 if (!HPDF_Page_Validate (page))
1671 return NULL;
1672
1673 attr = (HPDF_PageAttr)page->attr;
1674
1675 if (encoder && !HPDF_Encoder_Validate (encoder)) {
1676 HPDF_RaiseError (page->error, HPDF_INVALID_ENCODER, 0);
1677 return NULL;
1678 }
1679
1680 annot = HPDF_MarkupAnnot_New (page->mmgr, attr->xref, rect, text, encoder, HPDF_ANNOT_SQUARE);
1681 if (annot) {
1682 if (AddAnnotation (page, annot) != HPDF_OK) {
1683 HPDF_CheckError (page->error);
1684 annot = NULL;
1685 }
1686 } else
1687 HPDF_CheckError (page->error);
1688
1689 return annot;
1690}
1691
1692HPDF_EXPORT(HPDF_Dict)
1693HPDF_Page_Create3DView (HPDF_Page page,
1694 HPDF_U3D u3d,
1695 HPDF_Annotation annot3d,
1696 const char *name)
1697{
1698 HPDF_PageAttr attr;
1699 HPDF_Dict view;
1700
1701 HPDF_PTRACE((" HPDF_Page_Create3DView\n"));
1702 HPDF_UNUSED(annot3d);
1703
1704 if (!HPDF_Page_Validate (page))
1705 return NULL;
1706
1707 attr = (HPDF_PageAttr)page->attr;
1708
1709 view = HPDF_3DView_New( page->mmgr, attr->xref, u3d, name);
1710 if (!view) {
1711 HPDF_CheckError (page->error);
1712 }
1713 return view;
1714}
1715
1716HPDF_Annotation
1717HPDF_Page_CreateTextMarkupAnnot (HPDF_Page page,
1718 HPDF_Rect rect,
1719 const char *text,
1720 HPDF_Encoder encoder,
1721 HPDF_AnnotType subType)
1722{
1723 HPDF_PageAttr attr;
1724 HPDF_Annotation annot;
1725
1726 HPDF_PTRACE((" HPDF_Page_CreateTextMarkupAnnot\n"));
1727
1728 if (!HPDF_Page_Validate (page))
1729 return NULL;
1730
1731 attr = (HPDF_PageAttr)page->attr;
1732
1733 if (encoder && !HPDF_Encoder_Validate (encoder)) {
1734 HPDF_RaiseError (page->error, HPDF_INVALID_ENCODER, 0);
1735 return NULL;
1736 }
1737
1738 annot = HPDF_MarkupAnnot_New ( page->mmgr, attr->xref, rect, text, encoder, subType);
1739 if (annot) {
1740 if (AddAnnotation (page, annot) != HPDF_OK) {
1741 HPDF_CheckError (page->error);
1742 annot = NULL;
1743 }
1744 } else
1745 HPDF_CheckError (page->error);
1746
1747 return annot;
1748}
1749
1750
1751HPDF_EXPORT(HPDF_Annotation)
1752HPDF_Page_CreateHighlightAnnot (HPDF_Page page,
1753 HPDF_Rect rect,
1754 const char *text,
1755 HPDF_Encoder encoder)
1756{
1757 HPDF_PTRACE((" HPDF_Page_CreateHighlightAnnot\n"));
1758
1759 return HPDF_Page_CreateTextMarkupAnnot( page, rect, text, encoder, HPDF_ANNOT_HIGHTLIGHT);
1760}
1761
1762HPDF_EXPORT(HPDF_Annotation)
1763HPDF_Page_CreateSquigglyAnnot (HPDF_Page page,
1764 HPDF_Rect rect,
1765 const char *text,
1766 HPDF_Encoder encoder)
1767{
1768 HPDF_PTRACE((" HPDF_Page_CreateSquigglyAnnot\n"));
1769
1770 return HPDF_Page_CreateTextMarkupAnnot( page, rect, text, encoder, HPDF_ANNOT_SQUIGGLY);
1771}
1772
1773HPDF_EXPORT(HPDF_Annotation)
1774HPDF_Page_CreateUnderlineAnnot (HPDF_Page page,
1775 HPDF_Rect rect,
1776 const char *text,
1777 HPDF_Encoder encoder)
1778{
1779 HPDF_PTRACE((" HPDF_Page_CreateUnderlineAnnot\n"));
1780
1781 return HPDF_Page_CreateTextMarkupAnnot( page, rect, text, encoder, HPDF_ANNOT_UNDERLINE);
1782}
1783
1784HPDF_EXPORT(HPDF_Annotation)
1785HPDF_Page_CreateStrikeOutAnnot (HPDF_Page page,
1786 HPDF_Rect rect,
1787 const char *text,
1788 HPDF_Encoder encoder)
1789{
1790 HPDF_PTRACE((" HPDF_Page_CreateStrikeOutAnnot\n"));
1791
1792 return HPDF_Page_CreateTextMarkupAnnot( page, rect, text, encoder, HPDF_ANNOT_STRIKE_OUT);
1793}
1794
1795HPDF_EXPORT(HPDF_Annotation)
1796HPDF_Page_CreatePopupAnnot ( HPDF_Page page,
1797 HPDF_Rect rect,
1798 HPDF_Annotation parent)
1799{
1800 HPDF_PageAttr attr;
1801 HPDF_Annotation annot;
1802
1803 HPDF_PTRACE((" HPDF_Page_CreatePopupAnnot\n"));
1804
1805 if (!HPDF_Page_Validate (page))
1806 return NULL;
1807
1808 attr = (HPDF_PageAttr)page->attr;
1809
1810 annot = HPDF_PopupAnnot_New ( page->mmgr, attr->xref, rect, parent);
1811 if (annot) {
1812 if (AddAnnotation (page, annot) != HPDF_OK) {
1813 HPDF_CheckError (page->error);
1814 annot = NULL;
1815 }
1816 } else
1817 HPDF_CheckError (page->error);
1818
1819 return annot;
1820}
1821
1822HPDF_EXPORT(HPDF_Annotation)
1823HPDF_Page_CreateStampAnnot ( HPDF_Page page,
1824 HPDF_Rect rect,
1825 HPDF_StampAnnotName name,
1826 const char* text,
1827 HPDF_Encoder encoder)
1828{
1829 HPDF_PageAttr attr;
1830 HPDF_Annotation annot;
1831
1832 HPDF_PTRACE((" HPDF_Page_CreateStampAnnot\n"));
1833
1834 if (!HPDF_Page_Validate (page))
1835 return NULL;
1836
1837 attr = (HPDF_PageAttr)page->attr;
1838
1839 annot = HPDF_StampAnnot_New ( page->mmgr, attr->xref, rect, name, text, encoder);
1840 if (annot) {
1841 if (AddAnnotation (page, annot) != HPDF_OK) {
1842 HPDF_CheckError (page->error);
1843 annot = NULL;
1844 }
1845 } else
1846 HPDF_CheckError (page->error);
1847
1848 return annot;
1849}
1850
1851HPDF_EXPORT(HPDF_Annotation)
1852HPDF_Page_CreateProjectionAnnot(HPDF_Page page,
1853 HPDF_Rect rect,
1854 const char* text,
1855 HPDF_Encoder encoder)
1856{
1857 HPDF_PageAttr attr;
1858 HPDF_Annotation annot;
1859
1860 HPDF_PTRACE((" HPDF_Page_CreateProjectionAnnot\n"));
1861
1862 if (!HPDF_Page_Validate (page))
1863 return NULL;
1864
1865 attr = (HPDF_PageAttr)page->attr;
1866
1867 annot = HPDF_ProjectionAnnot_New (page->mmgr, attr->xref, rect, text, encoder);
1868 if (annot) {
1869 if (AddAnnotation (page, annot) != HPDF_OK) {
1870 HPDF_CheckError (page->error);
1871 annot = NULL;
1872 }
1873 } else
1874 HPDF_CheckError (page->error);
1875
1876 return annot;
1877}
1878
1879
1880HPDF_EXPORT(HPDF_3DMeasure)
1881HPDF_Page_Create3DC3DMeasure(HPDF_Page page,
1882 HPDF_Point3D firstanchorpoint,
1883 HPDF_Point3D textanchorpoint)
1884{
1885 HPDF_PageAttr attr;
1886 HPDF_Annotation measure;
1887
1888 HPDF_PTRACE((" HPDF_Page_Create3DC3DMeasure\n"));
1889
1890 if (!HPDF_Page_Validate (page))
1891 return NULL;
1892
1893 attr = (HPDF_PageAttr)page->attr;
1894
1895 measure = HPDF_3DC3DMeasure_New(page->mmgr, attr->xref, firstanchorpoint, textanchorpoint);
1896 if ( !measure)
1897 HPDF_CheckError (page->error);
1898
1899 return measure;
1900}
1901
1902HPDF_EXPORT(HPDF_3DMeasure)
1903HPDF_Page_CreatePD33DMeasure(HPDF_Page page,
1904 HPDF_Point3D annotationPlaneNormal,
1905 HPDF_Point3D firstAnchorPoint,
1906 HPDF_Point3D secondAnchorPoint,
1907 HPDF_Point3D leaderLinesDirection,
1908 HPDF_Point3D measurementValuePoint,
1909 HPDF_Point3D textYDirection,
1910 HPDF_REAL value,
1911 const char* unitsString
1912 )
1913{
1914 HPDF_PageAttr attr;
1915 HPDF_Annotation measure;
1916
1917 HPDF_PTRACE((" HPDF_Page_CreatePD33DMeasure\n"));
1918
1919 if (!HPDF_Page_Validate (page))
1920 return NULL;
1921
1922 attr = (HPDF_PageAttr)page->attr;
1923
1924 measure = HPDF_PD33DMeasure_New(page->mmgr,
1925 attr->xref,
1926 annotationPlaneNormal,
1927 firstAnchorPoint,
1928 secondAnchorPoint,
1929 leaderLinesDirection,
1930 measurementValuePoint,
1931 textYDirection,
1932 value,
1933 unitsString
1934 );
1935 if ( !measure)
1936 HPDF_CheckError (page->error);
1937
1938 return measure;
1939}
1940
1941
1942HPDF_EXPORT(HPDF_ExData)
1943HPDF_Page_Create3DAnnotExData(HPDF_Page page)
1944{
1945 HPDF_PageAttr attr;
1946 HPDF_Annotation exData;
1947
1948 HPDF_PTRACE((" HPDF_Page_Create3DAnnotExData\n"));
1949
1950 if (!HPDF_Page_Validate (page))
1951 return NULL;
1952
1953 attr = (HPDF_PageAttr)page->attr;
1954
1955 exData = HPDF_3DAnnotExData_New(page->mmgr, attr->xref);
1956 if ( !exData)
1957 HPDF_CheckError (page->error);
1958
1959 return exData;
1960}
1961
1962
1963
1964void
1965HPDF_Page_SetFilter (HPDF_Page page,
1966 HPDF_UINT filter)
1967{
1968 HPDF_PageAttr attr;
1969
1970 HPDF_PTRACE((" HPDF_Page_SetFilter\n"));
1971
1972 attr = (HPDF_PageAttr)page->attr;
1973 attr->contents->filter = filter;
1974}
1975
1976