10 #include <m17n-misc.h>
12 /* Format MSG by FMT and print the result to the stderr, and exit. */
14 #define FATAL_ERROR(fmt, arg) \
21 char base64_encode_table[] =
22 { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
23 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
24 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
25 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
26 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
27 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
31 base64_encode (const unsigned char *src, int length, FILE *fp)
40 putc (base64_encode_table[0x3f & c >> 2], fp);
41 value = (0x03 & c) << 4;
44 putc (base64_encode_table[value], fp);
50 putc (base64_encode_table[value | (0x0f & c >> 4)], fp);
51 value = (0x0f & c) << 2;
54 putc (base64_encode_table[value], fp);
59 putc (base64_encode_table[value | (0x03 & c >> 6)], fp);
60 putc (base64_encode_table[0x3f & c], fp);
65 main (int argc, char **argv)
77 MConverter *converter;
80 MDrawMetric ink, logical;
81 int ascent, descent, height, width;
90 FATAL_ERROR ("Number of arguments %d must be 4\n", argc);
93 if (merror_code != MERROR_NONE)
94 FATAL_ERROR ("%s\n", "Failed to initialize the m17n library.");
96 lang = msymbol (argv[1]);
97 name = msymbol (argv[2]);
98 im = minput_open_im (lang, name, NULL);
100 FATAL_ERROR ("%s\n", "Failed to open the input method.");
101 ic = minput_create_ic (im, NULL);
103 FATAL_ERROR ("%s\n", "Failed to create the input context.");
105 for (i = 0; argv[3][i]; i++)
110 keybuf[0] = argv[3][i];
112 key = msymbol (keybuf);
113 if (minput_filter (ic, key, NULL) == 1)
115 minput_lookup (ic, key, NULL, mt);
118 mtext_cat (mt, ic->preedit);
119 len = mtext_len (mt);
121 mtext_put_prop (mt, 0, len, Mlanguage, lang);
124 MPlist *plist = mplist (), *p;
125 MFontset *fontset = mfontset ("generic");
126 MFace *face = mface ();
128 mface_put_prop (face, Mfontset, fontset);
129 mface_put_prop (face, Msize, (void *) (fontsize * dpi / 100));
130 p = mplist_add (plist, Mdevice, msymbol ("gd"));
131 p = mplist_add (p, Mface, face);
132 m17n_object_unref (face);
133 frame = mframe (plist);
134 m17n_object_unref (plist);
136 FATAL_ERROR ("%s\n", "Failed to open a frame.");
139 memset (&control, 0, sizeof control);
140 control.enable_bidi = 1;
142 mdraw_text_extents (frame, mt, 0, len, &control, &ink, &logical, NULL);
143 ascent = - logical.y;
144 if (ascent < - ink.y)
146 descent = logical.height + logical.y;
147 if (descent < ink.height + ink.y)
148 descent = ink.height + ink.y;
149 height = ascent + descent;
150 width = logical.width;
152 work = gdImageCreate (width, height);
153 work_white = gdImageColorAllocate (work, 255, 255, 255);
154 gdImageColorTransparent (work, work_white);
155 gdImageFilledRectangle (work, 0, 0, width - 1, height - 1, work_white);
156 mdraw_text_with_control (frame, work, 0, ascent, mt, 0, len, &control);
158 converter = mconv_stream_converter (msymbol ("utf-8"), stdout);
163 MDrawMetric *ink_array, *logical_array;
164 int array_size = len * 2;
169 ink_array = malloc (sizeof (MDrawMetric) * array_size);
170 logical_array = malloc (sizeof (MDrawMetric) * array_size);
172 if (mdraw_text_per_char_extents (frame, mt, 0, len, &control,
173 ink_array, logical_array,
174 array_size, &num_chars, NULL, NULL) < 0)
176 ink_array = realloc (ink_array, sizeof (MDrawMetric) * num_chars);
177 logical_array = realloc (logical_array, sizeof (MDrawMetric) * num_chars);
178 mdraw_text_per_char_extents (frame, mt, 0, len, &control,
179 ink_array, logical_array,
180 num_chars, &num_chars, NULL, NULL);
183 for (from = 0, i = 0; i < num_chars;)
187 int x = logical_array[i].x;
189 image = gdImageCreate (logical_array[i].width, height);
190 white = gdImageColorAllocate (image, 255, 255, 255);
191 gdImageColorTransparent (image, white);
192 gdImageCopy (image, work, 0, 0,
193 logical_array[i].x, 0, logical_array[i].width, height);
194 printf ("<img src=\"data:image/png;base64,");
195 buf = gdImagePngPtr (image, &size);
196 base64_encode (buf, size, stdout);
198 gdImageDestroy (image);
200 while (++i < num_chars && logical_array[i].x == x);
204 to = mdraw_coordinates_position (frame, mt, 0, len,
205 logical_array[i].x, 0, &control);
206 printf ("\" alt=\"");
207 mconv_encode_range (converter, mt, from, to);