synctex_parser.m.svn-base 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249
  1. /*
  2. Copyright (c) 2008, 2009, 2010 , 2011 jerome DOT laurens AT u-bourgogne DOT fr
  3. This file is part of the SyncTeX package.
  4. Latest Revision: Tue Jun 14 08:23:30 UTC 2011
  5. Version: 1.16
  6. See synctex_parser_readme.txt for more details
  7. License:
  8. --------
  9. Permission is hereby granted, free of charge, to any person
  10. obtaining a copy of this software and associated documentation
  11. files (the "Software"), to deal in the Software without
  12. restriction, including without limitation the rights to use,
  13. copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the
  15. Software is furnished to do so, subject to the following
  16. conditions:
  17. The above copyright notice and this permission notice shall be
  18. included in all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. OTHER DEALINGS IN THE SOFTWARE
  27. Except as contained in this notice, the name of the copyright holder
  28. shall not be used in advertising or otherwise to promote the sale,
  29. use or other dealings in this Software without prior written
  30. authorization from the copyright holder.
  31. Acknowledgments:
  32. ----------------
  33. The author received useful remarks from the pdfTeX developers, especially Hahn The Thanh,
  34. and significant help from XeTeX developer Jonathan Kew
  35. Nota Bene:
  36. ----------
  37. If you include or use a significant part of the synctex package into a software,
  38. I would appreciate to be listed as contributor and see "SyncTeX" highlighted.
  39. Version 1
  40. Thu Jun 19 09:39:21 UTC 2008
  41. */
  42. /* We assume that high level application like pdf viewers will want
  43. * to embed this code as is. We assume that they also have locale.h and setlocale.
  44. * For other tools such as TeXLive tools, you must define SYNCTEX_USE_LOCAL_HEADER,
  45. * when building. You also have to create and customize synctex_parser_local.h to fit your system.
  46. * In particular, the HAVE_LOCALE_H and HAVE_SETLOCALE macros should be properly defined.
  47. * With this design, you should not need to edit this file. */
  48. # if defined(SYNCTEX_USE_LOCAL_HEADER)
  49. # include "synctex_parser_local.h"
  50. # else
  51. # define HAVE_LOCALE_H 1
  52. # define HAVE_SETLOCALE 1
  53. # if defined(_MSC_VER)
  54. # define SYNCTEX_INLINE __inline
  55. # else
  56. # define SYNCTEX_INLINE inline
  57. # endif
  58. # endif
  59. #include <stdlib.h>
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include <errno.h>
  63. #include <limits.h>
  64. #if defined(HAVE_LOCALE_H)
  65. #include <locale.h>
  66. #endif
  67. /* The data is organized in a graph with multiple entries.
  68. * The root object is a scanner, it is created with the contents on a synctex file.
  69. * Each leaf of the tree is a synctex_node_t object.
  70. * There are 3 subtrees, two of them sharing the same leaves.
  71. * The first tree is the list of input records, where input file names are associated with tags.
  72. * The second tree is the box tree as given by TeX when shipping pages out.
  73. * First level objects are sheets, containing boxes, glues, kerns...
  74. * The third tree allows to browse leaves according to tag and line.
  75. */
  76. #include "synctex_parser.h"
  77. #include "synctex_parser_utils.h"
  78. /* These are the possible extensions of the synctex file */
  79. const char * synctex_suffix = ".synctex";
  80. const char * synctex_suffix_gz = ".gz";
  81. /* each synctex node has a class */
  82. typedef struct __synctex_class_t _synctex_class_t;
  83. typedef _synctex_class_t * synctex_class_t;
  84. /* synctex_node_t is a pointer to a node
  85. * _synctex_node is the target of the synctex_node_t pointer
  86. * It is a pseudo object oriented program.
  87. * class is a pointer to the class object the node belongs to.
  88. * implementation is meant to contain the private data of the node
  89. * basically, there are 2 kinds of information: navigation information and
  90. * synctex information. Both will depend on the type of the node,
  91. * thus different nodes will have different private data.
  92. * There is no inheritancy overhead.
  93. */
  94. typedef union _synctex_info_t {
  95. int INT;
  96. char * PTR;
  97. } synctex_info_t;
  98. struct _synctex_node {
  99. synctex_class_t class;
  100. synctex_info_t * implementation;
  101. };
  102. /* Each node of the tree, except the scanner itself belongs to a class.
  103. * The class object is just a struct declaring the owning scanner
  104. * This is a pointer to the scanner as root of the tree.
  105. * The type is used to identify the kind of node.
  106. * The class declares pointers to a creator and a destructor method.
  107. * The log and display fields are used to log and display the node.
  108. * display will also display the child, sibling and parent sibling.
  109. * parent, child and sibling are used to navigate the tree,
  110. * from TeX box hierarchy point of view.
  111. * The friend field points to a method which allows to navigate from friend to friend.
  112. * A friend is a node with very close tag and line numbers.
  113. * Finally, the info field point to a method giving the private node info offset.
  114. */
  115. typedef synctex_node_t *(*_synctex_node_getter_t)(synctex_node_t);
  116. typedef synctex_info_t *(*_synctex_info_getter_t)(synctex_node_t);
  117. struct __synctex_class_t {
  118. synctex_scanner_t scanner;
  119. int type;
  120. synctex_node_t (*new)(synctex_scanner_t scanner);
  121. void (*free)(synctex_node_t);
  122. void (*log)(synctex_node_t);
  123. void (*display)(synctex_node_t);
  124. _synctex_node_getter_t parent;
  125. _synctex_node_getter_t child;
  126. _synctex_node_getter_t sibling;
  127. _synctex_node_getter_t friend;
  128. _synctex_node_getter_t next_box;
  129. _synctex_info_getter_t info;
  130. };
  131. # ifdef SYNCTEX_NOTHING
  132. # pragma mark -
  133. # pragma mark Abstract OBJECTS and METHODS
  134. # endif
  135. /* These macros are shortcuts
  136. * This macro checks if a message can be sent.
  137. */
  138. # define SYNCTEX_CAN_PERFORM(NODE,SELECTOR)\
  139. (NULL!=((((NODE)->class))->SELECTOR))
  140. /* This macro is some kind of objc_msg_send.
  141. * It takes care of sending the proper message if possible.
  142. */
  143. # define SYNCTEX_MSG_SEND(NODE,SELECTOR) if (NODE && SYNCTEX_CAN_PERFORM(NODE,SELECTOR)) {\
  144. (*((((NODE)->class))->SELECTOR))(NODE);\
  145. }
  146. /* read only safe getter
  147. */
  148. # define SYNCTEX_GET(NODE,SELECTOR)((NODE && SYNCTEX_CAN_PERFORM(NODE,SELECTOR))?SYNCTEX_GETTER(NODE,SELECTOR)[0]:(NULL))
  149. /* read/write getter
  150. */
  151. # define SYNCTEX_GETTER(NODE,SELECTOR)\
  152. ((synctex_node_t *)((*((((NODE)->class))->SELECTOR))(NODE)))
  153. # define SYNCTEX_FREE(NODE) SYNCTEX_MSG_SEND(NODE,free);
  154. /* Parent getter and setter
  155. */
  156. # define SYNCTEX_PARENT(NODE) SYNCTEX_GET(NODE,parent)
  157. # define SYNCTEX_SET_PARENT(NODE,NEW_PARENT) if (NODE && NEW_PARENT && SYNCTEX_CAN_PERFORM(NODE,parent)){\
  158. SYNCTEX_GETTER(NODE,parent)[0]=NEW_PARENT;\
  159. }
  160. /* Child getter and setter
  161. */
  162. # define SYNCTEX_CHILD(NODE) SYNCTEX_GET(NODE,child)
  163. # define SYNCTEX_SET_CHILD(NODE,NEW_CHILD) if (NODE && NEW_CHILD){\
  164. SYNCTEX_GETTER(NODE,child)[0]=NEW_CHILD;\
  165. SYNCTEX_GETTER(NEW_CHILD,parent)[0]=NODE;\
  166. }
  167. /* Sibling getter and setter
  168. */
  169. # define SYNCTEX_SIBLING(NODE) SYNCTEX_GET(NODE,sibling)
  170. # define SYNCTEX_SET_SIBLING(NODE,NEW_SIBLING) if (NODE && NEW_SIBLING) {\
  171. SYNCTEX_GETTER(NODE,sibling)[0]=NEW_SIBLING;\
  172. if (SYNCTEX_CAN_PERFORM(NEW_SIBLING,parent) && SYNCTEX_CAN_PERFORM(NODE,parent)) {\
  173. SYNCTEX_GETTER(NEW_SIBLING,parent)[0]=SYNCTEX_GETTER(NODE,parent)[0];\
  174. }\
  175. }
  176. /* Friend getter and setter. A friend is a kern, math, glue or void box node which tag and line numbers are similar.
  177. * This is a first filter on the nodes that avoids testing all of them.
  178. * Friends are used mainly in forward synchronization aka from source to output.
  179. */
  180. # define SYNCTEX_FRIEND(NODE) SYNCTEX_GET(NODE,friend)
  181. # define SYNCTEX_SET_FRIEND(NODE,NEW_FRIEND) if (NODE && NEW_FRIEND){\
  182. SYNCTEX_GETTER(NODE,friend)[0]=NEW_FRIEND;\
  183. }
  184. /* Next box getter and setter. The box tree can be traversed from one horizontal box to the other.
  185. * Navigation starts with the deeper boxes.
  186. */
  187. # define SYNCTEX_NEXT_HORIZ_BOX(NODE) SYNCTEX_GET(NODE,next_box)
  188. # define SYNCTEX_SET_NEXT_HORIZ_BOX(NODE,NEXT_BOX) if (NODE && NEXT_BOX){\
  189. SYNCTEX_GETTER(NODE,next_box)[0]=NEXT_BOX;\
  190. }
  191. void _synctex_free_node(synctex_node_t node);
  192. void _synctex_free_leaf(synctex_node_t node);
  193. /* A node is meant to own its child and sibling.
  194. * It is not owned by its parent, unless it is its first child.
  195. * This destructor is for all nodes with children.
  196. */
  197. void _synctex_free_node(synctex_node_t node) {
  198. if (node) {
  199. (*((node->class)->sibling))(node);
  200. SYNCTEX_FREE(SYNCTEX_SIBLING(node));
  201. SYNCTEX_FREE(SYNCTEX_CHILD(node));
  202. free(node);
  203. }
  204. return;
  205. }
  206. /* A node is meant to own its child and sibling.
  207. * It is not owned by its parent, unless it is its first child.
  208. * This destructor is for nodes with no child.
  209. */
  210. void _synctex_free_leaf(synctex_node_t node) {
  211. if (node) {
  212. SYNCTEX_FREE(SYNCTEX_SIBLING(node));
  213. free(node);
  214. }
  215. return;
  216. }
  217. # ifdef __SYNCTEX_WORK__
  218. # include "/usr/include/zlib.h"
  219. # else
  220. # include <zlib.h>
  221. # endif
  222. /* The synctex scanner is the root object.
  223. * Is is initialized with the contents of a text file or a gzipped file.
  224. * The buffer_? are first used to parse the text.
  225. */
  226. struct __synctex_scanner_t {
  227. gzFile file; /* The (possibly compressed) file */
  228. char * buffer_cur; /* current location in the buffer */
  229. char * buffer_start; /* start of the buffer */
  230. char * buffer_end; /* end of the buffer */
  231. char * output_fmt; /* dvi or pdf, not yet used */
  232. char * output; /* the output name used to create the scanner */
  233. char * synctex; /* the .synctex or .synctex.gz name used to create the scanner */
  234. int version; /* 1, not yet used */
  235. struct {
  236. unsigned has_parsed:1; /* Whether the scanner has parsed its underlying synctex file. */
  237. unsigned reserved:sizeof(unsigned)-1; /* alignment */
  238. } flags;
  239. int pre_magnification; /* magnification from the synctex preamble */
  240. int pre_unit; /* unit from the synctex preamble */
  241. int pre_x_offset; /* X offste from the synctex preamble */
  242. int pre_y_offset; /* Y offset from the synctex preamble */
  243. int count; /* Number of records, from the synctex postamble */
  244. float unit; /* real unit, from synctex preamble or post scriptum */
  245. float x_offset; /* X offset, from synctex preamble or post scriptum */
  246. float y_offset; /* Y Offset, from synctex preamble or post scriptum */
  247. synctex_node_t sheet; /* The first sheet node, its siblings are the other sheet nodes */
  248. synctex_node_t input; /* The first input node, its siblings are the other input nodes */
  249. int number_of_lists; /* The number of friend lists */
  250. synctex_node_t * lists_of_friends;/* The friend lists */
  251. _synctex_class_t class[synctex_node_number_of_types]; /* The classes of the nodes of the scanner */
  252. };
  253. /* SYNCTEX_CUR, SYNCTEX_START and SYNCTEX_END are convenient shortcuts
  254. */
  255. # define SYNCTEX_CUR (scanner->buffer_cur)
  256. # define SYNCTEX_START (scanner->buffer_start)
  257. # define SYNCTEX_END (scanner->buffer_end)
  258. # ifdef SYNCTEX_NOTHING
  259. # pragma mark -
  260. # pragma mark OBJECTS, their creators and destructors.
  261. # endif
  262. /* Here, we define the indices for the different informations.
  263. * They are used to declare the size of the implementation.
  264. * For example, if one object uses SYNCTEX_HORIZ_IDX is its size,
  265. * then its info will contain a tag, line, column, horiz but no width nor height nor depth
  266. */
  267. /* The sheet is a first level node.
  268. * It has no parent (the parent is the scanner itself)
  269. * Its sibling points to another sheet.
  270. * Its child points to its first child, in general a box.
  271. * A sheet node contains only one synctex information: the page.
  272. * This is the 1 based page index as given by TeX.
  273. */
  274. /* The next macros are used to access the node info
  275. * SYNCTEX_INFO(node) points to the first synctex integer or pointer data of node
  276. * SYNCTEX_INFO(node)[index] is the information at index
  277. * for example, the page of a sheet is stored in SYNCTEX_INFO(sheet)[SYNCTEX_PAGE_IDX]
  278. */
  279. # define SYNCTEX_INFO(NODE) ((*((((NODE)->class))->info))(NODE))
  280. # define SYNCTEX_PAGE_IDX 0
  281. # define SYNCTEX_PAGE(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_PAGE_IDX].INT
  282. /* This macro defines implementation offsets
  283. * It is only used for pointer values
  284. */
  285. # define SYNCTEX_MAKE_GET(SYNCTEX_GETTER,OFFSET)\
  286. synctex_node_t * SYNCTEX_GETTER (synctex_node_t node);\
  287. synctex_node_t * SYNCTEX_GETTER (synctex_node_t node) {\
  288. return node?(synctex_node_t *)((&((node)->implementation))+OFFSET):NULL;\
  289. }
  290. SYNCTEX_MAKE_GET(_synctex_implementation_0,0)
  291. SYNCTEX_MAKE_GET(_synctex_implementation_1,1)
  292. SYNCTEX_MAKE_GET(_synctex_implementation_2,2)
  293. SYNCTEX_MAKE_GET(_synctex_implementation_3,3)
  294. SYNCTEX_MAKE_GET(_synctex_implementation_4,4)
  295. SYNCTEX_MAKE_GET(_synctex_implementation_5,5)
  296. typedef struct {
  297. synctex_class_t class;
  298. synctex_info_t implementation[3+SYNCTEX_PAGE_IDX+1];/* child, sibling, next box,
  299. * SYNCTEX_PAGE_IDX */
  300. } synctex_sheet_t;
  301. synctex_node_t _synctex_new_sheet(synctex_scanner_t scanner);
  302. void _synctex_display_sheet(synctex_node_t sheet);
  303. void _synctex_log_sheet(synctex_node_t sheet);
  304. static _synctex_class_t synctex_class_sheet = {
  305. NULL, /* No scanner yet */
  306. synctex_node_type_sheet, /* Node type */
  307. &_synctex_new_sheet, /* creator */
  308. &_synctex_free_node, /* destructor */
  309. &_synctex_log_sheet, /* log */
  310. &_synctex_display_sheet, /* display */
  311. NULL, /* No parent */
  312. &_synctex_implementation_0, /* child */
  313. &_synctex_implementation_1, /* sibling */
  314. NULL, /* No friend */
  315. &_synctex_implementation_2, /* Next box */
  316. (_synctex_info_getter_t)&_synctex_implementation_3 /* info */
  317. };
  318. /* sheet node creator */
  319. synctex_node_t _synctex_new_sheet(synctex_scanner_t scanner) {
  320. synctex_node_t node = _synctex_malloc(sizeof(synctex_sheet_t));
  321. if (node) {
  322. node->class = scanner?scanner->class+synctex_node_type_sheet:(synctex_class_t)&synctex_class_sheet;
  323. }
  324. return node;
  325. }
  326. /* A box node contains navigation and synctex information
  327. * There are different kind of boxes.
  328. * Only horizontal boxes are treated differently because of their visible size.
  329. */
  330. # define SYNCTEX_TAG_IDX 0
  331. # define SYNCTEX_LINE_IDX (SYNCTEX_TAG_IDX+1)
  332. # define SYNCTEX_COLUMN_IDX (SYNCTEX_LINE_IDX+1)
  333. # define SYNCTEX_HORIZ_IDX (SYNCTEX_COLUMN_IDX+1)
  334. # define SYNCTEX_VERT_IDX (SYNCTEX_HORIZ_IDX+1)
  335. # define SYNCTEX_WIDTH_IDX (SYNCTEX_VERT_IDX+1)
  336. # define SYNCTEX_HEIGHT_IDX (SYNCTEX_WIDTH_IDX+1)
  337. # define SYNCTEX_DEPTH_IDX (SYNCTEX_HEIGHT_IDX+1)
  338. /* the corresponding info accessors */
  339. # define SYNCTEX_TAG(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_TAG_IDX].INT
  340. # define SYNCTEX_LINE(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_LINE_IDX].INT
  341. # define SYNCTEX_COLUMN(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_COLUMN_IDX].INT
  342. # define SYNCTEX_HORIZ(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_HORIZ_IDX].INT
  343. # define SYNCTEX_VERT(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_VERT_IDX].INT
  344. # define SYNCTEX_WIDTH(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_WIDTH_IDX].INT
  345. # define SYNCTEX_HEIGHT(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_HEIGHT_IDX].INT
  346. # define SYNCTEX_DEPTH(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_DEPTH_IDX].INT
  347. # define SYNCTEX_ABS_WIDTH(NODE) ((SYNCTEX_WIDTH(NODE)>0?SYNCTEX_WIDTH(NODE):-SYNCTEX_WIDTH(NODE)))
  348. # define SYNCTEX_ABS_HEIGHT(NODE) ((SYNCTEX_HEIGHT(NODE)>0?SYNCTEX_HEIGHT(NODE):-SYNCTEX_HEIGHT(NODE)))
  349. # define SYNCTEX_ABS_DEPTH(NODE) ((SYNCTEX_DEPTH(NODE)>0?SYNCTEX_DEPTH(NODE):-SYNCTEX_DEPTH(NODE)))
  350. typedef struct {
  351. synctex_class_t class;
  352. synctex_info_t implementation[5+SYNCTEX_DEPTH_IDX+1]; /* parent,child,sibling,friend,next box,
  353. * SYNCTEX_TAG,SYNCTEX_LINE,SYNCTEX_COLUMN,
  354. * SYNCTEX_HORIZ,SYNCTEX_VERT,SYNCTEX_WIDTH,SYNCTEX_HEIGHT,SYNCTEX_DEPTH */
  355. } synctex_vert_box_node_t;
  356. synctex_node_t _synctex_new_vbox(synctex_scanner_t scanner);
  357. void _synctex_log_box(synctex_node_t sheet);
  358. void _synctex_display_vbox(synctex_node_t node);
  359. /* These are static class objects, each scanner will make a copy of them and setup the scanner field.
  360. */
  361. static _synctex_class_t synctex_class_vbox = {
  362. NULL, /* No scanner yet */
  363. synctex_node_type_vbox, /* Node type */
  364. &_synctex_new_vbox, /* creator */
  365. &_synctex_free_node, /* destructor */
  366. &_synctex_log_box, /* log */
  367. &_synctex_display_vbox, /* display */
  368. &_synctex_implementation_0, /* parent */
  369. &_synctex_implementation_1, /* child */
  370. &_synctex_implementation_2, /* sibling */
  371. &_synctex_implementation_3, /* friend */
  372. &_synctex_implementation_4, /* next box */
  373. (_synctex_info_getter_t)&_synctex_implementation_5
  374. };
  375. /* vertical box node creator */
  376. synctex_node_t _synctex_new_vbox(synctex_scanner_t scanner) {
  377. synctex_node_t node = _synctex_malloc(sizeof(synctex_vert_box_node_t));
  378. if (node) {
  379. node->class = scanner?scanner->class+synctex_node_type_vbox:(synctex_class_t)&synctex_class_vbox;
  380. }
  381. return node;
  382. }
  383. # define SYNCTEX_HORIZ_V_IDX (SYNCTEX_DEPTH_IDX+1)
  384. # define SYNCTEX_VERT_V_IDX (SYNCTEX_HORIZ_V_IDX+1)
  385. # define SYNCTEX_WIDTH_V_IDX (SYNCTEX_VERT_V_IDX+1)
  386. # define SYNCTEX_HEIGHT_V_IDX (SYNCTEX_WIDTH_V_IDX+1)
  387. # define SYNCTEX_DEPTH_V_IDX (SYNCTEX_HEIGHT_V_IDX+1)
  388. /* the corresponding info accessors */
  389. # define SYNCTEX_HORIZ_V(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_HORIZ_V_IDX].INT
  390. # define SYNCTEX_VERT_V(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_VERT_V_IDX].INT
  391. # define SYNCTEX_WIDTH_V(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_WIDTH_V_IDX].INT
  392. # define SYNCTEX_HEIGHT_V(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_HEIGHT_V_IDX].INT
  393. # define SYNCTEX_DEPTH_V(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_DEPTH_V_IDX].INT
  394. # define SYNCTEX_ABS_WIDTH_V(NODE) ((SYNCTEX_WIDTH_V(NODE)>0?SYNCTEX_WIDTH_V(NODE):-SYNCTEX_WIDTH_V(NODE)))
  395. # define SYNCTEX_ABS_HEIGHT_V(NODE) ((SYNCTEX_HEIGHT_V(NODE)>0?SYNCTEX_HEIGHT_V(NODE):-SYNCTEX_HEIGHT_V(NODE)))
  396. # define SYNCTEX_ABS_DEPTH_V(NODE) ((SYNCTEX_DEPTH_V(NODE)>0?SYNCTEX_DEPTH_V(NODE):-SYNCTEX_DEPTH_V(NODE)))
  397. /* Horizontal boxes must contain visible size, because 0 width does not mean emptiness */
  398. typedef struct {
  399. synctex_class_t class;
  400. synctex_info_t implementation[5+SYNCTEX_DEPTH_V_IDX+1]; /*parent,child,sibling,friend,next box,
  401. * SYNCTEX_TAG,SYNCTEX_LINE,SYNCTEX_COLUMN,
  402. * SYNCTEX_HORIZ,SYNCTEX_VERT,SYNCTEX_WIDTH,SYNCTEX_HEIGHT,SYNCTEX_DEPTH,
  403. * SYNCTEX_HORIZ_V,SYNCTEX_VERT_V,SYNCTEX_WIDTH_V,SYNCTEX_HEIGHT_V,SYNCTEX_DEPTH_V*/
  404. } synctex_horiz_box_node_t;
  405. synctex_node_t _synctex_new_hbox(synctex_scanner_t scanner);
  406. void _synctex_display_hbox(synctex_node_t node);
  407. void _synctex_log_horiz_box(synctex_node_t sheet);
  408. static _synctex_class_t synctex_class_hbox = {
  409. NULL, /* No scanner yet */
  410. synctex_node_type_hbox, /* Node type */
  411. &_synctex_new_hbox, /* creator */
  412. &_synctex_free_node, /* destructor */
  413. &_synctex_log_horiz_box, /* log */
  414. &_synctex_display_hbox, /* display */
  415. &_synctex_implementation_0, /* parent */
  416. &_synctex_implementation_1, /* child */
  417. &_synctex_implementation_2, /* sibling */
  418. &_synctex_implementation_3, /* friend */
  419. &_synctex_implementation_4, /* next box */
  420. (_synctex_info_getter_t)&_synctex_implementation_5
  421. };
  422. /* horizontal box node creator */
  423. synctex_node_t _synctex_new_hbox(synctex_scanner_t scanner) {
  424. synctex_node_t node = _synctex_malloc(sizeof(synctex_horiz_box_node_t));
  425. if (node) {
  426. node->class = scanner?scanner->class+synctex_node_type_hbox:(synctex_class_t)&synctex_class_hbox;
  427. }
  428. return node;
  429. }
  430. /* This void box node implementation is either horizontal or vertical
  431. * It does not contain a child field.
  432. */
  433. typedef struct {
  434. synctex_class_t class;
  435. synctex_info_t implementation[3+SYNCTEX_DEPTH_IDX+1]; /* parent,sibling,friend,
  436. * SYNCTEX_TAG,SYNCTEX_LINE,SYNCTEX_COLUMN,
  437. * SYNCTEX_HORIZ,SYNCTEX_VERT,SYNCTEX_WIDTH,SYNCTEX_HEIGHT,SYNCTEX_DEPTH*/
  438. } synctex_void_box_node_t;
  439. synctex_node_t _synctex_new_void_vbox(synctex_scanner_t scanner);
  440. void _synctex_log_void_box(synctex_node_t sheet);
  441. void _synctex_display_void_vbox(synctex_node_t node);
  442. static _synctex_class_t synctex_class_void_vbox = {
  443. NULL, /* No scanner yet */
  444. synctex_node_type_void_vbox,/* Node type */
  445. &_synctex_new_void_vbox, /* creator */
  446. &_synctex_free_node, /* destructor */
  447. &_synctex_log_void_box, /* log */
  448. &_synctex_display_void_vbox,/* display */
  449. &_synctex_implementation_0, /* parent */
  450. NULL, /* No child */
  451. &_synctex_implementation_1, /* sibling */
  452. &_synctex_implementation_2, /* friend */
  453. NULL, /* No next box */
  454. (_synctex_info_getter_t)&_synctex_implementation_3
  455. };
  456. /* vertical void box node creator */
  457. synctex_node_t _synctex_new_void_vbox(synctex_scanner_t scanner) {
  458. synctex_node_t node = _synctex_malloc(sizeof(synctex_void_box_node_t));
  459. if (node) {
  460. node->class = scanner?scanner->class+synctex_node_type_void_vbox:(synctex_class_t)&synctex_class_void_vbox;
  461. }
  462. return node;
  463. }
  464. synctex_node_t _synctex_new_void_hbox(synctex_scanner_t scanner);
  465. void _synctex_display_void_hbox(synctex_node_t node);
  466. static _synctex_class_t synctex_class_void_hbox = {
  467. NULL, /* No scanner yet */
  468. synctex_node_type_void_hbox,/* Node type */
  469. &_synctex_new_void_hbox, /* creator */
  470. &_synctex_free_node, /* destructor */
  471. &_synctex_log_void_box, /* log */
  472. &_synctex_display_void_hbox,/* display */
  473. &_synctex_implementation_0, /* parent */
  474. NULL, /* No child */
  475. &_synctex_implementation_1, /* sibling */
  476. &_synctex_implementation_2, /* friend */
  477. NULL, /* No next box */
  478. (_synctex_info_getter_t)&_synctex_implementation_3
  479. };
  480. /* horizontal void box node creator */
  481. synctex_node_t _synctex_new_void_hbox(synctex_scanner_t scanner) {
  482. synctex_node_t node = _synctex_malloc(sizeof(synctex_void_box_node_t));
  483. if (node) {
  484. node->class = scanner?scanner->class+synctex_node_type_void_hbox:(synctex_class_t)&synctex_class_void_hbox;
  485. }
  486. return node;
  487. }
  488. /* The medium nodes correspond to kern, glue, penalty and math nodes. */
  489. typedef struct {
  490. synctex_class_t class;
  491. synctex_info_t implementation[3+SYNCTEX_WIDTH_IDX+1]; /* parent,sibling,friend,
  492. * SYNCTEX_TAG,SYNCTEX_LINE,SYNCTEX_COLUMN,
  493. * SYNCTEX_HORIZ,SYNCTEX_VERT,SYNCTEX_WIDTH */
  494. } synctex_medium_node_t;
  495. #define SYNCTEX_IS_BOX(NODE)\
  496. ((NODE->class->type == synctex_node_type_vbox)\
  497. || (NODE->class->type == synctex_node_type_void_vbox)\
  498. || (NODE->class->type == synctex_node_type_hbox)\
  499. || (NODE->class->type == synctex_node_type_void_hbox))
  500. #define SYNCTEX_HAS_CHILDREN(NODE) (NODE && SYNCTEX_CHILD(NODE))
  501. void _synctex_log_medium_node(synctex_node_t node);
  502. /* math node creator */
  503. synctex_node_t _synctex_new_math(synctex_scanner_t scanner);
  504. void _synctex_display_math(synctex_node_t node);
  505. static _synctex_class_t synctex_class_math = {
  506. NULL, /* No scanner yet */
  507. synctex_node_type_math, /* Node type */
  508. &_synctex_new_math, /* creator */
  509. &_synctex_free_leaf, /* destructor */
  510. &_synctex_log_medium_node, /* log */
  511. &_synctex_display_math, /* display */
  512. &_synctex_implementation_0, /* parent */
  513. NULL, /* No child */
  514. &_synctex_implementation_1, /* sibling */
  515. &_synctex_implementation_2, /* friend */
  516. NULL, /* No next box */
  517. (_synctex_info_getter_t)&_synctex_implementation_3
  518. };
  519. synctex_node_t _synctex_new_math(synctex_scanner_t scanner) {
  520. synctex_node_t node = _synctex_malloc(sizeof(synctex_medium_node_t));
  521. if (node) {
  522. node->class = scanner?scanner->class+synctex_node_type_math:(synctex_class_t)&synctex_class_math;
  523. }
  524. return node;
  525. }
  526. /* kern node creator */
  527. synctex_node_t _synctex_new_kern(synctex_scanner_t scanner);
  528. void _synctex_display_kern(synctex_node_t node);
  529. static _synctex_class_t synctex_class_kern = {
  530. NULL, /* No scanner yet */
  531. synctex_node_type_kern, /* Node type */
  532. &_synctex_new_kern, /* creator */
  533. &_synctex_free_leaf, /* destructor */
  534. &_synctex_log_medium_node, /* log */
  535. &_synctex_display_kern, /* display */
  536. &_synctex_implementation_0, /* parent */
  537. NULL, /* No child */
  538. &_synctex_implementation_1, /* sibling */
  539. &_synctex_implementation_2, /* friend */
  540. NULL, /* No next box */
  541. (_synctex_info_getter_t)&_synctex_implementation_3
  542. };
  543. synctex_node_t _synctex_new_kern(synctex_scanner_t scanner) {
  544. synctex_node_t node = _synctex_malloc(sizeof(synctex_medium_node_t));
  545. if (node) {
  546. node->class = scanner?scanner->class+synctex_node_type_kern:(synctex_class_t)&synctex_class_kern;
  547. }
  548. return node;
  549. }
  550. /* The small nodes correspond to glue and boundary nodes. */
  551. typedef struct {
  552. synctex_class_t class;
  553. synctex_info_t implementation[3+SYNCTEX_VERT_IDX+1]; /* parent,sibling,friend,
  554. * SYNCTEX_TAG,SYNCTEX_LINE,SYNCTEX_COLUMN,
  555. * SYNCTEX_HORIZ,SYNCTEX_VERT */
  556. } synctex_small_node_t;
  557. void _synctex_log_small_node(synctex_node_t node);
  558. /* glue node creator */
  559. synctex_node_t _synctex_new_glue(synctex_scanner_t scanner);
  560. void _synctex_display_glue(synctex_node_t node);
  561. static _synctex_class_t synctex_class_glue = {
  562. NULL, /* No scanner yet */
  563. synctex_node_type_glue, /* Node type */
  564. &_synctex_new_glue, /* creator */
  565. &_synctex_free_leaf, /* destructor */
  566. &_synctex_log_medium_node, /* log */
  567. &_synctex_display_glue, /* display */
  568. &_synctex_implementation_0, /* parent */
  569. NULL, /* No child */
  570. &_synctex_implementation_1, /* sibling */
  571. &_synctex_implementation_2, /* friend */
  572. NULL, /* No next box */
  573. (_synctex_info_getter_t)&_synctex_implementation_3
  574. };
  575. synctex_node_t _synctex_new_glue(synctex_scanner_t scanner) {
  576. synctex_node_t node = _synctex_malloc(sizeof(synctex_medium_node_t));
  577. if (node) {
  578. node->class = scanner?scanner->class+synctex_node_type_glue:(synctex_class_t)&synctex_class_glue;
  579. }
  580. return node;
  581. }
  582. /* boundary node creator */
  583. synctex_node_t _synctex_new_boundary(synctex_scanner_t scanner);
  584. void _synctex_display_boundary(synctex_node_t node);
  585. static _synctex_class_t synctex_class_boundary = {
  586. NULL, /* No scanner yet */
  587. synctex_node_type_boundary, /* Node type */
  588. &_synctex_new_boundary, /* creator */
  589. &_synctex_free_leaf, /* destructor */
  590. &_synctex_log_small_node, /* log */
  591. &_synctex_display_boundary, /* display */
  592. &_synctex_implementation_0, /* parent */
  593. NULL, /* No child */
  594. &_synctex_implementation_1, /* sibling */
  595. &_synctex_implementation_2, /* friend */
  596. NULL, /* No next box */
  597. (_synctex_info_getter_t)&_synctex_implementation_3
  598. };
  599. synctex_node_t _synctex_new_boundary(synctex_scanner_t scanner) {
  600. synctex_node_t node = _synctex_malloc(sizeof(synctex_small_node_t));
  601. if (node) {
  602. node->class = scanner?scanner->class+synctex_node_type_boundary:(synctex_class_t)&synctex_class_boundary;
  603. }
  604. return node;
  605. }
  606. # define SYNCTEX_NAME_IDX (SYNCTEX_TAG_IDX+1)
  607. # define SYNCTEX_NAME(NODE) SYNCTEX_INFO(NODE)[SYNCTEX_NAME_IDX].PTR
  608. /* Input nodes only know about their sibling, which is another input node.
  609. * The synctex information is the SYNCTEX_TAG and SYNCTEX_NAME*/
  610. typedef struct {
  611. synctex_class_t class;
  612. synctex_info_t implementation[1+SYNCTEX_NAME_IDX+1]; /* sibling,
  613. * SYNCTEX_TAG,SYNCTEX_NAME */
  614. } synctex_input_t;
  615. synctex_node_t _synctex_new_input(synctex_scanner_t scanner);
  616. void _synctex_free_input(synctex_node_t node);
  617. void _synctex_display_input(synctex_node_t node);
  618. void _synctex_log_input(synctex_node_t sheet);
  619. static _synctex_class_t synctex_class_input = {
  620. NULL, /* No scanner yet */
  621. synctex_node_type_input, /* Node type */
  622. &_synctex_new_input, /* creator */
  623. &_synctex_free_input, /* destructor */
  624. &_synctex_log_input, /* log */
  625. &_synctex_display_input, /* display */
  626. NULL, /* No parent */
  627. NULL, /* No child */
  628. &_synctex_implementation_0, /* sibling */
  629. NULL, /* No friend */
  630. NULL, /* No next box */
  631. (_synctex_info_getter_t)&_synctex_implementation_1
  632. };
  633. synctex_node_t _synctex_new_input(synctex_scanner_t scanner) {
  634. synctex_node_t node = _synctex_malloc(sizeof(synctex_input_t));
  635. if (node) {
  636. node->class = scanner?scanner->class+synctex_node_type_input:(synctex_class_t)&synctex_class_input;
  637. }
  638. return node;
  639. }
  640. void _synctex_free_input(synctex_node_t node){
  641. if (node) {
  642. SYNCTEX_FREE(SYNCTEX_SIBLING(node));
  643. free(SYNCTEX_NAME(node));
  644. free(node);
  645. }
  646. }
  647. # ifdef SYNCTEX_NOTHING
  648. # pragma mark -
  649. # pragma mark Navigation
  650. # endif
  651. synctex_node_t synctex_node_parent(synctex_node_t node)
  652. {
  653. return SYNCTEX_PARENT(node);
  654. }
  655. synctex_node_t synctex_node_sheet(synctex_node_t node)
  656. {
  657. while(node && node->class->type != synctex_node_type_sheet) {
  658. node = SYNCTEX_PARENT(node);
  659. }
  660. /* exit the while loop either when node is NULL or node is a sheet */
  661. return node;
  662. }
  663. synctex_node_t synctex_node_child(synctex_node_t node)
  664. {
  665. return SYNCTEX_CHILD(node);
  666. }
  667. synctex_node_t synctex_node_sibling(synctex_node_t node)
  668. {
  669. return SYNCTEX_SIBLING(node);
  670. }
  671. synctex_node_t synctex_node_next(synctex_node_t node) {
  672. if (SYNCTEX_CHILD(node)) {
  673. return SYNCTEX_CHILD(node);
  674. }
  675. sibling:
  676. if (SYNCTEX_SIBLING(node)) {
  677. return SYNCTEX_SIBLING(node);
  678. }
  679. if ((node = SYNCTEX_PARENT(node))) {
  680. if (node->class->type == synctex_node_type_sheet) {/* EXC_BAD_ACCESS? */
  681. return NULL;
  682. }
  683. goto sibling;
  684. }
  685. return NULL;
  686. }
  687. # ifdef SYNCTEX_NOTHING
  688. # pragma mark -
  689. # pragma mark CLASS
  690. # endif
  691. /* Public node accessor: the type */
  692. synctex_node_type_t synctex_node_type(synctex_node_t node) {
  693. if (node) {
  694. return (((node)->class))->type;
  695. }
  696. return synctex_node_type_error;
  697. }
  698. /* Public node accessor: the human readable type */
  699. const char * synctex_node_isa(synctex_node_t node) {
  700. static const char * isa[synctex_node_number_of_types] =
  701. {"Not a node","input","sheet","vbox","void vbox","hbox","void hbox","kern","glue","math","boundary"};
  702. return isa[synctex_node_type(node)];
  703. }
  704. # ifdef SYNCTEX_NOTHING
  705. # pragma mark -
  706. # pragma mark SYNCTEX_LOG
  707. # endif
  708. # define SYNCTEX_LOG(NODE) SYNCTEX_MSG_SEND(NODE,log)
  709. /* Public node logger */
  710. void synctex_node_log(synctex_node_t node) {
  711. SYNCTEX_LOG(node);
  712. }
  713. # define SYNCTEX_DISPLAY(NODE) SYNCTEX_MSG_SEND(NODE,display)
  714. void synctex_node_display(synctex_node_t node) {
  715. SYNCTEX_DISPLAY(node);
  716. }
  717. void _synctex_display_input(synctex_node_t node) {
  718. printf("....Input:%i:%s\n",
  719. SYNCTEX_TAG(node),
  720. SYNCTEX_NAME(node));
  721. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  722. }
  723. void _synctex_log_sheet(synctex_node_t sheet) {
  724. if (sheet) {
  725. printf("%s:%i\n",synctex_node_isa(sheet),SYNCTEX_PAGE(sheet));
  726. printf("SELF:%p",(void *)sheet);
  727. printf(" SYNCTEX_PARENT:%p",(void *)SYNCTEX_PARENT(sheet));
  728. printf(" SYNCTEX_CHILD:%p",(void *)SYNCTEX_CHILD(sheet));
  729. printf(" SYNCTEX_SIBLING:%p",(void *)SYNCTEX_SIBLING(sheet));
  730. printf(" SYNCTEX_FRIEND:%p\n",(void *)SYNCTEX_FRIEND(sheet));
  731. }
  732. }
  733. void _synctex_log_small_node(synctex_node_t node) {
  734. printf("%s:%i,%i:%i,%i\n",
  735. synctex_node_isa(node),
  736. SYNCTEX_TAG(node),
  737. SYNCTEX_LINE(node),
  738. SYNCTEX_HORIZ(node),
  739. SYNCTEX_VERT(node));
  740. printf("SELF:%p",(void *)node);
  741. printf(" SYNCTEX_PARENT:%p",(void *)SYNCTEX_PARENT(node));
  742. printf(" SYNCTEX_CHILD:%p",(void *)SYNCTEX_CHILD(node));
  743. printf(" SYNCTEX_SIBLING:%p",(void *)SYNCTEX_SIBLING(node));
  744. printf(" SYNCTEX_FRIEND:%p\n",(void *)SYNCTEX_FRIEND(node));
  745. }
  746. void _synctex_log_medium_node(synctex_node_t node) {
  747. printf("%s:%i,%i:%i,%i:%i\n",
  748. synctex_node_isa(node),
  749. SYNCTEX_TAG(node),
  750. SYNCTEX_LINE(node),
  751. SYNCTEX_HORIZ(node),
  752. SYNCTEX_VERT(node),
  753. SYNCTEX_WIDTH(node));
  754. printf("SELF:%p",(void *)node);
  755. printf(" SYNCTEX_PARENT:%p",(void *)SYNCTEX_PARENT(node));
  756. printf(" SYNCTEX_CHILD:%p",(void *)SYNCTEX_CHILD(node));
  757. printf(" SYNCTEX_SIBLING:%p",(void *)SYNCTEX_SIBLING(node));
  758. printf(" SYNCTEX_FRIEND:%p\n",(void *)SYNCTEX_FRIEND(node));
  759. }
  760. void _synctex_log_void_box(synctex_node_t node) {
  761. printf("%s",synctex_node_isa(node));
  762. printf(":%i",SYNCTEX_TAG(node));
  763. printf(",%i",SYNCTEX_LINE(node));
  764. printf(",%i",0);
  765. printf(":%i",SYNCTEX_HORIZ(node));
  766. printf(",%i",SYNCTEX_VERT(node));
  767. printf(":%i",SYNCTEX_WIDTH(node));
  768. printf(",%i",SYNCTEX_HEIGHT(node));
  769. printf(",%i",SYNCTEX_DEPTH(node));
  770. printf("\nSELF:%p",(void *)node);
  771. printf(" SYNCTEX_PARENT:%p",(void *)SYNCTEX_PARENT(node));
  772. printf(" SYNCTEX_CHILD:%p",(void *)SYNCTEX_CHILD(node));
  773. printf(" SYNCTEX_SIBLING:%p",(void *)SYNCTEX_SIBLING(node));
  774. printf(" SYNCTEX_FRIEND:%p\n",(void *)SYNCTEX_FRIEND(node));
  775. }
  776. void _synctex_log_box(synctex_node_t node) {
  777. printf("%s",synctex_node_isa(node));
  778. printf(":%i",SYNCTEX_TAG(node));
  779. printf(",%i",SYNCTEX_LINE(node));
  780. printf(",%i",0);
  781. printf(":%i",SYNCTEX_HORIZ(node));
  782. printf(",%i",SYNCTEX_VERT(node));
  783. printf(":%i",SYNCTEX_WIDTH(node));
  784. printf(",%i",SYNCTEX_HEIGHT(node));
  785. printf(",%i",SYNCTEX_DEPTH(node));
  786. printf("\nSELF:%p",(void *)node);
  787. printf(" SYNCTEX_PARENT:%p",(void *)SYNCTEX_PARENT(node));
  788. printf(" SYNCTEX_CHILD:%p",(void *)SYNCTEX_CHILD(node));
  789. printf(" SYNCTEX_SIBLING:%p",(void *)SYNCTEX_SIBLING(node));
  790. printf(" SYNCTEX_FRIEND:%p\n",(void *)SYNCTEX_FRIEND(node));
  791. }
  792. void _synctex_log_horiz_box(synctex_node_t node) {
  793. printf("%s",synctex_node_isa(node));
  794. printf(":%i",SYNCTEX_TAG(node));
  795. printf(",%i",SYNCTEX_LINE(node));
  796. printf(",%i",0);
  797. printf(":%i",SYNCTEX_HORIZ(node));
  798. printf(",%i",SYNCTEX_VERT(node));
  799. printf(":%i",SYNCTEX_WIDTH(node));
  800. printf(",%i",SYNCTEX_HEIGHT(node));
  801. printf(",%i",SYNCTEX_DEPTH(node));
  802. printf("/%i",SYNCTEX_HORIZ_V(node));
  803. printf(",%i",SYNCTEX_VERT_V(node));
  804. printf(":%i",SYNCTEX_WIDTH_V(node));
  805. printf(",%i",SYNCTEX_HEIGHT_V(node));
  806. printf(",%i",SYNCTEX_DEPTH_V(node));
  807. printf("\nSELF:%p",(void *)node);
  808. printf(" SYNCTEX_PARENT:%p",(void *)SYNCTEX_PARENT(node));
  809. printf(" SYNCTEX_CHILD:%p",(void *)SYNCTEX_CHILD(node));
  810. printf(" SYNCTEX_SIBLING:%p",(void *)SYNCTEX_SIBLING(node));
  811. printf(" SYNCTEX_FRIEND:%p\n",(void *)SYNCTEX_FRIEND(node));
  812. }
  813. void _synctex_log_input(synctex_node_t node) {
  814. printf("%s",synctex_node_isa(node));
  815. printf(":%i",SYNCTEX_TAG(node));
  816. printf(",%s",SYNCTEX_NAME(node));
  817. printf(" SYNCTEX_SIBLING:%p",(void *)SYNCTEX_SIBLING(node));
  818. }
  819. void _synctex_display_sheet(synctex_node_t sheet) {
  820. if (sheet) {
  821. printf("....{%i\n",SYNCTEX_PAGE(sheet));
  822. SYNCTEX_DISPLAY(SYNCTEX_CHILD(sheet));
  823. printf("....}\n");
  824. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(sheet));
  825. }
  826. }
  827. void _synctex_display_vbox(synctex_node_t node) {
  828. printf("....[%i,%i:%i,%i:%i,%i,%i\n",
  829. SYNCTEX_TAG(node),
  830. SYNCTEX_LINE(node),
  831. SYNCTEX_HORIZ(node),
  832. SYNCTEX_VERT(node),
  833. SYNCTEX_WIDTH(node),
  834. SYNCTEX_HEIGHT(node),
  835. SYNCTEX_DEPTH(node));
  836. SYNCTEX_DISPLAY(SYNCTEX_CHILD(node));
  837. printf("....]\n");
  838. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  839. }
  840. void _synctex_display_hbox(synctex_node_t node) {
  841. printf("....(%i,%i:%i,%i:%i,%i,%i\n",
  842. SYNCTEX_TAG(node),
  843. SYNCTEX_LINE(node),
  844. SYNCTEX_HORIZ(node),
  845. SYNCTEX_VERT(node),
  846. SYNCTEX_WIDTH(node),
  847. SYNCTEX_HEIGHT(node),
  848. SYNCTEX_DEPTH(node));
  849. SYNCTEX_DISPLAY(SYNCTEX_CHILD(node));
  850. printf("....)\n");
  851. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  852. }
  853. void _synctex_display_void_vbox(synctex_node_t node) {
  854. printf("....v%i,%i;%i,%i:%i,%i,%i\n",
  855. SYNCTEX_TAG(node),
  856. SYNCTEX_LINE(node),
  857. SYNCTEX_HORIZ(node),
  858. SYNCTEX_VERT(node),
  859. SYNCTEX_WIDTH(node),
  860. SYNCTEX_HEIGHT(node),
  861. SYNCTEX_DEPTH(node));
  862. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  863. }
  864. void _synctex_display_void_hbox(synctex_node_t node) {
  865. printf("....h%i,%i:%i,%i:%i,%i,%i\n",
  866. SYNCTEX_TAG(node),
  867. SYNCTEX_LINE(node),
  868. SYNCTEX_HORIZ(node),
  869. SYNCTEX_VERT(node),
  870. SYNCTEX_WIDTH(node),
  871. SYNCTEX_HEIGHT(node),
  872. SYNCTEX_DEPTH(node));
  873. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  874. }
  875. void _synctex_display_glue(synctex_node_t node) {
  876. printf("....glue:%i,%i:%i,%i\n",
  877. SYNCTEX_TAG(node),
  878. SYNCTEX_LINE(node),
  879. SYNCTEX_HORIZ(node),
  880. SYNCTEX_VERT(node));
  881. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  882. }
  883. void _synctex_display_math(synctex_node_t node) {
  884. printf("....math:%i,%i:%i,%i\n",
  885. SYNCTEX_TAG(node),
  886. SYNCTEX_LINE(node),
  887. SYNCTEX_HORIZ(node),
  888. SYNCTEX_VERT(node));
  889. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  890. }
  891. void _synctex_display_kern(synctex_node_t node) {
  892. printf("....kern:%i,%i:%i,%i:%i\n",
  893. SYNCTEX_TAG(node),
  894. SYNCTEX_LINE(node),
  895. SYNCTEX_HORIZ(node),
  896. SYNCTEX_VERT(node),
  897. SYNCTEX_WIDTH(node));
  898. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  899. }
  900. void _synctex_display_boundary(synctex_node_t node) {
  901. printf("....boundary:%i,%i:%i,%i\n",
  902. SYNCTEX_TAG(node),
  903. SYNCTEX_LINE(node),
  904. SYNCTEX_HORIZ(node),
  905. SYNCTEX_VERT(node));
  906. SYNCTEX_DISPLAY(SYNCTEX_SIBLING(node));
  907. }
  908. # ifdef SYNCTEX_NOTHING
  909. # pragma mark -
  910. # pragma mark SCANNER
  911. # endif
  912. /* Here are gathered all the possible status that the next scanning functions will return.
  913. * All these functions return a status, and pass their result through pointers.
  914. * Negative values correspond to errors.
  915. * The management of the buffer is causing some significant overhead.
  916. * Every function that may access the buffer returns a status related to the buffer and file state.
  917. * status >= SYNCTEX_STATUS_OK means the function worked as expected
  918. * status < SYNCTEX_STATUS_OK means the function did not work as expected
  919. * status == SYNCTEX_STATUS_NOT_OK means the function did not work as expected but there is still some material to parse.
  920. * status == SYNCTEX_STATUS_EOF means the function did not work as expected and there is no more material.
  921. * status<SYNCTEX_STATUS_EOF means an error
  922. */
  923. typedef int synctex_status_t;
  924. /* When the end of the synctex file has been reached: */
  925. # define SYNCTEX_STATUS_EOF 0
  926. /* When the function could not return the value it was asked for: */
  927. # define SYNCTEX_STATUS_NOT_OK (SYNCTEX_STATUS_EOF+1)
  928. /* When the function returns the value it was asked for: */
  929. # define SYNCTEX_STATUS_OK (SYNCTEX_STATUS_NOT_OK+1)
  930. /* Generic error: */
  931. # define SYNCTEX_STATUS_ERROR -1
  932. /* Parameter error: */
  933. # define SYNCTEX_STATUS_BAD_ARGUMENT -2
  934. # define SYNCTEX_FILE (scanner->file)
  935. /* Actually, the minimum buffer size is driven by integer and float parsing.
  936. * ±0.123456789e123
  937. */
  938. # define SYNCTEX_BUFFER_MIN_SIZE 16
  939. # define SYNCTEX_BUFFER_SIZE 32768
  940. # ifdef SYNCTEX_NOTHING
  941. # pragma mark -
  942. # pragma mark Prototypes
  943. # endif
  944. void _synctex_log_void_box(synctex_node_t node);
  945. void _synctex_log_box(synctex_node_t node);
  946. void _synctex_log_horiz_box(synctex_node_t node);
  947. void _synctex_log_input(synctex_node_t node);
  948. synctex_status_t _synctex_buffer_get_available_size(synctex_scanner_t scanner, size_t * size_ptr);
  949. synctex_status_t _synctex_next_line(synctex_scanner_t scanner);
  950. synctex_status_t _synctex_match_string(synctex_scanner_t scanner, const char * the_string);
  951. synctex_status_t _synctex_decode_int(synctex_scanner_t scanner, int* value_ref);
  952. synctex_status_t _synctex_decode_string(synctex_scanner_t scanner, char ** value_ref);
  953. synctex_status_t _synctex_scan_input(synctex_scanner_t scanner);
  954. synctex_status_t _synctex_scan_preamble(synctex_scanner_t scanner);
  955. synctex_status_t _synctex_scan_float_and_dimension(synctex_scanner_t scanner, float * value_ref);
  956. synctex_status_t _synctex_scan_post_scriptum(synctex_scanner_t scanner);
  957. int _synctex_scan_postamble(synctex_scanner_t scanner);
  958. synctex_status_t _synctex_setup_visible_box(synctex_node_t box);
  959. synctex_status_t _synctex_horiz_box_setup_visible(synctex_node_t node,int h, int v);
  960. synctex_status_t _synctex_scan_sheet(synctex_scanner_t scanner, synctex_node_t parent);
  961. synctex_status_t _synctex_scan_nested_sheet(synctex_scanner_t scanner);
  962. synctex_status_t _synctex_scan_content(synctex_scanner_t scanner);
  963. int synctex_scanner_pre_x_offset(synctex_scanner_t scanner);
  964. int synctex_scanner_pre_y_offset(synctex_scanner_t scanner);
  965. const char * synctex_scanner_get_output_fmt(synctex_scanner_t scanner);
  966. int _synctex_node_is_box(synctex_node_t node);
  967. int _synctex_bail(void);
  968. /* Try to ensure that the buffer contains at least size bytes.
  969. * Passing a huge size argument means the whole buffer length.
  970. * Passing a null size argument means return the available buffer length, without reading the file.
  971. * In that case, the return status is always SYNCTEX_STATUS_OK unless the given scanner is NULL,
  972. * in which case, SYNCTEX_STATUS_BAD_ARGUMENT is returned.
  973. * The value returned in size_ptr is the number of bytes now available in the buffer.
  974. * This is a nonnegative integer, it may take the value 0.
  975. * It is the responsibility of the caller to test whether this size is conforming to its needs.
  976. * Negative values may return in case of error, actually
  977. * when there was an error reading the synctex file. */
  978. synctex_status_t _synctex_buffer_get_available_size(synctex_scanner_t scanner, size_t * size_ptr) {
  979. size_t available = 0;
  980. if (NULL == scanner || NULL == size_ptr) {
  981. return SYNCTEX_STATUS_BAD_ARGUMENT;
  982. }
  983. # define size (* size_ptr)
  984. if (size>SYNCTEX_BUFFER_SIZE){
  985. size = SYNCTEX_BUFFER_SIZE;
  986. }
  987. available = SYNCTEX_END - SYNCTEX_CUR; /* available is the number of unparsed chars in the buffer */
  988. if (size<=available) {
  989. /* There are already sufficiently many characters in the buffer */
  990. size = available;
  991. return SYNCTEX_STATUS_OK;
  992. }
  993. if (SYNCTEX_FILE) {
  994. /* Copy the remaining part of the buffer to the beginning,
  995. * then read the next part of the file */
  996. int already_read = 0;
  997. if (available) {
  998. memmove(SYNCTEX_START, SYNCTEX_CUR, available);
  999. }
  1000. SYNCTEX_CUR = SYNCTEX_START + available; /* the next character after the move, will change. */
  1001. /* Fill the buffer up to its end */
  1002. already_read = gzread(SYNCTEX_FILE,(void *)SYNCTEX_CUR,SYNCTEX_BUFFER_SIZE - available);
  1003. if (already_read>0) {
  1004. /* We assume that 0<already_read<=SYNCTEX_BUFFER_SIZE - available, such that
  1005. * SYNCTEX_CUR + already_read = SYNCTEX_START + available + already_read <= SYNCTEX_START + SYNCTEX_BUFFER_SIZE */
  1006. SYNCTEX_END = SYNCTEX_CUR + already_read;
  1007. /* If the end of the file was reached, all the required SYNCTEX_BUFFER_SIZE - available
  1008. * may not be filled with values from the file.
  1009. * In that case, the buffer should stop properly after already_read characters. */
  1010. * SYNCTEX_END = '\0';
  1011. SYNCTEX_CUR = SYNCTEX_START;
  1012. size = SYNCTEX_END - SYNCTEX_CUR; /* == old available + already_read*/
  1013. return SYNCTEX_STATUS_OK; /* May be available is less than size, the caller will have to test. */
  1014. } else if (0>already_read) {
  1015. /* There is a possible error in reading the file */
  1016. int errnum = 0;
  1017. const char * error_string = gzerror(SYNCTEX_FILE, &errnum);
  1018. if (Z_ERRNO == errnum) {
  1019. /* There is an error in zlib caused by the file system */
  1020. _synctex_error("gzread error from the file system (%i)",errno);
  1021. return SYNCTEX_STATUS_ERROR;
  1022. } else if (errnum) {
  1023. _synctex_error("gzread error (%i:%i,%s)",already_read,errnum,error_string);
  1024. return SYNCTEX_STATUS_ERROR;
  1025. }
  1026. }
  1027. /* Nothing was read, we are at the end of the file. */
  1028. gzclose(SYNCTEX_FILE);
  1029. SYNCTEX_FILE = NULL;
  1030. SYNCTEX_END = SYNCTEX_CUR;
  1031. SYNCTEX_CUR = SYNCTEX_START;
  1032. * SYNCTEX_END = '\0';/* Terminate the string properly.*/
  1033. size = SYNCTEX_END - SYNCTEX_CUR;
  1034. return SYNCTEX_STATUS_EOF; /* there might be a bit of text left */
  1035. }
  1036. /* We cannot enlarge the buffer because the end of the file was reached. */
  1037. size = available;
  1038. return SYNCTEX_STATUS_EOF;
  1039. # undef size
  1040. }
  1041. /* Used when parsing the synctex file.
  1042. * Advance to the next character starting a line.
  1043. * Actually, only '\n' is recognized as end of line marker.
  1044. * On normal completion, the returned value is the number of unparsed characters available in the buffer.
  1045. * In general, it is a positive value, 0 meaning that the end of file was reached.
  1046. * -1 is returned in case of error, actually because there was an error while feeding the buffer.
  1047. * When the function returns with no error, SYNCTEX_CUR points to the first character of the next line, if any.
  1048. * J. Laurens: Sat May 10 07:52:31 UTC 2008
  1049. */
  1050. synctex_status_t _synctex_next_line(synctex_scanner_t scanner) {
  1051. synctex_status_t status = SYNCTEX_STATUS_OK;
  1052. size_t available = 0;
  1053. if (NULL == scanner) {
  1054. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1055. }
  1056. infinite_loop:
  1057. while(SYNCTEX_CUR<SYNCTEX_END) {
  1058. if (*SYNCTEX_CUR == '\n') {
  1059. ++SYNCTEX_CUR;
  1060. available = 1;
  1061. return _synctex_buffer_get_available_size(scanner, &available);
  1062. }
  1063. ++SYNCTEX_CUR;
  1064. }
  1065. /* Here, we have SYNCTEX_CUR == SYNCTEX_END, such that the next call to _synctex_buffer_get_available_size
  1066. * will read another bunch of synctex file. Little by little, we advance to the end of the file. */
  1067. available = 1;
  1068. status = _synctex_buffer_get_available_size(scanner, &available);
  1069. if (status<=0) {
  1070. return status;
  1071. }
  1072. goto infinite_loop;
  1073. }
  1074. /* Scan the given string.
  1075. * Both scanner and the_string must not be NULL, and the_string must not be 0 length.
  1076. * SYNCTEX_STATUS_OK is returned if the string is found,
  1077. * SYNCTEX_STATUS_EOF is returned when the EOF is reached,
  1078. * SYNCTEX_STATUS_NOT_OK is returned is the string is not found,
  1079. * an error status is returned otherwise.
  1080. * This is a critical method because buffering renders things more difficult.
  1081. * The given string might be as long as the maximum size_t value.
  1082. * As side effect, the buffer state may have changed if the given argument string can't fit into the buffer.
  1083. */
  1084. synctex_status_t _synctex_match_string(synctex_scanner_t scanner, const char * the_string) {
  1085. size_t tested_len = 0; /* the number of characters at the beginning of the_string that match */
  1086. size_t remaining_len = 0; /* the number of remaining characters of the_string that should match */
  1087. size_t available = 0;
  1088. synctex_status_t status = 0;
  1089. if (NULL == scanner || NULL == the_string) {
  1090. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1091. }
  1092. remaining_len = strlen(the_string); /* All the_string should match */
  1093. if (0 == remaining_len) {
  1094. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1095. }
  1096. /* How many characters available in the buffer? */
  1097. available = remaining_len;
  1098. status = _synctex_buffer_get_available_size(scanner,&available);
  1099. if (status<SYNCTEX_STATUS_EOF) {
  1100. return status;
  1101. }
  1102. /* Maybe we have less characters than expected because the buffer is too small. */
  1103. if (available>=remaining_len) {
  1104. /* The buffer is sufficiently big to hold the expected number of characters. */
  1105. if (strncmp((char *)SYNCTEX_CUR,the_string,remaining_len)) {
  1106. return SYNCTEX_STATUS_NOT_OK;
  1107. }
  1108. return_OK:
  1109. /* Advance SYNCTEX_CUR to the next character after the_string. */
  1110. SYNCTEX_CUR += remaining_len;
  1111. return SYNCTEX_STATUS_OK;
  1112. } else if (strncmp((char *)SYNCTEX_CUR,the_string,available)) {
  1113. /* No need to goo further, this is not the expected string in the buffer. */
  1114. return SYNCTEX_STATUS_NOT_OK;
  1115. } else if (SYNCTEX_FILE) {
  1116. /* The buffer was too small to contain remaining_len characters.
  1117. * We have to cut the string into pieces. */
  1118. z_off_t offset = 0L;
  1119. /* the first part of the string is found, advance the_string to the next untested character. */
  1120. the_string += available;
  1121. /* update the remaining length and the parsed length. */
  1122. remaining_len -= available;
  1123. tested_len += available;
  1124. SYNCTEX_CUR += available; /* We validate the tested characters. */
  1125. if (0 == remaining_len) {
  1126. /* Nothing left to test, we have found the given string, we return the length. */
  1127. return tested_len;
  1128. }
  1129. /* We also have to record the current state of the file cursor because
  1130. * if the_string does not match, all this should be a totally blank operation,
  1131. * for which the file and buffer states should not be modified at all.
  1132. * In fact, the states of the buffer before and after this function are in general different
  1133. * but they are totally equivalent as long as the values of the buffer before SYNCTEX_CUR
  1134. * can be safely discarded. */
  1135. offset = gztell(SYNCTEX_FILE);
  1136. /* offset now corresponds to the first character of the file that was not buffered. */
  1137. available = SYNCTEX_CUR - SYNCTEX_START; /* available can be used as temporary placeholder. */
  1138. /* available now corresponds to the number of chars that where already buffered and
  1139. * that match the head of the_string. If in fine the_string does not match, all these chars must be recovered
  1140. * because the buffer contents is completely replaced by _synctex_buffer_get_available_size.
  1141. * They were buffered from offset-len location in the file. */
  1142. offset -= available;
  1143. more_characters:
  1144. /* There is still some work to be done, so read another bunch of file.
  1145. * This is the second call to _synctex_buffer_get_available_size,
  1146. * which means that the actual contents of the buffer will be discarded.
  1147. * We will definitely have to recover the previous state in case we do not find the expected string. */
  1148. available = remaining_len;
  1149. status = _synctex_buffer_get_available_size(scanner,&available);
  1150. if (status<SYNCTEX_STATUS_EOF) {
  1151. return status; /* This is an error, no need to go further. */
  1152. }
  1153. if (available==0) {
  1154. /* Missing characters: recover the initial state of the file and return. */
  1155. return_NOT_OK:
  1156. if (offset != gzseek(SYNCTEX_FILE,offset,SEEK_SET)) {
  1157. /* This is a critical error, we could not recover the previous state. */
  1158. _synctex_error("can't seek file");
  1159. return SYNCTEX_STATUS_ERROR;
  1160. }
  1161. /* Next time we are asked to fill the buffer,
  1162. * we will read a complete bunch of text from the file. */
  1163. SYNCTEX_CUR = SYNCTEX_END;
  1164. return SYNCTEX_STATUS_NOT_OK;
  1165. }
  1166. if (available<remaining_len) {
  1167. /* We'll have to loop one more time. */
  1168. if (strncmp((char *)SYNCTEX_CUR,the_string,available)) {
  1169. /* This is not the expected string, recover the previous state and return. */
  1170. goto return_NOT_OK;
  1171. }
  1172. /* Advance the_string to the first untested character. */
  1173. the_string += available;
  1174. /* update the remaining length and the parsed length. */
  1175. remaining_len -= available;
  1176. tested_len += available;
  1177. SYNCTEX_CUR += available; /* We validate the tested characters. */
  1178. if (0 == remaining_len) {
  1179. /* Nothing left to test, we have found the given string. */
  1180. return SYNCTEX_STATUS_OK;
  1181. }
  1182. goto more_characters;
  1183. }
  1184. /* This is the last step. */
  1185. if (strncmp((char *)SYNCTEX_CUR,the_string,remaining_len)) {
  1186. /* This is not the expected string, recover the previous state and return. */
  1187. goto return_NOT_OK;
  1188. }
  1189. goto return_OK;
  1190. } else {
  1191. /* The buffer can't contain the given string argument, and the EOF was reached */
  1192. return SYNCTEX_STATUS_EOF;
  1193. }
  1194. }
  1195. /* Used when parsing the synctex file.
  1196. * Decode an integer.
  1197. * First, field separators, namely ':' and ',' characters are skipped
  1198. * The returned value is negative if there is an unrecoverable error.
  1199. * It is SYNCTEX_STATUS_NOT_OK if an integer could not be parsed, for example
  1200. * if the characters at the current cursor position are not digits or
  1201. * if the end of the file has been reached.
  1202. * It is SYNCTEX_STATUS_OK if an int has been successfully parsed.
  1203. * The given scanner argument must not be NULL, on the contrary, value_ref may be NULL.
  1204. */
  1205. synctex_status_t _synctex_decode_int(synctex_scanner_t scanner, int* value_ref) {
  1206. char * ptr = NULL;
  1207. char * end = NULL;
  1208. int result = 0;
  1209. size_t available = 0;
  1210. synctex_status_t status = 0;
  1211. if (NULL == scanner) {
  1212. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1213. }
  1214. available = SYNCTEX_BUFFER_MIN_SIZE;
  1215. status = _synctex_buffer_get_available_size(scanner, &available);
  1216. if (status<SYNCTEX_STATUS_EOF) {
  1217. return status;/* Forward error. */
  1218. }
  1219. if (available==0) {
  1220. return SYNCTEX_STATUS_EOF;/* it is the end of file. */
  1221. }
  1222. ptr = SYNCTEX_CUR;
  1223. if (*ptr==':' || *ptr==',') {
  1224. ++ptr;
  1225. --available;
  1226. if (available==0) {
  1227. return SYNCTEX_STATUS_NOT_OK;/* It is not possible to scan an int */
  1228. }
  1229. }
  1230. result = (int)strtol(ptr, &end, 10);
  1231. if (end>ptr) {
  1232. SYNCTEX_CUR = end;
  1233. if (value_ref) {
  1234. * value_ref = result;
  1235. }
  1236. return SYNCTEX_STATUS_OK;/* Successfully scanned an int */
  1237. }
  1238. return SYNCTEX_STATUS_NOT_OK;/* Could not scan an int */
  1239. }
  1240. /* The purpose of this function is to read a string.
  1241. * A string is an array of characters from the current parser location
  1242. * and before the next '\n' character.
  1243. * If a string was properly decoded, it is returned in value_ref and
  1244. * the cursor points to the new line marker.
  1245. * The returned string was alloced on the heap, the caller is the owner and
  1246. * is responsible to free it in due time.
  1247. * If no string is parsed, * value_ref is undefined.
  1248. * The maximum length of a string that a scanner can decode is platform dependent, namely UINT_MAX.
  1249. * If you just want to blindly parse the file up to the end of the current line,
  1250. * use _synctex_next_line instead.
  1251. * On return, the scanner cursor is unchanged if a string could not be scanned or
  1252. * points to the terminating '\n' character otherwise. As a consequence,
  1253. * _synctex_next_line is necessary after.
  1254. * If either scanner or value_ref is NULL, it is considered as an error and
  1255. * SYNCTEX_STATUS_BAD_ARGUMENT is returned.
  1256. */
  1257. synctex_status_t _synctex_decode_string(synctex_scanner_t scanner, char ** value_ref) {
  1258. char * end = NULL;
  1259. size_t current_size = 0;
  1260. size_t new_size = 0;
  1261. size_t len = 0;/* The number of bytes to copy */
  1262. size_t available = 0;
  1263. synctex_status_t status = 0;
  1264. if (NULL == scanner || NULL == value_ref) {
  1265. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1266. }
  1267. /* The buffer must at least contain one character: the '\n' end of line marker */
  1268. if (SYNCTEX_CUR>=SYNCTEX_END) {
  1269. available = 1;
  1270. status = _synctex_buffer_get_available_size(scanner,&available);
  1271. if (status < 0) {
  1272. return status;
  1273. }
  1274. if (0 == available) {
  1275. return SYNCTEX_STATUS_EOF;
  1276. }
  1277. }
  1278. /* Now we are sure that there is at least one available character, either because
  1279. * SYNCTEX_CUR was already < SYNCTEX_END, or because the buffer has been properly filled. */
  1280. /* end will point to the next unparsed '\n' character in the file, when mapped to the buffer. */
  1281. end = SYNCTEX_CUR;
  1282. * value_ref = NULL;/* Initialize, it will be realloc'ed */
  1283. /* We scan all the characters up to the next '\n' */
  1284. next_character:
  1285. if (end<SYNCTEX_END) {
  1286. if (*end == '\n') {
  1287. /* OK, we found where to stop */
  1288. len = end - SYNCTEX_CUR;
  1289. if (current_size>UINT_MAX-len-1) {
  1290. /* But we have reached the limit: we do not have current_size+len+1>UINT_MAX.
  1291. * We return the missing amount of memory.
  1292. * This will never occur in practice. */
  1293. return UINT_MAX-len-1 - current_size;
  1294. }
  1295. new_size = current_size+len;
  1296. /* We have current_size+len+1<=UINT_MAX
  1297. * or equivalently new_size<UINT_MAX,
  1298. * where we have assumed that len<UINT_MAX */
  1299. if ((* value_ref = realloc(* value_ref,new_size+1)) != NULL) {
  1300. if (memcpy((*value_ref)+current_size,SYNCTEX_CUR,len)) {
  1301. (* value_ref)[new_size]='\0'; /* Terminate the string */
  1302. SYNCTEX_CUR += len;/* Advance to the terminating '\n' */
  1303. return SYNCTEX_STATUS_OK;
  1304. }
  1305. free(* value_ref);
  1306. * value_ref = NULL;
  1307. _synctex_error("could not copy memory (1).");
  1308. return SYNCTEX_STATUS_ERROR;
  1309. }
  1310. _synctex_error("could not allocate memory (1).");
  1311. return SYNCTEX_STATUS_ERROR;
  1312. } else {
  1313. ++end;
  1314. goto next_character;
  1315. }
  1316. } else {
  1317. /* end == SYNCTEX_END */
  1318. len = SYNCTEX_END - SYNCTEX_CUR;
  1319. if (current_size>UINT_MAX-len-1) {
  1320. /* We have reached the limit. */
  1321. _synctex_error("limit reached (missing %i).",current_size-(UINT_MAX-len-1));
  1322. return SYNCTEX_STATUS_ERROR;
  1323. }
  1324. new_size = current_size+len;
  1325. if ((* value_ref = realloc(* value_ref,new_size+1)) != NULL) {
  1326. if (memcpy((*value_ref)+current_size,SYNCTEX_CUR,len)) {
  1327. (* value_ref)[new_size]='\0'; /* Terminate the string */
  1328. SYNCTEX_CUR = SYNCTEX_END;/* Advance the cursor to the end of the bufer */
  1329. return SYNCTEX_STATUS_OK;
  1330. }
  1331. free(* value_ref);
  1332. * value_ref = NULL;
  1333. _synctex_error("could not copy memory (2).");
  1334. return SYNCTEX_STATUS_ERROR;
  1335. }
  1336. /* Huge memory problem */
  1337. _synctex_error("could not allocate memory (2).");
  1338. return SYNCTEX_STATUS_ERROR;
  1339. }
  1340. }
  1341. /* Used when parsing the synctex file.
  1342. * Read an Input record.
  1343. */
  1344. synctex_status_t _synctex_scan_input(synctex_scanner_t scanner) {
  1345. synctex_status_t status = 0;
  1346. size_t available = 0;
  1347. synctex_node_t input = NULL;
  1348. if (NULL == scanner) {
  1349. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1350. }
  1351. status = _synctex_match_string(scanner,"Input:");
  1352. if (status<SYNCTEX_STATUS_OK) {
  1353. return status;
  1354. }
  1355. /* Create a node */
  1356. input = _synctex_new_input(scanner);
  1357. if (NULL == input) {
  1358. _synctex_error("could not create an input node.");
  1359. return SYNCTEX_STATUS_ERROR;
  1360. }
  1361. /* Decode the synctag */
  1362. status = _synctex_decode_int(scanner,&(SYNCTEX_TAG(input)));
  1363. if (status<SYNCTEX_STATUS_OK) {
  1364. _synctex_error("bad format of input node.");
  1365. SYNCTEX_FREE(input);
  1366. return status;
  1367. }
  1368. /* The next character is a field separator, we expect one character in the buffer. */
  1369. available = 1;
  1370. status = _synctex_buffer_get_available_size(scanner, &available);
  1371. if (status<=SYNCTEX_STATUS_ERROR) {
  1372. return status;
  1373. }
  1374. if (0 == available) {
  1375. return SYNCTEX_STATUS_EOF;
  1376. }
  1377. /* We can now safely advance to the next character, stepping over the field separator. */
  1378. ++SYNCTEX_CUR;
  1379. --available;
  1380. /* Then we scan the file name */
  1381. status = _synctex_decode_string(scanner,&(SYNCTEX_NAME(input)));
  1382. if (status<SYNCTEX_STATUS_OK) {
  1383. SYNCTEX_FREE(input);
  1384. return status;
  1385. }
  1386. /* Prepend this input node to the input linked list of the scanner */
  1387. SYNCTEX_SET_SIBLING(input,scanner->input);
  1388. scanner->input = input;
  1389. return _synctex_next_line(scanner);/* read the line termination character, if any */
  1390. /* Now, set up the path */
  1391. }
  1392. typedef synctex_status_t (*synctex_decoder_t)(synctex_scanner_t,void *);
  1393. synctex_status_t _synctex_scan_named(synctex_scanner_t scanner,const char * name,void * value_ref,synctex_decoder_t decoder);
  1394. /* Used when parsing the synctex file.
  1395. * Read one of the settings.
  1396. * On normal completion, returns SYNCTEX_STATUS_OK.
  1397. * On error, returns SYNCTEX_STATUS_ERROR.
  1398. * Both arguments must not be NULL.
  1399. * On return, the scanner points to the next character after the decoded object whatever it is.
  1400. * It is the responsibility of the caller to prepare the scanner for the next line.
  1401. */
  1402. synctex_status_t _synctex_scan_named(synctex_scanner_t scanner,const char * name,void * value_ref,synctex_decoder_t decoder) {
  1403. synctex_status_t status = 0;
  1404. if (NULL == scanner || NULL == name || NULL == value_ref || NULL == decoder) {
  1405. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1406. }
  1407. not_found:
  1408. status = _synctex_match_string(scanner,name);
  1409. if (status<SYNCTEX_STATUS_NOT_OK) {
  1410. return status;
  1411. } else if (status == SYNCTEX_STATUS_NOT_OK) {
  1412. status = _synctex_next_line(scanner);
  1413. if (status<SYNCTEX_STATUS_OK) {
  1414. return status;
  1415. }
  1416. goto not_found;
  1417. }
  1418. /* A line is found, scan the value */
  1419. return (*decoder)(scanner,value_ref);
  1420. }
  1421. /* Used when parsing the synctex file.
  1422. * Read the preamble.
  1423. */
  1424. synctex_status_t _synctex_scan_preamble(synctex_scanner_t scanner) {
  1425. synctex_status_t status = 0;
  1426. if (NULL == scanner) {
  1427. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1428. }
  1429. status = _synctex_scan_named(scanner,"SyncTeX Version:",&(scanner->version),(synctex_decoder_t)&_synctex_decode_int);
  1430. if (status<SYNCTEX_STATUS_OK) {
  1431. return status;
  1432. }
  1433. status = _synctex_next_line(scanner);
  1434. if (status<SYNCTEX_STATUS_OK) {
  1435. return status;
  1436. }
  1437. /* Read all the input records */
  1438. do {
  1439. status = _synctex_scan_input(scanner);
  1440. if (status<SYNCTEX_STATUS_NOT_OK) {
  1441. return status;
  1442. }
  1443. } while(status == SYNCTEX_STATUS_OK);
  1444. /* the loop exits when status == SYNCTEX_STATUS_NOT_OK */
  1445. /* Now read all the required settings. */
  1446. status = _synctex_scan_named(scanner,"Output:",&(scanner->output_fmt),(synctex_decoder_t)&_synctex_decode_string);
  1447. if (status<SYNCTEX_STATUS_NOT_OK) {
  1448. return status;
  1449. }
  1450. status = _synctex_next_line(scanner);
  1451. if (status<SYNCTEX_STATUS_OK) {
  1452. return status;
  1453. }
  1454. status = _synctex_scan_named(scanner,"Magnification:",&(scanner->pre_magnification),(synctex_decoder_t)&_synctex_decode_int);
  1455. if (status<SYNCTEX_STATUS_OK) {
  1456. return status;
  1457. }
  1458. status = _synctex_next_line(scanner);
  1459. if (status<SYNCTEX_STATUS_OK) {
  1460. return status;
  1461. }
  1462. status = _synctex_scan_named(scanner,"Unit:",&(scanner->pre_unit),(synctex_decoder_t)&_synctex_decode_int);
  1463. if (status<SYNCTEX_STATUS_OK) {
  1464. return status;
  1465. }
  1466. status = _synctex_next_line(scanner);
  1467. if (status<SYNCTEX_STATUS_OK) {
  1468. return status;
  1469. }
  1470. status = _synctex_scan_named(scanner,"X Offset:",&(scanner->pre_x_offset),(synctex_decoder_t)&_synctex_decode_int);
  1471. if (status<SYNCTEX_STATUS_OK) {
  1472. return status;
  1473. }
  1474. status = _synctex_next_line(scanner);
  1475. if (status<SYNCTEX_STATUS_OK) {
  1476. return status;
  1477. }
  1478. status = _synctex_scan_named(scanner,"Y Offset:",&(scanner->pre_y_offset),(synctex_decoder_t)&_synctex_decode_int);
  1479. if (status<SYNCTEX_STATUS_OK) {
  1480. return status;
  1481. }
  1482. return _synctex_next_line(scanner);
  1483. }
  1484. /* parse a float with a dimension */
  1485. synctex_status_t _synctex_scan_float_and_dimension(synctex_scanner_t scanner, float * value_ref) {
  1486. synctex_status_t status = 0;
  1487. char * endptr = NULL;
  1488. float f = 0;
  1489. #ifdef HAVE_SETLOCALE
  1490. char * loc = setlocale(LC_NUMERIC, NULL);
  1491. #endif
  1492. size_t available = 0;
  1493. if (NULL == scanner || NULL == value_ref) {
  1494. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1495. }
  1496. available = SYNCTEX_BUFFER_MIN_SIZE;
  1497. status = _synctex_buffer_get_available_size(scanner, &available);
  1498. if (status<SYNCTEX_STATUS_EOF) {
  1499. _synctex_error("problem with float.");
  1500. return status;
  1501. }
  1502. #ifdef HAVE_SETLOCALE
  1503. setlocale(LC_NUMERIC, "C");
  1504. #endif
  1505. f = strtod(SYNCTEX_CUR,&endptr);
  1506. #ifdef HAVE_SETLOCALE
  1507. setlocale(LC_NUMERIC, loc);
  1508. #endif
  1509. if (endptr == SYNCTEX_CUR) {
  1510. _synctex_error("a float was expected.");
  1511. return SYNCTEX_STATUS_ERROR;
  1512. }
  1513. SYNCTEX_CUR = endptr;
  1514. if ((status = _synctex_match_string(scanner,"in")) >= SYNCTEX_STATUS_OK) {
  1515. f *= 72.27f*65536;
  1516. } else if (status<SYNCTEX_STATUS_EOF) {
  1517. report_unit_error:
  1518. _synctex_error("problem with unit.");
  1519. return status;
  1520. } else if ((status = _synctex_match_string(scanner,"cm")) >= SYNCTEX_STATUS_OK) {
  1521. f *= 72.27f*65536/2.54f;
  1522. } else if (status<0) {
  1523. goto report_unit_error;
  1524. } else if ((status = _synctex_match_string(scanner,"mm")) >= SYNCTEX_STATUS_OK) {
  1525. f *= 72.27f*65536/25.4f;
  1526. } else if (status<0) {
  1527. goto report_unit_error;
  1528. } else if ((status = _synctex_match_string(scanner,"pt")) >= SYNCTEX_STATUS_OK) {
  1529. f *= 65536.0f;
  1530. } else if (status<0) {
  1531. goto report_unit_error;
  1532. } else if ((status = _synctex_match_string(scanner,"bp")) >= SYNCTEX_STATUS_OK) {
  1533. f *= 72.27f/72*65536.0f;
  1534. } else if (status<0) {
  1535. goto report_unit_error;
  1536. } else if ((status = _synctex_match_string(scanner,"pc")) >= SYNCTEX_STATUS_OK) {
  1537. f *= 12.0*65536.0f;
  1538. } else if (status<0) {
  1539. goto report_unit_error;
  1540. } else if ((status = _synctex_match_string(scanner,"sp")) >= SYNCTEX_STATUS_OK) {
  1541. f *= 1.0f;
  1542. } else if (status<0) {
  1543. goto report_unit_error;
  1544. } else if ((status = _synctex_match_string(scanner,"dd")) >= SYNCTEX_STATUS_OK) {
  1545. f *= 1238.0f/1157*65536.0f;
  1546. } else if (status<0) {
  1547. goto report_unit_error;
  1548. } else if ((status = _synctex_match_string(scanner,"cc")) >= SYNCTEX_STATUS_OK) {
  1549. f *= 14856.0f/1157*65536;
  1550. } else if (status<0) {
  1551. goto report_unit_error;
  1552. } else if ((status = _synctex_match_string(scanner,"nd")) >= SYNCTEX_STATUS_OK) {
  1553. f *= 685.0f/642*65536;
  1554. } else if (status<0) {
  1555. goto report_unit_error;
  1556. } else if ((status = _synctex_match_string(scanner,"nc")) >= SYNCTEX_STATUS_OK) {
  1557. f *= 1370.0f/107*65536;
  1558. } else if (status<0) {
  1559. goto report_unit_error;
  1560. }
  1561. *value_ref = f;
  1562. return SYNCTEX_STATUS_OK;
  1563. }
  1564. /* parse the post scriptum
  1565. * SYNCTEX_STATUS_OK is returned on completion
  1566. * a negative error is returned otherwise */
  1567. synctex_status_t _synctex_scan_post_scriptum(synctex_scanner_t scanner) {
  1568. synctex_status_t status = 0;
  1569. char * endptr = NULL;
  1570. #ifdef HAVE_SETLOCALE
  1571. char * loc = setlocale(LC_NUMERIC, NULL);
  1572. #endif
  1573. if (NULL == scanner) {
  1574. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1575. }
  1576. /* Scan the file until a post scriptum line is found */
  1577. post_scriptum_not_found:
  1578. status = _synctex_match_string(scanner,"Post scriptum:");
  1579. if (status<SYNCTEX_STATUS_NOT_OK) {
  1580. return status;
  1581. }
  1582. if (status == SYNCTEX_STATUS_NOT_OK) {
  1583. status = _synctex_next_line(scanner);
  1584. if (status<SYNCTEX_STATUS_EOF) {
  1585. return status;
  1586. } else if (status<SYNCTEX_STATUS_OK) {
  1587. return SYNCTEX_STATUS_OK;/* The EOF is found, we have properly scanned the file */
  1588. }
  1589. goto post_scriptum_not_found;
  1590. }
  1591. /* We found the name, advance to the next line. */
  1592. next_line:
  1593. status = _synctex_next_line(scanner);
  1594. if (status<SYNCTEX_STATUS_EOF) {
  1595. return status;
  1596. } else if (status<SYNCTEX_STATUS_OK) {
  1597. return SYNCTEX_STATUS_OK;/* The EOF is found, we have properly scanned the file */
  1598. }
  1599. /* Scanning the information */
  1600. status = _synctex_match_string(scanner,"Magnification:");
  1601. if (status == SYNCTEX_STATUS_OK ) {
  1602. #ifdef HAVE_SETLOCALE
  1603. setlocale(LC_NUMERIC, "C");
  1604. #endif
  1605. scanner->unit = strtod(SYNCTEX_CUR,&endptr);
  1606. #ifdef HAVE_SETLOCALE
  1607. setlocale(LC_NUMERIC, loc);
  1608. #endif
  1609. if (endptr == SYNCTEX_CUR) {
  1610. _synctex_error("bad magnification in the post scriptum, a float was expected.");
  1611. return SYNCTEX_STATUS_ERROR;
  1612. }
  1613. if (scanner->unit<=0) {
  1614. _synctex_error("bad magnification in the post scriptum, a positive float was expected.");
  1615. return SYNCTEX_STATUS_ERROR;
  1616. }
  1617. SYNCTEX_CUR = endptr;
  1618. goto next_line;
  1619. }
  1620. if (status<SYNCTEX_STATUS_EOF){
  1621. report_record_problem:
  1622. _synctex_error("Problem reading the Post Scriptum records");
  1623. return status; /* echo the error. */
  1624. }
  1625. status = _synctex_match_string(scanner,"X Offset:");
  1626. if (status == SYNCTEX_STATUS_OK) {
  1627. status = _synctex_scan_float_and_dimension(scanner, &(scanner->x_offset));
  1628. if (status<SYNCTEX_STATUS_OK) {
  1629. _synctex_error("problem with X offset in the Post Scriptum.");
  1630. return status;
  1631. }
  1632. goto next_line;
  1633. } else if (status<SYNCTEX_STATUS_EOF){
  1634. goto report_record_problem;
  1635. }
  1636. status = _synctex_match_string(scanner,"Y Offset:");
  1637. if (status==SYNCTEX_STATUS_OK) {
  1638. status = _synctex_scan_float_and_dimension(scanner, &(scanner->y_offset));
  1639. if (status<SYNCTEX_STATUS_OK) {
  1640. _synctex_error("problem with Y offset in the Post Scriptum.");
  1641. return status;
  1642. }
  1643. goto next_line;
  1644. } else if (status<SYNCTEX_STATUS_EOF){
  1645. goto report_record_problem;
  1646. }
  1647. goto next_line;
  1648. }
  1649. /* SYNCTEX_STATUS_OK is returned if the postamble is read
  1650. * SYNCTEX_STATUS_NOT_OK is returned if the postamble is not at the current location
  1651. * a negative error otherwise
  1652. * The postamble comprises the post scriptum section.
  1653. */
  1654. int _synctex_scan_postamble(synctex_scanner_t scanner) {
  1655. int status = 0;
  1656. if (NULL == scanner) {
  1657. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1658. }
  1659. status = _synctex_match_string(scanner,"Postamble:");
  1660. if (status < SYNCTEX_STATUS_OK) {
  1661. return status;
  1662. }
  1663. count_again:
  1664. status = _synctex_next_line(scanner);
  1665. if (status < SYNCTEX_STATUS_OK) {
  1666. return status;
  1667. }
  1668. status = _synctex_scan_named(scanner,"Count:",&(scanner->count),(synctex_decoder_t)&_synctex_decode_int);
  1669. if (status < SYNCTEX_STATUS_EOF) {
  1670. return status; /* forward the error */
  1671. } else if (status < SYNCTEX_STATUS_OK) { /* No Count record found */
  1672. status = _synctex_next_line(scanner); /* Advance one more line */
  1673. if (status<SYNCTEX_STATUS_OK) {
  1674. return status;
  1675. }
  1676. goto count_again;
  1677. }
  1678. /* Now we scan the last part of the SyncTeX file: the Post Scriptum section. */
  1679. return _synctex_scan_post_scriptum(scanner);
  1680. }
  1681. /* Horizontal boxes also have visible size.
  1682. * Visible size are bigger than real size.
  1683. * For example 0 width boxes may contain text.
  1684. * At creation time, the visible size is set to the values of the real size.
  1685. */
  1686. synctex_status_t _synctex_setup_visible_box(synctex_node_t box) {
  1687. if (box) {
  1688. switch(box->class->type) {
  1689. case synctex_node_type_hbox:
  1690. if (SYNCTEX_INFO(box) != NULL) {
  1691. SYNCTEX_HORIZ_V(box) = SYNCTEX_HORIZ(box);
  1692. SYNCTEX_VERT_V(box) = SYNCTEX_VERT(box);
  1693. SYNCTEX_WIDTH_V(box) = SYNCTEX_WIDTH(box);
  1694. SYNCTEX_HEIGHT_V(box) = SYNCTEX_HEIGHT(box);
  1695. SYNCTEX_DEPTH_V(box) = SYNCTEX_DEPTH(box);
  1696. return SYNCTEX_STATUS_OK;
  1697. }
  1698. return SYNCTEX_STATUS_ERROR;
  1699. }
  1700. }
  1701. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1702. }
  1703. /* This method is sent to an horizontal box to setup the visible size
  1704. * Some box have 0 width but do contain text material.
  1705. * With this method, one can enlarge the box to contain the given point (h,v).
  1706. */
  1707. synctex_status_t _synctex_horiz_box_setup_visible(synctex_node_t node,int h, int v) {
  1708. # ifdef __DARWIN_UNIX03
  1709. # pragma unused(v)
  1710. # endif
  1711. int itsBtm, itsTop;
  1712. if (NULL == node || node->class->type != synctex_node_type_hbox) {
  1713. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1714. }
  1715. if (SYNCTEX_WIDTH_V(node)<0) {
  1716. itsBtm = SYNCTEX_HORIZ_V(node);
  1717. itsTop = SYNCTEX_HORIZ_V(node)-SYNCTEX_WIDTH_V(node);
  1718. if (h<itsBtm) {
  1719. SYNCTEX_HORIZ_V(node) = h;
  1720. SYNCTEX_WIDTH_V(node) = SYNCTEX_HORIZ_V(node) - itsTop;
  1721. } else if (h>itsTop) {
  1722. SYNCTEX_WIDTH_V(node) = SYNCTEX_HORIZ_V(node) - h;
  1723. }
  1724. } else {
  1725. itsBtm = SYNCTEX_HORIZ_V(node);
  1726. itsTop = SYNCTEX_HORIZ_V(node)+SYNCTEX_WIDTH_V(node);
  1727. if (h<itsBtm) {
  1728. SYNCTEX_HORIZ_V(node) = h;
  1729. SYNCTEX_WIDTH_V(node) = itsTop - SYNCTEX_HORIZ_V(node);
  1730. } else if (h>itsTop) {
  1731. SYNCTEX_WIDTH_V(node) = h - SYNCTEX_HORIZ_V(node);
  1732. }
  1733. }
  1734. return SYNCTEX_STATUS_OK;
  1735. }
  1736. /* Here are the control characters that strat each line of the synctex output file.
  1737. * Their values define the meaning of the line.
  1738. */
  1739. # define SYNCTEX_CHAR_BEGIN_SHEET '{'
  1740. # define SYNCTEX_CHAR_END_SHEET '}'
  1741. # define SYNCTEX_CHAR_BEGIN_VBOX '['
  1742. # define SYNCTEX_CHAR_END_VBOX ']'
  1743. # define SYNCTEX_CHAR_BEGIN_HBOX '('
  1744. # define SYNCTEX_CHAR_END_HBOX ')'
  1745. # define SYNCTEX_CHAR_ANCHOR '!'
  1746. # define SYNCTEX_CHAR_VOID_VBOX 'v'
  1747. # define SYNCTEX_CHAR_VOID_HBOX 'h'
  1748. # define SYNCTEX_CHAR_KERN 'k'
  1749. # define SYNCTEX_CHAR_GLUE 'g'
  1750. # define SYNCTEX_CHAR_MATH '$'
  1751. # define SYNCTEX_CHAR_BOUNDARY 'x'
  1752. # define SYNCTEX_RETURN(STATUS) return STATUS;
  1753. /* Used when parsing the synctex file. A '{' character has just been parsed.
  1754. * The purpose is to gobble everything until the closing '}'.
  1755. * Actually only one nesting depth has been observed when using the clip option
  1756. * of \includegraphics option. Here we use arbitrary level of depth.
  1757. */
  1758. synctex_status_t _synctex_scan_nested_sheet(synctex_scanner_t scanner) {
  1759. unsigned int depth = 0;
  1760. deeper:
  1761. ++depth;
  1762. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1763. _synctex_error("Unexpected end of nested sheet (1).");
  1764. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1765. }
  1766. scan_next_line:
  1767. if (SYNCTEX_CUR<SYNCTEX_END) {
  1768. if (*SYNCTEX_CUR == SYNCTEX_CHAR_END_SHEET) {
  1769. ++SYNCTEX_CUR;
  1770. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1771. _synctex_error("Unexpected end of nested sheet (2).");
  1772. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1773. }
  1774. if (--depth>0) {
  1775. goto scan_next_line;
  1776. } else {
  1777. SYNCTEX_RETURN(SYNCTEX_STATUS_OK);
  1778. }
  1779. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_SHEET) {
  1780. ++SYNCTEX_CUR;
  1781. goto deeper;
  1782. } else if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1783. _synctex_error("Unexpected end of nested sheet (3).");
  1784. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1785. }
  1786. }
  1787. _synctex_error("Unexpected end of nested sheet (4).");
  1788. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1789. }
  1790. /* Used when parsing the synctex file.
  1791. * The sheet argument is a newly created sheet node that will hold the contents.
  1792. * Something is returned in case of error.
  1793. */
  1794. synctex_status_t _synctex_scan_sheet(synctex_scanner_t scanner, synctex_node_t sheet) {
  1795. synctex_node_t parent = sheet;
  1796. synctex_node_t child = NULL;
  1797. synctex_node_t sibling = NULL;
  1798. synctex_node_t box = sheet;
  1799. int friend_index = 0;
  1800. synctex_info_t * info = NULL;
  1801. synctex_status_t status = 0;
  1802. size_t available = 0;
  1803. if ((NULL == scanner) || (NULL == sheet)) {
  1804. return SYNCTEX_STATUS_BAD_ARGUMENT;
  1805. }
  1806. /* We MUST start with a box, so at this level, the unique possibility is '[', '(' or "}". */
  1807. prepare_loop:
  1808. if (SYNCTEX_CUR<SYNCTEX_END) {
  1809. if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_VBOX) {
  1810. scan_vbox:
  1811. ++SYNCTEX_CUR;
  1812. if ((child = _synctex_new_vbox(scanner)) && (info = SYNCTEX_INFO(child))) {
  1813. # define SYNCTEX_DECODE_FAILED(WHAT) \
  1814. (_synctex_decode_int(scanner,&(info[WHAT].INT))<SYNCTEX_STATUS_OK)
  1815. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  1816. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  1817. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  1818. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  1819. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  1820. || SYNCTEX_DECODE_FAILED(SYNCTEX_HEIGHT_IDX)
  1821. || SYNCTEX_DECODE_FAILED(SYNCTEX_DEPTH_IDX)
  1822. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1823. _synctex_error("Bad vbox record.");
  1824. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1825. }
  1826. SYNCTEX_SET_CHILD(parent,child);
  1827. parent = child;
  1828. child = NULL;
  1829. goto child_loop;/* next created node will be a child */
  1830. } else {
  1831. _synctex_error("Can't create vbox record.");
  1832. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1833. }
  1834. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_HBOX) {
  1835. scan_hbox:
  1836. ++SYNCTEX_CUR;
  1837. if ((child = _synctex_new_hbox(scanner)) && (info = SYNCTEX_INFO(child))) {
  1838. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  1839. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  1840. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  1841. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  1842. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  1843. || SYNCTEX_DECODE_FAILED(SYNCTEX_HEIGHT_IDX)
  1844. || SYNCTEX_DECODE_FAILED(SYNCTEX_DEPTH_IDX)
  1845. || _synctex_setup_visible_box(child)<SYNCTEX_STATUS_OK
  1846. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1847. _synctex_error("Bad hbox record.");
  1848. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1849. }
  1850. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  1851. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child)+SYNCTEX_ABS_WIDTH(child),SYNCTEX_VERT(child));
  1852. SYNCTEX_SET_CHILD(parent,child);
  1853. parent = child;
  1854. child = NULL;
  1855. goto child_loop;/* next created node will be a child */
  1856. } else {
  1857. _synctex_error("Can't create hbox record.");
  1858. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1859. }
  1860. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_END_SHEET) {
  1861. scan_teehs:
  1862. ++SYNCTEX_CUR;
  1863. if (NULL == parent || parent->class->type != synctex_node_type_sheet
  1864. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1865. _synctex_error("Unexpected end of sheet.");
  1866. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1867. }
  1868. SYNCTEX_RETURN(SYNCTEX_STATUS_OK);
  1869. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_SHEET) {
  1870. /* Addendum to version 1.10 to manage nested sheets */
  1871. ++SYNCTEX_CUR;
  1872. if (_synctex_scan_nested_sheet(scanner)<SYNCTEX_STATUS_OK) {
  1873. _synctex_error("Unexpected nested sheet.");
  1874. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1875. }
  1876. goto prepare_loop;
  1877. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_ANCHOR) {
  1878. scan_anchor:
  1879. ++SYNCTEX_CUR;
  1880. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1881. _synctex_error("Missing anchor.");
  1882. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1883. }
  1884. goto prepare_loop;
  1885. } else {
  1886. /* _synctex_error("Ignored record %c\n",*SYNCTEX_CUR); */
  1887. ++SYNCTEX_CUR;
  1888. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1889. _synctex_error("Unexpected end.");
  1890. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1891. }
  1892. goto prepare_loop;
  1893. }
  1894. } else {
  1895. available = 1;
  1896. status = _synctex_buffer_get_available_size(scanner,&available);
  1897. if (status<SYNCTEX_STATUS_OK && available>0){
  1898. _synctex_error("Uncomplete sheet(0)");
  1899. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1900. } else {
  1901. goto prepare_loop;
  1902. }
  1903. }
  1904. _synctex_bail();
  1905. /* The child loop means that we go do one level, when we just created a box node,
  1906. * the next node created is a child of this box. */
  1907. child_loop:
  1908. if (SYNCTEX_CUR<SYNCTEX_END) {
  1909. if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_VBOX) {
  1910. goto scan_vbox;
  1911. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_END_VBOX) {
  1912. scan_xobv:
  1913. ++SYNCTEX_CUR;
  1914. if (NULL != parent && parent->class->type == synctex_node_type_vbox) {
  1915. #define SYNCTEX_UPDATE_BOX_FRIEND(NODE)\
  1916. friend_index = ((SYNCTEX_INFO(NODE))[SYNCTEX_TAG_IDX].INT+(SYNCTEX_INFO(NODE))[SYNCTEX_LINE_IDX].INT)%(scanner->number_of_lists);\
  1917. SYNCTEX_SET_FRIEND(NODE,(scanner->lists_of_friends)[friend_index]);\
  1918. (scanner->lists_of_friends)[friend_index] = NODE;
  1919. if (NULL == SYNCTEX_CHILD(parent)) {
  1920. /* only void boxes are friends */
  1921. SYNCTEX_UPDATE_BOX_FRIEND(parent);
  1922. }
  1923. child = parent;
  1924. parent = SYNCTEX_PARENT(child);
  1925. } else {
  1926. _synctex_error("Unexpected end of vbox, ignored.");
  1927. }
  1928. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1929. _synctex_error("Uncomplete sheet.");
  1930. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1931. }
  1932. goto sibling_loop;
  1933. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_HBOX) {
  1934. goto scan_hbox;
  1935. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_END_HBOX) {
  1936. scan_xobh:
  1937. ++SYNCTEX_CUR;
  1938. if ((parent) && parent->class->type == synctex_node_type_hbox) {
  1939. if (NULL == child) {
  1940. /* Only boxes with no children are friends,
  1941. * boxes with children are indirectly friends through one of their descendants. */
  1942. SYNCTEX_UPDATE_BOX_FRIEND(parent);
  1943. }
  1944. /* setting the next horizontal box at the end ensures that a child is recorded before any of its ancestors. */
  1945. SYNCTEX_SET_NEXT_HORIZ_BOX(box,parent);
  1946. box = parent;
  1947. child = parent;
  1948. parent = SYNCTEX_PARENT(child);
  1949. } else {
  1950. _synctex_error("Unexpected enf of hbox, ignored.");
  1951. }
  1952. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1953. _synctex_error("Uncomplete sheet.");
  1954. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1955. }
  1956. goto sibling_loop;
  1957. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_VOID_VBOX) {
  1958. ++SYNCTEX_CUR;
  1959. if (NULL != (child = _synctex_new_void_vbox(scanner))
  1960. && NULL != (info = SYNCTEX_INFO(child))) {
  1961. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  1962. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  1963. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  1964. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  1965. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  1966. || SYNCTEX_DECODE_FAILED(SYNCTEX_HEIGHT_IDX)
  1967. || SYNCTEX_DECODE_FAILED(SYNCTEX_DEPTH_IDX)
  1968. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1969. _synctex_error("Bad void vbox record.");
  1970. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1971. }
  1972. SYNCTEX_SET_CHILD(parent,child);
  1973. #define SYNCTEX_UPDATE_FRIEND(NODE)\
  1974. friend_index = (info[SYNCTEX_TAG_IDX].INT+info[SYNCTEX_LINE_IDX].INT)%(scanner->number_of_lists);\
  1975. SYNCTEX_SET_FRIEND(NODE,(scanner->lists_of_friends)[friend_index]);\
  1976. (scanner->lists_of_friends)[friend_index] = NODE;
  1977. SYNCTEX_UPDATE_FRIEND(child);
  1978. goto sibling_loop;
  1979. } else {
  1980. _synctex_error("Can't create vbox record.");
  1981. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1982. }
  1983. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_VOID_HBOX) {
  1984. ++SYNCTEX_CUR;
  1985. if (NULL != (child = _synctex_new_void_hbox(scanner))
  1986. && NULL != (info = SYNCTEX_INFO(child))) {
  1987. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  1988. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  1989. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  1990. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  1991. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  1992. || SYNCTEX_DECODE_FAILED(SYNCTEX_HEIGHT_IDX)
  1993. || SYNCTEX_DECODE_FAILED(SYNCTEX_DEPTH_IDX)
  1994. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  1995. _synctex_error("Bad void hbox record.");
  1996. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  1997. }
  1998. SYNCTEX_SET_CHILD(parent,child);
  1999. SYNCTEX_UPDATE_FRIEND(child);
  2000. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2001. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child)+SYNCTEX_ABS_WIDTH(child),SYNCTEX_VERT(child));
  2002. goto sibling_loop;
  2003. } else {
  2004. _synctex_error("Can't create void hbox record.");
  2005. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2006. }
  2007. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_KERN) {
  2008. ++SYNCTEX_CUR;
  2009. if (NULL != (child = _synctex_new_kern(scanner))
  2010. && NULL != (info = SYNCTEX_INFO(child))) {
  2011. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2012. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2013. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2014. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2015. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  2016. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2017. _synctex_error("Bad kern record.");
  2018. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2019. }
  2020. SYNCTEX_SET_CHILD(parent,child);
  2021. SYNCTEX_UPDATE_FRIEND(child);
  2022. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2023. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child)-SYNCTEX_WIDTH(child),SYNCTEX_VERT(child));
  2024. goto sibling_loop;
  2025. } else {
  2026. _synctex_error("Can't create kern record.");
  2027. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2028. }
  2029. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_GLUE) {
  2030. ++SYNCTEX_CUR;
  2031. if (NULL != (child = _synctex_new_glue(scanner))
  2032. && NULL != (info = SYNCTEX_INFO(child))) {
  2033. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2034. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2035. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2036. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2037. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2038. _synctex_error("Bad glue record.");
  2039. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2040. }
  2041. SYNCTEX_SET_CHILD(parent,child);
  2042. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2043. SYNCTEX_UPDATE_FRIEND(child);
  2044. goto sibling_loop;
  2045. } else {
  2046. _synctex_error("Can't create glue record.");
  2047. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2048. }
  2049. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_MATH) {
  2050. ++SYNCTEX_CUR;
  2051. if (NULL != (child = _synctex_new_math(scanner))
  2052. && NULL != (info = SYNCTEX_INFO(child))) {
  2053. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2054. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2055. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2056. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2057. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2058. _synctex_error("Bad math record.");
  2059. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2060. }
  2061. SYNCTEX_SET_CHILD(parent,child);
  2062. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2063. SYNCTEX_UPDATE_FRIEND(child);
  2064. goto sibling_loop;
  2065. } else {
  2066. _synctex_error("Can't create math record.");
  2067. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2068. }
  2069. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_BOUNDARY) {
  2070. ++SYNCTEX_CUR;
  2071. if (NULL != (child = _synctex_new_boundary(scanner))
  2072. && NULL != (info = SYNCTEX_INFO(child))) {
  2073. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2074. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2075. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2076. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2077. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2078. _synctex_error("Bad boundary record.");
  2079. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2080. }
  2081. SYNCTEX_SET_CHILD(parent,child);
  2082. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2083. SYNCTEX_UPDATE_FRIEND(child);
  2084. goto sibling_loop;
  2085. } else {
  2086. _synctex_error("Can't create math record.");
  2087. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2088. }
  2089. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_END_SHEET) {
  2090. goto scan_teehs;
  2091. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_SHEET) {
  2092. /* Addendum to version 1.10 to manage nested sheets */
  2093. ++SYNCTEX_CUR;
  2094. if (_synctex_scan_nested_sheet(scanner)<SYNCTEX_STATUS_OK) {
  2095. _synctex_error("Unexpected nested sheet.");
  2096. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2097. }
  2098. goto child_loop;
  2099. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_ANCHOR) {
  2100. goto scan_anchor;
  2101. } else {
  2102. /* _synctex_error("Ignored record %c\n",*SYNCTEX_CUR); */
  2103. ++SYNCTEX_CUR;
  2104. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2105. _synctex_error("Unexpected end.");
  2106. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2107. }
  2108. goto child_loop;
  2109. }
  2110. } else {
  2111. available = 1;
  2112. status = _synctex_buffer_get_available_size(scanner,&available);
  2113. if (status<SYNCTEX_STATUS_OK && available>0){
  2114. _synctex_error("Uncomplete sheet(0)");
  2115. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2116. } else {
  2117. goto child_loop;
  2118. }
  2119. }
  2120. _synctex_bail();
  2121. /* The vertical loop means that we are on the same level, for example when we just ended a box.
  2122. * If a node is created now, it will be a sibling of the current node, sharing the same parent. */
  2123. sibling_loop:
  2124. if (SYNCTEX_CUR<SYNCTEX_END) {
  2125. if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_VBOX) {
  2126. ++SYNCTEX_CUR;
  2127. if (NULL != (sibling = _synctex_new_vbox(scanner))
  2128. && NULL != (info = SYNCTEX_INFO(sibling))) {
  2129. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2130. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2131. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2132. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2133. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  2134. || SYNCTEX_DECODE_FAILED(SYNCTEX_HEIGHT_IDX)
  2135. || SYNCTEX_DECODE_FAILED(SYNCTEX_DEPTH_IDX)
  2136. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2137. _synctex_error("Bad vbox record (2).");
  2138. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2139. }
  2140. SYNCTEX_SET_SIBLING(child,sibling);
  2141. parent = sibling;
  2142. child = NULL;
  2143. goto child_loop;
  2144. } else {
  2145. _synctex_error("Can't create vbox record (2).");
  2146. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2147. }
  2148. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_END_VBOX) {
  2149. goto scan_xobv;
  2150. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_BEGIN_HBOX) {
  2151. ++SYNCTEX_CUR;
  2152. if (NULL != (sibling = _synctex_new_hbox(scanner)) &&
  2153. NULL != (info = SYNCTEX_INFO(sibling))) {
  2154. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2155. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2156. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2157. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2158. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  2159. || SYNCTEX_DECODE_FAILED(SYNCTEX_HEIGHT_IDX)
  2160. || SYNCTEX_DECODE_FAILED(SYNCTEX_DEPTH_IDX)
  2161. || _synctex_setup_visible_box(sibling)<SYNCTEX_STATUS_OK
  2162. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2163. _synctex_error("Bad hbox record (2).");
  2164. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2165. }
  2166. SYNCTEX_SET_SIBLING(child,sibling);
  2167. child = sibling;
  2168. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2169. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child)+SYNCTEX_ABS_WIDTH(child),SYNCTEX_VERT(child));
  2170. parent = child;
  2171. child = NULL;
  2172. goto child_loop;
  2173. } else {
  2174. _synctex_error("Can't create hbox record (2).");
  2175. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2176. }
  2177. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_END_HBOX) {
  2178. goto scan_xobh;
  2179. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_VOID_VBOX) {
  2180. ++SYNCTEX_CUR;
  2181. if (NULL != (sibling = _synctex_new_void_vbox(scanner)) &&
  2182. NULL != (info = SYNCTEX_INFO(sibling))) {
  2183. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2184. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2185. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2186. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2187. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  2188. || SYNCTEX_DECODE_FAILED(SYNCTEX_HEIGHT_IDX)
  2189. || SYNCTEX_DECODE_FAILED(SYNCTEX_DEPTH_IDX)
  2190. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2191. _synctex_error("Bad void vbox record (2).");
  2192. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2193. }
  2194. SYNCTEX_SET_SIBLING(child,sibling);
  2195. child = sibling;
  2196. SYNCTEX_UPDATE_FRIEND(child);
  2197. goto sibling_loop;
  2198. } else {
  2199. _synctex_error("can't create void vbox record (2).");
  2200. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2201. }
  2202. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_VOID_HBOX) {
  2203. ++SYNCTEX_CUR;
  2204. if (NULL != (sibling = _synctex_new_void_hbox(scanner)) &&
  2205. NULL != (info = SYNCTEX_INFO(sibling))) {
  2206. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2207. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2208. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2209. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2210. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  2211. || SYNCTEX_DECODE_FAILED(SYNCTEX_HEIGHT_IDX)
  2212. || SYNCTEX_DECODE_FAILED(SYNCTEX_DEPTH_IDX)
  2213. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2214. _synctex_error("Bad void hbox record (2).");
  2215. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2216. }
  2217. SYNCTEX_SET_SIBLING(child,sibling);
  2218. child = sibling;
  2219. SYNCTEX_UPDATE_FRIEND(child);
  2220. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2221. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child)+SYNCTEX_ABS_WIDTH(child),SYNCTEX_VERT(child));
  2222. goto sibling_loop;
  2223. } else {
  2224. _synctex_error("can't create void hbox record (2).");
  2225. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2226. }
  2227. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_KERN) {
  2228. ++SYNCTEX_CUR;
  2229. if (NULL != (sibling = _synctex_new_kern(scanner))
  2230. && NULL != (info = SYNCTEX_INFO(sibling))) {
  2231. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2232. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2233. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2234. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2235. || SYNCTEX_DECODE_FAILED(SYNCTEX_WIDTH_IDX)
  2236. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2237. _synctex_error("Bad kern record (2).");
  2238. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2239. }
  2240. SYNCTEX_SET_SIBLING(child,sibling);
  2241. child = sibling;
  2242. SYNCTEX_UPDATE_FRIEND(child);
  2243. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2244. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child)-SYNCTEX_WIDTH(child),SYNCTEX_VERT(child));
  2245. goto sibling_loop;
  2246. } else {
  2247. _synctex_error("Can't create kern record (2).");
  2248. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2249. }
  2250. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_GLUE) {
  2251. ++SYNCTEX_CUR;
  2252. if (NULL != (sibling = _synctex_new_glue(scanner))
  2253. && NULL != (info = SYNCTEX_INFO(sibling))) {
  2254. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2255. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2256. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2257. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2258. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2259. _synctex_error("Bad glue record (2).");
  2260. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2261. }
  2262. SYNCTEX_SET_SIBLING(child,sibling);
  2263. child = sibling;
  2264. SYNCTEX_UPDATE_FRIEND(child);
  2265. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2266. goto sibling_loop;
  2267. } else {
  2268. _synctex_error("Can't create glue record (2).");
  2269. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2270. }
  2271. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_MATH) {
  2272. ++SYNCTEX_CUR;
  2273. if (NULL != (sibling = _synctex_new_math(scanner))
  2274. && NULL != (info = SYNCTEX_INFO(sibling))) {
  2275. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2276. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2277. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2278. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2279. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2280. _synctex_error("Bad math record (2).");
  2281. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2282. }
  2283. SYNCTEX_SET_SIBLING(child,sibling);
  2284. child = sibling;
  2285. SYNCTEX_UPDATE_FRIEND(child);
  2286. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2287. goto sibling_loop;
  2288. } else {
  2289. _synctex_error("Can't create math record (2).");
  2290. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2291. }
  2292. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_BOUNDARY) {
  2293. ++SYNCTEX_CUR;
  2294. if (NULL != (sibling = _synctex_new_boundary(scanner))
  2295. && NULL != (info = SYNCTEX_INFO(sibling))) {
  2296. if (SYNCTEX_DECODE_FAILED(SYNCTEX_TAG_IDX)
  2297. || SYNCTEX_DECODE_FAILED(SYNCTEX_LINE_IDX)
  2298. || SYNCTEX_DECODE_FAILED(SYNCTEX_HORIZ_IDX)
  2299. || SYNCTEX_DECODE_FAILED(SYNCTEX_VERT_IDX)
  2300. || _synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2301. _synctex_error("Bad boundary record (2).");
  2302. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2303. }
  2304. SYNCTEX_SET_SIBLING(child,sibling);
  2305. child = sibling;
  2306. SYNCTEX_UPDATE_FRIEND(child);
  2307. _synctex_horiz_box_setup_visible(parent,SYNCTEX_HORIZ(child),SYNCTEX_VERT(child));
  2308. goto sibling_loop;
  2309. } else {
  2310. _synctex_error("Can't create boundary record (2).");
  2311. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2312. }
  2313. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_END_SHEET) {
  2314. goto scan_teehs;
  2315. } else if (*SYNCTEX_CUR == SYNCTEX_CHAR_ANCHOR) {
  2316. ++SYNCTEX_CUR;
  2317. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2318. _synctex_error("Missing anchor (2).");
  2319. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2320. }
  2321. goto sibling_loop;
  2322. } else {
  2323. ++SYNCTEX_CUR;
  2324. /* _synctex_error("Ignored record %c(2)\n",*SYNCTEX_CUR); */
  2325. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2326. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2327. }
  2328. goto sibling_loop;
  2329. }
  2330. } else {
  2331. available = 1;
  2332. status = _synctex_buffer_get_available_size(scanner,&available);
  2333. if (status<SYNCTEX_STATUS_OK && available>0){
  2334. goto sibling_loop;
  2335. } else {
  2336. _synctex_error("Uncomplete sheet(2)");
  2337. SYNCTEX_RETURN(SYNCTEX_STATUS_ERROR);
  2338. }
  2339. }
  2340. # undef SYNCTEX_DECODE_FAILED
  2341. }
  2342. /* Used when parsing the synctex file
  2343. */
  2344. synctex_status_t _synctex_scan_content(synctex_scanner_t scanner) {
  2345. synctex_node_t sheet = NULL;
  2346. synctex_status_t status = 0;
  2347. if (NULL == scanner) {
  2348. return SYNCTEX_STATUS_BAD_ARGUMENT;
  2349. }
  2350. /* set up the lists of friends */
  2351. if (NULL == scanner->lists_of_friends) {
  2352. scanner->number_of_lists = 1024;
  2353. scanner->lists_of_friends = (synctex_node_t *)_synctex_malloc(scanner->number_of_lists*sizeof(synctex_node_t));
  2354. if (NULL == scanner->lists_of_friends) {
  2355. _synctex_error("malloc:2");
  2356. return SYNCTEX_STATUS_ERROR;
  2357. }
  2358. }
  2359. /* Find where this section starts */
  2360. content_not_found:
  2361. status = _synctex_match_string(scanner,"Content:");
  2362. if (status<SYNCTEX_STATUS_EOF) {
  2363. return status;
  2364. }
  2365. if (_synctex_next_line(scanner)<SYNCTEX_STATUS_OK) {
  2366. _synctex_error("Uncomplete Content.");
  2367. return SYNCTEX_STATUS_ERROR;
  2368. }
  2369. if (status == SYNCTEX_STATUS_NOT_OK) {
  2370. goto content_not_found;
  2371. }
  2372. next_sheet:
  2373. if (*SYNCTEX_CUR != SYNCTEX_CHAR_BEGIN_SHEET) {
  2374. status = _synctex_scan_postamble(scanner);
  2375. if (status < SYNCTEX_STATUS_EOF) {
  2376. _synctex_error("Bad content.");
  2377. return status;
  2378. }
  2379. if (status<SYNCTEX_STATUS_OK) {
  2380. status = _synctex_next_line(scanner);
  2381. if (status < SYNCTEX_STATUS_OK) {
  2382. _synctex_error("Bad content.");
  2383. return status;
  2384. }
  2385. goto next_sheet;
  2386. }
  2387. return SYNCTEX_STATUS_OK;
  2388. }
  2389. ++SYNCTEX_CUR;
  2390. /* Create a new sheet node */
  2391. sheet = _synctex_new_sheet(scanner);
  2392. status = _synctex_decode_int(scanner,&(SYNCTEX_PAGE(sheet)));
  2393. if (status<SYNCTEX_STATUS_OK) {
  2394. _synctex_error("Missing sheet number.");
  2395. bail:
  2396. SYNCTEX_FREE(sheet);
  2397. return SYNCTEX_STATUS_ERROR;
  2398. }
  2399. status = _synctex_next_line(scanner);
  2400. if (status<SYNCTEX_STATUS_OK) {
  2401. _synctex_error("Uncomplete file.");
  2402. goto bail;
  2403. }
  2404. status = _synctex_scan_sheet(scanner,sheet);
  2405. if (status<SYNCTEX_STATUS_OK) {
  2406. _synctex_error("Bad sheet content.");
  2407. goto bail;
  2408. }
  2409. SYNCTEX_SET_SIBLING(sheet,scanner->sheet);
  2410. scanner->sheet = sheet;
  2411. sheet = NULL;
  2412. /* Now read the list of Inputs between 2 sheets. */
  2413. do {
  2414. status = _synctex_scan_input(scanner);
  2415. if (status<SYNCTEX_STATUS_EOF) {
  2416. _synctex_error("Bad input section.");
  2417. goto bail;
  2418. }
  2419. }
  2420. while(status >= SYNCTEX_STATUS_OK);
  2421. goto next_sheet;
  2422. }
  2423. int _synctex_open(const char * output, const char * build_directory, char ** synctex_name_ref, gzFile * file_ref, synctex_bool_t add_quotes, synctex_io_mode_t * io_modeRef);
  2424. /* Where the synctex scanner is created. */
  2425. synctex_scanner_t synctex_scanner_new_with_output_file(const char * output, const char * build_directory, int parse) {
  2426. gzFile file = NULL;
  2427. char * synctex = NULL;
  2428. synctex_scanner_t scanner = NULL;
  2429. synctex_io_mode_t io_mode = 0;
  2430. /* Here we assume that int are smaller than void * */
  2431. if (sizeof(int)>sizeof(void*)) {
  2432. _synctex_error("INTERNAL INCONSISTENCY: int's are unexpectedly bigger than pointers, bailing out.");
  2433. return NULL;
  2434. }
  2435. /* We ensure that SYNCTEX_BUFFER_SIZE < UINT_MAX, I don't know if it makes sense... */
  2436. if (SYNCTEX_BUFFER_SIZE >= UINT_MAX) {
  2437. _synctex_error("SyncTeX BUG: Internal inconsistency, bad SYNCTEX_BUFFER_SIZE (1)");
  2438. return NULL;
  2439. }
  2440. /* for integers: */
  2441. if (SYNCTEX_BUFFER_SIZE < SYNCTEX_BUFFER_MIN_SIZE) {
  2442. _synctex_error("SyncTeX BUG: Internal inconsistency, bad SYNCTEX_BUFFER_SIZE (2)");
  2443. return NULL;
  2444. }
  2445. /* now open the synctex file */
  2446. if (_synctex_open(output,build_directory,&synctex,&file,synctex_ADD_QUOTES,&io_mode) || !file) {
  2447. if (_synctex_open(output,build_directory,&synctex,&file,synctex_DONT_ADD_QUOTES,&io_mode) || !file) {
  2448. return NULL;
  2449. }
  2450. }
  2451. scanner = (synctex_scanner_t)_synctex_malloc(sizeof(_synctex_scanner_t));
  2452. if (NULL == scanner) {
  2453. _synctex_error("SyncTeX: malloc problem");
  2454. free(synctex);
  2455. gzclose(file);
  2456. return NULL;
  2457. }
  2458. /* make a private copy of output for the scanner */
  2459. if (NULL == (scanner->output = (char *)malloc(strlen(output)+1))){
  2460. _synctex_error("! synctex_scanner_new_with_output_file: Memory problem (2), scanner's output is not reliable.");
  2461. } else if (scanner->output != strcpy(scanner->output,output)) {
  2462. _synctex_error("! synctex_scanner_new_with_output_file: Copy problem, scanner's output is not reliable.");
  2463. }
  2464. scanner->synctex = synctex;/* Now the scanner owns synctex */
  2465. SYNCTEX_FILE = file;
  2466. return parse? synctex_scanner_parse(scanner):scanner;
  2467. }
  2468. int __synctex_open(const char * output, char ** synctex_name_ref, gzFile * file_ref, synctex_bool_t add_quotes, synctex_io_mode_t * io_mode_ref);
  2469. /* This functions opens the file at the "output" given location.
  2470. * It manages the problem of quoted filenames that appear with pdftex and filenames containing the space character.
  2471. * In TeXLive 2008, the synctex file created with pdftex did contain unexpected quotes.
  2472. * This function will remove them if possible.
  2473. * All the reference arguments will take a value on return. They must be non NULL.
  2474. * 0 on success, non 0 on error. */
  2475. int __synctex_open(const char * output, char ** synctex_name_ref, gzFile * file_ref, synctex_bool_t add_quotes, synctex_io_mode_t * io_mode_ref) {
  2476. if (synctex_name_ref && file_ref && io_mode_ref) {
  2477. /* 1 local variables that uses dynamic memory */
  2478. char * synctex_name = NULL;
  2479. gzFile the_file = NULL;
  2480. char * quoteless_synctex_name = NULL;
  2481. size_t size = 0;
  2482. synctex_io_mode_t io_mode = *io_mode_ref;
  2483. const char * mode = _synctex_get_io_mode_name(io_mode);
  2484. /* now create the synctex file name */
  2485. size = strlen(output)+strlen(synctex_suffix)+strlen(synctex_suffix_gz)+1;
  2486. synctex_name = (char *)malloc(size);
  2487. if (NULL == synctex_name) {
  2488. _synctex_error("! __synctex_open: Memory problem (1)\n");
  2489. return 1;
  2490. }
  2491. /* we have reserved for synctex enough memory to copy output (including its 2 eventual quotes), both suffices,
  2492. * including the terminating character. size is free now. */
  2493. if (synctex_name != strcpy(synctex_name,output)) {
  2494. _synctex_error("! __synctex_open: Copy problem\n");
  2495. return_on_error:
  2496. free(synctex_name);
  2497. free(quoteless_synctex_name);
  2498. return 2;
  2499. }
  2500. /* remove the last path extension if any */
  2501. _synctex_strip_last_path_extension(synctex_name);
  2502. if (!strlen(synctex_name)) {
  2503. goto return_on_error;
  2504. }
  2505. /* now insert quotes. */
  2506. if (add_quotes) {
  2507. char * quoted = NULL;
  2508. if (_synctex_copy_with_quoting_last_path_component(synctex_name,&quoted,size) || (NULL == quoted)) {
  2509. /* There was an error or quoting does not make sense: */
  2510. goto return_on_error;
  2511. }
  2512. quoteless_synctex_name = synctex_name;
  2513. synctex_name = quoted;
  2514. }
  2515. /* Now add to synctex_name the first path extension. */
  2516. if (synctex_name != strcat(synctex_name,synctex_suffix)){
  2517. _synctex_error("! __synctex_open: Concatenation problem (can't add suffix '%s')\n",synctex_suffix);
  2518. goto return_on_error;
  2519. }
  2520. /* Add to quoteless_synctex_name as well, if relevant. */
  2521. if (quoteless_synctex_name && (quoteless_synctex_name != strcat(quoteless_synctex_name,synctex_suffix))){
  2522. free(quoteless_synctex_name);
  2523. quoteless_synctex_name = NULL;
  2524. }
  2525. if (NULL == (the_file = gzopen(synctex_name,mode))) {
  2526. /* Could not open this file */
  2527. if (errno != ENOENT) {
  2528. /* The file does exist, this is a lower level error, I can't do anything. */
  2529. _synctex_error("SyncTeX: could not open %s, error %i\n",synctex_name,errno);
  2530. goto return_on_error;
  2531. }
  2532. /* Apparently, there is no uncompressed synctex file. Try the compressed version */
  2533. if (synctex_name != strcat(synctex_name,synctex_suffix_gz)){
  2534. _synctex_error("! __synctex_open: Concatenation problem (can't add suffix '%s')\n",synctex_suffix_gz);
  2535. goto return_on_error;
  2536. }
  2537. io_mode |= synctex_io_gz_mask;
  2538. mode = _synctex_get_io_mode_name(io_mode); /* the file is a compressed and is a binary file, this caused errors on Windows */
  2539. /* Add the suffix to the quoteless_synctex_name as well. */
  2540. if (quoteless_synctex_name && (quoteless_synctex_name != strcat(quoteless_synctex_name,synctex_suffix_gz))){
  2541. free(quoteless_synctex_name);
  2542. quoteless_synctex_name = NULL;
  2543. }
  2544. if (NULL == (the_file = gzopen(synctex_name,mode))) {
  2545. /* Could not open this file */
  2546. if (errno != ENOENT) {
  2547. /* The file does exist, this is a lower level error, I can't do anything. */
  2548. _synctex_error("SyncTeX: could not open %s, error %i\n",synctex_name,errno);
  2549. }
  2550. goto return_on_error;
  2551. }
  2552. }
  2553. /* At this point, the file is properly open.
  2554. * If we are in the add_quotes mode, we change the file name by removing the quotes. */
  2555. if (quoteless_synctex_name) {
  2556. gzclose(the_file);
  2557. if (rename(synctex_name,quoteless_synctex_name)) {
  2558. _synctex_error("SyncTeX: could not rename %s to %s, error %i\n",synctex_name,quoteless_synctex_name,errno);
  2559. /* We could not rename, reopen the file with the quoted name. */
  2560. if (NULL == (the_file = gzopen(synctex_name,mode))) {
  2561. /* No luck, could not re open this file, something has happened meanwhile */
  2562. if (errno != ENOENT) {
  2563. /* The file does not exist any more, it has certainly be removed somehow
  2564. * this is a lower level error, I can't do anything. */
  2565. _synctex_error("SyncTeX: could not open again %s, error %i\n",synctex_name,errno);
  2566. }
  2567. goto return_on_error;
  2568. }
  2569. } else {
  2570. /* The file has been successfully renamed */
  2571. if (NULL == (the_file = gzopen(quoteless_synctex_name,mode))) {
  2572. /* Could not open this file */
  2573. if (errno != ENOENT) {
  2574. /* The file does exist, this is a lower level error, I can't do anything. */
  2575. _synctex_error("SyncTeX: could not open renamed %s, error %i\n",quoteless_synctex_name,errno);
  2576. }
  2577. goto return_on_error;
  2578. }
  2579. /* The quote free file name should replace the old one:*/
  2580. free(synctex_name);
  2581. synctex_name = quoteless_synctex_name;
  2582. quoteless_synctex_name = NULL;
  2583. }
  2584. }
  2585. /* The operation is successfull, return the arguments by value. */
  2586. * file_ref = the_file;
  2587. * io_mode_ref = io_mode;
  2588. * synctex_name_ref = synctex_name;
  2589. return 0;
  2590. }
  2591. return 3; /* Bad parameter. */
  2592. }
  2593. /* Opens the ouput file, taking into account the eventual build_directory.
  2594. * 0 on success, non 0 on error. */
  2595. int _synctex_open(const char * output, const char * build_directory, char ** synctex_name_ref, gzFile * file_ref, synctex_bool_t add_quotes, synctex_io_mode_t * io_mode_ref) {
  2596. # define synctex_name (*synctex_name_ref)
  2597. # define the_file (*file_ref)
  2598. int result = __synctex_open(output,synctex_name_ref,file_ref,add_quotes,io_mode_ref);
  2599. if ((result || !*file_ref) && build_directory && strlen(build_directory)) {
  2600. char * build_output;
  2601. const char *lpc;
  2602. size_t size;
  2603. synctex_bool_t is_absolute;
  2604. build_output = NULL;
  2605. lpc = _synctex_last_path_component(output);
  2606. size = strlen(build_directory)+strlen(lpc)+2; /* One for the '/' and one for the '\0'. */
  2607. is_absolute = _synctex_path_is_absolute(build_directory);
  2608. if (!is_absolute) {
  2609. size += strlen(output);
  2610. }
  2611. if ((build_output = (char *)malloc(size))) {
  2612. if (is_absolute) {
  2613. build_output[0] = '\0';
  2614. } else {
  2615. if (build_output != strcpy(build_output,output)) {
  2616. return -4;
  2617. }
  2618. build_output[lpc-output]='\0';
  2619. }
  2620. if (build_output == strcat(build_output,build_directory)) {
  2621. /* Append a path separator if necessary. */
  2622. if (!SYNCTEX_IS_PATH_SEPARATOR(build_output[strlen(build_directory)-1])) {
  2623. if (build_output != strcat(build_output,"/")) {
  2624. return -2;
  2625. }
  2626. }
  2627. /* Append the last path component of the output. */
  2628. if (build_output != strcat(build_output,lpc)) {
  2629. return -3;
  2630. }
  2631. return __synctex_open(build_output,synctex_name_ref,file_ref,add_quotes,io_mode_ref);
  2632. }
  2633. }
  2634. return -1;
  2635. }
  2636. return result;
  2637. # undef synctex_name
  2638. # undef the_file
  2639. }
  2640. /* The scanner destructor
  2641. */
  2642. void synctex_scanner_free(synctex_scanner_t scanner) {
  2643. if (NULL == scanner) {
  2644. return;
  2645. }
  2646. if (SYNCTEX_FILE) {
  2647. gzclose(SYNCTEX_FILE);
  2648. SYNCTEX_FILE = NULL;
  2649. }
  2650. SYNCTEX_FREE(scanner->sheet);
  2651. SYNCTEX_FREE(scanner->input);
  2652. free(SYNCTEX_START);
  2653. free(scanner->output_fmt);
  2654. free(scanner->output);
  2655. free(scanner->synctex);
  2656. free(scanner->lists_of_friends);
  2657. free(scanner);
  2658. }
  2659. /* Where the synctex scanner parses the contents of the file. */
  2660. synctex_scanner_t synctex_scanner_parse(synctex_scanner_t scanner) {
  2661. synctex_status_t status = 0;
  2662. if (!scanner || scanner->flags.has_parsed) {
  2663. return scanner;
  2664. }
  2665. scanner->flags.has_parsed=1;
  2666. scanner->pre_magnification = 1000;
  2667. scanner->pre_unit = 8192;
  2668. scanner->pre_x_offset = scanner->pre_y_offset = 578;
  2669. /* initialize the offset with a fake unprobable value,
  2670. * If there is a post scriptum section, this value will be overriden by the real life value */
  2671. scanner->x_offset = scanner->y_offset = 6.027e23f;
  2672. scanner->class[synctex_node_type_sheet] = synctex_class_sheet;
  2673. scanner->class[synctex_node_type_input] = synctex_class_input;
  2674. (scanner->class[synctex_node_type_input]).scanner = scanner;
  2675. (scanner->class[synctex_node_type_sheet]).scanner = scanner;
  2676. scanner->class[synctex_node_type_vbox] = synctex_class_vbox;
  2677. (scanner->class[synctex_node_type_vbox]).scanner = scanner;
  2678. scanner->class[synctex_node_type_void_vbox] = synctex_class_void_vbox;
  2679. (scanner->class[synctex_node_type_void_vbox]).scanner = scanner;
  2680. scanner->class[synctex_node_type_hbox] = synctex_class_hbox;
  2681. (scanner->class[synctex_node_type_hbox]).scanner = scanner;
  2682. scanner->class[synctex_node_type_void_hbox] = synctex_class_void_hbox;
  2683. (scanner->class[synctex_node_type_void_hbox]).scanner = scanner;
  2684. scanner->class[synctex_node_type_kern] = synctex_class_kern;
  2685. (scanner->class[synctex_node_type_kern]).scanner = scanner;
  2686. scanner->class[synctex_node_type_glue] = synctex_class_glue;
  2687. (scanner->class[synctex_node_type_glue]).scanner = scanner;
  2688. scanner->class[synctex_node_type_math] = synctex_class_math;
  2689. (scanner->class[synctex_node_type_math]).scanner = scanner;
  2690. scanner->class[synctex_node_type_boundary] = synctex_class_boundary;
  2691. (scanner->class[synctex_node_type_boundary]).scanner = scanner;
  2692. SYNCTEX_START = (char *)malloc(SYNCTEX_BUFFER_SIZE+1); /* one more character for null termination */
  2693. if (NULL == SYNCTEX_START) {
  2694. _synctex_error("SyncTeX: malloc error");
  2695. synctex_scanner_free(scanner);
  2696. return NULL;
  2697. }
  2698. SYNCTEX_END = SYNCTEX_START+SYNCTEX_BUFFER_SIZE;
  2699. /* SYNCTEX_END always points to a null terminating character.
  2700. * Maybe there is another null terminating character between SYNCTEX_CUR and SYNCTEX_END-1.
  2701. * At least, we are sure that SYNCTEX_CUR points to a string covering a valid part of the memory. */
  2702. *SYNCTEX_END = '\0';
  2703. SYNCTEX_CUR = SYNCTEX_END;
  2704. status = _synctex_scan_preamble(scanner);
  2705. if (status<SYNCTEX_STATUS_OK) {
  2706. _synctex_error("SyncTeX Error: Bad preamble\n");
  2707. bailey:
  2708. synctex_scanner_free(scanner);
  2709. return NULL;
  2710. }
  2711. status = _synctex_scan_content(scanner);
  2712. if (status<SYNCTEX_STATUS_OK) {
  2713. _synctex_error("SyncTeX Error: Bad content\n");
  2714. goto bailey;
  2715. }
  2716. /* Everything is finished, free the buffer, close the file */
  2717. free((void *)SYNCTEX_START);
  2718. SYNCTEX_START = SYNCTEX_CUR = SYNCTEX_END = NULL;
  2719. gzclose(SYNCTEX_FILE);
  2720. SYNCTEX_FILE = NULL;
  2721. /* Final tuning: set the default values for various parameters */
  2722. /* 1 pre_unit = (scanner->pre_unit)/65536 pt = (scanner->pre_unit)/65781.76 bp
  2723. * 1 pt = 65536 sp */
  2724. if (scanner->pre_unit<=0) {
  2725. scanner->pre_unit = 8192;
  2726. }
  2727. if (scanner->pre_magnification<=0) {
  2728. scanner->pre_magnification = 1000;
  2729. }
  2730. if (scanner->unit <= 0) {
  2731. /* no post magnification */
  2732. scanner->unit = scanner->pre_unit / 65781.76;/* 65781.76 or 65536.0*/
  2733. } else {
  2734. /* post magnification */
  2735. scanner->unit *= scanner->pre_unit / 65781.76;
  2736. }
  2737. scanner->unit *= scanner->pre_magnification / 1000.0;
  2738. if (scanner->x_offset > 6e23) {
  2739. /* no post offset */
  2740. scanner->x_offset = scanner->pre_x_offset * (scanner->pre_unit / 65781.76);
  2741. scanner->y_offset = scanner->pre_y_offset * (scanner->pre_unit / 65781.76);
  2742. } else {
  2743. /* post offset */
  2744. scanner->x_offset /= 65781.76f;
  2745. scanner->y_offset /= 65781.76f;
  2746. }
  2747. return scanner;
  2748. #undef SYNCTEX_FILE
  2749. }
  2750. /* Scanner accessors.
  2751. */
  2752. int synctex_scanner_pre_x_offset(synctex_scanner_t scanner){
  2753. return scanner?scanner->pre_x_offset:0;
  2754. }
  2755. int synctex_scanner_pre_y_offset(synctex_scanner_t scanner){
  2756. return scanner?scanner->pre_y_offset:0;
  2757. }
  2758. int synctex_scanner_x_offset(synctex_scanner_t scanner){
  2759. return scanner?scanner->x_offset:0;
  2760. }
  2761. int synctex_scanner_y_offset(synctex_scanner_t scanner){
  2762. return scanner?scanner->y_offset:0;
  2763. }
  2764. float synctex_scanner_magnification(synctex_scanner_t scanner){
  2765. return scanner?scanner->unit:1;
  2766. }
  2767. void synctex_scanner_display(synctex_scanner_t scanner) {
  2768. if (NULL == scanner) {
  2769. return;
  2770. }
  2771. printf("The scanner:\noutput:%s\noutput_fmt:%s\nversion:%i\n",scanner->output,scanner->output_fmt,scanner->version);
  2772. printf("pre_unit:%i\nx_offset:%i\ny_offset:%i\n",scanner->pre_unit,scanner->pre_x_offset,scanner->pre_y_offset);
  2773. printf("count:%i\npost_magnification:%f\npost_x_offset:%f\npost_y_offset:%f\n",
  2774. scanner->count,scanner->unit,scanner->x_offset,scanner->y_offset);
  2775. printf("The input:\n");
  2776. SYNCTEX_DISPLAY(scanner->input);
  2777. if (scanner->count<1000) {
  2778. printf("The sheets:\n");
  2779. SYNCTEX_DISPLAY(scanner->sheet);
  2780. printf("The friends:\n");
  2781. if (scanner->lists_of_friends) {
  2782. int i = scanner->number_of_lists;
  2783. synctex_node_t node;
  2784. while(i--) {
  2785. printf("Friend index:%i\n",i);
  2786. node = (scanner->lists_of_friends)[i];
  2787. while(node) {
  2788. printf("%s:%i,%i\n",
  2789. synctex_node_isa(node),
  2790. SYNCTEX_TAG(node),
  2791. SYNCTEX_LINE(node)
  2792. );
  2793. node = SYNCTEX_FRIEND(node);
  2794. }
  2795. }
  2796. }
  2797. } else {
  2798. printf("SyncTeX Warning: Too many objects\n");
  2799. }
  2800. }
  2801. /* Public*/
  2802. const char * synctex_scanner_get_name(synctex_scanner_t scanner,int tag) {
  2803. synctex_node_t input = NULL;
  2804. if (NULL == scanner) {
  2805. return NULL;
  2806. }
  2807. input = scanner->input;
  2808. do {
  2809. if (tag == SYNCTEX_TAG(input)) {
  2810. return (SYNCTEX_NAME(input));
  2811. }
  2812. } while((input = SYNCTEX_SIBLING(input)) != NULL);
  2813. return NULL;
  2814. }
  2815. int _synctex_scanner_get_tag(synctex_scanner_t scanner,const char * name);
  2816. int _synctex_scanner_get_tag(synctex_scanner_t scanner,const char * name) {
  2817. synctex_node_t input = NULL;
  2818. if (NULL == scanner) {
  2819. return 0;
  2820. }
  2821. input = scanner->input;
  2822. do {
  2823. if (_synctex_is_equivalent_file_name(name,(SYNCTEX_NAME(input)))) {
  2824. return SYNCTEX_TAG(input);
  2825. }
  2826. } while((input = SYNCTEX_SIBLING(input)) != NULL);
  2827. return 0;
  2828. }
  2829. int synctex_scanner_get_tag(synctex_scanner_t scanner,const char * name) {
  2830. size_t char_index = strlen(name);
  2831. if ((scanner = synctex_scanner_parse(scanner)) && (0 < char_index)) {
  2832. /* the name is not void */
  2833. char_index -= 1;
  2834. if (!SYNCTEX_IS_PATH_SEPARATOR(name[char_index])) {
  2835. /* the last character of name is not a path separator */
  2836. int result = _synctex_scanner_get_tag(scanner,name);
  2837. if (result) {
  2838. return result;
  2839. } else {
  2840. /* the given name was not the one known by TeX
  2841. * try a name relative to the enclosing directory of the scanner->output file */
  2842. const char * relative = name;
  2843. const char * ptr = scanner->output;
  2844. while((strlen(relative) > 0) && (strlen(ptr) > 0) && (*relative == *ptr))
  2845. {
  2846. relative += 1;
  2847. ptr += 1;
  2848. }
  2849. /* Find the last path separator before relative */
  2850. while(relative > name) {
  2851. if (SYNCTEX_IS_PATH_SEPARATOR(*(relative-1))) {
  2852. break;
  2853. }
  2854. relative -= 1;
  2855. }
  2856. if ((relative > name) && (result = _synctex_scanner_get_tag(scanner,relative))) {
  2857. return result;
  2858. }
  2859. if (SYNCTEX_IS_PATH_SEPARATOR(name[0])) {
  2860. /* No tag found for the given absolute name,
  2861. * Try each relative path starting from the shortest one */
  2862. while(0<char_index) {
  2863. char_index -= 1;
  2864. if (SYNCTEX_IS_PATH_SEPARATOR(name[char_index])
  2865. && (result = _synctex_scanner_get_tag(scanner,name+char_index+1))) {
  2866. return result;
  2867. }
  2868. }
  2869. }
  2870. }
  2871. return result;
  2872. }
  2873. }
  2874. return 0;
  2875. }
  2876. synctex_node_t synctex_scanner_input(synctex_scanner_t scanner) {
  2877. return scanner?scanner->input:NULL;
  2878. }
  2879. const char * synctex_scanner_get_output_fmt(synctex_scanner_t scanner) {
  2880. return NULL != scanner && scanner->output_fmt?scanner->output_fmt:"";
  2881. }
  2882. const char * synctex_scanner_get_output(synctex_scanner_t scanner) {
  2883. return NULL != scanner && scanner->output?scanner->output:"";
  2884. }
  2885. const char * synctex_scanner_get_synctex(synctex_scanner_t scanner) {
  2886. return NULL != scanner && scanner->synctex?scanner->synctex:"";
  2887. }
  2888. # ifdef SYNCTEX_NOTHING
  2889. # pragma mark -
  2890. # pragma mark Public node attributes
  2891. # endif
  2892. int synctex_node_h(synctex_node_t node){
  2893. if (!node) {
  2894. return 0;
  2895. }
  2896. return SYNCTEX_HORIZ(node);
  2897. }
  2898. int synctex_node_v(synctex_node_t node){
  2899. if (!node) {
  2900. return 0;
  2901. }
  2902. return SYNCTEX_VERT(node);
  2903. }
  2904. int synctex_node_width(synctex_node_t node){
  2905. if (!node) {
  2906. return 0;
  2907. }
  2908. return SYNCTEX_WIDTH(node);
  2909. }
  2910. int synctex_node_box_h(synctex_node_t node){
  2911. if (!node) {
  2912. return 0;
  2913. }
  2914. if (SYNCTEX_IS_BOX(node)) {
  2915. result:
  2916. return SYNCTEX_HORIZ(node);
  2917. }
  2918. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  2919. goto result;
  2920. }
  2921. return 0;
  2922. }
  2923. int synctex_node_box_v(synctex_node_t node){
  2924. if (!node) {
  2925. return 0;
  2926. }
  2927. if (SYNCTEX_IS_BOX(node)) {
  2928. result:
  2929. return SYNCTEX_VERT(node);
  2930. }
  2931. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  2932. goto result;
  2933. }
  2934. return 0;
  2935. }
  2936. int synctex_node_box_width(synctex_node_t node){
  2937. if (!node) {
  2938. return 0;
  2939. }
  2940. if (SYNCTEX_IS_BOX(node)) {
  2941. result:
  2942. return SYNCTEX_WIDTH(node);
  2943. }
  2944. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  2945. goto result;
  2946. }
  2947. return 0;
  2948. }
  2949. int synctex_node_box_height(synctex_node_t node){
  2950. if (!node) {
  2951. return 0;
  2952. }
  2953. if (SYNCTEX_IS_BOX(node)) {
  2954. result:
  2955. return SYNCTEX_HEIGHT(node);
  2956. }
  2957. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  2958. goto result;
  2959. }
  2960. return 0;
  2961. }
  2962. int synctex_node_box_depth(synctex_node_t node){
  2963. if (!node) {
  2964. return 0;
  2965. }
  2966. if (SYNCTEX_IS_BOX(node)) {
  2967. result:
  2968. return SYNCTEX_DEPTH(node);
  2969. }
  2970. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  2971. goto result;
  2972. }
  2973. return 0;
  2974. }
  2975. # ifdef SYNCTEX_NOTHING
  2976. # pragma mark -
  2977. # pragma mark Public node visible attributes
  2978. # endif
  2979. float synctex_node_visible_h(synctex_node_t node){
  2980. if (!node) {
  2981. return 0;
  2982. }
  2983. return SYNCTEX_HORIZ(node)*node->class->scanner->unit+node->class->scanner->x_offset;
  2984. }
  2985. float synctex_node_visible_v(synctex_node_t node){
  2986. if (!node) {
  2987. return 0;
  2988. }
  2989. return SYNCTEX_VERT(node)*node->class->scanner->unit+node->class->scanner->y_offset;
  2990. }
  2991. float synctex_node_visible_width(synctex_node_t node){
  2992. if (!node) {
  2993. return 0;
  2994. }
  2995. return SYNCTEX_WIDTH(node)*node->class->scanner->unit;
  2996. }
  2997. float synctex_node_box_visible_h(synctex_node_t node){
  2998. if (!node) {
  2999. return 0;
  3000. }
  3001. switch(node->class->type) {
  3002. case synctex_node_type_vbox:
  3003. case synctex_node_type_void_vbox:
  3004. case synctex_node_type_void_hbox:
  3005. return SYNCTEX_HORIZ(node)*node->class->scanner->unit+node->class->scanner->x_offset;
  3006. case synctex_node_type_hbox:
  3007. result:
  3008. return SYNCTEX_HORIZ_V(node)*node->class->scanner->unit+node->class->scanner->x_offset;
  3009. }
  3010. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  3011. goto result;
  3012. }
  3013. return 0;
  3014. }
  3015. float synctex_node_box_visible_v(synctex_node_t node){
  3016. if (!node) {
  3017. return 0;
  3018. }
  3019. switch(node->class->type) {
  3020. case synctex_node_type_vbox:
  3021. case synctex_node_type_void_vbox:
  3022. case synctex_node_type_void_hbox:
  3023. return SYNCTEX_VERT(node)*node->class->scanner->unit+node->class->scanner->y_offset;
  3024. case synctex_node_type_hbox:
  3025. result:
  3026. return SYNCTEX_VERT_V(node)*node->class->scanner->unit+node->class->scanner->y_offset;
  3027. }
  3028. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  3029. goto result;
  3030. }
  3031. return 0;
  3032. }
  3033. float synctex_node_box_visible_width(synctex_node_t node){
  3034. if (!node) {
  3035. return 0;
  3036. }
  3037. switch(node->class->type) {
  3038. case synctex_node_type_vbox:
  3039. case synctex_node_type_void_vbox:
  3040. case synctex_node_type_void_hbox:
  3041. return SYNCTEX_WIDTH(node)*node->class->scanner->unit;
  3042. case synctex_node_type_hbox:
  3043. result:
  3044. return SYNCTEX_WIDTH_V(node)*node->class->scanner->unit;
  3045. }
  3046. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  3047. goto result;
  3048. }
  3049. return 0;
  3050. }
  3051. float synctex_node_box_visible_height(synctex_node_t node){
  3052. if (!node) {
  3053. return 0;
  3054. }
  3055. switch(node->class->type) {
  3056. case synctex_node_type_vbox:
  3057. case synctex_node_type_void_vbox:
  3058. case synctex_node_type_void_hbox:
  3059. return SYNCTEX_HEIGHT(node)*node->class->scanner->unit;
  3060. case synctex_node_type_hbox:
  3061. result:
  3062. return SYNCTEX_HEIGHT_V(node)*node->class->scanner->unit;
  3063. }
  3064. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  3065. goto result;
  3066. }
  3067. return 0;
  3068. }
  3069. float synctex_node_box_visible_depth(synctex_node_t node){
  3070. if (!node) {
  3071. return 0;
  3072. }
  3073. switch(node->class->type) {
  3074. case synctex_node_type_vbox:
  3075. case synctex_node_type_void_vbox:
  3076. case synctex_node_type_void_hbox:
  3077. return SYNCTEX_DEPTH(node)*node->class->scanner->unit;
  3078. case synctex_node_type_hbox:
  3079. result:
  3080. return SYNCTEX_DEPTH_V(node)*node->class->scanner->unit;
  3081. }
  3082. if ((node = SYNCTEX_PARENT(node)) && (node->class->type != synctex_node_type_sheet)) {
  3083. goto result;
  3084. }
  3085. return 0;
  3086. }
  3087. # ifdef SYNCTEX_NOTHING
  3088. # pragma mark -
  3089. # pragma mark Other public node attributes
  3090. # endif
  3091. int synctex_node_page(synctex_node_t node){
  3092. synctex_node_t parent = NULL;
  3093. if (!node) {
  3094. return -1;
  3095. }
  3096. parent = SYNCTEX_PARENT(node);
  3097. while(parent) {
  3098. node = parent;
  3099. parent = SYNCTEX_PARENT(node);
  3100. }
  3101. if (node->class->type == synctex_node_type_sheet) {
  3102. return SYNCTEX_PAGE(node);
  3103. }
  3104. return -1;
  3105. }
  3106. int synctex_node_tag(synctex_node_t node) {
  3107. return node?SYNCTEX_TAG(node):-1;
  3108. }
  3109. int synctex_node_line(synctex_node_t node) {
  3110. return node?SYNCTEX_LINE(node):-1;
  3111. }
  3112. int synctex_node_column(synctex_node_t node) {
  3113. # ifdef __DARWIN_UNIX03
  3114. # pragma unused(node)
  3115. # endif
  3116. return -1;
  3117. }
  3118. # ifdef SYNCTEX_NOTHING
  3119. # pragma mark -
  3120. # pragma mark Sheet
  3121. # endif
  3122. synctex_node_t synctex_sheet_content(synctex_scanner_t scanner,int page) {
  3123. if (scanner) {
  3124. synctex_node_t sheet = scanner->sheet;
  3125. while(sheet) {
  3126. if (page == SYNCTEX_PAGE(sheet)) {
  3127. return SYNCTEX_CHILD(sheet);
  3128. }
  3129. sheet = SYNCTEX_SIBLING(sheet);
  3130. }
  3131. }
  3132. return NULL;
  3133. }
  3134. # ifdef SYNCTEX_NOTHING
  3135. # pragma mark -
  3136. # pragma mark Query
  3137. # endif
  3138. int synctex_display_query(synctex_scanner_t scanner,const char * name,int line,int column) {
  3139. # ifdef __DARWIN_UNIX03
  3140. # pragma unused(column)
  3141. # endif
  3142. int tag = synctex_scanner_get_tag(scanner,name);
  3143. size_t size = 0;
  3144. int friend_index = 0;
  3145. int max_line = 0;
  3146. synctex_node_t node = NULL;
  3147. if (tag == 0) {
  3148. printf("SyncTeX Warning: No tag for %s\n",name);
  3149. return -1;
  3150. }
  3151. free(SYNCTEX_START);
  3152. SYNCTEX_CUR = SYNCTEX_END = SYNCTEX_START = NULL;
  3153. max_line = line < INT_MAX-scanner->number_of_lists ? line+scanner->number_of_lists:INT_MAX;
  3154. while(line<max_line) {
  3155. /* This loop will only be performed once for advanced viewers */
  3156. friend_index = (tag+line)%(scanner->number_of_lists);
  3157. if ((node = (scanner->lists_of_friends)[friend_index])) {
  3158. do {
  3159. if ((synctex_node_type(node)>=synctex_node_type_boundary)
  3160. && (tag == SYNCTEX_TAG(node))
  3161. && (line == SYNCTEX_LINE(node))) {
  3162. if (SYNCTEX_CUR == SYNCTEX_END) {
  3163. size += 16;
  3164. SYNCTEX_END = realloc(SYNCTEX_START,size*sizeof(synctex_node_t *));
  3165. SYNCTEX_CUR += SYNCTEX_END - SYNCTEX_START;
  3166. SYNCTEX_START = SYNCTEX_END;
  3167. SYNCTEX_END = SYNCTEX_START + size*sizeof(synctex_node_t *);
  3168. }
  3169. *(synctex_node_t *)SYNCTEX_CUR = node;
  3170. SYNCTEX_CUR += sizeof(synctex_node_t);
  3171. }
  3172. } while((node = SYNCTEX_FRIEND(node)));
  3173. if (SYNCTEX_START == NULL) {
  3174. /* We did not find any matching boundary, retry with glue or kern */
  3175. node = (scanner->lists_of_friends)[friend_index];/* no need to test it again, already done */
  3176. do {
  3177. if ((synctex_node_type(node)>=synctex_node_type_kern)
  3178. && (tag == SYNCTEX_TAG(node))
  3179. && (line == SYNCTEX_LINE(node))) {
  3180. if (SYNCTEX_CUR == SYNCTEX_END) {
  3181. size += 16;
  3182. SYNCTEX_END = realloc(SYNCTEX_START,size*sizeof(synctex_node_t *));
  3183. SYNCTEX_CUR += SYNCTEX_END - SYNCTEX_START;
  3184. SYNCTEX_START = SYNCTEX_END;
  3185. SYNCTEX_END = SYNCTEX_START + size*sizeof(synctex_node_t *);
  3186. }
  3187. *(synctex_node_t *)SYNCTEX_CUR = node;
  3188. SYNCTEX_CUR += sizeof(synctex_node_t);
  3189. }
  3190. } while((node = SYNCTEX_FRIEND(node)));
  3191. if (SYNCTEX_START == NULL) {
  3192. /* We did not find any matching glue or kern, retry with boxes */
  3193. node = (scanner->lists_of_friends)[friend_index];/* no need to test it again, already done */
  3194. do {
  3195. if ((tag == SYNCTEX_TAG(node))
  3196. && (line == SYNCTEX_LINE(node))) {
  3197. if (SYNCTEX_CUR == SYNCTEX_END) {
  3198. size += 16;
  3199. SYNCTEX_END = realloc(SYNCTEX_START,size*sizeof(synctex_node_t *));
  3200. SYNCTEX_CUR += SYNCTEX_END - SYNCTEX_START;
  3201. SYNCTEX_START = SYNCTEX_END;
  3202. SYNCTEX_END = SYNCTEX_START + size*sizeof(synctex_node_t *);
  3203. }
  3204. *(synctex_node_t *)SYNCTEX_CUR = node;
  3205. SYNCTEX_CUR += sizeof(synctex_node_t);
  3206. }
  3207. } while((node = SYNCTEX_FRIEND(node)));
  3208. }
  3209. }
  3210. SYNCTEX_END = SYNCTEX_CUR;
  3211. /* Now reverse the order to have nodes in display order, and keep just a few nodes */
  3212. if ((SYNCTEX_START) && (SYNCTEX_END))
  3213. {
  3214. synctex_node_t * start_ref = (synctex_node_t *)SYNCTEX_START;
  3215. synctex_node_t * end_ref = (synctex_node_t *)SYNCTEX_END;
  3216. end_ref -= 1;
  3217. while(start_ref < end_ref) {
  3218. node = *start_ref;
  3219. *start_ref = *end_ref;
  3220. *end_ref = node;
  3221. start_ref += 1;
  3222. end_ref -= 1;
  3223. }
  3224. /* Basically, we keep the first node for each parent.
  3225. * More precisely, we keep only nodes that are not descendants of
  3226. * their predecessor's parent. */
  3227. start_ref = (synctex_node_t *)SYNCTEX_START;
  3228. end_ref = (synctex_node_t *)SYNCTEX_START;
  3229. next_end:
  3230. end_ref += 1; /* we allways have start_ref<= end_ref*/
  3231. if (end_ref < (synctex_node_t *)SYNCTEX_END) {
  3232. node = *end_ref;
  3233. while((node = SYNCTEX_PARENT(node))) {
  3234. if (SYNCTEX_PARENT(*start_ref) == node) {
  3235. goto next_end;
  3236. }
  3237. }
  3238. start_ref += 1;
  3239. *start_ref = *end_ref;
  3240. goto next_end;
  3241. }
  3242. start_ref += 1;
  3243. SYNCTEX_END = (char *)start_ref;
  3244. SYNCTEX_CUR = NULL;// added on behalf of Jose Alliste
  3245. return (SYNCTEX_END-SYNCTEX_START)/sizeof(synctex_node_t);// added on behalf Jan Sundermeyer
  3246. }
  3247. SYNCTEX_CUR = NULL;
  3248. // return (SYNCTEX_END-SYNCTEX_START)/sizeof(synctex_node_t); removed on behalf Jan Sundermeyer
  3249. }
  3250. # if defined(__SYNCTEX_STRONG_DISPLAY_QUERY__)
  3251. break;
  3252. # else
  3253. ++line;
  3254. # endif
  3255. }
  3256. return 0;
  3257. }
  3258. synctex_node_t synctex_next_result(synctex_scanner_t scanner) {
  3259. if (NULL == SYNCTEX_CUR) {
  3260. SYNCTEX_CUR = SYNCTEX_START;
  3261. } else {
  3262. SYNCTEX_CUR+=sizeof(synctex_node_t);
  3263. }
  3264. if (SYNCTEX_CUR<SYNCTEX_END) {
  3265. return *(synctex_node_t*)SYNCTEX_CUR;
  3266. } else {
  3267. return NULL;
  3268. }
  3269. }
  3270. /* This struct records a point in TeX coordinates.*/
  3271. typedef struct {
  3272. int h;
  3273. int v;
  3274. } synctex_point_t;
  3275. /* This struct records distances, the left one is positive or 0 and the right one is negative or 0.
  3276. * When comparing the locations of 2 different graphical objects on the page, we will have to also record the
  3277. * horizontal distance as signed to keep track of the typesetting order.*/
  3278. typedef struct {
  3279. int left;
  3280. int right;
  3281. } synctex_distances_t;
  3282. typedef struct {
  3283. synctex_point_t left;
  3284. synctex_point_t right;
  3285. } synctex_offsets_t;
  3286. typedef struct {
  3287. synctex_node_t left;
  3288. synctex_node_t right;
  3289. } synctex_node_set_t;
  3290. /* The smallest container between two has the smallest width or height.
  3291. * This comparison is used when there are 2 overlapping boxes that contain the hit point.
  3292. * For ConTeXt, the problem appears at each page.
  3293. * The chosen box is the one with the smallest height, then the smallest width. */
  3294. SYNCTEX_INLINE static synctex_node_t _synctex_smallest_container(synctex_node_t node, synctex_node_t other_node);
  3295. /* Returns the distance between the hit point hitPoint=(H,V) and the given node. */
  3296. synctex_bool_t _synctex_point_in_box(synctex_point_t hitPoint, synctex_node_t node, synctex_bool_t visible);
  3297. int _synctex_node_distance_to_point(synctex_point_t hitPoint, synctex_node_t node, synctex_bool_t visible);
  3298. /* The best container is the deeper box that contains the hit point (H,V).
  3299. * _synctex_eq_deepest_container starts with node whereas
  3300. * _synctex_box_child_deepest starts with node's children, if any
  3301. * if node is not a box, or a void box, NULL is returned.
  3302. * We traverse the node tree in a deep first manner and stop as soon as a result is found. */
  3303. static synctex_node_t _synctex_eq_deepest_container(synctex_point_t hitPoint,synctex_node_t node, synctex_bool_t visible);
  3304. /* Once a best container is found, the closest children are the closest nodes to the left or right of the hit point.
  3305. * Only horizontal and vertical offsets are used to compare the positions of the nodes. */
  3306. SYNCTEX_INLINE static int _synctex_eq_get_closest_children_in_box(synctex_point_t hitPoint, synctex_node_t node, synctex_node_set_t* bestNodesRef,synctex_distances_t* bestDistancesRef, synctex_bool_t visible);
  3307. /* The closest container is the box that is the one closest to the given point.
  3308. * The "visible" version takes into account the visible dimensions instead of the real ones given by TeX. */
  3309. SYNCTEX_INLINE static synctex_node_t _synctex_eq_closest_child(synctex_point_t hitPoint,synctex_node_t node, synctex_bool_t visible);
  3310. #define SYNCTEX_MASK_LEFT 1
  3311. #define SYNCTEX_MASK_RIGHT 2
  3312. int synctex_edit_query(synctex_scanner_t scanner,int page,float h,float v) {
  3313. synctex_node_t sheet = NULL;
  3314. synctex_node_t node = NULL; /* placeholder */
  3315. synctex_node_t other_node = NULL; /* placeholder */
  3316. synctex_point_t hitPoint = {0,0}; /* placeholder */
  3317. synctex_node_set_t bestNodes = {NULL,NULL}; /* holds the best node */
  3318. synctex_distances_t bestDistances = {INT_MAX,INT_MAX}; /* holds the best distances for the best node */
  3319. synctex_node_t bestContainer = NULL; /* placeholder */
  3320. if (NULL == (scanner = synctex_scanner_parse(scanner)) || 0 >= scanner->unit) {/* scanner->unit must be >0 */
  3321. return 0;
  3322. }
  3323. /* Convert the given point to scanner integer coordinates */
  3324. hitPoint.h = (h-scanner->x_offset)/scanner->unit;
  3325. hitPoint.v = (v-scanner->y_offset)/scanner->unit;
  3326. /* We will store in the scanner's buffer the result of the query. */
  3327. free(SYNCTEX_START);
  3328. SYNCTEX_START = SYNCTEX_END = SYNCTEX_CUR = NULL;
  3329. /* Find the proper sheet */
  3330. sheet = scanner->sheet;
  3331. while((sheet) && SYNCTEX_PAGE(sheet) != page) {
  3332. sheet = SYNCTEX_SIBLING(sheet);
  3333. }
  3334. if (NULL == sheet) {
  3335. return -1;
  3336. }
  3337. /* Now sheet points to the sheet node with proper page number */
  3338. /* Here is how we work:
  3339. * At first we do not consider the visible box dimensions. This will cover the most frequent cases.
  3340. * Then we try with the visible box dimensions.
  3341. * We try to find a non void box containing the hit point.
  3342. * We browse all the horizontal boxes until we find one containing the hit point. */
  3343. if ((node = SYNCTEX_NEXT_HORIZ_BOX(sheet))) {
  3344. do {
  3345. if (_synctex_point_in_box(hitPoint,node,synctex_YES)) {
  3346. /* Maybe the hitPoint belongs to a contained vertical box. */
  3347. end:
  3348. /* This trick is for catching overlapping boxes */
  3349. if ((other_node = SYNCTEX_NEXT_HORIZ_BOX(node))) {
  3350. do {
  3351. if (_synctex_point_in_box(hitPoint,other_node,synctex_YES)) {
  3352. node = _synctex_smallest_container(other_node,node);
  3353. }
  3354. } while((other_node = SYNCTEX_NEXT_HORIZ_BOX(other_node)));
  3355. }
  3356. /* node is the smallest horizontal box that contains hitPoint. */
  3357. if ((bestContainer = _synctex_eq_deepest_container(hitPoint,node,synctex_YES))) {
  3358. node = bestContainer;
  3359. }
  3360. _synctex_eq_get_closest_children_in_box(hitPoint,node,&bestNodes,&bestDistances,synctex_YES);
  3361. if (bestNodes.right && bestNodes.left) {
  3362. if ((SYNCTEX_TAG(bestNodes.right)!=SYNCTEX_TAG(bestNodes.left))
  3363. || (SYNCTEX_LINE(bestNodes.right)!=SYNCTEX_LINE(bestNodes.left))
  3364. || (SYNCTEX_COLUMN(bestNodes.right)!=SYNCTEX_COLUMN(bestNodes.left))) {
  3365. if ((SYNCTEX_START = malloc(2*sizeof(synctex_node_t)))) {
  3366. if (bestDistances.left>bestDistances.right) {
  3367. ((synctex_node_t *)SYNCTEX_START)[0] = bestNodes.right;
  3368. ((synctex_node_t *)SYNCTEX_START)[1] = bestNodes.left;
  3369. } else {
  3370. ((synctex_node_t *)SYNCTEX_START)[0] = bestNodes.left;
  3371. ((synctex_node_t *)SYNCTEX_START)[1] = bestNodes.right;
  3372. }
  3373. SYNCTEX_END = SYNCTEX_START + 2*sizeof(synctex_node_t);
  3374. SYNCTEX_CUR = NULL;
  3375. return (SYNCTEX_END-SYNCTEX_START)/sizeof(synctex_node_t);
  3376. }
  3377. return SYNCTEX_STATUS_ERROR;
  3378. }
  3379. /* both nodes have the same input coordinates
  3380. * We choose the one closest to the hit point */
  3381. if (bestDistances.left>bestDistances.right) {
  3382. bestNodes.left = bestNodes.right;
  3383. }
  3384. bestNodes.right = NULL;
  3385. } else if (bestNodes.right) {
  3386. bestNodes.left = bestNodes.right;
  3387. } else if (!bestNodes.left){
  3388. bestNodes.left = node;
  3389. }
  3390. if ((SYNCTEX_START = malloc(sizeof(synctex_node_t)))) {
  3391. * (synctex_node_t *)SYNCTEX_START = bestNodes.left;
  3392. SYNCTEX_END = SYNCTEX_START + sizeof(synctex_node_t);
  3393. SYNCTEX_CUR = NULL;
  3394. return (SYNCTEX_END-SYNCTEX_START)/sizeof(synctex_node_t);
  3395. }
  3396. return SYNCTEX_STATUS_ERROR;
  3397. }
  3398. } while ((node = SYNCTEX_NEXT_HORIZ_BOX(node)));
  3399. /* All the horizontal boxes have been tested,
  3400. * None of them contains the hit point.
  3401. */
  3402. }
  3403. /* We are not lucky */
  3404. if ((node = SYNCTEX_CHILD(sheet))) {
  3405. goto end;
  3406. }
  3407. return 0;
  3408. }
  3409. # ifdef SYNCTEX_NOTHING
  3410. # pragma mark -
  3411. # pragma mark Utilities
  3412. # endif
  3413. int _synctex_bail(void) {
  3414. _synctex_error("SyncTeX ERROR\n");
  3415. return -1;
  3416. }
  3417. /* Rougly speaking, this is:
  3418. * node's h coordinate - hitPoint's h coordinate.
  3419. * If node is to the right of the hit point, then this distance is positive,
  3420. * if node is to the left of the hit point, this distance is negative.*/
  3421. int _synctex_point_h_distance(synctex_point_t hitPoint, synctex_node_t node, synctex_bool_t visible);
  3422. int _synctex_point_h_distance(synctex_point_t hitPoint, synctex_node_t node, synctex_bool_t visible) {
  3423. if (node) {
  3424. int min,med,max;
  3425. switch(node->class->type) {
  3426. /* The distance between a point and a box is special.
  3427. * It is not the euclidian distance, nor something similar.
  3428. * We have to take into account the particular layout,
  3429. * and the box hierarchy.
  3430. * Given a box, there are 9 regions delimited by the lines of the edges of the box.
  3431. * The origin being at the top left corner of the page,
  3432. * we also give names to the vertices of the box.
  3433. *
  3434. * 1 | 2 | 3
  3435. * ---A---B--->
  3436. * 4 | 5 | 6
  3437. * ---C---D--->
  3438. * 7 | 8 | 9
  3439. * v v
  3440. */
  3441. case synctex_node_type_hbox:
  3442. /* getting the box bounds, taking into account negative width, height and depth. */
  3443. min = visible?SYNCTEX_HORIZ_V(node):SYNCTEX_HORIZ(node);
  3444. max = min + (visible?SYNCTEX_ABS_WIDTH_V(node):SYNCTEX_ABS_WIDTH(node));
  3445. /* We allways have min <= max */
  3446. if (hitPoint.h<min) {
  3447. return min - hitPoint.h; /* regions 1+4+7, result is > 0 */
  3448. } else if (hitPoint.h>max) {
  3449. return max - hitPoint.h; /* regions 3+6+9, result is < 0 */
  3450. } else {
  3451. return 0; /* regions 2+5+8, inside the box, except for vertical coordinates */
  3452. }
  3453. break;
  3454. case synctex_node_type_vbox:
  3455. case synctex_node_type_void_vbox:
  3456. case synctex_node_type_void_hbox:
  3457. /* getting the box bounds, taking into account negative width, height and depth.
  3458. * For these boxes, no visible dimension available */
  3459. min = SYNCTEX_HORIZ(node);
  3460. max = min + SYNCTEX_ABS_WIDTH(node);
  3461. /* We allways have min <= max */
  3462. if (hitPoint.h<min) {
  3463. return min - hitPoint.h; /* regions 1+4+7, result is > 0 */
  3464. } else if (hitPoint.h>max) {
  3465. return max - hitPoint.h; /* regions 3+6+9, result is < 0 */
  3466. } else {
  3467. return 0; /* regions 2+5+8, inside the box, except for vertical coordinates */
  3468. }
  3469. break;
  3470. case synctex_node_type_kern:
  3471. /* IMPORTANT NOTICE: the location of the kern is recorded AFTER the move.
  3472. * The distance to the kern is very special,
  3473. * in general, there is no text material in the kern,
  3474. * this is why we compute the offset relative to the closest edge of the kern.*/
  3475. max = SYNCTEX_WIDTH(node);
  3476. if (max<0) {
  3477. min = SYNCTEX_HORIZ(node);
  3478. max = min - max;
  3479. } else {
  3480. min = -max;
  3481. max = SYNCTEX_HORIZ(node);
  3482. min += max;
  3483. }
  3484. med = (min+max)/2;
  3485. /* positive kern: '.' means text, '>' means kern offset
  3486. * .............
  3487. * min>>>>med>>>>max
  3488. * ...............
  3489. * negative kern: '.' means text, '<' means kern offset
  3490. * ............................
  3491. * min<<<<med<<<<max
  3492. * .................................
  3493. * Actually, we do not take into account negative widths.
  3494. * There is a problem for such situation when there is efectively overlapping text.
  3495. * But this should be extremely rare. I guess that in that case, many different choices
  3496. * could be made, one being in contradiction of the other.
  3497. * It means that the best choice should be made according to the situation that occurs
  3498. * most frequently.
  3499. */
  3500. if (hitPoint.h<min) {
  3501. return min - hitPoint.h + 1; /* penalty to ensure other nodes are chosen first in case of overlapping ones */
  3502. } else if (hitPoint.h>max) {
  3503. return max - hitPoint.h - 1; /* same kind of penalty */
  3504. } else if (hitPoint.h>med) {
  3505. /* do things like if the node had 0 width and was placed at the max edge + 1*/
  3506. return max - hitPoint.h + 1; /* positive, the kern is to the right of the hitPoint */
  3507. } else {
  3508. return min - hitPoint.h - 1; /* negative, the kern is to the left of the hitPoint */
  3509. }
  3510. case synctex_node_type_glue:
  3511. case synctex_node_type_math:
  3512. return SYNCTEX_HORIZ(node) - hitPoint.h;
  3513. }
  3514. }
  3515. return INT_MAX;/* We always assume that the node is faraway to the right*/
  3516. }
  3517. /* Rougly speaking, this is:
  3518. * node's v coordinate - hitPoint's v coordinate.
  3519. * If node is at the top of the hit point, then this distance is positive,
  3520. * if node is at the bottom of the hit point, this distance is negative.*/
  3521. int _synctex_point_v_distance(synctex_point_t hitPoint, synctex_node_t node,synctex_bool_t visible);
  3522. int _synctex_point_v_distance(synctex_point_t hitPoint, synctex_node_t node,synctex_bool_t visible) {
  3523. # ifdef __DARWIN_UNIX03
  3524. # pragma unused(visible)
  3525. # endif
  3526. if (node) {
  3527. int min,max;
  3528. switch(node->class->type) {
  3529. /* The distance between a point and a box is special.
  3530. * It is not the euclidian distance, nor something similar.
  3531. * We have to take into account the particular layout,
  3532. * and the box hierarchy.
  3533. * Given a box, there are 9 regions delimited by the lines of the edges of the box.
  3534. * The origin being at the top left corner of the page,
  3535. * we also give names to the vertices of the box.
  3536. *
  3537. * 1 | 2 | 3
  3538. * ---A---B--->
  3539. * 4 | 5 | 6
  3540. * ---C---D--->
  3541. * 7 | 8 | 9
  3542. * v v
  3543. */
  3544. case synctex_node_type_hbox:
  3545. /* getting the box bounds, taking into account negative width, height and depth. */
  3546. min = SYNCTEX_VERT_V(node);
  3547. max = min + SYNCTEX_ABS_DEPTH_V(node);
  3548. min -= SYNCTEX_ABS_HEIGHT_V(node);
  3549. /* We allways have min <= max */
  3550. if (hitPoint.v<min) {
  3551. return min - hitPoint.v; /* regions 1+2+3, result is > 0 */
  3552. } else if (hitPoint.v>max) {
  3553. return max - hitPoint.v; /* regions 7+8+9, result is < 0 */
  3554. } else {
  3555. return 0; /* regions 4.5.6, inside the box, except for horizontal coordinates */
  3556. }
  3557. break;
  3558. case synctex_node_type_vbox:
  3559. case synctex_node_type_void_vbox:
  3560. case synctex_node_type_void_hbox:
  3561. /* getting the box bounds, taking into account negative width, height and depth. */
  3562. min = SYNCTEX_VERT(node);
  3563. max = min + SYNCTEX_ABS_DEPTH(node);
  3564. min -= SYNCTEX_ABS_HEIGHT(node);
  3565. /* We allways have min <= max */
  3566. if (hitPoint.v<min) {
  3567. return min - hitPoint.v; /* regions 1+2+3, result is > 0 */
  3568. } else if (hitPoint.v>max) {
  3569. return max - hitPoint.v; /* regions 7+8+9, result is < 0 */
  3570. } else {
  3571. return 0; /* regions 4.5.6, inside the box, except for horizontal coordinates */
  3572. }
  3573. break;
  3574. case synctex_node_type_kern:
  3575. case synctex_node_type_glue:
  3576. case synctex_node_type_math:
  3577. return SYNCTEX_VERT(node) - hitPoint.v;
  3578. }
  3579. }
  3580. return INT_MAX;/* We always assume that the node is faraway to the top*/
  3581. }
  3582. SYNCTEX_INLINE static synctex_node_t _synctex_smallest_container(synctex_node_t node, synctex_node_t other_node) {
  3583. float height, other_height;
  3584. if (SYNCTEX_ABS_WIDTH(node)<SYNCTEX_ABS_WIDTH(other_node)) {
  3585. return node;
  3586. }
  3587. if (SYNCTEX_ABS_WIDTH(node)>SYNCTEX_ABS_WIDTH(other_node)) {
  3588. return other_node;
  3589. }
  3590. height = SYNCTEX_ABS_DEPTH(node) + SYNCTEX_ABS_HEIGHT(node);
  3591. other_height = SYNCTEX_ABS_DEPTH(other_node) + SYNCTEX_ABS_HEIGHT(other_node);
  3592. if (height<other_height) {
  3593. return node;
  3594. }
  3595. if (height>other_height) {
  3596. return other_node;
  3597. }
  3598. return node;
  3599. }
  3600. synctex_bool_t _synctex_point_in_box(synctex_point_t hitPoint, synctex_node_t node, synctex_bool_t visible) {
  3601. if (node) {
  3602. if (0 == _synctex_point_h_distance(hitPoint,node,visible)
  3603. && 0 == _synctex_point_v_distance(hitPoint,node,visible)) {
  3604. return synctex_YES;
  3605. }
  3606. }
  3607. return synctex_NO;
  3608. }
  3609. int _synctex_node_distance_to_point(synctex_point_t hitPoint, synctex_node_t node, synctex_bool_t visible) {
  3610. # ifdef __DARWIN_UNIX03
  3611. # pragma unused(visible)
  3612. # endif
  3613. int result = INT_MAX; /* when the distance is meaning less (sheet, input...) */
  3614. if (node) {
  3615. int minH,maxH,minV,maxV;
  3616. switch(node->class->type) {
  3617. /* The distance between a point and a box is special.
  3618. * It is not the euclidian distance, nor something similar.
  3619. * We have to take into account the particular layout,
  3620. * and the box hierarchy.
  3621. * Given a box, there are 9 regions delimited by the lines of the edges of the box.
  3622. * The origin being at the top left corner of the page,
  3623. * we also give names to the vertices of the box.
  3624. *
  3625. * 1 | 2 | 3
  3626. * ---A---B--->
  3627. * 4 | 5 | 6
  3628. * ---C---D--->
  3629. * 7 | 8 | 9
  3630. * v v
  3631. * In each region, there is a different formula.
  3632. * In the end we have a continuous distance which may not be a mathematical distance but who cares. */
  3633. case synctex_node_type_vbox:
  3634. case synctex_node_type_void_vbox:
  3635. case synctex_node_type_hbox:
  3636. case synctex_node_type_void_hbox:
  3637. /* getting the box bounds, taking into account negative widths. */
  3638. minH = SYNCTEX_HORIZ(node);
  3639. maxH = minH + SYNCTEX_ABS_WIDTH(node);
  3640. minV = SYNCTEX_VERT(node);
  3641. maxV = minV + SYNCTEX_ABS_DEPTH(node);
  3642. minV -= SYNCTEX_ABS_HEIGHT(node);
  3643. /* In what region is the point hitPoint=(H,V) ? */
  3644. if (hitPoint.v<minV) {
  3645. if (hitPoint.h<minH) {
  3646. /* This is region 1. The distance to the box is the L1 distance PA. */
  3647. result = minV - hitPoint.v + minH - hitPoint.h;/* Integer overflow? probability epsilon */
  3648. } else if (hitPoint.h<=maxH) {
  3649. /* This is region 2. The distance to the box is the geometrical distance to the top edge. */
  3650. result = minV - hitPoint.v;
  3651. } else {
  3652. /* This is region 3. The distance to the box is the L1 distance PB. */
  3653. result = minV - hitPoint.v + hitPoint.h - maxH;
  3654. }
  3655. } else if (hitPoint.v<=maxV) {
  3656. if (hitPoint.h<minH) {
  3657. /* This is region 4. The distance to the box is the geometrical distance to the left edge. */
  3658. result = minH - hitPoint.h;
  3659. } else if (hitPoint.h<=maxH) {
  3660. /* This is region 4. We are inside the box. */
  3661. result = 0;
  3662. } else {
  3663. /* This is region 6. The distance to the box is the geometrical distance to the right edge. */
  3664. result = hitPoint.h - maxH;
  3665. }
  3666. } else {
  3667. if (hitPoint.h<minH) {
  3668. /* This is region 7. The distance to the box is the L1 distance PC. */
  3669. result = hitPoint.v - maxV + minH - hitPoint.h;
  3670. } else if (hitPoint.h<=maxH) {
  3671. /* This is region 8. The distance to the box is the geometrical distance to the top edge. */
  3672. result = hitPoint.v - maxV;
  3673. } else {
  3674. /* This is region 9. The distance to the box is the L1 distance PD. */
  3675. result = hitPoint.v - maxV + hitPoint.h - maxH;
  3676. }
  3677. }
  3678. break;
  3679. case synctex_node_type_kern:
  3680. maxH = SYNCTEX_WIDTH(node);
  3681. if (maxH<0) {
  3682. minH = SYNCTEX_HORIZ(node);
  3683. maxH = minH - maxH;
  3684. } else {
  3685. minH = -maxH;
  3686. maxH = SYNCTEX_HORIZ(node);
  3687. minH += maxH;
  3688. }
  3689. minV = SYNCTEX_VERT(node);
  3690. if (hitPoint.h<minH) {
  3691. if (hitPoint.v>minV) {
  3692. result = hitPoint.v - minV + minH - hitPoint.h;
  3693. } else {
  3694. result = minV - hitPoint.v + minH - hitPoint.h;
  3695. }
  3696. } else if (hitPoint.h>maxH) {
  3697. if (hitPoint.v>minV) {
  3698. result = hitPoint.v - minV + hitPoint.h - maxH;
  3699. } else {
  3700. result = minV - hitPoint.v + hitPoint.h - maxH;
  3701. }
  3702. } else if (hitPoint.v>minV) {
  3703. result = hitPoint.v - minV;
  3704. } else {
  3705. result = minV - hitPoint.v;
  3706. }
  3707. break;
  3708. case synctex_node_type_glue:
  3709. case synctex_node_type_math:
  3710. minH = SYNCTEX_HORIZ(node);
  3711. minV = SYNCTEX_VERT(node);
  3712. if (hitPoint.h<minH) {
  3713. if (hitPoint.v>minV) {
  3714. result = hitPoint.v - minV + minH - hitPoint.h;
  3715. } else {
  3716. result = minV - hitPoint.v + minH - hitPoint.h;
  3717. }
  3718. } else if (hitPoint.v>minV) {
  3719. result = hitPoint.v - minV + hitPoint.h - minH;
  3720. } else {
  3721. result = minV - hitPoint.v + hitPoint.h - minH;
  3722. }
  3723. break;
  3724. }
  3725. }
  3726. return result;
  3727. }
  3728. static synctex_node_t _synctex_eq_deepest_container(synctex_point_t hitPoint,synctex_node_t node, synctex_bool_t visible) {
  3729. if (node) {
  3730. synctex_node_t result = NULL;
  3731. synctex_node_t child = NULL;
  3732. switch(node->class->type) {
  3733. case synctex_node_type_vbox:
  3734. case synctex_node_type_hbox:
  3735. /* test the deep nodes first */
  3736. if ((child = SYNCTEX_CHILD(node))) {
  3737. do {
  3738. if ((result = _synctex_eq_deepest_container(hitPoint,child,visible))) {
  3739. return result;
  3740. }
  3741. } while((child = SYNCTEX_SIBLING(child)));
  3742. }
  3743. /* is the hit point inside the box? */
  3744. if (_synctex_point_in_box(hitPoint,node,visible)) {
  3745. /* for vboxes we try to use some node inside.
  3746. * Walk through the list of siblings until we find the closest one.
  3747. * Only consider siblings with children. */
  3748. if ((node->class->type == synctex_node_type_vbox) && (child = SYNCTEX_CHILD(node))) {
  3749. int bestDistance = INT_MAX;
  3750. do {
  3751. if (SYNCTEX_CHILD(child)) {
  3752. int distance = _synctex_node_distance_to_point(hitPoint,child,visible);
  3753. if (distance < bestDistance) {
  3754. bestDistance = distance;
  3755. node = child;
  3756. }
  3757. }
  3758. } while((child = SYNCTEX_SIBLING(child)));
  3759. }
  3760. return node;
  3761. }
  3762. }
  3763. }
  3764. return NULL;
  3765. }
  3766. /* Compares the locations of the hitPoint with the locations of the various nodes contained in the box.
  3767. * As it is an horizontal box, we only compare horizontal coordinates. */
  3768. SYNCTEX_INLINE static int __synctex_eq_get_closest_children_in_hbox(synctex_point_t hitPoint, synctex_node_t node, synctex_node_set_t* bestNodesRef,synctex_distances_t* bestDistancesRef, synctex_bool_t visible);
  3769. SYNCTEX_INLINE static int __synctex_eq_get_closest_children_in_hbox(synctex_point_t hitPoint, synctex_node_t node, synctex_node_set_t* bestNodesRef,synctex_distances_t* bestDistancesRef, synctex_bool_t visible) {
  3770. int result = 0;
  3771. if ((node = SYNCTEX_CHILD(node))) {
  3772. do {
  3773. int off7 = _synctex_point_h_distance(hitPoint,node,visible);
  3774. if (off7 > 0) {
  3775. /* node is to the right of the hit point.
  3776. * We compare node and the previously recorded one, through the recorded distance.
  3777. * If the nodes have the same tag, prefer the one with the smallest line number,
  3778. * if the nodes also have the same line number, prefer the one with the smallest column. */
  3779. if (bestDistancesRef->right > off7) {
  3780. bestDistancesRef->right = off7;
  3781. bestNodesRef->right = node;
  3782. result |= SYNCTEX_MASK_RIGHT;
  3783. } else if (bestDistancesRef->right == off7 && bestNodesRef->right) {
  3784. if (SYNCTEX_TAG(bestNodesRef->right) == SYNCTEX_TAG(node)
  3785. && (SYNCTEX_LINE(bestNodesRef->right) > SYNCTEX_LINE(node)
  3786. || (SYNCTEX_LINE(bestNodesRef->right) == SYNCTEX_LINE(node)
  3787. && SYNCTEX_COLUMN(bestNodesRef->right) > SYNCTEX_COLUMN(node)))) {
  3788. bestNodesRef->right = node;
  3789. result |= SYNCTEX_MASK_RIGHT;
  3790. }
  3791. }
  3792. } else if (off7 == 0) {
  3793. /* hitPoint is inside node. */
  3794. bestDistancesRef->left = bestDistancesRef->right = 0;
  3795. bestNodesRef->left = node;
  3796. bestNodesRef->right = NULL;
  3797. result |= SYNCTEX_MASK_LEFT;
  3798. } else { /* here off7 < 0, hitPoint is to the right of node */
  3799. off7 = -off7;
  3800. if (bestDistancesRef->left > off7) {
  3801. bestDistancesRef->left = off7;
  3802. bestNodesRef->left = node;
  3803. result |= SYNCTEX_MASK_LEFT;
  3804. } else if (bestDistancesRef->left == off7 && bestNodesRef->left) {
  3805. if (SYNCTEX_TAG(bestNodesRef->left) == SYNCTEX_TAG(node)
  3806. && (SYNCTEX_LINE(bestNodesRef->left) > SYNCTEX_LINE(node)
  3807. || (SYNCTEX_LINE(bestNodesRef->left) == SYNCTEX_LINE(node)
  3808. && SYNCTEX_COLUMN(bestNodesRef->left) > SYNCTEX_COLUMN(node)))) {
  3809. bestNodesRef->left = node;
  3810. result |= SYNCTEX_MASK_LEFT;
  3811. }
  3812. }
  3813. }
  3814. } while((node = SYNCTEX_SIBLING(node)));
  3815. if (result & SYNCTEX_MASK_LEFT) {
  3816. /* the left node is new, try to narrow the result */
  3817. if ((node = _synctex_eq_deepest_container(hitPoint,bestNodesRef->left,visible))) {
  3818. bestNodesRef->left = node;
  3819. }
  3820. if ((node = _synctex_eq_closest_child(hitPoint,bestNodesRef->left,visible))) {
  3821. bestNodesRef->left = node;
  3822. }
  3823. }
  3824. if (result & SYNCTEX_MASK_RIGHT) {
  3825. /* the right node is new, try to narrow the result */
  3826. if ((node = _synctex_eq_deepest_container(hitPoint,bestNodesRef->right,visible))) {
  3827. bestNodesRef->right = node;
  3828. }
  3829. if ((node = _synctex_eq_closest_child(hitPoint,bestNodesRef->right,visible))) {
  3830. bestNodesRef->right = node;
  3831. }
  3832. }
  3833. }
  3834. return result;
  3835. }
  3836. SYNCTEX_INLINE static int __synctex_eq_get_closest_children_in_vbox(synctex_point_t hitPoint, synctex_node_t node, synctex_node_set_t* bestNodesRef,synctex_distances_t* bestDistancesRef,synctex_bool_t visible);
  3837. SYNCTEX_INLINE static int __synctex_eq_get_closest_children_in_vbox(synctex_point_t hitPoint, synctex_node_t node, synctex_node_set_t* bestNodesRef,synctex_distances_t* bestDistancesRef,synctex_bool_t visible) {
  3838. int result = 0;
  3839. if ((node = SYNCTEX_CHILD(node))) {
  3840. do {
  3841. int off7 = _synctex_point_v_distance(hitPoint,node,visible);/* this is what makes the difference with the h version above */
  3842. if (off7 > 0) {
  3843. /* node is to the top of the hit point (below because TeX is oriented from top to bottom.
  3844. * We compare node and the previously recorded one, through the recorded distance.
  3845. * If the nodes have the same tag, prefer the one with the smallest line number,
  3846. * if the nodes also have the same line number, prefer the one with the smallest column. */
  3847. if (bestDistancesRef->right > off7) {
  3848. bestDistancesRef->right = off7;
  3849. bestNodesRef->right = node;
  3850. result |= SYNCTEX_MASK_RIGHT;
  3851. } else if (bestDistancesRef->right == off7 && bestNodesRef->right) {
  3852. if (SYNCTEX_TAG(bestNodesRef->right) == SYNCTEX_TAG(node)
  3853. && (SYNCTEX_LINE(bestNodesRef->right) > SYNCTEX_LINE(node)
  3854. || (SYNCTEX_LINE(bestNodesRef->right) == SYNCTEX_LINE(node)
  3855. && SYNCTEX_COLUMN(bestNodesRef->right) > SYNCTEX_COLUMN(node)))) {
  3856. bestNodesRef->right = node;
  3857. result |= SYNCTEX_MASK_RIGHT;
  3858. }
  3859. }
  3860. } else if (off7 == 0) {
  3861. bestDistancesRef->left = bestDistancesRef->right = 0;
  3862. bestNodesRef->left = node;
  3863. bestNodesRef->right = NULL;
  3864. result |= SYNCTEX_MASK_LEFT;
  3865. } else { /* here off7 < 0 */
  3866. off7 = -off7;
  3867. if (bestDistancesRef->left > off7) {
  3868. bestDistancesRef->left = off7;
  3869. bestNodesRef->left = node;
  3870. result |= SYNCTEX_MASK_LEFT;
  3871. } else if (bestDistancesRef->left == off7 && bestNodesRef->left) {
  3872. if (SYNCTEX_TAG(bestNodesRef->left) == SYNCTEX_TAG(node)
  3873. && (SYNCTEX_LINE(bestNodesRef->left) > SYNCTEX_LINE(node)
  3874. || (SYNCTEX_LINE(bestNodesRef->left) == SYNCTEX_LINE(node)
  3875. && SYNCTEX_COLUMN(bestNodesRef->left) > SYNCTEX_COLUMN(node)))) {
  3876. bestNodesRef->left = node;
  3877. result |= SYNCTEX_MASK_LEFT;
  3878. }
  3879. }
  3880. }
  3881. } while((node = SYNCTEX_SIBLING(node)));
  3882. if (result & SYNCTEX_MASK_LEFT) {
  3883. /* the left node is new, try to narrow the result */
  3884. if ((node = _synctex_eq_deepest_container(hitPoint,bestNodesRef->left,visible))) {
  3885. bestNodesRef->left = node;
  3886. }
  3887. if ((node = _synctex_eq_closest_child(hitPoint,bestNodesRef->left,visible))) {
  3888. bestNodesRef->left = node;
  3889. }
  3890. }
  3891. if (result & SYNCTEX_MASK_RIGHT) {
  3892. /* the right node is new, try to narrow the result */
  3893. if ((node = _synctex_eq_deepest_container(hitPoint,bestNodesRef->right,visible))) {
  3894. bestNodesRef->right = node;
  3895. }
  3896. if ((node = _synctex_eq_closest_child(hitPoint,bestNodesRef->right,visible))) {
  3897. bestNodesRef->right = node;
  3898. }
  3899. }
  3900. }
  3901. return result;
  3902. }
  3903. SYNCTEX_INLINE static int _synctex_eq_get_closest_children_in_box(synctex_point_t hitPoint, synctex_node_t node, synctex_node_set_t* bestNodesRef,synctex_distances_t* bestDistancesRef,synctex_bool_t visible) {
  3904. if (node) {
  3905. switch(node->class->type) {
  3906. case synctex_node_type_hbox:
  3907. return __synctex_eq_get_closest_children_in_hbox(hitPoint, node, bestNodesRef, bestDistancesRef,visible);
  3908. case synctex_node_type_vbox:
  3909. return __synctex_eq_get_closest_children_in_vbox(hitPoint, node, bestNodesRef, bestDistancesRef,visible);
  3910. }
  3911. }
  3912. return 0;
  3913. }
  3914. SYNCTEX_INLINE static synctex_node_t __synctex_eq_closest_child(synctex_point_t hitPoint, synctex_node_t node,int* distanceRef, synctex_bool_t visible);
  3915. SYNCTEX_INLINE static synctex_node_t __synctex_eq_closest_child(synctex_point_t hitPoint, synctex_node_t node,int* distanceRef, synctex_bool_t visible) {
  3916. synctex_node_t best_node = NULL;
  3917. if ((node = SYNCTEX_CHILD(node))) {
  3918. do {
  3919. int distance = _synctex_node_distance_to_point(hitPoint,node,visible);
  3920. synctex_node_t candidate = NULL;
  3921. if (distance<=*distanceRef) {
  3922. *distanceRef = distance;
  3923. best_node = node;
  3924. }
  3925. switch(node->class->type) {
  3926. case synctex_node_type_vbox:
  3927. case synctex_node_type_hbox:
  3928. if ((candidate = __synctex_eq_closest_child(hitPoint,node,distanceRef,visible))) {
  3929. best_node = candidate;
  3930. }
  3931. }
  3932. } while((node = SYNCTEX_SIBLING(node)));
  3933. }
  3934. return best_node;
  3935. }
  3936. SYNCTEX_INLINE static synctex_node_t _synctex_eq_closest_child(synctex_point_t hitPoint,synctex_node_t node, synctex_bool_t visible) {
  3937. if (node) {
  3938. switch(node->class->type) {
  3939. case synctex_node_type_hbox:
  3940. case synctex_node_type_vbox:
  3941. {
  3942. int best_distance = INT_MAX;
  3943. synctex_node_t best_node = __synctex_eq_closest_child(hitPoint,node,&best_distance,visible);
  3944. if ((best_node)) {
  3945. synctex_node_t child = NULL;
  3946. switch(best_node->class->type) {
  3947. case synctex_node_type_vbox:
  3948. case synctex_node_type_hbox:
  3949. if ((child = SYNCTEX_CHILD(best_node))) {
  3950. best_distance = _synctex_node_distance_to_point(hitPoint,child,visible);
  3951. while((child = SYNCTEX_SIBLING(child))) {
  3952. int distance = _synctex_node_distance_to_point(hitPoint,child,visible);
  3953. if (distance<=best_distance) {
  3954. best_distance = distance;
  3955. best_node = child;
  3956. }
  3957. }
  3958. }
  3959. }
  3960. }
  3961. return best_node;
  3962. }
  3963. }
  3964. }
  3965. return NULL;
  3966. }
  3967. # ifdef SYNCTEX_NOTHING
  3968. # pragma mark -
  3969. # pragma mark Updater
  3970. # endif
  3971. typedef int (*synctex_fprintf_t)(void *, const char * , ...); /* print formatted to either FILE * or gzFile */
  3972. # define SYNCTEX_BITS_PER_BYTE 8
  3973. struct __synctex_updater_t {
  3974. void *file; /* the foo.synctex or foo.synctex.gz I/O identifier */
  3975. synctex_fprintf_t fprintf; /* either fprintf or gzprintf */
  3976. int length; /* the number of chars appended */
  3977. struct _flags {
  3978. unsigned int no_gz:1; /* Whether zlib is used or not */
  3979. unsigned int reserved:SYNCTEX_BITS_PER_BYTE*sizeof(int)-1; /* Align */
  3980. } flags;
  3981. };
  3982. # define SYNCTEX_FILE updater->file
  3983. # define SYNCTEX_NO_GZ ((updater->flags).no_gz)
  3984. # define SYNCTEX_fprintf (*(updater->fprintf))
  3985. synctex_updater_t synctex_updater_new_with_output_file(const char * output, const char * build_directory) {
  3986. synctex_updater_t updater = NULL;
  3987. char * synctex = NULL;
  3988. synctex_io_mode_t io_mode = 0;
  3989. const char * mode = NULL;
  3990. /* prepare the updater, the memory is the only one dynamically allocated */
  3991. updater = (synctex_updater_t)_synctex_malloc(sizeof(synctex_updater_t));
  3992. if (NULL == updater) {
  3993. _synctex_error("! synctex_updater_new_with_file: malloc problem");
  3994. return NULL;
  3995. }
  3996. if (_synctex_open(output,build_directory,&synctex,&SYNCTEX_FILE,synctex_ADD_QUOTES,&io_mode)
  3997. && _synctex_open(output,build_directory,&synctex,&SYNCTEX_FILE,synctex_DONT_ADD_QUOTES,&io_mode)) {
  3998. return_on_error:
  3999. free(updater);
  4000. updater = NULL;
  4001. return NULL;
  4002. }
  4003. /* OK, the file exists, we close it and reopen it with the correct mode.
  4004. * The receiver is now the owner of the "synctex" variable. */
  4005. gzclose(SYNCTEX_FILE);
  4006. SYNCTEX_FILE = NULL;
  4007. SYNCTEX_NO_GZ = (io_mode&synctex_io_gz_mask)?synctex_NO:synctex_YES;
  4008. mode = _synctex_get_io_mode_name(io_mode|synctex_io_append_mask);/* either "a" or "ab", depending on the file extension */
  4009. if (SYNCTEX_NO_GZ) {
  4010. if (NULL == (SYNCTEX_FILE = (void *)fopen(synctex,mode))) {
  4011. no_write_error:
  4012. _synctex_error("! synctex_updater_new_with_file: Can't append to %s",synctex);
  4013. free(synctex);
  4014. goto return_on_error;
  4015. }
  4016. updater->fprintf = (synctex_fprintf_t)(&fprintf);
  4017. } else {
  4018. if (NULL == (SYNCTEX_FILE = (void *)gzopen(synctex,mode))) {
  4019. goto no_write_error;
  4020. }
  4021. updater->fprintf = (synctex_fprintf_t)(&gzprintf);
  4022. }
  4023. printf("SyncTeX: updating %s...",synctex);
  4024. free(synctex);
  4025. return updater;
  4026. }
  4027. void synctex_updater_append_magnification(synctex_updater_t updater, char * magnification){
  4028. if (NULL==updater) {
  4029. return;
  4030. }
  4031. if (magnification && strlen(magnification)) {
  4032. updater->length += SYNCTEX_fprintf(SYNCTEX_FILE,"Magnification:%s\n",magnification);
  4033. }
  4034. }
  4035. void synctex_updater_append_x_offset(synctex_updater_t updater, char * x_offset){
  4036. if (NULL==updater) {
  4037. return;
  4038. }
  4039. if (x_offset && strlen(x_offset)) {
  4040. updater->length += SYNCTEX_fprintf(SYNCTEX_FILE,"X Offset:%s\n",x_offset);
  4041. }
  4042. }
  4043. void synctex_updater_append_y_offset(synctex_updater_t updater, char * y_offset){
  4044. if (NULL==updater) {
  4045. return;
  4046. }
  4047. if (y_offset && strlen(y_offset)) {
  4048. updater->length += SYNCTEX_fprintf(SYNCTEX_FILE,"Y Offset:%s\n",y_offset);
  4049. }
  4050. }
  4051. void synctex_updater_free(synctex_updater_t updater){
  4052. if (NULL==updater) {
  4053. return;
  4054. }
  4055. if (updater->length>0) {
  4056. SYNCTEX_fprintf(SYNCTEX_FILE,"!%i\n",updater->length);
  4057. }
  4058. if (SYNCTEX_NO_GZ) {
  4059. fclose((FILE *)SYNCTEX_FILE);
  4060. } else {
  4061. gzclose((gzFile)SYNCTEX_FILE);
  4062. }
  4063. free(updater);
  4064. printf("... done.\n");
  4065. return;
  4066. }