| 1 |
/* llanglib.c |
| 2 |
|
| 3 |
Copyright 2006-2008 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 2 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include "ptexlib.h" |
| 21 |
#include "lua/luatex-api.h" |
| 22 |
|
| 23 |
#define LANG_METATABLE "luatex.lang" |
| 24 |
|
| 25 |
#define check_islang(L,b) (struct tex_language **)luaL_checkudata(L,b,LANG_METATABLE) |
| 26 |
|
| 27 |
static int lang_new(lua_State * L) |
| 28 |
{ |
| 29 |
struct tex_language **lang; |
| 30 |
if (lua_gettop(L) == 0) { |
| 31 |
lang = lua_newuserdata(L, sizeof(struct tex_language *)); |
| 32 |
*lang = new_language(-1); |
| 33 |
if (!*lang) { |
| 34 |
return luaL_error(L, "lang.new(): no room for a new language"); |
| 35 |
} |
| 36 |
} else { |
| 37 |
int lualang; |
| 38 |
lang = lua_newuserdata(L, sizeof(struct tex_language *)); |
| 39 |
lualang = lua_tointeger(L, 1); |
| 40 |
*lang = get_language(lualang); |
| 41 |
if (!*lang) { |
| 42 |
return luaL_error(L, "lang.new(%d): undefined language", lualang); |
| 43 |
} |
| 44 |
} |
| 45 |
luaL_getmetatable(L, LANG_METATABLE); |
| 46 |
lua_setmetatable(L, -2); |
| 47 |
return 1; |
| 48 |
} |
| 49 |
|
| 50 |
static int lang_id(lua_State * L) |
| 51 |
{ |
| 52 |
struct tex_language **lang_ptr; |
| 53 |
lang_ptr = check_islang(L, 1); |
| 54 |
lua_pushinteger(L, (*lang_ptr)->id); |
| 55 |
return 1; |
| 56 |
} |
| 57 |
|
| 58 |
static int lang_patterns(lua_State * L) |
| 59 |
{ |
| 60 |
struct tex_language **lang_ptr; |
| 61 |
lang_ptr = check_islang(L, 1); |
| 62 |
if (lua_gettop(L) != 1) { |
| 63 |
if (lua_type(L, 2) != LUA_TSTRING) { |
| 64 |
return luaL_error(L, "lang.patterns(): argument should be a string"); |
| 65 |
} |
| 66 |
load_patterns(*lang_ptr, (const unsigned char *) lua_tostring(L, 2)); |
| 67 |
return 0; |
| 68 |
} else { |
| 69 |
if ((*lang_ptr)->patterns != NULL) { |
| 70 |
lua_pushstring(L, (char *) hnj_serialize((*lang_ptr)->patterns)); |
| 71 |
} else { |
| 72 |
lua_pushnil(L); |
| 73 |
} |
| 74 |
return 1; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static int lang_clear_patterns(lua_State * L) |
| 79 |
{ |
| 80 |
struct tex_language **lang_ptr; |
| 81 |
lang_ptr = check_islang(L, 1); |
| 82 |
clear_patterns(*lang_ptr); |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static int lang_hyphenation(lua_State * L) |
| 87 |
{ |
| 88 |
struct tex_language **lang_ptr; |
| 89 |
lang_ptr = check_islang(L, 1); |
| 90 |
if (lua_gettop(L) != 1) { |
| 91 |
if (lua_type(L, 2) != LUA_TSTRING) { |
| 92 |
return luaL_error(L, "lang.hyphenation(): argument should be a string"); |
| 93 |
} |
| 94 |
load_hyphenation(*lang_ptr, (const unsigned char *) lua_tostring(L, 2)); |
| 95 |
return 0; |
| 96 |
} else { |
| 97 |
if ((*lang_ptr)->exceptions != 0) { |
| 98 |
lua_pushstring(L, exception_strings(*lang_ptr)); |
| 99 |
} else { |
| 100 |
lua_pushnil(L); |
| 101 |
} |
| 102 |
return 1; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static int lang_pre_hyphen_char(lua_State * L) |
| 107 |
{ |
| 108 |
struct tex_language **lang_ptr; |
| 109 |
lang_ptr = check_islang(L, 1); |
| 110 |
if (lua_gettop(L) != 1) { |
| 111 |
if (lua_type(L, 2) != LUA_TNUMBER) { |
| 112 |
return luaL_error(L, "lang.prehyphenchar(): argument should be a character number"); |
| 113 |
} |
| 114 |
(*lang_ptr)->pre_hyphen_char = (int) lua_tointeger(L, 2); |
| 115 |
return 0; |
| 116 |
} else { |
| 117 |
lua_pushinteger(L, (*lang_ptr)->pre_hyphen_char); |
| 118 |
return 1; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static int lang_post_hyphen_char(lua_State * L) |
| 123 |
{ |
| 124 |
struct tex_language **lang_ptr; |
| 125 |
lang_ptr = check_islang(L, 1); |
| 126 |
if (lua_gettop(L) != 1) { |
| 127 |
if (lua_type(L, 2) != LUA_TNUMBER) { |
| 128 |
return luaL_error(L, "lang.posthyphenchar(): argument should be a character number"); |
| 129 |
} |
| 130 |
(*lang_ptr)->post_hyphen_char = (int) lua_tointeger(L, 2); |
| 131 |
return 0; |
| 132 |
} else { |
| 133 |
lua_pushinteger(L, (*lang_ptr)->post_hyphen_char); |
| 134 |
return 1; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
static int lang_pre_exhyphen_char(lua_State * L) |
| 139 |
{ |
| 140 |
struct tex_language **lang_ptr; |
| 141 |
lang_ptr = check_islang(L, 1); |
| 142 |
if (lua_gettop(L) != 1) { |
| 143 |
if (lua_type(L, 2) != LUA_TNUMBER) { |
| 144 |
return luaL_error(L, "lang.preexhyphenchar(): argument should be a character number"); |
| 145 |
} |
| 146 |
(*lang_ptr)->pre_exhyphen_char = (int) lua_tointeger(L, 2); |
| 147 |
return 0; |
| 148 |
} else { |
| 149 |
lua_pushinteger(L, (*lang_ptr)->pre_exhyphen_char); |
| 150 |
return 1; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
static int lang_sethjcode(lua_State * L) |
| 155 |
{ |
| 156 |
struct tex_language **lang_ptr; |
| 157 |
lang_ptr = check_islang(L, 1); |
| 158 |
if (lua_type(L, 2) != LUA_TNUMBER) { |
| 159 |
return luaL_error(L, "lang.sethjcode(): argument should be a character number"); |
| 160 |
} else { |
| 161 |
int i = (int) lua_tointeger(L, 2) ; |
| 162 |
if (lua_type(L, 3) == LUA_TNUMBER) { |
| 163 |
set_hj_code((*lang_ptr)->id,i,(int) lua_tointeger(L, 3),-1); |
| 164 |
} else { |
| 165 |
set_hj_code((*lang_ptr)->id,i,i,-1); |
| 166 |
} |
| 167 |
} |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
static int lang_gethjcode(lua_State * L) |
| 172 |
{ |
| 173 |
struct tex_language **lang_ptr; |
| 174 |
lang_ptr = check_islang(L, 1); |
| 175 |
if (lua_type(L, 2) != LUA_TNUMBER) { |
| 176 |
return luaL_error(L, "lang.gethjcode(): argument should be a character number"); |
| 177 |
} else { |
| 178 |
lua_pushinteger(L, get_hj_code((*lang_ptr)->id,lua_tointeger(L, 2))); |
| 179 |
} |
| 180 |
return 1; |
| 181 |
} |
| 182 |
|
| 183 |
static int lang_post_exhyphen_char(lua_State * L) |
| 184 |
{ |
| 185 |
struct tex_language **lang_ptr; |
| 186 |
lang_ptr = check_islang(L, 1); |
| 187 |
if (lua_gettop(L) != 1) { |
| 188 |
if (lua_type(L, 2) != LUA_TNUMBER) { |
| 189 |
return luaL_error(L, "lang.postexhyphenchar(): argument should be a character number"); |
| 190 |
} |
| 191 |
(*lang_ptr)->post_exhyphen_char = (int) lua_tointeger(L, 2); |
| 192 |
return 0; |
| 193 |
} else { |
| 194 |
lua_pushinteger(L, (*lang_ptr)->post_exhyphen_char); |
| 195 |
return 1; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
static int lang_hyphenation_min(lua_State * L) |
| 200 |
{ |
| 201 |
struct tex_language **lang_ptr; |
| 202 |
lang_ptr = check_islang(L, 1); |
| 203 |
if (lua_gettop(L) != 1) { |
| 204 |
if (lua_type(L, 2) != LUA_TNUMBER) { |
| 205 |
return luaL_error(L, "lang.hyphenationmin(): argument should be a number"); |
| 206 |
} |
| 207 |
(*lang_ptr)->hyphenation_min = (int) lua_tointeger(L, 2); |
| 208 |
return 0; |
| 209 |
} else { |
| 210 |
lua_pushinteger(L, (*lang_ptr)->hyphenation_min); |
| 211 |
return 1; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
static int lang_clear_hyphenation(lua_State * L) |
| 216 |
{ |
| 217 |
struct tex_language **lang_ptr; |
| 218 |
lang_ptr = check_islang(L, 1); |
| 219 |
clear_hyphenation(*lang_ptr); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
static int do_lang_clean(lua_State * L) |
| 224 |
{ |
| 225 |
char *cleaned; |
| 226 |
if (lua_type(L, 1) == LUA_TSTRING) { |
| 227 |
(void) clean_hyphenation(cur_lang_par, lua_tostring(L, 1), &cleaned); |
| 228 |
} else { |
| 229 |
struct tex_language **lang_ptr; |
| 230 |
lang_ptr = check_islang(L, 1); |
| 231 |
if (lang_ptr == NULL) { |
| 232 |
return luaL_error(L, "lang.clean(): first argument should be a string or language"); |
| 233 |
} else if (lua_type(L, 2) != LUA_TSTRING) { |
| 234 |
return luaL_error(L, "lang.clean(): second argument should be a string"); |
| 235 |
} else { |
| 236 |
(void) clean_hyphenation((*lang_ptr)->id,lua_tostring(L, 2), &cleaned); |
| 237 |
} |
| 238 |
} |
| 239 |
lua_pushstring(L, cleaned); |
| 240 |
return 1; |
| 241 |
} |
| 242 |
|
| 243 |
static int do_lang_hyphenate(lua_State * L) |
| 244 |
{ |
| 245 |
halfword t = null; |
| 246 |
halfword h = *check_isnode(L, 1); |
| 247 |
if (lua_isuserdata(L, 2)) { |
| 248 |
t = *check_isnode(L, 2); |
| 249 |
} |
| 250 |
if (t == null) { |
| 251 |
t = h; |
| 252 |
while (vlink(t) != null) { |
| 253 |
t = vlink(t); |
| 254 |
} |
| 255 |
} |
| 256 |
hnj_hyphenation(h, t); |
| 257 |
lua_nodelib_push_fast(L, h); |
| 258 |
lua_nodelib_push_fast(L, t); |
| 259 |
lua_pushboolean(L, 1); |
| 260 |
return 3; |
| 261 |
} |
| 262 |
|
| 263 |
static const struct luaL_Reg langlib_d[] = { |
| 264 |
{"clear_patterns", lang_clear_patterns}, |
| 265 |
{"clear_hyphenation", lang_clear_hyphenation}, |
| 266 |
{"patterns", lang_patterns}, |
| 267 |
{"hyphenation", lang_hyphenation}, |
| 268 |
{"prehyphenchar", lang_pre_hyphen_char}, |
| 269 |
{"posthyphenchar", lang_post_hyphen_char}, |
| 270 |
{"preexhyphenchar", lang_pre_exhyphen_char}, |
| 271 |
{"postexhyphenchar", lang_post_exhyphen_char}, |
| 272 |
{"hyphenationmin", lang_hyphenation_min}, |
| 273 |
{"sethjcode", lang_sethjcode}, |
| 274 |
{"gethjcode", lang_gethjcode}, |
| 275 |
{"id", lang_id}, |
| 276 |
/*tex sentinel */ |
| 277 |
{NULL, NULL} |
| 278 |
}; |
| 279 |
|
| 280 |
static const struct luaL_Reg langlib[] = { |
| 281 |
{"clear_patterns", lang_clear_patterns}, |
| 282 |
{"clear_hyphenation", lang_clear_hyphenation}, |
| 283 |
{"patterns", lang_patterns}, |
| 284 |
{"hyphenation", lang_hyphenation}, |
| 285 |
{"prehyphenchar", lang_pre_hyphen_char}, |
| 286 |
{"posthyphenchar", lang_post_hyphen_char}, |
| 287 |
{"preexhyphenchar", lang_pre_exhyphen_char}, |
| 288 |
{"postexhyphenchar", lang_post_exhyphen_char}, |
| 289 |
{"hyphenationmin", lang_hyphenation_min}, |
| 290 |
{"sethjcode", lang_sethjcode}, |
| 291 |
{"gethjcode", lang_gethjcode}, |
| 292 |
{"id", lang_id}, |
| 293 |
{"clean", do_lang_clean}, |
| 294 |
{"hyphenate", do_lang_hyphenate}, |
| 295 |
{"new", lang_new}, |
| 296 |
/*tex sentinel */ |
| 297 |
{NULL, NULL} |
| 298 |
}; |
| 299 |
|
| 300 |
int luaopen_lang(lua_State * L) |
| 301 |
{ |
| 302 |
luaL_newmetatable(L, LANG_METATABLE); |
| 303 |
/*tex push metatable */ |
| 304 |
lua_pushvalue(L, -1); |
| 305 |
/*tex metatable.__index = metatable */ |
| 306 |
lua_setfield(L, -2, "__index"); |
| 307 |
/*tex set dict methods */ |
| 308 |
luaL_openlib(L, NULL, langlib_d, 0); |
| 309 |
luaL_openlib(L, "lang", langlib, 0); |
| 310 |
return 1; |
| 311 |
} |