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
|
/*
* 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.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.complexscripts.scripts.ScriptProcessor;
import org.apache.fop.complexscripts.util.GlyphSequence;
// CSOFF: InnerAssignmentCheck
// CSOFF: LineLengthCheck
/**
* <p>The <code>GlyphDefinitionTable</code> class is a glyph table that implements
* glyph definition functionality according to the OpenType GDEF table.</p>
*
* <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
*/
public class GlyphDefinitionTable extends GlyphTable {
/** logging instance */
private static final Log log = LogFactory.getLog(GlyphDefinitionTable.class); // CSOK: ConstantNameCheck
/** glyph class subtable type */
public static final int GDEF_LOOKUP_TYPE_GLYPH_CLASS = 1;
/** attachment point subtable type */
public static final int GDEF_LOOKUP_TYPE_ATTACHMENT_POINT = 2;
/** ligature caret subtable type */
public static final int GDEF_LOOKUP_TYPE_LIGATURE_CARET = 3;
/** mark attachment subtable type */
public static final int GDEF_LOOKUP_TYPE_MARK_ATTACHMENT = 4;
/** pre-defined glyph class - base glyph */
public static final int GLYPH_CLASS_BASE = 1;
/** pre-defined glyph class - ligature glyph */
public static final int GLYPH_CLASS_LIGATURE = 2;
/** pre-defined glyph class - mark glyph */
public static final int GLYPH_CLASS_MARK = 3;
/** pre-defined glyph class - component glyph */
public static final int GLYPH_CLASS_COMPONENT = 4;
/** singleton glyph class table */
private GlyphClassSubtable gct;
/** singleton attachment point table */
// private AttachmentPointSubtable apt; // NOT YET USED
/** singleton ligature caret table */
// private LigatureCaretSubtable lct; // NOT YET USED
/** singleton mark attachment table */
private MarkAttachmentSubtable mat;
/**
* Instantiate a <code>GlyphDefinitionTable</code> object using the specified subtables.
* @param subtables a list of identified subtables
*/
public GlyphDefinitionTable ( List subtables ) {
super ( null, new HashMap(0) );
if ( ( subtables == null ) || ( subtables.size() == 0 ) ) {
throw new AdvancedTypographicTableFormatException ( "subtables must be non-empty" );
} else {
for ( Iterator it = subtables.iterator(); it.hasNext();) {
Object o = it.next();
if ( o instanceof GlyphDefinitionSubtable ) {
addSubtable ( (GlyphSubtable) o );
} else {
throw new AdvancedTypographicTableFormatException ( "subtable must be a glyph definition subtable" );
}
}
freezeSubtables();
}
}
/**
* Reorder combining marks in glyph sequence so that they precede (within the sequence) the base
* character to which they are applied. N.B. In the case of LTR segments, marks are not reordered by this,
* method since when the segment is reversed by BIDI processing, marks are automatically reordered to precede
* their base glyph.
* @param gs an input glyph sequence
* @param gpa associated glyph position adjustments (also reordered)
* @param script a script identifier
* @param language a language identifier
* @return the reordered (output) glyph sequence
*/
public GlyphSequence reorderCombiningMarks ( GlyphSequence gs, int[][] gpa, String script, String language ) {
ScriptProcessor sp = ScriptProcessor.getInstance ( script );
return sp.reorderCombiningMarks ( this, gs, gpa, script, language );
}
/** {@inheritDoc} */
protected void addSubtable ( GlyphSubtable subtable ) {
if ( subtable instanceof GlyphClassSubtable ) {
this.gct = (GlyphClassSubtable) subtable;
} else if ( subtable instanceof AttachmentPointSubtable ) {
// TODO - not yet used
// this.apt = (AttachmentPointSubtable) subtable;
} else if ( subtable instanceof LigatureCaretSubtable ) {
// TODO - not yet used
// this.lct = (LigatureCaretSubtable) subtable;
} else if ( subtable instanceof MarkAttachmentSubtable ) {
this.mat = (MarkAttachmentSubtable) subtable;
} else {
throw new UnsupportedOperationException ( "unsupported glyph definition subtable type: " + subtable );
}
}
/**
* Determine if glyph belongs to pre-defined glyph class.
* @param gid a glyph identifier (index)
* @param gc a pre-defined glyph class (GLYPH_CLASS_BASE|GLYPH_CLASS_LIGATURE|GLYPH_CLASS_MARK|GLYPH_CLASS_COMPONENT).
* @return true if glyph belongs to specified glyph class
*/
public boolean isGlyphClass ( int gid, int gc ) {
if ( gct != null ) {
return gct.isGlyphClass ( gid, gc );
} else {
return false;
}
}
/**
* Determine glyph class.
* @param gid a glyph identifier (index)
* @return a pre-defined glyph class (GLYPH_CLASS_BASE|GLYPH_CLASS_LIGATURE|GLYPH_CLASS_MARK|GLYPH_CLASS_COMPONENT).
*/
public int getGlyphClass ( int gid ) {
if ( gct != null ) {
return gct.getGlyphClass ( gid );
} else {
return -1;
}
}
/**
* Determine if glyph belongs to (font specific) mark attachment class.
* @param gid a glyph identifier (index)
* @param mac a (font specific) mark attachment class
* @return true if glyph belongs to specified mark attachment class
*/
public boolean isMarkAttachClass ( int gid, int mac ) {
if ( mat != null ) {
return mat.isMarkAttachClass ( gid, mac );
} else {
return false;
}
}
/**
* Determine mark attachment class.
* @param gid a glyph identifier (index)
* @return a non-negative mark attachment class, or -1 if no class defined
*/
public int getMarkAttachClass ( int gid ) {
if ( mat != null ) {
return mat.getMarkAttachClass ( gid );
} else {
return -1;
}
}
/**
* Map a lookup type name to its constant (integer) value.
* @param name lookup type name
* @return lookup type
*/
public static int getLookupTypeFromName ( String name ) {
int t;
String s = name.toLowerCase();
if ( "glyphclass".equals ( s ) ) {
t = GDEF_LOOKUP_TYPE_GLYPH_CLASS;
} else if ( "attachmentpoint".equals ( s ) ) {
t = GDEF_LOOKUP_TYPE_ATTACHMENT_POINT;
} else if ( "ligaturecaret".equals ( s ) ) {
t = GDEF_LOOKUP_TYPE_LIGATURE_CARET;
} else if ( "markattachment".equals ( s ) ) {
t = GDEF_LOOKUP_TYPE_MARK_ATTACHMENT;
} else {
t = -1;
}
return t;
}
/**
* Map a lookup type constant (integer) value to its name.
* @param type lookup type
* @return lookup type name
*/
public static String getLookupTypeName ( int type ) {
String tn = null;
switch ( type ) {
case GDEF_LOOKUP_TYPE_GLYPH_CLASS:
tn = "glyphclass";
break;
case GDEF_LOOKUP_TYPE_ATTACHMENT_POINT:
tn = "attachmentpoint";
break;
case GDEF_LOOKUP_TYPE_LIGATURE_CARET:
tn = "ligaturecaret";
break;
case GDEF_LOOKUP_TYPE_MARK_ATTACHMENT:
tn = "markattachment";
break;
default:
tn = "unknown";
break;
}
return tn;
}
/**
* Create a definition subtable according to the specified arguments.
* @param type subtable type
* @param id subtable identifier
* @param sequence subtable sequence
* @param flags subtable flags (must be zero)
* @param format subtable format
* @param mapping subtable mapping table
* @param entries subtable entries
* @return a glyph subtable instance
*/
public static GlyphSubtable createSubtable ( int type, String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
GlyphSubtable st = null;
switch ( type ) {
case GDEF_LOOKUP_TYPE_GLYPH_CLASS:
st = GlyphClassSubtable.create ( id, sequence, flags, format, mapping, entries );
break;
case GDEF_LOOKUP_TYPE_ATTACHMENT_POINT:
st = AttachmentPointSubtable.create ( id, sequence, flags, format, mapping, entries );
break;
case GDEF_LOOKUP_TYPE_LIGATURE_CARET:
st = LigatureCaretSubtable.create ( id, sequence, flags, format, mapping, entries );
break;
case GDEF_LOOKUP_TYPE_MARK_ATTACHMENT:
st = MarkAttachmentSubtable.create ( id, sequence, flags, format, mapping, entries );
break;
default:
break;
}
return st;
}
private abstract static class GlyphClassSubtable extends GlyphDefinitionSubtable {
GlyphClassSubtable ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
super ( id, sequence, flags, format, mapping );
}
/** {@inheritDoc} */
public int getType() {
return GDEF_LOOKUP_TYPE_GLYPH_CLASS;
}
/**
* Determine if glyph belongs to pre-defined glyph class.
* @param gid a glyph identifier (index)
* @param gc a pre-defined glyph class (GLYPH_CLASS_BASE|GLYPH_CLASS_LIGATURE|GLYPH_CLASS_MARK|GLYPH_CLASS_COMPONENT).
* @return true if glyph belongs to specified glyph class
*/
public abstract boolean isGlyphClass ( int gid, int gc );
/**
* Determine glyph class.
* @param gid a glyph identifier (index)
* @return a pre-defined glyph class (GLYPH_CLASS_BASE|GLYPH_CLASS_LIGATURE|GLYPH_CLASS_MARK|GLYPH_CLASS_COMPONENT).
*/
public abstract int getGlyphClass ( int gid );
static GlyphDefinitionSubtable create ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
if ( format == 1 ) {
return new GlyphClassSubtableFormat1 ( id, sequence, flags, format, mapping, entries );
} else {
throw new UnsupportedOperationException();
}
}
}
private static class GlyphClassSubtableFormat1 extends GlyphClassSubtable {
GlyphClassSubtableFormat1 ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
super ( id, sequence, flags, format, mapping, entries );
}
/** {@inheritDoc} */
public List getEntries() {
return null;
}
/** {@inheritDoc} */
public boolean isCompatible ( GlyphSubtable subtable ) {
return subtable instanceof GlyphClassSubtable;
}
/** {@inheritDoc} */
public boolean isGlyphClass ( int gid, int gc ) {
GlyphClassMapping cm = getClasses();
if ( cm != null ) {
return cm.getClassIndex ( gid, 0 ) == gc;
} else {
return false;
}
}
/** {@inheritDoc} */
public int getGlyphClass ( int gid ) {
GlyphClassMapping cm = getClasses();
if ( cm != null ) {
return cm.getClassIndex ( gid, 0 );
} else {
return -1;
}
}
}
private abstract static class AttachmentPointSubtable extends GlyphDefinitionSubtable {
AttachmentPointSubtable ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
super ( id, sequence, flags, format, mapping );
}
/** {@inheritDoc} */
public int getType() {
return GDEF_LOOKUP_TYPE_ATTACHMENT_POINT;
}
static GlyphDefinitionSubtable create ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
if ( format == 1 ) {
return new AttachmentPointSubtableFormat1 ( id, sequence, flags, format, mapping, entries );
} else {
throw new UnsupportedOperationException();
}
}
}
private static class AttachmentPointSubtableFormat1 extends AttachmentPointSubtable {
AttachmentPointSubtableFormat1 ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
super ( id, sequence, flags, format, mapping, entries );
}
/** {@inheritDoc} */
public List getEntries() {
return null;
}
/** {@inheritDoc} */
public boolean isCompatible ( GlyphSubtable subtable ) {
return subtable instanceof AttachmentPointSubtable;
}
}
private abstract static class LigatureCaretSubtable extends GlyphDefinitionSubtable {
LigatureCaretSubtable ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
super ( id, sequence, flags, format, mapping );
}
/** {@inheritDoc} */
public int getType() {
return GDEF_LOOKUP_TYPE_LIGATURE_CARET;
}
static GlyphDefinitionSubtable create ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
if ( format == 1 ) {
return new LigatureCaretSubtableFormat1 ( id, sequence, flags, format, mapping, entries );
} else {
throw new UnsupportedOperationException();
}
}
}
private static class LigatureCaretSubtableFormat1 extends LigatureCaretSubtable {
LigatureCaretSubtableFormat1 ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
super ( id, sequence, flags, format, mapping, entries );
}
/** {@inheritDoc} */
public List getEntries() {
return null;
}
/** {@inheritDoc} */
public boolean isCompatible ( GlyphSubtable subtable ) {
return subtable instanceof LigatureCaretSubtable;
}
}
private abstract static class MarkAttachmentSubtable extends GlyphDefinitionSubtable {
MarkAttachmentSubtable ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
super ( id, sequence, flags, format, mapping );
}
/** {@inheritDoc} */
public int getType() {
return GDEF_LOOKUP_TYPE_MARK_ATTACHMENT;
}
/**
* Determine if glyph belongs to (font specific) mark attachment class.
* @param gid a glyph identifier (index)
* @param mac a (font specific) mark attachment class
* @return true if glyph belongs to specified mark attachment class
*/
public abstract boolean isMarkAttachClass ( int gid, int mac );
/**
* Determine mark attachment class.
* @param gid a glyph identifier (index)
* @return a non-negative mark attachment class, or -1 if no class defined
*/
public abstract int getMarkAttachClass ( int gid );
static GlyphDefinitionSubtable create ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
if ( format == 1 ) {
return new MarkAttachmentSubtableFormat1 ( id, sequence, flags, format, mapping, entries );
} else {
throw new UnsupportedOperationException();
}
}
}
private static class MarkAttachmentSubtableFormat1 extends MarkAttachmentSubtable {
MarkAttachmentSubtableFormat1 ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
super ( id, sequence, flags, format, mapping, entries );
}
/** {@inheritDoc} */
public List getEntries() {
return null;
}
/** {@inheritDoc} */
public boolean isCompatible ( GlyphSubtable subtable ) {
return subtable instanceof MarkAttachmentSubtable;
}
/** {@inheritDoc} */
public boolean isMarkAttachClass ( int gid, int mac ) {
GlyphClassMapping cm = getClasses();
if ( cm != null ) {
return cm.getClassIndex ( gid, 0 ) == mac;
} else {
return false;
}
}
/** {@inheritDoc} */
public int getMarkAttachClass ( int gid ) {
GlyphClassMapping cm = getClasses();
if ( cm != null ) {
return cm.getClassIndex ( gid, 0 );
} else {
return -1;
}
}
}
}
|