summaryrefslogtreecommitdiff
path: root/unit1.pas
blob: a7e6ec70ad65dcfc1fb29a9aedf1f9df9db113eb (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Spin;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    FontDialog1: TFontDialog;
    Image1: TImage;
    ListBox1: TListBox;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    SpinEdit1: TSpinEdit;
    SpinEdit2: TSpinEdit;
    SpinEdit3: TSpinEdit;
    SpinEdit4: TSpinEdit;
    SpinEdit5: TSpinEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
    procedure SpinEdit2Change(Sender: TObject);
  private
    w,h,
    xMinMin,xMaxMax,
    yMinMin,yMaxMax,
    xMin,xMax,
    yMin,yMax:       longint;
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

uses math;

{ TForm1 }

procedure TForm1.SpinEdit1Change(Sender: TObject);
var
  c:               char;
  i,j,
  xOf,yOf:         longint;
  b:               boolean;
begin
  image1.canvas.font.size:=spinEdit1.value;
  h:=image1.canvas.textHeight('X');
  w:=image1.canvas.textWidth('X');
  xMin:=w div 2;
  xMax:=w div 2;
  yMin:=h div 2;
  yMax:=h div 2;
  xMinMin:=-(w div 2);
  xMaxMax:=w + (w div 2);
  yMinMin:=-(h div 2);
  yMaxMax:=h + (h div 2);
  image1.canvas.rectangle(-1,-1,image1.width+1,image1.height+1);
  for c:=#32 to #126 do begin
    xOf:=2*w*(ord(c) mod 16)+w;
    yOf:=2*h*(ord(c) div 16)+h;
    image1.canvas.textOut(xOf,yOf,c);
    if not (c in ['0'..'9','a'..'z','A'..'Z']) then
      continue;
    for i:=xMin downto xMinMin do begin
      b:=false;
      for j:=yMinMin to yMaxMax do
        b:=b or (image1.picture.bitmap.canvas.pixels[xOf+i,yOf+j]<>$ffffff);
      if not b then begin
        xMin:=min(xMin,i+1);
        break;
      end;
    end;
    for i:=xMax to xMaxMax do begin
      b:=false;
      for j:=yMinMin to yMaxMax do
        b:=b or (image1.picture.bitmap.canvas.pixels[xOf+i,yOf+j]<>$ffffff);
      if not b then begin
        xMax:=max(xMax,i-1);
        break;
      end;
    end;
    for i:=yMin downto yMinMin do begin
      b:=false;
      for j:=xMinMin to xMaxMax do
        b:=b or (image1.picture.bitmap.canvas.pixels[xOf+j,yOf+i]<>$ffffff);
      if not b then begin
        yMin:=min(yMin,i+1);
        break;
      end;
    end;
    for i:=yMax to yMaxMax do begin
      b:=false;
      for j:=xMinMin to xMaxMax do
        b:=b or (image1.picture.bitmap.canvas.pixels[xOf+j,yOf+i]<>$ffffff);
      if not b then begin
        yMax:=max(yMax,i-1);
        break;
      end;
    end;
  end;
  form1.caption:=intToStr(w)+'x'+intToStr(h)+' ('+intToStr(xMin)+'..'+intToStr(xMax)+'x'+intToStr(yMin)+'..'+intToStr(yMax)+')';
end;

procedure TForm1.SpinEdit2Change(Sender: TObject);
var
  c:           char;
  i,j,xOf,yOf: longint;
  warn:        byte;
  s:           string;
begin
  s:='';
  for c:=#32 to #126 do begin
    xOf:=2*w*(ord(c) mod 16)+w+spinEdit2.value;
    yOf:=2*h*(ord(c) div 16)+h+spinEdit3.value;
    warn:=0;
    for i:=xMinMin to -1 do
      for j:=yMinMin to yMaxMax do
        warn:=warn or byte(image1.picture.bitmap.canvas.pixels[xOf+i,yOf+j]<>$ffffff);
    if spinEdit4.value>0 then
      for i:=spinEdit4.value to xMaxMax do
        for j:=yMinMin to yMaxMax do
          warn:=warn or (byte(image1.picture.bitmap.canvas.pixels[xOf+i,yOf+j]<>$ffffff) shl 2);
    for i:=yMinMin to -1 do
      for j:=xMinMin to xMaxMax do
        warn:=warn or (byte(image1.picture.bitmap.canvas.pixels[xOf+j,yOf+i]<>$ffffff) shl 3);
    if spinEdit5.value>0 then
      for i:=spinEdit5.value to yMaxMax do
        for j:=xMinMin to xMaxMax do
          warn:=warn or (byte(image1.picture.bitmap.canvas.pixels[xOf+j,yOf+i]<>$ffffff) shl 1);
    if warn<>0 then
      s:=s+''''+c+''': '+intToStr(warn)+#13;
  end;
  if s<>'' then
    messageDlg(s,mtInformation,[mbOk],0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  f:           textFile;
  c,d:         char;
  i,j,xOf,yOf: longint;
begin
  if saveDialog1.execute then
    case listBox1.itemIndex of
      0:
        image1.picture.saveToFile(saveDialog1.fileName);
      1: begin
        assignFile(f,saveDialog1.fileName);
        rewrite(f);
        writeln(f,'char const symbols['+intToStr(126+1-32)+'*'+intToStr(spinEdit4.value)+'] = {');
        for c:=#32 to #126 do begin
          xOf:=2*w*(ord(c) mod 16)+w+spinEdit2.value;
          yOf:=2*h*(ord(c) div 16)+h+spinEdit3.value;
          write(f,'  ');
          for i:=0 to spinEdit4.value-1 do begin
            d:=#0;
            for j:=0 to min(7,spinEdit5.value) do
              if image1.picture.bitmap.canvas.pixels[xOf+i,yOf+j] and $ff<$80 then
                d:=char(ord(d) or (1 shl j));
            write(f,'0x'+intToHex(ord(d),2));
            if i<w-1 then
              write(f,',');
          end;
          if c<#126 then
            write(f,',')
          else
            write(f,' ');
          write(f,'  // ');
          case c of
            ' ': writeln(f,'space');
            '\': writeln(f,'backslash');
            else writeln(f,c);
          end{of case};
        end;
        writeln(f,'};');
        closeFile(f);
      end;
    end{of case};
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if openDialog1.execute then begin
    image1.picture.loadFromFile(openDialog1.fileName);
    spinEdit2.onChange(sender);
  end;
end;

procedure TForm1.FormResize(Sender: TObject);
var
  i: tImage;
begin
  i:=tImage.create(form1);
  i.top:=image1.top;
  i.left:=image1.left;
  i.onClick:=image1.onClick;
  i.width:=form1.clientWidth-i.left;
  i.height:=form1.clientHeight-i.top;
  i.parent:=image1.parent;
  i.canvas.font:=image1.canvas.font;
  image1.free;
  image1:=i;
  spinEdit1.onChange(sender);
end;

procedure TForm1.Image1Click(Sender: TObject);
begin
  if fontDialog1.execute then
    image1.canvas.font:=fontDialog1.font;
  spinEdit1.onChange(sender);
end;

end.