10 #include FT_FREETYPE_H
13 #include <X11/keysym.h>
14 #include <X11/Xutil.h>
18 //#define FONT_NAME "-adobe-courier-medium-r-normal--12-120-75-75-m-70-iso8859-1"
19 #define FONT_NAME "6x13"
20 #define FONT_HEIGHT 14
22 int font_height, font_width;
31 GC gc_norm, gc_rev, gc_xor;
32 unsigned long valuemask;
33 unsigned long foreground, background;
47 Bitmap bitmap[0x10000];
49 /* Unicode to glyph index mapping table. */
53 draw_bitmap (int index, int x, int y)
55 Bitmap *bmp = bitmap + index;
56 unsigned char *buf = bmp->buf;
63 for (i = 0; i < bmp->rows; i++, buf += bmp->pitch)
64 for (j = 0; j < bmp->width; j++)
65 if (buf[j / 8] & (1 << (7 - (j % 8))))
66 XDrawPoint (display, win, gc_norm, x + j, y + i);
73 fprintf (stderr, "Error by %s\n", msg);
79 read_unicode_seq (char *filename, int *code)
81 FILE *fp = fopen (filename, "r");
86 fprintf (stderr, "File \"%s\" can't be opened.\n", filename);
90 && fscanf (fp, "%x", code + i) == 1)
98 main (int argc, char **argv)
102 int i, j, max_glyph_idx;
105 #define UPDATE_RENDERING 1
106 #define UPDATE_BITMAP 2
107 OTF_GlyphString gstring;
109 int unicode_seq[256];
113 +-------------------------+
114 | +--- rendering area --+ |
119 | +---------------------+ |
120 | +--- bitmap area -----+ |
124 | +---------------------+ |
125 +-------------------------+
128 int win_width, win_height;
129 int cols = 16, rows = 8;
130 int max_glyph_width, max_glyph_height;
131 int inner_width, rendering_area_height, bitmap_area_height;
132 int x, y, x0, y0, x1, y1;
135 gstring.size = gstring.used = 256;
136 g = calloc (256, sizeof (OTF_Glyph));
139 if (argc != 2 && argc != 3)
141 fprintf (stderr, "Usage, otfview OTF-FILE [CODE-FILE]\n");
145 otf = OTF_open (argv[1]);
147 || OTF_get_table (otf, "head") < 0
148 || OTF_get_table (otf, "cmap") < 0)
150 OTF_perror ("otfview");
154 err = FT_Init_FreeType (&library);
156 quit ("FT_Init_FreeType");
157 err = FT_New_Face (library, argv[1], 0, &face);
158 if (err == FT_Err_Unknown_File_Format)
159 quit ("FT_New_Face: unknown file format");
161 quit ("FT_New_Face: unknown error");
162 err = FT_Set_Pixel_Sizes (face, 0, PIXEL_SIZE);
164 quit ("FT_Set_Char_Size");
166 memset (utog, 0, sizeof (utog));
167 x0 = x1 = y0 = y1 = 0;
168 for (i = 0; i < 0x10000; i++)
170 err = FT_Load_Glyph (face, i, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
172 bitmap[i].buf = NULL;
175 Bitmap *bmp = bitmap + i;
179 bmp->advance = face->glyph->metrics.horiAdvance >> 6;
180 bmp->left = face->glyph->bitmap_left;
181 bmp->top = face->glyph->bitmap_top;
182 bmp->rows = face->glyph->bitmap.rows;
183 bmp->width = face->glyph->bitmap.width;
184 bmp->pitch = face->glyph->bitmap.pitch;
185 bmpsize = bmp->rows * bmp->pitch;
186 bmp->buf = malloc (bmpsize);
187 memcpy (bmp->buf, face->glyph->bitmap.buffer, bmpsize);
192 if (x1 < bmp->left + bmp->width)
193 x1 = bmp->left + bmp->width;
194 if (y1 < bmp->rows - bmp->top)
195 y1 = bmp->rows - bmp->top;
199 max_glyph_height = y1 - y0;
200 max_glyph_width = x1 - x0;
202 for (i = 0; i < 0x10000; i++)
204 gstring.glyphs[i & 0xFF].c = i;
205 if ((i & 0xFF) == 0xFF)
207 OTF_drive_cmap (otf, &gstring);
208 for (j = 0; j < 0x100; j++)
210 utog[(i & 0xFF00) + j] = gstring.glyphs[j].glyph_id;
211 if (gstring.glyphs[j].glyph_id > 0)
212 bitmap[gstring.glyphs[j].glyph_id].unicode = (i & 0xFF00) + j;
218 n_codes = read_unicode_seq (argv[2], unicode_seq);
220 display = XOpenDisplay (NULL);
221 screen = DefaultScreen (display);
222 font = XLoadQueryFont (display, FONT_NAME);
224 font = XLoadQueryFont (display, "fixed");
225 font_height = font->ascent + font->descent;
226 font_width = font->max_bounds.width;
228 inner_width = (max_glyph_width + 1) * cols + font_width * 4 + 2;
229 rendering_area_height= (max_glyph_height + 1) * 3 + font_height;
230 bitmap_area_height = (max_glyph_height + 1) * rows + font_height + 2;
231 win_width = inner_width + margin * 2;
232 win_height = rendering_area_height + bitmap_area_height + margin * 3;
234 win = XCreateSimpleWindow (display, RootWindow (display, screen),
235 0, 0, win_width, win_height, 1,
236 BlackPixel (display, screen),
237 WhitePixel (display, screen));
239 valuemask = GCForeground | GCBackground | GCFunction | GCFont;
241 values.foreground = BlackPixel (display, screen);
242 values.background = WhitePixel (display, screen);
243 values.function = GXcopy;
244 values.font = font->fid;
245 gc_norm = XCreateGC (display, win, valuemask, &values);
247 values.foreground = WhitePixel (display, screen);
248 values.background = BlackPixel (display, screen);
249 values.function = GXcopy;
250 gc_rev = XCreateGC (display, win, valuemask, &values);
252 values.foreground = BlackPixel (display, screen);
253 values.background = WhitePixel (display, screen);
254 values.function = values.foreground ? GXxor : GXequiv;
255 gc_xor = XCreateGC (display, win, valuemask, &values);
257 XMapWindow (display, win);
258 XSelectInput (display, win, ExposureMask | KeyPressMask | ButtonPressMask);
266 XNextEvent (display, &event);
272 int x = event.xbutton.x;
273 int y = event.xbutton.y;
275 if (margin <= x && x < margin + inner_width
276 && margin + 1 <= y && y < margin + 1 + font_height)
278 n_codes = read_unicode_seq (argv[2], unicode_seq);
279 update_mask = UPDATE_RENDERING;
291 n = XLookupString ((XKeyEvent *) &event, buf, 512, &keysym, NULL);
296 if (buf[0] == 'n' || buf[0] == ' ')
298 if (first_idx + cols * rows <= max_glyph_idx)
300 first_idx += cols * rows;
301 update_mask |= UPDATE_BITMAP;
305 else if (buf[0] == 'p'
306 || keysym == XK_BackSpace || keysym == XK_Delete)
310 first_idx -= cols * rows;
311 update_mask |= UPDATE_BITMAP;
319 update_mask = UPDATE_RENDERING | UPDATE_BITMAP;
325 if (update_mask == (UPDATE_RENDERING | UPDATE_BITMAP))
327 XClearWindow (display, win);
329 y = margin + font->ascent;
330 XDrawImageString (display, win, gc_norm, x, y, "Unicode: ", 9);
331 y += font_height + (max_glyph_height - font_height) / 2;
332 XDrawImageString (display, win, gc_norm, x, y, " cmap: ", 9);
333 y += max_glyph_height + 1;
334 XDrawImageString (display, win, gc_norm, x, y, " GSUB: ", 9);
335 y += max_glyph_height + 1;
336 XDrawImageString (display, win, gc_norm, x, y, " GPOS: ", 9);
338 y = margin * 2 + rendering_area_height;
339 XDrawLine (display, win, gc_norm, x, y, x + inner_width - 1, y);
340 y += font_height + 1;
341 for (i = 0; i <= rows; i++, y += max_glyph_height + 1)
342 XDrawLine (display, win, gc_norm, x, y, x + inner_width - 1, y);
343 y = margin * 2 + rendering_area_height;
344 XDrawLine (display, win, gc_norm, x, y,
345 x, y + bitmap_area_height - 1);
346 x += font_width * 4 + 1;
347 for (i = 0; i <= cols; i++, x += max_glyph_width + 1)
348 XDrawLine (display, win, gc_norm, x, y,
349 x, y + bitmap_area_height - 1);
350 y += font->ascent + 1;
351 x = (margin + font_width * 4 + 2
352 + (max_glyph_width - font_width * 4) / 2);
353 for (i = 0; i < cols; i++, x += max_glyph_width + 1)
355 sprintf (buf, "xxx%X", i);
356 XDrawImageString (display, win, gc_norm, x, y, buf, 4);
360 if (update_mask & UPDATE_RENDERING)
362 x = margin + font_width * 9;
363 y = margin + font->ascent;
364 for (i = 0; i < n_codes; i++)
366 sprintf (buf + i * 5, "%04X ", unicode_seq[i]);
367 gstring.glyphs[i].c = unicode_seq[i];
369 gstring.used = n_codes;
370 XDrawImageString (display, win, gc_norm, x, y, buf, n_codes * 5);
372 OTF_drive_cmap (otf, &gstring);
373 y = margin + font_height + 1;
374 for (i = 0; i < n_codes; i++, x += max_glyph_width)
375 draw_bitmap (gstring.glyphs[i].glyph_id, x - x0, y - y0);
377 OTF_drive_gsub (otf, &gstring, "deva", NULL, NULL);
378 x = margin + font_width * 9;
379 y += max_glyph_height;
380 for (i = 0; i < gstring.used; i++, x += max_glyph_width)
381 draw_bitmap (gstring.glyphs[i].glyph_id, x - x0, y - y0);
383 OTF_drive_gpos (otf, &gstring, "deva", NULL, NULL);
384 x = margin + font_width * 9 - x0;
385 y += max_glyph_height - y0;
386 for (i = 0; i < gstring.used; i++)
388 int xoff = 0, yoff = 0;
389 OTF_Glyph *g = gstring.glyphs + i;
391 switch (g->positioning_type)
394 if (g->f.f1.format & OTF_XPlacement)
395 xoff = PIXEL_SIZE * ((double) (g->f.f1.value->XPlacement)
396 * 100 / otf->head->unitsPerEm);
397 if (g->f.f1.format & OTF_YPlacement)
398 yoff = PIXEL_SIZE * ((double) (g->f.f1.value->YPlacement)
399 * 100 / otf->head->unitsPerEm);
404 * ((double) (g->f.f4.base_anchor->XCoordinate
405 - g->f.f4.mark_anchor->XCoordinate)
406 * 100 / otf->head->unitsPerEm));
408 * ((double) (g->f.f4.base_anchor->YCoordinate
409 - g->f.f4.mark_anchor->YCoordinate)
410 * 100 / otf->head->unitsPerEm));
414 draw_bitmap (gstring.glyphs[i].glyph_id, x + xoff, y + yoff);
415 x += bitmap[gstring.glyphs[i].glyph_id].advance;
419 if (update_mask & UPDATE_BITMAP)
422 y = (margin * 2 + rendering_area_height + font_height + 2
423 + (max_glyph_height - font_height) / 2 + font->ascent);
424 for (i = 0; i < rows; i++, y += max_glyph_height + 1)
426 sprintf (buf, "%03Xx", (first_idx + i * cols) / 16);
427 XDrawImageString (display, win, gc_norm, x, y, buf, 4);
429 x += font_width * 4 + 1;
430 y = margin * 2 + rendering_area_height + font_height + 2;
431 for (i = 0; i < rows; i++)
432 for (j = 0; j < cols; j++)
434 XClearArea (display, win, x + (max_glyph_width + 1) * j,
435 y + (max_glyph_height + 1) * i,
436 max_glyph_width, max_glyph_height, False);
437 draw_bitmap (first_idx + i * cols + j,
438 x + (max_glyph_width + 1) * j - x0,
439 y + (max_glyph_height + 1) * i - y0);