summaryrefslogtreecommitdiff
path: root/modules/cairo/seed-cairo-image-surface.c
blob: ccc871a636172aee58a58a4b2e19efa7ff840080 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */

/*
 * This file is part of Seed, the GObject Introspection<->Javascript bindings.
 *
 * Seed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2 of
 * the License, or (at your option) any later version.
 * Seed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seed.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
 */

#include <seed.h>
#include <cairo/cairo.h>

#include "seed-cairo.h"
#include "seed-cairo-surface.h"

#define CAIRO_SURFACE_PRIV(obj)                                                \
    ((cairo_surface_t*) seed_object_get_private(obj))

#define CHECK_SURFACE(obj)                                                     \
    ({                                                                         \
        if (!seed_object_is_of_class(ctx, obj,                                 \
                                     seed_cairo_image_surface_class)) {        \
            seed_make_exception(ctx, exception, "ArgumentError",               \
                                "Object is not a Cairo Surface");              \
            return seed_make_undefined(ctx);                                   \
        }                                                                      \
        if (!seed_object_get_private(obj)) {                                   \
            seed_make_exception(ctx, exception, "ArgumentError",               \
                                "Cairo surface has been destroyed");           \
            return seed_make_undefined(ctx);                                   \
        }                                                                      \
    })

#define CHECK_THIS()                                                           \
    if (!seed_object_get_private(this_object)) {                               \
        seed_make_exception(ctx, exception, "ArgumentError",                   \
                            "Cairo surface has been destroyed");               \
        return seed_make_undefined(ctx);                                       \
    }

SeedClass seed_cairo_image_surface_class;
SeedObject image_surface_constructor_ref;

static SeedObject
seed_object_from_cairo_image_surface(SeedContext ctx, cairo_surface_t* surf)
{
    SeedObject jsobj;

    jsobj = cairo_surface_get_user_data(surf, seed_get_cairo_key());
    if (jsobj)
        return jsobj;

    jsobj = seed_make_object(ctx, seed_cairo_image_surface_class, surf);
    cairo_surface_set_user_data(surf, seed_get_cairo_key(), jsobj,
                                seed_cairo_destroy_func);
    return jsobj;
}

static SeedValue
seed_cairo_image_surface_get_format(SeedContext ctx,
                                    SeedObject this_object,
                                    SeedString property_name,
                                    SeedException* exception)
{
    CHECK_THIS();
    return seed_value_from_long(ctx,
                                cairo_image_surface_get_format(
                                  seed_object_to_cairo_surface(ctx, this_object,
                                                               exception)),
                                exception);
}

static SeedValue
seed_cairo_image_surface_get_width(SeedContext ctx,
                                   SeedObject this_object,
                                   SeedString property_name,
                                   SeedException* exception)
{
    CHECK_THIS();
    return seed_value_from_int(ctx,
                               cairo_image_surface_get_width(
                                 seed_object_to_cairo_surface(ctx, this_object,
                                                              exception)),
                               exception);
}

static SeedValue
seed_cairo_image_surface_get_height(SeedContext ctx,
                                    SeedObject this_object,
                                    SeedString property_name,
                                    SeedException* exception)
{
    CHECK_THIS();
    return seed_value_from_int(ctx,
                               cairo_image_surface_get_height(
                                 seed_object_to_cairo_surface(ctx, this_object,
                                                              exception)),
                               exception);
}

static SeedValue
seed_cairo_image_surface_get_stride(SeedContext ctx,
                                    SeedObject this_object,
                                    SeedString property_name,
                                    SeedException* exception)
{
    CHECK_THIS();
    return seed_value_from_int(ctx,
                               cairo_image_surface_get_stride(
                                 seed_object_to_cairo_surface(ctx, this_object,
                                                              exception)),
                               exception);
}

static SeedObject
seed_cairo_construct_image_surface(SeedContext ctx,
                                   SeedObject constructor,
                                   size_t argument_count,
                                   const SeedValue arguments[],
                                   SeedException* exception)
{
    cairo_surface_t* ret;
    gint width, height;
    cairo_format_t format;
    if (argument_count != 3) {
        EXPECTED_EXCEPTION("ImageSurface", "3 arguments");
    }

    format = seed_value_to_long(ctx, arguments[0], exception);
    width = seed_value_to_int(ctx, arguments[1], exception);
    height = seed_value_to_int(ctx, arguments[2], exception);
    ret = cairo_image_surface_create(format, width, height);

    return seed_object_from_cairo_image_surface(ctx, ret);
}

seed_static_value image_surface_values[] = {
    { "format", seed_cairo_image_surface_get_format, 0,
      SEED_PROPERTY_ATTRIBUTE_READ_ONLY | SEED_PROPERTY_ATTRIBUTE_DONT_DELETE },
    { "width", seed_cairo_image_surface_get_width, 0,
      SEED_PROPERTY_ATTRIBUTE_READ_ONLY | SEED_PROPERTY_ATTRIBUTE_DONT_DELETE },
    { "height", seed_cairo_image_surface_get_height, 0,
      SEED_PROPERTY_ATTRIBUTE_READ_ONLY | SEED_PROPERTY_ATTRIBUTE_DONT_DELETE },
    { "stride", seed_cairo_image_surface_get_stride, 0,
      SEED_PROPERTY_ATTRIBUTE_READ_ONLY | SEED_PROPERTY_ATTRIBUTE_DONT_DELETE },
    { 0, 0, 0, 0 }
};

void
seed_define_cairo_image_surface(SeedContext ctx, SeedObject namespace_ref)
{
    seed_class_definition image_def = seed_empty_class;
    // TODO: FIX
    seed_static_function webkit_fail = { 0, 0, 0 };

    image_def.class_name = "ImageSurface";
    image_def.static_values = image_surface_values;
    image_def.parent_class = seed_get_cairo_surface_class();
    // FAIL
    image_def.static_functions = &webkit_fail;
    seed_cairo_image_surface_class = seed_create_class(&image_def);

    image_surface_constructor_ref
      = seed_make_constructor(ctx, NULL,
                              // seed_cairo_image_surface_class,
                              seed_cairo_construct_image_surface);
    seed_object_set_property(ctx, namespace_ref, "ImageSurface",
                             image_surface_constructor_ref);
}