aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/complexscripts/fonts/GPOSTestCase.java
blob: 28e364c433bc70fe19b9ac7197cb177bbc0dff35 (plain)
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
/*
 * 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.io.File;
import java.util.List;
import java.util.Map;

import org.apache.fop.complexscripts.fonts.GlyphSubtable;
import org.apache.fop.complexscripts.fonts.GlyphPositioningSubtable;
import org.apache.fop.complexscripts.fonts.GlyphPositioningTable;
import org.apache.fop.complexscripts.fonts.GlyphTable.LookupSpec;
import org.apache.fop.complexscripts.fonts.GlyphTable.LookupTable;
import org.apache.fop.complexscripts.fonts.ttx.TTXFile;
import org.apache.fop.complexscripts.util.GlyphContextTester;
import org.apache.fop.complexscripts.util.GlyphSequence;
import org.apache.fop.complexscripts.util.ScriptContextTester;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class GPOSTestCase implements ScriptContextTester, GlyphContextTester {

    private static String ttxFilesRoot = "test/resources/complexscripts";

    private static String[][] ttxFonts = {
        { "f0", "arab/ttx/arab-001.ttx" },              // simplified arabic
        { "f1", "arab/ttx/arab-002.ttx" },              // traditional arabic
        { "f2", "arab/ttx/arab-003.ttx" },              // lateef
        { "f3", "arab/ttx/arab-004.ttx" },              // scheherazade
    };

    private static Object[][] ltSingle = {
        { GlyphPositioningTable.GPOS_LOOKUP_TYPE_SINGLE },
        // arab-001.ttx
        { "f0", "lu1", "arab", "dflt", "mark",
          new Object[][] {
                {
                    new String[] { "fathatan" },
                    new int[][] {
                        { 0, 0, -412, 0 }
                    }
                },
                {
                    new String[] { "fatha" },
                    new int[][] {
                        { 0, 0, -410, 0 }
                    }
                },
          },
        },
        { "f0", "lu9", "arab", "*", "*",
          new Object[][] {
                {
                    new String[] { "fathatan" },
                    new int[][] {
                        { 50, 0, 0, 0 }
                    }
                },
                {
                    new String[] { "fatha" },
                    new int[][] {
                        { 50, 0, 0, 0 }
                    }
                },
          },
        },
        { "f0", "lu10", "arab", "*", "*",
          new Object[][] {
                {
                    new String[] { "kasratan" },
                    new int[][] {
                        { 0, -200, 0, 0 }
                    }
                },
                {
                    new String[] { "kasra" },
                    new int[][] {
                        { 0, -200, 0, 0 }
                    }
                },
          },
        },
        { "f0", "lu11", "arab", "*", "*",
          new Object[][] {
                {
                    new String[] { "kasratan" },
                    new int[][] {
                        { 0, -300, 0, 0 }
                    }
                },
                {
                    new String[] { "kasra" },
                    new int[][] {
                        { 0, -300, 0, 0 }
                    }
                },
                {
                    new String[] { "uni0655" },
                    new int[][] {
                        { 0, -250, 0, 0 }
                    }
                },
          },
        },
        // arab-002.ttx - maybe add tests
        // arab-003.ttx - maybe add tests
        // arab-004.ttx - maybe add tests
    };

    private static Object[][] ltPair = {
        { GlyphPositioningTable.GPOS_LOOKUP_TYPE_PAIR },
        // arab-001.ttx
        { "f0", "lu0", "arab", "dflt", "kern",
          new Object[][] {
                {
                    new String[] { "wawwithhamzaabove", "hamza" },
                    new int[][] {
                        { -300, 0, -300, 0 }, { 0, 0, 0, 0 }
                    }
                },
                {
                    new String[] { "reh", "alefwithmaddaabove" },
                    new int[][] {
                        { -500, 0, -500, 0 }, { 0, 0, 0, 0 }
                    }
                },
                {
                    new String[] { "zain", "zain" },
                    new int[][] {
                        { -190, 0, -190, 0 }, { 0, 0, 0, 0 }
                    }
                },
                {
                    new String[] { "waw", "uni0649.init" },
                    new int[][] {
                        { -145, 0, -145, 0 }, { 0, 0, 0, 0 }
                    }
                },
                {
                    new String[] { "jeh", "uni06A5.init" },
                    new int[][] {
                        { -345, 0, -345, 0 }, { 0, 0, 0, 0 }
                    }
                },
          },
        },
        // arab-002.ttx - maybe add tests
        // arab-003.ttx - maybe add tests
        // arab-004.ttx - maybe add tests
    };

    private static Object[][] ltCursive = {
        { GlyphPositioningTable.GPOS_LOOKUP_TYPE_CURSIVE },
        // arab-001.ttx - none used
        // arab-002.ttx - none used
        // arab-003.ttx - maybe add tests
        { "f2", "lu0", "arab", "dflt", "curs",
          new Object[][] {
                {
                    new String[] { "uni0644.init.preAlef", "uni0622.fina.postLamIni" },
                    new int[][] {
                        // { 576, 0, 0, 0 }, { 0, 0, 0, 0 } - with zero widths
                        { 295, 0, 0, 0 }, { 0, 0, 0, 0 }
                    }
                },
                {
                    new String[] { "uni0644.medi.preAlef", "uni0622.fina.postLamMed" },
                    new int[][] {
                        // { 550, 0, 0, 0 }, { 0, 0, 0, 0 } - with zero widths
                        { 282, 0, 0, 0 }, { 0, 0, 0, 0 }
                    }
                },
          },
        },
        // arab-004.ttx - none used
    };

    private static Object[][] ltMarkToBase = {
        { GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_BASE },
        // arab-001.ttx - maybe add tests
        // arab-002.ttx
        { "f1", "lu4", "arab", "dflt", "mark",
          new Object[][] {
                {
                    new String[] { "beh", "fatha" },
                    new int[][] {
                        // { 0, 0, 0, 0 }, { 266, -672, 0, 0 } - with zero widths
                        { 0, 0, 0, 0 }, { 266, -672, -199, 0 }
                    }
                },
                {
                    new String[] { "alefwithhamzabelow", "kasra" },
                    new int[][] {
                        // { 0, 0, 0, 0 }, { -48, 344, 0, 0 } - with zero widths
                        { 0, 0, 0, 0 }, { -48, 344, -199, 0 }
                    }
                },
          },
        },
        // arab-003.ttx - maybe add tests
        // arab-004.ttx - maybe add tests
    };

    private static Object[][] ltMarkToLigature = {
        { GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE },
        // arab-001.ttx
        { "f0", "lu4", "arab", "dflt", "mark",
          new Object[][] {
                {
                    new String[] { "rayaleflam", "fatha", "fatha", "fatha", "fatha" },
                    new int[][] {
                        { 0, 0, 0, 0 }, { 1260, -1150, 0, 0 }, { 910, -1020, 0, 0 }, { 590, -630, 0, 0 }, { 110, -720, 0, 0 }
                    }
                },
                {
                    new String[] { "rayaleflam", "kasra", "kasra", "kasra", "kasra" },
                    new int[][] {
                        { 0, 0, 0, 0 }, { 1110 , 225, 0, 0 }, { 760, 275, 0, 0 }, { 520, 475, 0, 0 }, { 110, 425, 0, 0 }
                    }
                },
          },
        },
        // arab-002.ttx - maybe add tests
        // arab-003.ttx - maybe add tests
        // arab-004.ttx - maybe add tests
    };

    private static Object[][] ltMarkToMark = {
        { GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_MARK },
        // arab-001.ttx - maybe add tests
        // arab-002.ttx - maybe add tests
        // arab-003.ttx - maybe add tests
        // arab-004.ttx
        { "f3", "lu3", "arab", "dflt", "mkmk",
          new Object[][] {
                {
                    new String[] { "uni064F", "uni064E" },
                    new int[][] {
                        { 0, 0, 0, 0 }, { -15, 495, 0, 0 }
                    }
                },
                {
                    new String[] { "uni0651", "uni0670" },
                    new int[][] {
                        { 0, 0, 0, 0 }, { -30, 705, 0, 0 }
                    }
                },
          },
        },
    };

    private static Object[][] ltContextual = {
        { GlyphPositioningTable.GPOS_LOOKUP_TYPE_CONTEXTUAL },
        // arab-001.ttx - none used
        // arab-002.ttx - none used
        // arab-003.ttx - none used
        // arab-004.ttx - none used
    };

    private static Object[][] ltChainedContextual = {
        { GlyphPositioningTable.GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL },
        // arab-001.ttx
        { "f0", "lu3", "arab", "dflt", "mark",
          new Object[][] {
                {
                    new String[] { "behmedial", "fatha", "lam" },
                    new int[][] {
                        { 0, 0, 0, 0 }, { 50, 0, 0, 0 }, { 0, 0, 0, 0 }
                    }
                },
          },
        },
        // arab-002.ttx
        { "f1", "lu6", "arab", "dflt", "mark",
          new Object[][] {
                {
                    new String[] { "zain", "fatha", "kafinitial" },
                    new int[][] {
                        { 0, 0, 0, 0 }, { 0, 250, 0, 0 }, { 0, 0, 0, 0 }
                    }
                },
          },
        },
        // arab-003.ttx - none used
        // arab-004.ttx
        { "f3", "lu5", "arab", "dflt", "mark",
          new Object[][] {
                {
                    new String[] { "uni064D", "uni0622.fina.postLamIni", "uni0650" },
                    new int[][] {
                        { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 55, 424, 0, 0 }
                    }
                },
          },
        },
    };

    @Test
    public void testGPOSSingle() throws Exception {
        performPositioning ( ltSingle );
    }

    @Test
    public void testGPOSPair() throws Exception {
        performPositioning ( ltPair );
    }

    @Test
    public void testGPOSCursive() throws Exception {
        performPositioning ( ltCursive );
    }

    @Test
    public void testGPOSMarkToBase() throws Exception {
        performPositioning ( ltMarkToBase );
    }

    @Test
    public void testGPOSMarkToLigature() throws Exception {
        performPositioning ( ltMarkToLigature );
    }

    @Test
    public void testGPOSMarkToMark() throws Exception {
        performPositioning ( ltMarkToMark );
    }

    @Test
    public void testGPOSContextual() throws Exception {
        performPositioning ( ltContextual );
    }

    @Test
    public void testGPOSChainedContextual() throws Exception {
        performPositioning ( ltChainedContextual );
    }

    /**
     * Perform positioning on all test data in test specification TS.
     * @param ts test specification
     */
    private void performPositioning ( Object[][] ts ) {
        assert ts.length > 0;
        Object[] tp = ts[0];
        for ( int i = 1; i < ts.length; i++ ) {
            performPositioning ( tp, ts[i] );
        }
    }

    /**
     * Perform positioning on all test data TD using test parameters TP.
     * @param tp test parameters
     * @param td test data
     */
    private void performPositioning ( Object[] tp, Object[] td ) {
        assert tp.length > 0;
        if ( td.length > 5 ) {
            String fid = (String) td[0];
            String lid = (String) td[1];
            String script = (String) td[2];
            String language = (String) td[3];
            String feature = (String) td[4];
            TTXFile tf = findTTX ( fid );
            assertTrue ( tf != null );
            GlyphPositioningTable gpos = tf.getGPOS();
            assertTrue ( gpos != null );
            GlyphPositioningSubtable[] sta = findGPOSSubtables ( gpos, script, language, feature, lid );
            assertTrue ( sta != null );
            assertTrue ( sta.length > 0 );
            ScriptContextTester sct = findScriptContextTester ( script, language, feature );
            Object[][] tia = (Object[][]) td[5];                // test instance array
            for ( Object[] ti : tia ) {                         // test instance
                if ( ti != null ) {
                    if ( ti.length > 0 ) {                      // must have at least input glyphs
                        String[] igia = (String[]) ti[0];       // input glyph id array
                        int[][] ogpa = (int[][]) ti[1];         // output glyph positioning array
                        GlyphSequence igs = tf.getGlyphSequence ( igia );
                        int[] widths = tf.getWidths();
                        int[][] tgpa = new int [ igia.length ] [ 4 ];
                        boolean adjusted = GlyphPositioningSubtable.position ( igs, script, language, feature, 1000, sta, widths, tgpa, sct );
                        assertTrue ( adjusted );
                        assertSamePositions ( ogpa, tgpa );
                    }
                }
            }
        }
    }

    private String findTTXPath ( String fid ) {
        for ( String[] fs : ttxFonts ) {
            if ( ( fs != null ) && ( fs.length > 1 ) ) {
                if ( fs[0].equals ( fid ) ) {
                    return ttxFilesRoot + File.separator + fs[1];
                }
            }
        }
        return null;
    }

    private TTXFile findTTX ( String fid ) {
        String pn = findTTXPath ( fid );
        assertTrue ( pn != null );
        try { 
            TTXFile tf = TTXFile.getFromCache ( pn );
            return tf;
        } catch ( Exception e ) {
            fail ( e.getMessage() );
            return null;
        }
    }

    private GlyphPositioningSubtable[] findGPOSSubtables ( GlyphPositioningTable gpos, String script, String language, String feature, String lid ) {
        LookupTable lt = gpos.getLookupTable ( lid );
        if ( lt != null ) {
            return (GlyphPositioningSubtable[]) lt.getSubtables();
        } else {
            return null;
        }
    }

    private ScriptContextTester findScriptContextTester ( String script, String language, String feature ) {
        return this;
    }

    @Override
    public GlyphContextTester getTester ( String feature ) {
        return this;
    }

    @Override
    public boolean test ( String script, String language, String feature, GlyphSequence gs, int index, int flags ) {
        return true;
    }

    private void assertSamePositions ( int[][] pa1, int[][] pa2 ) {
        assertNotNull ( pa1 );
        assertNotNull ( pa2 );
        assertEquals ( "unequal adjustment count", pa1.length, pa2.length );
        for ( int i = 0; i < pa1.length; i++ ) {
            int[] a1 = pa1 [ i ];
            int[] a2 = pa2 [ i ];
            assertNotNull ( a1 );
            assertNotNull ( a2 );
            assertEquals ( "bad adjustment array length", 4, a1.length );
            assertEquals ( "bad adjustment array length", 4, a2.length );
            for ( int k = 0; k < a1.length; k++ ) {
                int p1 = a1[k];
                int p2 = a2[k];
                assertEquals ( "bad adjustment", p1, p2 );
            }
        }
    }
}