-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.OpenType.CPAL.pas
More file actions
369 lines (307 loc) · 16.1 KB
/
PascalType.Tables.OpenType.CPAL.pas
File metadata and controls
369 lines (307 loc) · 16.1 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
unit PascalType.Tables.OpenType.CPAL;
////////////////////////////////////////////////////////////////////////////////
// //
// 'CPAL' 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 Anders Melander. //
// //
// Portions created by Anders Melander are Copyright (C) 2024 //
// by Anders Melander. All Rights Reserved. //
// //
////////////////////////////////////////////////////////////////////////////////
interface
{$I PT_Compiler.inc}
uses
Generics.Collections,
Generics.Defaults,
Classes,
PascalType.Types,
PascalType.Types.Color,
PascalType.Classes,
PascalType.Tables,
PascalType.Tables.OpenType,
PascalType.Tables.OpenType.Common;
//------------------------------------------------------------------------------
//
// TOpenTypeColorCPALTable
//
//------------------------------------------------------------------------------
// Microsoft Color Palette table
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/CPAL
//------------------------------------------------------------------------------
{$MINENUMSIZE 4}
// CPAL version 0 does not have any flags indicating intended usage
// but by convention, for CPAL version 0, and CPAL version 1 with no
// flags
// - Palette #0: Dark on light (i.e. Light color scheme)
// - Palette #1: Light on dark (i.e. Dark color scheme)
type
TColorPaletteFlag = (
cpfUsableWithLightBackground, // Palette is appropriate to use when displaying the font on a light background such as white.
cpfUsableWithDarkBackground // Palette is appropriate to use when displaying the font on a dark background such as black.
);
TColorPaletteFlags = set of TColorPaletteFlag;
TColorPalette = record
FirstColorIndex: integer;
NameIndex: integer;
Flags: TColorPaletteFlags;
end;
type
TOpenTypeColorCPALTable = class(TCustomOpenTypeNamedTable, IPascalTypeColorLookup, IPascalTypeColorProvider)
private
FVersion: Word;
FColors: TArray<TPaletteColor>;
FPalettes: TArray<TColorPalette>;
FColorNames: TArray<integer>;
FPaletteSize: integer;
protected
function GetColor(Index: integer): TPaletteColor;
function GetColorCount: integer;
function GetPalette(Index: integer): TColorPalette;
function GetPaletteCount: integer;
function GetColorName(Index: integer): integer;
private
// IPascalTypeColorProvider
function GetColorLookup: IPascalTypeColorLookup;
// IPascalTypeColorLookup
function GetPaletteColor(PaletteIndex, ColorIndex: integer): TPaletteColor;
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: Word read FVersion write FVersion;
property PaletteSize: integer read FPaletteSize write FPaletteSize;
property ColorNames[Index: integer]: integer read GetColorName;
property ColorCount: integer read GetColorCount;
property Colors[Index: integer]: TPaletteColor read GetColor;
property PaletteCount: integer read GetPaletteCount;
property Palettes[Index: integer]: TColorPalette read GetPalette;
property PaletteColors[PaletteIndex, ColorIndex: integer]: TPaletteColor read GetPaletteColor;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
SysUtils,
PascalType.ResourceStrings;
//------------------------------------------------------------------------------
//
// TOpenTypeColorCPALTable
//
//------------------------------------------------------------------------------
constructor TOpenTypeColorCPALTable.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
end;
destructor TOpenTypeColorCPALTable.Destroy;
begin
inherited;
end;
//------------------------------------------------------------------------------
class function TOpenTypeColorCPALTable.GetTableType: TTableType;
begin
Result := 'CPAL';
end;
//------------------------------------------------------------------------------
procedure TOpenTypeColorCPALTable.Assign(Source: TPersistent);
begin
inherited;
if Source is TOpenTypeColorCPALTable then
begin
FVersion := TOpenTypeColorCPALTable(Source).FVersion;
FPaletteSize := TOpenTypeColorCPALTable(Source).FPaletteSize;
FColors := Copy(TOpenTypeColorCPALTable(Source).FColors);
FPalettes := Copy(TOpenTypeColorCPALTable(Source).FPalettes);
FColorNames := Copy(TOpenTypeColorCPALTable(Source).FColorNames);
end;
end;
//------------------------------------------------------------------------------
function TOpenTypeColorCPALTable.GetColorCount: integer;
begin
Result := Length(FColors);
end;
function TOpenTypeColorCPALTable.GetColor(Index: integer): TPaletteColor;
begin
Result := FColors[Index];
end;
//------------------------------------------------------------------------------
function TOpenTypeColorCPALTable.GetPaletteCount: integer;
begin
Result := Length(FPalettes);
end;
function TOpenTypeColorCPALTable.GetPalette(Index: integer): TColorPalette;
begin
Result := FPalettes[Index];
end;
//------------------------------------------------------------------------------
function TOpenTypeColorCPALTable.GetColorName(Index: integer): integer;
begin
Result := FColorNames[Index];
end;
//------------------------------------------------------------------------------
procedure TOpenTypeColorCPALTable.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos: Int64;
colorRecordsArrayOffset: integer;
offsetPaletteTypeArray: integer;
offsetPaletteLabelArray: integer;
offsetPaletteEntryLabelArray: integer;
begin
StartPos := Stream.Position;
inherited;
// +----------+-------------------------+-------------------------------------------------------------------------+
// | Type | Name | Description |
// +==========+=========================+=========================================================================+
// | uint16 | version | Table version number (=0 or 1). |
// +----------+-------------------------+-------------------------------------------------------------------------+
// | uint16 | numPaletteEntries | Number of palette entries in each palette. |
// +----------+-------------------------+-------------------------------------------------------------------------+
// | uint16 | numPalettes | Number of palettes in the table. |
// +----------+-------------------------+-------------------------------------------------------------------------+
// | uint16 | numColorRecords | Total number of color records combined for all palettes. |
// +----------+-------------------------+-------------------------------------------------------------------------+
// | Offset32 | colorRecordsArrayOffset | Offset to the color records array. |
// +----------+-------------------------+-------------------------------------------------------------------------+
// Version 1:
// ----------+---------------------------------+----------------------------------------------------------------------------------------------------
// Offset32 | offsetPaletteTypeArray | Offset from the beginning of CPAL table to the Palette Type Array. Set to 0 if no array is provided.
// Offset32 | offsetPaletteLabelArray | Offset from the beginning of CPAL table to the Palette Labels Array. Set to 0 if no array is provided.
// Offset32 | offsetPaletteEntryLabelArray | Offset from the beginning of CPAL table to the Palette Entry Label Array.Set to 0 if no array is provided.
// ----------+---------------------------------+----------------------------------------------------------------------------------------------------
// version
FVersion := BigEndianValue.ReadWord(Stream);
if (Version <> 0) and (Version <> 1) then
raise EPascalTypeError.Create(RCStrUnsupportedVersion);
// numPaletteEntries
FPaletteSize := BigEndianValue.ReadWord(Stream);
// numPalettes
SetLength(FPalettes, BigEndianValue.ReadWord(Stream));
// numColorRecords
SetLength(FColors, BigEndianValue.ReadWord(Stream));
// colorRecordsArrayOffset
colorRecordsArrayOffset := BigEndianValue.ReadInteger(Stream);
if (Version > 0) then
begin
offsetPaletteTypeArray := BigEndianValue.ReadInteger(Stream);
offsetPaletteLabelArray := BigEndianValue.ReadInteger(Stream);
offsetPaletteEntryLabelArray := BigEndianValue.ReadInteger(Stream);
end else
begin
offsetPaletteTypeArray := 0;
offsetPaletteLabelArray := 0;
offsetPaletteEntryLabelArray := 0;
end;
// colorRecordIndices[numPalettes]
for var i := 0 to High(FPalettes) do
begin
FPalettes[i].FirstColorIndex := BigEndianValue.ReadWord(Stream);
FPalettes[i].NameIndex := -1;
FPalettes[i].Flags := [];
end;
// -----------+---------------------------------+----------------------------------------------------------------------------------------------------
// Type | Name | Description
// -----------+---------------------------------+----------------------------------------------------------------------------------------------------
// ColorRecord| colorRecords[numColorRecords] | Color records for all palettes.
// -----------+---------------------------------+----------------------------------------------------------------------------------------------------
Stream.Position := StartPos + colorRecordsArrayOffset;
for var i := 0 to High(FColors) do
begin
FColors[i].Blue := BigEndianValue.ReadByte(Stream);
FColors[i].Green := BigEndianValue.ReadByte(Stream);
FColors[i].Red := BigEndianValue.ReadByte(Stream);
FColors[i].Alpha := BigEndianValue.ReadByte(Stream);
end;
// ----------+---------------------------------+----------------------------------------------------------------------------------------------------
// Type | Name | Description
// ----------+---------------------------------+----------------------------------------------------------------------------------------------------
// uint32 | paletteTypes[numPalettes] | Array of 32-bit flag fields that describe properties of each palette.
// ----------+---------------------------------+----------------------------------------------------------------------------------------------------
if (offsetPaletteTypeArray <> 0) then
begin
Stream.Position := StartPos + offsetPaletteTypeArray;
for var i := 0 to High(FPalettes) do
// {$MINENUMSIZE 4} should have made the byte cast unnecessary - but it doesn't
FPalettes[i].Flags := TColorPaletteFlags(Byte(BigEndianValue.ReadCardinal(Stream)));
end;
// ----------+---------------------------------+----------------------------------------------------------------------------------------------------
// Type | Name | Description
// ----------+---------------------------------+----------------------------------------------------------------------------------------------------
// uint16 | paletteLabels[numPalettes] | Array of 'name' table IDs. Use 0xFFFF for a particular palette if no string is provided.
// ----------+---------------------------------+----------------------------------------------------------------------------------------------------
if (offsetPaletteLabelArray <> 0) then
begin
Stream.Position := StartPos + offsetPaletteLabelArray;
for var i := 0 to High(FPalettes) do
begin
var Index := BigEndianValue.ReadWord(Stream);
if (Index <> $FFFF) then
FPalettes[i].NameIndex := Index;
end;
end;
// ----------+---------------------------------------+----------------------------------------------------------------------------------------------------
// Type | Name | Description
// ----------+---------------------------------------+----------------------------------------------------------------------------------------------------
// uint16 | paletteEntryLabels[numPaletteEntries] | Array of 'name' table IDs. Use 0xFFFF for a particular entry if no string is provided.
// ----------+---------------------------------------+----------------------------------------------------------------------------------------------------
if (offsetPaletteEntryLabelArray <> 0) then
begin
Setlength(FColorNames, FPaletteSize);
Stream.Position := StartPos + offsetPaletteEntryLabelArray;
for var i := 0 to High(FColorNames) do
begin
var Index := BigEndianValue.ReadWord(Stream);
if (Index <> $FFFF) then
FColorNames[i] := Index
else
FColorNames[i] := -1;
end;
end else
Setlength(FColorNames, 0);
end;
//------------------------------------------------------------------------------
function TOpenTypeColorCPALTable.GetColorLookup: IPascalTypeColorLookup;
begin
if (Length(FColors) > 0) then
Result := Self
else
Result := nil;
end;
function TOpenTypeColorCPALTable.GetPaletteColor(PaletteIndex, ColorIndex: integer): TPaletteColor;
begin
Assert(ColorIndex <> -1);
Assert(ColorIndex <> $FFFF);
var FirstIndex := FPalettes[PaletteIndex].FirstColorIndex;
Result := FColors[FirstIndex + ColorIndex];
end;
//------------------------------------------------------------------------------
initialization
PascalTypeTableClasses.RegisterTables([TOpenTypeColorCPALTable]);
end.