-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.OpenType.JSTF.pas
More file actions
760 lines (621 loc) · 25.3 KB
/
PascalType.Tables.OpenType.JSTF.pas
File metadata and controls
760 lines (621 loc) · 25.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
unit PascalType.Tables.OpenType.JSTF;
////////////////////////////////////////////////////////////////////////////////
// //
// 'JSTF' table type //
// //
////////////////////////////////////////////////////////////////////////////////
// //
// 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}
uses
Generics.Collections,
Generics.Defaults,
Classes,
PascalType.Types,
PascalType.Classes,
PascalType.Tables,
PascalType.Tables.OpenType,
PascalType.Tables.OpenType.Common;
//------------------------------------------------------------------------------
//
// TCustomOpenTypeJustificationLanguageSystemTable
//
//------------------------------------------------------------------------------
type
// not entirely implemented, for more information see
// http://www.microsoft.com/typography/otspec/jstf.htm
TCustomOpenTypeJustificationLanguageSystemTable = class(TCustomOpenTypeNamedTable)
private
protected
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
end;
TOpenTypeJustificationLanguageSystemTableClass = class of TCustomOpenTypeJustificationLanguageSystemTable;
//------------------------------------------------------------------------------
//
// TOpenTypeJustificationLanguageSystemTable
//
//------------------------------------------------------------------------------
type
TOpenTypeJustificationLanguageSystemTable = class(TCustomOpenTypeJustificationLanguageSystemTable)
protected
class function GetDisplayName: string; override;
public
class function GetTableType: TTableType; override;
end;
//------------------------------------------------------------------------------
//
// TOpenTypeExtenderGlyphTable
//
//------------------------------------------------------------------------------
type
TOpenTypeExtenderGlyphTable = class(TCustomPascalTypeTable)
private
FGlyphID: TGlyphString; // GlyphIDs-in increasing numerical order
protected
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
end;
//------------------------------------------------------------------------------
//
// TCustomOpenTypeJustificationScriptTable
//
//------------------------------------------------------------------------------
type
TCustomOpenTypeJustificationScriptTable = class(TCustomOpenTypeNamedTable)
private
FExtenderGlyphTable : TOpenTypeExtenderGlyphTable;
FDefaultLangSys : TCustomOpenTypeJustificationLanguageSystemTable;
FLanguageSystemTables: TPascalTypeTableInterfaceList<TCustomOpenTypeJustificationLanguageSystemTable>;
function GetLanguageSystemTable(Index: Integer): TCustomOpenTypeJustificationLanguageSystemTable;
function GetLanguageSystemTableCount: Integer;
procedure SetDefaultLangSys(const Value: TCustomOpenTypeJustificationLanguageSystemTable);
protected
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
property DefaultLangSys: TCustomOpenTypeJustificationLanguageSystemTable read FDefaultLangSys write SetDefaultLangSys;
property LanguageSystemTableCount: Integer read GetLanguageSystemTableCount;
property LanguageSystemTable[Index: Integer]: TCustomOpenTypeJustificationLanguageSystemTable read GetLanguageSystemTable;
end;
//------------------------------------------------------------------------------
//
// TOpenTypeJustificationScriptTable
//
//------------------------------------------------------------------------------
type
TOpenTypeJustificationScriptTable = class(TCustomOpenTypeJustificationScriptTable)
protected
class function GetDisplayName: string; override;
public
class function GetTableType: TTableType; override;
end;
//------------------------------------------------------------------------------
//
// TOpenTypeJustificationTable
//
//------------------------------------------------------------------------------
type
TOpenTypeJustificationTable = class(TCustomPascalTypeNamedTable)
private type
TJustificationScriptDirectoryEntry = packed record
Tag: TTableType;
Offset: Word;
end;
private
FVersion : TFixedPoint; // Version of the JSTF table-initially set to 0x00010000
FScripts : TPascalTypeTableInterfaceList<TCustomOpenTypeJustificationScriptTable>;
procedure SetVersion(const Value: TFixedPoint);
function GetScriptCount: Cardinal;
protected
procedure VersionChanged; virtual;
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
class function GetTableType: TTableType; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
property Version: TFixedPoint read FVersion write SetVersion;
property ScriptCount: Cardinal read GetScriptCount;
end;
//------------------------------------------------------------------------------
//
// Justification language system
//
//------------------------------------------------------------------------------
procedure RegisterJustificationLanguageSystem(LanguageSystemClass: TOpenTypeJustificationLanguageSystemTableClass);
procedure RegisterJustificationLanguageSystems(LanguageSystemClasses: array of TOpenTypeJustificationLanguageSystemTableClass);
function FindJustificationLanguageSystemByType(TableType: TTableType): TOpenTypeJustificationLanguageSystemTableClass;
var
GJustificationLanguageSystemClasses: array of TOpenTypeJustificationLanguageSystemTableClass;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
SysUtils,
PascalType.ResourceStrings;
//------------------------------------------------------------------------------
//
// Justification language system
//
//------------------------------------------------------------------------------
function IsJustificationLanguageSystemClassRegistered(LanguageSystemClass: TOpenTypeJustificationLanguageSystemTableClass): Boolean;
var
TableClassIndex: Integer;
begin
Result := False;
for TableClassIndex := 0 to High(GJustificationLanguageSystemClasses) do
if GJustificationLanguageSystemClasses[TableClassIndex] = LanguageSystemClass
then
begin
Result := True;
Exit;
end;
end;
procedure RegisterJustificationLanguageSystem(LanguageSystemClass: TOpenTypeJustificationLanguageSystemTableClass);
begin
Assert(IsJustificationLanguageSystemClassRegistered(LanguageSystemClass) = False);
SetLength(GJustificationLanguageSystemClasses, Length(GJustificationLanguageSystemClasses) + 1);
GJustificationLanguageSystemClasses[High(GJustificationLanguageSystemClasses)] := LanguageSystemClass;
end;
procedure RegisterJustificationLanguageSystems(LanguageSystemClasses: array of TOpenTypeJustificationLanguageSystemTableClass);
var
LangSysIndex: Integer;
begin
for LangSysIndex := 0 to High(LanguageSystemClasses) do
RegisterJustificationLanguageSystem(LanguageSystemClasses[LangSysIndex]);
end;
function FindJustificationLanguageSystemByType(TableType: TTableType): TOpenTypeJustificationLanguageSystemTableClass;
var
LangSysIndex: Integer;
begin
Result := nil;
for LangSysIndex := 0 to High(GJustificationLanguageSystemClasses) do
if GJustificationLanguageSystemClasses[LangSysIndex].GetTableType = TableType then
begin
Result := GJustificationLanguageSystemClasses[LangSysIndex];
Exit;
end;
// raise EPascalTypeError.Create('Unknown Table Class: ' + TableType);
end;
//------------------------------------------------------------------------------
//
// TCustomOpenTypeJustificationLanguageSystemTable
//
//------------------------------------------------------------------------------
constructor TCustomOpenTypeJustificationLanguageSystemTable.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
end;
destructor TCustomOpenTypeJustificationLanguageSystemTable.Destroy;
begin
inherited;
end;
procedure TCustomOpenTypeJustificationLanguageSystemTable.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
// check (minimum) table size
if Stream.Position + 2 > Stream.Size then
raise EPascalTypeError.Create(RCStrTableIncomplete);
end;
procedure TCustomOpenTypeJustificationLanguageSystemTable.SaveToStream
(Stream: TStream);
begin
inherited;
raise EPascalTypeNotImplemented.Create(RCStrNotImplemented);
end;
//------------------------------------------------------------------------------
//
// TOpenTypeJustificationLanguageSystemTable
//
//------------------------------------------------------------------------------
class function TOpenTypeJustificationLanguageSystemTable.GetDisplayName: string;
begin
Result := 'Default';
end;
class function TOpenTypeJustificationLanguageSystemTable.GetTableType: TTableType;
begin
Result := 'DFLT';
end;
//------------------------------------------------------------------------------
//
// TOpenTypeExtenderGlyphTable
//
//------------------------------------------------------------------------------
procedure TOpenTypeExtenderGlyphTable.Assign(Source: TPersistent);
begin
inherited;
if Source is TOpenTypeExtenderGlyphTable then
FGlyphID := Copy(TOpenTypeExtenderGlyphTable(Source).FGlyphID);
end;
procedure TOpenTypeExtenderGlyphTable.LoadFromStream(Stream: TStream; Size: Cardinal);
var
GlyphIdIndex: Integer;
begin
inherited;
// check (minimum) table size
if Stream.Position + 2 > Stream.Size then
raise EPascalTypeError.Create(RCStrTableIncomplete);
// set length of glyphID array
SetLength(FGlyphID, BigEndianValue.ReadWord(Stream));
// read glyph IDs from stream
for GlyphIdIndex := 0 to High(FGlyphID) do
FGlyphID[GlyphIdIndex] := BigEndianValue.ReadWord(Stream)
end;
procedure TOpenTypeExtenderGlyphTable.SaveToStream(Stream: TStream);
var
GlyphIdIndex: Integer;
begin
inherited;
// write length of glyphID array to stream
BigEndianValue.WriteWord(Stream, Length(FGlyphID));
// write glyph IDs to stream
for GlyphIdIndex := 0 to High(FGlyphID) do
BigEndianValue.WriteWord(Stream, FGlyphID[GlyphIdIndex]);
end;
//------------------------------------------------------------------------------
//
// TCustomOpenTypeJustificationScriptTable
//
//------------------------------------------------------------------------------
constructor TCustomOpenTypeJustificationScriptTable.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
FLanguageSystemTables := TPascalTypeTableInterfaceList<TCustomOpenTypeJustificationLanguageSystemTable>.Create(Self);
end;
destructor TCustomOpenTypeJustificationScriptTable.Destroy;
begin
FreeAndNil(FDefaultLangSys);
FreeAndNil(FExtenderGlyphTable);
FreeAndNil(FLanguageSystemTables);
inherited;
end;
procedure TCustomOpenTypeJustificationScriptTable.Assign(Source: TPersistent);
begin
inherited;
if Source is TCustomOpenTypeJustificationScriptTable then
begin
if (TCustomOpenTypeJustificationScriptTable(Source).FExtenderGlyphTable <> nil) then
begin
if (FExtenderGlyphTable = nil) then
FExtenderGlyphTable := TOpenTypeExtenderGlyphTable.Create;
FExtenderGlyphTable.Assign(TCustomOpenTypeJustificationScriptTable(Source).FExtenderGlyphTable);
end else
FreeAndNil(FExtenderGlyphTable);
if (TCustomOpenTypeJustificationScriptTable(Source).FDefaultLangSys <> nil) then
begin
if (FDefaultLangSys = nil) then
FDefaultLangSys := TOpenTypeJustificationLanguageSystemTable.Create(Self);
FDefaultLangSys.Assign(TCustomOpenTypeJustificationScriptTable(Source).FDefaultLangSys);
end else
FreeAndNil(FDefaultLangSys);
FLanguageSystemTables.Assign(TCustomOpenTypeJustificationScriptTable(Source).FLanguageSystemTables);
end;
end;
function TCustomOpenTypeJustificationScriptTable.GetLanguageSystemTable(Index: Integer): TCustomOpenTypeJustificationLanguageSystemTable;
begin
if (Index < 0) or (Index >= FLanguageSystemTables.Count) then
raise EPascalTypeError.CreateFmt(RCStrIndexOutOfBounds, [Index]);
Result := FLanguageSystemTables[Index];
end;
procedure TCustomOpenTypeJustificationScriptTable.SetDefaultLangSys(const Value: TCustomOpenTypeJustificationLanguageSystemTable);
begin
if (Value <> nil) then
begin
if (FDefaultLangSys = nil) then
FDefaultLangSys := TOpenTypeJustificationLanguageSystemTable.Create(Self);
FDefaultLangSys.Assign(Value);
end else
FreeAndNil(FDefaultLangSys);
Changed;
end;
function TCustomOpenTypeJustificationScriptTable.GetLanguageSystemTableCount: Integer;
begin
Result := FLanguageSystemTables.Count;
end;
procedure TCustomOpenTypeJustificationScriptTable.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos : Int64;
LangSysIndex : Integer;
LangSysRecords: array of TTagOffsetRecord;
LangTable : TCustomOpenTypeJustificationLanguageSystemTable;
LangTableClass: TOpenTypeJustificationLanguageSystemTableClass;
ExtenderGlyph : Word;
DefaultLangSys: Word;
begin
inherited;
StartPos := Stream.Position;
// check (minimum) table size
if Stream.Position + 6 > Stream.Size then
raise EPascalTypeError.Create(RCStrTableIncomplete);
// read extender glyph offset
ExtenderGlyph := BigEndianValue.ReadWord(Stream);
// read default language system offset
DefaultLangSys := BigEndianValue.ReadWord(Stream);
// read language system record count
SetLength(LangSysRecords, BigEndianValue.ReadWord(Stream));
for LangSysIndex := 0 to High(LangSysRecords) do
begin
// read table type
Stream.Read(LangSysRecords[LangSysIndex].Tag, SizeOf(TTableType));
// read offset
LangSysRecords[LangSysIndex].Offset := BigEndianValue.ReadWord(Stream);
end;
// load default language system
if ExtenderGlyph <> 0 then
begin
Stream.Position := StartPos + ExtenderGlyph;
if (FExtenderGlyphTable = nil) then
FExtenderGlyphTable := TOpenTypeExtenderGlyphTable.Create;
FExtenderGlyphTable.LoadFromStream(Stream);
end else
FreeAndNil(FExtenderGlyphTable);
// load default language system
if DefaultLangSys <> 0 then
begin
Stream.Position := StartPos + DefaultLangSys;
if (FDefaultLangSys = nil) then
FDefaultLangSys := TOpenTypeJustificationLanguageSystemTable.Create(Self);
FDefaultLangSys.LoadFromStream(Stream);
end else
FreeAndNil(FDefaultLangSys);
// clear existing language tables
FLanguageSystemTables.Clear;
for LangSysIndex := 0 to High(LangSysRecords) do
begin
LangTableClass := FindJustificationLanguageSystemByType(LangSysRecords[LangSysIndex].Tag);
if (LangTableClass <> nil) then
begin
// create language table entry
// add to language system tables
LangTable := FLanguageSystemTables.Add(LangTableClass);
// set position
Stream.Position := StartPos + LangSysRecords[LangSysIndex].Offset;
// read language system table entry from stream
LangTable.LoadFromStream(Stream);
end;
end;
end;
procedure TCustomOpenTypeJustificationScriptTable.SaveToStream(Stream: TStream);
var
StartPos : Int64;
LangSysIndex : Integer;
LangSysRecords: array of TTagOffsetRecord;
Value16 : Word;
DefLangSysOff : Word;
ExtGlyphOff : Word;
begin
inherited;
// remember start position of the stream
StartPos := Stream.Position;
// find offset for data
if (FDefaultLangSys <> nil) then
Value16 := 2 + 4 * FLanguageSystemTables.Count
else
Value16 := 0;
if (FExtenderGlyphTable <> nil) then
Value16 := Value16 + 2;
Stream.Position := StartPos + Value16;
// write extender glyph table
if (FExtenderGlyphTable <> nil) then
begin
ExtGlyphOff := Word(Stream.Position - StartPos);
FExtenderGlyphTable.SaveToStream(Stream);
end else
ExtGlyphOff := 0;
// write default language system table
if (FDefaultLangSys <> nil) then
begin
DefLangSysOff := Word(Stream.Position - StartPos);
FDefaultLangSys.SaveToStream(Stream);
end else
DefLangSysOff := 0;
// build directory (to be written later) and write data
SetLength(LangSysRecords, FLanguageSystemTables.Count);
for LangSysIndex := 0 to High(LangSysRecords) do
begin
var LTable := FLanguageSystemTables[LangSysIndex];
// get table type
LangSysRecords[LangSysIndex].Tag := LTable.TableType;
LangSysRecords[LangSysIndex].Offset := Stream.Position;
// write feature to stream
LTable.SaveToStream(Stream);
end;
// write extender glyph offset
BigEndianValue.WriteWord(Stream, ExtGlyphOff);
// write default language system offset
BigEndianValue.WriteWord(Stream, DefLangSysOff);
// write directory
Stream.Position := StartPos;
for LangSysIndex := 0 to High(LangSysRecords) do
begin
// write tag
Stream.Write(LangSysRecords[LangSysIndex].Tag, SizeOf(TTableType));
// write offset
BigEndianValue.WriteWord(Stream, LangSysRecords[LangSysIndex].Offset);
end;
end;
//------------------------------------------------------------------------------
//
// TOpenTypeJustificationScriptTable
//
//------------------------------------------------------------------------------
class function TOpenTypeJustificationScriptTable.GetDisplayName: string;
begin
Result := 'Default';
end;
class function TOpenTypeJustificationScriptTable.GetTableType: TTableType;
begin
Result := 'DFLT';
end;
//------------------------------------------------------------------------------
//
// TOpenTypeJustificationTable
//
//------------------------------------------------------------------------------
constructor TOpenTypeJustificationTable.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
FVersion.Value := 1;
FScripts := TPascalTypeTableInterfaceList<TCustomOpenTypeJustificationScriptTable>.Create(Self);
end;
destructor TOpenTypeJustificationTable.Destroy;
begin
FreeAndNil(FScripts);
inherited;
end;
class function TOpenTypeJustificationTable.GetTableType: TTableType;
begin
Result := 'JSTF';
end;
procedure TOpenTypeJustificationTable.Assign(Source: TPersistent);
begin
inherited;
if Source is TOpenTypeJustificationTable then
begin
FVersion := TOpenTypeJustificationTable(Source).FVersion;
FScripts.Assign(TOpenTypeJustificationTable(Source).FScripts);
end;
end;
function TOpenTypeJustificationTable.GetScriptCount: Cardinal;
begin
Result := FScripts.Count;
end;
procedure TOpenTypeJustificationTable.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos : Int64;
DirIndex : Integer;
Directory: array of TJustificationScriptDirectoryEntry;
Script : TCustomOpenTypeJustificationScriptTable;
begin
inherited;
// +------------------+------------------------------+--------------------------------------------------------------+
// | Type | Name | Description |
// +==================+==============================+==============================================================+
// | uint16 | majorVersion | Major version of the JSTF table - set to 1. |
// +------------------+------------------------------+--------------------------------------------------------------+
// | uint16 | minorVersion | Minor version of the JSTF table - set to 0. |
// +------------------+------------------------------+--------------------------------------------------------------+
// | uint16 | jstfScriptCount | Number of JstfScriptRecords in this table. |
// +------------------+------------------------------+--------------------------------------------------------------+
StartPos := Stream.Position;
if Stream.Position + 6 > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read version
FVersion.Fixed := BigEndianValue.ReadInteger(Stream);
if Version.Value <> 1 then
raise EPascalTypeError.Create(RCStrUnsupportedVersion);
// read Justification Script Count
SetLength(Directory, BigEndianValue.ReadWord(Stream));
// check if table is complete
if Stream.Position + Length(Directory) * SizeOf(TJustificationScriptDirectoryEntry) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read directory entry
for DirIndex := 0 to High(Directory) do
begin
// read tag
Stream.Read(Directory[DirIndex].Tag, SizeOf(Cardinal));
// read offset
Directory[DirIndex].Offset := BigEndianValue.ReadWord(Stream);
end;
// clear existing scripts
FScripts.Clear;
// read digital scripts
for DirIndex := 0 to High(Directory) do
begin
// TODO: Find matching justification script by tag!!!
Script := FScripts.Add;
// jump to the right position
Stream.Position := StartPos + Directory[DirIndex].Offset;
// load digital signature from stream
Script.LoadFromStream(Stream);
end;
end;
procedure TOpenTypeJustificationTable.SaveToStream(Stream: TStream);
var
StartPos : Int64;
DirIndex : Integer;
Directory: array of TJustificationScriptDirectoryEntry;
begin
inherited;
// store stream start position
StartPos := Stream.Position;
// write version
BigEndianValue.WriteCardinal(Stream, Cardinal(FVersion));
// write Justification Script Count
BigEndianValue.WriteWord(Stream, Length(Directory));
// set directory length
SetLength(Directory, FScripts.Count);
// offset directory
Stream.Seek(soFromCurrent, FScripts.Count * 3 * SizeOf(Word));
// build directory and store signature
for DirIndex := 0 to FScripts.Count - 1 do
begin
Directory[DirIndex].Offset := Stream.Position - StartPos;
Directory[DirIndex].Tag := FScripts[DirIndex].TableType;
SaveToStream(Stream);
end;
// locate directory
Stream.Position := StartPos + 3 * SizeOf(Word);
// write directory entries
for DirIndex := 0 to High(Directory) do
begin
// write tag
Stream.Write(Directory[DirIndex].Tag, SizeOf(Cardinal));
// write offset
BigEndianValue.WriteWord(Stream, Directory[DirIndex].Offset);
end;
end;
procedure TOpenTypeJustificationTable.SetVersion(const Value: TFixedPoint);
begin
if (FVersion.Fract <> Value.Fract) or (FVersion.Value <> Value.Value) then
begin
FVersion := Value;
VersionChanged;
end;
end;
procedure TOpenTypeJustificationTable.VersionChanged;
begin
Changed;
end;
//------------------------------------------------------------------------------
initialization
PascalTypeTableClasses.RegisterTables([TOpenTypeJustificationTable]);
end.