summaryrefslogtreecommitdiff
path: root/source/luametatex/source/lua/lmtlanguagelib.c
blob: 55646e3efaf7920cf8b32b217e8b79def3530b58 (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
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
/*
    See license.txt in the root of this project.
*/

/*tex

    This is the interface to everything that relates to hyphenation in the frontend: defining
    a new language, setting properties for hyphenation, loading patterns and exceptions.

*/

# include "luametatex.h"

# define LANGUAGE_METATABLE "luatex.language"
# define LANGUAGE_FUNCTIONS "luatex.language.wordhandlers"

/* todo: get rid of top */

typedef struct languagelib_language {
    tex_language *lang;
} languagelib_language;

static int languagelib_new(lua_State *L)
{
    languagelib_language *ulang = lua_newuserdatauv(L, sizeof(tex_language *), 0);
    if (lua_type(L, 1) == LUA_TNUMBER) {
        halfword lualang = lmt_tohalfword(L, 1);
        ulang->lang = tex_get_language(lualang);
        if (! ulang->lang) {
            return luaL_error(L, "undefined language %d", lualang);
        }
    } else {
        ulang->lang = tex_new_language(-1);
        if (! ulang->lang) {
            return luaL_error(L, "no room for a new language");
        }
    }
    luaL_getmetatable(L, LANGUAGE_METATABLE);
    lua_setmetatable(L, -2);
    return 1;
}

static tex_language *languagelib_object(lua_State* L)
{
    tex_language *lang = NULL;
    switch (lua_type(L, 1)) {
        case LUA_TNUMBER:
            lang = tex_get_language(lmt_tohalfword(L, 1));
            break;
        case LUA_TUSERDATA:
            {
                languagelib_language *ulang = lua_touserdata(L, 1);
                if (ulang && lua_getmetatable(L, 1)) {
                    luaL_getmetatable(L, LANGUAGE_METATABLE);
                    if (lua_rawequal(L, -1, -2)) {
                        lang = ulang->lang;
                    }
                    lua_pop(L, 2);
                }
                break;
            }
        case LUA_TBOOLEAN:
            if (lua_toboolean(L, 1)) {
                lang = tex_get_language(language_par);
            }
            break;
    }
    if (! lang) {
        luaL_error(L, "argument should be a valid language id, language object, or true");
    }
    return lang;
}

static int languagelib_id(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    lua_pushinteger(L, lang->id);
    return 1;
}

static int languagelib_patterns(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_gettop(L) == 1) {
        if (lang->patterns) {
            lua_pushstring(L, (char *) hnj_dictionary_tostring(lang->patterns));
        } else {
            lua_pushnil(L);
        }
        return 1;
    } else if (lua_type(L, 2) == LUA_TSTRING) {
        tex_load_patterns(lang, (const unsigned char *) lua_tostring(L, 2));
        return 0;
    } else {
        return luaL_error(L, "argument should be a string");
    }
}

static int languagelib_clear_patterns(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    tex_clear_patterns(lang);
    return 0;
}

static int languagelib_hyphenation(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_gettop(L) == 1) {
        if (lang->exceptions) {
            luaL_Buffer b;
            int done = 0;
            luaL_buffinit(L, &b);
            if (lua_rawgeti(L, LUA_REGISTRYINDEX, lang->exceptions) == LUA_TTABLE) {
                lua_pushnil(L);
                while (lua_next(L, -2)) {
                    if (done) {
                        luaL_addlstring(&b, " ", 1);
                    } else {
                        done = 1;
                    }
                    luaL_addvalue(&b);
                }
            }
            luaL_pushresult(&b);
        } else {
            lua_pushnil(L);
        }
        return 1;
    } else if (lua_type(L, 2) == LUA_TSTRING) {
        tex_load_hyphenation(lang, (const unsigned char *) lua_tostring(L, 2));
        return 0;
    } else {
        return luaL_error(L, "argument should be a string");
    }
}

static int languagelib_pre_hyphen_char(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_gettop(L) == 1) {
        lua_pushinteger(L, lang->pre_hyphen_char);
        return 1;
    } else if (lua_type(L, 2) == LUA_TNUMBER) {
        lang->pre_hyphen_char = lmt_tohalfword(L, 2);
    } else {
        return luaL_error(L, "argument should be a character number");
    }
    return 0;
}

static int languagelib_post_hyphen_char(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_gettop(L) == 1) {
        lua_pushinteger(L, lang->post_hyphen_char);
        return 1;
    } else if (lua_type(L, 2) == LUA_TNUMBER) {
        lang->post_hyphen_char = lmt_tohalfword(L, 2);
    } else {
        return luaL_error(L, "argument should be a character number");
    }
    return 0;
}

