synctex_parser.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. Copyright (c) 2008-2017 jerome DOT laurens AT u-bourgogne DOT fr
  3. This file is part of the __SyncTeX__ package.
  4. [//]: # (Latest Revision: Fri Jul 14 16:20:41 UTC 2017)
  5. [//]: # (Version: 1.21)
  6. See `synctex_parser_readme.md` for more details
  7. ## License
  8. Permission is hereby granted, free of charge, to any person
  9. obtaining a copy of this software and associated documentation
  10. files (the "Software"), to deal in the Software without
  11. restriction, including without limitation the rights to use,
  12. copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the
  14. Software is furnished to do so, subject to the following
  15. conditions:
  16. The above copyright notice and this permission notice shall be
  17. included in all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. OTHER DEALINGS IN THE SOFTWARE
  26. Except as contained in this notice, the name of the copyright holder
  27. shall not be used in advertising or otherwise to promote the sale,
  28. use or other dealings in this Software without prior written
  29. authorization from the copyright holder.
  30. ## Acknowledgments:
  31. The author received useful remarks from the __pdfTeX__ developers, especially Hahn The Thanh,
  32. and significant help from __XeTeX__ developer Jonathan Kew.
  33. ## Nota Bene:
  34. If you include or use a significant part of the __SyncTeX__ package into a software,
  35. I would appreciate to be listed as contributor and see "__SyncTeX__" highlighted.
  36. */
  37. #ifndef __SYNCTEX_PARSER__
  38. # define __SYNCTEX_PARSER__
  39. #include "synctex_version.h"
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* The main synctex object is a scanner.
  44. * Its implementation is considered private.
  45. * The basic workflow is
  46. * - create a "synctex scanner" with the contents of a file
  47. * - perform actions on that scanner like
  48. synctex_display_query or synctex_edit_query below.
  49. * - perform actions on nodes returned by the scanner
  50. * - free the scanner when the work is done
  51. */
  52. typedef struct synctex_scanner_t synctex_scanner_s;
  53. typedef synctex_scanner_s * synctex_scanner_p;
  54. /**
  55. * This is the designated method to create
  56. * a new synctex scanner object.
  57. * - argument output: the pdf/dvi/xdv file associated
  58. * to the synctex file.
  59. * If necessary, it can be the tex file that
  60. * originated the synctex file but this might cause
  61. * problems if the \jobname has a custom value.
  62. * Despite this method can accept a relative path
  63. * in practice, you should only pass full paths.
  64. * The path should be encoded by the underlying
  65. * file system, assuming that it is based on
  66. * 8 bits characters, including UTF8,
  67. * not 16 bits nor 32 bits.
  68. * The last file extension is removed and
  69. * replaced by the proper extension,
  70. * either synctex or synctex.gz.
  71. * - argument build_directory: It is the directory where
  72. * all the auxiliary stuff is created.
  73. * If no synctex file is found in the same directory
  74. * as the output file, then we try to find one in
  75. * this build directory.
  76. * It is the directory where all the auxiliary
  77. * stuff is created. Sometimes, the synctex output
  78. * file and the pdf, dvi or xdv files are not
  79. * created in the same location. See MikTeX.
  80. * This directory path can be NULL,
  81. * it will be ignored then.
  82. * It can be either absolute or relative to the
  83. * directory of the output pdf (dvi or xdv) file.
  84. * Please note that this new argument is provided
  85. * as a convenience but should not be used.
  86. * Available since version 1.5.
  87. * - argument parse: In general, use 1.
  88. * Use 0 only if you do not want to parse the
  89. * content but just check for existence.
  90. * Available since version 1.5
  91. * - resturn: a scanner. NULL is returned in case
  92. * of an error or non existent file.
  93. */
  94. synctex_scanner_p synctex_scanner_new_with_output_file(const char * output, const char * build_directory, int parse);
  95. /**
  96. * Designated method to delete a synctex scanner object,
  97. * including all its internal resources.
  98. * Frees all the memory, you must call it when you are finished with the scanner.
  99. * - argument scanner: a scanner.
  100. * - returns: an integer used for testing purposes.
  101. */
  102. int synctex_scanner_free(synctex_scanner_p scanner);
  103. /**
  104. * Send this message to force the scanner to
  105. * parse the contents of the synctex output file.
  106. * Nothing is performed if the file was already parsed.
  107. * In each query below, this message is sent,
  108. * but if you need to access information more directly,
  109. * you must ensure that the parsing did occur.
  110. * Usage:
  111. * if((my_scanner = synctex_scanner_parse(my_scanner))) {
  112. * continue with my_scanner...
  113. * } else {
  114. * there was a problem
  115. * }
  116. * - returns: the argument on success.
  117. * On failure, frees scanner and returns NULL.
  118. */
  119. synctex_scanner_p synctex_scanner_parse(synctex_scanner_p scanner);
  120. /* synctex_node_p is the type for all synctex nodes.
  121. * Its implementation is considered private.
  122. * The synctex file is parsed into a tree of nodes, either sheet, form, boxes, math nodes... */
  123. typedef struct synctex_node_t synctex_node_s;
  124. typedef synctex_node_s * synctex_node_p;
  125. /* The main entry points.
  126. * Given the file name, a line and a column number, synctex_display_query returns the number of nodes
  127. * satisfying the contrain. Use code like
  128. *
  129. * if(synctex_display_query(scanner,name,line,column,page_hint)>0) {
  130. * synctex_node_p node;
  131. * while((node = synctex_scanner_next_result(scanner))) {
  132. * // do something with node
  133. * ...
  134. * }
  135. * }
  136. *
  137. * Please notice that since version 1.19,
  138. * there is a new argument page_hint.
  139. * The results in pages closer to page_hint are given first.
  140. * For example, one can
  141. * - highlight each resulting node in the output, using synctex_node_visible_h and synctex_node_visible_v
  142. * - highlight all the rectangles enclosing those nodes, using synctex_node_box_visible_... functions
  143. * - highlight just the character using that information
  144. *
  145. * Given the page and the position in the page, synctex_edit_query returns the number of nodes
  146. * satisfying the contrain. Use code like
  147. *
  148. * if(synctex_edit_query(scanner,page,h,v)>0) {
  149. * synctex_node_p node;
  150. * while(node = synctex_scanner_next_result(scanner)) {
  151. * // do something with node
  152. * ...
  153. * }
  154. * }
  155. *
  156. * For example, one can
  157. * - highlight each resulting line in the input,
  158. * - highlight just the character using that information
  159. *
  160. * page is 1 based
  161. * h and v are coordinates in 72 dpi unit, relative to the top left corner of the page.
  162. * If you make a new query, the result of the previous one is discarded. If you need to make more than one query
  163. * in parallel, use the iterator API exposed in
  164. * the synctex_parser_private.h header.
  165. * If one of this function returns a negative integer,
  166. * it means that an error occurred.
  167. *
  168. * Both methods are conservative, in the sense that matching is weak.
  169. * If the exact column number is not found, there will be an answer with the whole line.
  170. *
  171. * Sumatra-PDF, Skim, iTeXMac2, TeXShop and Texworks are examples of open source software that use this library.
  172. * You can browse their code for a concrete implementation.
  173. */
  174. typedef long synctex_status_t;
  175. /* The page_hint argument is used to resolve ambiguities.
  176. * Whenever, different matches occur, the ones closest
  177. * to the page will be given first. Pass a negative number
  178. * when in doubt. Using pdf forms may lead to ambiguities.
  179. */
  180. synctex_status_t synctex_display_query(synctex_scanner_p scanner,const char * name,int line,int column, int page_hint);
  181. synctex_status_t synctex_edit_query(synctex_scanner_p scanner,int page,float h,float v);
  182. synctex_node_p synctex_scanner_next_result(synctex_scanner_p scanner);
  183. synctex_status_t synctex_scanner_reset_result(synctex_scanner_p scanner);
  184. /**
  185. * The horizontal and vertical location,
  186. * the width, height and depth of a box enclosing node.
  187. * All dimensions are given in page coordinates
  188. * as opposite to TeX coordinates.
  189. * The origin is at the top left corner of the page.
  190. * Code example for Qt5:
  191. * (from TeXworks source TWSynchronize.cpp)
  192. * QRectF nodeRect(synctex_node_box_visible_h(node),
  193. * synctex_node_box_visible_v(node) -
  194. * synctex_node_box_visible_height(node),
  195. * synctex_node_box_visible_width(node),
  196. * synctex_node_box_visible_height(node) +
  197. * synctex_node_box_visible_depth(node));
  198. * Code example for Cocoa:
  199. * NSRect bounds = [pdfPage
  200. * boundsForBox:kPDFDisplayBoxMediaBox];
  201. * NSRect nodeRect = NSMakeRect(
  202. * synctex_node_box_visible_h(node),
  203. * NSMaxY(bounds)-synctex_node_box_visible_v(node) +
  204. * synctex_node_box_visible_height(node),
  205. * synctex_node_box_visible_width(node),
  206. * synctex_node_box_visible_height(node) +
  207. * synctex_node_box_visible_depth(node)
  208. * );
  209. * The visible dimensions are bigger than real ones
  210. * to compensate 0 width boxes or nodes intentionnaly
  211. * put outside the box (using \kern for example).
  212. * - parameter node: a node.
  213. * - returns: a float.
  214. * - author: JL
  215. */
  216. float synctex_node_box_visible_h(synctex_node_p node);
  217. float synctex_node_box_visible_v(synctex_node_p node);
  218. float synctex_node_box_visible_width(synctex_node_p node);
  219. float synctex_node_box_visible_height(synctex_node_p node);
  220. float synctex_node_box_visible_depth(synctex_node_p node);
  221. /**
  222. * For quite all nodes, horizontal and vertical coordinates, and width.
  223. * All dimensions are given in page coordinates
  224. * as opposite to TeX coordinates.
  225. * The origin is at the top left corner of the page.
  226. * The visible dimensions are bigger than real ones
  227. * to compensate 0 width boxes or nodes intentionnaly
  228. * put outside the box (using \kern for example).
  229. * All nodes have coordinates, but all nodes don't
  230. * have non null size. For example, math nodes
  231. * have no width according to TeX, and in that case
  232. * synctex_node_visible_width simply returns 0.
  233. * The same holds for kern nodes that do not have
  234. * height nor depth, etc...
  235. */
  236. float synctex_node_visible_h(synctex_node_p node);
  237. float synctex_node_visible_v(synctex_node_p node);
  238. float synctex_node_visible_width(synctex_node_p node);
  239. float synctex_node_visible_height(synctex_node_p node);
  240. float synctex_node_visible_depth(synctex_node_p node);
  241. /**
  242. * Given a node, access to its tag, line and column.
  243. * The line and column numbers are 1 based.
  244. * The latter is not yet fully supported in TeX,
  245. * the default implementation returns 0
  246. * which means the whole line.
  247. * synctex_node_get_name returns the path of the
  248. * TeX source file that was used to create the node.
  249. * When the tag is known, the scanner of the node
  250. * will also give that same file name, see
  251. * synctex_scanner_get_name below.
  252. * For an hbox node, the mean line is the mean
  253. * of all the lines of the child nodes.
  254. * Sometimes, when synchronization form pdf to source
  255. * fails with the line, one should try with the
  256. * mean line.
  257. */
  258. int synctex_node_tag(synctex_node_p node);
  259. int synctex_node_line(synctex_node_p node);
  260. int synctex_node_mean_line(synctex_node_p node);
  261. int synctex_node_column(synctex_node_p node);
  262. const char * synctex_node_get_name(synctex_node_p node);
  263. /**
  264. This is the page where the node appears.
  265. * This is a 1 based index as given by TeX.
  266. */
  267. int synctex_node_page(synctex_node_p node);
  268. /**
  269. * Display all the information contained in the scanner.
  270. * If the records are too numerous, only the first ones are displayed.
  271. * This is mainly for informational purpose to help developers.
  272. */
  273. void synctex_scanner_display(synctex_scanner_p scanner);
  274. /* Managing the input file names.
  275. * Given a tag, synctex_scanner_get_name will return the corresponding file name.
  276. * Conversely, given a file name, synctex_scanner_get_tag will return, the corresponding tag.
  277. * The file name must be the very same as understood by TeX.
  278. * For example, if you \input myDir/foo.tex, the file name is myDir/foo.tex.
  279. * No automatic path expansion is performed.
  280. * Finally, synctex_scanner_input is the first input node of the scanner.
  281. * To browse all the input node, use a loop like
  282. * ...
  283. * synctex_node_p = input_node;
  284. * ...
  285. * if((input_node = synctex_scanner_input(scanner))) {
  286. * do {
  287. * blah
  288. * } while((input_node=synctex_node_sibling(input_node)));
  289. * }
  290. *
  291. * The output is the name that was used to create the scanner.
  292. * The synctex is the real name of the synctex file,
  293. * it was obtained from output by setting the proper file extension.
  294. */
  295. const char * synctex_scanner_get_name(synctex_scanner_p scanner,int tag);
  296. int synctex_scanner_get_tag(synctex_scanner_p scanner,const char * name);
  297. synctex_node_p synctex_scanner_input(synctex_scanner_p scanner);
  298. synctex_node_p synctex_scanner_input_with_tag(synctex_scanner_p scanner,int tag);
  299. const char * synctex_scanner_get_output(synctex_scanner_p scanner);
  300. const char * synctex_scanner_get_synctex(synctex_scanner_p scanner);
  301. /* The x and y offset of the origin in TeX coordinates. The magnification
  302. These are used by pdf viewers that want to display the real box size.
  303. For example, getting the horizontal coordinates of a node would require
  304. synctex_node_box_h(node)*synctex_scanner_magnification(scanner)+synctex_scanner_x_offset(scanner)
  305. Getting its TeX width would simply require
  306. synctex_node_box_width(node)*synctex_scanner_magnification(scanner)
  307. but direct methods are available for that below.
  308. */
  309. int synctex_scanner_x_offset(synctex_scanner_p scanner);
  310. int synctex_scanner_y_offset(synctex_scanner_p scanner);
  311. float synctex_scanner_magnification(synctex_scanner_p scanner);
  312. /**
  313. * ## Browsing the nodes
  314. * parent, child and sibling are standard names for tree nodes.
  315. * The parent is one level higher,
  316. * the child is one level deeper,
  317. * and the sibling is at the same level.
  318. * A node and its sibling have the same parent.
  319. * A node is the parent of its children.
  320. * A node is either the child of its parent,
  321. * or belongs to the sibling chain of its parent's child.
  322. * The sheet or form of a node is the topmost ancestor,
  323. * it is of type sheet or form.
  324. * The next node is either the child, the sibling or the parent's sibling,
  325. * unless the parent is a sheet, a form or NULL.
  326. * This allows to navigate through all the nodes of a given sheet node:
  327. *
  328. * synctex_node_p node = sheet;
  329. * while((node = synctex_node_next(node))) {
  330. * // do something with node
  331. * }
  332. *
  333. * With synctex_sheet_content and synctex_form_content,
  334. * you can retrieve the sheet node given the page
  335. * or form tag.
  336. * The page is 1 based, according to TeX standards.
  337. * Conversely synctex_node_parent_sheet or
  338. * synctex_node_parent_form allows to retrieve
  339. * the sheet or the form containing a given node.
  340. * Notice that a node is not contained in a sheet
  341. * and a form at the same time.
  342. * Some nodes are not contained in either (handles).
  343. */
  344. synctex_node_p synctex_node_parent(synctex_node_p node);
  345. synctex_node_p synctex_node_parent_sheet(synctex_node_p node);
  346. synctex_node_p synctex_node_parent_form(synctex_node_p node);
  347. synctex_node_p synctex_node_child(synctex_node_p node);
  348. synctex_node_p synctex_node_last_child(synctex_node_p node);
  349. synctex_node_p synctex_node_sibling(synctex_node_p node);
  350. synctex_node_p synctex_node_last_sibling(synctex_node_p node);
  351. synctex_node_p synctex_node_arg_sibling(synctex_node_p node);
  352. synctex_node_p synctex_node_next(synctex_node_p node);
  353. /**
  354. * Top level entry points.
  355. * The scanner owns a list of sheet siblings and
  356. * a list of form siblings.
  357. * Sheets or forms have one child which is a box:
  358. * theie contents.
  359. * - argument page: 1 based sheet page number.
  360. * - argument tag: 1 based form tag number.
  361. */
  362. synctex_node_p synctex_sheet(synctex_scanner_p scanner,int page);
  363. synctex_node_p synctex_sheet_content(synctex_scanner_p scanner,int page);
  364. synctex_node_p synctex_form(synctex_scanner_p scanner,int tag);
  365. synctex_node_p synctex_form_content(synctex_scanner_p scanner,int tag);
  366. /* This is primarily used for debugging purpose.
  367. * The second one logs information for the node and recursively displays information for its next node */
  368. void synctex_node_log(synctex_node_p node);
  369. void synctex_node_display(synctex_node_p node);
  370. /* For quite all nodes, horizontal, vertical coordinates, and width.
  371. * These are expressed in TeX small points coordinates, with origin at the top left corner.
  372. */
  373. int synctex_node_h(synctex_node_p node);
  374. int synctex_node_v(synctex_node_p node);
  375. int synctex_node_width(synctex_node_p node);
  376. int synctex_node_height(synctex_node_p node);
  377. int synctex_node_depth(synctex_node_p node);
  378. /* For all nodes, dimensions of the enclosing box.
  379. * These are expressed in TeX small points coordinates, with origin at the top left corner.
  380. * A box is enclosing itself.
  381. */
  382. int synctex_node_box_h(synctex_node_p node);
  383. int synctex_node_box_v(synctex_node_p node);
  384. int synctex_node_box_width(synctex_node_p node);
  385. int synctex_node_box_height(synctex_node_p node);
  386. int synctex_node_box_depth(synctex_node_p node);
  387. #ifdef __cplusplus
  388. }
  389. #endif
  390. #endif