-
Notifications
You must be signed in to change notification settings - Fork 0
/
cairo_wrap.h
653 lines (486 loc) · 14.3 KB
/
cairo_wrap.h
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
* Lua interface for the cairo graphics library
*
Copyright 2007 Niels Martin Hansen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Contact:
E-mail: <jiifurusu@gmail.com>
IRC: jfs in #aegisub on irc.rizon.net
*/
#ifndef CAIRO_WRAP_H
#define CAIRO_WRAP_H
#include "../lua51/src/lua.h"
#include <cairo.h>
#include <string>
#include <map>
int luaopen_cairo(lua_State *L);
template <class ChildClass>
class LuaCairoBase {
private:
// Default handlers for metatable stuff
static int lua_index(lua_State *L)
{
LuaCairoBase **obj = (LuaCairoBase**)lua_touserdata(L, 1);
return (*obj)->internal_lua_index(L);
}
static int lua_newindex(lua_State *L)
{
LuaCairoBase **obj = (LuaCairoBase**)lua_touserdata(L, 1);
return (*obj)->internal_lua_newindex(L);
}
static int lua_callobj(lua_State *L)
{
LuaCairoBase **obj = (LuaCairoBase**)lua_touserdata(L, 1);
return (*obj)->internal_lua_callobj(L);
}
static int lua_gc(lua_State *L)
{
LuaCairoBase **obj = (LuaCairoBase**)lua_touserdata(L, 1);
delete *obj;
return 0;
}
// Hidden constructors
LuaCairoBase() { }
LuaCairoBase(const LuaCairoBase &obj) { }
// List of callables
typedef std::map<std::string, lua_CFunction> CallableMap;
CallableMap callables;
// "Magic" value - used to test that pointers are really valid
lua_CFunction magic;
protected:
virtual int internal_lua_index(lua_State *L)
{
if (lua_type(L, 2) == LUA_TSTRING) {
const char *field = lua_tostring(L, 2);
CallableMap::iterator func = callables.find(field);
if (func != callables.end()) {
lua_pushvalue(L, 1);
lua_pushcclosure(L, func->second, 1);
return 1;
}
if (strcmp("_type", lua_tostring(L, 2))) {
lua_pushstring(L, GetTypeName());
return 1;
}
}
return 0;
}
virtual int internal_lua_newindex(lua_State *L)
{
lua_pushfstring(L, "Cairo object of type '%s' can not have field set", GetTypeName());
lua_error(L);
return 0;
}
virtual int internal_lua_callobj(lua_State *L)
{
lua_pushfstring(L, "Cairo objct of type '%s' can not be called", GetTypeName());
lua_error(L);
return 0;
}
virtual const char *GetTypeName() { return "_base"; }
virtual void CreateMetaTable(lua_State *L)
{
lua_newtable(L);
lua_pushcclosure(L, lua_index, 0);
lua_setfield(L, -2, "__index");
lua_pushcclosure(L, lua_newindex, 0);
lua_setfield(L, -2, "__newindex");
lua_pushcclosure(L, lua_callobj, 0);
lua_setfield(L, -2, "__call");
lua_pushcclosure(L, lua_gc, 0);
lua_setfield(L, -2, "__gc");
}
void AddCallable(lua_CFunction func, const char *name)
{
callables[name] = func;
}
// Primary constructor for use with inherited stuff
LuaCairoBase(lua_State *L)
{
LuaCairoBase **ud = (LuaCairoBase**)lua_newuserdata(L, sizeof(ChildClass*));
*ud = this;
CreateMetaTable(L);
lua_setmetatable(L, -2);
magic = luaopen_cairo;
}
public:
virtual ~LuaCairoBase() { }
// Helper: Get the object pointer from a callback
static ChildClass *GetObjPointer(lua_State *L, int index)
{
if (!lua_isuserdata(L, index)) {
lua_pushliteral(L, "Passed non-userdata where one expected");
lua_error(L);
}
ChildClass *obj = CheckPointer(*(void**)lua_touserdata(L, index));
return obj;
}
// Check whether a pointer plausibly points to an object of this class
static ChildClass *CheckPointer(void *ptr)
{
ChildClass *testptr = (ChildClass*)ptr;
if (testptr->magic == luaopen_cairo)
return testptr;
else
return 0;
}
};
#define CALLABLE(name) static int lua_ ## name (lua_State *L)
class LuaCairoContext : public LuaCairoBase<LuaCairoContext> {
private:
cairo_t *context;
CALLABLE(reference);
CALLABLE(status);
CALLABLE(save);
CALLABLE(restore);
CALLABLE(get_target);
CALLABLE(push_group);
CALLABLE(push_group_with_content);
CALLABLE(pop_group);
CALLABLE(pop_group_to_source);
CALLABLE(get_group_target);
CALLABLE(set_source_rgb);
CALLABLE(set_source_rgba);
CALLABLE(set_source);
CALLABLE(set_source_surface);
CALLABLE(get_source);
CALLABLE(set_antialias);
CALLABLE(get_antialias);
CALLABLE(set_dash);
CALLABLE(get_dash_count);
CALLABLE(get_dash);
CALLABLE(set_fill_rule);
CALLABLE(get_fill_rule);
CALLABLE(set_line_cap);
CALLABLE(get_line_cap);
CALLABLE(set_line_join);
CALLABLE(get_line_join);
CALLABLE(set_line_width);
CALLABLE(get_line_width);
CALLABLE(set_miter_limit);
CALLABLE(get_miter_limit);
CALLABLE(set_operator);
CALLABLE(get_operator);
CALLABLE(set_tolerance);
CALLABLE(get_tolerance);
CALLABLE(clip);
CALLABLE(clip_preserve);
CALLABLE(clip_extents);
CALLABLE(reset_clip);
// rectangle_list_destroy is not needed,
// copy_clip_rectangle_list will convert the rect list into a pure Lua structure
CALLABLE(copy_clip_rectangle_list);
CALLABLE(fill);
CALLABLE(fill_preserve);
CALLABLE(fill_extents);
CALLABLE(in_fill);
CALLABLE(mask);
CALLABLE(mask_surface);
CALLABLE(paint);
CALLABLE(paint_with_alpha);
CALLABLE(stroke);
CALLABLE(stroke_preserve);
CALLABLE(stroke_extents);
CALLABLE(in_stroke);
CALLABLE(copy_page);
CALLABLE(show_page);
// Path operations
CALLABLE(copy_path);
CALLABLE(copy_path_flat);
CALLABLE(append_path);
CALLABLE(get_current_point);
CALLABLE(new_path);
CALLABLE(new_sub_path);
CALLABLE(close_path);
CALLABLE(arc);
CALLABLE(arc_negative);
CALLABLE(curve_to);
CALLABLE(line_to);
CALLABLE(move_to);
CALLABLE(rectangle);
CALLABLE(glyph_path);
CALLABLE(text_path);
CALLABLE(rel_curve_to);
CALLABLE(rel_line_to);
CALLABLE(rel_move_to);
// Transformations
CALLABLE(translate);
CALLABLE(scale);
CALLABLE(rotate);
CALLABLE(transform);
CALLABLE(set_matrix);
CALLABLE(get_matrix);
CALLABLE(identity_matrix);
CALLABLE(user_to_device);
CALLABLE(user_to_device_distance);
CALLABLE(device_to_user);
CALLABLE(device_to_user_distance);
// Text/font operations
CALLABLE(select_font_face);
CALLABLE(set_font_size);
CALLABLE(set_font_matrix);
CALLABLE(get_font_matrix);
CALLABLE(set_font_options);
CALLABLE(get_font_options);
CALLABLE(set_font_face);
CALLABLE(get_font_face);
CALLABLE(set_scaled_font);
CALLABLE(get_scaled_font);
CALLABLE(show_text);
CALLABLE(show_glyphs);
CALLABLE(font_extents);
CALLABLE(text_extents);
CALLABLE(glyph_extents);
protected:
const char *GetTypeName();
public:
// Create another reference for a context
LuaCairoContext(lua_State *L, cairo_t *_context);
// Destructor
virtual ~LuaCairoContext();
};
class LuaCairoSurface : public LuaCairoBase<LuaCairoSurface> {
private:
CALLABLE(create_similar);
CALLABLE(reference);
CALLABLE(status);
// Create cairo context for this surface
// This deviates from the regular cairo API
CALLABLE(create_context);
CALLABLE(finish);
CALLABLE(flush);
CALLABLE(get_font_options);
CALLABLE(get_content);
CALLABLE(mark_dirty);
CALLABLE(mark_dirty_rectangle);
CALLABLE(set_device_offset);
CALLABLE(get_device_offset);
CALLABLE(set_fallback_resolution);
CALLABLE(get_type);
// Image surface functions
CALLABLE(image_get_format);
CALLABLE(image_get_width);
CALLABLE(image_get_height);
// These replace the get_data and get_stride functions
CALLABLE(image_set_pixel);
CALLABLE(image_get_pixel);
protected:
// Protected because inheriting classes might want it too
cairo_surface_t *surface;
const char *GetTypeName();
// For child classes that will set surface themselves
LuaCairoSurface(lua_State *L);
public:
// Create another reference for a surface
LuaCairoSurface(lua_State *L, cairo_surface_t *_surface);
// Destructor
virtual ~LuaCairoSurface();
cairo_surface_t *GetSurface() { return surface; }
// Creation functions - these aren't in image surface objects but in a global table
CALLABLE(image_surface_create);
};
class LuaCairoFontFace : public LuaCairoBase<LuaCairoFontFace> {
private:
cairo_font_face_t *font_face;
CALLABLE(create_scaled_font);
CALLABLE(reference);
CALLABLE(status);
CALLABLE(get_type);
protected:
const char *GetTypeName();
public:
// Create another reference for a font face
LuaCairoFontFace(lua_State *L, cairo_font_face_t *_font_face);
// Destructor
virtual ~LuaCairoFontFace();
cairo_font_face_t *GetFontFace() { return font_face; }
};
class LuaCairoScaledFont : public LuaCairoBase<LuaCairoScaledFont> {
private:
cairo_scaled_font_t *scaled_font;
CALLABLE(reference);
CALLABLE(status);
CALLABLE(extents);
CALLABLE(text_extents);
CALLABLE(glyph_extents);
CALLABLE(get_font_face);
CALLABLE(get_font_options);
CALLABLE(get_font_matrix);
CALLABLE(get_ctm);
CALLABLE(get_type);
protected:
const char *GetTypeName();
public:
// Create another reference for a scaled font
LuaCairoScaledFont(lua_State *L, cairo_scaled_font_t *_scaled_font);
// Destructor
virtual ~LuaCairoScaledFont();
cairo_scaled_font_t *GetScaledFont() { return scaled_font; }
};
class LuaCairoFontOptions : public LuaCairoBase<LuaCairoFontOptions> {
private:
cairo_font_options_t *font_options;
bool owned;
CALLABLE(copy);
CALLABLE(status);
CALLABLE(merge);
CALLABLE(hash);
CALLABLE(equal);
CALLABLE(set_antialias);
CALLABLE(get_antialias);
CALLABLE(set_subpixel_order);
CALLABLE(get_subpixel_order);
CALLABLE(set_hint_style);
CALLABLE(get_hint_style);
CALLABLE(set_hint_metrics);
CALLABLE(get_hint_metrics);
void RegFontOptionsCallables();
protected:
const char *GetTypeName();
public:
// Create a new font options object - will be owned
LuaCairoFontOptions(lua_State *L);
// Wrap an existing font options object - will not be owned
LuaCairoFontOptions(lua_State *L, cairo_font_options_t *_font_options);
// Destructor - only destroy font_options if owned
virtual ~LuaCairoFontOptions();
cairo_font_options_t *GetFontOptions() { return font_options; }
// Creation function - global
CALLABLE(create);
};
class LuaCairoMatrix : public LuaCairoBase<LuaCairoMatrix> {
private:
cairo_matrix_t matrix;
CALLABLE(init);
CALLABLE(init_identity);
CALLABLE(init_translate);
CALLABLE(init_scale);
CALLABLE(init_rotate);
CALLABLE(translate);
CALLABLE(scale);
CALLABLE(rotate);
// Matrix inversion
CALLABLE(invert);
// Matrix multiplication
CALLABLE(multiply);
CALLABLE(transform_distance);
CALLABLE(transform_point);
// Pointwise arithmetic on matrices - not part of cairo API
CALLABLE(op_add);
CALLABLE(op_sub);
CALLABLE(op_mul);
CALLABLE(op_div);
CALLABLE(op_unm);
// Equality operator
CALLABLE(op_eq);
// Not in cairo API
CALLABLE(copy);
void RegMatrixCallables(lua_State *L);
protected:
virtual int internal_lua_index(lua_State *L);
virtual int internal_lua_newindex(lua_State *L);
const char *GetTypeName();
// Extend the meta table with various operators
void CreateMetaTable(lua_State *L);
public:
// Create new matrix, inited to identity matrix
LuaCairoMatrix(lua_State *L);
// Duplicate exixting matrix
LuaCairoMatrix(lua_State *L, const cairo_matrix_t *_matrix);
// Destructor
virtual ~LuaCairoMatrix();
cairo_matrix_t *GetMatrix();
// Creation function - global
CALLABLE(create);
};
class LuaCairoPath : public LuaCairoBase<LuaCairoPath> {
private:
cairo_path_t *path;
// Specifies whether the memory for the cairo_path_t object is owned by the cairo library.
// If cairo owns it we cannot add/remove elements from the path.
bool cairo_owns_memory;
// Number of path elemts we have allocated memory for. Undefined if cairo_owns_memory.
// This is different from path->length because that specifies the number of elements in use.
size_t path_elements_allocated;
// Management
void EnsurePathOwned(); // ensure that we own the path memory
void EnsureSpaceFor(size_t n); // ensure we own the path memory and there's space to add at least n more elements
// TODO: figure out what methods are needed
// Something to iterate over the parts at least
// Support for creating/modifying paths?
CALLABLE(clear);
CALLABLE(move_to);
CALLABLE(line_to);
CALLABLE(curve_to);
CALLABLE(close);
// Functional programming support
CALLABLE(map); // transform each path segment with a function
CALLABLE(map_coords); // transform each coordinate pair with a function
CALLABLE(fold); // fold path segments into a single result value
CALLABLE(fold_coords); // fold coordinate pairs into a single result value
void RegPathCallables(lua_State *L);
protected:
virtual int internal_lua_index(lua_State *L);
const char *GetTypeName();
public:
// Create object with new path - we will own the memory
LuaCairoPath(lua_State *L);
// Create object based on path - does not copy path, and lets cairo own the memory
LuaCairoPath(lua_State *L, cairo_path_t *_path);
// Destructor
virtual ~LuaCairoPath();
cairo_path_t *GetPath() { return path; }
// Modifying the path
void ClearPath();
void MoveTo(double x, double y);
void LineTo(double x, double y);
void CurveTo(double x0, double y0, double x1, double y1, double x2, double y2);
void ClosePath();
};
class LuaCairoPattern : public LuaCairoBase<LuaCairoPattern> {
private:
cairo_pattern_t *pattern;
CALLABLE(add_color_stop_rgb);
CALLABLE(add_color_stop_rgba);
CALLABLE(get_color_stop_count);
CALLABLE(get_color_stop_rgba);
CALLABLE(get_rgba);
CALLABLE(get_surface);
CALLABLE(get_linear_points);
CALLABLE(get_radial_circles);
CALLABLE(reference);
CALLABLE(status);
CALLABLE(set_extend);
CALLABLE(get_extend);
CALLABLE(set_filter);
CALLABLE(get_filter);
CALLABLE(set_matrix);
CALLABLE(get_matrix);
CALLABLE(get_type);
protected:
const char *GetTypeName();
public:
// Create another reference for a pattern object
LuaCairoPattern(lua_State *L, cairo_pattern_t *_pattern);
// Destructor
virtual ~LuaCairoPattern();
cairo_pattern_t *GetPattern() { return pattern; }
// Creation functions - these aren't in pattern objects but in a global table
CALLABLE(create_rgb);
CALLABLE(create_rgba);
CALLABLE(create_for_surface);
CALLABLE(create_linear);
CALLABLE(create_radial);
};
#undef CALLABLE
#endif