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

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