| 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 |
#include <fontforge-config.h> |
| 30 |
|
| 31 |
#ifndef _NO_PYTHON |
| 32 |
|
| 33 |
#include "cvundoes.h" |
| 34 |
#include "ffglib.h" |
| 35 |
#include "ffpython.h" |
| 36 |
#include "fontforgeui.h" |
| 37 |
#include "hotkeys.h" |
| 38 |
#include "scriptfuncs.h" |
| 39 |
#include "scripting.h" |
| 40 |
#include "splinestroke.h" |
| 41 |
#include "splineutil.h" |
| 42 |
#include "ttf.h" |
| 43 |
#include "ustring.h" |
| 44 |
|
| 45 |
#include <dirent.h> |
| 46 |
#include <errno.h> |
| 47 |
#include <fcntl.h> |
| 48 |
#include <math.h> |
| 49 |
#include <stdarg.h> |
| 50 |
#include <stdio.h> |
| 51 |
#include <sys/types.h> |
| 52 |
#include <unistd.h> |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
セル(_C) |
| 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 |
ル(_C)" and |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static const char mn_order[] = NU_("}{:[]&=~%^;?+/,#$-@*!.7869054321QXZVJWFYGKUBHPCDMLTNSIROEA"); |
| 122 |
|
| 123 |
struct py_menu_item { |
| 124 |
PyObject *func; |
| 125 |
PyObject *check; |
| 126 |
PyObject *data; |
| 127 |
}; |
| 128 |
|
| 129 |
typedef void (*ff_menu_callback)(GWindow gw, struct gmenuitem *mi, GEvent *e); |
| 130 |
|
| 131 |
enum py_menu_type { pmt_font=0, pmt_char=1, pmt_size=2 }; |
| 132 |
enum py_menu_flag { pmf_font=1, pmf_char=2 }; |
| 133 |
|
| 134 |
static struct py_menu_data { |
| 135 |
struct py_menu_item *items; |
| 136 |
int cnt, max; |
| 137 |
GMenuItem2 *menu; |
| 138 |
ff_menu_callback moveto, invoke; |
| 139 |
void (*setmenu)(GMenuItem2 *menu); |
| 140 |
uint16_t mn_offset; |
| 141 |
unichar_t *mn_string; |
| 142 |
GHashTable *mn_avail; |
| 143 |
char *hotkey_prefix; |
| 144 |
} *py_menus; |
| 145 |
|
| 146 |
static unichar_t AllocateNextMnemonic(struct py_menu_data *pmd) { |
| 147 |
unichar_t uc = 0; |
| 148 |
while ( (uc=pmd->mn_string[pmd->mn_offset])!=0 && |
| 149 |
!g_hash_table_contains(pmd->mn_avail, GUINT_TO_POINTER(uc)) ) |
| 150 |
++pmd->mn_offset; |
| 151 |
|
| 152 |
if ( uc!=0 ) |
| 153 |
g_hash_table_remove(pmd->mn_avail, GUINT_TO_POINTER(uc)); |
| 154 |
|
| 155 |
return (unichar_t) uc; |
| 156 |
} |
| 157 |
|
| 158 |
static unichar_t FindMnemonic(unichar_t *menu_string, unichar_t alt, |
| 159 |
struct py_menu_data *pmd) { |
| 160 |
unichar_t *cp; |
| 161 |
|
| 162 |
if ( g_hash_table_contains(pmd->mn_avail, GUINT_TO_POINTER(alt)) ) { |
| 163 |
g_hash_table_remove(pmd->mn_avail, GUINT_TO_POINTER(alt)); |
| 164 |
return alt; |
| 165 |
} |
| 166 |
for ( cp = menu_string; *cp!=0; ++cp ) { |
| 167 |
alt = toupper(*cp); |
| 168 |
if ( g_hash_table_contains(pmd->mn_avail, GUINT_TO_POINTER(alt)) ) { |
| 169 |
g_hash_table_remove(pmd->mn_avail, GUINT_TO_POINTER(alt)); |
| 170 |
return alt; |
| 171 |
} |
| 172 |
} |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
static unichar_t *SetMnemonicSuffix(const unichar_t *menu_string, unichar_t e, unichar_t c) { |
| 178 |
int i; |
| 179 |
unichar_t *r; |
| 180 |
i = u_strlen(menu_string); |
| 181 |
r = malloc((i+5) * sizeof(unichar_t)); |
| 182 |
memcpy(r, menu_string, sizeof(unichar_t)*(i+1)); |
| 183 |
|
| 184 |
i--; |
| 185 |
|
| 186 |
|
| 187 |
while ( i>=0 && r[i]==' ' ) |
| 188 |
--i; |
| 189 |
r[i+1] = 0; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
if ( i>=0 && r[i]==')' ) { |
| 195 |
--i; |
| 196 |
if ( i>=0 && r[i]==e ) { |
| 197 |
--i; |
| 198 |
if ( i>=0 && r[i]=='(' ) { |
| 199 |
--i; |
| 200 |
while ( i>=0 && r[i]==' ' ) |
| 201 |
--i; |
| 202 |
r[i+1] = 0; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
if ( c!=0 ) { |
| 209 |
r[i+1] = ' '; |
| 210 |
r[i+2] = '('; |
| 211 |
r[i+3] = c; |
| 212 |
r[i+4] = ')'; |
| 213 |
r[i+5] = 0; |
| 214 |
} |
| 215 |
return r; |
| 216 |
} |
| 217 |
|
| 218 |
static void py_tllistcheck(struct gmenuitem *mi, PyObject *owner, struct py_menu_data *pmd) { |
| 219 |
PyObject *arglist, *result; |
| 220 |
|
| 221 |
if ( mi == NULL ) |
| 222 |
return; |
| 223 |
|
| 224 |
for ( mi = mi->sub; mi!=NULL && (mi->ti.text!=NULL || mi->ti.line); ++mi ) { |
| 225 |
if ( mi->mid==-1 ) |
| 226 |
continue; |
| 227 |
if ( mi->mid<0 || mi->mid>=pmd->cnt ) { |
| 228 |
fprintf( stderr, "Bad Menu ID in python menu %d\n", mi->mid ); |
| 229 |
mi->ti.disabled = true; |
| 230 |
continue; |
| 231 |
} |
| 232 |
if ( pmd->items[mi->mid].check==NULL ) { |
| 233 |
mi->ti.disabled = false; |
| 234 |
continue; |
| 235 |
} |
| 236 |
arglist = PyTuple_New(2); |
| 237 |
Py_XINCREF(pmd->items[mi->mid].data); |
| 238 |
Py_XINCREF(owner); |
| 239 |
PyTuple_SetItem(arglist,0,pmd->items[mi->mid].data); |
| 240 |
PyTuple_SetItem(arglist,1,owner); |
| 241 |
result = PyObject_CallObject(pmd->items[mi->mid].check, arglist); |
| 242 |
Py_DECREF(arglist); |
| 243 |
if ( result==NULL ) |
| 244 |
; |
| 245 |
else if ( !PyLong_Check(result)) { |
| 246 |
char *menu_item_name = u2utf8_copy(mi->ti.text); |
| 247 |
LogError(_("Return from enabling function for menu item %s must be boolean"), menu_item_name ); |
| 248 |
free( menu_item_name ); |
| 249 |
mi->ti.disabled = true; |
| 250 |
} else |
| 251 |
mi->ti.disabled = PyLong_AsLong(result)==0; |
| 252 |
Py_XDECREF(result); |
| 253 |
if ( PyErr_Occurred()!=NULL ) |
| 254 |
PyErr_Print(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
static void py_menuactivate(struct gmenuitem *mi, PyObject *owner, struct py_menu_data *pmd) { |
| 259 |
PyObject *arglist, *result; |
| 260 |
|
| 261 |
if ( mi->mid==-1 ) |
| 262 |
return; |
| 263 |
|
| 264 |
assert( py_menus!=NULL ); |
| 265 |
|
| 266 |
if ( mi->mid<0 || mi->mid>=pmd->cnt ) { |
| 267 |
fprintf( stderr, "Bad Menu ID in python menu %d\n", mi->mid ); |
| 268 |
return; |
| 269 |
} |
| 270 |
if ( pmd->items[mi->mid].func==NULL ) { |
| 271 |
return; |
| 272 |
} |
| 273 |
arglist = PyTuple_New(2); |
| 274 |
Py_XINCREF(pmd->items[mi->mid].data); |
| 275 |
Py_XINCREF(owner); |
| 276 |
PyTuple_SetItem(arglist,0,pmd->items[mi->mid].data); |
| 277 |
PyTuple_SetItem(arglist,1,owner); |
| 278 |
result = PyObject_CallObject(pmd->items[mi->mid].func, arglist); |
| 279 |
Py_DECREF(arglist); |
| 280 |
Py_XDECREF(result); |
| 281 |
if ( PyErr_Occurred()!=NULL ) |
| 282 |
PyErr_Print(); |
| 283 |
} |
| 284 |
|
| 285 |
void cvpy_tllistcheck(GWindow gw,struct gmenuitem *mi,GEvent *e) { |
| 286 |
CharView *cv = (CharView *) GDrawGetUserData(gw); |
| 287 |
PyObject *pysc = PySC_From_SC(cv->b.sc); |
| 288 |
|
| 289 |
sc_active_in_ui = cv->b.sc; |
| 290 |
layer_active_in_ui = CVLayer((CharViewBase *) cv); |
| 291 |
PyFF_Glyph_Set_Layer(sc_active_in_ui,layer_active_in_ui); |
| 292 |
py_tllistcheck(mi,pysc,py_menus + pmt_char); |
| 293 |
sc_active_in_ui = NULL; |
| 294 |
layer_active_in_ui = ly_fore; |
| 295 |
} |
| 296 |
|
| 297 |
static void cvpy_menuactivate(GWindow gw,struct gmenuitem *mi,GEvent *e) { |
| 298 |
CharView *cv = (CharView *) GDrawGetUserData(gw); |
| 299 |
PyObject *pysc = PySC_From_SC(cv->b.sc); |
| 300 |
|
| 301 |
sc_active_in_ui = cv->b.sc; |
| 302 |
layer_active_in_ui = CVLayer((CharViewBase *) cv); |
| 303 |
PyFF_Glyph_Set_Layer(sc_active_in_ui,layer_active_in_ui); |
| 304 |
py_menuactivate(mi,pysc,py_menus + pmt_char); |
| 305 |
sc_active_in_ui = NULL; |
| 306 |
layer_active_in_ui = ly_fore; |
| 307 |
} |
| 308 |
|
| 309 |
void fvpy_tllistcheck(GWindow gw,struct gmenuitem *mi,GEvent *e) { |
| 310 |
FontViewBase *fv = (FontViewBase *) GDrawGetUserData(gw); |
| 311 |
PyObject *pyfv = PyFF_FontForFV(fv); |
| 312 |
|
| 313 |
fv_active_in_ui = fv; |
| 314 |
layer_active_in_ui = fv->active_layer; |
| 315 |
py_tllistcheck(mi,pyfv,py_menus + pmt_font); |
| 316 |
fv_active_in_ui = NULL; |
| 317 |
} |
| 318 |
|
| 319 |
static void fvpy_menuactivate(GWindow gw,struct gmenuitem *mi,GEvent *e) { |
| 320 |
FontViewBase *fv = (FontViewBase *) GDrawGetUserData(gw); |
| 321 |
PyObject *pyfv = PyFF_FontForFV(fv); |
| 322 |
|
| 323 |
fv_active_in_ui = fv; |
| 324 |
layer_active_in_ui = fv->active_layer; |
| 325 |
py_menuactivate(mi,pyfv,py_menus + pmt_font); |
| 326 |
fv_active_in_ui = NULL; |
| 327 |
} |
| 328 |
|
| 329 |
static void PyMenuInit() { |
| 330 |
int t; |
| 331 |
unichar_t *mn_string, *cp; |
| 332 |
if ( py_menus!=NULL ) |
| 333 |
return; |
| 334 |
|
| 335 |
py_menus = calloc(pmt_size, sizeof(struct py_menu_data)); |
| 336 |
mn_string = utf82u_copy(U_(mn_order)); |
| 337 |
|
| 338 |
for ( t=0; t<pmt_size; ++t ) { |
| 339 |
py_menus[t].mn_string = mn_string; |
| 340 |
py_menus[t].mn_avail = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 341 |
for ( cp = mn_string; *cp!=0; ++cp ) |
| 342 |
g_hash_table_add(py_menus[t].mn_avail, GUINT_TO_POINTER(*cp)); |
| 343 |
} |
| 344 |
|
| 345 |
py_menus[pmt_font].hotkey_prefix = "FontView.Menu.Tools."; |
| 346 |
py_menus[pmt_font].moveto = fvpy_tllistcheck; |
| 347 |
py_menus[pmt_font].invoke = fvpy_menuactivate; |
| 348 |
py_menus[pmt_font].setmenu = FVSetToolsSubmenu; |
| 349 |
|
| 350 |
py_menus[pmt_char].hotkey_prefix = "CharView.Menu.Tools."; |
| 351 |
py_menus[pmt_char].moveto = cvpy_tllistcheck; |
| 352 |
py_menus[pmt_char].invoke = cvpy_menuactivate; |
| 353 |
py_menus[pmt_char].setmenu = CVSetToolsSubmenu; |
| 354 |
} |
| 355 |
|
| 356 |
static struct flaglist menuviews[] = { |
| 357 |
{ "Font", pmf_font }, |
| 358 |
{ "Glyph", pmf_char }, |
| 359 |
{ "Char", pmf_char }, |
| 360 |
FLAGLIST_EMPTY |
| 361 |
}; |
| 362 |
|
| 363 |
struct py_menu_text { |
| 364 |
const char *localized; |
| 365 |
const char *untranslated; |
| 366 |
const char *identifier; |
| 367 |
}; |
| 368 |
|
| 369 |
struct py_menu_spec { |
| 370 |
int depth, divider; |
| 371 |
struct py_menu_text *levels; |
| 372 |
const char *shortcut_str; |
| 373 |
PyObject *func, *check, *data; |
| 374 |
}; |
| 375 |
|
| 376 |
static int MenuDataAdd(struct py_menu_spec *spec, struct py_menu_data *pmd) { |
| 377 |
if ( pmd->cnt >= pmd->max ) |
| 378 |
pmd->items = realloc(pmd->items,(pmd->max+=10)*sizeof(struct py_menu_item)); |
| 379 |
|
| 380 |
pmd->items[pmd->cnt].func = spec->func; |
| 381 |
Py_XINCREF(spec->func); |
| 382 |
|
| 383 |
pmd->items[pmd->cnt].check= spec->check; |
| 384 |
Py_XINCREF(spec->check); |
| 385 |
|
| 386 |
pmd->items[pmd->cnt].data = spec->data; |
| 387 |
Py_XINCREF(spec->data); |
| 388 |
|
| 389 |
return pmd->cnt++; |
| 390 |
} |
| 391 |
|
| 392 |
static unichar_t *DoMnemonic(unichar_t *trans, unichar_t *alt, |
| 393 |
struct py_menu_data *pmd) { |
| 394 |
unichar_t tmp_alt, *tmp_uni; |
| 395 |
|
| 396 |
tmp_alt = FindMnemonic(trans, *alt, pmd); |
| 397 |
if ( tmp_alt!=0 && tmp_alt!=*alt ) { |
| 398 |
|
| 399 |
|
| 400 |
tmp_uni = SetMnemonicSuffix(trans, *alt, 0); |
| 401 |
free(trans); |
| 402 |
trans = tmp_uni; |
| 403 |
*alt = tmp_alt; |
| 404 |
} else if ( tmp_alt==0 ) { |
| 405 |
*alt = AllocateNextMnemonic(pmd); |
| 406 |
tmp_uni = SetMnemonicSuffix(trans, 0, *alt); |
| 407 |
free(trans); |
| 408 |
trans = tmp_uni; |
| 409 |
} |
| 410 |
return trans; |
| 411 |
} |
| 412 |
|
| 413 |
static void InsertSubMenus(struct py_menu_spec *spec, struct py_menu_data *pmd) { |
| 414 |
int i, j; |
| 415 |
GMenuItem2 *mmn, *orig_menu, **mn; |
| 416 |
unichar_t alt; |
| 417 |
char *untrans, *action, *tmp_str; |
| 418 |
unichar_t *trans, *tmp_uni; |
| 419 |
|
| 420 |
mn = &pmd->menu; |
| 421 |
orig_menu = pmd->menu; |
| 422 |
|
| 423 |
for ( i=0; i<spec->depth; ++i ) { |
| 424 |
if ( i==spec->depth-1 && spec->divider ) { |
| 425 |
untrans = "_____UNMATCH___ABLE______"; |
| 426 |
trans = NULL; |
| 427 |
action = NULL; |
| 428 |
alt = 0; |
| 429 |
} else { |
| 430 |
|
| 431 |
|
| 432 |
tmp_uni = utf82u_mncopy(spec->levels[i].untranslated, &alt); |
| 433 |
untrans = u2utf8_copy(tmp_uni); |
| 434 |
free(tmp_uni); |
| 435 |
trans = utf82u_mncopy(spec->levels[i].localized, &alt); |
| 436 |
if ( i==0 ) { |
| 437 |
action = strconcat(pmd->hotkey_prefix, untrans); |
| 438 |
} else { |
| 439 |
tmp_str = strconcat3(action, ".", untrans); |
| 440 |
free(action); |
| 441 |
action = tmp_str; |
| 442 |
} |
| 443 |
} |
| 444 |
j = 0; |
| 445 |
if ( *mn != NULL ) { |
| 446 |
for ( j=0; (*mn)[j].ti.text!=NULL || (*mn)[j].ti.line; ++j ) { |
| 447 |
if ( (*mn)[j].ti.text==NULL ) |
| 448 |
continue; |
| 449 |
if ( strcmp((const char *) (*mn)[j].ti.text_untranslated,untrans)==0 ) |
| 450 |
break; |
| 451 |
} |
| 452 |
} |
| 453 |
if ( *mn==NULL || (*mn)[j].ti.text==NULL ) { |
| 454 |
*mn = realloc(*mn,(j+2)*sizeof(GMenuItem2)); |
| 455 |
memset(*mn+j,0,2*sizeof(GMenuItem2)); |
| 456 |
} |
| 457 |
mmn = *mn; |
| 458 |
if ( mmn[j].ti.text==NULL ) { |
| 459 |
mmn[j].ti.fg = mmn[j].ti.bg = COLOR_DEFAULT; |
| 460 |
if ( i==0 && !spec->divider ) |
| 461 |
trans = DoMnemonic(trans, &alt, pmd); |
| 462 |
if ( i!=spec->depth-1 ) { |
| 463 |
mmn[j].ti.text = trans; |
| 464 |
mmn[j].ti.text_untranslated = untrans; |
| 465 |
mmn[j].ti.text_is_1byte = false; |
| 466 |
mmn[j].ti.mnemonic = alt; |
| 467 |
mmn[j].mid = -1; |
| 468 |
mmn[j].moveto = pmd->moveto; |
| 469 |
mn = &mmn[j].sub; |
| 470 |
} else if ( spec->divider ) { |
| 471 |
mmn[j].ti.line = true; |
| 472 |
} else { |
| 473 |
mmn[j].ti.text = trans; |
| 474 |
mmn[j].ti.text_untranslated = untrans; |
| 475 |
mmn[j].ti.text_is_1byte = false; |
| 476 |
mmn[j].ti.mnemonic = alt; |
| 477 |
mmn[j].invoke = pmd->invoke; |
| 478 |
mmn[j].mid = MenuDataAdd(spec,pmd); |
| 479 |
} |
| 480 |
} else { |
| 481 |
if ( mmn[j].sub != NULL ) { |
| 482 |
if ( i==0 && !spec->divider ) { |
| 483 |
if ( mmn[j].ti.mnemonic != 0 ) |
| 484 |
g_hash_table_add(pmd->mn_avail, GUINT_TO_POINTER(mmn[j].ti.mnemonic)); |
| 485 |
trans = DoMnemonic(trans, &alt, pmd); |
| 486 |
} |
| 487 |
free(mmn[j].ti.text); |
| 488 |
mmn[j].ti.text = trans; |
| 489 |
mmn[j].ti.mnemonic = alt; |
| 490 |
} |
| 491 |
if ( i!=spec->depth-1 ) |
| 492 |
mn = &mmn[j].sub; |
| 493 |
else { |
| 494 |
mmn[j].invoke = pmd->invoke; |
| 495 |
mmn[j].mid = MenuDataAdd(spec,pmd); |
| 496 |
fprintf( stderr, "Redefining menu item %s\n", untrans ); |
| 497 |
} |
| 498 |
free(untrans); |
| 499 |
} |
| 500 |
} |
| 501 |
if ( spec->shortcut_str!=NULL ) |
| 502 |
hotkeySetFull(action, spec->shortcut_str, false, hk_python); |
| 503 |
if ( orig_menu!=pmd->menu ) |
| 504 |
(*pmd->setmenu)(pmd->menu); |
| 505 |
} |
| 506 |
|
| 507 |
static int DecodeMenuLevel(struct py_menu_text *text, PyObject *args) { |
| 508 |
if ( PyUnicode_Check(args) ) { |
| 509 |
text->localized = text->untranslated = PyUnicode_AsUTF8(args); |
| 510 |
} else if ( PyTuple_Check(args) ) { |
| 511 |
for ( int i=0; i<PyTuple_Size(args); ++i ) { |
| 512 |
if ( !PyUnicode_Check(PyTuple_GetItem(args, i)) ) { |
| 513 |
PyErr_Format(PyExc_ValueError, "All elements of a `name` or `submenu` tuple must be strings" ); |
| 514 |
return false; |
| 515 |
} |
| 516 |
} |
| 517 |
if ( PyTuple_Size(args)==2 ) { |
| 518 |
text->localized = text->untranslated = PyUnicode_AsUTF8(PyTuple_GetItem(args, 0)); |
| 519 |
text->identifier = PyUnicode_AsUTF8(PyTuple_GetItem(args, 1)); |
| 520 |
} else if ( PyTuple_Size(args)==3 ) { |
| 521 |
text->localized = PyUnicode_AsUTF8(PyTuple_GetItem(args, 0)); |
| 522 |
text->untranslated = PyUnicode_AsUTF8(PyTuple_GetItem(args, 1)); |
| 523 |
text->identifier = PyUnicode_AsUTF8(PyTuple_GetItem(args, 2)); |
| 524 |
} else { |
| 525 |
PyErr_Format(PyExc_ValueError, "A `name` or `submenu` tuple must have either 2 or 3 elements" ); |
| 526 |
return false; |
| 527 |
} |
| 528 |
} else { |
| 529 |
PyErr_Format(PyExc_ValueError, "A `name` or `submenu` object must be either string or tuple" ); |
| 530 |
return false; |
| 531 |
} |
| 532 |
return true; |
| 533 |
} |
| 534 |
|
| 535 |
static const char *rmi_keywords[] = { "callback", "enable", "data", "context", "hotkey", "name", "submenu", "keyword_only", NULL }; |
| 536 |
static const char *rmdiv_keywords[] = { "context", "divider", "submenu", NULL }; |
| 537 |
|
| 538 |
static PyObject *PyFF_registerMenuItem(PyObject *self, PyObject *args, PyObject *kwargs) { |
| 539 |
int i, flags, by_keyword = false, keyword_only = false; |
| 540 |
struct py_menu_spec spec; |
| 541 |
PyObject *context = Py_None, *name = Py_None, *submenu = Py_None; |
| 542 |
|
| 543 |
memset(&spec, 0, sizeof(spec)); |
| 544 |
|
| 545 |
spec.func = spec.check = spec.data = Py_None; |
| 546 |
|
| 547 |
if ( no_windowing_ui ) |
| 548 |
Py_RETURN_NONE; |
| 549 |
|
| 550 |
if ( PyArg_ParseTupleAndKeywords(args, kwargs, "Op|$O", (char**) rmdiv_keywords, |
| 551 |
&context, &spec.divider, &submenu) ) { |
| 552 |
if ( !spec.divider ) { |
| 553 |
PyErr_Format(PyExc_ValueError, "'divider' must be True when using this combination of keywords." ); |
| 554 |
return NULL; |
| 555 |
} |
| 556 |
by_keyword = true; |
| 557 |
} else { |
| 558 |
PyErr_Clear(); |
| 559 |
if ( PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOsO$Op", (char**) rmi_keywords, |
| 560 |
&spec.func, &spec.check, &spec.data, &context, |
| 561 |
&spec.shortcut_str, &name, &submenu, &keyword_only) ) { |
| 562 |
by_keyword = true; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
if ( by_keyword ) { |
| 567 |
if ( PyList_Check(submenu) ) { |
| 568 |
spec.depth = PyList_Size(submenu)+1; |
| 569 |
spec.levels = calloc(spec.depth, sizeof(struct py_menu_text)); |
| 570 |
for (i=0; i<spec.depth-1; ++i) { |
| 571 |
if ( !DecodeMenuLevel(spec.levels+i, PyList_GetItem(submenu, i)) ) { |
| 572 |
free(spec.levels); |
| 573 |
return NULL; |
| 574 |
} |
| 575 |
} |
| 576 |
} else if ( PyTuple_Check(submenu) || PyUnicode_Check(submenu) ) { |
| 577 |
spec.depth = 2; |
| 578 |
spec.levels = calloc(spec.depth, sizeof(struct py_menu_text)); |
| 579 |
if ( !DecodeMenuLevel(spec.levels, submenu) ) { |
| 580 |
free(spec.levels); |
| 581 |
return NULL; |
| 582 |
} |
| 583 |
} else if ( submenu == Py_None ) { |
| 584 |
spec.depth = 1; |
| 585 |
spec.levels = calloc(spec.depth, sizeof(struct py_menu_text)); |
| 586 |
} else { |
| 587 |
PyErr_Format(PyExc_ValueError, "Unrecognized type for `submenu` argument" ); |
| 588 |
return NULL; |
| 589 |
} |
| 590 |
|
| 591 |
if ( PyTuple_Check(name) || PyUnicode_Check(name) ) { |
| 592 |
if ( !DecodeMenuLevel(spec.levels+spec.depth-1, name) ) { |
| 593 |
free(spec.levels); |
| 594 |
return NULL; |
| 595 |
} |
| 596 |
} else if ( !spec.divider ) { |
| 597 |
PyErr_Format(PyExc_ValueError, "Unrecognized type for `name` argument" ); |
| 598 |
return NULL; |
| 599 |
} |
| 600 |
} else { |
| 601 |
if ( kwargs!=NULL && PyObject_IsTrue(PyDict_GetItemString(kwargs, "keyword_only"))==1 ) { |
| 602 |
return NULL; |
| 603 |
} |
| 604 |
|
| 605 |
PyErr_Clear(); |
| 606 |
|
| 607 |
|
| 608 |
spec.depth = PyTuple_Size(args)-5; |
| 609 |
if ( spec.depth<1 ) { |
| 610 |
PyErr_Format(PyExc_TypeError, "Positional interface: Too few arguments"); |
| 611 |
return NULL; |
| 612 |
} |
| 613 |
|
| 614 |
spec.func = PyTuple_GetItem(args, 0); |
| 615 |
spec.check = PyTuple_GetItem(args, 1); |
| 616 |
spec.data = PyTuple_GetItem(args, 2); |
| 617 |
context = PyTuple_GetItem(args, 3); |
| 618 |
|
| 619 |
if ( PyTuple_GetItem(args,4)!=Py_None ) { |
| 620 |
spec.shortcut_str = PyUnicode_AsUTF8(PyTuple_GetItem(args, 4)); |
| 621 |
if (spec.shortcut_str == NULL) { |
| 622 |
PyErr_Format(PyExc_ValueError, "Positional interface: Shortcut argument is not string" ); |
| 623 |
return NULL; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
spec.levels = calloc(spec.depth, sizeof(struct py_menu_text)); |
| 628 |
for ( i=0; i<spec.depth; ++i ) { |
| 629 |
if ( !DecodeMenuLevel(spec.levels+i, PyTuple_GetItem(args, i+5)) ) { |
| 630 |
free(spec.levels); |
| 631 |
return NULL; |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
if ( !spec.divider && !PyCallable_Check(spec.func) ) { |
| 637 |
PyErr_Format(PyExc_TypeError, "First ('callback') argument is not callable" ); |
| 638 |
free(spec.levels); |
| 639 |
return NULL; |
| 640 |
} |
| 641 |
|
| 642 |
if (spec.check!=Py_None && !PyCallable_Check(spec.check)) { |
| 643 |
PyErr_Format(PyExc_TypeError, "Second ('enable') argument is not callable" ); |
| 644 |
free(spec.levels); |
| 645 |
return NULL; |
| 646 |
} else if (spec.check==Py_None) { |
| 647 |
spec.check = NULL; |
| 648 |
} |
| 649 |
|
| 650 |
flags = FlagsFromTuple(context, menuviews, "menu window" ); |
| 651 |
if ( flags==-1 ) { |
| 652 |
PyErr_Format(PyExc_ValueError, "Unknown context for menu" ); |
| 653 |
free(spec.levels); |
| 654 |
return NULL; |
| 655 |
} |
| 656 |
|
| 657 |
if (spec.shortcut_str!=NULL && !HotkeyParse(NULL, spec.shortcut_str)) { |
| 658 |
PyErr_Format(PyExc_ValueError, "Cannot parse shortcut string" ); |
| 659 |
free(spec.levels); |
| 660 |
return NULL; |
| 661 |
} |
| 662 |
|
| 663 |
PyMenuInit(); |
| 664 |
|
| 665 |
if ( flags&pmf_font ) |
| 666 |
InsertSubMenus(&spec, py_menus + pmt_font); |
| 667 |
if ( flags&pmf_char ) |
| 668 |
InsertSubMenus(&spec, py_menus + pmt_char); |
| 669 |
|
| 670 |
Py_RETURN_NONE; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
PyMethodDef module_fontforge_ui_methods[] = { |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
PYMETHODDEF_EMPTY |
| 684 |
}; |
| 685 |
|
| 686 |
|
| 687 |
static PyMethodDef* |
| 688 |
copyUIMethodsToBaseTable( PyMethodDef* ui, PyMethodDef* md ) |
| 689 |
{ |
| 690 |
TRACE("copyUIMethodsToBaseTable()\n"); |
| 691 |
|
| 692 |
for( ; md->ml_name; ) |
| 693 |
{ |
| 694 |
md++; |
| 695 |
} |
| 696 |
for( ; ui->ml_name; ui++, md++ ) |
| 697 |
{ |
| 698 |
memcpy( md, ui, sizeof(PyMethodDef)); |
| 699 |
} |
| 700 |
return md; |
| 701 |
} |
| 702 |
|
| 703 |
void PythonUI_Init(void) { |
| 704 |
TRACE("PythonUI_Init()\n"); |
| 705 |
FfPy_Replace_MenuItemStub((PyCFunction)PyFF_registerMenuItem); |
| 706 |
|
| 707 |
copyUIMethodsToBaseTable( module_fontforge_ui_methods, module_fontforge_methods ); |
| 708 |
} |
| 709 |
#endif |
| 710 |
|