-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfontloader.pas
340 lines (288 loc) · 7.9 KB
/
fontloader.pas
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009-2012 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
Fonts class for the Mufasa Macro Library
}
unit fontloader;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,Graphics,bitmaps,
ocrutil,lclintf; // contains the actual `loading'
{
We will not give any access to actual indices.
}
type
TMFont = class(TObject)
public
Name: String;
Data: TOcrData;
constructor Create;
destructor Destroy; override;
function Copy: TMFont;
end;
{ TMFonts }
TMFonts = class(TObject)
private
Fonts: TList;
FPath: String;
Client : TObject;
function GetFontIndex(const Name: String): Integer;
function GetFontByIndex(Index : integer): TMfont;
procedure SetPath(const aPath: String);
function GetPath: String;
public
constructor Create(Owner : TObject);
destructor Destroy; override;
function GetFont(const Name: String): TOcrData;
function FreeFont(const Name: String): Boolean;
function LoadFont(const Name: String; Shadow: Boolean): boolean;
function LoadSystemFont(const SysFont : TFont; const FontName : string) : boolean;
function Copy(Owner : TObject): TMFonts;
function Count : integer;
property Path : string read GetPath write SetPath;
property Font[Index : integer]: TMfont read GetFontByIndex; default;
end;
implementation
uses
MufasaTypes, Client, strutils;
constructor TMFont.Create;
begin
inherited;
Name:='';
end;
destructor TMFont.Destroy;
begin
Name:='';
inherited;
end;
function TMFont.Copy: TMFont;
var
i, l, ll:integer;
begin
Result := TMFont.Create;
Result.Name := Self.Name;
Move(Self.Data.ascii[0], Result.Data.ascii[0], length(Self.Data.ascii) * SizeOf(TocrGlyphMetric));
l := Length(Self.Data.Pos);
SetLength(Result.Data.pos, l);
for i := 0 to l - 1 do
begin
ll := length(Self.Data.Pos[i]);
setlength(Result.Data.Pos[i], ll);
Move(Self.Data.Pos[i][0], Result.Data.Pos[i][0], ll*SizeOf(Integer));
end;
SetLength(Result.Data.pos_adj, length(Self.Data.pos_adj));
Move(Self.Data.pos_adj[0], Result.Data.pos_adj[0], length(Self.Data.pos_adj) * SizeOf(real));
l := Length(Self.Data.neg);
SetLength(Result.Data.neg, l);
for i := 0 to l - 1 do
begin
ll := length(Self.Data.neg[i]);
setlength(Result.Data.neg[i], ll);
Move(Self.Data.neg[i][0], Result.Data.neg[i][0], ll*SizeOf(Integer));
end;
SetLength(Result.Data.neg_adj, length(Self.Data.neg_adj));
Move(Self.Data.neg_adj[0], Result.Data.neg_adj[0], length(Self.Data.neg_adj) * SizeOf(real));
SetLength(Result.Data.map, length(Self.Data.map));
Move(Self.Data.map[0], Result.Data.map[0], length(Self.Data.map) * SizeOf(char));
Result.Data.Width := Self.Data.Width;
Result.Data.Height := Self.Data.Height;
Result.Data.inputs := Self.Data.inputs;
Result.Data.outputs := Self.Data.outputs;
Result.Data.max_height:= Self.Data.max_height;
Result.Data.max_width:= Self.Data.max_width;
end;
function TMFonts.GetFontByIndex(Index : integer): TMfont;
begin
// TODO: Check bounds?
result := TMfont(Fonts.Items[index]);
end;
constructor TMFonts.Create(Owner : TObject);
begin
inherited Create;
Fonts := TList.Create;
Client := Owner;
end;
destructor TMFonts.Destroy;
var
i:integer;
begin
for i := 0 to Fonts.Count - 1 do
TMFont(Fonts.Items[i]).Free;
Fonts.Free;
inherited;
end;
procedure TMFonts.SetPath(const aPath: String);
begin
FPath := aPath;
end;
function TMFonts.GetPath: String;
begin
Exit(FPath);
end;
function TMFonts.GetFontIndex(const Name: String): Integer;
var
i: integer;
begin
for i := 0 to Fonts.Count - 1 do
begin
if lowercase(Name) = lowercase(TMFont(Fonts.Items[i]).Name) then
Exit(i);
end;
raise Exception.Create('Font [' + Name + '] not found.');
Exit(-1);
end;
function TMFonts.GetFont(const Name: String): TOcrData;
var
i: integer;
begin
i := GetFontIndex(Name);
Exit(TMFont(Fonts.Items[i]).Data);
end;
function TMFonts.FreeFont(const Name: String): boolean;
var
i: integer;
begin
i := GetFontIndex(Name);
result := (i <> -1);
if result then
begin
TMFont(Fonts.Items[i]).Free;
Fonts.Delete(i);
end;
end;
function TMFonts.LoadFont(const Name: String; Shadow: Boolean): boolean;
var
f: TMFont;
CanonicalName, fontPath, n: String;
i: Integer;
FontFound: Boolean;
begin
Result := True;
CanonicalName := '';
fontPath := '';
FontFound := False;
// TODO: Use UTF8 here?
if not DirectoryExists(FPath + Name) then
begin
if not DirectoryExists(Name) then
begin
raise Exception.Create('LoadFont: Directory ' + Name + ' does not exist (either absolute or in FontPath');
Exit(False);
end
else
begin
FontFound := True;
fontPath := Name;
if Name[Length(Name)] = DS then
CanonicalName := ExtractFileDir(Name)
else
CanonicalName := ExtractFilePath(Name) + ExtractFileName(Name);
CanonicalName := system.Copy(CanonicalName, rpos(DS, CanonicalName) + 1, Length(CanonicalName) - rpos(DS, CanonicalName));
end;
Writeln('LoadFont debug, CanonicalName = ' + CanonicalName);
if not FontFound then
begin
raise Exception.Create('LoadFont: Directory ' + FPath + Name + ' does not exist.');
Exit(False);
end;
end else
begin
CanonicalName := Name;
fontPath := FPath + Name;
end;
n := CanonicalName;
if Shadow then
n := n + '_s';
FontFound := False;
for i := 0 to Fonts.Count - 1 do
begin
if lowercase(n) = lowercase(TMFont(Fonts.Items[i]).Name) then
begin
FontFound := True;
break;
end;
end;
if FontFound then
begin
TClient(Client).Writeln('Font ' + Name + ' already loaded as: ' + n);
Exit(False);
end;
f := TMFont.Create;
f.Name := CanonicalName;
if Shadow then
F.Name := F.Name + '_s';
f.Data := InitOCR(LoadGlyphMasks(fontPath + DS, Shadow));
Fonts.Add(f);
TClient(Client).Writeln('Loaded Font ' + f.Name);
end;
function TMFonts.LoadSystemFont(const SysFont: TFont; const FontName: string): boolean;
var
Masks : TocrGlyphMaskArray;
i,c : integer;
w,h : integer;
Bmp : TBitmap;
NewFont : TMFont;
MBmp : TMufasaBitmap;
begin
SetLength(Masks,255);
MBmp := TMufasaBitmap.Create;
Bmp := TBitmap.Create;
c := 0;
with Bmp.canvas do
begin
Font := SysFont;
Font.Color:= clWhite;
Font.Quality:= fqNonAntialiased;
Brush.Color:= clBlack;
Pen.Style:= psClear;
for i := 1 to 255 do
begin
GetTextSize(chr(i),w,h);
if (w<=0) or (h<=0) then
Continue;
Bmp.SetSize(w,h);
TextOut(0,0,chr(i));
MBmp.LoadFromTBitmap(bmp);
Masks[c] := LoadGlyphMask(MBmp,false,chr(i));
inc(c);
end;
end;
setlength(masks,c);
if c > 0 then
begin
NewFont := TMFont.Create;
NewFont.Name:= FontName;
NewFont.Data := InitOCR(masks);
Fonts.Add(NewFont);
result := true;
end;
bmp.free;
MBmp.free;
end;
function TMFonts.Copy(Owner : TObject): TMFonts;
var
i:integer;
begin
Result := TMFonts.Create(Owner);
Result.Path := FPath;
for i := 0 to Self.Fonts.Count -1 do
Result.Fonts.Add(TMFont(Self.Fonts.Items[i]).Copy());
end;
function TMFonts.Count: integer;
begin
result := Fonts.Count;
end;
end.