%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,ch,swidth,down,left


  %routine read hex(%integer %name x, %integer w)
     %integer n, i
     x = 0
     w = (w+3)>>2
     read symbol(ch) %until '0'<=ch<='9' %or 'A'<=ch<='F'
     i = 0
     %cycle
        %if '0'<=ch<='9' %start
           n = ch-'0'
        %else
           n = ch-'A'+10
        %finish
        %if i<8 %start
          x = x<<4+n
          i = i+1
        %else %if n#0 %and i>=8
           print string("Char ")
           write(char, 0)
           print string(" too long")
           new line
        %finish
        w = w-1
        read symbol(ch)
        %exit %if w=0 %or %not('0'<=ch<='9' %or 'A'<=ch<='F')
     %repeat
     x = x<<((8-i)<<2)
  %end

  %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
  %on %event 9 %start; -> done; %finish

  openinput(3,file)
  height = 0
  maxwidth = 0
  minwidth = 17
  font(i) = 520 %for i=0,1,255; !Default: null character
  font(32) = 526
  font(i) = 0 %for i=263, 1, 4999
  font(260) = 0; font(261) = 0; font(262) = 0; !This is the null character
  font(263) = 0; font(265) = 0;
  index = 266;                  !This is where the rest starts
  !read symbol(ch) %until ch=27
  !read symbol(ch)
  !read symbol(ch) %if ch='['
  !%if ch#'0' %start
     !print string("Leadin 0 expected")
     !%return
  !%finish
  read symbol(ch) %until ch=';'
  read(height)
  read symbol(ch) %until ch=';'
  read(depth)
  read symbol(ch) %until ch=';'
  read(swidth)
  font(264) = swidth
  read symbol(ch) %until ch=10 %or ch=13
  %cycle
    read symbol(ch) %until ch=27
    ch = next symbol
    read symbol(ch) %if ch='['
    read(char)
    read symbol(ch) %until ch=';'
    read(num)
    read symbol(ch) %until ch=';'
    read(down)
    read symbol(ch) %until ch=';'
    read(width)
    read symbol(ch) %until ch=';'
    read(left)
    read symbol(ch) %until ch#' '
    %exit %unless ch='K'
    font(char)=index<<1
    !font(index+1)=width
    maxwidth = width %if width>maxwidth
    minwidth = width %if width<minwidth
    font(index+2) = num
    %while num#0 %cycle
      read hex(n, width)
      %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) = n
       %for i = num-1,-1,1 %cycle
         read hex(n, width);
         wide = 1 %if n&16_FFFF0000#0
         junk(i) = 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
    font(index+2) = i-1-down
    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
done:
  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 = 0, 1, 255 %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.".lft")
openoutput(1,f.".bft")
select output(1)
%for i = 0, 1, index %cycle
   print word(font(i))
%repeat
%end %of %program
