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.

GlyphSubstitutionTable.java 63KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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.ArrayList;
  20. import java.util.List;
  21. import java.util.Map;
  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.CharAssociation;
  26. import org.apache.fop.complexscripts.util.GlyphSequence;
  27. import org.apache.fop.complexscripts.util.GlyphTester;
  28. // CSOFF: LineLengthCheck
  29. /**
  30. * <p>The <code>GlyphSubstitutionTable</code> class is a glyph table that implements
  31. * <code>GlyphSubstitution</code> functionality.</p>
  32. *
  33. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  34. */
  35. public class GlyphSubstitutionTable extends GlyphTable {
  36. /** logging instance */
  37. private static final Log log = LogFactory.getLog(GlyphSubstitutionTable.class);
  38. /** single substitution subtable type */
  39. public static final int GSUB_LOOKUP_TYPE_SINGLE = 1;
  40. /** multiple substitution subtable type */
  41. public static final int GSUB_LOOKUP_TYPE_MULTIPLE = 2;
  42. /** alternate substitution subtable type */
  43. public static final int GSUB_LOOKUP_TYPE_ALTERNATE = 3;
  44. /** ligature substitution subtable type */
  45. public static final int GSUB_LOOKUP_TYPE_LIGATURE = 4;
  46. /** contextual substitution subtable type */
  47. public static final int GSUB_LOOKUP_TYPE_CONTEXTUAL = 5;
  48. /** chained contextual substitution subtable type */
  49. public static final int GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL = 6;
  50. /** extension substitution substitution subtable type */
  51. public static final int GSUB_LOOKUP_TYPE_EXTENSION_SUBSTITUTION = 7;
  52. /** reverse chained contextual single substitution subtable type */
  53. public static final int GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE = 8;
  54. /**
  55. * Instantiate a <code>GlyphSubstitutionTable</code> object using the specified lookups
  56. * and subtables.
  57. * @param gdef glyph definition table that applies
  58. * @param lookups a map of lookup specifications to subtable identifier strings
  59. * @param subtables a list of identified subtables
  60. */
  61. public GlyphSubstitutionTable(GlyphDefinitionTable gdef, Map lookups, List subtables) {
  62. super(gdef, lookups);
  63. if ((subtables == null) || (subtables.size() == 0)) {
  64. throw new AdvancedTypographicTableFormatException("subtables must be non-empty");
  65. } else {
  66. for (Object o : subtables) {
  67. if (o instanceof GlyphSubstitutionSubtable) {
  68. addSubtable((GlyphSubtable) o);
  69. } else {
  70. throw new AdvancedTypographicTableFormatException("subtable must be a glyph substitution subtable");
  71. }
  72. }
  73. freezeSubtables();
  74. }
  75. }
  76. /**
  77. * Perform substitution processing using all matching lookups.
  78. * @param gs an input glyph sequence
  79. * @param script a script identifier
  80. * @param language a language identifier
  81. * @return the substituted (output) glyph sequence
  82. */
  83. public GlyphSequence substitute(GlyphSequence gs, String script, String language) {
  84. GlyphSequence ogs;
  85. Map/*<LookupSpec,List<LookupTable>>*/ lookups = matchLookups(script, language, "*");
  86. if ((lookups != null) && (lookups.size() > 0)) {
  87. ScriptProcessor sp = ScriptProcessor.getInstance(script);
  88. ogs = sp.substitute(this, gs, script, language, lookups);
  89. } else {
  90. ogs = gs;
  91. }
  92. return ogs;
  93. }
  94. /**
  95. * Map a lookup type name to its constant (integer) value.
  96. * @param name lookup type name
  97. * @return lookup type
  98. */
  99. public static int getLookupTypeFromName(String name) {
  100. int t;
  101. String s = name.toLowerCase();
  102. if ("single".equals(s)) {
  103. t = GSUB_LOOKUP_TYPE_SINGLE;
  104. } else if ("multiple".equals(s)) {
  105. t = GSUB_LOOKUP_TYPE_MULTIPLE;
  106. } else if ("alternate".equals(s)) {
  107. t = GSUB_LOOKUP_TYPE_ALTERNATE;
  108. } else if ("ligature".equals(s)) {
  109. t = GSUB_LOOKUP_TYPE_LIGATURE;
  110. } else if ("contextual".equals(s)) {
  111. t = GSUB_LOOKUP_TYPE_CONTEXTUAL;
  112. } else if ("chainedcontextual".equals(s)) {
  113. t = GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
  114. } else if ("extensionsubstitution".equals(s)) {
  115. t = GSUB_LOOKUP_TYPE_EXTENSION_SUBSTITUTION;
  116. } else if ("reversechainiingcontextualsingle".equals(s)) {
  117. t = GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE;
  118. } else {
  119. t = -1;
  120. }
  121. return t;
  122. }
  123. /**
  124. * Map a lookup type constant (integer) value to its name.
  125. * @param type lookup type
  126. * @return lookup type name
  127. */
  128. public static String getLookupTypeName(int type) {
  129. String tn = null;
  130. switch (type) {
  131. case GSUB_LOOKUP_TYPE_SINGLE:
  132. tn = "single";
  133. break;
  134. case GSUB_LOOKUP_TYPE_MULTIPLE:
  135. tn = "multiple";
  136. break;
  137. case GSUB_LOOKUP_TYPE_ALTERNATE:
  138. tn = "alternate";
  139. break;
  140. case GSUB_LOOKUP_TYPE_LIGATURE:
  141. tn = "ligature";
  142. break;
  143. case GSUB_LOOKUP_TYPE_CONTEXTUAL:
  144. tn = "contextual";
  145. break;
  146. case GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL:
  147. tn = "chainedcontextual";
  148. break;
  149. case GSUB_LOOKUP_TYPE_EXTENSION_SUBSTITUTION:
  150. tn = "extensionsubstitution";
  151. break;
  152. case GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE:
  153. tn = "reversechainiingcontextualsingle";
  154. break;
  155. default:
  156. tn = "unknown";
  157. break;
  158. }
  159. return tn;
  160. }
  161. /**
  162. * Create a substitution subtable according to the specified arguments.
  163. * @param type subtable type
  164. * @param id subtable identifier
  165. * @param sequence subtable sequence
  166. * @param flags subtable flags
  167. * @param format subtable format
  168. * @param coverage subtable coverage table
  169. * @param entries subtable entries
  170. * @return a glyph subtable instance
  171. */
  172. public static GlyphSubtable createSubtable(int type, String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  173. GlyphSubtable st = null;
  174. switch (type) {
  175. case GSUB_LOOKUP_TYPE_SINGLE:
  176. st = SingleSubtable.create(id, sequence, flags, format, coverage, entries);
  177. break;
  178. case GSUB_LOOKUP_TYPE_MULTIPLE:
  179. st = MultipleSubtable.create(id, sequence, flags, format, coverage, entries);
  180. break;
  181. case GSUB_LOOKUP_TYPE_ALTERNATE:
  182. st = AlternateSubtable.create(id, sequence, flags, format, coverage, entries);
  183. break;
  184. case GSUB_LOOKUP_TYPE_LIGATURE:
  185. st = LigatureSubtable.create(id, sequence, flags, format, coverage, entries);
  186. break;
  187. case GSUB_LOOKUP_TYPE_CONTEXTUAL:
  188. st = ContextualSubtable.create(id, sequence, flags, format, coverage, entries);
  189. break;
  190. case GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL:
  191. st = ChainedContextualSubtable.create(id, sequence, flags, format, coverage, entries);
  192. break;
  193. case GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE:
  194. st = ReverseChainedSingleSubtable.create(id, sequence, flags, format, coverage, entries);
  195. break;
  196. default:
  197. break;
  198. }
  199. return st;
  200. }
  201. /**
  202. * Create a substitution subtable according to the specified arguments.
  203. * @param type subtable type
  204. * @param id subtable identifier
  205. * @param sequence subtable sequence
  206. * @param flags subtable flags
  207. * @param format subtable format
  208. * @param coverage list of coverage table entries
  209. * @param entries subtable entries
  210. * @return a glyph subtable instance
  211. */
  212. public static GlyphSubtable createSubtable(int type, String id, int sequence, int flags, int format, List coverage, List entries) {
  213. return createSubtable(type, id, sequence, flags, format, GlyphCoverageTable.createCoverageTable(coverage), entries);
  214. }
  215. private abstract static class SingleSubtable extends GlyphSubstitutionSubtable {
  216. SingleSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  217. super(id, sequence, flags, format, coverage);
  218. }
  219. /** {@inheritDoc} */
  220. public int getType() {
  221. return GSUB_LOOKUP_TYPE_SINGLE;
  222. }
  223. /** {@inheritDoc} */
  224. public boolean isCompatible(GlyphSubtable subtable) {
  225. return subtable instanceof SingleSubtable;
  226. }
  227. /** {@inheritDoc} */
  228. public boolean substitute(GlyphSubstitutionState ss) {
  229. int gi = ss.getGlyph();
  230. int ci;
  231. if ((ci = getCoverageIndex(gi)) < 0) {
  232. return false;
  233. } else {
  234. int go = getGlyphForCoverageIndex(ci, gi);
  235. if ((go < 0) || (go > 65535)) {
  236. go = 65535;
  237. }
  238. ss.putGlyph(go, ss.getAssociation(), Boolean.TRUE);
  239. ss.consume(1);
  240. return true;
  241. }
  242. }
  243. /**
  244. * Obtain glyph for coverage index.
  245. * @param ci coverage index
  246. * @param gi original glyph index
  247. * @return substituted glyph value
  248. * @throws IllegalArgumentException if coverage index is not valid
  249. */
  250. public abstract int getGlyphForCoverageIndex(int ci, int gi) throws IllegalArgumentException;
  251. static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  252. if (format == 1) {
  253. return new SingleSubtableFormat1(id, sequence, flags, format, coverage, entries);
  254. } else if (format == 2) {
  255. return new SingleSubtableFormat2(id, sequence, flags, format, coverage, entries);
  256. } else {
  257. throw new UnsupportedOperationException();
  258. }
  259. }
  260. }
  261. private static class SingleSubtableFormat1 extends SingleSubtable {
  262. private int delta;
  263. private int ciMax;
  264. SingleSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  265. super(id, sequence, flags, format, coverage, entries);
  266. populate(entries);
  267. }
  268. /** {@inheritDoc} */
  269. public List getEntries() {
  270. List entries = new ArrayList(1);
  271. entries.add(delta);
  272. return entries;
  273. }
  274. /** {@inheritDoc} */
  275. public int getGlyphForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
  276. if (ci <= ciMax) {
  277. return gi + delta;
  278. } else {
  279. throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + ciMax);
  280. }
  281. }
  282. private void populate(List entries) {
  283. if ((entries == null) || (entries.size() != 1)) {
  284. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null and contain exactly one entry");
  285. } else {
  286. Object o = entries.get(0);
  287. int delta = 0;
  288. if (o instanceof Integer) {
  289. delta = (Integer) o;
  290. } else {
  291. throw new AdvancedTypographicTableFormatException("illegal entries entry, must be Integer, but is: " + o);
  292. }
  293. this.delta = delta;
  294. this.ciMax = getCoverageSize() - 1;
  295. }
  296. }
  297. }
  298. private static class SingleSubtableFormat2 extends SingleSubtable {
  299. private int[] glyphs;
  300. SingleSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  301. super(id, sequence, flags, format, coverage, entries);
  302. populate(entries);
  303. }
  304. /** {@inheritDoc} */
  305. public List getEntries() {
  306. List entries = new ArrayList(glyphs.length);
  307. for (int glyph : glyphs) {
  308. entries.add(glyph);
  309. }
  310. return entries;
  311. }
  312. /** {@inheritDoc} */
  313. public int getGlyphForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
  314. if (glyphs == null) {
  315. return -1;
  316. } else if (ci >= glyphs.length) {
  317. throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + glyphs.length);
  318. } else {
  319. return glyphs [ ci ];
  320. }
  321. }
  322. private void populate(List entries) {
  323. int i = 0;
  324. int n = entries.size();
  325. int[] glyphs = new int [ n ];
  326. for (Object o : entries) {
  327. if (o instanceof Integer) {
  328. int gid = (Integer) o;
  329. if ((gid >= 0) && (gid < 65536)) {
  330. glyphs[i++] = gid;
  331. } else {
  332. throw new AdvancedTypographicTableFormatException("illegal glyph index: " + gid);
  333. }
  334. } else {
  335. throw new AdvancedTypographicTableFormatException("illegal entries entry, must be Integer: " + o);
  336. }
  337. }
  338. assert i == n;
  339. assert this.glyphs == null;
  340. this.glyphs = glyphs;
  341. }
  342. }
  343. private abstract static class MultipleSubtable extends GlyphSubstitutionSubtable {
  344. public MultipleSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  345. super(id, sequence, flags, format, coverage);
  346. }
  347. /** {@inheritDoc} */
  348. public int getType() {
  349. return GSUB_LOOKUP_TYPE_MULTIPLE;
  350. }
  351. /** {@inheritDoc} */
  352. public boolean isCompatible(GlyphSubtable subtable) {
  353. return subtable instanceof MultipleSubtable;
  354. }
  355. /** {@inheritDoc} */
  356. public boolean substitute(GlyphSubstitutionState ss) {
  357. int gi = ss.getGlyph();
  358. int ci;
  359. if ((ci = getCoverageIndex(gi)) < 0) {
  360. return false;
  361. } else {
  362. int[] ga = getGlyphsForCoverageIndex(ci, gi);
  363. if (ga != null) {
  364. ss.putGlyphs(ga, CharAssociation.replicate(ss.getAssociation(), ga.length), Boolean.TRUE);
  365. ss.consume(1);
  366. }
  367. return true;
  368. }
  369. }
  370. /**
  371. * Obtain glyph sequence for coverage index.
  372. * @param ci coverage index
  373. * @param gi original glyph index
  374. * @return sequence of glyphs to substitute for input glyph
  375. * @throws IllegalArgumentException if coverage index is not valid
  376. */
  377. public abstract int[] getGlyphsForCoverageIndex(int ci, int gi) throws IllegalArgumentException;
  378. static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  379. if (format == 1) {
  380. return new MultipleSubtableFormat1(id, sequence, flags, format, coverage, entries);
  381. } else {
  382. throw new UnsupportedOperationException();
  383. }
  384. }
  385. }
  386. private static class MultipleSubtableFormat1 extends MultipleSubtable {
  387. private int[][] gsa; // glyph sequence array, ordered by coverage index
  388. MultipleSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  389. super(id, sequence, flags, format, coverage, entries);
  390. populate(entries);
  391. }
  392. /** {@inheritDoc} */
  393. public List getEntries() {
  394. if (gsa != null) {
  395. List entries = new ArrayList(1);
  396. entries.add(gsa);
  397. return entries;
  398. } else {
  399. return null;
  400. }
  401. }
  402. /** {@inheritDoc} */
  403. public int[] getGlyphsForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
  404. if (gsa == null) {
  405. return null;
  406. } else if (ci >= gsa.length) {
  407. throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + gsa.length);
  408. } else {
  409. return gsa [ ci ];
  410. }
  411. }
  412. private void populate(List entries) {
  413. if (entries == null) {
  414. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  415. } else if (entries.size() != 1) {
  416. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  417. } else {
  418. Object o;
  419. if (((o = entries.get(0)) == null) || !(o instanceof int[][])) {
  420. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an int[][], but is: " + ((o != null) ? o.getClass() : null));
  421. } else {
  422. gsa = (int[][]) o;
  423. }
  424. }
  425. }
  426. }
  427. private abstract static class AlternateSubtable extends GlyphSubstitutionSubtable {
  428. public AlternateSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  429. super(id, sequence, flags, format, coverage);
  430. }
  431. /** {@inheritDoc} */
  432. public int getType() {
  433. return GSUB_LOOKUP_TYPE_ALTERNATE;
  434. }
  435. /** {@inheritDoc} */
  436. public boolean isCompatible(GlyphSubtable subtable) {
  437. return subtable instanceof AlternateSubtable;
  438. }
  439. /** {@inheritDoc} */
  440. public boolean substitute(GlyphSubstitutionState ss) {
  441. int gi = ss.getGlyph();
  442. int ci;
  443. if ((ci = getCoverageIndex(gi)) < 0) {
  444. return false;
  445. } else {
  446. int[] ga = getAlternatesForCoverageIndex(ci, gi);
  447. int ai = ss.getAlternatesIndex(ci);
  448. int go;
  449. if ((ai < 0) || (ai >= ga.length)) {
  450. go = gi;
  451. } else {
  452. go = ga [ ai ];
  453. }
  454. if ((go < 0) || (go > 65535)) {
  455. go = 65535;
  456. }
  457. ss.putGlyph(go, ss.getAssociation(), Boolean.TRUE);
  458. ss.consume(1);
  459. return true;
  460. }
  461. }
  462. /**
  463. * Obtain glyph alternates for coverage index.
  464. * @param ci coverage index
  465. * @param gi original glyph index
  466. * @return sequence of glyphs to substitute for input glyph
  467. * @throws IllegalArgumentException if coverage index is not valid
  468. */
  469. public abstract int[] getAlternatesForCoverageIndex(int ci, int gi) throws IllegalArgumentException;
  470. static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  471. if (format == 1) {
  472. return new AlternateSubtableFormat1(id, sequence, flags, format, coverage, entries);
  473. } else {
  474. throw new UnsupportedOperationException();
  475. }
  476. }
  477. }
  478. private static class AlternateSubtableFormat1 extends AlternateSubtable {
  479. private int[][] gaa; // glyph alternates array, ordered by coverage index
  480. AlternateSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  481. super(id, sequence, flags, format, coverage, entries);
  482. populate(entries);
  483. }
  484. /** {@inheritDoc} */
  485. public List getEntries() {
  486. List entries = new ArrayList(gaa.length);
  487. for (int[] aGaa : gaa) {
  488. entries.add(aGaa);
  489. }
  490. return entries;
  491. }
  492. /** {@inheritDoc} */
  493. public int[] getAlternatesForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
  494. if (gaa == null) {
  495. return null;
  496. } else if (ci >= gaa.length) {
  497. throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + gaa.length);
  498. } else {
  499. return gaa [ ci ];
  500. }
  501. }
  502. private void populate(List entries) {
  503. int i = 0;
  504. int n = entries.size();
  505. int[][] gaa = new int [ n ][];
  506. for (Object o : entries) {
  507. if (o instanceof int[]) {
  508. gaa[i++] = (int[]) o;
  509. } else {
  510. throw new AdvancedTypographicTableFormatException("illegal entries entry, must be int[]: " + o);
  511. }
  512. }
  513. assert i == n;
  514. assert this.gaa == null;
  515. this.gaa = gaa;
  516. }
  517. }
  518. private abstract static class LigatureSubtable extends GlyphSubstitutionSubtable {
  519. public LigatureSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  520. super(id, sequence, flags, format, coverage);
  521. }
  522. /** {@inheritDoc} */
  523. public int getType() {
  524. return GSUB_LOOKUP_TYPE_LIGATURE;
  525. }
  526. /** {@inheritDoc} */
  527. public boolean isCompatible(GlyphSubtable subtable) {
  528. return subtable instanceof LigatureSubtable;
  529. }
  530. /** {@inheritDoc} */
  531. public boolean substitute(GlyphSubstitutionState ss) {
  532. int gi = ss.getGlyph();
  533. int ci;
  534. if ((ci = getCoverageIndex(gi)) < 0) {
  535. return false;
  536. } else {
  537. LigatureSet ls = getLigatureSetForCoverageIndex(ci, gi);
  538. if (ls != null) {
  539. boolean reverse = false;
  540. GlyphTester ignores = ss.getIgnoreDefault();
  541. int[] counts = ss.getGlyphsAvailable(0, reverse, ignores);
  542. int nga = counts[0];
  543. int ngi;
  544. if (nga > 1) {
  545. int[] iga = ss.getGlyphs(0, nga, reverse, ignores, null, counts);
  546. Ligature l = findLigature(ls, iga);
  547. if (l != null) {
  548. int go = l.getLigature();
  549. if ((go < 0) || (go > 65535)) {
  550. go = 65535;
  551. }
  552. int nmg = 1 + l.getNumComponents();
  553. // fetch matched number of component glyphs to determine matched and ignored count
  554. ss.getGlyphs(0, nmg, reverse, ignores, null, counts);
  555. nga = counts[0];
  556. ngi = counts[1];
  557. // fetch associations of matched component glyphs
  558. CharAssociation[] laa = ss.getAssociations(0, nga);
  559. // output ligature glyph and its association
  560. ss.putGlyph(go, CharAssociation.join(laa), Boolean.TRUE);
  561. // fetch and output ignored glyphs (if necessary)
  562. if (ngi > 0) {
  563. ss.putGlyphs(ss.getIgnoredGlyphs(0, ngi), ss.getIgnoredAssociations(0, ngi), null);
  564. }
  565. ss.consume(nga + ngi);
  566. }
  567. }
  568. }
  569. return true;
  570. }
  571. }
  572. private Ligature findLigature(LigatureSet ls, int[] glyphs) {
  573. Ligature[] la = ls.getLigatures();
  574. int k = -1;
  575. int maxComponents = -1;
  576. for (int i = 0, n = la.length; i < n; i++) {
  577. Ligature l = la [ i ];
  578. if (l.matchesComponents(glyphs)) {
  579. int nc = l.getNumComponents();
  580. if (nc > maxComponents) {
  581. maxComponents = nc;
  582. k = i;
  583. }
  584. }
  585. }
  586. if (k >= 0) {
  587. return la [ k ];
  588. } else {
  589. return null;
  590. }
  591. }
  592. /**
  593. * Obtain ligature set for coverage index.
  594. * @param ci coverage index
  595. * @param gi original glyph index
  596. * @return ligature set (or null if none defined)
  597. * @throws IllegalArgumentException if coverage index is not valid
  598. */
  599. public abstract LigatureSet getLigatureSetForCoverageIndex(int ci, int gi) throws IllegalArgumentException;
  600. static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  601. if (format == 1) {
  602. return new LigatureSubtableFormat1(id, sequence, flags, format, coverage, entries);
  603. } else {
  604. throw new UnsupportedOperationException();
  605. }
  606. }
  607. }
  608. private static class LigatureSubtableFormat1 extends LigatureSubtable {
  609. private LigatureSet[] ligatureSets;
  610. public LigatureSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  611. super(id, sequence, flags, format, coverage, entries);
  612. populate(entries);
  613. }
  614. /** {@inheritDoc} */
  615. public List getEntries() {
  616. List entries = new ArrayList(ligatureSets.length);
  617. for (LigatureSet ligatureSet : ligatureSets) {
  618. entries.add(ligatureSet);
  619. }
  620. return entries;
  621. }
  622. /** {@inheritDoc} */
  623. public LigatureSet getLigatureSetForCoverageIndex(int ci, int gi) throws IllegalArgumentException {
  624. if (ligatureSets == null) {
  625. return null;
  626. } else if (ci >= ligatureSets.length) {
  627. throw new IllegalArgumentException("coverage index " + ci + " out of range, maximum coverage index is " + ligatureSets.length);
  628. } else {
  629. return ligatureSets [ ci ];
  630. }
  631. }
  632. private void populate(List entries) {
  633. int i = 0;
  634. int n = entries.size();
  635. LigatureSet[] ligatureSets = new LigatureSet [ n ];
  636. for (Object o : entries) {
  637. if (o instanceof LigatureSet) {
  638. ligatureSets[i++] = (LigatureSet) o;
  639. } else {
  640. throw new AdvancedTypographicTableFormatException("illegal ligatures entry, must be LigatureSet: " + o);
  641. }
  642. }
  643. assert i == n;
  644. assert this.ligatureSets == null;
  645. this.ligatureSets = ligatureSets;
  646. }
  647. }
  648. private abstract static class ContextualSubtable extends GlyphSubstitutionSubtable {
  649. public ContextualSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  650. super(id, sequence, flags, format, coverage);
  651. }
  652. /** {@inheritDoc} */
  653. public int getType() {
  654. return GSUB_LOOKUP_TYPE_CONTEXTUAL;
  655. }
  656. /** {@inheritDoc} */
  657. public boolean isCompatible(GlyphSubtable subtable) {
  658. return subtable instanceof ContextualSubtable;
  659. }
  660. /** {@inheritDoc} */
  661. public boolean substitute(GlyphSubstitutionState ss) {
  662. int gi = ss.getGlyph();
  663. int ci;
  664. if ((ci = getCoverageIndex(gi)) < 0) {
  665. return false;
  666. } else {
  667. int[] rv = new int[1];
  668. RuleLookup[] la = getLookups(ci, gi, ss, rv);
  669. if (la != null) {
  670. ss.apply(la, rv[0]);
  671. }
  672. return true;
  673. }
  674. }
  675. /**
  676. * Obtain rule lookups set associated current input glyph context.
  677. * @param ci coverage index of glyph at current position
  678. * @param gi glyph index of glyph at current position
  679. * @param ss glyph substitution state
  680. * @param rv array of ints used to receive multiple return values, must be of length 1 or greater,
  681. * where the first entry is used to return the input sequence length of the matched rule
  682. * @return array of rule lookups or null if none applies
  683. */
  684. public abstract RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv);
  685. static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  686. if (format == 1) {
  687. return new ContextualSubtableFormat1(id, sequence, flags, format, coverage, entries);
  688. } else if (format == 2) {
  689. return new ContextualSubtableFormat2(id, sequence, flags, format, coverage, entries);
  690. } else if (format == 3) {
  691. return new ContextualSubtableFormat3(id, sequence, flags, format, coverage, entries);
  692. } else {
  693. throw new UnsupportedOperationException();
  694. }
  695. }
  696. }
  697. private static class ContextualSubtableFormat1 extends ContextualSubtable {
  698. private RuleSet[] rsa; // rule set array, ordered by glyph coverage index
  699. ContextualSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  700. super(id, sequence, flags, format, coverage, entries);
  701. populate(entries);
  702. }
  703. /** {@inheritDoc} */
  704. public List getEntries() {
  705. if (rsa != null) {
  706. List entries = new ArrayList(1);
  707. entries.add(rsa);
  708. return entries;
  709. } else {
  710. return null;
  711. }
  712. }
  713. /** {@inheritDoc} */
  714. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  715. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  716. }
  717. /** {@inheritDoc} */
  718. public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
  719. assert ss != null;
  720. assert (rv != null) && (rv.length > 0);
  721. assert rsa != null;
  722. if (rsa.length > 0) {
  723. RuleSet rs = rsa [ 0 ];
  724. if (rs != null) {
  725. Rule[] ra = rs.getRules();
  726. for (Rule r : ra) {
  727. if ((r != null) && (r instanceof ChainedGlyphSequenceRule)) {
  728. ChainedGlyphSequenceRule cr = (ChainedGlyphSequenceRule) r;
  729. int[] iga = cr.getGlyphs(gi);
  730. if (matches(ss, iga, 0, rv)) {
  731. return r.getLookups();
  732. }
  733. }
  734. }
  735. }
  736. }
  737. return null;
  738. }
  739. static boolean matches(GlyphSubstitutionState ss, int[] glyphs, int offset, int[] rv) {
  740. if ((glyphs == null) || (glyphs.length == 0)) {
  741. return true; // match null or empty glyph sequence
  742. } else {
  743. boolean reverse = offset < 0;
  744. GlyphTester ignores = ss.getIgnoreDefault();
  745. int[] counts = ss.getGlyphsAvailable(offset, reverse, ignores);
  746. int nga = counts[0];
  747. int ngm = glyphs.length;
  748. if (nga < ngm) {
  749. return false; // insufficient glyphs available to match
  750. } else {
  751. int[] ga = ss.getGlyphs(offset, ngm, reverse, ignores, null, counts);
  752. for (int k = 0; k < ngm; k++) {
  753. if (ga [ k ] != glyphs [ k ]) {
  754. return false; // match fails at ga [ k ]
  755. }
  756. }
  757. if (rv != null) {
  758. rv[0] = counts[0] + counts[1];
  759. }
  760. return true; // all glyphs match
  761. }
  762. }
  763. }
  764. private void populate(List entries) {
  765. if (entries == null) {
  766. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  767. } else if (entries.size() != 1) {
  768. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  769. } else {
  770. Object o;
  771. if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
  772. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  773. } else {
  774. rsa = (RuleSet[]) o;
  775. }
  776. }
  777. }
  778. }
  779. private static class ContextualSubtableFormat2 extends ContextualSubtable {
  780. private GlyphClassTable cdt; // class def table
  781. private int ngc; // class set count
  782. private RuleSet[] rsa; // rule set array, ordered by class number [0...ngc - 1]
  783. ContextualSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  784. super(id, sequence, flags, format, coverage, entries);
  785. populate(entries);
  786. }
  787. /** {@inheritDoc} */
  788. public List getEntries() {
  789. if (rsa != null) {
  790. List entries = new ArrayList(3);
  791. entries.add(cdt);
  792. entries.add(ngc);
  793. entries.add(rsa);
  794. return entries;
  795. } else {
  796. return null;
  797. }
  798. }
  799. /** {@inheritDoc} */
  800. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  801. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  802. }
  803. /** {@inheritDoc} */
  804. public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
  805. assert ss != null;
  806. assert (rv != null) && (rv.length > 0);
  807. assert rsa != null;
  808. if (rsa.length > 0) {
  809. RuleSet rs = rsa [ 0 ];
  810. if (rs != null) {
  811. Rule[] ra = rs.getRules();
  812. for (Rule r : ra) {
  813. if ((r != null) && (r instanceof ChainedClassSequenceRule)) {
  814. ChainedClassSequenceRule cr = (ChainedClassSequenceRule) r;
  815. int[] ca = cr.getClasses(cdt.getClassIndex(gi, ss.getClassMatchSet(gi)));
  816. if (matches(ss, cdt, ca, 0, rv)) {
  817. return r.getLookups();
  818. }
  819. }
  820. }
  821. }
  822. }
  823. return null;
  824. }
  825. static boolean matches(GlyphSubstitutionState ss, GlyphClassTable cdt, int[] classes, int offset, int[] rv) {
  826. if ((cdt == null) || (classes == null) || (classes.length == 0)) {
  827. return true; // match null class definitions, null or empty class sequence
  828. } else {
  829. boolean reverse = offset < 0;
  830. GlyphTester ignores = ss.getIgnoreDefault();
  831. int[] counts = ss.getGlyphsAvailable(offset, reverse, ignores);
  832. int nga = counts[0];
  833. int ngm = classes.length;
  834. if (nga < ngm) {
  835. return false; // insufficient glyphs available to match
  836. } else {
  837. int[] ga = ss.getGlyphs(offset, ngm, reverse, ignores, null, counts);
  838. for (int k = 0; k < ngm; k++) {
  839. int gi = ga [ k ];
  840. int ms = ss.getClassMatchSet(gi);
  841. int gc = cdt.getClassIndex(gi, ms);
  842. if ((gc < 0) || (gc >= cdt.getClassSize(ms))) {
  843. return false; // none or invalid class fails mat ch
  844. } else if (gc != classes [ k ]) {
  845. return false; // match fails at ga [ k ]
  846. }
  847. }
  848. if (rv != null) {
  849. rv[0] = counts[0] + counts[1];
  850. }
  851. return true; // all glyphs match
  852. }
  853. }
  854. }
  855. private void populate(List entries) {
  856. if (entries == null) {
  857. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  858. } else if (entries.size() != 3) {
  859. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 3 entries");
  860. } else {
  861. Object o;
  862. if (((o = entries.get(0)) == null) || !(o instanceof GlyphClassTable)) {
  863. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphClassTable, but is: " + ((o != null) ? o.getClass() : null));
  864. } else {
  865. cdt = (GlyphClassTable) o;
  866. }
  867. if (((o = entries.get(1)) == null) || !(o instanceof Integer)) {
  868. throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  869. } else {
  870. ngc = (Integer) (o);
  871. }
  872. if (((o = entries.get(2)) == null) || !(o instanceof RuleSet[])) {
  873. throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  874. } else {
  875. rsa = (RuleSet[]) o;
  876. if (rsa.length != ngc) {
  877. throw new AdvancedTypographicTableFormatException("illegal entries, RuleSet[] length is " + rsa.length + ", but expected " + ngc + " glyph classes");
  878. }
  879. }
  880. }
  881. }
  882. }
  883. private static class ContextualSubtableFormat3 extends ContextualSubtable {
  884. private RuleSet[] rsa; // rule set array, containing a single rule set
  885. ContextualSubtableFormat3(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  886. super(id, sequence, flags, format, coverage, entries);
  887. populate(entries);
  888. }
  889. /** {@inheritDoc} */
  890. public List getEntries() {
  891. if (rsa != null) {
  892. List entries = new ArrayList(1);
  893. entries.add(rsa);
  894. return entries;
  895. } else {
  896. return null;
  897. }
  898. }
  899. /** {@inheritDoc} */
  900. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  901. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  902. }
  903. /** {@inheritDoc} */
  904. public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
  905. assert ss != null;
  906. assert (rv != null) && (rv.length > 0);
  907. assert rsa != null;
  908. if (rsa.length > 0) {
  909. RuleSet rs = rsa [ 0 ];
  910. if (rs != null) {
  911. Rule[] ra = rs.getRules();
  912. for (Rule r : ra) {
  913. if ((r != null) && (r instanceof ChainedCoverageSequenceRule)) {
  914. ChainedCoverageSequenceRule cr = (ChainedCoverageSequenceRule) r;
  915. GlyphCoverageTable[] gca = cr.getCoverages();
  916. if (matches(ss, gca, 0, rv)) {
  917. return r.getLookups();
  918. }
  919. }
  920. }
  921. }
  922. }
  923. return null;
  924. }
  925. static boolean matches(GlyphSubstitutionState ss, GlyphCoverageTable[] gca, int offset, int[] rv) {
  926. if ((gca == null) || (gca.length == 0)) {
  927. return true; // match null or empty coverage array
  928. } else {
  929. boolean reverse = offset < 0;
  930. GlyphTester ignores = ss.getIgnoreDefault();
  931. int[] counts = ss.getGlyphsAvailable(offset, reverse, ignores);
  932. int nga = counts[0];
  933. int ngm = gca.length;
  934. if (nga < ngm) {
  935. return false; // insufficient glyphs available to match
  936. } else {
  937. int[] ga = ss.getGlyphs(offset, ngm, reverse, ignores, null, counts);
  938. for (int k = 0; k < ngm; k++) {
  939. GlyphCoverageTable ct = gca [ k ];
  940. if (ct != null) {
  941. if (ct.getCoverageIndex(ga [ k ]) < 0) {
  942. return false; // match fails at ga [ k ]
  943. }
  944. }
  945. }
  946. if (rv != null) {
  947. rv[0] = counts[0] + counts[1];
  948. }
  949. return true; // all glyphs match
  950. }
  951. }
  952. }
  953. private void populate(List entries) {
  954. if (entries == null) {
  955. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  956. } else if (entries.size() != 1) {
  957. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  958. } else {
  959. Object o;
  960. if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
  961. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  962. } else {
  963. rsa = (RuleSet[]) o;
  964. }
  965. }
  966. }
  967. }
  968. private abstract static class ChainedContextualSubtable extends GlyphSubstitutionSubtable {
  969. public ChainedContextualSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  970. super(id, sequence, flags, format, coverage);
  971. }
  972. /** {@inheritDoc} */
  973. public int getType() {
  974. return GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
  975. }
  976. /** {@inheritDoc} */
  977. public boolean isCompatible(GlyphSubtable subtable) {
  978. return subtable instanceof ChainedContextualSubtable;
  979. }
  980. /** {@inheritDoc} */
  981. public boolean substitute(GlyphSubstitutionState ss) {
  982. int gi = ss.getGlyph();
  983. int ci;
  984. if ((ci = getCoverageIndex(gi)) < 0) {
  985. return false;
  986. } else {
  987. int[] rv = new int[1];
  988. RuleLookup[] la = getLookups(ci, gi, ss, rv);
  989. if (la != null) {
  990. ss.apply(la, rv[0]);
  991. return true;
  992. } else {
  993. return false;
  994. }
  995. }
  996. }
  997. /**
  998. * Obtain rule lookups set associated current input glyph context.
  999. * @param ci coverage index of glyph at current position
  1000. * @param gi glyph index of glyph at current position
  1001. * @param ss glyph substitution state
  1002. * @param rv array of ints used to receive multiple return values, must be of length 1 or greater
  1003. * @return array of rule lookups or null if none applies
  1004. */
  1005. public abstract RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv);
  1006. static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1007. if (format == 1) {
  1008. return new ChainedContextualSubtableFormat1(id, sequence, flags, format, coverage, entries);
  1009. } else if (format == 2) {
  1010. return new ChainedContextualSubtableFormat2(id, sequence, flags, format, coverage, entries);
  1011. } else if (format == 3) {
  1012. return new ChainedContextualSubtableFormat3(id, sequence, flags, format, coverage, entries);
  1013. } else {
  1014. throw new UnsupportedOperationException();
  1015. }
  1016. }
  1017. }
  1018. private static class ChainedContextualSubtableFormat1 extends ChainedContextualSubtable {
  1019. private RuleSet[] rsa; // rule set array, ordered by glyph coverage index
  1020. ChainedContextualSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1021. super(id, sequence, flags, format, coverage, entries);
  1022. populate(entries);
  1023. }
  1024. /** {@inheritDoc} */
  1025. public List getEntries() {
  1026. if (rsa != null) {
  1027. List entries = new ArrayList(1);
  1028. entries.add(rsa);
  1029. return entries;
  1030. } else {
  1031. return null;
  1032. }
  1033. }
  1034. /** {@inheritDoc} */
  1035. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1036. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1037. }
  1038. /** {@inheritDoc} */
  1039. public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
  1040. assert ss != null;
  1041. assert (rv != null) && (rv.length > 0);
  1042. assert rsa != null;
  1043. if (rsa.length > 0) {
  1044. RuleSet rs = rsa [ 0 ];
  1045. if (rs != null) {
  1046. Rule[] ra = rs.getRules();
  1047. for (Rule r : ra) {
  1048. if ((r != null) && (r instanceof ChainedGlyphSequenceRule)) {
  1049. ChainedGlyphSequenceRule cr = (ChainedGlyphSequenceRule) r;
  1050. int[] iga = cr.getGlyphs(gi);
  1051. if (matches(ss, iga, 0, rv)) {
  1052. int[] bga = cr.getBacktrackGlyphs();
  1053. if (matches(ss, bga, -1, null)) {
  1054. int[] lga = cr.getLookaheadGlyphs();
  1055. if (matches(ss, lga, rv[0], null)) {
  1056. return r.getLookups();
  1057. }
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. return null;
  1065. }
  1066. private boolean matches(GlyphSubstitutionState ss, int[] glyphs, int offset, int[] rv) {
  1067. return ContextualSubtableFormat1.matches(ss, glyphs, offset, rv);
  1068. }
  1069. private void populate(List entries) {
  1070. if (entries == null) {
  1071. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1072. } else if (entries.size() != 1) {
  1073. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  1074. } else {
  1075. Object o;
  1076. if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
  1077. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1078. } else {
  1079. rsa = (RuleSet[]) o;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. private static class ChainedContextualSubtableFormat2 extends ChainedContextualSubtable {
  1085. private GlyphClassTable icdt; // input class def table
  1086. private GlyphClassTable bcdt; // backtrack class def table
  1087. private GlyphClassTable lcdt; // lookahead class def table
  1088. private int ngc; // class set count
  1089. private RuleSet[] rsa; // rule set array, ordered by class number [0...ngc - 1]
  1090. ChainedContextualSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1091. super(id, sequence, flags, format, coverage, entries);
  1092. populate(entries);
  1093. }
  1094. /** {@inheritDoc} */
  1095. public List getEntries() {
  1096. if (rsa != null) {
  1097. List entries = new ArrayList(5);
  1098. entries.add(icdt);
  1099. entries.add(bcdt);
  1100. entries.add(lcdt);
  1101. entries.add(ngc);
  1102. entries.add(rsa);
  1103. return entries;
  1104. } else {
  1105. return null;
  1106. }
  1107. }
  1108. /** {@inheritDoc} */
  1109. public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
  1110. assert ss != null;
  1111. assert (rv != null) && (rv.length > 0);
  1112. assert rsa != null;
  1113. if (rsa.length > 0) {
  1114. RuleSet rs = rsa [ 0 ];
  1115. if (rs != null) {
  1116. Rule[] ra = rs.getRules();
  1117. for (Rule r : ra) {
  1118. if ((r != null) && (r instanceof ChainedClassSequenceRule)) {
  1119. ChainedClassSequenceRule cr = (ChainedClassSequenceRule) r;
  1120. int[] ica = cr.getClasses(icdt.getClassIndex(gi, ss.getClassMatchSet(gi)));
  1121. if (matches(ss, icdt, ica, 0, rv)) {
  1122. int[] bca = cr.getBacktrackClasses();
  1123. if (matches(ss, bcdt, bca, -1, null)) {
  1124. int[] lca = cr.getLookaheadClasses();
  1125. if (matches(ss, lcdt, lca, rv[0], null)) {
  1126. return r.getLookups();
  1127. }
  1128. }
  1129. }
  1130. }
  1131. }
  1132. }
  1133. }
  1134. return null;
  1135. }
  1136. private boolean matches(GlyphSubstitutionState ss, GlyphClassTable cdt, int[] classes, int offset, int[] rv) {
  1137. return ContextualSubtableFormat2.matches(ss, cdt, classes, offset, rv);
  1138. }
  1139. /** {@inheritDoc} */
  1140. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1141. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1142. }
  1143. private void populate(List entries) {
  1144. if (entries == null) {
  1145. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1146. } else if (entries.size() != 5) {
  1147. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 5 entries");
  1148. } else {
  1149. Object o;
  1150. if (((o = entries.get(0)) == null) || !(o instanceof GlyphClassTable)) {
  1151. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphClassTable, but is: " + ((o != null) ? o.getClass() : null));
  1152. } else {
  1153. icdt = (GlyphClassTable) o;
  1154. }
  1155. if (((o = entries.get(1)) != null) && !(o instanceof GlyphClassTable)) {
  1156. throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an GlyphClassTable, but is: " + o.getClass());
  1157. } else {
  1158. bcdt = (GlyphClassTable) o;
  1159. }
  1160. if (((o = entries.get(2)) != null) && !(o instanceof GlyphClassTable)) {
  1161. throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be an GlyphClassTable, but is: " + o.getClass());
  1162. } else {
  1163. lcdt = (GlyphClassTable) o;
  1164. }
  1165. if (((o = entries.get(3)) == null) || !(o instanceof Integer)) {
  1166. throw new AdvancedTypographicTableFormatException("illegal entries, fourth entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  1167. } else {
  1168. ngc = (Integer) (o);
  1169. }
  1170. if (((o = entries.get(4)) == null) || !(o instanceof RuleSet[])) {
  1171. throw new AdvancedTypographicTableFormatException("illegal entries, fifth entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1172. } else {
  1173. rsa = (RuleSet[]) o;
  1174. if (rsa.length != ngc) {
  1175. throw new AdvancedTypographicTableFormatException("illegal entries, RuleSet[] length is " + rsa.length + ", but expected " + ngc + " glyph classes");
  1176. }
  1177. }
  1178. }
  1179. }
  1180. }
  1181. private static class ChainedContextualSubtableFormat3 extends ChainedContextualSubtable {
  1182. private RuleSet[] rsa; // rule set array, containing a single rule set
  1183. ChainedContextualSubtableFormat3(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1184. super(id, sequence, flags, format, coverage, entries);
  1185. populate(entries);
  1186. }
  1187. /** {@inheritDoc} */
  1188. public List getEntries() {
  1189. if (rsa != null) {
  1190. List entries = new ArrayList(1);
  1191. entries.add(rsa);
  1192. return entries;
  1193. } else {
  1194. return null;
  1195. }
  1196. }
  1197. /** {@inheritDoc} */
  1198. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1199. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1200. }
  1201. /** {@inheritDoc} */
  1202. public RuleLookup[] getLookups(int ci, int gi, GlyphSubstitutionState ss, int[] rv) {
  1203. assert ss != null;
  1204. assert (rv != null) && (rv.length > 0);
  1205. assert rsa != null;
  1206. if (rsa.length > 0) {
  1207. RuleSet rs = rsa [ 0 ];
  1208. if (rs != null) {
  1209. Rule[] ra = rs.getRules();
  1210. for (Rule r : ra) {
  1211. if ((r != null) && (r instanceof ChainedCoverageSequenceRule)) {
  1212. ChainedCoverageSequenceRule cr = (ChainedCoverageSequenceRule) r;
  1213. GlyphCoverageTable[] igca = cr.getCoverages();
  1214. if (matches(ss, igca, 0, rv)) {
  1215. GlyphCoverageTable[] bgca = cr.getBacktrackCoverages();
  1216. if (matches(ss, bgca, -1, null)) {
  1217. GlyphCoverageTable[] lgca = cr.getLookaheadCoverages();
  1218. if (matches(ss, lgca, rv[0], null)) {
  1219. return r.getLookups();
  1220. }
  1221. }
  1222. }
  1223. }
  1224. }
  1225. }
  1226. }
  1227. return null;
  1228. }
  1229. private boolean matches(GlyphSubstitutionState ss, GlyphCoverageTable[] gca, int offset, int[] rv) {
  1230. return ContextualSubtableFormat3.matches(ss, gca, offset, rv);
  1231. }
  1232. private void populate(List entries) {
  1233. if (entries == null) {
  1234. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1235. } else if (entries.size() != 1) {
  1236. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  1237. } else {
  1238. Object o;
  1239. if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
  1240. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1241. } else {
  1242. rsa = (RuleSet[]) o;
  1243. }
  1244. }
  1245. }
  1246. }
  1247. private abstract static class ReverseChainedSingleSubtable extends GlyphSubstitutionSubtable {
  1248. public ReverseChainedSingleSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1249. super(id, sequence, flags, format, coverage);
  1250. }
  1251. /** {@inheritDoc} */
  1252. public int getType() {
  1253. return GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE;
  1254. }
  1255. /** {@inheritDoc} */
  1256. public boolean isCompatible(GlyphSubtable subtable) {
  1257. return subtable instanceof ReverseChainedSingleSubtable;
  1258. }
  1259. /** {@inheritDoc} */
  1260. public boolean usesReverseScan() {
  1261. return true;
  1262. }
  1263. static GlyphSubstitutionSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1264. if (format == 1) {
  1265. return new ReverseChainedSingleSubtableFormat1(id, sequence, flags, format, coverage, entries);
  1266. } else {
  1267. throw new UnsupportedOperationException();
  1268. }
  1269. }
  1270. }
  1271. private static class ReverseChainedSingleSubtableFormat1 extends ReverseChainedSingleSubtable {
  1272. ReverseChainedSingleSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1273. super(id, sequence, flags, format, coverage, entries);
  1274. populate(entries);
  1275. }
  1276. /** {@inheritDoc} */
  1277. public List getEntries() {
  1278. return null;
  1279. }
  1280. private void populate(List entries) {
  1281. }
  1282. }
  1283. /**
  1284. * The <code>Ligature</code> class implements a ligature lookup result in terms of
  1285. * a ligature glyph (code) and the <emph>N+1...</emph> components that comprise the ligature,
  1286. * where the <emph>Nth</emph> component was consumed in the coverage table lookup mapping to
  1287. * this ligature instance.
  1288. */
  1289. public static class Ligature {
  1290. private final int ligature; // (resulting) ligature glyph
  1291. private final int[] components; // component glyph codes (note that first component is implied)
  1292. /**
  1293. * Instantiate a ligature.
  1294. * @param ligature glyph id
  1295. * @param components sequence of <emph>N+1...</emph> component glyph (or character) identifiers
  1296. */
  1297. public Ligature(int ligature, int[] components) {
  1298. if ((ligature < 0) || (ligature > 65535)) {
  1299. throw new AdvancedTypographicTableFormatException("invalid ligature glyph index: " + ligature);
  1300. } else if (components == null) {
  1301. throw new AdvancedTypographicTableFormatException("invalid ligature components, must be non-null array");
  1302. } else {
  1303. for (int gc : components) {
  1304. if ((gc < 0) || (gc > 65535)) {
  1305. throw new AdvancedTypographicTableFormatException("invalid component glyph index: " + gc);
  1306. }
  1307. }
  1308. this.ligature = ligature;
  1309. this.components = components;
  1310. }
  1311. }
  1312. /** @return ligature glyph id */
  1313. public int getLigature() {
  1314. return ligature;
  1315. }
  1316. /** @return array of <emph>N+1...</emph> components */
  1317. public int[] getComponents() {
  1318. return components;
  1319. }
  1320. /** @return components count */
  1321. public int getNumComponents() {
  1322. return components.length;
  1323. }
  1324. /**
  1325. * Determine if input sequence at offset matches ligature's components.
  1326. * @param glyphs array of glyph components to match (including first, implied glyph)
  1327. * @return true if matches
  1328. */
  1329. public boolean matchesComponents(int[] glyphs) {
  1330. if (glyphs.length < (components.length + 1)) {
  1331. return false;
  1332. } else {
  1333. for (int i = 0, n = components.length; i < n; i++) {
  1334. if (glyphs [ i + 1 ] != components [ i ]) {
  1335. return false;
  1336. }
  1337. }
  1338. return true;
  1339. }
  1340. }
  1341. /** {@inheritDoc} */
  1342. public String toString() {
  1343. StringBuffer sb = new StringBuffer();
  1344. sb.append("{components={");
  1345. for (int i = 0, n = components.length; i < n; i++) {
  1346. if (i > 0) {
  1347. sb.append(',');
  1348. }
  1349. sb.append(Integer.toString(components[i]));
  1350. }
  1351. sb.append("},ligature=");
  1352. sb.append(Integer.toString(ligature));
  1353. sb.append("}");
  1354. return sb.toString();
  1355. }
  1356. }
  1357. /**
  1358. * The <code>LigatureSet</code> class implements a set of ligatures.
  1359. */
  1360. public static class LigatureSet {
  1361. private final Ligature[] ligatures; // set of ligatures all of which share the first (implied) component
  1362. private final int maxComponents; // maximum number of components (including first)
  1363. /**
  1364. * Instantiate a set of ligatures.
  1365. * @param ligatures collection of ligatures
  1366. */
  1367. public LigatureSet(List ligatures) {
  1368. this ((Ligature[]) ligatures.toArray(new Ligature [ ligatures.size() ]));
  1369. }
  1370. /**
  1371. * Instantiate a set of ligatures.
  1372. * @param ligatures array of ligatures
  1373. */
  1374. public LigatureSet(Ligature[] ligatures) {
  1375. if (ligatures == null) {
  1376. throw new AdvancedTypographicTableFormatException("invalid ligatures, must be non-null array");
  1377. } else {
  1378. this.ligatures = ligatures;
  1379. int ncMax = -1;
  1380. for (Ligature l : ligatures) {
  1381. int nc = l.getNumComponents() + 1;
  1382. if (nc > ncMax) {
  1383. ncMax = nc;
  1384. }
  1385. }
  1386. maxComponents = ncMax;
  1387. }
  1388. }
  1389. /** @return array of ligatures in this ligature set */
  1390. public Ligature[] getLigatures() {
  1391. return ligatures;
  1392. }
  1393. /** @return count of ligatures in this ligature set */
  1394. public int getNumLigatures() {
  1395. return ligatures.length;
  1396. }
  1397. /** @return maximum number of components in one ligature (including first component) */
  1398. public int getMaxComponents() {
  1399. return maxComponents;
  1400. }
  1401. /** {@inheritDoc} */
  1402. public String toString() {
  1403. StringBuffer sb = new StringBuffer();
  1404. sb.append("{ligs={");
  1405. for (int i = 0, n = ligatures.length; i < n; i++) {
  1406. if (i > 0) {
  1407. sb.append(',');
  1408. }
  1409. sb.append(ligatures[i]);
  1410. }
  1411. sb.append("}}");
  1412. return sb.toString();
  1413. }
  1414. }
  1415. }