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

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