//--- // classes: Node classes used for mathematical notations //--- #include #include #include #include #include #include #include /* 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) { 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); } //--- // 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_layout(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 */ 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_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 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) { 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]. // // 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_layout(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) { 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) { node->width = 9; node->height = 9; node->line = 4; } void sum_render(__attribute__((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) { node->width = 9; node->height = 9; node->line = 4; } void prod_render(__attribute__((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) { 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. // // ------> ] related to TEX_VEC_ARROW_LENGTH // ] TEX_VEC_SPACING // 1 // |_| |_| // TEX_VEC_MARGIN //--- void vec_layout(struct TeX_Node *node) { 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) { int w, h; TeX_size("lim", &w, &h); node->width = w; node->height = h; node->line = h >> 1; } void lim_render(__attribute__((unused)) struct TeX_Node const * node, int x, int y, int color) { TeX_text("lim", x, y, color); } //--- // The class table and lookup functions //--- static struct TeX_Class const class_table[] = { /* Text has ID 0 */ { "\\text", text_layout, text_render }, /* Fractions */ { "frac", frac_layout, frac_render }, /* Size-aware opening and closing elements */ { "left", leftright_layout, leftright_render }, { "right", leftright_layout, leftright_render }, /* Superscripts and subscripts */ { "\\sup", supsubscript_layout, supsubscript_render }, { "\\sub", supsubscript_layout, supsubscript_render }, /* Large operator symbols, integral */ { "sum", sum_layout, sum_render }, { "prod", prod_layout, prod_render }, { "int", int_layout, int_render }, /* Vectors, limits */ { "vec", vec_layout, vec_render }, { "lim", lim_layout, lim_render }, /* 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]; }