Debian Codegraph @

fontforgeexe/startui.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29 #include <fontforge-version-extras.h>
30
31 #include "autosave.h"
32 #include "bitmapchar.h"
33 #include "clipnoui.h"
34 #include "encoding.h"
35 #include "ffgdk.h"
36 #include "ffglib.h"
37 #include "fontforgeui.h"
38 #include "gfile.h"
39 #include "gresedit.h"
40 #include "hotkeys.h"
41 #include "lookups.h"
42 #include "prefs.h"
43 #include "start.h"
44 #include "ustring.h"
45
46 #include <locale.h>
47 #include <stdlib.h> /* getenv,setenv */
48 #include <sys/stat.h>
49 #include <sys/time.h>
50 #include <sys/types.h>
51 #include <time.h>
52 #include <unistd.h>
53
54 #if defined(__MINGW32__)
55 #include <windows.h>
56 #define sleep(n) Sleep(1000 * (n))
57 #endif
58
59 #ifdef _NO_LIBPNG
60 # define PNGLIBNAME "libpng"
61 #else
62 # include <png.h> /* for version number to find up shared image name */
63 # if !defined(PNG_LIBPNG_VER_MAJOR) || (PNG_LIBPNG_VER_MAJOR==1 && PNG_LIBPNG_VER_MINOR<2)
64 # define PNGLIBNAME "libpng"
65 # else
66 # define xstr(s) str(s)
67 # define str(s) #s
68 # define PNGLIBNAME "libpng" xstr(PNG_LIBPNG_VER_MAJOR) xstr(PNG_LIBPNG_VER_MINOR)
69 # endif
70 #endif
71
72 // Must be included after png.h because it messes with setjmp
73 #include "scripting.h"
74
75 extern int AutoSaveFrequency;
76 int splash = 1;
77 static int localsplash;
78 static int unique = 0;
79
80 static void _dousage(void) {
81 printf( "fontforge [options] [fontfiles]\n" );
82 printf( "\t-new\t\t\t (creates a new font)\n" );
83 printf( "\t-last\t\t\t (loads the last sfd file closed)\n" );
84 #if HANYANG
85 printf( "\t-newkorean\t\t (creates a new korean font)\n" );
86 #endif
87 printf( "\t-recover none|auto|inquire|clean (control error recovery)\n" );
88 printf( "\t-allglyphs\t\t (load all glyphs in the 'glyf' table\n\t\t\t\t of a truetype collection)\n" );
89 printf( "\t-nosplash\t\t (no splash screen)\n" );
90 printf( "\t-quiet\t\t\t (don't print non-essential\n\t\t\t\t information to stderr)\n" );
91 printf( "\t-unique\t\t\t (if a fontforge is already running open all\n\t\t\t\t arguments in it and have this process exit)\n" );
92 printf( "\t-display display-name\t (sets the X display)\n" );
93 printf( "\t-sync\t\t\t (syncs the display, debugging)\n" );
94 #if MyMemory
95 printf( "\t-memory\t\t\t (turns on memory checks, debugging)\n" );
96 #endif
97 printf( "\t-help\t\t\t (displays this message, and exits)\n" );
98 printf( "\t-docs\t\t\t (displays this message, invokes a browser)\n\t\t\t\t (Using the BROWSER environment variable)\n" );
99 printf( "\t-version\t\t (prints the version of fontforge and exits)\n" );
100 #ifndef _NO_PYTHON
101 printf( "\t-lang=py\t\t (use python for scripts (may precede -script))\n" );
102 #endif
103 #ifndef _NO_FFSCRIPT
104 printf( "\t-lang=ff\t\t (use fontforge's legacy scripting language)\n" );
105 #endif
106 printf( "\t-script scriptfile\t (executes scriptfile)\n" );
107 printf( "\t\tmust be the first option (or follow -lang).\n" );
108 printf( "\t\tAll others passed to scriptfile.\n" );
109 printf( "\t-dry scriptfile\t\t (syntax checks scriptfile)\n" );
110 printf( "\t\tmust be the first option. All others passed to scriptfile.\n" );
111 printf( "\t\tOnly for fontforge's own scripting language, not python.\n" );
112 printf( "\t-c script-string\t (executes argument as scripting cmds)\n" );
113 printf( "\t\tmust be the first option. All others passed to the script.\n" );
114 printf( "\t-skippyfile\t\t (do not execute python init scripts)\n" );
115 printf( "\t-skippyplug\t\t (do not load python plugins)\n" );
116 printf( "\n" );
117 printf( "FontForge will read postscript (pfa, pfb, ps, cid), opentype (otf),\n" );
118 printf( "\ttruetype (ttf,ttc), macintosh resource fonts (dfont,bin,hqx),\n" );
119 printf( "\tand bdf and pcf fonts. It will also read its own format --\n" );
120 printf( "\tsfd files.\n" );
121 printf( "If no fontfiles are specified (and -new is not either and there's nothing\n" );
122 printf( "\tto recover) then fontforge will produce an open font dlg.\n" );
123 printf( "If a scriptfile is specified then FontForge will not open the X display\n" );
124 printf( "\tnor will it process any additional arguments. It will execute the\n" );
125 printf( "\tscriptfile and give it any remaining arguments\n" );
126 printf( "If the first argument is an executable filename, and that file's first\n" );
127 printf( "\tline contains \"fontforge\" then it will be treated as a scriptfile.\n\n" );
128 printf( "For more information see:\n\thttp://fontforge.sourceforge.net/\n" );
129 printf( "Send bug reports to:\[email protected]\n" );
130 }
131
132 static void dousage(void) {
133 _dousage();
134 exit(0);
135 }
136
137 static void dohelp(void) {
138 _dousage();
139 help("index.html", NULL);
140 exit(0);
141 }
142
143 struct delayed_event {
144 void *data;
145 void (*func)(void *);
146 };
147
148 extern GImage splashimage_legacy;
149 static GWindow splashw;
150 static GTimer *autosave_timer, *splasht;
151 GResFont splash_font = GRESFONT_INIT("400 10pt " SERIF_UI_FAMILIES);
152 GResFont splash_monofont = GRESFONT_INIT("400 10pt " MONO_UI_FAMILIES);
153 GResFont splash_italicfont = GRESFONT_INIT("400 10pt italic " SERIF_UI_FAMILIES);
154 GResImage splashresimage = GRESIMAGE_INIT("splash2019.png");
155 Color splashbg = 0xffffff;
156 Color splashfg = 0x000000;
157 GImage *splashimagep;
158 static int as,fh, linecnt;
159 static unichar_t msg[546];
160 static unichar_t *lines[32], *is, *ie;
161
162 static void SplashImageInit() {
163 MiscWinInit();
164 if (splashimagep == NULL)
165 splashimagep = GResImageGetImage(&splashresimage);
166 if (splashimagep == NULL)
167 splashimagep = &splashimage_legacy;
168 return;
169 }
170
171 void *_SplashResImageSet(char *res, void *def) {
172 // Image was already set; this is a hook for side effects
173 GImage *i = GResImageGetImage((GResImage *)def);
174 if ( i!=NULL )
175 splashimagep = i;
176 return NULL;
177 }
178
179 void ShowAboutScreen(void) {
180 static int first=1;
181
182 if ( first ) {
183 GDrawResize(splashw,splashimagep->u.image->width,splashimagep->u.image->height+linecnt*fh);
184 first = false;
185 }
186
187 if ( splasht!=NULL )
188 GDrawCancelTimer(splasht);
189 splasht=NULL;
190
191 GDrawSetVisible(splashw,true);
192 }
193
194 static void SplashLayout() {
195 unichar_t *start, *pt, *lastspace;
196
197 SplashImageInit();
198
199 u_strcpy(msg, utf82u_copy("As he drew closer to completing his book on Renaissance printing (The Craft of Printing and the Publication of Shakespeare’s Works), George Williams IV suggested that his son, George Williams V, write a chapter on computer typography. FontForge—previously called PfaEdit—was his response."));
200
201 GDrawSetFont(splashw,splash_font.fi);
202 linecnt = 0;
203 lines[linecnt++] = msg-1;
204 for ( start = msg; *start!='\0'; start = pt ) {
205 lastspace = NULL;
206 for ( pt=start; ; ++pt ) {
207 if ( *pt==' ' || *pt=='\0' ) {
208 if ( GDrawGetTextWidth(splashw,start,pt-start)<splashimagep->u.image->width-10 )
209 lastspace = pt;
210 else
211 break;
212 if ( *pt=='\0' )
213 break;
214 }
215 }
216 if ( lastspace!=NULL )
217 pt = lastspace;
218 lines[linecnt++] = pt;
219 if ( *pt ) ++pt;
220 }
221
222 uc_strcpy(pt," ");
223 pt += u_strlen(pt);
224 lines[linecnt++] = pt;
225
226 uc_strcpy(pt, " As of 2012 FontForge development continues on GitHub.");
227 pt += u_strlen(pt);
228 lines[linecnt++] = pt;
229
230 uc_strcpy(pt," ");
231 pt += u_strlen(pt);
232 lines[linecnt++] = pt;
233
234 uc_strcpy(pt," Version:");
235 uc_strcat(pt,FONTFORGE_VERSION);
236 pt += u_strlen(pt);
237 lines[linecnt++] = pt;
238
239 // Can be empty if e.g. building from a tarball
240 if (FONTFORGE_GIT_VERSION[0] != '\0') {
241 uc_strcpy(pt," ");
242 uc_strcat(pt, FONTFORGE_GIT_VERSION);
243 pt += u_strlen(pt);
244 lines[linecnt++] = pt;
245 }
246
247 uc_strcat(pt," Built: ");
248 uc_strcat(pt,FONTFORGE_MODTIME_STR);
249 uc_strcat(pt,"-ML");
250 #ifdef FREETYPE_HAS_DEBUGGER
251 uc_strcat(pt,"-TtfDb");
252 #endif
253 #ifdef _NO_PYTHON
254 uc_strcat(pt,"-NoPython");
255 #endif
256 #ifdef FONTFORGE_CONFIG_USE_DOUBLE
257 uc_strcat(pt,"-D");
258 #endif
259 pt += u_strlen(pt);
260 lines[linecnt++] = pt;
261 lines[linecnt] = NULL;
262 is = u_strchr(msg,'(')+1;
263 ie = u_strchr(msg,')');
264 }
265
266 void DelayEvent(void (*func)(void *), void *data) {
267 struct delayed_event *info = calloc(1,sizeof(struct delayed_event));
268
269 info->data = data;
270 info->func = func;
271 GDrawRequestTimer(splashw,100,0,info);
272 }
273
274 static void DoDelayedEvents(GEvent *event) {
275 GTimer *t = event->u.timer.timer;
276 struct delayed_event *info = (struct delayed_event *) (event->u.timer.userdata);
277
278 if ( info!=NULL ) {
279 (info->func)(info->data);
280 free(info);
281 }
282 GDrawCancelTimer(t);
283 }
284
285 struct argsstruct {
286 int next;
287 int argc;
288 char **argv;
289 int any;
290 };
291
292 static void SendNextArg(struct argsstruct *args) {
293 int i;
294 char *msg;
295 static GTimer *timeout;
296
297 if ( timeout!=NULL ) {
298 GDrawCancelTimer(timeout);
299 timeout = NULL;
300 }
301
302 for ( i=args->next; i<args->argc; ++i ) {
303 if ( *args->argv[i]!='-' ||
304 strcmp(args->argv[i],"-quit")==0 || strcmp(args->argv[i],"--quit")==0 ||
305 strcmp(args->argv[i],"-new")==0 || strcmp(args->argv[i],"--new")==0 )
306 break;
307 }
308 if ( i>=args->argc ) {
309 if ( args->any )
310 exit(0); /* Sent everything */
311 msg = "-open";
312 } else
313 msg = args->argv[i];
314 args->next = i+1;
315 args->any = true;
316
317 GDrawGrabSelection(splashw,sn_user1);
318 GDrawAddSelectionType(splashw,sn_user1,"STRING",
319 copy(msg),strlen(msg),1,
320 NULL,NULL);
321
322 /* If we just sent the other fontforge a request to die, it will never*/
323 /* take the selection back. So we should just die quietly */
324 /* But we can't die instantly, or it will never get our death threat */
325 /* (it won't have a chance to ask us for the selection if we're dead)*/
326 timeout = GDrawRequestTimer(splashw,1000,0,NULL);
327 }
328
329 /* When we want to send filenames to another running fontforge we want a */
330 /* different event handler. We won't have a splash window in that case, */
331 /* just an invisible utility window on which we perform a little selection */
332 /* dance */
333 static int request_e_h(GWindow gw, GEvent *event) {
334
335 if ( event->type == et_selclear ) {
336 SendNextArg( GDrawGetUserData(gw));
337 } else if ( event->type == et_timer )
338 exit( 0 );
339
340 return( true );
341 }
342
343 static void PingOtherFontForge(int argc, char **argv) {
344 struct argsstruct args;
345
346 args.next = 1;
347 args.argc = argc;
348 args.argv = argv;
349 args.any = false;
350 GDrawSetUserData(splashw,&args);
351 SendNextArg(&args);
352 GDrawEventLoop(NULL);
353 exit( 0 ); /* But the event loop should never return */
354 }
355
356 static void start_splash_screen(void){
357 GDrawSetVisible(splashw,true);
358 GDrawSync(NULL);
359 GDrawProcessPendingEvents(NULL);
360 GDrawProcessPendingEvents(NULL);
361
362 splasht = GDrawRequestTimer(splashw,7000,1000,NULL);
363
364 localsplash = false;
365 }
366
367 static int splash_e_h(GWindow gw, GEvent *event) {
368 GRect old;
369 int i, y, x;
370 static char *foolishness[] = {
371 /* GT: These strings are for fun. If they are offensive or incomprehensible */
372 /* GT: simply translate them as something dull like: "FontForge" */
373 /* GT: This is a spoof of political slogans, designed to point out how foolish they are */
374 N_("A free press discriminates\nagainst the illiterate."),
375 N_("A free press discriminates\nagainst the illiterate."),
376 /* GT: This is a pun on the old latin drinking song "Gaudeamus igature!" */
377 N_("Gaudeamus Ligature!"),
378 N_("Gaudeamus Ligature!"),
379 /* GT: Spoof on the bible */
380 N_("In the beginning was the letter..."),
381 /* GT: Some wit at MIT came up with this ("ontology recapitulates phylogony" is the original) */
382 N_("fontology recapitulates file-ogeny")
383 };
384
385 switch ( event->type ) {
386 case et_create:
387 GDrawGrabSelection(gw,sn_user1);
388 break;
389 case et_expose:
390 GDrawPushClip(gw,&event->u.expose.rect,&old);
391 GDrawDrawImage(gw,splashimagep,NULL,0,0);
392 if ((event->u.expose.rect.y+event->u.expose.rect.height) > splashimagep->u.image->height) {
393 GDrawSetFont(gw,splash_font.fi);
394 y = splashimagep->u.image->height + as + fh/2;
395 for ( i=1; i<linecnt; ++i ) {
396 // The number 10 comes from lines[linecnt] created in the function SplashLayout. It refers
397 // to the line at which we want to make the font monospace. If you add or remove a line,
398 // you will need to change this.
399 if (i == 10) {
400 x = 8+GDrawDrawText(gw,8,y,lines[i-1]+1,0,splashfg);
401 GDrawSetFont(gw,splash_monofont.fi);
402 GDrawDrawText(gw,8,y,lines[i-1]+1,lines[i]-lines[i-1]-1,splashfg);
403 } else if ( is>=lines[i-1]+1 && is<lines[i] ) {
404 x = 8+GDrawDrawText(gw,8,y,lines[i-1]+1,is-lines[i-1]-1,splashfg);
405 GDrawSetFont(gw,splash_italicfont.fi);
406 GDrawDrawText(gw,x,y,is,lines[i]-is,splashfg);
407 } else if ( ie>=lines[i-1]+1 && ie<lines[i] ) {
408 x = 8+GDrawDrawText(gw,8,y,lines[i-1]+1,ie-lines[i-1]-1,splashfg);
409 GDrawSetFont(gw,splash_font.fi);
410 GDrawDrawText(gw,x,y,ie,lines[i]-ie,splashfg);
411 } else
412 GDrawDrawText(gw,8,y,lines[i-1]+1,lines[i]-lines[i-1]-1,splashfg);
413 y += fh;
414 }
415 }
416 GDrawPopClip(gw,&old);
417 break;
418 case et_map:
419 // The splash screen used to gradually resize the longer it was displayed.
420 // This was removed. But there is a gxdraw bug which prevents
421 // the splash from being displayed properly unless a resize occurs.
422 // So this forces a resize to make it display properly...
423 GDrawGetSize(gw, &old);
424 if (old.height < splashimagep->u.image->height) {
425 GDrawResize(gw,splashimagep->u.image->width,splashimagep->u.image->height);
426 }
427 break;
428 case et_timer:
429 if ( event->u.timer.timer==autosave_timer ) {
430 DoAutoSaves();
431 } else if ( event->u.timer.timer==splasht ) {
432 GGadgetEndPopup();
433 GDrawSetVisible(gw,false);
434 GDrawCancelTimer(splasht);
435 splasht = NULL;
436 } else {
437 DoDelayedEvents(event);
438 }
439 break;
440 case et_char:
441 case et_mousedown:
442 case et_close:
443 GGadgetEndPopup();
444 GDrawSetVisible(gw,false);
445 break;
446 case et_mousemove:
447 GGadgetPreparePopup8(gw,_(foolishness[rand()%(sizeof(foolishness)/sizeof(foolishness[0]))]) );
448 break;
449 case et_selclear:
450 /* If this happens, it means someone wants to send us a message with a*/
451 /* filename to open. So we need to ask for it, process it, and then */
452 /* take the selection back again */
453 if ( event->u.selclear.sel == sn_user1 ) {
454 int len;
455 char *arg;
456 arg = GDrawRequestSelection(splashw,sn_user1,"STRING",&len);
457 if ( arg==NULL )
458 return( true );
459 if ( strcmp(arg,"-new")==0 || strcmp(arg,"--new")==0 )
460 FontNew();
461 else if ( strcmp(arg,"-open")==0 || strcmp(arg,"--open")==0 )
462 _FVMenuOpen(NULL);
463 else if ( strcmp(arg,"-quit")==0 || strcmp(arg,"--quit")==0 )
464 MenuExit(NULL,NULL,NULL);
465 else
466 ViewPostScriptFont(arg,0);
467 free(arg);
468 GDrawGrabSelection(splashw,sn_user1);
469 }
470 break;
471 case et_destroy:
472 IError("Who killed the splash screen?");
473 break;
474 default: break;
475 }
476 return( true );
477 }
478
479 static int ReopenLastFonts(void) {
480 char buffer[1024];
481 char *ffdir = getFontForgeUserDir(Config);
482 FILE *old;
483 int any = 0;
484
485 if ( ffdir==NULL ) return false;
486
487 sprintf( buffer, "%s/FontsOpenAtLastQuit", ffdir );
488 old = fopen(buffer,"r");
489 if ( old==NULL ) {
490 free(ffdir);
491 return false;
492 }
493 while ( fgets(buffer,sizeof(buffer),old)!=NULL ) {
494 if ( ViewPostScriptFont(g_strchomp(buffer),0)!=0 )
495 any = 1;
496 }
497 fclose(old);
498 free(ffdir);
499 return any;
500 }
501
502 static void GrokNavigationMask(void) {
503 extern int navigation_mask;
504
505 navigation_mask = GMenuItemParseMask(H_("NavigationMask|None"));
506 }
507
508 /**
509 * Create the directory basedir/dirname with the given mode.
510 * Silently ignore any errors that might happen.
511 */
512 static void ffensuredir( const char* basedir, const char* dirname, mode_t mode ) {
513 const int buffersz = PATH_MAX;
514 char buffer[buffersz+1];
515
516 snprintf(buffer,buffersz,"%s/%s", basedir, dirname );
517 // ignore errors, this is just to help the user aftre all.
518 GFileMkDir( buffer, mode );
519 }
520
521 static void ensureDotFontForgeIsSetup() {
522 char *basedir = getFontForgeUserDir(Config);
523 if ( !basedir ) {
524 return;
525 }
526 ffensuredir( basedir, "", S_IRWXU );
527 ffensuredir( basedir, "python", S_IRWXU );
528 free(basedir);
529 }
530
531 #if defined(__MINGW32__)
532 /**
533 * \brief Load fonts from the specified folder for the UI to use.
534 * This should only be used if Cairo is used on Windows, which defaults to the
535 * Win32 font backend.
536 * This is an ANSI version, so files which contain characters outside of the
537 * user's locale will fail to be loaded.
538 * \param prefix The folder to read fonts from. Currently the pixmaps folder
539 * and the folder 'ui-fonts' in the FontForge preferences folder.
540 */
541 static void WinLoadUserFonts(const char *prefix) {
542 HANDLE fileHandle;
543 WIN32_FIND_DATA fileData;
544 char path[MAX_PATH], *ext;
545 HRESULT ret;
546
547 if (prefix == NULL) {
548 return;
549 }
550 ret = snprintf(path, MAX_PATH, "%s/*.???", prefix);
551 if (ret <= 0 || ret >= MAX_PATH) {
552 return;
553 }
554
555 fileHandle = FindFirstFileA(path, &fileData);
556 if (fileHandle != INVALID_HANDLE_VALUE) do {
557 ext = strrchr(fileData.cFileName, '.');
558 if (!ext || (strcasecmp(ext, ".ttf") && strcasecmp(ext, ".ttc") &&
559 strcasecmp(ext,".otf")))
560 {
561 continue;
562 }
563 ret = snprintf(path, MAX_PATH, "%s/%s", prefix, fileData.cFileName);
564 if (ret > 0 && ret < MAX_PATH) {
565 //printf("WIN32-FONT-TEST: %s\n", path);
566 ret = AddFontResourceExA(path, FR_PRIVATE, NULL);
567 //if (ret > 0) {
568 // printf("\tLOADED FONT OK!\n");
569 //}
570 }
571 } while (FindNextFileA(fileHandle, &fileData) != 0);
572 }
573 #endif
574
575
576 int fontforge_main( int argc, char **argv ) {
577 const char *load_prefs = getenv("FONTFORGE_LOADPREFS");
578 int i;
579 int recover=2;
580 int any;
581 int next_recent=0;
582 int run_python_init_files = true;
583 int import_python_plugins = true;
584 GRect pos;
585 GWindowAttrs wattrs;
586 char *display = NULL;
587 int ds, ld;
588 int openflags=0;
589 int doopen=0, quit_request=0;
590
591 #if !(GLIB_CHECK_VERSION(2, 35, 0))
592 g_type_init();
593 #endif
594
595 #ifdef __Mac
596 extern void setup_cocoa_app(void);
597 setup_cocoa_app();
598 hotkeySystemSetCanUseMacCommand(true);
599 #endif
600
601 /* Must be done before we cache the current directory */
602 /* Change to HOME dir if specified on the commandline */
603 for ( i=1; i<argc; ++i ) {
604 char *pt = argv[i];
605 if ( pt[0]=='-' && pt[1]=='-' ) ++pt;
606 if (strcmp(pt,"-home")==0) {
607 if (getenv("HOME")!=NULL) chdir(getenv("HOME"));
608 break; /* Done - Unnecessary to check more arguments */
609 }
610 if (strcmp(pt,"-quiet")==0)
611 quiet = 1;
612 }
613
614 if (!quiet) {
615 time_t tm = FONTFORGE_MODTIME_RAW;
616 struct tm* modtime = gmtime(&tm);
617 fprintf( stderr, "Copyright (c) 2000-%d. See AUTHORS for Contributors.\n", modtime->tm_year+1900 );
618 fprintf( stderr, " License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" );
619 fprintf( stderr, " with many parts BSD <http://fontforge.org/license.html>. Please read LICENSE.\n" );
620 fprintf( stderr, " Version: %s\n", FONTFORGE_VERSION );
621 fprintf( stderr, " Based on sources from %s"
622 "-ML"
623 #ifdef FREETYPE_HAS_DEBUGGER
624 "-TtfDb"
625 #endif
626 #ifdef _NO_PYTHON
627 "-NoPython"
628 #endif
629 #ifdef FONTFORGE_CONFIG_USE_DOUBLE
630 "-D"
631 #endif
632 ".\n",
633 FONTFORGE_MODTIME_STR );
634 // Can be empty if e.g. building from a tarball
635 if (FONTFORGE_GIT_VERSION[0] != '\0') {
636 fprintf( stderr, " Based on source from git with hash: %s\n", FONTFORGE_GIT_VERSION );
637 }
638 }
639
640 #if defined(__MINGW32__)
641 if( getenv("DISPLAY")==NULL ) {
642 putenv("DISPLAY=127.0.0.1:0.0");
643 }
644 if( getenv("LC_ALL")==NULL ){
645 char lang[8];
646 char env[32];
647 if( GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, lang, 8) > 0 ){
648 strcpy(env, "LC_ALL=");
649 strcat(env, lang);
650 putenv(env);
651 }
652 }
653 #endif
654
655 FF_SetUiInterface(&gdraw_ui_interface);
656 FF_SetPrefsInterface(&gdraw_prefs_interface);
657 FF_SetSCInterface(&gdraw_sc_interface);
658 FF_SetCVInterface(&gdraw_cv_interface);
659 FF_SetBCInterface(&gdraw_bc_interface);
660 FF_SetFVInterface(&gdraw_fv_interface);
661 FF_SetFIInterface(&gdraw_fi_interface);
662 FF_SetMVInterface(&gdraw_mv_interface);
663 FF_SetClipInterface(&gdraw_clip_interface);
664 #ifndef _NO_PYTHON
665 PythonUI_Init();
666 #endif
667
668 FindProgRoot(argv[0]);
669 InitSimpleStuff();
670
671 #if defined(__MINGW32__)
672 {
673 char path[MAX_PATH];
674 unsigned int len = GetModuleFileNameA(NULL, path, MAX_PATH);
675 path[len] = '\0';
676
677 //The '.exe' must be removed as resources presumes it's not there.
678 GResourceSetProg(GFileRemoveExtension(GFileNormalizePath(path)));
679 }
680 #else
681 GResourceSetProg(argv[0]);
682 #endif
683
684 GMenuSetShortcutDomain("FontForge-MenuShortCuts");
685 bind_textdomain_codeset("FontForge-MenuShortCuts","UTF-8");
686 bindtextdomain("FontForge-MenuShortCuts", getLocaleDir());
687
688 bind_textdomain_codeset("FontForge","UTF-8");
689 bindtextdomain("FontForge", getLocaleDir());
690 textdomain("FontForge");
691 {
692 GGadgetSetImageDir( getPixmapDir() );
693 char* path = smprintf("%s/resources/fontforge.resource", getShareDir());
694 GResourceAddResourceFile(path, GResourceProgramName,false);
695 free(path);
696 }
697 hotkeysLoad();
698 // loadPrefsFiles();
699 Prefs_LoadDefaultPreferences();
700
701 if ( load_prefs!=NULL && strcasecmp(load_prefs,"Always")==0 )
702 LoadPrefs();
703 if ( default_encoding==NULL )
704 default_encoding=FindOrMakeEncoding("ISO8859-1");
705 if ( default_encoding==NULL )
706 default_encoding=&custom; /* In case iconv is broken */
707
708 // This no longer starts embedded Python unless control passes to the Python executors,
709 // which exit independently rather than returning here.
710 CheckIsScript(argc,argv); /* Will run the script and exit if it is a script */
711 /* If there is no UI, there is always a script */
712 /* and we will never return from the above */
713 if ( load_prefs==NULL ||
714 (strcasecmp(load_prefs,"Always")!=0 && /* Already loaded */
715 strcasecmp(load_prefs,"Never")!=0 ))
716 LoadPrefs();
717 GrokNavigationMask();
718 for ( i=1; i<argc; ++i ) {
719 char *pt = argv[i];
720 if ( pt[0]=='-' && pt[1]=='-' )
721 ++pt;
722 if ( strcmp(pt,"-sync")==0 )
723 GResourceAddResourceString("Gdraw.Synchronize: true",argv[0]);
724 else if ( strcmp(pt,"-display")==0 && i<argc-1 )
725 display = argv[++i];
726 # if MyMemory
727 else if ( strcmp(pt,"-memory")==0 )
728 __malloc_debug(5);
729 # endif
730 else if ( strcmp(pt,"-nosplash")==0 )
731 splash = 0;
732 else if ( strcmp(pt,"-quiet")==0 )
733 /* already checked for this earlier, no need to do it again */;
734 else if ( strcmp(pt,"-unique")==0 )
735 unique = 1;
736 else if ( strcmp(pt,"-recover")==0 && i<argc-1 ) {
737 ++i;
738 if ( strcmp(argv[i],"none")==0 )
739 recover=0;
740 else if ( strcmp(argv[i],"clean")==0 )
741 recover= -1;
742 else if ( strcmp(argv[i],"auto")==0 )
743 recover= 1;
744 else if ( strcmp(argv[i],"inquire")==0 )
745 recover= 2;
746 else {
747 fprintf( stderr, "Invalid argument to -recover, must be none, auto, inquire or clean\n" );
748 dousage();
749 }
750 } else if ( strcmp(pt,"-recover=none")==0 ) {
751 recover = 0;
752 } else if ( strcmp(pt,"-recover=clean")==0 ) {
753 recover = -1;
754 } else if ( strcmp(pt,"-recover=auto")==0 ) {
755 recover = 1;
756 } else if ( strcmp(pt,"-recover=inquire")==0 ) {
757 recover = 2;
758 } else if ( strcmp(pt,"-docs")==0 )
759 dohelp();
760 else if ( strcmp(pt,"-help")==0 )
761 dousage();
762 else if ( strcmp(pt,"-version")==0 || strcmp(pt,"-v")==0 || strcmp(pt,"-V")==0 )
763 doversion(FONTFORGE_VERSION);
764 else if ( strcmp(pt,"-quit")==0 )
765 quit_request = true;
766 else if ( strcmp(pt,"-home")==0 )
767 /* already did a chdir earlier, don't need to do it again */;
768 }
769 gdk_set_allowed_backends("win32,quartz,x11");
770 gtk_init(&argc, &argv);
771 ensureDotFontForgeIsSetup();
772 #if defined(__MINGW32__)
773 //Load any custom fonts for the user interface
774 const char *system_load = getShareDir();
775 char *user_load = getFontForgeUserDir(Data);
776 char lbuf[MAX_PATH];
777 int lret;
778
779 if (system_load != NULL) {
780 //Follow the FontConfig APPSHAREFONTDIR location
781 lret = snprintf(lbuf, MAX_PATH, "%s/../fonts", system_load);
782 if (lret > 0 && lret < MAX_PATH) {
783 WinLoadUserFonts(lbuf);
784 }
785 }
786 if (user_load != NULL) {
787 lret = snprintf(lbuf, MAX_PATH, "%s/%s", user_load, "ui-fonts");
788 if (lret > 0 && lret < MAX_PATH) {
789 WinLoadUserFonts(lbuf);
790 }
791 free(user_load);
792 }
793 #endif
794 InitImageCache(); // This is in gtextinfo.c. It zeroes imagecache for us.
795 atexit(&ClearImageCache); // We register the destructor, which is also in gtextinfo.c.
796 GDrawCreateDisplays(display,argv[0]);
797 atexit(&GDrawDestroyDisplays); // We register the destructor so that it runs even if we call exit without finishing this function.
798 default_background = GDrawGetDefaultBackground(screen_display);
799 InitToolIconClut(default_background);
800 InitToolIcons();
801 InitCursors();
802
803 /**
804 * we have to do a quick sniff of argv[] here to see if the user
805 * wanted to skip loading these python init files.
806 */
807 for ( i=1; i<argc; ++i ) {
808 char *pt = argv[i];
809
810 if ( strcmp(pt,"-SkipPythonInitFiles")==0 || strcmp(pt,"-skippyfile")==0 ) {
811 run_python_init_files = false;
812 } else if ( strcmp(pt,"-skippyplug")==0 ) {
813 import_python_plugins = false;
814 }
815 }
816
817 // Silence unused warnings, depending on ifdefs
818 (void)run_python_init_files;
819 (void)import_python_plugins;
820
821 #ifndef _NO_PYTHON
822 /*# ifndef GWW_TEST*/
823 FontForge_InitializeEmbeddedPython(); /* !!!!!! debug (valgrind doesn't like python) */
824 /*# endif*/
825 #endif
826
827 #ifndef _NO_PYTHON
828 PyFF_ProcessInitFiles(run_python_init_files, import_python_plugins);
829 #endif
830
831 /* the splash screen used not to have a title bar (wam_nodecor) */
832 /* but I found I needed to know how much the window manager moved */
833 /* the window around, which I can determine if I have a positioned */
834 /* decorated window created at the beginning */
835 /* Actually I don't care any more */
836 wattrs.mask = wam_events|wam_cursor|wam_bordwidth|wam_backcol|wam_positioned|wam_utf8_wtitle|wam_isdlg;
837 wattrs.event_masks = ~(1<<et_charup);
838 wattrs.positioned = 1;
839 wattrs.cursor = ct_pointer;
840 wattrs.utf8_window_title = "FontForge";
841 wattrs.border_width = 2;
842 wattrs.background_color = splashbg;
843 wattrs.is_dlg = true;
844 pos.x = pos.y = 200;
845 SplashImageInit();
846 pos.width = splashimagep->u.image->width;
847 pos.height = splashimagep->u.image->height-1; // See splash_e_h:et_map
848 GDrawBindSelection(NULL,sn_user1,"FontForge");
849 if ( unique && GDrawSelectionOwned(NULL,sn_user1)) {
850 /* Different event handler, not a dialog */
851 wattrs.is_dlg = false;
852 splashw = GDrawCreateTopWindow(NULL,&pos,request_e_h,NULL,&wattrs);
853 PingOtherFontForge(argc,argv);
854 } else {
855 if ( quit_request )
856 exit( 0 );
857 splashw = GDrawCreateTopWindow(NULL,&pos,splash_e_h,NULL,&wattrs);
858 }
859
860 GDrawSetFont(splashw,splash_font.fi);
861
862 SplashLayout();
863 localsplash = splash;
864
865 if ( localsplash )
866 start_splash_screen();
867
868 //
869 // The below call will initialize the fontconfig cache if required.
870 // That can take a while the first time it happens.
871 //
872 GDrawWindowFontMetrics(splashw,splash_font.fi,&as,&ds,&ld);
873 fh = as+ds+ld;
874
875 if ( AutoSaveFrequency>0 )
876 autosave_timer=GDrawRequestTimer(splashw,2*AutoSaveFrequency*1000,AutoSaveFrequency*1000,NULL);
877
878 GDrawProcessPendingEvents(NULL);
879
880 any = 0;
881 if ( recover==-1 )
882 CleanAutoRecovery();
883 else if ( recover )
884 any = DoAutoRecoveryExtended( recover-1 );
885
886 openflags = 0;
887 for ( i=1; i<argc; ++i ) {
888 char *buffer = NULL;
889 char *pt = argv[i];
890
891 GDrawProcessPendingEvents(NULL);
892 if ( pt[0]=='-' && pt[1]=='-' && pt[2]!='\0')
893 ++pt;
894 if ( strcmp(pt,"-new")==0 ) {
895 FontNew();
896 any = 1;
897 # if HANYANG
898 } else if ( strcmp(pt,"-newkorean")==0 ) {
899 MenuNewComposition(NULL,NULL,NULL);
900 any = 1;
901 # endif
902 } else if ( strcmp(pt,"-SkipPythonInitFiles")==0 ||
903 strcmp(pt,"-skippyfile")==0 ||
904 strcmp(pt,"-skippyplug")==0 ) {
905 // already handled above.
906 } else if ( strcmp(pt,"-last")==0 ) {
907 if ( next_recent<RECENT_MAX && RecentFiles[next_recent]!=NULL )
908 if ( ViewPostScriptFont(RecentFiles[next_recent++],openflags))
909 any = 1;
910 } else if ( strcmp(pt,"-sync")==0 || strcmp(pt,"-memory")==0 ||
911 strcmp(pt,"-nosplash")==0 || strcmp(pt,"-recover=none")==0 ||
912 strcmp(pt,"-recover=clean")==0 || strcmp(pt,"-recover=auto")==0 ||
913 strcmp(pt,"-unique")==0 ||
914 strcmp(pt,"-home")==0 || strcmp(pt,"-quiet")==0
915 || strcmp(pt,"-forceuihidden")==0 )
916 /* Already done, needed to be before display opened */;
917 else if ( ( strcmp(pt,"-display")==0 || strcmp(pt,"-recover")==0 ) &&
918 i<argc-1 )
919 ++i; /* Already done, needed to be before display opened */
920 else if ( strcmp(pt,"-allglyphs")==0 )
921 openflags |= of_all_glyphs_in_ttc;
922 else if ( strcmp(pt,"-open")==0 )
923 doopen = true;
924 else {
925 // printf("else argv[i]:%s\n", argv[i] );
926 buffer = GFileGetAbsoluteName(argv[i]);
927 if ( GFileIsDir(buffer) ) {
928 char *fname;
929 fname = malloc(strlen(buffer)+strlen("/glyphs/contents.plist")+1);
930 strcpy(fname,buffer); strcat(fname,"/glyphs/contents.plist");
931 if ( GFileExists(fname)) {
932 /* It's probably a Unified Font Object directory */
933 free(fname);
934 if ( ViewPostScriptFont(buffer,openflags) )
935 any = 1;
936 } else {
937 strcpy(fname,buffer); strcat(fname,"/font.props");
938 if ( GFileExists(fname)) {
939 /* It's probably a sf dir collection */
940 free(fname);
941 if ( ViewPostScriptFont(buffer,openflags) )
942 any = 1;
943 } else {
944 free(fname);
945 if ( buffer[strlen(buffer)-1]!='/' ) {
946 /* If dirname doesn't end in "/" we'll be looking in parent dir */
947 buffer[strlen(buffer)+1]='\0';
948 buffer[strlen(buffer)] = '/';
949 }
950 fname = GetPostScriptFontName(buffer,false,false);
951 if ( fname!=NULL )
952 ViewPostScriptFont(fname,openflags);
953 any = 1; /* Even if we didn't get a font, don't bring up dlg again */
954 free(fname);
955 }
956 }
957 } else if ( ViewPostScriptFont(buffer,openflags)!=0 )
958 any = 1;
959 free(buffer);
960 }
961 }
962 if ( !any && !doopen )
963 any = ReopenLastFonts();
964
965 #ifdef __Mac
966 extern bool launch_cocoa_app(void);
967 any = launch_cocoa_app() || any;
968 #endif
969
970 if ( doopen || !any )
971 _FVMenuOpen(NULL);
972 GDrawEventLoop(NULL);
973 GDrawDestroyDisplays();
974
975 #ifndef _NO_PYTHON
976 /*# ifndef GWW_TEST*/
977 FontForge_FinalizeEmbeddedPython(); /* !!!!!! debug (valgrind doesn't like python) */
978 /*# endif*/
979 #endif
980
981 // These free menu translations, mostly.
982 BitmapViewFinishNonStatic();
983 MetricsViewFinishNonStatic();
984 CharViewFinishNonStatic();
985 FontViewFinishNonStatic();
986
987 ClearImageCache(); // This frees the contents of imagecache.
988 // hotkeysSave();
989 LastFonts_Save();
990
991 return( 0 );
992 }