You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GlyphDefinitionTable.java 17KB

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