6 #include <gdk-pixbuf/gdk-pixbuf.h>
10 #define FONT_PT_SIZE 60
13 delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
19 destroy (GtkWidget *widget, gpointer data)
25 main (int argc, char **argv)
38 char *family_names = NULL;
42 fprintf (stderr, "Usage: gdkdraw LANGUAGE\n");
47 lang = msymbol (argv[1]);
48 mt = mlanguage_text (lang);
51 fprintf (stderr, "No native name for the language \"%s\".\n", argv[1]);
55 nchars = mtext_len (mt);
57 /* Encode native language name in MT into the buffer TITLE. */
59 utf8 = msymbol ("utf-8");
60 allocated = nchars * 4; /* Usually this is enough. */
61 title = malloc (allocated);
62 title_len = mconv_encode_buffer (utf8, mt, title, allocated);
63 while (title_len < 0 && merror_code == MCONVERSION_RESULT_INSUFFICIENT_DST)
65 allocated += allocated;
66 title = realloc (title, allocated);
67 title_len = mconv_encode_buffer (utf8, mt, title, allocated);
71 fprintf (stderr, "Encoding of native language name failed.");
74 title[title_len] = '\0';
77 MPlist *param = mplist ();
78 MFace *face = mface ();
79 MFontset *fontset = mfontset ("generic");
81 mface_put_prop (face, Msize, (void *) (FONT_PT_SIZE * -10));
82 mface_put_prop (face, Mfontset, fontset);
84 mface_put_prop (face, Mfamily, msymbol (argv[2]));
85 mplist_add (param, Mdevice, msymbol ("gd"));
86 mplist_add (param, Mface, face);
87 frame = mframe (param);
88 m17n_object_unref (fontset);
89 m17n_object_unref (face);
90 m17n_object_unref (param);
94 fprintf (stderr, "Frame creation failed. Perhaps no GD library.\n");
99 /* Build GD image and draw MT on it while listing up font families
102 MDrawControl control;
104 int array_size, num_glyphs;
108 memset (&control, 0, sizeof (control));
109 control.anti_alias = 1;
110 control.enable_bidi = 1;
111 mdraw_text_extents (frame, mt, 0, mtext_len (mt), &control,
112 &metric, NULL, NULL);
114 image = gdImageCreateTrueColor (metric.width, metric.height);
115 bg_color = gdImageColorAllocate (image, 255, 255, 255);
116 gdImageFilledRectangle (image, 0, 0, metric.width - 1, metric.height - 1,
119 array_size = nchars * 2; /* Usually this is enough. */
120 glyphs = malloc (sizeof (MDrawGlyph) * array_size);
121 if (mdraw_glyph_list (frame, mt, 0, nchars, &control, glyphs, array_size,
124 array_size = num_glyphs;
125 glyphs = realloc (glyphs, sizeof (MDrawGlyph) * array_size);
126 mdraw_glyph_list (frame, mt, 0, nchars, &control, glyphs, array_size,
132 while (num_glyphs-- > 0)
133 if (glyphs[num_glyphs].font)
135 MSymbol family = mfont_get_prop (glyphs[num_glyphs].font, Mfamily);
139 family_list = mplist ();
140 mplist_add (family_list, Msymbol, family);
141 family_name_len = strlen (msymbol_name (family));
143 else if (! mplist_find_by_value (family_list, family))
145 mplist_push (family_list, Msymbol, family);
146 family_name_len += strlen (msymbol_name (family)) + 1;
150 mdraw_text_with_control (frame, (MDrawWindow) image,
151 - metric.x, - metric.y,
152 mt, 0, mtext_len (mt), &control);
153 m17n_object_unref (frame);
157 char *p = alloca (family_name_len + 1);
158 MPlist *pl = family_list;
161 p += sprintf (p, "%s", msymbol_name (mplist_value (pl)));
162 for (pl = mplist_next (pl); mplist_key (pl) != Mnil;
163 pl = mplist_next (pl))
164 p += sprintf (p, ",%s", msymbol_name (mplist_value (pl)));
166 m17n_object_unref (family_list);
173 GtkWidget *window, *box, *text, *label;
174 int n_channels, rowstride;
177 PangoAttrList *attr_list;
178 PangoAttribute *attr_size;
180 gtk_init (&argc, &argv);
182 attr_list = pango_attr_list_new ();
183 attr_size = pango_attr_size_new (FONT_PT_SIZE * PANGO_SCALE);
184 attr_size->start_index = 0, attr_size->end_index = title_len;
185 pango_attr_list_insert (attr_list, attr_size);
188 PangoAttribute *attr_family;
190 attr_family = pango_attr_family_new (family_names);
191 attr_family->start_index = 0, attr_family->end_index = title_len;
192 pango_attr_list_insert (attr_list, attr_family);
195 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8,
196 metric.width, metric.height);
197 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
198 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
199 pixels = gdk_pixbuf_get_pixels (pixbuf);
201 for (y = 0; y < metric.height; y++)
202 for (x = 0; x < metric.width; x++)
204 int cid = gdImageGetPixel (image, x, y);
205 p = pixels + y * rowstride + x * n_channels;
207 p[0] = gdImageRed (image, cid);
208 p[1] = gdImageGreen (image, cid);
209 p[2] = gdImageBlue (image, cid);
211 gdImageDestroy (image);
213 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
214 box = gtk_vbox_new (FALSE, 0);
215 text = gtk_image_new_from_pixbuf (pixbuf);
216 label = gtk_label_new ((gchar *) title);
217 gtk_label_set_attributes (GTK_LABEL (label), attr_list);
218 gtk_box_pack_start (GTK_BOX (box), text, TRUE, FALSE, 0);
219 gtk_box_pack_start (GTK_BOX (box), label, TRUE, FALSE, 0);
221 gtk_container_add (GTK_CONTAINER (window), box);
222 gtk_signal_connect (GTK_OBJECT (window), "delete_event",
223 GTK_SIGNAL_FUNC (delete_event), NULL);
224 gtk_signal_connect (GTK_OBJECT (window), "destroy",
225 GTK_SIGNAL_FUNC (destroy), NULL);
226 gtk_widget_show (text);
227 gtk_widget_show (label);
228 gtk_widget_show (box);
229 gtk_widget_show (window);