-
Notifications
You must be signed in to change notification settings - Fork 3
/
fontgrid.c
4794 lines (4097 loc) · 138 KB
/
fontgrid.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2008 Department of Mathematical Sciences, New Mexico State University
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* DEPARTMENT OF MATHEMATICAL SCIENCES OR NEW MEXICO STATE UNIVERSITY BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "fontgrid.h"
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms-compat.h>
#ifdef HAVE_XLIB
#include <gdk/gdkx.h>
#endif
#ifdef ENABLE_NLS
#include <libintl.h>
#define _(s) dgettext(GETTEXT_PACKAGE,s)
#else
#define _(s) (s)
#endif
/*
* Macros that represent the properties used by this type of object.
*/
#define FONTGRID_CLIPBOARD gdk_atom_intern("FONTGRID_CLIPBOARD", FALSE)
#define FONTGRID_GLYPHLIST gdk_atom_intern("FONTGRID_GLYPHLIST", FALSE)
/*
* Set several defaults.
*/
#define FGRID_MAX_COLS 16
#define FGRID_MAX_ROWS 16
#define FGRID_DEFAULT_COLS 16
#define FGRID_DEFAULT_ROWS 8
#define FGRID_DEFAULT_CELL_HEIGHT 18
/*
* Enums used for identifying properties.
*/
enum {
PROP_0 = 0,
PROP_CODE_BASE,
PROP_POWER2,
PROP_ORIENTATION,
PROP_FONT,
PROP_POINT_SIZE,
PROP_SPACING,
PROP_SKIP_BLANKS,
PROP_OVERWRITE,
PROP_COLORS,
PROP_INITIAL_GLYPH,
PROP_BPP,
PROP_HRES,
PROP_VRES
};
/**************************************************************************
*
* Selection macros for toggling & testing glyph selected state.
*
**************************************************************************/
/*
* Macros for dealing with the selected state of glyphs in the font grid.
*/
#define IsSelected(code, map) (map[(code) >> 5] & (1 << ((code) & 31)))
#define Select(code, map) (map[(code) >> 5] |= (1 << ((code) & 31)))
#define Unselect(code, map) (map[(code) >> 5] &= ~(1 << ((code) & 31)))
/**************************************************************************
*
* Signals.
*
**************************************************************************/
/*
* Enums that represent the signals these objects send out.
*/
enum {
SELECTION_START = 0,
SELECTION_EXTEND,
SELECTION_END,
ACTIVATE,
MODIFIED,
TURN_TO_PAGE
};
static GtkWidgetClass *parent_class = 0;
static guint fontgrid_signals[TURN_TO_PAGE + 1];
static bdf_glyph_t empty_glyph;
/*
* Used to determine spacing between digits when displaying.
*/
#define FONTGRID_DIGIT_WIDTH 6
#define FONTGRID_DIGIT_HEIGHT 10
/*
* A macro for getting the current foreground GC.
*/
#define WIDGET_FG_GC(w) ((w)->style->fg_gc[gtk_widget_get_state(w)])
#define HMARGINS(fw) ((fw)->hmargin << 1)
#define VMARGINS(fw) ((fw)->vmargin << 1)
static void
fontgrid_set_cell_geometry(Fontgrid *fw)
{
bdf_font_t *font;
gint lw;
font = fw->font;
lw = FONTGRID_DIGIT_WIDTH * 7;
/*
* The labels will always be numbers in base 8, 10, or 16, so we are only
* interested in the max ascent. Add a 2-pixel margin on top and bottom.
*/
fw->label_height = FONTGRID_DIGIT_HEIGHT + 4;
/*
* We want a minumum padding of 3 pixels on each side of the glyph bitmap
* in each cell. Thus the addition of 6 to each dimension.
*/
if (font != 0) {
fw->cell_width = font->bbx.width + 6;
fw->cell_height = font->bbx.height + 6;
} else {
/*
* Hard-code a minimum size for NULL fonts. The initial height of
* an empty cell is 20 to give it a better visual appearance.
*/
fw->cell_width = lw + 6;
fw->cell_height = FGRID_DEFAULT_CELL_HEIGHT + 6;
}
fw->cell_width = MAX(fw->cell_width, lw);
fw->cell_height = MAX(fw->cell_height, fw->label_height);
/*
* Now add the label size into the picture.
*/
fw->cell_height += fw->label_height - 1;
}
static void
fontgrid_set_rows_cols(Fontgrid *fw, GtkAllocation *core)
{
gint i;
guint16 dw, dh, wd, ht;
/*
* Limit the window size to 7/8 of the actual screen dimensions.
*/
dw = (gdk_screen_width() * 7) >> 3;
dh = (gdk_screen_height() * 7) >> 3;
if (!core) {
/*
* Adjust the rows and columns based on the preferred geometry.
*/
wd = (fw->cell_width * fw->cell_cols) + HMARGINS(fw);
ht = (fw->cell_height * fw->cell_rows) + VMARGINS(fw);
if (wd > dw)
fw->cell_cols = (dw - HMARGINS(fw)) / fw->cell_width;
if (ht > dh)
fw->cell_rows = (dh - VMARGINS(fw)) / fw->cell_height;
} else {
/*
* Adjust the rows and columns based on the current geometry.
*/
fw->cell_cols = (core->width - HMARGINS(fw)) / fw->cell_width;
fw->cell_rows = (core->height - VMARGINS(fw)) / fw->cell_height;
}
/*
* Adjust rows and columns to powers of two if necessary.
*/
if (fw->power2) {
/*
* Make sure the columns are a power of 2.
*/
for (i = 15; i >= 0; i--) {
if (fw->cell_cols & (1 << i)) {
fw->cell_cols = 1 << i;
break;
}
}
/*
* Make sure the rows are a power of 2.
*/
for (i = 15; i >= 0; i--) {
if (fw->cell_rows & (1 << i)) {
fw->cell_rows = 1 << i;
break;
}
}
}
/*
* Fall back to a minimum of two rows.
*/
if (fw->cell_rows == 0)
fw->cell_rows = 2;
/*
* Fall back to a minimum of two columns.
*/
if (fw->cell_cols == 0)
fw->cell_cols = 2;
/*
* Make sure the number of rows and cols are within the max limits.
*/
if (fw->cell_cols > FGRID_MAX_COLS)
fw->cell_cols = FGRID_MAX_COLS;
if (fw->cell_rows > FGRID_MAX_ROWS)
fw->cell_rows = FGRID_MAX_ROWS;
/*
* Set the new page size based on the calculated rows and columns.
*/
fw->pagesize = fw->cell_rows * fw->cell_cols;
}
/**************************************************************************
*
* GObjectClass functions.
*
**************************************************************************/
static void
fontgrid_set_property(GObject *obj, guint prop_id, const GValue *value,
GParamSpec *pspec)
{
Fontgrid *fw;
fw = FONTGRID(obj);
switch (prop_id) {
case PROP_CODE_BASE:
fw->base = g_value_get_uint(value);
/*
* Force the encodings to be redisplayed here?
*/
break;
case PROP_POWER2:
fw->power2 = g_value_get_boolean(value);
break;
case PROP_ORIENTATION:
fontgrid_set_orientation(fw, g_value_get_enum(value));
break;
case PROP_FONT:
/*
* Need to set the rows and columns back to their defaults when
* a new font is passed in case it is NULL.
*/
fw->font = (bdf_font_t *) g_value_get_pointer(value);
fontgrid_set_cell_geometry(fw);
fontgrid_set_rows_cols(fw, 0);
break;
case PROP_POINT_SIZE:
fw->point_size = g_value_get_uint(value);
break;
case PROP_SPACING:
fw->spacing = g_value_get_int(value);
break;
case PROP_SKIP_BLANKS:
fw->noblanks = g_value_get_boolean(value);
break;
case PROP_OVERWRITE:
fw->overwrite = g_value_get_boolean(value);
break;
case PROP_COLORS:
fw->colors = (guint16 *) g_value_get_pointer(value);
break;
case PROP_INITIAL_GLYPH:
fw->initial_glyph = g_value_get_int(value);
break;
case PROP_BPP:
fw->bpp = g_value_get_int(value);
break;
case PROP_HRES:
fw->hres = g_value_get_int(value);
break;
case PROP_VRES:
fw->vres = g_value_get_int(value);
break;
}
}
static void
fontgrid_get_property(GObject *obj, guint prop_id, GValue *value,
GParamSpec *pspec)
{
Fontgrid *f;
f = FONTGRID(obj);
switch (prop_id) {
case PROP_CODE_BASE:
g_value_set_uint(value, f->base);
break;
case PROP_POWER2:
g_value_set_boolean(value, f->power2);
break;
case PROP_ORIENTATION:
g_value_set_enum(value, f->orientation);
break;
case PROP_FONT:
g_value_set_pointer(value, f->font);
break;
case PROP_POINT_SIZE:
g_value_set_uint(value, f->point_size);
break;
case PROP_SPACING:
g_value_set_int(value, f->spacing);
break;
case PROP_SKIP_BLANKS:
g_value_set_boolean(value, f->noblanks);
break;
case PROP_COLORS:
g_value_set_pointer(value, f->colors);
break;
case PROP_INITIAL_GLYPH:
g_value_set_int(value, f->initial_glyph);
break;
case PROP_BPP:
g_value_set_int(value, f->bpp);
break;
case PROP_HRES:
g_value_set_int(value, f->hres);
break;
case PROP_VRES:
g_value_set_int(value, f->vres);
break;
}
}
/**************************************************************************
*
* GtkObjectClass functions.
*
**************************************************************************/
static void
#if GTK_CHECK_VERSION(3, 0, 0)
fontgrid_destroy(GtkWidget *obj)
#else
fontgrid_destroy(GtkObject *obj)
#endif
{
Fontgrid *f;
guint32 i;
bdf_glyphlist_t *gl;
/*
* Do some checks to make sure the incoming object exists and is the right
* kind.
*/
g_return_if_fail(obj != 0);
g_return_if_fail(IS_FONTGRID(obj));
f = FONTGRID(obj);
/*
* Clean up this object instance.
*/
if (f->font)
bdf_free_font(f->font);
f->font = 0;
#if !GTK_CHECK_VERSION(3, 0, 0)
if (f->xor_gc != 0)
g_object_unref(G_OBJECT(f->xor_gc));
f->xor_gc = 0;
#endif
if (f->points_size > 0)
g_free(f->points);
f->points_size = f->points_used = 0;
if (f->rgb_size > 0)
g_free(f->rgb);
f->rgb_used = f->rgb_size = 0;
/*
* Remove all ownership of selections.
*/
gtk_selection_remove_all(GTK_WIDGET(obj));
/*
* Free up the clipboard contents if there are any.
*/
gl = &f->clipboard;
for (i = 0; i < gl->glyphs_used; i++) {
if (gl->glyphs[i].name)
free(gl->glyphs[i].name);
if (gl->glyphs[i].bytes > 0)
free((char *) gl->glyphs[i].bitmap);
}
if (gl->glyphs_size > 0)
free((char *) gl->glyphs);
gl->glyphs_size = gl->glyphs_used = 0;
/*
* Follow the class chain back up to free up resources allocated in the
* parent classes.
*/
#if GTK_CHECK_VERSION(3, 0, 0)
GTK_WIDGET_CLASS(parent_class)->destroy(obj);
#else
GTK_OBJECT_CLASS(parent_class)->destroy(obj);
#endif
}
static void
fontgrid_finalize(GObject *obj)
{
/*
* Do some checks to make sure the incoming object exists and is the right
* kind.
*/
g_return_if_fail(obj != 0);
g_return_if_fail(IS_FONTGRID(obj));
/*
* Follow the class chain back up to free up resources allocated in the
* parent classes.
*/
G_OBJECT_CLASS(parent_class)->finalize(obj);
}
/**************************************************************************
*
* GtkWidgetClass functions.
*
**************************************************************************/
static void
fontgrid_preferred_size(GtkWidget *widget, GtkRequisition *preferred)
{
Fontgrid *fw;
fw = FONTGRID(widget);
preferred->width = (fw->cell_width * fw->cell_cols) + HMARGINS(fw);
preferred->height = (fw->cell_height * fw->cell_rows) + VMARGINS(fw);
}
#if GTK_CHECK_VERSION(3, 0, 0)
static void
fontgrid_get_preferred_width(GtkWidget *widget, gint *minimal_width, gint *natural_width)
{
GtkRequisition requisition;
fontgrid_preferred_size(widget, &requisition);
*minimal_width = *natural_width = requisition.width;
}
static void
fontgrid_get_preferred_height(GtkWidget *widget, gint *minimal_height, gint *natural_height)
{
GtkRequisition requisition;
fontgrid_preferred_size(widget, &requisition);
*minimal_height = *natural_height = requisition.height;
}
#endif
static void
fontgrid_actual_size(GtkWidget *widget, GtkAllocation *actual)
{
Fontgrid *fw;
fw = FONTGRID(widget);
gtk_widget_set_allocation(widget, actual);
/*
* Make sure the rows and columns are adjusted to fit the actual allocated
* size.
*/
fontgrid_set_rows_cols(fw, actual);
if (gtk_widget_get_realized(widget))
gdk_window_move_resize(gtk_widget_get_window(widget), actual->x, actual->y,
actual->width, actual->height);
}
static void
fontgrid_realize(GtkWidget *widget)
{
Fontgrid *fw;
GdkWindowAttr attributes;
#if !GTK_CHECK_VERSION(3, 0, 0)
GdkGCValues values;
#endif
gint attributes_mask;
GtkAllocation all;
g_return_if_fail(widget != NULL);
g_return_if_fail(IS_FONTGRID(widget));
fw = FONTGRID(widget);
gtk_widget_set_realized(widget, TRUE);
attributes.window_type = GDK_WINDOW_CHILD;
gtk_widget_get_allocation(widget, &all);
attributes.x = all.x;
attributes.y = all.y;
attributes.width = all.width;
attributes.height = all.height;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.visual = gtk_widget_get_visual(widget);
#if !GTK_CHECK_VERSION(3, 0, 0)
attributes.colormap = gtk_widget_get_colormap(widget);
#endif
attributes.event_mask = gtk_widget_get_events(widget);
attributes.event_mask |= (GDK_EXPOSURE_MASK|GDK_BUTTON_PRESS_MASK|
GDK_BUTTON_RELEASE_MASK|GDK_ENTER_NOTIFY_MASK|
GDK_POINTER_MOTION_MASK|
GDK_KEY_PRESS_MASK|GDK_KEY_RELEASE_MASK|
GDK_LEAVE_NOTIFY_MASK|GDK_FOCUS_CHANGE_MASK|
GDK_PROPERTY_CHANGE_MASK);
attributes_mask = GDK_WA_X|GDK_WA_Y|GDK_WA_VISUAL
#if !GTK_CHECK_VERSION(3, 0, 0)
|GDK_WA_COLORMAP
#endif
;
gtk_widget_set_window(widget, gdk_window_new(gtk_widget_get_parent_window(widget),
&attributes, attributes_mask));
gdk_window_set_user_data(gtk_widget_get_window(widget), widget);
gtk_widget_set_style(widget, gtk_style_attach(gtk_widget_get_style(widget), gtk_widget_get_window(widget)));
gtk_style_set_background(gtk_widget_get_style(widget), gtk_widget_get_window(widget), GTK_STATE_NORMAL);
#if !GTK_CHECK_VERSION(3, 0, 0)
if (fw->xor_gc != 0)
g_object_unref(G_OBJECT(fw->xor_gc));
/*
* Create the GC used to display selected cells.
*/
values.foreground.pixel =
gtk_widget_get_style(widget)->fg[gtk_widget_get_state(widget)].pixel ^
gtk_widget_get_style(widget)->bg[gtk_widget_get_state(widget)].pixel;
(void) memset((char *) &values.background, 0, sizeof(GdkColor));
values.function = GDK_XOR;
fw->xor_gc = gdk_gc_new_with_values(gtk_widget_get_window(widget), &values,
GDK_GC_FOREGROUND|
GDK_GC_BACKGROUND|GDK_GC_FUNCTION);
#endif
}
static bdf_glyph_t *
fontgrid_locate_glyph(bdf_glyph_t *glyphs, guint32 nglyphs, gint32 code,
gboolean exact_match)
{
gint32 l, r, m = 0;
if (code < 0 || glyphs == 0 || nglyphs == 0)
return 0;
for (l = 0, r = (gint32) (nglyphs - 1); l <= r; ) {
m = (l + r) >> 1;
if (glyphs[m].encoding < code)
l = m + 1;
else if (glyphs[m].encoding > code)
r = m - 1;
else {
if (exact_match)
return glyphs + m;
break;
}
}
if (exact_match)
return 0;
/*
* Adjust to the beginning or end if nothing was found in the search.
*/
while (m > 0 && glyphs[m].encoding > code)
m--;
while (m < (gint32) nglyphs && glyphs[m].encoding < code)
m++;
return (m < (gint32) nglyphs) ? glyphs + m : 0;
}
static void
fontgrid_get_glyph_points(Fontgrid *fw, gint x, gint y, gint rx, gint by,
bdf_glyph_t *glyph)
{
gint i, j, bpr, col;
unsigned char *bmap, *masks = 0;
switch (fw->bpp) {
case 1: masks = bdf_onebpp; break;
case 2: masks = bdf_twobpp; break;
case 4: masks = bdf_fourbpp; break;
case 8: masks = bdf_eightbpp; break;
}
fw->points_used = 0;
bmap = glyph->bitmap;
bpr = ((glyph->bbx.width * fw->bpp) + 7) >> 3;
for (i = 0; i + y - glyph->bbx.ascent < by && i < glyph->bbx.height; i++) {
for (col = j = 0; j + x < rx && j < glyph->bbx.width;
j++, col += fw->bpp) {
if (bmap[(i * bpr) + (col >> 3)] &
masks[(col & 7) / fw->bpp]) {
if (fw->points_used == fw->points_size) {
if (fw->points_size == 0)
fw->points =
(GdkPoint *) g_malloc(sizeof(GdkPoint) << 7);
else
fw->points =
(GdkPoint *) g_realloc(fw->points,
sizeof(GdkPoint) *
(fw->points_size + 128));
fw->points_size += 128;
}
fw->points[fw->points_used].x = j + x;
fw->points[fw->points_used].y = i + y - glyph->bbx.ascent;
fw->points_used++;
}
}
}
}
#if 0
static void
fontgrid_get_glyph_points_color(Fontgrid *fw, gint x, gint y, gint rx, gint by,
gint color_index, bdf_glyph_t *glyph)
{
gint i, j, bpr, col, byte, di = 0, si, cidx = 0;
unsigned char *bmap, *masks = 0;
switch (fw->bpp) {
case 1: masks = bdf_onebpp; di = 7; break;
case 2: masks = bdf_twobpp; di = 3; break;
case 4: masks = bdf_fourbpp; di = 1; break;
case 8: masks = bdf_eightbpp; di = 0; break;
}
fw->points_used = 0;
bmap = glyph->bitmap;
bpr = ((glyph->bbx.width * fw->bpp) + 7) >> 3;
for (i = 0; i + y - glyph->bbx.ascent < by && i < glyph->bbx.height; i++) {
for (col = j = 0; j + x < rx && j < glyph->bbx.width;
j++, col += fw->bpp) {
si = (col & 7) / fw->bpp;
byte = bmap[(i * bpr) + (col >> 3)] & masks[si];
if (byte) {
/*
* Check to see if the byte matches the color index being
* collected.
*/
if (di > si)
byte >>= (di - si) * fw->bpp;
if (byte == cidx) {
if (fw->points_used == fw->points_size) {
if (fw->points_size == 0)
fw->points = (GdkPoint *)
g_malloc(sizeof(GdkPoint) << 6);
else
fw->points = (GdkPoint *)
g_realloc(fw->points, sizeof(GdkPoint) *
(fw->points_size + 64));
fw->points_size += 64;
}
fw->points[fw->points_used].x = j + x;
fw->points[fw->points_used].y = i + y - glyph->bbx.ascent;
fw->points_used++;
}
}
}
}
}
#endif
/*
* This routine creates a 24 bits per pixel image of a glyph so it can be
* drawn using GtkRGB. This is less complicated than the old method of
* collecting and drawing individual pixels of each different color.
*/
static void
fontgrid_make_rgb_image(Fontgrid *fw, bdf_glyph_t *glyph)
{
GtkWidget *w = GTK_WIDGET(fw);
gint x, y, bpr, rgb_bpr, col, byte, di = 0, si;
guchar bg[4], pix[4], *bmap, *masks = 0;
/*
* Figure out the background color.
*/
bg[0] = (guchar) gtk_widget_get_style(w)->bg[gtk_widget_get_state(w)].red;
bg[1] = (guchar) gtk_widget_get_style(w)->bg[gtk_widget_get_state(w)].green;
bg[2] = (guchar) gtk_widget_get_style(w)->bg[gtk_widget_get_state(w)].blue;
switch (fw->bpp) {
case 1: masks = bdf_onebpp; di = 7; break;
case 2: masks = bdf_twobpp; di = 3; break;
case 4: masks = bdf_fourbpp; di = 1; break;
case 8: masks = bdf_eightbpp; di = 0; break;
}
bmap = glyph->bitmap;
bpr = ((glyph->bbx.width * fw->bpp) + 7) >> 3;
rgb_bpr = glyph->bbx.width * 3;
fw->rgb_used = rgb_bpr * glyph->bbx.height;
if (fw->rgb_size < fw->rgb_used) {
if (fw->rgb_size == 0)
fw->rgb = g_malloc(fw->rgb_used);
else
fw->rgb = g_realloc(fw->rgb, fw->rgb_used);
fw->rgb_size = fw->rgb_used;
}
for (y = 0; y < glyph->bbx.height; y++) {
for (col = x = 0; x < glyph->bbx.width; x++, col += fw->bpp) {
si = (col & 7) / fw->bpp;
byte = bmap[(y * bpr) + (col >> 3)] & masks[si];
if (di > si)
byte >>= (di - si) * fw->bpp;
if (byte) {
/*
* Look up the color.
*/
switch (fw->bpp) {
case 1: memset(pix, 0, 3); break;
case 2: memset(pix, fw->colors[byte-1], 3); break;
case 4: memset(pix, fw->colors[byte-1+4], 3); break;
case 8: memset(pix, byte, 3); break;
}
} else
/*
* Set the pixel to the background color.
*/
memcpy(pix, bg, 3);
memcpy(&fw->rgb[(y * rgb_bpr) + (x * 3)], pix, 3);
}
}
}
#if !GTK_CHECK_VERSION(3, 0, 0)
static void
fontgrid_draw_encoding(GtkWidget *w, gint x, gint y, gchar *num,
gint numlen)
{
gint i, j, d;
Fontgrid *fw = FONTGRID(w);
GdkPoint *dp;
if (!gtk_widget_get_realized(w))
return;
cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(w));
double font_size = fw->label_height - 2;
cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, font_size);
cairo_text_extents_t te;
cairo_text_extents(cr, "M", &te);
cairo_set_font_size(cr, font_size * te.width / te.height);
cairo_text_extents(cr, num, &te);
cairo_move_to(cr, (double)x - te.width / 2, (double)y + te.height / 2 - 0.5);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_show_text (cr, num);
cairo_destroy(cr);
}
static void
fontgrid_draw_cells(GtkWidget *widget, gint32 start, gint32 end,
gboolean labels, gboolean glyphs)
{
Fontgrid *fw;
gint x, y, wd, as, ds, len, lx, ly;
gint32 i, n, r, c;
guint32 nglyphs, ng;
gboolean mod;
bdf_font_t *font;
bdf_glyph_t *glyph, *gp;
FontgridInternalPageInfo *pi;
GdkGC *gc;
GdkRectangle rect;
gchar nbuf[16];
if (!gtk_widget_get_realized(widget) || (labels == FALSE && glyphs == FALSE))
return;
fw = FONTGRID(widget);
font = fw->font;
glyph = 0;
nglyphs = 0;
if (!fw->unencoded) {
pi = &fw->npage;
if (font) {
glyph = font->glyphs;
nglyphs = font->glyphs_used;
}
} else {
/*
* When viewing the unencoded glyph pages, all glyphs are labelled
* with an encoding of -1.
*/
strcpy(nbuf, "-1");
pi = &fw->upage;
if (font) {
glyph = font->unencoded;
nglyphs = font->unencoded_used;
}
}
/*
* The initial code to work from.
*/
n = pi->bcode;
/*
* Locate the glyph closest to the starting code.
*/
if ((glyph = fontgrid_locate_glyph(glyph, nglyphs, start, FALSE)) == 0)
nglyphs = 0;
gp = glyph;
gc = gtk_widget_get_style(widget)->fg_gc[gtk_widget_get_state(widget)];
for (ng = 0, i = start; i <= end; i++) {
/*
* Only draw those cells that are on the current page.
*/
if (i < pi->bcode || i >= pi->bcode + fw->pagesize)
continue;
if (fw->orientation == GTK_ORIENTATION_HORIZONTAL) {
r = (i - n) / fw->cell_cols;
c = (i - n) % fw->cell_cols;
} else {
c = (i - n) / fw->cell_rows;
r = (i - n) % fw->cell_rows;
}
x = fw->xoff + (c * fw->cell_width);
y = fw->yoff + (r * fw->cell_height);
if (labels) {
if (!fw->unencoded) {
switch (fw->base) {
case 8: sprintf(nbuf, "%o", i); break;
case 10: sprintf(nbuf, "%d", i); break;
case 16: sprintf(nbuf, "%X", i); break;
}
}
rect.x = x + 1;
rect.y = y + 1;
rect.width = fw->cell_width - 2;
rect.height = fw->label_height - 2;
gdk_draw_rectangle(gtk_widget_get_window(widget), gc, FALSE,
rect.x, rect.y, rect.width, rect.height);
len = strlen(nbuf);
wd = len * 6;
as = 8;
ds = 0;
lx = x + (fw->cell_width / 2) + 1;
ly = y + as;
mod = FALSE;
if (i <= 0xffff)
mod = (!fw->unencoded) ? bdf_glyph_modified(font, i, 0) :
bdf_glyph_modified(font, i, 1);
gdk_window_clear_area(gtk_widget_get_window(widget), rect.x + 1, rect.y + 1,
rect.width - 1, rect.height - 1);
if (!fw->unencoded && mod) {
gdk_draw_rectangle(gtk_widget_get_window(widget), gc, TRUE,
rect.x + 2, rect.y + 2,
rect.width - 3, rect.height - 3);
fontgrid_draw_encoding(widget, lx, ly, nbuf, len);
if (gp && gp->encoding == i) {
ng++;
gp++;
if (ng == nglyphs)
gp = 0;
}
} else {
/*
* If the glyph exists, then darken the rectangle to indicate
* this.
*/
if (gp && gp->encoding == i) {
gdk_draw_rectangle(gtk_widget_get_window(widget), gc, FALSE,
rect.x + 1, rect.y + 1,
rect.width - 2, rect.height - 2);
ng++;
gp++;
if (ng == nglyphs)
gp = 0;
}
fontgrid_draw_encoding(widget, lx, ly, nbuf, len);
}
}
if (glyphs) {
rect.x = x + 1;
rect.y = y + fw->label_height + 1;
rect.width = fw->cell_width - 2;
rect.height = (fw->cell_height - fw->label_height) - 2;
if (i <= 0xffff && nglyphs > 0 && glyph->encoding == i) {
/*
* Draw the glyph.
*/
/*
* Set the right and left limits for generating points.
*/
lx = x + fw->cell_width - 2;
ly = y + fw->cell_height - 2;
/*
* Adjust the X,Y coordinate pair so the bitmap points will
* be generated to center the glyphs horizontally and align
* them to the BDF font's baseline vertically.
*/
x += (fw->cell_width >> 1) -
((font->bbx.width + font->bbx.x_offset) >> 1) + 1;
y += fw->label_height + font->bbx.ascent + 3;
if (IsSelected(glyph->encoding, pi->selmap)) {
gdk_draw_rectangle(gtk_widget_get_window(widget), gc, TRUE,
rect.x + 1, rect.y + 1,
rect.width - 1, rect.height - 1);
if (glyph->bytes > 0) {
fontgrid_get_glyph_points(fw, x, y, lx, ly, glyph);
if (fw->points_used > 0)
gdk_draw_points(gtk_widget_get_window(widget), fw->xor_gc,
fw->points, fw->points_used);
}
} else {
/*
* The glyph is not selected, so draw it according to
* the bytes-per-pixel of the font.