core: add support for display-style summation symbols
This commit is contained in:
parent
a72c09e100
commit
06291194bc
|
@ -6,7 +6,7 @@ This library is a customizable 2D math rendering tool for calculators. It can be
|
|||
\frac{x_7}{\left\{\frac{\frac{2}{3}}{27}\right\}^2}
|
||||
```
|
||||
|
||||
![Sample on a Graph 35+E II](https://framapic.org/CkFr4ab3FjcZ/bXQg9LyozGqg.jpg)
|
||||
![Sample image](https://framapic.org/v4ODO95ftSAV/szXvX2amwuUN.png)
|
||||
|
||||
## Features
|
||||
|
||||
|
|
4
TODO.md
4
TODO.md
|
@ -1,11 +1,11 @@
|
|||
* Support angle brackets, and honor TEX_LEFTRIGHT_MAX_ANGLE
|
||||
* Write real error messages
|
||||
* Support Unicode symbols (probably requires help from the font side, urgh)
|
||||
* Don't use TEX_LAYOUT_SPACING for everything, just make it the default
|
||||
* Add a parameter to resolve as inline style or display style
|
||||
* Support character-level spacing
|
||||
* Be more generic in what nodes have display mode by default
|
||||
|
||||
* Add sum and products
|
||||
* Add products
|
||||
* Add square roots, and honor TEX_SQRT_SLANTED and TEX_SQRT_BAR_LENGTH
|
||||
* Add vectors
|
||||
* Add limits
|
||||
|
|
|
@ -234,6 +234,34 @@ void supsubscript_render(struct TeX_Node const * node, int x, int y,
|
|||
render_flow(args[0], x, y, color);
|
||||
}
|
||||
|
||||
//---
|
||||
// Summation symbols.
|
||||
// * args: 0 (ignored)
|
||||
// * flow: default
|
||||
//
|
||||
// Summation symbols are just nodes with a constant size and function.
|
||||
// They are automatically placed in display mode except if the user asked
|
||||
// for inline mode.
|
||||
//---
|
||||
|
||||
void sum_size(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);
|
||||
}
|
||||
|
||||
//---
|
||||
// The class table and lookup functions
|
||||
//---
|
||||
|
@ -249,6 +277,8 @@ struct TeX_Class TeX_table[] = {
|
|||
/* Superscripts and subscripts */
|
||||
{ "\\sup", supsubscript_size, supsubscript_render },
|
||||
{ "\\sub", supsubscript_size, supsubscript_render },
|
||||
/* Sum symbol */
|
||||
{ "sum", sum_size, sum_render },
|
||||
/* Integral */
|
||||
{ "int", NULL, NULL },
|
||||
/* NULL terminator */
|
||||
|
|
47
src/flow.c
47
src/flow.c
|
@ -135,8 +135,43 @@ void chunk_compute_inline(struct chunk *chunk)
|
|||
/* chunk_compute_display(): Compute chunk layout for display mode */
|
||||
void chunk_compute_display(struct chunk *chunk)
|
||||
{
|
||||
/* TODO: Display mode for exponents and subscripts */
|
||||
chunk_compute_inline(chunk);
|
||||
struct TeX_Node *base = chunk->base;
|
||||
struct TeX_Node *sup = chunk->sup;
|
||||
struct TeX_Node *sub = chunk->sub;
|
||||
|
||||
int spacing = TEX_LAYOUT_SPACING;
|
||||
|
||||
/* Chunk width and height */
|
||||
chunk->width = base->width;
|
||||
chunk->height = base->height;
|
||||
chunk->line = base->line;
|
||||
|
||||
if(sub)
|
||||
{
|
||||
chunk->width = max(chunk->width, sub->width);
|
||||
chunk->height += sub->height + spacing;
|
||||
}
|
||||
if(sup)
|
||||
{
|
||||
chunk->width = max(chunk->width, sup->width);
|
||||
chunk->height += sup->height + spacing;
|
||||
chunk->line += sup->height + spacing;
|
||||
}
|
||||
|
||||
/* Now that the dimensions are clear, compute the position */
|
||||
|
||||
if(sub)
|
||||
{
|
||||
sub->l += sub->line + (base->height - base->line) + spacing;
|
||||
sub->x = base->x + ((chunk->width - sub->width) >> 1);
|
||||
}
|
||||
if(sup)
|
||||
{
|
||||
sup->l -= (sup->height - sup->line) + base->line + spacing;
|
||||
sup->x = base->x + ((chunk->width - sup->width) >> 1);
|
||||
}
|
||||
|
||||
base->x += (chunk->width - base->width) >> 1;
|
||||
}
|
||||
|
||||
/* chunk_compute_slanted(): Compute chunk layout for slanted mode */
|
||||
|
@ -285,8 +320,14 @@ void size_flow(struct TeX_Flow *flow)
|
|||
/* Leave the last chunk and make a new one */
|
||||
update(&c, &x, &above, &below);
|
||||
|
||||
int mode = TEX_CHUNK_INLINE;
|
||||
if(!strcmp(class, "sum"))
|
||||
{
|
||||
mode = TEX_CHUNK_DISPLAY;
|
||||
}
|
||||
|
||||
node->x = x;
|
||||
chunk_make(&c, node, TEX_CHUNK_INLINE);
|
||||
chunk_make(&c, node, mode);
|
||||
}
|
||||
|
||||
/* Finish the last chunk */
|
||||
|
|
|
@ -161,9 +161,9 @@ static void fini(void)
|
|||
int main(void)
|
||||
{
|
||||
char const * formula =
|
||||
"\\frac{x_7\\left<X,Y\\right>+3\\left<\\frac{A}{B}\\right>}{"
|
||||
"\\frac{x_7}{"//\\left<X,Y\\right>+3\\left<\\frac{A}{B}\\right>}{"
|
||||
"\\left\\{\\frac{\\frac{2}{3}}{27}\\right\\}^2"
|
||||
"}";
|
||||
"} + \\sum_{k=1}^nk";
|
||||
|
||||
struct TeX_Flow *flow = TeX_parse(formula);
|
||||
if(!flow) { puts("parsing error!"); return 1; }
|
||||
|
|
Loading…
Reference in New Issue