fx9860g and fxcg50 2D math rendering library with support for TeX syntax.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

classes.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. //---
  2. // classes: Node classes used for mathematical notations
  3. //---
  4. #include <TeX/config.h>
  5. #include <TeX/node.h>
  6. #include <TeX/flow.h>
  7. #include <TeX/env.h>
  8. #include <TeX/classes.h>
  9. #include <TeX/interface.h>
  10. #include <TeX/defs.h>
  11. #include <string.h>
  12. /* Some quick macros to access parameters and their size */
  13. #define args ((node)->args)
  14. #define w(n) (args[n]->width)
  15. #define h(n) (args[n]->height)
  16. #define l(n) (args[n]->line)
  17. //---
  18. // Text.
  19. // * args: 0 (can't have any)
  20. // * flow: normal
  21. //---
  22. void text_layout(struct TeX_Node *node, TEX_UNUSED int display)
  23. {
  24. int w, h;
  25. TeX_size((char const *)node->text, &w, &h);
  26. node->width = w;
  27. node->height = h;
  28. node->line = h >> 1;
  29. }
  30. void text_render(struct TeX_Node const * node, int x, int y, int color)
  31. {
  32. TeX_text((void *)node->text, x, y, color);
  33. }
  34. //---
  35. // Environment nodes.
  36. //---
  37. void env_layout(struct TeX_Node *node, int display)
  38. {
  39. node->env->layout(node->env, display);
  40. node->width = node->env->width;
  41. node->height = node->env->height;
  42. node->line = node->env->line;
  43. }
  44. void env_render(struct TeX_Node const * node, int x, int y, int color)
  45. {
  46. node->env->render(node->env, x, y, color);
  47. }
  48. //---
  49. // Fractions.
  50. // * args: 2 (<2 invalidate, >2 ignore)
  51. // * flow: normal
  52. //
  53. // Graphical parameters:
  54. //
  55. // TEX_FRACTION_SPACING:
  56. // Spacing between bar and operands
  57. // TEX_FRACTION_BAR_THICKNESS:
  58. // Thickness (pixels) of fraction bar
  59. // TEX_FRACTION_MARGIN:
  60. // Additional bar length that extends beyond the operands
  61. //
  62. // 1
  63. // ] TEX_FRACTION_SPACING
  64. // ####### ] TEX_FRACTION_BAR_THICKNESS
  65. // ] TEX_FRACTION_SPACING
  66. // 2
  67. // |_| |_|
  68. // TEX_FRACTION_MARGIN
  69. //---
  70. void frac_layout(struct TeX_Node *node, TEX_UNUSED int display)
  71. {
  72. /* Drop the fraction if there are less than two arguments */
  73. if(!args[1]) { node->hidden = 1; return; }
  74. int margin = TEX_FRACTION_MARGIN;
  75. int thickness = TEX_FRACTION_BAR_THICKNESS;
  76. int spacing = TEX_FRACTION_SPACING;
  77. node->width = max(w(0), w(1)) + 2 * margin;
  78. node->height = h(0) + h(1) + 2 * spacing + thickness;
  79. node->line = h(0) + spacing + thickness / 2;
  80. }
  81. void frac_render(struct TeX_Node const * node, int x, int y, int color)
  82. {
  83. /* Fraction bar */
  84. for(int i = 0; i < TEX_FRACTION_BAR_THICKNESS; i++)
  85. {
  86. int y0 = y + h(0) + TEX_FRACTION_SPACING + i;
  87. TeX_line(x, y0, x + node->width - 1, y0, color);
  88. }
  89. /* First argument */
  90. TeX_flow_render(args[0],
  91. x + ((node->width - w(0)) >> 1),
  92. y,
  93. color);
  94. /* Second argument */
  95. TeX_flow_render(args[1],
  96. x + ((node->width - w(1)) >> 1),
  97. y + h(0) + 2*TEX_FRACTION_SPACING + TEX_FRACTION_BAR_THICKNESS,
  98. color);
  99. }
  100. //---
  101. // Left and right delimiters.
  102. // * args: 1 (<1 invalidate, >1 ignore)
  103. // * flow: adapt width and height to contents until matching delimiter
  104. //
  105. // Left and right delimiters are resizable bracket-like elements used to
  106. // parenthesize or decorate terms. They serve a special flow purpose and
  107. // are matched together by the TeX_flow_layout() routine.
  108. //
  109. // Graphical parameters:
  110. //
  111. // TEX_LEFTRIGHT_ALIGNED:
  112. // Forces the middle of the decoration to be aligned with the baseline
  113. // TEX_LEFTRIGHT_SYMMETRICAL:
  114. // Makes the size of the delimiter symmetrical around the baseline
  115. // TEX_LEFTRIGHT_ANGLE_WIDTH:
  116. // Width of the "<" and ">" angle delimiters
  117. //
  118. // Default: Aligned: Aligned symmetrical:
  119. // / x / x / x
  120. // | - | - | -
  121. // < y | y | y
  122. // | --- < --- < ---
  123. // \ z \ z | z
  124. // |
  125. // \.
  126. //
  127. // LaTeX's behavior on this is essentially centered symmetrical, but it
  128. // seems to resize the contents to make it smoother. Not possible here.
  129. //---
  130. void leftright_layout(struct TeX_Node *node, TEX_UNUSED int display)
  131. {
  132. int width, height;
  133. TeX_size("#", &width, &height);
  134. /* TODO: Not the same on fxcg50 */
  135. node->width = 3;
  136. node->height = height;
  137. node->line = height >> 1;
  138. if(node->subtype == '<' || node->subtype == '>')
  139. node->width = TEX_LEFTRIGHT_ANGLE_WIDTH;
  140. if(node->subtype == '|')
  141. node->width = 1;
  142. }
  143. void leftright_render(struct TeX_Node const * node, int x, int y, int color)
  144. {
  145. int h = node->height, w = node->width, l = node->line;
  146. switch(node->subtype)
  147. {
  148. case '(':
  149. TeX_pixel(x + 2, y, color);
  150. TeX_pixel(x + 1, y + 1, color);
  151. TeX_line(x, y + 2, x, y + h - 3, color);
  152. TeX_pixel(x + 1, y + h - 2, color);
  153. TeX_pixel(x + 2, y + h - 1, color);
  154. break;
  155. case ')':
  156. TeX_pixel(x, y, color);
  157. TeX_pixel(x + 1, y + 1, color);
  158. TeX_line(x + 2, y + 2, x + 2, y + h - 3, color);
  159. TeX_pixel(x + 1, y + h - 2, color);
  160. TeX_pixel(x, y + h - 1, color);
  161. break;
  162. case '[':
  163. TeX_pixel(x + 1, y, color);
  164. TeX_pixel(x + 2, y, color);
  165. TeX_line(x, y, x, y + h - 1, color);
  166. TeX_pixel(x + 1, y + h - 1, color);
  167. TeX_pixel(x + 2, y + h - 1, color);
  168. break;
  169. case ']':
  170. TeX_pixel(x, y, color);
  171. TeX_pixel(x + 1, y, color);
  172. TeX_line(x + 2, y, x + 2, y + h - 1, color);
  173. TeX_pixel(x, y + h - 1, color);
  174. TeX_pixel(x + 1, y + h - 1, color);
  175. break;
  176. case '{':
  177. TeX_pixel(x + 2, y, color);
  178. TeX_line(x + 1, y + 1, x + 1, y + l - 1, color);
  179. TeX_pixel(x, y + l, color);
  180. TeX_line(x + 1, y + l + 1, x + 1, y + h - 2, color);
  181. TeX_pixel(x + 2, y + h - 1, color);
  182. break;
  183. case '}':
  184. TeX_pixel(x, y, color);
  185. TeX_line(x + 1, y + 1, x + 1, y + l - 1, color);
  186. TeX_pixel(x + 2, y + l, color);
  187. TeX_line(x + 1, y + l + 1, x + 1, y + h - 2, color);
  188. TeX_pixel(x, y + h - 1, color);
  189. break;
  190. case '<':
  191. TeX_line(x, y + l, x + w - 1, y, color);
  192. TeX_line(x, y + l, x + w - 1, y + h - 1, color);
  193. break;
  194. case '>':
  195. TeX_line(x + w - 1, y + l, x, y, color);
  196. TeX_line(x + w - 1, y + l, x, y + h - 1, color);
  197. break;
  198. case '|':
  199. TeX_line(x, y, x, y + h - 1, color);
  200. break;
  201. }
  202. }
  203. //---
  204. // Superscripts and subscripts.
  205. // * args: 1 (<1 invalidate, >1 ignore)
  206. // * flow: elevated or lowered to extend the previous node
  207. //
  208. // Superscript and subscripts are very common elements. The class itself
  209. // does nothing but name them and render its argument. The only special
  210. // thing about them is their behavior in a flow, which depends on the
  211. // context and is handled by flow functions in [TeX.c].
  212. //
  213. // TODO: parameters for display mode (TEX_DISPLAY_SPACING)
  214. //
  215. // Graphical parameters:
  216. //
  217. // TEX_SUPERSCRIPT_DEPTH:
  218. // Distance between bottom of superscript and top of base
  219. // TEX_SUBSCRIPT_ELEVATION:
  220. // Distance between top of subscript and bottom of base
  221. // TEX_SUPERSCRIPT_SPACING:
  222. // Horizontal spacing between superscript and base
  223. // TEX_SUBSCRIPT_SPACING:
  224. // Horizontal spacing between subscript and base
  225. //
  226. // +-----+
  227. // | |
  228. // +------+ | 1 | --,
  229. // | Base | | | | TEX_SUPERSCRIPT_DEPTH
  230. // | | +-----+ --'
  231. // +------+
  232. // >--< TEX_SUPERSCRIPT_SPACING
  233. //
  234. // +------+
  235. // | | +-----+ --,
  236. // | Base | | | | TEX_SUBSCRIPT_ELEVATION
  237. // +------+ | 1 | --'
  238. // | |
  239. // +-----+
  240. // >--< TEX_SUBSCRIPT_SPACING
  241. //---
  242. void supsubscript_layout(struct TeX_Node *node, TEX_UNUSED int display)
  243. {
  244. /* Invalidate node if no argument is provided */
  245. if(!args[0]) { node->hidden = 1; return; }
  246. node->width = w(0);
  247. node->height = h(0);
  248. node->line = l(0);
  249. }
  250. void supsubscript_render(struct TeX_Node const * node, int x, int y,
  251. int color)
  252. {
  253. TeX_flow_render(args[0], x, y, color);
  254. }
  255. //---
  256. // Summation symbol.
  257. // * args: 0 (ignored)
  258. // * flow: display mode when available
  259. //
  260. // Summation symbols are just nodes with a constant size and function.
  261. //---
  262. void sum_layout(struct TeX_Node *node, TEX_UNUSED int display)
  263. {
  264. node->width = 9;
  265. node->height = 9;
  266. node->line = 4;
  267. }
  268. void sum_render(TEX_UNUSED struct TeX_Node const * node, int x, int y,
  269. int color)
  270. {
  271. TeX_line(x, y, x + 8, y, color);
  272. TeX_line(x, y, x + 4, y + 4, color);
  273. TeX_line(x, y + 8, x + 4, y + 4, color);
  274. TeX_line(x, y + 8, x + 8, y + 8, color);
  275. TeX_pixel(x + 8, y + 1, color);
  276. TeX_pixel(x + 8, y + 7, color);
  277. }
  278. //---
  279. // Product symbol.
  280. // * args: 0 (ignored)
  281. // * flow: display mode when available
  282. //---
  283. void prod_layout(struct TeX_Node *node, TEX_UNUSED int display)
  284. {
  285. node->width = 9;
  286. node->height = 9;
  287. node->line = 4;
  288. }
  289. void prod_render(TEX_UNUSED struct TeX_Node const * node, int x, int y,
  290. int color)
  291. {
  292. TeX_line(x, y, x + 8, y, color);
  293. TeX_line(x + 1, y, x + 1, y + 8, color);
  294. TeX_line(x + 0, y + 8, x + 2, y + 8, color);
  295. TeX_line(x + 7, y, x + 7, y + 8, color);
  296. TeX_line(x + 6, y + 8, x + 8, y + 8, color);
  297. }
  298. //---
  299. // Integral symbol
  300. // * args: 0 (ignored)
  301. // * flow: default
  302. //---
  303. void int_layout(struct TeX_Node *node, TEX_UNUSED int display)
  304. {
  305. node->width = 5;
  306. node->height = TEX_INT_HEIGHT;
  307. node->line = TEX_INT_HEIGHT >> 1;
  308. }
  309. void int_render(struct TeX_Node const * node, int x, int y, int color)
  310. {
  311. int h = node->height;
  312. TeX_pixel(x + 3, y, color);
  313. TeX_pixel(x + 4, y + 1, color);
  314. TeX_line(x + 2, y + 1, x + 2, y + h - 2, color);
  315. TeX_pixel(x, y + h - 2, color);
  316. TeX_pixel(x + 1, y + h - 1, color);
  317. }
  318. //---
  319. // Vectors
  320. // * args: 1 (<1 invalidate, >1 ignore)
  321. // * flow: normal
  322. //
  323. // This "\vec" command is an equivalent of "\overrightarrow" in LaTeX, it
  324. // covers its argument and extends.
  325. //
  326. // Graphical parameters:
  327. //
  328. // TEX_VEC_ARROW_LENGTH:
  329. // Number of pixels that extend diagonally from the tip of the arrow.
  330. // TEX_VEC_SPACING:
  331. // Number of pixels of spacing between argument and vector
  332. // TEX_VEC_MARGIN:
  333. // Horizontal margin around the argument.
  334. //
  335. // ------> ] 2 * TEX_VEC_ARROW_LENGTH + 1
  336. // ] TEX_VEC_SPACING
  337. // 1
  338. // |_| |_|
  339. // TEX_VEC_MARGIN
  340. //---
  341. void vec_layout(struct TeX_Node *node, TEX_UNUSED int display)
  342. {
  343. int arrow_height = 2 * TEX_VEC_ARROW_LENGTH + 1;
  344. node->width = w(0) + 2 * TEX_VEC_MARGIN;
  345. node->height = h(0) + TEX_VEC_SPACING + arrow_height;
  346. node->line = l(0) + TEX_VEC_SPACING + arrow_height;
  347. }
  348. void vec_render(struct TeX_Node const * node, int x, int y, int color)
  349. {
  350. int length = TEX_VEC_ARROW_LENGTH;
  351. int arrow_height = 2 * length + 1;
  352. int w = node->width;
  353. /* Draw arrow */
  354. TeX_line(x, y + length, x + w - 1, y + length, color);
  355. TeX_line(x + w - 1, y + length, x + w - 1 - length, y, color);
  356. TeX_line(x + w - 1, y + length, x + w - 1 - length, y + 2 * length,
  357. color);
  358. /* First argument */
  359. TeX_flow_render(args[0],
  360. x + TEX_VEC_MARGIN,
  361. y + TEX_VEC_SPACING + arrow_height,
  362. color);
  363. }
  364. //---
  365. // Limit notation
  366. // * args: 0 (ignored)
  367. // * flow: display mode when available
  368. //---
  369. void lim_layout(struct TeX_Node *node, TEX_UNUSED int display)
  370. {
  371. int w, h;
  372. TeX_size("lim", &w, &h);
  373. node->width = w;
  374. node->height = h;
  375. node->line = h >> 1;
  376. }
  377. void lim_render(TEX_UNUSED struct TeX_Node const * node, int x, int y,
  378. int color)
  379. {
  380. TeX_text("lim", x, y, color);
  381. }
  382. //---
  383. // Square roots
  384. // * args: 1 (<1 invalidate, >1 ignore)
  385. // * flow: normal
  386. //
  387. // Graphical parameters:
  388. //
  389. // TEX_SQRT_SLANTED:
  390. // Makes the vertical part of the square root slanted when it is small.
  391. // TEX_SQRT_SLANT_MAX:
  392. // Maximum height consider small
  393. // TEX_SQRT_SLANT_LENGTH:
  394. // Horizontal size occupied by the slanted part.
  395. // TEX_SQRT_BAR_LENGTH:
  396. // Length of the top-right bar marking the end of the argument (or 0)
  397. // TEX_SQRT_TIP_LENGTH:
  398. // Length of tip starting at the bottom left of the square root
  399. // TEX_SQRT_MARGIN:
  400. // Margin around argument
  401. //
  402. // If TEX_SQRT_SLANTED != 0 and height(1) <= TEX_SQRT_SLANT_MAX:
  403. //
  404. // TEX_SQRT_TIP_LENGTH
  405. // | _______ v
  406. // | / | | TEX_SQRT_BAR_LENGTH
  407. // | v / 1 ^
  408. // +-->| \/
  409. // ^ <->
  410. // TEX_SQRT_SLANT_LENGTH
  411. //
  412. // All other cases:
  413. //
  414. // TEX_SQRT_TIP_LENGTH
  415. // | _______ v
  416. // | | | | TEX_SQRT_BAR_LENGTH
  417. // | v | 1 ^
  418. // +-->| \|
  419. // ^
  420. //---
  421. void sqrt_layout(struct TeX_Node *node, TEX_UNUSED int display)
  422. {
  423. /* Invalidate the node if no argument is present */
  424. if(!args[0]) { node->hidden = 1; return; }
  425. int slanted = TEX_SQRT_SLANTED && h(0) <= TEX_SQRT_SLANT_MAX;
  426. node->width = w(0) + 2 * TEX_SQRT_MARGIN + TEX_SQRT_TIP_LENGTH;
  427. node->width += (TEX_SQRT_BAR_LENGTH > 0);
  428. node->width += (slanted ? TEX_SQRT_SLANT_LENGTH : 1);
  429. node->height = h(0) + TEX_SQRT_MARGIN + 1;
  430. node->line = l(0) + TEX_SQRT_MARGIN + 1;
  431. }
  432. void sqrt_render(struct TeX_Node const * node, int x, int y, int color)
  433. {
  434. int slanted = TEX_SQRT_SLANTED && h(0) <= TEX_SQRT_SLANT_MAX;
  435. int w = node->width;
  436. int h = node->height;
  437. int tip = TEX_SQRT_TIP_LENGTH;
  438. TeX_line(x, y + h - tip - 1, x + tip, y + h - 1, color);
  439. int bar_length = (slanted ? TEX_SQRT_SLANT_LENGTH : 1);
  440. TeX_line(x + tip + bar_length - 1, y, x + tip, y + h - 1, color);
  441. int base = tip + bar_length;
  442. TeX_line(x + base, y, x + w - 1, y, color);
  443. TeX_line(x + w - 1, y, x + w - 1, y + TEX_SQRT_BAR_LENGTH, color);
  444. TeX_flow_render(args[0],
  445. x + base + TEX_SQRT_MARGIN,
  446. y + 1 + TEX_SQRT_MARGIN,
  447. color);
  448. }
  449. //---
  450. // The class table and lookup functions
  451. //---
  452. /* Default sub- and superscript mode for integrals depends on config */
  453. #if TEX_INT_DISPLAY
  454. #define INT_MODE TEX_FLOW_PREFER_DISPLAY
  455. #else
  456. #define INT_MODE TEX_FLOW_INLINE
  457. #endif
  458. static struct TeX_Class const class_table[] = {
  459. /* Text and environments */
  460. { "\\text", text_layout, text_render, -1 },
  461. { "\\end", env_layout, env_render, -1 },
  462. /* Fractions */
  463. { "frac", frac_layout, frac_render, TEX_FLOW_INLINE },
  464. /* Size-aware opening and closing elements */
  465. { "left", leftright_layout, leftright_render, TEX_FLOW_INLINE },
  466. { "right", leftright_layout, leftright_render, TEX_FLOW_INLINE },
  467. /* Superscripts and subscripts */
  468. { "\\sup", supsubscript_layout, supsubscript_render, -1 },
  469. { "\\sub", supsubscript_layout, supsubscript_render, -1 },
  470. /* Large operator symbols, integral */
  471. { "sum", sum_layout, sum_render, TEX_FLOW_PREFER_DISPLAY },
  472. { "prod", prod_layout, prod_render, TEX_FLOW_PREFER_DISPLAY },
  473. { "int", int_layout, int_render, INT_MODE },
  474. /* Vectors, limits */
  475. { "vec", vec_layout, vec_render, TEX_FLOW_INLINE },
  476. { "lim", lim_layout, lim_render, TEX_FLOW_DISPLAY },
  477. /* Square root */
  478. { "sqrt", sqrt_layout, sqrt_render, TEX_FLOW_INLINE },
  479. /* NULL terminator */
  480. { NULL },
  481. };
  482. /* TeX_class_find(): Find a class using a command name */
  483. int TeX_class_find(char const *name)
  484. {
  485. /* TODO: Do a binary search instead of a linear one */
  486. for(int i = 1; class_table[i].name; i++)
  487. {
  488. if(!strcmp(class_table[i].name, name)) return i;
  489. }
  490. return -1;
  491. }
  492. /* TeX_class_of(): Get the class pointer of a node */
  493. struct TeX_Class const *TeX_class_of(struct TeX_Node const * node)
  494. {
  495. return &class_table[node->type];
  496. }