unit uTICharset;
{
This code is part of the TiCodEd Application, designed to develop for the
TI-99/4a Homecomputer in a modern, structured way.

BSD License
Copyright (c) 2026, Stefan Bauch, TiCodEd <at> lizardware <dot> de


Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

}
{$mode objfpc}{$H+}

interface

uses Graphics,math;

const TIChar : Array[32..126] of String[16] =
      ('0000000000000000'
      ,'0010101010100010'
      ,'0028282800000000'
      ,'0028287C287C2828'
      ,'0038545038145438'
      ,'0060640810204C0C'
      ,'0020505020544834'
      ,'0008081000000000'
      ,'0008102020201008'
      ,'0020100808081020'
      ,'000028107C102800'
      ,'000010107C101000'
      ,'0000000000301020'
      ,'000000007C000000'
      ,'0000000000003030'
      ,'0000040810204000'
      ,'0038444444444438'
      ,'0010301010101038'
      ,'003844040810207C'
      ,'0038440418044438'
      ,'00081828487C0808'
      ,'007C407804044438'
      ,'0018204078444438'
      ,'007C040810202020'
      ,'0038444438444438'
      ,'003844443C040830'
      ,'0000303000303000'
      ,'0000303000301020'
      ,'0008102040201008'
      ,'0000007C007C0000'
      ,'0020100804081020'
      ,'0038440408100010'
      ,'0038445C545C4038'
      ,'003844447C444444'
      ,'0078242438242478'
      ,'0038444040404438'
      ,'0078242424242478'
      ,'007C40407840407C'
      ,'007C404078404040'
      ,'003C40405C444438'
      ,'004444447C444444'
      ,'0038101010101038'
      ,'0004040404044438'
      ,'0044485060504844'
      ,'004040404040407C'
      ,'00446C5454444444'
      ,'00446464544C4C44'
      ,'007C44444444447C'
      ,'0078444478404040'
      ,'0038444444544834'
      ,'0078444478504844'
      ,'0038444038044438'
      ,'007C101010101010'
      ,'0044444444444438'
      ,'0044444428281010'
      ,'0044444454545428'
      ,'0044442810284444'
      ,'0044442810101010'
      ,'007C04081020407C'
      ,'0038202020202038'
      ,'0000402010080400'
      ,'0038080808080838'
      ,'0000102844000000'
      ,'000000000000007C'
      ,'0000201008000000'
      ,'00000038447C4444'
      ,'0000007824382478'
      ,'0000003C4040403C'
      ,'0000007824242478'
      ,'0000007C4078407C'
      ,'0000007C40784040'
      ,'0000003C405C4438'
      ,'00000044447C4444'
      ,'0000003810101038'
      ,'0000000808084830'
      ,'0000002428302824'
      ,'000000404040407C'
      ,'000000446C544444'
      ,'0000004464544C44'
      ,'0000007C4444447C'
      ,'0000007844784040'
      ,'0000003844544834'
      ,'0000007844784844'
      ,'0000003C40380478'
      ,'0000007C10101010'
      ,'0000004444444438'
      ,'0000004444282810'
      ,'0000004444545428'
      ,'0000004428102844'
      ,'0000004428101010'
      ,'0000007C0810207C'
      ,'0018202040202018'
      ,'0010101000101010'
      ,'0030080804080830'
      ,'0000205408000000');

TYPE

  tcset = record
            pat : String[16];
            idx : Integer;
            size: Integer;
            strt: Integer;
            cx  : Integer;
            cy  : Integer;
          end;

type TBinChar = array[0..7] of byte;


VAR   cset : Array[0..2,0..255] of Tcset;
      csprev : TBitmap;


function nibble2hex(s:String):char;
function hex2bin(h:String):AnsiString;
function GetTIChar(c:Integer):String;
function GetCharIfDefined(pg,ch:Integer):String;
procedure CallCharUpdate(pg,ch:Integer);
procedure InitCSet;
function GetChar(c:String):String;
function SaveCSet(fn:String):boolean;
function LoadCSet(fn:String):boolean;
procedure LoadFont(pg,cnt:integer;fn:shortstring);
procedure DumpFont(pg,cnt:integer;fn:shortstring);


