summaryrefslogtreecommitdiff
path: root/pdf/pdf_obj.h
blob: b5cdadd27d7e2adf26b2ca2f8638980512544e31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Copyright (C) 2020-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

#ifndef PDF_OBJECTS
#define PDF_OBJECTS

int pdfi_object_alloc(pdf_context *ctx, pdf_obj_type type, unsigned int size, pdf_obj **obj);
void pdfi_free_object(pdf_obj *o);
int pdfi_obj_to_string(pdf_context *ctx, pdf_obj *obj, byte **data, int *len);
int pdfi_obj_dict_to_stream(pdf_context *ctx, pdf_dict *dict, pdf_stream **stream, bool do_convert);
int pdfi_stream_to_dict(pdf_context *ctx, pdf_stream *stream, pdf_dict **dict);
int pdfi_obj_charstr_to_string(pdf_context *ctx, const char *charstr, pdf_string **string);
int pdfi_obj_charstr_to_name(pdf_context *ctx, const char *charstr, pdf_name **name);
int pdfi_obj_get_label(pdf_context *ctx, pdf_obj *obj, char **label);
int pdfi_num_alloc(pdf_context *ctx, double d, pdf_num **num);

static inline int
pdfi_obj_to_real(pdf_context *ctx, pdf_obj *obj, double *d)
{
    pdf_num *num = (pdf_num *)obj;

    switch (pdfi_type_of(num)) {
        case PDF_INT:
            *d = (double)num->value.i;
            break;
        case PDF_REAL:
            *d = num->value.d;
            break;
        default:
            return_error(gs_error_typecheck);
    }

    return 0;
}

static inline int
pdfi_obj_to_float(pdf_context *ctx, pdf_obj *obj, float *f)
{
    pdf_num *num = (pdf_num *)obj;

    switch (pdfi_type_of(num)) {
        case PDF_INT:
            *f = (float)num->value.i;
            break;
        case PDF_REAL:
            *f = (float)num->value.d;
            break;
        default:
            return_error(gs_error_typecheck);
    }

    return 0;
}

static inline int
pdfi_obj_to_int(pdf_context *ctx, pdf_obj *obj, int64_t *i)
{
    pdf_num *num = (pdf_num *)obj;
    int64_t tmp;

    switch (pdfi_type_of(num)) {
        case PDF_INT:
            *i = num->value.i;
            break;
        case PDF_REAL:
            /* We shouldn't be given a real here. We will grudgingly accept
             * (with a warning) an int given as a real, but will error out
             * otherwise. If we find a case where we need to accept reals
             * as ints, we'll do a new version of this function called something
             * like pdfi_obj_real_as_int what will just cast it down. */
            tmp = (int64_t)num->value.d;
            if ((double)tmp != num->value.d) {
                return_error(gs_error_typecheck);
            }
            pdfi_set_warning(ctx, 0, NULL, W_PDF_INT_AS_REAL, "pdfi_obj_to_int", NULL);
            *i = tmp;
            break;
        default:
            return_error(gs_error_typecheck);
    }

    return 0;
}

/* NOTE: the buffer object takes ownership of "data" */
static inline int
pdfi_buffer_set_data(pdf_obj *o, byte *data, int32_t length)
{
    pdf_buffer *b = (pdf_buffer *)o;
    if (pdfi_type_of(b) != PDF_BUFFER) {
        return_error(gs_error_typecheck);
    }

    if (b->data) {
        gs_free_object(OBJ_MEMORY(b), b->data, "pdfi_buffer_set_data(data)");
    }
    b->data = data;
    b->length = length;
    return 0;
}


#endif