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

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