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
|
/*
* 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.scripts;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.complexscripts.bidi.BidiClass;
import org.apache.fop.complexscripts.bidi.BidiConstants;
import org.apache.fop.complexscripts.fonts.GlyphDefinitionTable;
import org.apache.fop.complexscripts.util.GlyphContextTester;
import org.apache.fop.complexscripts.util.GlyphSequence;
import org.apache.fop.complexscripts.util.ScriptContextTester;
// CSOFF: AvoidNestedBlocksCheck
// CSOFF: NoWhitespaceAfterCheck
// CSOFF: InnerAssignmentCheck
// CSOFF: SimplifyBooleanReturnCheck
// CSOFF: LineLengthCheck
/**
* <p>The <code>ArabicScriptProcessor</code> class implements a script processor for
* performing glyph substitution and positioning operations on content associated with the Arabic script.</p>
*
* <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
*/
public class ArabicScriptProcessor extends DefaultScriptProcessor {
/** logging instance */
private static final Log log = LogFactory.getLog(ArabicScriptProcessor.class); // CSOK: ConstantNameCheck
/** features to use for substitutions */
private static final String[] gsubFeatures = // CSOK: ConstantNameCheck
{
"calt", // contextual alternates
"ccmp", // glyph composition/decomposition
"fina", // final (terminal) forms
"init", // initial forms
"isol", // isolated formas
"liga", // standard ligatures
"medi", // medial forms
"rlig" // required ligatures
};
/** features to use for positioning */
private static final String[] gposFeatures = // CSOK: ConstantNameCheck
{
"curs", // cursive positioning
"kern", // kerning
"mark", // mark to base or ligature positioning
"mkmk" // mark to mark positioning
};
private static class SubstitutionScriptContextTester implements ScriptContextTester {
private static Map/*<String,GlyphContextTester>*/ testerMap = new HashMap/*<String,GlyphContextTester>*/();
static {
testerMap.put ( "fina", new GlyphContextTester() {
public boolean test ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
return inFinalContext ( script, language, feature, gs, index, flags );
}
} );
testerMap.put ( "init", new GlyphContextTester() {
public boolean test ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
return inInitialContext ( script, language, feature, gs, index, flags );
}
} );
testerMap.put ( "isol", new GlyphContextTester() {
public boolean test ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
return inIsolateContext ( script, language, feature, gs, index, flags );
}
} );
testerMap.put ( "liga", new GlyphContextTester() {
public boolean test ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
return inLigatureContext ( script, language, feature, gs, index, flags );
}
} );
testerMap.put ( "medi", new GlyphContextTester() {
public boolean test ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
return inMedialContext ( script, language, feature, gs, index, flags );
}
} );
}
public GlyphContextTester getTester ( String feature ) {
return (GlyphContextTester) testerMap.get ( feature );
}
}
private static class PositioningScriptContextTester implements ScriptContextTester {
private static Map/*<String,GlyphContextTester>*/ testerMap = new HashMap/*<String,GlyphContextTester>*/();
public GlyphContextTester getTester ( String feature ) {
return (GlyphContextTester) testerMap.get ( feature );
}
}
private final ScriptContextTester subContextTester;
private final ScriptContextTester posContextTester;
ArabicScriptProcessor ( String script ) {
super ( script );
this.subContextTester = new SubstitutionScriptContextTester();
this.posContextTester = new PositioningScriptContextTester();
}
/** {@inheritDoc} */
public String[] getSubstitutionFeatures() {
return gsubFeatures;
}
/** {@inheritDoc} */
public ScriptContextTester getSubstitutionContextTester() {
return subContextTester;
}
/** {@inheritDoc} */
public String[] getPositioningFeatures() {
return gposFeatures;
}
/** {@inheritDoc} */
public ScriptContextTester getPositioningContextTester() {
return posContextTester;
}
/** {@inheritDoc} */
@Override
public GlyphSequence reorderCombiningMarks ( GlyphDefinitionTable gdef, GlyphSequence gs, int[][] gpa, String script, String language ) {
// a side effect of BIDI reordering is to order combining marks before their base, so we need to override the default here to
// prevent double reordering
return gs;
}
private static boolean inFinalContext ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
GlyphSequence.CharAssociation a = gs.getAssociation ( index );
int[] ca = gs.getCharacterArray ( false );
int nc = gs.getCharacterCount();
if ( nc == 0 ) {
return false;
} else {
int s = a.getStart();
int e = a.getEnd();
if ( ! hasFinalPrecedingContext ( ca, nc, s, e ) ) {
return false;
} else if ( forcesFinalThisContext ( ca, nc, s, e ) ) {
return true;
} else if ( ! hasFinalFollowingContext ( ca, nc, s, e ) ) {
return false;
} else {
return true;
}
}
}
private static boolean inInitialContext ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
GlyphSequence.CharAssociation a = gs.getAssociation ( index );
int[] ca = gs.getCharacterArray ( false );
int nc = gs.getCharacterCount();
if ( nc == 0 ) {
return false;
} else {
int s = a.getStart();
int e = a.getEnd();
if ( ! hasInitialPrecedingContext ( ca, nc, s, e ) ) {
return false;
} else if ( ! hasInitialFollowingContext ( ca, nc, s, e ) ) {
return false;
} else {
return true;
}
}
}
private static boolean inIsolateContext ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
GlyphSequence.CharAssociation a = gs.getAssociation ( index );
int nc = gs.getCharacterCount();
if ( nc == 0 ) {
return false;
} else if ( ( a.getStart() == 0 ) && ( a.getEnd() == nc ) ) {
return true;
} else {
return false;
}
}
private static boolean inLigatureContext ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
GlyphSequence.CharAssociation a = gs.getAssociation ( index );
int[] ca = gs.getCharacterArray ( false );
int nc = gs.getCharacterCount();
if ( nc == 0 ) {
return false;
} else {
int s = a.getStart();
int e = a.getEnd();
if ( ! hasLigaturePrecedingContext ( ca, nc, s, e ) ) {
return false;
} else if ( ! hasLigatureFollowingContext ( ca, nc, s, e ) ) {
return false;
} else {
return true;
}
}
}
private static boolean inMedialContext ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
GlyphSequence.CharAssociation a = gs.getAssociation ( index );
int[] ca = gs.getCharacterArray ( false );
int nc = gs.getCharacterCount();
if ( nc == 0 ) {
return false;
} else {
int s = a.getStart();
int e = a.getEnd();
if ( ! hasMedialPrecedingContext ( ca, nc, s, e ) ) {
return false;
} else if ( ! hasMedialThisContext ( ca, nc, s, e ) ) {
return false;
} else if ( ! hasMedialFollowingContext ( ca, nc, s, e ) ) {
return false;
} else {
return true;
}
}
}
private static boolean hasFinalPrecedingContext ( int[] ca, int nc, int s, int e ) {
int chp = 0;
int clp = 0;
for ( int i = s; i > 0; i-- ) {
int k = i - 1;
if ( ( k >= 0 ) && ( k < nc ) ) {
chp = ca [ k ];
clp = BidiClass.getBidiClass ( chp );
if ( clp != BidiConstants.NSM ) {
break;
}
}
}
if ( clp != BidiConstants.AL ) {
return false;
} else if ( hasIsolateInitial ( chp ) ) {
return false;
} else {
return true;
}
}
private static boolean forcesFinalThisContext ( int[] ca, int nc, int s, int e ) {
int chl = 0;
int cll = 0;
for ( int i = 0, n = e - s; i < n; i++ ) {
int k = n - i - 1;
int j = s + k;
if ( ( j >= 0 ) && ( j < nc ) ) {
chl = ca [ j ];
cll = BidiClass.getBidiClass ( chl );
if ( cll != BidiConstants.NSM ) {
break;
}
}
}
if ( cll != BidiConstants.AL ) {
return false;
}
if ( hasIsolateInitial ( chl ) ) {
return true;
} else {
return false;
}
}
private static boolean hasFinalFollowingContext ( int[] ca, int nc, int s, int e ) {
int chf = 0;
int clf = 0;
for ( int i = e, n = nc; i < n; i++ ) {
chf = ca [ i ];
clf = BidiClass.getBidiClass ( chf );
if ( clf != BidiConstants.NSM ) {
break;
}
}
if ( clf != BidiConstants.AL ) {
return true;
} else if ( hasIsolateFinal ( chf ) ) {
return true;
} else {
return false;
}
}
private static boolean hasInitialPrecedingContext ( int[] ca, int nc, int s, int e ) {
int chp = 0;
int clp = 0;
for ( int i = s; i > 0; i-- ) {
int k = i - 1;
if ( ( k >= 0 ) && ( k < nc ) ) {
chp = ca [ k ];
clp = BidiClass.getBidiClass ( chp );
if ( clp != BidiConstants.NSM ) {
break;
}
}
}
if ( clp != BidiConstants.AL ) {
return true;
} else if ( hasIsolateInitial ( chp ) ) {
return true;
} else {
return false;
}
}
private static boolean hasInitialFollowingContext ( int[] ca, int nc, int s, int e ) {
int chf = 0;
int clf = 0;
for ( int i = e, n = nc; i < n; i++ ) {
chf = ca [ i ];
clf = BidiClass.getBidiClass ( chf );
if ( clf != BidiConstants.NSM ) {
break;
}
}
if ( clf != BidiConstants.AL ) {
return false;
} else if ( hasIsolateFinal ( chf ) ) {
return false;
} else {
return true;
}
}
private static boolean hasMedialPrecedingContext ( int[] ca, int nc, int s, int e ) {
int chp = 0;
int clp = 0;
for ( int i = s; i > 0; i-- ) {
int k = i - 1;
if ( ( k >= 0 ) && ( k < nc ) ) {
chp = ca [ k ];
clp = BidiClass.getBidiClass ( chp );
if ( clp != BidiConstants.NSM ) {
break;
}
}
}
if ( clp != BidiConstants.AL ) {
return false;
} else if ( hasIsolateInitial ( chp ) ) {
return false;
} else {
return true;
}
}
private static boolean hasMedialThisContext ( int[] ca, int nc, int s, int e ) {
int chf = 0; // first non-NSM char in [s,e)
int clf = 0;
for ( int i = 0, n = e - s; i < n; i++ ) {
int k = s + i;
if ( ( k >= 0 ) && ( k < nc ) ) {
chf = ca [ s + i ];
clf = BidiClass.getBidiClass ( chf );
if ( clf != BidiConstants.NSM ) {
break;
}
}
}
if ( clf != BidiConstants.AL ) {
return false;
}
int chl = 0; // last non-NSM char in [s,e)
int cll = 0;
for ( int i = 0, n = e - s; i < n; i++ ) {
int k = n - i - 1;
int j = s + k;
if ( ( j >= 0 ) && ( j < nc ) ) {
chl = ca [ j ];
cll = BidiClass.getBidiClass ( chl );
if ( cll != BidiConstants.NSM ) {
break;
}
}
}
if ( cll != BidiConstants.AL ) {
return false;
}
if ( hasIsolateFinal ( chf ) ) {
return false;
} else if ( hasIsolateInitial ( chl ) ) {
return false;
} else {
return true;
}
}
private static boolean hasMedialFollowingContext ( int[] ca, int nc, int s, int e ) {
int chf = 0;
int clf = 0;
for ( int i = e, n = nc; i < n; i++ ) {
chf = ca [ i ];
clf = BidiClass.getBidiClass ( chf );
if ( clf != BidiConstants.NSM ) {
break;
}
}
if ( clf != BidiConstants.AL ) {
return false;
} else if ( hasIsolateFinal ( chf ) ) {
return false;
} else {
return true;
}
}
private static boolean hasLigaturePrecedingContext ( int[] ca, int nc, int s, int e ) {
return true;
}
private static boolean hasLigatureFollowingContext ( int[] ca, int nc, int s, int e ) {
int chf = 0;
int clf = 0;
for ( int i = e, n = nc; i < n; i++ ) {
chf = ca [ i ];
clf = BidiClass.getBidiClass ( chf );
if ( clf != BidiConstants.NSM ) {
break;
}
}
if ( clf == BidiConstants.AL ) {
return true;
} else {
return false;
}
}
/**
* Ordered array of Unicode scalars designating those Arabic (Script) Letters
* which exhibit an isolated form in word initial position.
*/
private static int[] isolatedInitials = {
0x0621, // HAMZA
0x0622, // ALEF WITH MADDA ABOVE
0x0623, // ALEF WITH HAMZA ABOVE
0x0624, // WAW WITH HAMZA ABOVE
0x0625, // ALEF WITH HAMZA BELOWW
0x0627, // ALEF
0x062F, // DAL
0x0630, // THAL
0x0631, // REH
0x0632, // ZAIN
0x0648, // WAW
0x0671, // ALEF WASLA
0x0672, // ALEF WITH WAVY HAMZA ABOVE
0x0673, // ALEF WITH WAVY HAMZA BELOW
0x0675, // HIGH HAMZA ALEF
0x0676, // HIGH HAMZA WAW
0x0677, // U WITH HAMZA ABOVE
0x0688, // DDAL
0x0689, // DAL WITH RING
0x068A, // DAL WITH DOT BELOW
0x068B, // DAL WITH DOT BELOW AND SMALL TAH
0x068C, // DAHAL
0x068D, // DDAHAL
0x068E, // DUL
0x068F, // DUL WITH THREE DOTS ABOVE DOWNWARDS
0x0690, // DUL WITH FOUR DOTS ABOVE
0x0691, // RREH
0x0692, // REH WITH SMALL V
0x0693, // REH WITH RING
0x0694, // REH WITH DOT BELOW
0x0695, // REH WITH SMALL V BELOW
0x0696, // REH WITH DOT BELOW AND DOT ABOVE
0x0697, // REH WITH TWO DOTS ABOVE
0x0698, // JEH
0x0699, // REH WITH FOUR DOTS ABOVE
0x06C4, // WAW WITH RING
0x06C5, // KIRGHIZ OE
0x06C6, // OE
0x06C7, // U
0x06C8, // YU
0x06C9, // KIRGHIZ YU
0x06CA, // WAW WITH TWO DOTS ABOVE
0x06CB, // VE
0x06CF, // WAW WITH DOT ABOVE
0x06EE, // DAL WITH INVERTED V
0x06EF // REH WITH INVERTED V
};
private static boolean hasIsolateInitial ( int ch ) {
return Arrays.binarySearch ( isolatedInitials, ch ) >= 0;
}
/**
* Ordered array of Unicode scalars designating those Arabic (Script) Letters
* which exhibit an isolated form in word final position.
*/
private static int[] isolatedFinals = {
0x0621 // HAMZA
};
private static boolean hasIsolateFinal ( int ch ) {
return Arrays.binarySearch ( isolatedFinals, ch ) >= 0;
}
}
|