summaryrefslogtreecommitdiffstats
path: root/libs/ucode-mod-plutosvg/src/plutosvg.c
blob: 3e56fe5b5313a9c92fa183c10ebc46484320fec8 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright (C) 2026 Daniel Golle <daniel@makrotopia.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include <string.h>

#include <plutosvg/plutosvg.h>
#include <uc-plutovg.h>

#define UC_PSVG_DOCUMENT "plutosvg.document"

typedef struct {
	uc_value_t *palette;
	uc_vm_t *vm;
} uc_psvg_palette_t;

static bool
uc_psvg_palette_lookup(uc_psvg_palette_t *ctx, const char *name, int length,
                       plutovg_color_t *color)
{
	uc_value_t *val;
	char *key;
	bool ok;

	key = strndup(name, (size_t)length);

	if (!key)
		return false;

	val = ucv_object_get(ctx->palette, key, NULL);
	ok = uc_pvg_color_get(val, color);
	free(key);

	return ok;
}

static bool
uc_psvg_palette_call(uc_psvg_palette_t *ctx, const char *name, int length,
                     plutovg_color_t *color)
{
	uc_value_t *rv;
	bool ok;

	uc_vm_stack_push(ctx->vm, ucv_get(ctx->palette));
	uc_vm_stack_push(ctx->vm, ucv_string_new_length(name, (size_t)length));

	if (uc_vm_call(ctx->vm, false, 1) != EXCEPTION_NONE)
		return false;

	rv = uc_vm_stack_pop(ctx->vm);
	ok = uc_pvg_color_get(rv, color);
	ucv_put(rv);

	return ok;
}

static bool
uc_psvg_palette_cb(void *closure, const char *name, int length,
                   plutovg_color_t *color)
{
	uc_psvg_palette_t *ctx = closure;

	if (ucv_type(ctx->palette) == UC_OBJECT)
		return uc_psvg_palette_lookup(ctx, name, length, color);

	if (ucv_is_callable(ctx->palette))
		return uc_psvg_palette_call(ctx, name, length, color);

	return false;
}

static void
uc_psvg_document_free(void *ptr)
{
	plutosvg_document_destroy(ptr);
}

/* a short string lives inside the variable holding the value */
static bool
uc_psvg_id_check(uc_vm_t *vm, uc_value_t *uv)
{
	if (uv == NULL || ucv_type(uv) == UC_NULL || ucv_type(uv) == UC_STRING)
		return true;

	uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Expecting element id string");

	return false;
}

static const plutovg_color_t *
uc_psvg_color_opt(uc_vm_t *vm, uc_value_t *uv, plutovg_color_t *storage,
                  bool *ok)
{
	*ok = true;

	if (uv == NULL || ucv_type(uv) == UC_NULL)
		return NULL;

	if (uc_pvg_color_get(uv, storage))
		return storage;

	uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Invalid color value");
	*ok = false;

	return NULL;
}

static plutosvg_palette_func_t
uc_psvg_palette_opt(uc_value_t *uv, uc_psvg_palette_t *ctx, uc_vm_t *vm)
{
	if (uv == NULL || ucv_type(uv) == UC_NULL)
		return NULL;

	if (ucv_type(uv) != UC_OBJECT && !ucv_is_callable(uv))
		return NULL;

	ctx->palette = uv;
	ctx->vm = vm;

	return uc_psvg_palette_cb;
}

static uc_value_t *
uc_psvg_version(uc_vm_t *vm, size_t nargs)
{
	return ucv_int64_new(plutosvg_version());
}

static uc_value_t *
uc_psvg_version_string(uc_vm_t *vm, size_t nargs)
{
	return ucv_string_new(plutosvg_version_string());
}

static uc_value_t *
uc_psvg_document_wrap(uc_vm_t *vm, plutosvg_document_t *doc)
{
	uc_value_t *rv;

	if (!doc)
		return NULL;

	rv = ucv_resource_create(vm, UC_PSVG_DOCUMENT, doc);

	if (!rv)
		plutosvg_document_destroy(doc);

	return rv;
}

static uc_value_t *
uc_psvg_document_from_data(uc_vm_t *vm, size_t nargs)
{
	uc_value_t *arg = uc_fn_arg(0);
	plutosvg_document_t *doc;
	size_t len;
	char *copy;

	if (ucv_type(arg) != UC_STRING) {
		uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Expecting SVG data");

		return NULL;
	}

	len = ucv_string_length(arg);
	copy = malloc(len);

	if (!copy)
		return NULL;

	memcpy(copy, ucv_string_get(arg), len);

	doc = plutosvg_document_load_from_data(copy, (int)len,
		uc_pvg_number(uc_fn_arg(1), -1.0f),
		uc_pvg_number(uc_fn_arg(2), -1.0f), free, copy);

	if (!doc) {
		free(copy);

		return NULL;
	}

	return uc_psvg_document_wrap(vm, doc);
}

static uc_value_t *
uc_psvg_document_from_file(uc_vm_t *vm, size_t nargs)
{
	uc_value_t *arg = uc_fn_arg(0);

	if (ucv_type(arg) != UC_STRING) {
		uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Expecting file name");

		return NULL;
	}

	return uc_psvg_document_wrap(vm,
		plutosvg_document_load_from_file(ucv_string_get(arg),
			uc_pvg_number(uc_fn_arg(1), -1.0f),
			uc_pvg_number(uc_fn_arg(2), -1.0f)));
}

