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 99KB

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