implementation

uses sysutils,StrUtils,uMain;

function inttostrfix(i,l:integer):String;
var s : string;
begin
  str(i:l,s);
  Result := StringReplace(s,' ','0',[rfReplaceAll]);
end;

Function nibble2hex(s:String):char;
begin
  case s of
    '0000' : nibble2hex := '0';
    '0001' : nibble2hex := '1';
    '0010' : nibble2hex := '2';
    '0011' : nibble2hex := '3';
    '0100' : nibble2hex := '4';
    '0101' : nibble2hex := '5';
    '0110' : nibble2hex := '6';
    '0111' : nibble2hex := '7';
    '1000' : nibble2hex := '8';
    '1001' : nibble2hex := '9';
    '1010' : nibble2hex := 'A';
    '1011' : nibble2hex := 'B';
    '1100' : nibble2hex := 'C';
    '1101' : nibble2hex := 'D';
    '1110' : nibble2hex := 'E';
    '1111' : nibble2hex := 'F';
     else  nibble2hex := 'X';
  end;
end;

function hex2bin(h:String):AnsiString;
var a : AnsiString;
    i : Integer;
begin
  a := '';
  for i:=1 to length(h) do case UpperCase(h[i]) of
    '0' : a := a + '0000';
    '1' : a := a + '0001';
    '2' : a := a + '0010';
    '3' : a := a + '0011';
    '4' : a := a + '0100';
    '5' : a := a + '0101';
    '6' : a := a + '0110';
    '7' : a := a + '0111';
    '8' : a := a + '1000';
    '9' : a := a + '1001';
    'A' : a := a + '1010';
    'B' : a := a + '1011';
    'C' : a := a + '1100';
    'D' : a := a + '1101';
    'E' : a := a + '1110';
    'F' : a := a + '1111';
    else  a := a + '0000';
  end;
  hex2bin := a;
end;


function GetTIChar(c: Integer): String;
begin
  if (c<32) or (c>126) then Result:=''
                       else Result:=TIChar[c];
end;

function GetCharIfDefined(pg, ch: Integer): String;
var s: String;
begin
  s := cset[pg,ch].pat;
  if pg=1 then
    s:=trim(s);
  if GetTIChar(ch)=s then s:='';
  Result := s;
end;

procedure CallCharUpdate(pg, ch: Integer);
var x,y,i:integer;
    b:Ansistring;
begin
  b := Hex2Bin(cset[pg,ch].pat);
  i := 1;
  for y:=0 to 7 do
    for x:=0 to 7 do begin
       if (length(b)<i) or (copy(b,i,1)='0')
          then csprev.Canvas.Pixels[pg*8+x,ch*8+y] := clWhite
          else csprev.Canvas.Pixels[pg*8+x,ch*8+y] := clBlack;
       inc(i);
    end;
end;

procedure InitCSet;
VAR ax,ay : integer;
begin
  csprev:=TBitMap.Create;
  csprev.SetSize(3*8,256*8);
  for ax:=0 to 2 do
    for ay:=0 to 255 do with cset[ax,ay] do begin
       if (ay>31) and (ay<127) and (ax<2)
         then pat := TIChar[ay]
         else pat := '';
       idx  := -1;
       size := -1;
       strt := -1;
       cx   := 0;
       cy   := 0;
       CallCharUpdate(ax,ay);
    end;
  with cset[2,0] do begin
     size := 0;
     strt := 0;
     cx   := 1;
     cy   := 1;
  end;
end;

function GetChar(c: String): String;
var s,t: String;
    i,pg,ch,rep : Integer;
begin
  s:='';
  try
    c:=copy(c,6,255); // remove #CHAR
    pg := min(StrToInt(copy(c,1,1))-1,2);
    ch := StrToInt(copy(c,2,3));
    if copy(c,5,1)=':' then rep := StrToInt(copy(c,6,1))
                       else rep := 1;
    for i:=0 to rep-1 do begin
       t:=cset[pg,ch+i].pat;
       if t='' then t:='0000000000000000';
       s:=s+t;
    end;
  finally
    Result := s;
  end;
end;

