File indexing completed on 2024-05-12 04:33:36

0001 /*
0002     SPDX-FileCopyrightText: 2008-2017 jerome DOT laurens AT u-bourgogne DOT fr
0003     SPDX-License-Identifier: X11
0004 
0005     This file is part of the __SyncTeX__ package.
0006 
0007     [//]: # (Latest Revision: Fri Jul 14 16:20:41 UTC 2017)
0008     [//]: # (Version: 1.19)
0009 
0010     See `synctex_parser_readme.md` for more details
0011 
0012     ## Acknowledgments:
0013 
0014     The author received useful remarks from the __pdfTeX__ developers, especially Hahn The Thanh,
0015     and significant help from __XeTeX__ developer Jonathan Kew.
0016 
0017     ## Nota Bene:
0018 
0019     If you include or use a significant part of the __SyncTeX__ package into a software,
0020     I would appreciate to be listed as contributor and see "__SyncTeX__" highlighted.
0021 */
0022 
0023 #ifndef __SYNCTEX_PARSER__
0024 #define __SYNCTEX_PARSER__
0025 
0026 #ifdef __cplusplus
0027 extern "C" {
0028 #endif
0029 
0030 #define SYNCTEX_VERSION_STRING "1.19"
0031 
0032 /*  The main synctex object is a scanner.
0033  *  Its implementation is considered private.
0034  *  The basic workflow is
0035  *  -   create a "synctex scanner" with the contents of a file
0036  *  -   perform actions on that scanner like
0037     synctex_display_query or synctex_edit_query below.
0038  *  -   perform actions on nodes returned by the scanner
0039  *  - free the scanner when the work is done
0040  */
0041 typedef struct synctex_scanner_t synctex_scanner_s;
0042 typedef synctex_scanner_s *synctex_scanner_p;
0043 
0044 /**
0045  *  This is the designated method to create
0046  *  a new synctex scanner object.
0047  *  - argument output: the pdf/dvi/xdv file associated
0048  *      to the synctex file.
0049  *      If necessary, it can be the tex file that
0050  *      originated the synctex file but this might cause
0051  *      problems if the \\jobname has a custom value.
0052  *      Despite this method can accept a relative path
0053  *      in practice, you should only pass full paths.
0054  *      The path should be encoded by the underlying
0055  *      file system, assuming that it is based on
0056  *      8 bits characters, including UTF8,
0057  *      not 16 bits nor 32 bits.
0058  *      The last file extension is removed and
0059  *      replaced by the proper extension,
0060  *      either synctex or synctex.gz.
0061  *  - argument build_directory: It is the directory where
0062  *      all the auxiliary stuff is created.
0063  *      If no synctex file is found in the same directory
0064  *      as the output file, then we try to find one in
0065  *      this build directory.
0066  *      It is the directory where all the auxiliary
0067  *      stuff is created. Sometimes, the synctex output
0068  *      file and the pdf, dvi or xdv files are not
0069  *      created in the same location. See MikTeX.
0070  *      This directory path can be NULL,
0071  *      it will be ignored then.
0072  *      It can be either absolute or relative to the
0073  *      directory of the output pdf (dvi or xdv) file.
0074  *      Please note that this new argument is provided
0075  *      as a convenience but should not be used.
0076  *      Available since version 1.5.
0077  *  - argument parse: In general, use 1.
0078  *      Use 0 only if you do not want to parse the
0079  *      content but just check for existence.
0080  *      Available since version 1.5
0081  *   - resturn: a scanner. NULL is returned in case
0082  *      of an error or non existent file.
0083  */
0084 synctex_scanner_p synctex_scanner_new_with_output_file(const char *output, const char *build_directory, int parse);
0085 
0086 /**
0087  *  Designated method to delete a synctex scanner object,
0088  *  including all its internal resources.
0089  *  Frees all the memory, you must call it when you are finished with the scanner.
0090  *  - argument scanner: a scanner.
0091  *  - returns: an integer used for testing purposes.
0092  */
0093 int synctex_scanner_free(synctex_scanner_p scanner);
0094 
0095 /**
0096  *  Send this message to force the scanner to
0097  *  parse the contents of the synctex output file.
0098  *  Nothing is performed if the file was already parsed.
0099  *  In each query below, this message is sent,
0100  *  but if you need to access information more directly,
0101  *  you must ensure that the parsing did occur.
0102  *  Usage:
0103  *      if((my_scanner = synctex_scanner_parse(my_scanner))) {
0104  *          continue with my_scanner...
0105  *      } else {
0106  *          there was a problem
0107  *      }
0108  *  - returns: the argument on success.
0109  *      On failure, frees scanner and returns NULL.
0110  */
0111 synctex_scanner_p synctex_scanner_parse(synctex_scanner_p scanner);
0112 
0113 /*  synctex_node_p is the type for all synctex nodes.
0114  *  Its implementation is considered private.
0115  *  The synctex file is parsed into a tree of nodes, either sheet, form, boxes, math nodes... */
0116 
0117 typedef struct synctex_node_t synctex_node_s;
0118 typedef synctex_node_s *synctex_node_p;
0119 
0120 /*  The main entry points.
0121  *  Given the file name, a line and a column number, synctex_display_query returns the number of nodes
0122  *  satisfying the contrain. Use code like
0123  *
0124  *      if(synctex_display_query(scanner,name,line,column,page_hint)>0) {
0125  *         synctex_node_p node;
0126  *         while((node = synctex_scanner_next_result(scanner))) {
0127  *             // do something with node
0128  *             ...
0129  *         }
0130  *     }
0131  *
0132  *  Please notice that since version 1.19,
0133  *  there is a new argument page_hint.
0134  *  The results in pages closer to page_hint are given first.
0135  *  For example, one can
0136  * - highlight each resulting node in the output, using synctex_node_visible_h and synctex_node_visible_v
0137  * - highlight all the rectangles enclosing those nodes, using synctex_node_box_visible_... functions
0138  * - highlight just the character using that information
0139  *
0140  *  Given the page and the position in the page, synctex_edit_query returns the number of nodes
0141  *  satisfying the contrain. Use code like
0142  *
0143  *     if(synctex_edit_query(scanner,page,h,v)>0) {
0144  *         synctex_node_p node;
0145  *         while(node = synctex_scanner_next_result(scanner)) {
0146  *             // do something with node
0147  *             ...
0148  *         }
0149  *     }
0150  *
0151  *  For example, one can
0152  * - highlight each resulting line in the input,
0153  * - highlight just the character using that information
0154  *
0155  *  page is 1 based
0156  *  h and v are coordinates in 72 dpi unit, relative to the top left corner of the page.
0157  *  If you make a new query, the result of the previous one is discarded. If you need to make more than one query
0158  *  in parallel, use the iterator API exposed in
0159  *  the synctex_parser_private.h header.
0160  *  If one of this function returns a negative integer,
0161  *  it means that an error occurred.
0162  *
0163  *  Both methods are conservative, in the sense that matching is weak.
0164  *  If the exact column number is not found, there will be an answer with the whole line.
0165  *
0166  *  Sumatra-PDF, Skim, iTeXMac2, TeXShop and Texworks are examples of open source software that use this library.
0167  *  You can browse their code for a concrete implementation.
0168  */
0169 typedef long synctex_status_t;
0170 /*  The page_hint argument is used to resolve ambiguities.
0171  *  Whenever, different matches occur, the ones closest
0172  *  to the page will be given first. Pass a negative number
0173  *  when in doubt. Using pdf forms may lead to ambiguities.
0174  */
0175 synctex_status_t synctex_display_query(synctex_scanner_p scanner, const char *name, int line, int column, int page_hint);
0176 synctex_status_t synctex_edit_query(synctex_scanner_p scanner, int page, float h, float v);
0177 synctex_node_p synctex_scanner_next_result(synctex_scanner_p scanner);
0178 synctex_status_t synctex_scanner_reset_result(synctex_scanner_p scanner);
0179 
0180 /**
0181  *  The horizontal and vertical location,
0182  *  the width, height and depth of a box enclosing node.
0183  *  All dimensions are given in page coordinates
0184  *  as opposite to TeX coordinates.
0185  *  The origin is at the top left corner of the page.
0186  *  Code example for Qt5:
0187  *  (from TeXworks source TWSynchronize.cpp)
0188  *  QRectF nodeRect(synctex_node_box_visible_h(node),
0189  *      synctex_node_box_visible_v(node) -
0190  *          synctex_node_box_visible_height(node),
0191  *      synctex_node_box_visible_width(node),
0192  *      synctex_node_box_visible_height(node) +
0193  *          synctex_node_box_visible_depth(node));
0194  *  Code example for Cocoa:
0195  *  NSRect bounds = [pdfPage
0196  *      boundsForBox:kPDFDisplayBoxMediaBox];
0197  *  NSRect nodeRect = NSMakeRect(
0198  *      synctex_node_box_visible_h(node),
0199  *      NSMaxY(bounds)-synctex_node_box_visible_v(node) +
0200  *          synctex_node_box_visible_height(node),
0201  *      synctex_node_box_visible_width(node),
0202  *      synctex_node_box_visible_height(node) +
0203  *          synctex_node_box_visible_depth(node)
0204  *      );
0205  *  The visible dimensions are bigger than real ones
0206  *  to compensate 0 width boxes or nodes intentionally
0207  *  put outside the box (using \\kern for example).
0208  *  - parameter node: a node.
0209  *  - returns: a float.
0210  *  - author: JL
0211  */
0212 float synctex_node_box_visible_h(synctex_node_p node);
0213 float synctex_node_box_visible_v(synctex_node_p node);
0214 float synctex_node_box_visible_width(synctex_node_p node);
0215 float synctex_node_box_visible_height(synctex_node_p node);
0216 float synctex_node_box_visible_depth(synctex_node_p node);
0217 
0218 /**
0219  *  For quite all nodes, horizontal and vertical coordinates, and width.
0220  *  All dimensions are given in page coordinates
0221  *  as opposite to TeX coordinates.
0222  *  The origin is at the top left corner of the page.
0223  *  The visible dimensions are bigger than real ones
0224  *  to compensate 0 width boxes or nodes intentionally
0225  *  put outside the box (using \\kern for example).
0226  *  All nodes have coordinates, but all nodes don't
0227  *  have non null size. For example, math nodes
0228  *  have no width according to TeX, and in that case
0229  *  synctex_node_visible_width simply returns 0.
0230  *  The same holds for kern nodes that do not have
0231  *  height nor depth, etc...
0232  */
0233 float synctex_node_visible_h(synctex_node_p node);
0234 float synctex_node_visible_v(synctex_node_p node);
0235 float synctex_node_visible_width(synctex_node_p node);
0236 float synctex_node_visible_height(synctex_node_p node);
0237 float synctex_node_visible_depth(synctex_node_p node);
0238 
0239 /**
0240  *  Given a node, access to its tag, line and column.
0241  *  The line and column numbers are 1 based.
0242  *  The latter is not yet fully supported in TeX,
0243  *  the default implementation returns 0
0244  *  which means the whole line.
0245  *  synctex_node_get_name returns the path of the
0246  *  TeX source file that was used to create the node.
0247  *  When the tag is known, the scanner of the node
0248  *  will also give that same file name, see
0249  *  synctex_scanner_get_name below.
0250  */
0251 int synctex_node_tag(synctex_node_p node);
0252 int synctex_node_line(synctex_node_p node);
0253 int synctex_node_column(synctex_node_p node);
0254 const char *synctex_node_get_name(synctex_node_p node);
0255 
0256 /**
0257  This is the page where the node appears.
0258  *  This is a 1 based index as given by TeX.
0259  */
0260 int synctex_node_page(synctex_node_p node);
0261 
0262 /**
0263  *  Display all the information contained in the scanner.
0264  *  If the records are too numerous, only the first ones are displayed.
0265  *  This is mainly for informational purpose to help developers.
0266  */
0267 void synctex_scanner_display(synctex_scanner_p scanner);
0268 
0269 /*  Managing the input file names.
0270  *  Given a tag, synctex_scanner_get_name will return the corresponding file name.
0271  *  Conversely, given a file name, synctex_scanner_get_tag will return, the corresponding tag.
0272  *  The file name must be the very same as understood by TeX.
0273  *  For example, if you \input myDir/foo.tex, the file name is myDir/foo.tex.
0274  *  No automatic path expansion is performed.
0275  *  Finally, synctex_scanner_input is the first input node of the scanner.
0276  *  To browse all the input node, use a loop like
0277  *      ...
0278  *      synctex_node_p = input_node;
0279  *      ...
0280  *      if((input_node = synctex_scanner_input(scanner))) {
0281  *          do {
0282  *              blah
0283  *          } while((input_node=synctex_node_sibling(input_node)));
0284  *     }
0285  *
0286  *  The output is the name that was used to create the scanner.
0287  *  The synctex is the real name of the synctex file,
0288  *  it was obtained from output by setting the proper file extension.
0289  */
0290 const char *synctex_scanner_get_name(synctex_scanner_p scanner, int tag);
0291 
0292 int synctex_scanner_get_tag(synctex_scanner_p scanner, const char *name);
0293 
0294 synctex_node_p synctex_scanner_input(synctex_scanner_p scanner);
0295 synctex_node_p synctex_scanner_input_with_tag(synctex_scanner_p scanner, int tag);
0296 const char *synctex_scanner_get_output(synctex_scanner_p scanner);
0297 const char *synctex_scanner_get_synctex(synctex_scanner_p scanner);
0298 
0299 /*  The x and y offset of the origin in TeX coordinates. The magnification
0300  These are used by pdf viewers that want to display the real box size.
0301  For example, getting the horizontal coordinates of a node would require
0302  synctex_node_box_h(node)*synctex_scanner_magnification(scanner)+synctex_scanner_x_offset(scanner)
0303  Getting its TeX width would simply require
0304  synctex_node_box_width(node)*synctex_scanner_magnification(scanner)
0305  but direct methods are available for that below.
0306  */
0307 int synctex_scanner_x_offset(synctex_scanner_p scanner);
0308 int synctex_scanner_y_offset(synctex_scanner_p scanner);
0309 float synctex_scanner_magnification(synctex_scanner_p scanner);
0310 
0311 /**
0312  *  ## Browsing the nodes
0313  *  parent, child and sibling are standard names for tree nodes.
0314  *  The parent is one level higher,
0315  *  the child is one level deeper,
0316  *  and the sibling is at the same level.
0317  *  A node and its sibling have the same parent.
0318  *  A node is the parent of its children.
0319  *  A node is either the child of its parent,
0320  *  or belongs to the sibling chain of its parent's child.
0321  *  The sheet or form of a node is the topmost ancestor,
0322  *  it is of type sheet or form.
0323  *  The next node is either the child, the sibling or the parent's sibling,
0324  *  unless the parent is a sheet, a form or NULL.
0325  *  This allows to navigate through all the nodes of a given sheet node:
0326  *
0327  *     synctex_node_p node = sheet;
0328  *     while((node = synctex_node_next(node))) {
0329  *         // do something with node
0330  *     }
0331  *
0332  *  With synctex_sheet_content and synctex_form_content,
0333  *  you can retrieve the sheet node given the page
0334  *  or form tag.
0335  *  The page is 1 based, according to TeX standards.
0336  *  Conversely synctex_node_parent_sheet or
0337  *  synctex_node_parent_form allows to retrieve
0338  *  the sheet or the form containing a given node.
0339  *  Notice that a node is not contained in a sheet
0340  *  and a form at the same time.
0341  *  Some nodes are not contained in either (handles).
0342  */
0343 
0344 synctex_node_p synctex_node_parent(synctex_node_p node);
0345 synctex_node_p synctex_node_parent_sheet(synctex_node_p node);
0346 synctex_node_p synctex_node_parent_form(synctex_node_p node);
0347 synctex_node_p synctex_node_child(synctex_node_p node);
0348 synctex_node_p synctex_node_last_child(synctex_node_p node);
0349 synctex_node_p synctex_node_sibling(synctex_node_p node);
0350 synctex_node_p synctex_node_last_sibling(synctex_node_p node);
0351 synctex_node_p synctex_node_arg_sibling(synctex_node_p node);
0352 synctex_node_p synctex_node_next(synctex_node_p node);
0353 
0354 /**
0355  *  Top level entry points.
0356  *  The scanner owns a list of sheet siblings and
0357  *  a list of form siblings.
0358  *  Sheets or forms have one child which is a box:
0359  *  theie contents.
0360  *  - argument page: 1 based sheet page number.
0361  *  - argument tag: 1 based form tag number.
0362  */
0363 synctex_node_p synctex_sheet(synctex_scanner_p scanner, int page);
0364 synctex_node_p synctex_sheet_content(synctex_scanner_p scanner, int page);
0365 synctex_node_p synctex_form(synctex_scanner_p scanner, int tag);
0366 synctex_node_p synctex_form_content(synctex_scanner_p scanner, int tag);
0367 
0368 /*  This is primarily used for debugging purpose.
0369  *  The second one logs information for the node and recursively displays information for its next node */
0370 void synctex_node_log(synctex_node_p node);
0371 void synctex_node_display(synctex_node_p node);
0372 
0373 /*  For quite all nodes, horizontal, vertical coordinates, and width.
0374  *  These are expressed in TeX small points coordinates, with origin at the top left corner.
0375  */
0376 int synctex_node_h(synctex_node_p node);
0377 int synctex_node_v(synctex_node_p node);
0378 int synctex_node_width(synctex_node_p node);
0379 int synctex_node_height(synctex_node_p node);
0380 int synctex_node_depth(synctex_node_p node);
0381 
0382 /*  For all nodes, dimensions of the enclosing box.
0383  *  These are expressed in TeX small points coordinates, with origin at the top left corner.
0384  *  A box is enclosing itself.
0385  */
0386 int synctex_node_box_h(synctex_node_p node);
0387 int synctex_node_box_v(synctex_node_p node);
0388 int synctex_node_box_width(synctex_node_p node);
0389 int synctex_node_box_height(synctex_node_p node);
0390 int synctex_node_box_depth(synctex_node_p node);
0391 
0392 #ifdef __cplusplus
0393 }
0394 #endif
0395 
0396 #endif