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.

GlyphPositioningTable.java 98KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283
  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.Arrays;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.fop.complexscripts.scripts.ScriptProcessor;
  27. import org.apache.fop.complexscripts.util.GlyphSequence;
  28. import org.apache.fop.complexscripts.util.GlyphTester;
  29. // CSOFF: LineLengthCheck
  30. /**
  31. * <p>The <code>GlyphPositioningTable</code> class is a glyph table that implements
  32. * <code>GlyphPositioning</code> functionality.</p>
  33. *
  34. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  35. */
  36. public class GlyphPositioningTable extends GlyphTable {
  37. /** logging instance */
  38. private static final Log log = LogFactory.getLog(GlyphPositioningTable.class);
  39. /** single positioning subtable type */
  40. public static final int GPOS_LOOKUP_TYPE_SINGLE = 1;
  41. /** multiple positioning subtable type */
  42. public static final int GPOS_LOOKUP_TYPE_PAIR = 2;
  43. /** cursive positioning subtable type */
  44. public static final int GPOS_LOOKUP_TYPE_CURSIVE = 3;
  45. /** mark to base positioning subtable type */
  46. public static final int GPOS_LOOKUP_TYPE_MARK_TO_BASE = 4;
  47. /** mark to ligature positioning subtable type */
  48. public static final int GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE = 5;
  49. /** mark to mark positioning subtable type */
  50. public static final int GPOS_LOOKUP_TYPE_MARK_TO_MARK = 6;
  51. /** contextual positioning subtable type */
  52. public static final int GPOS_LOOKUP_TYPE_CONTEXTUAL = 7;
  53. /** chained contextual positioning subtable type */
  54. public static final int GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL = 8;
  55. /** extension positioning subtable type */
  56. public static final int GPOS_LOOKUP_TYPE_EXTENSION_POSITIONING = 9;
  57. /**
  58. * Instantiate a <code>GlyphPositioningTable</code> object using the specified lookups
  59. * and subtables.
  60. * @param gdef glyph definition table that applies
  61. * @param lookups a map of lookup specifications to subtable identifier strings
  62. * @param subtables a list of identified subtables
  63. */
  64. public GlyphPositioningTable(GlyphDefinitionTable gdef, Map lookups, List subtables) {
  65. super(gdef, lookups);
  66. if ((subtables == null) || (subtables.size() == 0)) {
  67. throw new AdvancedTypographicTableFormatException("subtables must be non-empty");
  68. } else {
  69. for (Object o : subtables) {
  70. if (o instanceof GlyphPositioningSubtable) {
  71. addSubtable((GlyphSubtable) o);
  72. } else {
  73. throw new AdvancedTypographicTableFormatException("subtable must be a glyph positioning subtable");
  74. }
  75. }
  76. freezeSubtables();
  77. }
  78. }
  79. /**
  80. * Map a lookup type name to its constant (integer) value.
  81. * @param name lookup type name
  82. * @return lookup type
  83. */
  84. public static int getLookupTypeFromName(String name) {
  85. int t;
  86. String s = name.toLowerCase();
  87. if ("single".equals(s)) {
  88. t = GPOS_LOOKUP_TYPE_SINGLE;
  89. } else if ("pair".equals(s)) {
  90. t = GPOS_LOOKUP_TYPE_PAIR;
  91. } else if ("cursive".equals(s)) {
  92. t = GPOS_LOOKUP_TYPE_CURSIVE;
  93. } else if ("marktobase".equals(s)) {
  94. t = GPOS_LOOKUP_TYPE_MARK_TO_BASE;
  95. } else if ("marktoligature".equals(s)) {
  96. t = GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE;
  97. } else if ("marktomark".equals(s)) {
  98. t = GPOS_LOOKUP_TYPE_MARK_TO_MARK;
  99. } else if ("contextual".equals(s)) {
  100. t = GPOS_LOOKUP_TYPE_CONTEXTUAL;
  101. } else if ("chainedcontextual".equals(s)) {
  102. t = GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
  103. } else if ("extensionpositioning".equals(s)) {
  104. t = GPOS_LOOKUP_TYPE_EXTENSION_POSITIONING;
  105. } else {
  106. t = -1;
  107. }
  108. return t;
  109. }
  110. /**
  111. * Map a lookup type constant (integer) value to its name.
  112. * @param type lookup type
  113. * @return lookup type name
  114. */
  115. public static String getLookupTypeName(int type) {
  116. String tn;
  117. switch (type) {
  118. case GPOS_LOOKUP_TYPE_SINGLE:
  119. tn = "single";
  120. break;
  121. case GPOS_LOOKUP_TYPE_PAIR:
  122. tn = "pair";
  123. break;
  124. case GPOS_LOOKUP_TYPE_CURSIVE:
  125. tn = "cursive";
  126. break;
  127. case GPOS_LOOKUP_TYPE_MARK_TO_BASE:
  128. tn = "marktobase";
  129. break;
  130. case GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE:
  131. tn = "marktoligature";
  132. break;
  133. case GPOS_LOOKUP_TYPE_MARK_TO_MARK:
  134. tn = "marktomark";
  135. break;
  136. case GPOS_LOOKUP_TYPE_CONTEXTUAL:
  137. tn = "contextual";
  138. break;
  139. case GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL:
  140. tn = "chainedcontextual";
  141. break;
  142. case GPOS_LOOKUP_TYPE_EXTENSION_POSITIONING:
  143. tn = "extensionpositioning";
  144. break;
  145. default:
  146. tn = "unknown";
  147. break;
  148. }
  149. return tn;
  150. }
  151. /**
  152. * Create a positioning subtable according to the specified arguments.
  153. * @param type subtable type
  154. * @param id subtable identifier
  155. * @param sequence subtable sequence
  156. * @param flags subtable flags
  157. * @param format subtable format
  158. * @param coverage subtable coverage table
  159. * @param entries subtable entries
  160. * @return a glyph subtable instance
  161. */
  162. public static GlyphSubtable createSubtable(int type, String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  163. GlyphSubtable st = null;
  164. switch (type) {
  165. case GPOS_LOOKUP_TYPE_SINGLE:
  166. st = SingleSubtable.create(id, sequence, flags, format, coverage, entries);
  167. break;
  168. case GPOS_LOOKUP_TYPE_PAIR:
  169. st = PairSubtable.create(id, sequence, flags, format, coverage, entries);
  170. break;
  171. case GPOS_LOOKUP_TYPE_CURSIVE:
  172. st = CursiveSubtable.create(id, sequence, flags, format, coverage, entries);
  173. break;
  174. case GPOS_LOOKUP_TYPE_MARK_TO_BASE:
  175. st = MarkToBaseSubtable.create(id, sequence, flags, format, coverage, entries);
  176. break;
  177. case GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE:
  178. st = MarkToLigatureSubtable.create(id, sequence, flags, format, coverage, entries);
  179. break;
  180. case GPOS_LOOKUP_TYPE_MARK_TO_MARK:
  181. st = MarkToMarkSubtable.create(id, sequence, flags, format, coverage, entries);
  182. break;
  183. case GPOS_LOOKUP_TYPE_CONTEXTUAL:
  184. st = ContextualSubtable.create(id, sequence, flags, format, coverage, entries);
  185. break;
  186. case GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL:
  187. st = ChainedContextualSubtable.create(id, sequence, flags, format, coverage, entries);
  188. break;
  189. default:
  190. break;
  191. }
  192. return st;
  193. }
  194. /**
  195. * Create a positioning subtable according to the specified arguments.
  196. * @param type subtable type
  197. * @param id subtable identifier
  198. * @param sequence subtable sequence
  199. * @param flags subtable flags
  200. * @param format subtable format
  201. * @param coverage list of coverage table entries
  202. * @param entries subtable entries
  203. * @return a glyph subtable instance
  204. */
  205. public static GlyphSubtable createSubtable(int type, String id, int sequence, int flags, int format, List coverage, List entries) {
  206. return createSubtable(type, id, sequence, flags, format, GlyphCoverageTable.createCoverageTable(coverage), entries);
  207. }
  208. /**
  209. * Perform positioning processing using all matching lookups.
  210. * @param gs an input glyph sequence
  211. * @param script a script identifier
  212. * @param language a language identifier
  213. * @param fontSize size in device units
  214. * @param widths array of default advancements for each glyph
  215. * @param adjustments accumulated adjustments array (sequence) of 4-tuples of placement [PX,PY] and advance [AX,AY] adjustments, in that order,
  216. * with one 4-tuple for each element of glyph sequence
  217. * @return true if some adjustment is not zero; otherwise, false
  218. */
  219. public boolean position(GlyphSequence gs, String script, String language, int fontSize, int[] widths, int[][] adjustments) {
  220. Map/*<LookupSpec,List<LookupTable>>*/ lookups = matchLookups(script, language, "*");
  221. if ((lookups != null) && (lookups.size() > 0)) {
  222. ScriptProcessor sp = ScriptProcessor.getInstance(script);
  223. return sp.position(this, gs, script, language, fontSize, lookups, widths, adjustments);
  224. } else {
  225. return false;
  226. }
  227. }
  228. private abstract static class SingleSubtable extends GlyphPositioningSubtable {
  229. SingleSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  230. super(id, sequence, flags, format, coverage);
  231. }
  232. /** {@inheritDoc} */
  233. public int getType() {
  234. return GPOS_LOOKUP_TYPE_SINGLE;
  235. }
  236. /** {@inheritDoc} */
  237. public boolean isCompatible(GlyphSubtable subtable) {
  238. return subtable instanceof SingleSubtable;
  239. }
  240. /** {@inheritDoc} */
  241. public boolean position(GlyphPositioningState ps) {
  242. int gi = ps.getGlyph();
  243. int ci;
  244. if ((ci = getCoverageIndex(gi)) < 0) {
  245. return false;
  246. } else {
  247. Value v = getValue(ci, gi);
  248. if (v != null) {
  249. if (ps.adjust(v)) {
  250. ps.setAdjusted(true);
  251. }
  252. ps.consume(1);
  253. }
  254. return true;
  255. }
  256. }
  257. /**
  258. * Obtain positioning value for coverage index.
  259. * @param ci coverage index
  260. * @param gi input glyph index
  261. * @return positioning value or null if none applies
  262. */
  263. public abstract Value getValue(int ci, int gi);
  264. static GlyphPositioningSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  265. if (format == 1) {
  266. return new SingleSubtableFormat1(id, sequence, flags, format, coverage, entries);
  267. } else if (format == 2) {
  268. return new SingleSubtableFormat2(id, sequence, flags, format, coverage, entries);
  269. } else {
  270. throw new UnsupportedOperationException();
  271. }
  272. }
  273. }
  274. private static class SingleSubtableFormat1 extends SingleSubtable {
  275. private Value value;
  276. private int ciMax;
  277. SingleSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  278. super(id, sequence, flags, format, coverage, entries);
  279. populate(entries);
  280. }
  281. /** {@inheritDoc} */
  282. public List getEntries() {
  283. if (value != null) {
  284. List entries = new ArrayList(1);
  285. entries.add(value);
  286. return entries;
  287. } else {
  288. return null;
  289. }
  290. }
  291. /** {@inheritDoc} */
  292. public Value getValue(int ci, int gi) {
  293. if ((value != null) && (ci <= ciMax)) {
  294. return value;
  295. } else {
  296. return null;
  297. }
  298. }
  299. private void populate(List entries) {
  300. if ((entries == null) || (entries.size() != 1)) {
  301. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null and contain exactly one entry");
  302. } else {
  303. Value v;
  304. Object o = entries.get(0);
  305. if (o instanceof Value) {
  306. v = (Value) o;
  307. } else {
  308. throw new AdvancedTypographicTableFormatException("illegal entries entry, must be Value, but is: " + ((o != null) ? o.getClass() : null));
  309. }
  310. assert this.value == null;
  311. this.value = v;
  312. this.ciMax = getCoverageSize() - 1;
  313. }
  314. }
  315. }
  316. private static class SingleSubtableFormat2 extends SingleSubtable {
  317. private Value[] values;
  318. SingleSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  319. super(id, sequence, flags, format, coverage, entries);
  320. populate(entries);
  321. }
  322. /** {@inheritDoc} */
  323. public List getEntries() {
  324. if (values != null) {
  325. List entries = new ArrayList(values.length);
  326. Collections.addAll(entries, values);
  327. return entries;
  328. } else {
  329. return null;
  330. }
  331. }
  332. /** {@inheritDoc} */
  333. public Value getValue(int ci, int gi) {
  334. if ((values != null) && (ci < values.length)) {
  335. return values [ ci ];
  336. } else {
  337. return null;
  338. }
  339. }
  340. private void populate(List entries) {
  341. if (entries == null) {
  342. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  343. } else if (entries.size() != 1) {
  344. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  345. } else {
  346. Object o;
  347. if (((o = entries.get(0)) == null) || !(o instanceof Value[])) {
  348. throw new AdvancedTypographicTableFormatException("illegal entries, single entry must be a Value[], but is: " + ((o != null) ? o.getClass() : null));
  349. } else {
  350. Value[] va = (Value[]) o;
  351. if (va.length != getCoverageSize()) {
  352. throw new AdvancedTypographicTableFormatException("illegal values array, " + entries.size() + " values present, but requires " + getCoverageSize() + " values");
  353. } else {
  354. assert this.values == null;
  355. this.values = va;
  356. }
  357. }
  358. }
  359. }
  360. }
  361. private abstract static class PairSubtable extends GlyphPositioningSubtable {
  362. PairSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  363. super(id, sequence, flags, format, coverage);
  364. }
  365. /** {@inheritDoc} */
  366. public int getType() {
  367. return GPOS_LOOKUP_TYPE_PAIR;
  368. }
  369. /** {@inheritDoc} */
  370. public boolean isCompatible(GlyphSubtable subtable) {
  371. return subtable instanceof PairSubtable;
  372. }
  373. /** {@inheritDoc} */
  374. public boolean position(GlyphPositioningState ps) {
  375. boolean applied = false;
  376. int gi = ps.getGlyph(0);
  377. int ci;
  378. if ((ci = getCoverageIndex(gi)) >= 0) {
  379. int[] counts = ps.getGlyphsAvailable(0);
  380. int nga = counts[0];
  381. if (nga > 1) {
  382. int[] iga = ps.getGlyphs(0, 2, null, counts);
  383. if ((iga != null) && (iga.length == 2)) {
  384. PairValues pv = getPairValues(ci, iga[0], iga[1]);
  385. if (pv != null) {
  386. int offset = 0;
  387. int offsetLast = counts[0] + counts[1];
  388. // skip any ignored glyphs prior to first non-ignored glyph
  389. for ( ; offset < offsetLast; ++offset) {
  390. if (!ps.isIgnoredGlyph(offset)) {
  391. break;
  392. } else {
  393. ps.consume(1);
  394. }
  395. }
  396. // adjust first non-ignored glyph if first value isn't null
  397. Value v1 = pv.getValue1();
  398. if (v1 != null) {
  399. if (ps.adjust(v1, offset)) {
  400. ps.setAdjusted(true);
  401. }
  402. ps.consume(1); // consume first non-ignored glyph
  403. ++offset;
  404. }
  405. // skip any ignored glyphs prior to second non-ignored glyph
  406. for ( ; offset < offsetLast; ++offset) {
  407. if (!ps.isIgnoredGlyph(offset)) {
  408. break;
  409. } else {
  410. ps.consume(1);
  411. }
  412. }
  413. // adjust second non-ignored glyph if second value isn't null
  414. Value v2 = pv.getValue2();
  415. if (v2 != null) {
  416. if (ps.adjust(v2, offset)) {
  417. ps.setAdjusted(true);
  418. }
  419. ps.consume(1); // consume second non-ignored glyph
  420. ++offset;
  421. }
  422. applied = true;
  423. }
  424. }
  425. }
  426. }
  427. return applied;
  428. }
  429. /**
  430. * Obtain associated pair values.
  431. * @param ci coverage index
  432. * @param gi1 first input glyph index
  433. * @param gi2 second input glyph index
  434. * @return pair values or null if none applies
  435. */
  436. public abstract PairValues getPairValues(int ci, int gi1, int gi2);
  437. static GlyphPositioningSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  438. if (format == 1) {
  439. return new PairSubtableFormat1(id, sequence, flags, format, coverage, entries);
  440. } else if (format == 2) {
  441. return new PairSubtableFormat2(id, sequence, flags, format, coverage, entries);
  442. } else {
  443. throw new UnsupportedOperationException();
  444. }
  445. }
  446. }
  447. private static class PairSubtableFormat1 extends PairSubtable {
  448. private PairValues[][] pvm; // pair values matrix
  449. PairSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  450. super(id, sequence, flags, format, coverage, entries);
  451. populate(entries);
  452. }
  453. /** {@inheritDoc} */
  454. public List getEntries() {
  455. if (pvm != null) {
  456. List entries = new ArrayList(1);
  457. entries.add(pvm);
  458. return entries;
  459. } else {
  460. return null;
  461. }
  462. }
  463. /** {@inheritDoc} */
  464. public PairValues getPairValues(int ci, int gi1, int gi2) {
  465. if ((pvm != null) && (ci < pvm.length)) {
  466. PairValues[] pvt = pvm [ ci ];
  467. for (PairValues pv : pvt) {
  468. if (pv != null) {
  469. int g = pv.getGlyph();
  470. if (g < gi2) {
  471. continue;
  472. } else if (g == gi2) {
  473. return pv;
  474. } else {
  475. break;
  476. }
  477. }
  478. }
  479. }
  480. return null;
  481. }
  482. private void populate(List entries) {
  483. if (entries == null) {
  484. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  485. } else if (entries.size() != 1) {
  486. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  487. } else {
  488. Object o;
  489. if (((o = entries.get(0)) == null) || !(o instanceof PairValues[][])) {
  490. throw new AdvancedTypographicTableFormatException("illegal entries, first (and only) entry must be a PairValues[][], but is: " + ((o != null) ? o.getClass() : null));
  491. } else {
  492. pvm = (PairValues[][]) o;
  493. }
  494. }
  495. }
  496. }
  497. private static class PairSubtableFormat2 extends PairSubtable {
  498. private GlyphClassTable cdt1; // class def table 1
  499. private GlyphClassTable cdt2; // class def table 2
  500. private int nc1; // class 1 count
  501. private int nc2; // class 2 count
  502. private PairValues[][] pvm; // pair values matrix
  503. PairSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  504. super(id, sequence, flags, format, coverage, entries);
  505. populate(entries);
  506. }
  507. /** {@inheritDoc} */
  508. public List getEntries() {
  509. if (pvm != null) {
  510. List entries = new ArrayList(5);
  511. entries.add(cdt1);
  512. entries.add(cdt2);
  513. entries.add(nc1);
  514. entries.add(nc2);
  515. entries.add(pvm);
  516. return entries;
  517. } else {
  518. return null;
  519. }
  520. }
  521. /** {@inheritDoc} */
  522. public PairValues getPairValues(int ci, int gi1, int gi2) {
  523. if (pvm != null) {
  524. int c1 = cdt1.getClassIndex(gi1, 0);
  525. if ((c1 >= 0) && (c1 < nc1) && (c1 < pvm.length)) {
  526. PairValues[] pvt = pvm [ c1 ];
  527. if (pvt != null) {
  528. int c2 = cdt2.getClassIndex(gi2, 0);
  529. if ((c2 >= 0) && (c2 < nc2) && (c2 < pvt.length)) {
  530. return pvt [ c2 ];
  531. }
  532. }
  533. }
  534. }
  535. return null;
  536. }
  537. private void populate(List entries) {
  538. if (entries == null) {
  539. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  540. } else if (entries.size() != 5) {
  541. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 5 entries");
  542. } else {
  543. Object o;
  544. if (((o = entries.get(0)) == null) || !(o instanceof GlyphClassTable)) {
  545. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphClassTable, but is: " + ((o != null) ? o.getClass() : null));
  546. } else {
  547. cdt1 = (GlyphClassTable) o;
  548. }
  549. if (((o = entries.get(1)) == null) || !(o instanceof GlyphClassTable)) {
  550. throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an GlyphClassTable, but is: " + ((o != null) ? o.getClass() : null));
  551. } else {
  552. cdt2 = (GlyphClassTable) o;
  553. }
  554. if (((o = entries.get(2)) == null) || !(o instanceof Integer)) {
  555. throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  556. } else {
  557. nc1 = (Integer) (o);
  558. }
  559. if (((o = entries.get(3)) == null) || !(o instanceof Integer)) {
  560. throw new AdvancedTypographicTableFormatException("illegal entries, fourth entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  561. } else {
  562. nc2 = (Integer) (o);
  563. }
  564. if (((o = entries.get(4)) == null) || !(o instanceof PairValues[][])) {
  565. throw new AdvancedTypographicTableFormatException("illegal entries, fifth entry must be a PairValues[][], but is: " + ((o != null) ? o.getClass() : null));
  566. } else {
  567. pvm = (PairValues[][]) o;
  568. }
  569. }
  570. }
  571. }
  572. private abstract static class CursiveSubtable extends GlyphPositioningSubtable {
  573. CursiveSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  574. super(id, sequence, flags, format, coverage);
  575. }
  576. /** {@inheritDoc} */
  577. public int getType() {
  578. return GPOS_LOOKUP_TYPE_CURSIVE;
  579. }
  580. /** {@inheritDoc} */
  581. public boolean isCompatible(GlyphSubtable subtable) {
  582. return subtable instanceof CursiveSubtable;
  583. }
  584. /** {@inheritDoc} */
  585. public boolean position(GlyphPositioningState ps) {
  586. boolean applied = false;
  587. int gi = ps.getGlyph(0);
  588. int ci;
  589. if ((ci = getCoverageIndex(gi)) >= 0) {
  590. int[] counts = ps.getGlyphsAvailable(0);
  591. int nga = counts[0];
  592. if (nga > 1) {
  593. int[] iga = ps.getGlyphs(0, 2, null, counts);
  594. if ((iga != null) && (iga.length == 2)) {
  595. // int gi1 = gi;
  596. int ci1 = ci;
  597. int gi2 = iga [ 1 ];
  598. int ci2 = getCoverageIndex(gi2);
  599. Anchor[] aa = getExitEntryAnchors(ci1, ci2);
  600. if (aa != null) {
  601. Anchor exa = aa [ 0 ];
  602. Anchor ena = aa [ 1 ];
  603. // int exw = ps.getWidth ( gi1 );
  604. int enw = ps.getWidth(gi2);
  605. if ((exa != null) && (ena != null)) {
  606. Value v = ena.getAlignmentAdjustment(exa);
  607. v.adjust(-enw, 0, 0, 0);
  608. if (ps.adjust(v)) {
  609. ps.setAdjusted(true);
  610. }
  611. }
  612. // consume only first glyph of exit/entry glyph pair
  613. ps.consume(1);
  614. applied = true;
  615. }
  616. }
  617. }
  618. }
  619. return applied;
  620. }
  621. /**
  622. * Obtain exit anchor for first glyph with coverage index <code>ci1</code> and entry anchor for second
  623. * glyph with coverage index <code>ci2</code>.
  624. * @param ci1 coverage index of first glyph (may be negative)
  625. * @param ci2 coverage index of second glyph (may be negative)
  626. * @return array of two anchors or null if either coverage index is negative or corresponding anchor is
  627. * missing, where the first entry is the exit anchor of the first glyph and the second entry is the
  628. * entry anchor of the second glyph
  629. */
  630. public abstract Anchor[] getExitEntryAnchors(int ci1, int ci2);
  631. static GlyphPositioningSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  632. if (format == 1) {
  633. return new CursiveSubtableFormat1(id, sequence, flags, format, coverage, entries);
  634. } else {
  635. throw new UnsupportedOperationException();
  636. }
  637. }
  638. }
  639. private static class CursiveSubtableFormat1 extends CursiveSubtable {
  640. private Anchor[] aa; // anchor array, where even entries are entry anchors, and odd entries are exit anchors
  641. CursiveSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  642. super(id, sequence, flags, format, coverage, entries);
  643. populate(entries);
  644. }
  645. /** {@inheritDoc} */
  646. public List getEntries() {
  647. if (aa != null) {
  648. List entries = new ArrayList(1);
  649. entries.add(aa);
  650. return entries;
  651. } else {
  652. return null;
  653. }
  654. }
  655. /** {@inheritDoc} */
  656. public Anchor[] getExitEntryAnchors(int ci1, int ci2) {
  657. if ((ci1 >= 0) && (ci2 >= 0)) {
  658. int ai1 = (ci1 * 2) + 1; // ci1 denotes glyph with exit anchor
  659. int ai2 = (ci2 * 2) + 0; // ci2 denotes glyph with entry anchor
  660. if ((aa != null) && (ai1 < aa.length) && (ai2 < aa.length)) {
  661. Anchor exa = aa [ ai1 ];
  662. Anchor ena = aa [ ai2 ];
  663. if ((exa != null) && (ena != null)) {
  664. return new Anchor[] { exa, ena };
  665. }
  666. }
  667. }
  668. return null;
  669. }
  670. private void populate(List entries) {
  671. if (entries == null) {
  672. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  673. } else if (entries.size() != 1) {
  674. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  675. } else {
  676. Object o;
  677. if (((o = entries.get(0)) == null) || !(o instanceof Anchor[])) {
  678. throw new AdvancedTypographicTableFormatException("illegal entries, first (and only) entry must be a Anchor[], but is: " + ((o != null) ? o.getClass() : null));
  679. } else if ((((Anchor[]) o) .length % 2) != 0) {
  680. throw new AdvancedTypographicTableFormatException("illegal entries, Anchor[] array must have an even number of entries, but has: " + ((Anchor[]) o) .length);
  681. } else {
  682. aa = (Anchor[]) o;
  683. }
  684. }
  685. }
  686. }
  687. private abstract static class MarkToBaseSubtable extends GlyphPositioningSubtable {
  688. MarkToBaseSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  689. super(id, sequence, flags, format, coverage);
  690. }
  691. /** {@inheritDoc} */
  692. public int getType() {
  693. return GPOS_LOOKUP_TYPE_MARK_TO_BASE;
  694. }
  695. /** {@inheritDoc} */
  696. public boolean isCompatible(GlyphSubtable subtable) {
  697. return subtable instanceof MarkToBaseSubtable;
  698. }
  699. /** {@inheritDoc} */
  700. public boolean position(GlyphPositioningState ps) {
  701. boolean applied = false;
  702. int giMark = ps.getGlyph();
  703. int ciMark;
  704. if ((ciMark = getCoverageIndex(giMark)) >= 0) {
  705. MarkAnchor ma = getMarkAnchor(ciMark, giMark);
  706. if (ma != null) {
  707. for (int i = 0, n = ps.getPosition(); i < n; i++) {
  708. int gi = ps.getGlyph(-(i + 1));
  709. if (ps.isMark(gi)) {
  710. continue;
  711. } else {
  712. Anchor a = getBaseAnchor(gi, ma.getMarkClass());
  713. if (a != null) {
  714. Value v = a.getAlignmentAdjustment(ma);
  715. // start experimental fix for END OF AYAH in Lateef/Scheherazade
  716. int[] aa = ps.getAdjustment();
  717. if (aa[2] == 0) {
  718. v.adjust(0, 0, -ps.getWidth(giMark), 0);
  719. }
  720. // end experimental fix for END OF AYAH in Lateef/Scheherazade
  721. if (ps.adjust(v)) {
  722. ps.setAdjusted(true);
  723. }
  724. }
  725. ps.consume(1);
  726. applied = true;
  727. break;
  728. }
  729. }
  730. }
  731. }
  732. return applied;
  733. }
  734. /**
  735. * Obtain mark anchor associated with mark coverage index.
  736. * @param ciMark coverage index
  737. * @param giMark input glyph index of mark glyph
  738. * @return mark anchor or null if none applies
  739. */
  740. public abstract MarkAnchor getMarkAnchor(int ciMark, int giMark);
  741. /**
  742. * Obtain anchor associated with base glyph index and mark class.
  743. * @param giBase input glyph index of base glyph
  744. * @param markClass class number of mark glyph
  745. * @return anchor or null if none applies
  746. */
  747. public abstract Anchor getBaseAnchor(int giBase, int markClass);
  748. static GlyphPositioningSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  749. if (format == 1) {
  750. return new MarkToBaseSubtableFormat1(id, sequence, flags, format, coverage, entries);
  751. } else {
  752. throw new UnsupportedOperationException();
  753. }
  754. }
  755. }
  756. private static class MarkToBaseSubtableFormat1 extends MarkToBaseSubtable {
  757. private GlyphCoverageTable bct; // base coverage table
  758. private int nmc; // mark class count
  759. private MarkAnchor[] maa; // mark anchor array, ordered by mark coverage index
  760. private Anchor[][] bam; // base anchor matrix, ordered by base coverage index, then by mark class
  761. MarkToBaseSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  762. super(id, sequence, flags, format, coverage, entries);
  763. populate(entries);
  764. }
  765. /** {@inheritDoc} */
  766. public List getEntries() {
  767. if ((bct != null) && (maa != null) && (nmc > 0) && (bam != null)) {
  768. List entries = new ArrayList(4);
  769. entries.add(bct);
  770. entries.add(nmc);
  771. entries.add(maa);
  772. entries.add(bam);
  773. return entries;
  774. } else {
  775. return null;
  776. }
  777. }
  778. /** {@inheritDoc} */
  779. public MarkAnchor getMarkAnchor(int ciMark, int giMark) {
  780. if ((maa != null) && (ciMark < maa.length)) {
  781. return maa [ ciMark ];
  782. } else {
  783. return null;
  784. }
  785. }
  786. /** {@inheritDoc} */
  787. public Anchor getBaseAnchor(int giBase, int markClass) {
  788. int ciBase;
  789. if ((bct != null) && ((ciBase = bct.getCoverageIndex(giBase)) >= 0)) {
  790. if ((bam != null) && (ciBase < bam.length)) {
  791. Anchor[] ba = bam [ ciBase ];
  792. if ((ba != null) && (markClass < ba.length)) {
  793. return ba [ markClass ];
  794. }
  795. }
  796. }
  797. return null;
  798. }
  799. private void populate(List entries) {
  800. if (entries == null) {
  801. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  802. } else if (entries.size() != 4) {
  803. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 4 entries");
  804. } else {
  805. Object o;
  806. if (((o = entries.get(0)) == null) || !(o instanceof GlyphCoverageTable)) {
  807. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphCoverageTable, but is: " + ((o != null) ? o.getClass() : null));
  808. } else {
  809. bct = (GlyphCoverageTable) o;
  810. }
  811. if (((o = entries.get(1)) == null) || !(o instanceof Integer)) {
  812. throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  813. } else {
  814. nmc = (Integer) (o);
  815. }
  816. if (((o = entries.get(2)) == null) || !(o instanceof MarkAnchor[])) {
  817. throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be a MarkAnchor[], but is: " + ((o != null) ? o.getClass() : null));
  818. } else {
  819. maa = (MarkAnchor[]) o;
  820. }
  821. if (((o = entries.get(3)) == null) || !(o instanceof Anchor[][])) {
  822. throw new AdvancedTypographicTableFormatException("illegal entries, fourth entry must be a Anchor[][], but is: " + ((o != null) ? o.getClass() : null));
  823. } else {
  824. bam = (Anchor[][]) o;
  825. }
  826. }
  827. }
  828. }
  829. private abstract static class MarkToLigatureSubtable extends GlyphPositioningSubtable {
  830. MarkToLigatureSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  831. super(id, sequence, flags, format, coverage);
  832. }
  833. /** {@inheritDoc} */
  834. public int getType() {
  835. return GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE;
  836. }
  837. /** {@inheritDoc} */
  838. public boolean isCompatible(GlyphSubtable subtable) {
  839. return subtable instanceof MarkToLigatureSubtable;
  840. }
  841. /** {@inheritDoc} */
  842. public boolean position(GlyphPositioningState ps) {
  843. boolean applied = false;
  844. int giMark = ps.getGlyph();
  845. int ciMark;
  846. if ((ciMark = getCoverageIndex(giMark)) >= 0) {
  847. MarkAnchor ma = getMarkAnchor(ciMark, giMark);
  848. int mxc = getMaxComponentCount();
  849. if (ma != null) {
  850. for (int i = 0, n = ps.getPosition(); i < n; i++) {
  851. int gi = ps.getGlyph(-(i + 1));
  852. if (ps.isMark(gi)) {
  853. continue;
  854. } else {
  855. Anchor a = getLigatureAnchor(gi, mxc, i, ma.getMarkClass());
  856. if (a != null) {
  857. if (ps.adjust(a.getAlignmentAdjustment(ma))) {
  858. ps.setAdjusted(true);
  859. }
  860. }
  861. ps.consume(1);
  862. applied = true;
  863. break;
  864. }
  865. }
  866. }
  867. }
  868. return applied;
  869. }
  870. /**
  871. * Obtain mark anchor associated with mark coverage index.
  872. * @param ciMark coverage index
  873. * @param giMark input glyph index of mark glyph
  874. * @return mark anchor or null if none applies
  875. */
  876. public abstract MarkAnchor getMarkAnchor(int ciMark, int giMark);
  877. /**
  878. * Obtain maximum component count.
  879. * @return maximum component count (>=0)
  880. */
  881. public abstract int getMaxComponentCount();
  882. /**
  883. * Obtain anchor associated with ligature glyph index and mark class.
  884. * @param giLig input glyph index of ligature glyph
  885. * @param maxComponents maximum component count
  886. * @param component component number (0...maxComponents-1)
  887. * @param markClass class number of mark glyph
  888. * @return anchor or null if none applies
  889. */
  890. public abstract Anchor getLigatureAnchor(int giLig, int maxComponents, int component, int markClass);
  891. static GlyphPositioningSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  892. if (format == 1) {
  893. return new MarkToLigatureSubtableFormat1(id, sequence, flags, format, coverage, entries);
  894. } else {
  895. throw new UnsupportedOperationException();
  896. }
  897. }
  898. }
  899. private static class MarkToLigatureSubtableFormat1 extends MarkToLigatureSubtable {
  900. private GlyphCoverageTable lct; // ligature coverage table
  901. private int nmc; // mark class count
  902. private int mxc; // maximum ligature component count
  903. private MarkAnchor[] maa; // mark anchor array, ordered by mark coverage index
  904. private Anchor[][][] lam; // ligature anchor matrix, ordered by ligature coverage index, then ligature component, then mark class
  905. MarkToLigatureSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  906. super(id, sequence, flags, format, coverage, entries);
  907. populate(entries);
  908. }
  909. /** {@inheritDoc} */
  910. public List getEntries() {
  911. if (lam != null) {
  912. List entries = new ArrayList(5);
  913. entries.add(lct);
  914. entries.add(nmc);
  915. entries.add(mxc);
  916. entries.add(maa);
  917. entries.add(lam);
  918. return entries;
  919. } else {
  920. return null;
  921. }
  922. }
  923. /** {@inheritDoc} */
  924. public MarkAnchor getMarkAnchor(int ciMark, int giMark) {
  925. if ((maa != null) && (ciMark < maa.length)) {
  926. return maa [ ciMark ];
  927. } else {
  928. return null;
  929. }
  930. }
  931. /** {@inheritDoc} */
  932. public int getMaxComponentCount() {
  933. return mxc;
  934. }
  935. /** {@inheritDoc} */
  936. public Anchor getLigatureAnchor(int giLig, int maxComponents, int component, int markClass) {
  937. int ciLig;
  938. if ((lct != null) && ((ciLig = lct.getCoverageIndex(giLig)) >= 0)) {
  939. if ((lam != null) && (ciLig < lam.length)) {
  940. Anchor[][] lcm = lam [ ciLig ];
  941. if (component < maxComponents) {
  942. Anchor[] la = lcm [ component ];
  943. if ((la != null) && (markClass < la.length)) {
  944. return la [ markClass ];
  945. }
  946. }
  947. }
  948. }
  949. return null;
  950. }
  951. private void populate(List entries) {
  952. if (entries == null) {
  953. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  954. } else if (entries.size() != 5) {
  955. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 5 entries");
  956. } else {
  957. Object o;
  958. if (((o = entries.get(0)) == null) || !(o instanceof GlyphCoverageTable)) {
  959. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphCoverageTable, but is: " + ((o != null) ? o.getClass() : null));
  960. } else {
  961. lct = (GlyphCoverageTable) o;
  962. }
  963. if (((o = entries.get(1)) == null) || !(o instanceof Integer)) {
  964. throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  965. } else {
  966. nmc = (Integer) (o);
  967. }
  968. if (((o = entries.get(2)) == null) || !(o instanceof Integer)) {
  969. throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  970. } else {
  971. mxc = (Integer) (o);
  972. }
  973. if (((o = entries.get(3)) == null) || !(o instanceof MarkAnchor[])) {
  974. throw new AdvancedTypographicTableFormatException("illegal entries, fourth entry must be a MarkAnchor[], but is: " + ((o != null) ? o.getClass() : null));
  975. } else {
  976. maa = (MarkAnchor[]) o;
  977. }
  978. if (((o = entries.get(4)) == null) || !(o instanceof Anchor[][][])) {
  979. throw new AdvancedTypographicTableFormatException("illegal entries, fifth entry must be a Anchor[][][], but is: " + ((o != null) ? o.getClass() : null));
  980. } else {
  981. lam = (Anchor[][][]) o;
  982. }
  983. }
  984. }
  985. }
  986. private abstract static class MarkToMarkSubtable extends GlyphPositioningSubtable {
  987. MarkToMarkSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  988. super(id, sequence, flags, format, coverage);
  989. }
  990. /** {@inheritDoc} */
  991. public int getType() {
  992. return GPOS_LOOKUP_TYPE_MARK_TO_MARK;
  993. }
  994. /** {@inheritDoc} */
  995. public boolean isCompatible(GlyphSubtable subtable) {
  996. return subtable instanceof MarkToMarkSubtable;
  997. }
  998. /** {@inheritDoc} */
  999. public boolean position(GlyphPositioningState ps) {
  1000. boolean applied = false;
  1001. int giMark1 = ps.getGlyph();
  1002. int ciMark1;
  1003. if ((ciMark1 = getCoverageIndex(giMark1)) >= 0) {
  1004. MarkAnchor ma = getMark1Anchor(ciMark1, giMark1);
  1005. if (ma != null) {
  1006. if (ps.hasPrev()) {
  1007. Anchor a = getMark2Anchor(ps.getGlyph(-1), ma.getMarkClass());
  1008. if (a != null) {
  1009. if (ps.adjust(a.getAlignmentAdjustment(ma))) {
  1010. ps.setAdjusted(true);
  1011. }
  1012. }
  1013. ps.consume(1);
  1014. applied = true;
  1015. }
  1016. }
  1017. }
  1018. return applied;
  1019. }
  1020. /**
  1021. * Obtain mark 1 anchor associated with mark 1 coverage index.
  1022. * @param ciMark1 mark 1 coverage index
  1023. * @param giMark1 input glyph index of mark 1 glyph
  1024. * @return mark 1 anchor or null if none applies
  1025. */
  1026. public abstract MarkAnchor getMark1Anchor(int ciMark1, int giMark1);
  1027. /**
  1028. * Obtain anchor associated with mark 2 glyph index and mark 1 class.
  1029. * @param giMark2 input glyph index of mark 2 glyph
  1030. * @param markClass class number of mark 1 glyph
  1031. * @return anchor or null if none applies
  1032. */
  1033. public abstract Anchor getMark2Anchor(int giBase, int markClass);
  1034. static GlyphPositioningSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1035. if (format == 1) {
  1036. return new MarkToMarkSubtableFormat1(id, sequence, flags, format, coverage, entries);
  1037. } else {
  1038. throw new UnsupportedOperationException();
  1039. }
  1040. }
  1041. }
  1042. private static class MarkToMarkSubtableFormat1 extends MarkToMarkSubtable {
  1043. private GlyphCoverageTable mct2; // mark 2 coverage table
  1044. private int nmc; // mark class count
  1045. private MarkAnchor[] maa; // mark1 anchor array, ordered by mark1 coverage index
  1046. private Anchor[][] mam; // mark2 anchor matrix, ordered by mark2 coverage index, then by mark1 class
  1047. MarkToMarkSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1048. super(id, sequence, flags, format, coverage, entries);
  1049. populate(entries);
  1050. }
  1051. /** {@inheritDoc} */
  1052. public List getEntries() {
  1053. if ((mct2 != null) && (maa != null) && (nmc > 0) && (mam != null)) {
  1054. List entries = new ArrayList(4);
  1055. entries.add(mct2);
  1056. entries.add(nmc);
  1057. entries.add(maa);
  1058. entries.add(mam);
  1059. return entries;
  1060. } else {
  1061. return null;
  1062. }
  1063. }
  1064. /** {@inheritDoc} */
  1065. public MarkAnchor getMark1Anchor(int ciMark1, int giMark1) {
  1066. if ((maa != null) && (ciMark1 < maa.length)) {
  1067. return maa [ ciMark1 ];
  1068. } else {
  1069. return null;
  1070. }
  1071. }
  1072. /** {@inheritDoc} */
  1073. public Anchor getMark2Anchor(int giMark2, int markClass) {
  1074. int ciMark2;
  1075. if ((mct2 != null) && ((ciMark2 = mct2.getCoverageIndex(giMark2)) >= 0)) {
  1076. if ((mam != null) && (ciMark2 < mam.length)) {
  1077. Anchor[] ma = mam [ ciMark2 ];
  1078. if ((ma != null) && (markClass < ma.length)) {
  1079. return ma [ markClass ];
  1080. }
  1081. }
  1082. }
  1083. return null;
  1084. }
  1085. private void populate(List entries) {
  1086. if (entries == null) {
  1087. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1088. } else if (entries.size() != 4) {
  1089. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 4 entries");
  1090. } else {
  1091. Object o;
  1092. if (((o = entries.get(0)) == null) || !(o instanceof GlyphCoverageTable)) {
  1093. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphCoverageTable, but is: " + ((o != null) ? o.getClass() : null));
  1094. } else {
  1095. mct2 = (GlyphCoverageTable) o;
  1096. }
  1097. if (((o = entries.get(1)) == null) || !(o instanceof Integer)) {
  1098. throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  1099. } else {
  1100. nmc = (Integer) (o);
  1101. }
  1102. if (((o = entries.get(2)) == null) || !(o instanceof MarkAnchor[])) {
  1103. throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be a MarkAnchor[], but is: " + ((o != null) ? o.getClass() : null));
  1104. } else {
  1105. maa = (MarkAnchor[]) o;
  1106. }
  1107. if (((o = entries.get(3)) == null) || !(o instanceof Anchor[][])) {
  1108. throw new AdvancedTypographicTableFormatException("illegal entries, fourth entry must be a Anchor[][], but is: " + ((o != null) ? o.getClass() : null));
  1109. } else {
  1110. mam = (Anchor[][]) o;
  1111. }
  1112. }
  1113. }
  1114. }
  1115. private abstract static class ContextualSubtable extends GlyphPositioningSubtable {
  1116. ContextualSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1117. super(id, sequence, flags, format, coverage);
  1118. }
  1119. /** {@inheritDoc} */
  1120. public int getType() {
  1121. return GPOS_LOOKUP_TYPE_CONTEXTUAL;
  1122. }
  1123. /** {@inheritDoc} */
  1124. public boolean isCompatible(GlyphSubtable subtable) {
  1125. return subtable instanceof ContextualSubtable;
  1126. }
  1127. /** {@inheritDoc} */
  1128. public boolean position(GlyphPositioningState ps) {
  1129. boolean applied = false;
  1130. int gi = ps.getGlyph();
  1131. int ci;
  1132. if ((ci = getCoverageIndex(gi)) >= 0) {
  1133. int[] rv = new int[1];
  1134. RuleLookup[] la = getLookups(ci, gi, ps, rv);
  1135. if (la != null) {
  1136. ps.apply(la, rv[0]);
  1137. applied = true;
  1138. }
  1139. }
  1140. return applied;
  1141. }
  1142. /**
  1143. * Obtain rule lookups set associated current input glyph context.
  1144. * @param ci coverage index of glyph at current position
  1145. * @param gi glyph index of glyph at current position
  1146. * @param ps glyph positioning state
  1147. * @param rv array of ints used to receive multiple return values, must be of length 1 or greater,
  1148. * where the first entry is used to return the input sequence length of the matched rule
  1149. * @return array of rule lookups or null if none applies
  1150. */
  1151. public abstract RuleLookup[] getLookups(int ci, int gi, GlyphPositioningState ps, int[] rv);
  1152. static GlyphPositioningSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1153. if (format == 1) {
  1154. return new ContextualSubtableFormat1(id, sequence, flags, format, coverage, entries);
  1155. } else if (format == 2) {
  1156. return new ContextualSubtableFormat2(id, sequence, flags, format, coverage, entries);
  1157. } else if (format == 3) {
  1158. return new ContextualSubtableFormat3(id, sequence, flags, format, coverage, entries);
  1159. } else {
  1160. throw new UnsupportedOperationException();
  1161. }
  1162. }
  1163. }
  1164. private static class ContextualSubtableFormat1 extends ContextualSubtable {
  1165. private RuleSet[] rsa; // rule set array, ordered by glyph coverage index
  1166. ContextualSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1167. super(id, sequence, flags, format, coverage, entries);
  1168. populate(entries);
  1169. }
  1170. /** {@inheritDoc} */
  1171. public List getEntries() {
  1172. if (rsa != null) {
  1173. List entries = new ArrayList(1);
  1174. entries.add(rsa);
  1175. return entries;
  1176. } else {
  1177. return null;
  1178. }
  1179. }
  1180. /** {@inheritDoc} */
  1181. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1182. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1183. }
  1184. /** {@inheritDoc} */
  1185. public RuleLookup[] getLookups(int ci, int gi, GlyphPositioningState ps, int[] rv) {
  1186. assert ps != null;
  1187. assert (rv != null) && (rv.length > 0);
  1188. assert rsa != null;
  1189. if (rsa.length > 0) {
  1190. RuleSet rs = rsa [ 0 ];
  1191. if (rs != null) {
  1192. Rule[] ra = rs.getRules();
  1193. for (Rule r : ra) {
  1194. if ((r != null) && (r instanceof ChainedGlyphSequenceRule)) {
  1195. ChainedGlyphSequenceRule cr = (ChainedGlyphSequenceRule) r;
  1196. int[] iga = cr.getGlyphs(gi);
  1197. if (matches(ps, iga, 0, rv)) {
  1198. return r.getLookups();
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. return null;
  1205. }
  1206. static boolean matches(GlyphPositioningState ps, int[] glyphs, int offset, int[] rv) {
  1207. if ((glyphs == null) || (glyphs.length == 0)) {
  1208. return true; // match null or empty glyph sequence
  1209. } else {
  1210. boolean reverse = offset < 0;
  1211. GlyphTester ignores = ps.getIgnoreDefault();
  1212. int[] counts = ps.getGlyphsAvailable(offset, reverse, ignores);
  1213. int nga = counts[0];
  1214. int ngm = glyphs.length;
  1215. if (nga < ngm) {
  1216. return false; // insufficient glyphs available to match
  1217. } else {
  1218. int[] ga = ps.getGlyphs(offset, ngm, reverse, ignores, null, counts);
  1219. for (int k = 0; k < ngm; k++) {
  1220. if (ga [ k ] != glyphs [ k ]) {
  1221. return false; // match fails at ga [ k ]
  1222. }
  1223. }
  1224. if (rv != null) {
  1225. rv[0] = counts[0] + counts[1];
  1226. }
  1227. return true; // all glyphs match
  1228. }
  1229. }
  1230. }
  1231. private void populate(List entries) {
  1232. if (entries == null) {
  1233. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1234. } else if (entries.size() != 1) {
  1235. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  1236. } else {
  1237. Object o;
  1238. if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
  1239. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1240. } else {
  1241. rsa = (RuleSet[]) o;
  1242. }
  1243. }
  1244. }
  1245. }
  1246. private static class ContextualSubtableFormat2 extends ContextualSubtable {
  1247. private GlyphClassTable cdt; // class def table
  1248. private int ngc; // class set count
  1249. private RuleSet[] rsa; // rule set array, ordered by class number [0...ngc - 1]
  1250. ContextualSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1251. super(id, sequence, flags, format, coverage, entries);
  1252. populate(entries);
  1253. }
  1254. /** {@inheritDoc} */
  1255. public List getEntries() {
  1256. if (rsa != null) {
  1257. List entries = new ArrayList(3);
  1258. entries.add(cdt);
  1259. entries.add(ngc);
  1260. entries.add(rsa);
  1261. return entries;
  1262. } else {
  1263. return null;
  1264. }
  1265. }
  1266. /** {@inheritDoc} */
  1267. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1268. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1269. }
  1270. /** {@inheritDoc} */
  1271. public RuleLookup[] getLookups(int ci, int gi, GlyphPositioningState ps, int[] rv) {
  1272. assert ps != null;
  1273. assert (rv != null) && (rv.length > 0);
  1274. assert rsa != null;
  1275. if (rsa.length > 0) {
  1276. RuleSet rs = rsa [ 0 ];
  1277. if (rs != null) {
  1278. Rule[] ra = rs.getRules();
  1279. for (Rule r : ra) {
  1280. if ((r != null) && (r instanceof ChainedClassSequenceRule)) {
  1281. ChainedClassSequenceRule cr = (ChainedClassSequenceRule) r;
  1282. int[] ca = cr.getClasses(cdt.getClassIndex(gi, ps.getClassMatchSet(gi)));
  1283. if (matches(ps, cdt, ca, 0, rv)) {
  1284. return r.getLookups();
  1285. }
  1286. }
  1287. }
  1288. }
  1289. }
  1290. return null;
  1291. }
  1292. static boolean matches(GlyphPositioningState ps, GlyphClassTable cdt, int[] classes, int offset, int[] rv) {
  1293. if ((cdt == null) || (classes == null) || (classes.length == 0)) {
  1294. return true; // match null class definitions, null or empty class sequence
  1295. } else {
  1296. boolean reverse = offset < 0;
  1297. GlyphTester ignores = ps.getIgnoreDefault();
  1298. int[] counts = ps.getGlyphsAvailable(offset, reverse, ignores);
  1299. int nga = counts[0];
  1300. int ngm = classes.length;
  1301. if (nga < ngm) {
  1302. return false; // insufficient glyphs available to match
  1303. } else {
  1304. int[] ga = ps.getGlyphs(offset, ngm, reverse, ignores, null, counts);
  1305. for (int k = 0; k < ngm; k++) {
  1306. int gi = ga [ k ];
  1307. int ms = ps.getClassMatchSet(gi);
  1308. int gc = cdt.getClassIndex(gi, ms);
  1309. if ((gc < 0) || (gc >= cdt.getClassSize(ms))) {
  1310. return false; // none or invalid class fails mat ch
  1311. } else if (gc != classes [ k ]) {
  1312. return false; // match fails at ga [ k ]
  1313. }
  1314. }
  1315. if (rv != null) {
  1316. rv[0] = counts[0] + counts[1];
  1317. }
  1318. return true; // all glyphs match
  1319. }
  1320. }
  1321. }
  1322. private void populate(List entries) {
  1323. if (entries == null) {
  1324. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1325. } else if (entries.size() != 3) {
  1326. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 3 entries");
  1327. } else {
  1328. Object o;
  1329. if (((o = entries.get(0)) == null) || !(o instanceof GlyphClassTable)) {
  1330. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphClassTable, but is: " + ((o != null) ? o.getClass() : null));
  1331. } else {
  1332. cdt = (GlyphClassTable) o;
  1333. }
  1334. if (((o = entries.get(1)) == null) || !(o instanceof Integer)) {
  1335. throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  1336. } else {
  1337. ngc = (Integer) (o);
  1338. }
  1339. if (((o = entries.get(2)) == null) || !(o instanceof RuleSet[])) {
  1340. throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1341. } else {
  1342. rsa = (RuleSet[]) o;
  1343. if (rsa.length != ngc) {
  1344. throw new AdvancedTypographicTableFormatException("illegal entries, RuleSet[] length is " + rsa.length + ", but expected " + ngc + " glyph classes");
  1345. }
  1346. }
  1347. }
  1348. }
  1349. }
  1350. private static class ContextualSubtableFormat3 extends ContextualSubtable {
  1351. private RuleSet[] rsa; // rule set array, containing a single rule set
  1352. ContextualSubtableFormat3(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1353. super(id, sequence, flags, format, coverage, entries);
  1354. populate(entries);
  1355. }
  1356. /** {@inheritDoc} */
  1357. public List getEntries() {
  1358. if (rsa != null) {
  1359. List entries = new ArrayList(1);
  1360. entries.add(rsa);
  1361. return entries;
  1362. } else {
  1363. return null;
  1364. }
  1365. }
  1366. /** {@inheritDoc} */
  1367. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1368. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1369. }
  1370. /** {@inheritDoc} */
  1371. public RuleLookup[] getLookups(int ci, int gi, GlyphPositioningState ps, int[] rv) {
  1372. assert ps != null;
  1373. assert (rv != null) && (rv.length > 0);
  1374. assert rsa != null;
  1375. if (rsa.length > 0) {
  1376. RuleSet rs = rsa [ 0 ];
  1377. if (rs != null) {
  1378. Rule[] ra = rs.getRules();
  1379. for (Rule r : ra) {
  1380. if ((r != null) && (r instanceof ChainedCoverageSequenceRule)) {
  1381. ChainedCoverageSequenceRule cr = (ChainedCoverageSequenceRule) r;
  1382. GlyphCoverageTable[] gca = cr.getCoverages();
  1383. if (matches(ps, gca, 0, rv)) {
  1384. return r.getLookups();
  1385. }
  1386. }
  1387. }
  1388. }
  1389. }
  1390. return null;
  1391. }
  1392. static boolean matches(GlyphPositioningState ps, GlyphCoverageTable[] gca, int offset, int[] rv) {
  1393. if ((gca == null) || (gca.length == 0)) {
  1394. return true; // match null or empty coverage array
  1395. } else {
  1396. boolean reverse = offset < 0;
  1397. GlyphTester ignores = ps.getIgnoreDefault();
  1398. int[] counts = ps.getGlyphsAvailable(offset, reverse, ignores);
  1399. int nga = counts[0];
  1400. int ngm = gca.length;
  1401. if (nga < ngm) {
  1402. return false; // insufficient glyphs available to match
  1403. } else {
  1404. int[] ga = ps.getGlyphs(offset, ngm, reverse, ignores, null, counts);
  1405. for (int k = 0; k < ngm; k++) {
  1406. GlyphCoverageTable ct = gca [ k ];
  1407. if (ct != null) {
  1408. if (ct.getCoverageIndex(ga [ k ]) < 0) {
  1409. return false; // match fails at ga [ k ]
  1410. }
  1411. }
  1412. }
  1413. if (rv != null) {
  1414. rv[0] = counts[0] + counts[1];
  1415. }
  1416. return true; // all glyphs match
  1417. }
  1418. }
  1419. }
  1420. private void populate(List entries) {
  1421. if (entries == null) {
  1422. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1423. } else if (entries.size() != 1) {
  1424. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  1425. } else {
  1426. Object o;
  1427. if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
  1428. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1429. } else {
  1430. rsa = (RuleSet[]) o;
  1431. }
  1432. }
  1433. }
  1434. }
  1435. private abstract static class ChainedContextualSubtable extends GlyphPositioningSubtable {
  1436. ChainedContextualSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1437. super(id, sequence, flags, format, coverage);
  1438. }
  1439. /** {@inheritDoc} */
  1440. public int getType() {
  1441. return GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
  1442. }
  1443. /** {@inheritDoc} */
  1444. public boolean isCompatible(GlyphSubtable subtable) {
  1445. return subtable instanceof ChainedContextualSubtable;
  1446. }
  1447. /** {@inheritDoc} */
  1448. public boolean position(GlyphPositioningState ps) {
  1449. boolean applied = false;
  1450. int gi = ps.getGlyph();
  1451. int ci;
  1452. if ((ci = getCoverageIndex(gi)) >= 0) {
  1453. int[] rv = new int[1];
  1454. RuleLookup[] la = getLookups(ci, gi, ps, rv);
  1455. if (la != null) {
  1456. ps.apply(la, rv[0]);
  1457. applied = true;
  1458. }
  1459. }
  1460. return applied;
  1461. }
  1462. /**
  1463. * Obtain rule lookups set associated current input glyph context.
  1464. * @param ci coverage index of glyph at current position
  1465. * @param gi glyph index of glyph at current position
  1466. * @param ps glyph positioning state
  1467. * @param rv array of ints used to receive multiple return values, must be of length 1 or greater,
  1468. * where the first entry is used to return the input sequence length of the matched rule
  1469. * @return array of rule lookups or null if none applies
  1470. */
  1471. public abstract RuleLookup[] getLookups(int ci, int gi, GlyphPositioningState ps, int[] rv);
  1472. static GlyphPositioningSubtable create(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1473. if (format == 1) {
  1474. return new ChainedContextualSubtableFormat1(id, sequence, flags, format, coverage, entries);
  1475. } else if (format == 2) {
  1476. return new ChainedContextualSubtableFormat2(id, sequence, flags, format, coverage, entries);
  1477. } else if (format == 3) {
  1478. return new ChainedContextualSubtableFormat3(id, sequence, flags, format, coverage, entries);
  1479. } else {
  1480. throw new UnsupportedOperationException();
  1481. }
  1482. }
  1483. }
  1484. private static class ChainedContextualSubtableFormat1 extends ChainedContextualSubtable {
  1485. private RuleSet[] rsa; // rule set array, ordered by glyph coverage index
  1486. ChainedContextualSubtableFormat1(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1487. super(id, sequence, flags, format, coverage, entries);
  1488. populate(entries);
  1489. }
  1490. /** {@inheritDoc} */
  1491. public List getEntries() {
  1492. if (rsa != null) {
  1493. List entries = new ArrayList(1);
  1494. entries.add(rsa);
  1495. return entries;
  1496. } else {
  1497. return null;
  1498. }
  1499. }
  1500. /** {@inheritDoc} */
  1501. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1502. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1503. }
  1504. /** {@inheritDoc} */
  1505. public RuleLookup[] getLookups(int ci, int gi, GlyphPositioningState ps, int[] rv) {
  1506. assert ps != null;
  1507. assert (rv != null) && (rv.length > 0);
  1508. assert rsa != null;
  1509. if (rsa.length > 0) {
  1510. RuleSet rs = rsa [ 0 ];
  1511. if (rs != null) {
  1512. Rule[] ra = rs.getRules();
  1513. for (Rule r : ra) {
  1514. if ((r != null) && (r instanceof ChainedGlyphSequenceRule)) {
  1515. ChainedGlyphSequenceRule cr = (ChainedGlyphSequenceRule) r;
  1516. int[] iga = cr.getGlyphs(gi);
  1517. if (matches(ps, iga, 0, rv)) {
  1518. int[] bga = cr.getBacktrackGlyphs();
  1519. if (matches(ps, bga, -1, null)) {
  1520. int[] lga = cr.getLookaheadGlyphs();
  1521. if (matches(ps, lga, rv[0], null)) {
  1522. return r.getLookups();
  1523. }
  1524. }
  1525. }
  1526. }
  1527. }
  1528. }
  1529. }
  1530. return null;
  1531. }
  1532. private boolean matches(GlyphPositioningState ps, int[] glyphs, int offset, int[] rv) {
  1533. return ContextualSubtableFormat1.matches(ps, glyphs, offset, rv);
  1534. }
  1535. private void populate(List entries) {
  1536. if (entries == null) {
  1537. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1538. } else if (entries.size() != 1) {
  1539. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  1540. } else {
  1541. Object o;
  1542. if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
  1543. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1544. } else {
  1545. rsa = (RuleSet[]) o;
  1546. }
  1547. }
  1548. }
  1549. }
  1550. private static class ChainedContextualSubtableFormat2 extends ChainedContextualSubtable {
  1551. private GlyphClassTable icdt; // input class def table
  1552. private GlyphClassTable bcdt; // backtrack class def table
  1553. private GlyphClassTable lcdt; // lookahead class def table
  1554. private int ngc; // class set count
  1555. private RuleSet[] rsa; // rule set array, ordered by class number [0...ngc - 1]
  1556. ChainedContextualSubtableFormat2(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1557. super(id, sequence, flags, format, coverage, entries);
  1558. populate(entries);
  1559. }
  1560. /** {@inheritDoc} */
  1561. public List getEntries() {
  1562. if (rsa != null) {
  1563. List entries = new ArrayList(5);
  1564. entries.add(icdt);
  1565. entries.add(bcdt);
  1566. entries.add(lcdt);
  1567. entries.add(ngc);
  1568. entries.add(rsa);
  1569. return entries;
  1570. } else {
  1571. return null;
  1572. }
  1573. }
  1574. /** {@inheritDoc} */
  1575. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1576. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1577. }
  1578. /** {@inheritDoc} */
  1579. public RuleLookup[] getLookups(int ci, int gi, GlyphPositioningState ps, int[] rv) {
  1580. assert ps != null;
  1581. assert (rv != null) && (rv.length > 0);
  1582. assert rsa != null;
  1583. if (rsa.length > 0) {
  1584. RuleSet rs = rsa [ 0 ];
  1585. if (rs != null) {
  1586. Rule[] ra = rs.getRules();
  1587. for (Rule r : ra) {
  1588. if ((r != null) && (r instanceof ChainedClassSequenceRule)) {
  1589. ChainedClassSequenceRule cr = (ChainedClassSequenceRule) r;
  1590. int[] ica = cr.getClasses(icdt.getClassIndex(gi, ps.getClassMatchSet(gi)));
  1591. if (matches(ps, icdt, ica, 0, rv)) {
  1592. int[] bca = cr.getBacktrackClasses();
  1593. if (matches(ps, bcdt, bca, -1, null)) {
  1594. int[] lca = cr.getLookaheadClasses();
  1595. if (matches(ps, lcdt, lca, rv[0], null)) {
  1596. return r.getLookups();
  1597. }
  1598. }
  1599. }
  1600. }
  1601. }
  1602. }
  1603. }
  1604. return null;
  1605. }
  1606. private boolean matches(GlyphPositioningState ps, GlyphClassTable cdt, int[] classes, int offset, int[] rv) {
  1607. return ContextualSubtableFormat2.matches(ps, cdt, classes, offset, rv);
  1608. }
  1609. private void populate(List entries) {
  1610. if (entries == null) {
  1611. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1612. } else if (entries.size() != 5) {
  1613. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 5 entries");
  1614. } else {
  1615. Object o;
  1616. if (((o = entries.get(0)) == null) || !(o instanceof GlyphClassTable)) {
  1617. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an GlyphClassTable, but is: " + ((o != null) ? o.getClass() : null));
  1618. } else {
  1619. icdt = (GlyphClassTable) o;
  1620. }
  1621. if (((o = entries.get(1)) != null) && !(o instanceof GlyphClassTable)) {
  1622. throw new AdvancedTypographicTableFormatException("illegal entries, second entry must be an GlyphClassTable, but is: " + o.getClass());
  1623. } else {
  1624. bcdt = (GlyphClassTable) o;
  1625. }
  1626. if (((o = entries.get(2)) != null) && !(o instanceof GlyphClassTable)) {
  1627. throw new AdvancedTypographicTableFormatException("illegal entries, third entry must be an GlyphClassTable, but is: " + o.getClass());
  1628. } else {
  1629. lcdt = (GlyphClassTable) o;
  1630. }
  1631. if (((o = entries.get(3)) == null) || !(o instanceof Integer)) {
  1632. throw new AdvancedTypographicTableFormatException("illegal entries, fourth entry must be an Integer, but is: " + ((o != null) ? o.getClass() : null));
  1633. } else {
  1634. ngc = (Integer) (o);
  1635. }
  1636. if (((o = entries.get(4)) == null) || !(o instanceof RuleSet[])) {
  1637. throw new AdvancedTypographicTableFormatException("illegal entries, fifth entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1638. } else {
  1639. rsa = (RuleSet[]) o;
  1640. if (rsa.length != ngc) {
  1641. throw new AdvancedTypographicTableFormatException("illegal entries, RuleSet[] length is " + rsa.length + ", but expected " + ngc + " glyph classes");
  1642. }
  1643. }
  1644. }
  1645. }
  1646. }
  1647. private static class ChainedContextualSubtableFormat3 extends ChainedContextualSubtable {
  1648. private RuleSet[] rsa; // rule set array, containing a single rule set
  1649. ChainedContextualSubtableFormat3(String id, int sequence, int flags, int format, GlyphCoverageTable coverage, List entries) {
  1650. super(id, sequence, flags, format, coverage, entries);
  1651. populate(entries);
  1652. }
  1653. /** {@inheritDoc} */
  1654. public List getEntries() {
  1655. if (rsa != null) {
  1656. List entries = new ArrayList(1);
  1657. entries.add(rsa);
  1658. return entries;
  1659. } else {
  1660. return null;
  1661. }
  1662. }
  1663. /** {@inheritDoc} */
  1664. public void resolveLookupReferences(Map/*<String,LookupTable>*/ lookupTables) {
  1665. GlyphTable.resolveLookupReferences(rsa, lookupTables);
  1666. }
  1667. /** {@inheritDoc} */
  1668. public RuleLookup[] getLookups(int ci, int gi, GlyphPositioningState ps, int[] rv) {
  1669. assert ps != null;
  1670. assert (rv != null) && (rv.length > 0);
  1671. assert rsa != null;
  1672. if (rsa.length > 0) {
  1673. RuleSet rs = rsa [ 0 ];
  1674. if (rs != null) {
  1675. Rule[] ra = rs.getRules();
  1676. for (Rule r : ra) {
  1677. if ((r != null) && (r instanceof ChainedCoverageSequenceRule)) {
  1678. ChainedCoverageSequenceRule cr = (ChainedCoverageSequenceRule) r;
  1679. GlyphCoverageTable[] igca = cr.getCoverages();
  1680. if (matches(ps, igca, 0, rv)) {
  1681. GlyphCoverageTable[] bgca = cr.getBacktrackCoverages();
  1682. if (matches(ps, bgca, -1, null)) {
  1683. GlyphCoverageTable[] lgca = cr.getLookaheadCoverages();
  1684. if (matches(ps, lgca, rv[0], null)) {
  1685. return r.getLookups();
  1686. }
  1687. }
  1688. }
  1689. }
  1690. }
  1691. }
  1692. }
  1693. return null;
  1694. }
  1695. private boolean matches(GlyphPositioningState ps, GlyphCoverageTable[] gca, int offset, int[] rv) {
  1696. return ContextualSubtableFormat3.matches(ps, gca, offset, rv);
  1697. }
  1698. private void populate(List entries) {
  1699. if (entries == null) {
  1700. throw new AdvancedTypographicTableFormatException("illegal entries, must be non-null");
  1701. } else if (entries.size() != 1) {
  1702. throw new AdvancedTypographicTableFormatException("illegal entries, " + entries.size() + " entries present, but requires 1 entry");
  1703. } else {
  1704. Object o;
  1705. if (((o = entries.get(0)) == null) || !(o instanceof RuleSet[])) {
  1706. throw new AdvancedTypographicTableFormatException("illegal entries, first entry must be an RuleSet[], but is: " + ((o != null) ? o.getClass() : null));
  1707. } else {
  1708. rsa = (RuleSet[]) o;
  1709. }
  1710. }
  1711. }
  1712. }
  1713. /**
  1714. * The <code>DeviceTable</code> class implements a positioning device table record, comprising
  1715. * adjustments to be made to scaled design units according to the scaled size.
  1716. */
  1717. public static class DeviceTable {
  1718. private final int startSize;
  1719. private final int endSize;
  1720. private final int[] deltas;
  1721. /**
  1722. * Instantiate a DeviceTable.
  1723. * @param startSize the
  1724. * @param endSize the ending (scaled) size
  1725. * @param deltas adjustments for each scaled size
  1726. */
  1727. public DeviceTable(int startSize, int endSize, int[] deltas) {
  1728. assert startSize >= 0;
  1729. assert startSize <= endSize;
  1730. assert deltas != null;
  1731. assert deltas.length == (endSize - startSize) + 1;
  1732. this.startSize = startSize;
  1733. this.endSize = endSize;
  1734. this.deltas = deltas;
  1735. }
  1736. /** @return the start size */
  1737. public int getStartSize() {
  1738. return startSize;
  1739. }
  1740. /** @return the end size */
  1741. public int getEndSize() {
  1742. return endSize;
  1743. }
  1744. /** @return the deltas */
  1745. public int[] getDeltas() {
  1746. return deltas;
  1747. }
  1748. /**
  1749. * Find device adjustment.
  1750. * @param fontSize the font size to search for
  1751. * @return an adjustment if font size matches an entry
  1752. */
  1753. public int findAdjustment(int fontSize) {
  1754. // [TODO] at present, assumes that 1 device unit equals one point
  1755. int fs = fontSize / 1000;
  1756. if (fs < startSize) {
  1757. return 0;
  1758. } else if (fs <= endSize) {
  1759. return deltas [ fs - startSize ] * 1000;
  1760. } else {
  1761. return 0;
  1762. }
  1763. }
  1764. /** {@inheritDoc} */
  1765. public String toString() {
  1766. return "{ start = " + startSize + ", end = " + endSize + ", deltas = " + Arrays.toString(deltas) + "}";
  1767. }
  1768. }
  1769. /**
  1770. * The <code>Value</code> class implements a positioning value record, comprising placement
  1771. * and advancement information in X and Y axes, and optionally including device data used to
  1772. * perform device (grid-fitted) specific fine grain adjustments.
  1773. */
  1774. public static class Value {
  1775. /** X_PLACEMENT value format flag */
  1776. public static final int X_PLACEMENT = 0x0001;
  1777. /** Y_PLACEMENT value format flag */
  1778. public static final int Y_PLACEMENT = 0x0002;
  1779. /** X_ADVANCE value format flag */
  1780. public static final int X_ADVANCE = 0x0004;
  1781. /** Y_ADVANCE value format flag */
  1782. public static final int Y_ADVANCE = 0x0008;
  1783. /** X_PLACEMENT_DEVICE value format flag */
  1784. public static final int X_PLACEMENT_DEVICE = 0x0010;
  1785. /** Y_PLACEMENT_DEVICE value format flag */
  1786. public static final int Y_PLACEMENT_DEVICE = 0x0020;
  1787. /** X_ADVANCE_DEVICE value format flag */
  1788. public static final int X_ADVANCE_DEVICE = 0x0040;
  1789. /** Y_ADVANCE_DEVICE value format flag */
  1790. public static final int Y_ADVANCE_DEVICE = 0x0080;
  1791. /** X_PLACEMENT value index (within adjustments arrays) */
  1792. public static final int IDX_X_PLACEMENT = 0;
  1793. /** Y_PLACEMENT value index (within adjustments arrays) */
  1794. public static final int IDX_Y_PLACEMENT = 1;
  1795. /** X_ADVANCE value index (within adjustments arrays) */
  1796. public static final int IDX_X_ADVANCE = 2;
  1797. /** Y_ADVANCE value index (within adjustments arrays) */
  1798. public static final int IDX_Y_ADVANCE = 3;
  1799. private int xPlacement; // x placement
  1800. private int yPlacement; // y placement
  1801. private int xAdvance; // x advance
  1802. private int yAdvance; // y advance
  1803. private final DeviceTable xPlaDevice; // x placement device table
  1804. private final DeviceTable yPlaDevice; // y placement device table
  1805. private final DeviceTable xAdvDevice; // x advance device table
  1806. private final DeviceTable yAdvDevice; // x advance device table
  1807. /**
  1808. * Instantiate a Value.
  1809. * @param xPlacement the x placement or zero
  1810. * @param yPlacement the y placement or zero
  1811. * @param xAdvance the x advance or zero
  1812. * @param yAdvance the y advance or zero
  1813. * @param xPlaDevice the x placement device table or null
  1814. * @param yPlaDevice the y placement device table or null
  1815. * @param xAdvDevice the x advance device table or null
  1816. * @param yAdvDevice the y advance device table or null
  1817. */
  1818. public Value(int xPlacement, int yPlacement, int xAdvance, int yAdvance, DeviceTable xPlaDevice, DeviceTable yPlaDevice, DeviceTable xAdvDevice, DeviceTable yAdvDevice) {
  1819. this.xPlacement = xPlacement;
  1820. this.yPlacement = yPlacement;
  1821. this.xAdvance = xAdvance;
  1822. this.yAdvance = yAdvance;
  1823. this.xPlaDevice = xPlaDevice;
  1824. this.yPlaDevice = yPlaDevice;
  1825. this.xAdvDevice = xAdvDevice;
  1826. this.yAdvDevice = yAdvDevice;
  1827. }
  1828. /** @return the x placement */
  1829. public int getXPlacement() {
  1830. return xPlacement;
  1831. }
  1832. /** @return the y placement */
  1833. public int getYPlacement() {
  1834. return yPlacement;
  1835. }
  1836. /** @return the x advance */
  1837. public int getXAdvance() {
  1838. return xAdvance;
  1839. }
  1840. /** @return the y advance */
  1841. public int getYAdvance() {
  1842. return yAdvance;
  1843. }
  1844. /** @return the x placement device table */
  1845. public DeviceTable getXPlaDevice() {
  1846. return xPlaDevice;
  1847. }
  1848. /** @return the y placement device table */
  1849. public DeviceTable getYPlaDevice() {
  1850. return yPlaDevice;
  1851. }
  1852. /** @return the x advance device table */
  1853. public DeviceTable getXAdvDevice() {
  1854. return xAdvDevice;
  1855. }
  1856. /** @return the y advance device table */
  1857. public DeviceTable getYAdvDevice() {
  1858. return yAdvDevice;
  1859. }
  1860. /**
  1861. * Apply value to specific adjustments to without use of device table adjustments.
  1862. * @param xPlacement the x placement or zero
  1863. * @param yPlacement the y placement or zero
  1864. * @param xAdvance the x advance or zero
  1865. * @param yAdvance the y advance or zero
  1866. */
  1867. public void adjust(int xPlacement, int yPlacement, int xAdvance, int yAdvance) {
  1868. this.xPlacement += xPlacement;
  1869. this.yPlacement += yPlacement;
  1870. this.xAdvance += xAdvance;
  1871. this.yAdvance += yAdvance;
  1872. }
  1873. /**
  1874. * Apply value to adjustments using font size for device table adjustments.
  1875. * @param adjustments array of four integers containing X,Y placement and X,Y advance adjustments
  1876. * @param fontSize font size for device table adjustments
  1877. * @return true if some adjustment was made
  1878. */
  1879. public boolean adjust(int[] adjustments, int fontSize) {
  1880. boolean adjust = false;
  1881. int dv;
  1882. if ((dv = xPlacement) != 0) {
  1883. adjustments [ IDX_X_PLACEMENT ] += dv;
  1884. adjust = true;
  1885. }
  1886. if ((dv = yPlacement) != 0) {
  1887. adjustments [ IDX_Y_PLACEMENT ] += dv;
  1888. adjust = true;
  1889. }
  1890. if ((dv = xAdvance) != 0) {
  1891. adjustments [ IDX_X_ADVANCE ] += dv;
  1892. adjust = true;
  1893. }
  1894. if ((dv = yAdvance) != 0) {
  1895. adjustments [ IDX_Y_ADVANCE ] += dv;
  1896. adjust = true;
  1897. }
  1898. if (fontSize != 0) {
  1899. DeviceTable dt;
  1900. if ((dt = xPlaDevice) != null) {
  1901. if ((dv = dt.findAdjustment(fontSize)) != 0) {
  1902. adjustments [ IDX_X_PLACEMENT ] += dv;
  1903. adjust = true;
  1904. }
  1905. }
  1906. if ((dt = yPlaDevice) != null) {
  1907. if ((dv = dt.findAdjustment(fontSize)) != 0) {
  1908. adjustments [ IDX_Y_PLACEMENT ] += dv;
  1909. adjust = true;
  1910. }
  1911. }
  1912. if ((dt = xAdvDevice) != null) {
  1913. if ((dv = dt.findAdjustment(fontSize)) != 0) {
  1914. adjustments [ IDX_X_ADVANCE ] += dv;
  1915. adjust = true;
  1916. }
  1917. }
  1918. if ((dt = yAdvDevice) != null) {
  1919. if ((dv = dt.findAdjustment(fontSize)) != 0) {
  1920. adjustments [ IDX_Y_ADVANCE ] += dv;
  1921. adjust = true;
  1922. }
  1923. }
  1924. }
  1925. return adjust;
  1926. }
  1927. /** {@inheritDoc} */
  1928. public String toString() {
  1929. StringBuffer sb = new StringBuffer();
  1930. boolean first = true;
  1931. sb.append("{ ");
  1932. if (xPlacement != 0) {
  1933. if (!first) {
  1934. sb.append(", ");
  1935. } else {
  1936. first = false;
  1937. }
  1938. sb.append("xPlacement = " + xPlacement);
  1939. }
  1940. if (yPlacement != 0) {
  1941. if (!first) {
  1942. sb.append(", ");
  1943. } else {
  1944. first = false;
  1945. }
  1946. sb.append("yPlacement = " + yPlacement);
  1947. }
  1948. if (xAdvance != 0) {
  1949. if (!first) {
  1950. sb.append(", ");
  1951. } else {
  1952. first = false;
  1953. }
  1954. sb.append("xAdvance = " + xAdvance);
  1955. }
  1956. if (yAdvance != 0) {
  1957. if (!first) {
  1958. sb.append(", ");
  1959. } else {
  1960. first = false;
  1961. }
  1962. sb.append("yAdvance = " + yAdvance);
  1963. }
  1964. if (xPlaDevice != null) {
  1965. if (!first) {
  1966. sb.append(", ");
  1967. } else {
  1968. first = false;
  1969. }
  1970. sb.append("xPlaDevice = " + xPlaDevice);
  1971. }
  1972. if (yPlaDevice != null) {
  1973. if (!first) {
  1974. sb.append(", ");
  1975. } else {
  1976. first = false;
  1977. }
  1978. sb.append("xPlaDevice = " + yPlaDevice);
  1979. }
  1980. if (xAdvDevice != null) {
  1981. if (!first) {
  1982. sb.append(", ");
  1983. } else {
  1984. first = false;
  1985. }
  1986. sb.append("xAdvDevice = " + xAdvDevice);
  1987. }
  1988. if (yAdvDevice != null) {
  1989. if (!first) {
  1990. sb.append(", ");
  1991. } else {
  1992. first = false;
  1993. }
  1994. sb.append("xAdvDevice = " + yAdvDevice);
  1995. }
  1996. sb.append(" }");
  1997. return sb.toString();
  1998. }
  1999. }
  2000. /**
  2001. * The <code>PairValues</code> class implements a pair value record, comprising a glyph id (or zero)
  2002. * and two optional positioning values.
  2003. */
  2004. public static class PairValues {
  2005. private final int glyph; // glyph id (or 0)
  2006. private final Value value1; // value for first glyph in pair (or null)
  2007. private final Value value2; // value for second glyph in pair (or null)
  2008. /**
  2009. * Instantiate a PairValues.
  2010. * @param glyph the glyph id (or zero)
  2011. * @param value1 the value of the first glyph in pair (or null)
  2012. * @param value2 the value of the second glyph in pair (or null)
  2013. */
  2014. public PairValues(int glyph, Value value1, Value value2) {
  2015. assert glyph >= 0;
  2016. this.glyph = glyph;
  2017. this.value1 = value1;
  2018. this.value2 = value2;
  2019. }
  2020. /** @return the glyph id */
  2021. public int getGlyph() {
  2022. return glyph;
  2023. }
  2024. /** @return the first value */
  2025. public Value getValue1() {
  2026. return value1;
  2027. }
  2028. /** @return the second value */
  2029. public Value getValue2() {
  2030. return value2;
  2031. }
  2032. /** {@inheritDoc} */
  2033. public String toString() {
  2034. StringBuffer sb = new StringBuffer();
  2035. boolean first = true;
  2036. sb.append("{ ");
  2037. if (glyph != 0) {
  2038. if (!first) {
  2039. sb.append(", ");
  2040. } else {
  2041. first = false;
  2042. }
  2043. sb.append("glyph = " + glyph);
  2044. }
  2045. if (value1 != null) {
  2046. if (!first) {
  2047. sb.append(", ");
  2048. } else {
  2049. first = false;
  2050. }
  2051. sb.append("value1 = " + value1);
  2052. }
  2053. if (value2 != null) {
  2054. if (!first) {
  2055. sb.append(", ");
  2056. } else {
  2057. first = false;
  2058. }
  2059. sb.append("value2 = " + value2);
  2060. }
  2061. sb.append(" }");
  2062. return sb.toString();
  2063. }
  2064. }
  2065. /**
  2066. * The <code>Anchor</code> class implements a anchor record, comprising an X,Y coordinate pair,
  2067. * an optional anchor point index (or -1), and optional X or Y device tables (or null if absent).
  2068. */
  2069. public static class Anchor {
  2070. private final int x; // xCoordinate (in design units)
  2071. private final int y; // yCoordinate (in design units)
  2072. private final int anchorPoint; // anchor point index (or -1)
  2073. private final DeviceTable xDevice; // x device table
  2074. private final DeviceTable yDevice; // y device table
  2075. /**
  2076. * Instantiate an Anchor (format 1).
  2077. * @param x the x coordinate
  2078. * @param y the y coordinate
  2079. */
  2080. public Anchor(int x, int y) {
  2081. this (x, y, -1, null, null);
  2082. }
  2083. /**
  2084. * Instantiate an Anchor (format 2).
  2085. * @param x the x coordinate
  2086. * @param y the y coordinate
  2087. * @param anchorPoint anchor index (or -1)
  2088. */
  2089. public Anchor(int x, int y, int anchorPoint) {
  2090. this (x, y, anchorPoint, null, null);
  2091. }
  2092. /**
  2093. * Instantiate an Anchor (format 3).
  2094. * @param x the x coordinate
  2095. * @param y the y coordinate
  2096. * @param xDevice the x device table (or null if not present)
  2097. * @param yDevice the y device table (or null if not present)
  2098. */
  2099. public Anchor(int x, int y, DeviceTable xDevice, DeviceTable yDevice) {
  2100. this (x, y, -1, xDevice, yDevice);
  2101. }
  2102. /**
  2103. * Instantiate an Anchor based on an existing anchor.
  2104. * @param a the existing anchor
  2105. */
  2106. protected Anchor(Anchor a) {
  2107. this (a.x, a.y, a.anchorPoint, a.xDevice, a.yDevice);
  2108. }
  2109. private Anchor(int x, int y, int anchorPoint, DeviceTable xDevice, DeviceTable yDevice) {
  2110. assert (anchorPoint >= 0) || (anchorPoint == -1);
  2111. this.x = x;
  2112. this.y = y;
  2113. this.anchorPoint = anchorPoint;
  2114. this.xDevice = xDevice;
  2115. this.yDevice = yDevice;
  2116. }
  2117. /** @return the x coordinate */
  2118. public int getX() {
  2119. return x;
  2120. }
  2121. /** @return the y coordinate */
  2122. public int getY() {
  2123. return y;
  2124. }
  2125. /** @return the anchor point index (or -1 if not specified) */
  2126. public int getAnchorPoint() {
  2127. return anchorPoint;
  2128. }
  2129. /** @return the x device table (or null if not specified) */
  2130. public DeviceTable getXDevice() {
  2131. return xDevice;
  2132. }
  2133. /** @return the y device table (or null if not specified) */
  2134. public DeviceTable getYDevice() {
  2135. return yDevice;
  2136. }
  2137. /**
  2138. * Obtain adjustment value required to align the specified anchor
  2139. * with this anchor.
  2140. * @param a the anchor to align
  2141. * @return the adjustment value needed to effect alignment
  2142. */
  2143. public Value getAlignmentAdjustment(Anchor a) {
  2144. assert a != null;
  2145. // TODO - handle anchor point
  2146. // TODO - handle device tables
  2147. return new Value(x - a.x, y - a.y, 0, 0, null, null, null, null);
  2148. }
  2149. /** {@inheritDoc} */
  2150. public String toString() {
  2151. StringBuffer sb = new StringBuffer();
  2152. sb.append("{ [" + x + "," + y + "]");
  2153. if (anchorPoint != -1) {
  2154. sb.append(", anchorPoint = " + anchorPoint);
  2155. }
  2156. if (xDevice != null) {
  2157. sb.append(", xDevice = " + xDevice);
  2158. }
  2159. if (yDevice != null) {
  2160. sb.append(", yDevice = " + yDevice);
  2161. }
  2162. sb.append(" }");
  2163. return sb.toString();
  2164. }
  2165. }
  2166. /**
  2167. * The <code>MarkAnchor</code> class is a subclass of the <code>Anchor</code> class, adding a mark
  2168. * class designation.
  2169. */
  2170. public static class MarkAnchor extends Anchor {
  2171. private final int markClass; // mark class
  2172. /**
  2173. * Instantiate a MarkAnchor
  2174. * @param markClass the mark class
  2175. * @param a the underlying anchor (whose fields are copied)
  2176. */
  2177. public MarkAnchor(int markClass, Anchor a) {
  2178. super(a);
  2179. this.markClass = markClass;
  2180. }
  2181. /** @return the mark class */
  2182. public int getMarkClass() {
  2183. return markClass;
  2184. }
  2185. /** {@inheritDoc} */
  2186. public String toString() {
  2187. return "{ markClass = " + markClass + ", anchor = " + super.toString() + " }";
  2188. }
  2189. }
  2190. }