function SaveCSet(fn: String): boolean;
var f : textfile;
  pg,ch : Integer;
  s : string;
begin
  Result := false;
  try
    assign(f,fn);
    rewrite(f);
    for pg:=0 to 2 do
      for ch:=0 to 255 do
        if (cset[pg,ch].pat<>'') and (cset[pg,ch].pat<>GetTIChar(ch)) then with cset[pg,ch] do begin
           s:= Inttostr((pg+1)*1000+ch)+',"'+pat+'",'+IntToStr(size)+'-'+IntToStrFix(strt,3)+':'+IntToStrFix(cx,2)+'-'+IntToStrFix(cy,2);
           writeln(f,s);
        end;
    writeln(f);
    Result := true;
  finally
    close(f);
  end;
end;

function LoadCSet(fn: String): boolean;
var f : textfile;
  pg,ch : Integer;
  s : string;
begin
  Result := false;
  if not FileExists(fn) then exit;
  try
    assign(f,fn);
    reset(f);
    readln(f,s);
    while not eof(f) do begin
       pg := StrToInt(copy(s,1,1))-1;
       ch := StrToInt(copy(s,2,3));
       with cset[pg,ch] do begin
         pat  := copy(s,7,16);
         size := StrToInt(copy(s,25,1));
         strt := StrToInt(copy(s,27,3));
         cx   := StrToInt(copy(s,31,2));
         cy   := StrToInt(copy(s,34,2));
         idx  := -1;
         CallCharUpdate(pg,ch);
       end;
       readln(f,s);
    end;
    Result := true;
  finally
    close(f);
  end;
end;


procedure LoadFont(pg,cnt:integer;fn:shortstring);
const bcTI : TBinChar =($07,$54,$49,$46,$49,$4C,$45,$53);
      bcMG : TBinChar =($4D,$47,$30,$32,$0,$0,$0,$0);
var f : file of TBinChar;
    s : Shortstring;
    bc : TBinChar;
    i,maxcnt : integer;

begin
   WriteLogLine(3,'Import Font-File: '+fn);
   assignfile(f,fn);
   reset(f);
   if pg=0 then maxcnt:=156 else maxcnt:=255;
   read(f,bc);
   // check for TI-Files
   if comparebyte(bc,bcTI,8)=0 then begin
     seek(f,16); // skip TI-FILES Header
     WriteLogLine(3,'TIFILES-Header: Skipping 128 bytes')
   end else
   // check for Magellan Charset-Files
   if comparebyte(bc,bcMG,4)=0 then begin
     seek(f,1); // skip TI-FILES Header
     WriteLogLine(3,'Magellan-Header: Skipping 8 bytes')
   end else seek(f,0);


   while (not eof(f)) and (cnt<=maxcnt) do begin
     read(f,bc);
     s:='';
     for i:=0 to 7 do begin
       s:=s+inttohex(bc[i]);
     end;
     WriteLogLine(5,'  '+inttostr(cnt)+': '+s);
     cset[pg,cnt].pat  := s;
     cset[pg,cnt].strt := cnt;
     cset[pg,cnt].size := 0;
     cset[pg,cnt].cx   := 0;
     cset[pg,cnt].cy   := 0;
     CallCharUpdate(pg,cnt);
     inc(cnt);
   end;
   closefile(f);
end;

procedure DumpFont(pg,cnt:integer;fn:shortstring);
var f : file of TBinChar;
    s : Shortstring;
    bc : TBinChar;
    i,ch,maxcnt : integer;

begin
   WriteLogLine(3,'Export Font-File: '+fn);
   assignfile(f,fn);
   rewrite(f);
   if cnt=32 then maxcnt:=127 else
     if pg=0 then maxcnt:=156 else maxcnt:=255;

   for ch:=cnt to maxcnt do begin
     s := cset[pg,ch].pat + '0000000000000000';
     for i:=0 to 7 do bc[i]:=hex2dec(copy(s,i*2+1,2));
     write(f,bc);
   end;
   closefile(f);
   WriteLogLine(3,'Font-File written. Characters #'+inttostr(cnt)+' - #'+inttostr(maxcnt));
end;

end.