static int languagelib_pre_exhyphen_char(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_gettop(L) == 1) {
        lua_pushinteger(L, lang->pre_exhyphen_char);
        return 1;
    } else if (lua_type(L, 2) == LUA_TNUMBER) {
        lang->pre_exhyphen_char = lmt_tohalfword(L, 2);
        return 0;
    } else {
        return luaL_error(L, "argument should be a character number");
    }
}

/* We push nuts! */

int lmt_handle_word(tex_language *lang, const char *original, const char *word, int length, halfword first, halfword last, char **replacement)
{
    if (lang->wordhandler && word && first && last) {
        lua_State *L = lmt_lua_state.lua_instance;
        int stacktop = lua_gettop(L);
        int result = 0;
        int res;
        *replacement = NULL;
        lua_pushcfunction(L, lmt_traceback); /* goes before function */
        lua_rawgeti(L, LUA_REGISTRYINDEX, lmt_language_state.handler_table_id);
        lua_rawgeti(L, -1, lang->id);
        lua_pushinteger(L, lang->id);
        lua_pushstring(L, original);
        lua_pushstring(L, word);
        lua_pushinteger(L, length);
        lua_pushinteger(L, first);
        lua_pushinteger(L, last);
        res = lua_pcall(L, 6, 1, 0);
        if (res) {
            lua_remove(L, stacktop + 1);
            lmt_error(L, "function call", -1, res == LUA_ERRRUN ? 0 : 1);
        }
        ++lmt_language_state.handler_count;
        switch (lua_type(L, -1)) {
            case LUA_TSTRING:
                *replacement = (char *) lmt_memory_strdup(lua_tostring(L, -1));
                break;
            case LUA_TNUMBER:
                result = lmt_tointeger(L, -1);
                break;
            default:
                break;
        }
        lua_settop(L, stacktop);
        return result;
    }
    return 0;
}

void lmt_initialize_languages(void)
{
     lua_State *L = lmt_lua_state.lua_instance;
     lua_newtable(L);
     lmt_language_state.handler_table_id = luaL_ref(L, LUA_REGISTRYINDEX);
     lua_pushstring(L, LANGUAGE_FUNCTIONS);
     lua_rawgeti(L, LUA_REGISTRYINDEX, lmt_language_state.handler_table_id);
     lua_settable(L, LUA_REGISTRYINDEX);
}

static int languagelib_setwordhandler(lua_State* L)
{
    tex_language *lang = languagelib_object(L);
    switch (lua_type(L, 2)) {
        case LUA_TBOOLEAN:
            if (lua_toboolean(L, 2)) {
                goto DEFAULT;
            } else {
                // fall-through
            }
        case LUA_TNIL:
            {
                if (lang->wordhandler) {
                    lua_rawgeti(L, LUA_REGISTRYINDEX, lmt_language_state.handler_table_id);
                    lua_pushnil(L);
                    lua_rawseti(L, -2, lang->id);
                    lang->wordhandler = 0;
                }
                break;
            }
        case LUA_TFUNCTION:
            {
                lua_rawgeti(L, LUA_REGISTRYINDEX, lmt_language_state.handler_table_id);
                lua_pushvalue(L, 2);
                lua_rawseti(L, -2, lang->id);
                lang->wordhandler = 1;
                break;
            }
        default:
          DEFAULT:
            return luaL_error(L, "argument should be a function, false or nil");
    }
    return 0;
}

static int languagelib_sethjcode(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_type(L, 2) == LUA_TNUMBER) {
        halfword i = lmt_tohalfword(L, 2) ;
        if (lua_type(L, 3) == LUA_TNUMBER) {
            tex_set_hj_code(lang->id, i, lmt_tohalfword(L, 3), -1);
        } else {
            tex_set_hj_code(lang->id, i, i, -1);
        }
        return 0;
    } else {
        return luaL_error(L, "argument should be a character number");
    }
}

static int languagelib_gethjcode(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_type(L, 2) == LUA_TNUMBER) {
        lua_pushinteger(L, tex_get_hj_code(lang->id, lmt_tohalfword(L, 2)));
        return 1;
    } else {
        return luaL_error(L, "argument should be a character number");
    }
}

static int languagelib_post_exhyphen_char(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_gettop(L) == 1) {
        lua_pushinteger(L, lang->post_exhyphen_char);
        return 1;
    } else if (lua_type(L, 2) == LUA_TNUMBER) {
        lang->post_exhyphen_char = lmt_tohalfword(L, 2);
        return 0;
    } else {
        return luaL_error(L, "argument should be a character number");
    }
}

