forked from edivando-fpc/BGRABitmap
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bgrafreetype.pas
770 lines (685 loc) · 25.4 KB
/
bgrafreetype.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
{ ***************************************************************************
* *
* 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 BGRAFreeType;
{
Font rendering units : BGRAText, BGRATextFX, BGRAVectorize, BGRAFreeType
This units provide a font renderer with FreeType fonts, using the integrated FreeType font engine in Lazarus.
The simplest way to render effects is to use TBGRAFreeTypeFontRenderer class.
To do this, create an instance of this class and assign it to a TBGRABitmap.FontRenderer property. Now functions
to draw text like TBGRABitmap.TextOut will use the chosen renderer.
>> Note that you need to define the default FreeType font collection
>> using EasyLazFreeType unit.
To set the effects, keep a variable containing
the TBGRAFreeTypeFontRenderer class and modify ShadowVisible and other effects parameters. The FontHinted property
allows you to choose if the font is snapped to pixels to make it more readable.
TBGRAFreeTypeDrawer class is the class that provides basic FreeType drawing
by deriving the TFreeTypeDrawer type. You can use it directly, but it is not
recommended, because there are less text layout parameters. However, it is
necessary if you want to create TBGRATextEffect objects using FreeType fonts.
}
interface
{$i bgrabitmap.inc}{$H+}
uses
Types, Classes, SysUtils, BGRATypes, {$IFNDEF FPC} GraphType,{$ENDIF} BGRAGraphics, BGRABitmapTypes, {$IFDEF FPC}EasyLazFreeType,{$ENDIF}
BGRACustomTextFX, FPImage, BGRAPhongTypes;
type
TBGRAFreeTypeDrawer = class;
//this is the class to assign to FontRenderer property of TBGRABitmap
{ TBGRAFreeTypeFontRenderer }
TBGRAFreeTypeFontRenderer = class(TBGRACustomFontRenderer)
private
FDrawer: TBGRAFreeTypeDrawer;
FFont: TFreeTypeFont;
function GetCollection: TCustomFreeTypeFontCollection;
function GetDrawer(ASurface: TBGRACustomBitmap): TBGRAFreeTypeDrawer;
function GetShaderLightPosition: TPoint;
procedure SetShaderLightPosition(AValue: TPoint);
protected
FShaderOwner: boolean;
FShader: TCustomPhongShading;
procedure UpdateFont;
procedure Init;
procedure TextOutAnglePatch(ADest: TBGRACustomBitmap; x, y: single; orientation: integer; s: string;
c: TBGRAPixel; tex: IBGRAScanner; align: TAlignment);
public
FontHinted: boolean;
ShaderActive: boolean;
ShadowVisible: boolean;
ShadowColor: TBGRAPixel;
ShadowRadius: integer;
ShadowOffset: TPoint;
ShadowQuality: TRadialBlurType;
OutlineColor: TBGRAPixel;
OutlineVisible,OuterOutlineOnly: boolean;
OutlineTexture: IBGRAScanner;
constructor Create; overload;
constructor Create(AShader: TCustomPhongShading; AShaderOwner: boolean); overload;
function GetFontPixelMetric: TFontPixelMetric; override;
procedure TextOutAngle(ADest: TBGRACustomBitmap; x, y: single; orientation: integer; s: string; c: TBGRAPixel; align: TAlignment); overload; override;
procedure TextOutAngle(ADest: TBGRACustomBitmap; x, y: single; orientation: integer; s: string; texture: IBGRAScanner; align: TAlignment); overload; override;
procedure TextOut(ADest: TBGRACustomBitmap; x, y: single; s: string; texture: IBGRAScanner; align: TAlignment); overload; override;
procedure TextOut(ADest: TBGRACustomBitmap; x, y: single; s: string; c: TBGRAPixel; align: TAlignment); overload; override;
procedure TextRect(ADest: TBGRACustomBitmap; ARect: TRect; x, y: integer; s: string; style: TTextStyle; c: TBGRAPixel); overload; override;
procedure TextRect(ADest: TBGRACustomBitmap; ARect: TRect; x, y: integer; s: string; style: TTextStyle; texture: IBGRAScanner); overload; override;
function TextSize(s: string): TSize; overload; override;
function TextSize(sUTF8: string; AMaxWidth: integer; {%H-}ARightToLeft: boolean): TSize; overload; override;
function TextFitInfo(sUTF8: string; AMaxWidth: integer): integer; override;
destructor Destroy; override;
property Collection: TCustomFreeTypeFontCollection read GetCollection;
property ShaderLightPosition: TPoint read GetShaderLightPosition write SetShaderLightPosition;
end;
{ TBGRAFreeTypeDrawer }
TBGRAFreeTypeDrawer = class(TFreeTypeDrawer)
private
FMask: TBGRACustomBitmap;
FColor: TBGRAPixel;
FInCreateTextEffect: boolean;
procedure RenderDirectly(x, y, tx: integer; data: pointer);
procedure RenderDirectlyClearType(x, y, tx: integer; data: pointer);
function ShadowActuallyVisible :boolean;
function OutlineActuallyVisible: boolean;
function ShaderActuallyActive : boolean;
public
Destination: TBGRACustomBitmap;
ClearTypeRGBOrder: boolean;
Texture: IBGRAScanner;
Shader: TCustomPhongShading;
ShaderActive: boolean;
ShadowVisible: boolean;
ShadowColor: TBGRAPixel;
ShadowRadius: integer;
ShadowOffset: TPoint;
ShadowQuality: TRadialBlurType;
OutlineColor: TBGRAPixel;
OutlineVisible,OuterOutlineOnly: boolean;
OutlineTexture: IBGRAScanner;
constructor Create(ADestination: TBGRACustomBitmap);
procedure DrawText(AText: string; AFont: TFreeTypeRenderableFont; x,y: single; AColor: TFPColor); overload; override;
procedure DrawText(AText: string; AFont: TFreeTypeRenderableFont; x,y: single; AColor: TBGRAPixel); overload;
procedure DrawText(AText: string; AFont: TFreeTypeRenderableFont; x,y: single; AColor: TBGRAPixel; AAlign: TFreeTypeAlignments); overload;
{ If this code does not compile, you probably have an older version of Lazarus. To fix the problem,
go into "bgrabitmap.inc" and comment the compiler directives }
{$IFDEF BGRABITMAP_USE_LCL12}
procedure DrawTextWordBreak(AText: string; AFont: TFreeTypeRenderableFont; x, y, AMaxWidth: Single; AColor: TBGRAPixel; AAlign: TFreeTypeAlignments); overload;
procedure DrawTextRect(AText: string; AFont: TFreeTypeRenderableFont; X1,Y1,X2,Y2: Single; AColor: TBGRAPixel; AAlign: TFreeTypeAlignments); overload;
{$ENDIF}
{$IFDEF BGRABITMAP_USE_LCL15}
procedure DrawGlyph(AGlyph: integer; AFont: TFreeTypeRenderableFont; x,y: single; AColor: TFPColor); overload; override;
procedure DrawGlyph(AGlyph: integer; AFont: TFreeTypeRenderableFont; x,y: single; AColor: TBGRAPixel); overload;
procedure DrawGlyph(AGlyph: integer; AFont: TFreeTypeRenderableFont; x,y: single; AColor: TBGRAPixel; AAlign: TFreeTypeAlignments); overload;
{$ENDIF}
function CreateTextEffect(AText: string; AFont: TFreeTypeRenderableFont): TBGRACustomTextEffect;
destructor Destroy; override;
end;
implementation
uses BGRABlend, Math, BGRATransform;
{ TBGRAFreeTypeFontRenderer }
function TBGRAFreeTypeFontRenderer.GetCollection: TCustomFreeTypeFontCollection;
begin
result := EasyLazFreeType.FontCollection;
end;
function TBGRAFreeTypeFontRenderer.GetDrawer(ASurface: TBGRACustomBitmap): TBGRAFreeTypeDrawer;
begin
result := FDrawer;
result.ShadowColor := ShadowColor;
result.ShadowOffset := ShadowOffset;
result.ShadowRadius := ShadowRadius;
result.ShadowVisible := ShadowVisible;
result.ShadowQuality := ShadowQuality;
result.ClearTypeRGBOrder := FontQuality <> fqFineClearTypeBGR;
result.Destination := ASurface;
result.OutlineColor := OutlineColor;
result.OutlineVisible := OutlineVisible;
result.OuterOutlineOnly := OuterOutlineOnly;
result.OutlineTexture := OutlineTexture;
if ShaderActive then result.Shader := FShader
else result.Shader := nil;
end;
function TBGRAFreeTypeFontRenderer.GetShaderLightPosition: TPoint;
begin
if FShader = nil then
result := point(0,0)
else
result := FShader.LightPosition;
end;
procedure TBGRAFreeTypeFontRenderer.SetShaderLightPosition(AValue: TPoint);
begin
if FShader <> nil then
FShader.LightPosition := AValue;
end;
procedure TBGRAFreeTypeFontRenderer.UpdateFont;
var fts: TFreeTypeStyles;
filename: string;
begin
fts := [];
if fsBold in FontStyle then fts := fts +[ftsBold];
if fsItalic in FontStyle then fts := fts +[ftsItalic];
try
filename := FontName;
{$IFDEF BGRABITMAP_USE_LCL12}
FFont.SetNameAndStyle(filename,fts);
{$ELSE}
FFont.Name := filename;
FFont.Style := fts;
{$ENDIF}
except
on ex: exception do
begin
end;
end;
if FontEmHeight >= 0 then
FFont.SizeInPixels := FontEmHeight
else
FFont.LineFullHeight := -FontEmHeight;
case FontQuality of
fqSystem:
begin
FFont.Quality := grqMonochrome;
FFont.ClearType := false;
end;
fqSystemClearType:
begin
FFont.Quality:= grqLowQuality;
FFont.ClearType:= true;
end;
fqFineAntialiasing:
begin
FFont.Quality:= grqHighQuality;
FFont.ClearType:= false;
end;
fqFineClearTypeRGB,fqFineClearTypeBGR:
begin
FFont.Quality:= grqHighQuality;
FFont.ClearType:= true;
end;
end;
FFont.Hinted := FontHinted;
{$IFDEF BGRABITMAP_USE_LCL12}
FFont.StrikeOutDecoration := fsStrikeOut in FontStyle;
FFont.UnderlineDecoration := fsUnderline in FontStyle;
{$ENDIF}
end;
procedure TBGRAFreeTypeFontRenderer.Init;
begin
ShaderActive := true;
FDrawer := TBGRAFreeTypeDrawer.Create(nil);
FFont := TFreeTypeFont.Create;
FontHinted:= True;
ShadowColor := BGRABlack;
ShadowVisible := false;
ShadowOffset := Point(5,5);
ShadowRadius := 5;
ShadowQuality:= rbFast;
end;
procedure TBGRAFreeTypeFontRenderer.TextOutAnglePatch(ADest: TBGRACustomBitmap;
x, y: single; orientation: integer; s: string; c: TBGRAPixel;
tex: IBGRAScanner; align: TAlignment);
const orientationToDeg = -0.1;
var
temp: TBGRACustomBitmap;
coord: TPointF;
angleDeg: single;
OldOrientation: integer;
filter: TResampleFilter;
OldFontQuality: TBGRAFontQuality;
begin
OldOrientation := FontOrientation;
FontOrientation:= 0;
OldFontQuality := FontQuality;
if FontQuality in[fqFineClearTypeRGB,fqFineClearTypeBGR] then FontQuality:= fqFineAntialiasing
else if FontQuality = fqSystemClearType then FontQuality:= fqSystem;
temp := BGRABitmapFactory.Create;
with TextSize(s) do
temp.SetSize(cx,cy);
temp.FillTransparent;
if tex<>nil then
TextOut(temp,0,0, s, tex, taLeftJustify)
else
TextOut(temp,0,0, s, c, taLeftJustify);
orientation:= orientation mod 3600;
if orientation < 0 then orientation := orientation +3600;
angleDeg := orientation * orientationToDeg;
coord := PointF(x,y);
case align of
taRightJustify: coord := coord - AffMatrixMult(AffineMatrixRotationDeg(angleDeg),PointF(temp.Width,0));
taCenter: coord := coord - AffMatrixMult(AffineMatrixRotationDeg(angleDeg),PointF(temp.Width,0))*0.5;
end;
case orientation of
0,900,1800,2700: filter := rfBox;
else filter := rfCosine;
end;
ADest.PutImageAngle(coord.x,coord.y, temp, angleDeg, filter);
temp.Free;
FontOrientation:= OldOrientation;
FontQuality:= OldFontQuality;
end;
constructor TBGRAFreeTypeFontRenderer.Create;
begin
Init;
end;
constructor TBGRAFreeTypeFontRenderer.Create(AShader: TCustomPhongShading;
AShaderOwner: boolean);
begin
Init;
FShader := AShader;
FShaderOwner := AShaderOwner;
end;
function TBGRAFreeTypeFontRenderer.GetFontPixelMetric: TFontPixelMetric;
begin
UpdateFont;
result.Baseline := round(FFont.Ascent);
result.CapLine:= round(FFont.Ascent*0.2);
result.DescentLine:= round(FFont.Ascent+FFont.Descent);
result.Lineheight := round(FFont.LineFullHeight);
result.xLine := round(FFont.Ascent*0.45);
result.Defined := True;
end;
procedure TBGRAFreeTypeFontRenderer.TextOutAngle(ADest: TBGRACustomBitmap; x,
y: single; orientation: integer; s: string; c: TBGRAPixel; align: TAlignment);
begin
TextOutAnglePatch(ADest, x,y, orientation, s, c, nil, align);
{procedure TForm1.TextOutAnglePatch(ADest: TBGRABitmap;
x, y: single; orientationTenthDegCCW: integer;
s: string; c: TBGRAPixel; AAlign: TAlignment; AResampleFilter: TResampleFilter);
const orientationToDeg = -0.1;
var
temp: TBGRABitmap;
coord: TPointF;
angleDeg: single;
begin
temp := TBGRABitmap.Create;
ADest.CopyPropertiesTo(temp);
temp.FontOrientation := 0;
with temp.TextSize(s) do
temp.SetSize(cx,cy);
temp.FillTransparent;
+
temp.TextOut(0,0, s, c);
angleDeg := orientationTenthDegCCW * orientationToDeg;
coord := PointF(x,y);
case AAlign of
taRightJustify: coord := coord - AffineMatrixRotationDeg(angleDeg)*PointF(temp.Width,0);
taCenter: coord := coord - AffineMatrixRotationDeg(angleDeg)*PointF(temp.Width,0)*0.5;
end;
ADest.PutImageAngle(coord.x,coord.y, temp, angleDeg, rfBox);
temp.Free;
end; }
end;
procedure TBGRAFreeTypeFontRenderer.TextOutAngle(ADest: TBGRACustomBitmap; x,
y: single; orientation: integer; s: string; texture: IBGRAScanner;
align: TAlignment);
begin
TextOutAnglePatch(ADest, x,y, orientation, s, BGRAPixelTransparent, texture, align);
end;
procedure TBGRAFreeTypeFontRenderer.TextOut(ADest: TBGRACustomBitmap; x,
y: single; s: string; texture: IBGRAScanner; align: TAlignment);
begin
FDrawer.Texture := texture;
TextOut(ADest,x,y,s,BGRAWhite,align);
FDrawer.Texture := nil;
end;
procedure TBGRAFreeTypeFontRenderer.TextOut(ADest: TBGRACustomBitmap; x,
y: single; s: string; c: TBGRAPixel; align: TAlignment);
var
ftaAlign: TFreeTypeAlignments;
begin
UpdateFont;
ftaAlign:= [ftaTop];
case align of
taLeftJustify: ftaAlign := ftaAlign +[ftaLeft];
taCenter: ftaAlign := ftaAlign +[ftaCenter];
taRightJustify: ftaAlign := ftaAlign +[ftaRight];
end;
GetDrawer(ADest).DrawText(s,FFont,x,y,BGRAToFPColor(c),ftaAlign);
end;
procedure TBGRAFreeTypeFontRenderer.TextRect(ADest: TBGRACustomBitmap;
ARect: TRect; x, y: integer; s: string; style: TTextStyle; c: TBGRAPixel);
var align: TFreeTypeAlignments;
intersectedClip,previousClip: TRect;
begin
previousClip := ADest.ClipRect;
if style.Clipping then
begin
intersectedClip := rect(0,0,0,0);
if not IntersectRect(intersectedClip, previousClip, ARect) then exit;
ADest.ClipRect := intersectedClip;
end;
UpdateFont;
align := [];
case style.Alignment of
taCenter: begin ARect.Left := x; align := align +[ftaCenter]; end;
taRightJustify: begin ARect.Left := x; align := align +[ftaRight]; end;
else
align := align +[ftaLeft];
end;
case style.Layout of
{$IFDEF BGRABITMAP_USE_LCL12}
tlCenter: begin ARect.Top := y; align := align +[ftaVerticalCenter]; end;
{$ENDIF}
tlBottom: begin ARect.top := y; align := align +[ftaBottom]; end;
else align := align +[ftaTop];
end;
try
{$IFDEF BGRABITMAP_USE_LCL12}
if style.Wordbreak then
GetDrawer(ADest).DrawTextRect(s, FFont, ARect.Left,ARect.Top,ARect.Right,ARect.Bottom,BGRAToFPColor(c),align)
else
{$ENDIF}
begin
case style.Layout of
tlCenter: y := (ARect.Top+ARect.Bottom) div 2;
tlBottom: y := ARect.Bottom;
else
y := ARect.Top;
end;
case style.Alignment of
taLeftJustify: GetDrawer(ADest).DrawText(s,FFont,ARect.Left,y,BGRAToFPColor(c),align);
taCenter: GetDrawer(ADest).DrawText(s,FFont,(ARect.Left+ARect.Right-1) div 2,y,BGRAToFPColor(c),align);
taRightJustify: GetDrawer(ADest).DrawText(s,FFont,ARect.Right,y,BGRAToFPColor(c),align);
end;
end;
finally
if style.Clipping then
ADest.ClipRect := previousClip;
end;
end;
procedure TBGRAFreeTypeFontRenderer.TextRect(ADest: TBGRACustomBitmap;
ARect: TRect; x, y: integer; s: string; style: TTextStyle;
texture: IBGRAScanner);
begin
FDrawer.Texture := texture;
TextRect(ADest,ARect,x,y,s,style,BGRAWhite);
FDrawer.Texture := nil;
end;
function TBGRAFreeTypeFontRenderer.TextSize(s: string): TSize;
begin
UpdateFont;
result.cx := round(FFont.TextWidth(s));
result.cy := round(FFont.LineFullHeight);
end;
function TBGRAFreeTypeFontRenderer.TextSize(sUTF8: string; AMaxWidth: integer;
ARightToLeft: boolean): TSize;
var
remains: string;
w,h,totalH: single;
begin
UpdateFont;
result.cx := 0;
totalH := 0;
h := FFont.LineFullHeight;
repeat
FFont.SplitText(sUTF8, AMaxWidth, remains);
w := FFont.TextWidth(sUTF8);
if round(w)>result.cx then result.cx := round(w);
totalH := totalH +totalH +h;
sUTF8 := remains;
until remains = '';
result.cy := ceil(totalH);
end;
function TBGRAFreeTypeFontRenderer.TextFitInfo(sUTF8: string; AMaxWidth: integer): integer;
var
remains: string;
begin
UpdateFont;
FFont.SplitText(sUTF8, AMaxWidth, remains);
result := length(sUTF8);
end;
destructor TBGRAFreeTypeFontRenderer.Destroy;
begin
FDrawer.Free;
FFont.Free;
if FShaderOwner then FShader.Free;
inherited Destroy;
end;
{ TBGRAFreeTypeDrawer }
procedure TBGRAFreeTypeDrawer.RenderDirectly( x,y,tx: integer;
data: pointer );
var psrc: pbyte;
pdest: PBGRAPixel;
c: TBGRAPixel;
begin
if Destination <> nil then
begin
//ensure rendering in bounds
if (y < 0) or (y >= Destination.height) or (x < 0) or (x > Destination.width-tx) then exit;
psrc := pbyte(data);
pdest := Destination.ScanLine[y]+x;
if Texture = nil then
begin
c := FColor;
while tx > 0 do
begin
DrawPixelInlineWithAlphaCheck(pdest,c,psrc^);
inc(psrc);
inc(pdest);
dec(tx);
end;
end else
begin
Texture.ScanMoveTo(x,y);
while tx > 0 do
begin
DrawPixelInlineWithAlphaCheck(pdest,Texture.ScanNextPixel,psrc^);
inc(psrc);
inc(pdest);
dec(tx);
end;
end;
end;
end;
procedure TBGRAFreeTypeDrawer.RenderDirectlyClearType(x, y, tx: integer; data: pointer);
var xb: integer;
psrc: pbyte;
pdest: PBGRAPixel;
begin
if Destination <> nil then
begin
tx := tx div 3;
if tx=0 then exit;
if (FMask <> nil) and (FMask.Width <> tx) then
FMask.SetSize(tx,1)
else if FMask = nil then FMask := BGRABitmapFactory.create(tx,1);
pdest := FMask.Data;
psrc := pbyte(data);
pdest^.red := (psrc^ + psrc^ + (psrc+1)^) div 3;
pdest^.green := (psrc^+ (psrc+1)^ + (psrc+2)^) div 3;
if tx > 1 then
pdest^.blue := ((psrc+1)^ + (psrc+2)^ + (psrc+3)^) div 3
else
pdest^.blue := ((psrc+1)^ + (psrc+2)^ + (psrc+2)^) div 3;
inc(pdest);
inc(psrc,3);
for xb := 1 to tx-2 do
begin
pdest^.red := ((psrc-1)^+ psrc^ + (psrc+1)^) div 3;
pdest^.green := (psrc^+ (psrc+1)^ + (psrc+2)^) div 3;
pdest^.blue := ((psrc+1)^ + (psrc+2)^ + (psrc+3)^) div 3;
inc(pdest);
inc(psrc,3);
end;
if tx > 1 then
begin
pdest^.red := ((psrc-1)^+ psrc^ + (psrc+1)^) div 3;
pdest^.green := (psrc^+ (psrc+1)^ + (psrc+2)^) div 3;
pdest^.blue := ((psrc+1)^ + (psrc+2)^ + (psrc+2)^) div 3;
end;
BGRAFillClearTypeRGBMask(Destination,x div 3,y,FMask,FColor,Texture,ClearTypeRGBOrder);
end;
end;
function TBGRAFreeTypeDrawer.ShadowActuallyVisible: boolean;
begin
result := ShadowVisible and (ShadowColor.alpha <> 0);
end;
function TBGRAFreeTypeDrawer.OutlineActuallyVisible: boolean;
begin
result := ((OutlineTexture <> nil) or (OutlineColor.alpha <> 0)) and OutlineVisible;
end;
function TBGRAFreeTypeDrawer.ShaderActuallyActive: boolean;
begin
result := (Shader <> nil) and ShaderActive;
end;
constructor TBGRAFreeTypeDrawer.Create(ADestination: TBGRACustomBitmap);
begin
Destination := ADestination;
ClearTypeRGBOrder:= true;
ShaderActive := true;
ShadowQuality:= rbFast;
end;
procedure TBGRAFreeTypeDrawer.DrawText(AText: string;
AFont: TFreeTypeRenderableFont; x, y: single; AColor: TFPColor);
var fx: TBGRACustomTextEffect;
procedure DoOutline;
begin
if OutlineActuallyVisible then
begin
if OutlineTexture <> nil then
fx.DrawOutline(Destination,round(x),round(y), OutlineTexture)
else
fx.DrawOutline(Destination,round(x),round(y), OutlineColor);
end;
end;
begin
if not FInCreateTextEffect and (ShadowActuallyVisible or OutlineActuallyVisible or ShaderActuallyActive) then
begin
fx := CreateTextEffect(AText, AFont);
fx.ShadowQuality := ShadowQuality;
y := y - AFont.Ascent;
if ShadowActuallyVisible then fx.DrawShadow(Destination, round(x+ShadowOffset.X),round(y+ShadowOffset.Y), ShadowRadius, ShadowColor);
if OuterOutlineOnly then DoOutline;
if texture <> nil then
begin
if ShaderActuallyActive then
fx.DrawShaded(Destination,floor(x),floor(y), Shader, round(fx.TextSize.cy*0.05), texture)
else
fx.Draw(Destination,round(x),round(y), texture);
end else
begin
if ShaderActuallyActive then
fx.DrawShaded(Destination,floor(x),floor(y), Shader, round(fx.TextSize.cy*0.05), FPColorToBGRA(AColor))
else
fx.Draw(Destination,round(x),round(y), FPColorToBGRA(AColor));
end;
if not OuterOutlineOnly then DoOutline;
fx.Free;
end else
begin
FColor := FPColorToBGRA(AColor);
if AFont.ClearType then
AFont.RenderText(AText, x, y, Destination.ClipRect, {$IFDEF OBJ}@{$ENDIF}RenderDirectlyClearType)
else
AFont.RenderText(AText, x, y, Destination.ClipRect, {$IFDEF OBJ}@{$ENDIF}RenderDirectly);
end;
end;
procedure TBGRAFreeTypeDrawer.DrawText(AText: string;
AFont: TFreeTypeRenderableFont; x, y: single; AColor: TBGRAPixel);
begin
DrawText(AText, AFont, x,y, BGRAToFPColor(AColor));
end;
procedure TBGRAFreeTypeDrawer.DrawText(AText: string;
AFont: TFreeTypeRenderableFont; x, y: single; AColor: TBGRAPixel;
AAlign: TFreeTypeAlignments);
begin
DrawText(AText, AFont, x,y, BGRAToFPColor(AColor), AAlign);
end;
{$IFDEF BGRABITMAP_USE_LCL12}
procedure TBGRAFreeTypeDrawer.DrawTextWordBreak(AText: string;
AFont: TFreeTypeRenderableFont; x, y, AMaxWidth: Single; AColor: TBGRAPixel;
AAlign: TFreeTypeAlignments);
begin
DrawTextWordBreak(AText,AFont,x,y,AMaxWidth,BGRAToFPColor(AColor),AAlign);
end;
procedure TBGRAFreeTypeDrawer.DrawTextRect(AText: string;
AFont: TFreeTypeRenderableFont; X1, Y1, X2, Y2: Single; AColor: TBGRAPixel;
AAlign: TFreeTypeAlignments);
begin
DrawTextRect(AText,AFont,X1,Y1,X2,Y2,BGRAToFPColor(AColor),AAlign);
end;
{$ENDIF}
{$IFDEF BGRABITMAP_USE_LCL15}
procedure TBGRAFreeTypeDrawer.DrawGlyph(AGlyph: integer;
AFont: TFreeTypeRenderableFont; x, y: single; AColor: TFPColor);
var f: TFreeTypeFont;
begin
if not (AFont is TFreeTypeFont) then exit;
f := TFreeTypeFont(Afont);
FColor := FPColorToBGRA(AColor);
if AFont.ClearType then
f.RenderGlyph(AGlyph, x, y, Destination.ClipRect, {$IFDEF OBJ}@{$ENDIF}RenderDirectlyClearType)
else
f.RenderGlyph(AGlyph, x, y, Destination.ClipRect, {$IFDEF OBJ}@{$ENDIF}RenderDirectly);
end;
procedure TBGRAFreeTypeDrawer.DrawGlyph(AGlyph: integer;
AFont: TFreeTypeRenderableFont; x, y: single; AColor: TBGRAPixel);
begin
DrawGlyph(AGlyph, AFont, x,y, BGRAToFPColor(AColor));
end;
procedure TBGRAFreeTypeDrawer.DrawGlyph(AGlyph: integer;
AFont: TFreeTypeRenderableFont; x, y: single; AColor: TBGRAPixel;
AAlign: TFreeTypeAlignments);
begin
DrawGlyph(AGlyph, AFont, x,y, BGRAToFPColor(AColor), AAlign);
end;
{$ENDIF}
function TBGRAFreeTypeDrawer.CreateTextEffect(AText: string;
AFont: TFreeTypeRenderableFont): TBGRACustomTextEffect;
var
mask: TBGRACustomBitmap;
tx,ty,marginHoriz,marginVert: integer;
tempDest: TBGRACustomBitmap;
tempTex: IBGRAScanner;
tempClearType: boolean;
begin
FInCreateTextEffect:= True;
try
tx := ceil(AFont.TextWidth(AText));
ty := ceil(AFont.TextHeight(AText));
marginHoriz := ty div 2;
marginVert := 1;
mask := BGRABitmapFactory.Create(tx+2*marginHoriz,ty+2*marginVert,BGRABlack);
tempDest := Destination;
tempTex := Texture;
tempClearType:= AFont.ClearType;
Destination := mask;
Texture := nil;
AFont.ClearType := false;
DrawText(AText,AFont,marginHoriz,marginVert,BGRAWhite,[ftaTop,ftaLeft]);
Destination := tempDest;
Texture := tempTex;
AFont.ClearType := tempClearType;
mask.ConvertToLinearRGB;
result := TBGRACustomTextEffect.Create(mask, true,tx,ty,point(-marginHoriz,-marginVert));
finally
FInCreateTextEffect:= false;
end;
end;
destructor TBGRAFreeTypeDrawer.Destroy;
begin
FMask.Free;
inherited Destroy;
end;
end.