6 #include <gdk-pixbuf/gdk-pixbuf.h>
10 #define FONT_PT_SIZE 40
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;
40 gtk_init (&argc, &argv);
44 fprintf (stderr, "Usage: gdkdraw LANGUAGE\n");
49 lang = msymbol (argv[1]);
50 mt = mlanguage_text (lang);
53 fprintf (stderr, "No native name for the language \"%s\".\n", argv[1]);
57 nchars = mtext_len (mt);
59 /* Encode native language name in MT into the buffer TITLE. */
61 utf8 = msymbol ("utf-8");
62 allocated = nchars * 4; /* Usually this is enough. */
63 title = malloc (allocated);
64 title_len = mconv_encode_buffer (utf8, mt, title, allocated);
65 while (title_len < 0 && merror_code == MCONVERSION_RESULT_INSUFFICIENT_DST)
67 allocated += allocated;
68 title = realloc (title, allocated);
69 title_len = mconv_encode_buffer (utf8, mt, title, allocated);
73 fprintf (stderr, "Encoding of native language name failed.");
76 title[title_len] = '\0';
79 MPlist *param = mplist ();
80 MFace *face = mface ();
81 MFontset *fontset = mfontset ("generic");
83 mface_put_prop (face, Msize, (void *) (FONT_PT_SIZE * -10));
84 mface_put_prop (face, Mfontset, fontset);
86 mface_put_prop (face, Mfamily, msymbol (argv[2]));
87 mplist_add (param, Mdevice, msymbol ("gd"));
88 mplist_add (param, Mface, face);
89 frame = mframe (param);
90 m17n_object_unref (fontset);
91 m17n_object_unref (face);
92 m17n_object_unref (param);
96 fprintf (stderr, "Frame creation failed. Perhaps no GD library.\n");
101 /* Build GD image and draw MT on it while listing up font families
104 MDrawControl control;
106 int array_size, num_glyphs;
110 memset (&control, 0, sizeof (control));
111 control.anti_alias = 1;
112 control.enable_bidi = 1;
113 mdraw_text_extents (frame, mt, 0, mtext_len (mt), &control,
114 &metric, NULL, NULL);
116 image = gdImageCreateTrueColor (metric.width, metric.height);
117 bg_color = gdImageColorAllocate (image, 255, 255, 255);
118 gdImageFilledRectangle (image, 0, 0, metric.width - 1, metric.height - 1,
121 array_size = nchars * 2; /* Usually this is enough. */
122 glyphs = malloc (sizeof (MDrawGlyph) * array_size);
123 if (mdraw_glyph_list (frame, mt, 0, nchars, &control, glyphs, array_size,
126 array_size = num_glyphs;
127 glyphs = realloc (glyphs, sizeof (MDrawGlyph) * array_size);
128 mdraw_glyph_list (frame, mt, 0, nchars, &control, glyphs, array_size,
134 while (num_glyphs-- > 0)
135 if (glyphs[num_glyphs].font)
137 MSymbol family = mfont_get_prop (glyphs[num_glyphs].font, Mfamily);
141 family_list = mplist ();
142 mplist_add (family_list, Msymbol, family);
143 family_name_len = strlen (msymbol_name (family));
145 else if (! mplist_find_by_value (family_list, family))
147 mplist_push (family_list, Msymbol, family);
148 family_name_len += strlen (msymbol_name (family)) + 1;
152 mdraw_text_with_control (frame, (MDrawWindow) image,
153 - metric.x, - metric.y,
154 mt, 0, mtext_len (mt), &control);
155 m17n_object_unref (frame);
159 char *p = alloca (family_name_len + 1);
160 MPlist *pl = family_list;
163 p += sprintf (p, "%s", msymbol_name (mplist_value (pl)));
164 for (pl = mplist_next (pl); mplist_key (pl) != Mnil;
165 pl = mplist_next (pl))
166 p += sprintf (p, ",%s", msymbol_name (mplist_value (pl)));
168 m17n_object_unref (family_list);
175 GtkWidget *window, *box, *text, *label;
176 int n_channels, rowstride;
179 PangoAttrList *attr_list;
180 PangoAttribute *attr_size;
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_box_pack_start (GTK_BOX (box), text, TRUE, FALSE, 0);
218 gtk_box_pack_start (GTK_BOX (box), label, TRUE, FALSE, 0);
220 gtk_container_add (GTK_CONTAINER (window), box);
221 gtk_signal_connect (GTK_OBJECT (window), "delete_event",
222 GTK_SIGNAL_FUNC (delete_event), NULL);
223 gtk_signal_connect (GTK_OBJECT (window), "destroy",
224 GTK_SIGNAL_FUNC (destroy), NULL);
225 gtk_widget_show (text);
226 gtk_widget_show (label);
227 gtk_label_set_attributes (GTK_LABEL (label), attr_list);
228 gtk_widget_show (box);
229 gtk_widget_show (window);