Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

GlyphPositioningTable.java 100KB

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