//--- // node: Mathematical constructs in the formula tree //--- #ifndef TEX_NODE #define TEX_NODE #include #include #include #include /* A special class number for nodes that contain plain text. */ #define TEX_NODECLASS_TEXT 0 /* Special class for environment nodes, also a special case */ #define TEX_NODECLASS_ENV 1 struct TeX_Flow; struct TeX_Env; /* TeX_Node This object represents a mathematical construct. It can be plain text, a fraction, a square root or a matrix. It has specific layout and rendering that are supplied by its class. */ struct TeX_Node { /* Pointer to next element in the flow */ struct TeX_Node *next; union { /* Plain text content encoded as UTF-8 */ uint8_t *text; /* Environment pointer */ struct TeX_Env *env; /* Functional arguments */ struct TeX_Flow *args[TEX_NODE_MAX_CHILDREN]; }; /* Node is invalid or hidden */ uint hidden :1; /* Node class identifier */ uint type :7; /* Class variant or fixed argument */ uint subtype :8; /* Size and placement */ uint16_t width; uint16_t height; uint16_t line; /* Location within the flow, as x and baseline displacement */ uint16_t x; int16_t l; } __attribute__((packed, aligned(sizeof (void *)))); //--- // Node creation and destruction functions //--- /* TeX_node_text(): Make a text node */ struct TeX_Node *TeX_node_text(char const *utf8); /* TeX_node_command(): Make a command node without arguments Returns NULL if no such command is defined. */ struct TeX_Node *TeX_node_command(char const *command); /* TeX_node_add_arg(): Add an argument to a command node If [node] is a command node and has at least on free argument slot, then adds [arg] to it. Otherwise, destroys [arg]. Always returns [flow] (useful for nested calls). */ struct TeX_Node *TeX_node_add_arg(struct TeX_Node *node, struct TeX_Flow *arg); /* TeX_node_absorb(): Absorb text into a command node This special function is for command nodes that use the absorption syntax, eg. "\left(" where the argument consists of a single character and is not surrounded by braces. The first character of [text] (which should be only one character if it comes from the parser) is saved as argument. [text] is not freed. Always returns [node]. */ struct TeX_Node *TeX_node_absorb(struct TeX_Node *node, char const *text); /* TeX_node_env(): Make a environment node This function creates an node that wraps an environment and inserts it into a formula. */ struct TeX_Node *TeX_node_env(struct TeX_Env *env); /* TeX_node_free(): Free a TeX_Node and all its children */ void TeX_node_free(struct TeX_Node *node); //--- // Layout and rendering //--- /* TeX_node_layout(): Compute the layout of a node This functions computes the layout of a node and its children, and stores the results in the node's metadata. */ void TeX_node_layout(struct TeX_Node *node); /* TeX_node_render(): Render a node and its children The node's layout must have been computed first. */ void TeX_node_render(struct TeX_Node const * node, int x, int y, int color); #endif /* TEX_NODE */