-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Shaper.Script.Hangul.pas
More file actions
506 lines (427 loc) · 17.1 KB
/
PascalType.Shaper.Script.Hangul.pas
File metadata and controls
506 lines (427 loc) · 17.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
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
unit PascalType.Shaper.Script.Hangul;
////////////////////////////////////////////////////////////////////////////////
// //
// Shaper for Hangul. //
// //
// Based on the FontKit Hangul shaper (which in turn is probably based //
// on the Harfbuzz Hangul shaper. //
// //
////////////////////////////////////////////////////////////////////////////////
// //
// 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) 2023 //
// by Anders Melander. All Rights Reserved. //
// //
////////////////////////////////////////////////////////////////////////////////
interface
uses
PascalType.Types,
PascalType.Unicode,
PascalType.GlyphString,
PascalType.Shaper,
PascalType.Shaper.Script.Default,
PascalType.Shaper.Plan,
PascalType.Shaper.Layout;
//------------------------------------------------------------------------------
//
// TPascalTypeHangulShaper
//
//------------------------------------------------------------------------------
//
// This is a shaper for the Hangul script, used by the Korean language.
//
// It does the following:
// - decompose if unsupported by the font:
// <LV> -> <L,V>
// <LVT> -> <L,V,T>
// <LV,T> -> <L,V,T>
//
// - compose if supported by the font:
// <L,V> -> <LV>
// <L,V,T> -> <LVT>
// <LV,T> -> <LVT>
//
// - reorder tone marks (S is any valid syllable):
// <S, M> -> <M, S>
//
// - apply ljmo, vjmo, and tjmo OpenType features to decomposed Jamo sequences.
//
// This logic is based on the following documents:
// - http://www.microsoft.com/typography/OpenTypeDev/hangul/intro.htm
// - http://ktug.org/~nomos/harfbuzz-hangul/hangulshaper.pdf
//
//------------------------------------------------------------------------------
type
TPascalTypeHangulShaper = class(TCustomPascalTypeShaper)
private
function Decompose(var AGlyphs: TPascalTypeGlyphString; Index: integer): integer;
function Compose(var AGlyphs: TPascalTypeGlyphString; Index: integer): integer;
function InsertDottedCircle(var AGlyphs: TPascalTypeGlyphString; Index: integer): integer;
procedure ReorderToneMark(var AGlyphs: TPascalTypeGlyphString; Index: integer);
protected
function ZeroMarkWidths: TZeroMarkWidths; override;
function NeedUnicodeComposition: boolean; override;
procedure PlanFeatures(AStage: TPascalTypeShapingPlanStage); override;
procedure PlanPostprocessing(AStage: TPascalTypeShapingPlanStage); override;
procedure AssignLocalFeatures(const AFeatures: TPascalTypeFeatures; var AGlyphs: TPascalTypeGlyphString); override;
end;
//------------------------------------------------------------------------------
//
// Hangul feature plans
//
//------------------------------------------------------------------------------
const
HangulFeatures: TTableNames = [
'ljmo',
'vjmo',
'tjmo'
];
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
System.Classes,
PascalType.Shaper.Layout.OpenType;
// Character categories
type
THangulCategory = (
hcX, // Other character
hcL, // Leading consonant
hcV, // Medial vowel
hcT, // Trailing consonant
hcLV, // Composed <LV> syllable
hcLVT, // Composed <LVT> syllable
hcM // Tone mark
);
// This function classifies a character using the above categories.
function GetHangulCategory(ACodePoint: TPascalTypeCodePoint): THangulCategory;
begin
if (Hangul.IsL(ACodePoint)) then
Result := hcL
else
if (Hangul.IsV(ACodePoint)) then
Result := hcV
else
if (Hangul.IsT(ACodePoint)) then
Result := hcT
else
if (Hangul.IsHangulLV(ACodePoint)) then
Result := hcLV
else
if (Hangul.IsHangul(ACodePoint)) then
Result := hcLVT
else
if (Hangul.IsTone(ACodePoint)) then
Result := hcM
else
Result := hcX;
end;
type
TState = (sStart, sL, sLV, sLVT);
// State machine actions
TStateAction = (saNone, saDecompose, saCompose, saToneMark, saInvalid);
TStateEntry = record
Action: TStateAction;
NextState: TState;
end;
TStateEntries = array[THangulCategory] of TStateEntry;
TStateMachine = array[TState] of TStateEntries;
const
// A state machine that accepts valid syllables, and applies actions along the way.
// Ported from FontKit.
None = #0#0#0#0;
StateMachine: TStateMachine = (
// X L V T LV LVT M
// State 0: start state
((Action: saNone; NextState: sStart), (Action: saNone; NextState: sL), (Action: saNone; NextState: sStart), (Action: saNone; NextState: sStart), (Action: saDecompose; NextState: sLV), (Action: saDecompose; NextState: sLVT), (Action: saInvalid; NextState: sStart)),
// State 1: <L>
((Action: saNone; NextState: sStart), (Action: saNone; NextState: sL), (Action: saCompose; NextState: sLV), (Action: saNone; NextState: sStart), (Action: saDecompose; NextState: sLV), (Action: saDecompose; NextState: sLVT), (Action: saInvalid; NextState: sStart)),
// State 2: <L,V> or <LV>
((Action: saNone; NextState: sStart), (Action: saNone; NextState: sL), (Action: saNone; NextState: sStart), (Action: saCompose; NextState: sLVT), (Action: saDecompose; NextState: sLV), (Action: saDecompose; NextState: sLVT), (Action: saToneMark; NextState: sStart)),
// State 3: <L,V,T> or <LVT>
((Action: saNone; NextState: sStart), (Action: saNone; NextState: sL), (Action: saNone; NextState: sStart), (Action: saNone; NextState: sStart), (Action: saDecompose; NextState: sLV), (Action: saDecompose; NextState: sLVT), (Action: saToneMark; NextState: sStart))
);
//------------------------------------------------------------------------------
//
// TPascalTypeHangulShaper
//
//------------------------------------------------------------------------------
procedure TPascalTypeHangulShaper.PlanFeatures(AStage: TPascalTypeShapingPlanStage);
begin
AStage.Add(HangulFeatures, False);
end;
procedure TPascalTypeHangulShaper.PlanPostprocessing(AStage: TPascalTypeShapingPlanStage);
begin
AStage.Add(CommonFeatures);
end;
function TPascalTypeHangulShaper.ZeroMarkWidths: TZeroMarkWidths;
begin
Result := zmwNever;
end;
function TPascalTypeHangulShaper.Decompose(var AGlyphs: TPascalTypeGlyphString; Index: integer): integer;
var
SIndex, TIndex: Integer;
LCodePoint, VCodePoint, TCodePoint: TPascalTypeCodePoint;
Glyph: TPascalTypeGlyph;
LGlyphID, VGlyphID, TGlyphID: Cardinal;
begin
Result := Index;
Glyph := AGlyphs[Index];
// Copied from PascalType.Unicode DecomposeHangul
SIndex := Glyph.CodePoints[0] - Hangul.HangulSBase;
LCodePoint := Hangul.JamoLBase + (SIndex div Hangul.JamoNCount);
VCodePoint := Hangul.JamoVBase + ((SIndex mod Hangul.JamoNCount) div Hangul.JamoTCount);
TIndex := SIndex mod Hangul.JamoTCount;
if TIndex <> 0 then
TCodePoint := Hangul.JamoTBase + TIndex
else
TCodePoint := 0;
// Keep original glyph if font doesn't have the decomposed glyphs
LGlyphID := Font.GetGlyphByCodePoint(LCodePoint);
if (LGlyphID = 0) then
exit;
VGlyphID := Font.GetGlyphByCodePoint(VCodePoint);
if (VGlyphID = 0) then
exit;
if (TCodePoint <> 0) then
begin
TGlyphID := Font.GetGlyphByCodePoint(TCodePoint);
if (TGlyphID = 0) then
exit;
end else
TGlyphID := 0;
// Replace the current glyph with decomposed L, V, and T glyphs,
// and apply the proper OpenType features to each component.
var Cluster := Glyph.Cluster;
var PlanFeatures: TPascalTypeFeatures;
PlanFeatures.Assign(Glyph.PlanFeatures);
AGlyphs.Delete(Index);
Glyph := AGlyphs.Insert(Result, Cluster);
Glyph.GlyphID := LGlyphID;
Glyph.CodePoints := [LCodePoint];
Glyph.PlanFeatures.Assign(PlanFeatures);
Glyph.PlanFeatures.Add('ljmo');
Inc(Result);
Glyph := AGlyphs.Insert(Result, Cluster);
Glyph.GlyphID := VGlyphID;
Glyph.CodePoints := [VCodePoint];
Glyph.PlanFeatures.Assign(PlanFeatures);
Glyph.PlanFeatures.Add('vjmo');
Inc(Result);
if (TCodePoint <> 0) then
begin
Glyph := AGlyphs.Insert(Result, Cluster);
Glyph.GlyphID := TGlyphID;
Glyph.CodePoints := [TCodePoint];
Glyph.PlanFeatures.Assign(PlanFeatures);
Glyph.PlanFeatures.Add('tjmo');
Inc(Result);
end;
Dec(Result); // Offset the +1 that is applied after we return
end;
function TPascalTypeHangulShaper.Compose(var AGlyphs: TPascalTypeGlyphString; Index: integer): integer;
var
Glyph: TPascalTypeGlyph;
Category, PrevCategory: THangulCategory;
PrevCodePoint: TPascalTypeCodePoint;
LVCodePoint, LCodePoint, VCodePoint, TCodePoint, SCodePoint: TPascalTypeCodePoint;
LjmoGlyph, VjmoGlyph, TjmoGlyph: TPascalTypeGlyph;
GlyphID: Cardinal;
Count: integer;
begin
Assert(Index > 0);
Result := Index;
Glyph := AGlyphs[Index];
Category := GetHangulCategory(Glyph.CodePoints[0]);
PrevCodePoint := AGlyphs[Index-1].CodePoints[0];
PrevCategory := GetHangulCategory(PrevCodePoint);
// Figure out what type of syllable we're dealing with
if (PrevCategory = hcLV) and (Category = hcT) then
begin
// <LV,T>
LVCodePoint := PrevCodePoint;
LjmoGlyph := nil;
VjmoGlyph := nil;
TjmoGlyph := Glyph;
end else
begin
if (Category = hcV) then
begin
// <L,V>
LjmoGlyph := AGlyphs[Index-1];
VjmoGlyph := Glyph;
TjmoGlyph := nil;
end else
begin
// <L,V,T>
LjmoGlyph := AGlyphs[Index-2];
VjmoGlyph := AGlyphs[Index-1];
TjmoGlyph := Glyph;
end;
LCodePoint := LjmoGlyph.CodePoints[0];
VCodePoint := VjmoGlyph.CodePoints[0];
// Make sure L and V are combining characters
if (Hangul.IsJamoL(LCodePoint) and Hangul.IsJamoV(VCodePoint)) then
LVCodePoint := Hangul.HangulSBase + ((LCodePoint - Hangul.JamoLBase) * Hangul.JamoVCount + (VCodePoint - Hangul.JamoVBase)) * Hangul.JamoTCount
else
LVCodePoint := 0;
end;
if (TjmoGlyph <> nil) then
TCodePoint := TjmoGlyph.CodePoints[0]
else
TCodePoint := Hangul.JamoTBase;
if (LVCodePoint <> 0) and ((TCodePoint = Hangul.JamoTBase) or Hangul.IsJamoT(TCodePoint)) then
begin
SCodePoint := LVCodePoint + (TCodePoint - Hangul.JamoTBase);
// Replace with a composed glyph if supported by the font,
// otherwise apply the proper OpenType features to each component.
GlyphID := Font.GetGlyphByCodePoint(SCodePoint);
if (GlyphID <> 0) then
begin
var Cluster := Glyph.Cluster;
var PlanFeatures: TPascalTypeFeatures;
PlanFeatures.Assign(Glyph.PlanFeatures);
if (PrevCategory = hcV) then
Count := 3
else
Count := 2;
// Delete the glyphs we just matched, i.e. backward up to and including the current glyph
Dec(Index, Count-1);
AGlyphs.Delete(Index, Count);
Glyph := AGlyphs.Insert(Index, Cluster);
Glyph.PlanFeatures.Assign(PlanFeatures);
Glyph.CodePoints := [SCodePoint];
Glyph.GlyphID := GlyphID;
Result := Index - Count + 1;
exit;
end;
end;
// Didn't compose (either a non-combining component or unsupported by font).
if (LjmoGlyph <> nil) then
LjmoGlyph.PlanFeatures.Add('ljmo');
if (VjmoGlyph <> nil) then
VjmoGlyph.PlanFeatures.Add('vjmo');
if (TjmoGlyph <> nil) then
TjmoGlyph.PlanFeatures.Add('tjmo');
if (PrevCategory = hcLV) then
begin
// Sequence was originally <L,V>, which got combined earlier.
// Either the T was non-combining, or the LVT glyph wasn't supported.
// Decompose the glyph again and apply OT PlanFeatures.
Decompose(AGlyphs, Index - 1);
Result := Index + 1;
end;
end;
procedure TPascalTypeHangulShaper.ReorderToneMark(var AGlyphs: TPascalTypeGlyphString; Index: integer);
function GetLength(CodePoint: TPascalTypeCodePoint): integer;
begin
case GetHangulCategory(CodePoint) of
hcLV, hcLVT:
Result := 1;
hcV:
Result := 2;
hcT:
Result := 3;
else
Result := 0;
end;
end;
var
GlyphIndex: integer;
PrevCodePoint: TPascalTypeCodePoint;
Count: integer;
begin
Assert(Index > 0);
// Move tone mark to the beginning of the previous syllable, unless it is zero width
GlyphIndex := Font.GetGlyphByCodePoint(AGlyphs[Index].CodePoints[0]);
if (GlyphIndex = 0) or (Font.GetAdvanceWidth(GlyphIndex) = 0) then
exit;
PrevCodePoint := AGlyphs[Index-1].CodePoints[0];
Count := GetLength(PrevCodePoint);
AGlyphs.Move(Index, Index-Count);
end;
function TPascalTypeHangulShaper.InsertDottedCircle(var AGlyphs: TPascalTypeGlyphString; Index: integer): integer;
begin
Assert(Index > 0);
Result := Index;
var GlyphID := Font.GetGlyphByCodePoint(PascalTypeUnicode.cpDottedCircle);
if (GlyphID = 0) then
exit;
var ToneMarkGlyph := AGlyphs[Index];
// If the tone mark is zero width, insert the dotted circle before, otherwise after
if (Font.GetAdvanceWidth(ToneMarkGlyph.GlyphID) <> 0) then
Inc(Index);
var Glyph := AGlyphs.Insert(Index, ToneMarkGlyph.Cluster); // TODO : Is this the correct cluster?
Glyph.GlyphID := GlyphID;
Glyph.CodePoints := [PascalTypeUnicode.cpDottedCircle];
Glyph.PlanFeatures.Assign(ToneMarkGlyph.PlanFeatures);
Inc(Result);
end;
procedure TPascalTypeHangulShaper.AssignLocalFeatures(const AFeatures: TPascalTypeFeatures; var AGlyphs: TPascalTypeGlyphString);
var
StateEntry: TStateEntry;
Category: THangulCategory;
Glyph: TPascalTypeGlyph;
CodePoint: TPascalTypeCodePoint;
i: integer;
begin
inherited AssignLocalFeatures(AFeatures, AGlyphs);
// Apply the state machine to map glyphs to PlanFeatures
StateEntry.NextState := sStart;
i := 0;
while (i < AGlyphs.Count) do
begin
Glyph := AGlyphs[i];
CodePoint := Glyph.CodePoints[0];
Category := GetHangulCategory(CodePoint);
StateEntry := StateMachine[StateEntry.NextState, Category];
case StateEntry.Action of
saDecompose:
// Decompose the composed syllable if it is not supported by the font.
if (not Font.HasGlyphByCodePoint(CodePoint)) then
i := Decompose(AGlyphs, i);
saCompose:
// Found a decomposed syllable.
// Try to compose if supported by the font.
i := Compose(AGlyphs, i);
saToneMark:
// Got a valid syllable, followed by a tone mark.
// Move the tone mark to the beginning of the syllable.
ReorderToneMark(AGlyphs, i);
saInvalid:
// Tone mark has no valid syllable to attach to, so insert a dotted circle
i := InsertDottedCircle(AGlyphs, i);
end;
Inc(i);
end;
end;
function TPascalTypeHangulShaper.NeedUnicodeComposition: boolean;
begin
Result := True;
end;
initialization
TPascalTypeShaper.RegisterShaperForScript('hang', TPascalTypeHangulShaper); // Hangul
end.