-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontextual.rs
526 lines (482 loc) · 16.7 KB
/
contextual.rs
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
use super::{coverage_index, covered, glyph_class};
use crate::hb::ot_layout_gsubgpos::OT::hb_ot_apply_context_t;
use crate::hb::ot_layout_gsubgpos::{
apply_lookup, match_backtrack, match_func_t, match_glyph, match_input, match_lookahead, Apply,
WouldApply, WouldApplyContext,
};
use skrifa::raw::tables::layout::{
ChainedSequenceContextFormat1, ChainedSequenceContextFormat2, ChainedSequenceContextFormat3,
SequenceContextFormat1, SequenceContextFormat2, SequenceContextFormat3, SequenceLookupRecord,
};
use skrifa::raw::types::BigEndian;
use ttf_parser::GlyphId;
impl WouldApply for SequenceContextFormat1<'_> {
fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
coverage_index(self.coverage(), ctx.glyphs[0])
.and_then(|index| {
self.seq_rule_sets()
.get(index as usize)
.transpose()
.ok()
.flatten()
})
.map(|set| {
set.seq_rules().iter().any(|rule| {
rule.map(|rule| {
let input = rule.input_sequence();
ctx.glyphs.len() == input.len() + 1
&& input.iter().enumerate().all(|(i, value)| {
match_glyph(ctx.glyphs[i + 1], value.get().to_u16())
})
})
.unwrap_or(false)
})
})
.unwrap_or_default()
}
}
impl Apply for SequenceContextFormat1<'_> {
fn apply(
&self,
ctx: &mut crate::hb::ot_layout_gsubgpos::OT::hb_ot_apply_context_t,
) -> Option<()> {
let glyph = skrifa::GlyphId::from(ctx.buffer.cur(0).as_glyph().0);
let index = self.coverage().ok()?.get(glyph)? as usize;
let set = self.seq_rule_sets().get(index)?.ok()?;
for rule in set.seq_rules().iter().filter_map(|rule| rule.ok()) {
let input = rule.input_sequence();
if apply_context(ctx, input, &match_glyph, rule.seq_lookup_records()).is_some() {
return Some(());
}
}
None
}
}
impl WouldApply for SequenceContextFormat2<'_> {
fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
let class_def = self.class_def().ok();
let match_fn = &match_class(&class_def);
let class = glyph_class(self.class_def(), ctx.glyphs[0]);
self.class_seq_rule_sets()
.get(class as usize)
.transpose()
.ok()
.flatten()
.map(|set| {
set.class_seq_rules().iter().any(|rule| {
rule.map(|rule| {
let input = rule.input_sequence();
ctx.glyphs.len() == input.len() + 1
&& input
.iter()
.enumerate()
.all(|(i, value)| match_fn(ctx.glyphs[i + 1], value.get()))
})
.unwrap_or(false)
})
})
.unwrap_or_default()
}
}
impl Apply for SequenceContextFormat2<'_> {
fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
let input_classes = self.class_def().ok();
let glyph = ctx.buffer.cur(0).as_skrifa_glyph16();
self.coverage().ok()?.get(glyph)?;
let index = input_classes.as_ref()?.get(glyph) as usize;
let set = self.class_seq_rule_sets().get(index)?.ok()?;
for rule in set.class_seq_rules().iter().filter_map(|rule| rule.ok()) {
let input = rule.input_sequence();
if apply_context(
ctx,
input,
&match_class(&input_classes),
rule.seq_lookup_records(),
)
.is_some()
{
return Some(());
}
}
None
}
}
impl WouldApply for SequenceContextFormat3<'_> {
fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
let coverages = self.coverages();
ctx.glyphs.len() == usize::from(coverages.len()) + 1
&& coverages
.iter()
.enumerate()
.all(|(i, coverage)| covered(coverage, ctx.glyphs[i + 1]))
}
}
impl Apply for SequenceContextFormat3<'_> {
fn apply(
&self,
ctx: &mut crate::hb::ot_layout_gsubgpos::OT::hb_ot_apply_context_t,
) -> Option<()> {
let glyph = skrifa::GlyphId::from(ctx.buffer.cur(0).as_glyph().0);
let input_coverages = self.coverages();
input_coverages.get(0).ok()?.get(glyph)?;
let input = |glyph: GlyphId, index: u16| {
input_coverages
.get(index as usize + 1)
.map(|cov| cov.get(skrifa::GlyphId::from(glyph.0)).is_some())
.unwrap_or_default()
};
let mut match_end = 0;
let mut match_positions = smallvec::SmallVec::from_elem(0, 4);
if match_input(
ctx,
input_coverages.len() as u16 - 1,
&input,
&mut match_end,
&mut match_positions,
None,
) {
ctx.buffer
.unsafe_to_break_from_outbuffer(Some(ctx.buffer.idx), Some(match_end));
apply_lookup(
ctx,
input_coverages.len() - 1,
&mut match_positions,
match_end,
self.seq_lookup_records(),
);
Some(())
} else {
ctx.buffer
.unsafe_to_concat(Some(ctx.buffer.idx), Some(match_end));
None
}
}
}
impl WouldApply for ChainedSequenceContextFormat1<'_> {
fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
coverage_index(self.coverage(), ctx.glyphs[0])
.and_then(|index| {
self.chained_seq_rule_sets()
.get(index as usize)
.transpose()
.ok()
.flatten()
})
.map(|set| {
set.chained_seq_rules().iter().any(|rule| {
rule.map(|rule| {
let input = rule.input_sequence();
(!ctx.zero_context
|| (rule.backtrack_glyph_count() == 0
&& rule.lookahead_glyph_count() == 0))
&& ctx.glyphs.len() == input.len() + 1
&& input.iter().enumerate().all(|(i, value)| {
match_glyph(ctx.glyphs[i + 1], value.get().to_u16())
})
})
.unwrap_or(false)
})
})
.unwrap_or_default()
}
}
impl Apply for ChainedSequenceContextFormat1<'_> {
fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
let glyph = skrifa::GlyphId::from(ctx.buffer.cur(0).as_glyph().0);
let index = self.coverage().ok()?.get(glyph)? as usize;
let set = self.chained_seq_rule_sets().get(index)?.ok()?;
for rule in set.chained_seq_rules().iter().filter_map(|rule| rule.ok()) {
let backtrack = rule.backtrack_sequence();
let input = rule.input_sequence();
let lookahead = rule.lookahead_sequence();
if apply_chain_context(
ctx,
backtrack,
input,
lookahead,
[&match_glyph; 3],
rule.seq_lookup_records(),
)
.is_some()
{
return Some(());
}
}
None
}
}
impl WouldApply for ChainedSequenceContextFormat2<'_> {
fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
let class_def = self.input_class_def().ok();
let match_fn = &match_class(&class_def);
let class = glyph_class(self.input_class_def(), ctx.glyphs[0]);
self.chained_class_seq_rule_sets()
.get(class as usize)
.transpose()
.ok()
.flatten()
.map(|set| {
set.chained_class_seq_rules().iter().any(|rule| {
rule.map(|rule| {
let input = rule.input_sequence();
(!ctx.zero_context
|| (rule.backtrack_glyph_count() == 0
&& rule.lookahead_glyph_count() == 0))
&& ctx.glyphs.len() == input.len() + 1
&& input
.iter()
.enumerate()
.all(|(i, value)| match_fn(ctx.glyphs[i + 1], value.get()))
})
.unwrap_or(false)
})
})
.unwrap_or_default()
}
}
/// Value represents glyph class.
fn match_class<'a>(
class_def: &'a Option<skrifa::raw::tables::layout::ClassDef<'a>>,
) -> impl Fn(GlyphId, u16) -> bool + 'a {
|glyph, value| {
class_def
.as_ref()
.map(|class_def| class_def.get(skrifa::GlyphId16::new(glyph.0)) == value)
.unwrap_or(false)
}
}
impl Apply for ChainedSequenceContextFormat2<'_> {
fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
let backtrack_classes = self.backtrack_class_def().ok();
let input_classes = self.input_class_def().ok();
let lookahead_classes = self.lookahead_class_def().ok();
let glyph = ctx.buffer.cur(0).as_skrifa_glyph16();
self.coverage().ok()?.get(glyph)?;
let index = input_classes.as_ref()?.get(glyph) as usize;
let set = self.chained_class_seq_rule_sets().get(index)?.ok()?;
for rule in set
.chained_class_seq_rules()
.iter()
.filter_map(|rule| rule.ok())
{
let backtrack = rule.backtrack_sequence();
let input = rule.input_sequence();
let lookahead = rule.lookahead_sequence();
if apply_chain_context(
ctx,
backtrack,
input,
lookahead,
[
&match_class(&backtrack_classes),
&match_class(&input_classes),
&match_class(&lookahead_classes),
],
rule.seq_lookup_records(),
)
.is_some()
{
return Some(());
}
}
None
}
}
impl WouldApply for ChainedSequenceContextFormat3<'_> {
fn would_apply(&self, ctx: &WouldApplyContext) -> bool {
let input_coverages = self.input_coverages();
(!ctx.zero_context
|| (self.backtrack_coverage_offsets().len() == 0
&& self.lookahead_coverage_offsets().len() == 0))
&& (ctx.glyphs.len() == input_coverages.len() + 1
&& input_coverages.iter().enumerate().all(|(i, coverage)| {
coverage
.map(|cov| {
cov.get(skrifa::GlyphId::from(ctx.glyphs[i + 1].0))
.is_some()
})
.unwrap_or(false)
}))
}
}
impl Apply for ChainedSequenceContextFormat3<'_> {
fn apply(&self, ctx: &mut hb_ot_apply_context_t) -> Option<()> {
let glyph = skrifa::GlyphId::from(ctx.buffer.cur(0).as_glyph().0);
let input_coverages = self.input_coverages();
input_coverages.get(0).ok()?.get(glyph)?;
let backtrack_coverages = self.backtrack_coverages();
let lookahead_coverages = self.lookahead_coverages();
let back = |glyph: GlyphId, index: u16| {
backtrack_coverages
.get(index as usize)
.map(|cov| cov.get(skrifa::GlyphId::from(glyph.0)).is_some())
.unwrap_or_default()
};
let ahead = |glyph: GlyphId, index: u16| {
lookahead_coverages
.get(index as usize)
.map(|cov| cov.get(skrifa::GlyphId::from(glyph.0)).is_some())
.unwrap_or_default()
};
let input = |glyph: GlyphId, index: u16| {
input_coverages
.get(index as usize + 1)
.map(|cov| cov.get(skrifa::GlyphId::from(glyph.0)).is_some())
.unwrap_or_default()
};
let mut end_index = ctx.buffer.idx;
let mut match_end = 0;
let mut match_positions = smallvec::SmallVec::from_elem(0, 4);
let input_matches = match_input(
ctx,
input_coverages.len() as u16 - 1,
&input,
&mut match_end,
&mut match_positions,
None,
);
if input_matches {
end_index = match_end;
}
if !(input_matches
&& match_lookahead(
ctx,
lookahead_coverages.len() as u16,
&ahead,
match_end,
&mut end_index,
))
{
ctx.buffer
.unsafe_to_concat(Some(ctx.buffer.idx), Some(end_index));
return None;
}
let mut start_index = ctx.buffer.out_len;
if !match_backtrack(
ctx,
backtrack_coverages.len() as u16,
&back,
&mut start_index,
) {
ctx.buffer
.unsafe_to_concat_from_outbuffer(Some(start_index), Some(end_index));
return None;
}
ctx.buffer
.unsafe_to_break_from_outbuffer(Some(start_index), Some(end_index));
apply_lookup(
ctx,
input_coverages.len() - 1,
&mut match_positions,
match_end,
self.seq_lookup_records(),
);
Some(())
}
}
trait ToU16: Copy {
fn to_u16(self) -> u16;
}
impl ToU16 for BigEndian<skrifa::GlyphId16> {
fn to_u16(self) -> u16 {
self.get().to_u16()
}
}
impl ToU16 for BigEndian<u16> {
fn to_u16(self) -> u16 {
self.get()
}
}
fn apply_context<T: ToU16>(
ctx: &mut hb_ot_apply_context_t,
input: &[T],
match_func: &match_func_t,
lookups: &[SequenceLookupRecord],
) -> Option<()> {
let match_func = |glyph, index| {
let value = input.get(index as usize).unwrap().to_u16();
match_func(glyph, value)
};
let mut match_end = 0;
let mut match_positions = smallvec::SmallVec::from_elem(0, 4);
if match_input(
ctx,
input.len() as _,
&match_func,
&mut match_end,
&mut match_positions,
None,
) {
ctx.buffer
.unsafe_to_break(Some(ctx.buffer.idx), Some(match_end));
apply_lookup(
ctx,
usize::from(input.len()),
&mut match_positions,
match_end,
lookups,
);
return Some(());
}
None
}
fn apply_chain_context<T: ToU16>(
ctx: &mut hb_ot_apply_context_t,
backtrack: &[T],
input: &[T],
lookahead: &[T],
match_funcs: [&match_func_t; 3],
lookups: &[SequenceLookupRecord],
) -> Option<()> {
// NOTE: Whenever something in this method changes, we also need to
// change it in the `apply` implementation for ChainedContextLookup.
let f1 = |glyph, index| {
let value = (*backtrack.get(index as usize).unwrap()).to_u16();
match_funcs[0](glyph, value)
};
let f2 = |glyph, index| {
let value = (*lookahead.get(index as usize).unwrap()).to_u16();
match_funcs[2](glyph, value)
};
let f3 = |glyph, index| {
let value = (*input.get(index as usize).unwrap()).to_u16();
match_funcs[1](glyph, value)
};
let mut end_index = ctx.buffer.idx;
let mut match_end = 0;
let mut match_positions = smallvec::SmallVec::from_elem(0, 4);
let input_matches = match_input(
ctx,
input.len() as u16,
&f3,
&mut match_end,
&mut match_positions,
None,
);
if input_matches {
end_index = match_end;
}
if !(input_matches
&& match_lookahead(ctx, lookahead.len() as u16, &f2, match_end, &mut end_index))
{
ctx.buffer
.unsafe_to_concat(Some(ctx.buffer.idx), Some(end_index));
return None;
}
let mut start_index = ctx.buffer.out_len;
if !match_backtrack(ctx, backtrack.len() as u16, &f1, &mut start_index) {
ctx.buffer
.unsafe_to_concat_from_outbuffer(Some(start_index), Some(end_index));
return None;
}
ctx.buffer
.unsafe_to_break_from_outbuffer(Some(start_index), Some(end_index));
apply_lookup(
ctx,
usize::from(input.len()),
&mut match_positions,
match_end,
lookups,
);
Some(())
}