Debian Codegraph @

pdf/pdf_obj.h

raw
1 /* Copyright (C) 2020-2024 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 #ifndef PDF_OBJECTS
17 #define PDF_OBJECTS
18
19 int pdfi_object_alloc(pdf_context *ctx, pdf_obj_type type, unsigned int size, pdf_obj **obj);
20 void pdfi_free_object(pdf_obj *o);
21 int pdfi_obj_to_string(pdf_context *ctx, pdf_obj *obj, byte **data, int *len);
22 int pdfi_obj_dict_to_stream(pdf_context *ctx, pdf_dict *dict, pdf_stream **stream, bool do_convert);
23 int pdfi_get_stream_dict(pdf_context *ctx, pdf_stream *stream, pdf_dict **dict);
24 int pdfi_obj_charstr_to_string(pdf_context *ctx, const char *charstr, pdf_string **string);
25 int pdfi_obj_charstr_to_name(pdf_context *ctx, const char *charstr, pdf_name **name);
26 int pdfi_obj_get_label(pdf_context *ctx, pdf_obj *obj, char **label);
27 int pdfi_num_alloc(pdf_context *ctx, double d, pdf_num **num);
28
29 static inline int
30 pdfi_obj_to_real(pdf_context *ctx, pdf_obj *obj, double *d)
31 {
32 pdf_num *num = (pdf_num *)obj;
33
34 switch (pdfi_type_of(num)) {
35 case PDF_INT:
36 *d = (double)num->value.i;
37 break;
38 case PDF_REAL:
39 *d = num->value.d;
40 break;
41 default:
42 return_error(gs_error_typecheck);
43 }
44
45 return 0;
46 }
47
48 static inline int
49 pdfi_obj_to_float(pdf_context *ctx, pdf_obj *obj, float *f)
50 {
51 pdf_num *num = (pdf_num *)obj;
52
53 switch (pdfi_type_of(num)) {
54 case PDF_INT:
55 *f = (float)num->value.i;
56 break;
57 case PDF_REAL:
58 *f = (float)num->value.d;
59 break;
60 default:
61 return_error(gs_error_typecheck);
62 }
63
64 return 0;
65 }
66
67 static inline int
68 pdfi_obj_to_int(pdf_context *ctx, pdf_obj *obj, int64_t *i)
69 {
70 pdf_num *num = (pdf_num *)obj;
71 int64_t tmp;
72
73 switch (pdfi_type_of(num)) {
74 case PDF_INT:
75 *i = num->value.i;
76 break;
77 case PDF_REAL:
78 /* We shouldn't be given a real here. We will grudgingly accept
79 * (with a warning) an int given as a real, but will error out
80 * otherwise. If we find a case where we need to accept reals
81 * as ints, we'll do a new version of this function called something
82 * like pdfi_obj_real_as_int what will just cast it down. */
83 tmp = (int64_t)num->value.d;
84 if ((double)tmp != num->value.d) {
85 return_error(gs_error_typecheck);
86 }
87 pdfi_set_warning(ctx, 0, NULL, W_PDF_INT_AS_REAL, "pdfi_obj_to_int", NULL);
88 *i = tmp;
89 break;
90 default:
91 return_error(gs_error_typecheck);
92 }
93
94 return 0;
95 }
96
97 /* NOTE: the buffer object takes ownership of "data" */
98 static inline int
99 pdfi_buffer_set_data(pdf_obj *o, byte *data, int32_t length)
100 {
101 pdf_buffer *b = (pdf_buffer *)o;
102 if (pdfi_type_of(b) != PDF_BUFFER) {
103 return_error(gs_error_typecheck);
104 }
105
106 if (b->data) {
107 gs_free_object(OBJ_MEMORY(b), b->data, "pdfi_buffer_set_data(data)");
108 }
109 b->data = data;
110 b->length = length;
111 return 0;
112 }
113
114
115 #endif