summaryrefslogtreecommitdiff
path: root/fonts/utilities/pkbbox/pkbbox.pas
blob: a9ea1d3c9502ecf07aac357669dac9f3687ddf1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Program PKtoSFP;

uses OpCrt,
     Clock,
     Files,
     MoreDos,
     MoreSys,
     Strings,
     OpString,
     Tp_Msgs,
     Sps_Cpyr,
     PkDcl,
     TexDcl,
     TfmDcl;

const
  Vers          = '0.2';
  BannerStr     = 'This is PKbbox vers '+Vers;
  ProgName      = 'PKbbox';

var
  DesignSize  : Real;
  DesignRatio : Real;

type
  CharRec = Record
              CC              : Byte;
              llx,lly,urx,ury : Integer;
            end;

var
  RC          : Integer;
  PkFont      : PkFontObj;
  PkChar      : PkCharObj;
  PkFn        : FileSpecStr;
  AfmFn       : FileSpecStr;
  Count       : Word;
  WorkReal    : Real;
  WorkString  : String;
  Chars       : Array[0..255] of CharRec;
  T           : Text;
  StdErr      : Text;
  Line,s      : String;
  I,err       : Integer;

  lc_Width    : Real;
  lc_Count    : Word;

  MaxDescender: LongInt;
  MaxAscender : LongInt;

  CharName    : String[12];
  HorizDpi    : Integer;
  VertDpi     : Integer;
  ScaleAdj    : Real;
  RemapSuccess: Boolean;
  Multiplier  : Real;
  llx,lly,urx,ury : Integer;

begin
  AssignCrt(StdErr);
  Rewrite(StdErr);

  Assign(Output,'');
  Rewrite(Output);

  If ParamCount < 2 then
    begin
      Writeln(StdErr,'Usage: PKBBOX pkfile afmfile > newafmfile');
      Halt(0);
    end;

  Writeln(StdErr,BannerStr);

  PkFn := ParamStr(1);
  AfmFn := ParamStr(2);

  PkFont.Init;
  PkFont.OpenPkFile(RC,PkFn);
  If RC <> 0 then
    begin
      Write(StdErr,ProgName,': cannot read PK file: ');
      If RC > 0 then
        Write(StdErr,TpErrMsg(RC))
      Else
        Write(StdErr,PkErrMsg(RC));
      Halt(1);
    end;

  DesignSize  := 1.0*PkFont.DesignSize/1048576.0;
  DesignRatio := 300.0/72.27*DesignSize;

  HorizDpi := Round(1.0*PkFont.Hppp/65536.0*72.27);
  VertDpi  := Round(1.0*PkFont.Vppp/65536.0*72.27);
  ScaleAdj := 1.0*HorizDpi/300.0;

  Multiplier := 1000.0 / DesignRatio;

  FillChar(Chars,SizeOf(Chars),$00);

  MaxAscender  := 0;
  MaxDescender := 0;
  lc_Width := 0.0;
  lc_Count := 0;
  PkChar.Init;
  PkChar.ReadPkChar(RC,PkFont);
  While RC = 0 do
    begin
      CharName := Pad(TexChar(Char(PkChar.CharacterCode)),6);

      MaxAscender  := Max(MaxAscender,PkChar.VOff);
      MaxDescender := Max(MaxDescender,PkChar.Height-PkChar.VOff);

      llx := 0;
      lly := Trunc((PkChar.Voff-PkChar.Height)*Multiplier);
      urx := Trunc(PkChar.Width*Multiplier);
      ury := Trunc(PkChar.Height*Multiplier) + lly;

      Chars[PkChar.CharacterCode].llx := llx;
      Chars[PkChar.CharacterCode].lly := lly;
      Chars[PkChar.CharacterCode].urx := urx;
      Chars[PkChar.CharacterCode].ury := ury;

      PkChar.Done;
      PkChar.Init;
      PkChar.ReadPkChar(RC,PkFont);
    end;

  If RC <> Err_PkNoMore then
    begin
      Write(StdErr,ProgName,': Error scanning PK file: ');
      If RC > 0 then
        Write(StdErr,TpErrMsg(RC))
      Else
        Write(StdErr,PkErrMsg(RC));
      Halt(1);
    end;

  PkFont.Done;

  Assign(T,ParamStr(2));
  {$I-} Reset(T); {$I+}
  RC := IoResult;

  If RC <> 0 then
    begin
      Write(StdErr,ProgName,': cannot read AFM file: ',TpErrMsg(RC));
      Halt(1);
    end;

  While Not Eof(T) do
    begin
      Readln(T,Line);
      S := WordN(Line,2,' ');
      Val(S,I,err);
      If (WordN(Line,1,' ') = 'C') and (err = 0) and (I >= -1) and (I <= 255) then
        begin
          If I = -1 then
            Writeln(Line,' B 0 0 0 0 ;')
          Else
            Writeln(Line,' B ',Chars[I].llx,' ',Chars[I].lly,' ',Chars[I].urx,' ',Chars[I].ury,' ;');
        end
      Else
        Writeln(Line);
    end;

  Close(T);

  Writeln('Done.');
end.