forked from Lephenixnoir/TeX
578 lines
14 KiB
C
578 lines
14 KiB
C
//---
|
|
// classes: Node classes used for mathematical notations
|
|
//---
|
|
|
|
#include <TeX/config.h>
|
|
#include <TeX/node.h>
|
|
#include <TeX/flow.h>
|
|
#include <TeX/env.h>
|
|
#include <TeX/classes.h>
|
|
|
|
#include <TeX/interface.h>
|
|
#include <TeX/defs.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
/* Some quick macros to access parameters and their size */
|
|
#define args ((node)->args)
|
|
#define w(n) (args[n]->width)
|
|
#define h(n) (args[n]->height)
|
|
#define l(n) (args[n]->line)
|
|
|
|
//---
|
|
// Text.
|
|
// * args: 0 (can't have any)
|
|
// * flow: normal
|
|
//---
|
|
|
|
void text_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
int w, h;
|
|
TeX_size((char const *)node->text, &w, &h);
|
|
|
|
node->width = w;
|
|
node->height = h;
|
|
node->line = h >> 1;
|
|
}
|
|
|
|
void text_render(struct TeX_Node const * node, int x, int y, int color)
|
|
{
|
|
TeX_text((void *)node->text, x, y, color);
|
|
}
|
|
|
|
//---
|
|
// Environment nodes.
|
|
//---
|
|
|
|
void env_layout(struct TeX_Node *node, int display)
|
|
{
|
|
node->env->layout(node->env, display);
|
|
|
|
node->width = node->env->width;
|
|
node->height = node->env->height;
|
|
node->line = node->env->line;
|
|
}
|
|
|
|
void env_render(struct TeX_Node const * node, int x, int y, int color)
|
|
{
|
|
node->env->render(node->env, x, y, color);
|
|
}
|
|
|
|
//---
|
|
// Fractions.
|
|
// * args: 2 (<2 invalidate, >2 ignore)
|
|
// * flow: normal
|
|
//
|
|
// Graphical parameters:
|
|
//
|
|
// TEX_FRACTION_SPACING:
|
|
// Spacing between bar and operands
|
|
// TEX_FRACTION_BAR_THICKNESS:
|
|
// Thickness (pixels) of fraction bar
|
|
// TEX_FRACTION_MARGIN:
|
|
// Additional bar length that extends beyond the operands
|
|
//
|
|
// 1
|
|
// ] TEX_FRACTION_SPACING
|
|
// ####### ] TEX_FRACTION_BAR_THICKNESS
|
|
// ] TEX_FRACTION_SPACING
|
|
// 2
|
|
// |_| |_|
|
|
// TEX_FRACTION_MARGIN
|
|
//---
|
|
|
|
void frac_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
/* Drop the fraction if there are less than two arguments */
|
|
if(!args[1]) { node->hidden = 1; return; }
|
|
|
|
int margin = TEX_FRACTION_MARGIN;
|
|
int thickness = TEX_FRACTION_BAR_THICKNESS;
|
|
int spacing = TEX_FRACTION_SPACING;
|
|
|
|
node->width = max(w(0), w(1)) + 2 * margin;
|
|
node->height = h(0) + h(1) + 2 * spacing + thickness;
|
|
node->line = h(0) + spacing + thickness / 2;
|
|
}
|
|
|
|
void frac_render(struct TeX_Node const * node, int x, int y, int color)
|
|
{
|
|
/* Fraction bar */
|
|
for(int i = 0; i < TEX_FRACTION_BAR_THICKNESS; i++)
|
|
{
|
|
int y0 = y + h(0) + TEX_FRACTION_SPACING + i;
|
|
TeX_line(x, y0, x + node->width - 1, y0, color);
|
|
}
|
|
|
|
/* First argument */
|
|
TeX_flow_render(args[0],
|
|
x + ((node->width - w(0)) >> 1),
|
|
y,
|
|
color);
|
|
|
|
/* Second argument */
|
|
TeX_flow_render(args[1],
|
|
x + ((node->width - w(1)) >> 1),
|
|
y + h(0) + 2*TEX_FRACTION_SPACING + TEX_FRACTION_BAR_THICKNESS,
|
|
color);
|
|
}
|
|
|
|
//---
|
|
// Left and right delimiters.
|
|
// * args: 1 (<1 invalidate, >1 ignore)
|
|
// * flow: adapt width and height to contents until matching delimiter
|
|
//
|
|
// Left and right delimiters are resizable bracket-like elements used to
|
|
// parenthesize or decorate terms. They serve a special flow purpose and
|
|
// are matched together by the TeX_flow_layout() routine.
|
|
//
|
|
// Graphical parameters:
|
|
//
|
|
// TEX_LEFTRIGHT_ALIGNED:
|
|
// Forces the middle of the decoration to be aligned with the baseline
|
|
// TEX_LEFTRIGHT_SYMMETRICAL:
|
|
// Makes the size of the delimiter symmetrical around the baseline
|
|
// TEX_LEFTRIGHT_MAX_ANGLE:
|
|
// Maximum angle allowed for "<" and ">" delimiters, radians (causes
|
|
// these delimiters to grow in width if their content is tall)
|
|
//
|
|
// Default: Aligned: Aligned symmetrical:
|
|
// / x / x / x
|
|
// | - | - | -
|
|
// < y | y | y
|
|
// | --- < --- < ---
|
|
// \ z \ z | z
|
|
// |
|
|
// \.
|
|
//
|
|
// LaTeX's behavior on this is essentially centered symmetrical, but it
|
|
// seems to resize the contents to make it smoother. Not possible here.
|
|
//---
|
|
|
|
void leftright_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
int width, height;
|
|
TeX_size("#", &width, &height);
|
|
|
|
/* TODO: Not the same on fxcg50 */
|
|
node->width = 3;
|
|
node->height = height;
|
|
node->line = height >> 1;
|
|
|
|
if(node->subtype == '<' || node->subtype == '>')
|
|
node->width = TEX_LEFTRIGHT_ANGLE_WIDTH;
|
|
if(node->subtype == '|')
|
|
node->width = 1;
|
|
}
|
|
|
|
void leftright_render(struct TeX_Node const * node, int x, int y, int color)
|
|
{
|
|
int h = node->height, w = node->width, l = node->line;
|
|
|
|
switch(node->subtype)
|
|
{
|
|
case '(':
|
|
TeX_pixel(x + 2, y, color);
|
|
TeX_pixel(x + 1, y + 1, color);
|
|
TeX_line(x, y + 2, x, y + h - 3, color);
|
|
TeX_pixel(x + 1, y + h - 2, color);
|
|
TeX_pixel(x + 2, y + h - 1, color);
|
|
break;
|
|
|
|
case ')':
|
|
TeX_pixel(x, y, color);
|
|
TeX_pixel(x + 1, y + 1, color);
|
|
TeX_line(x + 2, y + 2, x + 2, y + h - 3, color);
|
|
TeX_pixel(x + 1, y + h - 2, color);
|
|
TeX_pixel(x, y + h - 1, color);
|
|
break;
|
|
|
|
case '[':
|
|
TeX_pixel(x + 1, y, color);
|
|
TeX_pixel(x + 2, y, color);
|
|
TeX_line(x, y, x, y + h - 1, color);
|
|
TeX_pixel(x + 1, y + h - 1, color);
|
|
TeX_pixel(x + 2, y + h - 1, color);
|
|
break;
|
|
|
|
case ']':
|
|
TeX_pixel(x, y, color);
|
|
TeX_pixel(x + 1, y, color);
|
|
TeX_line(x + 2, y, x + 2, y + h - 1, color);
|
|
TeX_pixel(x, y + h - 1, color);
|
|
TeX_pixel(x + 1, y + h - 1, color);
|
|
break;
|
|
|
|
case '{':
|
|
TeX_pixel(x + 2, y, color);
|
|
TeX_line(x + 1, y + 1, x + 1, y + l - 1, color);
|
|
TeX_pixel(x, y + l, color);
|
|
TeX_line(x + 1, y + l + 1, x + 1, y + h - 2, color);
|
|
TeX_pixel(x + 2, y + h - 1, color);
|
|
break;
|
|
|
|
case '}':
|
|
TeX_pixel(x, y, color);
|
|
TeX_line(x + 1, y + 1, x + 1, y + l - 1, color);
|
|
TeX_pixel(x + 2, y + l, color);
|
|
TeX_line(x + 1, y + l + 1, x + 1, y + h - 2, color);
|
|
TeX_pixel(x, y + h - 1, color);
|
|
break;
|
|
|
|
case '<':
|
|
TeX_line(x, y + l, x + w - 1, y, color);
|
|
TeX_line(x, y + l, x + w - 1, y + h - 1, color);
|
|
break;
|
|
|
|
case '>':
|
|
TeX_line(x + w - 1, y + l, x, y, color);
|
|
TeX_line(x + w - 1, y + l, x, y + h - 1, color);
|
|
break;
|
|
|
|
case '|':
|
|
TeX_line(x, y, x, y + h - 1, color);
|
|
break;
|
|
}
|
|
}
|
|
|
|
//---
|
|
// Superscripts and subscripts.
|
|
// * args: 1 (<1 invalidate, >1 ignore)
|
|
// * flow: elevated or lowered to extend the previous node
|
|
//
|
|
// Superscript and subscripts are very common elements. The class itself
|
|
// does nothing but name them and render its argument. The only special
|
|
// thing about them is their behavior in a flow, which depends on the
|
|
// context and is handled by flow functions in [TeX.c].
|
|
//
|
|
// TODO: parameters for display mode (TEX_DISPLAY_SPACING)
|
|
//
|
|
// Graphical parameters:
|
|
//
|
|
// TEX_SUPERSCRIPT_DEPTH:
|
|
// Distance between bottom of superscript and top of base
|
|
// TEX_SUBSCRIPT_ELEVATION:
|
|
// Distance between top of subscript and bottom of base
|
|
// TEX_SUPERSCRIPT_SPACING:
|
|
// Horizontal spacing between superscript and base
|
|
// TEX_SUBSCRIPT_SPACING:
|
|
// Horizontal spacing between subscript and base
|
|
//
|
|
// +-----+
|
|
// | |
|
|
// +------+ | 1 | --,
|
|
// | Base | | | | TEX_SUPERSCRIPT_DEPTH
|
|
// | | +-----+ --'
|
|
// +------+
|
|
// >--< TEX_SUPERSCRIPT_SPACING
|
|
//
|
|
// +------+
|
|
// | | +-----+ --,
|
|
// | Base | | | | TEX_SUBSCRIPT_ELEVATION
|
|
// +------+ | 1 | --'
|
|
// | |
|
|
// +-----+
|
|
// >--< TEX_SUBSCRIPT_SPACING
|
|
//---
|
|
|
|
void supsubscript_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
/* Invalidate node if no argument is provided */
|
|
if(!args[0]) { node->hidden = 1; return; }
|
|
|
|
node->width = w(0);
|
|
node->height = h(0);
|
|
node->line = l(0);
|
|
}
|
|
|
|
void supsubscript_render(struct TeX_Node const * node, int x, int y,
|
|
int color)
|
|
{
|
|
TeX_flow_render(args[0], x, y, color);
|
|
}
|
|
|
|
//---
|
|
// Summation symbol.
|
|
// * args: 0 (ignored)
|
|
// * flow: display mode when available
|
|
//
|
|
// Summation symbols are just nodes with a constant size and function.
|
|
//---
|
|
|
|
void sum_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
node->width = 9;
|
|
node->height = 9;
|
|
node->line = 4;
|
|
}
|
|
|
|
void sum_render(TEX_UNUSED struct TeX_Node const * node, int x, int y,
|
|
int color)
|
|
{
|
|
TeX_line(x, y, x + 8, y, color);
|
|
TeX_line(x, y, x + 4, y + 4, color);
|
|
TeX_line(x, y + 8, x + 4, y + 4, color);
|
|
TeX_line(x, y + 8, x + 8, y + 8, color);
|
|
TeX_pixel(x + 8, y + 1, color);
|
|
TeX_pixel(x + 8, y + 7, color);
|
|
}
|
|
|
|
//---
|
|
// Product symbol.
|
|
// * args: 0 (ignored)
|
|
// * flow: display mode when available
|
|
//---
|
|
|
|
void prod_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
node->width = 9;
|
|
node->height = 9;
|
|
node->line = 4;
|
|
}
|
|
|
|
void prod_render(TEX_UNUSED struct TeX_Node const * node, int x, int y,
|
|
int color)
|
|
{
|
|
TeX_line(x, y, x + 8, y, color);
|
|
TeX_line(x + 1, y, x + 1, y + 8, color);
|
|
TeX_line(x + 0, y + 8, x + 2, y + 8, color);
|
|
TeX_line(x + 7, y, x + 7, y + 8, color);
|
|
TeX_line(x + 6, y + 8, x + 8, y + 8, color);
|
|
}
|
|
|
|
//---
|
|
// Integral symbol
|
|
// * args: 0 (ignored)
|
|
// * flow: default
|
|
//---
|
|
|
|
void int_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
node->width = 5;
|
|
node->height = TEX_INT_HEIGHT;
|
|
node->line = TEX_INT_HEIGHT >> 1;
|
|
}
|
|
|
|
void int_render(struct TeX_Node const * node, int x, int y, int color)
|
|
{
|
|
int h = node->height;
|
|
|
|
TeX_pixel(x + 3, y, color);
|
|
TeX_pixel(x + 4, y + 1, color);
|
|
TeX_line(x + 2, y + 1, x + 2, y + h - 2, color);
|
|
TeX_pixel(x, y + h - 2, color);
|
|
TeX_pixel(x + 1, y + h - 1, color);
|
|
}
|
|
|
|
//---
|
|
// Vectors
|
|
// * args: 1 (<1 invalidate, >1 ignore)
|
|
// * flow: normal
|
|
//
|
|
// This "\vec" command is an equivalent of "\overrightarrow" in LaTeX, it
|
|
// covers its argument and extends.
|
|
//
|
|
// Graphical parameters:
|
|
//
|
|
// TEX_VEC_ARROW_LENGTH:
|
|
// Number of pixels that extend diagonally from the tip of the arrow.
|
|
// TEX_VEC_SPACING:
|
|
// Number of pixels of spacing between argument and vector
|
|
// TEX_VEC_MARGIN:
|
|
// Horizontal margin around the argument.
|
|
//
|
|
// ------> ] 2 * TEX_VEC_ARROW_LENGTH + 1
|
|
// ] TEX_VEC_SPACING
|
|
// 1
|
|
// |_| |_|
|
|
// TEX_VEC_MARGIN
|
|
//---
|
|
|
|
void vec_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
int arrow_height = 2 * TEX_VEC_ARROW_LENGTH + 1;
|
|
|
|
node->width = w(0) + 2 * TEX_VEC_MARGIN;
|
|
node->height = h(0) + TEX_VEC_SPACING + arrow_height;
|
|
node->line = l(0) + TEX_VEC_SPACING + arrow_height;
|
|
}
|
|
|
|
void vec_render(struct TeX_Node const * node, int x, int y, int color)
|
|
{
|
|
int length = TEX_VEC_ARROW_LENGTH;
|
|
int arrow_height = 2 * length + 1;
|
|
int w = node->width;
|
|
|
|
/* Draw arrow */
|
|
TeX_line(x, y + length, x + w - 1, y + length, color);
|
|
TeX_line(x + w - 1, y + length, x + w - 1 - length, y, color);
|
|
TeX_line(x + w - 1, y + length, x + w - 1 - length, y + 2 * length,
|
|
color);
|
|
|
|
/* First argument */
|
|
TeX_flow_render(args[0],
|
|
x + TEX_VEC_MARGIN,
|
|
y + TEX_VEC_SPACING + arrow_height,
|
|
color);
|
|
}
|
|
|
|
//---
|
|
// Limit notation
|
|
// * args: 0 (ignored)
|
|
// * flow: display mode when available
|
|
//---
|
|
|
|
void lim_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
int w, h;
|
|
TeX_size("lim", &w, &h);
|
|
node->width = w;
|
|
node->height = h;
|
|
node->line = h >> 1;
|
|
}
|
|
|
|
void lim_render(TEX_UNUSED struct TeX_Node const * node, int x, int y,
|
|
int color)
|
|
{
|
|
TeX_text("lim", x, y, color);
|
|
}
|
|
|
|
//---
|
|
// Square roots
|
|
// * args: 1 (<1 invalidate, >1 ignore)
|
|
// * flow: normal
|
|
//
|
|
// Graphical parameters:
|
|
//
|
|
// TEX_SQRT_SLANTED:
|
|
// Makes the vertical part of the square root slanted when it is small.
|
|
// TEX_SQRT_SLANT_MAX:
|
|
// Maximum height consider small
|
|
// TEX_SQRT_SLANT_LENGTH:
|
|
// Horizontal size occupied by the slanted part.
|
|
// TEX_SQRT_BAR_LENGTH:
|
|
// Length of the top-right bar marking the end of the argument (or 0)
|
|
// TEX_SQRT_TIP_LENGTH:
|
|
// Length of tip starting at the bottom left of the square root
|
|
// TEX_SQRT_MARGIN:
|
|
// Margin around argument
|
|
//
|
|
// If TEX_SQRT_SLANTED != 0 and height(1) <= TEX_SQRT_SLANT_MAX:
|
|
//
|
|
// TEX_SQRT_TIP_LENGTH
|
|
// | _______ v
|
|
// | / | | TEX_SQRT_BAR_LENGTH
|
|
// | v / 1 ^
|
|
// +-->| \/
|
|
// ^ <->
|
|
// TEX_SQRT_SLANT_LENGTH
|
|
//
|
|
// All other cases:
|
|
//
|
|
// TEX_SQRT_TIP_LENGTH
|
|
// | _______ v
|
|
// | | | | TEX_SQRT_BAR_LENGTH
|
|
// | v | 1 ^
|
|
// +-->| \|
|
|
// ^
|
|
//---
|
|
|
|
void sqrt_layout(struct TeX_Node *node, TEX_UNUSED int display)
|
|
{
|
|
/* Invalidate the node if no argument is present */
|
|
if(!args[0]) { node->hidden = 1; return; }
|
|
|
|
int slanted = TEX_SQRT_SLANTED && h(0) <= TEX_SQRT_SLANT_MAX;
|
|
|
|
node->width = w(0) + 2 * TEX_SQRT_MARGIN + TEX_SQRT_TIP_LENGTH;
|
|
node->width += (TEX_SQRT_BAR_LENGTH > 0);
|
|
node->width += (slanted ? TEX_SQRT_SLANT_LENGTH : 1);
|
|
|
|
node->height = h(0) + TEX_SQRT_MARGIN + 1;
|
|
node->line = l(0) + TEX_SQRT_MARGIN + 1;
|
|
}
|
|
|
|
void sqrt_render(struct TeX_Node const * node, int x, int y, int color)
|
|
{
|
|
int slanted = TEX_SQRT_SLANTED && h(0) <= TEX_SQRT_SLANT_MAX;
|
|
int w = node->width;
|
|
int h = node->height;
|
|
|
|
int tip = TEX_SQRT_TIP_LENGTH;
|
|
TeX_line(x, y + h - tip - 1, x + tip, y + h - 1, color);
|
|
|
|
int bar_length = (slanted ? TEX_SQRT_SLANT_LENGTH : 1);
|
|
TeX_line(x + tip + bar_length - 1, y, x + tip, y + h - 1, color);
|
|
|
|
int base = tip + bar_length;
|
|
TeX_line(x + base, y, x + w - 1, y, color);
|
|
TeX_line(x + w - 1, y, x + w - 1, y + TEX_SQRT_BAR_LENGTH, color);
|
|
|
|
TeX_flow_render(args[0],
|
|
x + base + TEX_SQRT_MARGIN,
|
|
y + 1 + TEX_SQRT_MARGIN,
|
|
color);
|
|
}
|
|
|
|
//---
|
|
// The class table and lookup functions
|
|
//---
|
|
|
|
/* Default sub- and superscript mode for integrals depends on config */
|
|
#if TEX_INT_DISPLAY
|
|
#define INT_MODE TEX_FLOW_PREFER_DISPLAY
|
|
#else
|
|
#define INT_MODE TEX_FLOW_INLINE
|
|
#endif
|
|
|
|
static struct TeX_Class const class_table[] = {
|
|
/* Text and environments */
|
|
{ "\\text", text_layout, text_render, -1 },
|
|
{ "\\end", env_layout, env_render, -1 },
|
|
|
|
/* Fractions */
|
|
{ "frac", frac_layout, frac_render, TEX_FLOW_INLINE },
|
|
|
|
/* Size-aware opening and closing elements */
|
|
{ "left", leftright_layout, leftright_render, TEX_FLOW_INLINE },
|
|
{ "right", leftright_layout, leftright_render, TEX_FLOW_INLINE },
|
|
|
|
/* Superscripts and subscripts */
|
|
{ "\\sup", supsubscript_layout, supsubscript_render, -1 },
|
|
{ "\\sub", supsubscript_layout, supsubscript_render, -1 },
|
|
|
|
/* Large operator symbols, integral */
|
|
{ "sum", sum_layout, sum_render, TEX_FLOW_PREFER_DISPLAY },
|
|
{ "prod", prod_layout, prod_render, TEX_FLOW_PREFER_DISPLAY },
|
|
{ "int", int_layout, int_render, INT_MODE },
|
|
|
|
/* Vectors, limits */
|
|
{ "vec", vec_layout, vec_render, TEX_FLOW_INLINE },
|
|
{ "lim", lim_layout, lim_render, TEX_FLOW_DISPLAY },
|
|
|
|
/* Square root */
|
|
{ "sqrt", sqrt_layout, sqrt_render, TEX_FLOW_INLINE },
|
|
|
|
/* NULL terminator */
|
|
{ NULL },
|
|
};
|
|
|
|
/* TeX_class_find(): Find a class using a command name */
|
|
int TeX_class_find(char const *name)
|
|
{
|
|
/* TODO: Do a binary search instead of a linear one */
|
|
for(int i = 1; class_table[i].name; i++)
|
|
{
|
|
if(!strcmp(class_table[i].name, name)) return i;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
/* TeX_class_of(): Get the class pointer of a node */
|
|
struct TeX_Class const *TeX_class_of(struct TeX_Node const * node)
|
|
{
|
|
return &class_table[node->type];
|
|
}
|