-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.OpenType.STAT.pas
More file actions
297 lines (247 loc) · 9.73 KB
/
PascalType.Tables.OpenType.STAT.pas
File metadata and controls
297 lines (247 loc) · 9.73 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
unit PascalType.Tables.OpenType.STAT;
////////////////////////////////////////////////////////////////////////////////
// //
// 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;
type
TAxisRecord = packed record
AxisTag: TTableType;
AxisNameID: Word;
AxisOrdering: Word;
end;
TCustomAxisValueTable = class(TCustomPascalTypeTable)
private
FFormat: Word;
FAxisIndex: Word;
FFlags: Word;
FValueNameID: Word;
protected
property AxisIndex: Word read FAxisIndex;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
property Format: Word read FFormat;
property Flags: Word read FFlags;
property ValueNameID: Word read FValueNameID;
end;
TAxisValueFormat1Table = class(TCustomAxisValueTable)
private
FValue: TFixedPoint;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
property AxisIndex;
property Value: TFixedPoint read FValue;
end;
TAxisValueFormat2Table = class(TCustomAxisValueTable)
private
FNominalValue: TFixedPoint;
FRangeMinValue: TFixedPoint;
FRangeMaxValue: TFixedPoint;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
property AxisIndex;
property NominalValue: TFixedPoint read FNominalValue;
property RangeMinValue: TFixedPoint read FRangeMinValue;
property RangeMaxValue: TFixedPoint read FRangeMaxValue;
end;
TAxisValueFormat3Table = class(TCustomAxisValueTable)
private
FValue: TFixedPoint;
FLinkedValue: TFixedPoint;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
property AxisIndex;
property Value: TFixedPoint read FValue;
property LinkedValue: TFixedPoint read FLinkedValue;
end;
TAxisValueRecord = record
AxisIndex: Word;
Value: TFixedPoint;
end;
TAxisValueFormat4Table = class(TCustomAxisValueTable)
private
FAxisValues: TArray<TAxisValueRecord>;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
property AxisValues: TArray<TAxisValueRecord> read FAxisValues;
end;
TPascalTypeStyleAttributesTable = class(TCustomPascalTypeNamedTable)
private
FMajorVersion: Word;
FMinorVersion: Word;
FDesignAxes: TArray<TAxisRecord>;
FAxisValueTables: TObjectList<TCustomAxisValueTable>;
FElidedFallbackNameID: Word;
function GetAxisValueTableCount: Integer;
function GetAxisValueTable(Index: Integer): TCustomAxisValueTable;
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
class function GetTableType: TTableType; override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
property AxisValueTableCount: Integer read GetAxisValueTableCount;
property AxisValueTable[Index: Integer]: TCustomAxisValueTable read GetAxisValueTable;
end;
implementation
uses
PascalType.ResourceStrings;
{ TCustomAxisValueTable }
procedure TCustomAxisValueTable.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
FFormat := BigEndianValue.ReadWord(Stream);
FAxisIndex := BigEndianValue.ReadWord(Stream);
FFlags := BigEndianValue.ReadWord(Stream);
FValueNameID := BigEndianValue.ReadWord(Stream);
// Rest is format-specific
end;
{ TAxisValueFormat1Table }
procedure TAxisValueFormat1Table.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
FValue.Fixed := BigEndianValue.ReadInteger(Stream);
end;
{ TAxisValueFormat2Table }
procedure TAxisValueFormat2Table.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
FNominalValue.Fixed := BigEndianValue.ReadInteger(Stream);
FRangeMinValue.Fixed := BigEndianValue.ReadInteger(Stream);
FRangeMaxValue.Fixed := BigEndianValue.ReadInteger(Stream);
end;
{ TAxisValueFormat3Table }
procedure TAxisValueFormat3Table.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
FValue.Fixed := BigEndianValue.ReadInteger(Stream);
FLinkedValue.Fixed := BigEndianValue.ReadInteger(Stream);
end;
{ TAxisValueFormat4Table }
procedure TAxisValueFormat4Table.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
SetLength(FAxisValues, FAxisIndex);
for var i := 0 to High(FAxisValues) do
begin
FAxisValues[i].AxisIndex := BigEndianValue.ReadWord(Stream);
FAxisValues[i].Value.Fixed := BigEndianValue.ReadInteger(Stream);
end;
end;
{ TPascalTypeStyleAttributesTable }
constructor TPascalTypeStyleAttributesTable.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
FAxisValueTables := TObjectList<TCustomAxisValueTable>.Create(True);
end;
destructor TPascalTypeStyleAttributesTable.Destroy;
begin
FAxisValueTables.Free;
inherited;
end;
function TPascalTypeStyleAttributesTable.GetAxisValueTableCount: Integer;
begin
Result := FAxisValueTables.Count;
end;
function TPascalTypeStyleAttributesTable.GetAxisValueTable(Index: Integer): TCustomAxisValueTable;
begin
Result := FAxisValueTables[Index];
end;
class function TPascalTypeStyleAttributesTable.GetTableType: TTableType;
begin
Result.AsAnsiChar := 'STAT';
end;
procedure TPascalTypeStyleAttributesTable.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
var StartPos := Stream.Position;
FMajorVersion := BigEndianValue.ReadWord(Stream);
FMinorVersion := BigEndianValue.ReadWord(Stream);
var DesignAxisSize := BigEndianValue.ReadWord(Stream);
var DesignAxisCount := BigEndianValue.ReadWord(Stream);
var DesignAxesOffset := BigEndianValue.ReadCardinal(Stream);
var AxisValueCount := BigEndianValue.ReadWord(Stream);
var AxisValueArrayOffset := BigEndianValue.ReadCardinal(Stream);
if (FMajorVersion >= 1) and (FMinorVersion >= 1) then
FElidedFallbackNameID := BigEndianValue.ReadWord(Stream)
else
FElidedFallbackNameID := 0;
if DesignAxesOffset <> 0 then
begin
Stream.Position := StartPos + DesignAxesOffset;
SetLength(FDesignAxes, DesignAxisCount);
for var i := 0 to DesignAxisCount - 1 do
begin
Stream.Read(FDesignAxes[i].AxisTag, 4);
FDesignAxes[i].AxisNameID := BigEndianValue.ReadWord(Stream);
FDesignAxes[i].AxisOrdering := BigEndianValue.ReadWord(Stream);
if DesignAxisSize > 8 then
Stream.Seek(DesignAxisSize - 8, soFromCurrent);
end;
end;
if (AxisValueArrayOffset <> 0) and (AxisValueCount > 0) then
begin
Stream.Position := StartPos + AxisValueArrayOffset;
var Offsets: TArray<Word>;
SetLength(Offsets, AxisValueCount);
for var i := 0 to AxisValueCount - 1 do
Offsets[i] := BigEndianValue.ReadWord(Stream);
for var i := 0 to AxisValueCount - 1 do
begin
Stream.Position := StartPos + AxisValueArrayOffset + Offsets[i];
var Format := BigEndianValue.ReadWord(Stream);
var Table: TCustomAxisValueTable := nil;
case Format of
1: Table := TAxisValueFormat1Table.Create(Self);
2: Table := TAxisValueFormat2Table.Create(Self);
3: Table := TAxisValueFormat3Table.Create(Self);
4: Table := TAxisValueFormat4Table.Create(Self);
end;
if Table <> nil then
begin
Table.FFormat := Format;
Table.LoadFromStream(Stream);
FAxisValueTables.Add(Table);
end;
end;
end;
end;
procedure TPascalTypeStyleAttributesTable.SaveToStream(Stream: TStream);
begin
raise EPascalTypeNotImplemented.Create(RCStrNotImplemented);
end;
initialization
PascalTypeTableClasses.RegisterTables([TPascalTypeStyleAttributesTable]);
end.