Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

GlyphDefinitionTable.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.complexscripts.fonts;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.complexscripts.scripts.ScriptProcessor;
  25. import org.apache.fop.complexscripts.util.GlyphSequence;
  26. // CSOFF: InnerAssignmentCheck
  27. // CSOFF: LineLengthCheck
  28. /**
  29. * <p>The <code>GlyphDefinitionTable</code> class is a glyph table that implements
  30. * glyph definition functionality according to the OpenType GDEF table.</p>
  31. *
  32. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  33. */
  34. public class GlyphDefinitionTable extends GlyphTable {
  35. /** logging instance */
  36. private static final Log log = LogFactory.getLog(GlyphDefinitionTable.class); // CSOK: ConstantNameCheck
  37. /** glyph class subtable type */
  38. public static final int GDEF_LOOKUP_TYPE_GLYPH_CLASS = 1;
  39. /** attachment point subtable type */
  40. public static final int GDEF_LOOKUP_TYPE_ATTACHMENT_POINT = 2;
  41. /** ligature caret subtable type */
  42. public static final int GDEF_LOOKUP_TYPE_LIGATURE_CARET = 3;
  43. /** mark attachment subtable type */
  44. public static final int GDEF_LOOKUP_TYPE_MARK_ATTACHMENT = 4;
  45. /** pre-defined glyph class - base glyph */
  46. public static final int GLYPH_CLASS_BASE = 1;
  47. /** pre-defined glyph class - ligature glyph */
  48. public static final int GLYPH_CLASS_LIGATURE = 2;
  49. /** pre-defined glyph class - mark glyph */
  50. public static final int GLYPH_CLASS_MARK = 3;
  51. /** pre-defined glyph class - component glyph */
  52. public static final int GLYPH_CLASS_COMPONENT = 4;
  53. /** singleton glyph class table */
  54. private GlyphClassSubtable gct;
  55. /** singleton attachment point table */
  56. // private AttachmentPointSubtable apt; // NOT YET USED
  57. /** singleton ligature caret table */
  58. // private LigatureCaretSubtable lct; // NOT YET USED
  59. /** singleton mark attachment table */
  60. private MarkAttachmentSubtable mat;
  61. /**
  62. * Instantiate a <code>GlyphDefinitionTable</code> object using the specified subtables.
  63. * @param subtables a list of identified subtables
  64. */
  65. public GlyphDefinitionTable ( List subtables ) {
  66. super ( null, new HashMap(0) );
  67. if ( ( subtables == null ) || ( subtables.size() == 0 ) ) {
  68. throw new AdvancedTypographicTableFormatException ( "subtables must be non-empty" );
  69. } else {
  70. for ( Iterator it = subtables.iterator(); it.hasNext();) {
  71. Object o = it.next();
  72. if ( o instanceof GlyphDefinitionSubtable ) {
  73. addSubtable ( (GlyphSubtable) o );
  74. } else {
  75. throw new AdvancedTypographicTableFormatException ( "subtable must be a glyph definition subtable" );
  76. }
  77. }
  78. freezeSubtables();
  79. }
  80. }
  81. /**
  82. * Reorder combining marks in glyph sequence so that they precede (within the sequence) the base
  83. * character to which they are applied. N.B. In the case of LTR segments, marks are not reordered by this,
  84. * method since when the segment is reversed by BIDI processing, marks are automatically reordered to precede
  85. * their base glyph.
  86. * @param gs an input glyph sequence
  87. * @param gpa associated glyph position adjustments (also reordered)
  88. * @param script a script identifier
  89. * @param language a language identifier
  90. * @return the reordered (output) glyph sequence
  91. */
  92. public GlyphSequence reorderCombiningMarks ( GlyphSequence gs, int[][] gpa, String script, String language ) {
  93. ScriptProcessor sp = ScriptProcessor.getInstance ( script );
  94. return sp.reorderCombiningMarks ( this, gs, gpa, script, language );
  95. }
  96. /** {@inheritDoc} */
  97. protected void addSubtable ( GlyphSubtable subtable ) {
  98. if ( subtable instanceof GlyphClassSubtable ) {
  99. this.gct = (GlyphClassSubtable) subtable;
  100. } else if ( subtable instanceof AttachmentPointSubtable ) {
  101. // TODO - not yet used
  102. // this.apt = (AttachmentPointSubtable) subtable;
  103. } else if ( subtable instanceof LigatureCaretSubtable ) {
  104. // TODO - not yet used
  105. // this.lct = (LigatureCaretSubtable) subtable;
  106. } else if ( subtable instanceof MarkAttachmentSubtable ) {
  107. this.mat = (MarkAttachmentSubtable) subtable;
  108. } else {
  109. throw new UnsupportedOperationException ( "unsupported glyph definition subtable type: " + subtable );
  110. }
  111. }
  112. /**
  113. * Determine if glyph belongs to pre-defined glyph class.
  114. * @param gid a glyph identifier (index)
  115. * @param gc a pre-defined glyph class (GLYPH_CLASS_BASE|GLYPH_CLASS_LIGATURE|GLYPH_CLASS_MARK|GLYPH_CLASS_COMPONENT).
  116. * @return true if glyph belongs to specified glyph class
  117. */
  118. public boolean isGlyphClass ( int gid, int gc ) {
  119. if ( gct != null ) {
  120. return gct.isGlyphClass ( gid, gc );
  121. } else {
  122. return false;
  123. }
  124. }
  125. /**
  126. * Determine glyph class.
  127. * @param gid a glyph identifier (index)
  128. * @return a pre-defined glyph class (GLYPH_CLASS_BASE|GLYPH_CLASS_LIGATURE|GLYPH_CLASS_MARK|GLYPH_CLASS_COMPONENT).
  129. */
  130. public int getGlyphClass ( int gid ) {
  131. if ( gct != null ) {
  132. return gct.getGlyphClass ( gid );
  133. } else {
  134. return -1;
  135. }
  136. }
  137. /**
  138. * Determine if glyph belongs to (font specific) mark attachment class.
  139. * @param gid a glyph identifier (index)
  140. * @param mac a (font specific) mark attachment class
  141. * @return true if glyph belongs to specified mark attachment class
  142. */
  143. public boolean isMarkAttachClass ( int gid, int mac ) {
  144. if ( mat != null ) {
  145. return mat.isMarkAttachClass ( gid, mac );
  146. } else {
  147. return false;
  148. }
  149. }
  150. /**
  151. * Determine mark attachment class.
  152. * @param gid a glyph identifier (index)
  153. * @return a non-negative mark attachment class, or -1 if no class defined
  154. */
  155. public int getMarkAttachClass ( int gid ) {
  156. if ( mat != null ) {
  157. return mat.getMarkAttachClass ( gid );
  158. } else {
  159. return -1;
  160. }
  161. }
  162. /**
  163. * Map a lookup type name to its constant (integer) value.
  164. * @param name lookup type name
  165. * @return lookup type
  166. */
  167. public static int getLookupTypeFromName ( String name ) {
  168. int t;
  169. String s = name.toLowerCase();
  170. if ( "glyphclass".equals ( s ) ) {
  171. t = GDEF_LOOKUP_TYPE_GLYPH_CLASS;
  172. } else if ( "attachmentpoint".equals ( s ) ) {
  173. t = GDEF_LOOKUP_TYPE_ATTACHMENT_POINT;
  174. } else if ( "ligaturecaret".equals ( s ) ) {
  175. t = GDEF_LOOKUP_TYPE_LIGATURE_CARET;
  176. } else if ( "markattachment".equals ( s ) ) {
  177. t = GDEF_LOOKUP_TYPE_MARK_ATTACHMENT;
  178. } else {
  179. t = -1;
  180. }
  181. return t;
  182. }
  183. /**
  184. * Map a lookup type constant (integer) value to its name.
  185. * @param type lookup type
  186. * @return lookup type name
  187. */
  188. public static String getLookupTypeName ( int type ) {
  189. String tn = null;
  190. switch ( type ) {
  191. case GDEF_LOOKUP_TYPE_GLYPH_CLASS:
  192. tn = "glyphclass";
  193. break;
  194. case GDEF_LOOKUP_TYPE_ATTACHMENT_POINT:
  195. tn = "attachmentpoint";
  196. break;
  197. case GDEF_LOOKUP_TYPE_LIGATURE_CARET:
  198. tn = "ligaturecaret";
  199. break;
  200. case GDEF_LOOKUP_TYPE_MARK_ATTACHMENT:
  201. tn = "markattachment";
  202. break;
  203. default:
  204. tn = "unknown";
  205. break;
  206. }
  207. return tn;
  208. }
  209. /**
  210. * Create a definition subtable according to the specified arguments.
  211. * @param type subtable type
  212. * @param id subtable identifier
  213. * @param sequence subtable sequence
  214. * @param flags subtable flags (must be zero)
  215. * @param format subtable format
  216. * @param mapping subtable mapping table
  217. * @param entries subtable entries
  218. * @return a glyph subtable instance
  219. */
  220. public static GlyphSubtable createSubtable ( int type, String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  221. GlyphSubtable st = null;
  222. switch ( type ) {
  223. case GDEF_LOOKUP_TYPE_GLYPH_CLASS:
  224. st = GlyphClassSubtable.create ( id, sequence, flags, format, mapping, entries );
  225. break;
  226. case GDEF_LOOKUP_TYPE_ATTACHMENT_POINT:
  227. st = AttachmentPointSubtable.create ( id, sequence, flags, format, mapping, entries );
  228. break;
  229. case GDEF_LOOKUP_TYPE_LIGATURE_CARET:
  230. st = LigatureCaretSubtable.create ( id, sequence, flags, format, mapping, entries );
  231. break;
  232. case GDEF_LOOKUP_TYPE_MARK_ATTACHMENT:
  233. st = MarkAttachmentSubtable.create ( id, sequence, flags, format, mapping, entries );
  234. break;
  235. default:
  236. break;
  237. }
  238. return st;
  239. }
  240. private abstract static class GlyphClassSubtable extends GlyphDefinitionSubtable {
  241. GlyphClassSubtable ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  242. super ( id, sequence, flags, format, mapping );
  243. }
  244. /** {@inheritDoc} */
  245. public int getType() {
  246. return GDEF_LOOKUP_TYPE_GLYPH_CLASS;
  247. }
  248. /**
  249. * Determine if glyph belongs to pre-defined glyph class.
  250. * @param gid a glyph identifier (index)
  251. * @param gc a pre-defined glyph class (GLYPH_CLASS_BASE|GLYPH_CLASS_LIGATURE|GLYPH_CLASS_MARK|GLYPH_CLASS_COMPONENT).
  252. * @return true if glyph belongs to specified glyph class
  253. */
  254. public abstract boolean isGlyphClass ( int gid, int gc );
  255. /**
  256. * Determine glyph class.
  257. * @param gid a glyph identifier (index)
  258. * @return a pre-defined glyph class (GLYPH_CLASS_BASE|GLYPH_CLASS_LIGATURE|GLYPH_CLASS_MARK|GLYPH_CLASS_COMPONENT).
  259. */
  260. public abstract int getGlyphClass ( int gid );
  261. static GlyphDefinitionSubtable create ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  262. if ( format == 1 ) {
  263. return new GlyphClassSubtableFormat1 ( id, sequence, flags, format, mapping, entries );
  264. } else {
  265. throw new UnsupportedOperationException();
  266. }
  267. }
  268. }
  269. private static class GlyphClassSubtableFormat1 extends GlyphClassSubtable {
  270. GlyphClassSubtableFormat1 ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  271. super ( id, sequence, flags, format, mapping, entries );
  272. }
  273. /** {@inheritDoc} */
  274. public List getEntries() {
  275. return null;
  276. }
  277. /** {@inheritDoc} */
  278. public boolean isCompatible ( GlyphSubtable subtable ) {
  279. return subtable instanceof GlyphClassSubtable;
  280. }
  281. /** {@inheritDoc} */
  282. public boolean isGlyphClass ( int gid, int gc ) {
  283. GlyphClassMapping cm = getClasses();
  284. if ( cm != null ) {
  285. return cm.getClassIndex ( gid, 0 ) == gc;
  286. } else {
  287. return false;
  288. }
  289. }
  290. /** {@inheritDoc} */
  291. public int getGlyphClass ( int gid ) {
  292. GlyphClassMapping cm = getClasses();
  293. if ( cm != null ) {
  294. return cm.getClassIndex ( gid, 0 );
  295. } else {
  296. return -1;
  297. }
  298. }
  299. }
  300. private abstract static class AttachmentPointSubtable extends GlyphDefinitionSubtable {
  301. AttachmentPointSubtable ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  302. super ( id, sequence, flags, format, mapping );
  303. }
  304. /** {@inheritDoc} */
  305. public int getType() {
  306. return GDEF_LOOKUP_TYPE_ATTACHMENT_POINT;
  307. }
  308. static GlyphDefinitionSubtable create ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  309. if ( format == 1 ) {
  310. return new AttachmentPointSubtableFormat1 ( id, sequence, flags, format, mapping, entries );
  311. } else {
  312. throw new UnsupportedOperationException();
  313. }
  314. }
  315. }
  316. private static class AttachmentPointSubtableFormat1 extends AttachmentPointSubtable {
  317. AttachmentPointSubtableFormat1 ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  318. super ( id, sequence, flags, format, mapping, entries );
  319. }
  320. /** {@inheritDoc} */
  321. public List getEntries() {
  322. return null;
  323. }
  324. /** {@inheritDoc} */
  325. public boolean isCompatible ( GlyphSubtable subtable ) {
  326. return subtable instanceof AttachmentPointSubtable;
  327. }
  328. }
  329. private abstract static class LigatureCaretSubtable extends GlyphDefinitionSubtable {
  330. LigatureCaretSubtable ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  331. super ( id, sequence, flags, format, mapping );
  332. }
  333. /** {@inheritDoc} */
  334. public int getType() {
  335. return GDEF_LOOKUP_TYPE_LIGATURE_CARET;
  336. }
  337. static GlyphDefinitionSubtable create ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  338. if ( format == 1 ) {
  339. return new LigatureCaretSubtableFormat1 ( id, sequence, flags, format, mapping, entries );
  340. } else {
  341. throw new UnsupportedOperationException();
  342. }
  343. }
  344. }
  345. private static class LigatureCaretSubtableFormat1 extends LigatureCaretSubtable {
  346. LigatureCaretSubtableFormat1 ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  347. super ( id, sequence, flags, format, mapping, entries );
  348. }
  349. /** {@inheritDoc} */
  350. public List getEntries() {
  351. return null;
  352. }
  353. /** {@inheritDoc} */
  354. public boolean isCompatible ( GlyphSubtable subtable ) {
  355. return subtable instanceof LigatureCaretSubtable;
  356. }
  357. }
  358. private abstract static class MarkAttachmentSubtable extends GlyphDefinitionSubtable {
  359. MarkAttachmentSubtable ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  360. super ( id, sequence, flags, format, mapping );
  361. }
  362. /** {@inheritDoc} */
  363. public int getType() {
  364. return GDEF_LOOKUP_TYPE_MARK_ATTACHMENT;
  365. }
  366. /**
  367. * Determine if glyph belongs to (font specific) mark attachment class.
  368. * @param gid a glyph identifier (index)
  369. * @param mac a (font specific) mark attachment class
  370. * @return true if glyph belongs to specified mark attachment class
  371. */
  372. public abstract boolean isMarkAttachClass ( int gid, int mac );
  373. /**
  374. * Determine mark attachment class.
  375. * @param gid a glyph identifier (index)
  376. * @return a non-negative mark attachment class, or -1 if no class defined
  377. */
  378. public abstract int getMarkAttachClass ( int gid );
  379. static GlyphDefinitionSubtable create ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  380. if ( format == 1 ) {
  381. return new MarkAttachmentSubtableFormat1 ( id, sequence, flags, format, mapping, entries );
  382. } else {
  383. throw new UnsupportedOperationException();
  384. }
  385. }
  386. }
  387. private static class MarkAttachmentSubtableFormat1 extends MarkAttachmentSubtable {
  388. MarkAttachmentSubtableFormat1 ( String id, int sequence, int flags, int format, GlyphMappingTable mapping, List entries ) {
  389. super ( id, sequence, flags, format, mapping, entries );
  390. }
  391. /** {@inheritDoc} */
  392. public List getEntries() {
  393. return null;
  394. }
  395. /** {@inheritDoc} */
  396. public boolean isCompatible ( GlyphSubtable subtable ) {
  397. return subtable instanceof MarkAttachmentSubtable;
  398. }
  399. /** {@inheritDoc} */
  400. public boolean isMarkAttachClass ( int gid, int mac ) {
  401. GlyphClassMapping cm = getClasses();
  402. if ( cm != null ) {
  403. return cm.getClassIndex ( gid, 0 ) == mac;
  404. } else {
  405. return false;
  406. }
  407. }
  408. /** {@inheritDoc} */
  409. public int getMarkAttachClass ( int gid ) {
  410. GlyphClassMapping cm = getClasses();
  411. if ( cm != null ) {
  412. return cm.getClassIndex ( gid, 0 );
  413. } else {
  414. return -1;
  415. }
  416. }
  417. }
  418. }