TeX/src/classes.c

256 lines
6.7 KiB
C

//---
// classes: Node classes used for mathematical notations
//---
#include <TeX/TeX.h>
#include <TeX/structure.h>
#include <TeX/render.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)
//---
// Fractions.
// * args: 2 (<2 invalidate, >2 ignore)
// * flow: normal
//
// Graphical parameters:
//
// TEX_LAYOUT_SPACING (inherited):
// Spacing between bar and operands
// TEX_FRACTION_BAR_THICKNESS:
// Thickness (pixels) of fraction bar
// TEX_FRACTION_BAR_MARGIN:
// Additional bar length that extends beyond the operands
//
// 1
// ] TEX_LAYOUT_SPACING
// ####### ] TEX_FRACTION_BAR_THICKNESS
// ] TEX_LAYOUT_SPACING
// 2
// |_| |_|
// TEX_FRACTION_BAR_MARGIN
//---
void frac_size(struct TeX_Node *node)
{
/* Drop the fraction if there are less than two arguments */
if(!args[1]) { node->hidden = 1; return; }
int margin = TEX_FRACTION_BAR_MARGIN;
int thickness = TEX_FRACTION_BAR_THICKNESS;
int spacing = TEX_LAYOUT_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_LAYOUT_SPACING + i;
TeX_line(x, y0, x + node->width - 1, y0, color);
}
/* First argument */
render_flow(args[0],
x + ((node->width - w(0)) >> 1),
y,
color);
/* Second argument */
render_flow(args[1],
x + ((node->width - w(1)) >> 1),
y + h(0) + 2 * TEX_LAYOUT_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 size_flow() 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_size(struct TeX_Node *node)
{
int width, height;
TeX_size("#", &width, &height);
/* TODO: Not the same on fxcg50 */
node->width = 3;
node->height = height;
node->line = (height+1) >> 1;
}
void leftright_render(struct TeX_Node const * node, int x, int y, int color)
{
int h = node->height, 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;
}
}
//---
// 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].
//
// 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
//
// +-----+
// | |
// +------+ 1 | --,
// | Base | | | TEX_SUPERSCRIPT_DEPTH
// | +-----+ --'
// +------+
//
// +------+
// | +-----+ --,
// | Base | | | TEX_SUBSCRIPT_ELEVATION
// +------| 1 | --'
// | |
// +-----+
//---
void supsubscript_size(struct TeX_Node *node)
{
/* 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)
{
render_flow(args[0], x, y, color);
}
//---
// The class table and lookup functions
//---
struct TeX_Class TeX_table[] = {
/* Text is omitted from the table and has ID 0 */
/* Fractions */
{ "frac", frac_size, frac_render },
/* Size-aware opening and closing elements */
{ "left", leftright_size, leftright_render },
{ "right", leftright_size, leftright_render },
/* Superscripts and subscripts */
{ "\\sup", supsubscript_size, supsubscript_render },
{ "\\sub", supsubscript_size, supsubscript_render },
/* Integral */
{ "int", NULL, NULL },
/* NULL terminator */
{ NULL, NULL, NULL },
};
/* class_find() - Find a class using a command name */
int class_find(const char *name)
{
/* TODO: Do a binary search instead of a linear one */
for(int i = 0; TeX_table[i].name; i++)
{
if(!strcmp(TeX_table[i].name, name)) return i + 1;
}
return -1;
}