1 /*
2  * Copyright 2008 Department of Mathematical Sciences, New Mexico State University
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * DEPARTMENT OF MATHEMATICAL SCIENCES OR NEW MEXICO STATE UNIVERSITY BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #ifndef _h_fontgrid
23 #define _h_fontgrid
24 
25 #include <gdk/gdk.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtksignal.h>
28 #include "bdfP.h"
29 #include "gtkcompat.h"
30 
31 G_BEGIN_DECLS
32 
33 /*
34  * The macros for accessing various parts of the widget class.
35  */
36 #define FONTGRID(o) \
37         (G_TYPE_CHECK_INSTANCE_CAST((o), fontgrid_get_type(), Fontgrid))
38 
39 #define FONTGRID_CLASS(c) \
40         (G_TYPE_CHECK_CLASS_CAST((c), fontgrid_get_type(), FontgridClass))
41 
42 #define IS_FONTGRID(o) G_TYPE_CHECK_INSTANCE_TYPE((o), fontgrid_get_type())
43 
44 #define IS_FONTGRID_CLASS(c) \
45         (G_TYPE_CHECK_CLASS_TYPE((c), fontgrid_get_type()))
46 
47 #define FONTGRID_GET_CLASS(o) \
48         (G_TYPE_INSTANCE_GET_CLASS((o), fontgrid_get_type(), FontgridClass))
49 
50 typedef struct _Fontgrid      Fontgrid;
51 typedef struct _FontgridClass FontgridClass;
52 
53 typedef struct {
54     gint32 minpage;
55     gint32 maxpage;
56     gint32 npage;
57     gint32 ppage;
58 
59     gint32 pageno;
60     gint32 bcode;
61     gint32 sel_start;
62     gint32 sel_end;
63 
64     guint32 selmap[2048];
65 } FontgridInternalPageInfo;
66 
67 struct _Fontgrid {
68     GtkWidget widget;
69 
70     bdf_font_t *font;
71     guint base;
72     gboolean power2;
73     gboolean overwrite;
74     gboolean noblanks;
75     GtkOrientation orientation;
76     guint32 point_size;
77     gint32 spacing;
78     guint16 *colors;
79     gint32 initial_glyph;
80     gint32 bpp;
81     gint32 hres;
82     gint32 vres;
83 
84     guint16 cell_rows;
85     guint16 cell_cols;
86     guint16 border;
87     guint16 hmargin;
88     guint16 vmargin;
89 
90     /*
91      * Private variables.
92      */
93     gboolean init;
94 
95     gboolean resizing;
96     gboolean from_keyboard;
97     gboolean no_sel_callback;
98 
99     guint16 label_height;
100     guint16 cell_width;
101     guint16 cell_height;
102     guint16 pagesize;
103     gint16 xoff;
104     gint16 yoff;
105 
106     gboolean unencoded;
107     gboolean debug;
108 
109     GdkGC *xor_gc;
110 
111     GdkPoint *points;
112     guint32 points_used;
113     guint32 points_size;
114 
115     /*
116      * For creating RGB glyph images.
117      */
118     guchar *rgb;
119     guint32 rgb_used;
120     guint32 rgb_size;
121 
122     /*
123      * Stuff related to the timer between clicks.
124      */
125     guint32 last_click;
126     guint32 mclick_time;
127 
128     /*
129      * The count accumulated from pressing number keys.
130      */
131     guint32 count;
132 
133     /*
134      * The clipboard used to store selections among other things.
135      */
136     bdf_glyphlist_t clipboard;
137 
138     /*
139      * Page information necessary for paging an drawing.
140      */
141     FontgridInternalPageInfo npage;
142     FontgridInternalPageInfo upage;
143 };
144 
145 struct _FontgridClass {
146     GtkWidgetClass parent_class;
147 
148     void (*selection_start)(GtkWidget *, gpointer, gpointer);
149     void (*selection_extend)(GtkWidget *, gpointer, gpointer);
150     void (*selection_end)(GtkWidget *, gpointer, gpointer);
151     void (*page)(GtkWidget *, gpointer, gpointer);
152     void (*activate)(GtkWidget *, gpointer, gpointer);
153     void (*modified)(GtkWidget *, gpointer, gpointer);
154 };
155 
156 /**************************************************************************
157  *
158  * Structures used for the API.
159  *
160  **************************************************************************/
161 
162 typedef struct {
163     gint32 previous_page;
164     gint32 current_page;
165     gint32 next_page;
166     gint32 encoded_glyphs;
167     gint32 unencoded_glyphs;
168     gboolean unencoded_page;
169 } FontgridPageInfo;
170 
171 typedef struct {
172     gchar *name;
173     gchar *comments;
174     gchar *messages;
175     glong bits_per_pixel;
176     glong default_char;
177     guint16 monowidth;
178     guint16 spacing;
179     gulong font_ascent;
180     gulong font_descent;
181     gulong resolution_x;
182     gulong resolution_y;
183     bdf_bbx_t bbx;
184 } FontgridFontInfo;
185 
186 /*
187  * Enum representing the callback reasons.
188  */
189 typedef enum {
190     FONTGRID_START_SELECTION = 0,
191     FONTGRID_EXTEND_SELECTION,
192     FONTGRID_END_SELECTION,
193     FONTGRID_ACTIVATE,
194     FONTGRID_BASE_CHANGE
195 } FontgridSelectionReason;
196 
197 typedef struct {
198     FontgridSelectionReason reason;
199     gint32 start;
200     gint32 end;
201     gint base;
202     bdf_glyph_t *glyphs;
203     guint32 num_glyphs;
204     gboolean unencoded;
205 } FontgridSelectionInfo;
206 
207 typedef enum {
208     FONTGRID_MODIFIED = 0,
209     FONTGRID_GLYPH_NAMES_MODIFIED,
210     FONTGRID_NAME_MODIFIED,
211     FONTGRID_PROPERTIES_MODIFIED,
212     FONTGRID_COMMENTS_MODIFIED,
213     FONTGRID_DEVICE_WIDTH_MODIFIED,
214     FONTGRID_GLYPHS_MODIFIED,
215     FONTGRID_GLYPHS_DELETED,
216     FONTGRID_GLYPHS_PASTED,
217     FONTGRID_FONT_METRICS_MODIFIED,
218     FONTGRID_PSF_MAPPINGS_MODIFIED
219 } FontgridModificationReason;
220 
221 typedef struct {
222     FontgridModificationReason reason;
223     gchar *name;
224     gint32 start;
225     gint32 end;
226     gboolean unencoded;
227 } FontgridModificationInfo;
228 
229 /**************************************************************************
230  *
231  * General API
232  *
233  **************************************************************************/
234 
235 extern GType fontgrid_get_type(void);
236 extern GtkWidget *fontgrid_new(const gchar *prop1, ...);
237 extern GtkWidget *fontgrid_newv(bdf_font_t *font, guint32 pointSize,
238                                 gint32 spacing, gboolean skipBlankPages,
239                                 gboolean overwriteMode, gboolean powersOfTwo,
240                                 guint16 *colorList,
241                                 gint32 initialGlyph, guint codeBase,
242                                 GtkOrientation orientation,
243                                 gint32 bitsPerPixel,
244                                 gint32 horizontalResolution,
245                                 gint32 verticalResolution,
246                                 FontgridPageInfo *initialPageInfo);
247 
248 /*
249  * A routine to force initialization before the widget is realized.  This is
250  * needed to get some fields filled in for apps using the widget.
251  */
252 extern void fontgrid_force_init(Fontgrid *);
253 
254 /*
255  * Selection information.
256  */
257 extern gboolean fontgrid_clipboard_empty(Fontgrid *);
258 extern gboolean fontgrid_has_selection(Fontgrid *, FontgridSelectionInfo *);
259 
260 /*
261  * Getting and setting widget values.
262  */
263 extern bdf_font_t *fontgrid_get_font(Fontgrid *);
264 extern void fontgrid_set_font(Fontgrid *, bdf_font_t *, gint32);
265 
266 extern gchar *fontgrid_get_font_messages(Fontgrid *);
267 
268 extern GtkOrientation fontgrid_get_orientation(Fontgrid *);
269 extern void fontgrid_set_orientation(Fontgrid *, GtkOrientation);
270 
271 extern gboolean fontgrid_viewing_unencoded(Fontgrid *);
272 extern void fontgrid_switch_encoding_view(Fontgrid *);
273 
274 extern guint fontgrid_get_code_base(Fontgrid *);
275 extern void fontgrid_set_code_base(Fontgrid *, guint);
276 
277 extern gchar *fontgrid_get_font_name(Fontgrid *);
278 extern void fontgrid_set_font_name(Fontgrid *, gchar *);
279 
280 extern gboolean fontgrid_get_font_modified(Fontgrid *);
281 extern void fontgrid_set_font_modified(Fontgrid *, gboolean);
282 
283 extern void fontgrid_set_unicode_glyph_names(Fontgrid *, FILE *);
284 extern void fontgrid_set_adobe_glyph_names(Fontgrid *, FILE *);
285 extern void fontgrid_set_code_glyph_names(Fontgrid *, gint);
286 
287 extern void fontgrid_set_font_property(Fontgrid *, bdf_property_t *);
288 extern void fontgrid_delete_font_property(Fontgrid *, gchar *);
289 
290 extern guint32 fontgrid_get_font_comments(Fontgrid *, gchar **);
291 extern void fontgrid_set_font_comments(Fontgrid *, gchar *);
292 
293 extern gint fontgrid_get_font_spacing(Fontgrid *);
294 extern void fontgrid_set_font_spacing(Fontgrid *, gint);
295 
296 extern guint16 fontgrid_get_font_device_width(Fontgrid *);
297 extern void fontgrid_set_font_device_width(Fontgrid *, guint16);
298 
299 extern void fontgrid_get_font_info(Fontgrid *, FontgridFontInfo *);
300 extern void fontgrid_set_font_info(Fontgrid *, FontgridFontInfo *);
301 
302 /*
303  * Navigation and page information.
304  */
305 extern void fontgrid_goto_page(Fontgrid *fw, gint32 pageno);
306 extern void fontgrid_goto_code(Fontgrid *, gint32 pageno);
307 extern void fontgrid_goto_first_page(Fontgrid *fw);
308 extern void fontgrid_goto_last_page(Fontgrid *fw);
309 extern void fontgrid_goto_next_page(Fontgrid *fw);
310 extern void fontgrid_goto_previous_page(Fontgrid *fw);
311 extern void fontgrid_get_page_info(Fontgrid *fw, FontgridPageInfo *pageinfo);
312 extern gboolean fontgrid_select_next_glyph(Fontgrid *fw, gint32 code);
313 extern gboolean fontgrid_select_previous_glyph(Fontgrid *fw, gint32 code);
314 
315 /*
316  * Font name functions.
317  */
318 extern void fontgrid_make_xlfd_font_name(Fontgrid *);
319 extern void fontgrid_update_font_name_from_properties(Fontgrid *);
320 extern void fontgrid_update_properties_from_font_name(Fontgrid *);
321 
322 /*
323  * Graphical operations.
324  */
325 extern void fontgrid_translate_glyphs(Fontgrid *fw, gint16 dx, gint16 dy,
326                                       gboolean all_glyphs);
327 extern void fontgrid_rotate_glyphs(Fontgrid *fw, gint16 degrees,
328                                    gboolean all_glyphs);
329 extern void fontgrid_shear_glyphs(Fontgrid *fw, gint16 degrees,
330                                   gboolean all_glyphs);
331 extern void fontgrid_embolden_glyphs(Fontgrid *fw, gboolean all_glyphs);
332 
333 /*
334  * Clipboard operations.  MERGE and OVERLAY are the same operation.
335  */
336 typedef enum {
337     FONTGRID_NORMAL_PASTE = 0,
338     FONTGRID_INSERT_PASTE,
339     FONTGRID_MERGE_PASTE,
340     FONTGRID_OVERLAY_PASTE
341 } FontgridPasteType;
342 
343 extern void fontgrid_copy_selection(Fontgrid *fw);
344 extern void fontgrid_cut_selection(Fontgrid *fw);
345 extern void fontgrid_paste_selection(Fontgrid *fw,
346                                      FontgridPasteType paste_type);
347 
348 /*
349  * Metrics, glyph, and PSF mappings updates.
350  */
351 extern void fontgrid_update_metrics(Fontgrid *fw, bdf_metrics_t *metrics);
352 extern void fontgrid_update_glyph(Fontgrid *fw, bdf_glyph_t *glyph,
353                                   gboolean unencoded);
354 
355 extern void fontgrid_update_psf_mappings(Fontgrid *fw, gint32 encoding,
356                                          bdf_psf_unimap_t *mappings);
357 G_END_DECLS
358 
359 #endif /* _h_fontgrid */
360