From d232315f10e3ba2074e7e39e833d13e437b9f086 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sun, 28 Jun 2020 16:53:02 +0200 Subject: [PATCH] Optimize output with mixed pow/subscript. This fixes: https://github.com/ArthurSonzogni/Diagon/issues/1 --- src/translator/math/Math.cpp | 28 +++++++++++++++++++ test/Math/mixed_pow_subscript/output | 14 +++++----- .../mixed_pow_subscript/output | 14 +++++----- test/Math_style=Latex/braces/output | 8 +++--- test/Math_style=Latex/integral/output | 4 +-- .../mixed_pow_subscript/output | 4 +-- test/Math_style=Latex/pow/output | 12 ++++---- test/Math_style=Latex/speed/output | 2 +- test/Math_style=Latex/string/output | 4 +-- test/Math_style=Latex/subscript/output | 12 ++++---- test/Math_style=Latex/sum/output | 4 +-- test/Math_style=Latex/variable/output | 2 +- 12 files changed, 68 insertions(+), 40 deletions(-) diff --git a/src/translator/math/Math.cpp b/src/translator/math/Math.cpp index 3bd061f..a6ec9cf 100644 --- a/src/translator/math/Math.cpp +++ b/src/translator/math/Math.cpp @@ -150,6 +150,19 @@ Draw ComposeDiagonalDown(const Draw& A, const Draw& B) { return composition; } +// Useful for x_a^b +Draw ComposeDiagonalUpAndDown(const Draw& A, const Draw& B, const Draw& C) { + Draw composition; + composition.Append(B, A.dim_x, 0); + composition.Append(A, 0, B.dim_y); + composition.Append(C, A.dim_x, B.dim_y + A.dim_y); + + composition.center_x = composition.dim_x / 2; + composition.center_y = A.center_y + B.dim_y; + + return composition; +} + Draw WrapWithParenthesis(const Draw& element, Style* style) { Draw draw; draw.Resize(element.dim_x + 2, element.dim_y); @@ -340,6 +353,21 @@ Draw Parse(MathParser::FactorContext* context, bool suppress_parenthesis) { suppress_parenthesis &= (context->valueBang().size() == 1); Draw draw = Parse(context->valueBang(0), style, suppress_parenthesis); + + // Optimization for a_b^c and a^c: + if (context->valueBang().size() == 3) { + if (context->powop(0)->POW() && context->powop(1)->SUBSCRIPT()) { + return ComposeDiagonalUpAndDown( + draw, Parse(context->valueBang(1), style, false), + Parse(context->valueBang(2), style, false)); + } + if (context->powop(1)->POW() && context->powop(0)->SUBSCRIPT()) { + return ComposeDiagonalUpAndDown( + draw, Parse(context->valueBang(2), style, false), + Parse(context->valueBang(1), style, false)); + } + } + for (int i = 1; i < context->valueBang().size(); ++i) { auto* compose = context->powop(i - 1)->POW() ? ComposeDiagonalUp : ComposeDiagonalDown; diff --git a/test/Math/mixed_pow_subscript/output b/test/Math/mixed_pow_subscript/output index 8f0c035..22fe3fc 100644 --- a/test/Math/mixed_pow_subscript/output +++ b/test/Math/mixed_pow_subscript/output @@ -1,7 +1,7 @@ - 3 -A - 2 - - 2 -A - 3 + 3 +A + 2 + + 2 +A + 3 diff --git a/test/Math_style=ASCII/mixed_pow_subscript/output b/test/Math_style=ASCII/mixed_pow_subscript/output index 8f0c035..22fe3fc 100644 --- a/test/Math_style=ASCII/mixed_pow_subscript/output +++ b/test/Math_style=ASCII/mixed_pow_subscript/output @@ -1,7 +1,7 @@ - 3 -A - 2 - - 2 -A - 3 + 3 +A + 2 + + 2 +A + 3 diff --git a/test/Math_style=Latex/braces/output b/test/Math_style=Latex/braces/output index f77b535..0c22523 100644 --- a/test/Math_style=Latex/braces/output +++ b/test/Math_style=Latex/braces/output @@ -1,7 +1,7 @@ -A_\left(1 + 2\right) \\ +A_{\left(1 + 2\right)} \\ \\ -A_1 + 2 \\ +A_{1 + 2} \\ \\ -A^\left(1 + 2\right) \\ +A^{\left(1 + 2\right)} \\ \\ -A^1 + 2 +A^{1 + 2} diff --git a/test/Math_style=Latex/integral/output b/test/Math_style=Latex/integral/output index 123505b..4a106c4 100644 --- a/test/Math_style=Latex/integral/output +++ b/test/Math_style=Latex/integral/output @@ -1,10 +1,10 @@ \int_{b}^{c} a \\ \\ -\int_{x = 1}^{100} x^2 \\ +\int_{x = 1}^{100} x^{2} \\ \\ \int_{x = 1}^{100} \frac{1}{2} \\ \\ -\int_{x = 1}^{100} \frac{x^2}{2} \\ +\int_{x = 1}^{100} \frac{x^{2}}{2} \\ \\ \int_{bbbbbbbbbbbb}^{c} a \\ \\ diff --git a/test/Math_style=Latex/mixed_pow_subscript/output b/test/Math_style=Latex/mixed_pow_subscript/output index 5b0d08e..604c13e 100644 --- a/test/Math_style=Latex/mixed_pow_subscript/output +++ b/test/Math_style=Latex/mixed_pow_subscript/output @@ -1,3 +1,3 @@ -A_2^3 \\ +A_{2}^{3} \\ \\ -A^2_3 +A^{2}_{3} diff --git a/test/Math_style=Latex/pow/output b/test/Math_style=Latex/pow/output index 1aae291..56e12dc 100644 --- a/test/Math_style=Latex/pow/output +++ b/test/Math_style=Latex/pow/output @@ -1,11 +1,11 @@ -A^2 \\ +A^{2} \\ \\ -A^\left(\frac{1}{2}\right)^\sqrt{2} \\ +A^{\left(\frac{1}{2}\right)}^{\sqrt{2}} \\ \\ -\left(A^\left(\frac{1}{2}\right)\right)^\sqrt{2} \\ +\left(A^{\left(\frac{1}{2}\right)}\right)^{\sqrt{2}} \\ \\ -A^\left(\left(\frac{1}{2}\right)^\sqrt{2}\right) \\ +A^{\left(\left(\frac{1}{2}\right)^{\sqrt{2}}\right)} \\ \\ -A^421 + \frac{1}{2} \\ +A^{421} + \frac{1}{2} \\ \\ -\frac{A^421}{2} +\frac{A^{421}}{2} diff --git a/test/Math_style=Latex/speed/output b/test/Math_style=Latex/speed/output index 17fe432..cc4602d 100644 --- a/test/Math_style=Latex/speed/output +++ b/test/Math_style=Latex/speed/output @@ -1 +1 @@ -speed = \sqrt{\left(\frac{df}{dx}\right)^2 + \left(\frac{df}{dy}\right)^2} +speed = \sqrt{\left(\frac{df}{dx}\right)^{2} + \left(\frac{df}{dy}\right)^{2}} diff --git a/test/Math_style=Latex/string/output b/test/Math_style=Latex/string/output index 4d0161e..0022816 100644 --- a/test/Math_style=Latex/string/output +++ b/test/Math_style=Latex/string/output @@ -1,7 +1,7 @@ "hello world" \\ \\ -"A_B"_"C_D" \\ +"A_B"_{"C_D"} \\ \\ "" \\ \\ -1^"2^3"^4 +1^{"2^3"}^{4} diff --git a/test/Math_style=Latex/subscript/output b/test/Math_style=Latex/subscript/output index c4fbdc6..3a5da11 100644 --- a/test/Math_style=Latex/subscript/output +++ b/test/Math_style=Latex/subscript/output @@ -1,11 +1,11 @@ -A_2 \\ +A_{2} \\ \\ -A_\left(\frac{1}{2}\right)_\sqrt{2} \\ +A_{\left(\frac{1}{2}\right)}_{\sqrt{2}} \\ \\ -\left(A_\left(\frac{1}{2}\right)\right)_\sqrt{2} \\ +\left(A_{\left(\frac{1}{2}\right)}\right)_{\sqrt{2}} \\ \\ -A_\left(\left(\frac{1}{2}\right)_\sqrt{2}\right) \\ +A_{\left(\left(\frac{1}{2}\right)_{\sqrt{2}}\right)} \\ \\ -A_421 + \frac{1}{2} \\ +A_{421} + \frac{1}{2} \\ \\ -\frac{A_421}{2} +\frac{A_{421}}{2} diff --git a/test/Math_style=Latex/sum/output b/test/Math_style=Latex/sum/output index 41f5544..bb4f7a1 100644 --- a/test/Math_style=Latex/sum/output +++ b/test/Math_style=Latex/sum/output @@ -1,10 +1,10 @@ \sum_{b}^{c} a \\ \\ -\sum_{x = 1}^{100} x^2 \\ +\sum_{x = 1}^{100} x^{2} \\ \\ \sum_{x = 1}^{100} \frac{1}{2} \\ \\ -\sum_{x = 1}^{100} \frac{x^2}{2} \\ +\sum_{x = 1}^{100} \frac{x^{2}}{2} \\ \\ \sum_{bbbbbbbbbbbb}^{c} a \\ \\ diff --git a/test/Math_style=Latex/variable/output b/test/Math_style=Latex/variable/output index 4475fcd..e2a703a 100644 --- a/test/Math_style=Latex/variable/output +++ b/test/Math_style=Latex/variable/output @@ -4,7 +4,7 @@ f'\left(x\right) \\ \\ a123456789°à@çè|#~#& \\ \\ -2_2m \\ +2_{2m} \\ \\ 2é8fsdmoiuuç \\ \\