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
|
#include "livecode/api/api.h"
namespace Inkscape {
namespace Livecode {
static Context *_context = nullptr;
Context &ctx() {
if (!_context) {
janet_panic("Livecode Context uninitialized!!");
}
return *_context;
}
extern "C" Janet cfun_input_point(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
ctx().input_point(janet_getcstring(argv, 0),
&janet_unwrap_point(argv[1]));
return janet_wrap_nil();
}
extern "C" Janet cfun_input_line(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
ctx().input_line(janet_getcstring(argv, 0),
&janet_unwrap_point(argv[1]),
&janet_unwrap_point(argv[2]));
return janet_wrap_nil();
}
extern "C" Janet cfun_input_arrow(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
ctx().input_arrow(janet_getcstring(argv, 0),
&janet_unwrap_point(argv[1]),
&janet_unwrap_point(argv[2]));
return janet_wrap_nil();
}
SPCSSAttr *get_css(Janet table) {
JanetKV const *kv = NULL;
SPCSSAttr *css = sp_repr_css_attr_new();
JanetDictView style = janet_getdictionary(&table, 0);
while ((kv = janet_dictionary_next(style.kvs, style.cap, kv))) {
if (!janet_checktype(kv->key, JANET_KEYWORD)) {
janet_panic(":style keys have to be keywords");
}
if (!janet_checktype(kv->value, JANET_STRING)) {
janet_panic(":style values have to be strings");
}
char const *key = (char const *)janet_unwrap_keyword(kv->key);
char const *value = (char const *)janet_unwrap_string(kv->value);
sp_repr_css_set_property(css, key, value);
}
return css;
}
Inkscape::XML::Node *get_node(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
char const *element = (char const *)janet_unwrap_keyword(argv[0]);
JanetDictView attrs = janet_getdictionary(argv, 1);
Inkscape::XML::Node *repr = ctx().createElement(element);
g_message("+ new node '%s'", element);
JanetKV const *kv = NULL;
while ((kv = janet_dictionary_next(attrs.kvs, attrs.cap, kv))) {
if (!janet_checktype(kv->key, JANET_KEYWORD)) {
janet_panic("attribute keys have to be keywords");
}
char const *key = (char const *)janet_unwrap_keyword(kv->key);
switch (janet_type(kv->value)) {
case JANET_NUMBER:
g_message(" setting '%s' = %f", key, janet_unwrap_number(kv->value));
sp_repr_set_svg_double(repr, key, janet_unwrap_number(kv->value));
break;
case JANET_STRING:
g_message(" setting '%s' = '%s'", key, janet_unwrap_string(kv->value));
repr->setAttribute(key, (gchar const *)janet_unwrap_string(kv->value));
break;
case JANET_STRUCT:
case JANET_TABLE: {
if (strcmp(key, "style") != 0) {
janet_panic("struct/table values are only allowed for :style");
}
Glib::ustring css_str;
SPCSSAttr *css = get_css(kv->value);
sp_repr_css_write_string(css, css_str);
sp_repr_css_attr_unref(css);
repr->setAttribute("style", css_str.c_str());
g_message(" setting '%s' = '%s'", key, css_str.c_str());
break;
}
default:
janet_panic("unsupported value type in attributes dict!");
break;
}
}
return repr;
}
extern "C" Janet cfun_draw_doc(int32_t argc, Janet *argv) {
char const *id = janet_getcstring(argv, 0);
ctx().draw_doc(id, get_node(argc - 1, argv + 1));
g_message("+ draw_doc id=%s", id);
return argv[0];
}
extern "C" Janet cfun_draw_ui(int32_t argc, Janet *argv) {
char const *id = janet_getcstring(argv, 0);
ctx().draw_ui(id, get_node(argc - 1, argv + 1));
g_message("+ draw_ui id=%s", id);
return argv[0];
}
const JanetReg it_cfuns[] = {
{
"input/point", cfun_input_point,
"(input/point id p)\n\nMake p modifiable via a handle.\n"
"This function mutates p."
},
{
"input/line", cfun_input_line,
"(input/line id p1 p2)\n\nMake a modifiable line between p1 and p2\n"
"This function mutates p1 and p2."
},
{
"input/arrow", cfun_input_arrow,
"(input/arrow id from to)\n\nMake a modifiable arrow between from and to\n"
"This function mutates from and to."
},
{
"draw/doc", cfun_draw_doc,
"(draw/doc id elem [attrs])\n\nDraw an SVG element to the document layer"
},
{
"draw/ui", cfun_draw_ui,
"(draw/ui id elem [attrs])\n\nDraw an SVG element to the UI layer"
},
{NULL, NULL, NULL}
};
void janet_lib_context(JanetTable *env, Context &context) {
_context = &context;
janet_cfuns(env, NULL, it_cfuns);
}
}
}
|