%s properties startchar bitmap
%%
%{
  #include <sys/types.h>
  #include <limits.h>
  #include <db.h>
  #include <stdio.h>
  #include <string.h>
  #include <fcntl.h>
  unsigned char bitbuffer[100];
  int  bitloc, BBw, BBh, BBox, BBoy, Encoding;
  int  minBBox, maxBBox, minBBoy, maxBBoy;

  struct BITMAPHDR {
    unsigned short width;
    unsigned short height;
    unsigned short chars_row;
    unsigned short fieldflags;
    unsigned short pad[4];      /* Long alignment */
  };

  struct BITMAPHDR bmh;
  struct BITMAPHDR *bm; 
  DB* db;
  DBT key, content;
%}

^STARTFONT.*$			{
			          db = dbopen("font.db", O_CREAT|O_TRUNC|O_RDWR,  
			          0644,DB_HASH, NULL ); 
				  if (!db) {
 				    puts("Dying in db creation\n");
 				    exit(1);
				  }
				}
^COMMENT.*$			/* ignore */
^FONTBOUNDINGBOX.*$ 		/* ignore */
^FONT.*$			/* ignore */
^SIZE.*$			/* ignore */

STARTPROPERTIES			{ BEGIN(properties); }
<properties>ENDPROPERTIES	{ BEGIN(0);}
<properties>.			/* ignore all properties */

ENDFONT.*			{ db->sync(db,0);
				  db->close(db);
				  yyterminate();
				}

^CHARS.*$
STARTCHAR			{ BEGIN(startchar); 
				}
<startchar>BITMAP		{ BEGIN(bitmap); bitloc=2*(BBoy+2); 
				  memset(bitbuffer,0,100); }
<startchar>BBX.*$		{ sscanf(yytext, "BBX %d %d %d %d", 
				  &BBw, &BBh, &BBox, &BBoy);
				}

<startchar>ENCODING.*$		{ sscanf(yytext, "ENCODING %d", &Encoding); }
<startchar>.			/* ignore everything else */

<bitmap>ENDCHAR			{ int i;
				  int of; 
				  char keydata[100];
				  if (bitloc >33) puts("ARGGGH!");
				  if (Encoding >= 0x3000 ) {
				    int SrcB,DstB;
				    SrcB = ((BBw+7)&~7)/8;
				    DstB = ((BBw+15)&~15)/8;
  				    sprintf(keydata, "%04x", Encoding ); 
				    fprintf(stderr, ".");

			            key.data = keydata;
				    key.size = strlen(keydata);
				    content.data = bitbuffer;				
				    content.size = 32;
				    
				    db->put(db, &key, &content,R_NOOVERWRITE);
			 	  } 
				  BEGIN(0); 
				}

<bitmap>[0-9A-Fa-f]{2}		{ int bit; 
				  sscanf(yytext, "%x", &bit);
				  bitbuffer[bitloc++] = (char) bit;
				}
<bitmap>[ \t\n]

\n				/* ignore any linefeeds */


%%
int yywrap() { return 0;}
int main() {
	yylex();
	return 0;
}
