-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.OpenType.PostScript.CharStrings.pas
More file actions
1059 lines (913 loc) · 31.8 KB
/
PascalType.Tables.OpenType.PostScript.CharStrings.pas
File metadata and controls
1059 lines (913 loc) · 31.8 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.Tables.OpenType.PostScript.CharStrings;
////////////////////////////////////////////////////////////////////////////////
// //
// 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 Anders Melander //
// //
// Portions created by Anders Melander are Copyright (C) 2025-2026 //
// by Anders Melander. All Rights Reserved. //
// //
////////////////////////////////////////////////////////////////////////////////
interface
{$I PT_Compiler.inc}
uses
Classes,
SysUtils,
Generics.Collections,
PascalType.Types,
PascalType.Classes,
PascalType.Tables.OpenType.PostScript,
PascalType.Tables.OpenType.Common.Variation;
type
EPascalTypePostscriptInterpreter = class(EPascalTypeError);
EPascalTypePostscriptStackOverflow = class(EPascalTypePostscriptInterpreter);
EPascalTypePostscriptStackUnderflow = class(EPascalTypePostscriptInterpreter);
EPascalTypePostscriptMaxRecursion = class(EPascalTypePostscriptInterpreter);
EPascalTypePostscriptHintMaskOverflow = class(EPascalTypePostscriptInterpreter);
type
TPascalTypePostscriptCharStringInterpreter = class
private
FStack: TList<Single>;
FPath: TList<TPascalTypeContour>;
FCurrentContour: TList<TContourPoint>;
FCurrentX, FCurrentY: Single;
FWidth: Single;
FHasWidth: Boolean;
FHasCheckedWidth: Boolean;
FDefaultWidthX: Single;
FNominalWidthX: Single;
FIsCFF2: Boolean;
FGlobalSubrs: TPascalTypePostscriptCharStringIndexTable;
FLocalSubrs: TPascalTypePostscriptCharStringIndexTable;
FItemVariationStore: TItemVariationStore;
FNormalizedCoords: TArray<TFixedPoint>;
FVsIndex: Integer;
FNumStems: Integer;
FTransientArray: array[0..31] of Single;
procedure Clear;
procedure NewContour;
procedure AddPoint(X, Y: Single; Flags: Byte);
procedure ClosePath;
procedure Push(Value: Single);
function Pop: Single;
procedure ClearStack;
procedure Interpret(const ACharString: TArray<Byte>; ADepth: Integer = 0);
function GetSubr(Index: Integer; Global: Boolean): TArray<Byte>;
function GetBias(Count: Integer): Integer;
procedure OpMoveTo(Horizontal: Boolean = False; Vertical: Boolean = False);
procedure OpLineTo(Horizontal: Boolean = False; Vertical: Boolean = False);
procedure OpCurveTo;
procedure OpCurveLine;
procedure OpLineCurve;
procedure OpVVurveTo;
procedure OpHHurveTo;
procedure OpVHCurveTo;
procedure OpHVCurveTo;
procedure OpFlex;
procedure OpHFlex;
procedure OpHFlex1;
procedure OpFlex1;
procedure CheckWidth(NumExpected: Integer);
procedure Require(NumExpected: Integer);
public
constructor Create;
destructor Destroy; override;
function Execute(const ACharString: TArray<Byte>;
const AGlobalSubrs, ALocalSubrs: TPascalTypePostscriptCharStringIndexTable;
const ADefaultWidthX, ANominalWidthX: Single;
AIsCFF2: Boolean = False;
const AItemVariationStore: TItemVariationStore = nil;
const ANormalizedCoords: TArray<TFixedPoint> = nil): TPascalTypePath;
property Width: Single read FWidth;
end;
const
// https://learn.microsoft.com/en-us/typography/opentype/otspec190/cff2charstr#appendix_CommandCodes
// CFF: 48, CFF2: 513
CFFMaxStackDepth = 513;
implementation
uses
Math,
PascalType.ResourceStrings;
const
// Type 2 CharString Opcodes (1-byte)
cOpcodeReserved_00= 0;
cOpcodeHStem = 1;
cOpcodeReserved_02= 2;
cOpcodeVStem = 3;
cOpcodeVMoveTo = 4;
cOpcodeRLineto = 5;
cOpcodeHLineto = 6;
cOpcodeVLineto = 7;
cOpcodeRRCurveto = 8;
cOpcodeReserved_09= 9;
cOpcodeCallSubr = 10;
cOpcodeReturn = 11;
cOpcodeEscape = 12;
cOpcodeReserved_0D= 13;
cOpcodeEndChar = 14;
cOpcodeVsIndex = 15;
cOpcodeBlend = 16;
cOpcodeReserved_11= 17;
cOpcodeHStemHM = 18;
cOpcodeHintMask = 19;
cOpcodeCntrMask = 20;
cOpcodeRMoveTo = 21;
cOpcodeHMoveTo = 22;
cOpcodeVStemHM = 23;
cOpcodeRCurveLine = 24;
cOpcodeRLineCurve = 25;
cOpcodeVVCurveto = 26;
cOpcodeHHCurveto = 27;
cOpcodeShortInt = 28;
cOpcodeCallGSubr = 29;
cOpcodeVHCurveto = 30;
cOpcodeHVCurveto = 31;
cOpcodeFixed = 255;
// 2-byte operators (preceded by cOpcodeEscape)
cOpcodeAnd = 3;
cOpcodeOr = 4;
cOpcodeNot = 5;
cOpcodeAbs = 9;
cOpcodeAdd = 10;
cOpcodeSub = 11;
cOpcodeDiv = 12;
cOpcodeNeg = 14;
cOpcodeEq = 15;
cOpcodeDrop = 18;
cOpcodePut = 20;
cOpcodeGet = 21;
cOpcodeIfElse = 22;
cOpcodeRandom = 23;
cOpcodeMul = 24;
cOpcodeSqrt = 26;
cOpcodeDup = 27;
cOpcodeExch = 28;
cOpcodeIndex = 29;
cOpcodeRoll = 30;
cOpcodeHFlex = 34;
cOpcodeFlex = 35;
cOpcodeHFlex1 = 36;
cOpcodeFlex1 = 37;
{ TPascalTypePostscriptCharStringInterpreter }
constructor TPascalTypePostscriptCharStringInterpreter.Create;
begin
FStack := TList<Single>.Create;
FPath := TList<TPascalTypeContour>.Create;
FCurrentContour := TList<TContourPoint>.Create;
end;
destructor TPascalTypePostscriptCharStringInterpreter.Destroy;
begin
FCurrentContour.Free;
FPath.Free;
FStack.Free;
inherited;
end;
procedure TPascalTypePostscriptCharStringInterpreter.Clear;
begin
FStack.Clear;
FPath.Clear;
FCurrentContour.Clear;
FCurrentX := 0;
FCurrentY := 0;
FWidth := 0;
FHasWidth := False;
FHasCheckedWidth := False;
FVsIndex := 0;
FNumStems := 0;
FillChar(FTransientArray, SizeOf(FTransientArray), 0);
end;
procedure TPascalTypePostscriptCharStringInterpreter.NewContour;
begin
if FCurrentContour.Count > 0 then
begin
FPath.Add(FCurrentContour.ToArray);
FCurrentContour.Clear;
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.AddPoint(X, Y: Single; Flags: Byte);
begin
var Point: TContourPoint;
Point.XPos := X;
Point.YPos := Y;
Point.Flags := Flags;
FCurrentContour.Add(Point);
FCurrentX := X;
FCurrentY := Y;
end;
procedure TPascalTypePostscriptCharStringInterpreter.ClosePath;
begin
NewContour;
end;
procedure TPascalTypePostscriptCharStringInterpreter.Push(Value: Single);
begin
if FStack.Count >= CFFMaxStackDepth then
raise EPascalTypePostscriptStackOverflow.Create('Operand stack overflow');
FStack.Add(Value);
end;
function TPascalTypePostscriptCharStringInterpreter.Pop: Single;
begin
if FStack.Count = 0 then
raise EPascalTypePostscriptStackUnderflow.Create(RCStrCFFStackUnderflow);
Result := FStack[FStack.Count - 1];
FStack.Delete(FStack.Count - 1);
end;
procedure TPascalTypePostscriptCharStringInterpreter.ClearStack;
begin
FStack.Clear;
end;
function TPascalTypePostscriptCharStringInterpreter.GetBias(Count: Integer): Integer;
const
CBiasSmall = 107;
CBiasMedium = 1131;
CBiasLarge = 32768;
begin
if Count < 1240 then
Result := CBiasSmall
else
if Count < 33900 then
Result := CBiasMedium
else
Result := CBiasLarge;
end;
function TPascalTypePostscriptCharStringInterpreter.GetSubr(Index: Integer; Global: Boolean): TArray<Byte>;
begin
var Table: TPascalTypePostscriptCharStringIndexTable;
if Global then
Table := FGlobalSubrs
else
Table := FLocalSubrs;
if (Table = nil) or (Table.Count = 0) then
raise EPascalTypePostscriptInterpreter.Create('Subroutine table missing or empty');
var ActualIndex := Index + GetBias(Table.Count);
Result := Table.CharString[ActualIndex];
end;
procedure TPascalTypePostscriptCharStringInterpreter.Require(NumExpected: Integer);
begin
if (FStack.Count < NumExpected) then
raise EPascalTypeError.Create(RCStrCFFStackUnderflow);
end;
procedure TPascalTypePostscriptCharStringInterpreter.CheckWidth(NumExpected: Integer);
begin
(*
Adobe Technical Note #5117 ("The Type 2 CharString Format"), Section 3.2 "Glyph Widths".
- The first stack-clearing operator in a CharString (one of hstem, hstemhm,
vstem, vstemhm, rmoveto, hmoveto, or vmoveto) can be preceded by an optional
number that specifies the glyph's width.
- The width is present if the number of arguments on the stack is one more
than the number of arguments required for the operator.
Operators with an even number of operands: hstem, vstem, rmoveto
Operators with an odd number of operands: hmoveto, vmoveto
*)
// CFF2 CharStrings do not contain a value for width.
// https://learn.microsoft.com/en-us/typography/opentype/otspec190/cff2charstr#appendix-c-changes-from-type-2-charstrings
if FIsCFF2 or FHasCheckedWidth or FHasWidth then
exit;
FHasCheckedWidth := True;
if (FStack.Count > NumExpected) then
begin
FWidth := FNominalWidthX + FStack[0];
FStack.Delete(0);
FHasWidth := True;
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.Interpret(const ACharString: TArray<Byte>; ADepth: Integer);
const
CMaxRecursionDepth = 32; // Spec says 10
var
i: Integer;
begin
if ADepth > CMaxRecursionDepth then
raise EPascalTypePostscriptMaxRecursion.Create('Maximum subroutine recursion depth exceeded');
i := 0;
while i < Length(ACharString) do
begin
var Opcode := ACharString[i];
Inc(i);
case Opcode of
cOpcodeHStem..cOpcodeReturn,
cOpcodeEndChar..cOpcodeHStemHM,
cOpcodeRMoveTo..cOpcodeHHCurveto,
cOpcodeCallGSubr..cOpcodeHVCurveto: // 1-byte operators
begin
case Opcode of
cOpcodeHStem, cOpcodeVStem, cOpcodeHStemHM, cOpcodeVStemHM:
begin
CheckWidth(FStack.Count and $FFFE);
FNumStems := FNumStems + FStack.Count div 2;
end;
cOpcodeVMoveTo: OpMoveTo(False, True);
cOpcodeHMoveTo: OpMoveTo(True, False);
cOpcodeRMoveTo: OpMoveTo;
cOpcodeRLineto: OpLineTo;
cOpcodeHLineto: OpLineTo(True, False);
cOpcodeVLineto: OpLineTo(False, True);
cOpcodeRRCurveto: OpCurveTo;
cOpcodeRCurveLine: OpCurveLine;
cOpcodeRLineCurve: OpLineCurve;
cOpcodeVVCurveto: OpVVurveTo;
cOpcodeHHCurveto: OpHHurveTo;
cOpcodeVHCurveto: OpVHCurveTo;
cOpcodeHVCurveto: OpHVCurveTo;
cOpcodeCallSubr:
Interpret(GetSubr(Round(Pop), False), ADepth + 1);
cOpcodeCallGSubr:
Interpret(GetSubr(Round(Pop), True), ADepth + 1);
cOpcodeReturn:
if not FIsCFF2 then
Exit;
cOpcodeEndChar:
begin
CheckWidth(0);
ClosePath;
Exit;
end;
cOpcodeVsIndex:
if FIsCFF2 then
begin
FVsIndex := Round(Pop);
end;
cOpcodeBlend:
if FIsCFF2 then
begin
if (FItemVariationStore = nil) then
raise EPascalTypePostscriptInterpreter.Create('Item Variation Store missing for blend');
var n := Round(Pop);
if (FVsIndex < 0) or (FVsIndex >= FItemVariationStore.DataSetCount) then
raise EPascalTypePostscriptInterpreter.Create('Invalid vsindex for blend');
var DataSet := FItemVariationStore.DataSets[FVsIndex];
var RegionCount := DataSet.RegionIndexCount;
// We already popped n, so we need n * RegionCount more.
if FStack.Count < n * RegionCount + n then
raise EPascalTypePostscriptStackUnderflow.Create(RCStrCFFStackUnderflow);
// Temporarily store deltas and values
var Values: TArray<Single>;
SetLength(Values, n);
var RegionList := FItemVariationStore.RegionList;
var DeltaIndex := FStack.Count - n * RegionCount;
var ValIndex := DeltaIndex - n;
for var k := 0 to n - 1 do
Values[k] := FStack[ValIndex + k];
for var k := 0 to n - 1 do
begin
for var j := 0 to RegionCount - 1 do
begin
var Delta := FStack[DeltaIndex + k * RegionCount + j];
Values[k] := Values[k] + Delta * RegionList.CalculateScalar(DataSet.RegionIndices[j], FNormalizedCoords);
end;
end;
// Remove operands from stack
for var k := 1 to n * (RegionCount + 1) do
FStack.Delete(ValIndex);
// Push results
for var k := 0 to n - 1 do
Push(Values[k]);
end;
end;
end;
cOpcodeEscape: // 2-byte operators
begin
if i >= Length(ACharString) then
break;
var Opcode2 := ACharString[i];
Inc(i);
case Opcode2 of
cOpcodeAnd:
begin
var v2 := Pop;
var v1 := Pop;
if (v1 <> 0) and (v2 <> 0) then
Push(1)
else
Push(0);
end;
cOpcodeOr:
begin
var v2 := Pop;
var v1 := Pop;
if (v1 <> 0) or (v2 <> 0) then
Push(1)
else
Push(0);
end;
cOpcodeNot:
if Pop <> 0 then
Push(0)
else
Push(1);
cOpcodeAbs:
Push(Abs(Pop));
cOpcodeAdd:
begin
var v2 := Pop;
var v1 := Pop;
Push(v1 + v2);
end;
cOpcodeSub:
begin
var v2 := Pop;
var v1 := Pop;
Push(v1 - v2);
end;
cOpcodeDiv:
begin
var v2 := Pop;
var v1 := Pop;
if v2 <> 0 then
Push(v1 / v2)
else
Push(0);
end;
cOpcodeNeg:
Push(-Pop);
cOpcodeEq:
begin
var v2 := Pop;
var v1 := Pop;
if v1 = v2 then
Push(1)
else
Push(0);
end;
cOpcodeDrop:
Pop;
cOpcodePut:
begin
var idx := Round(Pop);
var val := Pop;
if (idx >= 0) and (idx <= High(FTransientArray)) then
FTransientArray[idx] := val;
end;
cOpcodeGet:
begin
var idx := Round(Pop);
if (idx >= 0) and (idx <= High(FTransientArray)) then
Push(FTransientArray[idx])
else
Push(0);
end;
cOpcodeIfElse:
begin
var v4 := Pop;
var v3 := Pop;
var v2 := Pop;
var v1 := Pop;
if v3 <= v4 then
Push(v1)
else
Push(v2);
end;
cOpcodeRandom:
Push(1.0 - Random); // range (0, 1]
cOpcodeMul:
begin
var v2 := Pop;
var v1 := Pop;
Push(v1 * v2);
end;
cOpcodeSqrt:
Push(Sqrt(Abs(Pop)));
cOpcodeDup:
begin
var val := Pop;
Push(val);
Push(val);
end;
cOpcodeExch:
begin
var v2 := Pop;
var v1 := Pop;
Push(v2);
Push(v1);
end;
cOpcodeIndex:
begin
var idx := Round(Pop);
if (idx < 0) or (idx >= FStack.Count) then
raise EPascalTypePostscriptStackUnderflow.Create(RCStrCFFStackUnderflow);
Push(FStack[FStack.Count - 1 - idx]);
end;
cOpcodeRoll:
begin
var j := Round(Pop);
var n := Round(Pop);
if (n > 1) and (n <= FStack.Count) then
begin
j := j mod n;
if j < 0 then
j := j + n;
if j <> 0 then
begin
var temp: TArray<Single>;
SetLength(temp, n);
for var k := 0 to n - 1 do
temp[k] := FStack[FStack.Count - n + k];
for var k := 0 to n - 1 do
FStack[FStack.Count - n + (k + j) mod n] := temp[k];
end;
end;
end;
cOpcodeHFlex: OpHFlex;
cOpcodeFlex: OpFlex;
cOpcodeHFlex1: OpHFlex1;
cOpcodeFlex1: OpFlex1;
else
ClearStack;
end;
// Stack clearing 2-byte commands
case Opcode2 of
cOpcodeHFlex, cOpcodeFlex, cOpcodeHFlex1, cOpcodeFlex1:
ClearStack;
end;
end;
cOpcodeHintMask, cOpcodeCntrMask:
begin
CheckWidth(FStack.Count and $FFFE);
FNumStems := FNumStems + FStack.Count div 2;
var MaskSize := (FNumStems + 7) div 8;
if i + MaskSize > Length(ACharString) then
raise EPascalTypePostscriptHintMaskOverflow.Create('Hint mask extends beyond CharString bounds');
i := i + MaskSize;
end;
cOpcodeShortInt:
begin
if i + 1 >= Length(ACharString) then
Break;
var s1 := SmallInt((ACharString[i] shl 8) or ACharString[i+1]);
Inc(i, 2);
Push(s1);
end;
32..246:
Push(Opcode - 139);
247..250:
begin
if i >= Length(ACharString) then
Break;
var v := (Opcode - 247) shl 8 + ACharString[i] + 108;
Inc(i);
Push(v);
end;
251..254:
begin
if i >= Length(ACharString) then
Break;
var v := -((Opcode - 251) shl 8) - ACharString[i] - 108;
Inc(i);
Push(v);
end;
cOpcodeFixed: // fixed 16.16
begin
if i + 3 >= Length(ACharString) then
Break;
var v := integer((ACharString[i] shl 24) or (ACharString[i+1] shl 16) or (ACharString[i+2] shl 8) or ACharString[i+3]);
Inc(i, 4);
Push(v / 65536.0);
end;
else
// Invalid
ClearStack;
end;
// Stack clearing commands
case Opcode of
cOpcodeHStem, cOpcodeVStem, cOpcodeHStemHM, cOpcodeVStemHM,
cOpcodeVMoveTo, cOpcodeHMoveTo, cOpcodeRMoveTo,
cOpcodeHintMask, cOpcodeCntrMask:
ClearStack;
cOpcodeRLineto, cOpcodeHLineto, cOpcodeVLineto,
cOpcodeRRCurveto, cOpcodeRCurveLine, cOpcodeRLineCurve,
cOpcodeVVCurveto, cOpcodeHHCurveto, cOpcodeVHCurveto, cOpcodeHVCurveto:
ClearStack;
end;
// If the charstring has a width other than that of defaultWidthX
// (see Technical Note #5176, [...]), it must be specified as the
// first number in the charstring, and encoded as the difference
// from nominalWidthX.
FHasCheckedWidth := True;
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpMoveTo(Horizontal, Vertical: Boolean);
begin
if Horizontal or Vertical then
begin
CheckWidth(1);
Require(1);
end else
begin
CheckWidth(2);
Require(2);
end;
NewContour;
if Horizontal then
AddPoint(FCurrentX + FStack[0], FCurrentY, PT_ON_CURVE)
else
if Vertical then
AddPoint(FCurrentX, FCurrentY + FStack[0], PT_ON_CURVE)
else
AddPoint(FCurrentX + FStack[0], FCurrentY + FStack[1], PT_ON_CURVE);
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpLineTo(Horizontal, Vertical: Boolean);
begin
var i := 0;
if Horizontal then
begin
// Alternating horizontal and vertical lines starting with horizontal (hlineto)
var IsHorizontal := True;
while i < FStack.Count do
begin
if IsHorizontal then
AddPoint(FCurrentX + FStack[i], FCurrentY, PT_ON_CURVE)
else
AddPoint(FCurrentX, FCurrentY + FStack[i], PT_ON_CURVE);
IsHorizontal := not IsHorizontal;
Inc(i);
end;
end else
if Vertical then
begin
// Alternating vertical and horizontal lines starting with vertical (vlineto)
var IsHorizontal := False;
while i < FStack.Count do
begin
if IsHorizontal then
AddPoint(FCurrentX + FStack[i], FCurrentY, PT_ON_CURVE)
else
AddPoint(FCurrentX, FCurrentY + FStack[i], PT_ON_CURVE);
IsHorizontal := not IsHorizontal;
Inc(i);
end;
end else
begin
// Sequence of relative lines (dx, dy)
while i + 1 < FStack.Count do
begin
AddPoint(FCurrentX + FStack[i], FCurrentY + FStack[i+1], PT_ON_CURVE);
Inc(i, 2);
end;
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpCurveTo;
begin
var i := 0;
// Sequence of relative cubic curves (dx1, dy1, dx2, dy2, dx3, dy3)
while i + 5 < FStack.Count do
begin
AddPoint(FCurrentX + FStack[i], FCurrentY + FStack[i+1], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+2], FCurrentY + FStack[i+3], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+4], FCurrentY + FStack[i+5], PT_ON_CURVE);
Inc(i, 6);
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpCurveLine;
begin
var i := 0;
// Sequence of curves followed by one final line
while i + 5 < FStack.Count - 2 do
begin
AddPoint(FCurrentX + FStack[i], FCurrentY + FStack[i+1], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+2], FCurrentY + FStack[i+3], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+4], FCurrentY + FStack[i+5], PT_ON_CURVE);
Inc(i, 6);
end;
if i + 1 < FStack.Count then
AddPoint(FCurrentX + FStack[i], FCurrentY + FStack[i+1], PT_ON_CURVE);
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpLineCurve;
begin
var i := 0;
// Sequence of lines followed by one final curve
while i + 1 < FStack.Count - 6 do
begin
AddPoint(FCurrentX + FStack[i], FCurrentY + FStack[i+1], PT_ON_CURVE);
Inc(i, 2);
end;
if i + 5 < FStack.Count then
begin
AddPoint(FCurrentX + FStack[i], FCurrentY + FStack[i+1], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+2], FCurrentY + FStack[i+3], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+4], FCurrentY + FStack[i+5], PT_ON_CURVE);
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpVVurveTo;
begin
var i := 0;
var dx: Single := 0;
// Vertical curve: dxa is optionally 1st operand, then blocks of (dy1, dx2, dy2, dy3)
if (FStack.Count mod 4 <> 0) then
begin
dx := FStack[0];
Inc(i);
end;
while i + 3 < FStack.Count do
begin
var BaselineX := FCurrentX + dx;
AddPoint(BaselineX, FCurrentY + FStack[i], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+1], FCurrentY + FStack[i+2], PT_CUBIC_CONTROL);
AddPoint(FCurrentX, FCurrentY + FStack[i+3], PT_ON_CURVE);
Inc(i, 4);
dx := 0;
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpHHurveTo;
begin
var i := 0;
var dy: Single := 0;
// Horizontal curve: dya is optionally 1st operand, then blocks of (dx1, dx2, dy2, dx3)
if (FStack.Count mod 4 <> 0) then
begin
dy := FStack[0];
Inc(i);
end;
while i + 3 < FStack.Count do
begin
var BaselineY := FCurrentY + dy;
AddPoint(FCurrentX + FStack[i], BaselineY, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+1], FCurrentY + FStack[i+2], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[i+3], FCurrentY, PT_ON_CURVE);
Inc(i, 4);
dy := 0;
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpVHCurveTo;
begin
var i := 0;
var IsVertical := True;
// Alternating vertical-horizontal and horizontal-vertical curves (vhcurveto)
while i + 3 < FStack.Count do
begin
var d1 := FStack[i];
var d2 := FStack[i+1];
var d3 := FStack[i+2];
var d4 := FStack[i+3];
Inc(i, 4);
// If an odd number of operands is provided, the last operand is a 5th coordinate
var d5: Single;
if i = FStack.Count - 1 then
begin
d5 := FStack[i];
Inc(i);
end else
d5 := 0;
if IsVertical then
begin
// Vertical-Horizontal: d1=dy1, d2=dx2, d3=dy2, d4=dx3, d5=dy3
AddPoint(FCurrentX, FCurrentY + d1, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + d2, FCurrentY + d3, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + d4, FCurrentY + d5, PT_ON_CURVE);
end else
begin
// Horizontal-Vertical: d1=dxa, d2=dxb, d3=dyb, d4=dyc, d5=dxd
AddPoint(FCurrentX + d1, FCurrentY, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + d2, FCurrentY + d3, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + d5, FCurrentY + d4, PT_ON_CURVE);
end;
IsVertical := not IsVertical;
end;
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpFlex;
begin
Require(12);
// Flex sequence (dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4, dx5, dy5, dx6, dy6)
AddPoint(FCurrentX + FStack[0], FCurrentY + FStack[1], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[2], FCurrentY + FStack[3], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[4], FCurrentY + FStack[5], PT_ON_CURVE);
AddPoint(FCurrentX + FStack[6], FCurrentY + FStack[7], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[8], FCurrentY + FStack[9], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[10], FCurrentY + FStack[11], PT_ON_CURVE);
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpHFlex;
begin
Require(7);
// Horizontal flex: (dx1, 0, dx2, dy2, dx3, 0, dx4, 0, dx5, -dy2, dx6, 0)
AddPoint(FCurrentX + FStack[0], FCurrentY, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[1], FCurrentY + FStack[2], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[3], FCurrentY, PT_ON_CURVE);
AddPoint(FCurrentX + FStack[4], FCurrentY, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[5], FCurrentY - FStack[2], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[6], FCurrentY, PT_ON_CURVE);
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpHFlex1;
begin
Require(9);
// Horizontal flex variant: (dx1, dy1, dx2, dy2, dx3, 0, dx4, 0, dx5, dy5, dx6, 0)
AddPoint(FCurrentX + FStack[0], FCurrentY + FStack[1], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[2], FCurrentY + FStack[3], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[4], FCurrentY, PT_ON_CURVE);
AddPoint(FCurrentX + FStack[5], FCurrentY, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[6], FCurrentY + FStack[7], PT_CUBIC_CONTROL);
AddPoint(FCurrentX + FStack[8], FCurrentY, PT_ON_CURVE);
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpFlex1;
begin
Require(11);
// Flex sequence is dx1 dy1 dx2 dy2 dx3 dy3 dx4 dy4 dx5 dy5 d6
var dx1 := FStack[0]; var dy1 := FStack[1];
var dx2 := FStack[2]; var dy2 := FStack[3];
var dx3 := FStack[4]; var dy3 := FStack[5];
var dx4 := FStack[6]; var dy4 := FStack[7];
var dx5 := FStack[8]; var dy5 := FStack[9];
var d6 := FStack[10];
// start_x/y is current point at beginning of flex sequence
var start_x := FCurrentX;
var start_y := FCurrentY;
// First curve: (dx1, dy1), (dx2, dy2), (dx3, dy3) relative
AddPoint(start_x + dx1, start_y + dy1, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + dx2, FCurrentY + dy2, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + dx3, FCurrentY + dy3, PT_ON_CURVE);
// Second curve: (dx4, dy4), (dx5, dy5), (d6, 0 or 0, d6) relative
AddPoint(FCurrentX + dx4, FCurrentY + dy4, PT_CUBIC_CONTROL);
AddPoint(FCurrentX + dx5, FCurrentY + dy5, PT_CUBIC_CONTROL);
var sum_dx := FCurrentX - start_x;
var sum_dy := FCurrentY - start_y;
// The last curve point is either d6 on the major axis or 0 on the minor axis
if Abs(sum_dx) > Abs(sum_dy) then
AddPoint(start_x + d6, start_y, PT_ON_CURVE)
else
AddPoint(start_x, start_y + d6, PT_ON_CURVE);
end;
procedure TPascalTypePostscriptCharStringInterpreter.OpHVCurveTo;
begin
var i := 0;
var IsHorizontal := True;
// Alternating horizontal-vertical and vertical-horizontal curves (hvcurveto)
while i + 3 < FStack.Count do
begin
var d1 := FStack[i];
var d2 := FStack[i+1];
var d3 := FStack[i+2];
var d4 := FStack[i+3];
Inc(i, 4);
// If an odd number of operands is provided, the last operand is a 5th coordinate
var d5: Single;
if i = FStack.Count - 1 then
begin
d5 := FStack[i];
Inc(i);