/* BGF file information

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA. */

#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "bogl.h"
#include "bogl-bgf.h"

int
bogl_cloexec(int fd)
{
  /* We don't care */
  return 0;
}

int main(int argc, char *argv[])
{
  struct bogl_font *font;
  int mask;
  wchar_t wchi, wclo;
  int w, h;
  unsigned count;

  if (argc < 2) {
    fprintf (stderr, "Usage: %s font.bgf\n", argv[0]);
    return 1;
  }

  if ((font = bogl_mmap_font(argv[1], 1)) == NULL) {
    fprintf(stderr, "Bad font\n");
    return 1;
  }

  h = font->height;
  mask = font->index_mask;

  printf("Name: %s\n", font->name);
  printf("Height: %d\n", font->height);
  printf("Default char: U+%04X\n", font->default_char);
  printf("Index mask: %04x\n", mask);

  count = 0;
  for (wchi = 0; wchi < 0x10000; wchi += mask+1)
  {
    for (wclo = 0; wclo < mask; wclo++)
    {
      int i;
      for (i = font->offset[wclo]; font->index[i]; i+= 2)
      {
	int x, y;
	u_int32_t *character;

	if ((font->index[i] & ~mask) != wchi)
	  continue;

	printf("\n");
	count++;

	w = font->index[i] & mask;
	printf("Char U+%04X\n", wclo | wchi);
	printf("Width %d:\n", w);

	character = &font->content[font->index[i+1]];
	for (y = 0; y < h; y++)
	  {
	    u_int32_t c = *character++;
	    putchar(' ');
	    for (x = 0; x < w; x++)
	      {
		if (c & 0x80000000)
		  putchar('X');
		else
		  putchar(' ');
		c <<= 1;
	      }
	    putchar('\n');
	  }
	break;
      }
    }
  }

  printf("Glyphs: %u\n", count);

  bogl_munmap_font(font);
  return 0;
}
