-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Shaper.Plan.pas
More file actions
398 lines (330 loc) · 13.7 KB
/
PascalType.Shaper.Plan.pas
File metadata and controls
398 lines (330 loc) · 13.7 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
unit PascalType.Shaper.Plan;
////////////////////////////////////////////////////////////////////////////////
// //
// 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
uses
System.Classes,
Generics.Defaults,
Generics.Collections,
PascalType.Types,
PascalType.Classes,
PascalType.Unicode,
PascalType.GlyphString,
PascalType.FontFace.SFNT,
PascalType.Tables.OpenType.GSUB,
PascalType.Tables.OpenType.Feature;
//------------------------------------------------------------------------------
//
// TPascalTypeShapingPlanStage
//
//------------------------------------------------------------------------------
// A collection of features.
//------------------------------------------------------------------------------
type
TPascalTypeShapingPlanStages = class;
TPascalTypeShapingPlan = class;
TPascalTypeShapingPlanStage = class
public type
TPascalTypeShapingPlanDelegate = function(AProcessor: TObject; var AGlyphs: TPascalTypeGlyphString): TTableNames;
private
FStages: TPascalTypeShapingPlanStages;
FFeatures: TPascalTypeFeatures;
FDelegate: TPascalTypeShapingPlanDelegate;
function GetCount: integer;
function GetFeature(Index: integer): TTableName;
function GetPlan: TPascalTypeShapingPlan;
public
constructor Create(AStages: TPascalTypeShapingPlanStages);
procedure Add(const AFeature: TTableName; AGlobal: boolean = True); overload;
procedure Add(const AFeature: TTableType; AGlobal: boolean = True); overload;
procedure Add(const AFeatures: TTableNames; AGlobal: boolean = True); overload;
procedure Add(const AFeatures: TArray<TTableType>; AGlobal: boolean = True); overload;
procedure Remove(const AFeature: TTableName);
function GetEnumerator: TEnumerator<TTableName>;
property Plan: TPascalTypeShapingPlan read GetPlan;
property Delegate: TPascalTypeShapingPlanDelegate read FDelegate write FDelegate;
property Count: integer read GetCount;
property FeatureList[Index: integer]: TTableName read GetFeature; default;
property Features: TPascalTypeFeatures read FFeatures;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeShapingPlanStages
//
//------------------------------------------------------------------------------
// A list of feature stages.
//------------------------------------------------------------------------------
TPascalTypeShapingPlanStages = class
private
FPlan: TPascalTypeShapingPlan;
FStages: TList<TPascalTypeShapingPlanStage>;
FAllFeatures: TDictionary<TTableName, TPascalTypeShapingPlanStage>;
function GetCount: integer;
function GetStage(Index: integer): TPascalTypeShapingPlanStage;
protected
procedure DoAddFeature(const AFeature: TTableName; AGlobal: boolean; AStage: TPascalTypeShapingPlanStage);
procedure DoRemoveFeature(const AFeature: TTableName);
procedure RemoveFeature(const AFeature: TTableName);
function HasFeature(const AFeature: TTableName): boolean;
property Plan: TPascalTypeShapingPlan read FPlan;
public
constructor Create(APlan: TPascalTypeShapingPlan);
destructor Destroy; override;
function Add: TPascalTypeShapingPlanStage;
function GetEnumerator: TEnumerator<TPascalTypeShapingPlanStage>;
property Count: integer read GetCount;
property Stages[Index: integer]: TPascalTypeShapingPlanStage read GetStage; default;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeShapingPlan
//
//------------------------------------------------------------------------------
// A collection of feature stages to be applied as part of the shaping. Each
// stage contains a number of features. The shaper applies the stages
// sequentially.
// Inspired by FontKit.
//------------------------------------------------------------------------------
TPascalTypeShapingPlan = class
private
FStages: TPascalTypeShapingPlanStages;
FGlobalFeatures: TPascalTypeFeatures;
FFallbackMarkPositioning: boolean;
FAdjustMarkOffsetsWhenZeroing: boolean;
protected
function GetGlobalFeatures: PPascalTypeFeatures;
procedure DoAddFeature(const AFeature: TTableName; AGlobal: boolean; AStage: TPascalTypeShapingPlanStage);
procedure DoRemoveFeature(const AFeature: TTableName);
public
constructor Create; virtual;
destructor Destroy; override;
procedure AddFeature(const AFeature: TTableName; AGlobal: boolean = True; AStage: TPascalTypeShapingPlanStage = nil);
procedure AddFeatures(const AFeatures: TTableNames; AGlobal: boolean = True; AStage: TPascalTypeShapingPlanStage = nil);
procedure RemoveFeature(const AFeature: TTableName);
function HasFeature(const AFeature: TTableName): boolean;
procedure ApplyUserFeatures(const AFeatures: TPascalTypeFeatures);
function GetEnumerator: TEnumerator<TPascalTypeShapingPlanStage>;
property Stages: TPascalTypeShapingPlanStages read FStages;
property GlobalFeatures: PPascalTypeFeatures read GetGlobalFeatures;
// FallbackMarkPositioning: When true, marks are positioned using a fallback
// algorithm if the font does not provide GPOS mark positioning.
property FallbackMarkPositioning: boolean read FFallbackMarkPositioning write FFallbackMarkPositioning;
// AdjustMarkOffsetsWhenZeroing: When true, mark offsets are adjusted to maintain
// their relative position when their advances are zeroed.
property AdjustMarkOffsetsWhenZeroing: boolean read FAdjustMarkOffsetsWhenZeroing write FAdjustMarkOffsetsWhenZeroing;
end;
type
TPascalTypeShapingPlanClass = class of TPascalTypeShapingPlan;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
//------------------------------------------------------------------------------
//
// TPascalTypeShapingPlanStage
//
//------------------------------------------------------------------------------
constructor TPascalTypeShapingPlanStage.Create(AStages: TPascalTypeShapingPlanStages);
begin
inherited Create;
FStages := AStages;
end;
procedure TPascalTypeShapingPlanStage.Add(const AFeature: TTableName; AGlobal: boolean);
begin
if (FStages.HasFeature(AFeature)) then
exit;
FFeatures.Add(AFeature);
FStages.DoAddFeature(AFeature, AGlobal, Self);
end;
procedure TPascalTypeShapingPlanStage.Add(const AFeature: TTableType; AGlobal: boolean);
begin
Add(AFeature.AsAnsiChar, AGlobal);
end;
procedure TPascalTypeShapingPlanStage.Add(const AFeatures: TTableNames; AGlobal: boolean);
var
Feature: TTableName;
begin
for Feature in AFeatures do
Add(Feature, AGlobal);
end;
procedure TPascalTypeShapingPlanStage.Add(const AFeatures: TArray<TTableType>; AGlobal: boolean);
var
Feature: TTableType;
begin
for Feature in AFeatures do
Add(Feature.AsAnsiChar, AGlobal);
end;
function TPascalTypeShapingPlanStage.GetCount: integer;
begin
Result := FFeatures.Count;
end;
function TPascalTypeShapingPlanStage.GetEnumerator: TEnumerator<TTableName>;
begin
Result := FFeatures.GetEnumerator;
end;
function TPascalTypeShapingPlanStage.GetFeature(Index: integer): TTableName;
begin
Result := FFeatures.Tags[Index];
end;
function TPascalTypeShapingPlanStage.GetPlan: TPascalTypeShapingPlan;
begin
Result := FStages.Plan;
end;
procedure TPascalTypeShapingPlanStage.Remove(const AFeature: TTableName);
begin
FFeatures.Remove(AFeature);
FStages.DoRemoveFeature(AFeature);
end;
//------------------------------------------------------------------------------
//
// TPascalTypeShapingPlanStages
//
//------------------------------------------------------------------------------
constructor TPascalTypeShapingPlanStages.Create(APlan: TPascalTypeShapingPlan);
begin
inherited Create;
FPlan := APlan;
FStages := TObjectList<TPascalTypeShapingPlanStage>.Create;
FAllFeatures := TDictionary<TTableName, TPascalTypeShapingPlanStage>.Create;
end;
destructor TPascalTypeShapingPlanStages.Destroy;
begin
FStages.Free;
FAllFeatures.Free;
inherited;
end;
function TPascalTypeShapingPlanStages.Add: TPascalTypeShapingPlanStage;
begin
Result := TPascalTypeShapingPlanStage.Create(Self);
FStages.Add(Result);
end;
procedure TPascalTypeShapingPlanStages.DoAddFeature(const AFeature: TTableName; AGlobal: boolean; AStage: TPascalTypeShapingPlanStage);
begin
FAllFeatures.Add(AFeature, AStage);
Plan.DoAddFeature(AFeature, AGlobal, AStage);
end;
procedure TPascalTypeShapingPlanStages.DoRemoveFeature(const AFeature: TTableName);
begin
FAllFeatures.Remove(AFeature);
Plan.DoRemoveFeature(AFeature);
end;
function TPascalTypeShapingPlanStages.GetCount: integer;
begin
Result := FStages.Count;
end;
function TPascalTypeShapingPlanStages.GetEnumerator: TEnumerator<TPascalTypeShapingPlanStage>;
begin
Result := FStages.GetEnumerator;
end;
function TPascalTypeShapingPlanStages.GetStage(Index: integer): TPascalTypeShapingPlanStage;
begin
Result := FStages[Index];
end;
function TPascalTypeShapingPlanStages.HasFeature(const AFeature: TTableName): boolean;
begin
Result := FAllFeatures.ContainsKey(AFeature);
end;
procedure TPascalTypeShapingPlanStages.RemoveFeature(const AFeature: TTableName);
var
Stage: TPascalTypeShapingPlanStage;
begin
if (FAllFeatures.TryGetValue(AFeature, Stage)) then
Stage.Remove(AFeature);
end;
//------------------------------------------------------------------------------
//
// TPascalTypeShapingPlan
//
//------------------------------------------------------------------------------
constructor TPascalTypeShapingPlan.Create;
begin
inherited Create;
FStages := TPascalTypeShapingPlanStages.Create(Self);
end;
destructor TPascalTypeShapingPlan.Destroy;
begin
FStages.Free;
inherited;
end;
procedure TPascalTypeShapingPlan.DoAddFeature(const AFeature: TTableName; AGlobal: boolean; AStage: TPascalTypeShapingPlanStage);
begin
if (AGlobal) then
FGlobalFeatures.Add(AFeature);
end;
procedure TPascalTypeShapingPlan.DoRemoveFeature(const AFeature: TTableName);
begin
FGlobalFeatures.Remove(AFeature);
end;
function TPascalTypeShapingPlan.GetEnumerator: TEnumerator<TPascalTypeShapingPlanStage>;
begin
Result := FStages.GetEnumerator;
end;
function TPascalTypeShapingPlan.GetGlobalFeatures: PPascalTypeFeatures;
begin
Result := @FGlobalFeatures;
end;
procedure TPascalTypeShapingPlan.AddFeature(const AFeature: TTableName; AGlobal: boolean; AStage: TPascalTypeShapingPlanStage);
begin
// Stage.Add also checks for this but since we would like to
// avoid adding a new empty stage if the feature already exist,
// we need to do it here too.
if (HasFeature(AFeature)) then
exit;
if (AStage = nil) then
begin
if (FStages.Count > 0) then
AStage := FStages[FStages.Count-1]
else
AStage := FStages.Add;
end;
AStage.Add(AFeature, AGlobal);
end;
procedure TPascalTypeShapingPlan.AddFeatures(const AFeatures: TTableNames; AGlobal: boolean; AStage: TPascalTypeShapingPlanStage);
begin
for var Feature in AFeatures do
AddFeature(Feature, AGlobal, AStage);
end;
procedure TPascalTypeShapingPlan.ApplyUserFeatures(const AFeatures: TPascalTypeFeatures);
begin
for var Feature in AFeatures do
if (AFeatures.IsEnabled(Feature)) then
AddFeature(Feature)
else
RemoveFeature(Feature);
end;
function TPascalTypeShapingPlan.HasFeature(const AFeature: TTableName): boolean;
begin
Result := FStages.HasFeature(AFeature);
end;
procedure TPascalTypeShapingPlan.RemoveFeature(const AFeature: TTableName);
begin
FStages.RemoveFeature(AFeature);
end;
end.