-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
bgratext.pas
2181 lines (1985 loc) 路 75.1 KB
/
bgratext.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{ @abstract(Base implementation of system text rendering.)
This unit provides basic text rendering functions using system renderer
(LCL or MSEgui).
Text functions use a temporary bitmap where the operating system text drawing
is used. Then it is scaled down (if antialiasing is activated) and colored.
These routines are rather slow, so you may use other font renderers
like TBGRATextEffectFontRenderer in BGRATextFX if you want to use LCL fonts,
or, if you have TrueType fonts files, you may use TBGRAFreeTypeFontRenderer
in BGRAFreeType.
**Font rendering units** : BGRAText, BGRATextFX, BGRAVectorize, BGRAFreeType
}
unit BGRAText;
{$mode objfpc}{$H+}
{$i bgrabitmap.inc}
interface
{$IFDEF LINUX}
{$DEFINE SYSTEM_RENDERER_IS_FINE}
{$DEFINE SYSTEM_CLEARTYPE_RENDERER_IS_FINE}
{$DEFINE RENDER_TEXT_ON_TBITMAP}
{$ENDIF}
{$IFDEF FREEBSD}
{$DEFINE SYSTEM_RENDERER_IS_FINE}
{$DEFINE SYSTEM_CLEARTYPE_RENDERER_IS_FINE}
{$ENDIF}
{$IFDEF DARWIN}
{$DEFINE SYSTEM_RENDERER_IS_FINE}
{$DEFINE RENDER_TEXT_ON_TBITMAP}
{$ENDIF}
{$IFDEF WINDOWS}
{$IFNDEF LEGACY_FONT_VERTICAL_OFFSET}
{$DEFINE FIX_FONT_VERTICAL_OFFSET}
{$ENDIF}
{$IFDEF LCL}
{$DEFINE TEXTOUT_KERNING_PATCH}
{$ENDIF}
{$ENDIF}
{$IFDEF BGRABITMAP_USE_MSEGUI}
{$DEFINE RENDER_TEXT_ON_TBITMAP}
{$ENDIF}
uses
BGRAClasses, SysUtils, BGRAGraphics, BGRABitmapTypes, BGRAPen, BGRAGrayscaleMask
{$IFDEF LCL},InterfaceBase, LCLVersion{$ENDIF};
{$IF defined(LCLCOCOA) and (lcl_fullversion < 3030000)}
{$DEFINE TEXT_ANGLE_PATCH}
{$ENDIF}
const
RenderTextOnBitmap = {$IFDEF RENDER_TEXT_ON_TBITMAP}true{$ELSE}false{$ENDIF};
type
TWordBreakHandler = BGRABitmapTypes.TWordBreakHandler;
{ Font renderer using system rendering (LCL, mseGUI). }
TBGRASystemFontRenderer = class(TBGRACustomFontRenderer)
protected
FFont: TFont; //font parameters
FWordBreakHandler: TWordBreakHandler;
FOwnUnderline: boolean;
procedure UpdateFont; virtual;
function InternalTextSize(sUTF8: string; AShowPrefix: boolean): TSize;
function InternalTextSizeAngle(sUTF8: string; AShowPrefix: boolean; AOrientation: integer): TSize; virtual;
function InternalTextSizeStyle(sUTF8: string; AStyle: TTextStyle; AMaxWidth: integer): TSize; virtual;
procedure InternalTextWordBreak(ADest: TBGRACustomBitmap; ATextUTF8: string;
x, y, AMaxWidth: integer; AColor: TBGRAPixel; ATexture: IBGRAScanner;
AHorizAlign: TAlignment; AVertAlign: TTextLayout; ARightToLeft: boolean);
procedure InternalTextRect(ADest: TBGRACustomBitmap; ARect: TRect; x, y: integer; sUTF8: string; style: TTextStyle; c: TBGRAPixel; ATexture: IBGRAScanner);
procedure InternalTextOut(ADest: TBGRACustomBitmap; x, y: single; sUTF8: string; c: TBGRAPixel; texture: IBGRAScanner;
align: TAlignment; AShowPrefix: boolean = false; ARightToLeft: boolean = false);
{$IFDEF TEXT_ANGLE_PATCH}procedure InternalTextOutAnglePatch(ADest: TBGRACustomBitmap; x, y: single; orientation: integer;
s: string; c: TBGRAPixel; tex: IBGRAScanner; align: TAlignment;
AShowPrefix: boolean = false; ARightToLeft: boolean = false);{$ENDIF}
procedure InternalTextOutAngle(ADest: TBGRACustomBitmap; x, y: single; AOrientation: integer; sUTF8: string; c: TBGRAPixel; texture: IBGRAScanner;
align: TAlignment; AShowPrefix: boolean = false; ARightToLeft: boolean = false); virtual;
procedure InternalTextOutEllipse(ADest: TBGRACustomBitmap; x, y, availableWidth: single; sUTF8: string; c: TBGRAPixel; texture: IBGRAScanner;
align: TAlignment; AShowPrefix: boolean = false; ARightToLeft: boolean = false);
procedure InternalSplitText(var ATextUTF8: string; AMaxWidth: integer; out ARemainsUTF8: string; out ALineEndingBreak: boolean;
AWordBreak: TWordBreakHandler); overload;
procedure InternalSplitText(var ATextUTF8: string; AMaxWidth: integer; out ARemainsUTF8: string;
AWordBreak: TWordBreakHandler); overload;
function InternalGetFontPixelMetric: TFontPixelMetric;
procedure DefaultWorkBreakHandler(var ABeforeUTF8, AAfterUTF8: string);
public
OverrideUnderlineDecoration: boolean; // draw unerline according to computed font pixel metric instead of using system rendering of underline
procedure SplitText(var ATextUTF8: string; AMaxWidth: integer; out ARemainsUTF8: string);
function GetFontPixelMetric: TFontPixelMetric; override;
function FontExists(AName: string): boolean; override;
class function PatchSystemFontName(AName: string): string;
procedure TextOutAngle(ADest: TBGRACustomBitmap; x, y: single; orientationTenthDegCCW: integer; sUTF8: string; c: TBGRAPixel; align: TAlignment); overload; override;
procedure TextOutAngle(ADest: TBGRACustomBitmap; x, y: single; orientationTenthDegCCW: integer; sUTF8: string; c: TBGRAPixel; align: TAlignment; ARightToLeft: boolean); overload; override;
procedure TextOutAngle(ADest: TBGRACustomBitmap; x, y: single; orientationTenthDegCCW: integer; sUTF8: string; texture: IBGRAScanner; align: TAlignment); overload; override;
procedure TextOutAngle(ADest: TBGRACustomBitmap; x, y: single; orientationTenthDegCCW: integer; sUTF8: string; texture: IBGRAScanner; align: TAlignment; ARightToLeft: boolean); overload; override;
procedure TextOut(ADest: TBGRACustomBitmap; x, y: single; sUTF8: string; texture: IBGRAScanner; align: TAlignment); overload; override;
procedure TextOut(ADest: TBGRACustomBitmap; x, y: single; sUTF8: string; c: TBGRAPixel; align: TAlignment); overload; override;
procedure TextOut(ADest: TBGRACustomBitmap; x, y: single; sUTF8: string; texture: IBGRAScanner; align: TAlignment; ARightToLeft: boolean); overload; override;
procedure TextOut(ADest: TBGRACustomBitmap; x, y: single; sUTF8: string; c: TBGRAPixel; align: TAlignment; ARightToLeft: boolean); overload; override;
procedure TextRect(ADest: TBGRACustomBitmap; ARect: TRect; x, y: integer; sUTF8: string; style: TTextStyle; c: TBGRAPixel); overload; override;
procedure TextRect(ADest: TBGRACustomBitmap; ARect: TRect; x, y: integer; sUTF8: string; style: TTextStyle; texture: IBGRAScanner); overload; override;
procedure TextWordBreak(ADest: TBGRACustomBitmap; AText: string; x, y, AMaxWidth: integer; AColor: TBGRAPixel; AHorizAlign: TAlignment; AVertAlign: TTextLayout; ARightToLeft: boolean = false); overload;
procedure TextWordBreak(ADest: TBGRACustomBitmap; AText: string; x, y, AMaxWidth: integer; ATexture: IBGRAScanner; AHorizAlign: TAlignment; AVertAlign: TTextLayout; ARightToLeft: boolean = false); overload;
function TextSize(sUTF8: string): TSize; overload; override;
function TextSizeAngle(sUTF8: string; orientationTenthDegCCW: integer): TSize; override;
function TextSize(sUTF8: string; AMaxWidth: integer; {%H-}ARightToLeft: boolean): TSize; overload; override;
function TextFitInfo(sUTF8: string; AMaxWidth: integer): integer; override;
constructor Create;
destructor Destroy; override;
property OnWordBreak: TWordBreakHandler read FWordBreakHandler write FWordBreakHandler;
end;
{$IFDEF BGRABITMAP_USE_MSEGUI}
{ Font renderer using MSE calls to the operating system }
TMSEFontRenderer = class(TBGRASystemFontRenderer);
{$ELSE}
{ Font renderer using LCL calls to the operating system }
TLCLFontRenderer = class(TBGRASystemFontRenderer);
{$ENDIF}
function CleanTextOutString(s: string): string; //this works with UTF8 strings as well
function RemoveLineEnding(var s: string; indexByte: integer): boolean; //this works with UTF8 strings however the index is the byte index
function RemoveLineEndingUTF8(var sUTF8: string; indexUTF8: integer): boolean;
procedure BGRATextOut(bmp: TBGRACustomBitmap; Font: TFont; Quality: TBGRAFontQuality; xf, yf: single; sUTF8: string;
c: TBGRAPixel; tex: IBGRAScanner; align: TAlignment; CustomAntialiasingLevel: Integer = 0);
procedure BGRATextOutAngle(bmp: TBGRACustomBitmap; Font: TFont; Quality: TBGRAFontQuality; xf, yf: single; orientationTenthDegCCW: integer;
sUTF8: string; c: TBGRAPixel; tex: IBGRAScanner; align: TAlignment; CustomAntialiasingLevel: Integer = 0);
procedure BGRATextRect(bmp: TBGRACustomBitmap; Font: TFont; Quality: TBGRAFontQuality; ARect: TRect; xf, yf: single;
sUTF8: string; style: TTextStyle; c: TBGRAPixel; tex: IBGRAScanner; CustomAntialiasingLevel: Integer = 0);
function BGRATextSize(Font: TFont; Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: Integer): TSize;
function BGRATextSizeAngle(Font: TFont; AOrientation: integer; Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: Integer): TSize;
function BGRATextFitInfo(Font: TFont; Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: Integer; AMaxWidth: integer): integer;
function BGRATextFitInfoAngle(Font: TFont; AOrientation: integer; Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: Integer; AMaxWidth: integer): integer;
function BGRATextSizeStyle(Font: TFont; Quality: TBGRAFontQuality;
sUTF8: string; AStyle: TTextStyle; CustomAntialiasingLevel: integer;
AMaxWidth: integer): TSize;
function BGRAOriginalTextSize(Font: TFont; Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: integer): TSize;
function BGRAOriginalTextSizeAngle(Font: TFont; AOrientation: integer; Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: Integer): TSize;
function BGRAOriginalTextSizeEx(Font: TFont; Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: Integer;
out actualAntialiasingLevel: integer; out extraVerticalMarginDueToRotation: integer): TSize;
function BGRAOriginalTextSizeExAngle(Font: TFont; AOrientation: integer; Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: Integer;
out actualAntialiasingLevel: integer; out extraVerticalMarginDueToRotation: integer): TSize;
function BGRAOriginalTextSizeStyle(
Font: TFont; Quality: TBGRAFontQuality; sUTF8: string; AStyle: TTextStyle;
CustomAntialiasingLevel: Integer; AMaxWidth: integer): TSize;
function BGRATextUnderline(ATopLeft: TPointF; AWidth: Single; AMetrics: TFontPixelMetric): ArrayOfTPointF; overload;
function BGRATextUnderline(ATopLeft: TPointF; AWidth: Single; ABaseline, AEmHeight: single): ArrayOfTPointF; overload;
function BGRATextStrikeOut(ATopLeft: TPointF; AWidth: Single; AMetrics: TFontPixelMetric): ArrayOfTPointF; overload;
function BGRATextStrikeOut(ATopLeft: TPointF; AWidth: Single; ABaseline, AEmHeight, AXHeight: single): ArrayOfTPointF; overload;
function GetFontHeightSign: integer;
function FontEmHeightSign: integer;
function FontFullHeightSign: integer;
function SystemFontAvailable: boolean;
function GetFineClearTypeAuto: TBGRAFontQuality;
function FixSystemFontFullHeight({%H-}AFontName: string; AFontHeight: integer): integer;
{$IFDEF LCL}
function LCLFontAvailable: boolean;
function FixLCLFontFullHeight(AFontName: string; AFontHeight: integer): integer;
{$ENDIF}
procedure BGRAFillClearTypeGrayscaleMask(dest: TBGRACustomBitmap; x,y: integer; xThird: integer; mask: TGrayscaleMask; color: TBGRAPixel; texture: IBGRAScanner = nil; RGBOrder: boolean=true);
procedure BGRAFillClearTypeMask(dest: TBGRACustomBitmap; x,y: integer; xThird: integer; mask: TBGRACustomBitmap; color: TBGRAPixel; texture: IBGRAScanner = nil; RGBOrder: boolean=true);
procedure BGRAFillClearTypeRGBMask(dest: TBGRACustomBitmap; x,y: integer; mask: TBGRACustomBitmap; color: TBGRAPixel; texture: IBGRAScanner = nil; KeepRGBOrder: boolean=true);
const FontAntialiasingLevel = {$IFDEF SYSTEM_RENDERER_IS_FINE}3{$ELSE}6{$ENDIF};
const FontDefaultQuality = fqAntialiased;
const IsLclFontRendererFine = {$IFDEF SYSTEM_RENDERER_IS_FINE}true{$ELSE}false{$ENDIF};
function GetLCLFontPixelMetric(AFont: TFont): TFontPixelMetric;
var
BGRATextOutImproveReadabilityProc : procedure (bmp: TBGRACustomBitmap; AFont: TFont; xf,yf: single; text: string; color: TBGRAPixel; tex: IBGRAScanner; align: TAlignment; mode : TBGRATextOutImproveReadabilityMode);
procedure BitmapTextOut(ABitmap: TBitmap; ACoord: TPoint; AText: string);
procedure BitmapTextOutAngle(ABitmap: TBitmap; ACoord: TPoint; AText: string; AOrientation: integer);
procedure BitmapTextRect(ABitmap: TBitmap; ARect: TRect; ACoord: TPoint;
AText: string; const AStyle: TTextStyle);
function BitmapTextExtent(ABitmap: TBitmap; AText: string): TSize;
function BitmapTextExtentStyle(ABitmap: TBitmap; AText: string; AMaxWidth: integer;
const AStyle: TTextStyle): TSize;
function BitmapTextExtentAngle(ABitmap: TBitmap; AText: string; AOrientation: integer): TSize;
function BitmapTextFitInfo(ABitmap: TBitmap; AText: string; AMaxWidth: integer): integer;
function BitmapTextFitInfoAngle(ABitmap: TBitmap; AText: string; AMaxWidth: integer; AOrientation: integer): integer;
procedure BitmapFillRect(ABitmap: TBitmap; ARect: TRect; AColor: TColor);
implementation
uses Math, BGRATransform, BGRABlend, BGRAUTF8, BGRAUnicode, BGRATextBidi
{$IFDEF LCL}, Forms, LCLIntf, LCLType{$IF lcl_fullversion >= 1070000}, lclplatformdef{$ENDIF}{$ENDIF};
const MaxPixelMetricCount = 100;
var
SystemFontDisabledValue: boolean;
fqFineClearTypeComputed: boolean;
fqFineClearTypeValue: TBGRAFontQuality;
FontHeightSignComputed: boolean;
FontHeightSignValue: integer;
FontPixelMetricArray: array[0..MaxPixelMetricCount-1] of record
usage: integer;
name: string;
height: integer;
italic: boolean;
bold: boolean;
metric: TFontPixelMetric;
end;
FontPixelMetricCount: integer;
function RemovePrefix(sUTF8: string): string;
var i,resLen: integer;
begin
setlength(result, length(sUTF8));
resLen := 0;
i := 1;
while i <= length(sUTF8) do
begin
if sUTF8[i] = '&' then
begin // double ('&&') indicate single char '&'
if (i < length(sUTF8)) and (sUTF8[i+1] = '&') then
begin
inc(resLen);
result[resLen] := '&';
inc(i,2);
end else
// single indicate underline
inc(i);
end else
begin
inc(resLen);
result[resLen] := sUTF8[i];
inc(i);
end;
end;
setlength(result,resLen);
end;
procedure SplitByPrefix(sUTF8: string; AParts: TStrings);
var i, j: integer;
temp: string;
tempLen, charLen: integer;
underline: boolean;
procedure FlushUnderline;
begin
if underline and (tempLen > 0) then
begin
AParts.Add(copy(temp, 1, tempLen));
underline := false;
tempLen := 0;
end;
end;
begin
setlength(temp, length(sUTF8));
tempLen := 0;
underline := false;
i := 1;
while i <= length(sUTF8) do
begin
if sUTF8[i] = '&' then
begin // double ('&&') indicate single char '&'
if (i < length(sUTF8)) and (sUTF8[i+1] = '&') then
begin
FlushUnderline;
inc(tempLen);
temp[tempLen] := '&';
inc(i,2);
end else
// single indicate underline
AParts.Add(copy(temp, 1, tempLen));
underline := true;
tempLen := 0;
inc(i);
end else
begin
if GetBidiClassUTF8(@sUTF8[i]) <> ubcNonSpacingMark then
FlushUnderline;
charLen := UTF8CharacterLength(@sUTF8[i]);
for j := 1 to charLen do
begin
inc(tempLen);
temp[tempLen] := sUTF8[i];
inc(i);
end;
end;
end;
AParts.Add(copy(temp, 1, tempLen));
end;
{$IF defined(BGRABITMAP_USE_MSEGUI)}
{$i bgramsegui_text.inc}
{$ELSEIF defined(BGRABITMAP_USE_LCL)}
procedure BitmapTextOut(ABitmap: TBitmap; ACoord: TPoint; AText: string);
begin
ABitmap.Canvas.Brush.Style := bsClear;
ABitmap.Canvas.TextOut(ACoord.X, ACoord.Y, AText);
end;
procedure BitmapTextOutAngle(ABitmap: TBitmap; ACoord: TPoint; AText: string; AOrientation: integer);
begin
ABitmap.Canvas.Font.Orientation := AOrientation;
BitmapTextOut(ABitmap, ACoord, AText);
end;
procedure BitmapTextRect(ABitmap: TBitmap; ARect: TRect; ACoord: TPoint;
AText: string; const AStyle: TTextStyle);
begin
ABitmap.Canvas.Brush.Style := bsClear;
{$IFDEF DARWIN}
if AStyle.RightToLeft then AText := UTF8EmbedDirection(AText, true);
{$ENDIF}
ABitmap.Canvas.TextRect(ARect, ACoord.X, ACoord.Y, AText, AStyle);
end;
function BitmapTextExtent(ABitmap: TBitmap; AText: string): TSize;
begin
{$IFDEF DARWIN}
AText := StringReplace(AText, ' ', UTF8_NO_BREAK_SPACE, [rfReplaceAll]);
{$ENDIF}
result := ABitmap.Canvas.TextExtent(AText);
end;
function BitmapTextExtentStyle(ABitmap: TBitmap; AText: string; AMaxWidth: integer;
const AStyle: TTextStyle): TSize;
var bounds: TRect;
flags: Cardinal;
begin
bounds := Rect(0, 0, AMaxWidth, MaxInt);
flags := DT_CALCRECT;
if AStyle.SingleLine then flags := flags OR DT_SINGLELINE;
if AStyle.EndEllipsis then flags := flags or DT_END_ELLIPSIS;
if AStyle.ExpandTabs then flags := flags OR DT_EXPANDTABS;
if AStyle.Wordbreak then flags := flags OR DT_WORDBREAK;
if AStyle.RightToLeft then flags := flags or DT_RTLREADING;
if not AStyle.ShowPrefix then flags := flags OR DT_NOPREFIX;
LCLIntf.DrawText(ABitmap.Canvas.Handle, PChar(AText), Length(AText),
bounds, flags);
Result := Size(bounds.Width, bounds.Height);
end;
function BitmapTextExtentAngle(ABitmap: TBitmap; AText: string; AOrientation: integer): TSize;
begin
ABitmap.Canvas.Font.Orientation := AOrientation;
result := BitmapTextExtent(ABitmap, AText);
end;
function BitmapTextFitInfo(ABitmap: TBitmap; AText: string; AMaxWidth: integer): integer;
begin
{$IFDEF DARWIN}
AText := StringReplace(AText, ' ', UTF8_NO_BREAK_SPACE, [rfReplaceAll]);
{$ENDIF}
result := ABitmap.Canvas.TextFitInfo(AText, AMaxWidth);
end;
function BitmapTextFitInfoAngle(ABitmap: TBitmap; AText: string; AMaxWidth: integer; AOrientation: integer): integer;
begin
ABitmap.Canvas.Font.Orientation := AOrientation;
result := BitmapTextFitInfo(ABitmap, AText, AMaxWidth);
end;
procedure BitmapFillRect(ABitmap: TBitmap; ARect: TRect; AColor: TColor);
begin
ABitmap.Canvas.Brush.Style := bsSolid;
ABitmap.Canvas.Brush.Color := AColor;
ABitmap.Canvas.Pen.Style := psClear;
ABitmap.Canvas.FillRect(ARect);
end;
{$ELSE}
procedure BitmapTextOut(ABitmap: TBitmap; ACoord: TPoint; AText: string);
begin raise exception.Create('Not implemented') end;
procedure BitmapTextOutAngle(ABitmap: TBitmap; ACoord: TPoint; AText: string; AOrientation: integer);
begin raise exception.Create('Not implemented') end;
procedure BitmapTextRect(ABitmap: TBitmap; ARect: TRect; ACoord: TPoint;
AText: string; const AStyle: TTextStyle);
begin raise exception.Create('Not implemented') end;
function BitmapTextExtent(ABitmap: TBitmap; AText: string): TSize;
begin raise exception.Create('Not implemented') end;
function BitmapTextExtentStyle(ABitmap: TBitmap; AText: string; AMaxWidth: integer;
const AStyle: TTextStyle): TSize;
begin raise exception.Create('Not implemented') end;
function BitmapTextExtentAngle(ABitmap: TBitmap; AText: string; AOrientation: integer): TSize;
begin raise exception.Create('Not implemented') end;
function BitmapTextFitInfo(ABitmap: TBitmap; AText: string; AMaxWidth: integer): integer;
begin raise exception.Create('Not implemented') end;
function BitmapTextFitInfoAngle(ABitmap: TBitmap; AText: string; AMaxWidth: integer; AOrientation: integer): integer;
begin raise exception.Create('Not implemented') end;
procedure BitmapFillRect(ABitmap: TBitmap; ARect: TRect; AColor: TColor);
begin raise exception.Create('Not implemented') end;
{$ENDIF}
procedure ComputeFontVerticalBounds(text: string; font: TFont; out top, bottom, totalHeight: integer);
var
xb,yb: integer;
pmask: PBGRAPixel;
nbPix: array of integer;
nbCur: integer;
mean: integer;
mask: TBGRACustomBitmap;
size: TSize;
begin
if not SystemFontAvailable then
begin
top := 0;
bottom := 0;
totalHeight := 0;
exit;
end;
size := BGRAOriginalTextSize(font,fqSystem,text,FontAntialiasingLevel);
mask := BGRABitmapFactory.Create(size.cx,size.cy,BGRABlack);
mask.Canvas.Font := font;
mask.Canvas.Font.Quality := fqAntialiased;
mask.Canvas.Font.Color := clWhite;
mask.Canvas.Font.Style := font.style * [fsBold,fsItalic];
BitmapTextOut(mask.Bitmap, Point(0,0), text);
top := -1;
bottom := -1;
totalHeight:= mask.Height;
mean := 0;
setlength(nbPix, mask.Height);
for yb := 0 to mask.Height-1 do
begin
pmask := mask.scanline[yb];
nbCur := 0;
for xb := 0 to mask.Width-1 do
begin
if (pmask^.green > 0) then inc(nbCur);
inc(pmask);
end;
nbPix[yb] := nbCur;
inc(mean,nbCur);
end;
mean := (mean+ (mask.Height div 2)) div mask.Height;
for yb := 0 to high(nbPix) do
begin
if nbPix[yb]> mean div 3 then
begin
if top = -1 then top := yb
else bottom := yb+1;
end;
end;
mask.Free;
end;
function ComputeFontPixelMetric(AFont: TFont): TFontPixelMetric;
begin
ComputeFontVerticalBounds('acemu',AFont,result.xLine,result.Baseline,result.Lineheight);
ComputeFontVerticalBounds('gDjSO',AFont,result.CapLine,result.DescentLine,result.Lineheight);
if result.xLine = -1 then result.xLine := result.CapLine else
if result.CapLine = -1 then result.CapLine := result.xLine;
if result.DescentLine = -1 then result.DescentLine := result.Baseline else
if result.Baseline = -1 then result.Baseline := result.DescentLine;
result.Defined := (result.xLine <> -1) and (result.CapLine <> -1) and (result.Baseline <> -1) and (result.DescentLine <> -1) and
(result.Lineheight <> -1);
end;
function ComparePixelMetric(index: integer; font: TFont): integer;
begin
if (index < 0) or (index >= FontPixelMetricCount) then
result := 0
else
begin
with FontPixelMetricArray[index] do
if (name = font.Name) and (height = font.Height) then
result := 0 else
if (height > font.Height) then
result := 1 else
if (height < font.Height) then
result := -1 else
if name > font.Name then
result := 1 else
if name < font.Name then
result := -1
else result := 0;
end;
end;
procedure FindPixelMetricPos(AFont: TFont; out startPos,endPos: integer);
var middle,iStart,iEnd: integer;
begin
if FontPixelMetricCount = 0 then
begin
startPos := 0;
endPos := 0;
end;
iStart:= 0;
iEnd:= FontPixelMetricCount;
while iStart < iEnd do
begin
middle := (iStart+iEnd) div 2;
if ComparePixelMetric(middle,AFont) >= 0 then
iEnd := middle
else
iStart := middle+1;
end;
startPos := iStart;
iStart:= startPos;
iEnd:= FontPixelMetricCount;
while iStart < iEnd do
begin
middle := (iStart+iEnd) div 2;
if ComparePixelMetric(middle,AFont) <= 0 then
iStart := middle+1
else
iEnd := middle;
end;
endPos := iEnd;
end;
procedure RemoveOldPixelMetric;
var sum,nb,i: integer;
begin
if FontPixelMetricCount = 0 then exit;
sum := 0;
for i := 0 to FontPixelMetricCount-1 do
inc(sum, FontPixelMetricArray[i].usage);
sum := sum div FontPixelMetricCount;
nb := 0;
for i := 0 to FontPixelMetricCount-1 do
begin
if FontPixelMetricArray[i].usage > sum then
begin
FontPixelMetricArray[nb] := FontPixelMetricArray[i];
inc(nb);
end;
end;
FontPixelMetricCount := nb;
end;
function GetLCLFontPixelMetric(AFont: TFont): TFontPixelMetric;
var i,startPos,endPos: integer;
prevHeight,fixHeight: integer;
begin
if (AFont.Height < -200) or (AFont.Height > 150) then
begin
prevHeight := AFont.Height;
if AFont.Height < 0 then
fixHeight := -200
else
fixHeight := 150;
AFont.Height := fixHeight;
result := GetLCLFontPixelMetric(AFont);
AFont.Height := prevHeight;
result.Baseline := round(result.Baseline/fixHeight*prevHeight);
result.CapLine := round(result.CapLine/fixHeight*prevHeight);
result.DescentLine := round(result.DescentLine/fixHeight*prevHeight);
result.Lineheight := round(result.Lineheight/fixHeight*prevHeight);
result.xLine := round(result.xLine/fixHeight*prevHeight);
exit;
end;
FindPixelMetricPos(AFont,startPos,endPos);
for i := startPos to endPos-1 do
if (FontPixelMetricArray[i].bold = AFont.bold) and
(FontPixelMetricArray[i].italic = AFont.Italic) then
begin
result := FontPixelMetricArray[i].metric;
inc(FontPixelMetricArray[i].usage);
exit;
end;
if FontPixelMetricCount = MaxPixelMetricCount then RemoveOldPixelMetric;
for i := FontPixelMetricCount downto endPos+1 do
FontPixelMetricArray[i] := FontPixelMetricArray[i-1];
inc(FontPixelMetricCount);
with FontPixelMetricArray[endPos]do
begin
italic := AFont.Italic;
bold := AFont.Bold;
usage := 1;
name := AFont.Name;
height:= AFont.Height;
metric := ComputeFontPixelMetric(AFont);
result := metric;
end;
end;
const DefaultFontHeightSign = -1;
function BGRATextUnderline(ATopLeft: TPointF;
AWidth: Single; AMetrics: TFontPixelMetric): ArrayOfTPointF;
begin
result := BGRATextUnderline(ATopLeft, AWidth, AMetrics.Baseline,AMetrics.Baseline-AMetrics.CapLine);
end;
function BGRATextUnderline(ATopLeft: TPointF;
AWidth: Single; ABaseline, AEmHeight: single): ArrayOfTPointF;
var height,y: single;
begin
height := AEmHeight*0.080;
y := ATopLeft.y+ABaseline+1.6*height;
result := ComputeWidePolylinePoints([PointF(ATopLeft.x,y),
PointF(ATopLeft.x+AWidth,y)],height,BGRABlack,pecFlat,pjsMiter,
SolidPenStyle, []);
end;
function BGRATextStrikeOut(ATopLeft: TPointF; AWidth: Single;
AMetrics: TFontPixelMetric): ArrayOfTPointF;
begin
result := BGRATextStrikeOut(ATopLeft, AWidth, AMetrics.Baseline,AMetrics.Baseline-AMetrics.CapLine,AMetrics.Baseline-AMetrics.xLine);
end;
function BGRATextStrikeOut(ATopLeft: TPointF; AWidth: Single; ABaseline,
AEmHeight, AXHeight: single): ArrayOfTPointF;
var height,y: single;
begin
height := AEmHeight*0.075;
y := ATopLeft.y+ABaseline-AXHeight*0.5;
result := ComputeWidePolylinePoints([PointF(ATopLeft.x,y),
PointF(ATopLeft.x+AWidth,y)],height,BGRABlack,pecFlat,pjsMiter,
SolidPenStyle, []);
end;
function GetFontHeightSign: integer;
var
HeightP1, HeightM1: integer;
tempBmp: TBitmap;
begin
if SystemFontDisabledValue then
begin
result := DefaultFontHeightSign;
exit;
end;
if FontHeightSignComputed then
begin
result := FontHeightSignValue;
exit;
end;
if {$IFDEF LCL}WidgetSet.LCLPlatform = lpNoGUI{$ELSE}False{$ENDIF} then
begin
SystemFontDisabledValue:= True;
result := -1;
exit;
end;
tempBmp := nil;
try
tempBmp := TBitmap.Create;
tempBmp.Width := 30;
tempBmp.Height := 30;
tempBmp.Canvas.Font.Name := 'Arial';
tempBmp.Canvas.Font.Style := [];
tempBmp.Canvas.Font.Height := 20;
HeightP1 := BitmapTextExtent(tempBmp, 'Hg').cy;
tempBmp.Canvas.Font.Height := -20;
HeightM1 := BitmapTextExtent(tempBmp, 'Hg').cy;
if HeightP1 > HeightM1 then
FontHeightSignValue := 1
else
FontHeightSignValue := -1;
FontHeightSignComputed := true;
result := FontHeightSignValue;
except
on ex: Exception do
begin
SystemFontDisabledValue := True;
result := -1;
end;
end;
tempBmp.Free;
end;
function GetFineClearTypeAuto: TBGRAFontQuality;
var
lclBmp: TBitmap;
bgra: TBGRACustomBitmap;
x,y: integer;
begin
if fqFineClearTypeComputed then
begin
result:= fqFineClearTypeValue;
exit;
end;
result := fqFineAntialiasing;
if not SystemFontDisabledValue and not ({$IFDEF LCL}WidgetSet.LCLPlatform = lpNoGUI{$ELSE}False{$ENDIF}) then
begin
lclBmp := TBitmap.Create;
lclBmp.Width := 1;
lclBmp.Height := 1;
lclBmp.Canvas.Font.Height := -50;
lclBmp.Canvas.Font.Quality := fqCleartype;
lclBmp.Canvas.Font.Color := clBlack;
with BitmapTextExtent(lclBmp, '/') do
begin
lclBmp.Width := cx;
lclBmp.Height := cy;
end;
BitmapFillRect(lclBmp, rect(0,0,lclBmp.Width,lclBmp.Height), clWhite);
BitmapTextOut(lclBmp, Point(0,0), '/');
bgra:= BGRABitmapFactory.Create(lclBmp);
x:= bgra.Width div 2;
for y := 0 to bgra.Height-1 do
with bgra.GetPixel(x,y) do
if (red<>blue) then
begin
if blue < red then
result:= fqFineClearTypeRGB
else
result:= fqFineClearTypeBGR;
break;
end else
if (green = 0) then break;
bgra.Free;
lclBmp.Free;
end;
fqFineClearTypeValue := result;
fqFineClearTypeComputed:= true;
end;
{$IFNDEF WINDOWS}
var LCLFontFullHeightRatio : array of record
FontName: string;
Ratio: single;
end;
{$ENDIF}
function FixSystemFontFullHeight(AFontName: string; AFontHeight: integer): integer;
{$IFNDEF WINDOWS}
const TestHeight = 200;
var
i: Integer;
ratio : single;
f: TFont;
h: LongInt;
begin
if (AFontHeight = 0) or
(AFontHeight*FontEmHeightSign > 0) then
result := AFontHeight
else
begin
ratio := EmptySingle;
for i := 0 to high(LCLFontFullHeightRatio) do
if CompareText(AFontName, LCLFontFullHeightRatio[i].FontName)=0 then
begin
ratio := LCLFontFullHeightRatio[i].Ratio;
break;
end;
if ratio = EmptySingle then
begin
f := TFont.Create;
f.Quality := fqDefault;
f.Name := AFontName;
f.Height := FontFullHeightSign*TestHeight;
h := BGRATextSize(f, fqSystem, 'Hg', 1).cy;
f.Free;
if h = 0 then ratio := 1
else ratio := TestHeight/h;
setlength(LCLFontFullHeightRatio, length(LCLFontFullHeightRatio)+1);
LCLFontFullHeightRatio[high(LCLFontFullHeightRatio)].FontName:= AFontName;
LCLFontFullHeightRatio[high(LCLFontFullHeightRatio)].Ratio:= ratio;
end;
result := round(AFontHeight*ratio);
end;
end;
{$ELSE}
begin
result := AFontHeight;
end;
{$ENDIF}
{$IFDEF LCL}
function LCLFontAvailable: boolean;
begin
result := SystemFontAvailable;
end;
function FixLCLFontFullHeight(AFontName: string; AFontHeight: integer): integer;
begin
result := FixSystemFontFullHeight(AFontName, AFontHeight);
end;
{$ENDIF}
function FontEmHeightSign: integer;
begin
result := GetFontHeightSign;
end;
function FontFullHeightSign: integer;
begin
result := -FontEmHeightSign;
end;
function SystemFontAvailable: boolean;
begin
if not FontHeightSignComputed then GetFontHeightSign;
result := not SystemFontDisabledValue;
end;
procedure BGRAFillClearTypeGrayscaleMask(dest: TBGRACustomBitmap; x,
y: integer; xThird: integer; mask: TGrayscaleMask; color: TBGRAPixel;
texture: IBGRAScanner; RGBOrder: boolean);
begin
BGRAGrayscaleMask.BGRAFillClearTypeGrayscaleMask(dest,x,y,xThird,mask,color,texture,RGBOrder);
end;
procedure BGRAFillClearTypeMask(dest: TBGRACustomBitmap; x,y: integer; xThird: integer; mask: TBGRACustomBitmap; color: TBGRAPixel; texture: IBGRAScanner; RGBOrder: boolean);
begin
BGRABlend.BGRAFillClearTypeMask(dest,x,y,xThird,mask,color,texture,RGBOrder);
end;
procedure BGRAFillClearTypeRGBMask(dest: TBGRACustomBitmap; x, y: integer;
mask: TBGRACustomBitmap; color: TBGRAPixel; texture: IBGRAScanner;
KeepRGBOrder: boolean);
begin
BGRABlend.BGRAFillClearTypeRGBMask(dest,x,y,mask,color,texture,KeepRGBOrder);
end;
function BGRAOriginalTextSizeEx(Font: TFont; Quality: TBGRAFontQuality;
sUTF8: string; CustomAntialiasingLevel: Integer;
out actualAntialiasingLevel: integer;
out extraVerticalMarginDueToRotation: integer): TSize;
begin
result := BGRAOriginalTextSizeExAngle(Font, Font.Orientation, Quality, sUTF8,
CustomAntialiasingLevel, actualAntialiasingLevel, extraVerticalMarginDueToRotation);
end;
function BGRAOriginalTextSizeExAngle(Font: TFont; AOrientation: integer;
Quality: TBGRAFontQuality; sUTF8: string; CustomAntialiasingLevel: Integer;
out actualAntialiasingLevel: integer;
out extraVerticalMarginDueToRotation: integer): TSize;
var
tempBmp: TBitmap;
begin
actualAntialiasingLevel:= CustomAntialiasingLevel;
extraVerticalMarginDueToRotation := 0;
if not SystemFontAvailable then
result := Size(0,0)
else
begin
tempBmp := nil;
try
tempBmp := TBitmap.Create;
{$IFDEF BGRABITMAP_USE_MSEGUI}
tempBmp.Width := 1;
tempBmp.Height := 1;
{$ENDIF}
tempBmp.Canvas.Font := Font;
if Quality in[fqFineClearTypeBGR,fqFineClearTypeRGB,fqFineAntialiasing] then
begin
tempBmp.Canvas.Font.Height := Font.Height*CustomAntialiasingLevel;
end else
begin
tempBmp.Canvas.Font.Height := Font.Height;
actualAntialiasingLevel:= 1;
end;
result := BitmapTextExtentAngle(tempBmp, sUTF8, AOrientation);
if Font.Orientation <> 0 then
begin
tempBmp.Canvas.Font.Orientation:= 0;
extraVerticalMarginDueToRotation := result.cy -
BitmapTextExtentAngle(tempBmp, sUTF8, AOrientation).cy;
end;
except
on ex: exception do
begin
result := Size(0,0);
SystemFontDisabledValue := True;
end;
end;
tempBmp.Free;
end;
end;
function BGRADefaultTextOutStyle(ARightToLeft: boolean; AShowPrefix: boolean): TTextStyle;
begin
fillchar({%H-}result, sizeof(result), 0);
result.SingleLine := true;
result.Alignment := taLeftJustify;
result.Layout := tlTop;
result.RightToLeft := ARightToLeft;
result.ShowPrefix := AShowPrefix;
end;
function BGRAOriginalTextSizeStyle(Font: TFont; Quality: TBGRAFontQuality;
sUTF8: string; AStyle: TTextStyle; CustomAntialiasingLevel: Integer;
AMaxWidth: integer): TSize;
var
tempBmp: TBitmap;
begin
if not SystemFontAvailable then
result := Size(0,0)
else
begin
tempBmp := nil;
try
tempBmp := TBitmap.Create;
{$IFDEF BGRABITMAP_USE_MSEGUI}
tempBmp.Width := 1;
tempBmp.Height := 1;
{$ENDIF}
tempBmp.Canvas.Font := Font;
if Quality in[fqFineClearTypeBGR,fqFineClearTypeRGB,fqFineAntialiasing] then
begin
tempBmp.Canvas.Font.Height := Font.Height*CustomAntialiasingLevel;
end else
begin
tempBmp.Canvas.Font.Height := Font.Height;
end;
result := BitmapTextExtentStyle(tempBmp, sUTF8, AMaxWidth, AStyle);
except
on ex: exception do
begin
result := Size(0,0);
SystemFontDisabledValue := True;
end;
end;
tempBmp.Free;
end;
end;
function BGRATextFitInfo(Font: TFont; Quality: TBGRAFontQuality; sUTF8: string;
CustomAntialiasingLevel: Integer; AMaxWidth: integer): integer;
begin
result := BGRATextFitInfoAngle(Font, Font.Orientation, Quality, sUTF8,
CustomAntialiasingLevel, AMaxWidth);
end;
function BGRATextFitInfoAngle(Font: TFont; AOrientation: integer; Quality: TBGRAFontQuality; sUTF8: string;
CustomAntialiasingLevel: Integer; AMaxWidth: integer): integer;
var
actualAntialiasingLevel{$IFDEF LCL}{$IF lcl_fullversion < 1070000}, len1{$ENDIF}{$ENDIF}: Integer;
tempBmp: TBitmap;
begin
if (AMaxWidth = 0) or (length(sUTF8)=0) then exit(0);
actualAntialiasingLevel:= CustomAntialiasingLevel;
if not SystemFontAvailable then
result := 0
else
begin
tempBmp := nil;
try
tempBmp := TBitmap.Create;
tempBmp.Canvas.Font := Font;
if Quality in[fqFineClearTypeBGR,fqFineClearTypeRGB,fqFineAntialiasing] then
begin
tempBmp.Canvas.Font.Height := Font.Height*CustomAntialiasingLevel;
end else
begin
tempBmp.Canvas.Font.Height := Font.Height;
actualAntialiasingLevel:= 1;
end;
{$IFDEF LCL}{$IF lcl_fullversion < 1070000}
len1 := BitmapTextExtentAngle(tempBmp,
copy(sUTF8,1,UTF8CharacterLength(@sUTF8[1])),
AOrientation).cx;
if len1 > AMaxWidth*actualAntialiasingLevel then exit(0);
{$ENDIF}{$ENDIF}
result := BitmapTextFitInfoAngle(tempBmp, sUTF8,
AMaxWidth*actualAntialiasingLevel, AOrientation);
except
on ex: exception do
begin
result := 0;
SystemFontDisabledValue := True;
end;
end;
tempBmp.Free;
end;
end;
function BGRATextSizeStyle(Font: TFont; Quality: TBGRAFontQuality;
sUTF8: string; AStyle: TTextStyle;
CustomAntialiasingLevel: integer; AMaxWidth: integer): TSize;
begin
{$IFDEF SYSTEM_RENDERER_IS_FINE}
if Quality = fqFineAntialiasing then Quality:= fqSystem;
{$ENDIF}
result := BGRAOriginalTextSizeStyle(Font, Quality, sUTF8,
AStyle, CustomAntialiasingLevel, AMaxWidth);
if Quality in[fqFineClearTypeBGR,fqFineClearTypeRGB,fqFineAntialiasing] then