static int languagelib_hyphenation_min(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    if (lua_gettop(L) == 1) {
        lua_pushinteger(L, lang->hyphenation_min);
        return 1;
    } else if (lua_type(L, 2) == LUA_TNUMBER) {
        lang->hyphenation_min = lmt_tohalfword(L, 2);
        return 0;
    } else {
        return luaL_error(L, "argument should be a number");
    }
}

static int languagelib_clear_hyphenation(lua_State *L)
{
    tex_language *lang = languagelib_object(L);
    tex_clear_hyphenation(lang);
    return 0;
}

static int languagelib_clean(lua_State *L)
{
    char *cleaned = NULL;
    if (lua_type(L, 1) == LUA_TSTRING) {
        tex_clean_hyphenation(cur_lang_par, lua_tostring(L, 1), &cleaned);
    } else {
        tex_language *lang = languagelib_object(L);
        if (lang) {
            if (lua_type(L, 2) == LUA_TSTRING) {
                tex_clean_hyphenation(lang->id, lua_tostring(L, 2), &cleaned);
            } else {
                return luaL_error(L, "second argument should be a string");
            }
        } else {
            return luaL_error(L, "first argument should be a string or language");
        }
    }
    lua_pushstring(L, cleaned);
    lmt_memory_free(cleaned);
    return 1;
}

static int languagelib_hyphenate(lua_State *L)
{
    halfword h = lmt_check_isnode(L, 1);
    halfword t = null;
    if (lua_isuserdata(L, 2)) {
        t = lmt_check_isnode(L, 2);
    }
    if (! t) {
        t = h;
        while (node_next(t)) {
            t = node_next(t);
        }
    }
    tex_hyphenate_list(h, t);
    lmt_push_node_fast(L, h);
    lmt_push_node_fast(L, t);
    lua_pushboolean(L, 1);
    return 3;
}

static int languagelib_current(lua_State *L)
{
    lua_pushinteger(L, language_par);
    return 1;
}

static int languagelib_has_language(lua_State *L)
{
    halfword h = lmt_check_isnode(L, 1);
    while (h) {
        if (node_type(h) == glyph_node && get_glyph_language(h) > 0) {
            lua_pushboolean(L, 1);
            return 1;
        } else {
            h = node_next(h);
        }
    }
    lua_pushboolean(L,0);
    return 1;
}

static const struct luaL_Reg langlib_metatable[] = {
    { "clearpatterns",     languagelib_clear_patterns     },
    { "clearhyphenation",  languagelib_clear_hyphenation  },
    { "patterns",          languagelib_patterns           },
    { "hyphenation",       languagelib_hyphenation        },
    { "prehyphenchar",     languagelib_pre_hyphen_char    },
    { "posthyphenchar",    languagelib_post_hyphen_char   },
    { "preexhyphenchar",   languagelib_pre_exhyphen_char  },
    { "postexhyphenchar",  languagelib_post_exhyphen_char },
    { "hyphenationmin",    languagelib_hyphenation_min    },
    { "sethjcode",         languagelib_sethjcode          },
    { "gethjcode",         languagelib_gethjcode          },
    { "setwordhandler",    languagelib_setwordhandler     },
    { "id",                languagelib_id                 },
    { NULL,                NULL                           },
};

static const struct luaL_Reg langlib_function_list[] = {
    { "clearpatterns",     languagelib_clear_patterns     },
    { "clearhyphenation",  languagelib_clear_hyphenation  },
    { "patterns",          languagelib_patterns           },
    { "hyphenation",       languagelib_hyphenation        },
    { "prehyphenchar",     languagelib_pre_hyphen_char    },
    { "posthyphenchar",    languagelib_post_hyphen_char   },
    { "preexhyphenchar",   languagelib_pre_exhyphen_char  },
    { "postexhyphenchar",  languagelib_post_exhyphen_char },
    { "hyphenationmin",    languagelib_hyphenation_min    },
    { "sethjcode",         languagelib_sethjcode          },
    { "gethjcode",         languagelib_gethjcode          },
    { "setwordhandler",    languagelib_setwordhandler     },
    { "id",                languagelib_id                 },
    { "clean",             languagelib_clean              }, /* maybe obsolete */
    { "has_language",      languagelib_has_language       },
    { "hyphenate",         languagelib_hyphenate          },
    { "current",           languagelib_current            },
    { "new",               languagelib_new                },
    { NULL,                NULL                           },
};

int luaopen_language(lua_State *L)
{
    luaL_newmetatable(L, LANGUAGE_METATABLE);
    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");
    luaL_setfuncs(L, langlib_metatable, 0);
    lua_newtable(L);
    luaL_setfuncs(L, langlib_function_list, 0);
    return 1;
}