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.

TTXFile.java 150KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448
  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.ttx;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.nio.IntBuffer;
  22. import java.util.Arrays;
  23. import java.util.ArrayList;
  24. import java.util.Comparator;
  25. import java.util.HashMap;
  26. import java.util.LinkedHashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Set;
  30. import java.util.Stack;
  31. import java.util.TreeMap;
  32. import java.util.Vector;
  33. import javax.xml.parsers.FactoryConfigurationError;
  34. import javax.xml.parsers.ParserConfigurationException;
  35. import javax.xml.parsers.SAXParser;
  36. import javax.xml.parsers.SAXParserFactory;
  37. import org.apache.commons.logging.Log;
  38. import org.apache.commons.logging.LogFactory;
  39. import org.apache.fop.complexscripts.fonts.GlyphClassTable;
  40. import org.apache.fop.complexscripts.fonts.GlyphCoverageTable;
  41. import org.apache.fop.complexscripts.fonts.GlyphDefinitionSubtable;
  42. import org.apache.fop.complexscripts.fonts.GlyphDefinitionTable;
  43. import org.apache.fop.complexscripts.fonts.GlyphMappingTable;
  44. import org.apache.fop.complexscripts.fonts.GlyphPositioningSubtable;
  45. import org.apache.fop.complexscripts.fonts.GlyphPositioningTable;
  46. import org.apache.fop.complexscripts.fonts.GlyphPositioningTable.Anchor;
  47. import org.apache.fop.complexscripts.fonts.GlyphPositioningTable.MarkAnchor;
  48. import org.apache.fop.complexscripts.fonts.GlyphPositioningTable.PairValues;
  49. import org.apache.fop.complexscripts.fonts.GlyphPositioningTable.Value;
  50. import org.apache.fop.complexscripts.fonts.GlyphSubstitutionSubtable;
  51. import org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable;
  52. import org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable.Ligature;
  53. import org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable.LigatureSet;
  54. import org.apache.fop.complexscripts.fonts.GlyphSubtable;
  55. import org.apache.fop.complexscripts.fonts.GlyphTable;
  56. import org.apache.fop.complexscripts.fonts.GlyphTable.RuleLookup;
  57. import org.apache.fop.complexscripts.util.GlyphSequence;
  58. import org.apache.fop.complexscripts.util.UTF32;
  59. import org.apache.fop.util.CharUtilities;
  60. import org.xml.sax.Attributes;
  61. import org.xml.sax.Locator;
  62. import org.xml.sax.SAXException;
  63. import org.xml.sax.helpers.DefaultHandler;
  64. // CSOFF: LineLengthCheck
  65. /**
  66. * This class supports a subset of the <code>TTX</code> file as produced by the Adobe FLEX
  67. * SDK (AFDKO). In particular, it is used to parse a <code>TTX</code> file in order to
  68. * extract character to glyph code mapping data, glyph definition data, glyph substitution
  69. * data, and glyph positioning data.
  70. *
  71. * <code>TTX</code> files are used in FOP for testing and debugging purposes only. Such
  72. * files are used to represent font data employed by complex script processing, and
  73. * normally extracted directly from an opentype (or truetype) file. However, due to
  74. * copyright restrictions, it is not possible to include most opentype (or truetype) font
  75. * files directly in the FOP distribution. In such cases, <code>TTX</code> files are used
  76. * to distribute a subset of the complex script advanced table information contained in
  77. * certain font files to facilitate testing.
  78. *
  79. * @author Glenn Adams
  80. */
  81. public class TTXFile {
  82. /** logging instance */
  83. private static final Log log = LogFactory.getLog(TTXFile.class); // CSOK: ConstantNameCheck
  84. /** default script tag */
  85. private static final String DEFAULT_SCRIPT_TAG = "dflt";
  86. /** default language tag */
  87. private static final String DEFAULT_LANGUAGE_TAG = "dflt";
  88. /** ttxfile cache */
  89. private static Map<String,TTXFile> cache = new HashMap<String,TTXFile>();
  90. // transient parsing state
  91. private Locator locator; // current document locator
  92. private Stack<String[]> elements; // stack of ttx elements being parsed
  93. private Map<String,Integer> glyphIds; // map of glyph names to glyph identifiers
  94. private List<int[]> cmapEntries; // list of <charCode,glyphCode> pairs
  95. private Vector<int[]> hmtxEntries; // vector of <width,lsb> pairs
  96. private Map<String,Integer> glyphClasses; // map of glyph names to glyph classes
  97. private Map<String,Map<String,List<String>>> scripts; // map of script tag to Map<language-tag,List<features-id>>>
  98. private Map<String,List<String>> languages; // map of language tag to List<feature-id>
  99. private Map<String,Object[]> features; // map of feature id to Object[2] : { feature-tag, List<lookup-id> }
  100. private List<String> languageFeatures; // list of language system feature ids, where first is (possibly null) required feature id
  101. private List<String> featureLookups; // list of lookup ids for feature being constructed
  102. private List<Integer> coverageEntries; // list of entries for coverage table being constructed
  103. private Map<String,GlyphCoverageTable> coverages; // map of coverage table keys to coverage tables
  104. private List subtableEntries; // list of lookup subtable entries
  105. private List<GlyphSubtable> subtables; // list of constructed subtables
  106. private List<Integer> alternates; // list of alternates in alternate set being constructed
  107. private List<Ligature> ligatures; // list of ligatures in ligature set being constructed
  108. private List<Integer> substitutes; // list of substitutes in (multiple substitution) sequence being constructed
  109. private List<PairValues> pairs; // list of pair value records being constructed
  110. private List<PairValues[]> pairSets; // list of pair value sets (as arrays) being constructed
  111. private List<Anchor> anchors; // list of anchors of base|mark|component record being constructed
  112. private List<Anchor[]> components; // list of ligature component anchors being constructed
  113. private List<MarkAnchor> markAnchors; // list of mark anchors being constructed
  114. private List<Anchor[]> baseOrMarkAnchors; // list of base|mark2 anchors being constructed
  115. private List<Anchor[][]> ligatureAnchors; // list of ligature anchors being constructed
  116. private List<Anchor[]> attachmentAnchors; // list of entry|exit attachment anchors being constructed
  117. private List<RuleLookup> ruleLookups; // list of rule lookups being constructed
  118. private int glyphIdMax; // maximum glyph id
  119. private int cmPlatform; // plaform id of cmap being constructed
  120. private int cmEncoding; // plaform id of cmap being constructed
  121. private int cmLanguage; // plaform id of cmap being constructed
  122. private int flIndex; // index of feature being constructed
  123. private int flSequence; // feature sequence within feature list
  124. private int ltIndex; // index of lookup table being constructed
  125. private int ltSequence; // lookup sequence within table
  126. private int ltFlags; // flags of current lookup being constructed
  127. private int stSequence; // subtable sequence number within lookup
  128. private int stFormat; // format of current subtable being constructed
  129. private int ctFormat; // format of coverage table being constructed
  130. private int ctIndex; // index of coverage table being constructed
  131. private int rlSequence; // rule lookup sequence index
  132. private int rlLookup; // rule lookup lookup index
  133. private int psIndex; // pair set index
  134. private int vf1; // value format 1 (used with pair pos and single pos)
  135. private int vf2; // value format 2 (used with pair pos)
  136. private int g2; // glyph id 2 (used with pair pos)
  137. private int xCoord; // x coordinate of anchor being constructed
  138. private int yCoord; // y coordinate of anchor being constructed
  139. private int markClass; // mark class of mark anchor being constructed
  140. private String defaultScriptTag; // tag of default script
  141. private String scriptTag; // tag of script being constructed
  142. private String defaultLanguageTag; // tag of default language system
  143. private String languageTag; // tag of language system being constructed
  144. private String featureTag; // tag of feature being constructed
  145. private Value v1; // positioining value 1
  146. private Value v2; // positioining value 2
  147. // resultant state
  148. private int upem; // units per em
  149. private Map<Integer,Integer> cmap; // constructed character map
  150. private Map<Integer,Integer> gmap; // constructed glyph map
  151. private int[][] hmtx; // constructed horizontal metrics - array of design { width, lsb } pairs, indexed by glyph code
  152. private int[] widths; // pdf normalized widths (millipoints)
  153. private GlyphDefinitionTable gdef; // constructed glyph definition table
  154. private GlyphSubstitutionTable gsub; // constructed glyph substitution table
  155. private GlyphPositioningTable gpos; // constructed glyph positioning table
  156. public TTXFile() {
  157. elements = new Stack<String[]>();
  158. glyphIds = new HashMap<String,Integer>();
  159. cmapEntries = new ArrayList<int[]>();
  160. hmtxEntries = new Vector<int[]>();
  161. glyphClasses = new HashMap<String,Integer>();
  162. scripts = new HashMap<String,Map<String,List<String>>>();
  163. languages = new HashMap<String,List<String>>();
  164. features = new HashMap<String,Object[]>();
  165. languageFeatures = new ArrayList<String>();
  166. featureLookups = new ArrayList<String>();
  167. coverageEntries = new ArrayList<Integer>();
  168. coverages = new HashMap<String,GlyphCoverageTable>();
  169. subtableEntries = new ArrayList();
  170. subtables = new ArrayList<GlyphSubtable>();
  171. alternates = new ArrayList<Integer>();
  172. ligatures = new ArrayList<Ligature>();
  173. substitutes = new ArrayList<Integer>();
  174. pairs = new ArrayList<PairValues>();
  175. pairSets = new ArrayList<PairValues[]>();
  176. anchors = new ArrayList<Anchor>();
  177. markAnchors = new ArrayList<MarkAnchor>();
  178. baseOrMarkAnchors = new ArrayList<Anchor[]>();
  179. ligatureAnchors = new ArrayList<Anchor[][]>();
  180. components = new ArrayList<Anchor[]>();
  181. attachmentAnchors = new ArrayList<Anchor[]>();
  182. ruleLookups = new ArrayList<RuleLookup>();
  183. glyphIdMax = -1;
  184. cmPlatform = -1;
  185. cmEncoding = -1;
  186. cmLanguage = -1;
  187. flIndex = -1;
  188. flSequence = 0;
  189. ltIndex = -1;
  190. ltSequence = 0;
  191. ltFlags = 0;
  192. stSequence = 0;
  193. stFormat = 0;
  194. ctFormat = -1;
  195. ctIndex = -1;
  196. rlSequence = -1;
  197. rlLookup = -1;
  198. psIndex = -1;
  199. vf1 = -1;
  200. vf2 = -1;
  201. g2 = -1;
  202. xCoord = Integer.MIN_VALUE;
  203. yCoord = Integer.MIN_VALUE;
  204. markClass = -1;
  205. defaultScriptTag = DEFAULT_SCRIPT_TAG;
  206. scriptTag = null;
  207. defaultLanguageTag = DEFAULT_LANGUAGE_TAG;
  208. languageTag = null;
  209. featureTag = null;
  210. v1 = null;
  211. v2 = null;
  212. upem = -1;
  213. }
  214. public void parse(String filename) {
  215. parse(new File(filename));
  216. }
  217. public void parse(File f) {
  218. assert f != null;
  219. try {
  220. SAXParserFactory spf = SAXParserFactory.newInstance();
  221. SAXParser sp = spf.newSAXParser();
  222. sp.parse(f, new Handler());
  223. } catch (FactoryConfigurationError e) {
  224. throw new RuntimeException(e.getMessage());
  225. } catch (ParserConfigurationException e) {
  226. throw new RuntimeException(e.getMessage());
  227. } catch (SAXException e) {
  228. throw new RuntimeException(e.getMessage());
  229. } catch (IOException e) {
  230. throw new RuntimeException(e.getMessage());
  231. }
  232. }
  233. public GlyphSequence mapCharsToGlyphs(String s) {
  234. Integer[] ca = UTF32.toUTF32(s, 0, true);
  235. int ng = ca.length;
  236. IntBuffer cb = IntBuffer.allocate(ng);
  237. IntBuffer gb = IntBuffer.allocate(ng);
  238. for (Integer c : ca) {
  239. int g = mapCharToGlyph((int) c);
  240. if (g >= 0) {
  241. cb.put(c);
  242. gb.put(g);
  243. } else {
  244. throw new IllegalArgumentException("character " + CharUtilities.format(c) + " has no corresponding glyph");
  245. }
  246. }
  247. cb.rewind();
  248. gb.rewind();
  249. return new GlyphSequence(cb, gb, null);
  250. }
  251. public int mapCharToGlyph(int c) {
  252. if (cmap != null) {
  253. Integer g = cmap.get(Integer.valueOf(c));
  254. if (g != null) {
  255. return (int) g;
  256. } else {
  257. return -1;
  258. }
  259. } else {
  260. return -1;
  261. }
  262. }
  263. public int getGlyph(String gid) {
  264. return mapGlyphId0(gid);
  265. }
  266. public GlyphSequence getGlyphSequence(String[] gids) {
  267. assert gids != null;
  268. int ng = gids.length;
  269. IntBuffer cb = IntBuffer.allocate(ng);
  270. IntBuffer gb = IntBuffer.allocate(ng);
  271. for (String gid : gids) {
  272. int g = mapGlyphId0(gid);
  273. if (g >= 0) {
  274. int c = mapGlyphIdToChar(gid);
  275. if (c < 0) {
  276. c = CharUtilities.NOT_A_CHARACTER;
  277. }
  278. cb.put(c);
  279. gb.put(g);
  280. } else {
  281. throw new IllegalArgumentException("unmapped glyph id \"" + gid + "\"");
  282. }
  283. }
  284. cb.rewind();
  285. gb.rewind();
  286. return new GlyphSequence(cb, gb, null);
  287. }
  288. public int[] getWidths(String[] gids) {
  289. assert gids != null;
  290. int ng = gids.length;
  291. int[] widths = new int [ ng ];
  292. int i = 0;
  293. for (String gid : gids) {
  294. int g = mapGlyphId0(gid);
  295. int w = 0;
  296. if (g >= 0) {
  297. if ((hmtx != null) && (g < hmtx.length)) {
  298. int[] mtx = hmtx [ g ];
  299. assert mtx != null;
  300. assert mtx.length > 0;
  301. w = mtx[0];
  302. }
  303. }
  304. widths [ i++ ] = w;
  305. }
  306. assert i == ng;
  307. return widths;
  308. }
  309. public int[] getWidths() {
  310. if (this.widths == null) {
  311. if ((hmtx != null) && (upem > 0)) {
  312. int[] widths = new int [ hmtx.length ];
  313. for (int i = 0, n = widths.length; i < n; i++) {
  314. widths [ i ] = getPDFWidth(hmtx [ i ] [ 0 ], upem);
  315. }
  316. this.widths = widths;
  317. }
  318. }
  319. return this.widths;
  320. }
  321. public static int getPDFWidth(int tw, int upem) {
  322. // N.B. The following is copied (with minor edits) from TTFFile to insure same results
  323. int pw;
  324. if (tw < 0) {
  325. long rest1 = tw % upem;
  326. long storrest = 1000 * rest1;
  327. long ledd2 = (storrest != 0) ? (rest1 / storrest) : 0;
  328. pw = - ((-1000 * tw) / upem - (int) ledd2);
  329. } else {
  330. pw = (tw / upem) * 1000 + ((tw % upem) * 1000) / upem;
  331. }
  332. return pw;
  333. }
  334. public GlyphDefinitionTable getGDEF() {
  335. return gdef;
  336. }
  337. public GlyphSubstitutionTable getGSUB() {
  338. return gsub;
  339. }
  340. public GlyphPositioningTable getGPOS() {
  341. return gpos;
  342. }
  343. public static synchronized TTXFile getFromCache(String filename) {
  344. assert cache != null;
  345. TTXFile f;
  346. if ((f = (TTXFile) cache.get(filename)) == null) {
  347. f = new TTXFile();
  348. f.parse(filename);
  349. cache.put(filename, f);
  350. }
  351. return f;
  352. }
  353. public static synchronized void clearCache() {
  354. cache.clear();
  355. }
  356. private class Handler extends DefaultHandler {
  357. private Handler() {
  358. }
  359. @Override
  360. public void startDocument() {
  361. }
  362. @Override
  363. public void endDocument() {
  364. }
  365. @Override
  366. public void setDocumentLocator(Locator locator) {
  367. TTXFile.this.locator = locator;
  368. }
  369. @Override
  370. public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
  371. String[] en = makeExpandedName(uri, localName, qName);
  372. if (en[0] != null) {
  373. unsupportedElement(en);
  374. } else if (en[1].equals("Alternate")) {
  375. String[] pn = new String[] { null, "AlternateSet" };
  376. if (isParent(pn)) {
  377. String glyph = attrs.getValue("glyph");
  378. if (glyph == null) {
  379. missingRequiredAttribute(en, "glyph");
  380. }
  381. int gid = mapGlyphId(glyph, en);
  382. alternates.add(Integer.valueOf(gid));
  383. } else {
  384. notPermittedInElementContext(en, getParent(), pn);
  385. }
  386. } else if (en[1].equals("AlternateSet")) {
  387. String[] pn = new String[] { null, "AlternateSubst" };
  388. if (isParent(pn)) {
  389. String glyph = attrs.getValue("glyph");
  390. if (glyph == null) {
  391. missingRequiredAttribute(en, "glyph");
  392. }
  393. int gid = mapGlyphId(glyph, en);
  394. coverageEntries.add(Integer.valueOf(gid));
  395. } else {
  396. notPermittedInElementContext(en, getParent(), pn);
  397. }
  398. } else if (en[1].equals("AlternateSubst")) {
  399. String[] pn = new String[] { null, "Lookup" };
  400. if (isParent(pn)) {
  401. String index = attrs.getValue("index");
  402. if (index == null) {
  403. missingRequiredAttribute(en, "index");
  404. }
  405. String format = attrs.getValue("Format");
  406. int sf = -1;
  407. if (format == null) {
  408. missingRequiredAttribute(en, "Format");
  409. } else {
  410. sf = Integer.parseInt(format);
  411. switch (sf) {
  412. case 1:
  413. break;
  414. default:
  415. unsupportedFormat(en, sf);
  416. break;
  417. }
  418. }
  419. assertCoverageClear();
  420. ctIndex = 0;
  421. ctFormat = 1;
  422. assertSubtableClear();
  423. assert sf >= 0;
  424. stFormat = sf;
  425. } else {
  426. notPermittedInElementContext(en, getParent(), pn);
  427. }
  428. } else if (en[1].equals("BacktrackCoverage")) {
  429. String[] pn1 = new String[] { null, "ChainContextSubst" };
  430. String[] pn2 = new String[] { null, "ChainContextPos" };
  431. String[][] pnx = new String[][] { pn1, pn2 };
  432. if (isParent(pnx)) {
  433. String index = attrs.getValue("index");
  434. int ci = -1;
  435. if (index == null) {
  436. missingRequiredAttribute(en, "index");
  437. } else {
  438. ci = Integer.parseInt(index);
  439. }
  440. String format = attrs.getValue("Format");
  441. int cf = -1;
  442. if (format == null) {
  443. missingRequiredAttribute(en, "Format");
  444. } else {
  445. cf = Integer.parseInt(format);
  446. switch (cf) {
  447. case 1:
  448. case 2:
  449. break;
  450. default:
  451. unsupportedFormat(en, cf);
  452. break;
  453. }
  454. }
  455. assertCoverageClear();
  456. ctIndex = ci;
  457. ctFormat = cf;
  458. } else {
  459. notPermittedInElementContext(en, getParent(), pnx);
  460. }
  461. } else if (en[1].equals("BaseAnchor")) {
  462. String[] pn = new String[] { null, "BaseRecord" };
  463. if (isParent(pn)) {
  464. String index = attrs.getValue("index");
  465. if (index == null) {
  466. missingRequiredAttribute(en, "index");
  467. }
  468. String format = attrs.getValue("Format");
  469. if (format == null) {
  470. missingRequiredAttribute(en, "Format");
  471. }
  472. assert xCoord == Integer.MIN_VALUE;
  473. assert yCoord == Integer.MIN_VALUE;
  474. } else {
  475. notPermittedInElementContext(en, getParent(), pn);
  476. }
  477. } else if (en[1].equals("BaseArray")) {
  478. String[] pn = new String[] { null, "MarkBasePos" };
  479. if (! isParent(pn)) {
  480. notPermittedInElementContext(en, getParent(), pn);
  481. }
  482. } else if (en[1].equals("BaseCoverage")) {
  483. String[] pn = new String[] { null, "MarkBasePos" };
  484. if (isParent(pn)) {
  485. String format = attrs.getValue("Format");
  486. int cf = -1;
  487. if (format == null) {
  488. missingRequiredAttribute(en, "Format");
  489. } else {
  490. cf = Integer.parseInt(format);
  491. switch (cf) {
  492. case 1:
  493. case 2:
  494. break;
  495. default:
  496. unsupportedFormat(en, cf);
  497. break;
  498. }
  499. }
  500. assertCoverageClear();
  501. ctIndex = 0;
  502. ctFormat = cf;
  503. } else {
  504. notPermittedInElementContext(en, getParent(), pn);
  505. }
  506. } else if (en[1].equals("BaseRecord")) {
  507. String[] pn = new String[] { null, "BaseArray" };
  508. if (isParent(pn)) {
  509. String index = attrs.getValue("index");
  510. if (index == null) {
  511. missingRequiredAttribute(en, "index");
  512. }
  513. } else {
  514. notPermittedInElementContext(en, getParent(), pn);
  515. }
  516. } else if (en[1].equals("ChainContextPos") || en[1].equals("ChainContextSubst")) {
  517. String[] pn = new String[] { null, "Lookup" };
  518. if (isParent(pn)) {
  519. String index = attrs.getValue("index");
  520. if (index == null) {
  521. missingRequiredAttribute(en, "index");
  522. }
  523. String format = attrs.getValue("Format");
  524. int sf = -1;
  525. if (format == null) {
  526. missingRequiredAttribute(en, "Format");
  527. } else {
  528. sf = Integer.parseInt(format);
  529. switch (sf) {
  530. case 1:
  531. case 2:
  532. case 3:
  533. break;
  534. default:
  535. unsupportedFormat(en, sf);
  536. break;
  537. }
  538. }
  539. assertSubtableClear();
  540. assert sf >= 0;
  541. stFormat = sf;
  542. } else {
  543. notPermittedInElementContext(en, getParent(), pn);
  544. }
  545. } else if (en[1].equals("Class")) {
  546. String[] pn = new String[] { null, "MarkRecord" };
  547. if (isParent(pn)) {
  548. String value = attrs.getValue("value");
  549. int v = -1;
  550. if (value == null) {
  551. missingRequiredAttribute(en, "value");
  552. } else {
  553. v = Integer.parseInt(value);
  554. }
  555. assert markClass == -1;
  556. markClass = v;
  557. } else {
  558. notPermittedInElementContext(en, getParent(), pn);
  559. }
  560. } else if (en[1].equals("ClassDef")) {
  561. String[] pn1 = new String[] { null, "GlyphClassDef" };
  562. String[] pn2 = new String[] { null, "MarkAttachClassDef" };
  563. String[][] pnx = new String[][] { pn1, pn2 };
  564. if (isParent(pnx)) {
  565. String glyph = attrs.getValue("glyph");
  566. if (glyph == null) {
  567. missingRequiredAttribute(en, "glyph");
  568. }
  569. String glyphClass = attrs.getValue("class");
  570. if (glyphClass == null) {
  571. missingRequiredAttribute(en, "class");
  572. }
  573. if (! glyphIds.containsKey(glyph)) {
  574. unsupportedGlyph(en, glyph);
  575. } else if (isParent(pn1)) {
  576. if (glyphClasses.containsKey(glyph)) {
  577. duplicateGlyphClass(en, glyph, glyphClass);
  578. } else {
  579. glyphClasses.put(glyph, Integer.parseInt(glyphClass));
  580. }
  581. } else if (isParent(pn2)) {
  582. if (glyphClasses.containsKey(glyph)) {
  583. duplicateGlyphClass(en, glyph, glyphClass);
  584. } else {
  585. glyphClasses.put(glyph, Integer.parseInt(glyphClass));
  586. }
  587. }
  588. } else {
  589. notPermittedInElementContext(en, getParent(), pnx);
  590. }
  591. } else if (en[1].equals("ComponentRecord")) {
  592. String[] pn = new String[] { null, "LigatureAttach" };
  593. if (isParent(pn)) {
  594. String index = attrs.getValue("index");
  595. if (index == null) {
  596. missingRequiredAttribute(en, "index");
  597. }
  598. assert anchors.size() == 0;
  599. } else {
  600. notPermittedInElementContext(en, getParent(), pn);
  601. }
  602. } else if (en[1].equals("Coverage")) {
  603. String[] pn1 = new String[] { null, "CursivePos" };
  604. String[] pn2 = new String[] { null, "LigCaretList" };
  605. String[] pn3 = new String[] { null, "MultipleSubst" };
  606. String[] pn4 = new String[] { null, "PairPos" };
  607. String[] pn5 = new String[] { null, "SinglePos" };
  608. String[][] pnx = new String[][] { pn1, pn2, pn3, pn4, pn5 };
  609. if (isParent(pnx)) {
  610. String format = attrs.getValue("Format");
  611. int cf = -1;
  612. if (format == null) {
  613. missingRequiredAttribute(en, "Format");
  614. } else {
  615. cf = Integer.parseInt(format);
  616. switch (cf) {
  617. case 1:
  618. case 2:
  619. break;
  620. default:
  621. unsupportedFormat(en, cf);
  622. break;
  623. }
  624. }
  625. assertCoverageClear();
  626. ctIndex = 0;
  627. ctFormat = cf;
  628. } else {
  629. notPermittedInElementContext(en, getParent(), pnx);
  630. }
  631. } else if (en[1].equals("CursivePos")) {
  632. String[] pn = new String[] { null, "Lookup" };
  633. if (isParent(pn)) {
  634. String index = attrs.getValue("index");
  635. if (index == null) {
  636. missingRequiredAttribute(en, "index");
  637. }
  638. String format = attrs.getValue("Format");
  639. int sf = -1;
  640. if (format == null) {
  641. missingRequiredAttribute(en, "Format");
  642. } else {
  643. sf = Integer.parseInt(format);
  644. switch (sf) {
  645. case 1:
  646. break;
  647. default:
  648. unsupportedFormat(en, sf);
  649. break;
  650. }
  651. }
  652. assertSubtableClear();
  653. assert sf >= 0;
  654. stFormat = sf;
  655. assert attachmentAnchors.size() == 0;
  656. } else {
  657. notPermittedInElementContext(en, getParent(), pn);
  658. }
  659. } else if (en[1].equals("DefaultLangSys")) {
  660. String[] pn = new String[] { null, "Script" };
  661. if (! isParent(pn)) {
  662. notPermittedInElementContext(en, getParent(), pn);
  663. } else {
  664. assertLanguageFeaturesClear();
  665. assert languageTag == null;
  666. languageTag = defaultLanguageTag;
  667. }
  668. } else if (en[1].equals("EntryAnchor")) {
  669. String[] pn = new String[] { null, "EntryExitRecord" };
  670. if (isParent(pn)) {
  671. String format = attrs.getValue("Format");
  672. if (format == null) {
  673. missingRequiredAttribute(en, "Format");
  674. }
  675. assert xCoord == Integer.MIN_VALUE;
  676. assert yCoord == Integer.MIN_VALUE;
  677. } else {
  678. notPermittedInElementContext(en, getParent(), pn);
  679. }
  680. } else if (en[1].equals("EntryExitRecord")) {
  681. String[] pn = new String[] { null, "CursivePos" };
  682. if (isParent(pn)) {
  683. String index = attrs.getValue("index");
  684. if (index == null) {
  685. missingRequiredAttribute(en, "index");
  686. }
  687. } else {
  688. notPermittedInElementContext(en, getParent(), pn);
  689. }
  690. } else if (en[1].equals("ExitAnchor")) {
  691. String[] pn = new String[] { null, "EntryExitRecord" };
  692. if (isParent(pn)) {
  693. String format = attrs.getValue("Format");
  694. if (format == null) {
  695. missingRequiredAttribute(en, "Format");
  696. }
  697. assert xCoord == Integer.MIN_VALUE;
  698. assert yCoord == Integer.MIN_VALUE;
  699. } else {
  700. notPermittedInElementContext(en, getParent(), pn);
  701. }
  702. } else if (en[1].equals("Feature")) {
  703. String[] pn = new String[] { null, "FeatureRecord" };
  704. if (! isParent(pn)) {
  705. notPermittedInElementContext(en, getParent(), pn);
  706. } else {
  707. assertFeatureLookupsClear();
  708. }
  709. } else if (en[1].equals("FeatureIndex")) {
  710. String[] pn1 = new String[] { null, "DefaultLangSys" };
  711. String[] pn2 = new String[] { null, "LangSys" };
  712. String[][] pnx = new String[][] { pn1, pn2 };
  713. if (isParent(pnx)) {
  714. String index = attrs.getValue("index");
  715. if (index == null) {
  716. missingRequiredAttribute(en, "index");
  717. }
  718. String value = attrs.getValue("value");
  719. int v = -1;
  720. if (value == null) {
  721. missingRequiredAttribute(en, "value");
  722. } else {
  723. v = Integer.parseInt(value);
  724. }
  725. if (languageFeatures.size() == 0) {
  726. languageFeatures.add(null);
  727. }
  728. if ((v >= 0) && (v < 65535)) {
  729. languageFeatures.add(makeFeatureId(v));
  730. }
  731. } else {
  732. notPermittedInElementContext(en, getParent(), pnx);
  733. }
  734. } else if (en[1].equals("FeatureList")) {
  735. String[] pn1 = new String[] { null, "GSUB" };
  736. String[] pn2 = new String[] { null, "GPOS" };
  737. String[][] pnx = new String[][] { pn1, pn2 };
  738. if (! isParent(pnx)) {
  739. notPermittedInElementContext(en, getParent(), pnx);
  740. }
  741. } else if (en[1].equals("FeatureRecord")) {
  742. String[] pn = new String[] { null, "FeatureList" };
  743. if (isParent(pn)) {
  744. String index = attrs.getValue("index");
  745. int fi = -1;
  746. if (index == null) {
  747. missingRequiredAttribute(en, "index");
  748. } else {
  749. fi = Integer.parseInt(index);
  750. }
  751. assertFeatureClear();
  752. assert flIndex == -1;
  753. flIndex = fi;
  754. } else {
  755. notPermittedInElementContext(en, getParent(), pn);
  756. }
  757. } else if (en[1].equals("FeatureTag")) {
  758. String[] pn = new String[] { null, "FeatureRecord" };
  759. if (isParent(pn)) {
  760. String value = attrs.getValue("value");
  761. if (value == null) {
  762. missingRequiredAttribute(en, "value");
  763. } else {
  764. assert featureTag == null;
  765. featureTag = value;
  766. }
  767. } else {
  768. notPermittedInElementContext(en, getParent(), pn);
  769. }
  770. } else if (en[1].equals("GDEF")) {
  771. String[] pn = new String[] { null, "ttFont" };
  772. if (isParent(pn)) {
  773. assertSubtablesClear();
  774. } else {
  775. notPermittedInElementContext(en, getParent(), pn);
  776. }
  777. } else if (en[1].equals("GPOS")) {
  778. String[] pn = new String[] { null, "ttFont" };
  779. if (isParent(pn)) {
  780. assertCoveragesClear();
  781. assertSubtablesClear();
  782. } else {
  783. notPermittedInElementContext(en, getParent(), pn);
  784. }
  785. } else if (en[1].equals("GSUB")) {
  786. String[] pn = new String[] { null, "ttFont" };
  787. if (isParent(pn)) {
  788. assertCoveragesClear();
  789. assertSubtablesClear();
  790. } else {
  791. notPermittedInElementContext(en, getParent(), pn);
  792. }
  793. } else if (en[1].equals("Glyph")) {
  794. String[] pn1 = new String[] { null, "Coverage" };
  795. String[] pn2 = new String[] { null, "InputCoverage" };
  796. String[] pn3 = new String[] { null, "LookAheadCoverage" };
  797. String[] pn4 = new String[] { null, "BacktrackCoverage" };
  798. String[] pn5 = new String[] { null, "MarkCoverage" };
  799. String[] pn6 = new String[] { null, "Mark1Coverage" };
  800. String[] pn7 = new String[] { null, "Mark2Coverage" };
  801. String[] pn8 = new String[] { null, "BaseCoverage" };
  802. String[] pn9 = new String[] { null, "LigatureCoverage" };
  803. String[][] pnx = new String[][] { pn1, pn2, pn3, pn4, pn5, pn6, pn7, pn8, pn9 };
  804. if (isParent(pnx)) {
  805. String value = attrs.getValue("value");
  806. if (value == null) {
  807. missingRequiredAttribute(en, "value");
  808. } else {
  809. int gid = mapGlyphId(value, en);
  810. coverageEntries.add(Integer.valueOf(gid));
  811. }
  812. } else {
  813. notPermittedInElementContext(en, getParent(), pnx);
  814. }
  815. } else if (en[1].equals("GlyphClassDef")) {
  816. String[] pn = new String[] { null, "GDEF" };
  817. if (isParent(pn)) {
  818. String format = attrs.getValue("Format");
  819. int sf = -1;
  820. if (format == null) {
  821. missingRequiredAttribute(en, "Format");
  822. } else {
  823. sf = Integer.parseInt(format);
  824. switch (sf) {
  825. case 1:
  826. case 2:
  827. break;
  828. default:
  829. unsupportedFormat(en, sf);
  830. break;
  831. }
  832. }
  833. assertSubtableClear();
  834. assert sf >= 0;
  835. // force format 1 since TTX always writes entries as non-range entries
  836. if (sf != 1) {
  837. sf = 1;
  838. }
  839. stFormat = sf;
  840. assert glyphClasses.isEmpty();
  841. } else {
  842. notPermittedInElementContext(en, getParent(), pn);
  843. }
  844. } else if (en[1].equals("GlyphID")) {
  845. String[] pn = new String[] { null, "GlyphOrder" };
  846. if (isParent(pn)) {
  847. String id = attrs.getValue("id");
  848. int gid = -1;
  849. if (id == null) {
  850. missingRequiredAttribute(en, "id");
  851. } else {
  852. gid = Integer.parseInt(id);
  853. }
  854. String name = attrs.getValue("name");
  855. if (name == null) {
  856. missingRequiredAttribute(en, "name");
  857. }
  858. if (glyphIds.containsKey(name)) {
  859. duplicateGlyph(en, name, gid);
  860. } else {
  861. if (gid > glyphIdMax) {
  862. glyphIdMax = gid;
  863. }
  864. glyphIds.put(name, gid);
  865. }
  866. } else {
  867. notPermittedInElementContext(en, getParent(), pn);
  868. }
  869. } else if (en[1].equals("GlyphOrder")) {
  870. String[] pn = new String[] { null, "ttFont" };
  871. if (! isParent(pn)) {
  872. notPermittedInElementContext(en, getParent(), pn);
  873. }
  874. } else if (en[1].equals("InputCoverage")) {
  875. String[] pn1 = new String[] { null, "ChainContextSubst" };
  876. String[] pn2 = new String[] { null, "ChainContextPos" };
  877. String[][] pnx = new String[][] { pn1, pn2 };
  878. if (isParent(pnx)) {
  879. String index = attrs.getValue("index");
  880. int ci = -1;
  881. if (index == null) {
  882. missingRequiredAttribute(en, "index");
  883. } else {
  884. ci = Integer.parseInt(index);
  885. }
  886. String format = attrs.getValue("Format");
  887. int cf = -1;
  888. if (format == null) {
  889. missingRequiredAttribute(en, "Format");
  890. } else {
  891. cf = Integer.parseInt(format);
  892. switch (cf) {
  893. case 1:
  894. case 2:
  895. break;
  896. default:
  897. unsupportedFormat(en, cf);
  898. break;
  899. }
  900. }
  901. assertCoverageClear();
  902. ctIndex = ci;
  903. ctFormat = cf;
  904. } else {
  905. notPermittedInElementContext(en, getParent(), pnx);
  906. }
  907. } else if (en[1].equals("LangSys")) {
  908. String[] pn = new String[] { null, "LangSysRecord" };
  909. if (! isParent(pn)) {
  910. notPermittedInElementContext(en, getParent(), pn);
  911. } else {
  912. assertLanguageFeaturesClear();
  913. }
  914. } else if (en[1].equals("LangSysRecord")) {
  915. String[] pn = new String[] { null, "Script" };
  916. if (isParent(pn)) {
  917. String index = attrs.getValue("index");
  918. if (index == null) {
  919. missingRequiredAttribute(en, "index");
  920. }
  921. } else {
  922. notPermittedInElementContext(en, getParent(), pn);
  923. }
  924. } else if (en[1].equals("LangSysTag")) {
  925. String[] pn = new String[] { null, "LangSysRecord" };
  926. if (isParent(pn)) {
  927. String value = attrs.getValue("value");
  928. if (value == null) {
  929. missingRequiredAttribute(en, "value");
  930. } else {
  931. assert languageTag == null;
  932. languageTag = value;
  933. }
  934. } else {
  935. notPermittedInElementContext(en, getParent(), pn);
  936. }
  937. } else if (en[1].equals("LigCaretList")) {
  938. String[] pn = new String[] { null, "GDEF" };
  939. if (! isParent(pn)) {
  940. notPermittedInElementContext(en, getParent(), pn);
  941. }
  942. } else if (en[1].equals("Ligature")) {
  943. String[] pn = new String[] { null, "LigatureSet" };
  944. if (isParent(pn)) {
  945. String components = attrs.getValue("components");
  946. if (components == null) {
  947. missingRequiredAttribute(en, "components");
  948. }
  949. int[] cids = mapGlyphIds(components, en);
  950. String glyph = attrs.getValue("glyph");
  951. if (glyph == null) {
  952. missingRequiredAttribute(en, "glyph");
  953. }
  954. int gid = mapGlyphId(glyph, en);
  955. ligatures.add(new Ligature(gid, cids));
  956. } else {
  957. notPermittedInElementContext(en, getParent(), pn);
  958. }
  959. } else if (en[1].equals("LigatureAnchor")) {
  960. String[] pn = new String[] { null, "ComponentRecord" };
  961. if (isParent(pn)) {
  962. String index = attrs.getValue("index");
  963. if (index == null) {
  964. missingRequiredAttribute(en, "index");
  965. }
  966. String format = attrs.getValue("Format");
  967. if (format == null) {
  968. missingRequiredAttribute(en, "Format");
  969. }
  970. assert xCoord == Integer.MIN_VALUE;
  971. assert yCoord == Integer.MIN_VALUE;
  972. } else {
  973. notPermittedInElementContext(en, getParent(), pn);
  974. }
  975. } else if (en[1].equals("LigatureArray")) {
  976. String[] pn = new String[] { null, "MarkLigPos" };
  977. if (! isParent(pn)) {
  978. notPermittedInElementContext(en, getParent(), pn);
  979. }
  980. } else if (en[1].equals("LigatureAttach")) {
  981. String[] pn = new String[] { null, "LigatureArray" };
  982. if (isParent(pn)) {
  983. String index = attrs.getValue("index");
  984. if (index == null) {
  985. missingRequiredAttribute(en, "index");
  986. }
  987. assert components.size() == 0;
  988. } else {
  989. notPermittedInElementContext(en, getParent(), pn);
  990. }
  991. } else if (en[1].equals("LigatureCoverage")) {
  992. String[] pn = new String[] { null, "MarkLigPos" };
  993. if (isParent(pn)) {
  994. String format = attrs.getValue("Format");
  995. int cf = -1;
  996. if (format == null) {
  997. missingRequiredAttribute(en, "Format");
  998. } else {
  999. cf = Integer.parseInt(format);
  1000. switch (cf) {
  1001. case 1:
  1002. case 2:
  1003. break;
  1004. default:
  1005. unsupportedFormat(en, cf);
  1006. break;
  1007. }
  1008. }
  1009. assertCoverageClear();
  1010. ctIndex = 0;
  1011. ctFormat = cf;
  1012. } else {
  1013. notPermittedInElementContext(en, getParent(), pn);
  1014. }
  1015. } else if (en[1].equals("LigatureSet")) {
  1016. String[] pn = new String[] { null, "LigatureSubst" };
  1017. if (isParent(pn)) {
  1018. String glyph = attrs.getValue("glyph");
  1019. if (glyph == null) {
  1020. missingRequiredAttribute(en, "glyph");
  1021. }
  1022. int gid = mapGlyphId(glyph, en);
  1023. coverageEntries.add(Integer.valueOf(gid));
  1024. } else {
  1025. notPermittedInElementContext(en, getParent(), pn);
  1026. }
  1027. } else if (en[1].equals("LigatureSubst")) {
  1028. String[] pn = new String[] { null, "Lookup" };
  1029. if (isParent(pn)) {
  1030. String index = attrs.getValue("index");
  1031. if (index == null) {
  1032. missingRequiredAttribute(en, "index");
  1033. }
  1034. String format = attrs.getValue("Format");
  1035. int sf = -1;
  1036. if (format == null) {
  1037. missingRequiredAttribute(en, "Format");
  1038. } else {
  1039. sf = Integer.parseInt(format);
  1040. switch (sf) {
  1041. case 1:
  1042. break;
  1043. default:
  1044. unsupportedFormat(en, sf);
  1045. break;
  1046. }
  1047. }
  1048. assertCoverageClear();
  1049. ctIndex = 0;
  1050. ctFormat = 1;
  1051. assertSubtableClear();
  1052. assert sf >= 0;
  1053. stFormat = sf;
  1054. } else {
  1055. notPermittedInElementContext(en, getParent(), pn);
  1056. }
  1057. } else if (en[1].equals("LookAheadCoverage")) {
  1058. String[] pn1 = new String[] { null, "ChainContextSubst" };
  1059. String[] pn2 = new String[] { null, "ChainContextPos" };
  1060. String[][] pnx = new String[][] { pn1, pn2 };
  1061. if (isParent(pnx)) {
  1062. String index = attrs.getValue("index");
  1063. int ci = -1;
  1064. if (index == null) {
  1065. missingRequiredAttribute(en, "index");
  1066. } else {
  1067. ci = Integer.parseInt(index);
  1068. }
  1069. String format = attrs.getValue("Format");
  1070. int cf = -1;
  1071. if (format == null) {
  1072. missingRequiredAttribute(en, "Format");
  1073. } else {
  1074. cf = Integer.parseInt(format);
  1075. switch (cf) {
  1076. case 1:
  1077. case 2:
  1078. break;
  1079. default:
  1080. unsupportedFormat(en, cf);
  1081. break;
  1082. }
  1083. }
  1084. assertCoverageClear();
  1085. ctIndex = ci;
  1086. ctFormat = cf;
  1087. } else {
  1088. notPermittedInElementContext(en, getParent(), pnx);
  1089. }
  1090. } else if (en[1].equals("Lookup")) {
  1091. String[] pn = new String[] { null, "LookupList" };
  1092. if (isParent(pn)) {
  1093. String index = attrs.getValue("index");
  1094. int li = -1;
  1095. if (index == null) {
  1096. missingRequiredAttribute(en, "index");
  1097. } else {
  1098. li = Integer.parseInt(index);
  1099. }
  1100. assertLookupClear();
  1101. assert ltIndex == -1;
  1102. ltIndex = li;
  1103. } else {
  1104. notPermittedInElementContext(en, getParent(), pn);
  1105. }
  1106. } else if (en[1].equals("LookupFlag")) {
  1107. String[] pn = new String[] { null, "Lookup" };
  1108. if (isParent(pn)) {
  1109. String value = attrs.getValue("value");
  1110. int lf = 0;
  1111. if (value == null) {
  1112. missingRequiredAttribute(en, "value");
  1113. } else {
  1114. lf = Integer.parseInt(value);
  1115. }
  1116. assert ltFlags == 0;
  1117. ltFlags = lf;
  1118. } else {
  1119. notPermittedInElementContext(en, getParent(), pn);
  1120. }
  1121. } else if (en[1].equals("LookupList")) {
  1122. String[] pn1 = new String[] { null, "GSUB" };
  1123. String[] pn2 = new String[] { null, "GPOS" };
  1124. String[][] pnx = new String[][] { pn1, pn2 };
  1125. if (! isParent(pnx)) {
  1126. notPermittedInElementContext(en, getParent(), pnx);
  1127. }
  1128. } else if (en[1].equals("LookupListIndex")) {
  1129. String[] pn1 = new String[] { null, "Feature" };
  1130. String[] pn2 = new String[] { null, "SubstLookupRecord" };
  1131. String[] pn3 = new String[] { null, "PosLookupRecord" };
  1132. String[][] pnx = new String[][] { pn1, pn2, pn3 };
  1133. if (isParent(pnx)) {
  1134. String index = attrs.getValue("index");
  1135. String value = attrs.getValue("value");
  1136. int v = -1;
  1137. if (value == null) {
  1138. missingRequiredAttribute(en, "value");
  1139. } else {
  1140. v = Integer.parseInt(value);
  1141. }
  1142. String[][] pny = new String[][] { pn2, pn3 };
  1143. if (isParent(pny)) {
  1144. assert rlLookup == -1;
  1145. assert v != -1;
  1146. rlLookup = v;
  1147. } else {
  1148. featureLookups.add(makeLookupId(v));
  1149. }
  1150. } else {
  1151. notPermittedInElementContext(en, getParent(), pnx);
  1152. }
  1153. } else if (en[1].equals("LookupType")) {
  1154. String[] pn = new String[] { null, "Lookup" };
  1155. if (isParent(pn)) {
  1156. String value = attrs.getValue("value");
  1157. if (value == null) {
  1158. missingRequiredAttribute(en, "value");
  1159. }
  1160. } else {
  1161. notPermittedInElementContext(en, getParent(), pn);
  1162. }
  1163. } else if (en[1].equals("Mark1Array")) {
  1164. String[] pn = new String[] { null, "MarkMarkPos" };
  1165. if (! isParent(pn)) {
  1166. notPermittedInElementContext(en, getParent(), pn);
  1167. }
  1168. } else if (en[1].equals("Mark1Coverage")) {
  1169. String[] pn = new String[] { null, "MarkMarkPos" };
  1170. if (isParent(pn)) {
  1171. String format = attrs.getValue("Format");
  1172. int cf = -1;
  1173. if (format == null) {
  1174. missingRequiredAttribute(en, "Format");
  1175. } else {
  1176. cf = Integer.parseInt(format);
  1177. switch (cf) {
  1178. case 1:
  1179. case 2:
  1180. break;
  1181. default:
  1182. unsupportedFormat(en, cf);
  1183. break;
  1184. }
  1185. }
  1186. assertCoverageClear();
  1187. ctIndex = 0;
  1188. ctFormat = cf;
  1189. } else {
  1190. notPermittedInElementContext(en, getParent(), pn);
  1191. }
  1192. } else if (en[1].equals("Mark2Anchor")) {
  1193. String[] pn = new String[] { null, "Mark2Record" };
  1194. if (isParent(pn)) {
  1195. String format = attrs.getValue("Format");
  1196. if (format == null) {
  1197. missingRequiredAttribute(en, "Format");
  1198. }
  1199. assert xCoord == Integer.MIN_VALUE;
  1200. assert yCoord == Integer.MIN_VALUE;
  1201. } else {
  1202. notPermittedInElementContext(en, getParent(), pn);
  1203. }
  1204. } else if (en[1].equals("Mark2Array")) {
  1205. String[] pn = new String[] { null, "MarkMarkPos" };
  1206. if (! isParent(pn)) {
  1207. notPermittedInElementContext(en, getParent(), pn);
  1208. }
  1209. } else if (en[1].equals("Mark2Coverage")) {
  1210. String[] pn = new String[] { null, "MarkMarkPos" };
  1211. if (isParent(pn)) {
  1212. String format = attrs.getValue("Format");
  1213. int cf = -1;
  1214. if (format == null) {
  1215. missingRequiredAttribute(en, "Format");
  1216. } else {
  1217. cf = Integer.parseInt(format);
  1218. switch (cf) {
  1219. case 1:
  1220. case 2:
  1221. break;
  1222. default:
  1223. unsupportedFormat(en, cf);
  1224. break;
  1225. }
  1226. }
  1227. assertCoverageClear();
  1228. ctIndex = 0;
  1229. ctFormat = cf;
  1230. } else {
  1231. notPermittedInElementContext(en, getParent(), pn);
  1232. }
  1233. } else if (en[1].equals("Mark2Record")) {
  1234. String[] pn = new String[] { null, "Mark2Array" };
  1235. if (isParent(pn)) {
  1236. String index = attrs.getValue("index");
  1237. if (index == null) {
  1238. missingRequiredAttribute(en, "index");
  1239. }
  1240. } else {
  1241. notPermittedInElementContext(en, getParent(), pn);
  1242. }
  1243. } else if (en[1].equals("MarkAnchor")) {
  1244. String[] pn = new String[] { null, "MarkRecord" };
  1245. if (isParent(pn)) {
  1246. String format = attrs.getValue("Format");
  1247. if (format == null) {
  1248. missingRequiredAttribute(en, "Format");
  1249. }
  1250. assert xCoord == Integer.MIN_VALUE;
  1251. assert yCoord == Integer.MIN_VALUE;
  1252. } else {
  1253. notPermittedInElementContext(en, getParent(), pn);
  1254. }
  1255. } else if (en[1].equals("MarkArray")) {
  1256. String[] pn1 = new String[] { null, "MarkBasePos" };
  1257. String[] pn2 = new String[] { null, "MarkLigPos" };
  1258. String[][] pnx = new String[][] { pn1, pn2 };
  1259. if (! isParent(pnx)) {
  1260. notPermittedInElementContext(en, getParent(), pnx);
  1261. }
  1262. } else if (en[1].equals("MarkAttachClassDef")) {
  1263. String[] pn = new String[] { null, "GDEF" };
  1264. if (isParent(pn)) {
  1265. String format = attrs.getValue("Format");
  1266. int sf = -1;
  1267. if (format == null) {
  1268. missingRequiredAttribute(en, "Format");
  1269. } else {
  1270. sf = Integer.parseInt(format);
  1271. switch (sf) {
  1272. case 1:
  1273. case 2:
  1274. break;
  1275. default:
  1276. unsupportedFormat(en, sf);
  1277. break;
  1278. }
  1279. }
  1280. assertSubtableClear();
  1281. assert sf >= 0;
  1282. // force format 1 since TTX always writes entries as non-range entries
  1283. if (sf != 1) {
  1284. sf = 1;
  1285. }
  1286. stFormat = sf;
  1287. assert glyphClasses.isEmpty();
  1288. } else {
  1289. notPermittedInElementContext(en, getParent(), pn);
  1290. }
  1291. } else if (en[1].equals("MarkBasePos")) {
  1292. String[] pn = new String[] { null, "Lookup" };
  1293. if (isParent(pn)) {
  1294. String index = attrs.getValue("index");
  1295. if (index == null) {
  1296. missingRequiredAttribute(en, "index");
  1297. }
  1298. String format = attrs.getValue("Format");
  1299. int sf = -1;
  1300. if (format == null) {
  1301. missingRequiredAttribute(en, "Format");
  1302. } else {
  1303. sf = Integer.parseInt(format);
  1304. switch (sf) {
  1305. case 1:
  1306. break;
  1307. default:
  1308. unsupportedFormat(en, sf);
  1309. break;
  1310. }
  1311. }
  1312. assertSubtableClear();
  1313. assert sf >= 0;
  1314. stFormat = sf;
  1315. assert markAnchors.size() == 0;
  1316. assert baseOrMarkAnchors.size() == 0;
  1317. } else {
  1318. notPermittedInElementContext(en, getParent(), pn);
  1319. }
  1320. } else if (en[1].equals("MarkCoverage")) {
  1321. String[] pn1 = new String[] { null, "MarkBasePos" };
  1322. String[] pn2 = new String[] { null, "MarkLigPos" };
  1323. String[][] pnx = new String[][] { pn1, pn2 };
  1324. if (isParent(pnx)) {
  1325. String format = attrs.getValue("Format");
  1326. int cf = -1;
  1327. if (format == null) {
  1328. missingRequiredAttribute(en, "Format");
  1329. } else {
  1330. cf = Integer.parseInt(format);
  1331. switch (cf) {
  1332. case 1:
  1333. case 2:
  1334. break;
  1335. default:
  1336. unsupportedFormat(en, cf);
  1337. break;
  1338. }
  1339. }
  1340. assertCoverageClear();
  1341. ctIndex = 0;
  1342. ctFormat = cf;
  1343. } else {
  1344. notPermittedInElementContext(en, getParent(), pnx);
  1345. }
  1346. } else if (en[1].equals("MarkLigPos")) {
  1347. String[] pn = new String[] { null, "Lookup" };
  1348. if (isParent(pn)) {
  1349. String index = attrs.getValue("index");
  1350. if (index == null) {
  1351. missingRequiredAttribute(en, "index");
  1352. }
  1353. String format = attrs.getValue("Format");
  1354. int sf = -1;
  1355. if (format == null) {
  1356. missingRequiredAttribute(en, "Format");
  1357. } else {
  1358. sf = Integer.parseInt(format);
  1359. switch (sf) {
  1360. case 1:
  1361. break;
  1362. default:
  1363. unsupportedFormat(en, sf);
  1364. break;
  1365. }
  1366. }
  1367. assertSubtableClear();
  1368. assert sf >= 0;
  1369. stFormat = sf;
  1370. assert markAnchors.size() == 0;
  1371. assert ligatureAnchors.size() == 0;
  1372. } else {
  1373. notPermittedInElementContext(en, getParent(), pn);
  1374. }
  1375. } else if (en[1].equals("MarkMarkPos")) {
  1376. String[] pn = new String[] { null, "Lookup" };
  1377. if (isParent(pn)) {
  1378. String index = attrs.getValue("index");
  1379. if (index == null) {
  1380. missingRequiredAttribute(en, "index");
  1381. }
  1382. String format = attrs.getValue("Format");
  1383. int sf = -1;
  1384. if (format == null) {
  1385. missingRequiredAttribute(en, "Format");
  1386. } else {
  1387. sf = Integer.parseInt(format);
  1388. switch (sf) {
  1389. case 1:
  1390. break;
  1391. default:
  1392. unsupportedFormat(en, sf);
  1393. break;
  1394. }
  1395. }
  1396. assertSubtableClear();
  1397. assert sf >= 0;
  1398. stFormat = sf;
  1399. assert markAnchors.size() == 0;
  1400. assert baseOrMarkAnchors.size() == 0;
  1401. } else {
  1402. notPermittedInElementContext(en, getParent(), pn);
  1403. }
  1404. } else if (en[1].equals("MarkRecord")) {
  1405. String[] pn1 = new String[] { null, "MarkArray" };
  1406. String[] pn2 = new String[] { null, "Mark1Array" };
  1407. String[][] pnx = new String[][] { pn1, pn2 };
  1408. if (isParent(pnx)) {
  1409. String index = attrs.getValue("index");
  1410. if (index == null) {
  1411. missingRequiredAttribute(en, "index");
  1412. }
  1413. } else {
  1414. notPermittedInElementContext(en, getParent(), pnx);
  1415. }
  1416. } else if (en[1].equals("MultipleSubst")) {
  1417. String[] pn = new String[] { null, "Lookup" };
  1418. if (isParent(pn)) {
  1419. String index = attrs.getValue("index");
  1420. if (index == null) {
  1421. missingRequiredAttribute(en, "index");
  1422. }
  1423. String format = attrs.getValue("Format");
  1424. int sf = -1;
  1425. if (format == null) {
  1426. missingRequiredAttribute(en, "Format");
  1427. } else {
  1428. sf = Integer.parseInt(format);
  1429. switch (sf) {
  1430. case 1:
  1431. break;
  1432. default:
  1433. unsupportedFormat(en, sf);
  1434. break;
  1435. }
  1436. }
  1437. assertSubtableClear();
  1438. assert sf >= 0;
  1439. stFormat = sf;
  1440. } else {
  1441. notPermittedInElementContext(en, getParent(), pn);
  1442. }
  1443. } else if (en[1].equals("PairPos")) {
  1444. String[] pn = new String[] { null, "Lookup" };
  1445. if (isParent(pn)) {
  1446. String index = attrs.getValue("index");
  1447. if (index == null) {
  1448. missingRequiredAttribute(en, "index");
  1449. }
  1450. String format = attrs.getValue("Format");
  1451. int sf = -1;
  1452. if (format == null) {
  1453. missingRequiredAttribute(en, "Format");
  1454. } else {
  1455. sf = Integer.parseInt(format);
  1456. switch (sf) {
  1457. case 1:
  1458. case 2:
  1459. break;
  1460. default:
  1461. unsupportedFormat(en, sf);
  1462. break;
  1463. }
  1464. }
  1465. assertSubtableClear();
  1466. assert sf >= 0;
  1467. stFormat = sf;
  1468. } else {
  1469. notPermittedInElementContext(en, getParent(), pn);
  1470. }
  1471. } else if (en[1].equals("PairSet")) {
  1472. String[] pn = new String[] { null, "PairPos" };
  1473. if (isParent(pn)) {
  1474. String index = attrs.getValue("index");
  1475. int psi = -1;
  1476. if (index == null) {
  1477. missingRequiredAttribute(en, "index");
  1478. } else {
  1479. psi = Integer.parseInt(index);
  1480. }
  1481. assert psIndex == -1;
  1482. psIndex = psi;
  1483. } else {
  1484. notPermittedInElementContext(en, getParent(), pn);
  1485. }
  1486. } else if (en[1].equals("PairValueRecord")) {
  1487. String[] pn = new String[] { null, "PairSet" };
  1488. if (isParent(pn)) {
  1489. String index = attrs.getValue("index");
  1490. if (index == null) {
  1491. missingRequiredAttribute(en, "index");
  1492. } else {
  1493. assertPairClear();
  1494. }
  1495. } else {
  1496. notPermittedInElementContext(en, getParent(), pn);
  1497. }
  1498. } else if (en[1].equals("PosLookupRecord")) {
  1499. String[] pn1 = new String[] { null, "ChainContextSubst" };
  1500. String[] pn2 = new String[] { null, "ChainContextPos" };
  1501. String[][] pnx = new String[][] { pn1, pn2 };
  1502. if (isParent(pnx)) {
  1503. String index = attrs.getValue("index");
  1504. if (index == null) {
  1505. missingRequiredAttribute(en, "index");
  1506. }
  1507. } else {
  1508. notPermittedInElementContext(en, getParent(), pnx);
  1509. }
  1510. } else if (en[1].equals("ReqFeatureIndex")) {
  1511. String[] pn1 = new String[] { null, "DefaultLangSys" };
  1512. String[] pn2 = new String[] { null, "LangSys" };
  1513. String[][] pnx = new String[][] { pn1, pn2 };
  1514. if (isParent(pnx)) {
  1515. String value = attrs.getValue("value");
  1516. int v = -1;
  1517. if (value == null) {
  1518. missingRequiredAttribute(en, "value");
  1519. } else {
  1520. v = Integer.parseInt(value);
  1521. }
  1522. String fid;
  1523. if ((v >= 0) && (v < 65535)) {
  1524. fid = makeFeatureId(v);
  1525. } else {
  1526. fid = null;
  1527. }
  1528. assertLanguageFeaturesClear();
  1529. languageFeatures.add(fid);
  1530. } else {
  1531. notPermittedInElementContext(en, getParent(), pnx);
  1532. }
  1533. } else if (en[1].equals("Script")) {
  1534. String[] pn = new String[] { null, "ScriptRecord" };
  1535. if (! isParent(pn)) {
  1536. notPermittedInElementContext(en, getParent(), pn);
  1537. }
  1538. } else if (en[1].equals("ScriptList")) {
  1539. String[] pn1 = new String[] { null, "GSUB" };
  1540. String[] pn2 = new String[] { null, "GPOS" };
  1541. String[][] pnx = new String[][] { pn1, pn2 };
  1542. if (! isParent(pnx)) {
  1543. notPermittedInElementContext(en, getParent(), pnx);
  1544. }
  1545. } else if (en[1].equals("ScriptRecord")) {
  1546. String[] pn = new String[] { null, "ScriptList" };
  1547. if (isParent(pn)) {
  1548. String index = attrs.getValue("index");
  1549. if (index == null) {
  1550. missingRequiredAttribute(en, "index");
  1551. }
  1552. } else {
  1553. notPermittedInElementContext(en, getParent(), pn);
  1554. }
  1555. } else if (en[1].equals("ScriptTag")) {
  1556. String[] pn = new String[] { null, "ScriptRecord" };
  1557. if (isParent(pn)) {
  1558. String value = attrs.getValue("value");
  1559. if (value == null) {
  1560. missingRequiredAttribute(en, "value");
  1561. } else {
  1562. assert scriptTag == null;
  1563. scriptTag = value;
  1564. }
  1565. } else {
  1566. notPermittedInElementContext(en, getParent(), pn);
  1567. }
  1568. } else if (en[1].equals("SecondGlyph")) {
  1569. String[] pn = new String[] { null, "PairValueRecord" };
  1570. if (isParent(pn)) {
  1571. String value = attrs.getValue("value");
  1572. if (value == null) {
  1573. missingRequiredAttribute(en, "value");
  1574. } else {
  1575. int gid = mapGlyphId(value, en);
  1576. assert g2 == -1;
  1577. g2 = gid;
  1578. }
  1579. } else {
  1580. notPermittedInElementContext(en, getParent(), pn);
  1581. }
  1582. } else if (en[1].equals("Sequence")) {
  1583. String[] pn = new String[] { null, "MultipleSubst" };
  1584. if (isParent(pn)) {
  1585. String index = attrs.getValue("index");
  1586. if (index == null) {
  1587. missingRequiredAttribute(en, "index");
  1588. } else {
  1589. int i = Integer.parseInt(index);
  1590. if (i != subtableEntries.size()) {
  1591. invalidIndex(en, i, subtableEntries.size());
  1592. }
  1593. }
  1594. } else {
  1595. notPermittedInElementContext(en, getParent(), pn);
  1596. }
  1597. } else if (en[1].equals("SequenceIndex")) {
  1598. String[] pn1 = new String[] { null, "PosLookupRecord" };
  1599. String[] pn2 = new String[] { null, "SubstLookupRecord" };
  1600. String[][] pnx = new String[][] { pn1, pn2 };
  1601. if (isParent(pnx)) {
  1602. String value = attrs.getValue("value");
  1603. int v = -1;
  1604. if (value == null) {
  1605. missingRequiredAttribute(en, "value");
  1606. } else {
  1607. v = Integer.parseInt(value);
  1608. }
  1609. assert rlSequence == -1;
  1610. assert v != -1;
  1611. rlSequence = v;
  1612. } else {
  1613. notPermittedInElementContext(en, getParent(), pnx);
  1614. }
  1615. } else if (en[1].equals("SinglePos")) {
  1616. String[] pn = new String[] { null, "Lookup" };
  1617. if (isParent(pn)) {
  1618. String index = attrs.getValue("index");
  1619. if (index == null) {
  1620. missingRequiredAttribute(en, "index");
  1621. }
  1622. String format = attrs.getValue("Format");
  1623. int sf = -1;
  1624. if (format == null) {
  1625. missingRequiredAttribute(en, "Format");
  1626. } else {
  1627. sf = Integer.parseInt(format);
  1628. switch (sf) {
  1629. case 1:
  1630. case 2:
  1631. break;
  1632. default:
  1633. unsupportedFormat(en, sf);
  1634. break;
  1635. }
  1636. }
  1637. assertSubtableClear();
  1638. assert sf >= 0;
  1639. stFormat = sf;
  1640. } else {
  1641. notPermittedInElementContext(en, getParent(), pn);
  1642. }
  1643. } else if (en[1].equals("SingleSubst")) {
  1644. String[] pn = new String[] { null, "Lookup" };
  1645. if (isParent(pn)) {
  1646. String index = attrs.getValue("index");
  1647. if (index == null) {
  1648. missingRequiredAttribute(en, "index");
  1649. }
  1650. String format = attrs.getValue("Format");
  1651. int sf = -1;
  1652. if (format == null) {
  1653. missingRequiredAttribute(en, "Format");
  1654. } else {
  1655. sf = Integer.parseInt(format);
  1656. switch (sf) {
  1657. case 1:
  1658. case 2:
  1659. break;
  1660. default:
  1661. unsupportedFormat(en, sf);
  1662. break;
  1663. }
  1664. }
  1665. assertCoverageClear();
  1666. ctIndex = 0;
  1667. ctFormat = 1;
  1668. assertSubtableClear();
  1669. assert sf >= 0;
  1670. stFormat = sf;
  1671. } else {
  1672. notPermittedInElementContext(en, getParent(), pn);
  1673. }
  1674. } else if (en[1].equals("SubstLookupRecord")) {
  1675. String[] pn = new String[] { null, "ChainContextSubst" };
  1676. if (isParent(pn)) {
  1677. String index = attrs.getValue("index");
  1678. if (index == null) {
  1679. missingRequiredAttribute(en, "index");
  1680. }
  1681. } else {
  1682. notPermittedInElementContext(en, getParent(), pn);
  1683. }
  1684. } else if (en[1].equals("Substitute")) {
  1685. String[] pn = new String[] { null, "Sequence" };
  1686. if (isParent(pn)) {
  1687. String index = attrs.getValue("index");
  1688. if (index == null) {
  1689. missingRequiredAttribute(en, "index");
  1690. } else {
  1691. int i = Integer.parseInt(index);
  1692. if (i != substitutes.size()) {
  1693. invalidIndex(en, i, substitutes.size());
  1694. }
  1695. }
  1696. String value = attrs.getValue("value");
  1697. if (value == null) {
  1698. missingRequiredAttribute(en, "value");
  1699. } else {
  1700. int gid = mapGlyphId(value, en);
  1701. substitutes.add(Integer.valueOf(gid));
  1702. }
  1703. } else {
  1704. notPermittedInElementContext(en, getParent(), pn);
  1705. }
  1706. } else if (en[1].equals("Substitution")) {
  1707. String[] pn = new String[] { null, "SingleSubst" };
  1708. if (isParent(pn)) {
  1709. String in = attrs.getValue("in");
  1710. int igid = -1;
  1711. int ogid = -1;
  1712. if (in == null) {
  1713. missingRequiredAttribute(en, "in");
  1714. } else {
  1715. igid = mapGlyphId(in, en);
  1716. }
  1717. String out = attrs.getValue("out");
  1718. if (out == null) {
  1719. missingRequiredAttribute(en, "out");
  1720. } else {
  1721. ogid = mapGlyphId(out, en);
  1722. }
  1723. coverageEntries.add(Integer.valueOf(igid));
  1724. subtableEntries.add(Integer.valueOf(ogid));
  1725. } else {
  1726. notPermittedInElementContext(en, getParent(), pn);
  1727. }
  1728. } else if (en[1].equals("Value")) {
  1729. String[] pn = new String[] { null, "SinglePos" };
  1730. if (isParent(pn)) {
  1731. String index = attrs.getValue("index");
  1732. if (vf1 < 0) {
  1733. missingParameter(en, "value format");
  1734. } else {
  1735. subtableEntries.add(parseValue(en, attrs, vf1));
  1736. }
  1737. } else {
  1738. notPermittedInElementContext(en, getParent(), pn);
  1739. }
  1740. } else if (en[1].equals("Value1")) {
  1741. String[] pn = new String[] { null, "PairValueRecord" };
  1742. if (isParent(pn)) {
  1743. if (vf1 < 0) {
  1744. missingParameter(en, "value format 1");
  1745. } else {
  1746. assert v1 == null;
  1747. v1 = parseValue(en, attrs, vf1);
  1748. }
  1749. } else {
  1750. notPermittedInElementContext(en, getParent(), pn);
  1751. }
  1752. } else if (en[1].equals("Value2")) {
  1753. String[] pn = new String[] { null, "PairValueRecord" };
  1754. if (isParent(pn)) {
  1755. if (vf2 < 0) {
  1756. missingParameter(en, "value format 2");
  1757. } else {
  1758. assert v2 == null;
  1759. v2 = parseValue(en, attrs, vf2);
  1760. }
  1761. } else {
  1762. notPermittedInElementContext(en, getParent(), pn);
  1763. }
  1764. } else if (en[1].equals("ValueFormat")) {
  1765. String[] pn = new String[] { null, "SinglePos" };
  1766. if (isParent(pn)) {
  1767. String value = attrs.getValue("value");
  1768. int vf = -1;
  1769. if (value == null) {
  1770. missingRequiredAttribute(en, "value");
  1771. } else {
  1772. vf = Integer.parseInt(value);
  1773. }
  1774. assert vf1 == -1;
  1775. vf1 = vf;
  1776. } else {
  1777. notPermittedInElementContext(en, getParent(), pn);
  1778. }
  1779. } else if (en[1].equals("ValueFormat1")) {
  1780. String[] pn = new String[] { null, "PairPos" };
  1781. if (isParent(pn)) {
  1782. String value = attrs.getValue("value");
  1783. int vf = -1;
  1784. if (value == null) {
  1785. missingRequiredAttribute(en, "value");
  1786. } else {
  1787. vf = Integer.parseInt(value);
  1788. }
  1789. assert vf1 == -1;
  1790. vf1 = vf;
  1791. } else {
  1792. notPermittedInElementContext(en, getParent(), pn);
  1793. }
  1794. } else if (en[1].equals("ValueFormat2")) {
  1795. String[] pn = new String[] { null, "PairPos" };
  1796. if (isParent(pn)) {
  1797. String value = attrs.getValue("value");
  1798. int vf = -1;
  1799. if (value == null) {
  1800. missingRequiredAttribute(en, "value");
  1801. } else {
  1802. vf = Integer.parseInt(value);
  1803. }
  1804. assert vf2 == -1;
  1805. vf2 = vf;
  1806. } else {
  1807. notPermittedInElementContext(en, getParent(), pn);
  1808. }
  1809. } else if (en[1].equals("Version")) {
  1810. String[] pn1 = new String[] { null, "GDEF" };
  1811. String[] pn2 = new String[] { null, "GPOS" };
  1812. String[] pn3 = new String[] { null, "GSUB" };
  1813. String[][] pnx = new String[][] { pn1, pn2, pn3 };
  1814. if (isParent(pnx)) {
  1815. String value = attrs.getValue("value");
  1816. if (value == null) {
  1817. missingRequiredAttribute(en, "value");
  1818. }
  1819. } else {
  1820. notPermittedInElementContext(en, getParent(), pnx);
  1821. }
  1822. } else if (en[1].equals("XCoordinate")) {
  1823. String[] pn1 = new String[] { null, "BaseAnchor" };
  1824. String[] pn2 = new String[] { null, "EntryAnchor" };
  1825. String[] pn3 = new String[] { null, "ExitAnchor" };
  1826. String[] pn4 = new String[] { null, "LigatureAnchor" };
  1827. String[] pn5 = new String[] { null, "MarkAnchor" };
  1828. String[] pn6 = new String[] { null, "Mark2Anchor" };
  1829. String[][] pnx = new String[][] { pn1, pn2, pn3, pn4, pn5, pn6 };
  1830. if (isParent(pnx)) {
  1831. String value = attrs.getValue("value");
  1832. int x = 0;
  1833. if (value == null) {
  1834. missingRequiredAttribute(en, "value");
  1835. } else {
  1836. x = Integer.parseInt(value);
  1837. }
  1838. assert xCoord == Integer.MIN_VALUE;
  1839. xCoord = x;
  1840. } else {
  1841. notPermittedInElementContext(en, getParent(), pnx);
  1842. }
  1843. } else if (en[1].equals("YCoordinate")) {
  1844. String[] pn1 = new String[] { null, "BaseAnchor" };
  1845. String[] pn2 = new String[] { null, "EntryAnchor" };
  1846. String[] pn3 = new String[] { null, "ExitAnchor" };
  1847. String[] pn4 = new String[] { null, "LigatureAnchor" };
  1848. String[] pn5 = new String[] { null, "MarkAnchor" };
  1849. String[] pn6 = new String[] { null, "Mark2Anchor" };
  1850. String[][] pnx = new String[][] { pn1, pn2, pn3, pn4, pn5, pn6 };
  1851. if (isParent(pnx)) {
  1852. String value = attrs.getValue("value");
  1853. int y = 0;
  1854. if (value == null) {
  1855. missingRequiredAttribute(en, "value");
  1856. } else {
  1857. y = Integer.parseInt(value);
  1858. }
  1859. assert yCoord == Integer.MIN_VALUE;
  1860. yCoord = y;
  1861. } else {
  1862. notPermittedInElementContext(en, getParent(), pnx);
  1863. }
  1864. } else if (en[1].equals("checkSumAdjustment")) {
  1865. String[] pn = new String[] { null, "head" };
  1866. if (isParent(pn)) {
  1867. String value = attrs.getValue("value");
  1868. if (value == null) {
  1869. missingRequiredAttribute(en, "value");
  1870. }
  1871. } else {
  1872. notPermittedInElementContext(en, getParent(), pn);
  1873. }
  1874. } else if (en[1].equals("cmap")) {
  1875. String[] pn = new String[] { null, "ttFont" };
  1876. if (! isParent(pn)) {
  1877. notPermittedInElementContext(en, getParent(), pn);
  1878. }
  1879. } else if (en[1].equals("cmap_format_0")) {
  1880. String[] pn = new String[] { null, "cmap" };
  1881. if (isParent(pn)) {
  1882. String platformID = attrs.getValue("platformID");
  1883. if (platformID == null) {
  1884. missingRequiredAttribute(en, "platformID");
  1885. }
  1886. String platEncID = attrs.getValue("platEncID");
  1887. if (platEncID == null) {
  1888. missingRequiredAttribute(en, "platEncID");
  1889. }
  1890. String language = attrs.getValue("language");
  1891. if (language == null) {
  1892. missingRequiredAttribute(en, "language");
  1893. }
  1894. } else {
  1895. notPermittedInElementContext(en, getParent(), pn);
  1896. }
  1897. } else if (en[1].equals("cmap_format_4")) {
  1898. String[] pn = new String[] { null, "cmap" };
  1899. if (isParent(pn)) {
  1900. String platformID = attrs.getValue("platformID");
  1901. int pid = -1;
  1902. if (platformID == null) {
  1903. missingRequiredAttribute(en, "platformID");
  1904. } else {
  1905. pid = Integer.parseInt(platformID);
  1906. }
  1907. String platEncID = attrs.getValue("platEncID");
  1908. int eid = -1;
  1909. if (platEncID == null) {
  1910. missingRequiredAttribute(en, "platEncID");
  1911. } else {
  1912. eid = Integer.parseInt(platEncID);
  1913. }
  1914. String language = attrs.getValue("language");
  1915. int lid = -1;
  1916. if (language == null) {
  1917. missingRequiredAttribute(en, "language");
  1918. } else {
  1919. lid = Integer.parseInt(language);
  1920. }
  1921. assert cmapEntries.size() == 0;
  1922. assert cmPlatform == -1;
  1923. assert cmEncoding == -1;
  1924. assert cmLanguage == -1;
  1925. cmPlatform = pid;
  1926. cmEncoding = eid;
  1927. cmLanguage = lid;
  1928. } else {
  1929. notPermittedInElementContext(en, getParent(), pn);
  1930. }
  1931. } else if (en[1].equals("created")) {
  1932. String[] pn = new String[] { null, "head" };
  1933. if (isParent(pn)) {
  1934. String value = attrs.getValue("value");
  1935. if (value == null) {
  1936. missingRequiredAttribute(en, "value");
  1937. }
  1938. } else {
  1939. notPermittedInElementContext(en, getParent(), pn);
  1940. }
  1941. } else if (en[1].equals("flags")) {
  1942. String[] pn = new String[] { null, "head" };
  1943. if (isParent(pn)) {
  1944. String value = attrs.getValue("value");
  1945. if (value == null) {
  1946. missingRequiredAttribute(en, "value");
  1947. }
  1948. } else {
  1949. notPermittedInElementContext(en, getParent(), pn);
  1950. }
  1951. } else if (en[1].equals("fontDirectionHint")) {
  1952. String[] pn = new String[] { null, "head" };
  1953. if (isParent(pn)) {
  1954. String value = attrs.getValue("value");
  1955. if (value == null) {
  1956. missingRequiredAttribute(en, "value");
  1957. }
  1958. } else {
  1959. notPermittedInElementContext(en, getParent(), pn);
  1960. }
  1961. } else if (en[1].equals("fontRevision")) {
  1962. String[] pn = new String[] { null, "head" };
  1963. if (isParent(pn)) {
  1964. String value = attrs.getValue("value");
  1965. if (value == null) {
  1966. missingRequiredAttribute(en, "value");
  1967. }
  1968. } else {
  1969. notPermittedInElementContext(en, getParent(), pn);
  1970. }
  1971. } else if (en[1].equals("glyphDataFormat")) {
  1972. String[] pn = new String[] { null, "head" };
  1973. if (isParent(pn)) {
  1974. String value = attrs.getValue("value");
  1975. if (value == null) {
  1976. missingRequiredAttribute(en, "value");
  1977. }
  1978. } else {
  1979. notPermittedInElementContext(en, getParent(), pn);
  1980. }
  1981. } else if (en[1].equals("head")) {
  1982. String[] pn = new String[] { null, "ttFont" };
  1983. if (! isParent(pn)) {
  1984. notPermittedInElementContext(en, getParent(), pn);
  1985. }
  1986. } else if (en[1].equals("hmtx")) {
  1987. String[] pn = new String[] { null, "ttFont" };
  1988. if (! isParent(pn)) {
  1989. notPermittedInElementContext(en, getParent(), pn);
  1990. } else if (glyphIdMax > 0) {
  1991. hmtxEntries.setSize(glyphIdMax + 1);
  1992. }
  1993. } else if (en[1].equals("indexToLocFormat")) {
  1994. String[] pn = new String[] { null, "head" };
  1995. if (isParent(pn)) {
  1996. String value = attrs.getValue("value");
  1997. if (value == null) {
  1998. missingRequiredAttribute(en, "value");
  1999. }
  2000. } else {
  2001. notPermittedInElementContext(en, getParent(), pn);
  2002. }
  2003. } else if (en[1].equals("lowestRecPPEM")) {
  2004. String[] pn = new String[] { null, "head" };
  2005. if (isParent(pn)) {
  2006. String value = attrs.getValue("value");
  2007. if (value == null) {
  2008. missingRequiredAttribute(en, "value");
  2009. }
  2010. } else {
  2011. notPermittedInElementContext(en, getParent(), pn);
  2012. }
  2013. } else if (en[1].equals("macStyle")) {
  2014. String[] pn = new String[] { null, "head" };
  2015. if (isParent(pn)) {
  2016. String value = attrs.getValue("value");
  2017. if (value == null) {
  2018. missingRequiredAttribute(en, "value");
  2019. }
  2020. } else {
  2021. notPermittedInElementContext(en, getParent(), pn);
  2022. }
  2023. } else if (en[1].equals("magicNumber")) {
  2024. String[] pn = new String[] { null, "head" };
  2025. if (isParent(pn)) {
  2026. String value = attrs.getValue("value");
  2027. if (value == null) {
  2028. missingRequiredAttribute(en, "value");
  2029. }
  2030. } else {
  2031. notPermittedInElementContext(en, getParent(), pn);
  2032. }
  2033. } else if (en[1].equals("map")) {
  2034. String[] pn1 = new String[] { null, "cmap_format_0" };
  2035. String[] pn2 = new String[] { null, "cmap_format_4" };
  2036. String[][] pnx = new String[][] { pn1, pn2 };
  2037. if (isParent(pnx)) {
  2038. String code = attrs.getValue("code");
  2039. int cid = -1;
  2040. if (code == null) {
  2041. missingRequiredAttribute(en, "code");
  2042. } else {
  2043. code = code.toLowerCase();
  2044. if (code.startsWith("0x")) {
  2045. cid = Integer.parseInt(code.substring(2), 16);
  2046. } else {
  2047. cid = Integer.parseInt(code, 10);
  2048. }
  2049. }
  2050. String name = attrs.getValue("name");
  2051. int gid = -1;
  2052. if (name == null) {
  2053. missingRequiredAttribute(en, "name");
  2054. } else {
  2055. gid = mapGlyphId(name, en);
  2056. }
  2057. if ((cmPlatform == 3) && (cmEncoding == 1)) {
  2058. cmapEntries.add(new int[] { cid, gid });
  2059. }
  2060. } else {
  2061. notPermittedInElementContext(en, getParent(), pnx);
  2062. }
  2063. } else if (en[1].equals("modified")) {
  2064. String[] pn = new String[] { null, "head" };
  2065. if (isParent(pn)) {
  2066. String value = attrs.getValue("value");
  2067. if (value == null) {
  2068. missingRequiredAttribute(en, "value");
  2069. }
  2070. } else {
  2071. notPermittedInElementContext(en, getParent(), pn);
  2072. }
  2073. } else if (en[1].equals("mtx")) {
  2074. String[] pn = new String[] { null, "hmtx" };
  2075. if (isParent(pn)) {
  2076. String name = attrs.getValue("name");
  2077. int gid = -1;
  2078. if (name == null) {
  2079. missingRequiredAttribute(en, "name");
  2080. } else {
  2081. gid = mapGlyphId(name, en);
  2082. }
  2083. String width = attrs.getValue("width");
  2084. int w = -1;
  2085. if (width == null) {
  2086. missingRequiredAttribute(en, "width");
  2087. } else {
  2088. w = Integer.parseInt(width);
  2089. }
  2090. String lsb = attrs.getValue("lsb");
  2091. int l = -1;
  2092. if (lsb == null) {
  2093. missingRequiredAttribute(en, "lsb");
  2094. } else {
  2095. l = Integer.parseInt(lsb);
  2096. }
  2097. hmtxEntries.set(gid, new int[] { w, l });
  2098. } else {
  2099. notPermittedInElementContext(en, getParent(), pn);
  2100. }
  2101. } else if (en[1].equals("tableVersion")) {
  2102. String[] pn1 = new String[] { null, "cmap" };
  2103. String[] pn2 = new String[] { null, "head" };
  2104. String[][] pnx = new String[][] { pn1, pn2 };
  2105. if (isParent(pn1)) { // child of cmap
  2106. String version = attrs.getValue("version");
  2107. if (version == null) {
  2108. missingRequiredAttribute(en, "version");
  2109. }
  2110. } else if (isParent(pn2)) { // child of head
  2111. String value = attrs.getValue("value");
  2112. if (value == null) {
  2113. missingRequiredAttribute(en, "value");
  2114. }
  2115. } else {
  2116. notPermittedInElementContext(en, getParent(), pnx);
  2117. }
  2118. } else if (en[1].equals("ttFont")) {
  2119. String[] pn = new String[] { null, null };
  2120. if (isParent(pn)) {
  2121. String sfntVersion = attrs.getValue("sfntVersion");
  2122. if (sfntVersion == null) {
  2123. missingRequiredAttribute(en, "sfntVersion");
  2124. }
  2125. String ttLibVersion = attrs.getValue("ttLibVersion");
  2126. if (ttLibVersion == null) {
  2127. missingRequiredAttribute(en, "ttLibVersion");
  2128. }
  2129. } else {
  2130. notPermittedInElementContext(en, getParent(), null);
  2131. }
  2132. } else if (en[1].equals("unitsPerEm")) {
  2133. String[] pn = new String[] { null, "head" };
  2134. if (isParent(pn)) {
  2135. String value = attrs.getValue("value");
  2136. int v = -1;
  2137. if (value == null) {
  2138. missingRequiredAttribute(en, "value");
  2139. } else {
  2140. v = Integer.parseInt(value);
  2141. }
  2142. assert upem == -1;
  2143. upem = v;
  2144. } else {
  2145. notPermittedInElementContext(en, getParent(), pn);
  2146. }
  2147. } else if (en[1].equals("xMax")) {
  2148. String[] pn = new String[] { null, "head" };
  2149. if (isParent(pn)) {
  2150. String value = attrs.getValue("value");
  2151. if (value == null) {
  2152. missingRequiredAttribute(en, "value");
  2153. }
  2154. } else {
  2155. notPermittedInElementContext(en, getParent(), pn);
  2156. }
  2157. } else if (en[1].equals("xMin")) {
  2158. String[] pn = new String[] { null, "head" };
  2159. if (isParent(pn)) {
  2160. String value = attrs.getValue("value");
  2161. if (value == null) {
  2162. missingRequiredAttribute(en, "value");
  2163. }
  2164. } else {
  2165. notPermittedInElementContext(en, getParent(), pn);
  2166. }
  2167. } else if (en[1].equals("yMax")) {
  2168. String[] pn = new String[] { null, "head" };
  2169. if (isParent(pn)) {
  2170. String value = attrs.getValue("value");
  2171. if (value == null) {
  2172. missingRequiredAttribute(en, "value");
  2173. }
  2174. } else {
  2175. notPermittedInElementContext(en, getParent(), pn);
  2176. }
  2177. } else if (en[1].equals("yMin")) {
  2178. String[] pn = new String[] { null, "head" };
  2179. if (isParent(pn)) {
  2180. String value = attrs.getValue("value");
  2181. if (value == null) {
  2182. missingRequiredAttribute(en, "value");
  2183. }
  2184. } else {
  2185. notPermittedInElementContext(en, getParent(), pn);
  2186. }
  2187. } else {
  2188. unsupportedElement(en);
  2189. }
  2190. elements.push(en);
  2191. }
  2192. @Override
  2193. public void endElement(String uri, String localName, String qName) throws SAXException {
  2194. if (elements.empty()) {
  2195. throw new SAXException("element stack is unbalanced, no elements on stack!");
  2196. }
  2197. String[] enParent = elements.peek();
  2198. if (enParent == null) {
  2199. throw new SAXException("element stack is empty, elements are not balanced");
  2200. }
  2201. String[] en = makeExpandedName(uri, localName, qName);
  2202. if (! sameExpandedName(enParent, en)) {
  2203. throw new SAXException("element stack is unbalanced, expanded name mismatch");
  2204. }
  2205. if (en[0] != null) {
  2206. unsupportedElement(en);
  2207. } else if (isAnchorElement(en[1])) {
  2208. if (xCoord == Integer.MIN_VALUE) {
  2209. missingParameter(en, "x coordinate");
  2210. } else if (yCoord == Integer.MIN_VALUE) {
  2211. missingParameter(en, "y coordinate");
  2212. } else {
  2213. if (en[1].equals("EntryAnchor")) {
  2214. if (anchors.size() > 0) {
  2215. duplicateParameter(en, "entry anchor");
  2216. }
  2217. } else if (en[1].equals("ExitAnchor")) {
  2218. if (anchors.size() > 1) {
  2219. duplicateParameter(en, "exit anchor");
  2220. } else if (anchors.size() == 0) {
  2221. anchors.add(null);
  2222. }
  2223. }
  2224. anchors.add(new GlyphPositioningTable.Anchor(xCoord, yCoord));
  2225. xCoord = yCoord = Integer.MIN_VALUE;
  2226. }
  2227. } else if (en[1].equals("AlternateSet")) {
  2228. subtableEntries.add(extractAlternates());
  2229. } else if (en[1].equals("AlternateSubst")) {
  2230. if (! sortEntries(coverageEntries, subtableEntries)) {
  2231. mismatchedEntries(en, coverageEntries.size(), subtableEntries.size());
  2232. }
  2233. addGSUBSubtable(GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_ALTERNATE, extractCoverage());
  2234. } else if (en[1].equals("BacktrackCoverage")) {
  2235. String ck = makeCoverageKey("bk", ctIndex);
  2236. if (coverages.containsKey(ck)) {
  2237. duplicateCoverageIndex(en, ctIndex);
  2238. } else {
  2239. coverages.put(ck, extractCoverage());
  2240. }
  2241. } else if (en[1].equals("BaseCoverage")) {
  2242. coverages.put("base", extractCoverage());
  2243. } else if (en[1].equals("BaseRecord")) {
  2244. baseOrMarkAnchors.add(extractAnchors());
  2245. } else if (en[1].equals("ChainContextPos") || en[1].equals("ChainContextSubst")) {
  2246. GlyphCoverageTable coverage = null;
  2247. if (stFormat == 3) {
  2248. GlyphCoverageTable igca[] = getCoveragesWithPrefix("in");
  2249. GlyphCoverageTable bgca[] = getCoveragesWithPrefix("bk");
  2250. GlyphCoverageTable lgca[] = getCoveragesWithPrefix("la");
  2251. if ((igca.length == 0) || hasMissingCoverage(igca)) {
  2252. missingCoverage(en, "input", igca.length);
  2253. } else if (hasMissingCoverage(bgca)) {
  2254. missingCoverage(en, "backtrack", bgca.length);
  2255. } else if (hasMissingCoverage(lgca)) {
  2256. missingCoverage(en, "lookahead", lgca.length);
  2257. } else {
  2258. GlyphTable.Rule r = new GlyphTable.ChainedCoverageSequenceRule(extractRuleLookups(), igca.length, igca, bgca, lgca);
  2259. GlyphTable.RuleSet rs = new GlyphTable.HomogeneousRuleSet(new GlyphTable.Rule[] {r});
  2260. GlyphTable.RuleSet[] rsa = new GlyphTable.RuleSet[] {rs};
  2261. coverage = igca [ 0 ];
  2262. subtableEntries.add(rsa);
  2263. }
  2264. } else {
  2265. unsupportedFormat(en, stFormat);
  2266. }
  2267. if (en[1].equals("ChainContextPos")) {
  2268. addGPOSSubtable(GlyphPositioningTable.GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL, coverage);
  2269. } else if (en[1].equals("ChainContextSubst")) {
  2270. addGSUBSubtable(GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_CHAINED_CONTEXTUAL, coverage);
  2271. }
  2272. } else if (en[1].equals("ComponentRecord")) {
  2273. components.add(extractAnchors());
  2274. } else if (en[1].equals("Coverage")) {
  2275. coverages.put("main", extractCoverage());
  2276. } else if (en[1].equals("DefaultLangSys") || en[1].equals("LangSysRecord")) {
  2277. if (languageTag == null) {
  2278. missingTag(en, "language");
  2279. } else if (languages.containsKey(languageTag)) {
  2280. duplicateTag(en, "language", languageTag);
  2281. } else {
  2282. languages.put(languageTag, extractLanguageFeatures());
  2283. languageTag = null;
  2284. }
  2285. } else if (en[1].equals("CursivePos")) {
  2286. GlyphCoverageTable ct = coverages.get("main");
  2287. if (ct == null) {
  2288. missingParameter(en, "coverages");
  2289. } else if (stFormat == 1) {
  2290. subtableEntries.add(extractAttachmentAnchors());
  2291. } else {
  2292. unsupportedFormat(en, stFormat);
  2293. }
  2294. addGPOSSubtable(GlyphPositioningTable.GPOS_LOOKUP_TYPE_CURSIVE, ct);
  2295. } else if (en[1].equals("EntryExitRecord")) {
  2296. int na = anchors.size();
  2297. if (na == 0) {
  2298. missingParameter(en, "entry or exit anchor");
  2299. } else if (na == 1) {
  2300. anchors.add(null);
  2301. } else if (na > 2) {
  2302. duplicateParameter(en, "entry or exit anchor");
  2303. }
  2304. attachmentAnchors.add(extractAnchors());
  2305. } else if (en[1].equals("BaseRecord")) {
  2306. baseOrMarkAnchors.add(extractAnchors());
  2307. } else if (en[1].equals("FeatureRecord")) {
  2308. if (flIndex != flSequence) {
  2309. mismatchedIndex(en, "feature", flIndex, flSequence);
  2310. } else if (featureTag == null) {
  2311. missingTag(en, "feature");
  2312. } else {
  2313. String fid = makeFeatureId(flIndex);
  2314. features.put(fid, extractFeature());
  2315. nextFeature();
  2316. }
  2317. } else if (en[1].equals("GDEF")) {
  2318. if (subtables.size() > 0) {
  2319. gdef = new GlyphDefinitionTable(subtables);
  2320. }
  2321. clearTable();
  2322. } else if (en[1].equals("GPOS")) {
  2323. if (subtables.size() > 0) {
  2324. gpos = new GlyphPositioningTable(gdef, extractLookups(), subtables);
  2325. }
  2326. clearTable();
  2327. } else if (en[1].equals("GSUB")) {
  2328. if (subtables.size() > 0) {
  2329. gsub = new GlyphSubstitutionTable(gdef, extractLookups(), subtables);
  2330. }
  2331. clearTable();
  2332. } else if (en[1].equals("GlyphClassDef")) {
  2333. GlyphMappingTable mapping = extractClassDefMapping(glyphClasses, stFormat, true);
  2334. addGDEFSubtable(GlyphDefinitionTable.GDEF_LOOKUP_TYPE_GLYPH_CLASS, mapping);
  2335. } else if (en[1].equals("InputCoverage")) {
  2336. String ck = makeCoverageKey("in", ctIndex);
  2337. if (coverages.containsKey(ck)) {
  2338. duplicateCoverageIndex(en, ctIndex);
  2339. } else {
  2340. coverages.put(ck, extractCoverage());
  2341. }
  2342. } else if (en[1].equals("LigatureAttach")) {
  2343. ligatureAnchors.add(extractComponents());
  2344. } else if (en[1].equals("LigatureCoverage")) {
  2345. coverages.put("liga", extractCoverage());
  2346. } else if (en[1].equals("LigatureSet")) {
  2347. subtableEntries.add(extractLigatures());
  2348. } else if (en[1].equals("LigatureSubst")) {
  2349. if (! sortEntries(coverageEntries, subtableEntries)) {
  2350. mismatchedEntries(en, coverageEntries.size(), subtableEntries.size());
  2351. }
  2352. GlyphCoverageTable coverage = extractCoverage();
  2353. addGSUBSubtable(GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_LIGATURE, coverage);
  2354. } else if (en[1].equals("LookAheadCoverage")) {
  2355. String ck = makeCoverageKey("la", ctIndex);
  2356. if (coverages.containsKey(ck)) {
  2357. duplicateCoverageIndex(en, ctIndex);
  2358. } else {
  2359. coverages.put(ck, extractCoverage());
  2360. }
  2361. } else if (en[1].equals("Lookup")) {
  2362. if (ltIndex != ltSequence) {
  2363. mismatchedIndex(en, "lookup", ltIndex, ltSequence);
  2364. } else {
  2365. nextLookup();
  2366. }
  2367. } else if (en[1].equals("MarkAttachClassDef")) {
  2368. GlyphMappingTable mapping = extractClassDefMapping(glyphClasses, stFormat, true);
  2369. addGDEFSubtable(GlyphDefinitionTable.GDEF_LOOKUP_TYPE_MARK_ATTACHMENT, mapping);
  2370. } else if (en[1].equals("MarkCoverage")) {
  2371. coverages.put("mark", extractCoverage());
  2372. } else if (en[1].equals("Mark1Coverage")) {
  2373. coverages.put("mrk1", extractCoverage());
  2374. } else if (en[1].equals("Mark2Coverage")) {
  2375. coverages.put("mrk2", extractCoverage());
  2376. } else if (en[1].equals("MarkBasePos")) {
  2377. GlyphCoverageTable mct = coverages.get("mark");
  2378. GlyphCoverageTable bct = coverages.get("base");
  2379. if (mct == null) {
  2380. missingParameter(en, "mark coverages");
  2381. } else if (bct == null) {
  2382. missingParameter(en, "base coverages");
  2383. } else if (stFormat == 1) {
  2384. MarkAnchor[] maa = extractMarkAnchors();
  2385. Anchor[][] bam = extractBaseOrMarkAnchors();
  2386. subtableEntries.add(bct);
  2387. subtableEntries.add(computeClassCount(bam));
  2388. subtableEntries.add(maa);
  2389. subtableEntries.add(bam);
  2390. } else {
  2391. unsupportedFormat(en, stFormat);
  2392. }
  2393. addGPOSSubtable(GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_BASE, mct);
  2394. } else if (en[1].equals("MarkLigPos")) {
  2395. GlyphCoverageTable mct = coverages.get("mark");
  2396. GlyphCoverageTable lct = coverages.get("liga");
  2397. if (mct == null) {
  2398. missingParameter(en, "mark coverages");
  2399. } else if (lct == null) {
  2400. missingParameter(en, "ligature coverages");
  2401. } else if (stFormat == 1) {
  2402. MarkAnchor[] maa = extractMarkAnchors();
  2403. Anchor[][][] lam = extractLigatureAnchors();
  2404. subtableEntries.add(lct);
  2405. subtableEntries.add(computeLigaturesClassCount(lam));
  2406. subtableEntries.add(computeLigaturesComponentCount(lam));
  2407. subtableEntries.add(maa);
  2408. subtableEntries.add(lam);
  2409. } else {
  2410. unsupportedFormat(en, stFormat);
  2411. }
  2412. addGPOSSubtable(GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE, mct);
  2413. } else if (en[1].equals("MarkMarkPos")) {
  2414. GlyphCoverageTable mct1 = coverages.get("mrk1");
  2415. GlyphCoverageTable mct2 = coverages.get("mrk2");
  2416. if (mct1 == null) {
  2417. missingParameter(en, "mark coverages 1");
  2418. } else if (mct2 == null) {
  2419. missingParameter(en, "mark coverages 2");
  2420. } else if (stFormat == 1) {
  2421. MarkAnchor[] maa = extractMarkAnchors();
  2422. Anchor[][] mam = extractBaseOrMarkAnchors();
  2423. subtableEntries.add(mct2);
  2424. subtableEntries.add(computeClassCount(mam));
  2425. subtableEntries.add(maa);
  2426. subtableEntries.add(mam);
  2427. } else {
  2428. unsupportedFormat(en, stFormat);
  2429. }
  2430. addGPOSSubtable(GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_MARK, mct1);
  2431. } else if (en[1].equals("MarkRecord")) {
  2432. if (markClass == -1) {
  2433. missingParameter(en, "mark class");
  2434. } else if (anchors.size() == 0) {
  2435. missingParameter(en, "mark anchor");
  2436. } else if (anchors.size() > 1) {
  2437. duplicateParameter(en, "mark anchor");
  2438. } else {
  2439. markAnchors.add(new GlyphPositioningTable.MarkAnchor(markClass, anchors.get(0)));
  2440. markClass = -1;
  2441. anchors.clear();
  2442. }
  2443. } else if (en[1].equals("Mark2Record")) {
  2444. baseOrMarkAnchors.add(extractAnchors());
  2445. } else if (en[1].equals("MultipleSubst")) {
  2446. GlyphCoverageTable coverage = coverages.get("main");
  2447. addGSUBSubtable(GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_MULTIPLE, coverage, extractSequenceEntries());
  2448. } else if (en[1].equals("PairPos")) {
  2449. assertSubtableEntriesClear();
  2450. if (stFormat == 1) {
  2451. if (pairSets.size() == 0) {
  2452. missingParameter(en, "pair set");
  2453. } else {
  2454. subtableEntries.add(extractPairSets());
  2455. }
  2456. } else if (stFormat == 2) {
  2457. unsupportedFormat(en, stFormat);
  2458. }
  2459. GlyphCoverageTable coverage = coverages.get("main");
  2460. addGPOSSubtable(GlyphPositioningTable.GPOS_LOOKUP_TYPE_PAIR, coverage);
  2461. vf1 = vf2 = -1; psIndex = -1;
  2462. } else if (en[1].equals("PairSet")) {
  2463. if (psIndex != pairSets.size()) {
  2464. invalidIndex(en, psIndex, pairSets.size());
  2465. } else {
  2466. pairSets.add(extractPairs());
  2467. }
  2468. } else if (en[1].equals("PairValueRecord")) {
  2469. if (g2 == -1) {
  2470. missingParameter(en, "second glyph");
  2471. } else if ((v1 == null) && (v2 == null)) {
  2472. missingParameter(en, "first or second value");
  2473. } else {
  2474. pairs.add(new PairValues(g2, v1, v2));
  2475. clearPair();
  2476. }
  2477. } else if (en[1].equals("PosLookupRecord") || en[1].equals("SubstLookupRecord")) {
  2478. if (rlSequence < 0) {
  2479. missingParameter(en, "sequence index");
  2480. } else if (rlLookup < 0) {
  2481. missingParameter(en, "lookup index");
  2482. } else {
  2483. ruleLookups.add(new GlyphTable.RuleLookup(rlSequence, rlLookup));
  2484. rlSequence = rlLookup = -1;
  2485. }
  2486. } else if (en[1].equals("Script")) {
  2487. if (scriptTag == null) {
  2488. missingTag(en, "script");
  2489. } else if (scripts.containsKey(scriptTag)) {
  2490. duplicateTag(en, "script", scriptTag);
  2491. } else {
  2492. scripts.put(scriptTag, extractLanguages());
  2493. scriptTag = null;
  2494. }
  2495. } else if (en[1].equals("Sequence")) {
  2496. subtableEntries.add(extractSubstitutes());
  2497. } else if (en[1].equals("SinglePos")) {
  2498. int nv = subtableEntries.size();
  2499. if (stFormat == 1) {
  2500. if (nv < 0) {
  2501. missingParameter(en, "value");
  2502. } else if (nv > 1) {
  2503. duplicateParameter(en, "value");
  2504. }
  2505. } else if (stFormat == 2) {
  2506. GlyphPositioningTable.Value[] pva = (GlyphPositioningTable.Value[]) subtableEntries.toArray(new GlyphPositioningTable.Value [ nv ]);
  2507. subtableEntries.clear();
  2508. subtableEntries.add(pva);
  2509. }
  2510. GlyphCoverageTable coverage = coverages.get("main");
  2511. addGPOSSubtable(GlyphPositioningTable.GPOS_LOOKUP_TYPE_SINGLE, coverage);
  2512. vf1 = -1;
  2513. } else if (en[1].equals("SingleSubst")) {
  2514. if (! sortEntries(coverageEntries, subtableEntries)) {
  2515. mismatchedEntries(en, coverageEntries.size(), subtableEntries.size());
  2516. }
  2517. GlyphCoverageTable coverage = extractCoverage();
  2518. addGSUBSubtable(GlyphSubstitutionTable.GSUB_LOOKUP_TYPE_SINGLE, coverage);
  2519. } else if (en[1].equals("cmap")) {
  2520. cmap = getCMAP();
  2521. gmap = getGMAP();
  2522. cmapEntries.clear();
  2523. } else if (en[1].equals("cmap_format_4")) {
  2524. cmPlatform = cmEncoding = cmLanguage = -1;
  2525. } else if (en[1].equals("hmtx")) {
  2526. hmtx = getHMTX();
  2527. hmtxEntries.clear();
  2528. } else if (en[1].equals("ttFont")) {
  2529. if (cmap == null) {
  2530. missingParameter(en, "cmap");
  2531. }
  2532. if (hmtx == null) {
  2533. missingParameter(en, "hmtx");
  2534. }
  2535. }
  2536. elements.pop();
  2537. }
  2538. @Override
  2539. public void characters(char[] chars, int start, int length) {
  2540. }
  2541. private String[] getParent() {
  2542. if (! elements.empty()) {
  2543. return elements.peek();
  2544. } else {
  2545. return new String[] { null, null };
  2546. }
  2547. }
  2548. private boolean isParent(Object enx) {
  2549. if (enx instanceof String[][]) {
  2550. for (String[] en : (String[][]) enx) {
  2551. if (isParent(en)) {
  2552. return true;
  2553. }
  2554. }
  2555. return false;
  2556. } else if (enx instanceof String[]) {
  2557. String[] en = (String[]) enx;
  2558. if (! elements.empty()) {
  2559. String[] pn = elements.peek();
  2560. return (pn != null) && sameExpandedName(en, pn);
  2561. } else if ((en[0] == null) && (en[1] == null)) {
  2562. return true;
  2563. } else {
  2564. return false;
  2565. }
  2566. } else {
  2567. return false;
  2568. }
  2569. }
  2570. private boolean isAnchorElement(String ln) {
  2571. if (ln.equals("BaseAnchor")) {
  2572. return true;
  2573. } else if (ln.equals("EntryAnchor")) {
  2574. return true;
  2575. } else if (ln.equals("ExitAnchor")) {
  2576. return true;
  2577. } else if (ln.equals("LigatureAnchor")) {
  2578. return true;
  2579. } else if (ln.equals("MarkAnchor")) {
  2580. return true;
  2581. } else if (ln.equals("Mark2Anchor")) {
  2582. return true;
  2583. } else {
  2584. return false;
  2585. }
  2586. }
  2587. private Map<Integer,Integer> getCMAP() {
  2588. Map<Integer,Integer> cmap = new TreeMap();
  2589. for (int[] cme : cmapEntries) {
  2590. Integer c = Integer.valueOf(cme[0]);
  2591. Integer g = Integer.valueOf(cme[1]);
  2592. cmap.put(c, g);
  2593. }
  2594. return cmap;
  2595. }
  2596. private Map<Integer,Integer> getGMAP() {
  2597. Map<Integer,Integer> gmap = new TreeMap();
  2598. for (int[] cme : cmapEntries) {
  2599. Integer c = Integer.valueOf(cme[0]);
  2600. Integer g = Integer.valueOf(cme[1]);
  2601. gmap.put(g, c);
  2602. }
  2603. return gmap;
  2604. }
  2605. private int[][] getHMTX() {
  2606. int ne = hmtxEntries.size();
  2607. int[][] hmtx = new int [ ne ] [ 2 ];
  2608. for (int i = 0; i < ne; i++) {
  2609. int[] ea = hmtxEntries.get(i);
  2610. if (ea != null) {
  2611. hmtx [ i ] [ 0 ] = ea[0];
  2612. hmtx [ i ] [ 1 ] = ea[1];
  2613. }
  2614. }
  2615. return hmtx;
  2616. }
  2617. private GlyphClassTable extractClassDefMapping(Map<String,Integer> glyphClasses, int format, boolean clearSourceMap) {
  2618. GlyphClassTable ct;
  2619. if (format == 1) {
  2620. ct = extractClassDefMapping1(extractClassMappings(glyphClasses, clearSourceMap));
  2621. } else if (format == 2) {
  2622. ct = extractClassDefMapping2(extractClassMappings(glyphClasses, clearSourceMap));
  2623. } else {
  2624. ct = null;
  2625. }
  2626. return ct;
  2627. }
  2628. private GlyphClassTable extractClassDefMapping1(int[][] cma) {
  2629. List entries = new ArrayList<Integer>();
  2630. int s = -1;
  2631. int l = -1;
  2632. Integer zero = Integer.valueOf(0);
  2633. for (int[] m : cma) {
  2634. int g = m[0];
  2635. int c = m[1];
  2636. if (s < 0) {
  2637. s = g;
  2638. l = g - 1;
  2639. entries.add(Integer.valueOf(s));
  2640. }
  2641. while (g > (l + 1)) {
  2642. entries.add(zero);
  2643. l++;
  2644. }
  2645. assert l == (g - 1);
  2646. entries.add(Integer.valueOf(c));
  2647. l = g;
  2648. }
  2649. return GlyphClassTable.createClassTable(entries);
  2650. }
  2651. private GlyphClassTable extractClassDefMapping2(int[][] cma) {
  2652. List entries = new ArrayList<Integer>();
  2653. int s = -1;
  2654. int e = s;
  2655. int l = -1;
  2656. for (int[] m : cma) {
  2657. int g = m[0];
  2658. int c = m[1];
  2659. if (c != l) {
  2660. if (s >= 0) {
  2661. entries.add(new GlyphClassTable.MappingRange(s, e, l));
  2662. }
  2663. s = e = g;
  2664. } else {
  2665. e = g;
  2666. }
  2667. l = c;
  2668. }
  2669. return GlyphClassTable.createClassTable(entries);
  2670. }
  2671. private int[][] extractClassMappings(Map<String,Integer> glyphClasses, boolean clearSourceMap) {
  2672. int nc = glyphClasses.size();
  2673. int i = 0;
  2674. int[][] cma = new int [ nc ] [ 2 ];
  2675. for (Map.Entry<String,Integer> e : glyphClasses.entrySet()) {
  2676. Integer gid = glyphIds.get(e.getKey());
  2677. assert gid != null;
  2678. int[] m = cma [ i ];
  2679. m [ 0 ] = (int) gid;
  2680. m [ 1 ] = (int) e.getValue();
  2681. i++;
  2682. }
  2683. if (clearSourceMap) {
  2684. glyphClasses.clear();
  2685. }
  2686. return sortClassMappings(cma);
  2687. }
  2688. private int[][] sortClassMappings(int[][] cma) {
  2689. Arrays.sort(cma, new Comparator<int[]>() {
  2690. public int compare(int[] m1, int[] m2) {
  2691. assert m1.length > 0;
  2692. assert m2.length > 0;
  2693. if (m1[0] < m2[0]) {
  2694. return -1;
  2695. } else if (m1[0] > m2[0]) {
  2696. return 1;
  2697. } else {
  2698. return 0;
  2699. }
  2700. }
  2701. }
  2702. );
  2703. return cma;
  2704. }
  2705. // sort coverage entries and subtable entries together
  2706. private boolean sortEntries(List cel, List sel) {
  2707. assert cel != null;
  2708. assert sel != null;
  2709. if (cel.size() == sel.size()) {
  2710. int np = cel.size();
  2711. Object[][] pa = new Object [ np ] [ 2 ];
  2712. for (int i = 0; i < np; i++) {
  2713. pa [ i ] [ 0 ] = cel.get(i);
  2714. pa [ i ] [ 1 ] = sel.get(i);
  2715. }
  2716. Arrays.sort(pa, new Comparator<Object[]>() {
  2717. public int compare(Object[] p1, Object[] p2) {
  2718. assert p1.length == 2;
  2719. assert p2.length == 2;
  2720. int c1 = (Integer) p1[0];
  2721. int c2 = (Integer) p2[0];
  2722. if (c1 < c2) {
  2723. return -1;
  2724. } else if (c1 > c2) {
  2725. return 1;
  2726. } else {
  2727. return 0;
  2728. }
  2729. }
  2730. }
  2731. );
  2732. cel.clear();
  2733. sel.clear();
  2734. for (int i = 0; i < np; i++) {
  2735. cel.add(pa [ i ] [ 0 ]);
  2736. sel.add(pa [ i ] [ 1 ]);
  2737. }
  2738. assert cel.size() == sel.size();
  2739. return true;
  2740. } else {
  2741. return false;
  2742. }
  2743. }
  2744. private String makeCoverageKey(String prefix, int index) {
  2745. assert prefix != null;
  2746. assert prefix.length() == 2;
  2747. assert index < 100;
  2748. return prefix + CharUtilities.padLeft(Integer.toString(index, 10), 2, '0');
  2749. }
  2750. private List extractCoverageEntries() {
  2751. List entries = new ArrayList<Integer>(coverageEntries);
  2752. clearCoverage();
  2753. return entries;
  2754. }
  2755. private void clearCoverageEntries() {
  2756. coverageEntries.clear();
  2757. ctFormat = -1;
  2758. ctIndex = -1;
  2759. }
  2760. private void assertCoverageEntriesClear() {
  2761. assert coverageEntries.size() == 0;
  2762. }
  2763. private GlyphCoverageTable extractCoverage() {
  2764. assert (ctFormat == 1) || (ctFormat == 2);
  2765. assert ctIndex >= 0;
  2766. GlyphCoverageTable coverage = GlyphCoverageTable.createCoverageTable(extractCoverageEntries());
  2767. clearCoverage();
  2768. return coverage;
  2769. }
  2770. private void clearCoverages() {
  2771. coverages.clear();
  2772. }
  2773. private void assertCoverageClear() {
  2774. assert ctFormat == -1;
  2775. assert ctIndex == -1;
  2776. assertCoverageEntriesClear();
  2777. }
  2778. private void clearCoverage() {
  2779. ctFormat = -1;
  2780. ctIndex = -1;
  2781. clearCoverageEntries();
  2782. }
  2783. private void assertCoveragesClear() {
  2784. assert coverages.size() == 0;
  2785. }
  2786. private GlyphCoverageTable[] getCoveragesWithPrefix(String prefix) {
  2787. assert prefix != null;
  2788. int prefixLength = prefix.length();
  2789. Set<String> keys = coverages.keySet();
  2790. int mi = -1; // maximum coverage table index
  2791. for (String k : keys) {
  2792. if (k.startsWith(prefix)) {
  2793. int i = Integer.parseInt(k.substring(prefixLength));
  2794. if (i > mi) {
  2795. mi = i;
  2796. }
  2797. }
  2798. }
  2799. GlyphCoverageTable[] gca = new GlyphCoverageTable [ mi + 1 ];
  2800. for (String k : keys) {
  2801. if (k.startsWith(prefix)) {
  2802. int i = Integer.parseInt(k.substring(prefixLength));
  2803. if (i >= 0) {
  2804. gca [ i ] = coverages.get(k);
  2805. }
  2806. }
  2807. }
  2808. return gca;
  2809. }
  2810. private boolean hasMissingCoverage(GlyphCoverageTable[] gca) {
  2811. assert gca != null;
  2812. int nc = 0;
  2813. for (int i = 0, n = gca.length; i < n; i++) {
  2814. if (gca [ i ] != null) {
  2815. nc++;
  2816. }
  2817. }
  2818. return nc != gca.length;
  2819. }
  2820. private String makeFeatureId(int fid) {
  2821. assert fid >= 0;
  2822. return "f" + fid;
  2823. }
  2824. private String makeLookupId(int lid) {
  2825. assert lid >= 0;
  2826. return "lu" + lid;
  2827. }
  2828. private void clearScripts() {
  2829. scripts.clear();
  2830. }
  2831. private List<String> extractLanguageFeatures() {
  2832. List<String> lfl = new ArrayList<String>(languageFeatures);
  2833. clearLanguageFeatures();
  2834. return lfl;
  2835. }
  2836. private void assertLanguageFeaturesClear() {
  2837. assert languageFeatures.size() == 0;
  2838. }
  2839. private void clearLanguageFeatures() {
  2840. languageFeatures.clear();
  2841. }
  2842. private Map<String,List<String>> extractLanguages() {
  2843. Map<String,List<String>> lm = new HashMap(languages);
  2844. clearLanguages();
  2845. return lm;
  2846. }
  2847. private void clearLanguages() {
  2848. languages.clear();
  2849. }
  2850. private void assertFeatureLookupsClear() {
  2851. assert featureLookups.size() == 0;
  2852. }
  2853. private List extractFeatureLookups() {
  2854. List lookups = new ArrayList<String>(featureLookups);
  2855. clearFeatureLookups();
  2856. return lookups;
  2857. }
  2858. private void clearFeatureLookups() {
  2859. featureLookups.clear();
  2860. }
  2861. private void assertFeatureClear() {
  2862. assert flIndex == -1;
  2863. assert featureTag == null;
  2864. assertFeatureLookupsClear();
  2865. }
  2866. private Object[] extractFeature() {
  2867. Object[] fa = new Object [ 2 ];
  2868. fa[0] = featureTag;
  2869. fa[1] = extractFeatureLookups();
  2870. clearFeature();
  2871. return fa;
  2872. }
  2873. private void clearFeature() {
  2874. flIndex = -1;
  2875. featureTag = null;
  2876. clearFeatureLookups();
  2877. }
  2878. private void nextFeature() {
  2879. flSequence++;
  2880. }
  2881. private void clearFeatures() {
  2882. features.clear();
  2883. }
  2884. private void clearSubtableInLookup() {
  2885. stFormat = 0;
  2886. clearCoverages();
  2887. }
  2888. private void clearSubtablesInLookup() {
  2889. clearSubtableInLookup();
  2890. stSequence = 0;
  2891. }
  2892. private void clearSubtablesInTable() {
  2893. clearSubtablesInLookup();
  2894. subtables.clear();
  2895. }
  2896. private void nextSubtableInLookup() {
  2897. stSequence++;
  2898. clearSubtableInLookup();
  2899. }
  2900. private void assertLookupClear() {
  2901. assert ltIndex == -1;
  2902. assert ltFlags == 0;
  2903. }
  2904. private void clearLookup() {
  2905. ltIndex = -1;
  2906. ltFlags = 0;
  2907. clearSubtablesInLookup();
  2908. }
  2909. private Map<GlyphTable.LookupSpec,List<String>> extractLookups() {
  2910. Map<GlyphTable.LookupSpec,List<String>> lookups = new LinkedHashMap<GlyphTable.LookupSpec,List<String>>();
  2911. for (String st : scripts.keySet()) {
  2912. Map<String,List<String>> lm = scripts.get(st);
  2913. if (lm != null) {
  2914. for (String lt : lm.keySet()) {
  2915. List<String> fids = lm.get(lt);
  2916. if (fids != null) {
  2917. for (String fid : fids) {
  2918. if (fid != null) {
  2919. Object[] fa = features.get(fid);
  2920. if (fa != null) {
  2921. assert fa.length == 2;
  2922. String ft = (String) fa[0];
  2923. List<String> lids = (List<String>) fa[1];
  2924. if ((lids != null) && (lids.size() > 0)) {
  2925. GlyphTable.LookupSpec ls = new GlyphTable.LookupSpec(st, lt, ft);
  2926. lookups.put(ls, lids);
  2927. }
  2928. }
  2929. }
  2930. }
  2931. }
  2932. }
  2933. }
  2934. }
  2935. clearScripts();
  2936. clearLanguages();
  2937. clearFeatures();
  2938. return lookups;
  2939. }
  2940. private void clearLookups() {
  2941. clearLookup();
  2942. clearSubtablesInTable();
  2943. ltSequence = 0;
  2944. flSequence = 0;
  2945. }
  2946. private void nextLookup() {
  2947. ltSequence++;
  2948. clearLookup();
  2949. }
  2950. private void clearTable() {
  2951. clearLookups();
  2952. }
  2953. private void assertSubtableClear() {
  2954. assert stFormat == 0;
  2955. assertCoverageEntriesClear();
  2956. }
  2957. private void assertSubtablesClear() {
  2958. assertSubtableClear();
  2959. assert subtables.size() == 0;
  2960. }
  2961. private void clearSubtableEntries() {
  2962. subtableEntries.clear();
  2963. }
  2964. private void assertSubtableEntriesClear() {
  2965. assert subtableEntries.size() == 0;
  2966. }
  2967. private List extractSubtableEntries() {
  2968. List entries = new ArrayList(subtableEntries);
  2969. clearSubtableEntries();
  2970. return entries;
  2971. }
  2972. private int[] extractAlternates() {
  2973. int[] aa = new int [ alternates.size() ];
  2974. int i = 0;
  2975. for (Integer a : alternates) {
  2976. aa[i++] = (int) a;
  2977. }
  2978. clearAlternates();
  2979. return aa;
  2980. }
  2981. private void clearAlternates() {
  2982. alternates.clear();
  2983. }
  2984. private LigatureSet extractLigatures() {
  2985. LigatureSet ls = new LigatureSet(ligatures);
  2986. clearLigatures();
  2987. return ls;
  2988. }
  2989. private void clearLigatures() {
  2990. ligatures.clear();
  2991. }
  2992. private int[] extractSubstitutes() {
  2993. int[] aa = new int [ substitutes.size() ];
  2994. int i = 0;
  2995. for (Integer a : substitutes) {
  2996. aa[i++] = (int) a;
  2997. }
  2998. clearSubstitutes();
  2999. return aa;
  3000. }
  3001. private void clearSubstitutes() {
  3002. substitutes.clear();
  3003. }
  3004. private List extractSequenceEntries() {
  3005. List sequences = extractSubtableEntries();
  3006. int[][] sa = new int [ sequences.size() ] [];
  3007. int i = 0;
  3008. for (Object s : sequences) {
  3009. if (s instanceof int[]) {
  3010. sa[i++] = (int[]) s;
  3011. }
  3012. }
  3013. List entries = new ArrayList();
  3014. entries.add(sa);
  3015. return entries;
  3016. }
  3017. private RuleLookup[] extractRuleLookups() {
  3018. RuleLookup[] lookups = (RuleLookup[]) ruleLookups.toArray(new RuleLookup [ ruleLookups.size() ]);
  3019. clearRuleLookups();
  3020. return lookups;
  3021. }
  3022. private void clearRuleLookups() {
  3023. ruleLookups.clear();
  3024. }
  3025. private GlyphPositioningTable.Value parseValue(String[] en, Attributes attrs, int format) throws SAXException {
  3026. String xPlacement = attrs.getValue("XPlacement");
  3027. int xp = 0;
  3028. if (xPlacement != null) {
  3029. xp = Integer.parseInt(xPlacement);
  3030. } else if ((format & GlyphPositioningTable.Value.X_PLACEMENT) != 0) {
  3031. missingParameter(en, "xPlacement");
  3032. }
  3033. String yPlacement = attrs.getValue("YPlacement");
  3034. int yp = 0;
  3035. if (yPlacement != null) {
  3036. yp = Integer.parseInt(yPlacement);
  3037. } else if ((format & GlyphPositioningTable.Value.Y_PLACEMENT) != 0) {
  3038. missingParameter(en, "yPlacement");
  3039. }
  3040. String xAdvance = attrs.getValue("XAdvance");
  3041. int xa = 0;
  3042. if (xAdvance != null) {
  3043. xa = Integer.parseInt(xAdvance);
  3044. } else if ((format & GlyphPositioningTable.Value.X_ADVANCE) != 0) {
  3045. missingParameter(en, "xAdvance");
  3046. }
  3047. String yAdvance = attrs.getValue("YAdvance");
  3048. int ya = 0;;
  3049. if (yAdvance != null) {
  3050. ya = Integer.parseInt(yAdvance);
  3051. } else if ((format & GlyphPositioningTable.Value.Y_ADVANCE) != 0) {
  3052. missingParameter(en, "yAdvance");
  3053. }
  3054. return new GlyphPositioningTable.Value(xp, yp, xa, ya, null, null, null, null);
  3055. }
  3056. private void assertPairClear() {
  3057. assert g2 == -1;
  3058. assert v1 == null;
  3059. assert v2 == null;
  3060. }
  3061. private void clearPair() {
  3062. g2 = -1;
  3063. v1 = null;
  3064. v2 = null;
  3065. }
  3066. private void assertPairsClear() {
  3067. assert pairs.size() == 0;
  3068. }
  3069. private void clearPairs() {
  3070. pairs.clear();
  3071. psIndex = -1;
  3072. }
  3073. private PairValues[] extractPairs() {
  3074. PairValues[] pva = (PairValues[]) pairs.toArray(new PairValues [ pairs.size() ]);
  3075. clearPairs();
  3076. return pva;
  3077. }
  3078. private void assertPairSetsClear() {
  3079. assert pairSets.size() == 0;
  3080. }
  3081. private void clearPairSets() {
  3082. pairSets.clear();
  3083. }
  3084. private PairValues[][] extractPairSets() {
  3085. PairValues[][] pvm = (PairValues[][]) pairSets.toArray(new PairValues [ pairSets.size() ][]);
  3086. clearPairSets();
  3087. return pvm;
  3088. }
  3089. private Anchor[] extractAnchors() {
  3090. Anchor[] aa = (Anchor[]) anchors.toArray(new Anchor [ anchors.size() ]);
  3091. anchors.clear();
  3092. return aa;
  3093. }
  3094. private MarkAnchor[] extractMarkAnchors() {
  3095. MarkAnchor[] maa = new MarkAnchor [ markAnchors.size() ];
  3096. maa = (MarkAnchor[]) markAnchors.toArray(new MarkAnchor [ maa.length ]);
  3097. markAnchors.clear();
  3098. return maa;
  3099. }
  3100. private Anchor[][] extractBaseOrMarkAnchors() {
  3101. int na = baseOrMarkAnchors.size();
  3102. int ncMax = 0;
  3103. for (Anchor[] aa : baseOrMarkAnchors) {
  3104. if (aa != null) {
  3105. int nc = aa.length;
  3106. if (nc > ncMax) {
  3107. ncMax = nc;
  3108. }
  3109. }
  3110. }
  3111. Anchor[][] am = new Anchor [ na ][ ncMax ];
  3112. for (int i = 0; i < na; i++) {
  3113. Anchor[] aa = baseOrMarkAnchors.get(i);
  3114. if (aa != null) {
  3115. for (int j = 0; j < ncMax; j++) {
  3116. if (j < aa.length) {
  3117. am [ i ] [ j ] = aa [ j ];
  3118. }
  3119. }
  3120. }
  3121. }
  3122. baseOrMarkAnchors.clear();
  3123. return am;
  3124. }
  3125. private Integer computeClassCount(Anchor[][] am) {
  3126. int ncMax = 0;
  3127. for (int i = 0, n = am.length; i < n; i++) {
  3128. Anchor[] aa = am [ i ];
  3129. if (aa != null) {
  3130. int nc = aa.length;
  3131. if (nc > ncMax) {
  3132. ncMax = nc;
  3133. }
  3134. }
  3135. }
  3136. return Integer.valueOf(ncMax);
  3137. }
  3138. private Anchor[][] extractComponents() {
  3139. Anchor[][] cam = new Anchor [ components.size() ][];
  3140. cam = (Anchor[][]) components.toArray(new Anchor [ cam.length ][]);
  3141. components.clear();
  3142. return cam;
  3143. }
  3144. private Anchor[][][] extractLigatureAnchors() {
  3145. int na = ligatureAnchors.size();
  3146. int ncMax = 0;
  3147. int nxMax = 0;
  3148. for (Anchor[][] cm : ligatureAnchors) {
  3149. if (cm != null) {
  3150. int nx = cm.length;
  3151. if (nx > nxMax) {
  3152. nxMax = nx;
  3153. }
  3154. for (Anchor[] aa : cm) {
  3155. if (aa != null) {
  3156. int nc = aa.length;
  3157. if (nc > ncMax) {
  3158. ncMax = nc;
  3159. }
  3160. }
  3161. }
  3162. }
  3163. }
  3164. Anchor[][][] lam = new Anchor [ na ] [ nxMax ] [ ncMax ];
  3165. for (int i = 0; i < na; i++) {
  3166. Anchor[][] cm = ligatureAnchors.get(i);
  3167. if (cm != null) {
  3168. for (int j = 0; j < nxMax; j++) {
  3169. if (j < cm.length) {
  3170. Anchor[] aa = cm [ j ];
  3171. if (aa != null) {
  3172. for (int k = 0; k < ncMax; k++) {
  3173. if (k < aa.length) {
  3174. lam [ i ] [ j ] [ k ] = aa [ k ];
  3175. }
  3176. }
  3177. }
  3178. }
  3179. }
  3180. }
  3181. }
  3182. ligatureAnchors.clear();
  3183. return lam;
  3184. }
  3185. private Integer computeLigaturesClassCount(Anchor[][][] lam) {
  3186. int ncMax = 0;
  3187. if (lam != null) {
  3188. for (Anchor[][] cm : lam) {
  3189. if (cm != null) {
  3190. for (Anchor[] aa : cm) {
  3191. if (aa != null) {
  3192. int nc = aa.length;;
  3193. if (nc > ncMax) {
  3194. ncMax = nc;
  3195. }
  3196. }
  3197. }
  3198. }
  3199. }
  3200. }
  3201. return Integer.valueOf(ncMax);
  3202. }
  3203. private Integer computeLigaturesComponentCount(Anchor[][][] lam) {
  3204. int nxMax = 0;
  3205. if (lam != null) {
  3206. for (Anchor[][] cm : lam) {
  3207. if (cm != null) {
  3208. int nx = cm.length;;
  3209. if (nx > nxMax) {
  3210. nxMax = nx;
  3211. }
  3212. }
  3213. }
  3214. }
  3215. return Integer.valueOf(nxMax);
  3216. }
  3217. private Anchor[] extractAttachmentAnchors() {
  3218. int na = attachmentAnchors.size();
  3219. Anchor[] aa = new Anchor [ na * 2 ];
  3220. for (int i = 0; i < na; i++) {
  3221. Anchor[] ea = attachmentAnchors.get(i);
  3222. int ne = ea.length;
  3223. if (ne > 0) {
  3224. aa [ (i * 2) + 0 ] = ea[0];
  3225. }
  3226. if (ne > 1) {
  3227. aa [ (i * 2) + 1 ] = ea[1];
  3228. }
  3229. }
  3230. attachmentAnchors.clear();
  3231. return aa;
  3232. }
  3233. private void addGDEFSubtable(int stType, GlyphMappingTable mapping) {
  3234. subtables.add(GlyphDefinitionTable.createSubtable(stType, makeLookupId(ltSequence), stSequence, ltFlags, stFormat, mapping, extractSubtableEntries()));
  3235. nextSubtableInLookup();
  3236. }
  3237. private void addGSUBSubtable(int stType, GlyphCoverageTable coverage, List entries) {
  3238. subtables.add(GlyphSubstitutionTable.createSubtable(stType, makeLookupId(ltSequence), stSequence, ltFlags, stFormat, coverage, entries));
  3239. nextSubtableInLookup();
  3240. }
  3241. private void addGSUBSubtable(int stType, GlyphCoverageTable coverage) {
  3242. addGSUBSubtable(stType, coverage, extractSubtableEntries());
  3243. }
  3244. private void addGPOSSubtable(int stType, GlyphCoverageTable coverage, List entries) {
  3245. subtables.add(GlyphPositioningTable.createSubtable(stType, makeLookupId(ltSequence), stSequence, ltFlags, stFormat, coverage, entries));
  3246. nextSubtableInLookup();
  3247. }
  3248. private void addGPOSSubtable(int stType, GlyphCoverageTable coverage) {
  3249. addGPOSSubtable(stType, coverage, extractSubtableEntries());
  3250. }
  3251. }
  3252. private int mapGlyphId0(String glyph) {
  3253. assert glyphIds != null;
  3254. Integer gid = glyphIds.get(glyph);
  3255. if (gid != null) {
  3256. return (int) gid;
  3257. } else {
  3258. return -1;
  3259. }
  3260. }
  3261. private int mapGlyphId(String glyph, String[] currentElement) throws SAXException {
  3262. int g = mapGlyphId0(glyph);
  3263. if (g < 0) {
  3264. unsupportedGlyph(currentElement, glyph);
  3265. return -1;
  3266. } else {
  3267. return g;
  3268. }
  3269. }
  3270. private int[] mapGlyphIds(String glyphs, String[] currentElement) throws SAXException {
  3271. String[] ga = glyphs.split(",");
  3272. int[] gids = new int [ ga.length ];
  3273. int i = 0;
  3274. for (String glyph : ga) {
  3275. gids[i++] = mapGlyphId(glyph, currentElement);
  3276. }
  3277. return gids;
  3278. }
  3279. private int mapGlyphIdToChar(String glyph) {
  3280. assert glyphIds != null;
  3281. Integer gid = glyphIds.get(glyph);
  3282. if (gid != null) {
  3283. if (gmap != null) {
  3284. Integer cid = gmap.get(gid);
  3285. if (cid != null) {
  3286. return cid.intValue();
  3287. }
  3288. }
  3289. }
  3290. return -1;
  3291. }
  3292. private String formatLocator() {
  3293. if (locator == null) {
  3294. return "{null}";
  3295. } else {
  3296. return "{" + locator.getSystemId() + ":" + locator.getLineNumber() + ":" + locator.getColumnNumber() + "}";
  3297. }
  3298. }
  3299. private void unsupportedElement(String[] en) throws SAXException {
  3300. throw new SAXException(formatLocator() + ": unsupported element " + formatExpandedName(en));
  3301. }
  3302. private void notPermittedInElementContext(String[] en, String[] cn, Object xns) throws SAXException {
  3303. assert en != null;
  3304. assert cn != null;
  3305. String s = "element " + formatExpandedName(en) + " not permitted in current element context " + formatExpandedName(cn);
  3306. if (xns == null) {
  3307. s += ", expected root context";
  3308. } else if (xns instanceof String[][]) {
  3309. int nxn = 0;
  3310. s += ", expected one of { ";
  3311. for (String[] xn : (String[][]) xns) {
  3312. if (nxn++ > 0) {
  3313. s += ", ";
  3314. }
  3315. s += formatExpandedName(xn);
  3316. }
  3317. s += " }";
  3318. } else if (xns instanceof String[]) {
  3319. s += ", expected " + formatExpandedName((String[]) xns);
  3320. }
  3321. throw new SAXException(formatLocator() + ": " + s);
  3322. }
  3323. private void missingRequiredAttribute(String[] en, String name) throws SAXException {
  3324. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " missing required attribute " + name);
  3325. }
  3326. private void duplicateGlyph(String[] en, String name, int gid) throws SAXException {
  3327. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " contains duplicate name \"" + name + "\", with identifier value " + gid);
  3328. }
  3329. private void unsupportedGlyph(String[] en, String name) throws SAXException {
  3330. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " refers to unsupported glyph id \"" + name + "\"");
  3331. }
  3332. private void duplicateCMAPCharacter(String[] en, int cid) throws SAXException {
  3333. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " contains duplicate cmap character code: " + CharUtilities.format(cid));
  3334. }
  3335. private void duplicateCMAPGlyph(String[] en, int gid) throws SAXException {
  3336. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " contains duplicate cmap glyph code: " + gid);
  3337. }
  3338. private void duplicateGlyphClass(String[] en, String name, String glyphClass) throws SAXException {
  3339. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " contains duplicate glyph class for \"" + name + "\", with class value " + glyphClass);
  3340. }
  3341. private void unsupportedFormat(String[] en, int format) throws SAXException {
  3342. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " refers to unsupported table format \"" + format + "\"");
  3343. }
  3344. private void invalidIndex(String[] en, int actual, int expected) throws SAXException {
  3345. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " specifies invalid index " + actual + ", expected " + expected);
  3346. }
  3347. private void mismatchedIndex(String[] en, String label, int actual, int expected) throws SAXException {
  3348. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " mismatched " + label + " index: got " + actual + ", expected " + expected);
  3349. }
  3350. private void mismatchedEntries(String[] en, int nce, int nse) throws SAXException {
  3351. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " mismatched coverage and subtable entry counts, # coverages " + nce + ", # entries " + nse);
  3352. }
  3353. private void missingParameter(String[] en, String label) throws SAXException {
  3354. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " missing " + label + " parameter");
  3355. }
  3356. private void duplicateParameter(String[] en, String label) throws SAXException {
  3357. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " duplicate " + label + " parameter");
  3358. }
  3359. private void duplicateCoverageIndex(String[] en, int index) throws SAXException {
  3360. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " duplicate coverage table index " + index);
  3361. }
  3362. private void missingCoverage(String[] en, String type, int expected) throws SAXException {
  3363. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " missing " + type + " coverage table, expected " + ((expected > 0) ? expected : 1) + " table(s)");
  3364. }
  3365. private void missingTag(String[] en, String label) throws SAXException {
  3366. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " missing " + label + " tag");
  3367. }
  3368. private void duplicateTag(String[] en, String label, String tag) throws SAXException {
  3369. throw new SAXException(formatLocator() + ": element " + formatExpandedName(en) + " duplicate " + label + " tag: " + tag);
  3370. }
  3371. private static String[] makeExpandedName(String uri, String localName, String qName) {
  3372. if ((uri != null) && (uri.length() == 0)) {
  3373. uri = null;
  3374. }
  3375. if ((localName != null) && (localName.length() == 0)) {
  3376. localName = null;
  3377. }
  3378. if ((uri == null) && (localName == null)) {
  3379. uri = extractPrefix(qName);
  3380. localName = extractLocalName(qName);
  3381. }
  3382. return new String[] { uri, localName };
  3383. }
  3384. private static String extractPrefix(String qName) {
  3385. String[] sa = qName.split(":");
  3386. if (sa.length == 2) {
  3387. return sa[0];
  3388. } else {
  3389. return null;
  3390. }
  3391. }
  3392. private static String extractLocalName(String qName) {
  3393. String[] sa = qName.split(":");
  3394. if (sa.length == 2) {
  3395. return sa[1];
  3396. } else if (sa.length == 1) {
  3397. return sa[0];
  3398. } else {
  3399. return null;
  3400. }
  3401. }
  3402. private static boolean sameExpandedName(String[] n1, String[] n2) {
  3403. String u1 = n1[0];
  3404. String u2 = n2[0];
  3405. if ((u1 == null) ^ (u2 == null)) {
  3406. return false;
  3407. }
  3408. if ((u1 != null) && (u2 != null)) {
  3409. if (! u1.equals(u2)) {
  3410. return false;
  3411. }
  3412. }
  3413. String l1 = n1[1];
  3414. String l2 = n2[1];
  3415. if ((l1 == null) ^ (l2 == null)) {
  3416. return false;
  3417. }
  3418. if ((l1 != null) && (l2 != null)) {
  3419. if (! l1.equals(l2)) {
  3420. return false;
  3421. }
  3422. }
  3423. return true;
  3424. }
  3425. private static String formatExpandedName(String[] n) {
  3426. String u = (n[0] != null) ? n[0] : "null";
  3427. String l = (n[1] != null) ? n[1] : "null";
  3428. return "{" + u + "}" + l;
  3429. }
  3430. }