%begin
%integer index, i, j
%integer %array font(0:4999)
%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,wide,depth


  %integerfn reverse(%integer x)
    %integer n=0,y=16_80000000,c=32
    %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)
    maxwidth = width %if width>maxwidth
    minwidth = width %if width<minwidth
    font(index+2) = num
    %while num#0 %cycle
      read(n)
      %exit %if n#0
      num = num-1
    %repeat
    font(index+2) = font(index+2)-num
    wide = 0
    %if num#0 %start
       wide = 1 %if n&16_FFFF0000#0
       junk(num) = reverse(n)
       %for i = num-1,-1,1 %cycle
         read(n);
         wide = 1 %if n&16_FFFF0000#0
         junk(i) = reverse(n)
       %repeat
    %finish
    %if wide=0 %then font(index+1) = width %else font(index+1) = -width
    i = 1
    i = i+1 %while i<=num %and junk(i)=0
    font(index) = num-i+1
    height = num-i+1 %if num-i+1>height;     !Note highest character
    index = index+3
    %while i<=num %cycle
       %if wide=0 %start
          font(index) = junk(i)>>16
          index = index+1
       %else
          font(index) = junk(i)>>16
          font(index+1) = junk(i)&16_FFFF
          index = index+2
       %finish
       i = i+1
    %repeat
  %repeat
  n = font(65)>>1
  depth = height-font(n+2)-font(n)
  font(256) = height
  font(257) = depth
  font(258) = maxwidth
  font(259) = minwidth
  write(height, 5); write(depth, 5); write(maxwidth, 5); write(minwidth, 5); newline
  index = index-1
  %for char = min, 1, max %cycle
     num = font(char)>>1
     font(num+2) = height-depth-font(num+2)-font(num)
  %repeat
  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.".bft")
select output(1)
%for i = 0, 1, index %cycle
   print word(font(i))
%repeat
%end %of %program