static uc_value_t *
uc_psvg_document_width(uc_vm_t *vm, size_t nargs)
{
	plutosvg_document_t *doc = uc_fn_thisval(UC_PSVG_DOCUMENT);

	if (!doc)
		return NULL;

	return ucv_double_new(plutosvg_document_get_width(doc));
}

static uc_value_t *
uc_psvg_document_height(uc_vm_t *vm, size_t nargs)
{
	plutosvg_document_t *doc = uc_fn_thisval(UC_PSVG_DOCUMENT);

	if (!doc)
		return NULL;

	return ucv_double_new(plutosvg_document_get_height(doc));
}

static uc_value_t *
uc_psvg_document_extents(uc_vm_t *vm, size_t nargs)
{
	plutosvg_document_t *doc = uc_fn_thisval(UC_PSVG_DOCUMENT);
	uc_value_t *idval = uc_fn_arg(0);
	plutovg_rect_t rect;
	const char *id;

	if (!doc || !uc_psvg_id_check(vm, idval))
		return NULL;

	id = (ucv_type(idval) == UC_STRING) ? ucv_string_get(idval) : NULL;

	if (!plutosvg_document_extents(doc, id, &rect))
		return NULL;

	return uc_pvg_rect_new(vm, &rect);
}

static uc_value_t *
uc_psvg_document_render(uc_vm_t *vm, size_t nargs)
{
	plutosvg_document_t *doc = uc_fn_thisval(UC_PSVG_DOCUMENT);
	plutosvg_palette_func_t palette_func;
	uc_value_t *idval = uc_fn_arg(1);
	const plutovg_color_t *current;
	plutovg_canvas_t *canvas;
	uc_psvg_palette_t ctx;
	plutovg_color_t color;
	const char *id;
	bool ok;

	if (!doc || !uc_psvg_id_check(vm, idval))
		return NULL;

	canvas = ucv_resource_data(uc_fn_arg(0), UC_PVG_CANVAS);

	if (!canvas) {
		uc_vm_raise_exception(vm, EXCEPTION_TYPE,
			"Expecting plutovg canvas object");

		return NULL;
	}

	id = (ucv_type(idval) == UC_STRING) ? ucv_string_get(idval) : NULL;
	current = uc_psvg_color_opt(vm, uc_fn_arg(2), &color, &ok);

	if (!ok)
		return NULL;

	palette_func = uc_psvg_palette_opt(uc_fn_arg(3), &ctx, vm);

	return ucv_boolean_new(plutosvg_document_render(doc, id, canvas,
		current, palette_func, &ctx));
}

static uc_value_t *
uc_psvg_document_render_to_surface(uc_vm_t *vm, size_t nargs)
{
	plutosvg_document_t *doc = uc_fn_thisval(UC_PSVG_DOCUMENT);
	plutosvg_palette_func_t palette_func;
	uc_value_t *idval = uc_fn_arg(0);
	const plutovg_color_t *current;
	plutovg_surface_t *surface;
	uc_psvg_palette_t ctx;
	plutovg_color_t color;
	uc_value_t *rv, *name;
	const char *id;
	bool ok;

	if (!doc || !uc_psvg_id_check(vm, idval))
		return NULL;

	if (!ucv_resource_type_lookup(vm, UC_PVG_SURFACE)) {
		name = ucv_string_new("plutovg");
		ucv_put(uc_vm_invoke(vm, "require", 1, name));
		ucv_put(name);
	}

	if (!ucv_resource_type_lookup(vm, UC_PVG_SURFACE)) {
		uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
			"The plutovg module is required to create surfaces");

		return NULL;
	}

	id = (ucv_type(idval) == UC_STRING) ? ucv_string_get(idval) : NULL;
	current = uc_psvg_color_opt(vm, uc_fn_arg(3), &color, &ok);

	if (!ok)
		return NULL;

	palette_func = uc_psvg_palette_opt(uc_fn_arg(4), &ctx, vm);

	surface = plutosvg_document_render_to_surface(doc, id,
		(int)uc_pvg_number(uc_fn_arg(1), -1.0f),
		(int)uc_pvg_number(uc_fn_arg(2), -1.0f), current,
		palette_func, &ctx);

	if (!surface)
		return NULL;

	rv = ucv_resource_create(vm, UC_PVG_SURFACE, surface);

	if (!rv)
		plutovg_surface_destroy(surface);

	return rv;
}

static const uc_function_list_t document_fns[] = {
	{ "width",		uc_psvg_document_width },
	{ "height",		uc_psvg_document_height },
	{ "extents",		uc_psvg_document_extents },
	{ "render",		uc_psvg_document_render },
	{ "render_to_surface",	uc_psvg_document_render_to_surface },
};

static const uc_function_list_t module_fns[] = {
	{ "version",		uc_psvg_version },
	{ "version_string",	uc_psvg_version_string },
	{ "document",		uc_psvg_document_from_data },
	{ "document_from_file",	uc_psvg_document_from_file },
};

void
uc_module_init(uc_vm_t *vm, uc_value_t *scope)
{
	uc_function_list_register(scope, module_fns);

	uc_type_declare(vm, UC_PSVG_DOCUMENT, document_fns,
		uc_psvg_document_free);
}