-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Renderer.pas
More file actions
1654 lines (1389 loc) · 55.3 KB
/
PascalType.Renderer.pas
File metadata and controls
1654 lines (1389 loc) · 55.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
unit PascalType.Renderer;
////////////////////////////////////////////////////////////////////////////////
// //
// Version: MPL 1.1 or LGPL 2.1 with linking exception //
// //
// The contents of this file are subject to the Mozilla Public License //
// Version 1.1 (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. //
// //
// Alternatively, the contents of this file may be used under the terms of //
// the Free Pascal modified version of the GNU Lesser General Public //
// License Version 2.1 (the "FPC modified LGPL License"), in which case the //
// provisions of this license are applicable instead of those above. //
// Please see the file LICENSE.txt for additional information concerning //
// this license. //
// //
// The code is part of the PascalType Project //
// //
// The initial developer of this code is Christian-W. Budde //
// //
// Portions created by Christian-W. Budde are Copyright (C) 2010-2017 //
// by Christian-W. Budde. All Rights Reserved. //
// //
////////////////////////////////////////////////////////////////////////////////
interface
{$I PT_Compiler.inc}
{-$define DEBUG_CURVE}
uses
Classes,
Graphics,
PascalType.Types,
PascalType.Types.Color,
PascalType.Classes,
PascalType.GlyphString,
PascalType.Painter,
PascalType.FontFace,
PascalType.FontFace.SFNT;
type
TFontPoint = packed record
X, Y: TScaleType;
end;
type
TGlyphMetric = record
HorizontalMetric: record
AdvanceWidth: TScaleType;
Bearing: TScaleType;
end;
VerticalMetric: record
AdvanceHeight: TScaleType;
TopSideBearing: TScaleType;
end;
end;
//------------------------------------------------------------------------------
//
// OpenType Typographic metrics
//
//------------------------------------------------------------------------------
// Vertical metrics
//------------------------------------------------------------------------------
//
// v
// .............................................................. oooo ..................
// ^ Internal leading --| oo::::oo
// | | o:::oo:::o
// ...|.. FFFFFFFFFFFFFFFFFFFFFF iiii ........................o::o o::o................
// | F::::::::::::::::::::F i::::i ^ ^ o:::oo:::o ^
// | F::::::::::::::::::::F iiii | oo::::oo |
// | FF::::::FFFFFFFFF::::F | oooo Cap height
// ...|.... F:::::F ..... FFFFFFiiiiiii .. ggggggggg . ggggg aaaaaaaaaaaaa ........ |
// | F:::::F i:::::i g:::::::::ggg::::g a::::::::::::a ^ |
// | F::::::FFFFFFFFFF i::::i g:::::::::::::::::g aaaaaaaaa:::::a | |
// Ascender F:::::::::::::::F i::::i g::::::ggggg::::::gg a::::a | |
// | F:::::::::::::::F i::::i g:::::g g:::::g aaaaaaa:::::a | |
// | F::::::FFFFFFFFFF i::::i g:::::g g:::::g aa::::::::::::a | |
// | F:::::F i::::i g:::::g g:::::g a::::aaaa::::::a x-height |
// | F:::::F i::::i g::::::g g:::::ga::::a a:::::a | |
// | FF:::::::FF i::::::ig:::::::ggggg:::::ga::::a a:::::a | |
// | F::::::::FF i::::::i g::::::::::::::::ga:::::aaaa::::::a | |
// | F::::::::FF i::::::i gg::::::::::::::g a::::::::::aa:::a v v
// Baseline -->*.. FFFFFFFFFFF ......... iiiiiiii gggggggg::::::g aaaaaaaaaa aaaa ...........
// | g:::::g |
// | gggggg g:::::g |
// Descender g:::::gg gg:::::g |-- em height
// (negative) g::::::ggg:::::::g |
// | gg:::::::::::::g |
// | ggg::::::ggg |
// v gggggg v
// .............................................................................
// ^
// | Line gap (leading)
// v
// .............................................................................
//
//------------------------------------------------------------------------------
// Horizontal metrics, advances, and bearings
//------------------------------------------------------------------------------
//
//
// |<------ Width ---->| |<----- Width ---->|
// xMin-->* .<--xMax . .
// . . . .
// . ffffffffffffffff . . .
// . f::::::::::::::::f . .
// f::::::::::::::::::f . .
// f::::::fffffff:::::f . . yMax
// f:::::f ffffff . ggggggggg ggggg .*...
// f:::::f . . g:::::::::ggg::::g ^
// f:::::::ffffff . g:::::::::::::::::g .
// f::::::::::::f . g::::::ggggg::::::gg .
// f::::::::::::f . g:::::g g:::::g .
// f:::::::ffffff . g:::::g g:::::g .
// f:::::f . g:::::g g:::::g .
// f:::::f . g::::::g g:::::g .
// f:::::::f . g:::::::ggggg:::::g Height
// f:::::::f . g::::::::::::::::g .
// f:::::::f . gg::::::::::::::g .
// Origin-->* fffffffff . * gggggggg::::::g .
// . . . . g:::::g .
// . . . . gggggg g:::::g .
// |-->| |-->| g:::::gg gg:::::g .
// . | | . g::::::ggg:::::::g .
// . OffsetX/LSB RSB . gg:::::::::::::g .
// . . ggg::::::ggg v
// |-------- AdvanceX -------->| gggggg ........*..
// yMin
//
// Horizontal layout Vertical layout
// ---------+------------------------------------+----------------------------
// LSB | Positive | Negative (mostly: -Width/2)
// RSB | Positive (AdvanceX-LSB-(xMax-xMin) | 0
// AdvanceX | Positive | 0
// AdvanceY | 0 | Positive
// ---------+------------------------------------+----------------------------
type
TPascalTypeFontMetric = record
Origin: TFloatPoint; // Offset of origin.
Baseline: TRenderFloat; // Baseline.
Ascender: TRenderFloat; // Ascender, relative to baseline.
Descender: TRenderFloat; // Descender, relative to baseline (negative).
XHeight: TRenderFloat; // Height of minors.
CapHeight: TRenderFloat; // Height of majors.
LineGap: TRenderFLoat; // Line gap.
end;
type
TPascalTypeRenderOption = (
roColor, // Enable font color features
roPoints, // Display curve control points
roFill, // Fill shapes
roStroke, // Stroke shapes
roColorize, // Colorize glyphs/graphemes
roMetrics // Display glyph metrics
);
TPascalTypeRenderOptions = set of TPascalTypeRenderOption;
(*
** TPascalTypeRenderColorMode
**
** Specifies how colors are assigned when roColorize is applied.
*)
type
TPascalTypeRenderColorMode = (
rcmGlyph, // Assign color per glyph.
rcmGrapheme // Assign color per grapheme (cluster).
);
(*
** TPascalTypeVerticalMetricType
**
** The method used to calculate key elements of the FontMetrics structure.
** Specifically Baseline, Ascender, Descender, and LineGap.
*)
type
TPascalTypeVerticalMetricType = (
vmtAuto, // Automatically select metric type based on
// the typographic flag in the OS/2 table.
// * This is the default.
vmtTypographic, // Use typographic metrics (sTypoAscender,
// sTypoDescender, sTypoLineGap).
vmtWindows // Use classic Windows metrics (usWinAscent,
// usWinDescent).
);
(*
** TPascalTypeRenderVerticalOrigin
**
** Specifies the vertical offset that is applied when positioning glyphs.
**
** The offset is basically used to move (usually down) the glyph origin/baseline
** from the user supplied Y coordinate. In other words: It specifies the method
** used to calculate the distance from the supplied Y coordinate to the baseline.
*)
type
TPascalTypeRenderVerticalOrigin = (
voZero, // No baseline offset.
// Top of glyphs will be clipped.
voBaseline, // Offset is that of the normal baseline/ascender.
// Top of glyphs might be clipped.
voAuto, // The baseline is adjusted based on glyph path
// points.
// Clipping will not occur. Performs a linear
// scan of the output glyph string to find the
// maximum height of their outer bounding box.
voTight, // The baseline is adjusted based on the
// font bounding box.
// Clipping should not occur.
voCenter, // Same as voBaseline plus half a line gap.
voWindows, // Closely matches the vertical origin of
// Windows ExtTextOut.
// Uses OS2.WindowsAscent if available. Falls
// back to hhea.Ascent and then to head.YMax.
// * This is the default.
voCustom // The user supplied VerticalOriginValue offset
// value is used.
);
type
TPascalTypeRenderHorizontalOrigin = (
hoZero, // First glyph starts at X=0.
// Left side of glyphs might be clipped.
// * This is the default.
hoAuto, // First glyph is positioned so the leftmost glyph
// path point starts at X=0. Left side of glyph
// will not be clipped.
hoTight, // First glyph is positioned so left side bearing
// starts at X=0. Left side of glyph should not be
// clipped.
hoCustom // The user supplied HorizontalOriginValue offset
// value is used.
);
//------------------------------------------------------------------------------
//
// TPascalTypeRenderer
//
//------------------------------------------------------------------------------
// A class that knows how to render a glyph
//------------------------------------------------------------------------------
type
TPascalTypeRenderer = class(TInterfacedPersistent, IPascalTypeFontFaceNotification)
strict private
class var
FDebugRectSize: Single;
FDebugCircleRadius: Single;
FDebugRectColor: Cardinal;
FDebugCircleColor: Cardinal;
FDebugFontMetricsColor: Cardinal;
FDebugFontMetricsBaselineColor: Cardinal;
FDebugGlyphMetricsColor: Cardinal;
public
class property DebugRectSize: Single read FDebugRectSize write FDebugRectSize;
class property DebugCircleRadius: Single read FDebugCircleRadius write FDebugCircleRadius;
class property DebugRectColor: Cardinal read FDebugRectColor write FDebugRectColor;
class property DebugCircleColor: Cardinal read FDebugCircleColor write FDebugCircleColor;
class property DebugFontMetricsColor: Cardinal read FDebugFontMetricsColor write FDebugFontMetricsColor;
class property DebugFontMetricsBaselineColor: Cardinal read FDebugFontMetricsBaselineColor write FDebugFontMetricsBaselineColor;
class property DebugGlyphMetricsColor: Cardinal read FDebugGlyphMetricsColor write FDebugGlyphMetricsColor;
strict private type
TRenderFlags = set of (
rfHasScalerX,
rfHasScalerY,
rfHasMetrics,
rfHasColor
);
strict private
FOptions: TPascalTypeRenderOptions;
FColorMode: TPascalTypeRenderColorMode;
FFlags: TRenderFlags;
FFontFace: TPascalTypeFontFace;
FFontHeight: Integer;
FPixelPerInchX: Integer;
FPixelPerInchY: Integer;
FScalerX: TScaleType;
FScalerY: TScaleType;
FScalerAbsY: TScaleType;
FVerticalMetricType: TPascalTypeVerticalMetricType;
FVerticalOrigin: TPascalTypeRenderVerticalOrigin;
FHorizontalOrigin: TPascalTypeRenderHorizontalOrigin;
FFontMetrics: TPascalTypeFontMetric;
FCustomOrigin: TFloatPoint;
FColorGlyphLookup: IPascalTypeColorGlyphLookup;
FColorLookup: IPascalTypeColorLookup;
procedure SetFontSize(const Value: Integer);
function GetFontSize: Integer;
function GetPixelPerInch: Integer;
procedure SetPixelPerInch(const Value: Integer);
procedure SetPixelPerInchX(const Value: Integer);
procedure SetPixelPerInchY(const Value: Integer);
procedure SetFontHeight(const Value: Integer);
procedure SetFontFace(const Value: TPascalTypeFontFace);
procedure SetHorizontalOrigin(const Value: TPascalTypeRenderHorizontalOrigin);
procedure SetVerticalOrigin(const Value: TPascalTypeRenderVerticalOrigin);
procedure SetVerticalMetricType(const Value: TPascalTypeVerticalMetricType);
procedure SetHorizontalOriginValue(const Value: TRenderFloat);
procedure SetVerticalOriginValue(const Value: TRenderFloat);
function GetHorizontalOriginValue: TRenderFloat;
function GetVerticalOriginValue: TRenderFloat;
function GetScalerX: TScaleType;
function GetScalerY: TScaleType;
function GetScalerAbsY: TScaleType;
function GetFontMetrics: TPascalTypeFontMetric;
private
// IPascalTypeFontFaceNotification
procedure FontFaceNotification(Sender: TCustomPascalTypeFontFacePersistent; Notification: TFontFaceNotification);
protected
procedure CalculateScalerX;
procedure CalculateScalerY;
procedure CalculateMetrics;
function GetGlyphMetric(GlyphIndex: Integer): TGlyphMetric;
function GetAdvanceWidth(GlyphIndex: Integer): TScaleType;
function GetKerning(Last, Next: Integer): TScaleType;
function GetGlyphPosition(AGlyph: TPascalTypeGlyph; X, Y: TRenderFloat; ADirection: TPascalTypeDirection): TFloatPoint;
function IsDrawable(AGlyph: TPascalTypeGlyph): boolean;
procedure FontHeightChanged; virtual;
procedure PixelPerInchXChanged; virtual;
procedure PixelPerInchYChanged; virtual;
procedure FontChanging;
procedure FontChanged;
property ScalerX: TScaleType read GetScalerX;
property ScalerY: TScaleType read GetScalerY;
property ScalerAbsY: TScaleType read GetScalerAbsY;
property FontMetrics: TPascalTypeFontMetric read GetFontMetrics;
protected
procedure RenderGlyph(GlyphIndex: Integer; const Painter: IPascalTypePainter; const Cursor: TFloatPoint);
procedure RenderGlyphPath(const GlyphPath: TPascalTypePath; const Painter: IPascalTypePainter; const Cursor: TFloatPoint);
procedure RenderDebugCircle(const Painter: IPascalTypePainter; const p: TFloatPoint);
procedure RenderDebugRect(const Painter: IPascalTypePainter; const p: TFloatPoint);
class constructor Create;
public
constructor Create; virtual;
destructor Destroy; override;
procedure RenderText(const Text: string; const Painter: IPascalTypePainter); overload;
procedure RenderText(const Text: string; const Painter: IPascalTypePainter; var X, Y: TRenderFloat); overload;
procedure RenderShapedText(ShapedText: TPascalTypeGlyphString; const Painter: IPascalTypePainter); overload;
procedure RenderShapedText(ShapedText: TPascalTypeGlyphString; const Painter: IPascalTypePainter; var X, Y: TRenderFloat); overload;
procedure RenderShapedGlyph(AGlyph: TPascalTypeGlyph; const Painter: IPascalTypePainter; X, Y: TRenderFloat; ADirection: TPascalTypeDirection = dirDefault);
procedure AdvanceGlyphPosition(AGlyph: TPascalTypeGlyph; var X, Y: TRenderFloat);
property Options: TPascalTypeRenderOptions read FOptions write FOptions;
property ColorMode: TPascalTypeRenderColorMode read FColorMode write FColorMode;
property VerticalMetricType: TPascalTypeVerticalMetricType read FVerticalMetricType write SetVerticalMetricType;
property VerticalOrigin: TPascalTypeRenderVerticalOrigin read FVerticalOrigin write SetVerticalOrigin;
property HorizontalOrigin: TPascalTypeRenderHorizontalOrigin read FHorizontalOrigin write SetHorizontalOrigin;
property VerticalOriginValue: TRenderFloat read GetVerticalOriginValue write SetVerticalOriginValue;
property HorizontalOriginValue: TRenderFloat read GetHorizontalOriginValue write SetHorizontalOriginValue;
property FontFace: TPascalTypeFontFace read FFontFace write SetFontFace; // TODO : This ought to be TCustomPascalTypeFontFace
property FontHeight: Integer read FFontHeight write SetFontHeight default -11;
property FontSize: Integer read GetFontSize write SetFontSize stored False;
property PixelPerInch: Integer read GetPixelPerInch write SetPixelPerInch default 96;
property PixelPerInchX: Integer read FPixelPerInchX write SetPixelPerInchX default 96;
property PixelPerInchY: Integer read FPixelPerInchY write SetPixelPerInchY default 96;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
Math,
SysUtils,
PascalType.Unicode,
PascalType.Tables.TrueType.glyf,
PascalType.Tables.OpenType.COLR,
PascalType.Tables.OpenType.CPAL,
PascalType.ResourceStrings;
function ColorHSLtoRGB(H, S, L: Integer): Cardinal;
function RGB(R, G, B: Byte): Cardinal;
begin
Result := $FF000000 or R or (G shl 8) or (B shl 16);
end;
var
V, M, M1, M2, VSF: Integer;
begin
S := EnsureRange(S, 0, 255);
L := EnsureRange(L, 0, 255);
if L <= $7F then
V := L * (256 + S) shr 8
else
V := L + S - Round(L * S / 255);
if V <= 0 then
Result := RGB(L, L, L)
else
begin
M := L * 2 - V;
H := H * 6;
VSF := (V - M) * (H and $FF) shr 8;
M1 := M + VSF;
M2 := V - VSF;
case H shr 8 of
0: Result := RGB(V, M1, M);
1: Result := RGB(M2, V, M);
2: Result := RGB(M, V, M1);
3: Result := RGB(M, M2, V);
4: Result := RGB(M1, M, V);
5: Result := RGB(V, M, M2);
else
Result := 0;
end;
end;
end;
function ClusterToColor(Index: integer): Cardinal;
const
Saturation: integer = 150;
Lightness: integer = 150;//180;
const
GoldenRatioConjugate = 0.618033988749895 * 255;
begin
var Hue := Round(Index * GoldenRatioConjugate) mod 256;
Result := ColorHSLtoRGB(Hue, Saturation, Lightness);
end;
//------------------------------------------------------------------------------
//
// TPascalTypeRenderer
//
//------------------------------------------------------------------------------
class constructor TPascalTypeRenderer.Create;
begin
FDebugCircleRadius := 2.0;
FDebugRectSize := 2.0;
FDebugCircleColor := Cardinal(clRed) or $A0000000;
FDebugRectColor := Cardinal(clBlue) or $A0000000;
FDebugFontMetricsColor := Cardinal(clSkyBlue) or $B0000000;
FDebugFontMetricsBaselineColor := Cardinal(clRed) or $B07F00FF;
FDebugGlyphMetricsColor := Cardinal(clGreen) or $B0000000;
end;
constructor TPascalTypeRenderer.Create;
begin
inherited;
FPixelPerInchX := 96;
FPixelPerInchY := 96;
FFontHeight := -11;
FVerticalMetricType := vmtAuto;
FVerticalOrigin := voWindows;
FHorizontalOrigin := hoTight;
FOptions := [roFill];
end;
destructor TPascalTypeRenderer.Destroy;
begin
FontFace := nil; // Unsubscribes
inherited;
end;
procedure TPascalTypeRenderer.SetFontFace(const Value: TPascalTypeFontFace);
begin
if (FFontFace = Value) then
exit;
if (FFontFace <> nil) then
FFontFace.Unsubscribe(Self);
FFontFace := Value;
if (FFontFace <> nil) then
FFontFace.Subscribe(Self);
FontChanged;
end;
procedure TPascalTypeRenderer.PixelPerInchXChanged;
begin
// nothing in here yet (trigger recalculation of cache here soon!)
end;
procedure TPascalTypeRenderer.PixelPerInchYChanged;
begin
// nothing in here yet (trigger recalculation of cache here soon!)
end;
procedure TPascalTypeRenderer.RenderDebugCircle(const Painter: IPascalTypePainter; const p: TFloatPoint);
begin
Painter.BeginPath;
Painter.SetColor(FDebugCircleColor);
Painter.Circle(p, DebugCircleRadius);
Painter.EndPath(True);
end;
procedure TPascalTypeRenderer.RenderDebugRect(const Painter: IPascalTypePainter; const p: TFloatPoint);
var
r: TFloatRect;
begin
Painter.BeginPath;
Painter.SetColor(FDebugRectColor);
r.TopLeft := p;
r.BottomRight := p;
r.Inflate(DebugRectSize, DebugRectSize);
Painter.Rectangle(r);
Painter.EndPath(True);
end;
type
TPointType = (
ptOnCurve,
ptQuadControl,
ptCubicControl
);
TPathState = (
psCurve, // The previous point was a curve-point.
psControl, // The previous point was a quadratic control-point.
psCubicControl1, // The previous point was the first cubic control-point.
psCubicControl2 // The previous point was the second cubic control-point.
);
TPathEmit = (
emitNone, // Draw nothing.
emitLine, // Draw a line.
emitQuadratic, // Draw a Quadratic bezier.
emitHalfway, // Draw a Quadratic bezier.
// The two previous points were control-points; This
// implies an implicit curve-point halfway between the
// control-points.
emitCubic // Draw a Cubic bezier.
);
TStateTransition = record
NextState: TPathState;
Emit: TPathEmit;
end;
const
StateMachine: array[TPointType, TPathState] of TStateTransition = (
// ptOnCurve
(
(NextState: psCurve; Emit: emitLine), // psCurve -> ptOnCurve
(NextState: psCurve; Emit: emitQuadratic), // psControl -> ptOnCurve
(NextState: psCurve; Emit: emitLine), // psCubicControl1 -> ptOnCurve (Invalid)
(NextState: psCurve; Emit: emitCubic) // psCubicControl2 -> ptOnCurve
),
// ptQuadControl
(
(NextState: psControl; Emit: emitNone), // psCurve -> ptQuadControl
(NextState: psControl; Emit: emitHalfway), // psControl -> ptQuadControl
(NextState: psControl; Emit: emitNone), // psCubicControl1 -> ptQuadControl (Invalid)
(NextState: psControl; Emit: emitNone) // psCubicControl2 -> ptQuadControl (Invalid)
),
// ptCubicControl
(
(NextState: psCubicControl1; Emit: emitNone), // psCurve -> ptCubicControl
(NextState: psCubicControl1; Emit: emitNone), // psControl -> ptCubicControl (Invalid)
(NextState: psCubicControl2; Emit: emitNone), // psCubicControl1 -> ptCubicControl
(NextState: psCubicControl1; Emit: emitNone) // psCubicControl2 -> ptCubicControl (Invalid)
)
);
procedure TPascalTypeRenderer.RenderGlyphPath(const GlyphPath: TPascalTypePath; const Painter: IPascalTypePainter; const Cursor: TFloatPoint);
var
Origin: TFloatPoint;
PointIndex: Integer;
CurrentPoint: TFloatPoint;
StartPoint: TFloatPoint;
ControlPoint: TFloatPoint;
ControlPoint2: TFloatPoint;
MidPoint: TFloatPoint;
PointType: TPointType;
Contour: TPascalTypeContour;
PathState: TPathState;
StateTransition: TStateTransition;
i: integer;
begin
if (Length(GlyphPath) = 0) then
exit;
Origin.X := Cursor.X + FontMetrics.Origin.X;
Origin.Y := Cursor.Y + FontMetrics.Origin.Y;
if (not (roPoints in FOptions)) then
Painter.BeginGlyph;
for Contour in GlyphPath do
begin
if (Length(Contour) < 2) then
continue;
// Determine the starting state and actual move-to point
CurrentPoint.X := Origin.X + Contour[0].XPos * ScalerX;
CurrentPoint.Y := Origin.Y + Contour[0].YPos * ScalerY;
if (Contour[0].Flags and PT_ON_CURVE <> 0) then
// It's a curve-point
PathState := psCurve
else
begin
// It's a control-point. See if the prior point in the closed polygon
// (i.e. last point in the array) is a curve-point.
ControlPoint := CurrentPoint;
if (Contour[High(Contour)].Flags and PT_ON_CURVE <> 0) then
begin
// Last point was a curve-point. Use it as the current point and use
// the first point as the control-point.
// Seen with: Kalinga Bold, small letter "r"
CurrentPoint.X := Origin.X + Contour[High(Contour)].XPos * ScalerX;
CurrentPoint.Y := Origin.Y + Contour[High(Contour)].YPos * ScalerY;
end else
begin
// Both first and last points are control-points.
// Synthesize a curve-point in between the two control-points.
// Seen with: SimSun-ExtB, small letter "a"
CurrentPoint.X := Origin.X + (Contour[0].XPos + Contour[High(Contour)].XPos) * 0.5 * ScalerX;
CurrentPoint.Y := Origin.Y + (Contour[0].YPos + Contour[High(Contour)].YPos) * 0.5 * ScalerY;
end;
PathState := psControl;
end;
StartPoint := CurrentPoint;
// Move to the first curve-point (the one we just found above)
if (not (roPoints in FOptions)) then
Painter.BeginPath;
if (roPoints in FOptions) then
RenderDebugCircle(Painter, CurrentPoint)
else
Painter.MoveTo(CurrentPoint);
// Note that PointIndex wraps around to zero
{$define OLD}
{$ifdef OLD}
PointIndex := 1;
{$else}
if (Contour[0].Flags and PT_ON_CURVE <> 0) then
PointIndex := 1
else
PointIndex := 0;
{$endif}
for i := 0 to High(Contour) do
begin
// Get the next point
{$ifdef OLD}
CurrentPoint.X := Origin.X + Contour[PointIndex].XPos * ScalerX;
CurrentPoint.Y := Origin.Y + Contour[PointIndex].YPos * ScalerY;
if (Contour[PointIndex].Flags and PT_CUBIC_CONTROL <> 0) then
PointType := ptCubicControl
else
if (Contour[PointIndex].Flags and PT_ON_CURVE <> 0) then
PointType := ptOnCurve
else
PointType := ptQuadControl;
{$else}
var Index := (PointIndex + i) mod Length(Contour);
CurrentPoint.X := Origin.X + Contour[Index].XPos * ScalerX;
CurrentPoint.Y := Origin.Y + Contour[Index].YPos * ScalerY;
if (Contour[Index].Flags and PT_CUBIC_CONTROL <> 0) then
PointType := ptCubicControl
else
if (Contour[Index].Flags and PT_ON_CURVE <> 0) then
PointType := ptOnCurve
else
PointType := ptQuadControl;
{$endif}
StateTransition := StateMachine[PointType, PathState];
PathState := StateTransition.NextState;
case StateTransition.Emit of
emitNone:
begin
// Store control points for quadratic/cubic curves
if (PathState = psCubicControl2) then
ControlPoint2 := CurrentPoint
else
ControlPoint := CurrentPoint;
if (roPoints in FOptions) then
RenderDebugRect(Painter, CurrentPoint);
end;
emitLine:
begin
if (roPoints in FOptions) then
RenderDebugCircle(Painter, CurrentPoint)
else
Painter.LineTo(CurrentPoint);
end;
emitQuadratic: // TrueType quadratic curve
begin
if (roPoints in FOptions) then
RenderDebugCircle(Painter, CurrentPoint)
else
Painter.QuadraticBezierTo(ControlPoint, CurrentPoint);
end;
emitHalfway: // Implicit on-curve point between two quadratic control points (TrueType)
begin
MidPoint.X := (ControlPoint.X + CurrentPoint.X) * 0.5;
MidPoint.Y := (ControlPoint.Y + CurrentPoint.Y) * 0.5;
if (roPoints in FOptions) then
begin
RenderDebugCircle(Painter, MidPoint);
RenderDebugRect(Painter, CurrentPoint);
end else
Painter.QuadraticBezierTo(ControlPoint, MidPoint);
ControlPoint := CurrentPoint;
end;
emitCubic: // PostScript cubic curve (CFF/CFF2)
begin
if (roPoints in FOptions) then
RenderDebugCircle(Painter, CurrentPoint)
else
Painter.CubicBezierTo(ControlPoint, ControlPoint2, CurrentPoint);
end;
end;
{$ifdef OLD}
PointIndex := (PointIndex + 1) mod Length(Contour);
{$endif}
end;
if (not (roPoints in FOptions)) then
begin
case PathState of
psControl:
Painter.QuadraticBezierTo(ControlPoint, StartPoint);
psCubicControl2:
Painter.CubicBezierTo(ControlPoint, ControlPoint2, StartPoint);
end;
Painter.EndPath(True);
end;
end;
// Handle closing the path if it ends with control points
if (not (roPoints in FOptions)) then
Painter.EndGlyph;
end;
procedure TPascalTypeRenderer.RenderGlyph(GlyphIndex: Integer; const Painter: IPascalTypePainter; const Cursor: TFloatPoint);
var
GlyphPath: TPascalTypePath;
ColoredGlyphs: TColoredGlyphs;
i: integer;
ColorABGR: TPaletteColor;
begin
var SaveColor := Painter.Color;
var FillColor := SaveColor;
if (not (roFill in FOptions)) then
FillColor := FillColor and $00FFFFFF;
(*
var StrokeColor := Painter.StrokeColor;
if (not (roStroke in FOptions)) then
StrokeColor := StrokeColor and $00FFFFFF;
*)
// Look for a color glyph with the specified GlyphID
if (roColor in FOptions) and (FColorGlyphLookup <> nil) and FColorGlyphLookup.GetColoredGlyph(GlyphIndex, ColoredGlyphs) then
begin
for i := 0 to High(ColoredGlyphs) do
begin
if (not ColoredGlyphs[i].IsForegroundColor) and (ColoredGlyphs[i].IsValid) then
begin
// Resolve layer color using palette #0
// TODO : Select palette based on lighness of default color
ColorABGR := FColorLookup.GetPaletteColor(0, ColoredGlyphs[i].PaletteIndex);
if (ColorABGR.Alpha = 0) then
continue;
Painter.Color := (ColorABGR.ARGB and $FF00FF00) or ((ColorABGR.ARGB and $00FF0000) shr 16) or ((ColorABGR.ARGB and $000000FF) shl 16);
end else
// Use default color
Painter.Color := FillColor;
// TODO : Recurse? Specs doesn't state if we should do this.
// RenderGlyph(ColoredGlyphs[i].GlyphID, Painter, Cursor);
GlyphPath := FontFace.GetGlyphPath(ColoredGlyphs[i].GlyphID);
RenderGlyphPath(GlyphPath, Painter, Cursor);
end;
Painter.Color := SaveColor;
exit;
end;
GlyphPath := FontFace.GetGlyphPath(GlyphIndex);
Painter.Color := FillColor;
begin
RenderGlyphPath(GlyphPath, Painter, Cursor);
end;
Painter.Color := SaveColor;
end;
function TPascalTypeRenderer.IsDrawable(AGlyph: TPascalTypeGlyph): boolean;
begin
// Ignore Emoji variation selector that was substituted to .notdef
// Works around issue rendering #$263A#$FE0F with Twemoji font.
Result := (AGlyph.GlyphID <> GlyphNotDef) or (not AGlyph.IsSubstituted) or (Length(AGlyph.CodePoints) <> 1) or
(not PascalTypeUnicode.IsVariationSelector(AGlyph.CodePoints[0]));
end;
function TPascalTypeRenderer.GetGlyphPosition(AGlyph: TPascalTypeGlyph; X, Y: TRenderFloat; ADirection: TPascalTypeDirection): TFloatPoint;
begin
// Position glyph relative to cursor
if (ADirection = dirTopDown) then
begin
// For vertical layout, the origin is already adjusted by XOffset/YOffset
Result.X := X + ScalerX * AGlyph.XOffset - FontMetrics.Origin.X;
Result.Y := Y + ScalerY * AGlyph.YOffset - FontMetrics.Origin.Y;
end else
begin
Result.X := X + ScalerX * AGlyph.XOffset;
Result.Y := Y + ScalerY * AGlyph.YOffset;
end;
end;
procedure TPascalTypeRenderer.AdvanceGlyphPosition(AGlyph: TPascalTypeGlyph; var X, Y: TRenderFloat);
begin
X := X + ScalerX * AGlyph.XAdvance;
Y := Y + ScalerY * AGlyph.YAdvance;
end;
procedure TPascalTypeRenderer.RenderShapedGlyph(AGlyph: TPascalTypeGlyph; const Painter: IPascalTypePainter; X, Y: TRenderFloat; ADirection: TPascalTypeDirection);
begin
if (FontFace = nil) then
exit;
if (not IsDrawable(AGlyph)) then
exit;
var Cursor := GetGlyphPosition(AGlyph, X, Y, ADirection);
// Rasterize glyph
RenderGlyph(AGlyph.GlyphID, Painter, Cursor);
end;
procedure TPascalTypeRenderer.RenderShapedText(ShapedText: TPascalTypeGlyphString; const Painter: IPascalTypePainter);
var
X, Y: TRenderFloat;
begin
X := 0;
Y := 0;
RenderShapedText(ShapedText, Painter, X, Y);
end;
procedure TPascalTypeRenderer.RenderShapedText(ShapedText: TPascalTypeGlyphString; const Painter: IPascalTypePainter; var X, Y: TRenderFloat);
function FloatPoint(X, Y: TRenderFloat): TFloatPoint;
begin
Result.X := X;
Result.Y := Y;
end;
procedure DrawLineHorizontal(X1, X2, Y: TRenderFloat; const Caption: string = '');
begin
Painter.BeginPath;
Painter.MoveTo(FloatPoint(X1, Y));
Painter.LineTo(FloatPoint(X2, Y));
Painter.EndPath(False);
end;
procedure DrawLineVertical(X, Y1, Y2: TRenderFloat; const Caption: string = '');
begin
Painter.BeginPath;
Painter.MoveTo(FloatPoint(X, Y1));
Painter.LineTo(FloatPoint(X, Y2));
Painter.EndPath(False);
end;
function GetAutoOffset: TFloatPoint;
begin
Result := Default(TFloatPoint);
if (not (FHorizontalOrigin in [hoAuto, hoTight])) and (FVerticalOrigin <> voAuto) then
exit;
if (FVerticalOrigin <> voAuto) then
begin
// hoTight does nothing without HorizontalMetrics
if (FHorizontalOrigin = hoTight) and (FontFace.HorizontalMetrics = nil) then
exit;
end;
var MinXReach: Single := MaxSingle;
var MaxYReach: Single := -MaxSingle;
var CursorX: TRenderFloat := 0.0;
var CursorY: TRenderFloat := 0.0;
var HasInk := False;
for var Glyph in ShapedText do
begin
var XMin, YMin, XMax, YMax: SmallInt;
if FontFace.GetGlyphExtents(Glyph.GlyphID, XMin, YMin, XMax, YMax) then
begin
var XPos: TRenderFloat := CursorX + Glyph.XOffset;
var YPos: TRenderFloat := CursorY + Glyph.YOffset;
// Only consider glyph if it has "ink".
// - We qualify this rule to only apply to substituted glyphs so it
// will still be possible to have leading explicit white space.
// - Also, once we have encountered a glyph with ink, we process all
// the following glyphs regardless of ink.
if (HasInk) or (not Glyph.IsSubstituted) or (XMax > XMin) or (YMax > YMin) then
begin
HasInk := True;
case FHorizontalOrigin of
hoAuto:
MinXReach := Min(MinXReach, XPos + XMin);
hoTight:
begin
var LSB: Integer;
if (FontFace.HorizontalMetrics <> nil) then
begin
// If 'hmtx' table contains fewer entries that there are glyphs, then the last
// entry repeats.
var GlyphID := Min(Glyph.GlyphID, FontFace.HorizontalMetrics.HorizontalMetricCount-1);
LSB := FontFace.HorizontalMetrics[GlyphID].Bearing;
end else
LSB := XMin;
MinXReach := Min(MinXReach, XPos + LSB);
end;
end;
if (FVerticalOrigin = voAuto) then
MaxYReach := Max(MaxYReach, YPos + YMax);
end;
end;
CursorX := CursorX + Glyph.XAdvance;
CursorY := CursorY + Glyph.YAdvance;
end;