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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.complexscripts.fonts;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;
import org.apache.fop.complexscripts.util.GlyphContextTester;
import org.apache.fop.complexscripts.util.GlyphSequence;
import org.apache.fop.complexscripts.util.GlyphTester;
import org.apache.fop.complexscripts.util.ScriptContextTester;
// CSOFF: LineLengthCheck
/**
* <p>The <code>GlyphProcessingState</code> implements a common, base state object used during glyph substitution
* and positioning processing.</p>
*
* <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
*/
public class GlyphProcessingState {
/** governing glyph definition table */
protected GlyphDefinitionTable gdef;
/** governing script */
protected String script;
/** governing language */
protected String language;
/** governing feature */
protected String feature;
/** current input glyph sequence */
protected GlyphSequence igs;
/** current index in input sequence */
protected int index;
/** last (maximum) index of input sequence (exclusive) */
protected int indexLast;
/** consumed, updated after each successful subtable application */
protected int consumed;
/** lookup flags */
protected int lookupFlags;
/** class match set */
protected int classMatchSet;
/** script specific context tester or null */
protected ScriptContextTester sct;
/** glyph context tester or null */
protected GlyphContextTester gct;
/** ignore base glyph tester */
protected GlyphTester ignoreBase;
/** ignore ligature glyph tester */
protected GlyphTester ignoreLigature;
/** ignore mark glyph tester */
protected GlyphTester ignoreMark;
/** default ignore glyph tester */
protected GlyphTester ignoreDefault;
/** current subtable */
private GlyphSubtable subtable;
/**
* Construct default (reset) glyph processing state.
*/
public GlyphProcessingState() {
}
/**
* Construct glyph processing state.
* @param gs input glyph sequence
* @param script script identifier
* @param language language identifier
* @param feature feature identifier
* @param sct script context tester (or null)
*/
protected GlyphProcessingState(GlyphSequence gs, String script, String language, String feature, ScriptContextTester sct) {
this.script = script;
this.language = language;
this.feature = feature;
this.igs = gs;
this.indexLast = gs.getGlyphCount();
this.sct = sct;
this.gct = (sct != null) ? sct.getTester(feature) : null;
this.ignoreBase = new GlyphTester() { public boolean test(int gi, int flags) { return isIgnoredBase(gi, flags); } };
this.ignoreLigature = new GlyphTester() { public boolean test(int gi, int flags) { return isIgnoredLigature(gi, flags); } };
this.ignoreMark = new GlyphTester() { public boolean test(int gi, int flags) { return isIgnoredMark(gi, flags); } };
}
/**
* Construct glyph processing state using an existing state object using shallow copy
* except as follows: input glyph sequence is copied deep except for its characters array.
* @param s existing processing state to copy from
*/
protected GlyphProcessingState(GlyphProcessingState s) {
this (new GlyphSequence(s.igs), s.script, s.language, s.feature, s.sct);
setPosition(s.index);
}
/**
* Reset glyph processing state.
* @param gs input glyph sequence
* @param script script identifier
* @param language language identifier
* @param feature feature identifier
* @param sct script context tester (or null)
*/
protected GlyphProcessingState reset(GlyphSequence gs, String script, String language, String feature, ScriptContextTester sct) {
this.gdef = null;
this.script = script;
this.language = language;
this.feature = feature;
this.igs = gs;
this.index = 0;
this.indexLast = gs.getGlyphCount();
this.consumed = 0;
this.lookupFlags = 0;
this.classMatchSet = 0;
this.sct = sct;
this.gct = (sct != null) ? sct.getTester(feature) : null;
this.ignoreBase = new GlyphTester() { public boolean test(int gi, int flags) { return isIgnoredBase(gi, flags); } };
this.ignoreLigature = new GlyphTester() { public boolean test(int gi, int flags) { return isIgnoredLigature(gi, flags); } };
this.ignoreMark = new GlyphTester() { public boolean test(int gi, int flags) { return isIgnoredMark(gi, flags); } };
this.ignoreDefault = null;
this.subtable = null;
return this;
}
/**
* Set governing glyph definition table.
* @param gdef glyph definition table (or null, to unset)
*/
public void setGDEF(GlyphDefinitionTable gdef) {
if (this.gdef == null) {
this.gdef = gdef;
} else if (gdef == null) {
this.gdef = null;
}
}
/**
* Obtain governing glyph definition table.
* @return glyph definition table (or null, to not set)
*/
public GlyphDefinitionTable getGDEF() {
return gdef;
}
/**
* Set governing lookup flags
* @param flags lookup flags (or zero, to unset)
*/
public void setLookupFlags(int flags) {
if (this.lookupFlags == 0) {
this.lookupFlags = flags;
} else if (flags == 0) {
this.lookupFlags = 0;
}
}
/**
* Obtain governing lookup flags.
* @return lookup flags (zero may indicate unset or no flags)
*/
public int getLookupFlags() {
return lookupFlags;
}
/**
* Obtain governing class match set.
* @param gi glyph index that may be used to determine which match set applies
* @return class match set (zero may indicate unset or no set)
*/
public int getClassMatchSet(int gi) {
return 0;
}
/**
* Set default ignore tester.
* @param ignoreDefault glyph tester (or null, to unset)
*/
public void setIgnoreDefault(GlyphTester ignoreDefault) {
if (this.ignoreDefault == null) {
this.ignoreDefault = ignoreDefault;
} else if (ignoreDefault == null) {
this.ignoreDefault = null;
}
}
/**
* Obtain governing default ignores tester.
* @return default ignores tester
*/
public GlyphTester getIgnoreDefault() {
return ignoreDefault;
}
/**
* Update glyph subtable specific state. Each time a
* different glyph subtable is to be applied, it is used
* to update this state prior to application, after which
* this state is to be reset.
* @param st glyph subtable to use for update
*/
public void updateSubtableState(GlyphSubtable st) {
if (this.subtable != st) {
setGDEF(st.getGDEF());
setLookupFlags(st.getFlags());
setIgnoreDefault(getIgnoreTester(getLookupFlags()));
this.subtable = st;
}
}
/**
* Obtain current position index in input glyph sequence.
* @return current index
*/
public int getPosition() {
return index;
}
/**
* Set (seek to) position index in input glyph sequence.
* @param index to seek to
* @throws IndexOutOfBoundsException if index is less than zero
* or exceeds last valid position
*/
public void setPosition(int index) throws IndexOutOfBoundsException {
if ((index >= 0) && (index <= indexLast)) {
this.index = index;
} else {
throw new IndexOutOfBoundsException();
}
}
/**
* Obtain last valid position index in input glyph sequence.
* @return current last index
*/
public int getLastPosition() {
return indexLast;
}
/**
* Determine if at least one glyph remains in
* input sequence.
* @return true if one or more glyph remains
*/
public boolean hasNext() {
return hasNext(1);
}
/**
* Determine if at least <code>count</code> glyphs remain in
* input sequence.
* @param count of glyphs to test
* @return true if at least <code>count</code> glyphs are available
*/
public boolean hasNext(int count) {
return (index + count) <= indexLast;
}
/**
* Update the current position index based upon previously consumed
* glyphs, i.e., add the consuemd count to the current position index.
* If no glyphs were previously consumed, then forces exactly one
* glyph to be consumed.
* @return the new (updated) position index
*/
public int next() {
if (index < indexLast) {
// force consumption of at least one input glyph
if (consumed == 0) {
consumed = 1;
}
index += consumed;
consumed = 0;
if (index > indexLast) {
index = indexLast;
}
}
return index;
}
/**
* Determine if at least one backtrack (previous) glyph is present
* in input sequence.
* @return true if one or more glyph remains
*/
public boolean hasPrev() {
return hasPrev(1);
}
/**
* Determine if at least <code>count</code> backtrack (previous) glyphs
* are present in input sequence.
* @param count of glyphs to test
* @return true if at least <code>count</code> glyphs are available
*/
public boolean hasPrev(int count) {
return (index - count) >= 0;
}
/**
* Update the current position index based upon previously consumed
* glyphs, i.e., subtract the consuemd count from the current position index.
* If no glyphs were previously consumed, then forces exactly one
* glyph to be consumed. This method is used to traverse an input
* glyph sequence in reverse order.
* @return the new (updated) position index
*/
public int prev() {
if (index > 0) {
// force consumption of at least one input glyph
if (consumed == 0) {
consumed = 1;
}
index -= consumed;
consumed = 0;
if (index < 0) {
index = 0;
}
}
return index;
}
/**
* Record the consumption of <code>count</code> glyphs such that
* this consumption never exceeds the number of glyphs in the input glyph
* sequence.
* @param count of glyphs to consume
* @return newly adjusted consumption count
* @throws IndexOutOfBoundsException if count would cause consumption
* to exceed count of glyphs in input glyph sequence
*/
public int consume(int count) throws IndexOutOfBoundsException {
if ((consumed + count) <= indexLast) {
consumed += count;
return consumed;
} else {
throw new IndexOutOfBoundsException();
}
}
/**
* Determine if any consumption has occurred.
* @return true if consumption count is greater than zero
*/
public boolean didConsume() {
return consumed > 0;
}
/**
* Obtain reference to input glyph sequence, which must not be modified.
* @return input glyph sequence
*/
public GlyphSequence getInput() {
return igs;
}
/**
* Obtain glyph at specified offset from current position.
* @param offset from current position
* @return glyph at specified offset from current position
* @throws IndexOutOfBoundsException if no glyph available at offset
*/
public int getGlyph(int offset) throws IndexOutOfBoundsException {
int i = index + offset;
if ((i >= 0) && (i < indexLast)) {
return igs.getGlyph(i);
} else {
throw new IndexOutOfBoundsException("attempting index at " + i);
}
}
/**
* Obtain glyph at current position.
* @return glyph at current position
* @throws IndexOutOfBoundsException if no glyph available
*/
public int getGlyph() throws IndexOutOfBoundsException {
return getGlyph(0);
}
/**
* Set (replace) glyph at specified offset from current position.
* @param offset from current position
* @param glyph to set at specified offset from current position
* @throws IndexOutOfBoundsException if specified offset is not valid position
*/
public void setGlyph(int offset, int glyph) throws IndexOutOfBoundsException {
int i = index + offset;
if ((i >= 0) && (i < indexLast)) {
igs.setGlyph(i, glyph);
} else {
throw new IndexOutOfBoundsException("attempting index at " + i);
}
}
/**
* Obtain character association of glyph at specified offset from current position.
* @param offset from current position
* @return character association of glyph at current position
* @throws IndexOutOfBoundsException if offset results in an invalid index into input glyph sequence
*/
public GlyphSequence.CharAssociation getAssociation(int offset) throws IndexOutOfBoundsException {
int i = index + offset;
if ((i >= 0) && (i < indexLast)) {
return igs.getAssociation(i);
} else {
throw new IndexOutOfBoundsException("attempting index at " + i);
}
}
/**
* Obtain character association of glyph at current position.
* @return character association of glyph at current position
* @throws IndexOutOfBoundsException if no glyph available
*/
public GlyphSequence.CharAssociation getAssociation() throws IndexOutOfBoundsException {
return getAssociation(0);
}
/**
* Obtain <code>count</code> glyphs starting at specified offset from current position. If
* <code>reverseOrder</code> is true, then glyphs are returned in reverse order starting at specified offset
* and going in reverse towards beginning of input glyph sequence.
* @param offset from current position
* @param count number of glyphs to obtain
* @param reverseOrder true if to obtain in reverse order
* @param ignoreTester glyph tester to use to determine which glyphs are ignored (or null, in which case none are ignored)
* @param glyphs array to use to fetch glyphs
* @param counts int[2] array to receive fetched glyph counts, where counts[0] will
* receive the number of glyphs obtained, and counts[1] will receive the number of glyphs
* ignored
* @return array of glyphs
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public int[] getGlyphs(int offset, int count, boolean reverseOrder, GlyphTester ignoreTester, int[] glyphs, int[] counts) throws IndexOutOfBoundsException {
if (count < 0) {
count = getGlyphsAvailable(offset, reverseOrder, ignoreTester) [ 0 ];
}
int start = index + offset;
if (start < 0) {
throw new IndexOutOfBoundsException("will attempt index at " + start);
} else if (!reverseOrder && ((start + count) > indexLast)) {
throw new IndexOutOfBoundsException("will attempt index at " + (start + count));
} else if (reverseOrder && ((start + 1) < count)) {
throw new IndexOutOfBoundsException("will attempt index at " + (start - count));
}
if (glyphs == null) {
glyphs = new int [ count ];
} else if (glyphs.length != count) {
throw new IllegalArgumentException("glyphs array is non-null, but its length (" + glyphs.length + "), is not equal to count (" + count + ")");
}
if (!reverseOrder) {
return getGlyphsForward(start, count, ignoreTester, glyphs, counts);
} else {
return getGlyphsReverse(start, count, ignoreTester, glyphs, counts);
}
}
private int[] getGlyphsForward(int start, int count, GlyphTester ignoreTester, int[] glyphs, int[] counts) throws IndexOutOfBoundsException {
int counted = 0;
int ignored = 0;
for (int i = start, n = indexLast; (i < n) && (counted < count); i++) {
int gi = getGlyph(i - index);
if (gi == 65535) {
ignored++;
} else {
if ((ignoreTester == null) || !ignoreTester.test(gi, getLookupFlags())) {
glyphs [ counted++ ] = gi;
} else {
ignored++;
}
}
}
if ((counts != null) && (counts.length > 1)) {
counts[0] = counted;
counts[1] = ignored;
}
return glyphs;
}
private int[] getGlyphsReverse(int start, int count, GlyphTester ignoreTester, int[] glyphs, int[] counts) throws IndexOutOfBoundsException {
int counted = 0;
int ignored = 0;
for (int i = start; (i >= 0) && (counted < count); i--) {
int gi = getGlyph(i - index);
if (gi == 65535) {
ignored++;
} else {
if ((ignoreTester == null) || !ignoreTester.test(gi, getLookupFlags())) {
glyphs [ counted++ ] = gi;
} else {
ignored++;
}
}
}
if ((counts != null) && (counts.length > 1)) {
counts[0] = counted;
counts[1] = ignored;
}
return glyphs;
}
/**
* Obtain <code>count</code> glyphs starting at specified offset from current position. If
* offset is negative, then glyphs are returned in reverse order starting at specified offset
* and going in reverse towards beginning of input glyph sequence.
* @param offset from current position
* @param count number of glyphs to obtain
* @param glyphs array to use to fetch glyphs
* @param counts int[2] array to receive fetched glyph counts, where counts[0] will
* receive the number of glyphs obtained, and counts[1] will receive the number of glyphs
* ignored
* @return array of glyphs
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public int[] getGlyphs(int offset, int count, int[] glyphs, int[] counts) throws IndexOutOfBoundsException {
return getGlyphs(offset, count, offset < 0, ignoreDefault, glyphs, counts);
}
/**
* Obtain all glyphs starting from current position to end of input glyph sequence.
* @return array of available glyphs
* @throws IndexOutOfBoundsException if no glyph available
*/
public int[] getGlyphs() throws IndexOutOfBoundsException {
return getGlyphs(0, indexLast - index, false, null, null, null);
}
/**
* Obtain <code>count</code> ignored glyphs starting at specified offset from current position. If
* <code>reverseOrder</code> is true, then glyphs are returned in reverse order starting at specified offset
* and going in reverse towards beginning of input glyph sequence.
* @param offset from current position
* @param count number of glyphs to obtain
* @param reverseOrder true if to obtain in reverse order
* @param ignoreTester glyph tester to use to determine which glyphs are ignored (or null, in which case none are ignored)
* @param glyphs array to use to fetch glyphs
* @param counts int[2] array to receive fetched glyph counts, where counts[0] will
* receive the number of glyphs obtained, and counts[1] will receive the number of glyphs
* ignored
* @return array of glyphs
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public int[] getIgnoredGlyphs(int offset, int count, boolean reverseOrder, GlyphTester ignoreTester, int[] glyphs, int[] counts) throws IndexOutOfBoundsException {
return getGlyphs(offset, count, reverseOrder, new NotGlyphTester(ignoreTester), glyphs, counts);
}
/**
* Obtain <code>count</code> ignored glyphs starting at specified offset from current position. If <code>offset</code> is
* negative, then fetch in reverse order.
* @param offset from current position
* @param count number of glyphs to obtain
* @return array of glyphs
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public int[] getIgnoredGlyphs(int offset, int count) throws IndexOutOfBoundsException {
return getIgnoredGlyphs(offset, count, offset < 0, ignoreDefault, null, null);
}
/**
* Determine if glyph at specified offset from current position is ignored. If <code>offset</code> is
* negative, then test in reverse order.
* @param offset from current position
* @param ignoreTester glyph tester to use to determine which glyphs are ignored (or null, in which case none are ignored)
* @return true if glyph is ignored
* @throws IndexOutOfBoundsException if offset results in an
* invalid index into input glyph sequence
*/
public boolean isIgnoredGlyph(int offset, GlyphTester ignoreTester) throws IndexOutOfBoundsException {
return (ignoreTester != null) && ignoreTester.test(getGlyph(offset), getLookupFlags());
}
/**
* Determine if glyph at specified offset from current position is ignored. If <code>offset</code> is
* negative, then test in reverse order.
* @param offset from current position
* @return true if glyph is ignored
* @throws IndexOutOfBoundsException if offset results in an
* invalid index into input glyph sequence
*/
public boolean isIgnoredGlyph(int offset) throws IndexOutOfBoundsException {
return isIgnoredGlyph(offset, ignoreDefault);
}
/**
* Determine if glyph at current position is ignored.
* @return true if glyph is ignored
* @throws IndexOutOfBoundsException if offset results in an
* invalid index into input glyph sequence
*/
public boolean isIgnoredGlyph() throws IndexOutOfBoundsException {
return isIgnoredGlyph(getPosition());
}
/**
* Determine number of glyphs available starting at specified offset from current position. If
* <code>reverseOrder</code> is true, then search backwards in input glyph sequence.
* @param offset from current position
* @param reverseOrder true if to obtain in reverse order
* @param ignoreTester glyph tester to use to determine which glyphs to count (or null, in which case none are ignored)
* @return an int[2] array where counts[0] is the number of glyphs available, and counts[1] is the number of glyphs ignored
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public int[] getGlyphsAvailable(int offset, boolean reverseOrder, GlyphTester ignoreTester) throws IndexOutOfBoundsException {
int start = index + offset;
if ((start < 0) || (start > indexLast)) {
return new int[] { 0, 0 };
} else if (!reverseOrder) {
return getGlyphsAvailableForward(start, ignoreTester);
} else {
return getGlyphsAvailableReverse(start, ignoreTester);
}
}
private int[] getGlyphsAvailableForward(int start, GlyphTester ignoreTester) throws IndexOutOfBoundsException {
int counted = 0;
int ignored = 0;
if (ignoreTester == null) {
counted = indexLast - start;
} else {
for (int i = start, n = indexLast; i < n; i++) {
int gi = getGlyph(i - index);
if (gi == 65535) {
ignored++;
} else {
if (ignoreTester.test(gi, getLookupFlags())) {
ignored++;
} else {
counted++;
}
}
}
}
return new int[] { counted, ignored };
}
private int[] getGlyphsAvailableReverse(int start, GlyphTester ignoreTester) throws IndexOutOfBoundsException {
int counted = 0;
int ignored = 0;
if (ignoreTester == null) {
counted = start + 1;
} else {
for (int i = start; i >= 0; i--) {
int gi = getGlyph(i - index);
if (gi == 65535) {
ignored++;
} else {
if (ignoreTester.test(gi, getLookupFlags())) {
ignored++;
} else {
counted++;
}
}
}
}
return new int[] { counted, ignored };
}
/**
* Determine number of glyphs available starting at specified offset from current position. If
* <code>reverseOrder</code> is true, then search backwards in input glyph sequence. Uses the
* default ignores tester.
* @param offset from current position
* @param reverseOrder true if to obtain in reverse order
* @return an int[2] array where counts[0] is the number of glyphs available, and counts[1] is the number of glyphs ignored
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public int[] getGlyphsAvailable(int offset, boolean reverseOrder) throws IndexOutOfBoundsException {
return getGlyphsAvailable(offset, reverseOrder, ignoreDefault);
}
/**
* Determine number of glyphs available starting at specified offset from current position. If
* offset is negative, then search backwards in input glyph sequence. Uses the
* default ignores tester.
* @param offset from current position
* @return an int[2] array where counts[0] is the number of glyphs available, and counts[1] is the number of glyphs ignored
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public int[] getGlyphsAvailable(int offset) throws IndexOutOfBoundsException {
return getGlyphsAvailable(offset, offset < 0);
}
/**
* Obtain <code>count</code> character associations of glyphs starting at specified offset from current position. If
* <code>reverseOrder</code> is true, then associations are returned in reverse order starting at specified offset
* and going in reverse towards beginning of input glyph sequence.
* @param offset from current position
* @param count number of associations to obtain
* @param reverseOrder true if to obtain in reverse order
* @param ignoreTester glyph tester to use to determine which glyphs are ignored (or null, in which case none are ignored)
* @param associations array to use to fetch associations
* @param counts int[2] array to receive fetched association counts, where counts[0] will
* receive the number of associations obtained, and counts[1] will receive the number of glyphs whose
* associations were ignored
* @return array of associations
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public GlyphSequence.CharAssociation[] getAssociations(int offset, int count, boolean reverseOrder, GlyphTester ignoreTester, GlyphSequence.CharAssociation[] associations, int[] counts)
throws IndexOutOfBoundsException {
if (count < 0) {
count = getGlyphsAvailable(offset, reverseOrder, ignoreTester) [ 0 ];
}
int start = index + offset;
if (start < 0) {
throw new IndexOutOfBoundsException("will attempt index at " + start);
} else if (!reverseOrder && ((start + count) > indexLast)) {
throw new IndexOutOfBoundsException("will attempt index at " + (start + count));
} else if (reverseOrder && ((start + 1) < count)) {
throw new IndexOutOfBoundsException("will attempt index at " + (start - count));
}
if (associations == null) {
associations = new GlyphSequence.CharAssociation [ count ];
} else if (associations.length != count) {
throw new IllegalArgumentException("associations array is non-null, but its length (" + associations.length + "), is not equal to count (" + count + ")");
}
if (!reverseOrder) {
return getAssociationsForward(start, count, ignoreTester, associations, counts);
} else {
return getAssociationsReverse(start, count, ignoreTester, associations, counts);
}
}
private GlyphSequence.CharAssociation[] getAssociationsForward(int start, int count, GlyphTester ignoreTester, GlyphSequence.CharAssociation[] associations, int[] counts)
throws IndexOutOfBoundsException {
int counted = 0;
int ignored = 0;
for (int i = start, n = indexLast, k = 0; i < n; i++) {
int gi = getGlyph(i - index);
if (gi == 65535) {
ignored++;
} else {
if ((ignoreTester == null) || !ignoreTester.test(gi, getLookupFlags())) {
if (k < count) {
associations [ k++ ] = getAssociation(i - index);
counted++;
} else {
break;
}
} else {
ignored++;
}
}
}
if ((counts != null) && (counts.length > 1)) {
counts[0] = counted;
counts[1] = ignored;
}
return associations;
}
private GlyphSequence.CharAssociation[] getAssociationsReverse(int start, int count, GlyphTester ignoreTester, GlyphSequence.CharAssociation[] associations, int[] counts)
throws IndexOutOfBoundsException {
int counted = 0;
int ignored = 0;
for (int i = start, k = 0; i >= 0; i--) {
int gi = getGlyph(i - index);
if (gi == 65535) {
ignored++;
} else {
if ((ignoreTester == null) || !ignoreTester.test(gi, getLookupFlags())) {
if (k < count) {
associations [ k++ ] = getAssociation(i - index);
counted++;
} else {
break;
}
} else {
ignored++;
}
}
}
if ((counts != null) && (counts.length > 1)) {
counts[0] = counted;
counts[1] = ignored;
}
return associations;
}
/**
* Obtain <code>count</code> character associations of glyphs starting at specified offset from current position. If
* offset is negative, then search backwards in input glyph sequence. Uses the
* default ignores tester.
* @param offset from current position
* @param count number of associations to obtain
* @return array of associations
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public GlyphSequence.CharAssociation[] getAssociations(int offset, int count) throws IndexOutOfBoundsException {
return getAssociations(offset, count, offset < 0, ignoreDefault, null, null);
}
/**
* Obtain <code>count</code> character associations of ignored glyphs starting at specified offset from current position. If
* <code>reverseOrder</code> is true, then glyphs are returned in reverse order starting at specified offset
* and going in reverse towards beginning of input glyph sequence.
* @param offset from current position
* @param count number of character associations to obtain
* @param reverseOrder true if to obtain in reverse order
* @param ignoreTester glyph tester to use to determine which glyphs are ignored (or null, in which case none are ignored)
* @param associations array to use to fetch associations
* @param counts int[2] array to receive fetched association counts, where counts[0] will
* receive the number of associations obtained, and counts[1] will receive the number of glyphs whose
* associations were ignored
* @return array of associations
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public GlyphSequence.CharAssociation[] getIgnoredAssociations(int offset, int count, boolean reverseOrder, GlyphTester ignoreTester, GlyphSequence.CharAssociation[] associations, int[] counts)
throws IndexOutOfBoundsException {
return getAssociations(offset, count, reverseOrder, new NotGlyphTester(ignoreTester), associations, counts);
}
/**
* Obtain <code>count</code> character associations of ignored glyphs starting at specified offset from current position. If
* offset is negative, then search backwards in input glyph sequence. Uses the
* default ignores tester.
* @param offset from current position
* @param count number of character associations to obtain
* @return array of associations
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public GlyphSequence.CharAssociation[] getIgnoredAssociations(int offset, int count) throws IndexOutOfBoundsException {
return getIgnoredAssociations(offset, count, offset < 0, ignoreDefault, null, null);
}
/**
* Replace subsequence of input glyph sequence starting at specified offset from current position and of
* length <code>count</code> glyphs with a subsequence of the sequence <code>gs</code> starting from the specified
* offset <code>gsOffset</code> of length <code>gsCount</code> glyphs.
* @param offset from current position
* @param count number of glyphs to replace, which, if negative means all glyphs from offset to end of input sequence
* @param gs glyph sequence from which to obtain replacement glyphs
* @param gsOffset offset of first glyph in replacement sequence
* @param gsCount count of glyphs in replacement sequence starting at <code>gsOffset</code>
* @return true if replacement occurred, or false if replacement would result in no change to input glyph sequence
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public boolean replaceInput(int offset, int count, GlyphSequence gs, int gsOffset, int gsCount) throws IndexOutOfBoundsException {
int nig = (igs != null) ? igs.getGlyphCount() : 0;
int position = getPosition() + offset;
if (position < 0) {
position = 0;
} else if (position > nig) {
position = nig;
}
if ((count < 0) || ((position + count) > nig)) {
count = nig - position;
}
int nrg = (gs != null) ? gs.getGlyphCount() : 0;
if (gsOffset < 0) {
gsOffset = 0;
} else if (gsOffset > nrg) {
gsOffset = nrg;
}
if ((gsCount < 0) || ((gsOffset + gsCount) > nrg)) {
gsCount = nrg - gsOffset;
}
int ng = nig + gsCount - count;
IntBuffer gb = IntBuffer.allocate(ng);
List al = new ArrayList(ng);
for (int i = 0, n = position; i < n; i++) {
gb.put(igs.getGlyph(i));
al.add(igs.getAssociation(i));
}
for (int i = gsOffset, n = gsOffset + gsCount; i < n; i++) {
gb.put(gs.getGlyph(i));
al.add(gs.getAssociation(i));
}
for (int i = position + count, n = nig; i < n; i++) {
gb.put(igs.getGlyph(i));
al.add(igs.getAssociation(i));
}
gb.flip();
if (igs.compareGlyphs(gb) != 0) {
this.igs = new GlyphSequence(igs.getCharacters(), gb, al);
this.indexLast = gb.limit();
return true;
} else {
return false;
}
}
/**
* Replace subsequence of input glyph sequence starting at specified offset from current position and of
* length <code>count</code> glyphs with all glyphs in the replacement sequence <code>gs</code>.
* @param offset from current position
* @param count number of glyphs to replace, which, if negative means all glyphs from offset to end of input sequence
* @param gs glyph sequence from which to obtain replacement glyphs
* @return true if replacement occurred, or false if replacement would result in no change to input glyph sequence
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public boolean replaceInput(int offset, int count, GlyphSequence gs) throws IndexOutOfBoundsException {
return replaceInput(offset, count, gs, 0, gs.getGlyphCount());
}
/**
* Erase glyphs in input glyph sequence starting at specified offset from current position, where each glyph
* in the specified <code>glyphs</code> array is matched, one at a time, and when a (forward searching) match is found
* in the input glyph sequence, the matching glyph is replaced with the glyph index 65535.
* @param offset from current position
* @param glyphs array of glyphs to erase
* @return the number of glyphs erased, which may be less than the number of specified glyphs
* @throws IndexOutOfBoundsException if offset or count results in an
* invalid index into input glyph sequence
*/
public int erase(int offset, int[] glyphs) throws IndexOutOfBoundsException {
int start = index + offset;
if ((start < 0) || (start > indexLast)) {
throw new IndexOutOfBoundsException("will attempt index at " + start);
} else {
int erased = 0;
for (int i = start - index, n = indexLast - start; i < n; i++) {
int gi = getGlyph(i);
if (gi == glyphs [ erased ]) {
setGlyph(i, 65535);
erased++;
}
}
return erased;
}
}
/**
* Determine if is possible that the current input sequence satisfies a script specific
* context testing predicate. If no predicate applies, then application is always possible.
* @return true if no script specific context tester applies or if a specified tester returns
* true for the current input sequence context
*/
public boolean maybeApplicable() {
if (gct == null) {
return true;
} else {
return gct.test(script, language, feature, igs, index, getLookupFlags());
}
}
/**
* Apply default application semantices; namely, consume one glyph.
*/
public void applyDefault() {
consumed += 1;
}
/**
* Determine if specified glyph is a base glyph according to the governing
* glyph definition table.
* @param gi glyph index to test
* @return true if glyph definition table records glyph as a base glyph; otherwise, false
*/
public boolean isBase(int gi) {
if (gdef != null) {
return gdef.isGlyphClass(gi, GlyphDefinitionTable.GLYPH_CLASS_BASE);
} else {
return false;
}
}
/**
* Determine if specified glyph is an ignored base glyph according to the governing
* glyph definition table.
* @param gi glyph index to test
* @param flags that apply to lookup in scope
* @return true if glyph definition table records glyph as a base glyph; otherwise, false
*/
public boolean isIgnoredBase(int gi, int flags) {
return ((flags & GlyphSubtable.LF_IGNORE_BASE) != 0) && isBase(gi);
}
/**
* Determine if specified glyph is an ligature glyph according to the governing
* glyph definition table.
* @param gi glyph index to test
* @return true if glyph definition table records glyph as a ligature glyph; otherwise, false
*/
public boolean isLigature(int gi) {
if (gdef != null) {
return gdef.isGlyphClass(gi, GlyphDefinitionTable.GLYPH_CLASS_LIGATURE);
} else {
return false;
}
}
/**
* Determine if specified glyph is an ignored ligature glyph according to the governing
* glyph definition table.
* @param gi glyph index to test
* @param flags that apply to lookup in scope
* @return true if glyph definition table records glyph as a ligature glyph; otherwise, false
*/
public boolean isIgnoredLigature(int gi, int flags) {
return ((flags & GlyphSubtable.LF_IGNORE_LIGATURE) != 0) && isLigature(gi);
}
/**
* Determine if specified glyph is a mark glyph according to the governing
* glyph definition table.
* @param gi glyph index to test
* @return true if glyph definition table records glyph as a mark glyph; otherwise, false
*/
public boolean isMark(int gi) {
if (gdef != null) {
return gdef.isGlyphClass(gi, GlyphDefinitionTable.GLYPH_CLASS_MARK);
} else {
return false;
}
}
/**
* Determine if specified glyph is an ignored ligature glyph according to the governing
* glyph definition table.
* @param gi glyph index to test
* @param flags that apply to lookup in scope
* @return true if glyph definition table records glyph as a ligature glyph; otherwise, false
*/
public boolean isIgnoredMark(int gi, int flags) {
if ((flags & GlyphSubtable.LF_IGNORE_MARK) != 0) {
return isMark(gi);
} else if ((flags & GlyphSubtable.LF_MARK_ATTACHMENT_TYPE) != 0) {
int lac = (flags & GlyphSubtable.LF_MARK_ATTACHMENT_TYPE) >> 8;
int gac = gdef.getMarkAttachClass(gi);
return (gac != lac);
} else {
return false;
}
}
/**
* Obtain an ignored glyph tester that corresponds to the specified lookup flags.
* @param flags lookup flags
* @return a glyph tester
*/
public GlyphTester getIgnoreTester(int flags) {
if ((flags & GlyphSubtable.LF_IGNORE_BASE) != 0) {
if ((flags & (GlyphSubtable.LF_IGNORE_LIGATURE | GlyphSubtable.LF_IGNORE_MARK)) == 0) {
return ignoreBase;
} else {
return getCombinedIgnoreTester(flags);
}
}
if ((flags & GlyphSubtable.LF_IGNORE_LIGATURE) != 0) {
if ((flags & (GlyphSubtable.LF_IGNORE_BASE | GlyphSubtable.LF_IGNORE_MARK)) == 0) {
return ignoreLigature;
} else {
return getCombinedIgnoreTester(flags);
}
}
if ((flags & GlyphSubtable.LF_IGNORE_MARK) != 0) {
if ((flags & (GlyphSubtable.LF_IGNORE_BASE | GlyphSubtable.LF_IGNORE_LIGATURE)) == 0) {
return ignoreMark;
} else {
return getCombinedIgnoreTester(flags);
}
}
return null;
}
/**
* Obtain an ignored glyph tester that corresponds to the specified multiple (combined) lookup flags.
* @param flags lookup flags
* @return a glyph tester
*/
public GlyphTester getCombinedIgnoreTester(int flags) {
GlyphTester[] gta = new GlyphTester [ 3 ];
int ngt = 0;
if ((flags & GlyphSubtable.LF_IGNORE_BASE) != 0) {
gta [ ngt++ ] = ignoreBase;
}
if ((flags & GlyphSubtable.LF_IGNORE_LIGATURE) != 0) {
gta [ ngt++ ] = ignoreLigature;
}
if ((flags & GlyphSubtable.LF_IGNORE_MARK) != 0) {
gta [ ngt++ ] = ignoreMark;
}
return getCombinedOrTester(gta, ngt);
}
/**
* Obtain an combined OR glyph tester.
* @param gta an array of glyph testers
* @param ngt number of glyph testers present in specified array
* @return a combined OR glyph tester
*/
public GlyphTester getCombinedOrTester(GlyphTester[] gta, int ngt) {
if (ngt > 0) {
return new CombinedOrGlyphTester(gta, ngt);
} else {
return null;
}
}
/**
* Obtain an combined AND glyph tester.
* @param gta an array of glyph testers
* @param ngt number of glyph testers present in specified array
* @return a combined AND glyph tester
*/
public GlyphTester getCombinedAndTester(GlyphTester[] gta, int ngt) {
if (ngt > 0) {
return new CombinedAndGlyphTester(gta, ngt);
} else {
return null;
}
}
/** combined OR glyph tester */
private static class CombinedOrGlyphTester implements GlyphTester {
private GlyphTester[] gta;
private int ngt;
CombinedOrGlyphTester(GlyphTester[] gta, int ngt) {
this.gta = gta;
this.ngt = ngt;
}
/** {@inheritDoc} */
public boolean test(int gi, int flags) {
for (int i = 0, n = ngt; i < n; i++) {
GlyphTester gt = gta [ i ];
if (gt != null) {
if (gt.test(gi, flags)) {
return true;
}
}
}
return false;
}
}
/** combined AND glyph tester */
private static class CombinedAndGlyphTester implements GlyphTester {
private GlyphTester[] gta;
private int ngt;
CombinedAndGlyphTester(GlyphTester[] gta, int ngt) {
this.gta = gta;
this.ngt = ngt;
}
/** {@inheritDoc} */
public boolean test(int gi, int flags) {
for (int i = 0, n = ngt; i < n; i++) {
GlyphTester gt = gta [ i ];
if (gt != null) {
if (!gt.test(gi, flags)) {
return false;
}
}
}
return true;
}
}
/** NOT glyph tester */
private static class NotGlyphTester implements GlyphTester {
private GlyphTester gt;
NotGlyphTester(GlyphTester gt) {
this.gt = gt;
}
/** {@inheritDoc} */
public boolean test(int gi, int flags) {
if (gt != null) {
if (gt.test(gi, flags)) {
return false;
}
}
return true;
}
}
}
|