-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbgrajsonstyles.pas
484 lines (440 loc) · 13.4 KB
/
bgrajsonstyles.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
unit BGRAJSONStyles;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, BGRABitmap, BGRABitmapTypes, fpjson, Math, LCLType;
type
{ TBGRAJSONRoundRect }
TRoundRectangleStyle = (rrsRounded, rrsSquare, rrsBevel);
TMeasurementUnit = (muPixels, muPercent);
TBGRAJSONRoundRect = class
private
FBackgroundColor: TBGRAPixel;
FBorderBottomLeftStyle: TRoundRectangleStyle;
FBorderBottomRightStyle: TRoundRectangleStyle;
FBorderColor: TBGRAPixel;
FBorderTopLeftStyle: TRoundRectangleStyle;
FBorderTopRightStyle: TRoundRectangleStyle;
FBorderWidth: single;
FBorderXRadius: single;
FBorderXRadiusUnit: TMeasurementUnit;
FBorderYRadius: single;
FBorderYRadiusUnit: TMeasurementUnit;
FHeight: single;
FHeightUnit: TMeasurementUnit;
FStyle: string;
FWidth: single;
FBorderOptions: TRoundRectangleOptions;
FWidthUnit: TMeasurementUnit;
procedure SetBackgroundColor(AValue: TBGRAPixel);
procedure SetBorderBottomLeftStyle(AValue: TRoundRectangleStyle);
procedure SetBorderBottomRightStyle(AValue: TRoundRectangleStyle);
procedure SetBorderColor(AValue: TBGRAPixel);
procedure SetBorderTopLeftStyle(AValue: TRoundRectangleStyle);
procedure SetBorderTopRightStyle(AValue: TRoundRectangleStyle);
procedure SetBorderWidth(AValue: single);
procedure SetBorderXRadius(AValue: single);
procedure SetBorderXRadiusUnit(AValue: TMeasurementUnit);
procedure SetBorderYRadius(AValue: single);
procedure SetBorderYRadiusUnit(AValue: TMeasurementUnit);
procedure SetHeight(AValue: single);
procedure SetHeightUnit(AValue: TMeasurementUnit);
procedure SetStyle(AValue: string);
procedure SetWidth(AValue: single);
procedure SetWidthUnit(AValue: TMeasurementUnit);
protected
procedure ProcessJSON;
procedure ProcessBorderTopLeftStyle;
procedure ProcessBorderBottomLeftStyle;
procedure ProcessBorderTopRightStyle;
procedure ProcessBorderBottomRightStyle;
public
constructor Create;
public
procedure LoadFromFile(FileName: string);
public
procedure Draw(DestBitmap: TBGRABitmap; DestRect: TRect);
public
property Width: single read FWidth write SetWidth;
property Height: single read FHeight write SetHeight;
property WidthUnit: TMeasurementUnit read FWidthUnit write SetWidthUnit;
property HeightUnit: TMeasurementUnit read FHeightUnit write SetHeightUnit;
property BackgroundColor: TBGRAPixel read FBackgroundColor write SetBackgroundColor;
property BorderColor: TBGRAPixel read FBorderColor write SetBorderColor;
property BorderWidth: single read FBorderWidth write SetBorderWidth;
property BorderXRadius: single read FBorderXRadius write SetBorderXRadius;
property BorderYRadius: single read FBorderYRadius write SetBorderYRadius;
property BorderXRadiusUnit: TMeasurementUnit
read FBorderXRadiusUnit write SetBorderXRadiusUnit;
property BorderYRadiusUnit: TMeasurementUnit
read FBorderYRadiusUnit write SetBorderYRadiusUnit;
property BorderTopLeftStyle: TRoundRectangleStyle
read FBorderTopLeftStyle write SetBorderTopLeftStyle;
property BorderTopRightStyle: TRoundRectangleStyle
read FBorderTopRightStyle write SetBorderTopRightStyle;
property BorderBottomLeftStyle: TRoundRectangleStyle
read FBorderBottomLeftStyle write SetBorderBottomLeftStyle;
property BorderBottomRightStyle: TRoundRectangleStyle
read FBorderBottomRightStyle write SetBorderBottomRightStyle;
public
property Style: string read FStyle write SetStyle;
end;
implementation
procedure JSONReadSingle(propName: string; var jObj: TJSONObject; out floatVar: single);
var
jTmp: TJSONData;
begin
jTmp := jObj.FindPath(propName);
if Assigned(jTmp) then
floatVar := jTmp.AsFloat;
end;
procedure JSONReadColor(propName: string; var jObj: TJSONObject;
out colorVar: TBGRAPixel);
var
jTmp: TJSONData;
begin
jTmp := jObj.FindPath(propName);
if Assigned(jTmp) then
colorVar := StrToBGRA(jTmp.AsString);
end;
function JSONReadBorderStyle(propName: string; var jObj: TJSONObject;
out styleVar: TRoundRectangleStyle): boolean;
var
jTmp: TJSONData;
begin
jTmp := jObj.FindPath(propName);
Result := Assigned(jTmp);
if Result then
case jTmp.AsString of
'rounded': styleVar := rrsRounded;
'bevel': styleVar := rrsBevel;
'square': styleVar := rrsSquare;
else
styleVar := rrsSquare;
end;
end;
function JSONReadMeasurementUnit(propName: string; var jObj: TJSONObject;
out unitVar: TMeasurementUnit): boolean;
var
jTmp: TJSONData;
begin
jTmp := jObj.FindPath(propName);
Result := Assigned(jTmp);
if Result then
case jTmp.AsString of
'%': unitVar := muPercent;
'px': unitVar := muPixels;
else
unitVar := muPixels;
end;
end;
procedure JSONReadSingleMeasurement(propName: string; var jObj: TJSONObject;
out floatVar: single; out unitVar: TMeasurementUnit);
var
jTmp: TJSONData;
sTmp: string;
begin
jTmp := jObj.FindPath(propName);
if Assigned(jTmp) then
begin
sTmp := jTmp.AsString;
if Pos('px', sTmp) <> 0 then
begin
unitVar := muPixels;
sTmp := StringReplace(sTmp, 'px', '', [rfReplaceAll]);
end
else if Pos('%', sTmp) <> 0 then
begin
unitVar := muPercent;
sTmp := StringReplace(sTmp, '%', '', [rfReplaceAll]);
end
else
unitVar := muPixels;
floatVar := StrToFloat(sTmp);
end;
end;
{ TBGRAJSONRoundRect }
procedure TBGRAJSONRoundRect.SetBackgroundColor(AValue: TBGRAPixel);
begin
if FBackgroundColor = AValue then
Exit;
FBackgroundColor := AValue;
end;
procedure TBGRAJSONRoundRect.SetBorderBottomLeftStyle(AValue: TRoundRectangleStyle);
begin
if FBorderBottomLeftStyle = AValue then
Exit;
FBorderBottomLeftStyle := AValue;
ProcessBorderBottomLeftStyle;
end;
procedure TBGRAJSONRoundRect.SetBorderBottomRightStyle(AValue: TRoundRectangleStyle);
begin
if FBorderBottomRightStyle = AValue then
Exit;
FBorderBottomRightStyle := AValue;
ProcessBorderBottomRightStyle;
end;
procedure TBGRAJSONRoundRect.SetBorderColor(AValue: TBGRAPixel);
begin
if FBorderColor = AValue then
Exit;
FBorderColor := AValue;
end;
procedure TBGRAJSONRoundRect.SetBorderTopLeftStyle(AValue: TRoundRectangleStyle);
begin
if FBorderTopLeftStyle = AValue then
Exit;
FBorderTopLeftStyle := AValue;
ProcessBorderTopLeftStyle;
end;
procedure TBGRAJSONRoundRect.SetBorderTopRightStyle(AValue: TRoundRectangleStyle);
begin
if FBorderTopRightStyle = AValue then
Exit;
FBorderTopRightStyle := AValue;
ProcessBorderTopRightStyle;
end;
procedure TBGRAJSONRoundRect.SetBorderWidth(AValue: single);
begin
if FBorderWidth = AValue then
Exit;
FBorderWidth := AValue;
end;
procedure TBGRAJSONRoundRect.SetBorderXRadius(AValue: single);
begin
if FBorderXRadius = AValue then
Exit;
FBorderXRadius := AValue;
end;
procedure TBGRAJSONRoundRect.SetBorderXRadiusUnit(AValue: TMeasurementUnit);
begin
if FBorderXRadiusUnit = AValue then
Exit;
FBorderXRadiusUnit := AValue;
end;
procedure TBGRAJSONRoundRect.SetBorderYRadius(AValue: single);
begin
if FBorderYRadius = AValue then
Exit;
FBorderYRadius := AValue;
end;
procedure TBGRAJSONRoundRect.SetBorderYRadiusUnit(AValue: TMeasurementUnit);
begin
if FBorderYRadiusUnit = AValue then
Exit;
FBorderYRadiusUnit := AValue;
end;
procedure TBGRAJSONRoundRect.SetHeight(AValue: single);
begin
if FHeight = AValue then
Exit;
FHeight := AValue;
end;
procedure TBGRAJSONRoundRect.SetHeightUnit(AValue: TMeasurementUnit);
begin
if FHeightUnit = AValue then
Exit;
FHeightUnit := AValue;
end;
procedure TBGRAJSONRoundRect.SetStyle(AValue: string);
begin
if FStyle = AValue then
Exit;
FStyle := AValue;
ProcessJSON;
end;
procedure TBGRAJSONRoundRect.SetWidth(AValue: single);
begin
if FWidth = AValue then
Exit;
FWidth := AValue;
end;
procedure TBGRAJSONRoundRect.SetWidthUnit(AValue: TMeasurementUnit);
begin
if FWidthUnit = AValue then
Exit;
FWidthUnit := AValue;
end;
procedure TBGRAJSONRoundRect.ProcessJSON;
var
jObj: TJSONObject = nil;
begin
try
jObj := TJSONObject(GetJSON(FStyle));
{ Size }
JSONReadSingleMeasurement('width', jObj, FWidth, FWidthUnit);
JSONReadSingleMeasurement('height', jObj, FHeight, FHeightUnit);
{ Border }
JSONReadSingle('border-width', jObj, FBorderWidth);
JSONReadSingleMeasurement('border-x-radius', jObj, FBorderXRadius,
FBorderXRadiusUnit);
JSONReadSingleMeasurement('border-y-radius', jObj, FBorderYRadius,
FBorderYRadiusUnit);
JSONReadColor('border-color', jObj, FBorderColor);
if JSONReadBorderStyle('border-top-left-style', jObj, FBorderTopLeftStyle) then
ProcessBorderTopLeftStyle;
if JSONReadBorderStyle('border-top-right-style', jObj, FBorderTopRightStyle) then
ProcessBorderTopRightStyle;
if JSONReadBorderStyle('border-bottom-left-style', jObj, FBorderBottomLeftStyle) then
ProcessBorderBottomLeftStyle;
if JSONReadBorderStyle('border-bottom-right-style', jObj,
FBorderBottomRightStyle) then
ProcessBorderBottomRightStyle;
{ Background }
JSONReadColor('background-color', jObj, FBackgroundColor);
finally
if Assigned(jObj) then
jObj.Free;
end;
end;
procedure TBGRAJSONRoundRect.ProcessBorderTopLeftStyle;
begin
case FBorderTopLeftStyle of
TRoundRectangleStyle.rrsRounded:
begin
Exclude(FBorderOptions, rrTopLeftSquare);
Exclude(FBorderOptions, rrTopLeftBevel);
end;
TRoundRectangleStyle.rrsBevel:
begin
Exclude(FBorderOptions, rrTopLeftSquare);
Include(FBorderOptions, rrTopLeftBevel);
end;
TRoundRectangleStyle.rrsSquare:
begin
Include(FBorderOptions, rrTopLeftSquare);
Exclude(FBorderOptions, rrTopLeftBevel);
end;
end;
end;
procedure TBGRAJSONRoundRect.ProcessBorderBottomLeftStyle;
begin
case FBorderBottomLeftStyle of
TRoundRectangleStyle.rrsRounded:
begin
Exclude(FBorderOptions, rrBottomLeftSquare);
Exclude(FBorderOptions, rrBottomLeftBevel);
end;
TRoundRectangleStyle.rrsBevel:
begin
Exclude(FBorderOptions, rrBottomLeftSquare);
Include(FBorderOptions, rrBottomLeftBevel);
end;
TRoundRectangleStyle.rrsSquare:
begin
Include(FBorderOptions, rrBottomLeftSquare);
Exclude(FBorderOptions, rrBottomLeftBevel);
end;
end;
end;
procedure TBGRAJSONRoundRect.ProcessBorderTopRightStyle;
begin
case FBorderTopRightStyle of
TRoundRectangleStyle.rrsRounded:
begin
Exclude(FBorderOptions, rrTopRightSquare);
Exclude(FBorderOptions, rrTopRightBevel);
end;
TRoundRectangleStyle.rrsBevel:
begin
Exclude(FBorderOptions, rrTopRightSquare);
Include(FBorderOptions, rrTopRightBevel);
end;
TRoundRectangleStyle.rrsSquare:
begin
Include(FBorderOptions, rrTopRightSquare);
Exclude(FBorderOptions, rrTopRightBevel);
end;
end;
end;
procedure TBGRAJSONRoundRect.ProcessBorderBottomRightStyle;
begin
case FBorderBottomRightStyle of
TRoundRectangleStyle.rrsRounded:
begin
Exclude(FBorderOptions, rrBottomRightSquare);
Exclude(FBorderOptions, rrBottomRightBevel);
end;
TRoundRectangleStyle.rrsBevel:
begin
Exclude(FBorderOptions, rrBottomRightSquare);
Include(FBorderOptions, rrBottomRightBevel);
end;
TRoundRectangleStyle.rrsSquare:
begin
Include(FBorderOptions, rrBottomRightSquare);
Exclude(FBorderOptions, rrBottomRightBevel);
end;
end;
end;
constructor TBGRAJSONRoundRect.Create;
begin
FBackgroundColor := BGRAWhite;
FBorderColor := BGRABlack;
FWidth := 100;
FHeight := 100;
FWidthUnit := muPixels;
FHeightUnit := muPixels;
FBorderXRadius := 1;
FBorderYRadius := 1;
FBorderXRadiusUnit := muPixels;
FBorderYRadiusUnit := muPixels;
FBorderWidth := 1;
FBorderTopLeftStyle := TRoundRectangleStyle.rrsSquare;
FBorderTopRightStyle := TRoundRectangleStyle.rrsSquare;
FBorderBottomLeftStyle := TRoundRectangleStyle.rrsSquare;
FBorderBottomRightStyle := TRoundRectangleStyle.rrsSquare;
end;
procedure TBGRAJSONRoundRect.LoadFromFile(FileName: string);
var
s: TStringList = nil;
begin
if FileExists(FileName) then
begin
s := TStringList.Create;
try
s.LoadFromFile(FileName, TEncoding.UTF8);
Style := s.Text;
finally
s.Free;
end;
end;
end;
procedure TBGRAJSONRoundRect.Draw(DestBitmap: TBGRABitmap; DestRect: TRect);
var
halfBorderWidth: single;
BorderXRealRadius, BorderYRealRadius: single;
realWidth, realHeight: single;
begin
try
halfBorderWidth := max(BorderWidth / 2, 1);
case FWidthUnit of
muPercent: realWidth := Width * DestRect.Width / 100;
else
realWidth := Width;
end;
case FHeightUnit of
muPercent: realHeight := Height * DestRect.Height / 100;
else
realHeight := Height;
end;
case FBorderXRadiusUnit of
muPercent: BorderXRealRadius := BorderXRadius * realWidth / 100;
else
BorderXRealRadius := BorderXRadius;
end;
case FBorderXRadiusUnit of
muPercent: BorderYRealRadius := BorderYRadius * realHeight / 100;
else
BorderYRealRadius := BorderYRadius;
end;
DestBitmap.RoundRectAntialias(DestRect.Left + halfBorderWidth,
DestRect.Top + halfBorderWidth,
DestRect.Left + realWidth - halfBorderWidth, DestRect.Top +
realHeight - halfBorderWidth, BorderXRealRadius,
BorderYRealRadius, BorderColor,
BorderWidth, BackgroundColor, FBorderOptions);
finally
end;
end;
end.