-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbgralazresource.pas
454 lines (398 loc) · 12.8 KB
/
bgralazresource.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program 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. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | mailedivando@gmail.com
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRALazResource;
{$i bgrabitmap.inc}{$H+}
interface
uses
Classes, SysUtils, {$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF}BGRATypes, BGRAMultiFileType;
type
{ TLazResourceEntry }
TLazResourceEntry = class(TMultiFileEntry)
private
procedure Serialize(ADestination: TStream);
protected
FName: utf8string;
FValueType: utf8string;
FContent: TStream;
function GetName: utf8string; override;
procedure SetName(AValue: utf8string); override;
function GetExtension: utf8string; override;
function GetFileSize: BGRAInt64; override;
public
constructor Create(AContainer: TMultiFileContainer; AName: utf8string; AValueType: utf8string; AContent: TStream);
destructor Destroy; override;
function CopyTo(ADestination: TStream): BGRAInt64; override;
end;
{ TFormDataEntry }
TFormDataEntry = class(TLazResourceEntry)
protected
FTextContent: TStream;
procedure RequireTextContent;
function GetExtension: utf8string; override;
function GetFileSize: BGRAInt64; override;
public
constructor Create(AContainer: TMultiFileContainer; AName: utf8string; ABinaryContent: TStream);
destructor Destroy; override;
function CopyTo(ADestination: TStream): BGRAInt64; override;
end;
{ TLazResourceContainer }
TLazResourceContainer = class(TMultiFileContainer)
protected
function CreateEntry(AName: utf8string; AExtension: utf8string; AContent: TStream): TMultiFileEntry; override;
public
procedure LoadFromStream(AStream: TStream); override;
procedure SaveToStream(ADestination: TStream); override;
end;
implementation
uses {$IFDEF FPC}LResources,{$ENDIF} BGRAUTF8;
{ TFormDataEntry }
procedure TFormDataEntry.RequireTextContent;
begin
if FTextContent = nil then
begin
FTextContent:= TMemoryStream.Create;
FContent.Position:= 0;
{$IFDEF FPC}//#
LRSObjectBinaryToText(FContent, FTextContent);
{$ENDIF}
end;
end;
function TFormDataEntry.GetExtension: utf8string;
begin
{$IFDEF FPC}
Result:= 'lfm';
{$ELSE}
Result:= 'dfm';
{$ENDIF}
end;
function TFormDataEntry.GetFileSize: BGRAInt64;
begin
RequireTextContent;
Result:= FTextContent.Size;
end;
constructor TFormDataEntry.Create(AContainer: TMultiFileContainer;
AName: utf8string; ABinaryContent: TStream);
begin
inherited Create(AContainer,AName,'FORMDATA',ABinaryContent);
end;
destructor TFormDataEntry.Destroy;
begin
FreeAndNil(FTextContent);
inherited Destroy;
end;
function TFormDataEntry.CopyTo(ADestination: TStream): BGRAInt64;
begin
RequireTextContent;
if FTextContent.Size = 0 then
result := 0
else
begin
FTextContent.Position:= 0;
result := ADestination.CopyFrom(FTextContent,FTextContent.Size);
end;
end;
{ TLazResourceEntry }
procedure TLazResourceEntry.Serialize(ADestination: TStream);
begin
FContent.Position := 0;
{$IFDEF FPC}//#
BinaryToLazarusResourceCode(FContent, ADestination, Name, FValueType);
{$ENDIF}
end;
function TLazResourceEntry.GetName: utf8string;
begin
Result:= FName;
end;
procedure TLazResourceEntry.SetName(AValue: utf8string);
begin
if AValue = FName then exit;
if Container.IndexOf(AVAlue, Extension) <> -1 then
raise Exception.Create('Name is already used for this extension');
FName := AValue;
end;
function TLazResourceEntry.GetExtension: utf8string;
begin
Result:= FValueType;
end;
function TLazResourceEntry.GetFileSize: BGRAInt64;
begin
Result:= FContent.Size;
end;
destructor TLazResourceEntry.Destroy;
begin
FreeAndNil(FContent);
inherited Destroy;
end;
constructor TLazResourceEntry.Create(AContainer: TMultiFileContainer; AName: utf8string; AValueType: utf8string;
AContent: TStream);
begin
inherited Create(AContainer);
FName := AName;
FValueType := UTF8UpperCase(AValueType);
FContent := AContent;
end;
function TLazResourceEntry.CopyTo(ADestination: TStream): BGRAInt64;
begin
if FContent.Size = 0 then
result := 0
else
begin
FContent.Position:= 0;
result := ADestination.CopyFrom(FContent, FContent.Size);
end;
end;
{ TLazResourceContainer }
procedure TLazResourceContainer.LoadFromStream(AStream: TStream);
const
entryStart = 'LazarusResources.Add(';
entryEnd = ');';
whiteSpace = [' ',#9,#10,#13,#26];
var
fileContent: String;
filePos : integer;
procedure SkipWhitespace;
begin
while (filePos <= length(fileContent)) and (fileContent[filePos] in whiteSpace) do inc(filePos);
end;
procedure SkipComma;
begin
SkipWhitespace;
if (filePos <= length(fileContent)) and (fileContent[filePos] = ',') then
inc(filePos)
else
raise Exception.Create('Comma expected');
end;
function ParseString(ignoreCommas: boolean): TStream;
var
expectPlus: boolean;
procedure AppendChar(c: {$IFDEF FPC}Char{$ELSE}AnsiChar{$ENDIF});
begin
result.WriteByte(ord(c));
end;
function ParseNumber: integer;
var numberStart, errPos: integer;
s: String;
begin
numberStart:= filePos;
if (filePos <= length(fileContent)) and (fileContent[filePos] = '$') then
begin
inc(filePos);
while (filePos <= length(fileContent)) and (fileContent[filePos] in['0'..'9','a'..'f','A'..'F']) do inc(filePos);
end else
begin
while (filePos <= length(fileContent)) and (fileContent[filePos] in['0'..'9']) do inc(filePos);
end;
s := copy(fileContent,numberStart,filePos-numberStart);
val(s, result, errPos);
if errPos <> 0 then
raise exception.Create('Invalid number "' + s + '"');
end;
function ParseStringPart: boolean;
var charCode: integer;
begin
SkipWhitespace;
if filePos <= length(fileContent) then
begin
if expectPlus then
if fileContent[filePos] <> '+' then
begin
result := false;
expectPlus := false;
exit;
end else
inc(filePos);
case fileContent[filePos] of
'+': raise exception.Create('Unexpected "+"');
'''': begin
inc(filePos);
while (filePos <= length(fileContent)) do
begin
if fileContent[filePos] = '''' then
begin
inc(filePos);
if (filePos <= length(fileContent)) and (fileContent[filePos] = '''') then
begin
AppendChar('''');
inc(filePos);
end
else break;
end else
if fileContent[filePos] in[#10,#13] then
raise Exception.Create('Unexpected end of line')
else
begin
AppendChar({$IFDEF FPC}Char{$ELSE}AnsiChar{$ENDIF}(fileContent[filePos]));
inc(filePos);
end;
end;
if (filePos <= length(fileContent)) and (fileContent[filePos] = '#') then
expectPlus := false
else
expectPlus := true;
result := true;
end;
'#': begin
inc(filePos);
charCode := ParseNumber;
if (charCode < 0) or (charCode > 255) then
raise exception.Create('Character code out of bounds');
AppendChar({$IFDEF FPC}Char{$ELSE}AnsiChar{$ENDIF}(chr(charCode)));
if (filePos <= length(fileContent)) and (fileContent[filePos] in['#','''']) then
expectPlus := false
else
expectPlus := true;
result := true;
end;
else
begin
result := false;
expectPlus := false;
end;
end;
end
else
begin
result := false;
expectPlus := false;
end;
end;
begin
result := TMemoryStream.Create;
expectPlus := false;
if not ParseStringPart then raise exception.Create('Expecting string');
repeat
if ignoreCommas then
begin
SkipWhitespace;
if (filePos <= length(fileContent)) and (fileContent[filePos] = ',') then
begin
inc(filePos);
expectPlus := false;
end;
end;
until not ParseStringPart;
end;
procedure ReadContent;
var
bytesRead: integer;
begin
setlength(fileContent,AStream.Size-AStream.Position);
bytesRead := AStream.Read(fileContent[1],length(fileContent));
setlength(fileContent, bytesRead);
filePos := 1;
end;
function StreamToUTF8String(AStream: TStream): utf8String;
begin
setlength(result, AStream.Size);
AStream.Position := 0;
AStream.Read(result[1], length(result));
AStream.Free;
end;
var
entryName: utf8string;
entryType: utf8string;
entryContent: TStream;
inArray: boolean;
begin
Clear;
ReadContent;
while filePos <= length(fileContent) do
begin
if (upcase(fileContent[filePos]) = upcase(entryStart[1])) and
(CompareText(copy(fileContent,filePos,length(entryStart)),entryStart)=0) then
begin
inc(filePos, length(entryStart));
entryName := StreamToUTF8String(ParseString(false));
SkipComma;
entryType := StreamToUTF8String(ParseString(false));
SkipComma;
SkipWhitespace;
if (filePos <= length(fileContent)) and (fileContent[filePos] = '[') then
begin
inArray := true;
inc(filePos);
end else
inArray := false;
entryContent := ParseString(inArray);
SkipWhitespace;
if inArray then
begin
if (filePos <= length(fileContent)) and (fileContent[filePos] = ']') then
inc(filePos)
else
raise exception.Create('Expecting "]"');
end;
if entryType = 'FORMDATA' then
AddEntry(TFormDataEntry.Create(self,entryName,entryContent))
else
AddEntry(TLazResourceEntry.Create(self,entryName,entryType,entryContent));
if (filePos+length(entryEnd)-1 <= length(fileContent)) and (CompareText(copy(fileContent,filePos,length(entryEnd)),entryEnd)=0) then
inc(filePos,length(entryEnd))
else
raise exception.Create('Expecting "'+entryEnd+'"');
end else
if fileContent[filePos] in whiteSpace then
inc(filePos)
else
raise exception.Create('Unexpected character "'+fileContent[filePos]+'"');
end;
end;
function TLazResourceContainer.CreateEntry(AName: utf8string; AExtension: utf8string;
AContent: TStream): TMultiFileEntry;
var
binContent: TMemoryStream;
begin
if UTF8CompareText(AExtension,'lfm')=0 then
begin
binContent := TMemoryStream.Create;
try
AContent.Position:= 0;
{$IFDEF FPC}//#
LRSObjectTextToBinary(AContent, binContent);
{$ENDIF}
result := TFormDataEntry.Create(self,AName,binContent);
except
on ex:Exception do
begin
binContent.Free;
result := nil;
end;
end;
AContent.Free;
end
else
result := TLazResourceEntry.Create(self,AName,UTF8UpperCase(AExtension),AContent);
end;
procedure TLazResourceContainer.SaveToStream(ADestination: TStream);
var
i: Integer;
begin
for i := 0 to Count-1 do
TLazResourceEntry(Entry[i]).Serialize(ADestination);
end;
end.