1 /* Copyright (C) 2007-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 #ifndef FONTFORGE_UIINTERFACE_H
29 #define FONTFORGE_UIINTERFACE_H
30 
31 #include <fontforge-config.h>
32 
33 #include "basics.h"
34 
35 /* This encapsulates a set of callbacks and stubs. The callbacks get activated*/
36 /*  when an event happens (a glyph in a font changes for example, then all */
37 /*  charviews looking at it must be updated), and the stubs provide some simple*/
38 /*  UI routines: Post an error, etc. */
39 
40 /* ************************************************************************** */
41 /* Basic, low-level UI routines for events we discover deep inside script code*/
42 /* ************************************************************************** */
43 
44 struct importparams;
45 struct exportparams;
46 
47 struct ui_interface {
48    /* The following is used to post a fontforge internal error */
49    /* currently it puts up a dlg displaying the error text */
50     void (*ierror)(const char *fmt,...);
51 
52    /* The following is a simple dialog to alert the user that s/he has */
53    /*  made an error. Currently it posts a modal dlg and waits for the */
54    /*  user to dismiss it */
55    /* The title argument is the window's title. The error argument is the */
56    /*  text of the message. It may contain printf formatting. It may contain */
57    /*  newlines to force line breaks -- even if it doesn't contain new lines */
58    /*  the routine will wrap the text if a line is too long */
59     void (*post_error)(const char *title,const char *error,...);
60 
61    /* The following is used to post a warning message in such a way that it */
62    /*  will not impede the user. Currently it creates a little window at the */
63    /*  bottom right of the screen and writes successive messages there */
64     void (*logwarning)(const char *fmt,...);
65 
66    /* The following is another way to post a warning message in such a way */
67    /*  that it will not impede the user. Currently it pops up a little */
68    /*  non-modal dlg which vanishes after a minute or two (or if the user */
69    /*  dismisses it, of course */
70     void (*post_warning)(const char *title,const char *statement,...);
71 
72    /* Occasionally we we be deep in a non-ui routine and we find we must ask */
73    /*  the user a question. In this routine the choices are displayed as */
74    /*  buttons, one button is the default, another is a cancel choice */
75     int (*ask)(const char *title, const char **answers,
76 	    int def, int cancel,const char *question,...);
77 
78    /* Similar to the above, except here the choices are presented as a */
79    /*  scrolled list. Return -1 if the user cancels */
80     int (*choose)(const char *title, const char **answers,
81 	    int def, int cancel,const char *question,...);
82 
83     /* Multiple things can be selected, sel is an in/out parameter, one byte */
84     /*  per entry in the choice array. 0=> not selected, 1=>selected */
85     int (*choose_multiple)(const char *title, const char **choices, char *sel,
86 	    int cnt, char *buts[2], const char *question,...);
87 
88    /* Here we want a string. We are passed a default answer (or NULL) */
89    /* The return is NULL on cancel, otherwise a string which must be freed */
90     char *(*ask_string)(const char *title,
91 	    const char *def,const char *question,...);
92    /* Same as above, except for entering a password */
93     char *(*ask_password)(const char *title,
94 	    const char *def,const char *question,...);
95 
96    /* The next two routines are only used in the python interface to provide */
97    /*  a python script running in ff a way to open a file */
98    /* Arguments are a window title for the dlg, a default file (or NULL), and */
99    /*  an initial filter (unix wildcards) or NULL */
100     char *(*open_file)(const char *title, const char *defaultfile,
101 	const char *initial_filter);
102     char *(*saveas_file)(const char *title, const char *defaultfile,
103 	const char *initial_filter);
104 
105     /* These routines are for a progress indicator */
106     void (*progress_start)(int delay, const char *title, const char *line1,
107 	const char *line2, int tot, int stages);
108     void (*progress_end)(void);
109     void (*progress_show)(void);
110     void (*progress_enable_stop)(int);
111     int (*progress_next)(void);
112     int (*progress_next_stage)(void);
113     int (*progress_increment)(int);
114     void (*progress_change_line1)(const char *);
115     void (*progress_change_line2)(const char *);
116     void (*progress_pause)(void);
117     void (*progress_resume)(void);
118     void (*progress_change_stages)(int);
119     void (*progress_change_total)(int);
120     int  (*progress_reset)(void);
121 
122     void (*allow_events)(void);
123 
124    /* These next few provide friendly names of various opentype tags */
125    /*  The ui version will probably be translated, while the non-ui list */
126    /*  will probably not. The distinction isn't necessary, but is present in ff*/
127     const char *(*strid)(int);
128     const char *(*mslang)(int);
129 
130     void (*import_dlg)(struct importparams *ip);
131     void (*export_dlg)(struct exportparams *ep);
132 };
133 extern struct ui_interface *ui_interface;
134 
135 #define IError			(ui_interface->ierror)
136 #define LogError		(ui_interface->logwarning)
137 #define ff_post_notice		(ui_interface->post_warning)
138 #define ff_post_error		(ui_interface->post_error)
139 #define ff_ask			(ui_interface->ask)
140 #define ff_choose		(ui_interface->choose)
141 #define ff_choose_multiple	(ui_interface->choose_multiple)
142 #define ff_ask_string		(ui_interface->ask_string)
143 #define ff_ask_password		(ui_interface->ask_password)
144 
145 #define ff_open_filename	(ui_interface->open_file)
146 #define ff_save_filename	(ui_interface->saveas_file)
147 
148 #define ff_progress_start_indicator	(ui_interface->progress_start)
149 #define ff_progress_end_indicator	(ui_interface->progress_end)
150 #define ff_progress_show		(ui_interface->progress_show)
151 #define ff_progress_enable_stop		(ui_interface->progress_enable_stop)
152 #define ff_progress_next		(ui_interface->progress_next)
153 #define ff_progress_next_stage		(ui_interface->progress_next_stage)
154 #define ff_progress_increment		(ui_interface->progress_increment)
155 #define ff_progress_change_line1	(ui_interface->progress_change_line1)
156 #define ff_progress_change_line2	(ui_interface->progress_change_line2)
157 #define ff_progress_pause_timer		(ui_interface->progress_pause)
158 #define ff_progress_resume_timer	(ui_interface->progress_resume)
159 #define ff_progress_change_stages	(ui_interface->progress_change_stages)
160 #define ff_progress_change_total	(ui_interface->progress_change_total)
161 #define ff_progress_reset		(ui_interface->progress_reset)
162 
163 #define ff_progress_allow_events	(ui_interface->allow_events)
164 
165 #define TTFNameIds			(ui_interface->strid)
166 #define MSLangString			(ui_interface->mslang)
167 
168 #define ImportParamsDlg			(ui_interface->import_dlg)
169 #define ExportParamsDlg			(ui_interface->export_dlg)
170 
171 void FF_SetUiInterface(struct ui_interface *uii);
172 
173 /* ************************************************************************** */
174 /*                                Preferences                                 */
175 /* ************************************************************************** */
176 struct val;
177 
178 struct prefs_interface {
179     void  (*save_prefs)(int not_if_running_script);
180     void  (*load_prefs)(void);
181     int   (*get_prefs)(char *name,struct val *value);
182     int   (*set_prefs)(char *name,struct val *val1, struct val *val2);
183     char *(*get_exe_share_dir)(void);
184     void  (*init_prefs)(void);
185 };
186 extern struct prefs_interface *prefs_interface;
187 
188 #define SavePrefs		(prefs_interface->save_prefs)
189 #define LoadPrefs		(prefs_interface->load_prefs)
190 #define GetPrefs		(prefs_interface->get_prefs)
191 #define SetPrefs		(prefs_interface->set_prefs)
192 #define getFontForgeShareDir	(prefs_interface->get_exe_share_dir)
193 #define SetDefaults		(prefs_interface->init_prefs)
194 
195 void FF_SetPrefsInterface(struct prefs_interface *prefsi);
196 
197 /* ************************************************************************** */
198 /*                          Updating glyph windows                            */
199 /* ************************************************************************** */
200 
201 struct splinechar;
202 struct layer;
203 
204 struct sc_interface {
205    /* Update all windows looking at this glyph */
206     void  (*update_all)(struct splinechar *);
207 
208    /* Background images or kerning info have changed for this glyph and */
209    /*  all windows displaying them need to be refreshed */
210     void  (*out_of_date_background)(struct splinechar *);
211 
212    /* The name or code point or encoding of this glyph has changed */
213    /*  update all window titles of any windows looking at us */
214     void (*refresh_titles)(struct splinechar *);
215 
216    /* The hints of the glyph have changed */
217     void (*hints_changed)(struct splinechar *);
218 
219    /* Mark the glyph as changed, and force an update */
220     void (*glyph_changed_update)(struct splinechar *,int layer);
221 
222    /* As above, except this time the change might take the glyph back to */
223    /*  an "unchanged" state (ie. an Undo) */
224     void (*glyph__changed_update)(struct splinechar *,int layer,int);
225 
226    /* The glyph's instructions have changed, so any dlgs looking at */
227    /*  our instructions need to be updated */
228     void (*instructions_changed)(struct splinechar *sc);
229 
230    /* We are removing this glyph (or something like it), get rid of any */
231    /* glyph outline windows which display it */
232     void (*close_all_windows)(struct splinechar *);
233 
234    /* Called when a multilayered glyph increases its layer count */
235    /*  the charview needs to add more layers to its layer window, etc. */
236     void (*more_layers)(struct splinechar *, struct layer *);
237 };
238 extern struct sc_interface *sc_interface;
239 
240 #define SCUpdateAll			(sc_interface->update_all)
241 #define SCOutOfDateBackground		(sc_interface->out_of_date_background)
242 #define SCRefreshTitles			(sc_interface->refresh_titles)
243 #define SCHintsChanged			(sc_interface->hints_changed)
244 #define _SCCharChangedUpdate		(sc_interface->glyph__changed_update)
245 #define SCCharChangedUpdate		(sc_interface->glyph_changed_update)
246 #define SCMarkInstrDlgAsChanged		(sc_interface->instructions_changed)
247 #define SCCloseAllViews			(sc_interface->close_all_windows)
248 #define SCMoreLayers			(sc_interface->more_layers)
249 
250 void FF_SetSCInterface(struct sc_interface *sci);
251 
252 /* ************************************************************************** */
253 /*                         Updating glyph windows 2                           */
254 /* ************************************************************************** */
255 
256 struct charviewbase;
257 struct splinefont;
258 
259 struct cv_interface {
260    /* Update all windows looking at what this char window looks at */
261    /*  which might be a glyph, or perhaps the grid layer */
262    /* And mark as changed */
263     void (*glyph_changed_update)(struct charviewbase *);
264     void (*_glyph_changed_update)(struct charviewbase *, int);
265 
266    /* A glyph's name has changed find all charviews with tabs with that name */
267    /*  and update those tabs */
268     void (*glyph_name_change)(struct splinefont *sf, const char *oldname, const char *newname);
269 
270    /* We've added a layer to a font */
271     void (*layer_palette_check)(struct splinefont *sf);
272 };
273 extern struct cv_interface *cv_interface;
274 
275 #define CVCharChangedUpdate		(cv_interface->glyph_changed_update)
276 #define _CVCharChangedUpdate		(cv_interface->_glyph_changed_update)
277 #define CVGlyphRenameFixup		(cv_interface->glyph_name_change)
278 #define CVLayerPaletteCheck		(cv_interface->layer_palette_check)
279 
280 void FF_SetCVInterface(struct cv_interface *cvi);
281 
282 /* ************************************************************************** */
283 /*                         Updating bitmap windows                            */
284 /* ************************************************************************** */
285 
286 struct bdfchar;
287 
288 struct bc_interface {
289    /* Update all windows looking at this bitmap glyph */
290    /* And mark as changed */
291     void (*glyph_changed_update)(struct bdfchar *);
292 
293    /* Force a refresh on all open bitmap windows of this glyph */
294     void (*refresh_all)(struct bdfchar *);
295 
296    /* Destroy all open bitmap windows of this glyph */
297     void (*destroy_all)(struct bdfchar *);
298 };
299 extern struct bc_interface *bc_interface;
300 
301 #define BCCharChangedUpdate		(bc_interface->glyph_changed_update)
302 #define BCRefreshAll			(bc_interface->refresh_all)
303 #define BCDestroyAll			(bc_interface->destroy_all)
304 
305 void FF_SetBCInterface(struct bc_interface *bci);
306 
307 /* ************************************************************************** */
308 /*                          Access to metrics views                           */
309 /* ************************************************************************** */
310 
311 struct metricsview;
312 struct splinefont;
313 
314 struct mv_interface {
315    /* Number of glyphs displayed in the view */
316     int (*glyph_cnt)(struct metricsview *);
317 
318    /* Access to the i'th member */
319     struct splinechar *(*get_glyph)(struct metricsview *,int);
320 
321    /* Kerning (or width) information for this font has changed. Remetric the */
322    /*  metric views*/
323     void (*rekern)(struct splinefont *);
324 
325    /* Feature/lookup/subtable info for the font has changed */
326    /* Features, lookups or subtables have been added or removed */
327    /* This call should probably be followed by a call to rekern to remetric */
328     void (*refeature)(struct splinefont *);
329 
330    /* Close any metrics views associated with this font */
331     void (*sf_close_metrics)(struct splinefont *sf);
332 };
333 extern struct mv_interface *mv_interface;
334 
335 #define MVGlyphCount			(mv_interface->glyph_cnt)
336 #define MVGlyphIndex			(mv_interface->get_glyph)
337 #define MVReKernAll			(mv_interface->rekern)
338 #define MVReFeatureAll			(mv_interface->refeature)
339 #define MVDestroyAll			(mv_interface->sf_close_metrics)
340 
341 void FF_SetMVInterface(struct mv_interface *mvi);
342 
343 /* ************************************************************************** */
344 /*                             Access to font info                            */
345 /* ************************************************************************** */
346 struct otlookup;
347 
348 struct fi_interface {
349    /* Insert a new lookup into the fontinfo lookup list */
350     void (*insert_lookup)(struct splinefont *, struct otlookup *);
351 
352    /* Merge lookup in from another font */
353     void (*copy_into)(struct splinefont *, struct splinefont *,
354 	    struct otlookup *, struct otlookup *, int, struct otlookup *);
355 
356    /* Removes any font info window for this font */
357     void (*destroy)(struct splinefont *);
358 };
359 extern struct fi_interface *fi_interface;
360 
361 #define FISortInsertLookup			(fi_interface->insert_lookup)
362 #define FIOTLookupCopyInto			(fi_interface->copy_into)
363 #define FontInfo_Destroy			(fi_interface->destroy)
364 
365 /* ************************************************************************** */
366 /*                           Updating font windows                            */
367 /* ************************************************************************** */
368 
369 struct fontviewbase;
370 struct bdffont;
371 
372 struct fv_interface {
373    /* Create a new font view. Whatever that may entail */
374     struct fontviewbase *(*create_view)(struct splinefont *,int hide);
375 
376    /* Create a new font view but without attaching it to a window */
377     struct fontviewbase *(*_create_view)(struct splinefont *);
378 
379    /* Free a font view (we assume all windows have already been destroyed) */
380     void (*close_view)(struct fontviewbase *);
381 
382    /* Free a font view (we assume all windows have already been destroyed) */
383     void (*free_view)(struct fontviewbase *);
384 
385    /* Set the window title of this fontview */
386     void (*set_title)(struct fontviewbase *);
387 
388    /* Set the window title of all fontviews associated with this font */
389     void (*set_titles)(struct splinefont *);
390 
391    /* Refresh all displays of all fontviews associated with this font */
392     void (*refresh_all)(struct splinefont *);
393 
394    /* Reformat this particular fontview (after encoding change, etc) */
395     void (*reformat_one)(struct fontviewbase *);
396 
397    /* Reformat all fontviews associated with this font */
398     void (*reformat_all)(struct splinefont *);
399 
400    /* The active layer has changed. Possibly because the old one was deleted */
401     void (*layer_changed)(struct fontviewbase *);
402 
403    /* toggle the change indicator of this glyph in the font view */
404     void (*flag_glyph_changed)(struct splinechar *);
405 
406    /* Retrieve the window's size in rows and columns */
407     int  (*win_info)(struct fontviewbase *, int *cols, int *rows);
408 
409    /* Is this font currently open? (It was open once, this check is to make   */
410    /*  sure the user hasn't closed it since they copied from it -- so we can  */
411    /*  follow references appropriately if the font we are pasting into doesn't*/
412    /*  have the needed glyph */
413     int  (*font_is_active)(struct splinefont *);
414 
415    /* Sometimes we just need a fontview, any fontview as a last resort fallback*/
416     struct fontviewbase *(*first_font)(void);
417 
418    /* Append this fontview to the list of them */
419     struct fontviewbase *(*append)(struct fontviewbase *);
420 
421    /* Look through all loaded fontviews and see if any contains a font */
422    /*  which lives in the given filename */
423     struct splinefont *(*font_of_filename)(const char *);
424 
425    /* We've just added some extra encoding slots, which means we may need */
426    /*  to increase the number of rows in the fontview display and perhaps */
427    /*  adjust its scrollbar */
428     void (*extra_enc_slots)(struct fontviewbase *,int new_enc_max);
429 
430    /* My fontviews contain a glyph cache (a BDFPieceMeal font) whenever */
431    /*  more glyphs are added to the font, more bitmap glyph slots need to */
432    /*  be added to the font cache */
433     void (*bigger_glyph_cache)(struct fontviewbase *,int new_glyph_cnt);
434 
435    /* If we want to change the font displayed in a fontview */
436     void (*change_display_bitmap)(struct fontviewbase *, struct bdffont *);
437 
438    /* We just deleted the active bitmap, so switch to a rasteriztion of the outlines */
439     void (*display_filled)(struct fontviewbase *);
440 
441    /* When we revert a font we need to change the alegence of all outline */
442    /*  glyph windows to the new value of the font */
443     void (*reattach_cvs)(struct splinefont *old, struct splinefont *new);
444 
445    /* deselect any selected glyphs */
446     void (*deselect_all)(struct fontviewbase *);
447 
448    /* Scroll (or whatever) the fontview so that the desired */
449    /*  gid is displayed */
450     void (*display_gid)(struct fontviewbase *,int gid);
451 
452    /* Scroll (or whatever) the fontview so that the desired */
453    /*  encoding is displayed */
454     void (*display_enc)(struct fontviewbase *,int enc);
455 
456    /* Scroll (or whatever) the fontview so that the desired */
457    /*  glyph is displayed */
458     void (*select_gid)(struct fontviewbase *,int gid);
459 
460    /* Close any open glyph instruction windows in the font */
461     int (*close_all_instrs)(struct splinefont *);
462 };
463 extern struct fv_interface *fv_interface;
464 
465 #define FontViewCreate		(fv_interface->create_view)
466 #define _FontViewCreate		(fv_interface->_create_view)
467 #define FontViewClose		(fv_interface->close_view)
468 #define FontViewFree		(fv_interface->free_view)
469 #define FVSetTitle		(fv_interface->set_title)
470 #define FVSetTitles		(fv_interface->set_titles)
471 #define FVRefreshAll		(fv_interface->refresh_all)
472 #define FontViewReformatOne	(fv_interface->reformat_one)
473 #define FontViewReformatAll	(fv_interface->reformat_all)
474 #define FontViewLayerChanged	(fv_interface->layer_changed)
475 #define FVToggleCharChanged	(fv_interface->flag_glyph_changed)
476 #define FVWinInfo		(fv_interface->win_info)
477 #define SFIsActive		(fv_interface->font_is_active)
478 #define FontViewFirst		(fv_interface->first_font)
479 #define FVAppend		(fv_interface->append)
480 #define FontWithThisFilename	(fv_interface->font_of_filename)
481 #define FVAdjustScrollBarRows	(fv_interface->extra_enc_slots)
482 #define FVBiggerGlyphCache	(fv_interface->bigger_glyph_cache)
483 #define FVChangeDisplayBitmap	(fv_interface->change_display_bitmap)
484 #define FVShowFilled		(fv_interface->display_filled)
485 #define FVReattachCVs		(fv_interface->reattach_cvs)
486 #define FVDisplayGID		(fv_interface->display_gid)
487 #define FVDisplayEnc		(fv_interface->display_enc)
488 #define FVChangeGID		(fv_interface->select_gid)
489 #define SFCloseAllInstrs	(fv_interface->close_all_instrs)
490 
491 void FF_SetFVInterface(struct fv_interface *fvi);
492 
493 /* ************************************************************************** */
494 /*                       Clipboard access (copy/paste)                        */
495 /* ************************************************************************** */
496 
497 struct clip_interface {
498    /* Announce we own the clipboard selection */
499     void  (*grab_clip)(void);
500    /* Either place data in the clipboard of a given type, or */
501    /*  provide a routine to call which will give data on demand */
502    /*  (and another routine to clean things up) */
503     void  (*add_data_type)(const char *type, void *data, int cnt, int size,
504 	void *(*gendata)(void *,int32 *len), void (*freedata)(void *));
505    /* Does the clipboard contain something of the given type? */
506     int   (*clip_has_type)(const char *mimetype);
507    /* Ask for the clipboard, and waits (and returns) for the response */
508     void *(*request_clip)(const char *mimetype,int *len);
509 
510 };
511 extern struct clip_interface *clip_interface;
512 
513 #define ClipboardGrab		(clip_interface->grab_clip)
514 #define ClipboardAddDataType	(clip_interface->add_data_type)
515 #define ClipboardRequest	(clip_interface->request_clip)
516 #define ClipboardHasType	(clip_interface->clip_has_type)
517 
518 extern const char *NOUI_TTFNameIds(int id);
519 extern const char *NOUI_MSLangString(int language);
520 
521 #endif /* FONTFORGE_UIINTERFACE_H */
522