Menu

[r3880]: / branches / parsnip / Src / Lib / SynEdit / SynTextDrawer.pas  Maximize  Restore  History

Download this file

1019 lines (898 with data), 28.3 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
{==============================================================================
Content: TheTextDrawer, a helper class for drawing of
fixed-pitched font characters
==============================================================================
The contents of this file are subject to the Mozilla Public License Ver. 1.0
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
==============================================================================
The Original Code is HANAI Tohru's private delphi library.
==============================================================================
The Initial Developer of the Original Code is HANAI Tohru (Japan)
Portions created by HANAI Tohru are Copyright (C) 1999.
All Rights Reserved.
==============================================================================
Contributor(s): HANAI Tohru
Unicode translation by Maël Hörz.
==============================================================================
History: 01/19/1999 HANAI Tohru
Initial Version
02/13/1999 HANAI Tohru
Changed default intercharacter spacing
09/09/1999 HANAI Tohru
Redesigned all. Simplified interfaces.
When drawing text now it uses TextOut + SetTextCharacter-
Extra insted ExtTextOut since ExtTextOut has a little
heavy behavior.
09/10/1999 HANAI Tohru
Added code to call ExtTextOut because there is a problem
when TextOut called with italicized raster type font.
After this changing, ExtTextOut is called without the
last parameter `lpDx' and be with SetTextCharacterExtra.
This pair performs faster than with `lpDx'.
09/14/1999 HANAI Tohru
Changed code for saving/restoring DC
09/15/1999 HANAI Tohru
Added X/Y parameters to ExtTextOut.
09/16/1999 HANAI Tohru
Redesigned for multi-bytes character drawing.
09/19/1999 HANAI Tohru
Since TheTextDrawer grew fat it was split into three
classes - TheFontStock, TheTextDrawer and TheTextDrawerEx.
Currently it should avoid TheTextDrawer because it is
slower than TheTextDrawer.
09/25/1999 HANAI Tohru
Added internally definition of LeadBytes for Delphi 2
10/01/1999 HANAI Tohru
To save font resources, now all fonts data are shared
among all of TheFontStock instances. With this changing,
there added a new class `TheFontsInfoManager' to manage
those shared data.
10/09/1999 HANAI Tohru
Added BaseStyle property to TheFontFont class.
==============================================================================}
// $Id: SynTextDrawer.pas,v 1.6.2.17 2008/09/17 13:59:12 maelh Exp $
// SynEdit note: The name had to be changed to get SynEdit to install
// together with mwEdit into the same Delphi installation
unit SynTextDrawer;
{$I SynEdit.inc}
interface
uses
SynUnicode,
SysUtils,
Classes,
Windows,
Graphics,
Math;
const
FontStyleCount = Ord(High(TFontStyle)) +1;
FontStyleCombineCount = (1 shl FontStyleCount);
type
PIntegerArray = ^TIntegerArray;
TIntegerArray = array[0..MaxInt div SizeOf(Integer) - 1] of Integer;
TheStockFontPatterns = 0..FontStyleCombineCount -1;
PheFontData = ^TheFontData;
TheFontData = record
Style: TFontStyles;
Handle: HFont;
CharAdv: Integer;
CharHeight: Integer;
end;
PheFontsData = ^TheFontsData;
TheFontsData = array[TheStockFontPatterns] of TheFontData;
PheSharedFontsInfo = ^TheSharedFontsInfo;
TheSharedFontsInfo = record
// reference counters
RefCount: Integer;
LockCount: Integer;
// font information
BaseFont: TFont;
BaseLF: TLogFont;
IsTrueType: Boolean;
FontsData: TheFontsData;
end;
{ TheStockFontManager }
TheFontsInfoManager = class
private
FFontsInfo: TList;
function FindFontsInfo(const LF: TLogFont): PheSharedFontsInfo;
function CreateFontsInfo(ABaseFont: TFont;
const LF: TLogFont): PheSharedFontsInfo;
procedure DestroyFontHandles(pFontsInfo: PheSharedFontsInfo);
procedure RetrieveLogFontForComparison(ABaseFont: TFont; var LF: TLogFont);
public
constructor Create;
destructor Destroy; override;
procedure LockFontsInfo(pFontsInfo: PheSharedFontsInfo);
procedure UnLockFontsInfo(pFontsInfo: PheSharedFontsInfo);
function GetFontsInfo(ABaseFont: TFont): PheSharedFontsInfo;
procedure ReleaseFontsInfo(pFontsInfo: PheSharedFontsInfo);
end;
{ TheFontStock }
TTextOutOptions = set of (tooOpaque, tooClipped);
TheExtTextOutProc = procedure (X, Y: Integer; fuOptions: TTextOutOptions;
const ARect: TRect; const Text: UnicodeString; Length: Integer) of object;
EheFontStockException = class(Exception);
TheFontStock = class
private
// private DC
FDC: HDC;
FDCRefCount: Integer;
// Shared fonts
FpInfo: PheSharedFontsInfo;
FUsingFontHandles: Boolean;
// Current font
FCrntFont: HFONT;
FCrntStyle: TFontStyles;
FpCrntFontData: PheFontData;
// local font info
FBaseLF: TLogFont;
function GetBaseFont: TFont;
function GetIsTrueType: Boolean;
protected
function InternalGetDC: HDC; virtual;
procedure InternalReleaseDC(Value: HDC); virtual;
function InternalCreateFont(Style: TFontStyles): HFONT; virtual;
function CalcFontAdvance(DC: HDC; pCharHeight: PInteger): Integer; virtual;
function GetCharAdvance: Integer; virtual;
function GetCharHeight: Integer; virtual;
function GetFontData(idx: Integer): PheFontData; virtual;
procedure UseFontHandles;
procedure ReleaseFontsInfo;
procedure SetBaseFont(Value: TFont); virtual;
procedure SetStyle(Value: TFontStyles); virtual;
property FontData[idx: Integer]: PheFontData read GetFontData;
property FontsInfo: PheSharedFontsInfo read FpInfo;
public
constructor Create(InitialFont: TFont); virtual;
destructor Destroy; override;
procedure ReleaseFontHandles; virtual;
property BaseFont: TFont read GetBaseFont;
property Style: TFontStyles read FCrntStyle write SetStyle;
property FontHandle: HFONT read FCrntFont;
property CharAdvance: Integer read GetCharAdvance;
property CharHeight: Integer read GetCharHeight;
property IsTrueType: Boolean read GetIsTrueType;
end;
{ TheTextDrawer }
EheTextDrawerException = class(Exception);
TheTextDrawer = class(TObject)
private
FDC: HDC;
FSaveDC: Integer;
// Font information
FFontStock: TheFontStock;
FStockBitmap: TBitmap;
FCalcExtentBaseStyle: TFontStyles;
FBaseCharWidth: Integer;
FBaseCharHeight: Integer;
// current font and properties
FCrntFont: HFONT;
FETODist: PIntegerArray;
// current font attributes
FColor: TColor;
FBkColor: TColor;
FCharExtra: Integer;
// Begin/EndDrawing calling count
FDrawingCount: Integer;
// GetCharABCWidthsW cache
FCharABCWidthCache : array [0..127] of TABC;
FCharWidthCache : array [0..127] of Integer;
protected
procedure ReleaseETODist; virtual;
procedure AfterStyleSet; virtual;
procedure DoSetCharExtra(Value: Integer); virtual;
procedure FlushCharABCWidthCache;
function GetCachedABCWidth(c : Cardinal; var abc : TABC) : Boolean;
property StockDC: HDC read FDC;
property DrawingCount: Integer read FDrawingCount;
property FontStock: TheFontStock read FFontStock;
property BaseCharWidth: Integer read FBaseCharWidth;
property BaseCharHeight: Integer read FBaseCharHeight;
public
constructor Create(CalcExtentBaseStyle: TFontStyles; BaseFont: TFont); virtual;
destructor Destroy; override;
function GetCharWidth: Integer; virtual;
function GetCharHeight: Integer; virtual;
procedure BeginDrawing(DC: HDC); virtual;
procedure EndDrawing; virtual;
procedure TextOut(X, Y: Integer; Text: PWideChar; Length: Integer); virtual;
procedure ExtTextOut(X, Y: Integer; Options: TTextOutOptions; ARect: TRect;
Text: PWideChar; Length: Integer); virtual;
function TextExtent(const Text: UnicodeString): TSize; overload;
function TextExtent(Text: PWideChar; Count: Integer): TSize; overload;
function TextWidth(const Text: UnicodeString): Integer; overload;
function TextWidth(Text: PWideChar; Count: Integer): Integer; overload;
procedure SetBaseFont(Value: TFont); virtual;
procedure SetBaseStyle(const Value: TFontStyles); virtual;
procedure SetStyle(Value: TFontStyles); virtual;
procedure SetForeColor(Value: TColor); virtual;
procedure SetBackColor(Value: TColor); virtual;
procedure SetCharExtra(Value: Integer); virtual;
procedure ReleaseTemporaryResources; virtual;
property CharWidth: Integer read GetCharWidth;
property CharHeight: Integer read GetCharHeight;
property BaseFont: TFont write SetBaseFont;
property BaseStyle: TFontStyles write SetBaseStyle;
property ForeColor: TColor write SetForeColor;
property BackColor: TColor write SetBackColor;
property Style: TFontStyles write SetStyle;
property CharExtra: Integer read FCharExtra write SetCharExtra;
end;
function GetFontsInfoManager: TheFontsInfoManager;
function UniversalExtTextOut(DC: HDC; X, Y: Integer; Options: TTextOutOptions;
Rect: TRect; Str: PWideChar; Count: Integer; ETODist: PIntegerArray): Boolean;
implementation
{$IFDEF SYN_UNISCRIBE}
uses
SynUsp10;
{$ENDIF}
var
gFontsInfoManager: TheFontsInfoManager;
{ utility routines }
function GetFontsInfoManager: TheFontsInfoManager;
begin
if not Assigned(gFontsInfoManager) then
gFontsInfoManager := TheFontsInfoManager.Create;
Result := gFontsInfoManager;
end;
function Min(x, y: integer): integer;
begin
if x < y then Result := x else Result := y;
end;
// UniversalExtTextOut uses UniScribe where available for the best possible
// output quality. This also avoids a bug in (Ext)TextOut that surfaces when
// displaying a combination of Chinese and Korean text.
//
// See here for details: http://groups.google.com/group/microsoft.public.win32.programmer.international/browse_thread/thread/77cd596f2b96dc76/146300208098285c?lnk=st&q=font+substitution+problem#146300208098285c
function UniversalExtTextOut(DC: HDC; X, Y: Integer; Options: TTextOutOptions;
Rect: TRect; Str: PWideChar; Count: Integer; ETODist: PIntegerArray): Boolean;
{$IFDEF SYN_UNISCRIBE}
const
SSAnalyseFlags = SSA_GLYPHS or SSA_FALLBACK or SSA_LINK;
SpaceString: UnicodeString = ' ';
{$ENDIF}
var
TextOutFlags: DWORD;
{$IFDEF SYN_UNISCRIBE}
GlyphBufferSize: Integer;
saa: TScriptStringAnalysis;
{$ENDIF}
begin
TextOutFlags := 0;
if tooOpaque in Options then
TextOutFlags := TextOutFlags or ETO_OPAQUE;
if tooClipped in Options then
TextOutFlags := TextOutFlags or ETO_CLIPPED;
{$IFDEF SYN_UNISCRIBE}
if Usp10IsInstalled then
begin
// UniScribe requires that the string contains at least one character.
// If UniversalExtTextOut should be used to fill the background we can just
// pass a string made of a space.
if Count <= 0 then
if tooOpaque in Options then
begin
// Clipping is necessary, since depending on X, Y the space will be
// printed outside Rect and potentially fill more than we want.
TextOutFlags := TextOutFlags or ETO_CLIPPED;
Str := PWideChar(SpaceString);
Count := 1;
end
else
begin
Result := False;
Exit;
end;
// According to the MS Windows SDK (1.5 * Count + 16) is the recommended
// value for GlyphBufferSize (see documentation of cGlyphs parameter of
// ScriptStringAnalyse function)
GlyphBufferSize := (3 * Count) div 2 + 16;
Result := Succeeded(ScriptStringAnalyse(DC, Str, Count, GlyphBufferSize, -1,
SSAnalyseFlags, 0, nil, nil, Pointer(ETODist), nil, nil, @saa));
Result := Result and Succeeded(ScriptStringOut(saa, X, Y, TextOutFlags,
@Rect, 0, 0, False));
Result := Result and Succeeded(ScriptStringFree(@saa));
end
else
{$ENDIF}
begin
Result := ExtTextOutW(DC, X, Y, TextOutFlags, @Rect, Str, Count,
Pointer(ETODist));
end;
end;
{ TheFontsInfoManager }
procedure TheFontsInfoManager.LockFontsInfo(
pFontsInfo: PheSharedFontsInfo);
begin
Inc(pFontsInfo^.LockCount);
end;
constructor TheFontsInfoManager.Create;
begin
inherited;
FFontsInfo := TList.Create;
end;
function TheFontsInfoManager.CreateFontsInfo(ABaseFont: TFont;
const LF: TLogFont): PheSharedFontsInfo;
begin
New(Result);
FillChar(Result^, SizeOf(TheSharedFontsInfo), 0);
with Result^ do
try
BaseFont := TFont.Create;
BaseFont.Assign(ABaseFont);
BaseLF := LF;
IsTrueType := (0 <> (TRUETYPE_FONTTYPE and LF.lfPitchAndFamily));
except
Result^.BaseFont.Free;
Dispose(Result);
raise;
end;
end;
procedure TheFontsInfoManager.UnlockFontsInfo(
pFontsInfo: PheSharedFontsInfo);
begin
with pFontsInfo^ do
begin
Dec(LockCount);
if 0 = LockCount then
DestroyFontHandles(pFontsInfo);
end;
end;
destructor TheFontsInfoManager.Destroy;
begin
gFontsInfoManager := nil;
if Assigned(FFontsInfo) then
begin
while FFontsInfo.Count > 0 do
begin
ASSERT(1 = PheSharedFontsInfo(FFontsInfo[FFontsInfo.Count - 1])^.RefCount);
ReleaseFontsInfo(PheSharedFontsInfo(FFontsInfo[FFontsInfo.Count - 1]));
end;
FFontsInfo.Free;
end;
inherited;
end;
procedure TheFontsInfoManager.DestroyFontHandles(
pFontsInfo: PheSharedFontsInfo);
var
i: Integer;
begin
with pFontsInfo^ do
for i := Low(TheStockFontPatterns) to High(TheStockFontPatterns) do
with FontsData[i] do
if Handle <> 0 then
begin
DeleteObject(Handle);
Handle := 0;
end;
end;
function TheFontsInfoManager.FindFontsInfo(
const LF: TLogFont): PheSharedFontsInfo;
var
i: Integer;
begin
for i := 0 to FFontsInfo.Count - 1 do
begin
Result := PheSharedFontsInfo(FFontsInfo[i]);
if CompareMem(@(Result^.BaseLF), @LF, SizeOf(TLogFont)) then
Exit;
end;
Result := nil;
end;
function TheFontsInfoManager.GetFontsInfo(ABaseFont: TFont): PheSharedFontsInfo;
var
LF: TLogFont;
begin
ASSERT(Assigned(ABaseFont));
RetrieveLogFontForComparison(ABaseFont, LF);
Result := FindFontsInfo(LF);
if not Assigned(Result) then
begin
Result := CreateFontsInfo(ABaseFont, LF);
FFontsInfo.Add(Result);
end;
if Assigned(Result) then
Inc(Result^.RefCount);
end;
procedure TheFontsInfoManager.ReleaseFontsInfo(pFontsInfo: PheSharedFontsInfo);
begin
ASSERT(Assigned(pFontsInfo));
with pFontsInfo^ do
begin
{$IFDEF HE_ASSERT}
ASSERT(LockCount < RefCount,
'Call DeactivateFontsInfo before calling this.');
{$ELSE}
ASSERT(LockCount < RefCount);
{$ENDIF}
if RefCount > 1 then
Dec(RefCount)
else
begin
FFontsInfo.Remove(pFontsInfo);
// free all objects
BaseFont.Free;
Dispose(pFontsInfo);
end;
end;
end;
procedure TheFontsInfoManager.RetrieveLogFontForComparison(ABaseFont: TFont;
var LF: TLogFont);
var
pEnd: PChar;
begin
GetObject(ABaseFont.Handle, SizeOf(TLogFont), @LF);
with LF do
begin
lfItalic := 0;
lfUnderline := 0;
lfStrikeOut := 0;
pEnd := StrEnd(lfFaceName);
FillChar(pEnd[1], @lfFaceName[High(lfFaceName)] - pEnd, 0);
end;
end;
{ TheFontStock }
// CalcFontAdvance : Calculation a advance of a character of a font.
// [*]hCalcFont will be selected as FDC's font if FDC wouldn't be zero.
function TheFontStock.CalcFontAdvance(DC: HDC; pCharHeight: PInteger): Integer;
var
TM: TTextMetric;
ABC: TABC;
HasABC: Boolean;
begin
// Calculate advance of a character.
// The following code uses ABC widths instead TextMetric.tmAveCharWidth
// because ABC widths always tells truth but tmAveCharWidth does not.
// A true-type font will have ABC widths but others like raster type will not
// so if the function fails then use TextMetric.tmAveCharWidth.
GetTextMetrics(DC, TM);
HasABC := GetCharABCWidths(DC, Ord('M'), Ord('M'), ABC);
if not HasABC then
begin
with ABC do
begin
abcA := 0;
abcB := TM.tmAveCharWidth;
abcC := 0;
end;
TM.tmOverhang := 0;
end;
// Result(CharWidth)
with ABC do
Result := abcA + Integer(abcB) + abcC + TM.tmOverhang;
// pCharHeight
if Assigned(pCharHeight) then
pCharHeight^ := Abs(TM.tmHeight) {+ TM.tmInternalLeading};
end;
constructor TheFontStock.Create(InitialFont: TFont);
begin
inherited Create;
SetBaseFont(InitialFont);
end;
destructor TheFontStock.Destroy;
begin
ReleaseFontsInfo;
ASSERT(FDCRefCount = 0);
inherited;
end;
function TheFontStock.GetBaseFont: TFont;
begin
Result := FpInfo^.BaseFont;
end;
function TheFontStock.GetCharAdvance: Integer;
begin
Result := FpCrntFontData^.CharAdv;
end;
function TheFontStock.GetCharHeight: Integer;
begin
Result := FpCrntFontData^.CharHeight;
end;
function TheFontStock.GetFontData(idx: Integer): PheFontData;
begin
Result := @FpInfo^.FontsData[idx];
end;
function TheFontStock.GetIsTrueType: Boolean;
begin
Result := FpInfo^.IsTrueType
end;
function TheFontStock.InternalCreateFont(Style: TFontStyles): HFONT;
const
Bolds: array[Boolean] of Integer = (400, 700);
begin
with FBaseLF do
begin
lfWeight := Bolds[fsBold in Style];
lfItalic := Ord(BOOL(fsItalic in Style));
lfUnderline := Ord(BOOL(fsUnderline in Style));
lfStrikeOut := Ord(BOOL(fsStrikeOut in Style));
end;
Result := CreateFontIndirect(FBaseLF);
end;
function TheFontStock.InternalGetDC: HDC;
begin
if FDCRefCount = 0 then
begin
ASSERT(FDC = 0);
FDC := GetDC(0);
end;
Inc(FDCRefCount);
Result := FDC;
end;
procedure TheFontStock.InternalReleaseDC(Value: HDC);
begin
Dec(FDCRefCount);
if FDCRefCount <= 0 then
begin
ASSERT((FDC <> 0) and (FDC = Value));
ReleaseDC(0, FDC);
FDC := 0;
ASSERT(FDCRefCount = 0);
end;
end;
procedure TheFontStock.ReleaseFontHandles;
begin
if FUsingFontHandles then
with GetFontsInfoManager do
begin
UnlockFontsInfo(FpInfo);
FUsingFontHandles := False;
end;
end;
procedure TheFontStock.ReleaseFontsInfo;
begin
if Assigned(FpInfo) then
with GetFontsInfoManager do
begin
if FUsingFontHandles then
begin
UnlockFontsInfo(FpInfo);
FUsingFontHandles := False;
end;
ReleaseFontsInfo(FpInfo);
FpInfo := nil;
end;
end;
procedure TheFontStock.SetBaseFont(Value: TFont);
var
pInfo: PheSharedFontsInfo;
begin
if Assigned(Value) then
begin
pInfo := GetFontsInfoManager.GetFontsInfo(Value);
if pInfo = FpInfo then
GetFontsInfoManager.ReleaseFontsInfo(pInfo)
else
begin
ReleaseFontsInfo;
FpInfo := pInfo;
FBaseLF := FpInfo^.BaseLF;
SetStyle(Value.Style);
end;
end
else
raise EheFontStockException.Create('SetBaseFont: ''Value'' must be specified.');
end;
procedure TheFontStock.SetStyle(Value: TFontStyles);
var
idx: Integer;
DC: HDC;
hOldFont: HFONT;
p: PheFontData;
begin
{$IFDEF HE_ASSERT}
ASSERT(SizeOf(TFontStyles) = 1,
'TheTextDrawer.SetStyle: There''s more than four font styles but the current '+
'code expects only four styles.');
{$ELSE}
ASSERT(SizeOf(TFontStyles) = 1);
{$ENDIF}
idx := Byte(Value);
ASSERT(idx <= High(TheStockFontPatterns));
UseFontHandles;
p := FontData[idx];
if FpCrntFontData = p then
Exit;
FpCrntFontData := p;
with p^ do
if Handle <> 0 then
begin
FCrntFont := Handle;
FCrntStyle := Style;
Exit;
end;
// create font
FCrntFont := InternalCreateFont(Value);
DC := InternalGetDC;
hOldFont := SelectObject(DC, FCrntFont);
// retrieve height and advances of new font
with FpCrntFontData^ do
begin
Handle := FCrntFont;
CharAdv := CalcFontAdvance(DC, @CharHeight);
end;
SelectObject(DC, hOldFont);
InternalReleaseDC(DC);
end;
procedure TheFontStock.UseFontHandles;
begin
if not FUsingFontHandles then
with GetFontsInfoManager do
begin
LockFontsInfo(FpInfo);
FUsingFontHandles := True;
end;
end;
{ TheTextDrawer }
constructor TheTextDrawer.Create(CalcExtentBaseStyle: TFontStyles; BaseFont: TFont);
begin
inherited Create;
FFontStock := TheFontStock.Create(BaseFont);
FStockBitmap := TBitmap.Create;
FCalcExtentBaseStyle := CalcExtentBaseStyle;
SetBaseFont(BaseFont);
FColor := clWindowText;
FBkColor := clWindow;
end;
destructor TheTextDrawer.Destroy;
begin
FStockBitmap.Free;
FFontStock.Free;
ReleaseETODist;
inherited;
end;
procedure TheTextDrawer.ReleaseETODist;
begin
if Assigned(FETODist) then
begin
FreeMem(FETODist);
FETODist := nil;
end;
end;
procedure TheTextDrawer.BeginDrawing(DC: HDC);
begin
if (FDC = DC) then
ASSERT(FDC <> 0)
else
begin
ASSERT((FDC = 0) and (DC <> 0) and (FDrawingCount = 0));
FDC := DC;
FSaveDC := SaveDC(DC);
SelectObject(DC, FCrntFont);
Windows.SetTextColor(DC, ColorToRGB(FColor));
Windows.SetBkColor(DC, ColorToRGB(FBkColor));
DoSetCharExtra(FCharExtra);
end;
Inc(FDrawingCount);
end;
procedure TheTextDrawer.EndDrawing;
begin
ASSERT(FDrawingCount >= 1);
Dec(FDrawingCount);
if FDrawingCount <= 0 then
begin
if FDC <> 0 then
RestoreDC(FDC, FSaveDC);
FSaveDC := 0;
FDC := 0;
FDrawingCount := 0;
end;
end;
function TheTextDrawer.GetCharWidth: Integer;
begin
Result := FBaseCharWidth + FCharExtra;
end;
function TheTextDrawer.GetCharHeight: Integer;
begin
Result := FBaseCharHeight;
end;
procedure TheTextDrawer.SetBaseFont(Value: TFont);
begin
if Assigned(Value) then
begin
FlushCharABCWidthCache;
ReleaseETODist;
FStockBitmap.Canvas.Font.Assign(Value);
FStockBitmap.Canvas.Font.Style := [];
with FFontStock do
begin
SetBaseFont(Value);
Style := FCalcExtentBaseStyle;
FBaseCharWidth := CharAdvance;
FBaseCharHeight := CharHeight;
end;
SetStyle(Value.Style);
end
else
raise EheTextDrawerException.Create('SetBaseFont: ''Value'' must be specified.');
end;
procedure TheTextDrawer.SetBaseStyle(const Value: TFontStyles);
begin
if FCalcExtentBaseStyle <> Value then
begin
FCalcExtentBaseStyle := Value;
FlushCharABCWidthCache;
ReleaseETODist;
with FFontStock do
begin
Style := Value;
FBaseCharWidth := CharAdvance;
FBaseCharHeight := CharHeight;
end;
end;
end;
procedure TheTextDrawer.SetStyle(Value: TFontStyles);
begin
with FFontStock do
begin
SetStyle(Value);
Self.FCrntFont := FontHandle;
end;
AfterStyleSet;
end;
procedure TheTextDrawer.AfterStyleSet;
begin
if FDC <> 0 then
SelectObject(FDC, FCrntFont);
end;
procedure TheTextDrawer.SetForeColor(Value: TColor);
begin
if FColor <> Value then
begin
FColor := Value;
if FDC <> 0 then
SetTextColor(FDC, ColorToRGB(Value));
end;
end;
procedure TheTextDrawer.SetBackColor(Value: TColor);
begin
if FBkColor <> Value then
begin
FBkColor := Value;
if FDC <> 0 then
Windows.SetBkColor(FDC, ColorToRGB(Value));
end;
end;
procedure TheTextDrawer.SetCharExtra(Value: Integer);
begin
if FCharExtra <> Value then
begin
FCharExtra := Value;
DoSetCharExtra(FCharExtra);
end;
end;
procedure TheTextDrawer.DoSetCharExtra(Value: Integer);
begin
if FDC <> 0 then
SetTextCharacterExtra(FDC, Value);
end;
procedure TheTextDrawer.FlushCharABCWidthCache;
begin
FillChar(FCharABCWidthCache, SizeOf(TABC)*Length(FCharABCWidthCache), 0);
FillChar(FCharWidthCache, SizeOf(Integer)*Length(FCharWidthCache), 0);
end;
function TheTextDrawer.GetCachedABCWidth(c : Cardinal; var abc : TABC) : Boolean;
begin
if c>High(FCharABCWidthCache) then begin
Result:=GetCharABCWidthsW(FDC, c, c, abc);
Exit;
end;
abc:=FCharABCWidthCache[c];
if (abc.abcA or Integer(abc.abcB) or abc.abcC)=0 then begin
Result:=GetCharABCWidthsW(FDC, c, c, abc);
if Result then
FCharABCWidthCache[c]:=abc;
end else Result:=True;
end;
procedure TheTextDrawer.TextOut(X, Y: Integer; Text: PWideChar;
Length: Integer);
var
r: TRect;
begin
r := Rect(X, Y, X, Y);
UniversalExtTextOut(FDC, X, Y, [], r, Text, Length, nil);
end;
procedure TheTextDrawer.ExtTextOut(X, Y: Integer; Options: TTextOutOptions;
ARect: TRect; Text: PWideChar; Length: Integer);
procedure InitETODist(CharWidth: Integer);
var
Size: TSize;
i: Integer;
begin
ReallocMem(FETODist, Length * SizeOf(Integer));
for i := 0 to Length - 1 do
begin
Size := TextExtent(PWideChar(@Text[i]), 1);
if Size.cx <> CharWidth then
FETODist[i] := Ceil(Size.cx / CharWidth) * CharWidth
else FETODist[i] := CharWidth;
end;
end;
procedure AdjustLastCharWidthAndRect;
var
LastChar: Cardinal;
RealCharWidth, CharWidth: Integer;
CharInfo: TABC;
tm: TTextMetricA;
begin
if Length <= 0 then Exit;
LastChar := Ord(Text[Length - 1]);
CharWidth := FETODist[Length - 1];
RealCharWidth := CharWidth;
if Win32PlatformIsUnicode then
begin
if GetCachedABCWidth(LastChar, CharInfo) then
begin
RealCharWidth := CharInfo.abcA + Integer(CharInfo.abcB);
if CharInfo.abcC >= 0 then
Inc(RealCharWidth, CharInfo.abcC);
end
else if LastChar < Ord(High(AnsiChar)) then
begin
GetTextMetricsA(FDC, tm);
RealCharWidth := tm.tmAveCharWidth + tm.tmOverhang;
end;
end
else if WideChar(LastChar) <= High(AnsiChar) then
begin
if GetCharABCWidthsA(FDC, LastChar, LastChar, CharInfo) then
begin
RealCharWidth := CharInfo.abcA + Integer(CharInfo.abcB);
if CharInfo.abcC >= 0 then
Inc(RealCharWidth, CharInfo.abcC);
end
else if LastChar < Ord(High(AnsiChar)) then
begin
GetTextMetricsA(FDC, tm);
RealCharWidth := tm.tmAveCharWidth + tm.tmOverhang;
end;
end;
if RealCharWidth > CharWidth then
Inc(ARect.Right, RealCharWidth - CharWidth);
FETODist[Length - 1] := Max(RealCharWidth, CharWidth);
end;
begin
InitETODist(GetCharWidth);
AdjustLastCharWidthAndRect;
UniversalExtTextOut(FDC, X, Y, Options, ARect, Text, Length, FETODist);
end;
procedure TheTextDrawer.ReleaseTemporaryResources;
begin
FFontStock.ReleaseFontHandles;
end;
function TheTextDrawer.TextExtent(const Text: UnicodeString): TSize;
begin
Result := SynUnicode.TextExtent(FStockBitmap.Canvas, Text);
end;
function TheTextDrawer.TextExtent(Text: PWideChar; Count: Integer): TSize;
begin
Result := SynUnicode.GetTextSize(FStockBitmap.Canvas.Handle, Text, Count);
end;
function TheTextDrawer.TextWidth(const Text: UnicodeString): Integer;
var
c : Cardinal;
begin
if Length(Text)=1 then begin
c:=Ord(Text[1]);
if c<=High(FCharWidthCache) then begin
Result:=FCharWidthCache[c];
if Result=0 then begin
Result:=SynUnicode.TextExtent(FStockBitmap.Canvas, Text).cX;
FCharWidthCache[c]:=Result;
end;
Exit;
end;
end;
Result := SynUnicode.TextExtent(FStockBitmap.Canvas, Text).cX;
end;
function TheTextDrawer.TextWidth(Text: PWideChar; Count: Integer): Integer;
begin
Result := SynUnicode.GetTextSize(FStockBitmap.Canvas.Handle, Text, Count).cX;
end;
initialization
finalization
gFontsInfoManager.Free;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.