%begin
%integer index, i, j
%integer %array font(0:4999)
%half %integer %array junk(1:64)
%routine read font(%string(255) file); ! Read in (one) font description
  %integer char,num,i,n,min,max,height,width,maxwidth,minwidth


  %integerfn reverse(%integer x)
    %integer n=0,y=16_8000,c=16
    %cycle
      n = n+y %if x&1#0
      x = x>>1; y = y>>1; c = c-1
    %repeatuntil c=0
    %result=n
  %end

  openinput(3,file)
  height = 0
  maxwidth = 0
  minwidth = 17
  font(i) = 520 %for i=0,1,255; !Default: null character
  font(i) = 0 %for i=263, 1, 4999
  font(260) = 0; font(261) = 0; font(262) = 0; !This is the null character
  index = 263;                  !This is where the rest starts
  read(min); read(max)
  %for char=min,1,max %cycle
    readsymbol(num) %until num=':';  !Ignore character name
    font(char)=index<<1
    read(num)
    read(width)
    %while num#0 %cycle
      read(n)
      %exit %if n#0
      num = num-1
    %repeat
    %if num#0 %start
       junk(num) = reverse(n)
       %for i = num-1,-1,1 %cycle
         read(n);
         junk(i) = reverse(n)
       %repeat
    %finish
    j = 0
    %while j<=16 %cycle
       i = 1
       i = i+1 %while i<=num %and 16_8000&junk(i)=0
       %if i>num %start
          junk(i) = junk(i)<<1 %for i = 1, 1, num
       %else
          %exit
       %finish
       j = j+1
    %repeat
    %while j<=16 %cycle
       i = 1
       i = i+1 %while i<=num %and (1<<j)&junk(i)=0
       %if i>num %start
          j = j+1
       %else
          %exit
       %finish
    %repeat
    i = 1
    i = i+1 %while i<=num %and junk(i)=0
    width = 16-j+1 %unless num=0
    font(index+1) = width
    font(index) = num-i+1
    maxwidth = width %if width>maxwidth
    minwidth = width %if width<minwidth
    height = num-i+1 %if num-i+1>height;     !Note highest character
    index = index+3
    font(index-1) = i-1
    %while i<=num %cycle
       font(index) = junk(i)
       i = i+1
       index = index+1
    %repeat
  %repeat
  font(256) = height
  font(257) = 0
  font(258) = maxwidth
  font(259) = minwidth
  index = index-1
  closeinput; selectinput(0)
%end
   %routine print byte(%integer b)
      b = b&16_FF
      print symbol(b)
   %end
   %routine print word(%integer w)
      print byte(w>>8)
      print byte(w)
   %end
   %routine print long(%integer l)
      print word(l>>16)
      print word(l)
   %end
   %string(255) f
   f = cliparam
readfont(f.".fnt")
openoutput(1,f."p.bft")
select output(1)
%for i = 0, 1, index %cycle
   print word(font(i))
%repeat
%end %of %program
