You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OTFAdvancedTypographicTableReader.java 160KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804
  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.io.IOException;
  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.fonts.truetype.FontFileReader;
  27. import org.apache.fop.fonts.truetype.TTFDirTabEntry;
  28. import org.apache.fop.fonts.truetype.TTFFile;
  29. import org.apache.fop.fonts.truetype.TTFTableName;
  30. // CSOFF: AvoidNestedBlocksCheck
  31. // CSOFF: NoWhitespaceAfterCheck
  32. // CSOFF: InnerAssignmentCheck
  33. // CSOFF: SimplifyBooleanReturnCheck
  34. // CSOFF: LineLengthCheck
  35. /**
  36. * <p>OpenType Font (OTF) advanced typographic table reader. Used by @{Link org.apache.fop.fonts.truetype.TTFFile}
  37. * to read advanced typographic tables (GDEF, GSUB, GPOS).</p>
  38. *
  39. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  40. */
  41. public final class OTFAdvancedTypographicTableReader {
  42. // logging state
  43. private static Log log = LogFactory.getLog(OTFAdvancedTypographicTableReader.class);
  44. // instance state
  45. private TTFFile ttf; // parent font file reader
  46. private FontFileReader in; // input reader
  47. private GlyphDefinitionTable gdef; // glyph definition table
  48. private GlyphSubstitutionTable gsub; // glyph substitution table
  49. private GlyphPositioningTable gpos; // glyph positioning table
  50. // transient parsing state
  51. private transient Map/*<String,Object[3]>*/ seScripts; // script-tag => Object[3] : { default-language-tag, List(language-tag), seLanguages }
  52. private transient Map/*<String,Object[2]>*/ seLanguages; // language-tag => Object[2] : { "f<required-feature-index>", List("f<feature-index>")
  53. private transient Map/*<String,List<String>>*/ seFeatures; // "f<feature-index>" => Object[2] : { feature-tag, List("lu<lookup-index>") }
  54. private transient GlyphMappingTable seMapping; // subtable entry mappings
  55. private transient List seEntries; // subtable entry entries
  56. private transient List seSubtables; // subtable entry subtables
  57. /**
  58. * Construct an <code>OTFAdvancedTypographicTableReader</code> instance.
  59. * @param ttf parent font file reader (must be non-null)
  60. * @param in font file reader (must be non-null)
  61. */
  62. public OTFAdvancedTypographicTableReader ( TTFFile ttf, FontFileReader in ) {
  63. assert ttf != null;
  64. assert in != null;
  65. this.ttf = ttf;
  66. this.in = in;
  67. }
  68. /**
  69. * Read all advanced typographic tables.
  70. * @throws AdvancedTypographicTableFormatException if ATT table has invalid format
  71. */
  72. public void readAll() throws AdvancedTypographicTableFormatException {
  73. try {
  74. readGDEF();
  75. readGSUB();
  76. readGPOS();
  77. } catch ( AdvancedTypographicTableFormatException e ) {
  78. resetATStateAll();
  79. throw e;
  80. } catch ( IOException e ) {
  81. resetATStateAll();
  82. throw new AdvancedTypographicTableFormatException ( e.getMessage(), e );
  83. } finally {
  84. resetATState();
  85. }
  86. }
  87. /**
  88. * Determine if advanced (typographic) table is present.
  89. * @return true if advanced (typographic) table is present
  90. */
  91. public boolean hasAdvancedTable() {
  92. return ( gdef != null ) || ( gsub != null ) || ( gpos != null );
  93. }
  94. /**
  95. * Returns the GDEF table or null if none present.
  96. * @return the GDEF table
  97. */
  98. public GlyphDefinitionTable getGDEF() {
  99. return gdef;
  100. }
  101. /**
  102. * Returns the GSUB table or null if none present.
  103. * @return the GSUB table
  104. */
  105. public GlyphSubstitutionTable getGSUB() {
  106. return gsub;
  107. }
  108. /**
  109. * Returns the GPOS table or null if none present.
  110. * @return the GPOS table
  111. */
  112. public GlyphPositioningTable getGPOS() {
  113. return gpos;
  114. }
  115. private void readLangSysTable(TTFTableName tableTag, long langSysTable, String langSysTag) throws IOException {
  116. in.seekSet(langSysTable);
  117. if (log.isDebugEnabled()) {
  118. log.debug(tableTag + " lang sys table: " + langSysTag );
  119. }
  120. // read lookup order (reorder) table offset
  121. int lo = in.readTTFUShort();
  122. // read required feature index
  123. int rf = in.readTTFUShort();
  124. String rfi;
  125. if ( rf != 65535 ) {
  126. rfi = "f" + rf;
  127. } else {
  128. rfi = null;
  129. }
  130. // read (non-required) feature count
  131. int nf = in.readTTFUShort();
  132. // dump info if debugging
  133. if (log.isDebugEnabled()) {
  134. log.debug(tableTag + " lang sys table reorder table: " + lo );
  135. log.debug(tableTag + " lang sys table required feature index: " + rf );
  136. log.debug(tableTag + " lang sys table non-required feature count: " + nf );
  137. }
  138. // read (non-required) feature indices
  139. int[] fia = new int[nf];
  140. List fl = new java.util.ArrayList();
  141. for ( int i = 0; i < nf; i++ ) {
  142. int fi = in.readTTFUShort();
  143. if (log.isDebugEnabled()) {
  144. log.debug(tableTag + " lang sys table non-required feature index: " + fi );
  145. }
  146. fia[i] = fi;
  147. fl.add ( "f" + fi );
  148. }
  149. if ( seLanguages == null ) {
  150. seLanguages = new java.util.LinkedHashMap();
  151. }
  152. seLanguages.put ( langSysTag, new Object[] { rfi, fl } );
  153. }
  154. private static String defaultTag = "dflt";
  155. private void readScriptTable(TTFTableName tableTag, long scriptTable, String scriptTag) throws IOException {
  156. in.seekSet(scriptTable);
  157. if (log.isDebugEnabled()) {
  158. log.debug(tableTag + " script table: " + scriptTag );
  159. }
  160. // read default language system table offset
  161. int dl = in.readTTFUShort();
  162. String dt = defaultTag;
  163. if ( dl > 0 ) {
  164. if (log.isDebugEnabled()) {
  165. log.debug(tableTag + " default lang sys tag: " + dt );
  166. log.debug(tableTag + " default lang sys table offset: " + dl );
  167. }
  168. }
  169. // read language system record count
  170. int nl = in.readTTFUShort();
  171. List ll = new java.util.ArrayList();
  172. if ( nl > 0 ) {
  173. String[] lta = new String[nl];
  174. int[] loa = new int[nl];
  175. // read language system records
  176. for ( int i = 0, n = nl; i < n; i++ ) {
  177. String lt = in.readTTFString(4);
  178. int lo = in.readTTFUShort();
  179. if (log.isDebugEnabled()) {
  180. log.debug(tableTag + " lang sys tag: " + lt );
  181. log.debug(tableTag + " lang sys table offset: " + lo );
  182. }
  183. lta[i] = lt;
  184. loa[i] = lo;
  185. if ( dl == lo ) {
  186. dl = 0;
  187. dt = lt;
  188. }
  189. ll.add ( lt );
  190. }
  191. // read non-default language system tables
  192. for ( int i = 0, n = nl; i < n; i++ ) {
  193. readLangSysTable ( tableTag, scriptTable + loa [ i ], lta [ i ] );
  194. }
  195. }
  196. // read default language system table (if specified)
  197. if ( dl > 0 ) {
  198. readLangSysTable ( tableTag, scriptTable + dl, dt );
  199. } else if ( dt != null ) {
  200. if (log.isDebugEnabled()) {
  201. log.debug(tableTag + " lang sys default: " + dt );
  202. }
  203. }
  204. seScripts.put ( scriptTag, new Object[] { dt, ll, seLanguages } );
  205. seLanguages = null;
  206. }
  207. private void readScriptList(TTFTableName tableTag, long scriptList) throws IOException {
  208. in.seekSet(scriptList);
  209. // read script record count
  210. int ns = in.readTTFUShort();
  211. if (log.isDebugEnabled()) {
  212. log.debug(tableTag + " script list record count: " + ns );
  213. }
  214. if ( ns > 0 ) {
  215. String[] sta = new String[ns];
  216. int[] soa = new int[ns];
  217. // read script records
  218. for ( int i = 0, n = ns; i < n; i++ ) {
  219. String st = in.readTTFString(4);
  220. int so = in.readTTFUShort();
  221. if (log.isDebugEnabled()) {
  222. log.debug(tableTag + " script tag: " + st );
  223. log.debug(tableTag + " script table offset: " + so );
  224. }
  225. sta[i] = st;
  226. soa[i] = so;
  227. }
  228. // read script tables
  229. for ( int i = 0, n = ns; i < n; i++ ) {
  230. seLanguages = null;
  231. readScriptTable ( tableTag, scriptList + soa [ i ], sta [ i ] );
  232. }
  233. }
  234. }
  235. private void readFeatureTable(TTFTableName tableTag, long featureTable, String featureTag, int featureIndex) throws IOException {
  236. in.seekSet(featureTable);
  237. if (log.isDebugEnabled()) {
  238. log.debug(tableTag + " feature table: " + featureTag );
  239. }
  240. // read feature params offset
  241. int po = in.readTTFUShort();
  242. // read lookup list indices count
  243. int nl = in.readTTFUShort();
  244. // dump info if debugging
  245. if (log.isDebugEnabled()) {
  246. log.debug(tableTag + " feature table parameters offset: " + po );
  247. log.debug(tableTag + " feature table lookup list index count: " + nl );
  248. }
  249. // read lookup table indices
  250. int[] lia = new int[nl];
  251. List lul = new java.util.ArrayList();
  252. for ( int i = 0; i < nl; i++ ) {
  253. int li = in.readTTFUShort();
  254. if (log.isDebugEnabled()) {
  255. log.debug(tableTag + " feature table lookup index: " + li );
  256. }
  257. lia[i] = li;
  258. lul.add ( "lu" + li );
  259. }
  260. seFeatures.put ( "f" + featureIndex, new Object[] { featureTag, lul } );
  261. }
  262. private void readFeatureList(TTFTableName tableTag, long featureList) throws IOException {
  263. in.seekSet(featureList);
  264. // read feature record count
  265. int nf = in.readTTFUShort();
  266. if (log.isDebugEnabled()) {
  267. log.debug(tableTag + " feature list record count: " + nf );
  268. }
  269. if ( nf > 0 ) {
  270. String[] fta = new String[nf];
  271. int[] foa = new int[nf];
  272. // read feature records
  273. for ( int i = 0, n = nf; i < n; i++ ) {
  274. String ft = in.readTTFString(4);
  275. int fo = in.readTTFUShort();
  276. if (log.isDebugEnabled()) {
  277. log.debug(tableTag + " feature tag: " + ft );
  278. log.debug(tableTag + " feature table offset: " + fo );
  279. }
  280. fta[i] = ft;
  281. foa[i] = fo;
  282. }
  283. // read feature tables
  284. for ( int i = 0, n = nf; i < n; i++ ) {
  285. if (log.isDebugEnabled()) {
  286. log.debug(tableTag + " feature index: " + i );
  287. }
  288. readFeatureTable ( tableTag, featureList + foa [ i ], fta [ i ], i );
  289. }
  290. }
  291. }
  292. static final class GDEFLookupType {
  293. static final int GLYPH_CLASS = 1;
  294. static final int ATTACHMENT_POINT = 2;
  295. static final int LIGATURE_CARET = 3;
  296. static final int MARK_ATTACHMENT = 4;
  297. private GDEFLookupType() {
  298. }
  299. public static int getSubtableType ( int lt ) {
  300. int st;
  301. switch ( lt ) {
  302. case GDEFLookupType.GLYPH_CLASS:
  303. st = GlyphDefinitionTable.GDEF_LOOKUP_TYPE_GLYPH_CLASS;
  304. break;
  305. case GDEFLookupType.ATTACHMENT_POINT:
  306. st = GlyphDefinitionTable.GDEF_LOOKUP_TYPE_ATTACHMENT_POINT;
  307. break;
  308. case GDEFLookupType.LIGATURE_CARET:
  309. st = GlyphDefinitionTable.GDEF_LOOKUP_TYPE_LIGATURE_CARET;
  310. break;
  311. case GDEFLookupType.MARK_ATTACHMENT:
  312. st = GlyphDefinitionTable.GDEF_LOOKUP_TYPE_MARK_ATTACHMENT;
  313. break;
  314. default:
  315. st = -1;
  316. break;
  317. }
  318. return st;
  319. }
  320. public static String toString(int type) {
  321. String s;
  322. switch ( type ) {
  323. case GLYPH_CLASS:
  324. s = "GlyphClass";
  325. break;
  326. case ATTACHMENT_POINT:
  327. s = "AttachmentPoint";
  328. break;
  329. case LIGATURE_CARET:
  330. s = "LigatureCaret";
  331. break;
  332. case MARK_ATTACHMENT:
  333. s = "MarkAttachment";
  334. break;
  335. default:
  336. s = "?";
  337. break;
  338. }
  339. return s;
  340. }
  341. }
  342. static final class GSUBLookupType {
  343. static final int SINGLE = 1;
  344. static final int MULTIPLE = 2;
  345. static final int ALTERNATE = 3;
  346. static final int LIGATURE = 4;
  347. static final int CONTEXTUAL = 5;
  348. static final int CHAINED_CONTEXTUAL = 6;
  349. static final int EXTENSION = 7;
  350. static final int REVERSE_CHAINED_SINGLE = 8;
  351. private GSUBLookupType() {
  352. }
  353. public static int getSubtableType ( int lt ) {
  354. int st;
  355. switch ( lt ) {
  356. case GSUBLookupType.SINGLE:
  357. st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_SINGLE;
  358. break;
  359. case GSUBLookupType.MULTIPLE:
  360. st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_MULTIPLE;
  361. break;
  362. case GSUBLookupType.ALTERNATE:
  363. st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_ALTERNATE;
  364. break;
  365. case GSUBLookupType.LIGATURE:
  366. st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_LIGATURE;
  367. break;
  368. case GSUBLookupType.CONTEXTUAL:
  369. st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_CONTEXTUAL;
  370. break;
  371. case GSUBLookupType.CHAINED_CONTEXTUAL:
  372. st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL;
  373. break;
  374. case GSUBLookupType.EXTENSION:
  375. st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_EXTENSION_SUBSTITUTION;
  376. break;
  377. case GSUBLookupType.REVERSE_CHAINED_SINGLE:
  378. st = GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_REVERSE_CHAINED_SINGLE;
  379. break;
  380. default:
  381. st = -1;
  382. break;
  383. }
  384. return st;
  385. }
  386. public static String toString(int type) {
  387. String s;
  388. switch ( type ) {
  389. case SINGLE:
  390. s = "Single";
  391. break;
  392. case MULTIPLE:
  393. s = "Multiple";
  394. break;
  395. case ALTERNATE:
  396. s = "Alternate";
  397. break;
  398. case LIGATURE:
  399. s = "Ligature";
  400. break;
  401. case CONTEXTUAL:
  402. s = "Contextual";
  403. break;
  404. case CHAINED_CONTEXTUAL:
  405. s = "ChainedContextual";
  406. break;
  407. case EXTENSION:
  408. s = "Extension";
  409. break;
  410. case REVERSE_CHAINED_SINGLE:
  411. s = "ReverseChainedSingle";
  412. break;
  413. default:
  414. s = "?";
  415. break;
  416. }
  417. return s;
  418. }
  419. }
  420. static final class GPOSLookupType {
  421. static final int SINGLE = 1;
  422. static final int PAIR = 2;
  423. static final int CURSIVE = 3;
  424. static final int MARK_TO_BASE = 4;
  425. static final int MARK_TO_LIGATURE = 5;
  426. static final int MARK_TO_MARK = 6;
  427. static final int CONTEXTUAL = 7;
  428. static final int CHAINED_CONTEXTUAL = 8;
  429. static final int EXTENSION = 9;
  430. private GPOSLookupType() {
  431. }
  432. public static String toString(int type) {
  433. String s;
  434. switch ( type ) {
  435. case SINGLE:
  436. s = "Single";
  437. break;
  438. case PAIR:
  439. s = "Pair";
  440. break;
  441. case CURSIVE:
  442. s = "Cursive";
  443. break;
  444. case MARK_TO_BASE:
  445. s = "MarkToBase";
  446. break;
  447. case MARK_TO_LIGATURE:
  448. s = "MarkToLigature";
  449. break;
  450. case MARK_TO_MARK:
  451. s = "MarkToMark";
  452. break;
  453. case CONTEXTUAL:
  454. s = "Contextual";
  455. break;
  456. case CHAINED_CONTEXTUAL:
  457. s = "ChainedContextual";
  458. break;
  459. case EXTENSION:
  460. s = "Extension";
  461. break;
  462. default:
  463. s = "?";
  464. break;
  465. }
  466. return s;
  467. }
  468. }
  469. static final class LookupFlag {
  470. static final int RIGHT_TO_LEFT = 0x0001;
  471. static final int IGNORE_BASE_GLYPHS = 0x0002;
  472. static final int IGNORE_LIGATURE = 0x0004;
  473. static final int IGNORE_MARKS = 0x0008;
  474. static final int USE_MARK_FILTERING_SET = 0x0010;
  475. static final int MARK_ATTACHMENT_TYPE = 0xFF00;
  476. private LookupFlag() {
  477. }
  478. public static String toString(int flags) {
  479. StringBuffer sb = new StringBuffer();
  480. boolean first = true;
  481. if ( ( flags & RIGHT_TO_LEFT ) != 0 ) {
  482. if ( first ) {
  483. first = false;
  484. } else {
  485. sb.append ( '|' );
  486. }
  487. sb.append ( "RightToLeft" );
  488. }
  489. if ( ( flags & IGNORE_BASE_GLYPHS ) != 0 ) {
  490. if ( first ) {
  491. first = false;
  492. } else {
  493. sb.append ( '|' );
  494. }
  495. sb.append ( "IgnoreBaseGlyphs" );
  496. }
  497. if ( ( flags & IGNORE_LIGATURE ) != 0 ) {
  498. if ( first ) {
  499. first = false;
  500. } else {
  501. sb.append ( '|' );
  502. }
  503. sb.append ( "IgnoreLigature" );
  504. }
  505. if ( ( flags & IGNORE_MARKS ) != 0 ) {
  506. if ( first ) {
  507. first = false;
  508. } else {
  509. sb.append ( '|' );
  510. }
  511. sb.append ( "IgnoreMarks" );
  512. }
  513. if ( ( flags & USE_MARK_FILTERING_SET ) != 0 ) {
  514. if ( first ) {
  515. first = false;
  516. } else {
  517. sb.append ( '|' );
  518. }
  519. sb.append ( "UseMarkFilteringSet" );
  520. }
  521. if ( sb.length() == 0 ) {
  522. sb.append ( '-' );
  523. }
  524. return sb.toString();
  525. }
  526. }
  527. private GlyphCoverageTable readCoverageTableFormat1(String label, long tableOffset, int coverageFormat) throws IOException {
  528. List entries = new java.util.ArrayList();
  529. in.seekSet(tableOffset);
  530. // skip over format (already known)
  531. in.skip ( 2 );
  532. // read glyph count
  533. int ng = in.readTTFUShort();
  534. int[] ga = new int[ng];
  535. for ( int i = 0, n = ng; i < n; i++ ) {
  536. int g = in.readTTFUShort();
  537. ga[i] = g;
  538. entries.add ( Integer.valueOf(g) );
  539. }
  540. // dump info if debugging
  541. if (log.isDebugEnabled()) {
  542. log.debug(label + " glyphs: " + toString(ga) );
  543. }
  544. return GlyphCoverageTable.createCoverageTable ( entries );
  545. }
  546. private GlyphCoverageTable readCoverageTableFormat2(String label, long tableOffset, int coverageFormat) throws IOException {
  547. List entries = new java.util.ArrayList();
  548. in.seekSet(tableOffset);
  549. // skip over format (already known)
  550. in.skip ( 2 );
  551. // read range record count
  552. int nr = in.readTTFUShort();
  553. for ( int i = 0, n = nr; i < n; i++ ) {
  554. // read range start
  555. int s = in.readTTFUShort();
  556. // read range end
  557. int e = in.readTTFUShort();
  558. // read range coverage (mapping) index
  559. int m = in.readTTFUShort();
  560. // dump info if debugging
  561. if (log.isDebugEnabled()) {
  562. log.debug(label + " range[" + i + "]: [" + s + "," + e + "]: " + m );
  563. }
  564. entries.add ( new GlyphCoverageTable.MappingRange ( s, e, m ) );
  565. }
  566. return GlyphCoverageTable.createCoverageTable ( entries );
  567. }
  568. private GlyphCoverageTable readCoverageTable(String label, long tableOffset) throws IOException {
  569. GlyphCoverageTable gct;
  570. long cp = in.getCurrentPos();
  571. in.seekSet(tableOffset);
  572. // read coverage table format
  573. int cf = in.readTTFUShort();
  574. if ( cf == 1 ) {
  575. gct = readCoverageTableFormat1 ( label, tableOffset, cf );
  576. } else if ( cf == 2 ) {
  577. gct = readCoverageTableFormat2 ( label, tableOffset, cf );
  578. } else {
  579. throw new AdvancedTypographicTableFormatException ( "unsupported coverage table format: " + cf );
  580. }
  581. in.seekSet ( cp );
  582. return gct;
  583. }
  584. private GlyphClassTable readClassDefTableFormat1(String label, long tableOffset, int classFormat) throws IOException {
  585. List entries = new java.util.ArrayList();
  586. in.seekSet(tableOffset);
  587. // skip over format (already known)
  588. in.skip ( 2 );
  589. // read start glyph
  590. int sg = in.readTTFUShort();
  591. entries.add ( Integer.valueOf(sg) );
  592. // read glyph count
  593. int ng = in.readTTFUShort();
  594. // read glyph classes
  595. int[] ca = new int[ng];
  596. for ( int i = 0, n = ng; i < n; i++ ) {
  597. int gc = in.readTTFUShort();
  598. ca[i] = gc;
  599. entries.add ( Integer.valueOf(gc) );
  600. }
  601. // dump info if debugging
  602. if (log.isDebugEnabled()) {
  603. log.debug(label + " glyph classes: " + toString(ca) );
  604. }
  605. return GlyphClassTable.createClassTable ( entries );
  606. }
  607. private GlyphClassTable readClassDefTableFormat2(String label, long tableOffset, int classFormat) throws IOException {
  608. List entries = new java.util.ArrayList();
  609. in.seekSet(tableOffset);
  610. // skip over format (already known)
  611. in.skip ( 2 );
  612. // read range record count
  613. int nr = in.readTTFUShort();
  614. for ( int i = 0, n = nr; i < n; i++ ) {
  615. // read range start
  616. int s = in.readTTFUShort();
  617. // read range end
  618. int e = in.readTTFUShort();
  619. // read range glyph class (mapping) index
  620. int m = in.readTTFUShort();
  621. // dump info if debugging
  622. if (log.isDebugEnabled()) {
  623. log.debug(label + " range[" + i + "]: [" + s + "," + e + "]: " + m );
  624. }
  625. entries.add ( new GlyphClassTable.MappingRange ( s, e, m ) );
  626. }
  627. return GlyphClassTable.createClassTable ( entries );
  628. }
  629. private GlyphClassTable readClassDefTable(String label, long tableOffset) throws IOException {
  630. GlyphClassTable gct;
  631. long cp = in.getCurrentPos();
  632. in.seekSet(tableOffset);
  633. // read class table format
  634. int cf = in.readTTFUShort();
  635. if ( cf == 1 ) {
  636. gct = readClassDefTableFormat1 ( label, tableOffset, cf );
  637. } else if ( cf == 2 ) {
  638. gct = readClassDefTableFormat2 ( label, tableOffset, cf );
  639. } else {
  640. throw new AdvancedTypographicTableFormatException ( "unsupported class definition table format: " + cf );
  641. }
  642. in.seekSet ( cp );
  643. return gct;
  644. }
  645. private void readSingleSubTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  646. String tableTag = "GSUB";
  647. in.seekSet(subtableOffset);
  648. // skip over format (already known)
  649. in.skip ( 2 );
  650. // read coverage offset
  651. int co = in.readTTFUShort();
  652. // read delta glyph
  653. int dg = in.readTTFShort();
  654. // dump info if debugging
  655. if (log.isDebugEnabled()) {
  656. log.debug(tableTag + " single substitution subtable format: " + subtableFormat + " (delta)" );
  657. log.debug(tableTag + " single substitution coverage table offset: " + co );
  658. log.debug(tableTag + " single substitution delta: " + dg );
  659. }
  660. // read coverage table
  661. seMapping = readCoverageTable ( tableTag + " single substitution coverage", subtableOffset + co );
  662. seEntries.add ( Integer.valueOf ( dg ) );
  663. }
  664. private void readSingleSubTableFormat2(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  665. String tableTag = "GSUB";
  666. in.seekSet(subtableOffset);
  667. // skip over format (already known)
  668. in.skip ( 2 );
  669. // read coverage offset
  670. int co = in.readTTFUShort();
  671. // read glyph count
  672. int ng = in.readTTFUShort();
  673. // dump info if debugging
  674. if (log.isDebugEnabled()) {
  675. log.debug(tableTag + " single substitution subtable format: " + subtableFormat + " (mapped)" );
  676. log.debug(tableTag + " single substitution coverage table offset: " + co );
  677. log.debug(tableTag + " single substitution glyph count: " + ng );
  678. }
  679. // read coverage table
  680. seMapping = readCoverageTable ( tableTag + " single substitution coverage", subtableOffset + co );
  681. // read glyph substitutions
  682. int[] gsa = new int[ng];
  683. for ( int i = 0, n = ng; i < n; i++ ) {
  684. int gs = in.readTTFUShort();
  685. if (log.isDebugEnabled()) {
  686. log.debug(tableTag + " single substitution glyph[" + i + "]: " + gs );
  687. }
  688. gsa[i] = gs;
  689. seEntries.add ( Integer.valueOf ( gs ) );
  690. }
  691. }
  692. private int readSingleSubTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  693. in.seekSet(subtableOffset);
  694. // read substitution subtable format
  695. int sf = in.readTTFUShort();
  696. if ( sf == 1 ) {
  697. readSingleSubTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  698. } else if ( sf == 2 ) {
  699. readSingleSubTableFormat2 ( lookupType, lookupFlags, subtableOffset, sf );
  700. } else {
  701. throw new AdvancedTypographicTableFormatException ( "unsupported single substitution subtable format: " + sf );
  702. }
  703. return sf;
  704. }
  705. private void readMultipleSubTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  706. String tableTag = "GSUB";
  707. in.seekSet(subtableOffset);
  708. // skip over format (already known)
  709. in.skip ( 2 );
  710. // read coverage offset
  711. int co = in.readTTFUShort();
  712. // read sequence count
  713. int ns = in.readTTFUShort();
  714. // dump info if debugging
  715. if (log.isDebugEnabled()) {
  716. log.debug(tableTag + " multiple substitution subtable format: " + subtableFormat + " (mapped)" );
  717. log.debug(tableTag + " multiple substitution coverage table offset: " + co );
  718. log.debug(tableTag + " multiple substitution sequence count: " + ns );
  719. }
  720. // read coverage table
  721. seMapping = readCoverageTable ( tableTag + " multiple substitution coverage", subtableOffset + co );
  722. // read sequence table offsets
  723. int[] soa = new int[ns];
  724. for ( int i = 0, n = ns; i < n; i++ ) {
  725. soa[i] = in.readTTFUShort();
  726. }
  727. // read sequence tables
  728. int[][] gsa = new int [ ns ] [];
  729. for ( int i = 0, n = ns; i < n; i++ ) {
  730. int so = soa[i];
  731. int[] ga;
  732. if ( so > 0 ) {
  733. in.seekSet(subtableOffset + so);
  734. // read glyph count
  735. int ng = in.readTTFUShort();
  736. ga = new int[ng];
  737. for ( int j = 0; j < ng; j++ ) {
  738. ga[j] = in.readTTFUShort();
  739. }
  740. } else {
  741. ga = null;
  742. }
  743. if (log.isDebugEnabled()) {
  744. log.debug(tableTag + " multiple substitution sequence[" + i + "]: " + toString ( ga ) );
  745. }
  746. gsa [ i ] = ga;
  747. }
  748. seEntries.add ( gsa );
  749. }
  750. private int readMultipleSubTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  751. in.seekSet(subtableOffset);
  752. // read substitution subtable format
  753. int sf = in.readTTFUShort();
  754. if ( sf == 1 ) {
  755. readMultipleSubTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  756. } else {
  757. throw new AdvancedTypographicTableFormatException ( "unsupported multiple substitution subtable format: " + sf );
  758. }
  759. return sf;
  760. }
  761. private void readAlternateSubTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  762. String tableTag = "GSUB";
  763. in.seekSet(subtableOffset);
  764. // skip over format (already known)
  765. in.skip ( 2 );
  766. // read coverage offset
  767. int co = in.readTTFUShort();
  768. // read alternate set count
  769. int ns = in.readTTFUShort();
  770. // dump info if debugging
  771. if (log.isDebugEnabled()) {
  772. log.debug(tableTag + " alternate substitution subtable format: " + subtableFormat + " (mapped)" );
  773. log.debug(tableTag + " alternate substitution coverage table offset: " + co );
  774. log.debug(tableTag + " alternate substitution alternate set count: " + ns );
  775. }
  776. // read coverage table
  777. seMapping = readCoverageTable ( tableTag + " alternate substitution coverage", subtableOffset + co );
  778. // read alternate set table offsets
  779. int[] soa = new int[ns];
  780. for ( int i = 0, n = ns; i < n; i++ ) {
  781. soa[i] = in.readTTFUShort();
  782. }
  783. // read alternate set tables
  784. for ( int i = 0, n = ns; i < n; i++ ) {
  785. int so = soa[i];
  786. in.seekSet(subtableOffset + so);
  787. // read glyph count
  788. int ng = in.readTTFUShort();
  789. int[] ga = new int[ng];
  790. for ( int j = 0; j < ng; j++ ) {
  791. int gs = in.readTTFUShort();
  792. ga[j] = gs;
  793. }
  794. if (log.isDebugEnabled()) {
  795. log.debug(tableTag + " alternate substitution alternate set[" + i + "]: " + toString ( ga ) );
  796. }
  797. seEntries.add ( ga );
  798. }
  799. }
  800. private int readAlternateSubTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  801. in.seekSet(subtableOffset);
  802. // read substitution subtable format
  803. int sf = in.readTTFUShort();
  804. if ( sf == 1 ) {
  805. readAlternateSubTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  806. } else {
  807. throw new AdvancedTypographicTableFormatException ( "unsupported alternate substitution subtable format: " + sf );
  808. }
  809. return sf;
  810. }
  811. private void readLigatureSubTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  812. String tableTag = "GSUB";
  813. in.seekSet(subtableOffset);
  814. // skip over format (already known)
  815. in.skip ( 2 );
  816. // read coverage offset
  817. int co = in.readTTFUShort();
  818. // read ligature set count
  819. int ns = in.readTTFUShort();
  820. // dump info if debugging
  821. if (log.isDebugEnabled()) {
  822. log.debug(tableTag + " ligature substitution subtable format: " + subtableFormat + " (mapped)" );
  823. log.debug(tableTag + " ligature substitution coverage table offset: " + co );
  824. log.debug(tableTag + " ligature substitution ligature set count: " + ns );
  825. }
  826. // read coverage table
  827. seMapping = readCoverageTable ( tableTag + " ligature substitution coverage", subtableOffset + co );
  828. // read ligature set table offsets
  829. int[] soa = new int[ns];
  830. for ( int i = 0, n = ns; i < n; i++ ) {
  831. soa[i] = in.readTTFUShort();
  832. }
  833. // read ligature set tables
  834. for ( int i = 0, n = ns; i < n; i++ ) {
  835. int so = soa[i];
  836. in.seekSet(subtableOffset + so);
  837. // read ligature table count
  838. int nl = in.readTTFUShort();
  839. int[] loa = new int[nl];
  840. for ( int j = 0; j < nl; j++ ) {
  841. loa[j] = in.readTTFUShort();
  842. }
  843. List ligs = new java.util.ArrayList();
  844. for ( int j = 0; j < nl; j++ ) {
  845. int lo = loa[j];
  846. in.seekSet(subtableOffset + so + lo);
  847. // read ligature glyph id
  848. int lg = in.readTTFUShort();
  849. // read ligature (input) component count
  850. int nc = in.readTTFUShort();
  851. int[] ca = new int [ nc - 1 ];
  852. // read ligature (input) component glyph ids
  853. for ( int k = 0; k < nc - 1; k++ ) {
  854. ca[k] = in.readTTFUShort();
  855. }
  856. if (log.isDebugEnabled()) {
  857. log.debug(tableTag + " ligature substitution ligature set[" + i + "]: ligature(" + lg + "), components: " + toString ( ca ) );
  858. }
  859. ligs.add ( new GlyphSubstitutionTable.Ligature ( lg, ca ) );
  860. }
  861. seEntries.add ( new GlyphSubstitutionTable.LigatureSet ( ligs ) );
  862. }
  863. }
  864. private int readLigatureSubTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  865. in.seekSet(subtableOffset);
  866. // read substitution subtable format
  867. int sf = in.readTTFUShort();
  868. if ( sf == 1 ) {
  869. readLigatureSubTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  870. } else {
  871. throw new AdvancedTypographicTableFormatException ( "unsupported ligature substitution subtable format: " + sf );
  872. }
  873. return sf;
  874. }
  875. private GlyphTable.RuleLookup[] readRuleLookups(int numLookups, String header) throws IOException {
  876. GlyphTable.RuleLookup[] la = new GlyphTable.RuleLookup [ numLookups ];
  877. for ( int i = 0, n = numLookups; i < n; i++ ) {
  878. int sequenceIndex = in.readTTFUShort();
  879. int lookupIndex = in.readTTFUShort();
  880. la [ i ] = new GlyphTable.RuleLookup ( sequenceIndex, lookupIndex );
  881. // dump info if debugging and header is non-null
  882. if ( log.isDebugEnabled() && ( header != null ) ) {
  883. log.debug(header + "lookup[" + i + "]: " + la[i]);
  884. }
  885. }
  886. return la;
  887. }
  888. private void readContextualSubTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  889. String tableTag = "GSUB";
  890. in.seekSet(subtableOffset);
  891. // skip over format (already known)
  892. in.skip ( 2 );
  893. // read coverage offset
  894. int co = in.readTTFUShort();
  895. // read rule set count
  896. int nrs = in.readTTFUShort();
  897. // read rule set offsets
  898. int[] rsoa = new int [ nrs ];
  899. for ( int i = 0; i < nrs; i++ ) {
  900. rsoa [ i ] = in.readTTFUShort();
  901. }
  902. // dump info if debugging
  903. if (log.isDebugEnabled()) {
  904. log.debug(tableTag + " contextual substitution format: " + subtableFormat + " (glyphs)" );
  905. log.debug(tableTag + " contextual substitution coverage table offset: " + co );
  906. log.debug(tableTag + " contextual substitution rule set count: " + nrs );
  907. for ( int i = 0; i < nrs; i++ ) {
  908. log.debug(tableTag + " contextual substitution rule set offset[" + i + "]: " + rsoa[i] );
  909. }
  910. }
  911. // read coverage table
  912. GlyphCoverageTable ct;
  913. if ( co > 0 ) {
  914. ct = readCoverageTable ( tableTag + " contextual substitution coverage", subtableOffset + co );
  915. } else {
  916. ct = null;
  917. }
  918. // read rule sets
  919. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ nrs ];
  920. String header = null;
  921. for ( int i = 0; i < nrs; i++ ) {
  922. GlyphTable.RuleSet rs;
  923. int rso = rsoa [ i ];
  924. if ( rso > 0 ) {
  925. // seek to rule set [ i ]
  926. in.seekSet ( subtableOffset + rso );
  927. // read rule count
  928. int nr = in.readTTFUShort();
  929. // read rule offsets
  930. int[] roa = new int [ nr ];
  931. GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
  932. for ( int j = 0; j < nr; j++ ) {
  933. roa [ j ] = in.readTTFUShort();
  934. }
  935. // read glyph sequence rules
  936. for ( int j = 0; j < nr; j++ ) {
  937. GlyphTable.GlyphSequenceRule r;
  938. int ro = roa [ j ];
  939. if ( ro > 0 ) {
  940. // seek to rule [ j ]
  941. in.seekSet ( subtableOffset + rso + ro );
  942. // read glyph count
  943. int ng = in.readTTFUShort();
  944. // read rule lookup count
  945. int nl = in.readTTFUShort();
  946. // read glyphs
  947. int[] glyphs = new int [ ng - 1 ];
  948. for ( int k = 0, nk = glyphs.length; k < nk; k++ ) {
  949. glyphs [ k ] = in.readTTFUShort();
  950. }
  951. // read rule lookups
  952. if (log.isDebugEnabled()) {
  953. header = tableTag + " contextual substitution lookups @rule[" + i + "][" + j + "]: ";
  954. }
  955. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  956. r = new GlyphTable.GlyphSequenceRule ( lookups, ng, glyphs );
  957. } else {
  958. r = null;
  959. }
  960. ra [ j ] = r;
  961. }
  962. rs = new GlyphTable.HomogeneousRuleSet ( ra );
  963. } else {
  964. rs = null;
  965. }
  966. rsa [ i ] = rs;
  967. }
  968. // store results
  969. seMapping = ct;
  970. seEntries.add ( rsa );
  971. }
  972. private void readContextualSubTableFormat2(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  973. String tableTag = "GSUB";
  974. in.seekSet(subtableOffset);
  975. // skip over format (already known)
  976. in.skip ( 2 );
  977. // read coverage offset
  978. int co = in.readTTFUShort();
  979. // read class def table offset
  980. int cdo = in.readTTFUShort();
  981. // read class rule set count
  982. int ngc = in.readTTFUShort();
  983. // read class rule set offsets
  984. int[] csoa = new int [ ngc ];
  985. for ( int i = 0; i < ngc; i++ ) {
  986. csoa [ i ] = in.readTTFUShort();
  987. }
  988. // dump info if debugging
  989. if (log.isDebugEnabled()) {
  990. log.debug(tableTag + " contextual substitution format: " + subtableFormat + " (glyph classes)" );
  991. log.debug(tableTag + " contextual substitution coverage table offset: " + co );
  992. log.debug(tableTag + " contextual substitution class set count: " + ngc );
  993. for ( int i = 0; i < ngc; i++ ) {
  994. log.debug(tableTag + " contextual substitution class set offset[" + i + "]: " + csoa[i] );
  995. }
  996. }
  997. // read coverage table
  998. GlyphCoverageTable ct;
  999. if ( co > 0 ) {
  1000. ct = readCoverageTable ( tableTag + " contextual substitution coverage", subtableOffset + co );
  1001. } else {
  1002. ct = null;
  1003. }
  1004. // read class definition table
  1005. GlyphClassTable cdt;
  1006. if ( cdo > 0 ) {
  1007. cdt = readClassDefTable ( tableTag + " contextual substitution class definition", subtableOffset + cdo );
  1008. } else {
  1009. cdt = null;
  1010. }
  1011. // read rule sets
  1012. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ ngc ];
  1013. String header = null;
  1014. for ( int i = 0; i < ngc; i++ ) {
  1015. int cso = csoa [ i ];
  1016. GlyphTable.RuleSet rs;
  1017. if ( cso > 0 ) {
  1018. // seek to rule set [ i ]
  1019. in.seekSet ( subtableOffset + cso );
  1020. // read rule count
  1021. int nr = in.readTTFUShort();
  1022. // read rule offsets
  1023. int[] roa = new int [ nr ];
  1024. GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
  1025. for ( int j = 0; j < nr; j++ ) {
  1026. roa [ j ] = in.readTTFUShort();
  1027. }
  1028. // read glyph sequence rules
  1029. for ( int j = 0; j < nr; j++ ) {
  1030. int ro = roa [ j ];
  1031. GlyphTable.ClassSequenceRule r;
  1032. if ( ro > 0 ) {
  1033. // seek to rule [ j ]
  1034. in.seekSet ( subtableOffset + cso + ro );
  1035. // read glyph count
  1036. int ng = in.readTTFUShort();
  1037. // read rule lookup count
  1038. int nl = in.readTTFUShort();
  1039. // read classes
  1040. int[] classes = new int [ ng - 1 ];
  1041. for ( int k = 0, nk = classes.length; k < nk; k++ ) {
  1042. classes [ k ] = in.readTTFUShort();
  1043. }
  1044. // read rule lookups
  1045. if (log.isDebugEnabled()) {
  1046. header = tableTag + " contextual substitution lookups @rule[" + i + "][" + j + "]: ";
  1047. }
  1048. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  1049. r = new GlyphTable.ClassSequenceRule ( lookups, ng, classes );
  1050. } else {
  1051. assert ro > 0 : "unexpected null subclass rule offset";
  1052. r = null;
  1053. }
  1054. ra [ j ] = r;
  1055. }
  1056. rs = new GlyphTable.HomogeneousRuleSet ( ra );
  1057. } else {
  1058. rs = null;
  1059. }
  1060. rsa [ i ] = rs;
  1061. }
  1062. // store results
  1063. seMapping = ct;
  1064. seEntries.add ( cdt );
  1065. seEntries.add ( Integer.valueOf ( ngc ) );
  1066. seEntries.add ( rsa );
  1067. }
  1068. private void readContextualSubTableFormat3(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1069. String tableTag = "GSUB";
  1070. in.seekSet(subtableOffset);
  1071. // skip over format (already known)
  1072. in.skip ( 2 );
  1073. // read glyph (input sequence length) count
  1074. int ng = in.readTTFUShort();
  1075. // read substitution lookup count
  1076. int nl = in.readTTFUShort();
  1077. // read glyph coverage offsets, one per glyph input sequence length count
  1078. int[] gcoa = new int [ ng ];
  1079. for ( int i = 0; i < ng; i++ ) {
  1080. gcoa [ i ] = in.readTTFUShort();
  1081. }
  1082. // dump info if debugging
  1083. if (log.isDebugEnabled()) {
  1084. log.debug(tableTag + " contextual substitution format: " + subtableFormat + " (glyph sets)" );
  1085. log.debug(tableTag + " contextual substitution glyph input sequence length count: " + ng );
  1086. log.debug(tableTag + " contextual substitution lookup count: " + nl );
  1087. for ( int i = 0; i < ng; i++ ) {
  1088. log.debug(tableTag + " contextual substitution coverage table offset[" + i + "]: " + gcoa[i] );
  1089. }
  1090. }
  1091. // read coverage tables
  1092. GlyphCoverageTable[] gca = new GlyphCoverageTable [ ng ];
  1093. for ( int i = 0; i < ng; i++ ) {
  1094. int gco = gcoa [ i ];
  1095. GlyphCoverageTable gct;
  1096. if ( gco > 0 ) {
  1097. gct = readCoverageTable ( tableTag + " contextual substitution coverage[" + i + "]", subtableOffset + gco );
  1098. } else {
  1099. gct = null;
  1100. }
  1101. gca [ i ] = gct;
  1102. }
  1103. // read rule lookups
  1104. String header = null;
  1105. if (log.isDebugEnabled()) {
  1106. header = tableTag + " contextual substitution lookups: ";
  1107. }
  1108. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  1109. // construct rule, rule set, and rule set array
  1110. GlyphTable.Rule r = new GlyphTable.CoverageSequenceRule ( lookups, ng, gca );
  1111. GlyphTable.RuleSet rs = new GlyphTable.HomogeneousRuleSet ( new GlyphTable.Rule[] {r} );
  1112. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet[] {rs};
  1113. // store results
  1114. assert ( gca != null ) && ( gca.length > 0 );
  1115. seMapping = gca[0];
  1116. seEntries.add ( rsa );
  1117. }
  1118. private int readContextualSubTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  1119. in.seekSet(subtableOffset);
  1120. // read substitution subtable format
  1121. int sf = in.readTTFUShort();
  1122. if ( sf == 1 ) {
  1123. readContextualSubTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  1124. } else if ( sf == 2 ) {
  1125. readContextualSubTableFormat2 ( lookupType, lookupFlags, subtableOffset, sf );
  1126. } else if ( sf == 3 ) {
  1127. readContextualSubTableFormat3 ( lookupType, lookupFlags, subtableOffset, sf );
  1128. } else {
  1129. throw new AdvancedTypographicTableFormatException ( "unsupported contextual substitution subtable format: " + sf );
  1130. }
  1131. return sf;
  1132. }
  1133. private void readChainedContextualSubTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1134. String tableTag = "GSUB";
  1135. in.seekSet(subtableOffset);
  1136. // skip over format (already known)
  1137. in.skip ( 2 );
  1138. // read coverage offset
  1139. int co = in.readTTFUShort();
  1140. // read rule set count
  1141. int nrs = in.readTTFUShort();
  1142. // read rule set offsets
  1143. int[] rsoa = new int [ nrs ];
  1144. for ( int i = 0; i < nrs; i++ ) {
  1145. rsoa [ i ] = in.readTTFUShort();
  1146. }
  1147. // dump info if debugging
  1148. if (log.isDebugEnabled()) {
  1149. log.debug(tableTag + " chained contextual substitution format: " + subtableFormat + " (glyphs)" );
  1150. log.debug(tableTag + " chained contextual substitution coverage table offset: " + co );
  1151. log.debug(tableTag + " chained contextual substitution rule set count: " + nrs );
  1152. for ( int i = 0; i < nrs; i++ ) {
  1153. log.debug(tableTag + " chained contextual substitution rule set offset[" + i + "]: " + rsoa[i] );
  1154. }
  1155. }
  1156. // read coverage table
  1157. GlyphCoverageTable ct;
  1158. if ( co > 0 ) {
  1159. ct = readCoverageTable ( tableTag + " chained contextual substitution coverage", subtableOffset + co );
  1160. } else {
  1161. ct = null;
  1162. }
  1163. // read rule sets
  1164. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ nrs ];
  1165. String header = null;
  1166. for ( int i = 0; i < nrs; i++ ) {
  1167. GlyphTable.RuleSet rs;
  1168. int rso = rsoa [ i ];
  1169. if ( rso > 0 ) {
  1170. // seek to rule set [ i ]
  1171. in.seekSet ( subtableOffset + rso );
  1172. // read rule count
  1173. int nr = in.readTTFUShort();
  1174. // read rule offsets
  1175. int[] roa = new int [ nr ];
  1176. GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
  1177. for ( int j = 0; j < nr; j++ ) {
  1178. roa [ j ] = in.readTTFUShort();
  1179. }
  1180. // read glyph sequence rules
  1181. for ( int j = 0; j < nr; j++ ) {
  1182. GlyphTable.ChainedGlyphSequenceRule r;
  1183. int ro = roa [ j ];
  1184. if ( ro > 0 ) {
  1185. // seek to rule [ j ]
  1186. in.seekSet ( subtableOffset + rso + ro );
  1187. // read backtrack glyph count
  1188. int nbg = in.readTTFUShort();
  1189. // read backtrack glyphs
  1190. int[] backtrackGlyphs = new int [ nbg ];
  1191. for ( int k = 0, nk = backtrackGlyphs.length; k < nk; k++ ) {
  1192. backtrackGlyphs [ k ] = in.readTTFUShort();
  1193. }
  1194. // read input glyph count
  1195. int nig = in.readTTFUShort();
  1196. // read glyphs
  1197. int[] glyphs = new int [ nig - 1 ];
  1198. for ( int k = 0, nk = glyphs.length; k < nk; k++ ) {
  1199. glyphs [ k ] = in.readTTFUShort();
  1200. }
  1201. // read lookahead glyph count
  1202. int nlg = in.readTTFUShort();
  1203. // read lookahead glyphs
  1204. int[] lookaheadGlyphs = new int [ nlg ];
  1205. for ( int k = 0, nk = lookaheadGlyphs.length; k < nk; k++ ) {
  1206. lookaheadGlyphs [ k ] = in.readTTFUShort();
  1207. }
  1208. // read rule lookup count
  1209. int nl = in.readTTFUShort();
  1210. // read rule lookups
  1211. if (log.isDebugEnabled()) {
  1212. header = tableTag + " contextual substitution lookups @rule[" + i + "][" + j + "]: ";
  1213. }
  1214. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  1215. r = new GlyphTable.ChainedGlyphSequenceRule ( lookups, nig, glyphs, backtrackGlyphs, lookaheadGlyphs );
  1216. } else {
  1217. r = null;
  1218. }
  1219. ra [ j ] = r;
  1220. }
  1221. rs = new GlyphTable.HomogeneousRuleSet ( ra );
  1222. } else {
  1223. rs = null;
  1224. }
  1225. rsa [ i ] = rs;
  1226. }
  1227. // store results
  1228. seMapping = ct;
  1229. seEntries.add ( rsa );
  1230. }
  1231. private void readChainedContextualSubTableFormat2(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1232. String tableTag = "GSUB";
  1233. in.seekSet(subtableOffset);
  1234. // skip over format (already known)
  1235. in.skip ( 2 );
  1236. // read coverage offset
  1237. int co = in.readTTFUShort();
  1238. // read backtrack class def table offset
  1239. int bcdo = in.readTTFUShort();
  1240. // read input class def table offset
  1241. int icdo = in.readTTFUShort();
  1242. // read lookahead class def table offset
  1243. int lcdo = in.readTTFUShort();
  1244. // read class set count
  1245. int ngc = in.readTTFUShort();
  1246. // read class set offsets
  1247. int[] csoa = new int [ ngc ];
  1248. for ( int i = 0; i < ngc; i++ ) {
  1249. csoa [ i ] = in.readTTFUShort();
  1250. }
  1251. // dump info if debugging
  1252. if (log.isDebugEnabled()) {
  1253. log.debug(tableTag + " chained contextual substitution format: " + subtableFormat + " (glyph classes)" );
  1254. log.debug(tableTag + " chained contextual substitution coverage table offset: " + co );
  1255. log.debug(tableTag + " chained contextual substitution class set count: " + ngc );
  1256. for ( int i = 0; i < ngc; i++ ) {
  1257. log.debug(tableTag + " chained contextual substitution class set offset[" + i + "]: " + csoa[i] );
  1258. }
  1259. }
  1260. // read coverage table
  1261. GlyphCoverageTable ct;
  1262. if ( co > 0 ) {
  1263. ct = readCoverageTable ( tableTag + " chained contextual substitution coverage", subtableOffset + co );
  1264. } else {
  1265. ct = null;
  1266. }
  1267. // read backtrack class definition table
  1268. GlyphClassTable bcdt;
  1269. if ( bcdo > 0 ) {
  1270. bcdt = readClassDefTable ( tableTag + " contextual substitution backtrack class definition", subtableOffset + bcdo );
  1271. } else {
  1272. bcdt = null;
  1273. }
  1274. // read input class definition table
  1275. GlyphClassTable icdt;
  1276. if ( icdo > 0 ) {
  1277. icdt = readClassDefTable ( tableTag + " contextual substitution input class definition", subtableOffset + icdo );
  1278. } else {
  1279. icdt = null;
  1280. }
  1281. // read lookahead class definition table
  1282. GlyphClassTable lcdt;
  1283. if ( lcdo > 0 ) {
  1284. lcdt = readClassDefTable ( tableTag + " contextual substitution lookahead class definition", subtableOffset + lcdo );
  1285. } else {
  1286. lcdt = null;
  1287. }
  1288. // read rule sets
  1289. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ ngc ];
  1290. String header = null;
  1291. for ( int i = 0; i < ngc; i++ ) {
  1292. int cso = csoa [ i ];
  1293. GlyphTable.RuleSet rs;
  1294. if ( cso > 0 ) {
  1295. // seek to rule set [ i ]
  1296. in.seekSet ( subtableOffset + cso );
  1297. // read rule count
  1298. int nr = in.readTTFUShort();
  1299. // read rule offsets
  1300. int[] roa = new int [ nr ];
  1301. GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
  1302. for ( int j = 0; j < nr; j++ ) {
  1303. roa [ j ] = in.readTTFUShort();
  1304. }
  1305. // read glyph sequence rules
  1306. for ( int j = 0; j < nr; j++ ) {
  1307. int ro = roa [ j ];
  1308. GlyphTable.ChainedClassSequenceRule r;
  1309. if ( ro > 0 ) {
  1310. // seek to rule [ j ]
  1311. in.seekSet ( subtableOffset + cso + ro );
  1312. // read backtrack glyph class count
  1313. int nbc = in.readTTFUShort();
  1314. // read backtrack glyph classes
  1315. int[] backtrackClasses = new int [ nbc ];
  1316. for ( int k = 0, nk = backtrackClasses.length; k < nk; k++ ) {
  1317. backtrackClasses [ k ] = in.readTTFUShort();
  1318. }
  1319. // read input glyph class count
  1320. int nic = in.readTTFUShort();
  1321. // read input glyph classes
  1322. int[] classes = new int [ nic - 1 ];
  1323. for ( int k = 0, nk = classes.length; k < nk; k++ ) {
  1324. classes [ k ] = in.readTTFUShort();
  1325. }
  1326. // read lookahead glyph class count
  1327. int nlc = in.readTTFUShort();
  1328. // read lookahead glyph classes
  1329. int[] lookaheadClasses = new int [ nlc ];
  1330. for ( int k = 0, nk = lookaheadClasses.length; k < nk; k++ ) {
  1331. lookaheadClasses [ k ] = in.readTTFUShort();
  1332. }
  1333. // read rule lookup count
  1334. int nl = in.readTTFUShort();
  1335. // read rule lookups
  1336. if (log.isDebugEnabled()) {
  1337. header = tableTag + " contextual substitution lookups @rule[" + i + "][" + j + "]: ";
  1338. }
  1339. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  1340. r = new GlyphTable.ChainedClassSequenceRule ( lookups, nic, classes, backtrackClasses, lookaheadClasses );
  1341. } else {
  1342. r = null;
  1343. }
  1344. ra [ j ] = r;
  1345. }
  1346. rs = new GlyphTable.HomogeneousRuleSet ( ra );
  1347. } else {
  1348. rs = null;
  1349. }
  1350. rsa [ i ] = rs;
  1351. }
  1352. // store results
  1353. seMapping = ct;
  1354. seEntries.add ( icdt );
  1355. seEntries.add ( bcdt );
  1356. seEntries.add ( lcdt );
  1357. seEntries.add ( Integer.valueOf ( ngc ) );
  1358. seEntries.add ( rsa );
  1359. }
  1360. private void readChainedContextualSubTableFormat3(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1361. String tableTag = "GSUB";
  1362. in.seekSet(subtableOffset);
  1363. // skip over format (already known)
  1364. in.skip ( 2 );
  1365. // read backtrack glyph count
  1366. int nbg = in.readTTFUShort();
  1367. // read backtrack glyph coverage offsets
  1368. int[] bgcoa = new int [ nbg ];
  1369. for ( int i = 0; i < nbg; i++ ) {
  1370. bgcoa [ i ] = in.readTTFUShort();
  1371. }
  1372. // read input glyph count
  1373. int nig = in.readTTFUShort();
  1374. // read input glyph coverage offsets
  1375. int[] igcoa = new int [ nig ];
  1376. for ( int i = 0; i < nig; i++ ) {
  1377. igcoa [ i ] = in.readTTFUShort();
  1378. }
  1379. // read lookahead glyph count
  1380. int nlg = in.readTTFUShort();
  1381. // read lookahead glyph coverage offsets
  1382. int[] lgcoa = new int [ nlg ];
  1383. for ( int i = 0; i < nlg; i++ ) {
  1384. lgcoa [ i ] = in.readTTFUShort();
  1385. }
  1386. // read substitution lookup count
  1387. int nl = in.readTTFUShort();
  1388. // dump info if debugging
  1389. if (log.isDebugEnabled()) {
  1390. log.debug(tableTag + " chained contextual substitution format: " + subtableFormat + " (glyph sets)" );
  1391. log.debug(tableTag + " chained contextual substitution backtrack glyph count: " + nbg );
  1392. for ( int i = 0; i < nbg; i++ ) {
  1393. log.debug(tableTag + " chained contextual substitution backtrack coverage table offset[" + i + "]: " + bgcoa[i] );
  1394. }
  1395. log.debug(tableTag + " chained contextual substitution input glyph count: " + nig );
  1396. for ( int i = 0; i < nig; i++ ) {
  1397. log.debug(tableTag + " chained contextual substitution input coverage table offset[" + i + "]: " + igcoa[i] );
  1398. }
  1399. log.debug(tableTag + " chained contextual substitution lookahead glyph count: " + nlg );
  1400. for ( int i = 0; i < nlg; i++ ) {
  1401. log.debug(tableTag + " chained contextual substitution lookahead coverage table offset[" + i + "]: " + lgcoa[i] );
  1402. }
  1403. log.debug(tableTag + " chained contextual substitution lookup count: " + nl );
  1404. }
  1405. // read backtrack coverage tables
  1406. GlyphCoverageTable[] bgca = new GlyphCoverageTable[nbg];
  1407. for ( int i = 0; i < nbg; i++ ) {
  1408. int bgco = bgcoa [ i ];
  1409. GlyphCoverageTable bgct;
  1410. if ( bgco > 0 ) {
  1411. bgct = readCoverageTable ( tableTag + " chained contextual substitution backtrack coverage[" + i + "]", subtableOffset + bgco );
  1412. } else {
  1413. bgct = null;
  1414. }
  1415. bgca[i] = bgct;
  1416. }
  1417. // read input coverage tables
  1418. GlyphCoverageTable[] igca = new GlyphCoverageTable[nig];
  1419. for ( int i = 0; i < nig; i++ ) {
  1420. int igco = igcoa [ i ];
  1421. GlyphCoverageTable igct;
  1422. if ( igco > 0 ) {
  1423. igct = readCoverageTable ( tableTag + " chained contextual substitution input coverage[" + i + "]", subtableOffset + igco );
  1424. } else {
  1425. igct = null;
  1426. }
  1427. igca[i] = igct;
  1428. }
  1429. // read lookahead coverage tables
  1430. GlyphCoverageTable[] lgca = new GlyphCoverageTable[nlg];
  1431. for ( int i = 0; i < nlg; i++ ) {
  1432. int lgco = lgcoa [ i ];
  1433. GlyphCoverageTable lgct;
  1434. if ( lgco > 0 ) {
  1435. lgct = readCoverageTable ( tableTag + " chained contextual substitution lookahead coverage[" + i + "]", subtableOffset + lgco );
  1436. } else {
  1437. lgct = null;
  1438. }
  1439. lgca[i] = lgct;
  1440. }
  1441. // read rule lookups
  1442. String header = null;
  1443. if (log.isDebugEnabled()) {
  1444. header = tableTag + " chained contextual substitution lookups: ";
  1445. }
  1446. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  1447. // construct rule, rule set, and rule set array
  1448. GlyphTable.Rule r = new GlyphTable.ChainedCoverageSequenceRule ( lookups, nig, igca, bgca, lgca );
  1449. GlyphTable.RuleSet rs = new GlyphTable.HomogeneousRuleSet ( new GlyphTable.Rule[] {r} );
  1450. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet[] {rs};
  1451. // store results
  1452. assert ( igca != null ) && ( igca.length > 0 );
  1453. seMapping = igca[0];
  1454. seEntries.add ( rsa );
  1455. }
  1456. private int readChainedContextualSubTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  1457. in.seekSet(subtableOffset);
  1458. // read substitution subtable format
  1459. int sf = in.readTTFUShort();
  1460. if ( sf == 1 ) {
  1461. readChainedContextualSubTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  1462. } else if ( sf == 2 ) {
  1463. readChainedContextualSubTableFormat2 ( lookupType, lookupFlags, subtableOffset, sf );
  1464. } else if ( sf == 3 ) {
  1465. readChainedContextualSubTableFormat3 ( lookupType, lookupFlags, subtableOffset, sf );
  1466. } else {
  1467. throw new AdvancedTypographicTableFormatException ( "unsupported chained contextual substitution subtable format: " + sf );
  1468. }
  1469. return sf;
  1470. }
  1471. private void readExtensionSubTableFormat1(int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset, int subtableFormat) throws IOException {
  1472. String tableTag = "GSUB";
  1473. in.seekSet(subtableOffset);
  1474. // skip over format (already known)
  1475. in.skip ( 2 );
  1476. // read extension lookup type
  1477. int lt = in.readTTFUShort();
  1478. // read extension offset
  1479. long eo = in.readTTFULong();
  1480. // dump info if debugging
  1481. if (log.isDebugEnabled()) {
  1482. log.debug(tableTag + " extension substitution subtable format: " + subtableFormat );
  1483. log.debug(tableTag + " extension substitution lookup type: " + lt );
  1484. log.debug(tableTag + " extension substitution lookup table offset: " + eo );
  1485. }
  1486. // read referenced subtable from extended offset
  1487. readGSUBSubtable ( lt, lookupFlags, lookupSequence, subtableSequence, subtableOffset + eo );
  1488. }
  1489. private int readExtensionSubTable(int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset) throws IOException {
  1490. in.seekSet(subtableOffset);
  1491. // read substitution subtable format
  1492. int sf = in.readTTFUShort();
  1493. if ( sf == 1 ) {
  1494. readExtensionSubTableFormat1 ( lookupType, lookupFlags, lookupSequence, subtableSequence, subtableOffset, sf );
  1495. } else {
  1496. throw new AdvancedTypographicTableFormatException ( "unsupported extension substitution subtable format: " + sf );
  1497. }
  1498. return sf;
  1499. }
  1500. private void readReverseChainedSingleSubTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1501. String tableTag = "GSUB";
  1502. in.seekSet(subtableOffset);
  1503. // skip over format (already known)
  1504. in.skip ( 2 );
  1505. // read coverage offset
  1506. int co = in.readTTFUShort();
  1507. // read backtrack glyph count
  1508. int nbg = in.readTTFUShort();
  1509. // read backtrack glyph coverage offsets
  1510. int[] bgcoa = new int [ nbg ];
  1511. for ( int i = 0; i < nbg; i++ ) {
  1512. bgcoa [ i ] = in.readTTFUShort();
  1513. }
  1514. // read lookahead glyph count
  1515. int nlg = in.readTTFUShort();
  1516. // read backtrack glyph coverage offsets
  1517. int[] lgcoa = new int [ nlg ];
  1518. for ( int i = 0; i < nlg; i++ ) {
  1519. lgcoa [ i ] = in.readTTFUShort();
  1520. }
  1521. // read substitution (output) glyph count
  1522. int ng = in.readTTFUShort();
  1523. // read substitution (output) glyphs
  1524. int[] glyphs = new int [ ng ];
  1525. for ( int i = 0, n = ng; i < n; i++ ) {
  1526. glyphs [ i ] = in.readTTFUShort();
  1527. }
  1528. // dump info if debugging
  1529. if (log.isDebugEnabled()) {
  1530. log.debug(tableTag + " reverse chained contextual substitution format: " + subtableFormat );
  1531. log.debug(tableTag + " reverse chained contextual substitution coverage table offset: " + co );
  1532. log.debug(tableTag + " reverse chained contextual substitution backtrack glyph count: " + nbg );
  1533. for ( int i = 0; i < nbg; i++ ) {
  1534. log.debug(tableTag + " reverse chained contextual substitution backtrack coverage table offset[" + i + "]: " + bgcoa[i] );
  1535. }
  1536. log.debug(tableTag + " reverse chained contextual substitution lookahead glyph count: " + nlg );
  1537. for ( int i = 0; i < nlg; i++ ) {
  1538. log.debug(tableTag + " reverse chained contextual substitution lookahead coverage table offset[" + i + "]: " + lgcoa[i] );
  1539. }
  1540. log.debug(tableTag + " reverse chained contextual substitution glyphs: " + toString(glyphs) );
  1541. }
  1542. // read coverage table
  1543. GlyphCoverageTable ct = readCoverageTable ( tableTag + " reverse chained contextual substitution coverage", subtableOffset + co );
  1544. // read backtrack coverage tables
  1545. GlyphCoverageTable[] bgca = new GlyphCoverageTable[nbg];
  1546. for ( int i = 0; i < nbg; i++ ) {
  1547. int bgco = bgcoa[i];
  1548. GlyphCoverageTable bgct;
  1549. if ( bgco > 0 ) {
  1550. bgct = readCoverageTable ( tableTag + " reverse chained contextual substitution backtrack coverage[" + i + "]", subtableOffset + bgco );
  1551. } else {
  1552. bgct = null;
  1553. }
  1554. bgca[i] = bgct;
  1555. }
  1556. // read lookahead coverage tables
  1557. GlyphCoverageTable[] lgca = new GlyphCoverageTable[nlg];
  1558. for ( int i = 0; i < nlg; i++ ) {
  1559. int lgco = lgcoa[i];
  1560. GlyphCoverageTable lgct;
  1561. if ( lgco > 0 ) {
  1562. lgct = readCoverageTable ( tableTag + " reverse chained contextual substitution lookahead coverage[" + i + "]", subtableOffset + lgco );
  1563. } else {
  1564. lgct = null;
  1565. }
  1566. lgca[i] = lgct;
  1567. }
  1568. // store results
  1569. seMapping = ct;
  1570. seEntries.add ( bgca );
  1571. seEntries.add ( lgca );
  1572. seEntries.add ( glyphs );
  1573. }
  1574. private int readReverseChainedSingleSubTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  1575. in.seekSet(subtableOffset);
  1576. // read substitution subtable format
  1577. int sf = in.readTTFUShort();
  1578. if ( sf == 1 ) {
  1579. readReverseChainedSingleSubTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  1580. } else {
  1581. throw new AdvancedTypographicTableFormatException ( "unsupported reverse chained single substitution subtable format: " + sf );
  1582. }
  1583. return sf;
  1584. }
  1585. private void readGSUBSubtable(int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset) throws IOException {
  1586. initATSubState();
  1587. int subtableFormat = -1;
  1588. switch ( lookupType ) {
  1589. case GSUBLookupType.SINGLE:
  1590. subtableFormat = readSingleSubTable ( lookupType, lookupFlags, subtableOffset );
  1591. break;
  1592. case GSUBLookupType.MULTIPLE:
  1593. subtableFormat = readMultipleSubTable ( lookupType, lookupFlags, subtableOffset );
  1594. break;
  1595. case GSUBLookupType.ALTERNATE:
  1596. subtableFormat = readAlternateSubTable ( lookupType, lookupFlags, subtableOffset );
  1597. break;
  1598. case GSUBLookupType.LIGATURE:
  1599. subtableFormat = readLigatureSubTable ( lookupType, lookupFlags, subtableOffset );
  1600. break;
  1601. case GSUBLookupType.CONTEXTUAL:
  1602. subtableFormat = readContextualSubTable ( lookupType, lookupFlags, subtableOffset );
  1603. break;
  1604. case GSUBLookupType.CHAINED_CONTEXTUAL:
  1605. subtableFormat = readChainedContextualSubTable ( lookupType, lookupFlags, subtableOffset );
  1606. break;
  1607. case GSUBLookupType.REVERSE_CHAINED_SINGLE:
  1608. subtableFormat = readReverseChainedSingleSubTable ( lookupType, lookupFlags, subtableOffset );
  1609. break;
  1610. case GSUBLookupType.EXTENSION:
  1611. subtableFormat = readExtensionSubTable ( lookupType, lookupFlags, lookupSequence, subtableSequence, subtableOffset );
  1612. break;
  1613. default:
  1614. break;
  1615. }
  1616. extractSESubState ( GlyphTable.GLYPH_TABLE_TYPE_SUBSTITUTION, lookupType, lookupFlags, lookupSequence, subtableSequence, subtableFormat );
  1617. resetATSubState();
  1618. }
  1619. private GlyphPositioningTable.DeviceTable readPosDeviceTable(long subtableOffset, long deviceTableOffset) throws IOException {
  1620. long cp = in.getCurrentPos();
  1621. in.seekSet(subtableOffset + deviceTableOffset);
  1622. // read start size
  1623. int ss = in.readTTFUShort();
  1624. // read end size
  1625. int es = in.readTTFUShort();
  1626. // read delta format
  1627. int df = in.readTTFUShort();
  1628. int s1;
  1629. int m1;
  1630. int dm;
  1631. int dd;
  1632. int s2;
  1633. if ( df == 1 ) {
  1634. s1 = 14;
  1635. m1 = 0x3;
  1636. dm = 1;
  1637. dd = 4;
  1638. s2 = 2;
  1639. } else if ( df == 2 ) {
  1640. s1 = 12;
  1641. m1 = 0xF;
  1642. dm = 7;
  1643. dd = 16;
  1644. s2 = 4;
  1645. } else if ( df == 3 ) {
  1646. s1 = 8;
  1647. m1 = 0xFF;
  1648. dm = 127;
  1649. dd = 256;
  1650. s2 = 8;
  1651. } else {
  1652. log.debug ( "unsupported device table delta format: " + df + ", ignoring device table" );
  1653. return null;
  1654. }
  1655. // read deltas
  1656. int n = ( es - ss ) + 1;
  1657. if ( n < 0 ) {
  1658. log.debug ( "invalid device table delta count: " + n + ", ignoring device table" );
  1659. return null;
  1660. }
  1661. int[] da = new int [ n ];
  1662. for ( int i = 0; ( i < n ) && ( s2 > 0 );) {
  1663. int p = in.readTTFUShort();
  1664. for ( int j = 0, k = 16 / s2; j < k; j++ ) {
  1665. int d = ( p >> s1 ) & m1;
  1666. if ( d > dm ) {
  1667. d -= dd;
  1668. }
  1669. if ( i < n ) {
  1670. da [ i++ ] = d;
  1671. } else {
  1672. break;
  1673. }
  1674. p <<= s2;
  1675. }
  1676. }
  1677. in.seekSet(cp);
  1678. return new GlyphPositioningTable.DeviceTable ( ss, es, da );
  1679. }
  1680. private GlyphPositioningTable.Value readPosValue(long subtableOffset, int valueFormat) throws IOException {
  1681. // XPlacement
  1682. int xp;
  1683. if ( ( valueFormat & GlyphPositioningTable.Value.X_PLACEMENT ) != 0 ) {
  1684. xp = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1685. } else {
  1686. xp = 0;
  1687. }
  1688. // YPlacement
  1689. int yp;
  1690. if ( ( valueFormat & GlyphPositioningTable.Value.Y_PLACEMENT ) != 0 ) {
  1691. yp = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1692. } else {
  1693. yp = 0;
  1694. }
  1695. // XAdvance
  1696. int xa;
  1697. if ( ( valueFormat & GlyphPositioningTable.Value.X_ADVANCE ) != 0 ) {
  1698. xa = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1699. } else {
  1700. xa = 0;
  1701. }
  1702. // YAdvance
  1703. int ya;
  1704. if ( ( valueFormat & GlyphPositioningTable.Value.Y_ADVANCE ) != 0 ) {
  1705. ya = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1706. } else {
  1707. ya = 0;
  1708. }
  1709. // XPlaDevice
  1710. GlyphPositioningTable.DeviceTable xpd;
  1711. if ( ( valueFormat & GlyphPositioningTable.Value.X_PLACEMENT_DEVICE ) != 0 ) {
  1712. int xpdo = in.readTTFUShort();
  1713. xpd = readPosDeviceTable ( subtableOffset, xpdo );
  1714. } else {
  1715. xpd = null;
  1716. }
  1717. // YPlaDevice
  1718. GlyphPositioningTable.DeviceTable ypd;
  1719. if ( ( valueFormat & GlyphPositioningTable.Value.Y_PLACEMENT_DEVICE ) != 0 ) {
  1720. int ypdo = in.readTTFUShort();
  1721. ypd = readPosDeviceTable ( subtableOffset, ypdo );
  1722. } else {
  1723. ypd = null;
  1724. }
  1725. // XAdvDevice
  1726. GlyphPositioningTable.DeviceTable xad;
  1727. if ( ( valueFormat & GlyphPositioningTable.Value.X_ADVANCE_DEVICE ) != 0 ) {
  1728. int xado = in.readTTFUShort();
  1729. xad = readPosDeviceTable ( subtableOffset, xado );
  1730. } else {
  1731. xad = null;
  1732. }
  1733. // YAdvDevice
  1734. GlyphPositioningTable.DeviceTable yad;
  1735. if ( ( valueFormat & GlyphPositioningTable.Value.Y_ADVANCE_DEVICE ) != 0 ) {
  1736. int yado = in.readTTFUShort();
  1737. yad = readPosDeviceTable ( subtableOffset, yado );
  1738. } else {
  1739. yad = null;
  1740. }
  1741. return new GlyphPositioningTable.Value ( xp, yp, xa, ya, xpd, ypd, xad, yad );
  1742. }
  1743. private void readSinglePosTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1744. String tableTag = "GPOS";
  1745. in.seekSet(subtableOffset);
  1746. // skip over format (already known)
  1747. in.skip ( 2 );
  1748. // read coverage offset
  1749. int co = in.readTTFUShort();
  1750. // read value format
  1751. int vf = in.readTTFUShort();
  1752. // read value
  1753. GlyphPositioningTable.Value v = readPosValue ( subtableOffset, vf );
  1754. // dump info if debugging
  1755. if (log.isDebugEnabled()) {
  1756. log.debug(tableTag + " single positioning subtable format: " + subtableFormat + " (delta)" );
  1757. log.debug(tableTag + " single positioning coverage table offset: " + co );
  1758. log.debug(tableTag + " single positioning value: " + v );
  1759. }
  1760. // read coverage table
  1761. GlyphCoverageTable ct = readCoverageTable ( tableTag + " single positioning coverage", subtableOffset + co );
  1762. // store results
  1763. seMapping = ct;
  1764. seEntries.add ( v );
  1765. }
  1766. private void readSinglePosTableFormat2(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1767. String tableTag = "GPOS";
  1768. in.seekSet(subtableOffset);
  1769. // skip over format (already known)
  1770. in.skip ( 2 );
  1771. // read coverage offset
  1772. int co = in.readTTFUShort();
  1773. // read value format
  1774. int vf = in.readTTFUShort();
  1775. // read value count
  1776. int nv = in.readTTFUShort();
  1777. // dump info if debugging
  1778. if (log.isDebugEnabled()) {
  1779. log.debug(tableTag + " single positioning subtable format: " + subtableFormat + " (mapped)" );
  1780. log.debug(tableTag + " single positioning coverage table offset: " + co );
  1781. log.debug(tableTag + " single positioning value count: " + nv );
  1782. }
  1783. // read coverage table
  1784. GlyphCoverageTable ct = readCoverageTable ( tableTag + " single positioning coverage", subtableOffset + co );
  1785. // read positioning values
  1786. GlyphPositioningTable.Value[] pva = new GlyphPositioningTable.Value[nv];
  1787. for ( int i = 0, n = nv; i < n; i++ ) {
  1788. GlyphPositioningTable.Value pv = readPosValue ( subtableOffset, vf );
  1789. if (log.isDebugEnabled()) {
  1790. log.debug(tableTag + " single positioning value[" + i + "]: " + pv );
  1791. }
  1792. pva[i] = pv;
  1793. }
  1794. // store results
  1795. seMapping = ct;
  1796. seEntries.add ( pva );
  1797. }
  1798. private int readSinglePosTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  1799. in.seekSet(subtableOffset);
  1800. // read positionining subtable format
  1801. int sf = in.readTTFUShort();
  1802. if ( sf == 1 ) {
  1803. readSinglePosTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  1804. } else if ( sf == 2 ) {
  1805. readSinglePosTableFormat2 ( lookupType, lookupFlags, subtableOffset, sf );
  1806. } else {
  1807. throw new AdvancedTypographicTableFormatException ( "unsupported single positioning subtable format: " + sf );
  1808. }
  1809. return sf;
  1810. }
  1811. private GlyphPositioningTable.PairValues readPosPairValues(long subtableOffset, boolean hasGlyph, int vf1, int vf2) throws IOException {
  1812. // read glyph (if present)
  1813. int glyph;
  1814. if ( hasGlyph ) {
  1815. glyph = in.readTTFUShort();
  1816. } else {
  1817. glyph = 0;
  1818. }
  1819. // read first value (if present)
  1820. GlyphPositioningTable.Value v1;
  1821. if ( vf1 != 0 ) {
  1822. v1 = readPosValue ( subtableOffset, vf1 );
  1823. } else {
  1824. v1 = null;
  1825. }
  1826. // read second value (if present)
  1827. GlyphPositioningTable.Value v2;
  1828. if ( vf2 != 0 ) {
  1829. v2 = readPosValue ( subtableOffset, vf2 );
  1830. } else {
  1831. v2 = null;
  1832. }
  1833. return new GlyphPositioningTable.PairValues ( glyph, v1, v2 );
  1834. }
  1835. private GlyphPositioningTable.PairValues[] readPosPairSetTable(long subtableOffset, int pairSetTableOffset, int vf1, int vf2) throws IOException {
  1836. String tableTag = "GPOS";
  1837. long cp = in.getCurrentPos();
  1838. in.seekSet(subtableOffset + pairSetTableOffset);
  1839. // read pair values count
  1840. int npv = in.readTTFUShort();
  1841. // dump info if debugging
  1842. if (log.isDebugEnabled()) {
  1843. log.debug(tableTag + " pair set table offset: " + pairSetTableOffset );
  1844. log.debug(tableTag + " pair set table values count: " + npv );
  1845. }
  1846. // read pair values
  1847. GlyphPositioningTable.PairValues[] pva = new GlyphPositioningTable.PairValues [ npv ];
  1848. for ( int i = 0, n = npv; i < n; i++ ) {
  1849. GlyphPositioningTable.PairValues pv = readPosPairValues ( subtableOffset, true, vf1, vf2 );
  1850. pva [ i ] = pv;
  1851. if (log.isDebugEnabled()) {
  1852. log.debug(tableTag + " pair set table value[" + i + "]: " + pv);
  1853. }
  1854. }
  1855. in.seekSet(cp);
  1856. return pva;
  1857. }
  1858. private void readPairPosTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1859. String tableTag = "GPOS";
  1860. in.seekSet(subtableOffset);
  1861. // skip over format (already known)
  1862. in.skip ( 2 );
  1863. // read coverage offset
  1864. int co = in.readTTFUShort();
  1865. // read value format for first glyph
  1866. int vf1 = in.readTTFUShort();
  1867. // read value format for second glyph
  1868. int vf2 = in.readTTFUShort();
  1869. // read number (count) of pair sets
  1870. int nps = in.readTTFUShort();
  1871. // dump info if debugging
  1872. if (log.isDebugEnabled()) {
  1873. log.debug(tableTag + " pair positioning subtable format: " + subtableFormat + " (glyphs)" );
  1874. log.debug(tableTag + " pair positioning coverage table offset: " + co );
  1875. log.debug(tableTag + " pair positioning value format #1: " + vf1 );
  1876. log.debug(tableTag + " pair positioning value format #2: " + vf2 );
  1877. }
  1878. // read coverage table
  1879. GlyphCoverageTable ct = readCoverageTable ( tableTag + " pair positioning coverage", subtableOffset + co );
  1880. // read pair value matrix
  1881. GlyphPositioningTable.PairValues[][] pvm = new GlyphPositioningTable.PairValues [ nps ][];
  1882. for ( int i = 0, n = nps; i < n; i++ ) {
  1883. // read pair set offset
  1884. int pso = in.readTTFUShort();
  1885. // read pair set table at offset
  1886. pvm [ i ] = readPosPairSetTable ( subtableOffset, pso, vf1, vf2 );
  1887. }
  1888. // store results
  1889. seMapping = ct;
  1890. seEntries.add ( pvm );
  1891. }
  1892. private void readPairPosTableFormat2(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  1893. String tableTag = "GPOS";
  1894. in.seekSet(subtableOffset);
  1895. // skip over format (already known)
  1896. in.skip ( 2 );
  1897. // read coverage offset
  1898. int co = in.readTTFUShort();
  1899. // read value format for first glyph
  1900. int vf1 = in.readTTFUShort();
  1901. // read value format for second glyph
  1902. int vf2 = in.readTTFUShort();
  1903. // read class def 1 offset
  1904. int cd1o = in.readTTFUShort();
  1905. // read class def 2 offset
  1906. int cd2o = in.readTTFUShort();
  1907. // read number (count) of classes in class def 1 table
  1908. int nc1 = in.readTTFUShort();
  1909. // read number (count) of classes in class def 2 table
  1910. int nc2 = in.readTTFUShort();
  1911. // dump info if debugging
  1912. if (log.isDebugEnabled()) {
  1913. log.debug(tableTag + " pair positioning subtable format: " + subtableFormat + " (glyph classes)" );
  1914. log.debug(tableTag + " pair positioning coverage table offset: " + co );
  1915. log.debug(tableTag + " pair positioning value format #1: " + vf1 );
  1916. log.debug(tableTag + " pair positioning value format #2: " + vf2 );
  1917. log.debug(tableTag + " pair positioning class def table #1 offset: " + cd1o );
  1918. log.debug(tableTag + " pair positioning class def table #2 offset: " + cd2o );
  1919. log.debug(tableTag + " pair positioning class #1 count: " + nc1 );
  1920. log.debug(tableTag + " pair positioning class #2 count: " + nc2 );
  1921. }
  1922. // read coverage table
  1923. GlyphCoverageTable ct = readCoverageTable ( tableTag + " pair positioning coverage", subtableOffset + co );
  1924. // read class definition table #1
  1925. GlyphClassTable cdt1 = readClassDefTable ( tableTag + " pair positioning class definition #1", subtableOffset + cd1o );
  1926. // read class definition table #2
  1927. GlyphClassTable cdt2 = readClassDefTable ( tableTag + " pair positioning class definition #2", subtableOffset + cd2o );
  1928. // read pair value matrix
  1929. GlyphPositioningTable.PairValues[][] pvm = new GlyphPositioningTable.PairValues [ nc1 ] [ nc2 ];
  1930. for ( int i = 0; i < nc1; i++ ) {
  1931. for ( int j = 0; j < nc2; j++ ) {
  1932. GlyphPositioningTable.PairValues pv = readPosPairValues ( subtableOffset, false, vf1, vf2 );
  1933. pvm [ i ] [ j ] = pv;
  1934. if (log.isDebugEnabled()) {
  1935. log.debug(tableTag + " pair set table value[" + i + "][" + j + "]: " + pv);
  1936. }
  1937. }
  1938. }
  1939. // store results
  1940. seMapping = ct;
  1941. seEntries.add ( cdt1 );
  1942. seEntries.add ( cdt2 );
  1943. seEntries.add ( Integer.valueOf ( nc1 ) );
  1944. seEntries.add ( Integer.valueOf ( nc2 ) );
  1945. seEntries.add ( pvm );
  1946. }
  1947. private int readPairPosTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  1948. in.seekSet(subtableOffset);
  1949. // read positioning subtable format
  1950. int sf = in.readTTFUShort();
  1951. if ( sf == 1 ) {
  1952. readPairPosTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  1953. } else if ( sf == 2 ) {
  1954. readPairPosTableFormat2 ( lookupType, lookupFlags, subtableOffset, sf );
  1955. } else {
  1956. throw new AdvancedTypographicTableFormatException ( "unsupported pair positioning subtable format: " + sf );
  1957. }
  1958. return sf;
  1959. }
  1960. private GlyphPositioningTable.Anchor readPosAnchor(long anchorTableOffset) throws IOException {
  1961. GlyphPositioningTable.Anchor a;
  1962. long cp = in.getCurrentPos();
  1963. in.seekSet(anchorTableOffset);
  1964. // read anchor table format
  1965. int af = in.readTTFUShort();
  1966. if ( af == 1 ) {
  1967. // read x coordinate
  1968. int x = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1969. // read y coordinate
  1970. int y = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1971. a = new GlyphPositioningTable.Anchor ( x, y );
  1972. } else if ( af == 2 ) {
  1973. // read x coordinate
  1974. int x = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1975. // read y coordinate
  1976. int y = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1977. // read anchor point index
  1978. int ap = in.readTTFUShort();
  1979. a = new GlyphPositioningTable.Anchor ( x, y, ap );
  1980. } else if ( af == 3 ) {
  1981. // read x coordinate
  1982. int x = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1983. // read y coordinate
  1984. int y = ttf.convertTTFUnit2PDFUnit ( in.readTTFShort() );
  1985. // read x device table offset
  1986. int xdo = in.readTTFUShort();
  1987. // read y device table offset
  1988. int ydo = in.readTTFUShort();
  1989. // read x device table (if present)
  1990. GlyphPositioningTable.DeviceTable xd;
  1991. if ( xdo != 0 ) {
  1992. xd = readPosDeviceTable ( cp, xdo );
  1993. } else {
  1994. xd = null;
  1995. }
  1996. // read y device table (if present)
  1997. GlyphPositioningTable.DeviceTable yd;
  1998. if ( ydo != 0 ) {
  1999. yd = readPosDeviceTable ( cp, ydo );
  2000. } else {
  2001. yd = null;
  2002. }
  2003. a = new GlyphPositioningTable.Anchor ( x, y, xd, yd );
  2004. } else {
  2005. throw new AdvancedTypographicTableFormatException ( "unsupported positioning anchor format: " + af );
  2006. }
  2007. in.seekSet(cp);
  2008. return a;
  2009. }
  2010. private void readCursivePosTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2011. String tableTag = "GPOS";
  2012. in.seekSet(subtableOffset);
  2013. // skip over format (already known)
  2014. in.skip ( 2 );
  2015. // read coverage offset
  2016. int co = in.readTTFUShort();
  2017. // read entry/exit count
  2018. int ec = in.readTTFUShort();
  2019. // dump info if debugging
  2020. if (log.isDebugEnabled()) {
  2021. log.debug(tableTag + " cursive positioning subtable format: " + subtableFormat );
  2022. log.debug(tableTag + " cursive positioning coverage table offset: " + co );
  2023. log.debug(tableTag + " cursive positioning entry/exit count: " + ec );
  2024. }
  2025. // read coverage table
  2026. GlyphCoverageTable ct = readCoverageTable ( tableTag + " cursive positioning coverage", subtableOffset + co );
  2027. // read entry/exit records
  2028. GlyphPositioningTable.Anchor[] aa = new GlyphPositioningTable.Anchor [ ec * 2 ];
  2029. for ( int i = 0, n = ec; i < n; i++ ) {
  2030. // read entry anchor offset
  2031. int eno = in.readTTFUShort();
  2032. // read exit anchor offset
  2033. int exo = in.readTTFUShort();
  2034. // read entry anchor
  2035. GlyphPositioningTable.Anchor ena;
  2036. if ( eno > 0 ) {
  2037. ena = readPosAnchor ( subtableOffset + eno );
  2038. } else {
  2039. ena = null;
  2040. }
  2041. // read exit anchor
  2042. GlyphPositioningTable.Anchor exa;
  2043. if ( exo > 0 ) {
  2044. exa = readPosAnchor ( subtableOffset + exo );
  2045. } else {
  2046. exa = null;
  2047. }
  2048. aa [ ( i * 2 ) + 0 ] = ena;
  2049. aa [ ( i * 2 ) + 1 ] = exa;
  2050. if (log.isDebugEnabled()) {
  2051. if ( ena != null ) {
  2052. log.debug(tableTag + " cursive entry anchor [" + i + "]: " + ena );
  2053. }
  2054. if ( exa != null ) {
  2055. log.debug(tableTag + " cursive exit anchor [" + i + "]: " + exa );
  2056. }
  2057. }
  2058. }
  2059. // store results
  2060. seMapping = ct;
  2061. seEntries.add ( aa );
  2062. }
  2063. private int readCursivePosTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  2064. in.seekSet(subtableOffset);
  2065. // read positioning subtable format
  2066. int sf = in.readTTFUShort();
  2067. if ( sf == 1 ) {
  2068. readCursivePosTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  2069. } else {
  2070. throw new AdvancedTypographicTableFormatException ( "unsupported cursive positioning subtable format: " + sf );
  2071. }
  2072. return sf;
  2073. }
  2074. private void readMarkToBasePosTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2075. String tableTag = "GPOS";
  2076. in.seekSet(subtableOffset);
  2077. // skip over format (already known)
  2078. in.skip ( 2 );
  2079. // read mark coverage offset
  2080. int mco = in.readTTFUShort();
  2081. // read base coverage offset
  2082. int bco = in.readTTFUShort();
  2083. // read mark class count
  2084. int nmc = in.readTTFUShort();
  2085. // read mark array offset
  2086. int mao = in.readTTFUShort();
  2087. // read base array offset
  2088. int bao = in.readTTFUShort();
  2089. // dump info if debugging
  2090. if (log.isDebugEnabled()) {
  2091. log.debug(tableTag + " mark-to-base positioning subtable format: " + subtableFormat );
  2092. log.debug(tableTag + " mark-to-base positioning mark coverage table offset: " + mco );
  2093. log.debug(tableTag + " mark-to-base positioning base coverage table offset: " + bco );
  2094. log.debug(tableTag + " mark-to-base positioning mark class count: " + nmc );
  2095. log.debug(tableTag + " mark-to-base positioning mark array offset: " + mao );
  2096. log.debug(tableTag + " mark-to-base positioning base array offset: " + bao );
  2097. }
  2098. // read mark coverage table
  2099. GlyphCoverageTable mct = readCoverageTable ( tableTag + " mark-to-base positioning mark coverage", subtableOffset + mco );
  2100. // read base coverage table
  2101. GlyphCoverageTable bct = readCoverageTable ( tableTag + " mark-to-base positioning base coverage", subtableOffset + bco );
  2102. // read mark anchor array
  2103. // seek to mark array
  2104. in.seekSet(subtableOffset + mao);
  2105. // read mark count
  2106. int nm = in.readTTFUShort();
  2107. if (log.isDebugEnabled()) {
  2108. log.debug(tableTag + " mark-to-base positioning mark count: " + nm );
  2109. }
  2110. // read mark anchor array, where i:{0...markCount}
  2111. GlyphPositioningTable.MarkAnchor[] maa = new GlyphPositioningTable.MarkAnchor [ nm ];
  2112. for ( int i = 0; i < nm; i++ ) {
  2113. // read mark class
  2114. int mc = in.readTTFUShort();
  2115. // read mark anchor offset
  2116. int ao = in.readTTFUShort();
  2117. GlyphPositioningTable.Anchor a;
  2118. if ( ao > 0 ) {
  2119. a = readPosAnchor ( subtableOffset + mao + ao );
  2120. } else {
  2121. a = null;
  2122. }
  2123. GlyphPositioningTable.MarkAnchor ma;
  2124. if ( a != null ) {
  2125. ma = new GlyphPositioningTable.MarkAnchor ( mc, a );
  2126. } else {
  2127. ma = null;
  2128. }
  2129. maa [ i ] = ma;
  2130. if (log.isDebugEnabled()) {
  2131. log.debug(tableTag + " mark-to-base positioning mark anchor[" + i + "]: " + ma);
  2132. }
  2133. }
  2134. // read base anchor matrix
  2135. // seek to base array
  2136. in.seekSet(subtableOffset + bao);
  2137. // read base count
  2138. int nb = in.readTTFUShort();
  2139. if (log.isDebugEnabled()) {
  2140. log.debug(tableTag + " mark-to-base positioning base count: " + nb );
  2141. }
  2142. // read anchor matrix, where i:{0...baseCount - 1}, j:{0...markClassCount - 1}
  2143. GlyphPositioningTable.Anchor[][] bam = new GlyphPositioningTable.Anchor [ nb ] [ nmc ];
  2144. for ( int i = 0; i < nb; i++ ) {
  2145. for ( int j = 0; j < nmc; j++ ) {
  2146. // read base anchor offset
  2147. int ao = in.readTTFUShort();
  2148. GlyphPositioningTable.Anchor a;
  2149. if ( ao > 0 ) {
  2150. a = readPosAnchor ( subtableOffset + bao + ao );
  2151. } else {
  2152. a = null;
  2153. }
  2154. bam [ i ] [ j ] = a;
  2155. if (log.isDebugEnabled()) {
  2156. log.debug(tableTag + " mark-to-base positioning base anchor[" + i + "][" + j + "]: " + a);
  2157. }
  2158. }
  2159. }
  2160. // store results
  2161. seMapping = mct;
  2162. seEntries.add ( bct );
  2163. seEntries.add ( Integer.valueOf ( nmc ) );
  2164. seEntries.add ( maa );
  2165. seEntries.add ( bam );
  2166. }
  2167. private int readMarkToBasePosTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  2168. in.seekSet(subtableOffset);
  2169. // read positioning subtable format
  2170. int sf = in.readTTFUShort();
  2171. if ( sf == 1 ) {
  2172. readMarkToBasePosTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  2173. } else {
  2174. throw new AdvancedTypographicTableFormatException ( "unsupported mark-to-base positioning subtable format: " + sf );
  2175. }
  2176. return sf;
  2177. }
  2178. private void readMarkToLigaturePosTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2179. String tableTag = "GPOS";
  2180. in.seekSet(subtableOffset);
  2181. // skip over format (already known)
  2182. in.skip ( 2 );
  2183. // read mark coverage offset
  2184. int mco = in.readTTFUShort();
  2185. // read ligature coverage offset
  2186. int lco = in.readTTFUShort();
  2187. // read mark class count
  2188. int nmc = in.readTTFUShort();
  2189. // read mark array offset
  2190. int mao = in.readTTFUShort();
  2191. // read ligature array offset
  2192. int lao = in.readTTFUShort();
  2193. // dump info if debugging
  2194. if (log.isDebugEnabled()) {
  2195. log.debug(tableTag + " mark-to-ligature positioning subtable format: " + subtableFormat );
  2196. log.debug(tableTag + " mark-to-ligature positioning mark coverage table offset: " + mco );
  2197. log.debug(tableTag + " mark-to-ligature positioning ligature coverage table offset: " + lco );
  2198. log.debug(tableTag + " mark-to-ligature positioning mark class count: " + nmc );
  2199. log.debug(tableTag + " mark-to-ligature positioning mark array offset: " + mao );
  2200. log.debug(tableTag + " mark-to-ligature positioning ligature array offset: " + lao );
  2201. }
  2202. // read mark coverage table
  2203. GlyphCoverageTable mct = readCoverageTable ( tableTag + " mark-to-ligature positioning mark coverage", subtableOffset + mco );
  2204. // read ligature coverage table
  2205. GlyphCoverageTable lct = readCoverageTable ( tableTag + " mark-to-ligature positioning ligature coverage", subtableOffset + lco );
  2206. // read mark anchor array
  2207. // seek to mark array
  2208. in.seekSet(subtableOffset + mao);
  2209. // read mark count
  2210. int nm = in.readTTFUShort();
  2211. if (log.isDebugEnabled()) {
  2212. log.debug(tableTag + " mark-to-ligature positioning mark count: " + nm );
  2213. }
  2214. // read mark anchor array, where i:{0...markCount}
  2215. GlyphPositioningTable.MarkAnchor[] maa = new GlyphPositioningTable.MarkAnchor [ nm ];
  2216. for ( int i = 0; i < nm; i++ ) {
  2217. // read mark class
  2218. int mc = in.readTTFUShort();
  2219. // read mark anchor offset
  2220. int ao = in.readTTFUShort();
  2221. GlyphPositioningTable.Anchor a;
  2222. if ( ao > 0 ) {
  2223. a = readPosAnchor ( subtableOffset + mao + ao );
  2224. } else {
  2225. a = null;
  2226. }
  2227. GlyphPositioningTable.MarkAnchor ma;
  2228. if ( a != null ) {
  2229. ma = new GlyphPositioningTable.MarkAnchor ( mc, a );
  2230. } else {
  2231. ma = null;
  2232. }
  2233. maa [ i ] = ma;
  2234. if (log.isDebugEnabled()) {
  2235. log.debug(tableTag + " mark-to-ligature positioning mark anchor[" + i + "]: " + ma);
  2236. }
  2237. }
  2238. // read ligature anchor matrix
  2239. // seek to ligature array
  2240. in.seekSet(subtableOffset + lao);
  2241. // read ligature count
  2242. int nl = in.readTTFUShort();
  2243. if (log.isDebugEnabled()) {
  2244. log.debug(tableTag + " mark-to-ligature positioning ligature count: " + nl );
  2245. }
  2246. // read ligature attach table offsets
  2247. int[] laoa = new int [ nl ];
  2248. for ( int i = 0; i < nl; i++ ) {
  2249. laoa [ i ] = in.readTTFUShort();
  2250. }
  2251. // iterate over ligature attach tables, recording maximum component count
  2252. int mxc = 0;
  2253. for ( int i = 0; i < nl; i++ ) {
  2254. int lato = laoa [ i ];
  2255. in.seekSet ( subtableOffset + lao + lato );
  2256. // read component count
  2257. int cc = in.readTTFUShort();
  2258. if ( cc > mxc ) {
  2259. mxc = cc;
  2260. }
  2261. }
  2262. if (log.isDebugEnabled()) {
  2263. log.debug(tableTag + " mark-to-ligature positioning maximum component count: " + mxc );
  2264. }
  2265. // read anchor matrix, where i:{0...ligatureCount - 1}, j:{0...maxComponentCount - 1}, k:{0...markClassCount - 1}
  2266. GlyphPositioningTable.Anchor[][][] lam = new GlyphPositioningTable.Anchor [ nl ][][];
  2267. for ( int i = 0; i < nl; i++ ) {
  2268. int lato = laoa [ i ];
  2269. // seek to ligature attach table for ligature[i]
  2270. in.seekSet ( subtableOffset + lao + lato );
  2271. // read component count
  2272. int cc = in.readTTFUShort();
  2273. GlyphPositioningTable.Anchor[][] lcm = new GlyphPositioningTable.Anchor [ cc ] [ nmc ];
  2274. for ( int j = 0; j < cc; j++ ) {
  2275. for ( int k = 0; k < nmc; k++ ) {
  2276. // read ligature anchor offset
  2277. int ao = in.readTTFUShort();
  2278. GlyphPositioningTable.Anchor a;
  2279. if ( ao > 0 ) {
  2280. a = readPosAnchor ( subtableOffset + lao + lato + ao );
  2281. } else {
  2282. a = null;
  2283. }
  2284. lcm [ j ] [ k ] = a;
  2285. if (log.isDebugEnabled()) {
  2286. log.debug(tableTag + " mark-to-ligature positioning ligature anchor[" + i + "][" + j + "][" + k + "]: " + a);
  2287. }
  2288. }
  2289. }
  2290. lam [ i ] = lcm;
  2291. }
  2292. // store results
  2293. seMapping = mct;
  2294. seEntries.add ( lct );
  2295. seEntries.add ( Integer.valueOf ( nmc ) );
  2296. seEntries.add ( Integer.valueOf ( mxc ) );
  2297. seEntries.add ( maa );
  2298. seEntries.add ( lam );
  2299. }
  2300. private int readMarkToLigaturePosTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  2301. in.seekSet(subtableOffset);
  2302. // read positioning subtable format
  2303. int sf = in.readTTFUShort();
  2304. if ( sf == 1 ) {
  2305. readMarkToLigaturePosTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  2306. } else {
  2307. throw new AdvancedTypographicTableFormatException ( "unsupported mark-to-ligature positioning subtable format: " + sf );
  2308. }
  2309. return sf;
  2310. }
  2311. private void readMarkToMarkPosTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2312. String tableTag = "GPOS";
  2313. in.seekSet(subtableOffset);
  2314. // skip over format (already known)
  2315. in.skip ( 2 );
  2316. // read mark #1 coverage offset
  2317. int m1co = in.readTTFUShort();
  2318. // read mark #2 coverage offset
  2319. int m2co = in.readTTFUShort();
  2320. // read mark class count
  2321. int nmc = in.readTTFUShort();
  2322. // read mark #1 array offset
  2323. int m1ao = in.readTTFUShort();
  2324. // read mark #2 array offset
  2325. int m2ao = in.readTTFUShort();
  2326. // dump info if debugging
  2327. if (log.isDebugEnabled()) {
  2328. log.debug(tableTag + " mark-to-mark positioning subtable format: " + subtableFormat );
  2329. log.debug(tableTag + " mark-to-mark positioning mark #1 coverage table offset: " + m1co );
  2330. log.debug(tableTag + " mark-to-mark positioning mark #2 coverage table offset: " + m2co );
  2331. log.debug(tableTag + " mark-to-mark positioning mark class count: " + nmc );
  2332. log.debug(tableTag + " mark-to-mark positioning mark #1 array offset: " + m1ao );
  2333. log.debug(tableTag + " mark-to-mark positioning mark #2 array offset: " + m2ao );
  2334. }
  2335. // read mark #1 coverage table
  2336. GlyphCoverageTable mct1 = readCoverageTable ( tableTag + " mark-to-mark positioning mark #1 coverage", subtableOffset + m1co );
  2337. // read mark #2 coverage table
  2338. GlyphCoverageTable mct2 = readCoverageTable ( tableTag + " mark-to-mark positioning mark #2 coverage", subtableOffset + m2co );
  2339. // read mark #1 anchor array
  2340. // seek to mark array
  2341. in.seekSet(subtableOffset + m1ao);
  2342. // read mark count
  2343. int nm1 = in.readTTFUShort();
  2344. if (log.isDebugEnabled()) {
  2345. log.debug(tableTag + " mark-to-mark positioning mark #1 count: " + nm1 );
  2346. }
  2347. // read mark anchor array, where i:{0...mark1Count}
  2348. GlyphPositioningTable.MarkAnchor[] maa = new GlyphPositioningTable.MarkAnchor [ nm1 ];
  2349. for ( int i = 0; i < nm1; i++ ) {
  2350. // read mark class
  2351. int mc = in.readTTFUShort();
  2352. // read mark anchor offset
  2353. int ao = in.readTTFUShort();
  2354. GlyphPositioningTable.Anchor a;
  2355. if ( ao > 0 ) {
  2356. a = readPosAnchor ( subtableOffset + m1ao + ao );
  2357. } else {
  2358. a = null;
  2359. }
  2360. GlyphPositioningTable.MarkAnchor ma;
  2361. if ( a != null ) {
  2362. ma = new GlyphPositioningTable.MarkAnchor ( mc, a );
  2363. } else {
  2364. ma = null;
  2365. }
  2366. maa [ i ] = ma;
  2367. if (log.isDebugEnabled()) {
  2368. log.debug(tableTag + " mark-to-mark positioning mark #1 anchor[" + i + "]: " + ma);
  2369. }
  2370. }
  2371. // read mark #2 anchor matrix
  2372. // seek to mark #2 array
  2373. in.seekSet(subtableOffset + m2ao);
  2374. // read mark #2 count
  2375. int nm2 = in.readTTFUShort();
  2376. if (log.isDebugEnabled()) {
  2377. log.debug(tableTag + " mark-to-mark positioning mark #2 count: " + nm2 );
  2378. }
  2379. // read anchor matrix, where i:{0...mark2Count - 1}, j:{0...markClassCount - 1}
  2380. GlyphPositioningTable.Anchor[][] mam = new GlyphPositioningTable.Anchor [ nm2 ] [ nmc ];
  2381. for ( int i = 0; i < nm2; i++ ) {
  2382. for ( int j = 0; j < nmc; j++ ) {
  2383. // read mark anchor offset
  2384. int ao = in.readTTFUShort();
  2385. GlyphPositioningTable.Anchor a;
  2386. if ( ao > 0 ) {
  2387. a = readPosAnchor ( subtableOffset + m2ao + ao );
  2388. } else {
  2389. a = null;
  2390. }
  2391. mam [ i ] [ j ] = a;
  2392. if (log.isDebugEnabled()) {
  2393. log.debug(tableTag + " mark-to-mark positioning mark #2 anchor[" + i + "][" + j + "]: " + a);
  2394. }
  2395. }
  2396. }
  2397. // store results
  2398. seMapping = mct1;
  2399. seEntries.add ( mct2 );
  2400. seEntries.add ( Integer.valueOf ( nmc ) );
  2401. seEntries.add ( maa );
  2402. seEntries.add ( mam );
  2403. }
  2404. private int readMarkToMarkPosTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  2405. in.seekSet(subtableOffset);
  2406. // read positioning subtable format
  2407. int sf = in.readTTFUShort();
  2408. if ( sf == 1 ) {
  2409. readMarkToMarkPosTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  2410. } else {
  2411. throw new AdvancedTypographicTableFormatException ( "unsupported mark-to-mark positioning subtable format: " + sf );
  2412. }
  2413. return sf;
  2414. }
  2415. private void readContextualPosTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2416. String tableTag = "GPOS";
  2417. in.seekSet(subtableOffset);
  2418. // skip over format (already known)
  2419. in.skip ( 2 );
  2420. // read coverage offset
  2421. int co = in.readTTFUShort();
  2422. // read rule set count
  2423. int nrs = in.readTTFUShort();
  2424. // read rule set offsets
  2425. int[] rsoa = new int [ nrs ];
  2426. for ( int i = 0; i < nrs; i++ ) {
  2427. rsoa [ i ] = in.readTTFUShort();
  2428. }
  2429. // dump info if debugging
  2430. if (log.isDebugEnabled()) {
  2431. log.debug(tableTag + " contextual positioning subtable format: " + subtableFormat + " (glyphs)" );
  2432. log.debug(tableTag + " contextual positioning coverage table offset: " + co );
  2433. log.debug(tableTag + " contextual positioning rule set count: " + nrs );
  2434. for ( int i = 0; i < nrs; i++ ) {
  2435. log.debug(tableTag + " contextual positioning rule set offset[" + i + "]: " + rsoa[i] );
  2436. }
  2437. }
  2438. // read coverage table
  2439. GlyphCoverageTable ct;
  2440. if ( co > 0 ) {
  2441. ct = readCoverageTable ( tableTag + " contextual positioning coverage", subtableOffset + co );
  2442. } else {
  2443. ct = null;
  2444. }
  2445. // read rule sets
  2446. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ nrs ];
  2447. String header = null;
  2448. for ( int i = 0; i < nrs; i++ ) {
  2449. GlyphTable.RuleSet rs;
  2450. int rso = rsoa [ i ];
  2451. if ( rso > 0 ) {
  2452. // seek to rule set [ i ]
  2453. in.seekSet ( subtableOffset + rso );
  2454. // read rule count
  2455. int nr = in.readTTFUShort();
  2456. // read rule offsets
  2457. int[] roa = new int [ nr ];
  2458. GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
  2459. for ( int j = 0; j < nr; j++ ) {
  2460. roa [ j ] = in.readTTFUShort();
  2461. }
  2462. // read glyph sequence rules
  2463. for ( int j = 0; j < nr; j++ ) {
  2464. GlyphTable.GlyphSequenceRule r;
  2465. int ro = roa [ j ];
  2466. if ( ro > 0 ) {
  2467. // seek to rule [ j ]
  2468. in.seekSet ( subtableOffset + rso + ro );
  2469. // read glyph count
  2470. int ng = in.readTTFUShort();
  2471. // read rule lookup count
  2472. int nl = in.readTTFUShort();
  2473. // read glyphs
  2474. int[] glyphs = new int [ ng - 1 ];
  2475. for ( int k = 0, nk = glyphs.length; k < nk; k++ ) {
  2476. glyphs [ k ] = in.readTTFUShort();
  2477. }
  2478. // read rule lookups
  2479. if (log.isDebugEnabled()) {
  2480. header = tableTag + " contextual positioning lookups @rule[" + i + "][" + j + "]: ";
  2481. }
  2482. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  2483. r = new GlyphTable.GlyphSequenceRule ( lookups, ng, glyphs );
  2484. } else {
  2485. r = null;
  2486. }
  2487. ra [ j ] = r;
  2488. }
  2489. rs = new GlyphTable.HomogeneousRuleSet ( ra );
  2490. } else {
  2491. rs = null;
  2492. }
  2493. rsa [ i ] = rs;
  2494. }
  2495. // store results
  2496. seMapping = ct;
  2497. seEntries.add ( rsa );
  2498. }
  2499. private void readContextualPosTableFormat2(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2500. String tableTag = "GPOS";
  2501. in.seekSet(subtableOffset);
  2502. // skip over format (already known)
  2503. in.skip ( 2 );
  2504. // read coverage offset
  2505. int co = in.readTTFUShort();
  2506. // read class def table offset
  2507. int cdo = in.readTTFUShort();
  2508. // read class rule set count
  2509. int ngc = in.readTTFUShort();
  2510. // read class rule set offsets
  2511. int[] csoa = new int [ ngc ];
  2512. for ( int i = 0; i < ngc; i++ ) {
  2513. csoa [ i ] = in.readTTFUShort();
  2514. }
  2515. // dump info if debugging
  2516. if (log.isDebugEnabled()) {
  2517. log.debug(tableTag + " contextual positioning subtable format: " + subtableFormat + " (glyph classes)" );
  2518. log.debug(tableTag + " contextual positioning coverage table offset: " + co );
  2519. log.debug(tableTag + " contextual positioning class set count: " + ngc );
  2520. for ( int i = 0; i < ngc; i++ ) {
  2521. log.debug(tableTag + " contextual positioning class set offset[" + i + "]: " + csoa[i] );
  2522. }
  2523. }
  2524. // read coverage table
  2525. GlyphCoverageTable ct;
  2526. if ( co > 0 ) {
  2527. ct = readCoverageTable ( tableTag + " contextual positioning coverage", subtableOffset + co );
  2528. } else {
  2529. ct = null;
  2530. }
  2531. // read class definition table
  2532. GlyphClassTable cdt;
  2533. if ( cdo > 0 ) {
  2534. cdt = readClassDefTable ( tableTag + " contextual positioning class definition", subtableOffset + cdo );
  2535. } else {
  2536. cdt = null;
  2537. }
  2538. // read rule sets
  2539. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ ngc ];
  2540. String header = null;
  2541. for ( int i = 0; i < ngc; i++ ) {
  2542. int cso = csoa [ i ];
  2543. GlyphTable.RuleSet rs;
  2544. if ( cso > 0 ) {
  2545. // seek to rule set [ i ]
  2546. in.seekSet ( subtableOffset + cso );
  2547. // read rule count
  2548. int nr = in.readTTFUShort();
  2549. // read rule offsets
  2550. int[] roa = new int [ nr ];
  2551. GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
  2552. for ( int j = 0; j < nr; j++ ) {
  2553. roa [ j ] = in.readTTFUShort();
  2554. }
  2555. // read glyph sequence rules
  2556. for ( int j = 0; j < nr; j++ ) {
  2557. int ro = roa [ j ];
  2558. GlyphTable.ClassSequenceRule r;
  2559. if ( ro > 0 ) {
  2560. // seek to rule [ j ]
  2561. in.seekSet ( subtableOffset + cso + ro );
  2562. // read glyph count
  2563. int ng = in.readTTFUShort();
  2564. // read rule lookup count
  2565. int nl = in.readTTFUShort();
  2566. // read classes
  2567. int[] classes = new int [ ng - 1 ];
  2568. for ( int k = 0, nk = classes.length; k < nk; k++ ) {
  2569. classes [ k ] = in.readTTFUShort();
  2570. }
  2571. // read rule lookups
  2572. if (log.isDebugEnabled()) {
  2573. header = tableTag + " contextual positioning lookups @rule[" + i + "][" + j + "]: ";
  2574. }
  2575. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  2576. r = new GlyphTable.ClassSequenceRule ( lookups, ng, classes );
  2577. } else {
  2578. r = null;
  2579. }
  2580. ra [ j ] = r;
  2581. }
  2582. rs = new GlyphTable.HomogeneousRuleSet ( ra );
  2583. } else {
  2584. rs = null;
  2585. }
  2586. rsa [ i ] = rs;
  2587. }
  2588. // store results
  2589. seMapping = ct;
  2590. seEntries.add ( cdt );
  2591. seEntries.add ( Integer.valueOf ( ngc ) );
  2592. seEntries.add ( rsa );
  2593. }
  2594. private void readContextualPosTableFormat3(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2595. String tableTag = "GPOS";
  2596. in.seekSet(subtableOffset);
  2597. // skip over format (already known)
  2598. in.skip ( 2 );
  2599. // read glyph (input sequence length) count
  2600. int ng = in.readTTFUShort();
  2601. // read positioning lookup count
  2602. int nl = in.readTTFUShort();
  2603. // read glyph coverage offsets, one per glyph input sequence length count
  2604. int[] gcoa = new int [ ng ];
  2605. for ( int i = 0; i < ng; i++ ) {
  2606. gcoa [ i ] = in.readTTFUShort();
  2607. }
  2608. // dump info if debugging
  2609. if (log.isDebugEnabled()) {
  2610. log.debug(tableTag + " contextual positioning subtable format: " + subtableFormat + " (glyph sets)" );
  2611. log.debug(tableTag + " contextual positioning glyph input sequence length count: " + ng );
  2612. log.debug(tableTag + " contextual positioning lookup count: " + nl );
  2613. for ( int i = 0; i < ng; i++ ) {
  2614. log.debug(tableTag + " contextual positioning coverage table offset[" + i + "]: " + gcoa[i] );
  2615. }
  2616. }
  2617. // read coverage tables
  2618. GlyphCoverageTable[] gca = new GlyphCoverageTable [ ng ];
  2619. for ( int i = 0; i < ng; i++ ) {
  2620. int gco = gcoa [ i ];
  2621. GlyphCoverageTable gct;
  2622. if ( gco > 0 ) {
  2623. gct = readCoverageTable ( tableTag + " contextual positioning coverage[" + i + "]", subtableOffset + gcoa[i] );
  2624. } else {
  2625. gct = null;
  2626. }
  2627. gca [ i ] = gct;
  2628. }
  2629. // read rule lookups
  2630. String header = null;
  2631. if (log.isDebugEnabled()) {
  2632. header = tableTag + " contextual positioning lookups: ";
  2633. }
  2634. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  2635. // construct rule, rule set, and rule set array
  2636. GlyphTable.Rule r = new GlyphTable.CoverageSequenceRule ( lookups, ng, gca );
  2637. GlyphTable.RuleSet rs = new GlyphTable.HomogeneousRuleSet ( new GlyphTable.Rule[] {r} );
  2638. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet[] {rs};
  2639. // store results
  2640. assert ( gca != null ) && ( gca.length > 0 );
  2641. seMapping = gca[0];
  2642. seEntries.add ( rsa );
  2643. }
  2644. private int readContextualPosTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  2645. in.seekSet(subtableOffset);
  2646. // read positioning subtable format
  2647. int sf = in.readTTFUShort();
  2648. if ( sf == 1 ) {
  2649. readContextualPosTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  2650. } else if ( sf == 2 ) {
  2651. readContextualPosTableFormat2 ( lookupType, lookupFlags, subtableOffset, sf );
  2652. } else if ( sf == 3 ) {
  2653. readContextualPosTableFormat3 ( lookupType, lookupFlags, subtableOffset, sf );
  2654. } else {
  2655. throw new AdvancedTypographicTableFormatException ( "unsupported contextual positioning subtable format: " + sf );
  2656. }
  2657. return sf;
  2658. }
  2659. private void readChainedContextualPosTableFormat1(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2660. String tableTag = "GPOS";
  2661. in.seekSet(subtableOffset);
  2662. // skip over format (already known)
  2663. in.skip ( 2 );
  2664. // read coverage offset
  2665. int co = in.readTTFUShort();
  2666. // read rule set count
  2667. int nrs = in.readTTFUShort();
  2668. // read rule set offsets
  2669. int[] rsoa = new int [ nrs ];
  2670. for ( int i = 0; i < nrs; i++ ) {
  2671. rsoa [ i ] = in.readTTFUShort();
  2672. }
  2673. // dump info if debugging
  2674. if (log.isDebugEnabled()) {
  2675. log.debug(tableTag + " chained contextual positioning subtable format: " + subtableFormat + " (glyphs)" );
  2676. log.debug(tableTag + " chained contextual positioning coverage table offset: " + co );
  2677. log.debug(tableTag + " chained contextual positioning rule set count: " + nrs );
  2678. for ( int i = 0; i < nrs; i++ ) {
  2679. log.debug(tableTag + " chained contextual positioning rule set offset[" + i + "]: " + rsoa[i] );
  2680. }
  2681. }
  2682. // read coverage table
  2683. GlyphCoverageTable ct;
  2684. if ( co > 0 ) {
  2685. ct = readCoverageTable ( tableTag + " chained contextual positioning coverage", subtableOffset + co );
  2686. } else {
  2687. ct = null;
  2688. }
  2689. // read rule sets
  2690. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ nrs ];
  2691. String header = null;
  2692. for ( int i = 0; i < nrs; i++ ) {
  2693. GlyphTable.RuleSet rs;
  2694. int rso = rsoa [ i ];
  2695. if ( rso > 0 ) {
  2696. // seek to rule set [ i ]
  2697. in.seekSet ( subtableOffset + rso );
  2698. // read rule count
  2699. int nr = in.readTTFUShort();
  2700. // read rule offsets
  2701. int[] roa = new int [ nr ];
  2702. GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
  2703. for ( int j = 0; j < nr; j++ ) {
  2704. roa [ j ] = in.readTTFUShort();
  2705. }
  2706. // read glyph sequence rules
  2707. for ( int j = 0; j < nr; j++ ) {
  2708. GlyphTable.ChainedGlyphSequenceRule r;
  2709. int ro = roa [ j ];
  2710. if ( ro > 0 ) {
  2711. // seek to rule [ j ]
  2712. in.seekSet ( subtableOffset + rso + ro );
  2713. // read backtrack glyph count
  2714. int nbg = in.readTTFUShort();
  2715. // read backtrack glyphs
  2716. int[] backtrackGlyphs = new int [ nbg ];
  2717. for ( int k = 0, nk = backtrackGlyphs.length; k < nk; k++ ) {
  2718. backtrackGlyphs [ k ] = in.readTTFUShort();
  2719. }
  2720. // read input glyph count
  2721. int nig = in.readTTFUShort();
  2722. // read glyphs
  2723. int[] glyphs = new int [ nig - 1 ];
  2724. for ( int k = 0, nk = glyphs.length; k < nk; k++ ) {
  2725. glyphs [ k ] = in.readTTFUShort();
  2726. }
  2727. // read lookahead glyph count
  2728. int nlg = in.readTTFUShort();
  2729. // read lookahead glyphs
  2730. int[] lookaheadGlyphs = new int [ nlg ];
  2731. for ( int k = 0, nk = lookaheadGlyphs.length; k < nk; k++ ) {
  2732. lookaheadGlyphs [ k ] = in.readTTFUShort();
  2733. }
  2734. // read rule lookup count
  2735. int nl = in.readTTFUShort();
  2736. // read rule lookups
  2737. if (log.isDebugEnabled()) {
  2738. header = tableTag + " contextual positioning lookups @rule[" + i + "][" + j + "]: ";
  2739. }
  2740. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  2741. r = new GlyphTable.ChainedGlyphSequenceRule ( lookups, nig, glyphs, backtrackGlyphs, lookaheadGlyphs );
  2742. } else {
  2743. r = null;
  2744. }
  2745. ra [ j ] = r;
  2746. }
  2747. rs = new GlyphTable.HomogeneousRuleSet ( ra );
  2748. } else {
  2749. rs = null;
  2750. }
  2751. rsa [ i ] = rs;
  2752. }
  2753. // store results
  2754. seMapping = ct;
  2755. seEntries.add ( rsa );
  2756. }
  2757. private void readChainedContextualPosTableFormat2(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2758. String tableTag = "GPOS";
  2759. in.seekSet(subtableOffset);
  2760. // skip over format (already known)
  2761. in.skip ( 2 );
  2762. // read coverage offset
  2763. int co = in.readTTFUShort();
  2764. // read backtrack class def table offset
  2765. int bcdo = in.readTTFUShort();
  2766. // read input class def table offset
  2767. int icdo = in.readTTFUShort();
  2768. // read lookahead class def table offset
  2769. int lcdo = in.readTTFUShort();
  2770. // read class set count
  2771. int ngc = in.readTTFUShort();
  2772. // read class set offsets
  2773. int[] csoa = new int [ ngc ];
  2774. for ( int i = 0; i < ngc; i++ ) {
  2775. csoa [ i ] = in.readTTFUShort();
  2776. }
  2777. // dump info if debugging
  2778. if (log.isDebugEnabled()) {
  2779. log.debug(tableTag + " chained contextual positioning subtable format: " + subtableFormat + " (glyph classes)" );
  2780. log.debug(tableTag + " chained contextual positioning coverage table offset: " + co );
  2781. log.debug(tableTag + " chained contextual positioning class set count: " + ngc );
  2782. for ( int i = 0; i < ngc; i++ ) {
  2783. log.debug(tableTag + " chained contextual positioning class set offset[" + i + "]: " + csoa[i] );
  2784. }
  2785. }
  2786. // read coverage table
  2787. GlyphCoverageTable ct;
  2788. if ( co > 0 ) {
  2789. ct = readCoverageTable ( tableTag + " chained contextual positioning coverage", subtableOffset + co );
  2790. } else {
  2791. ct = null;
  2792. }
  2793. // read backtrack class definition table
  2794. GlyphClassTable bcdt;
  2795. if ( bcdo > 0 ) {
  2796. bcdt = readClassDefTable ( tableTag + " contextual positioning backtrack class definition", subtableOffset + bcdo );
  2797. } else {
  2798. bcdt = null;
  2799. }
  2800. // read input class definition table
  2801. GlyphClassTable icdt;
  2802. if ( icdo > 0 ) {
  2803. icdt = readClassDefTable ( tableTag + " contextual positioning input class definition", subtableOffset + icdo );
  2804. } else {
  2805. icdt = null;
  2806. }
  2807. // read lookahead class definition table
  2808. GlyphClassTable lcdt;
  2809. if ( lcdo > 0 ) {
  2810. lcdt = readClassDefTable ( tableTag + " contextual positioning lookahead class definition", subtableOffset + lcdo );
  2811. } else {
  2812. lcdt = null;
  2813. }
  2814. // read rule sets
  2815. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet [ ngc ];
  2816. String header = null;
  2817. for ( int i = 0; i < ngc; i++ ) {
  2818. int cso = csoa [ i ];
  2819. GlyphTable.RuleSet rs;
  2820. if ( cso > 0 ) {
  2821. // seek to rule set [ i ]
  2822. in.seekSet ( subtableOffset + cso );
  2823. // read rule count
  2824. int nr = in.readTTFUShort();
  2825. // read rule offsets
  2826. int[] roa = new int [ nr ];
  2827. GlyphTable.Rule[] ra = new GlyphTable.Rule [ nr ];
  2828. for ( int j = 0; j < nr; j++ ) {
  2829. roa [ j ] = in.readTTFUShort();
  2830. }
  2831. // read glyph sequence rules
  2832. for ( int j = 0; j < nr; j++ ) {
  2833. GlyphTable.ChainedClassSequenceRule r;
  2834. int ro = roa [ j ];
  2835. if ( ro > 0 ) {
  2836. // seek to rule [ j ]
  2837. in.seekSet ( subtableOffset + cso + ro );
  2838. // read backtrack glyph class count
  2839. int nbc = in.readTTFUShort();
  2840. // read backtrack glyph classes
  2841. int[] backtrackClasses = new int [ nbc ];
  2842. for ( int k = 0, nk = backtrackClasses.length; k < nk; k++ ) {
  2843. backtrackClasses [ k ] = in.readTTFUShort();
  2844. }
  2845. // read input glyph class count
  2846. int nic = in.readTTFUShort();
  2847. // read input glyph classes
  2848. int[] classes = new int [ nic - 1 ];
  2849. for ( int k = 0, nk = classes.length; k < nk; k++ ) {
  2850. classes [ k ] = in.readTTFUShort();
  2851. }
  2852. // read lookahead glyph class count
  2853. int nlc = in.readTTFUShort();
  2854. // read lookahead glyph classes
  2855. int[] lookaheadClasses = new int [ nlc ];
  2856. for ( int k = 0, nk = lookaheadClasses.length; k < nk; k++ ) {
  2857. lookaheadClasses [ k ] = in.readTTFUShort();
  2858. }
  2859. // read rule lookup count
  2860. int nl = in.readTTFUShort();
  2861. // read rule lookups
  2862. if (log.isDebugEnabled()) {
  2863. header = tableTag + " contextual positioning lookups @rule[" + i + "][" + j + "]: ";
  2864. }
  2865. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  2866. r = new GlyphTable.ChainedClassSequenceRule ( lookups, nic, classes, backtrackClasses, lookaheadClasses );
  2867. } else {
  2868. r = null;
  2869. }
  2870. ra [ j ] = r;
  2871. }
  2872. rs = new GlyphTable.HomogeneousRuleSet ( ra );
  2873. } else {
  2874. rs = null;
  2875. }
  2876. rsa [ i ] = rs;
  2877. }
  2878. // store results
  2879. seMapping = ct;
  2880. seEntries.add ( icdt );
  2881. seEntries.add ( bcdt );
  2882. seEntries.add ( lcdt );
  2883. seEntries.add ( Integer.valueOf ( ngc ) );
  2884. seEntries.add ( rsa );
  2885. }
  2886. private void readChainedContextualPosTableFormat3(int lookupType, int lookupFlags, long subtableOffset, int subtableFormat) throws IOException {
  2887. String tableTag = "GPOS";
  2888. in.seekSet(subtableOffset);
  2889. // skip over format (already known)
  2890. in.skip ( 2 );
  2891. // read backtrack glyph count
  2892. int nbg = in.readTTFUShort();
  2893. // read backtrack glyph coverage offsets
  2894. int[] bgcoa = new int [ nbg ];
  2895. for ( int i = 0; i < nbg; i++ ) {
  2896. bgcoa [ i ] = in.readTTFUShort();
  2897. }
  2898. // read input glyph count
  2899. int nig = in.readTTFUShort();
  2900. // read backtrack glyph coverage offsets
  2901. int[] igcoa = new int [ nig ];
  2902. for ( int i = 0; i < nig; i++ ) {
  2903. igcoa [ i ] = in.readTTFUShort();
  2904. }
  2905. // read lookahead glyph count
  2906. int nlg = in.readTTFUShort();
  2907. // read backtrack glyph coverage offsets
  2908. int[] lgcoa = new int [ nlg ];
  2909. for ( int i = 0; i < nlg; i++ ) {
  2910. lgcoa [ i ] = in.readTTFUShort();
  2911. }
  2912. // read positioning lookup count
  2913. int nl = in.readTTFUShort();
  2914. // dump info if debugging
  2915. if (log.isDebugEnabled()) {
  2916. log.debug(tableTag + " chained contextual positioning subtable format: " + subtableFormat + " (glyph sets)" );
  2917. log.debug(tableTag + " chained contextual positioning backtrack glyph count: " + nbg );
  2918. for ( int i = 0; i < nbg; i++ ) {
  2919. log.debug(tableTag + " chained contextual positioning backtrack coverage table offset[" + i + "]: " + bgcoa[i] );
  2920. }
  2921. log.debug(tableTag + " chained contextual positioning input glyph count: " + nig );
  2922. for ( int i = 0; i < nig; i++ ) {
  2923. log.debug(tableTag + " chained contextual positioning input coverage table offset[" + i + "]: " + igcoa[i] );
  2924. }
  2925. log.debug(tableTag + " chained contextual positioning lookahead glyph count: " + nlg );
  2926. for ( int i = 0; i < nlg; i++ ) {
  2927. log.debug(tableTag + " chained contextual positioning lookahead coverage table offset[" + i + "]: " + lgcoa[i] );
  2928. }
  2929. log.debug(tableTag + " chained contextual positioning lookup count: " + nl );
  2930. }
  2931. // read backtrack coverage tables
  2932. GlyphCoverageTable[] bgca = new GlyphCoverageTable[nbg];
  2933. for ( int i = 0; i < nbg; i++ ) {
  2934. int bgco = bgcoa [ i ];
  2935. GlyphCoverageTable bgct;
  2936. if ( bgco > 0 ) {
  2937. bgct = readCoverageTable ( tableTag + " chained contextual positioning backtrack coverage[" + i + "]", subtableOffset + bgco );
  2938. } else {
  2939. bgct = null;
  2940. }
  2941. bgca[i] = bgct;
  2942. }
  2943. // read input coverage tables
  2944. GlyphCoverageTable[] igca = new GlyphCoverageTable[nig];
  2945. for ( int i = 0; i < nig; i++ ) {
  2946. int igco = igcoa [ i ];
  2947. GlyphCoverageTable igct;
  2948. if ( igco > 0 ) {
  2949. igct = readCoverageTable ( tableTag + " chained contextual positioning input coverage[" + i + "]", subtableOffset + igco );
  2950. } else {
  2951. igct = null;
  2952. }
  2953. igca[i] = igct;
  2954. }
  2955. // read lookahead coverage tables
  2956. GlyphCoverageTable[] lgca = new GlyphCoverageTable[nlg];
  2957. for ( int i = 0; i < nlg; i++ ) {
  2958. int lgco = lgcoa [ i ];
  2959. GlyphCoverageTable lgct;
  2960. if ( lgco > 0 ) {
  2961. lgct = readCoverageTable ( tableTag + " chained contextual positioning lookahead coverage[" + i + "]", subtableOffset + lgco );
  2962. } else {
  2963. lgct = null;
  2964. }
  2965. lgca[i] = lgct;
  2966. }
  2967. // read rule lookups
  2968. String header = null;
  2969. if (log.isDebugEnabled()) {
  2970. header = tableTag + " chained contextual positioning lookups: ";
  2971. }
  2972. GlyphTable.RuleLookup[] lookups = readRuleLookups ( nl, header );
  2973. // construct rule, rule set, and rule set array
  2974. GlyphTable.Rule r = new GlyphTable.ChainedCoverageSequenceRule ( lookups, nig, igca, bgca, lgca );
  2975. GlyphTable.RuleSet rs = new GlyphTable.HomogeneousRuleSet ( new GlyphTable.Rule[] {r} );
  2976. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet[] {rs};
  2977. // store results
  2978. assert ( igca != null ) && ( igca.length > 0 );
  2979. seMapping = igca[0];
  2980. seEntries.add ( rsa );
  2981. }
  2982. private int readChainedContextualPosTable(int lookupType, int lookupFlags, long subtableOffset) throws IOException {
  2983. in.seekSet(subtableOffset);
  2984. // read positioning subtable format
  2985. int sf = in.readTTFUShort();
  2986. if ( sf == 1 ) {
  2987. readChainedContextualPosTableFormat1 ( lookupType, lookupFlags, subtableOffset, sf );
  2988. } else if ( sf == 2 ) {
  2989. readChainedContextualPosTableFormat2 ( lookupType, lookupFlags, subtableOffset, sf );
  2990. } else if ( sf == 3 ) {
  2991. readChainedContextualPosTableFormat3 ( lookupType, lookupFlags, subtableOffset, sf );
  2992. } else {
  2993. throw new AdvancedTypographicTableFormatException ( "unsupported chained contextual positioning subtable format: " + sf );
  2994. }
  2995. return sf;
  2996. }
  2997. private void readExtensionPosTableFormat1(int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset, int subtableFormat) throws IOException {
  2998. String tableTag = "GPOS";
  2999. in.seekSet(subtableOffset);
  3000. // skip over format (already known)
  3001. in.skip ( 2 );
  3002. // read extension lookup type
  3003. int lt = in.readTTFUShort();
  3004. // read extension offset
  3005. long eo = in.readTTFULong();
  3006. // dump info if debugging
  3007. if (log.isDebugEnabled()) {
  3008. log.debug(tableTag + " extension positioning subtable format: " + subtableFormat );
  3009. log.debug(tableTag + " extension positioning lookup type: " + lt );
  3010. log.debug(tableTag + " extension positioning lookup table offset: " + eo );
  3011. }
  3012. // read referenced subtable from extended offset
  3013. readGPOSSubtable ( lt, lookupFlags, lookupSequence, subtableSequence, subtableOffset + eo );
  3014. }
  3015. private int readExtensionPosTable(int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset) throws IOException {
  3016. in.seekSet(subtableOffset);
  3017. // read positioning subtable format
  3018. int sf = in.readTTFUShort();
  3019. if ( sf == 1 ) {
  3020. readExtensionPosTableFormat1 ( lookupType, lookupFlags, lookupSequence, subtableSequence, subtableOffset, sf );
  3021. } else {
  3022. throw new AdvancedTypographicTableFormatException ( "unsupported extension positioning subtable format: " + sf );
  3023. }
  3024. return sf;
  3025. }
  3026. private void readGPOSSubtable(int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, long subtableOffset) throws IOException {
  3027. initATSubState();
  3028. int subtableFormat = -1;
  3029. switch ( lookupType ) {
  3030. case GPOSLookupType.SINGLE:
  3031. subtableFormat = readSinglePosTable ( lookupType, lookupFlags, subtableOffset );
  3032. break;
  3033. case GPOSLookupType.PAIR:
  3034. subtableFormat = readPairPosTable ( lookupType, lookupFlags, subtableOffset );
  3035. break;
  3036. case GPOSLookupType.CURSIVE:
  3037. subtableFormat = readCursivePosTable ( lookupType, lookupFlags, subtableOffset );
  3038. break;
  3039. case GPOSLookupType.MARK_TO_BASE:
  3040. subtableFormat = readMarkToBasePosTable ( lookupType, lookupFlags, subtableOffset );
  3041. break;
  3042. case GPOSLookupType.MARK_TO_LIGATURE:
  3043. subtableFormat = readMarkToLigaturePosTable ( lookupType, lookupFlags, subtableOffset );
  3044. break;
  3045. case GPOSLookupType.MARK_TO_MARK:
  3046. subtableFormat = readMarkToMarkPosTable ( lookupType, lookupFlags, subtableOffset );
  3047. break;
  3048. case GPOSLookupType.CONTEXTUAL:
  3049. subtableFormat = readContextualPosTable ( lookupType, lookupFlags, subtableOffset );
  3050. break;
  3051. case GPOSLookupType.CHAINED_CONTEXTUAL:
  3052. subtableFormat = readChainedContextualPosTable ( lookupType, lookupFlags, subtableOffset );
  3053. break;
  3054. case GPOSLookupType.EXTENSION:
  3055. subtableFormat = readExtensionPosTable ( lookupType, lookupFlags, lookupSequence, subtableSequence, subtableOffset );
  3056. break;
  3057. default:
  3058. break;
  3059. }
  3060. extractSESubState ( GlyphTable.GLYPH_TABLE_TYPE_POSITIONING, lookupType, lookupFlags, lookupSequence, subtableSequence, subtableFormat );
  3061. resetATSubState();
  3062. }
  3063. private void readLookupTable(TTFTableName tableTag, int lookupSequence, long lookupTable) throws IOException {
  3064. boolean isGSUB = tableTag.equals ( TTFTableName.GSUB );
  3065. boolean isGPOS = tableTag.equals ( TTFTableName.GPOS );
  3066. in.seekSet(lookupTable);
  3067. // read lookup type
  3068. int lt = in.readTTFUShort();
  3069. // read lookup flags
  3070. int lf = in.readTTFUShort();
  3071. // read sub-table count
  3072. int ns = in.readTTFUShort();
  3073. // dump info if debugging
  3074. if (log.isDebugEnabled()) {
  3075. String lts;
  3076. if ( isGSUB ) {
  3077. lts = GSUBLookupType.toString ( lt );
  3078. } else if ( isGPOS ) {
  3079. lts = GPOSLookupType.toString ( lt );
  3080. } else {
  3081. lts = "?";
  3082. }
  3083. log.debug(tableTag + " lookup table type: " + lt + " (" + lts + ")" );
  3084. log.debug(tableTag + " lookup table flags: " + lf + " (" + LookupFlag.toString ( lf ) + ")" );
  3085. log.debug(tableTag + " lookup table subtable count: " + ns );
  3086. }
  3087. // read subtable offsets
  3088. int[] soa = new int[ns];
  3089. for ( int i = 0; i < ns; i++ ) {
  3090. int so = in.readTTFUShort();
  3091. if (log.isDebugEnabled()) {
  3092. log.debug(tableTag + " lookup table subtable offset: " + so );
  3093. }
  3094. soa[i] = so;
  3095. }
  3096. // read mark filtering set
  3097. if ( ( lf & LookupFlag.USE_MARK_FILTERING_SET ) != 0 ) {
  3098. // read mark filtering set
  3099. int fs = in.readTTFUShort();
  3100. // dump info if debugging
  3101. if (log.isDebugEnabled()) {
  3102. log.debug(tableTag + " lookup table mark filter set: " + fs );
  3103. }
  3104. }
  3105. // read subtables
  3106. for ( int i = 0; i < ns; i++ ) {
  3107. int so = soa[i];
  3108. if ( isGSUB ) {
  3109. readGSUBSubtable ( lt, lf, lookupSequence, i, lookupTable + so );
  3110. } else if ( isGPOS ) {
  3111. readGPOSSubtable ( lt, lf, lookupSequence, i, lookupTable + so );
  3112. }
  3113. }
  3114. }
  3115. private void readLookupList(TTFTableName tableTag, long lookupList) throws IOException {
  3116. in.seekSet(lookupList);
  3117. // read lookup record count
  3118. int nl = in.readTTFUShort();
  3119. if (log.isDebugEnabled()) {
  3120. log.debug(tableTag + " lookup list record count: " + nl );
  3121. }
  3122. if ( nl > 0 ) {
  3123. int[] loa = new int[nl];
  3124. // read lookup records
  3125. for ( int i = 0, n = nl; i < n; i++ ) {
  3126. int lo = in.readTTFUShort();
  3127. if (log.isDebugEnabled()) {
  3128. log.debug(tableTag + " lookup table offset: " + lo );
  3129. }
  3130. loa[i] = lo;
  3131. }
  3132. // read lookup tables
  3133. for ( int i = 0, n = nl; i < n; i++ ) {
  3134. if (log.isDebugEnabled()) {
  3135. log.debug(tableTag + " lookup index: " + i );
  3136. }
  3137. readLookupTable ( tableTag, i, lookupList + loa [ i ] );
  3138. }
  3139. }
  3140. }
  3141. /**
  3142. * Read the common layout tables (used by GSUB and GPOS).
  3143. * @param tableTag tag of table being read
  3144. * @param scriptList offset to script list from beginning of font file
  3145. * @param featureList offset to feature list from beginning of font file
  3146. * @param lookupList offset to lookup list from beginning of font file
  3147. * @throws IOException In case of a I/O problem
  3148. */
  3149. private void readCommonLayoutTables(TTFTableName tableTag, long scriptList, long featureList, long lookupList) throws IOException {
  3150. if ( scriptList > 0 ) {
  3151. readScriptList ( tableTag, scriptList );
  3152. }
  3153. if ( featureList > 0 ) {
  3154. readFeatureList ( tableTag, featureList );
  3155. }
  3156. if ( lookupList > 0 ) {
  3157. readLookupList ( tableTag, lookupList );
  3158. }
  3159. }
  3160. private void readGDEFClassDefTable(TTFTableName tableTag, int lookupSequence, long subtableOffset) throws IOException {
  3161. initATSubState();
  3162. in.seekSet(subtableOffset);
  3163. // subtable is a bare class definition table
  3164. GlyphClassTable ct = readClassDefTable ( tableTag + " glyph class definition table", subtableOffset );
  3165. // store results
  3166. seMapping = ct;
  3167. // extract subtable
  3168. extractSESubState ( GlyphTable.GLYPH_TABLE_TYPE_DEFINITION, GDEFLookupType.GLYPH_CLASS, 0, lookupSequence, 0, 1 );
  3169. resetATSubState();
  3170. }
  3171. private void readGDEFAttachmentTable(TTFTableName tableTag, int lookupSequence, long subtableOffset) throws IOException {
  3172. initATSubState();
  3173. in.seekSet(subtableOffset);
  3174. // read coverage offset
  3175. int co = in.readTTFUShort();
  3176. // dump info if debugging
  3177. if (log.isDebugEnabled()) {
  3178. log.debug(tableTag + " attachment point coverage table offset: " + co );
  3179. }
  3180. // read coverage table
  3181. GlyphCoverageTable ct = readCoverageTable ( tableTag + " attachment point coverage", subtableOffset + co );
  3182. // store results
  3183. seMapping = ct;
  3184. // extract subtable
  3185. extractSESubState ( GlyphTable.GLYPH_TABLE_TYPE_DEFINITION, GDEFLookupType.ATTACHMENT_POINT, 0, lookupSequence, 0, 1 );
  3186. resetATSubState();
  3187. }
  3188. private void readGDEFLigatureCaretTable(TTFTableName tableTag, int lookupSequence, long subtableOffset) throws IOException {
  3189. initATSubState();
  3190. in.seekSet(subtableOffset);
  3191. // read coverage offset
  3192. int co = in.readTTFUShort();
  3193. // read ligature glyph count
  3194. int nl = in.readTTFUShort();
  3195. // read ligature glyph table offsets
  3196. int[] lgto = new int [ nl ];
  3197. for ( int i = 0; i < nl; i++ ) {
  3198. lgto [ i ] = in.readTTFUShort();
  3199. }
  3200. // dump info if debugging
  3201. if (log.isDebugEnabled()) {
  3202. log.debug(tableTag + " ligature caret coverage table offset: " + co );
  3203. log.debug(tableTag + " ligature caret ligature glyph count: " + nl );
  3204. for ( int i = 0; i < nl; i++ ) {
  3205. log.debug(tableTag + " ligature glyph table offset[" + i + "]: " + lgto[i] );
  3206. }
  3207. }
  3208. // read coverage table
  3209. GlyphCoverageTable ct = readCoverageTable ( tableTag + " ligature caret coverage", subtableOffset + co );
  3210. // store results
  3211. seMapping = ct;
  3212. // extract subtable
  3213. extractSESubState ( GlyphTable.GLYPH_TABLE_TYPE_DEFINITION, GDEFLookupType.LIGATURE_CARET, 0, lookupSequence, 0, 1 );
  3214. resetATSubState();
  3215. }
  3216. private void readGDEFMarkAttachmentTable(TTFTableName tableTag, int lookupSequence, long subtableOffset) throws IOException {
  3217. initATSubState();
  3218. in.seekSet(subtableOffset);
  3219. // subtable is a bare class definition table
  3220. GlyphClassTable ct = readClassDefTable ( tableTag + " glyph class definition table", subtableOffset );
  3221. // store results
  3222. seMapping = ct;
  3223. // extract subtable
  3224. extractSESubState ( GlyphTable.GLYPH_TABLE_TYPE_DEFINITION, GDEFLookupType.MARK_ATTACHMENT, 0, lookupSequence, 0, 1 );
  3225. resetATSubState();
  3226. }
  3227. private void readGDEFMarkGlyphsTableFormat1(TTFTableName tableTag, int lookupSequence, long subtableOffset, int subtableFormat) throws IOException {
  3228. initATSubState();
  3229. in.seekSet(subtableOffset);
  3230. // skip over format (already known)
  3231. in.skip ( 2 );
  3232. // read mark set class count
  3233. int nmc = in.readTTFUShort();
  3234. long[] mso = new long [ nmc ];
  3235. // read mark set coverage offsets
  3236. for ( int i = 0; i < nmc; i++ ) {
  3237. mso [ i ] = in.readTTFULong();
  3238. }
  3239. // dump info if debugging
  3240. if (log.isDebugEnabled()) {
  3241. log.debug(tableTag + " mark set subtable format: " + subtableFormat + " (glyph sets)" );
  3242. log.debug(tableTag + " mark set class count: " + nmc );
  3243. for ( int i = 0; i < nmc; i++ ) {
  3244. log.debug(tableTag + " mark set coverage table offset[" + i + "]: " + mso[i] );
  3245. }
  3246. }
  3247. // read mark set coverage tables, one per class
  3248. GlyphCoverageTable[] msca = new GlyphCoverageTable[nmc];
  3249. for ( int i = 0; i < nmc; i++ ) {
  3250. msca[i] = readCoverageTable ( tableTag + " mark set coverage[" + i + "]", subtableOffset + mso[i] );
  3251. }
  3252. // create combined class table from per-class coverage tables
  3253. GlyphClassTable ct = GlyphClassTable.createClassTable ( Arrays.asList ( msca ) );
  3254. // store results
  3255. seMapping = ct;
  3256. // extract subtable
  3257. extractSESubState ( GlyphTable.GLYPH_TABLE_TYPE_DEFINITION, GDEFLookupType.MARK_ATTACHMENT, 0, lookupSequence, 0, 1 );
  3258. resetATSubState();
  3259. }
  3260. private void readGDEFMarkGlyphsTable(TTFTableName tableTag, int lookupSequence, long subtableOffset) throws IOException {
  3261. in.seekSet(subtableOffset);
  3262. // read mark set subtable format
  3263. int sf = in.readTTFUShort();
  3264. if ( sf == 1 ) {
  3265. readGDEFMarkGlyphsTableFormat1 ( tableTag, lookupSequence, subtableOffset, sf );
  3266. } else {
  3267. throw new AdvancedTypographicTableFormatException ( "unsupported mark glyph sets subtable format: " + sf );
  3268. }
  3269. }
  3270. /**
  3271. * Read the GDEF table.
  3272. * @throws IOException In case of a I/O problem
  3273. */
  3274. private void readGDEF() throws IOException {
  3275. TTFTableName tableTag = TTFTableName.GDEF;
  3276. // Initialize temporary state
  3277. initATState();
  3278. // Read glyph definition (GDEF) table
  3279. TTFDirTabEntry dirTab = ttf.getDirectoryEntry( tableTag );
  3280. if ( gdef != null ) {
  3281. if (log.isDebugEnabled()) {
  3282. log.debug(tableTag + ": ignoring duplicate table");
  3283. }
  3284. } else if (dirTab != null) {
  3285. ttf.seekTab(in, tableTag, 0);
  3286. long version = in.readTTFULong();
  3287. if (log.isDebugEnabled()) {
  3288. log.debug(tableTag + " version: " + ( version / 65536 ) + "." + ( version % 65536 ));
  3289. }
  3290. // glyph class definition table offset (may be null)
  3291. int cdo = in.readTTFUShort();
  3292. // attach point list offset (may be null)
  3293. int apo = in.readTTFUShort();
  3294. // ligature caret list offset (may be null)
  3295. int lco = in.readTTFUShort();
  3296. // mark attach class definition table offset (may be null)
  3297. int mao = in.readTTFUShort();
  3298. // mark glyph sets definition table offset (may be null)
  3299. int mgo;
  3300. if ( version >= 0x00010002 ) {
  3301. mgo = in.readTTFUShort();
  3302. } else {
  3303. mgo = 0;
  3304. }
  3305. if (log.isDebugEnabled()) {
  3306. log.debug(tableTag + " glyph class definition table offset: " + cdo );
  3307. log.debug(tableTag + " attachment point list offset: " + apo );
  3308. log.debug(tableTag + " ligature caret list offset: " + lco );
  3309. log.debug(tableTag + " mark attachment class definition table offset: " + mao );
  3310. log.debug(tableTag + " mark glyph set definitions table offset: " + mgo );
  3311. }
  3312. // initialize subtable sequence number
  3313. int seqno = 0;
  3314. // obtain offset to start of gdef table
  3315. long to = dirTab.getOffset();
  3316. // (optionally) read glyph class definition subtable
  3317. if ( cdo != 0 ) {
  3318. readGDEFClassDefTable ( tableTag, seqno++, to + cdo );
  3319. }
  3320. // (optionally) read glyph attachment point subtable
  3321. if ( apo != 0 ) {
  3322. readGDEFAttachmentTable ( tableTag, seqno++, to + apo );
  3323. }
  3324. // (optionally) read ligature caret subtable
  3325. if ( lco != 0 ) {
  3326. readGDEFLigatureCaretTable ( tableTag, seqno++, to + lco );
  3327. }
  3328. // (optionally) read mark attachment class subtable
  3329. if ( mao != 0 ) {
  3330. readGDEFMarkAttachmentTable ( tableTag, seqno++, to + mao );
  3331. }
  3332. // (optionally) read mark glyph sets subtable
  3333. if ( mgo != 0 ) {
  3334. readGDEFMarkGlyphsTable ( tableTag, seqno++, to + mgo );
  3335. }
  3336. GlyphDefinitionTable gdef;
  3337. if ( ( gdef = constructGDEF() ) != null ) {
  3338. this.gdef = gdef;
  3339. }
  3340. }
  3341. }
  3342. /**
  3343. * Read the GSUB table.
  3344. * @throws IOException In case of a I/O problem
  3345. */
  3346. private void readGSUB() throws IOException {
  3347. TTFTableName tableTag = TTFTableName.GSUB;
  3348. // Initialize temporary state
  3349. initATState();
  3350. // Read glyph substitution (GSUB) table
  3351. TTFDirTabEntry dirTab = ttf.getDirectoryEntry ( tableTag );
  3352. if ( gpos != null ) {
  3353. if (log.isDebugEnabled()) {
  3354. log.debug(tableTag + ": ignoring duplicate table");
  3355. }
  3356. } else if (dirTab != null) {
  3357. ttf.seekTab(in, tableTag, 0);
  3358. int version = in.readTTFLong();
  3359. if (log.isDebugEnabled()) {
  3360. log.debug(tableTag + " version: " + ( version / 65536 ) + "." + ( version % 65536 ));
  3361. }
  3362. int slo = in.readTTFUShort();
  3363. int flo = in.readTTFUShort();
  3364. int llo = in.readTTFUShort();
  3365. if (log.isDebugEnabled()) {
  3366. log.debug(tableTag + " script list offset: " + slo );
  3367. log.debug(tableTag + " feature list offset: " + flo );
  3368. log.debug(tableTag + " lookup list offset: " + llo );
  3369. }
  3370. long to = dirTab.getOffset();
  3371. readCommonLayoutTables ( tableTag, to + slo, to + flo, to + llo );
  3372. GlyphSubstitutionTable gsub;
  3373. if ( ( gsub = constructGSUB() ) != null ) {
  3374. this.gsub = gsub;
  3375. }
  3376. }
  3377. }
  3378. /**
  3379. * Read the GPOS table.
  3380. * @throws IOException In case of a I/O problem
  3381. */
  3382. private void readGPOS() throws IOException {
  3383. TTFTableName tableTag = TTFTableName.GPOS;
  3384. // Initialize temporary state
  3385. initATState();
  3386. // Read glyph positioning (GPOS) table
  3387. TTFDirTabEntry dirTab = ttf.getDirectoryEntry ( tableTag );
  3388. if ( gpos != null ) {
  3389. if (log.isDebugEnabled()) {
  3390. log.debug(tableTag + ": ignoring duplicate table");
  3391. }
  3392. } else if (dirTab != null) {
  3393. ttf.seekTab(in, tableTag, 0);
  3394. int version = in.readTTFLong();
  3395. if (log.isDebugEnabled()) {
  3396. log.debug(tableTag + " version: " + ( version / 65536 ) + "." + ( version % 65536 ));
  3397. }
  3398. int slo = in.readTTFUShort();
  3399. int flo = in.readTTFUShort();
  3400. int llo = in.readTTFUShort();
  3401. if (log.isDebugEnabled()) {
  3402. log.debug(tableTag + " script list offset: " + slo );
  3403. log.debug(tableTag + " feature list offset: " + flo );
  3404. log.debug(tableTag + " lookup list offset: " + llo );
  3405. }
  3406. long to = dirTab.getOffset();
  3407. readCommonLayoutTables ( tableTag, to + slo, to + flo, to + llo );
  3408. GlyphPositioningTable gpos;
  3409. if ( ( gpos = constructGPOS() ) != null ) {
  3410. this.gpos = gpos;
  3411. }
  3412. }
  3413. }
  3414. /**
  3415. * Construct the (internal representation of the) GDEF table based on previously
  3416. * parsed state.
  3417. * @returns glyph definition table or null if insufficient or invalid state
  3418. */
  3419. private GlyphDefinitionTable constructGDEF() {
  3420. GlyphDefinitionTable gdef = null;
  3421. List subtables;
  3422. if ( ( subtables = constructGDEFSubtables() ) != null ) {
  3423. if ( subtables.size() > 0 ) {
  3424. gdef = new GlyphDefinitionTable ( subtables );
  3425. }
  3426. }
  3427. resetATState();
  3428. return gdef;
  3429. }
  3430. /**
  3431. * Construct the (internal representation of the) GSUB table based on previously
  3432. * parsed state.
  3433. * @returns glyph substitution table or null if insufficient or invalid state
  3434. */
  3435. private GlyphSubstitutionTable constructGSUB() {
  3436. GlyphSubstitutionTable gsub = null;
  3437. Map lookups;
  3438. if ( ( lookups = constructLookups() ) != null ) {
  3439. List subtables;
  3440. if ( ( subtables = constructGSUBSubtables() ) != null ) {
  3441. if ( ( lookups.size() > 0 ) && ( subtables.size() > 0 ) ) {
  3442. gsub = new GlyphSubstitutionTable ( gdef, lookups, subtables );
  3443. }
  3444. }
  3445. }
  3446. resetATState();
  3447. return gsub;
  3448. }
  3449. /**
  3450. * Construct the (internal representation of the) GPOS table based on previously
  3451. * parsed state.
  3452. * @returns glyph positioning table or null if insufficient or invalid state
  3453. */
  3454. private GlyphPositioningTable constructGPOS() {
  3455. GlyphPositioningTable gpos = null;
  3456. Map lookups;
  3457. if ( ( lookups = constructLookups() ) != null ) {
  3458. List subtables;
  3459. if ( ( subtables = constructGPOSSubtables() ) != null ) {
  3460. if ( ( lookups.size() > 0 ) && ( subtables.size() > 0 ) ) {
  3461. gpos = new GlyphPositioningTable ( gdef, lookups, subtables );
  3462. }
  3463. }
  3464. }
  3465. resetATState();
  3466. return gpos;
  3467. }
  3468. private void constructLookupsFeature ( Map lookups, String st, String lt, String fid ) {
  3469. Object[] fp = (Object[]) seFeatures.get ( fid );
  3470. if ( fp != null ) {
  3471. assert fp.length == 2;
  3472. String ft = (String) fp[0]; // feature tag
  3473. List/*<String>*/ lul = (List) fp[1]; // list of lookup table ids
  3474. if ( ( ft != null ) && ( lul != null ) && ( lul.size() > 0 ) ) {
  3475. GlyphTable.LookupSpec ls = new GlyphTable.LookupSpec ( st, lt, ft );
  3476. lookups.put ( ls, lul );
  3477. }
  3478. }
  3479. }
  3480. private void constructLookupsFeatures ( Map lookups, String st, String lt, List/*<String>*/ fids ) {
  3481. for ( Iterator fit = fids.iterator(); fit.hasNext();) {
  3482. String fid = (String) fit.next();
  3483. constructLookupsFeature ( lookups, st, lt, fid );
  3484. }
  3485. }
  3486. private void constructLookupsLanguage ( Map lookups, String st, String lt, Map/*<String,Object[2]>*/ languages ) {
  3487. Object[] lp = (Object[]) languages.get ( lt );
  3488. if ( lp != null ) {
  3489. assert lp.length == 2;
  3490. if ( lp[0] != null ) { // required feature id
  3491. constructLookupsFeature ( lookups, st, lt, (String) lp[0] );
  3492. }
  3493. if ( lp[1] != null ) { // non-required features ids
  3494. constructLookupsFeatures ( lookups, st, lt, (List) lp[1] );
  3495. }
  3496. }
  3497. }
  3498. private void constructLookupsLanguages ( Map lookups, String st, List/*<String>*/ ll, Map/*<String,Object[2]>*/ languages ) {
  3499. for ( Iterator lit = ll.iterator(); lit.hasNext();) {
  3500. String lt = (String) lit.next();
  3501. constructLookupsLanguage ( lookups, st, lt, languages );
  3502. }
  3503. }
  3504. private Map constructLookups() {
  3505. Map/*<GlyphTable.LookupSpec,List<String>>*/ lookups = new java.util.LinkedHashMap();
  3506. for ( Iterator sit = seScripts.keySet().iterator(); sit.hasNext();) {
  3507. String st = (String) sit.next();
  3508. Object[] sp = (Object[]) seScripts.get ( st );
  3509. if ( sp != null ) {
  3510. assert sp.length == 3;
  3511. Map/*<String,Object[2]>*/ languages = (Map) sp[2];
  3512. if ( sp[0] != null ) { // default language
  3513. constructLookupsLanguage ( lookups, st, (String) sp[0], languages );
  3514. }
  3515. if ( sp[1] != null ) { // non-default languages
  3516. constructLookupsLanguages ( lookups, st, (List) sp[1], languages );
  3517. }
  3518. }
  3519. }
  3520. return lookups;
  3521. }
  3522. private List constructGDEFSubtables() {
  3523. List/*<GlyphDefinitionSubtable>*/ subtables = new java.util.ArrayList();
  3524. if ( seSubtables != null ) {
  3525. for ( Iterator it = seSubtables.iterator(); it.hasNext();) {
  3526. Object[] stp = (Object[]) it.next();
  3527. GlyphSubtable st;
  3528. if ( ( st = constructGDEFSubtable ( stp ) ) != null ) {
  3529. subtables.add ( st );
  3530. }
  3531. }
  3532. }
  3533. return subtables;
  3534. }
  3535. private GlyphSubtable constructGDEFSubtable ( Object[] stp ) {
  3536. GlyphSubtable st = null;
  3537. assert ( stp != null ) && ( stp.length == 8 );
  3538. Integer tt = (Integer) stp[0]; // table type
  3539. Integer lt = (Integer) stp[1]; // lookup type
  3540. Integer ln = (Integer) stp[2]; // lookup sequence number
  3541. Integer lf = (Integer) stp[3]; // lookup flags
  3542. Integer sn = (Integer) stp[4]; // subtable sequence number
  3543. Integer sf = (Integer) stp[5]; // subtable format
  3544. GlyphMappingTable mapping = (GlyphMappingTable) stp[6];
  3545. List entries = (List) stp[7];
  3546. if ( tt.intValue() == GlyphTable.GLYPH_TABLE_TYPE_DEFINITION ) {
  3547. int type = GDEFLookupType.getSubtableType ( lt.intValue() );
  3548. String lid = "lu" + ln.intValue();
  3549. int sequence = sn.intValue();
  3550. int flags = lf.intValue();
  3551. int format = sf.intValue();
  3552. st = GlyphDefinitionTable.createSubtable ( type, lid, sequence, flags, format, mapping, entries );
  3553. }
  3554. return st;
  3555. }
  3556. private List constructGSUBSubtables() {
  3557. List/*<GlyphSubtable>*/ subtables = new java.util.ArrayList();
  3558. if ( seSubtables != null ) {
  3559. for ( Iterator it = seSubtables.iterator(); it.hasNext();) {
  3560. Object[] stp = (Object[]) it.next();
  3561. GlyphSubtable st;
  3562. if ( ( st = constructGSUBSubtable ( stp ) ) != null ) {
  3563. subtables.add ( st );
  3564. }
  3565. }
  3566. }
  3567. return subtables;
  3568. }
  3569. private GlyphSubtable constructGSUBSubtable ( Object[] stp ) {
  3570. GlyphSubtable st = null;
  3571. assert ( stp != null ) && ( stp.length == 8 );
  3572. Integer tt = (Integer) stp[0]; // table type
  3573. Integer lt = (Integer) stp[1]; // lookup type
  3574. Integer ln = (Integer) stp[2]; // lookup sequence number
  3575. Integer lf = (Integer) stp[3]; // lookup flags
  3576. Integer sn = (Integer) stp[4]; // subtable sequence number
  3577. Integer sf = (Integer) stp[5]; // subtable format
  3578. GlyphCoverageTable coverage = (GlyphCoverageTable) stp[6];
  3579. List entries = (List) stp[7];
  3580. if ( tt.intValue() == GlyphTable.GLYPH_TABLE_TYPE_SUBSTITUTION ) {
  3581. int type = GSUBLookupType.getSubtableType ( lt.intValue() );
  3582. String lid = "lu" + ln.intValue();
  3583. int sequence = sn.intValue();
  3584. int flags = lf.intValue();
  3585. int format = sf.intValue();
  3586. st = GlyphSubstitutionTable.createSubtable ( type, lid, sequence, flags, format, coverage, entries );
  3587. }
  3588. return st;
  3589. }
  3590. private List constructGPOSSubtables() {
  3591. List/*<GlyphSubtable>*/ subtables = new java.util.ArrayList();
  3592. if ( seSubtables != null ) {
  3593. for ( Iterator it = seSubtables.iterator(); it.hasNext();) {
  3594. Object[] stp = (Object[]) it.next();
  3595. GlyphSubtable st;
  3596. if ( ( st = constructGPOSSubtable ( stp ) ) != null ) {
  3597. subtables.add ( st );
  3598. }
  3599. }
  3600. }
  3601. return subtables;
  3602. }
  3603. private GlyphSubtable constructGPOSSubtable ( Object[] stp ) {
  3604. GlyphSubtable st = null;
  3605. assert ( stp != null ) && ( stp.length == 8 );
  3606. Integer tt = (Integer) stp[0]; // table type
  3607. Integer lt = (Integer) stp[1]; // lookup type
  3608. Integer ln = (Integer) stp[2]; // lookup sequence number
  3609. Integer lf = (Integer) stp[3]; // lookup flags
  3610. Integer sn = (Integer) stp[4]; // subtable sequence number
  3611. Integer sf = (Integer) stp[5]; // subtable format
  3612. GlyphCoverageTable coverage = (GlyphCoverageTable) stp[6];
  3613. List entries = (List) stp[7];
  3614. if ( tt.intValue() == GlyphTable.GLYPH_TABLE_TYPE_POSITIONING ) {
  3615. int type = GSUBLookupType.getSubtableType ( lt.intValue() );
  3616. String lid = "lu" + ln.intValue();
  3617. int sequence = sn.intValue();
  3618. int flags = lf.intValue();
  3619. int format = sf.intValue();
  3620. st = GlyphPositioningTable.createSubtable ( type, lid, sequence, flags, format, coverage, entries );
  3621. }
  3622. return st;
  3623. }
  3624. private void initATState() {
  3625. seScripts = new java.util.LinkedHashMap();
  3626. seLanguages = new java.util.LinkedHashMap();
  3627. seFeatures = new java.util.LinkedHashMap();
  3628. seSubtables = new java.util.ArrayList();
  3629. resetATSubState();
  3630. }
  3631. private void resetATState() {
  3632. seScripts = null;
  3633. seLanguages = null;
  3634. seFeatures = null;
  3635. seSubtables = null;
  3636. resetATSubState();
  3637. }
  3638. private void initATSubState() {
  3639. seMapping = null;
  3640. seEntries = new java.util.ArrayList();
  3641. }
  3642. private void extractSESubState ( int tableType, int lookupType, int lookupFlags, int lookupSequence, int subtableSequence, int subtableFormat ) {
  3643. if ( seEntries != null ) {
  3644. if ( ( tableType == GlyphTable.GLYPH_TABLE_TYPE_DEFINITION ) || ( seEntries.size() > 0 ) ) {
  3645. if ( seSubtables != null ) {
  3646. Integer tt = Integer.valueOf ( tableType );
  3647. Integer lt = Integer.valueOf ( lookupType );
  3648. Integer ln = Integer.valueOf ( lookupSequence );
  3649. Integer lf = Integer.valueOf ( lookupFlags );
  3650. Integer sn = Integer.valueOf ( subtableSequence );
  3651. Integer sf = Integer.valueOf ( subtableFormat );
  3652. seSubtables.add ( new Object[] { tt, lt, ln, lf, sn, sf, seMapping, seEntries } );
  3653. }
  3654. }
  3655. }
  3656. }
  3657. private void resetATSubState() {
  3658. seMapping = null;
  3659. seEntries = null;
  3660. }
  3661. private void resetATStateAll() {
  3662. resetATState();
  3663. gdef = null;
  3664. gsub = null;
  3665. gpos = null;
  3666. }
  3667. /** helper method for formatting an integer array for output */
  3668. private String toString ( int[] ia ) {
  3669. StringBuffer sb = new StringBuffer();
  3670. if ( ( ia == null ) || ( ia.length == 0 ) ) {
  3671. sb.append ( '-' );
  3672. } else {
  3673. boolean first = true;
  3674. for ( int i = 0; i < ia.length; i++ ) {
  3675. if ( ! first ) {
  3676. sb.append ( ' ' );
  3677. } else {
  3678. first = false;
  3679. }
  3680. sb.append ( ia[i] );
  3681. }
  3682. }
  3683. return sb.toString();
  3684. }
  3685. }